chiark / gitweb /
5c32efe7e68871332a449376c1e8744074e15d99
[chiark-utils.git] / cprogs / cgi-fcgi-interp.c
1 /*
2  * "Interpreter" that you can put in #! like this
3  *   #!/usr/bin/cgi-fcgi-interp [<options>] <interpreter>
4  *   #!/usr/bin/cgi-fcgi-interp [<options>],<interpreter>
5  */
6 /*
7  * cgi-fcgi-interp.[ch] - C helpers common to the whole of chiark-utils
8  *
9  * Copyright 2016 Ian Jackson
10  * Copyright 1982,1986,1993 The Regents of the University of California
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public
23  * License along with this file; if not, consult the Free Software
24  * Foundation's website at www.fsf.org, or the GNU Project website at
25  * www.gnu.org.
26  *
27  * See below for a BSD 3-clause notice regarding timespeccmp.
28  */
29 /*
30  * The result is a program which looks, when executed via the #!
31  * line, like a CGI program.  But the script inside will be executed
32  * via <interpreter> in an fcgi context.
33  *
34  * Options:
35  *
36  *  <interpreter>
37  *          The real interpreter to use.  Eg "perl".  Need not
38  *          be an absolute path; will be fed to execvp.
39  *
40  *  -G<ident-info>
41  *          Add <ident-info> to the unique identifying information for
42  *          this fcgi program.  May be repeated; order is significant.
43  *
44  *  -E<ident-info-env-var>
45  *          Look <ident-info-env-var> up in the environment and add
46  *          <ident-info-env-var>=<value> as if specified with -G.  If
47  *          the variable is unset in the environment, it is as if
48  *          -G<ident-info-env-var> was specified.
49  *
50  *  -g<ident>
51  *          Use <ident> rather than hex(sha256(<interp>\0<script>\0))
52  *          as the basename of the leafname of the fcgi rendezvous
53  *          socket.  If <ident> contains only hex digit characters it
54  *          ought to be no more than 32 characters.  <ident> should
55  *          not contain spaces or commas (see below).
56  *
57  *  -M<numservers>
58  *         Start <numservers> instances of the program.  This
59  *         determines the maximum concurrency.  (Note that unlike
60  *         speedy, the specified number of servers is started
61  *         right away.)  The default is 4.
62  *
63  *  -c<interval>
64  *         Stale server check interval, in seconds.  The worker
65  *         process group will get a SIGTERM when it is no longer
66  *         needed to process new requests.  Ideally it would continue
67  *         to serve any existing requests.  The SIGTERM will arrive no
68  *         earlier than <interval> after the last request arrived at
69  *         the containing webserver.  Default is 300.
70  *
71  *  -D
72  *         Debug mode.  Do not actually run program.  Instead, print
73  *         out what we would do.
74  *
75  * <options> and <interpreter> can be put into a single argument
76  * to cgi-fcgi-interp, separated by spaces or commas.  <interpreter>
77  * must come last.
78  *
79  * cgi-fcgi-interp automatically expires old sockets, including
80  * ones where the named script is out of date.
81  */
82 /*
83  * Uses one of two directories
84  *   /var/run/user/<UID>/cgi-fcgi-interp/
85  *   ~/.cgi-fcgi-interp/<node>/
86  * and inside there uses these paths
87  *   s<ident>
88  *   l<ident>    used to lock around garbage collection
89  *
90  * If -M<ident> is not specified then an initial substring of the
91  * lowercase hex of the sha256 of <interp>\0<script>\0 is
92  * used.  The substring is chosen so that the whole path is 10 bytes
93  * shorter than sizeof(sun_path).  But always at least 33 characters.
94  *
95  * <node> is truncated at the first `.' and after the first 32
96  * characters.
97  *
98  * Algorithm:
99  *  - see if /var/run/user exists
100  *       if so, lstat /var/run/user/<UID> and check that
101  *         we own it and it's X700; if not, fail
102  *         if it's ok then <base> is /var/run/user/<UID>
103  *       otherwise, look for and maybe create ~/.cgi-fcgi-interp
104  *         (where ~ is HOME or from getpwuid)
105  *         and then <base> is ~/.cgi-fcgi-interp/<node>
106  *  - calculate pathname (checking <ident> length is OK)
107  *  - check for and maybe create <base>
108  *  - stat and lstat the <script>
109  *  - stat the socket and check its timestamp
110  *       if it is too old, unlink it
111  *  - dup stderr, mark no cloexec
112  *  - set CHIARKUTILS_CGIFCGIINTERP_STAGE2=<stderr-copy-fd>
113  *  - run     cgi-fcgi -connect SOCKET <script>
114  *
115  * When CHIARKUTILS_CGIFCGIINTERP_STAGE2 is set, --stage2 does this:
116  *  - dup2 <was-stderr> to fd 2
117  *  - open /dev/null and expect fd 1 (and if not, close it)
118  *  - become a new process group
119  *  - lstat <socket> to find its inum, mtime
120  *  - fork/exec <interp> <script>
121  *  - periodically lstat <interp> and <script> and
122  *      if mtime is newer than our start time
123  *      kill process group (at second iteration)
124  */
125
126 #include "common.h"
127
128 #include <stdio.h>
129 #include <stdlib.h>
130 #include <string.h>
131 #include <errno.h>
132 #include <stdbool.h>
133 #include <assert.h>
134 #include <limits.h>
135
136 #include <sys/types.h>
137 #include <sys/stat.h>
138 #include <sys/utsname.h>
139 #include <sys/socket.h>
140 #include <sys/un.h>
141 #include <sys/file.h>
142 #include <unistd.h>
143 #include <fcntl.h>
144 #include <pwd.h>
145 #include <time.h>
146 #include <signal.h>
147 #include <sys/wait.h>
148         
149 #include <nettle/sha.h>
150
151 #include "myopt.h"
152
153 #define MINHEXHASH 33
154 #define STAGE2_VAR "CHIARKUTILS_CGIFCGIINTERP_STAGE2"
155
156 static const char *interp, *ident;
157 static int numservers=4, debugmode;
158 static int check_interval=300;
159
160 static struct sha256_ctx identsc;
161
162 static const char *stage2;
163
164 static void vmsgcore(int estatus, int errnoval, const char *fmt, va_list al) {
165   fputs("cgi-fcgi-interp: ",stderr);
166   vfprintf(stderr,fmt,al);
167   if (errnoval!=-1) fprintf(stderr,": %s",strerror(errnoval));
168   fputc('\n',stderr);
169   if (estatus) exit(estatus);
170 }
171
172 #define DEF_MSG(func, attrs, estatus, errnoval, after)  \
173   static void func(const char *fmt, ...)                \
174     __attribute__((format(printf,1,2))) attrs;          \
175   static void func(const char *fmt, ...) {              \
176     va_list al;                                         \
177     va_start(al,fmt);                                   \
178     vmsgcore(estatus,errnoval,fmt,al);                  \
179     after                                               \
180   }
181
182 DEF_MSG(warninge, /*empty*/, 0, errno, { });
183 DEF_MSG(warning , /*empty*/, 0, 0,     { });
184
185 #define DEF_DIE(func, errnoval) \
186   DEF_MSG(func, __attribute__((noreturn)), 127, errnoval, { abort(); })
187
188 DEF_DIE(diee, errno)
189 DEF_DIE(die,  0)
190
191 void common_diee(const char *m) { diee("%s", m); }
192 void common_die (const char *m) { die ("%s", m); }
193
194 static void fusagemessage(FILE *f) {
195   fprintf(f, "usage: #!/usr/bin/cgi-fcgi-interp [<options>]\n");
196 }
197
198 void usagemessage(void) { fusagemessage(stderr); }
199
200 static void of_help(const struct cmdinfo *ci, const char *val) {
201   fusagemessage(stdout);
202   if (ferror(stdout)) diee("write usage message to stdout");
203   exit(0);
204 }
205
206 static void of_iassign(const struct cmdinfo *ci, const char *val) {
207   long v;
208   char *ep;
209   errno= 0; v= strtol(val,&ep,10);
210   if (!*val || *ep || errno || v<INT_MIN || v>INT_MAX)
211     badusage("bad integer argument `%s' for --%s",val,ci->olong);
212   *ci->iassignto = v;
213 }
214
215 static void ident_addstring(const struct cmdinfo *ci, const char *string) {
216   /* ci may be 0 and is provided so this can be .call */
217   sha256_update(&identsc,strlen(string)+1,string);
218 }
219
220 static void off_ident_addenv(const struct cmdinfo *ci, const char *name) {
221   const char *val = getenv(name);
222   if (val) {
223     sha256_update(&identsc,strlen(name),name); /* no nul */
224     sha256_update(&identsc,1,"=");
225     ident_addstring(0,val);
226   } else {
227     ident_addstring(0,name);
228   }
229 }
230
231 #define MAX_OPTS 5
232
233 static const struct cmdinfo cmdinfos[]= {
234   { "help",   0, .call=of_help                                         },
235   { 0, 'g',   1,                    .sassignto= &ident                 },
236   { 0, 'G',   1, .call= ident_addstring                                },
237   { 0, 'E',   1, .call= off_ident_addenv                               },
238   { 0, 'M',   1, .call=of_iassign,  .iassignto= &numservers            },
239   { 0, 'D',   0,                    .iassignto= &debugmode,    .arg= 1 },
240   { 0, 'c',   1, .call=of_iassign,  .iassignto= &check_interval        },
241   { 0 }
242 };
243
244 static uid_t us;
245 static const char *run_base, *script, *socket_path;
246 static const char *run_base_mkdir_p;
247 static int stderr_copy;
248
249 static bool find_run_base_var_run(void) {
250   struct stat stab;
251   char *try;
252   int r;
253
254   try = m_asprintf("%s/%lu", "/var/run/user", us);
255   r = lstat(try, &stab);
256   if (r<0) {
257     if (errno == ENOENT ||
258         errno == ENOTDIR ||
259         errno == EACCES ||
260         errno == EPERM)
261       return 0; /* oh well */
262     diee("stat /var/run/user/UID");
263   }
264   if (!S_ISDIR(stab.st_mode)) {
265     warning("%s not a directory, falling back to ~\n", try);
266     return 0;
267   }
268   if (stab.st_uid != us) {
269     warning("%s not owned by uid %lu, falling back to ~\n", try,
270             (unsigned long)us);
271     return 0;
272   }
273   if (stab.st_mode & 0077) {
274     warning("%s writeable by group or other, falling back to ~\n", try);
275     return 0;
276   }
277   run_base = m_asprintf("%s/%s", try, "cgi-fcgi-interp");
278   return 1;
279 }
280
281 static bool find_run_base_home(void) {
282   struct passwd *pw;
283   struct utsname ut;
284   char *dot, *try;
285   int r;
286
287   pw = getpwuid(us);  if (!pw) diee("getpwent(uid)");
288
289   r = uname(&ut);   if (r) diee("uname(2)");
290   dot = strchr(ut.nodename, '.');
291   if (dot) *dot = 0;
292   if (sizeof(ut.nodename) > 32)
293     ut.nodename[32] = 0;
294
295   run_base_mkdir_p = m_asprintf("%s/%s", pw->pw_dir, ".cgi-fcgi-interp");
296   try = m_asprintf("%s/%s", run_base_mkdir_p, ut.nodename);
297   run_base = try;
298   return 1;
299 }
300
301 static void find_socket_path(void) {
302   struct sockaddr_un sun;
303   int r;
304
305   us = getuid();  if (us==(uid_t)-1) diee("getuid");
306
307   find_run_base_var_run() ||
308     find_run_base_home() ||
309     (abort(),0);
310
311   int maxidentlen = sizeof(sun.sun_path) - strlen(run_base) - 10 - 2;
312
313   if (!ident) {
314     if (maxidentlen < MINHEXHASH)
315       die("base directory `%s'"
316           " leaves only %d characters for id hash"
317           " which is too little (<%d)",
318           run_base, maxidentlen, MINHEXHASH);
319
320     int identlen = maxidentlen > 64 ? 64 : maxidentlen;
321     char *hexident = xmalloc(identlen + 2);
322     unsigned char bbuf[32];
323     int i;
324
325     ident_addstring(0,interp);
326     ident_addstring(0,script);
327     sha256_digest(&identsc,sizeof(bbuf),bbuf);
328
329     for (i=0; i<identlen; i += 2)
330       sprintf(hexident+i, "%02x", bbuf[i/2]);
331
332     hexident[identlen] = 0;
333     ident = hexident;
334   }
335
336   if (strlen(ident) > maxidentlen)
337     die("base directory `%s' plus ident `%s' too long"
338         " (with spare) for socket (max ident %d)\n",
339         run_base, ident, maxidentlen);
340
341   r = mkdir(run_base, 0700);
342   if (r && errno==ENOENT && run_base_mkdir_p) {
343     r = mkdir(run_base_mkdir_p, 0700);
344     if (r) diee("mkdir %s (since %s was ENOENT)",run_base_mkdir_p,run_base);
345     r = mkdir(run_base, 0700);
346   }
347   if (r) {
348     if (!(errno == EEXIST))
349       diee("mkdir %s",run_base);
350   }
351
352   socket_path = m_asprintf("%s/s%s",run_base,ident);
353 }  
354
355 /*
356  * Regarding the macro timespeccmp:
357  *
358  * Copyright (c) 1982, 1986, 1993
359  *      The Regents of the University of California.  All rights reserved.
360  *
361  * Redistribution and use in source and binary forms, with or without
362  * modification, are permitted provided that the following conditions
363  * are met:
364  * 1. Redistributions of source code must retain the above copyright
365  *    notice, this list of conditions and the following disclaimer.
366  * 2. Redistributions in binary form must reproduce the above copyright
367  *    notice, this list of conditions and the following disclaimer in the
368  *    documentation and/or other materials provided with the distribution.
369  * 4. Neither the name of the University nor the names of its contributors
370  *    may be used to endorse or promote products derived from this software
371  *    without specific prior written permission.
372  *
373  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
374  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
375  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
376  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
377  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
378  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
379  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
380  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
381  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
382  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
383  * SUCH DAMAGE.
384  *
385  *      @(#)time.h      8.5 (Berkeley) 5/4/95
386  * $FreeBSD: head/sys/sys/time.h 275985 2014-12-21 05:07:11Z imp $
387  */
388 #ifndef timespeccmp
389 #define timespeccmp(tvp, uvp, cmp)                                      \
390         (((tvp)->tv_sec == (uvp)->tv_sec) ?                             \
391             ((tvp)->tv_nsec cmp (uvp)->tv_nsec) :                       \
392             ((tvp)->tv_sec cmp (uvp)->tv_sec))
393 #endif /*timespeccmp*/
394
395
396
397 #ifdef st_mtime
398
399 static bool stab_isnewer(const struct stat *a, const struct stat *b) {
400   if (debugmode)
401     fprintf(stderr,"stab_isnewer mtim %lu.%06lu %lu.06%lu\n",
402             (unsigned long)a->st_mtim.tv_sec,
403             (unsigned long)a->st_mtim.tv_nsec,
404             (unsigned long)b->st_mtim.tv_sec,
405             (unsigned long)b->st_mtim.tv_nsec);
406   return timespeccmp(&a->st_mtim, &b->st_mtim, >);
407 }
408
409 static void stab_mtimenow(struct stat *out) {
410   int r = clock_gettime(CLOCK_REALTIME, &out->st_mtim);
411   if (r) diee("(stage2) clock_gettime");
412   if (debugmode)
413     fprintf(stderr,"stab_mtimenow mtim %lu.%06lu\n",
414             (unsigned long)out->st_mtim.tv_sec,
415             (unsigned long)out->st_mtim.tv_nsec);
416 }
417
418 #else /* !defined(st_mtime) */
419
420 static bool stab_isnewer(const struct stat *a, const struct stat *b) {
421   if (debugmode)
422     fprintf(stderr,"stab_isnewer mtime %lu %lu\n",
423             (unsigned long)a->st_mtime,
424             (unsigned long)b->st_mtime);
425   return a->st_mtime > &b->st_mtime;
426 }
427
428 static void stab_mtimenow(struct stat *out) {
429   out->st_mtime = time(NULL);
430   if (baseline_time.st_mtime == (time_t)-1) diee("(stage2) time()");
431   if (debugmode)
432     fprintf(stderr,"stab_mtimenow mtime %lu\n",
433             (unsigned long)out->st_mtime);
434 }
435
436 #endif /* !defined(st_mtime) */
437
438 static bool check_garbage_vs(const struct stat *started) {
439   struct stat script_stab;
440   int r;
441
442   r = lstat(script, &script_stab);
443   if (r) diee("lstat script (%s)",script);
444
445   if (stab_isnewer(&script_stab, started))
446     return 1;
447
448   if (S_ISLNK(script_stab.st_mode)) {
449     r = stat(script, &script_stab);
450     if (r) diee("stat script (%s0",script);
451
452     if (stab_isnewer(&script_stab, started))
453       return 1;
454   }
455
456   return 0;
457 }
458
459 static bool check_garbage(void) {
460   struct stat sock_stab;
461   int r;
462
463   r = lstat(socket_path, &sock_stab);
464   if (r) {
465     if ((errno == ENOENT))
466       return 0; /* well, no garbage then */
467     diee("stat socket (%s)",socket_path);
468   }
469
470   return check_garbage_vs(&sock_stab);
471 }
472
473 static void tidy_garbage(void) {
474   /* We lock l<ident> and re-check.  The effect of this is that each
475    * stale socket is removed only once.  So unless multiple updates to
476    * the script happen rapidly, we can't be racing with the cgi-fcgi
477    * (which is recreating the socket */
478   int lockfd = -1;
479   int r;
480
481   const char *lock_path = m_asprintf("%s/l%s",run_base,ident);
482
483   lockfd = open(lock_path, O_CREAT|O_RDWR, 0600);
484   if (lockfd<0) diee("create lock (%s)", lock_path);
485
486   r = flock(lockfd, LOCK_EX);
487   if (r) diee("lock lock (%s)", lock_path);
488
489   if (check_garbage()) {
490     r = unlink(socket_path);
491     if (r) {
492       if (!(errno == ENOENT))
493         diee("remove out-of-date socket (%s)", socket_path);
494     }
495   }
496
497   r = close(lockfd);
498   if (r) diee("close lock (%s)", lock_path);
499 }
500
501 static void make_stderr_copy(void) {
502   stderr_copy = dup(2);
503   if (stderr_copy < 0) diee("dup stderr (for copy for stage2)");
504 }
505
506 static void prep_stage2(void) {
507   int r;
508   
509   const char *stage2_val = m_asprintf("%d", stderr_copy);
510   r = setenv(STAGE2_VAR, stage2_val, 1);
511   if (r) diee("set %s (to announce to stage2)", STAGE2_VAR);
512 }
513
514 static void shbang_opts(const char *const **argv_io,
515                         const struct cmdinfo *cmdinfos) {
516   myopt(argv_io, cmdinfos);
517
518   interp = *(*argv_io)++;
519   if (!interp) badusage("need interpreter argument");
520 }
521
522 /* stage2 predeclarations */
523 static void record_baseline_time(void);
524 static void become_pgrp(void);
525 static void setup_handlers(void);
526 static void spawn_script(void);
527 static void queue_alarm(void);
528 static void await_something(void);
529
530 int main(int argc, const char *const *argv) {
531   const char *smashedopt;
532   int r;
533
534   stage2 = getenv(STAGE2_VAR);
535   if (stage2) {
536     int stderrfd = atoi(stage2);
537     assert(stderrfd>2);
538
539     r = dup2(stderrfd, 2);
540     assert(r==2);
541
542     r = open("/dev/null",O_WRONLY);
543     if (r<0) diee("open /dev/null as stdout");
544     if (r>=3) close(r);
545     else if (r!=1) die("open /dev/null for stdout gave bad fd %d",r);
546   }
547
548   sha256_init(&identsc);
549
550   if (argc>=2 &&
551       (smashedopt = argv[1]) &&
552       smashedopt[0]=='-' &&
553       (strchr(smashedopt,' ') || strchr(smashedopt,','))) {
554     /* single argument containg all the options and <interp> */
555     argv += 2; /* eat argv[0] and smashedopt */
556     const char *split_args[MAX_OPTS+1];
557     int split_argc = 0;
558     split_args[split_argc++] = argv[0];
559     for (;;) {
560       if (split_argc >= MAX_OPTS) die("too many options in combined arg");
561       split_args[split_argc++] = smashedopt;
562       if (smashedopt[0] != '-') /* never true on first iteration */
563         break;
564       char *delim = strchr(smashedopt,' ');
565       if (!delim) delim = strchr(smashedopt,',');
566       if (!delim) badusage("combined arg lacks <interpreter>");
567       *delim = 0;
568       smashedopt = delim+1;
569     }
570     assert(split_argc <= MAX_OPTS);
571     split_args[split_argc++] = 0;
572
573     const char *const *split_argv = split_args;
574
575     shbang_opts(&split_argv, cmdinfos);
576     /* sets interp */
577     if (!split_argv) badusage("combined arg too many non-option arguments");
578   } else {
579     shbang_opts(&argv, cmdinfos);
580   }
581
582   script = *argv++;
583   if (!script) badusage("need script argument");
584   if (*argv) badusage("too many arguments");
585
586   if (!stage2) {
587     
588     find_socket_path();
589
590     bool isgarbage = check_garbage();
591
592     if (debugmode) {
593       printf("socket: %s\n",socket_path);
594       printf("interp: %s\n",interp);
595       printf("script: %s\n",script);
596       printf("garbage: %d\n",isgarbage);
597       exit(0);
598     }
599
600     if (isgarbage)
601       tidy_garbage();
602
603     make_stderr_copy();
604     prep_stage2();
605
606     execlp("cgi-fcgi",
607            "cgi-fcgi", "-connect", socket_path,
608            script,
609            m_asprintf("%d", numservers),
610            (char*)0);
611     diee("exec cgi-fcgi");
612     
613   } else { /*stage2*/
614
615     record_baseline_time();
616     become_pgrp();
617     setup_handlers();
618     spawn_script();
619     queue_alarm();
620     await_something();
621     abort();
622
623   }
624 }
625
626 /* stage2 */
627
628 /* It is most convenient to handle the recheck timeout, as well as
629  * child death, in signal handlers.  Our signals all block each other,
630  * and the main program has signals blocked except in sigsuspend, so
631  * we don't need to worry about async-signal-safety, or errno. */
632
633 static struct stat baseline_time;
634 static pid_t script_child, stage2_pgrp;
635 static bool out_of_date;
636
637 static void record_baseline_time(void) {
638   stab_mtimenow(&baseline_time);
639 }
640
641 static void become_pgrp(void) {
642   int r;
643
644   stage2_pgrp = getpid();
645
646   r = setpgid(0,0);
647   if (r) diee("(stage2) setpgid");
648 }
649
650 static void atexit_handler(void) {
651   int r;
652
653   sighandler_t sigr = signal(SIGTERM,SIG_IGN);
654   if (sigr == SIG_ERR) warninge("(stage2) signal(SIGTERM,SIG_IGN)");
655
656   r = killpg(stage2_pgrp,SIGTERM);
657   if (r) warninge("(stage) killpg failed");
658 }
659
660 static void alarm_handler(int dummy) {
661   if (out_of_date)
662     /* second timeout */
663     exit(0); /* transfers control to atexit_handler */
664
665   out_of_date = check_garbage_vs(&baseline_time);
666   queue_alarm();
667 }
668
669 static void child_handler(int dummy) {
670   for (;;) {
671     int status;
672     pid_t got = waitpid(-1, &status, WNOHANG);
673     if (got == (pid_t)-1) diee("(stage2) waitpid");
674     if (got != script_child) {
675       warning("(stage2) waitpid got status %d for unknown child [%lu]",
676               status, (unsigned long)got);
677       continue;
678     }
679     if (WIFEXITED(status)) {
680       int v = WEXITSTATUS(status);
681       if (v) warning("program failed with error exit status %d", v);
682       exit(status);
683     } else if (WIFSIGNALED(status)) {
684       int s = WTERMSIG(status);
685       warning("program died due to fatal signal %s%s",
686               strsignal(s), WCOREDUMP(status) ? " (core dumped" : "");
687       assert(status & 0xff);
688       exit(status & 0xff);
689     } else {
690       die("program failed with crazy wait status %#x", status);
691     }
692   }
693   exit(127);
694 }
695
696 static void setup_handlers(void) {
697   struct sigaction sa;
698   int r;
699
700   r = atexit(atexit_handler);
701   if (r) diee("(stage2) atexit");
702
703   sigemptyset(&sa.sa_mask);
704   sigaddset(&sa.sa_mask, SIGALRM);
705   sigaddset(&sa.sa_mask, SIGCHLD);
706   sa.sa_flags = 0;
707
708   r = sigprocmask(SIG_BLOCK, &sa.sa_mask, 0);
709   if (r) diee("(stage2) sigprocmask(SIG_BLOCK,)");
710
711   sa.sa_handler = alarm_handler;
712   r = sigaction(SIGALRM, &sa, 0);
713   if (r) diee("(stage2) sigaction SIGALRM");
714
715   sa.sa_flags |= SA_NOCLDSTOP;
716   sa.sa_handler = child_handler;
717   r = sigaction(SIGCHLD, &sa, 0);
718   if (r) diee("(stage2) sigaction SIGCHLD");
719 }
720
721 static void spawn_script(void) {
722   script_child = fork();
723   if (script_child == (pid_t)-1) diee("(stage2) fork");
724   if (!script_child) {
725     execlp(interp,
726            interp, script, (char*)0);
727     diee("(stage2) exec interpreter (`%s', for `%s')\n",interp,script);
728   }
729 }
730
731 static void queue_alarm(void) {
732   alarm(check_interval);
733 }
734
735 static void await_something(void) {
736   int r;
737   sigset_t mask;
738   sigemptyset(&mask);
739
740   for (;;) {
741     r = sigsuspend(&mask);
742     assert(r==-1);
743     if (errno != EINTR) diee("(stage2) sigsuspend");
744   }
745 }