chiark / gitweb /
info in lockfile
[inn-innduct.git] / backends / innduct.c
index 8e599f56026835595780ad3a28725754699fd06f..1fd382835ec9c1355bcd70827585a2bba703e7d1 100644 (file)
@@ -1,10 +1,6 @@
 /*
  * TODO
- *  - actually implement badusage
- *  - options for all options
- *  - manpage
  *  - pid, sitename, hostname in lockfile
- *  - -k kill mode ?
  */
 
 /*
@@ -191,11 +187,11 @@ perl -ne 'print if m/-8\<-/..m/-\>8-/; print "\f" if m/-\^L-/' backends/innduct.
 
 /*----- general definitions, probably best not changed -----*/
 
-#define CONNCHILD_ESTATUS_STREAM   4
-#define CONNCHILD_ESTATUS_NOSTREAM 5
+#define CONNCHILD_ESTATUS_STREAM   24
+#define CONNCHILD_ESTATUS_NOSTREAM 25
 
-#define INNDCOMMCHILD_ESTATUS_FAIL     6
-#define INNDCOMMCHILD_ESTATUS_NONESUCH 7
+#define INNDCOMMCHILD_ESTATUS_FAIL     26
+#define INNDCOMMCHILD_ESTATUS_NONESUCH 27
 
 #define MAX_LINE_FEEDFILE (NNTP_MSGID_MAXLEN + sizeof(TOKEN)*2 + 10)
 
@@ -310,6 +306,7 @@ static const oop_rd_style peer_rd_style;
 static oop_rd_call peer_rd_err, peer_rd_ok;
 
 /*----- configuration options -----*/
+/* when changing defaults, remember to update the manpage */
 
 static const char *sitename, *remote_host;
 static const char *feedfile;
