chiark / gitweb /
prefork-interp: clean up old sockets
[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  *                            === acquires lock ===
22  *                            makes new listening socket
23  *                            makes watcher pipes
24  *                            forks watcher and awaits
25  *                            makes first-instance socketpair
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 (one fork, becomes session leader)
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  *                            setsid
55  *                            right away, forks init monitor
56  *                    [2]     closes outer caller fds and call(fake)
57  *        [server (pm)]       fd[012]: null
58  *                            other fds: listener, syslog
59  *                            runs in loop accepting and forking,
60  *                            reaping and limiting children (incl init monitor)
61  *                            reports failures of monitors to syslog
62  *                            
63  *  [client (C wrapper)]      if client connect succeeds:
64  *                            now fd: call(client-end)
65  *                               sends message with: cmdline, env
66  *                               sends fds
67  *
68  *        [server (script)]   accepts, forks subseq monitor
69  *
70  *          monitor [1]       [fd0: null]
71  *           (init            [fd[12]: init: fd2-from-outer; subseq: null]
72  *             or             errors: init: fd2; subseq: syslog
73  *            subseq)         other fds: syslog, call(server-end)
74  *                            sends ack byte
75  *                            receives args, env, fds
76  *                            forks executor
77  *
78  *            executor        sorts out fds:
79  *                            fd0, fd1, fd2: from-outer
80  *                            close fds: call(server-end)
81  *                            retained fds: syslog
82  *
83  *                            sets cmdline, env
84  *                            runs main part of script
85  *                            exits normally
86  *
87  *          [monitor]         [fd[012]: null]
88  *                            [fd[12]: init: fd2-from-outer; subseq: null]
89  *                            [errors: init: fd2; subseq: syslog]
90  *                            reaps executor
91  *                            reports status via socket
92  *
93  *    [client (C wrapper)]    [fd0, fd1, fd2: from-outer]
94  *                            [other fd: call(client-end)]
95  *                            receives status, exits appropriately
96  *                            (if was bad signal, reports to stderr, exits 127)
97  */
98
99 #include <arpa/inet.h>
100
101 #include <uv.h>
102
103 #include "prefork.h"
104
105 const char our_name[] = "prefork-interp";
106
107 static struct sockaddr_un sockaddr_sun;
108 static FILE *call_sock;
109
110 #define ACK_BYTE '\n'
111
112 static const char *const *executor_argv;
113
114 static const char header_magic[4] = "PFI\n";
115
116 void fusagemessage(FILE *f) {
117   fprintf(f, "usage: #!/usr/bin/prefork-interp [<options>]\n");
118 }
119
120 static int laundering;
121 static int max_sockets = 100; // maximum entries in the run dir is 2x this
122
123 static struct stat initial_stab;
124
125 const struct cmdinfo cmdinfos[]= {
126   PREFORK_CMDINFOS
127   { 0, 'U',   0,                    .iassignto= &laundering,    .arg= 'U' },
128   { 0 }
129 };
130
131 void ident_addinit(void) {
132   char ident_magic[1] = { 0 };
133   sha256_update(&identsc, sizeof(ident_magic), ident_magic);
134 }
135
136 static void propagate_exit_status(int status, const char *what) {
137   int r;
138
139   if (WIFEXITED(status)) {
140     _exit(status);
141   }
142
143   if (WIFSIGNALED(status)) {
144     int sig = WTERMSIG(status);
145     const char *signame = strsignal(sig);
146     if (signame == 0) signame = "unknown signal";
147
148     if (! WCOREDUMP(status) &&
149         (sig == SIGINT ||
150          sig == SIGHUP ||
151          sig == SIGPIPE ||
152          sig == SIGKILL)) {
153       struct sigaction sa;
154       FILLZERO(sa);
155       sa.sa_handler = SIG_DFL;
156       r = sigaction(sig, &sa, 0);
157       if (r) diee("failed to reset signal handler while propagating %s",
158                   signame);
159       
160       sigset_t sset;
161       sigemptyset(&sset);
162       sigaddset(&sset, sig);
163       r = sigprocmask(SIG_UNBLOCK, &sset, 0);
164       if (r) diee("failed to reset signal block while propagating %s",
165                   signame);
166
167       raise(sig);
168       die("unexpectedly kept running after raising (to propagate) %s",
169           signame);
170     }
171
172     die("%s failed due to signal %d %s%s", what, sig, signame,
173         WCOREDUMP(status) ? " (core dumped)" : "");
174   }
175
176   die("%s failed with weird wait status %d 0x%x", what, status, status);
177 }
178
179 typedef struct {
180   char *name_hash;
181   time_t atime;
182 } PrecleanEntry;
183
184 static int preclean_entry_compar_name(const void *av, const void *bv) {
185   const PrecleanEntry *a = av;
186   const PrecleanEntry *b = bv;
187   return strcmp(a->name_hash, b->name_hash);
188 }
189
190 static int preclean_entry_compar_atime(const void *av, const void *bv) {
191   const PrecleanEntry *ae = av;  time_t a = ae->atime;
192   const PrecleanEntry *be = bv;  time_t b = be->atime;
193   return (a > b ? +1 :
194           a < b ? -1 : 0);
195 }
196
197 static time_t preclean_stat_atime(const char *s_path) {
198   struct stat stab;
199   int r= lstat(s_path, &stab);
200   if (r) {
201     if (errno!=ENOENT) diee("pre-cleanup: stat socket (%s)", s_path);
202     return 0;
203   }
204   return stab.st_atime;
205 }
206
207 static void preclean(void) {
208   DIR *dir = opendir(run_base);
209   if (!dir) {
210     if (errno == ENOENT) return;
211     diee("pre-cleanup: open run dir (%s)", run_base);
212   }
213
214   PrecleanEntry *entries=0;
215   size_t avail_entries=0;
216   size_t used_entries=0;
217
218   struct dirent *de;
219   while ((errno = 0, de = readdir(dir))) {
220     char c0 = de->d_name[0];
221     if (!(c0 == 'l' || c0 == 's')) continue;
222     char *name_hash = m_asprintf("%s", de->d_name+1);
223     char *s_path = m_asprintf("%s/s%s", run_base, name_hash);
224     time_t atime = preclean_stat_atime(s_path);
225
226     if (avail_entries == used_entries) {
227       assert(avail_entries < INT_MAX / 4 / sizeof(PrecleanEntry));
228       avail_entries <<= 1;
229       avail_entries += 10;
230       entries = realloc(entries, avail_entries * sizeof(PrecleanEntry));
231     }
232     entries[used_entries].name_hash = name_hash;
233     entries[used_entries].atime = atime;
234     used_entries++;
235   }
236   if (errno) diee("pre-cleanup: read run dir (%s)", run_base);
237
238   // First we dedupe (after sorting by path)
239   qsort(entries, used_entries, sizeof(PrecleanEntry),
240         preclean_entry_compar_name);
241   PrecleanEntry *p, *q;
242   for (p=entries, q=entries; p < entries + used_entries; p++) {
243     if (q > entries && !strcmp(p->name_hash, (q-1)->name_hash))
244       continue;
245     *q++ = *p;
246   }
247   used_entries = q - entries;
248
249   // Now maybe delete some things
250   //
251   // Actually this has an off-by-one error since we are about
252   // to create a socket, so the actual number of sockets is one more.
253   // But, *actually*, since there might be multiple of us running at once,
254   // we might have even more than that.  This doesn't really matter.
255   if (used_entries > max_sockets) {
256     qsort(entries, used_entries, sizeof(PrecleanEntry),
257           preclean_entry_compar_atime);
258     for (p=entries; p < entries + max_sockets; p++) {
259       char *l_path = m_asprintf("%s/l%s", run_base, p->name_hash);
260       char *s_path = m_asprintf("%s/s%s", run_base, p->name_hash);
261       int lock_fd = flock_file(l_path);
262       // Recheck atime - we might have raced!
263       time_t atime = preclean_stat_atime(s_path);
264       if (atime == p->atime) {
265         // Raced.  This will leave use deleting too few things.  Whatever.
266       } else {
267         int r= unlink(s_path);
268         if (r && errno!=ENOENT) diee("preclean: delete stale (%s)", s_path);
269         r= unlink(l_path);
270         if (r) diee("preclean: delete stale lock (%s)", s_path);
271         // NB we don't hold the lock any more now.
272       }
273       close(lock_fd);
274       free(l_path);
275       free(s_path);
276     }
277   }
278
279   for (p=entries; p < entries + used_entries; p++)
280     free(p->name_hash);
281   free(entries);
282 }
283
284 static __attribute((noreturn)) void die_data_overflow(void) {
285   die("cannot handle data with length >2^32");
286 }
287
288 static void prepare_data(size_t *len, char **buf,
289                          const void *data, size_t dl) {
290   if (len) {
291     if (dl >= SIZE_MAX - *len)
292       die_data_overflow();
293     *len += dl;
294   }
295   if (buf) {
296     memcpy(*buf, data, dl);
297     *buf += dl;
298   }
299 }
300   
301 static void prepare_length(size_t *len, char **buf, size_t dl_sz) {
302   if (dl_sz > UINT32_MAX) die_data_overflow();
303   uint32_t dl = htonl(dl_sz);
304   prepare_data(len, buf, &dl, sizeof(dl));
305 }
306
307 static void prepare_string(size_t *len, char **buf, const char *s) {
308   size_t sl = strlen(s);
309   prepare_data(len, buf, s, sl+1);
310 }
311
312 static void prepare_message(size_t *len, char **buf) {
313   const char *s;
314
315   const char *const *p = (void*)environ;
316   while ((s = *p++)) {
317     if (strchr(s, '='))
318       prepare_string(len, buf, s);
319   }
320
321   prepare_string(len, buf, "");
322
323   p = executor_argv;
324   while ((s = *p++))
325     prepare_string(len, buf, s);
326 }
327
328 static void send_fd(int payload_fd) {
329   int via_fd = fileno(call_sock);
330
331   union {
332     struct cmsghdr align;
333     char buf[CMSG_SPACE(sizeof(payload_fd))];
334   } cmsg_buf;
335
336   struct msghdr msg;
337   FILLZERO(msg);
338   FILLZERO(cmsg_buf);
339
340   char dummy_byte = 0;
341   struct iovec iov;
342   FILLZERO(iov);
343   iov.iov_base = &dummy_byte;
344   iov.iov_len = 1;
345
346   msg.msg_name = 0;
347   msg.msg_iov = &iov;
348   msg.msg_iovlen = 1;
349   msg.msg_control = cmsg_buf.buf;
350   msg.msg_controllen = sizeof(cmsg_buf.buf);
351
352   struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
353   cmsg->cmsg_level = SOL_SOCKET;
354   cmsg->cmsg_type = SCM_RIGHTS;
355   cmsg->cmsg_len = CMSG_LEN(sizeof(payload_fd));
356   *(int*)CMSG_DATA(cmsg) = payload_fd;
357
358   msg.msg_controllen = sizeof(cmsg_buf.buf);
359
360   for (;;) {
361     ssize_t r = sendmsg(via_fd, &msg, 0);
362     if (r == -1) {
363       if (errno == EINTR) continue;
364       diee("send fd");
365     }
366     assert(r == 1);
367     break;
368   }
369 }
370
371 static void send_request(void) {
372   // Sending these first makes it easier for the script to
373   // use buffered IO for the message.
374   send_fd(0);
375   send_fd(1);
376   send_fd(2);
377
378   size_t len = 0;
379   prepare_message(&len, 0);
380
381   size_t tlen = len + 4;
382   char *m = xmalloc(tlen);
383   char *p = m;
384   prepare_length(0, &p, len);
385   prepare_message(0, &p);
386   assert(p == m + tlen);
387
388   ssize_t sr = fwrite(m, tlen, 1, call_sock);
389   if (sr != 1) diee("write request (buffer)");
390
391   if (fflush(call_sock)) diee("write request");
392 }
393
394 static FILE *call_sock_from_fd(int fd) {
395   int r;
396
397   FILE *call_sock = fdopen(fd, "r+");
398   if (!call_sock) diee("fdopen socket");
399
400   r = setvbuf(call_sock, 0, _IONBF, 0);
401   if (r) die("setvbuf socket");
402
403   return call_sock;
404 }
405
406 static bool was_eof(FILE *call_sock) {
407   return feof(call_sock) || errno==ECONNRESET;
408 }
409
410 // Returns -1 on EOF
411 static int protocol_read_maybe(void *data, size_t sz) {
412   if (!sz) return 0;
413   size_t sr = fread(data, sz, 1, call_sock);
414   if (sr != 1) {
415     if (was_eof(call_sock)) return -1;
416     diee("read() on monitor call socket (%zd)", sz);
417   }
418   return 0;
419 }
420
421 static void protocol_read(void *data, size_t sz) {
422   if (protocol_read_maybe(data, sz) < 0)
423     die("monitor process quit unexpectedly");
424 }
425
426 // Returns 0 if OK, error msg if peer was garbage.
427 static const char *read_greeting(void) {
428   char got_magic[sizeof(header_magic)];
429
430   if (protocol_read_maybe(&got_magic, sizeof(got_magic)) < 0)
431     return "initial monitor process quit";
432
433   if (memcmp(got_magic, header_magic, sizeof(header_magic)))
434     die("got unexpected protocol magic 0x%02x%02x%02x%02x",
435         got_magic[0], got_magic[1], got_magic[2], got_magic[3]);
436
437   uint32_t xdata_len;
438   protocol_read(&xdata_len, sizeof(xdata_len));
439   void *xdata = xmalloc(xdata_len);
440   protocol_read(xdata, xdata_len);
441
442   return 0;
443 }
444
445 // Returns: call(client-end), or 0 to mean "is garbage"
446 // find_socket_path must have been called
447 static FILE *connect_existing(void) {
448   int r;
449   int fd = -1;
450
451   fd = socket(AF_UNIX, SOCK_STREAM, 0);
452   if (fd==-1) diee("socket() for client");
453
454   socklen_t salen = sizeof(sockaddr_sun);
455   r = connect(fd, (const struct sockaddr*)&sockaddr_sun, salen);
456   if (r==-1) {
457     if (errno==ECONNREFUSED || errno==ENOENT) goto x_garbage;
458     diee("connect() %s", socket_path);
459   }
460
461   call_sock = call_sock_from_fd(fd);
462   fd = -1;
463
464   if (read_greeting())
465     goto x_garbage;
466
467   return call_sock;
468
469  x_garbage:
470   if (call_sock) { fclose(call_sock); call_sock=0; }
471   if (fd >= 0) close(fd);
472   return 0;
473 }
474
475 static void watcher_cb_stdin(uv_poll_t *handle, int status, int events) {
476   char c;
477   int r;
478   
479   if ((errno = -status)) diee("watcher: poll stdin");
480   for (;;) {
481     r= read(0, &c, 1);
482     if (r!=-1) _exit(0);
483     if (!(errno==EINTR || errno==EWOULDBLOCK || errno==EAGAIN))
484       diee("watcher: read sentinel stdin");
485   }
486 }
487
488 static void watcher_cb_sockpath(uv_fs_event_t *handle, const char *filename,
489                                 int events, int status) {
490   int r;
491   struct stat now_stab;
492
493   if ((errno = -status)) diee("watcher: poll stdin");
494   for (;;) {
495     r= stat(socket_path, &now_stab);
496     if (r==-1) {
497       if (errno==ENOENT) _exit(0);
498       if (errno==EINTR) continue;
499       diee("stat socket: %s", socket_path);
500     }
501     if (!stabs_same_inode(&now_stab, &initial_stab))
502       _exit(0);
503   }
504 }
505
506 // On entry, stderr is still inherited, but 0 and 1 are the pipes
507 static __attribute__((noreturn))
508 void become_watcher(void) {
509   uv_loop_t loop;
510   uv_poll_t uvhandle_stdin;
511   uv_fs_event_t uvhandle_sockpath;
512   int r;
513
514   nonblock(0);
515
516   errno= -uv_loop_init(&loop);
517   if (errno) diee("watcher: uv_loop_init");
518
519   errno= -uv_poll_init(&loop, &uvhandle_stdin, 0);
520   if (errno) diee("watcher: uv_poll_init");
521   errno= -uv_poll_start(&uvhandle_stdin,
522                         UV_READABLE | UV_WRITABLE | UV_DISCONNECT,
523                         watcher_cb_stdin);
524   if (errno) diee("watcher: uv_poll_start");
525
526   errno= -uv_fs_event_init(&loop, &uvhandle_sockpath);
527   if (errno) diee("watcher: uv_fs_event_init");
528
529   errno= -uv_fs_event_start(&uvhandle_sockpath, watcher_cb_sockpath,
530                             socket_path, 0);
531   if (errno) diee("watcher: uv_fs_event_start");
532
533   // OK everything is set up, let us daemonise
534   if (dup2(1,2) != 2) diee("watcher: set daemonised stderr");
535   r= setvbuf(stderr, 0, _IOLBF, BUFSIZ);
536   if (r) diee("watcher: setvbuf stderr");
537
538   pid_t child = fork();
539   if (child == (pid_t)-1) diee("watcher: fork");
540   if (child) _exit(0);
541
542   if (setsid() == (pid_t)-1) diee("watcher: setsid");
543
544   r= uv_run(&loop, UV_RUN_DEFAULT);
545   die("uv_run returned (%d)", r);
546 }
547
548 static __attribute__((noreturn))
549 void become_setup(int sfd, int fake_pair[2],
550                   int watcher_stdin, int watcher_stderr) {
551   close(fake_pair[0]);
552   int call_fd = fake_pair[1];
553
554   int null_0 = open("/dev/null", O_RDONLY);  if (null_0 < 0) diee("open null");
555   if (dup2(null_0, 0)) diee("dup2 /dev/null onto stdin");
556   close(null_0);
557   if (dup2(2, 1) != 1) die("dup2 stderr onto stdout");
558
559   nonblock(sfd);
560
561   // Extension could work like this:
562   //
563   // We advertise a new protocol (perhaps one which is nearly entirely
564   // different after the connect) by putting a name for it comma-separated
565   // next to "v1".  Simple extension can be done by having the script
566   // side say something about it in the ack xdata, which we currently ignore.
567   putenv(m_asprintf("PREFORK_INTERP=v1 %d,%d,%d,%d",
568                     sfd, call_fd, watcher_stdin, watcher_stderr));
569
570   execvp(executor_argv[0], (char**)executor_argv);
571   diee("execute %s", executor_argv[0]);
572 }
573
574 static void connect_or_spawn(void) {
575   int r;
576
577   call_sock = connect_existing();
578   if (call_sock) return;
579
580   // We're going to make a new one, so clean out old ones
581   preclean();
582
583   int lockfd = acquire_lock();
584   call_sock = connect_existing();
585   if (call_sock) { close(lockfd); return; }
586
587   // We must start a fresh one, and we hold the lock
588
589   r = unlink(socket_path);
590   if (r<0 && errno!=ENOENT)
591     diee("failed to remove stale socket %s", socket_path);
592
593   int sfd = socket(AF_UNIX, SOCK_STREAM, 0);
594   if (sfd<0) diee("socket() for new listener");
595
596   socklen_t salen = sizeof(sockaddr_sun);
597   r= bind(sfd, (const struct sockaddr*)&sockaddr_sun, salen);
598   if (r<0) diee("bind() on new listener");
599
600   r= stat(socket_path, &initial_stab);
601   if (r<0) diee("stat() fresh socket");
602
603   // We never want callers to get ECONNREFUSED.  But:
604   // There is a race here: from my RTFM they may get ECONNREFUSED
605   // if they try between our bind() and listen().  But if they do, they'll
606   // acquire the lock (serialising with us) and retry, and then it will work.
607   r = listen(sfd, INT_MAX);
608   if (r<0) diee("listen() for new listener");
609
610   // Fork watcher
611
612   int watcher_stdin[2];
613   int watcher_stderr[2];
614   if (pipe(watcher_stdin) || pipe(watcher_stderr))
615     diee("pipe() for socket inode watcher");
616
617   pid_t watcher = fork();
618   if (watcher == (pid_t)-1) diee("fork for watcher");
619   if (!watcher) {
620     close(sfd);
621     close(lockfd);
622     close(watcher_stdin[1]);
623     close(watcher_stderr[0]);
624     if (dup2(watcher_stdin[0], 0) != 0 ||
625         dup2(watcher_stderr[1], 1) != 1)
626       diee("initial dup2() for watcher");
627     close(watcher_stdin[0]);
628     close(watcher_stderr[1]);
629     become_watcher();
630   }
631
632   close(watcher_stdin[0]);
633   close(watcher_stderr[1]);
634   nonblock(watcher_stderr[0]);
635
636   // Fork setup
637
638   int fake_pair[2];
639   r = socketpair(AF_UNIX, SOCK_STREAM, 0, fake_pair);
640   if (r<0) diee("socketpair() for fake initial connection");
641
642   pid_t setup_pid = fork();
643   if (setup_pid == (pid_t)-1) diee("fork for spawn setup");
644   if (!setup_pid) become_setup(sfd, fake_pair,
645                                watcher_stdin[1], watcher_stderr[0]);
646   close(fake_pair[1]);
647   close(sfd);
648
649   call_sock = call_sock_from_fd(fake_pair[0]);
650
651   int status;
652   pid_t got = waitpid(setup_pid, &status, 0);
653   if (got == (pid_t)-1) diee("waitpid setup [%ld]", (long)setup_pid);
654   if (got != setup_pid) diee("waitpid setup [%ld] gave [%ld]!",
655                              (long)setup_pid, (long)got);
656   if (status != 0) propagate_exit_status(status, "setup");
657
658   const char *emsg = read_greeting();
659   if (emsg) die("setup failed: %s", emsg);
660
661   close(lockfd);
662   return;
663 }
664
665 static void make_executor_argv(const char *const *argv) {
666   switch (laundering) {
667   case 'U': break;
668   default: die("need -U (specifying unlaundered argument handling)");
669   }
670
671   const char *arg;
672   #define EACH_NEW_ARG(EACH) {                  \
673     arg = interp; { EACH }                      \
674     if ((arg = script)) { EACH }                \
675     const char *const *walk = argv;             \
676     while ((arg = *walk++)) { EACH }            \
677   }
678
679   size_t count = 1;
680   EACH_NEW_ARG( (void)arg; count++; );
681
682   const char **out = calloc(count, sizeof(char*));
683   executor_argv = (const char* const*)out;
684   if (!executor_argv) diee("allocate for arguments");
685
686   EACH_NEW_ARG( *out++ = arg; );
687   *out++ = 0;
688 }  
689
690 int main(int argc_unused, const char *const *argv) {
691   process_opts(&argv);
692
693   // Now we have
694   //  - possibly interp
695   //  - possibly script
696   //  - remaining args
697   // which ought to be passed on to the actual executor.
698   make_executor_argv(argv);
699
700   find_socket_path();
701   FILLZERO(sockaddr_sun);
702   sockaddr_sun.sun_family = AF_UNIX;
703   assert(strlen(socket_path) <= sizeof(sockaddr_sun.sun_path));
704   strncpy(sockaddr_sun.sun_path, socket_path, sizeof(sockaddr_sun.sun_path));
705
706   connect_or_spawn();
707
708   // We're committed now, send the request (or bail out)
709   send_request();
710
711   uint32_t status;
712   protocol_read(&status, sizeof(status));
713
714   status = ntohl(status);
715   if (status > INT_MAX) die("status 0x%lx does not fit in an int",
716                             (unsigned long)status);
717
718   propagate_exit_status(status, "invocation");
719 }