chiark / gitweb /
Server checks itself every hour, and logs with pid.
[userv.git] / client.c
index 7a810b534aa98d42da10dad7135d4b5810a825a2..4fbb710856cfeec82aff689388a35cb2ff493e1c 100644 (file)
--- a/client.c
+++ b/client.c
  * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  */
 
+/*
+ * Here too, we do some horrible asynchronous stuff with signals.
+ *
+ * The following objects &c. are used in signal handlers and so
+ * must be protected by calls to blocksignals if they are used in
+ * the main program:
+ *  stderr
+ *  swfile
+ *
+ * The following objects are used in the main program unprotected
+ * and so must not be used in signal handlers:
+ *  srfile
+ *  fdsetup[].copyfd
+ *  fdsetup[].pipefd
+ *
+ * The following objects/functions are not modified/called after the
+ * asynchronicity starts:
+ *  malloc
+ *  fdsetupsize
+ *  fdsetup[].mods
+ *  fdsetup[].filename
+ *  fdsetup[].oflags
+ *  results of argument parsing
+ *
+ * systemerror, swfile, fdsetup[].catpid and fdsetup[].killed are used
+ * for communication between the main thread and the signal handlers.
+ *
+ * All the signal handlers save errno so that is OK too.
+ */
+
 #include <fcntl.h>
 #include <stdarg.h>
 #include <errno.h>
 #include "common.h"
 #include "version.h"
 
