chiark / gitweb /
prefork-interp: watcher plan
[chiark-utils.git] / cprogs / prefork-interp.c
1 /*
2  * "Interpreter" that you can put in #! like this
3  *   #!/usr/bin/prefork-interp [<options>] <interpreter>
4  *
5  * Usages:
6  *   prefork-interp  [<option> ..] <interpreter>  [<script> [<args> ...]]
7  *   prefork-interp  [<option>,..],<interpreter>   <script> [<args> ...]
8  *   prefork-interp '[<option> ..] <interpreter>'  <script> [<args> ...]
9  *
10  * Options must specify argument laundering mode.
11  * Currently the only mode supported is:
12  *   -U    unlaundered: setup and executor both get all arguments and env vars
13  *         ident covers only env vars specified  with -E
14  *         ident covers only arguments interpreter and (if present) script
15  */
16 /*
17  * Process structure:
18  *  client (C wrapper)        connects to server
19  *                              (including reading ack byte)
20  *                            if fails or garbage
21  *                            === acquire lock ===
22  *                               makes new listening socket
23  *                               makes first-instance socketpair
24  *                               makes watcher pipes
25  *                            forks watcher and awaits
26  *                            forks setup (script, sock fds indicated in env)
27  *                            fd0, fd1, fd2: from-outer
28  *                            other fd: call(client-end)(fake)
29  *                            reaps setup (and reports error)
30  *                            (implicitly releases lock)
31  *
32  *     watcher                fd[012]: watcher pipes
33  *                            starts watch on socket path
34  *                            sets stderr to line buffered
35  *                            sets stdin to nonblocking
36  *                            daemonises
37  *                            when socket stat changes, quit
38  *
39  *     setup (pre-exec)       fd0: null,
40  *                            fd[12]: fd2-from-outer
41  *                            env fds: listener, call(server-end)(fake),
42  *                                      watcher read, watcher write
43  *                            close fd: lockfile
44  *                            possibly clean env, argv
45  *
46  *     setup (script)         runs initialisation parts of the script
47  *                            at prefork establishment point:
48  *     setup (pm) [1]         opens syslog
49  *                            forks for server
50  *                [2]         exits
51  *
52  *        server (pm) [1]     [fd0: null],
53  *                            [fd[12]: fd2-from-outer]
54  *                            right away, forks init monitor
55  *                    [2]     closes outer caller fds and call(fake)
56  *        [server (pm)]       fd[012]: null
57  *                            other fds: listener, syslog
58  *                            runs in loop accepting and forking,
59  *                            reaping and limiting children (incl init monitor)
60  *                            reports failures of monitors to syslog
61  *                            
62  *  [client (C wrapper)]      if client connect succeeds:
63  *                            now fd: call(client-end)
64  *                               sends message with: cmdline, env
65  *                               sends fds
66  *
67  *        [server (script)]   accepts, forks subseq monitor
68  *
69  *          monitor [1]       [fd0: null]
70  *           (init            [fd[12]: init: fd2-from-outer; subseq: null]
71  *             or             errors: init: fd2; subseq: syslog
72  *            subseq)         other fds: syslog, call(server-end)
73  *                            sends ack byte
74  *                            receives args, env, fds
75  *                            forks executor
76  *
77  *            executor        sorts out fds:
78  *                            fd0, fd1, fd2: from-outer
79  *                            close fds: call(server-end)
80  *                            retained fds: syslog
81  *
82  *                            sets cmdline, env
83  *                            runs main part of script
84  *                            exits normally
85  *
86  *          [monitor]         [fd[012]: null]
87  *                            [fd[12]: init: fd2-from-outer; subseq: null]
88  *                            [errors: init: fd2; subseq: syslog]
89  *                            reaps executor
90  *                            reports status via socket
91  *
92  *    [client (C wrapper)]    [fd0, fd1, fd2: from-outer]
93  *                            [other fd: call(client-end)]
94  *                            receives status, exits appropriately
95  *                            (if was bad signal, reports to stderr, exits 127)
96  */
97
98 #include <arpa/inet.h>
99
100 #include "prefork.h"
101
102 const char our_name[] = "prefork-interp";
103
104 static struct sockaddr_un sockaddr_sun;
105 static FILE *call_sock;
106
107 #define ACK_BYTE '\n'
108
109 static const char *const *executor_argv;
110
111 static const char header_magic[4] = "PFI\n";
112
113 void fusagemessage(FILE *f) {
114   fprintf(f, "usage: #!/usr/bin/prefork-interp [<options>]\n");
115 }
116
117 static int laundering;
118
119 const struct cmdinfo cmdinfos[]= {
120   PREFORK_CMDINFOS
121   { 0, 'U',   0,                    .iassignto= &laundering,    .arg= 'U' },
122   { 0 }
123 };
124
125 void ident_addinit(void) {
126   char ident_magic[1] = { 0 };
127   sha256_update(&identsc, sizeof(ident_magic), ident_magic);
128 }
129
130 static void propagate_exit_status(int status, const char *what) {
131   int r;
132
133   if (WIFEXITED(status)) {
134     _exit(status);
135   }
136
137   if (WIFSIGNALED(status)) {
138     int sig = WTERMSIG(status);
139     const char *signame = strsignal(sig);
140     if (signame == 0) signame = "unknown signal";
141
142     if (! WCOREDUMP(status) &&
143         (sig == SIGINT ||
144          sig == SIGHUP ||
145          sig == SIGPIPE ||
146          sig == SIGKILL)) {
147       struct sigaction sa;
148       FILLZERO(sa);
149       sa.sa_handler = SIG_DFL;
150       r = sigaction(sig, &sa, 0);
151       if (r) diee("failed to reset signal handler while propagating %s",
152                   signame);
153       
154       sigset_t sset;
155       sigemptyset(&sset);
156       sigaddset(&sset, sig);
157       r = sigprocmask(SIG_UNBLOCK, &sset, 0);
158       if (r) diee("failed to reset signal block while propagating %s",
159                   signame);
160
161       raise(sig);
162       die("unexpectedly kept running after raising (to propagate) %s",
163           signame);
164     }
165
166     die("%s failed due to signal %d %s%s", what, sig, signame,
167         WCOREDUMP(status) ? " (core dumped)" : "");
168   }
169
170   die("%s failed with weird wait status %d 0x%x", what, status, status);
171 }
172
173 static __attribute((noreturn)) void die_data_overflow(void) {
174   die("cannot handle data with length >2^32");
175 }
176
177 static void prepare_data(size_t *len, char **buf,
178                          const void *data, size_t dl) {
179   if (len) {
180     if (dl >= SIZE_MAX - *len)
181       die_data_overflow();
182     *len += dl;
183   }
184   if (buf) {
185     memcpy(*buf, data, dl);
186     *buf += dl;
187   }
188 }
189   
190 static void prepare_length(size_t *len, char **buf, size_t dl_sz) {
191   if (dl_sz > UINT32_MAX) die_data_overflow();
192   uint32_t dl = htonl(dl_sz);
193   prepare_data(len, buf, &dl, sizeof(dl));
194 }
195
196 static void prepare_string(size_t *len, char **buf, const char *s) {
197   size_t sl = strlen(s);
198   prepare_data(len, buf, s, sl+1);
199 }
200
201 static void prepare_message(size_t *len, char **buf) {
202   const char *s;
203
204   const char *const *p = (void*)environ;
205   while ((s = *p++)) {
206     if (strchr(s, '='))
207       prepare_string(len, buf, s);
208   }
209
210   prepare_string(len, buf, "");
211
212   p = executor_argv;
213   while ((s = *p++))
214     prepare_string(len, buf, s);
215 }
216
217 static void send_fd(int payload_fd) {
218   int via_fd = fileno(call_sock);
219
220   union {
221     struct cmsghdr align;
222     char buf[CMSG_SPACE(sizeof(payload_fd))];
223   } cmsg_buf;
224
225   struct msghdr msg;
226   FILLZERO(msg);
227   FILLZERO(cmsg_buf);
228
229   char dummy_byte = 0;
230   struct iovec iov;
231   FILLZERO(iov);
232   iov.iov_base = &dummy_byte;
233   iov.iov_len = 1;
234
235   msg.msg_name = 0;
236   msg.msg_iov = &iov;
237   msg.msg_iovlen = 1;
238   msg.msg_control = cmsg_buf.buf;
239   msg.msg_controllen = sizeof(cmsg_buf.buf);
240
241   struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
242   cmsg->cmsg_level = SOL_SOCKET;
243   cmsg->cmsg_type = SCM_RIGHTS;
244   cmsg->cmsg_len = CMSG_LEN(sizeof(payload_fd));
245   *(int*)CMSG_DATA(cmsg) = payload_fd;
246
247   msg.msg_controllen = sizeof(cmsg_buf.buf);
248
249   for (;;) {
250     ssize_t r = sendmsg(via_fd, &msg, 0);
251     if (r == -1) {
252       if (errno == EINTR) continue;
253       diee("send fd");
254     }
255     assert(r == 1);
256     break;
257   }
258 }
259
260 static void send_request(void) {
261   // Sending these first makes it easier for the script to
262   // use buffered IO for the message.
263   send_fd(0);
264   send_fd(1);
265   send_fd(2);
266
267   size_t len = 0;
268   prepare_message(&len, 0);
269
270   char *m = xmalloc(len + 4);
271   char *p = m;
272   prepare_length(0, &p, len);
273   prepare_message(0, &p);
274   assert(p == m + len + 4);
275
276   ssize_t sr = fwrite(p, len, 1, call_sock);
277   if (sr != 1) diee("write request (buffer)");
278
279   if (fflush(call_sock)) diee("write request");
280 }
281
282 static FILE *call_sock_from_fd(int fd) {
283   int r;
284
285   FILE *call_sock = fdopen(fd, "r+");
286   if (!call_sock) diee("fdopen socket");
287
288   r = setvbuf(call_sock, 0, _IONBF, 0);
289   if (r) die("setvbuf socket");
290
291   return call_sock;
292 }
293
294 static bool was_eof(FILE *call_sock) {
295   return feof(call_sock) || errno==ECONNRESET;
296 }
297
298 // Returns -1 on EOF
299 static int protocol_read_maybe(void *data, size_t sz) {
300   if (!sz) return 0;
301   size_t sr = fread(data, sz, 1, call_sock);
302   if (sr != 1) {
303     if (was_eof(call_sock)) return -1;
304     diee("read() on monitor call socket (%zd)", sz);
305   }
306   return 0;
307 }
308
309 static void protocol_read(void *data, size_t sz) {
310   if (protocol_read_maybe(data, sz) < 0)
311     die("monitor process quit unexpectedly");
312 }
313
314 // Returns 0 if OK, error msg if peer was garbage.
315 static const char *read_greeting(void) {
316   char got_magic[sizeof(header_magic)];
317
318   if (protocol_read_maybe(&got_magic, sizeof(got_magic)) < 0)
319     return "initial monitor process quit";
320
321   if (memcmp(got_magic, header_magic, sizeof(header_magic)))
322     die("got unexpected protocol magic 0x%02x%02x%02x%02x",
323         got_magic[0], got_magic[1], got_magic[2], got_magic[3]);
324
325   uint32_t xdata_len;
326   protocol_read(&xdata_len, sizeof(xdata_len));
327   void *xdata = xmalloc(xdata_len);
328   protocol_read(xdata, xdata_len);
329
330   return 0;
331 }
332
333 // Returns: call(client-end), or 0 to mean "is garbage"
334 // find_socket_path must have been called
335 static FILE *connect_existing(void) {
336   int r;
337   int fd = -1;
338
339   fd = socket(AF_UNIX, SOCK_STREAM, 0);
340   if (fd==-1) diee("socket() for client");
341
342   socklen_t salen = sizeof(sockaddr_sun);
343   r = connect(fd, (const struct sockaddr*)&sockaddr_sun, salen);
344   if (r==-1) {
345     if (errno==ECONNREFUSED || errno==ENOENT) goto x_garbage;
346     diee("connect() %s", socket_path);
347   }
348
349   call_sock = call_sock_from_fd(fd);
350   fd = -1;
351
352   if (read_greeting())
353     goto x_garbage;
354
355   return call_sock;
356
357  x_garbage:
358   if (call_sock) { fclose(call_sock); call_sock=0; }
359   if (fd >= 0) close(fd);
360   return 0;
361 }
362
363 static __attribute__((noreturn))
364 void become_setup(int sfd, int fake_pair[2]) {
365   close(fake_pair[0]);
366   int call_fd = fake_pair[1];
367
368   int null_0 = open("/dev/null", O_RDONLY);  if (null_0 < 0) diee("open null");
369   if (dup2(null_0, 0)) diee("dup2 /dev/null onto stdin");
370   if (dup2(2, 1) != 1) die("dup2 stderr onto stdout");
371
372   // Extension could work like this:
373   //
374   // We advertise a new protocol (perhaps one which is nearly entirely
375   // different after the connect) by putting a name for it comma-separated
376   // next to "v1".  Simple extension can be done by having the script
377   // side say something about it in the ack xdata, which we currently ignore.
378   putenv(m_asprintf("PREFORK_INTERP=v1 %d,%d %s",
379                     sfd, call_fd, socket_path));
380
381   execvp(executor_argv[0], (char**)executor_argv);
382   diee("execute %s", executor_argv[0]);
383 }
384
385 static void connect_or_spawn(void) {
386   int r;
387
388   call_sock = connect_existing();
389   if (call_sock) return;
390
391   int lockfd = acquire_lock();
392   call_sock = connect_existing();
393   if (call_sock) { close(lockfd); return; }
394
395   // We must start a fresh one, and we hold the lock
396
397   r = unlink(socket_path);
398   if (r<0 && errno!=ENOENT)
399     diee("failed to remove stale socket %s", socket_path);
400
401   int fake_pair[2];
402   r = socketpair(AF_UNIX, SOCK_STREAM, 0, fake_pair);
403   if (r<0) diee("socketpair() for fake initial connection");
404
405   int sfd = socket(AF_UNIX, SOCK_STREAM, 0);
406   if (sfd<0) diee("socket() for new listener");
407
408   socklen_t salen = sizeof(sockaddr_sun);
409   r= bind(sfd, (const struct sockaddr*)&sockaddr_sun, salen);
410   if (r<0) diee("bind() on new listener");
411
412   // We never want callers to get ECONNREFUSED.  But:
413   // There is a race here: from my RTFM they may get ECONNREFUSED
414   // if they try between our bind() and listen().  But if they do, they'll
415   // acquire the lock (serialising with us) and retry, and then it will work.
416   r = listen(sfd, INT_MAX);
417   if (r<0) diee("listen() for new listener");
418
419   pid_t setup_pid = fork();
420   if (setup_pid == (pid_t)-1) diee("fork for spawn setup");
421   if (!setup_pid) become_setup(sfd, fake_pair);
422   close(fake_pair[1]);
423   close(sfd);
424
425   call_sock = call_sock_from_fd(fake_pair[0]);
426
427   int status;
428   pid_t got = waitpid(setup_pid, &status, 0);
429   if (got == (pid_t)-1) diee("waitpid setup [%ld]", (long)setup_pid);
430   if (got != setup_pid) diee("waitpid setup [%ld] gave [%ld]!",
431                              (long)setup_pid, (long)got);
432   if (status != 0) propagate_exit_status(status, "setup");
433
434   const char *emsg = read_greeting();
435   if (emsg) die("setup failed: %s", emsg);
436
437   close(lockfd);
438   return;
439 }
440
441 static void make_executor_argv(const char *const *argv) {
442   switch (laundering) {
443   case 'U': break;
444   default: die("need -U (specifying unlaundered argument handling)");
445   }
446
447   const char *arg;
448   #define EACH_NEW_ARG(EACH) {                  \
449     arg = interp; { EACH }                      \
450     if ((arg = script)) { EACH }                \
451     const char *const *walk = argv;             \
452     while ((arg = *walk++)) { EACH }            \
453   }
454
455   size_t count = 1;
456   EACH_NEW_ARG( (void)arg; count++; );
457
458   const char **out = calloc(count, sizeof(char*));
459   executor_argv = (const char* const*)out;
460   if (!executor_argv) diee("allocate for arguments");
461
462   EACH_NEW_ARG( *out++ = arg; );
463   *out++ = 0;
464 }  
465
466 int main(int argc_unused, const char *const *argv) {
467   process_opts(&argv);
468
469   // Now we have
470   //  - possibly interp
471   //  - possibly script
472   //  - remaining args
473   // which ought to be passed on to the actual executor.
474   make_executor_argv(argv);
475
476   find_socket_path();
477   FILLZERO(sockaddr_sun);
478   sockaddr_sun.sun_family = AF_UNIX;
479   assert(strlen(socket_path) <= sizeof(sockaddr_sun.sun_path));
480   strncpy(sockaddr_sun.sun_path, socket_path, sizeof(sockaddr_sun.sun_path));
481
482   connect_or_spawn();
483
484   // We're committed now, send the request (or bail out)
485   send_request();
486
487   uint32_t status;
488   protocol_read(&status, sizeof(status));
489
490   status = ntohl(status);
491   if (status > INT_MAX) die("status 0x%lx does not fit in an int",
492                             (unsigned long)status);
493
494   propagate_exit_status(status, "invocation");
495 }