chiark / gitweb /
change messages to pipe
[inn-innduct.git] / backends / innduct.c
index 8f4e29e8e2ca91f125ae3a5c00b08f6205d6bfc3..3c746c3ef09cd8632f1a6162e5d474124893e48b 100644 (file)
  *  with GPLv3.  If not then please let me know.  -Ian Jackson.)
  */
 
-/*
- * todo
- *
- *  don't mind reconnecting if we just disconnected due to idle
- *  some weird disconnection event still investigating
- */
-
 /*
  * Newsfeeds file entries should look like this:
  *     host.name.of.site[/exclude,exclude,...]\
@@ -189,6 +182,7 @@ perl -ne 'print if m/-8\<-/..m/-\>8-/; print "\f" if m/-\^L-/' backends/innduct.
 
 #include "inn/list.h"
 #include "inn/innconf.h"
+#include "inn/messages.h"
 
 #include <sys/uio.h>
 #include <sys/types.h>
@@ -340,7 +334,6 @@ static void close_defer(void);
 static void search_backlog_file(void);
 static void preterminate(void);
 static void raise_default(int signo) NORET;
-static char *debug_report_ipf(InputFile *ipf);
 
 static void inputfile_reading_start(InputFile *ipf);
 static void inputfile_reading_stop(InputFile *ipf);
@@ -365,7 +358,7 @@ static oop_rd_call peer_rd_err, peer_rd_ok;
 static const char *sitename, *remote_host;
 static const char *feedfile, *path_run, *path_cli, *path_cli_dir;
 static int quiet_multiple=0;
-static int become_daemon=1, try_filemon=1;
+static int interactive=0, try_filemon=1;
 static int try_stream=1;
 static int port=119;
 static const char *inndconffile;
@@ -511,6 +504,7 @@ struct Conn {
   ISNODE(Conn);
   int fd; /* may be 0, meaning closed (during construction/destruction) */
   oop_read *rd; /* likewise */
+  int oopwriting; /* since on_fd is not idempotent */
   int max_queue, stream;
   const char *quitting;
   int since_activity; /* periods */
@@ -546,15 +540,17 @@ static int until_connect, until_backlog_nextscan;
 static double accept_proportion;
 static int nocheck, nocheck_reported, in_child;
 
-/* for simulation, debugging, etc. */
+/* for logging, simulation, debugging, etc. */
 int simulate_flush= -1;
