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