Pass key binding hook functions the current top keymap
[conkeror.git] / spawn-process-helper.c
blobc117bf819fe6c097edfe86c2dbe3383c392dec02
1 /**
2 * (C) Copyright 2008 Jeremy Maitin-Shepard
4 * Use, modification, and distribution are subject to the terms specified in the
5 * COPYING file.
6 **/
8 #include <sys/types.h>
9 #include <sys/socket.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <errno.h>
13 #include <stdlib.h>
14 #include <signal.h>
15 #include <sys/wait.h>
16 #include <sys/stat.h>
17 #include <string.h>
18 #include <fcntl.h>
19 #include <netinet/in.h>
21 void fail(const char *msg) {
22 fprintf(stderr, "%s\n", msg);
23 exit(1);
26 void failerr(const char *msg) {
27 perror(msg);
28 exit(1);
31 #define TRY(var, foo) var = foo; while (var == -1) { if(errno != EINTR) failerr(#foo); }
33 void *Malloc(size_t count) { void *r = malloc(count); if (!r) fail("malloc"); return r; }
35 /**
36 * read_all: read from the specified file descriptor, returning a
37 * malloc-allocated buffer containing the data that was read; the
38 * number of bytes read is stored in *bytes_read. If max_bytes is
39 * non-negative, it specifies the maximum number of bytes to read.
40 * Otherwise, read_all reads from the file descriptor until the end of
41 * file is reached.
43 char *read_all(int fd, int max_bytes, int *bytes_read) {
44 int capacity = 256;
45 if (max_bytes > 0)
46 capacity = max_bytes;
47 char *buffer = Malloc(capacity);
48 int count = 0;
49 if (max_bytes < 0 || max_bytes > 0) {
50 while (1) {
51 int remain;
52 if (count == capacity) {
53 capacity *= 2;
54 buffer = realloc(buffer, capacity);
55 if (!buffer)
56 fail("realloc failed");
58 remain = capacity - count;
59 if (max_bytes > 0 && remain > max_bytes)
60 remain = max_bytes;
61 TRY(remain, read(fd, buffer + count, remain));
62 count += remain;
63 if (remain == 0 || count == max_bytes)
64 break;
67 *bytes_read = count;
68 return buffer;
71 /**
72 * next_term: return the next NUL terminated string from buffer, and
73 * adjust buffer and len accordingly.
75 char *next_term(char **buffer, int *len) {
76 char *p = *buffer;
77 int x = strnlen(p, *len);
78 if (x == *len)
79 fail("error parsing");
80 *buffer += x + 1;
81 *len -= (x + 1);
82 return p;
85 struct fd_info {
86 int desired_fd;
87 int orig_fd;
88 char *path;
89 int open_mode;
90 int perms;
93 void write_all(int fd, const char *buf, int len) {
94 int result;
95 do {
96 TRY(result, write(fd, buf, len));
97 buf += result;
98 len -= result;
99 } while (len > 0);
103 * my_connect: Create a connection to the local Conkeror process on
104 * the specified TCP port. After connecting, the properly formatted
105 * header specifying the client_key and the "role" (file descriptor or
106 * -1 to indicate the control socket) are sent as well. The file
107 * descriptor for the socket is returned.
109 int my_connect(int port, char *client_key, int role) {
110 int sockfd;
111 int result;
112 struct sockaddr_in sa;
114 TRY(sockfd, socket(PF_INET, SOCK_STREAM, 0));
115 sa.sin_family = AF_INET;
116 sa.sin_port = htons(port);
117 sa.sin_addr.s_addr = inet_addr("127.0.0.1");
118 memset(sa.sin_zero, 0, sizeof(sa.sin_zero));
120 TRY(result, connect(sockfd, (struct sockaddr *)&sa, sizeof(sa)));
122 /* Send the client key */
123 write_all(sockfd, client_key, strlen(client_key));
125 /* Send the role */
126 if (role < 0) {
127 write_all(sockfd, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 15);
129 else {
130 char buf[16];
131 snprintf(buf, 16, "%15d", role);
132 write_all(sockfd, buf, 15);
135 return sockfd;
138 int child_pid = 0;
139 int control_fd;
142 * sigchld_handler: reap any waitable children. Once the child
143 * process exits, send the exit status back over the control socket,
144 * then exit. */
145 void sigchld_handler(int sig) {
146 int status;
147 int pid;
148 int err;
150 while (1) {
151 pid = waitpid(-1, &status, WNOHANG);
152 if (pid == 0)
153 return;
154 if (pid == -1) {
155 if (errno == ECHILD)
156 break;
157 failerr("waitpid");
160 /* Our child process exited */
161 if (pid == child_pid && (WIFEXITED(status) || WIFSIGNALED(status))) {
162 char buf[30];
163 snprintf(buf, 30, "%d", status);
164 write_all(control_fd, buf, strlen(buf) + 1);
165 exit(0);
170 void check_duplicate_fds(struct fd_info *fds, int fd_count) {
171 int i, j;
172 for (i = 0; i < fd_count; ++i) {
173 for (j = i + 1; j < fd_count; ++j) {
174 if (fds[i].desired_fd == fds[j].desired_fd)
175 fail("duplicate redirection requested");
181 * setup_fds: Make the requested redirections. For each entry in the
182 * fds array, rename orig_fd to desired_fd.
184 void setup_fds(struct fd_info *fds, int fd_count) {
185 int i, j, result;
186 for (i = 0; i < fd_count; ++i) {
187 int fd = fds[i].desired_fd;
188 /* Check if this file descriptor is still in use by any subsequent
189 redirection. */
190 for (j = i + 1; j < fd_count; ++j) {
191 if (fd == fds[j].orig_fd) {
192 /* It is in use. Pick a new file descriptor for fds[j]. */
193 int fd_new;
194 TRY(fd_new, dup(fds[j].orig_fd));
195 close(fds[j].orig_fd);
196 fds[j].orig_fd = fd_new;
197 break;
200 TRY(result, dup2(fd, fds[i].orig_fd));
201 close(fds[i].orig_fd);
205 int main(int argc, char **argv) {
207 int port;
208 char *client_key, *server_key, *executable, *workdir;
209 char **my_argv;
210 struct fd_info *fds;
211 int fd_count;
212 int i;
213 sigset_t my_mask, my_old_mask;
215 if (argc != 3 || (port = atoi(argv[2])) == 0)
216 fail("Invalid arguments");
218 sigemptyset(&my_mask);
219 sigaddset(&my_mask, SIGCHLD);
221 /* Block SIGPIPE to avoid a signal being generated while writing to a socket */
222 signal(SIGPIPE, SIG_IGN);
224 /* Parse key file */
226 char *buf;
227 int len;
228 int my_argc;
229 /* Read the entire file into buf. */
231 int file;
232 TRY(file, open(argv[1], O_RDONLY));
233 buf = read_all(file, -1, &len);
234 close(file);
236 /* Remove the temporary file */
237 remove(argv[1]);
239 client_key = next_term(&buf, &len);
240 server_key = next_term(&buf, &len);
241 executable = next_term(&buf, &len);
242 workdir = next_term(&buf, &len);
243 my_argc = atoi(next_term(&buf, &len));
244 my_argv = Malloc(sizeof(char *) * (my_argc + 1));
245 for (i = 0; i < my_argc; ++i)
246 my_argv[i] = next_term(&buf, &len);
247 my_argv[my_argc] = NULL;
248 fd_count = atoi(next_term(&buf, &len));
249 if (fd_count < 0) fail("invalid fd count");
250 fds = Malloc(sizeof(struct fd_info) * fd_count);
251 for (i = 0; i < fd_count; ++i) {
252 fds[i].desired_fd = atoi(next_term(&buf, &len));
253 fds[i].path = next_term(&buf, &len);
254 if (fds[i].path) {
255 fds[i].open_mode = atoi(next_term(&buf, &len));
256 fds[i].perms = atoi(next_term(&buf, &len));
259 if (len != 0)
260 fail("invalid input file");
263 /* Validate the file descriptor redirection request. */
264 check_duplicate_fds(fds, fd_count);
266 /* Create the control socket connection. */
267 control_fd = my_connect(port, client_key, -1);
269 /* Create a socket connection or open a local file for each
270 requested file descriptor redirection. */
271 for (i = 0; i < fd_count; ++i) {
272 if (fds[i].path) {
273 TRY(fds[i].orig_fd, open(fds[i].path, fds[i].open_mode, fds[i].perms));
274 } else {
275 fds[i].orig_fd = my_connect(port, client_key, fds[i].desired_fd);
279 /* Check server key */
281 int len = strlen(server_key);
282 int read_len;
283 char *buf = read_all(control_fd, len, &read_len);
284 if (len != read_len || memcmp(buf, server_key, len) != 0)
285 fail("server key mismatch");
286 free(buf);
289 /* Block SIGCHLD */
290 sigprocmask(SIG_BLOCK, &my_mask, &my_old_mask);
292 /* Create the child process */
293 child_pid = fork();
294 if (child_pid == 0) {
295 int result;
296 /* Unblock SIGCHLD */
297 sigprocmask(SIG_SETMASK, &my_old_mask, NULL);
299 /* Reset the SIGPIPE signal handler. */
300 signal(SIGPIPE, SIG_DFL);
302 /* Close the control socket, as it isn't needed from the child. */
303 close(control_fd);
305 /* Change to the specified working directory. */
306 if (workdir[0] != 0) {
307 if (chdir(workdir) == -1)
308 failerr(workdir);
311 /* Rearrange file descriptors according to the user specification */
312 setup_fds(fds, fd_count);
314 /* Exec */
315 TRY(result, execv(executable, my_argv));
317 } else if (child_pid == -1) {
318 failerr("fork");
319 } else {
320 /* We are in the parent process */
321 char msg;
322 int count;
324 /* Install SIGCHLD handler */
326 struct sigaction act;
327 act.sa_handler = sigchld_handler;
328 sigemptyset(&act.sa_mask);
329 act.sa_flags = SA_NOCLDSTOP;
330 sigaction(SIGCHLD, &act, NULL);
332 /* Unblock SIGCHLD */
333 sigprocmask(SIG_SETMASK, &my_old_mask, NULL);
335 /* Close all of the redirection file descriptors, as we don't need
336 them from the parent. */
337 for (i = 0; i < fd_count; ++i)
338 close(fds[i].orig_fd);
340 /* Wait for a message from the server telling us to exit early. */
341 TRY(count, read(control_fd, &msg, 1));
343 if (count == 0) {
344 /* End of file received: exit without killing child */
345 return;
348 /* Assume msg == 0 until we support more messages */
349 TRY(count, kill(child_pid, SIGTERM));
350 return;