commit 5f348ea

Michael Forney  ·  2018-11-14 08:33:29 +0000 UTC
parent 70e9715
launch: Drop launcher struct
1 files changed,  +33, -41
+33, -41
  1@@ -55,13 +55,12 @@
  2 
  3 #define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array)[0])
  4 
  5-static struct {
  6-	int socket;
  7-	int input_fds[128], num_input_fds;
  8-	int drm_fds[16], num_drm_fds;
  9-	int tty_fd;
 10-	bool active;
 11-} launcher;
 12+static bool nflag;
 13+static int sigfd[2], sock[2];
 14+static int input_fds[128], num_input_fds;
 15+static int drm_fds[16], num_drm_fds;
 16+static int tty_fd;
 17+static bool active;
 18 
 19 static struct {
 20 	bool altered;
 21@@ -70,9 +69,6 @@ static struct {
 22 	long console_mode;
 23 } original_vt_state;
 24 
 25-static bool nflag;
 26-static int sigfd[2];
 27-
 28 static void cleanup(void);
 29 
 30 static noreturn void usage(const char *name)
 31@@ -103,8 +99,8 @@ start_devices(void)
 32 {
 33 	int i;
 34 
 35-	for (i = 0; i < launcher.num_drm_fds; ++i) {
 36-		if (drmSetMaster(launcher.drm_fds[i]) < 0)
 37+	for (i = 0; i < num_drm_fds; ++i) {
 38+		if (drmSetMaster(drm_fds[i]) < 0)
 39 			die("failed to set DRM master");
 40 	}
 41 }
 42@@ -114,16 +110,16 @@ stop_devices(bool fatal)
 43 {
 44 	int i;
 45 
 46-	for (i = 0; i < launcher.num_drm_fds; ++i) {
 47-		if (drmDropMaster(launcher.drm_fds[i]) < 0 && fatal)
 48+	for (i = 0; i < num_drm_fds; ++i) {
 49+		if (drmDropMaster(drm_fds[i]) < 0 && fatal)
 50 			die("drmDropMaster:");
 51 	}
 52-	for (i = 0; i < launcher.num_input_fds; ++i) {
 53-		if (ioctl(launcher.input_fds[i], EVIOCREVOKE, 0) < 0 && errno != ENODEV && fatal)
 54+	for (i = 0; i < num_input_fds; ++i) {
 55+		if (ioctl(input_fds[i], EVIOCREVOKE, 0) < 0 && errno != ENODEV && fatal)
 56 			die("ioctl EVIOCREVOKE:");
 57-		close(launcher.input_fds[i]);
 58+		close(input_fds[i]);
 59 	}
 60-	launcher.num_input_fds = 0;
 61+	num_input_fds = 0;
 62 }
 63 
 64 static void
 65@@ -135,14 +131,14 @@ cleanup(void)
 66 		return;
 67 
 68 	/* Cleanup VT */
 69-	ioctl(launcher.tty_fd, VT_SETMODE, &mode);
 70-	ioctl(launcher.tty_fd, KDSETMODE, original_vt_state.console_mode);
 71-	ioctl(launcher.tty_fd, KDSKBMODE, original_vt_state.kb_mode);
 72+	ioctl(tty_fd, VT_SETMODE, &mode);
 73+	ioctl(tty_fd, KDSETMODE, original_vt_state.console_mode);
 74+	ioctl(tty_fd, KDSKBMODE, original_vt_state.kb_mode);
 75 
 76 	/* Stop devices before switching the VT to make sure we have released the DRM
 77 	 * device before the next session tries to claim it. */
 78 	stop_devices(false);
 79-	ioctl(launcher.tty_fd, VT_ACTIVATE, original_vt_state.vt);
 80+	ioctl(tty_fd, VT_ACTIVATE, original_vt_state.vt);
 81 
 82 	kill(0, SIGTERM);
 83 }
 84@@ -153,8 +149,8 @@ activate(void)
 85 	struct swc_launch_event event = {.type = SWC_LAUNCH_EVENT_ACTIVATE};
 86 
 87 	start_devices();
 88-	send(launcher.socket, &event, sizeof(event), 0);
 89-	launcher.active = true;
 90+	send(sock[0], &event, sizeof(event), 0);
 91+	active = true;
 92 }
 93 
 94 static void
 95@@ -162,9 +158,9 @@ deactivate(void)
 96 {
 97 	struct swc_launch_event event = {.type = SWC_LAUNCH_EVENT_DEACTIVATE};
 98 
 99-	send(launcher.socket, &event, sizeof(event), 0);
100+	send(sock[0], &event, sizeof(event), 0);
101 	stop_devices(true);
102-	launcher.active = false;
103+	active = false;
104 }
105 
106 static void
107@@ -205,15 +201,15 @@ handle_socket_data(int socket)
108 
109 		switch (major(st.st_rdev)) {
110 		case INPUT_MAJOR:
111-			if (!launcher.active)
112+			if (!active)
113 				goto fail;
114-			if (launcher.num_input_fds == ARRAY_LENGTH(launcher.input_fds)) {
115+			if (num_input_fds == ARRAY_LENGTH(input_fds)) {
116 				fprintf(stderr, "too many input devices opened\n");
117 				goto fail;
118 			}
119 			break;
120 		case DRM_MAJOR:
121-			if (launcher.num_drm_fds == ARRAY_LENGTH(launcher.drm_fds)) {
122+			if (num_drm_fds == ARRAY_LENGTH(drm_fds)) {
123 				fprintf(stderr, "too many DRM devices opened\n");
124 				goto fail;
125 			}
126@@ -232,19 +228,19 @@ handle_socket_data(int socket)
127 
128 		switch (major(st.st_rdev)) {
129 		case INPUT_MAJOR:
130-			launcher.input_fds[launcher.num_input_fds++] = fd;
131+			input_fds[num_input_fds++] = fd;
132 			break;
133 		case DRM_MAJOR:
134-			launcher.drm_fds[launcher.num_drm_fds++] = fd;
135+			drm_fds[num_drm_fds++] = fd;
136 			break;
137 		}
138 
139 		break;
140 	case SWC_LAUNCH_REQUEST_ACTIVATE_VT:
141-		if (!launcher.active)
142+		if (!active)
143 			goto fail;
144 
145-		if (ioctl(launcher.tty_fd, VT_ACTIVATE, request->vt) == -1)
146+		if (ioctl(tty_fd, VT_ACTIVATE, request->vt) == -1)
147 			fprintf(stderr, "failed to activate VT %d: %s\n", request->vt, strerror(errno));
148 		break;
149 	default:
150@@ -394,10 +390,10 @@ run(int fd) {
151 				exit(WEXITSTATUS(status));
152 			case SIGUSR1:
153 				deactivate();
154-				ioctl(launcher.tty_fd, VT_RELDISP, 1);
155+				ioctl(tty_fd, VT_RELDISP, 1);
156 				break;
157 			case SIGUSR2:
158-				ioctl(launcher.tty_fd, VT_RELDISP, VT_ACKACQ);
159+				ioctl(tty_fd, VT_RELDISP, VT_ACKACQ);
160 				activate();
161 				break;
162 			}
163@@ -409,7 +405,6 @@ int
164 main(int argc, char *argv[])
165 {
166 	int option;
167-	int sock[2];
168 	char *vt = NULL, buf[64];
169 	struct sigaction action = {
170 		.sa_handler = handle_signal,
171@@ -437,9 +432,6 @@ main(int argc, char *argv[])
172 
173 	if (socketpair(AF_LOCAL, SOCK_SEQPACKET, 0, sock) == -1)
174 		die("socketpair:");
175-
176-	launcher.socket = sock[0];
177-
178 	if (fcntl(sock[0], F_SETFD, FD_CLOEXEC) == -1)
179 		die("failed set CLOEXEC on socket:");
180 
181@@ -464,8 +456,8 @@ main(int argc, char *argv[])
182 	}
183 
184 	fprintf(stderr, "running on %s\n", vt);
185-	launcher.tty_fd = open_tty(vt);
186-	setup_tty(launcher.tty_fd);
187+	tty_fd = open_tty(vt);
188+	setup_tty(tty_fd);
189 
190 	sprintf(buf, "%d", sock[1]);
191 	setenv(SWC_LAUNCH_SOCKET_ENV, buf, 1);