+int logv_use_syslog;
+static const char *logv_prefix="";
 
 /*========== logging ==========*/
 
 static void logcore(int sysloglevel, const char *fmt, ...) PRINTF(2,3);
 static void logcore(int sysloglevel, const char *fmt, ...) {
   VA;
-  if (become_daemon) {
+  if (logv_use_syslog) {
     vsyslog(sysloglevel,fmt,al);
   } else {
     if (self_pid) fprintf(stderr,"[%lu] ",(unsigned long)self_pid);
@@ -575,13 +571,13 @@ static void logv(int sysloglevel, const char *pfx, int errnoval,
   if (sysloglevel >= LOG_ERR && (errnoval==EACCES || errnoval==EPERM))
     sysloglevel= LOG_ERR; /* run by wrong user, probably */
 
-  logcore(sysloglevel, "<%s>%s: %s%s%s",
-        sitename, pfx, msgbuf,
-        errnoval>=0 ? ": " : "",
-        errnoval>=0 ? strerror(errnoval) : "");
+  logcore(sysloglevel, "%s%s: %s%s%s",
+         logv_prefix, pfx, msgbuf,
+         errnoval>=0 ? ": " : "",
+         errnoval>=0 ? strerror(errnoval) : "");
 }
 
-#define diewrap(fn, pfx, sysloglevel, err, estatus)            \
+#define DEFFATAL(fn, pfx, sysloglevel, err, estatus)           \
   static void fn(const char *fmt, ...) NORET_PRINTF(1,2);      \
   static void fn(const char *fmt, ...) {                       \
     preterminate();                                            \
@@ -590,7 +586,7 @@ static void logv(int sysloglevel, const char *pfx, int errnoval,
     exit(estatus);                                             \
   }
 
-#define logwrap(fn, pfx, sysloglevel, err)             \
+#define DEFLOG(fn, pfx, sysloglevel, err)              \
   static void fn(const char *fmt, ...) PRINTF(1,2);    \
   static void fn(const char *fmt, ...) {               \
     VA;                                                        \
@@ -598,18 +594,31 @@ static void logv(int sysloglevel, const char *pfx, int errnoval,
     va_end(al);                                                \
   }
 
-diewrap(sysdie,   " critical", LOG_CRIT,    errno, 16);
-diewrap(die,      " critical", LOG_CRIT,    -1,    16);
+#define INNLOGSET_DECLARE(fn, pfx, sysloglevel)                              \
+  static void duct_log_##fn(int l, const char *fmt, va_list al, int errval) { \
+    logv(sysloglevel, pfx, errval ? errval : -1, fmt, al);                   \
+  }
+#define INNLOGSET_CALL(fn, pfx, sysloglevel)   \
+  message_handlers_##fn(1, duct_log_##fn);
 
-diewrap(sysfatal, " fatal",    LOG_ERR,     errno, 12);
-diewrap(fatal,    " fatal",    LOG_ERR,     -1,    12);
 
-logwrap(syswarn,  " warning",  LOG_WARNING, errno);
-logwrap(warn,     " warning",  LOG_WARNING, -1);
+static int innduct_fatal_cleanup(void) { return 12; } /* used for libinn die */
 
-logwrap(notice,   " notice",   LOG_NOTICE,  -1);
-logwrap(info,     " info",     LOG_INFO,    -1);
-logwrap(debug,    " debug",    LOG_DEBUG,   -1);
+/* We want to extend the set of logging functions from inn, and we
+ * want to prepend the site name to all our messages. */
+
+DEFFATAL(syscrash,    "critical", LOG_CRIT,    errno, 16);
+DEFFATAL(crash,       "critical", LOG_CRIT,    -1,    16);
+
+#define INNLOGSETS(INNLOGSET)                  \
+  INNLOGSET(die,      "fatal",    LOG_ERR)     \
+  INNLOGSET(warn,     "warning",  LOG_WARNING) \
+  INNLOGSET(notice,   "notice",   LOG_NOTICE)  \
+  INNLOGSET(trace,    "trace",    LOG_NOTICE)
+INNLOGSETS(INNLOGSET_DECLARE)
+
+DEFLOG(info,          "info",     LOG_INFO,    -1)
+DEFLOG(dbg,           "debug",    LOG_DEBUG,   -1)
 
 
 /*========== utility functions etc. ==========*/
@@ -637,7 +646,7 @@ static int close_perhaps(int *fd) {
 }
 static void xclose(int fd, const char *what, const char *what2) {
   int r= close(fd);
-  if (r) sysdie("close %s%s",what,what2?what2:"");
+  if (r) syscrash("close %s%s",what,what2?what2:"");
 }
 static void xclose_perhaps(int *fd, const char *what, const char *what2) {
   if (*fd <= 0) return;
@@ -649,8 +658,8 @@ static pid_t xfork(const char *what) {
   pid_t child;
 
   child= fork();
-  if (child==-1) sysfatal("cannot fork for %s",what);
-  debug("forked %s %ld", what, (unsigned long)child);
+  if (child==-1) sysdie("cannot fork for %s",what);
+  dbg("forked %s %ld", what, (unsigned long)child);
   if (!child) postfork();
   return child;
 }
@@ -687,11 +696,11 @@ static int xwaitpid(pid_t *pid, const char *what) {
   int status;
 
   int r= kill(*pid, SIGKILL);
-  if (r) sysdie("cannot kill %s child", what);
+  if (r) syscrash("cannot kill %s child", what);
 
   pid_t got= waitpid(*pid, &status, 0);
-  if (got==-1) sysdie("cannot reap %s child", what);
-  if (got==0) die("cannot reap %s child", what);
+  if (got==-1) syscrash("cannot reap %s child", what);
+  if (got==0) crash("cannot reap %s child", what);
 
   *pid= 0;
 
@@ -706,18 +715,18 @@ static void *zxmalloc(size_t sz) {
 
 static void xunlink(const char *path, const char *what) {
   int r= unlink(path);
-  if (r) sysdie("can't unlink %s %s", path, what);
+  if (r) syscrash("can't unlink %s %s", path, what);
 }
 
 static time_t xtime(void) {
   time_t now= time(0);
-  if (now==-1) sysdie("time(2) failed");
+  if (now==-1) syscrash("time(2) failed");
   return now;
 }
 
 static void xsigaction(int signo, const struct sigaction *sa) {
   int r= sigaction(signo,sa,0);
-  if (r) sysdie("sigaction failed for \"%s\"", strsignal(signo));
+  if (r) syscrash("sigaction failed for \"%s\"", strsignal(signo));
 }
 
 static void xsigsetdefault(int signo) {
@@ -729,24 +738,24 @@ static void xsigsetdefault(int signo) {
 
 static void xgettimeofday(struct timeval *tv_r) {
   int r= gettimeofday(tv_r,0);
-  if (r) sysdie("gettimeofday(2) failed");
+  if (r) syscrash("gettimeofday(2) failed");
 }
 
 static void xsetnonblock(int fd, int nonblocking) {
   int errnoval= oop_fd_nonblock(fd, nonblocking);
-  if (errnoval) { errno= errnoval; sysdie("setnonblocking"); }
+  if (errnoval) { errno= errnoval; syscrash("setnonblocking"); }
 }
 
 static void check_isreg(const struct stat *stab, const char *path,
                        const char *what) {
   if (!S_ISREG(stab->st_mode))
-    die("%s %s not a plain file (mode 0%lo)",
-       what, path, (unsigned long)stab->st_mode);
+    crash("%s %s not a plain file (mode 0%lo)",
+         what, path, (unsigned long)stab->st_mode);
 }
 
 static void xfstat(int fd, struct stat *stab_r, const char *what) {
   int r= fstat(fd, stab_r);
-  if (r) sysdie("could not fstat %s", what);
+  if (r) syscrash("could not fstat %s", what);
 }
 
 static void xfstat_isreg(int fd, struct stat *stab_r,
@@ -761,7 +770,7 @@ static void xlstat_isreg(const char *path, struct stat *stab,
   int r= lstat(path, stab);
   if (r) {
     if (errno==ENOENT && enoent_r) { *enoent_r=1; return; }
-    sysdie("could not lstat %s %s", what, path);
+    syscrash("could not lstat %s %s", what, path);
   }
   if (enoent_r) *enoent_r= 0;
   check_isreg(stab, path, what);
@@ -1178,7 +1187,7 @@ static void conn_idle_close(Conn *conn, const char *why) {
     if (!todo) {
       conn->quitting= why;
       conn->since_activity= 0;
-      debug("C%d is idle (%s), quitting", conn->fd, why);
+      dbg("C%d is idle (%s), quitting", conn->fd, why);
       break;
     }
   }
@@ -1308,24 +1317,24 @@ static void *connchild_event(oop_source *lp, int fd, oop_event e, void *u) {
     goto x;
   }
 
-#define CHK(field, val)                                                         \
-  if (h->cmsg_##field != val) {                                                 \
-    die("connect: child sent cmsg with cmsg_" #field "=%d, expected %d", \
-       h->cmsg_##field, val);                                           \
-    goto x;                                                             \
+#define CHK(field, val)                                                           \
+  if (h->cmsg_##field != val) {                                                   \
+    crash("connect: child sent cmsg with cmsg_" #field "=%d, expected %d", \
+         h->cmsg_##field, val);                                           \
+    goto x;                                                               \
   }
   CHK(level, SOL_SOCKET);
   CHK(type,  SCM_RIGHTS);
   CHK(len,   CMSG_LEN(sizeof(conn->fd)));
 #undef CHK
 
-  if (CMSG_NXTHDR(&msg,h)) die("connect: child sent many cmsgs");
+  if (CMSG_NXTHDR(&msg,h)) crash("connect: child sent many cmsgs");
 
   memcpy(&conn->fd, CMSG_DATA(h), sizeof(conn->fd));
 
   int status;
   pid_t got= waitpid(connecting_child, &status, 0);
-  if (got==-1) sysdie("connect: real wait for child");
+  if (got==-1) syscrash("connect: real wait for child");
   assert(got == connecting_child);
   connecting_child= 0;
 
@@ -1335,7 +1344,7 @@ static void *connchild_event(oop_source *lp, int fd, oop_event e, void *u) {
   case CONNCHILD_ESTATUS_STREAM:    conn->stream= 1;   break;
   case CONNCHILD_ESTATUS_NOSTREAM:  conn->stream= 0;   break;
   default:
-    fatal("connect: child gave unexpected exit status %d", es);
+    die("connect: child gave unexpected exit status %d", es);
   }
 
   /* Phew! */
@@ -1343,11 +1352,11 @@ static void *connchild_event(oop_source *lp, int fd, oop_event e, void *u) {
 
   loop->on_fd(loop, conn->fd, OOP_EXCEPTION, conn_exception, conn);
   conn->rd= oop_rd_new_fd(loop,conn->fd, 0, 0); /* sets nonblocking, too */
-  if (!conn->fd) die("oop_rd_new_fd conn failed (fd=%d)",conn->fd);
+  if (!conn->fd) crash("oop_rd_new_fd conn failed (fd=%d)",conn->fd);
   int r= oop_rd_read(conn->rd, &peer_rd_style, NNTP_STRLEN,
                     &peer_rd_ok, conn,
                     &peer_rd_err, conn);
-  if (r) sysdie("oop_rd_read for peer (fd=%d)",conn->fd);
+  if (r) syscrash("oop_rd_read for peer (fd=%d)",conn->fd);
 
   LIST_ADDHEAD(conns, conn);
   notice("C%d (now %d) connected %s",
@@ -1402,37 +1411,37 @@ static void connect_start(void) {
        --l;
       }
       if (!buf[0]) {
-       sysfatal("connect: connection attempt failed");
+       sysdie("connect: connection attempt failed");
       } else {
        buf[l]= 0;
-       fatal("connect: %s: %s", stripped ? "rejected" : "failed",
-             sanitise(buf,-1));
+       die("connect: %s: %s", stripped ? "rejected" : "failed",
+           sanitise(buf,-1));
       }
     }
     if (NNTPsendpassword((char*)remote_host, cn_from, cn_to) < 0)
-      sysfatal("connect: authentication failed");
+      sysdie("connect: authentication failed");
     if (try_stream) {
       if (fputs("MODE STREAM\r\n", cn_to)==EOF ||
          fflush(cn_to))
-       sysfatal("connect: could not send MODE STREAM");
+       sysdie("connect: could not send MODE STREAM");
       buf[sizeof(buf)-1]= 0;
       if (!fgets(buf, sizeof(buf)-1, cn_from)) {
        if (ferror(cn_from))
-         sysfatal("connect: could not read response to MODE STREAM");
+         sysdie("connect: could not read response to MODE STREAM");
        else
-         fatal("connect: connection close in response to MODE STREAM");
+         die("connect: connection close in response to MODE STREAM");
       }
       int l= strlen(buf);
       assert(l>=1);
       if (buf[l-1]!='\n')
-       fatal("connect: response to MODE STREAM is too long: %.100s...",
-             sanitise(buf,-1));
+       die("connect: response to MODE STREAM is too long: %.100s...",
+           sanitise(buf,-1));
       l--;  if (l>0 && buf[l-1]=='\r') l--;
       buf[l]= 0;
       char *ep;
       int rcode= strtoul(buf,&ep,10);
       if (ep != &buf[3])
-       fatal("connect: bad response to MODE STREAM: %.50s", sanitise(buf,-1));
+       die("connect: bad response to MODE STREAM: %.50s", sanitise(buf,-1));
 
       switch (rcode) {
       case 203:
@@ -1459,8 +1468,8 @@ static void connect_start(void) {
 
     msg.msg_controllen= cmsg->cmsg_len;
     r= sendmsg(socks[1], &msg, 0);
-    if (r<0) sysdie("sendmsg failed for new connection");
-    if (r!=1) die("sendmsg for new connection gave wrong result %d",r);
+    if (r<0) syscrash("sendmsg failed for new connection");
+    if (r!=1) crash("sendmsg for new connection gave wrong result %d",r);
 
     _exit(exitstatus);
   }
@@ -1547,12 +1556,16 @@ static void conn_maybe_write(Conn *conn)  {
     conn_make_some_xmits(conn);
     if (!conn->xmitu) {
       loop->cancel_fd(loop, conn->fd, OOP_WRITE);
+      conn->oopwriting= 0;
       return;
     }
 
     void *rp= conn_write_some_xmits(conn);
     if (rp==OOP_CONTINUE) {
-      loop->on_fd(loop, conn->fd, OOP_WRITE, conn_writeable, conn);
+      if (!conn->oopwriting) {
+       loop->on_fd(loop, conn->fd, OOP_WRITE, conn_writeable, conn);
+       conn->oopwriting= 1;
+      }
       return;
     } else if (rp==OOP_HALT) {
       return;
@@ -1597,7 +1610,7 @@ static void article_defer(Article *art /* not on a queue */, int whichcount) {
   open_defer();
   if (fprintf(defer, "%s %s\n", TokenToText(art->token), art->messageid) <0
       || fflush(defer))
-    sysfatal("write to defer file %s",path_defer);
+    sysdie("write to defer file %s",path_defer);
   article_done(art, whichcount);
 }
 
@@ -1885,7 +1898,7 @@ static void update_nocheck(int accepted) {
     notice("entering nocheck mode for the first time");
     nocheck_reported= 1;
   } else if (new_nocheck != nocheck) {
-    debug("nocheck mode %s", new_nocheck ? "start" : "stop");
+    dbg("nocheck mode %s", new_nocheck ? "start" : "stop");
   }
   nocheck= new_nocheck;
 }
@@ -1914,9 +1927,9 @@ static void article_done(Article *art, int whichcount) {
     int r= pwrite(ipf->fd, spaces, w, art->offset);
     if (r==-1) {
       if (errno==EINTR) continue;
-      sysdie("failed to blank entry for %s (length %d at offset %lu) in %s",
-            art->messageid, art->blanklen,
-            (unsigned long)art->offset, ipf->path);
+      syscrash("failed to blank entry for %s (length %d at offset %lu) in %s",
+              art->messageid, art->blanklen,
+              (unsigned long)art->offset, ipf->path);
     }
     assert(r>=0 && r<=w);
     art->blanklen -= w;
@@ -2058,7 +2071,7 @@ static InputFile *open_input_file(const char *path) {
   int fd= open(path, O_RDWR);
   if (fd<0) {
     if (errno==ENOENT) return 0;
-    sysfatal("unable to open input file %s", path);
+    sysdie("unable to open input file %s", path);
   }
   assert(fd>0);
 
@@ -2091,8 +2104,8 @@ static void *feedfile_got_bad_data(InputFile *ipf, off_t offset,
   ipf->readcount_err++;
   if (ipf->readcount_err > max_bad_data_initial +
       (ipf->readcount_ok+ipf->readcount_blank) / max_bad_data_ratio)
-    die("too much garbage in input file!  (%d errs, %d ok, %d blank)",
-       ipf->readcount_err, ipf->readcount_ok, ipf->readcount_blank);
+    crash("too much garbage in input file!  (%d errs, %d ok, %d blank)",
+         ipf->readcount_err, ipf->readcount_ok, ipf->readcount_blank);
   return OOP_CONTINUE;
 }
 
@@ -2103,8 +2116,8 @@ static void *feedfile_read_err(oop_source *lp, oop_read *rd,
   InputFile *ipf= ipf_v;
   assert(ev == OOP_RD_SYSTEM);
   errno= errnoval;
-  sysdie("error reading input file: %s, offset %lu",
-        ipf->path, (unsigned long)ipf->offset);
+  syscrash("error reading input file: %s, offset %lu",
+          ipf->path, (unsigned long)ipf->offset);
 }
 
 static void *feedfile_got_article(oop_source *lp, oop_read *rd,
@@ -2166,12 +2179,15 @@ static void *feedfile_got_article(oop_source *lp, oop_read *rd,
   art->offset= old_offset;
   art->blanklen= recsz;
   strcpy(art->messageid, space+1);
-  LIST_ADDTAIL(ipf->queue, art);
 
-  if (ipf->autodefer >= 0)
+  if (ipf->autodefer >= 0) {
     article_autodefer(ipf, art);
-  else if (ipf==backlog_input_file)
-    article_check_expired(art);
+  } else {
+    LIST_ADDTAIL(ipf->queue, art);
+
+    if (ipf==backlog_input_file)
+      article_check_expired(art);
+  }
 
   if (sms==sm_NORMAL && ipf==main_input_file &&
       ipf->offset >= target_max_feedfile_size)
@@ -2272,7 +2288,7 @@ struct Filemon_Perfile {
 
 static void filemon_method_startfile(InputFile *ipf, Filemon_Perfile *pf) {
   int wd= inotify_add_watch(filemon_inotify_fd, ipf->path, IN_MODIFY);
-  if (wd < 0) sysfatal("inotify_add_watch %s", ipf->path);
+  if (wd < 0) sysdie("inotify_add_watch %s", ipf->path);
 
   if (wd >= filemon_inotify_wdmax) {
     int newmax= wd+2;
@@ -2286,17 +2302,17 @@ static void filemon_method_startfile(InputFile *ipf, Filemon_Perfile *pf) {
   assert(!filemon_inotify_wd2ipf[wd]);
   filemon_inotify_wd2ipf[wd]= ipf;
 
-  debug("filemon inotify startfile %p wd=%d wdmax=%d",
-       ipf, wd, filemon_inotify_wdmax);
+  dbg("filemon inotify startfile %p wd=%d wdmax=%d",
+      ipf, wd, filemon_inotify_wdmax);
 
   pf->wd= wd;
 }
 
 static void filemon_method_stopfile(InputFile *ipf, Filemon_Perfile *pf) {
   int wd= pf->wd;
-  debug("filemon inotify stopfile %p wd=%d", ipf, wd);
+  dbg("filemon inotify stopfile %p wd=%d", ipf, wd);
   int r= inotify_rm_watch(filemon_inotify_fd, wd);
-  if (r) sysdie("inotify_rm_watch");
+  if (r) syscrash("inotify_rm_watch");
   filemon_inotify_wd2ipf[wd]= 0;
 }
 
@@ -2307,14 +2323,14 @@ static void *filemon_inotify_readable(oop_source *lp, int fd,
     int r= read(filemon_inotify_fd, &iev, sizeof(iev));
     if (r==-1) {
       if (isewouldblock(errno)) break;
-      sysdie("read from inotify master");
+      syscrash("read from inotify master");
     } else if (r==sizeof(iev)) {
       assert(iev.wd >= 0 && iev.wd < filemon_inotify_wdmax);
     } else {
-      die("inotify read %d bytes wanted struct of %d", r, (int)sizeof(iev));
+      crash("inotify read %d bytes wanted struct of %d", r, (int)sizeof(iev));
     }
     InputFile *ipf= filemon_inotify_wd2ipf[iev.wd];
-    /*debug("filemon inotify readable read %p wd=%d", ipf, iev.wd);*/
+    /*dbg("filemon inotify readable read %p wd=%d", ipf, iev.wd);*/
     tailing_make_readable(ipf);
   }
   return OOP_CONTINUE;
@@ -2329,7 +2345,7 @@ static int filemon_method_init(void) {
   xsetnonblock(filemon_inotify_fd, 1);
   loop->on_fd(loop, filemon_inotify_fd, OOP_READ, filemon_inotify_readable, 0);
 
-  debug("filemon inotify init filemon_inotify_fd=%d", filemon_inotify_fd);
+  dbg("filemon inotify init filemon_inotify_fd=%d", filemon_inotify_fd);
   return 1;
 }
 
@@ -2390,7 +2406,7 @@ static void inputfile_reading_resume(InputFile *ipf) {
 
   int r= oop_rd_read(ipf->rd, &feedfile_rdstyle, MAX_LINE_FEEDFILE,
                     feedfile_got_article,ipf, feedfile_read_err, ipf);
-  if (r) sysdie("unable start reading feedfile %s",ipf->path);
+  if (r) syscrash("unable start reading feedfile %s",ipf->path);
 
   ipf->paused= 0;
 }
@@ -2542,7 +2558,7 @@ static void statemc_lock(void) {
   
   for (;;) {
     lockfd= open(path_lock, O_CREAT|O_RDWR, 0600);
-    if (lockfd<0) sysfatal("open lockfile %s", path_lock);
+    if (lockfd<0) sysdie("open lockfile %s", path_lock);
 
     struct flock fl;
     memset(&fl,0,sizeof(fl));
@@ -2552,9 +2568,9 @@ static void statemc_lock(void) {
     if (r==-1) {
       if (errno==EACCES || isewouldblock(errno)) {
        if (quiet_multiple) exit(0);
-       fatal("another duct holds the lockfile");
+       die("another duct holds the lockfile");
       }
-      sysfatal("fcntl F_SETLK lockfile %s", path_lock);
+      sysdie("fcntl F_SETLK lockfile %s", path_lock);
     }
 
     xfstat_isreg(lockfd, &stabf, path_lock, "lockfile");
@@ -2568,18 +2584,18 @@ static void statemc_lock(void) {
   }
 
   FILE *lockfile= fdopen(lockfd, "w");
-  if (!lockfile) sysdie("fdopen lockfile");
+  if (!lockfile) syscrash("fdopen lockfile");
 
   int r= ftruncate(lockfd, 0);
-  if (r) sysdie("truncate lockfile to write new info");
+  if (r) syscrash("truncate lockfile to write new info");
 
   if (fprintf(lockfile, "pid %ld\nsite %s\nfeedfile %s\nfqdn %s\n",
              (unsigned long)self_pid,
              sitename, feedfile, remote_host) == EOF ||
       fflush(lockfile))
-    sysfatal("write info to lockfile %s", path_lock);
+    sysdie("write info to lockfile %s", path_lock);
 
-  debug("startup: locked");
+  dbg("startup: locked");
 }
 
 static void statemc_init(void) {
@@ -2590,9 +2606,9 @@ static void statemc_init(void) {
   int defer_noent;
   xlstat_isreg(path_defer, &stabdefer, &defer_noent, "defer file");
   if (defer_noent) {
-    debug("startup: ductdefer ENOENT");
+    dbg("startup: ductdefer ENOENT");
   } else {
-    debug("startup: ductdefer nlink=%ld", (long)stabdefer.st_nlink);
+    dbg("startup: ductdefer nlink=%ld", (long)stabdefer.st_nlink);
     switch (stabdefer.st_nlink==1) {
     case 1:
       open_defer(); /* so that we will later close it and rename it */
@@ -2602,8 +2618,8 @@ static void statemc_init(void) {
              " (presumably hardlink to backlog file)");
       break;
     default:
-      die("defer file %s has unexpected link count %d",
-         path_defer, stabdefer.st_nlink);
+      crash("defer file %s has unexpected link count %d",
+           path_defer, stabdefer.st_nlink);
     }
   }
 
@@ -2616,29 +2632,29 @@ static void statemc_init(void) {
   xlstat_isreg(feedfile, &stab_f, &noent_f, "feedfile");
 
   if (!noent_f && file_d && samefile(&stab_f, &stab_d)) {
-    debug("startup: F==D => Hardlinked");
+    dbg("startup: F==D => Hardlinked");
     xunlink(feedfile, "feed file (during startup)"); /* => Moved */
     noent_f= 1;
   }
 
   if (noent_f) {
-    debug("startup: F ENOENT => Moved");
+    dbg("startup: F ENOENT => Moved");
     if (file_d) startup_set_input_file(file_d);
     spawn_inndcomm_flush("feedfile missing at startup");
     /* => Flushing, sms:=FLUSHING */
   } else {
     if (file_d) {
-      debug("startup: F!=D => Separated");
+      dbg("startup: F!=D => Separated");
       startup_set_input_file(file_d);
       flushing_input_file= main_input_file;
       main_input_file= open_input_file(feedfile);
-      if (!main_input_file) die("feedfile vanished during startup");
+      if (!main_input_file) crash("feedfile vanished during startup");
       SMS(SEPARATED, max_separated_periods,
          "found both old and current feed files");
     } else {
-      debug("startup: F exists, D ENOENT => Normal");
+      dbg("startup: F exists, D ENOENT => Normal");
       InputFile *file_f= open_input_file(feedfile);
-      if (!file_f) die("feed file vanished during startup");
+      if (!file_f) crash("feed file vanished during startup");
       startup_set_input_file(file_f);
       SMS(NORMAL, spontaneous_flush_periods, "normal startup");
     }
@@ -2648,15 +2664,15 @@ static void statemc_init(void) {
 static void statemc_start_flush(const char *why) { /* Normal => Flushing */
   assert(sms == sm_NORMAL);
 
-  debug("starting flush (%s) (%lu >?= %lu) (%d)",
+  dbg("starting flush (%s) (%lu >?= %lu) (%d)",
        why,
        (unsigned long)(main_input_file ? main_input_file->offset : 0),
        (unsigned long)target_max_feedfile_size,
        until_flush);
 
   int r= link(feedfile, path_flushing);
-  if (r) sysfatal("link feedfile %s to flushing file %s",
-                 feedfile, path_flushing);
+  if (r) sysdie("link feedfile %s to flushing file %s",
+               feedfile, path_flushing);
   /* => Hardlinked */
 
   xunlink(feedfile, "old feedfile link");
@@ -2757,7 +2773,7 @@ static void statemc_check_backlog_done(void) {
   close_input_file(ipf);
   if (unlink(ipf->path)) {
     if (errno != ENOENT)
-      sysdie("could not unlink processed backlog file %s", ipf->path);
+      syscrash("could not unlink processed backlog file %s", ipf->path);
     warn("backlog file %s vanished while we were reading it"
         " so we couldn't remove it (but it's done now, anyway)",
         ipf->path);
@@ -2796,7 +2812,8 @@ static void statemc_check_flushing_done(void) {
 
 static void *statemc_check_input_done(oop_source *lp, struct timeval now,
                                      void *u) {
-  assert(!inputfile_is_done(main_input_file));
+  /* main input file may be idle but if so that's because
+   * we haven't got to it yet, but that doesn't mean it's really done */
   statemc_check_flushing_done();
   statemc_check_backlog_done();
   return OOP_CONTINUE;
@@ -2839,14 +2856,14 @@ static void open_defer(void) {
   if (defer) return;
 
   defer= fopen(path_defer, "a+");
-  if (!defer) sysfatal("could not open defer file %s", path_defer);
+  if (!defer) sysdie("could not open defer file %s", path_defer);
 
   /* truncate away any half-written records */
 
   xfstat_isreg(fileno(defer), &stab, path_defer, "newly opened defer file");
 
   if (stab.st_size > LONG_MAX)
-    die("defer file %s size is far too large", path_defer);
+    crash("defer file %s size is far too large", path_defer);
 
   if (!stab.st_size)
     return;
@@ -2856,14 +2873,14 @@ static void open_defer(void) {
   for (;;) {
     if (!truncto) break; /* was only (if anything) one half-truncated record */
     if (fseek(defer, truncto-1, SEEK_SET) < 0)
-      sysdie("seek in defer file %s while truncating partial", path_defer);
+      syscrash("seek in defer file %s while truncating partial", path_defer);
 
     int r= getc(defer);
     if (r==EOF) {
       if (ferror(defer))
-       sysdie("failed read from defer file %s", path_defer);
+       syscrash("failed read from defer file %s", path_defer);
       else
-       die("defer file %s shrank while we were checking it!", path_defer);
+       crash("defer file %s shrank while we were checking it!", path_defer);
     }
     if (r=='\n') break;
     truncto--;
@@ -2875,16 +2892,16 @@ static void open_defer(void) {
         path_defer, orgsize - truncto, orgsize, truncto);
 
     if (fflush(defer))
-      sysfatal("could not flush defer file %s", path_defer);
+      sysdie("could not flush defer file %s", path_defer);
     if (ftruncate(fileno(defer), truncto))
-      sysdie("could not truncate defer file %s", path_defer);
+      syscrash("could not truncate defer file %s", path_defer);
 
   } else {
     info("continuing existing defer file %s (%ld bytes)",
         path_defer, orgsize);
   }
   if (fseek(defer, truncto, SEEK_SET))
-    sysdie("could not seek to new end of defer file %s", path_defer);
+    syscrash("could not seek to new end of defer file %s", path_defer);
 }
 
 static void close_defer(void) {
@@ -2894,7 +2911,7 @@ static void close_defer(void) {
   struct stat stab;
   xfstat_isreg(fileno(defer), &stab, path_defer, "defer file");
 
-  if (fclose(defer)) sysfatal("could not close defer file %s", path_defer);
+  if (fclose(defer)) sysdie("could not close defer file %s", path_defer);
   defer= 0;
 
   time_t now= xtime();
@@ -2903,11 +2920,11 @@ static void close_defer(void) {
                           (unsigned long)now,
                           (unsigned long)stab.st_ino);
   if (link(path_defer, backlog))
-    sysfatal("could not install defer file %s as backlog file %s",
+    sysdie("could not install defer file %s as backlog file %s",
           path_defer, backlog);
   if (unlink(path_defer))
-    sysdie("could not unlink old defer link %s to backlog file %s",
-          path_defer, backlog);
+    syscrash("could not unlink old defer link %s to backlog file %s",
+            path_defer, backlog);
 
   free(backlog);
 
@@ -2939,15 +2956,15 @@ static void search_backlog_file(void) {
 
   switch (r) {
   case GLOB_ABORTED:
-    sysfatal("failed to expand backlog pattern %s", globpat_backlog);
+    sysdie("failed to expand backlog pattern %s", globpat_backlog);
   case GLOB_NOSPACE:
-    fatal("out of memory expanding backlog pattern %s", globpat_backlog);
+    die("out of memory expanding backlog pattern %s", globpat_backlog);
   case 0:
     for (i=0; i<gl.gl_pathc; i++) {
       const char *path= gl.gl_pathv[i];
 
       if (strchr(path,'#') || strchr(path,'~')) {
-       debug("backlog file search skipping %s", path);
+       dbg("backlog file search skipping %s", path);
        continue;
       }
       r= stat(path, &stab);
@@ -2967,12 +2984,12 @@ static void search_backlog_file(void) {
   case GLOB_NOMATCH: /* fall through */
     break;
   default:
-    sysdie("glob expansion of backlog pattern %s gave unexpected"
-          " nonzero (error?) return value %d", globpat_backlog, r);
+    syscrash("glob expansion of backlog pattern %s gave unexpected"
+            " nonzero (error?) return value %d", globpat_backlog, r);
   }
 
   if (!oldest_path) {
-    debug("backlog scan: none");
+    dbg("backlog scan: none");
 
     if (sms==sm_DROPPED) {
       preterminate();
@@ -2994,7 +3011,7 @@ static void search_backlog_file(void) {
   long age_deficiency= (backlog_retry_minperiods * period_seconds) - age;
 
   if (age_deficiency <= 0) {
-    debug("backlog scan: found age=%f deficiency=%ld oldest=%s",
+    dbg("backlog scan: found age=%f deficiency=%ld oldest=%s",
          age, age_deficiency, oldest_path);
 
     backlog_input_file= open_input_file(oldest_path);
@@ -3014,7 +3031,7 @@ static void search_backlog_file(void) {
       until_backlog_nextscan > backlog_spontrescan_periods)
     until_backlog_nextscan= backlog_spontrescan_periods;
 
-  debug("backlog scan: young age=%f deficiency=%ld nextscan=%d oldest=%s",
+  dbg("backlog scan: young age=%f deficiency=%ld nextscan=%d oldest=%s",
        age, age_deficiency, until_backlog_nextscan, oldest_path);
 
  xfree:
@@ -3046,8 +3063,9 @@ static void *sigarrived_event(oop_source *lp, int fd, oop_event e, void *u) {
   assert(fd=signal_self_pipe[0]);
   char buf[PIPE_BUF];
   int r= read(signal_self_pipe[0], buf, sizeof(buf));
-  if (r<0 && !isewouldblock(errno)) sysdie("failed to read signal self pipe");
-  if (r==0) die("eof on signal self pipe");
+  if (r<0 && !isewouldblock(errno))
+    syscrash("failed to read signal self pipe");
+  if (r==0) crash("eof on signal self pipe");
   if (terminate_sig_flag) {
     preterminate();
     notice("terminating (%s)", strsignal(terminate_sig_flag));
@@ -3071,9 +3089,9 @@ static void sigarrived_handler(int signum) {
 
 static void init_signals(void) {
   if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
-    sysdie("could not ignore SIGPIPE");
+    syscrash("could not ignore SIGPIPE");
 
-  if (pipe(signal_self_pipe)) sysfatal("create self-pipe for signals");
+  if (pipe(signal_self_pipe)) sysdie("create self-pipe for signals");
 
   xsetnonblock(signal_self_pipe[0],1);
   xsetnonblock(signal_self_pipe[1],1);
@@ -3138,9 +3156,9 @@ static void *inndcomm_event(oop_source *lp, int fd, oop_event e, void *u) {
 
       main_input_file= open_input_file(feedfile);
       if (!main_input_file)
-       die("flush succeeded but feedfile %s does not exist!"
-           " (this probably means feedfile does not correspond"
-           " to site %s in newsfeeds)", feedfile, sitename);
+       crash("flush succeeded but feedfile %s does not exist!"
+             " (this probably means feedfile does not correspond"
+             " to site %s in newsfeeds)", feedfile, sitename);
 
       if (flushing_input_file) {
        SMS(SEPARATED, max_separated_periods, "flush complete");
@@ -3181,7 +3199,7 @@ void spawn_inndcomm_flush(const char *why) { /* Moved => Flushing */
   assert(!inndcomm_child);
   assert(!inndcomm_sentinel_fd);
 
-  if (pipe(pipefds)) sysfatal("create pipe for inndcomm child sentinel");
+  if (pipe(pipefds)) sysdie("create pipe for inndcomm child sentinel");
 
   inndcomm_child= xfork("inndcomm child");
 
@@ -3229,7 +3247,7 @@ static void postfork_inputfile(InputFile *ipf) {
 static void postfork_stdio(FILE *f, const char *what, const char *what2) {
   /* we have no stdio streams that are buffered long-term */
   if (!f) return;
-  if (fclose(f)) sysdie("(in child) close %s%s", what, what2?what2:0);
+  if (fclose(f)) syscrash("(in child) close %s%s", what, what2?what2:0);
 }
 
 static void postfork(void) {
@@ -3289,7 +3307,7 @@ static void filepoll(void) {
   tailing_make_readable(flushing_input_file);
 }
 
-static char *debug_report_ipf(InputFile *ipf) {
+static char *dbg_report_ipf(InputFile *ipf) {
   if (!ipf) return xasprintf("none");
 
   const char *slash= strrchr(ipf->path,'/');
@@ -3305,19 +3323,19 @@ static char *debug_report_ipf(InputFile *ipf) {
 }
 
 static void period(void) {
-  char *dipf_main=     debug_report_ipf(main_input_file);
-  char *dipf_flushing= debug_report_ipf(flushing_input_file);
-  char *dipf_backlog=  debug_report_ipf(backlog_input_file);
-
-  debug("PERIOD"
-       " sms=%s[%d] conns=%d until_connect=%d"
-       " input_files main:%s flushing:%s backlog:%s[%d]"
-       " children connecting=%ld inndcomm=%ld lowvol_total=%d"
-       ,
-       sms_names[sms], until_flush, conns.count, until_connect,
-       dipf_main, dipf_flushing, dipf_backlog, until_backlog_nextscan,
-       (long)connecting_child, (long)inndcomm_child, lowvol_total
-       );
+  char *dipf_main=     dbg_report_ipf(main_input_file);
+  char *dipf_flushing= dbg_report_ipf(flushing_input_file);
+  char *dipf_backlog=  dbg_report_ipf(backlog_input_file);
+
+  dbg("PERIOD"
+      " sms=%s[%d] conns=%d until_connect=%d"
+      " input_files main:%s flushing:%s backlog:%s[%d]"
+      " children connecting=%ld inndcomm=%ld lowvol_total=%d"
+      ,
+      sms_names[sms], until_flush, conns.count, until_connect,
+      dipf_main, dipf_flushing, dipf_backlog, until_backlog_nextscan,
+      (long)connecting_child, (long)inndcomm_child, lowvol_total
+      );
 
   free(dipf_main);
   free(dipf_flushing);
@@ -3355,7 +3373,7 @@ static void dump_article_list(FILE *f, const CliCommand *c,
   
 static void dump_input_file(FILE *f, const CliCommand *c,
                            InputFile *ipf, const char *wh) {
-  char *dipf= debug_report_ipf(ipf);
+  char *dipf= dbg_report_ipf(ipf);
   fprintf(f,"input %s %s", wh, dipf);
   free(dipf);
   
@@ -3481,8 +3499,8 @@ static void vbadusage(const char *fmt, va_list al) {
   fprintf(stderr, "bad usage: %s\n"
          "say --help for help, or read the manpage\n",
          m);
-  if (become_daemon)
-    syslog(LOG_CRIT,"innduct: invoked with bad usage: %s",m);
+  if (interactive < 2)
+    syslog(LOG_ERR,"innduct: invoked with bad usage: %s",m);
   exit(8);
 }
 
@@ -3641,7 +3659,8 @@ static void help(const Option *o, const char *val);
 static const Option innduct_options[]= {
 {'f',"feedfile",         "F",     &feedfile,                 op_string      },
 {'q',"quiet-multiple",   0,       &quiet_multiple,           op_setint, 1   },
-{0,"no-daemon",          0,       &become_daemon,            op_setint, 0   },
+{0,"no-daemon",          0,       &interactive,              op_setint, 1   },
+{0,"interactive",        0,       &interactive,              op_setint, 2   },
 {0,"no-streaming",       0,       &try_stream,               op_setint, 0   },
 {0,"no-filemon",         0,       &try_filemon,              op_setint, 0   },
 {'C',"inndconf",         "F",     &inndconffile,             op_string      },
@@ -3706,6 +3725,11 @@ static int path_ends_slash(const char *specified) {
 }
 
 int main(int argc, char **argv) {
+  /* set up libinn logging */
+  message_program_name= "innduct";
+  message_fatal_cleanup= innduct_fatal_cleanup;
+  INNLOGSETS(INNLOGSET_CALL)
+
   if (!argv[1]) {
     printusage(stderr);
     exit(8);
@@ -3726,7 +3750,7 @@ int main(int argc, char **argv) {
   /* defaults */
 
   int r= innconf_read(inndconffile);
-  if (!r) badusage("could not read inn.conf (more info on stderr)");
+  if (!r) badusage("could not read inn.conf");
 
   if (!remote_host) remote_host= sitename;
 
@@ -3797,20 +3821,27 @@ int main(int argc, char **argv) {
   globpat_backlog=  xasprintf("%s_backlog*",  feedfile);
 
   oop_source_sys *sysloop= oop_sys_new();
-  if (!sysloop) sysdie("could not create liboop event loop");
+  if (!sysloop) syscrash("could not create liboop event loop");
   loop= (oop_source*)sysloop;
 
   LIST_INIT(conns);
 
-  if (become_daemon) {
+  if (interactive < 1) {
     int i;
     for (i=3; i<255; i++)
       /* do this now before we open syslog, etc. */
       close(i);
+  }
+
+  logv_prefix= xasprintf("%s| ", sitename);
+  if (interactive < 2) {
     openlog("innduct",LOG_NDELAY|LOG_PID,LOG_NEWS);
+    logv_use_syslog= 1;
+  }
 
+  if (interactive < 1) {
     int null= open("/dev/null",O_RDWR);
-    if (null<0) sysfatal("failed to open /dev/null");
+    if (null<0) sysdie("failed to open /dev/null");
     dup2(null,0);
     dup2(null,1);
     dup2(null,2);
@@ -3820,14 +3851,14 @@ int main(int argc, char **argv) {
     if (child1) _exit(0);
 
     pid_t sid= setsid();
-    if (sid == -1) sysfatal("setsid failed");
+    if (sid == -1) sysdie("setsid failed");
 
     pid_t child2= xfork("daemonise second fork");
     if (child2) _exit(0);
   }
 
   self_pid= getpid();
-  if (self_pid==-1) sysdie("getpid");
+  if (self_pid==-1) syscrash("getpid");
 
   r= chdir(path_run);
   if (r) sysdie("could not chdir to pathrun %s", path_run);
@@ -3842,7 +3873,7 @@ int main(int argc, char **argv) {
   r= SMsetup(SM_PREOPEN, &val); if (!r) warn("SMsetup SM_PREOPEN failed");
   r= SMinit(); if (!r) die("storage manager initialisation (SMinit) failed");
 
-  if (!become_daemon)
+  if (interactive >= 2)
     cli_stdio();
 
   cli_init();
@@ -3866,5 +3897,5 @@ int main(int argc, char **argv) {
 
   void *run= oop_sys_run(sysloop);
   assert(run == OOP_ERROR);
-  sysdie("event loop failed");
+  syscrash("event loop failed");
 }