2 * (C) Copyright 2008 Jeremy Maitin-Shepard
4 * Use, modification, and distribution are subject to the terms specified in the
9 #include <sys/socket.h>
19 #include <netinet/in.h>
21 #include <sys/resource.h>
23 void fail(const char *msg
) {
24 fprintf(stderr
, "%s\n", msg
);
28 void failerr(const char *msg
) {
33 #define TRY(var, foo) var = foo; while (var == -1) { if(errno != EINTR) failerr(#foo); }
35 void *Malloc(size_t count
) { void *r
= malloc(count
); if (!r
) fail("malloc"); return r
; }
38 * read_all: read from the specified file descriptor, returning a
39 * malloc-allocated buffer containing the data that was read; the
40 * number of bytes read is stored in *bytes_read. If max_bytes is
41 * non-negative, it specifies the maximum number of bytes to read.
42 * Otherwise, read_all reads from the file descriptor until the end of
45 char *read_all(int fd
, int max_bytes
, int *bytes_read
) {
49 char *buffer
= Malloc(capacity
);
51 if (max_bytes
< 0 || max_bytes
> 0) {
54 if (count
== capacity
) {
56 buffer
= realloc(buffer
, capacity
);
58 fail("realloc failed");
60 remain
= capacity
- count
;
61 if (max_bytes
> 0 && remain
> max_bytes
)
63 TRY(remain
, read(fd
, buffer
+ count
, remain
));
65 if (remain
== 0 || count
== max_bytes
)
74 * next_term: return the next NUL terminated string from buffer, and
75 * adjust buffer and len accordingly.
77 char *next_term(char **buffer
, int *len
) {
81 while (x
< max_len
&& p
[x
])
84 fail("error parsing");
98 void write_all(int fd
, const char *buf
, int len
) {
101 TRY(result
, write(fd
, buf
, len
));
108 * my_connect: Create a connection to the local Conkeror process on
109 * the specified TCP port. After connecting, the properly formatted
110 * header specifying the client_key and the "role" (file descriptor or
111 * -1 to indicate the control socket) are sent as well. The file
112 * descriptor for the socket is returned.
114 int my_connect(int port
, char *client_key
, int role
) {
117 struct sockaddr_in sa
;
119 TRY(sockfd
, socket(PF_INET
, SOCK_STREAM
, 0));
120 sa
.sin_family
= AF_INET
;
121 sa
.sin_port
= htons(port
);
122 sa
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
123 memset(sa
.sin_zero
, 0, sizeof(sa
.sin_zero
));
125 TRY(result
, connect(sockfd
, (struct sockaddr
*)&sa
, sizeof(sa
)));
127 /* Send the client key */
128 write_all(sockfd
, client_key
, strlen(client_key
));
132 write_all(sockfd
, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 15);
136 snprintf(buf
, 16, "%15d", role
);
137 write_all(sockfd
, buf
, 15);
147 * sigchld_handler: reap any waitable children. Once the child
148 * process exits, send the exit status back over the control socket,
150 void sigchld_handler(int sig
) {
156 pid
= waitpid(-1, &status
, WNOHANG
);
165 /* Our child process exited */
166 if (pid
== child_pid
&& (WIFEXITED(status
) || WIFSIGNALED(status
))) {
168 snprintf(buf
, 30, "%d", status
);
169 write_all(control_fd
, buf
, strlen(buf
) + 1);
175 void check_duplicate_fds(struct fd_info
*fds
, int fd_count
) {
177 for (i
= 0; i
< fd_count
; ++i
) {
178 for (j
= i
+ 1; j
< fd_count
; ++j
) {
179 if (fds
[i
].desired_fd
== fds
[j
].desired_fd
)
180 fail("duplicate redirection requested");
186 * setup_fds: Make the requested redirections. For each entry in the
187 * fds array, rename orig_fd to desired_fd.
189 void setup_fds(struct fd_info
*fds
, int fd_count
) {
191 for (i
= 0; i
< fd_count
; ++i
) {
192 int fd
= fds
[i
].desired_fd
;
193 if (fd
== fds
[i
].orig_fd
) {
194 /* file descriptor is already correct, nothing needs to be done for it */
197 /* Check if this file descriptor is still in use by any subsequent
199 for (j
= i
+ 1; j
< fd_count
; ++j
) {
200 if (fd
== fds
[j
].orig_fd
) {
201 /* It is in use. Pick a new file descriptor for fds[j]. */
203 TRY(fd_new
, dup(fds
[j
].orig_fd
));
204 close(fds
[j
].orig_fd
);
205 fds
[j
].orig_fd
= fd_new
;
209 TRY(result
, dup2(fds
[i
].orig_fd
, fd
));
210 close(fds
[i
].orig_fd
);
214 int main(int argc
, char **argv
) {
217 char *client_key
, *server_key
, *executable
, *workdir
;
222 sigset_t my_mask
, my_old_mask
;
224 if (argc
!= 3 || (port
= atoi(argv
[2])) == 0)
225 fail("Invalid arguments");
227 sigemptyset(&my_mask
);
228 sigaddset(&my_mask
, SIGCHLD
);
230 /* Block SIGPIPE to avoid a signal being generated while writing to a socket */
231 signal(SIGPIPE
, SIG_IGN
);
233 /* Close everything except STDERR. Mozilla leaves us with a bunch
234 of junk file descriptors. */
236 DIR *dir
= opendir("/proc/self/fd");
238 /* No proc filesystem available, just loop through file descriptors */
239 struct rlimit file_lim
;
240 int max_fileno
= 1024;
241 if (getrlimit(RLIMIT_NOFILE
, &file_lim
) == 0)
242 max_fileno
= file_lim
.rlim_cur
;
243 for (i
= 0; i
< max_fileno
; ++i
) {
244 if (i
== STDERR_FILENO
)
249 struct dirent
*dir_ent
;
250 int dir_fd
= dirfd(dir
);
251 while ((dir_ent
= readdir(dir
)) != NULL
) {
252 int file_desc
= atoi(dir_ent
->d_name
);
253 if (file_desc
== STDERR_FILENO
|| file_desc
== dir_fd
)
261 /* Create a default redirection of STDIN and STDOUT to /dev/null, because some
262 programs except STDIN and STDOUT to always be present. Any user-specified
263 redirections will override these.
266 /* At this point, the only open file descriptor is STDERR (2). Therefore, the
267 next two calls to open are guaranteed to use file descriptors 1 and 2
268 (STDIN and STDOUT, respectively).
270 if (open("/dev/null", O_RDONLY
) != STDIN_FILENO
)
271 fail("Failed to redirect STDIN to /dev/null");
273 if (open("/dev/null", O_RDWR
) != STDOUT_FILENO
)
274 fail("Failed to redirect STDOUT to /dev/null");
281 /* Read the entire file into buf. */
284 TRY(file
, open(argv
[1], O_RDONLY
));
285 buf
= read_all(file
, -1, &len
);
288 /* Remove the temporary file */
291 client_key
= next_term(&buf
, &len
);
292 server_key
= next_term(&buf
, &len
);
293 executable
= next_term(&buf
, &len
);
294 workdir
= next_term(&buf
, &len
);
295 my_argc
= atoi(next_term(&buf
, &len
));
296 my_argv
= Malloc(sizeof(char *) * (my_argc
+ 1));
297 for (i
= 0; i
< my_argc
; ++i
)
298 my_argv
[i
] = next_term(&buf
, &len
);
299 my_argv
[my_argc
] = NULL
;
300 fd_count
= atoi(next_term(&buf
, &len
));
301 if (fd_count
< 0) fail("invalid fd count");
302 fds
= Malloc(sizeof(struct fd_info
) * fd_count
);
303 for (i
= 0; i
< fd_count
; ++i
) {
304 fds
[i
].desired_fd
= atoi(next_term(&buf
, &len
));
305 fds
[i
].path
= next_term(&buf
, &len
);
306 if (fds
[i
].path
[0]) {
307 fds
[i
].open_mode
= atoi(next_term(&buf
, &len
));
308 fds
[i
].perms
= atoi(next_term(&buf
, &len
));
312 fail("invalid input file");
315 /* Validate the file descriptor redirection request. */
316 check_duplicate_fds(fds
, fd_count
);
318 /* Create the control socket connection. */
319 control_fd
= my_connect(port
, client_key
, -1);
321 /* Create a socket connection or open a local file for each
322 requested file descriptor redirection. */
323 for (i
= 0; i
< fd_count
; ++i
) {
324 if (fds
[i
].path
[0]) {
325 TRY(fds
[i
].orig_fd
, open(fds
[i
].path
, fds
[i
].open_mode
, fds
[i
].perms
));
327 fds
[i
].orig_fd
= my_connect(port
, client_key
, fds
[i
].desired_fd
);
331 /* Check server key */
333 int len
= strlen(server_key
);
335 char *buf
= read_all(control_fd
, len
, &read_len
);
336 if (len
!= read_len
|| memcmp(buf
, server_key
, len
) != 0)
337 fail("server key mismatch");
342 sigprocmask(SIG_BLOCK
, &my_mask
, &my_old_mask
);
344 /* Create the child process */
346 if (child_pid
== 0) {
348 /* Unblock SIGCHLD */
349 sigprocmask(SIG_SETMASK
, &my_old_mask
, NULL
);
351 /* Reset the SIGPIPE signal handler. */
352 signal(SIGPIPE
, SIG_DFL
);
354 /* Close the control socket, as it isn't needed from the child. */
357 /* Change to the specified working directory. */
358 if (workdir
[0] != 0) {
359 if (chdir(workdir
) == -1)
363 /* Rearrange file descriptors according to the user specification */
364 setup_fds(fds
, fd_count
);
367 TRY(result
, execv(executable
, my_argv
));
369 } else if (child_pid
== -1) {
372 /* We are in the parent process */
376 /* Install SIGCHLD handler */
378 struct sigaction act
;
379 act
.sa_handler
= sigchld_handler
;
380 sigemptyset(&act
.sa_mask
);
381 act
.sa_flags
= SA_NOCLDSTOP
;
382 sigaction(SIGCHLD
, &act
, NULL
);
384 /* Unblock SIGCHLD */
385 sigprocmask(SIG_SETMASK
, &my_old_mask
, NULL
);
387 /* Close all of the redirection file descriptors, as we don't need
388 them from the parent. */
389 for (i
= 0; i
< fd_count
; ++i
)
390 close(fds
[i
].orig_fd
);
392 /* Wait for a message from the server telling us to exit early. */
393 TRY(count
, read(control_fd
, &msg
, 1));
396 /* End of file received: exit without killing child */
400 /* Assume msg == 0 until we support more messages */
401 TRY(count
, kill(child_pid
, SIGTERM
));