/*
* "Interpreter" that you can put in #! like this
* #!/usr/bin/cgi-fcgi-interp [<options>] <interpreter>
- * #!/usr/bin/cgi-fcgi-interp [<options>],<interpreter>
+ *
+ * Usages:
+ * cgi-fcgi-interp [<option> ..] <interpreter> <script> [<ignored> ...]
+ * cgi-fcgi-interp [<option>,..],<interpreter> <script> [<ignored> ...]
+ * cgi-fcgi-interp '[<option> ..] <interpreter>' <script> [<ignored> ...]
*/
/*
- * cgi-fcgi-interp.[ch] - C helpers common to the whole of chiark-utils
+ * cgi-fcgi-interp.[ch] - Convenience wrapper for cgi-fcgi
*
* Copyright 2016 Ian Jackson
* Copyright 1982,1986,1993 The Regents of the University of California
* The real interpreter to use. Eg "perl". Need not
* be an absolute path; will be fed to execvp.
*
+ * -G<ident-info>
+ * Add <ident-info> to the unique identifying information for
+ * this fcgi program. May be repeated; order is significant.
+ *
+ * -E<ident-info-env-var>
+ * Look <ident-info-env-var> up in the environment and add
+ * <ident-info-env-var>=<value> as if specified with -G. If
+ * the variable is unset in the environment, it is as if
+ * -G<ident-info-env-var> was specified.
+ *
* -g<ident>
- * Use <ident> rather than hex(sha256(<script>))
+ * Use <ident> rather than hex(sha256(<interp>\0<script>\0))
* as the basename of the leafname of the fcgi rendezvous
* socket. If <ident> contains only hex digit characters it
* ought to be no more than 32 characters. <ident> should
* cgi-fcgi-interp automatically expires old sockets, including
* ones where the named script is out of date.
*/
-
/*
* Uses one of two directories
* /var/run/user/<UID>/cgi-fcgi-interp/
* s<ident>
* l<ident> used to lock around garbage collection
*
- * If -M<ident> is not specified then an initial substricg of the
- * lowercase hex of the sha256 of the <script> (ie, our argv[1]) is
+ * If -M<ident> is not specified then an initial substring of the
+ * lowercase hex of the sha256 of <interp>\0<script>\0 is
* used. The substring is chosen so that the whole path is 10 bytes
* shorter than sizeof(sun_path). But always at least 33 characters.
*
* - stat the socket and check its timestamp
* if it is too old, unlink it
* - dup stderr, mark no cloexec
- * - run cgi-fcgi -connect SOCKET \
- * cgi-fcgi-interp \
- * --stage2 <was-stderr> <socket> \
- -c<check-interval> \
- * \
- * <interp> <script>
+ * - set CHIARKUTILS_CGIFCGIINTERP_STAGE2=<stderr-copy-fd>
+ * - run cgi-fcgi -connect SOCKET <script>
*
- * --stage2 does this:
+ * When CHIARKUTILS_CGIFCGIINTERP_STAGE2 is set, --stage2 does this:
* - dup2 <was-stderr> to fd 2
* - open /dev/null and expect fd 1 (and if not, close it)
* - become a new process group
* kill process group (at second iteration)
*/
-#include "common.h"
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <errno.h>
-#include <stdbool.h>
-#include <assert.h>
-#include <limits.h>
-
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/utsname.h>
-#include <sys/socket.h>
-#include <sys/un.h>
-#include <sys/file.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <pwd.h>
-#include <err.h>
-
-#include <nettle/sha.h>
-
-#include "myopt.h"
-
-#define die common_die
-#define diee common_diee
-
-#define MINHEXHASH 33
-
-static const char *interp, *ident;
-static int numservers=4, debugmode, stage2;
-static int check_interval=300;
-
-void diee(const char *m) {
- err(127, "error: %s failed", m);
-}
-
-static void fusagemessage(FILE *f) {
- fprintf(f, "usage: #!/usr/bin/cgi-fcgi-interp [<options>]\n");
-}
-
-void usagemessage(void) { fusagemessage(stderr); }
+#include "prefork.h"
-static void of_help(const struct cmdinfo *ci, const char *val) {
- fusagemessage(stdout);
- if (ferror(stdout)) diee("write usage message to stdout");
- exit(0);
-}
+#define STAGE2_VAR "CHIARKUTILS_CGIFCGIINTERP_STAGE2"
-static void of_iassign(const struct cmdinfo *ci, const char *val) {
- long v;
- char *ep;
- errno= 0; v= strtol(val,&ep,10);
- if (!*val || *ep || errno || v<INT_MIN || v>INT_MAX)
- badusage("bad integer argument `%s' for --%s",val,ci->olong);
- *ci->iassignto = v;
-}
+static const char *stage2;
-#define MAX_OPTS 5
+const char our_name[] = "cgi-fcgi-interp";
-static const struct cmdinfo cmdinfos[]= {
- { "help", 0, .call= of_help },
- { 0, 'g', 1, .sassignto= &ident },
- { 0, 'M', 1, .call=of_iassign, .iassignto= &numservers },
- { 0, 'D', 0, .iassignto= &debugmode, .arg= 1 },
- { 0, 'c', 1, .call=of_iassign, .iassignto= &check_interval },
- { "--stage2",0, 0, .iassignto= &stage2, .arg= 1 },
+const struct cmdinfo cmdinfos[]= {
+ { "help", 0, .call=of_help },
+ { 0, 'g', 1, .sassignto= &ident },
+ { 0, 'G', 1, .call= ident_addstring },
+ { 0, 'E', 1, .call= off_ident_addenv },
+ { 0, 'M', 1, .call=of_iassign, .iassignto= &numservers },
+ { 0, 'D', 0, .iassignto= &debugmode, .arg= 1 },
+ { 0, 'c', 1, .call=of_iassign, .iassignto= &check_interval },
{ 0 }
};
-static uid_t us;
-static const char *run_base, *script, *socket_path;
-
-static bool find_run_base_var_run(void) {
- struct stat stab;
- char *try;
- int r;
-
- try = m_asprintf("%s/%lu", "/var/run/user", us);
- r = lstat(try, &stab);
- if (r<0) {
- if (errno == ENOENT ||
- errno == ENOTDIR ||
- errno == EACCES ||
- errno == EPERM)
- return 0; /* oh well */
- diee("stat /var/run/user/UID");
- }
- if (!S_ISDIR(stab.st_mode)) {
- warnx("%s not a directory, falling back to ~\n", try);
- return 0;
- }
- if (stab.st_uid != us) {
- warnx("%s not owned by uid %lu, falling back to ~\n", try,
- (unsigned long)us);
- return 0;
- }
- if (stab.st_mode & 0077) {
- warnx("%s writeable by group or other, falling back to ~\n", try);
- return 0;
- }
- run_base = m_asprintf("%s/%s", try, "cgi-fcgi-interp");
- return 1;
+void fusagemessage(FILE *f) {
+ fprintf(f, "usage: #!/usr/bin/cgi-fcgi-interp [<options>]\n");
}
-static bool find_run_base_home(void) {
- struct passwd *pw;
- struct utsname ut;
- char *dot, *try;
- int r;
-
- pw = getpwuid(us); if (!pw) diee("getpwent(uid)");
+static int stderr_copy;
- r = uname(&ut); if (r) diee("uname(2)");
- dot = strchr(ut.nodename, '.');
- if (dot) *dot = 0;
- if (sizeof(ut.nodename) > 32)
- ut.nodename[32] = 0;
-
- try = m_asprintf("%s/%s/%s", pw->pw_dir, ".cgi-fcgi-interp", ut.nodename);
- run_base = try;
- return 1;
+static void make_stderr_copy(void) {
+ stderr_copy = dup(2);
+ if (stderr_copy < 0) diee("dup stderr (for copy for stage2)");
}
-static void find_socket_path(void) {
- struct sockaddr_un sun;
+static void prep_stage2(void) {
int r;
-
- us = getuid(); if (us==(uid_t)-1) diee("getuid");
-
- find_run_base_var_run() ||
- find_run_base_home() ||
- (abort(),0);
-
- int maxidentlen = sizeof(sun.sun_path) - strlen(run_base) - 10 - 2;
-
- if (!ident) {
- if (maxidentlen < MINHEXHASH)
- errx(127,"base directory `%s'"
- " leaves only %d characters for id hash"
- " which is too little (<%d)",
- run_base, maxidentlen, MINHEXHASH);
-
- int identlen = maxidentlen > 64 ? 64 : maxidentlen;
- char *hexident = xmalloc(identlen + 2);
- struct sha256_ctx sc;
- unsigned char bbuf[32];
- int i;
-
- sha256_init(&sc);
- sha256_update(&sc,strlen(interp)+1,interp);
- sha256_update(&sc,strlen(script)+1,script);
- sha256_digest(&sc,sizeof(bbuf),bbuf);
-
- for (i=0; i<identlen; i += 2)
- sprintf(hexident+i, "%02x", bbuf[i/2]);
-
- hexident[identlen] = 0;
- ident = hexident;
- }
-
- if (strlen(ident) > maxidentlen)
- errx(127, "base directory `%s' plus ident `%s' too long"
- " (with spare) for socket (max ident %d)\n",
- run_base, ident, maxidentlen);
-
- r = mkdir(run_base, 0700);
- if (r) {
- if (!(errno == EEXIST))
- err(127,"mkdir %s",run_base);
- }
-
- socket_path = m_asprintf("%s/g%s",run_base,ident);
-}
-
-/*
- * Regarding the macro timespeccmp:
- *
- * Copyright (c) 1982, 1986, 1993
- * The Regents of the University of California. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 4. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- *
- * @(#)time.h 8.5 (Berkeley) 5/4/95
- * $FreeBSD: head/sys/sys/time.h 275985 2014-12-21 05:07:11Z imp $
- */
-#ifndef timespeccmp
-#define timespeccmp(tvp, uvp, cmp) \
- (((tvp)->tv_sec == (uvp)->tv_sec) ? \
- ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \
- ((tvp)->tv_sec cmp (uvp)->tv_sec))
-#endif /*timespeccmp*/
-
-
-
-static bool stab_isnewer(const struct stat *a, const struct stat *b) {
-#ifdef st_mtime
- return timespeccmp(&a->st_mtim, &b->st_mtim, >);
-#else
- return a->st_mtime > &b->st_mtime;
-#endif
-}
-
-static bool check_garbage_vs(const struct stat *started) {
- struct stat script_stab;
- struct stat sock_stab;
- int r;
-
- r = lstat(script, &script_stab);
- if (r) err(127,"lstat script (%s)",script);
-
- if (stab_isnewer(&script_stab, &sock_stab))
- return 1;
-
- if (S_ISLNK(script_stab.st_mode)) {
- r = stat(script, &script_stab);
- if (r) err(127,"stat script (%s0",script);
-
- if (stab_isnewer(&script_stab, &sock_stab))
- return 1;
- }
-
- return 0;
+
+ const char *stage2_val = m_asprintf("%d", stderr_copy);
+ r = setenv(STAGE2_VAR, stage2_val, 1);
+ if (r) diee("set %s (to announce to stage2)", STAGE2_VAR);
}
-static bool check_garbage(void) {
- struct stat sock_stab;
- int r;
-
- r = lstat(socket_path, &sock_stab);
- if (r) {
- if ((errno == ENOENT))
- return 0; /* well, no garbage then */
- err(127,"stat socket (%s)",socket_path);
- }
+/* stage2 predeclarations */
+static void record_baseline_time(void);
+static void become_pgrp(void);
+static void setup_handlers(void);
+static void spawn_script(void);
+static void queue_alarm(void);
+static void start_logging(void);
+static void await_something(void);
- return check_garbage_vs(&sock_stab);
-}
-
-static void tidy_garbage(void) {
- /* We lock l<ident> and re-check. The effect of this is that each
- * stale socket is removed only once. So unless multiple updates to
- * the script happen rapidly, we can't be racing with the cgi-fcgi
- * (which is recreating the socket */
- int lockfd = -1;
+int main(int unused_argc, const char *const *argv) {
int r;
- const char *lock_path = m_asprintf("%s/l%s",run_base,ident);
-
- lockfd = open(lock_path, O_CREAT|O_RDWR, 0600);
- if (lockfd<0) err(127,"create lock (%s)", lock_path);
-
- r = flock(lockfd, LOCK_EX);
- if (r) err(127,"lock lock (%s)", lock_path);
-
- if (check_garbage()) {
- r = unlink(socket_path);
- if (r) {
- if (!(errno == ENOENT))
- err(127,"remove out-of-date socket (%s)", socket_path);
- }
- }
-
- r = close(lockfd);
- if (r) errx(127,"close lock (%s)", lock_path);
-}
-
-static void shbang_opts(const char *const **argv_io,
- const struct cmdinfo *cmdinfos) {
- myopt(argv_io, cmdinfos);
-
- interp = *(*argv_io)++;
- if (!interp) errx(127,"need interpreter argument");
-}
+ stage2 = getenv(STAGE2_VAR);
+ if (stage2) {
+ int stderrfd = atoi(stage2);
+ assert(stderrfd>2);
-int main(int argc, const char *const *argv) {
- const char *smashedopt, *us;
-
- us = argv[0];
-
- if (argv>=4 && !strcmp(argv[1],"--stage2")) {
- ++argv;
- stage2 = 1;
-
- int stderrfd = atoi(*++argv);
r = dup2(stderrfd, 2);
assert(r==2);
r = open("/dev/null",O_WRONLY);
- if (r<0) err(127,"open /dev/null as stdout");
+ if (r<0) diee("open /dev/null as stdout");
if (r>=3) close(r);
- else if (r!=) errx(127,"open /dev/null gave bad fd %d",r);
+ else if (r!=1) die("open /dev/null for stdout gave bad fd %d",r);
- sock_path = *++argv;
+ r = close(stderrfd);
+ if (r) diee("close saved stderr fd");
}
- if (argc>=2 &&
- (smashedopt = argv[1]) &&
- smashedopt[0]=='-' &&
- (strchr(smashedopt,' ') || strchr(smashedopt,','))) {
- /* single argument containg all the options and <interp> */
- argv += 2; /* eat argv[0] and smashedopt */
- const char *split_args[MAX_OPTS+1];
- int split_argc = 0;
- split_args[split_argc++] = argv[0];
- for (;;) {
- if (split_argc >= MAX_OPTS) errx(127,"too many options in combined arg");
- split_args[split_argc++] = smashedopt;
- if (smashedopt[0] != '-') /* never true on first iteration */
- break;
- char *delim = strchr(smashedopt,' ');
- if (!delim) delim = strchr(smashedopt,',');
- if (!delim)
- errx(127,"combined arg lacks <interpreter>");
- *delim = 0;
- smashedopt = delim+1;
- }
- assert(split_argc <= MAX_OPTS);
- split_args[split_argc++] = 0;
-
- const char *const *split_argv = split_args;
-
- shbang_opts(&split_argv, cmdinfos);
- /* sets interp */
- if (!split_argv) errx(127,"combined arg too many non-option arguments");
- } else {
- shbang_opts(&argv, cmdinfos);
- }
-
- script = *argv++;
- if (!script) errx(127,"need script argument");
- if (*argv) errx(127,"too many arguments");
+ process_opts(&argv);
if (!stage2) {
if (isgarbage)
tidy_garbage();
+ make_stderr_copy();
+ prep_stage2();
+
execlp("cgi-fcgi",
- "cgi-fcti", "-connect", socket_path,
- us, "--stage2",
- m_asprintf("-c%d", check_interval),
- m_asprintf("%d", copy_stderr), socket_path,
- interp, script,
+ "cgi-fcgi", "-connect", socket_path,
+ script,
+ m_asprintf("%d", numservers),
(char*)0);
- err(127,"exec cgi-fcgi");
+ diee("exec cgi-fcgi");
} else { /*stage2*/
- check_baseline_time();
+ record_baseline_time();
become_pgrp();
setup_handlers();
spawn_script();
queue_alarm();
+ start_logging();
await_something();
abort();
* and the main program has signals blocked except in sigsuspend, so
* we don't need to worry about async-signal-safety, or errno. */
-static struct stab baseline_time;
+static struct stat baseline_time;
static pid_t script_child, stage2_pgrp;
static bool out_of_date;
+static int errpipe;
-void check_baseline_time(void) {
-#ifdef st_mtime
- int r = clock_gettime(CLOCK_REALTIME, &baselime_time.st_mtim);
- if (r) err(127,"(stage2) clock_gettime");
-#else
- baseline_time.st_mtime = time(NULL);
- if (baseline_time.st_mtime == (time_t)-1) err(127,"(stage2) time()");
-#endif
+static void record_baseline_time(void) {
+ stab_mtimenow(&baseline_time);
}
static void become_pgrp(void) {
stage2_pgrp = getpid();
r = setpgid(0,0);
- if (r) err(127,"(stage2) setpgid");
+ if (r) diee("(stage2) setpgid");
+}
+
+static void atexit_handler(void) {
+ int r;
+
+ sighandler_t sigr = signal(SIGTERM,SIG_IGN);
+ if (sigr == SIG_ERR) warninge("(stage2) signal(SIGTERM,SIG_IGN)");
+
+ r = killpg(stage2_pgrp,SIGTERM);
+ if (r) warninge("(stage) killpg failed");
+}
+
+static void alarm_handler(int dummy) {
+ if (out_of_date)
+ /* second timeout */
+ exit(0); /* transfers control to atexit_handler */
+
+ out_of_date = check_garbage_vs(&baseline_time);
+ queue_alarm();
+}
+
+static void child_handler(int dummy) {
+ for (;;) {
+ int status;
+ pid_t got = waitpid(-1, &status, WNOHANG);
+ if (got == (pid_t)-1) diee("(stage2) waitpid");
+ if (got != script_child) {
+ warning("(stage2) waitpid got status %d for unknown child [%lu]",
+ status, (unsigned long)got);
+ continue;
+ }
+ if (WIFEXITED(status)) {
+ int v = WEXITSTATUS(status);
+ if (v) warning("program failed with error exit status %d", v);
+ exit(status);
+ } else if (WIFSIGNALED(status)) {
+ int s = WTERMSIG(status);
+ warning("program died due to fatal signal %s%s",
+ strsignal(s), WCOREDUMP(status) ? " (core dumped" : "");
+ assert(status & 0xff);
+ exit(status & 0xff);
+ } else {
+ die("program failed with crazy wait status %#x", status);
+ }
+ }
+ exit(127);
}
static void setup_handlers(void) {
int r;
r = atexit(atexit_handler);
- if (r) err(127,"(stage2) atexit");
+ if (r) diee("(stage2) atexit");
sigemptyset(&sa.sa_mask);
sigaddset(&sa.sa_mask, SIGALRM);
sa.sa_flags = 0;
r = sigprocmask(SIG_BLOCK, &sa.sa_mask, 0);
- if (r) err(127,"(stage2) sigprocmask(SIG_BLOCK,)");
+ if (r) diee("(stage2) sigprocmask(SIG_BLOCK,)");
sa.sa_handler = alarm_handler;
r = sigaction(SIGALRM, &sa, 0);
- if (r) err(127,"(stage2) sigaction SIGALRM");
+ if (r) diee("(stage2) sigaction SIGALRM");
sa.sa_flags |= SA_NOCLDSTOP;
sa.sa_handler = child_handler;
r = sigaction(SIGCHLD, &sa, 0);
- if (r) err(127,"(stage2) sigaction SIGCHLD");
+ if (r) diee("(stage2) sigaction SIGCHLD");
}
-static void atexit_handler(void) {
+static void spawn_script(void) {
int r;
+ int errpipes[2];
- sighandler_t sigr = signal(SIGTERM,SIG_IGN);
- if (sigr == SIG_ERR) warn("(stage2) signal(SIGTERM,SIG_IGN)");
-
- r = killpg(stage2_pgrp,SIGTERM);
- if (r) warn("(stage) killpg failed");
-}
+ r = pipe(errpipes);
+ if (r) diee("(stage2) pipe");
-static void alarm_handler(int dummy) {
- if (out_of_date)
- /* second timeout */
- exit(0); /* transfers control to atexit_handler */
-
- out_of_date = check_garbage_vs(&baseline_time);
- queue_alarm();
-}
-
-static void spawn_script(void) {
script_child = fork();
- if (script_child == (pid_t)-1) err(127,"(stage2) fork");
+ if (script_child == (pid_t)-1) diee("(stage2) fork");
if (!script_child) {
+ r = close(errpipes[0]);
+ if (r) diee("(stage2 child) close errpipes[0]");
+
+ r = dup2(errpipes[1], 2);
+ if (r != 2) diee("(stage2 child) dup2 stderr");
+
execlp(interp,
interp, script, (char*)0);
- err(127,"(stage2) exec interpreter (`%s', for `%s')\n",interp,script);
+ diee("(stage2) exec interpreter (`%s', for `%s')\n",interp,script);
}
+
+ r = close(errpipes[1]);
+ if (r) diee("(stage2) close errpipes[1]");
+
+ errpipe = errpipes[0];
+ r = fcntl(errpipe, F_SETFL, O_NONBLOCK);
+ if (r) diee("(stage2) set errpipe nonblocking");
}
static void queue_alarm(void) {
alarm(check_interval);
}
+
+static void start_logging(void) {
+ int r;
+
+ openlog(script, LOG_NOWAIT|LOG_PID, LOG_USER);
+ logging = 1;
+ r = dup2(1,2);
+ if (r!=2) diee("dup2 stdout to stderr");
+}
+
+static void errpipe_readable(void) {
+ static char buf[1024];
+ static int pending;
+
+ /* %: does not contain newlines
+ * _: empty (garbage)
+ */
+
+ /* %%%%%%%%%%%__________________ */
+ /* ^ pending */
+
+ for (;;) {
+ int avail = sizeof(buf) - pending;
+ ssize_t got = read(errpipe, buf+pending, avail);
+ if (got==-1) {
+ if (errno==EINTR) continue;
+ else if (errno==EWOULDBLOCK || errno==EAGAIN) return;
+ else diee("(stage2) errpipe read");
+ got = 0;
+ } else if (got==0) {
+ warning("program closed its stderr fd");
+ errpipe = -1;
+ return;
+ }
+ int scanned = pending;
+ pending += got;
+ int eaten = 0;
+ for (;;) {
+ const char *newline = memchr(buf+scanned, '\n', pending-scanned);
+ int printupto, eat;
+ if (newline) {
+ printupto = newline-buf;
+ eat = printupto + 1;
+ } else if (!eaten && pending==sizeof(buf)) { /* overflow */
+ printupto = pending;
+ eat = printupto;
+ } else {
+ break;
+ }
+ syslog(LOG_ERR,"stderr: %.*s", printupto-eaten, buf+eaten);
+ eaten += eat;
+ scanned = eaten;
+ }
+ pending -= eaten;
+ memmove(buf, buf+eaten, pending);
+ }
+}
+
+static void await_something(void) {
+ int r;
+ sigset_t mask;
+ sigemptyset(&mask);
+
+ for (;;) {
+ fd_set rfds;
+ FD_ZERO(&rfds);
+ if (errpipe >= 0)
+ FD_SET(errpipe, &rfds);
+ r = pselect(errpipe+1, &rfds,0,0, 0, &mask);
+ if (r==-1) {
+ if (errno != EINTR) diee("(stage2) sigsuspend");
+ continue;
+ }
+ assert(r>0);
+ assert(FD_ISSET(errpipe, &rfds));
+ errpipe_readable();
+ }
+}