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