chiark / gitweb /
fishdescriptor: wip utility, bugfixes
[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] - Convenience wrapper for cgi-fcgi
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 #include <syslog.h>
149         
150 #include <nettle/sha.h>
151
152 #include "myopt.h"
153
154 #define MINHEXHASH 33
155 #define STAGE2_VAR "CHIARKUTILS_CGIFCGIINTERP_STAGE2"
156
157 static const char *interp, *ident;
158 static int numservers=4, debugmode;
159 static int check_interval=300;
160
161 static struct sha256_ctx identsc;
162
163 static bool logging;
164 static const char *stage2;
165
166 static void vmsgcore(int estatus, int errnoval, const char *fmt, va_list al) {
167   int r;
168
169   if (logging) {
170     const char *fmt_use = fmt;
171     char *fmt_free = 0;
172     if (errnoval) {
173       r = asprintf(&fmt_free, "%s: %%m", fmt);
174       if (r) {
175         fmt_free = 0;
176       } else {
177         fmt_use = fmt_free;
178       }
179     }
180     vsyslog(LOG_ERR, fmt_use, al);
181     free(fmt_free);
182   } else {
183     fputs("cgi-fcgi-interp: ",stderr);
184     vfprintf(stderr,fmt,al);
185     if (errnoval!=-1) fprintf(stderr,": %s",strerror(errnoval));
186     fputc('\n',stderr);
187   }
188   if (estatus) exit(estatus);
189 }
190
191 #define DEF_MSG(func, attrs, estatus, errnoval, after)  \
192   static void func(const char *fmt, ...)                \
193     __attribute__((format(printf,1,2))) attrs;          \
194   static void func(const char *fmt, ...) {              \
195     va_list al;                                         \
196     va_start(al,fmt);                                   \
197     vmsgcore(estatus,errnoval,fmt,al);                  \
198     after                                               \
199   }
200
201 DEF_MSG(warninge, /*empty*/, 0, errno, { });
202 DEF_MSG(warning , /*empty*/, 0, 0,     { });
203
204 #define DEF_DIE(func, errnoval) \
205   DEF_MSG(func, __attribute__((noreturn)), 127, errnoval, { abort(); })
206
207 DEF_DIE(diee, errno)
208 DEF_DIE(die,  0)
209
210 void common_diee(const char *m) { diee("%s", m); }
211 void common_die (const char *m) { die ("%s", m); }
212
213 static void fusagemessage(FILE *f) {
214   fprintf(f, "usage: #!/usr/bin/cgi-fcgi-interp [<options>]\n");
215 }
216
217 void usagemessage(void) { fusagemessage(stderr); }
218
219 static void of_help(const struct cmdinfo *ci, const char *val) {
220   fusagemessage(stdout);
221   if (ferror(stdout)) diee("write usage message to stdout");
222   exit(0);
223 }
224
225 static void of_iassign(const struct cmdinfo *ci, const char *val) {
226   long v;
227   char *ep;
228   errno= 0; v= strtol(val,&ep,10);
229   if (!*val || *ep || errno || v<INT_MIN || v>INT_MAX)
230     badusage("bad integer argument `%s' for --%s",val,ci->olong);
231   *ci->iassignto = v;
232 }
233
234 static void ident_addstring(const struct cmdinfo *ci, const char *string) {
235   /* ci may be 0 and is provided so this can be .call */
236   sha256_update(&identsc,strlen(string)+1,string);
237 }
238
239 static void off_ident_addenv(const struct cmdinfo *ci, const char *name) {
240   const char *val = getenv(name);
241   if (val) {
242     sha256_update(&identsc,strlen(name),name); /* no nul */
243     sha256_update(&identsc,1,"=");
244     ident_addstring(0,val);
245   } else {
246     ident_addstring(0,name);
247   }
248 }
249
250 #define MAX_OPTS 5
251
252 static const struct cmdinfo cmdinfos[]= {
253   { "help",   0, .call=of_help                                         },
254   { 0, 'g',   1,                    .sassignto= &ident                 },
255   { 0, 'G',   1, .call= ident_addstring                                },
256   { 0, 'E',   1, .call= off_ident_addenv                               },
257   { 0, 'M',   1, .call=of_iassign,  .iassignto= &numservers            },
258   { 0, 'D',   0,                    .iassignto= &debugmode,    .arg= 1 },
259   { 0, 'c',   1, .call=of_iassign,  .iassignto= &check_interval        },
260   { 0 }
261 };
262
263 static uid_t us;
264 static const char *run_base, *script, *socket_path;
265 static const char *run_base_mkdir_p;
266 static int stderr_copy;
267
268 static bool find_run_base_var_run(void) {
269   struct stat stab;
270   char *try;
271   int r;
272
273   try = m_asprintf("%s/%lu", "/var/run/user", us);
274   r = lstat(try, &stab);
275   if (r<0) {
276     if (errno == ENOENT ||
277         errno == ENOTDIR ||
278         errno == EACCES ||
279         errno == EPERM)
280       return 0; /* oh well */
281     diee("stat /var/run/user/UID");
282   }
283   if (!S_ISDIR(stab.st_mode)) {
284     warning("%s not a directory, falling back to ~\n", try);
285     return 0;
286   }
287   if (stab.st_uid != us) {
288     warning("%s not owned by uid %lu, falling back to ~\n", try,
289             (unsigned long)us);
290     return 0;
291   }
292   if (stab.st_mode & 0077) {
293     warning("%s writeable by group or other, falling back to ~\n", try);
294     return 0;
295   }
296   run_base = m_asprintf("%s/%s", try, "cgi-fcgi-interp");
297   return 1;
298 }
299
300 static bool find_run_base_home(void) {
301   struct passwd *pw;
302   struct utsname ut;
303   char *dot, *try;
304   int r;
305
306   pw = getpwuid(us);  if (!pw) diee("getpwent(uid)");
307
308   r = uname(&ut);   if (r) diee("uname(2)");
309   dot = strchr(ut.nodename, '.');
310   if (dot) *dot = 0;
311   if (sizeof(ut.nodename) > 32)
312     ut.nodename[32] = 0;
313
314   run_base_mkdir_p = m_asprintf("%s/%s", pw->pw_dir, ".cgi-fcgi-interp");
315   try = m_asprintf("%s/%s", run_base_mkdir_p, ut.nodename);
316   run_base = try;
317   return 1;
318 }
319
320 static void find_socket_path(void) {
321   struct sockaddr_un sun;
322   int r;
323
324   us = getuid();  if (us==(uid_t)-1) diee("getuid");
325
326   find_run_base_var_run() ||
327     find_run_base_home() ||
328     (abort(),0);
329
330   int maxidentlen = sizeof(sun.sun_path) - strlen(run_base) - 10 - 2;
331
332   if (!ident) {
333     if (maxidentlen < MINHEXHASH)
334       die("base directory `%s'"
335           " leaves only %d characters for id hash"
336           " which is too little (<%d)",
337           run_base, maxidentlen, MINHEXHASH);
338
339     int identlen = maxidentlen > 64 ? 64 : maxidentlen;
340     char *hexident = xmalloc(identlen + 2);
341     unsigned char bbuf[32];
342     int i;
343
344     ident_addstring(0,interp);
345     ident_addstring(0,script);
346     sha256_digest(&identsc,sizeof(bbuf),bbuf);
347
348     for (i=0; i<identlen; i += 2)
349       sprintf(hexident+i, "%02x", bbuf[i/2]);
350
351     hexident[identlen] = 0;
352     ident = hexident;
353   }
354
355   if (strlen(ident) > maxidentlen)
356     die("base directory `%s' plus ident `%s' too long"
357         " (with spare) for socket (max ident %d)\n",
358         run_base, ident, maxidentlen);
359
360   r = mkdir(run_base, 0700);
361   if (r && errno==ENOENT && run_base_mkdir_p) {
362     r = mkdir(run_base_mkdir_p, 0700);
363     if (r) diee("mkdir %s (since %s was ENOENT)",run_base_mkdir_p,run_base);
364     r = mkdir(run_base, 0700);
365   }
366   if (r) {
367     if (!(errno == EEXIST))
368       diee("mkdir %s",run_base);
369   }
370
371   socket_path = m_asprintf("%s/s%s",run_base,ident);
372 }  
373
374 /*
375  * Regarding the macro timespeccmp:
376  *
377  * Copyright (c) 1982, 1986, 1993
378  *      The Regents of the University of California.  All rights reserved.
379  *
380  * Redistribution and use in source and binary forms, with or without
381  * modification, are permitted provided that the following conditions
382  * are met:
383  * 1. Redistributions of source code must retain the above copyright
384  *    notice, this list of conditions and the following disclaimer.
385  * 2. Redistributions in binary form must reproduce the above copyright
386  *    notice, this list of conditions and the following disclaimer in the
387  *    documentation and/or other materials provided with the distribution.
388  * 4. Neither the name of the University nor the names of its contributors
389  *    may be used to endorse or promote products derived from this software
390  *    without specific prior written permission.
391  *
392  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
393  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
394  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
395  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
396  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
397  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
398  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
399  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
400  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
401  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
402  * SUCH DAMAGE.
403  *
404  *      @(#)time.h      8.5 (Berkeley) 5/4/95
405  * $FreeBSD: head/sys/sys/time.h 275985 2014-12-21 05:07:11Z imp $
406  */
407 #ifndef timespeccmp
408 #define timespeccmp(tvp, uvp, cmp)                                      \
409         (((tvp)->tv_sec == (uvp)->tv_sec) ?                             \
410             ((tvp)->tv_nsec cmp (uvp)->tv_nsec) :                       \
411             ((tvp)->tv_sec cmp (uvp)->tv_sec))
412 #endif /*timespeccmp*/
413
414
415
416 #ifdef st_mtime
417
418 static bool stab_isnewer(const struct stat *a, const struct stat *b) {
419   if (debugmode)
420     fprintf(stderr,"stab_isnewer mtim %lu.%06lu %lu.06%lu\n",
421             (unsigned long)a->st_mtim.tv_sec,
422             (unsigned long)a->st_mtim.tv_nsec,
423             (unsigned long)b->st_mtim.tv_sec,
424             (unsigned long)b->st_mtim.tv_nsec);
425   return timespeccmp(&a->st_mtim, &b->st_mtim, >);
426 }
427
428 static void stab_mtimenow(struct stat *out) {
429   int r = clock_gettime(CLOCK_REALTIME, &out->st_mtim);
430   if (r) diee("(stage2) clock_gettime");
431   if (debugmode)
432     fprintf(stderr,"stab_mtimenow mtim %lu.%06lu\n",
433             (unsigned long)out->st_mtim.tv_sec,
434             (unsigned long)out->st_mtim.tv_nsec);
435 }
436
437 #else /* !defined(st_mtime) */
438
439 static bool stab_isnewer(const struct stat *a, const struct stat *b) {
440   if (debugmode)
441     fprintf(stderr,"stab_isnewer mtime %lu %lu\n",
442             (unsigned long)a->st_mtime,
443             (unsigned long)b->st_mtime);
444   return a->st_mtime > b->st_mtime;
445 }
446
447 static void stab_mtimenow(struct stat *out) {
448   out->st_mtime = time(NULL);
449   if (out->st_mtime == (time_t)-1) diee("(stage2) time()");
450   if (debugmode)
451     fprintf(stderr,"stab_mtimenow mtime %lu\n",
452             (unsigned long)out->st_mtime);
453 }
454
455 #endif /* !defined(st_mtime) */
456
457 static bool check_garbage_vs(const struct stat *started) {
458   struct stat script_stab;
459   int r;
460
461   r = lstat(script, &script_stab);
462   if (r) diee("lstat script (%s)",script);
463
464   if (stab_isnewer(&script_stab, started))
465     return 1;
466
467   if (S_ISLNK(script_stab.st_mode)) {
468     r = stat(script, &script_stab);
469     if (r) diee("stat script (%s0",script);
470
471     if (stab_isnewer(&script_stab, started))
472       return 1;
473   }
474
475   return 0;
476 }
477
478 static bool check_garbage(void) {
479   struct stat sock_stab;
480   int r;
481
482   r = lstat(socket_path, &sock_stab);
483   if (r) {
484     if ((errno == ENOENT))
485       return 0; /* well, no garbage then */
486     diee("stat socket (%s)",socket_path);
487   }
488
489   return check_garbage_vs(&sock_stab);
490 }
491
492 static void tidy_garbage(void) {
493   /* We lock l<ident> and re-check.  The effect of this is that each
494    * stale socket is removed only once.  So unless multiple updates to
495    * the script happen rapidly, we can't be racing with the cgi-fcgi
496    * (which is recreating the socket */
497   int lockfd = -1;
498   int r;
499
500   const char *lock_path = m_asprintf("%s/l%s",run_base,ident);
501
502   lockfd = open(lock_path, O_CREAT|O_RDWR, 0600);
503   if (lockfd<0) diee("create lock (%s)", lock_path);
504
505   r = flock(lockfd, LOCK_EX);
506   if (r) diee("lock lock (%s)", lock_path);
507
508   if (check_garbage()) {
509     r = unlink(socket_path);
510     if (r) {
511       if (!(errno == ENOENT))
512         diee("remove out-of-date socket (%s)", socket_path);
513     }
514   }
515
516   r = close(lockfd);
517   if (r) diee("close lock (%s)", lock_path);
518 }
519
520 static void make_stderr_copy(void) {
521   stderr_copy = dup(2);
522   if (stderr_copy < 0) diee("dup stderr (for copy for stage2)");
523 }
524
525 static void prep_stage2(void) {
526   int r;
527   
528   const char *stage2_val = m_asprintf("%d", stderr_copy);
529   r = setenv(STAGE2_VAR, stage2_val, 1);
530   if (r) diee("set %s (to announce to stage2)", STAGE2_VAR);
531 }
532
533 static void shbang_opts(const char *const **argv_io,
534                         const struct cmdinfo *cmdinfos) {
535   myopt(argv_io, cmdinfos);
536
537   interp = *(*argv_io)++;
538   if (!interp) badusage("need interpreter argument");
539 }
540
541 /* stage2 predeclarations */
542 static void record_baseline_time(void);
543 static void become_pgrp(void);
544 static void setup_handlers(void);
545 static void spawn_script(void);
546 static void queue_alarm(void);
547 static void start_logging(void);
548 static void await_something(void);
549
550 int main(int argc, const char *const *argv) {
551   const char *smashedopt;
552   int r;
553
554   stage2 = getenv(STAGE2_VAR);
555   if (stage2) {
556     int stderrfd = atoi(stage2);
557     assert(stderrfd>2);
558
559     r = dup2(stderrfd, 2);
560     assert(r==2);
561
562     r = open("/dev/null",O_WRONLY);
563     if (r<0) diee("open /dev/null as stdout");
564     if (r>=3) close(r);
565     else if (r!=1) die("open /dev/null for stdout gave bad fd %d",r);
566
567     r = close(stderrfd);
568     if (r) diee("close saved stderr fd");
569   }
570
571   sha256_init(&identsc);
572
573   if (argc>=2 &&
574       (smashedopt = argv[1]) &&
575       smashedopt[0]=='-' &&
576       (strchr(smashedopt,' ') || strchr(smashedopt,','))) {
577     /* single argument containg all the options and <interp> */
578     argv += 2; /* eat argv[0] and smashedopt */
579     const char *split_args[MAX_OPTS+1];
580     int split_argc = 0;
581     split_args[split_argc++] = argv[0];
582     for (;;) {
583       if (split_argc >= MAX_OPTS) die("too many options in combined arg");
584       split_args[split_argc++] = smashedopt;
585       if (smashedopt[0] != '-') /* never true on first iteration */
586         break;
587       char *delim = strchr(smashedopt,' ');
588       if (!delim) delim = strchr(smashedopt,',');
589       if (!delim) badusage("combined arg lacks <interpreter>");
590       *delim = 0;
591       smashedopt = delim+1;
592     }
593     assert(split_argc <= MAX_OPTS);
594     split_args[split_argc++] = 0;
595
596     const char *const *split_argv = split_args;
597
598     shbang_opts(&split_argv, cmdinfos);
599     /* sets interp */
600     if (!split_argv) badusage("combined arg too many non-option arguments");
601   } else {
602     shbang_opts(&argv, cmdinfos);
603   }
604
605   script = *argv++;
606   if (!script) badusage("need script argument");
607
608   if (!stage2) {
609     
610     find_socket_path();
611
612     bool isgarbage = check_garbage();
613
614     if (debugmode) {
615       printf("socket: %s\n",socket_path);
616       printf("interp: %s\n",interp);
617       printf("script: %s\n",script);
618       printf("garbage: %d\n",isgarbage);
619       exit(0);
620     }
621
622     if (isgarbage)
623       tidy_garbage();
624
625     make_stderr_copy();
626     prep_stage2();
627
628     execlp("cgi-fcgi",
629            "cgi-fcgi", "-connect", socket_path,
630            script,
631            m_asprintf("%d", numservers),
632            (char*)0);
633     diee("exec cgi-fcgi");
634     
635   } else { /*stage2*/
636
637     record_baseline_time();
638     become_pgrp();
639     setup_handlers();
640     spawn_script();
641     queue_alarm();
642     start_logging();
643     await_something();
644     abort();
645
646   }
647 }
648
649 /* stage2 */
650
651 /* It is most convenient to handle the recheck timeout, as well as
652  * child death, in signal handlers.  Our signals all block each other,
653  * and the main program has signals blocked except in sigsuspend, so
654  * we don't need to worry about async-signal-safety, or errno. */
655
656 static struct stat baseline_time;
657 static pid_t script_child, stage2_pgrp;
658 static bool out_of_date;
659 static int errpipe;
660
661 static void record_baseline_time(void) {
662   stab_mtimenow(&baseline_time);
663 }
664
665 static void become_pgrp(void) {
666   int r;
667
668   stage2_pgrp = getpid();
669
670   r = setpgid(0,0);
671   if (r) diee("(stage2) setpgid");
672 }
673
674 static void atexit_handler(void) {
675   int r;
676
677   sighandler_t sigr = signal(SIGTERM,SIG_IGN);
678   if (sigr == SIG_ERR) warninge("(stage2) signal(SIGTERM,SIG_IGN)");
679
680   r = killpg(stage2_pgrp,SIGTERM);
681   if (r) warninge("(stage) killpg failed");
682 }
683
684 static void alarm_handler(int dummy) {
685   if (out_of_date)
686     /* second timeout */
687     exit(0); /* transfers control to atexit_handler */
688
689   out_of_date = check_garbage_vs(&baseline_time);
690   queue_alarm();
691 }
692
693 static void child_handler(int dummy) {
694   for (;;) {
695     int status;
696     pid_t got = waitpid(-1, &status, WNOHANG);
697     if (got == (pid_t)-1) diee("(stage2) waitpid");
698     if (got != script_child) {
699       warning("(stage2) waitpid got status %d for unknown child [%lu]",
700               status, (unsigned long)got);
701       continue;
702     }
703     if (WIFEXITED(status)) {
704       int v = WEXITSTATUS(status);
705       if (v) warning("program failed with error exit status %d", v);
706       exit(status);
707     } else if (WIFSIGNALED(status)) {
708       int s = WTERMSIG(status);
709       warning("program died due to fatal signal %s%s",
710               strsignal(s), WCOREDUMP(status) ? " (core dumped" : "");
711       assert(status & 0xff);
712       exit(status & 0xff);
713     } else {
714       die("program failed with crazy wait status %#x", status);
715     }
716   }
717   exit(127);
718 }
719
720 static void setup_handlers(void) {
721   struct sigaction sa;
722   int r;
723
724   r = atexit(atexit_handler);
725   if (r) diee("(stage2) atexit");
726
727   sigemptyset(&sa.sa_mask);
728   sigaddset(&sa.sa_mask, SIGALRM);
729   sigaddset(&sa.sa_mask, SIGCHLD);
730   sa.sa_flags = 0;
731
732   r = sigprocmask(SIG_BLOCK, &sa.sa_mask, 0);
733   if (r) diee("(stage2) sigprocmask(SIG_BLOCK,)");
734
735   sa.sa_handler = alarm_handler;
736   r = sigaction(SIGALRM, &sa, 0);
737   if (r) diee("(stage2) sigaction SIGALRM");
738
739   sa.sa_flags |= SA_NOCLDSTOP;
740   sa.sa_handler = child_handler;
741   r = sigaction(SIGCHLD, &sa, 0);
742   if (r) diee("(stage2) sigaction SIGCHLD");
743 }
744
745 static void spawn_script(void) {
746   int r;
747   int errpipes[2];
748
749   r = pipe(errpipes);
750   if (r) diee("(stage2) pipe");
751
752   script_child = fork();
753   if (script_child == (pid_t)-1) diee("(stage2) fork");
754   if (!script_child) {
755     r = close(errpipes[0]);
756     if (r) diee("(stage2 child) close errpipes[0]");
757
758     r = dup2(errpipes[1], 2);
759     if (r != 2) diee("(stage2 child) dup2 stderr");
760
761     execlp(interp,
762            interp, script, (char*)0);
763     diee("(stage2) exec interpreter (`%s', for `%s')\n",interp,script);
764   }
765
766   r = close(errpipes[1]);
767   if (r) diee("(stage2) close errpipes[1]");
768
769   errpipe = errpipes[0];
770   r = fcntl(errpipe, F_SETFL, O_NONBLOCK);
771   if (r) diee("(stage2) set errpipe nonblocking");
772 }
773
774 static void queue_alarm(void) {
775   alarm(check_interval);
776 }
777
778 static void start_logging(void) {
779   int r;
780
781   openlog(script, LOG_NOWAIT|LOG_PID, LOG_USER);
782   logging = 1;
783   r = dup2(1,2);
784   if (r!=2) diee("dup2 stdout to stderr");
785 }
786
787 static void errpipe_readable(void) {
788   static char buf[1024];
789   static int pending;
790
791   /* %: does not contain newlines
792    * _: empty (garbage)
793    */ 
794
795   /*           %%%%%%%%%%%__________________ */
796   /*                      ^ pending          */
797
798   for (;;) {
799     int avail = sizeof(buf) - pending;
800     ssize_t got = read(errpipe, buf+pending, avail);
801     if (got==-1) {
802       if (errno==EINTR) continue;
803       else if (errno==EWOULDBLOCK || errno==EAGAIN) return;
804       else diee("(stage2) errpipe read");
805       got = 0;
806     } else if (got==0) {
807       warning("program closed its stderr fd");
808       errpipe = -1;
809       return;
810     }
811     int scanned = pending;
812     pending += got;
813     int eaten = 0;
814     for (;;) {
815       const char *newline = memchr(buf+scanned, '\n', pending-scanned);
816       int printupto, eat;
817       if (newline) {
818         printupto = newline-buf;
819         eat = printupto + 1;
820       } else if (!eaten && pending==sizeof(buf)) { /* overflow */
821         printupto = pending;
822         eat = printupto;
823       } else {
824         break;
825       }
826       syslog(LOG_ERR,"stderr: %.*s", printupto-eaten, buf+eaten);
827       eaten += eat;
828       scanned = eaten;
829     }
830     pending -= eaten;
831     memmove(buf, buf+eaten, pending);
832   }
833 }     
834
835 static void await_something(void) {
836   int r;
837   sigset_t mask;
838   sigemptyset(&mask);
839
840   for (;;) {
841     fd_set rfds;
842     FD_ZERO(&rfds);
843     if (errpipe >= 0)
844       FD_SET(errpipe, &rfds);
845     r = pselect(errpipe+1, &rfds,0,0, 0, &mask);
846     if (r==-1) {
847       if (errno != EINTR) diee("(stage2) sigsuspend");
848       continue;
849     }
850     assert(r>0);
851     assert(FD_ISSET(errpipe, &rfds));
852     errpipe_readable();
853   }
854 }