-struct optioninfo;
-
-typedef void optionfunction(const struct optioninfo*, const char *value, char *key);
-
-struct optioninfo {
-  int abbrev;
-  const char *full;
-  int values; /* 0: no value; 1: single value; 2: key and value */
-  optionfunction *fn;
-};
-
 enum fdmodifiervalues {
   fdm_read=       00001,
   fdm_write=      00002,
@@ -76,71 +95,23 @@ struct fdmodifierinfo {
   int oflags;
 };
 
-const struct fdmodifierinfo fdmodifierinfos[]= {
-  { "read",      fdm_read,
-                 fdm_write,
-                 O_RDONLY                                                         },
-  { "write",     fdm_write,
-                 fdm_read,
-                 O_WRONLY                                                         },
-  { "overwrite", fdm_write|fdm_create|fdm_truncate,
-                 fdm_read|fdm_fd|fdm_exclusive,
-                 O_WRONLY|O_CREAT|O_TRUNC                                         },
-  { "create",    fdm_write|fdm_create,
-                 fdm_read|fdm_fd,
-                 O_WRONLY|O_CREAT                                                 },
-  { "creat",     fdm_write|fdm_create,
-                 fdm_read|fdm_fd,
-                 O_WRONLY|O_CREAT                                                 },
-  { "exclusive", fdm_write|fdm_create|fdm_exclusive,
-                 fdm_read|fdm_fd|fdm_truncate,
-                 O_WRONLY|O_CREAT|O_EXCL                                          },
-  { "excl",      fdm_write|fdm_create|fdm_exclusive,
-                 fdm_read|fdm_fd|fdm_truncate,
-                 O_WRONLY|O_CREAT|O_EXCL                                          },
-  { "truncate",  fdm_write|fdm_truncate,
-                 fdm_read|fdm_fd|fdm_exclusive,
-                 O_WRONLY|O_CREAT|O_EXCL                                          },
-  { "trunc",     fdm_write|fdm_truncate,
-                 fdm_read|fdm_fd|fdm_exclusive,
-                 O_WRONLY|O_CREAT|O_EXCL                                          },
-  { "append",    fdm_write|fdm_append,
-                 fdm_read|fdm_fd,
-                 O_WRONLY|O_CREAT|O_APPEND                                        },
-  { "sync",      fdm_write|fdm_sync,
-                 fdm_read|fdm_fd,
-                 O_WRONLY|O_CREAT|O_SYNC                                          },
-  { "wait",      fdm_wait,
-                 fdm_nowait|fdm_close,
-                 0                                                                },
-  { "nowait",    fdm_nowait,
-                 fdm_wait|fdm_close,
-                 0                                                                },
-  { "close",     fdm_close,
-                 fdm_wait|fdm_nowait,
-                 0                                                                },
-  { "fd",        fdm_fd,
-                 fdm_create|fdm_exclusive|fdm_truncate|fdm_append|fdm_sync,
-                 0                                                                },
-  {  0                                                                            }
-};
-
 struct fdsetupstate {
-  const char *filename;
-  int copyfd;
-  int mods, oflags, pipefd, killed;
-  pid_t catpid;
+  const char *filename; /* non-null iff this fd has been specified */
+  int copyfd; /* fd to copy, -1 unless mods & fdm_fd */
+  int mods, oflags, pipefd, killed; /* 0,0,-1,0 unless otherwise set */
+  pid_t catpid; /* -1 indicates no cat process */
 };
 
 enum signalsexitspecials { se_number=-100, se_numbernocore, se_highbit, se_stdout };
-enum overridetypes { ot_none, ot_string, ot_file };
+enum overridetypes { ot_none, ot_string, ot_file, ot_builtin };
 
 struct constkeyvaluepair { const char *key, *value; };
 
+/* Variables from command-line arguments */
 static const char *serviceuser;
-static uid_t serviceuid, myuid;
+static uid_t serviceuid;
 static struct fdsetupstate *fdsetup;
-static int fdsetupsize, builtin;
+static int fdsetupsize;
 static struct constkeyvaluepair *defvararray;
 static int defvaravail, defvarused;
 static unsigned long timeout;
@@ -149,7 +120,18 @@ static int sigpipeok, hidecwd;
 static int overridetype= ot_none;
 static const char *overridevalue, *spoofuser=0;
 
+/* Other state variables */
 static FILE *srfile, *swfile;
+static pid_t mypid;
+static uid_t myuid, spoofuid;
+static gid_t mygid, spoofgid, *gidarray;
+static int ngids;
+static struct opening_msg opening_mbuf;
+static const char *logname;
+static char *cwdbuf;
+static size_t cwdbufsize;
+static char *ovbuf;
+static int ovused, systemerror;
 
 static void blocksignals(int how) {
   sigset_t set;
@@ -168,6 +150,12 @@ static void blocksignals(int how) {
   }
 }
 
+/* Functions which may be called either from signal handlers or from
+ * the main thread.  They block signals in case they are on the main
+ * thread, and may only use signal handler objects.  None of them
+ * return.  If they did they'd have to restore the signal mask.
+ */
+
 static void NONRETURNPRINTFFORMAT(1,2) miscerror(const char *fmt, ...) {
   va_list al;
 
@@ -217,6 +205,33 @@ static void NONRETURNPRINTFFORMAT(1,2) protoerror(const char *fmt, ...) {
   exit(-1);
 }
 
+/*
+ * General-purpose functions; these do nothing special about signals,
+ * except that they can call error-handlers which may block them
+ * to print error messages.
+ */
+
+static void xfread(void *p, size_t sz, FILE *file) {
+  size_t nr;
+  nr= fread(p,1,sz,file);
+  if (nr != sz) protoreaderror(file,"in data");
+}
+
+static void xfwrite(const void *p, size_t sz, FILE *file) {
+  size_t nr;
+  nr= fwrite(p,1,sz,file); if (nr == sz) return;
+  syscallerror("writing to server");
+}
+
+static void xfflush(FILE *file) {
+  if (fflush(file)) syscallerror("flush server socket");
+}
+
+/* Functions which may be called only from the main thread.  These may
+ * use main-thread objects and must block signals before using signal
+ * handler objects.
+ */
+
 #ifdef DEBUG
 static void priv_suspend(void) { }
 static void priv_resume(void) { }
@@ -238,6 +253,51 @@ static void priv_permanentlyrevokesuspended(void) {
 }
 #endif
 
+static void checkmagic(unsigned long was, unsigned long should, const char *when) {
+  if (was != should)
+    protoerror("magic number %s was %08lx, expected %08lx",when,was,should);
+}
+
+static void getprogress(struct progress_msg *progress_r, FILE *file) {
+  int i, c;
+  unsigned long ul;
+
+  for (;;) {
+    xfread(progress_r,sizeof(struct progress_msg),file);
+    checkmagic(progress_r->magic,PROGRESS_MAGIC,"in progress message");
+    switch (progress_r->type) {
+    case pt_failed:
+      blocksignals(SIG_BLOCK);
+      fputs("userv: uservd reports that service failed\n",stderr);
+      exit(-1);
+    case pt_errmsg:
+      blocksignals(SIG_BLOCK);
+      fputs("uservd: ",stderr);
+      if (progress_r->data.errmsg.messagelen>MAX_ERRMSG_STRING)
+       protoerror("stderr message length %d is far too long",
+                  progress_r->data.errmsg.messagelen);
+      for (i=0; i<progress_r->data.errmsg.messagelen; i++) {
+       c= getc(file); if (c==EOF) protoreaderror(file,"in error message");
+       if (isprint(c)) putc(c,stderr);
+       else fprintf(stderr,"\\x%02x",(unsigned char)c);
+      }
+      putc('\n',stderr);
+      if (ferror(stderr)) syscallerror("printing error message");
+      xfread(&ul,sizeof(ul),file);
+      checkmagic(ul,PROGRESS_ERRMSG_END_MAGIC,"after error message");
+      blocksignals(SIG_UNBLOCK);
+      break;
+    default:
+      return;
+    }
+  }
+}
+
+/*
+ * Functions which are called only during setup, before
+ * the signal asynchronicity starts.  They can do anything they like.
+ */
+
 static void *xmalloc(size_t s) {
   void *p;
   p= malloc(s?s:1);
@@ -251,18 +311,6 @@ static void *xrealloc(void *p, size_t s) {
   return p;
 }
 
-static void xfread(void *p, size_t sz, FILE *file) {
-  size_t nr;
-  nr= fread(p,1,sz,file);
-  if (nr != sz) protoreaderror(file,"in data");
-}
-
-static void xfwrite(const void *p, size_t sz, FILE *file) {
-  size_t nr;
-  nr= fwrite(p,1,sz,file); if (nr == sz) return;
-  syscallerror("writing to server");
-}
-
 static void xfwritestring(const char *s, FILE *file) {
   int l;
   l= strlen(s);
@@ -271,12 +319,108 @@ static void xfwritestring(const char *s, FILE *file) {
   xfwrite(s,sizeof(*s)*l,file);
 }
 
-static void xfflush(FILE *file) {
-  if (fflush(file)) syscallerror("flush server socket");
+static void xfwritefds(int modifier, int expected, FILE *file) {
+  int i, fdcount;
+
+  for (i=0, fdcount=0; i<fdsetupsize; i++) {
+    if (!(fdsetup[i].filename && (fdsetup[i].mods & modifier)))
+      continue;
+    xfwrite(&i,sizeof(int),file); fdcount++;
+  }
+  assert(fdcount == expected);
+}
+
+/* Functions which may be called from signal handlers.  These
+ * may use signal-handler objects.  The main program may only
+ * call them with signals blocked, and they may not use any
+ * main-thread objects.
+ */
+
+static void disconnect(void) /* DOES return, unlike in daemon */ {
+  struct event_msg event_mbuf;
+  int r;
+
+  if (swfile) {
+    memset(&event_mbuf,0,sizeof(event_mbuf));
+    event_mbuf.magic= EVENT_MAGIC;
+    event_mbuf.type= et_disconnect;
+    r= fwrite(&event_mbuf,1,sizeof(event_mbuf),swfile);
+    if ((r != sizeof(event_mbuf) || fflush(swfile)) && errno != EPIPE)
+      syscallerror("write to server when disconnecting\n");
+  }
+  systemerror= 1;
+}
+
+static void sighandler_alrm(int ignored) /* DOES return, unlike in daemon */ {
+  int es;
+  es= errno;
+  fputs("userv: timeout\n",stderr);
+  disconnect();
+  errno= es;
+}
+
+static void sighandler_chld(int ignored) /* DOES return, unlike in daemon */ {
+  struct event_msg event_mbuf;
+  pid_t child;
+  int status, fd, r, es;
+
+  es= errno;
+  for (;;) {
+    child= wait3(&status,WNOHANG,0);
+    if (child == 0 || (child == -1 && errno == ECHILD)) break;
+    if (child == -1) syscallerror("wait for child process (in sigchld handler)");
+    for (fd=0; fd<fdsetupsize && fdsetup[fd].catpid != child; fd++);
+    if (fd>=fdsetupsize) continue; /* perhaps the caller gave us children */
+    if ((WIFEXITED(status) && WEXITSTATUS(status)==0) ||
+       (WIFSIGNALED(status) && WTERMSIG(status)==SIGPIPE) ||
+       (fdsetup[fd].killed && WIFSIGNALED(status) && WTERMSIG(status)==SIGKILL)) {
+      if (swfile && fdsetup[fd].mods & fdm_read) {
+       memset(&event_mbuf,0,sizeof(event_mbuf));
+       event_mbuf.magic= EVENT_MAGIC;
+       event_mbuf.type= et_closereadfd;
+       r= fwrite(&event_mbuf,1,sizeof(event_mbuf),swfile);
+       if (r != sizeof(event_mbuf) || fflush(swfile))
+         if (errno != EPIPE) syscallerror("inform service of closed read fd");
+      }
+    } else {
+      if (WIFEXITED(status))
+       fprintf(stderr,"userv: cat for fd %d exited with error exit status %d\n",
+               fd,WEXITSTATUS(status));
+      else if (WIFSIGNALED(status))
+       if (WCOREDUMP(status))
+         fprintf(stderr,"userv: cat for fd %d dumped core due to signal %s (%d)\n",
+                 fd,strsignal(WTERMSIG(status)),WTERMSIG(status));
+       else
+         fprintf(stderr,"userv: cat for fd %d terminated by signal %s (%d)\n",
+                 fd,strsignal(WTERMSIG(status)),WTERMSIG(status));
+      else
+       fprintf(stderr,"userv: cat for fd %d gave unknown wait status %d\n",
+               fd,status);
+      disconnect();
+    }
+    fdsetup[fd].catpid= -1;
+  }
+  errno= es;
 }
 
+/*
+ * Argument parsing.  These functions which are called only during
+ * setup, before the signal asynchronicity starts.
+ */
+
+struct optioninfo;
+
+typedef void optionfunction(const struct optioninfo*, const char *value, char *key);
+
+struct optioninfo {
+  int abbrev;
+  const char *full;
+  int values; /* 0: no value; 1: single value; 2: key and value */
+  optionfunction *fn;
+};
+
 static void usage(void) {
-  if (fprintf(stderr,
+  if (fputs(
     "usage: userv <options> [--] <service-user> <service-name> [<argument> ...]\n"
     "usage: userv <options> -B|--builtin [--] <builtin-service> [<info-argument> ...]\n"
     "options: -f|--file <fd>[<fdmodifiers>]=<filename>\n"
@@ -291,21 +435,71 @@ static void usage(void) {
     "fdmodifiers:            read    write  overwrite    trunc[ate]\n"
     "(separate with commas)  append  sync   excl[usive]  creat[e]  fd\n\n"
     "userv and uservd version " VERSION "; copyright (C)1996-1997 Ian Jackson.\n"
-    "there is NO WARRANTY; type `userv --copyright' for details.\n")
-      == EOF) syscallerror("write usage to stderr");
+    "there is NO WARRANTY; type `userv --copyright' for details.\n",
+            stderr) < 0)
+    syscallerror("write usage to stderr");
 }
 
 static void NONRETURNPRINTFFORMAT(1,2) usageerror(const char *fmt, ...) {
   va_list al;
   va_start(al,fmt);
-  fprintf(stderr,"userv: ");
+  fputs("userv: ",stderr);
   vfprintf(stderr,fmt,al);
-  fprintf(stderr,"\n\n");
+  fputs("\n\n",stderr);
   usage();
   exit(-1);
 }
 
-static void addfdmodifier(struct fdsetupstate *fdsus, int fd, const char *key) {
+static const struct fdmodifierinfo fdmodifierinfos[]= {
+  { "read",      fdm_read,
+                 fdm_write,
+                 O_RDONLY                                                         },
+  { "write",     fdm_write,
+                 fdm_read,
+                 O_WRONLY                                                         },
+  { "overwrite", fdm_write|fdm_create|fdm_truncate,
+                 fdm_read|fdm_fd|fdm_exclusive,
+                 O_WRONLY|O_CREAT|O_TRUNC                                         },
+  { "create",    fdm_write|fdm_create,
+                 fdm_read|fdm_fd,
+                 O_WRONLY|O_CREAT                                                 },
+  { "creat",     fdm_write|fdm_create,
+                 fdm_read|fdm_fd,
+                 O_WRONLY|O_CREAT                                                 },
+  { "exclusive", fdm_write|fdm_create|fdm_exclusive,
+                 fdm_read|fdm_fd|fdm_truncate,
+                 O_WRONLY|O_CREAT|O_EXCL                                          },
+  { "excl",      fdm_write|fdm_create|fdm_exclusive,
+                 fdm_read|fdm_fd|fdm_truncate,
+                 O_WRONLY|O_CREAT|O_EXCL                                          },
+  { "truncate",  fdm_write|fdm_truncate,
+                 fdm_read|fdm_fd|fdm_exclusive,
+                 O_WRONLY|O_CREAT|O_EXCL                                          },
+  { "trunc",     fdm_write|fdm_truncate,
+                 fdm_read|fdm_fd|fdm_exclusive,
+                 O_WRONLY|O_CREAT|O_EXCL                                          },
+  { "append",    fdm_write|fdm_append,
+                 fdm_read|fdm_fd,
+                 O_WRONLY|O_CREAT|O_APPEND                                        },
+  { "sync",      fdm_write|fdm_sync,
+                 fdm_read|fdm_fd,
+                 O_WRONLY|O_CREAT|O_SYNC                                          },
+  { "wait",      fdm_wait,
+                 fdm_nowait|fdm_close,
+                 0                                                                },
+  { "nowait",    fdm_nowait,
+                 fdm_wait|fdm_close,
+                 0                                                                },
+  { "close",     fdm_close,
+                 fdm_wait|fdm_nowait,
+                 0                                                                },
+  { "fd",        fdm_fd,
+                 fdm_create|fdm_exclusive|fdm_truncate|fdm_append|fdm_sync,
+                 0                                                                },
+  {  0                                                                            }
+};
+
+static void addfdmodifier(int fd, const char *key) {
   const struct fdmodifierinfo *fdmip;
   
   if (!*key) return;
@@ -348,11 +542,13 @@ static void of_file(const struct optioninfo *oip, const char *value, char *key)
     fdsetup= xrealloc(fdsetup,sizeof(struct fdsetupstate)*fdsetupsize);
     while (oldarraysize < fdsetupsize) {
       fdsetup[oldarraysize].filename= 0;
+      fdsetup[oldarraysize].pipefd= -1;
       fdsetup[oldarraysize].copyfd= -1;
       fdsetup[oldarraysize].mods= 0;
+      fdsetup[oldarraysize].oflags= 0;
       fdsetup[oldarraysize].catpid= -1;
       fdsetup[oldarraysize].killed= 0;
-      fdsetup[oldarraysize++].filename= 0;
+      fdsetup[oldarraysize].filename= 0;
       oldarraysize++;
     }
   }
@@ -364,15 +560,15 @@ static void of_file(const struct optioninfo *oip, const char *value, char *key)
     key= delim;
     delim= strchr(key,',');
     if (delim) *delim++= 0;
-    addfdmodifier(&fdsetup[fd],fd,key);
+    addfdmodifier(fd,key);
   }
   if (!(fdsetup[fd].mods & (fdm_read|fdm_write))) {
-    if (fd != 1 && fd != 2) {
-      addfdmodifier(&fdsetup[fd],fd,"read");
+    if (fd == 0) {
+      addfdmodifier(fd,"read");
     } else if (fdsetup[fd].mods & fdm_fd) {
-      addfdmodifier(&fdsetup[fd],fd,"write");
+      addfdmodifier(fd,"write");
     } else {
-      addfdmodifier(&fdsetup[fd],fd,"overwrite");
+      addfdmodifier(fd,"overwrite");
     }
   }
   if (fdsetup[fd].mods & fdm_fd) {
@@ -381,13 +577,13 @@ static void of_file(const struct optioninfo *oip, const char *value, char *key)
       copyfd= strtoul(value,&delim,0);
       if (*delim)
        usageerror("value part of argument to --file with fd modifier must be "
-                  "numeric or fd name- `%s' is not recognised",value);
+                  "numeric or fd name - `%s' is not recognised",value);
       else if (copyfd > MAX_ALLOW_FD)
        usageerror("file descriptor %lu named as target of file descriptor redirection"
                   " (for file descriptor %lu) is larger than maximum allowed (%d)",
                   copyfd,fd,MAX_ALLOW_FD);
     }
-    do { r= fstat(copyfd,&stab); } while (r && errno==EINTR);
+    r= fstat(copyfd,&stab);
     if (r) {
       if (oip) syscallerror("check filedescriptor %lu (named as target of file "
                            "descriptor redirection for %lu)",copyfd,fd);
@@ -408,7 +604,7 @@ static void of_fdwait(const struct optioninfo *oip, const char *value, char *key
     ul= strtoul(key,&delim,0);
     if (*delim) usageerror("first part of argument to --fdwait must be "
                           "numeric or fd name - `%s' is not recognised",key);
-    if (ul>INT_MAX) usageerror("first part of argument to --fdwait is far too large");
+    if (ul>MAX_ALLOW_FD) usageerror("first part of argument to --fdwait is too large");
     fd= ul;
   }
   if (fd >= fdsetupsize || !fdsetup[fd].filename)
@@ -442,14 +638,18 @@ static void of_defvar(const struct optioninfo *oip, const char *value, char *key
 
 static void of_timeout(const struct optioninfo *oip, const char *value, char *key) {
   char *endp;
-  timeout= strtoul(value,&endp,0);
+  unsigned long ul;
+  
+  ul= strtoul(value,&endp,0);
   if (*endp) usageerror("timeout value `%s' must be a plain decimal string",value);
-  if (timeout>INT_MAX) usageerror("timeout value %lu too large",timeout);
+  if (ul>INT_MAX) usageerror("timeout value %lu too large",ul);
+  timeout= ul;
 }
 
 static void of_signals(const struct optioninfo *oip, const char *value, char *key) {
   unsigned long numvalue;
   char *endp;
+  
   numvalue= strtoul(value,&endp,0);
   if (*endp) {
     if (!strcmp(value,"number")) signalsexit= se_number;
@@ -472,17 +672,13 @@ static void of_hidecwd(const struct optioninfo *oip, const char *value, char *ke
   hidecwd=1;
 }
 
-static void of_builtin(const struct optioninfo *oip, const char *value, char *key) {
-  builtin=1;
-}
-
 static void of_help(const struct optioninfo *oip, const char *value, char *key) {
   usage();
   exit(0);
 }
 
 static void of_copyright(const struct optioninfo *oip, const char *value, char *key) {
-  if (fprintf(stdout,
+  if (fputs(
 " userv - user service daemon and client; copyright (C)1996-1997 Ian Jackson\n\n"
 " This is free software; you can redistribute it and/or modify it under the\n"
 " terms of the GNU General Public License as published by the Free Software\n"
@@ -495,11 +691,15 @@ static void of_copyright(const struct optioninfo *oip, const char *value, char *
 " You should have received a copy of the GNU General Public License along\n"
 " with userv; if not, write to Ian Jackson <ian@chiark.greenend.org.uk> or\n"
 " to the Free Software Foundation, 59 Temple Place - Suite 330, Boston,\n"
-" MA 02111-1307, USA.\n"
-             ) == EOF) syscallerror("write usage to stderr");
+" MA 02111-1307, USA.\n",
+           stdout) < 0) syscallerror("write usage to stderr");
   exit(0);
 }
 
+static void of_builtin(const struct optioninfo *oip, const char *value, char *key) {
+  overridetype= ot_builtin;
+}
+
 static void of_override(const struct optioninfo *oip, const char *value, char *key) {
   overridetype= ot_string;
   overridevalue= value;
@@ -551,180 +751,41 @@ static void callvalueoption(const struct optioninfo *oip, char *arg) {
   }
 }
 
-static void checkmagic(unsigned long was, unsigned long should, const char *when) {
-  if (was != should)
-    protoerror("magic number %s was %08lx, expected %08lx",when,was,should);
-}
-
-static void getprogress(struct progress_msg *progress_r, FILE *file) {
-  int i, c;
-  unsigned long ul;
-
-  for (;;) {
-    xfread(progress_r,sizeof(struct progress_msg),file);
-    switch (progress_r->type) {
-    case pt_failed:
-      blocksignals(SIG_BLOCK);
-      fputs("userv: uservd reports that service failed\n",stderr);
-      exit(-1);
-    case pt_errmsg:
-      blocksignals(SIG_BLOCK);
-      fputs("uservd: ",stderr);
-      if (progress_r->data.errmsg.messagelen>4096)
-       protoerror("stderr message length %d is far too long",
-                  progress_r->data.errmsg.messagelen);
-      for (i=0; i<progress_r->data.errmsg.messagelen; i++) {
-       c= getc(file); if (c==EOF) protoreaderror(file,"in error message");
-       if (isprint(c)) putc(c,stderr);
-       else fprintf(stderr,"\\x%02x",(unsigned char)c);
-      }
-      putc('\n',stderr);
-      if (ferror(stderr)) syscallerror("printing error message");
-      xfread(&ul,sizeof(ul),file);
-      checkmagic(ul,PROGRESS_ERRMSG_END_MAGIC,"after error message");
-      blocksignals(SIG_UNBLOCK);
-      break;
-    default:
-      return;
-    }
-  }
-}
-
-static void xfwritefds(int modifier, int expected, FILE *file) {
-  int i, fdcount;
-
-  for (i=0, fdcount=0; i<fdsetupsize; i++) {
-    if (!(fdsetup[i].filename && (fdsetup[i].mods & modifier)))
-      continue;
-    xfwrite(&i,sizeof(int),file); fdcount++;
-  }
-  assert(fdcount == expected);
-}
-
-static void disconnect(void) /* DOES return, unlike in daemon */ {
-  struct event_msg event_mbuf;
-
-  if (!swfile) {
-    fputs("userv: failed, after service program terminated\n",stderr);
-    _exit(255);
-  }
-  memset(&event_mbuf,0,sizeof(event_mbuf));
-  event_mbuf.magic= EVENT_MAGIC;
-  event_mbuf.type= et_disconnect;
-  xfwrite(&event_mbuf,sizeof(event_mbuf),swfile);
-  xfflush(swfile);
-}
-
-static void sighandler_alrm(int ignored) /* DOES return, unlike in daemon */ {
-  int es;
-  es= errno;
-  fputs("userv: timeout\n",stderr);
-  disconnect();
-  errno= es;
-}
-
-static void sighandler_chld(int ignored) /* DOES return, unlike in daemon */ {
-  struct event_msg event_mbuf;
-  pid_t child;
-  int status, fd, r, es;
+/*
+ * Main thread main processing functions - in order of execution.
+ */
 
-  es= errno;
-  for (;;) {
-    child= wait3(&status,WNOHANG,0);
-    if (child == 0 || (child == -1 && errno == ECHILD)) break;
-    if (child == -1) syscallerror("wait for child process (in sigchld handler)");
-    for (fd=0; fd<fdsetupsize && fdsetup[fd].catpid != child; fd++);
-    if (fd>=fdsetupsize) continue; /* perhaps the invoker gave us children */
-    if ((WIFEXITED(status) && WEXITSTATUS(status)==0) ||
-       (WIFSIGNALED(status) && WTERMSIG(status)==SIGPIPE) ||
-       (fdsetup[fd].killed && WIFSIGNALED(status) && WTERMSIG(status)==SIGKILL)) {
-      if (swfile && fdsetup[fd].mods & fdm_read) {
-       memset(&event_mbuf,0,sizeof(event_mbuf));
-       event_mbuf.magic= EVENT_MAGIC;
-       event_mbuf.type= et_closereadfd;
-       r= fwrite(&event_mbuf,1,sizeof(event_mbuf),swfile);
-       if (r != sizeof(event_mbuf) || fflush(swfile))
-         if (errno != EPIPE) syscallerror("inform service of closed read fd");
-      }
-    } else {
-      if (WIFEXITED(status))
-       fprintf(stderr,"userv: cat for fd %d exited with error exit status %d\n",
-               fd,WEXITSTATUS(status));
-      else if (WIFSIGNALED(status))
-       if (WCOREDUMP(status))
-         fprintf(stderr,"userv: cat for fd %d dumped core due to signal %s (%d)\n",
-                 fd,strsignal(WTERMSIG(status)),WTERMSIG(status));
-       else
-         fprintf(stderr,"userv: cat for fd %d terminated by signal %s (%d)\n",
-                 fd,strsignal(WTERMSIG(status)),WTERMSIG(status));
-      else
-       fprintf(stderr,"userv: cat for fd %d gave unknown wait status %d\n",
-               fd,status);
-      disconnect();
-    }
-    fdsetup[fd].catpid= -1;
-  }
-  errno= es;
-}
+static void security_init(void) {
+  /* May not open any file descriptors. */
+  
+  mypid= getpid(); if (mypid == (pid_t)-1) syscallerror("getpid");
+  myuid= getuid(); if (myuid == (uid_t)-1) syscallerror("getuid");
+  mygid= getgid(); if (mygid == (gid_t)-1) syscallerror("getgid");
+  ngids= getgroups(0,0); if (ngids == (gid_t)-1) syscallerror("getgroups(0,0)");
+  gidarray= xmalloc(sizeof(gid_t)*ngids);
+  if (getgroups(ngids,gidarray) != ngids) syscallerror("getgroups(ngids,)");
+  
+  priv_suspend();
 
-static void catdup(const char *which, int from, int to) {
-  if (dup2(from,to)<0) {
-    blocksignals(SIG_BLOCK);
-    fprintf(stderr,"userv: %s: cannot dup for %s: %s\n",which,
-           to?"stdout":"stdin", strerror(errno));
-    exit(-1);
-  }
+  if (ngids > MAX_GIDS) miscerror("caller is in far too many gids");
 }
 
-int main(int argc, char *const *argv) {
+static void parse_arguments(int *argcp, char *const **argvp) {
   static char fd0key[]= "stdin,fd,read";
   static char fd1key[]= "stdout,fd,write";
   static char fd2key[]= "stderr,fd,write";
-  static char stderrbuf[BUFSIZ], stdoutbuf[1024];
-  
+
   char *const *argpp;
   char *argp;
   const struct optioninfo *oip;
-  struct sockaddr_un ssockname;
-  int sfd, ngids, i, tempfd, l, c, reading, fd, r, status, ngidssize;
-  sigset_t sset;
-  unsigned long ul;
-  size_t cwdbufsize;
-  char *cwdbuf, **mem;
-  struct opening_msg opening_mbuf;
-  struct request_msg request_mbuf;
-  struct progress_msg progress_mbuf;
-  struct event_msg event_mbuf;
-  struct passwd *pw;
-  struct group *gr;
-  gid_t mygid, spoofgid, *gidarray;
-  uid_t spoofuid;
-  pid_t mypid;
-  const char *logname;
-  FILE *ovfile;
-  char *ovbuf;
-  int ovavail, ovused;
-  char pipepathbuf[PIPEPATHMAXLEN], catnamebuf[sizeof(int)*3+30];
-  struct sigaction sig;
-
-#ifdef NDEBUG
-# error Do not disable assertions in this security-critical code !
-#endif
-
-  mypid= getpid(); if (mypid == (pid_t)-1) syscallerror("getpid");
-  myuid= getuid(); if (myuid == (uid_t)-1) syscallerror("getuid");
-  mygid= getgid(); if (mygid == (gid_t)-1) syscallerror("getgid");
-  ngids= getgroups(0,0); if (ngids == (gid_t)-1) syscallerror("getgroups(0,0)");
-  gidarray= xmalloc(sizeof(gid_t)*ngids);
-  if (getgroups(ngids,gidarray) != ngids) syscallerror("getgroups(ngids,)");
-  priv_suspend();
+  int fd;
 
-  assert(argv[0]);
+  assert((*argvp)[0]);
   of_file(0,"stdin",fd0key);
   of_file(0,"stdout",fd1key);
   of_file(0,"stderr",fd2key);
 
-  for (argpp= argv+1;
+  for (argpp= *argvp+1;
        (argp= *argpp) && *argp == '-' && argp[1];
        argpp++) {
     if (!*++argp) usageerror("unknown option/argument `%s'",*argpp);
@@ -759,15 +820,20 @@ int main(int argc, char *const *argv) {
       }
     }
   }
-  if (builtin) {
+  if (overridetype == ot_builtin) {
     serviceuser= "-";
   } else {
     if (!*argpp) usageerror("no service user given after options");
     serviceuser= *argpp++;
   }
-  if (!*argpp) usageerror(builtin ?
+  if (!*argpp) usageerror(overridetype == ot_builtin ?
                          "no service name given after options and service user" :
                          "no builtin service given after options");
+
+  *argcp-= (argpp-*argvp);
+  *argvp= argpp;
+
+  if (*argcp > MAX_ARGSDEFVAR) usageerror("far too many arguments");
   
   for (fd=0; fd<fdsetupsize; fd++) {
     if (!fdsetup[fd].filename) continue;
@@ -775,16 +841,13 @@ int main(int argc, char *const *argv) {
     assert(fdsetup[fd].mods & (fdm_read|fdm_write));
     fdsetup[fd].mods |= (fdsetup[fd].mods & fdm_read) ? fdm_close : fdm_wait;
   }
+}
 
-  if (setvbuf(stderr,stderrbuf,_IOLBF,sizeof(stderrbuf)))
-    syscallerror("set buffering on stderr");
-  if (setvbuf(stdout,stdoutbuf,_IOFBF,sizeof(stdoutbuf)))
-    syscallerror("set buffering on stdout");
-
-  argc-= (argpp-argv);
-  argv= argpp;
-  if (argc > MAX_ARGSDEFVAR) usageerror("far too many arguments");
-  if (ngids > MAX_GIDS) miscerror("caller is in far too many gids");
+static void determine_users(void) {
+  int ngidssize;
+  char **mem;
+  struct passwd *pw;
+  struct group *gr;
   
   spoofuid= myuid;
   spoofgid= mygid;
@@ -824,37 +887,55 @@ int main(int argc, char *const *argv) {
       for (mem= gr->gr_mem; *mem && strcmp(*mem,logname); mem++);
       if (!*mem) continue;
       if (ngids>=ngidssize) {
+      if (ngids>=MAX_GIDS) miscerror("spoofed user is member of too many groups");
        ngidssize= (ngids+5)<<1;
        gidarray= xrealloc(gidarray,sizeof(gid_t)*ngidssize);
       }
       gidarray[ngids++]= gr->gr_gid;
     }
   }
+}
 
+static void determine_cwd(void) {
   cwdbufsize= 0; cwdbuf= 0;
-  if (!hidecwd) {
-    for (;;) {
-      assert(cwdbufsize < INT_MAX/3);
-      cwdbufsize <<= 1; cwdbufsize+= 100;
-      cwdbuf= xrealloc(cwdbuf,cwdbufsize);
-      cwdbuf[cwdbufsize-1]= 0;
-      if (getcwd(cwdbuf,cwdbufsize-1)) { cwdbufsize= strlen(cwdbuf); break; }
-      if (errno != ERANGE) { cwdbufsize= 0; break; }
-    }
+
+  if (hidecwd) return;
+  
+  for (;;) {
+    if (cwdbufsize > MAX_GENERAL_STRING) { cwdbufsize= 0; free(cwdbuf); break; }
+    cwdbufsize <<= 1; cwdbufsize+= 100;
+    cwdbuf= xrealloc(cwdbuf,cwdbufsize);
+    cwdbuf[cwdbufsize-1]= 0;
+    if (getcwd(cwdbuf,cwdbufsize-1)) { cwdbufsize= strlen(cwdbuf); break; }
+    if (errno != ERANGE) { cwdbufsize= 0; free(cwdbuf); break; }
   }
+}
+
+static void process_override(const char *servicename) {
+  FILE *ovfile;
+  int ovavail, l, c;
 
   switch (overridetype) {
   case ot_none:
     ovused= -1;
     ovbuf= 0;
     break;
+  case ot_builtin:
+    l= strlen(servicename);
+    if (l >= MAX_OVERRIDE_LEN-20)
+      miscerror("builtin service string is too long (%d, max is %d)",
+               l,MAX_OVERRIDE_LEN-21);
+    l+= 20;
+    ovbuf= xmalloc(l);
+    snprintf(ovbuf,l,"execute-builtin %s\n",servicename);
+    ovused= strlen(ovbuf);
+    break;
   case ot_string:
     l= strlen(overridevalue);
     if (l >= MAX_OVERRIDE_LEN)
       miscerror("override string is too long (%d, max is %d)",l,MAX_OVERRIDE_LEN-1);
     ovbuf= xmalloc(l+2);
-    strcpy(ovbuf,overridevalue);
-    strcat(ovbuf,"\n");
+    snprintf(ovbuf,l+2,"%s\n",overridevalue);
     ovused= l+1;
     break;
   case ot_file:
@@ -878,6 +959,19 @@ int main(int argc, char *const *argv) {
   default:
     abort();
   }
+}
+
+static int server_connect(void) {
+  struct sockaddr_un ssockname;
+  int sfd;
+  struct sigaction sig;
+  sigset_t sset;
+
+  sigemptyset(&sset);
+  sigaddset(&sset,SIGCHLD);
+  sigaddset(&sset,SIGALRM);
+  sigaddset(&sset,SIGPIPE);
+  if (sigprocmask(SIG_UNBLOCK,&sset,0)) syscallerror("preliminarily unblock signals");
 
   sig.sa_handler= SIG_IGN;
   sigemptyset(&sig.sa_mask);
@@ -894,10 +988,14 @@ int main(int argc, char *const *argv) {
   while (connect(sfd,(struct sockaddr*)&ssockname,sizeof(ssockname))) {
     if (errno == ECONNREFUSED || errno == ENOENT)
       syscallerror("uservd daemon is not running - service not available");
-    syscallerror("unable to connect to uservd daemon");
+    if (errno != EINTR)
+      syscallerror("unable to connect to uservd daemon: %m");
   }
-  priv_suspend();
 
+  return sfd;
+}
+
+static void server_handshake(int sfd) {
   srfile= fdopen(sfd,"r");
   if (!srfile) syscallerror("turn socket fd into FILE* for read");
   if (setvbuf(srfile,0,_IOFBF,BUFSIZ)) syscallerror("set buffering on socket reads");
@@ -910,26 +1008,39 @@ int main(int argc, char *const *argv) {
   checkmagic(opening_mbuf.magic,OPENING_MAGIC,"in opening message");
   if (memcmp(protocolchecksumversion,opening_mbuf.protocolchecksumversion,PCSUMSIZE))
     protoerror("protocol version checksum mismatch - server not same as client");
+}
 
+static void server_preparepipes(void) {
+  char pipepathbuf[PIPEPATHMAXLEN+2];
+  int fd, tempfd;
+  
   for (fd=0; fd<fdsetupsize; fd++) {
     if (!fdsetup[fd].filename) continue;
+    pipepathbuf[PIPEPATHMAXLEN]= 0;
     sprintf(pipepathbuf, PIPEPATHFORMAT,
             (unsigned long)mypid, (unsigned long)opening_mbuf.serverpid, fd);
+    assert(!pipepathbuf[PIPEPATHMAXLEN]);
     priv_resume();
     if (unlink(pipepathbuf) && errno != ENOENT)
       syscallerror("remove any old pipe `%s'",pipepathbuf);
     if (mkfifo(pipepathbuf,0600)) /* permissions are irrelevant */
       syscallerror("create pipe `%s'",pipepathbuf);
     tempfd= open(pipepathbuf,O_RDWR);
-    if (tempfd == -1) syscallerror("prelim open pipe `%s' for read+write",pipepathbuf);
+    if (tempfd<0) syscallerror("prelim open pipe `%s' for read+write",pipepathbuf);
     assert(fdsetup[fd].mods & (fdm_read|fdm_write));
     fdsetup[fd].pipefd=
       open(pipepathbuf, (fdsetup[fd].mods & fdm_read) ? O_WRONLY : O_RDONLY);
-    if (fdsetup[fd].pipefd == -1) syscallerror("real open pipe `%s'",pipepathbuf);
+    if (fdsetup[fd].pipefd<0) syscallerror("real open pipe `%s'",pipepathbuf);
     if (close(tempfd)) syscallerror("close prelim fd onto pipe `%s'",pipepathbuf);
     priv_suspend();
   }
+}
 
+static void server_sendrequest(int argc, char *const *argv) {
+  struct request_msg request_mbuf;
+  unsigned long ul;
+  int fd, i;
+  
   memset(&request_mbuf,0,sizeof(request_mbuf));
   request_mbuf.magic= REQUEST_MAGIC;
   request_mbuf.clientpid= getpid();
@@ -968,14 +1079,27 @@ int main(int argc, char *const *argv) {
   }
   ul= REQUEST_END_MAGIC; xfwrite(&ul,sizeof(ul),swfile);
   xfflush(swfile);
+}
 
-  priv_permanentlyrevokesuspended(); /* Must not do this before we give our real id */
-
+static void server_awaitconfirm(void) {
+  struct progress_msg progress_mbuf;
+  
   getprogress(&progress_mbuf,srfile);
   if (progress_mbuf.type != pt_ok)
     protoerror("progress message during configuration phase"
               " unexpected type %d",progress_mbuf.type);
+}
 
+static void prepare_asynchsignals(void) {
+  static char stderrbuf[BUFSIZ], stdoutbuf[BUFSIZ];
+  
+  struct sigaction sig;
+
+  if (setvbuf(stderr,stderrbuf,_IOLBF,sizeof(stderrbuf)))
+    syscallerror("set buffering on stderr");
+  if (setvbuf(stdout,stdoutbuf,_IOFBF,sizeof(stdoutbuf)))
+    syscallerror("set buffering on stdout");
+  
   sig.sa_handler= sighandler_chld;
   sigemptyset(&sig.sa_mask);
   sigaddset(&sig.sa_mask,SIGCHLD);
@@ -986,11 +1110,29 @@ int main(int argc, char *const *argv) {
   sig.sa_handler= sighandler_alrm;
   if (sigaction(SIGALRM,&sig,0)) syscallerror("set up sigalrm handler");
 
+  if (!timeout) return;
+  if (alarm(timeout)<0) syscallerror("set up timeout alarm");
+}
+
+static void catdup(const char *which, int from, int to) {
+  if (dup2(from,to)<0) {
+    blocksignals(SIG_BLOCK);
+    fprintf(stderr,"userv: %s: cannot dup for %s: %s\n",which,
+           to?"stdout":"stdin", strerror(errno));
+    exit(-1);
+  }
+}
+
+static void connect_pipes(void) {
+  struct sigaction sig;
+  char catnamebuf[sizeof(int)*3+30];
+  int fd, reading;
+  
   for (fd=0; fd<fdsetupsize; fd++) {
     if (!fdsetup[fd].filename) continue;
     if (!(fdsetup[fd].mods & fdm_fd)) {
       fdsetup[fd].copyfd=
-       open(fdsetup[fd].filename,fdsetup[fd].oflags,0777);
+       open(fdsetup[fd].filename,fdsetup[fd].oflags|O_NOCTTY,0777);
       if (fdsetup[fd].copyfd<0)
        syscallerror("open file `%s' for fd %d",fdsetup[fd].filename,fd);
     }
@@ -1018,9 +1160,10 @@ int main(int argc, char *const *argv) {
       if (close(fdsetup[fd].copyfd)) syscallerror("close real fd for %d",fd);
     if (close(fdsetup[fd].pipefd)) syscallerror("close pipe fd for %d",fd);
   }
+}
 
-  if (timeout)
-    if (alarm(timeout)<0) syscallerror("set up timeout alarm");
+static void server_sendconfirm(void) {
+  struct event_msg event_mbuf;
 
   blocksignals(SIG_BLOCK);
   memset(&event_mbuf,0,sizeof(event_mbuf));
@@ -1029,13 +1172,23 @@ int main(int argc, char *const *argv) {
   xfwrite(&event_mbuf,sizeof(event_mbuf),swfile);
   xfflush(swfile);
   blocksignals(SIG_UNBLOCK);
+}
 
+static int server_awaitcompletion(void) {
+  struct progress_msg progress_mbuf;
+  
   getprogress(&progress_mbuf,srfile);
   if (progress_mbuf.type != pt_terminated)
     protoerror("progress message during execution phase"
               " unexpected type %d",progress_mbuf.type);
 
   swfile= 0;
+  return progress_mbuf.data.terminated.status;
+}
+
+static void dispose_remaining_pipes(void) {
+  sigset_t sset;
+  int fd, r;
 
   blocksignals(SIG_BLOCK);
   for (fd=0; fd<fdsetupsize; fd++) {
@@ -1056,10 +1209,13 @@ int main(int argc, char *const *argv) {
     if (r && errno != EINTR) syscallerror("sigsuspend failed in unexpected way");
     blocksignals(SIG_UNBLOCK);
   }
+}
 
+static void NONRETURNING process_exitstatus(int status) {
   blocksignals(SIG_BLOCK);
 
-  status= progress_mbuf.data.terminated.status;
+  if (systemerror) _exit(255);
+
   if (sigpipeok && signalsexit != se_stdout && WIFSIGNALED(status) &&
       WTERMSIG(status)==SIGPIPE && !WCOREDUMP(status)) status= 0;
   
@@ -1098,6 +1254,37 @@ int main(int argc, char *const *argv) {
     break;
   }
       
-  fprintf(stderr,"unknown wait status %d\n",status);
+  fprintf(stderr,"userv: unknown wait status %d\n",status);
   _exit(-1);
 }
+
+int main(int argc, char *const *argv) {
+  int status, socketfd;
+
+#ifdef NDEBUG
+# error Do not disable assertions in this security-critical code !
+#endif
+
+  security_init();
+  parse_arguments(&argc,&argv);
+  determine_users();
+  determine_cwd();
+  process_override(argv[0]);
+
+  socketfd= server_connect();
+  priv_suspend();
+  server_handshake(socketfd);
+  server_preparepipes();
+  server_sendrequest(argc,argv);
+
+  priv_permanentlyrevokesuspended();
+  
+  server_awaitconfirm();
+  prepare_asynchsignals();
+  connect_pipes();
+  server_sendconfirm();
+  status= server_awaitcompletion();
+  
+  dispose_remaining_pipes();
+  process_exitstatus(status);
+}