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