Gather up another utility.
[u/mdw/catacomb] / pixie.c
diff --git a/pixie.c b/pixie.c
index 5482798..2ce007d 100644 (file)
--- a/pixie.c
+++ b/pixie.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: pixie.c,v 1.9 2001/02/03 16:06:44 mdw Exp $
+ * $Id: pixie.c,v 1.14 2004/04/08 01:36:15 mdw Exp $
  *
  * Passphrase pixie for Catacomb
  *
  * MA 02111-1307, USA.
  */
 
-/*----- Revision history --------------------------------------------------* 
- *
- * $Log: pixie.c,v $
- * Revision 1.9  2001/02/03 16:06:44  mdw
- * Don't set a handler for @SIGINT@ if it's ignored at startup.  Add some
- * error handling for the @select@ loop.
- *
- * Revision 1.8  2001/01/25 22:19:31  mdw
- * Make flags be unsigned.
- *
- * Revision 1.7  2000/12/06 20:33:27  mdw
- * Make flags be macros rather than enumerations, to ensure that they're
- * unsigned.
- *
- * Revision 1.6  2000/10/08 12:06:46  mdw
- * Change size passed to socket function to be a @size_t@.  Insert missing
- * type name for flag declaration.
- *
- * Revision 1.5  2000/07/29 22:05:22  mdw
- * Miscellaneous tidyings:
- *
- *   * Change the timeout to something more appropriate for real use.
- *
- *   * Check assumptions about object types when binding the socket.  In
- *     particular, don't zap the socket if it's really something else.
- *
- *   * In @p_request@, return a failure if the shell command returned
- *     nonzero.  Fix a bug in @p_get@ which incorrectly passes on a success
- *     code when this happens.
- *
- *   * Dispose of the locked memory in client mode to avoid being
- *     antisocial.
- *
- *   * Also in client mode, don't report closure from the server if we're
- *     running noninteractively.
- *
- *   * Insert a missing option letter into the usage string.
- *
- *   * Change to the root directory after forking in daemon mode.
- *
- * Revision 1.4  2000/06/17 11:50:53  mdw
- * New pixie protocol allowing application to request passphrases and send
- * them to the pixie.  Use the secure arena interface for the input
- * buffer.  Extend the input buffer.  Other minor fixes.
- *
- * Revision 1.3  1999/12/22 22:14:40  mdw
- * Only produce initialization message if verbose.
- *
- * Revision 1.2  1999/12/22 22:13:42  mdw
- * Fix bug in passphrase flushing loop.
- *
- * Revision 1.1  1999/12/22 15:58:41  mdw
- * Passphrase pixie support.
- *
- */
-
 /*----- Header files ------------------------------------------------------*/
 
 #include "config.h"
@@ -166,7 +110,7 @@ static void log(const char *p, ...)
     d.len += strftime(d.buf, d.sz, "%Y-%m-%d %H:%M:%S ", tm);
   }
   va_start(ap, p);
-  dstr_vputf(&d, p, ap);
+  dstr_vputf(&d, p, &ap);
   va_end(ap);
 
   if (flags & F_SYSLOG)
@@ -652,7 +596,7 @@ static void pixserv_write(pixserv *px, const char *p, ...)
   va_list ap;
 
   va_start(ap, p);
-  dstr_vputf(&d, p, ap);
+  dstr_vputf(&d, p, &ap);
   write(px->fd, d.buf, d.len);
   va_end(ap);
   dstr_destroy(&d);
@@ -690,6 +634,7 @@ static unsigned long pixserv_timeout(const char *p)
 /* --- @pixserv_line@ --- *
  *
  * Arguments:  @char *s@ = pointer to the line read
+ *             @size_t len@ = length of the line
  *             @void *p@ = pointer to server block
  *
  * Returns:    ---
@@ -697,7 +642,7 @@ static unsigned long pixserv_timeout(const char *p)
  * Use:                Handles a line read from the client.
  */
 
-static void pixserv_line(char *s, void *p)
+static void pixserv_line(char *s, size_t len, void *p)
 {
   pixserv *px = p;
   char *q, *qq;
@@ -892,7 +837,8 @@ static void pixserv_accept(int fd, unsigned mode, void *p)
   if (mode != SEL_READ)
     return;
   if ((nfd = accept(fd, (struct sockaddr *)&sun, &sunsz)) < 0) {
-    if (verbose)
+    if (verbose && errno != EAGAIN && errno != EWOULDBLOCK &&
+       errno != ECONNABORTED && errno != EPROTO && errno != EINTR)
       log("new connection failed: %s", strerror(errno));
     return;
   }
@@ -1081,21 +1027,19 @@ static unsigned c_flags = 0;
 
 /* --- Line handler functions --- */
 
-static void c_uline(char *s, void *p)
+static void c_uline(char *s, size_t len, void *p)
 {
-  size_t sz;
   if (!s) {
     selbuf_destroy(&c_client);
     shutdown(c_server.reader.fd, 1);
     c_flags |= cf_uclose;
   } else {
-    sz = strlen(s);
-    s[sz++] = '\n';
-    write(c_server.reader.fd, s, sz);
+    s[len++] = '\n';
+    write(c_server.reader.fd, s, len);
   }
 }
 
-static void c_sline(char *s, void *p)
+static void c_sline(char *s, size_t len, void *p)
 {
   if (!s) {
     selbuf_destroy(&c_server);
@@ -1104,8 +1048,8 @@ static void c_sline(char *s, void *p)
       selbuf_destroy(&c_client);
     }
     exit(0);
-  } else 
-    puts(s);
+  }
+  puts(s);
 }
 
 /* --- @pix_client@ --- *
@@ -1155,8 +1099,10 @@ static void pix_client(struct sockaddr_un *sun, size_t sz, char *argv[])
 
   /* --- And repeat --- */
 
-  for (;;)
-    sel_select(&sel);
+  for (;;) {
+    if (sel_select(&sel))
+      die(EXIT_FAILURE, "select error: %s", strerror(errno));
+  }
 }
 
 /*----- Main code ---------------------------------------------------------*/
@@ -1438,7 +1384,7 @@ int main(int argc, char *argv[])
     chdir("/");
     setsid();
 
-    if (fork() >= 0)
+    if (fork() != 0)
       _exit(0);
   }