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