@@ -577,7 +574,7 @@ static pid_t xfork(const char *what) {
   pid_t child;
 
   child= fork();
-  if (child==-1) sysdie("cannot fork for %s",what);
+  if (child==-1) sysfatal("cannot fork for %s",what);
   debug("forked %s %ld", what, (unsigned long)child);
   if (!child) postfork();
   return child;
@@ -1659,7 +1656,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) sysdie("inotify_add_watch %s", ipf->path);
+  if (wd < 0) sysfatal("inotify_add_watch %s", ipf->path);
 
   if (wd >= filemon_inotify_wdmax) {
     int newmax= wd+2;
@@ -1906,8 +1903,10 @@ static void statemc_init(void) {
   path_defer=       xasprintf("%s_defer",     feedfile);
   globpat_backlog=  xasprintf("%s_backlog*",  feedfile);
 
+  int lockfd;
+  
   for (;;) {
-    int lockfd= open(path_lock, O_CREAT|O_RDWR, 0600);
+    lockfd= open(path_lock, O_CREAT|O_RDWR, 0600);
     if (lockfd<0) sysfatal("open lockfile %s", path_lock);
 
     struct flock fl;
@@ -1920,7 +1919,7 @@ static void statemc_init(void) {
        if (quiet_multiple) exit(0);
        fatal("another duct holds the lockfile");
       }
-      sysdie("fcntl F_SETLK lockfile %s", path_lock);
+      sysfatal("fcntl F_SETLK lockfile %s", path_lock);
     }
 
     xfstat_isreg(lockfd, &stabf, path_lock, "lockfile");
@@ -1932,6 +1931,20 @@ static void statemc_init(void) {
 
     xclose(lockfd, "stale lockfile ", path_lock);
   }
+  pid_t self= getpid();
+  if (self==-1) sysdie("getpid");
+
+  FILE *lockfile= fdopen(lockfd, "w");
+  if (!lockfile) sysdie("fdopen lockfile");
+
+  int r= ftruncate(lockfd, 0);
+  if (r) sysdie("truncate lockfile to write new info");
+
+  if (fprintf(lockfile, "pid %ld\nsite %s\nfeedfile %s\nfqdn %s\n",
+             (unsigned long)self, sitename, feedfile, remote_host) == EOF ||
+      fflush(lockfile))
+    sysfatal("write info to lockfile %s", path_lock);
+
   debug("startup: locked");
 
   search_backlog_file();
@@ -2000,8 +2013,8 @@ static void statemc_start_flush(const char *why) { /* Normal => Flushing */
        sm_period_counter);
 
   int r= link(feedfile, path_flushing);
-  if (r) sysdie("link feedfile %s to flushing file %s",
-               feedfile, path_flushing);
+  if (r) sysfatal("link feedfile %s to flushing file %s",
+                 feedfile, path_flushing);
   /* => Hardlinked */
 
   xunlink(feedfile, "old feedfile link");
@@ -2255,9 +2268,9 @@ static void search_backlog_file(void) {
 
   switch (r) {
   case GLOB_ABORTED:
-    sysdie("failed to expand backlog pattern %s", globpat_backlog);
+    sysfatal("failed to expand backlog pattern %s", globpat_backlog);
   case GLOB_NOSPACE:
-    die("out of memory expanding backlog pattern %s", globpat_backlog);
+    fatal("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];
@@ -2293,7 +2306,7 @@ static void search_backlog_file(void) {
     if (sms==sm_DROPPED) {
       notice("feed dropped and our work is complete");
       xunlink(path_lock, "lockfile for old feed");
-      exit(0);
+      exit(4);
     }
     until_backlog_nextscan= backlog_spontrescan_periods;
     goto xfree;
@@ -2419,7 +2432,7 @@ void spawn_inndcomm_flush(const char *why) { /* Moved => Flushing */
   assert(!inndcomm_child);
   assert(!inndcomm_sentinel_fd);
 
-  if (pipe(pipefds)) sysdie("create pipe for inndcomm child sentinel");
+  if (pipe(pipefds)) sysfatal("create pipe for inndcomm child sentinel");
 
   inndcomm_child= xfork("inndcomm child");
 
@@ -2675,10 +2688,14 @@ static void op_seconds(const Option *o, const char *val) {
   double v= strtod(val,&ep);
   if (ep==val) badusage("bad time/duration value for %s",o->lng);
 
-  if (!*ep || !strcmp(ep,"s")) unit= 1;
-  else if (!strcmp(ep,"m")) unit= 60;
-  else if (!strcmp(ep,"h")) unit= 3600;
-  else if (!strcmp(ep,"d")) unit= 86400;
+  if (!*ep || !strcmp(ep,"s") || !strcmp(ep,"sec")) unit= 1;
+  else if (!strcmp(ep,"m") || !strcmp(ep,"min"))    unit= 60;
+  else if (!strcmp(ep,"h") || !strcmp(ep,"hour"))   unit= 3600;
+  else if (!strcmp(ep,"d") || !strcmp(ep,"day"))    unit= 86400;
+  else if (!strcmp(ep,"das")) unit= 10;
+  else if (!strcmp(ep,"hs"))  unit= 100;
+  else if (!strcmp(ep,"ks"))  unit= 1000;
+  else if (!strcmp(ep,"Ms"))  unit= 1000000;
   else badusage("bad units %s for time/duration value for %s",ep,o->lng);
 
   v *= unit;
@@ -2752,7 +2769,7 @@ static void convert_to_periods_rndup(int *store) {
 int main(int argc, char **argv) {
   if (!argv[1]) {
     printusage(stderr);
-    exit(12);
+    exit(8);
   }
 
   parse_options(innduct_options, &argv);
@@ -2774,7 +2791,7 @@ int main(int argc, char **argv) {
 
   if (nocheck_decay < 0.1)
     badusage("nocheck decay articles must be at least 0.1");
-  nocheck_decay= 1 - 1.0/nocheck_decay;
+  nocheck_decay= pow(0.5, 1.0/nocheck_decay);
 
   convert_to_periods_rndup(&reconnect_delay_periods);
   convert_to_periods_rndup(&flushfail_retry_periods);
@@ -2822,7 +2839,7 @@ int main(int argc, char **argv) {
     openlog("innduct",LOG_NDELAY|LOG_PID,LOG_NEWS);
 
     int null= open("/dev/null",O_RDWR);
-    if (null<0) sysdie("failed to open /dev/null");
+    if (null<0) sysfatal("failed to open /dev/null");
     dup2(null,0);
     dup2(null,1);
     dup2(null,2);
@@ -2832,7 +2849,7 @@ int main(int argc, char **argv) {
     if (child1) _exit(0);
 
     pid_t sid= setsid();
-    if (sid != child1) sysdie("setsid failed");
+    if (sid != child1) sysfatal("setsid failed");
 
     pid_t child2= xfork("daemonise second fork");
     if (child2) _exit(0);