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>
22 void fail(const char *msg
) {
23 fprintf(stderr
, "%s\n", msg
);
27 void failerr(const char *msg
) {
32 #define TRY(var, foo) var = foo; while (var == -1) { if(errno != EINTR) failerr(#foo); }
34 void *Malloc(size_t count
) { void *r
= malloc(count
); if (!r
) fail("malloc"); return r
; }
37 * read_all: read from the specified file descriptor, returning a
38 * malloc-allocated buffer containing the data that was read; the
39 * number of bytes read is stored in *bytes_read. If max_bytes is
40 * non-negative, it specifies the maximum number of bytes to read.
41 * Otherwise, read_all reads from the file descriptor until the end of
44 char *read_all(int fd
, int max_bytes
, int *bytes_read
) {
48 char *buffer
= Malloc(capacity
);
50 if (max_bytes
< 0 || max_bytes
> 0) {
53 if (count
== capacity
) {
55 buffer
= realloc(buffer
, capacity
);
57 fail("realloc failed");
59 remain
= capacity
- count
;
60 if (max_bytes
> 0 && remain
> max_bytes
)
62 TRY(remain
, read(fd
, buffer
+ count
, remain
));
64 if (remain
== 0 || count
== max_bytes
)
73 * next_term: return the next NUL terminated string from buffer, and
74 * adjust buffer and len accordingly.
76 char *next_term(char **buffer
, int *len
) {
80 while (x
< max_len
&& p
[x
])
83 fail("error parsing");
97 void write_all(int fd
, const char *buf
, int len
) {
100 TRY(result
, write(fd
, buf
, len
));
107 * my_connect: Create a connection to the local Conkeror process on
108 * the specified TCP port. After connecting, the properly formatted
109 * header specifying the client_key and the "role" (file descriptor or
110 * -1 to indicate the control socket) are sent as well. The file
111 * descriptor for the socket is returned.
113 int my_connect(int port
, char *client_key
, int role
) {
116 struct sockaddr_in sa
;
118 TRY(sockfd
, socket(PF_INET
, SOCK_STREAM
, 0));
119 sa
.sin_family
= AF_INET
;
120 sa
.sin_port
= htons(port
);
121 sa
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
122 memset(sa
.sin_zero
, 0, sizeof(sa
.sin_zero
));
124 TRY(result
, connect(sockfd
, (struct sockaddr
*)&sa
, sizeof(sa
)));
126 /* Send the client key */
127 write_all(sockfd
, client_key
, strlen(client_key
));
131 write_all(sockfd
, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 15);
135 snprintf(buf
, 16, "%15d", role
);
136 write_all(sockfd
, buf
, 15);
146 * sigchld_handler: reap any waitable children. Once the child
147 * process exits, send the exit status back over the control socket,
149 void sigchld_handler(int sig
) {
155 pid
= waitpid(-1, &status
, WNOHANG
);
164 /* Our child process exited */
165 if (pid
== child_pid
&& (WIFEXITED(status
) || WIFSIGNALED(status
))) {
167 snprintf(buf
, 30, "%d", status
);
168 write_all(control_fd
, buf
, strlen(buf
) + 1);
174 void check_duplicate_fds(struct fd_info
*fds
, int fd_count
) {
176 for (i
= 0; i
< fd_count
; ++i
) {
177 for (j
= i
+ 1; j
< fd_count
; ++j
) {
178 if (fds
[i
].desired_fd
== fds
[j
].desired_fd
)
179 fail("duplicate redirection requested");
185 * setup_fds: Make the requested redirections. For each entry in the
186 * fds array, rename orig_fd to desired_fd.
188 void setup_fds(struct fd_info
*fds
, int fd_count
) {
190 for (i
= 0; i
< fd_count
; ++i
) {
191 int fd
= fds
[i
].desired_fd
;
192 /* Check if this file descriptor is still in use by any subsequent
194 for (j
= i
+ 1; j
< fd_count
; ++j
) {
195 if (fd
== fds
[j
].orig_fd
) {
196 /* It is in use. Pick a new file descriptor for fds[j]. */
198 TRY(fd_new
, dup(fds
[j
].orig_fd
));
199 close(fds
[j
].orig_fd
);
200 fds
[j
].orig_fd
= fd_new
;
204 TRY(result
, dup2(fds
[i
].orig_fd
, fd
));
205 close(fds
[i
].orig_fd
);
209 int main(int argc
, char **argv
) {
212 char *client_key
, *server_key
, *executable
, *workdir
;
217 sigset_t my_mask
, my_old_mask
;
219 if (argc
!= 3 || (port
= atoi(argv
[2])) == 0)
220 fail("Invalid arguments");
222 sigemptyset(&my_mask
);
223 sigaddset(&my_mask
, SIGCHLD
);
225 /* Block SIGPIPE to avoid a signal being generated while writing to a socket */
226 signal(SIGPIPE
, SIG_IGN
);
228 /* Close everything except STDERR. Mozilla leaves us with a bunch
229 of junk file descriptors. */
231 DIR *dir
= opendir("/proc/self/fd");
233 /* No proc filesystem available, just loop through file descriptors */
234 struct rlimit file_lim
;
235 int max_fileno
= 1024;
236 if (getrlimit(RLIMIT_NOFILE
, &file_lim
) == 0)
237 max_fileno
= file_lim
.rlim_cur
;
238 for (i
= 0; i
< max_fileno
; ++i
) {
239 if (i
== STDERR_FILENO
)
244 struct dirent
*dir_ent
;
245 int dir_fd
= dirfd(dir
);
246 while ((dir_ent
= readdir(dir
)) != NULL
) {
247 int file_desc
= atoi(dir_ent
->d_name
);
248 if (file_desc
== STDERR_FILENO
|| file_desc
== dir_fd
)
261 /* Read the entire file into buf. */
264 TRY(file
, open(argv
[1], O_RDONLY
));
265 buf
= read_all(file
, -1, &len
);
268 /* Remove the temporary file */
271 client_key
= next_term(&buf
, &len
);
272 server_key
= next_term(&buf
, &len
);
273 executable
= next_term(&buf
, &len
);
274 workdir
= next_term(&buf
, &len
);
275 my_argc
= atoi(next_term(&buf
, &len
));
276 my_argv
= Malloc(sizeof(char *) * (my_argc
+ 1));
277 for (i
= 0; i
< my_argc
; ++i
)
278 my_argv
[i
] = next_term(&buf
, &len
);
279 my_argv
[my_argc
] = NULL
;
280 fd_count
= atoi(next_term(&buf
, &len
));
281 if (fd_count
< 0) fail("invalid fd count");
282 fds
= Malloc(sizeof(struct fd_info
) * fd_count
);
283 for (i
= 0; i
< fd_count
; ++i
) {
284 fds
[i
].desired_fd
= atoi(next_term(&buf
, &len
));
285 fds
[i
].path
= next_term(&buf
, &len
);
286 if (fds
[i
].path
[0]) {
287 fds
[i
].open_mode
= atoi(next_term(&buf
, &len
));
288 fds
[i
].perms
= atoi(next_term(&buf
, &len
));
292 fail("invalid input file");
295 /* Validate the file descriptor redirection request. */
296 check_duplicate_fds(fds
, fd_count
);
298 /* Create the control socket connection. */
299 control_fd
= my_connect(port
, client_key
, -1);
301 /* Create a socket connection or open a local file for each
302 requested file descriptor redirection. */
303 for (i
= 0; i
< fd_count
; ++i
) {
304 if (fds
[i
].path
[0]) {
305 TRY(fds
[i
].orig_fd
, open(fds
[i
].path
, fds
[i
].open_mode
, fds
[i
].perms
));
307 fds
[i
].orig_fd
= my_connect(port
, client_key
, fds
[i
].desired_fd
);
311 /* Check server key */
313 int len
= strlen(server_key
);
315 char *buf
= read_all(control_fd
, len
, &read_len
);
316 if (len
!= read_len
|| memcmp(buf
, server_key
, len
) != 0)
317 fail("server key mismatch");
322 sigprocmask(SIG_BLOCK
, &my_mask
, &my_old_mask
);
324 /* Create the child process */
326 if (child_pid
== 0) {
328 /* Unblock SIGCHLD */
329 sigprocmask(SIG_SETMASK
, &my_old_mask
, NULL
);
331 /* Reset the SIGPIPE signal handler. */
332 signal(SIGPIPE
, SIG_DFL
);
334 /* Close the control socket, as it isn't needed from the child. */
337 /* Change to the specified working directory. */
338 if (workdir
[0] != 0) {
339 if (chdir(workdir
) == -1)
343 /* Rearrange file descriptors according to the user specification */
344 setup_fds(fds
, fd_count
);
347 TRY(result
, execv(executable
, my_argv
));
349 } else if (child_pid
== -1) {
352 /* We are in the parent process */
356 /* Install SIGCHLD handler */
358 struct sigaction act
;
359 act
.sa_handler
= sigchld_handler
;
360 sigemptyset(&act
.sa_mask
);
361 act
.sa_flags
= SA_NOCLDSTOP
;
362 sigaction(SIGCHLD
, &act
, NULL
);
364 /* Unblock SIGCHLD */
365 sigprocmask(SIG_SETMASK
, &my_old_mask
, NULL
);
367 /* Close all of the redirection file descriptors, as we don't need
368 them from the parent. */
369 for (i
= 0; i
< fd_count
; ++i
)
370 close(fds
[i
].orig_fd
);
372 /* Wait for a message from the server telling us to exit early. */
373 TRY(count
, read(control_fd
, &msg
, 1));
376 /* End of file received: exit without killing child */
380 /* Assume msg == 0 until we support more messages */
381 TRY(count
, kill(child_pid
, SIGTERM
));