chiark / gitweb /
changelog: Finalise 2.2
[innduct.git] / statemc.c
index 58aaefd6413fec8a5ee4a2ddd6109d01ef51c300..26b5c22e4673aabd319401d17ba5e9cfc3c586e8 100644 (file)
--- a/statemc.c
+++ b/statemc.c
@@ -3,25 +3,9 @@
  *  tailing reliable realtime streaming feeder for inn
  *  statemc.c - state machine core (see README.states).
  *
- *  Copyright (C) 2010 Ian Jackson <ijackson@chiark.greenend.org.uk>
- * 
- *  This program is free software: you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation, either version 3 of the License, or
- *  (at your option) any later version.
- * 
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- * 
- *  You should have received a copy of the GNU General Public License
- *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
- *
- *  (I believe that when you compile and link this as part of the inn2
- *  build, with the Makefile runes I have provided, all the libraries
- *  and files which end up included in innduct are licence-compatible
- *  with GPLv3.  If not then please let me know.  -Ian Jackson.)
+ *  Copyright Ian Jackson <ijackson@chiark.greenend.org.uk>
+ *  and contributors; see LICENCE.txt.
+ *  SPDX-License-Identifier: GPL-3.0-or-later
  */
 
 #include "innduct.h"
@@ -105,7 +89,7 @@ void statemc_init(void) {
     dbg("startup: ductdefer ENOENT");
   } else {
     dbg("startup: ductdefer nlink=%ld", (long)stabdefer.st_nlink);
-    switch (stabdefer.st_nlink==1) {
+    switch (stabdefer.st_nlink) {
     case 1:
       open_defer(); /* so that we will later close it and rename it */
       break;
@@ -114,8 +98,8 @@ void statemc_init(void) {
              " (presumably hardlink to backlog file)");
       break;
     default:
-      crash("defer file %s has unexpected link count %d",
-           path_defer, stabdefer.st_nlink);
+      crash("defer file %s has unexpected link count %ld",
+           path_defer, (long)stabdefer.st_nlink);
     }
   }
 
@@ -190,8 +174,12 @@ int trigger_flush_ok(const char *why) {
 
   case sm_SEPARATED:
   case sm_DROPPING:
-    warn("abandoning old feedfile after flush (%s), autodeferring",
-        why ? why : "took too long to complete");
+    if (conns.count)
+      warn("abandoning old feedfile after flush (%s), autodeferring",
+          why ? why : "took too long to complete");
+    else
+      info("autodeferring after flush (%s)",
+          why ? why : "no connections");
     assert(flushing_input_file);
     autodefer_input_file(flushing_input_file);
     return 1;
@@ -228,14 +216,15 @@ static void notice_processed_counts(Counts *counts, int completed,
 #define CNT(art,rc) (counts->results[art_##art][RC_##rc])
 
   char *inprog= ipf_xtra && !completed
-    ? xasprintf(" inprogress=%ld", ipf_xtra->inprogress)
-    : xasprintf("%s",""); /* GCC produces a stupid warning for printf("") ! */
+    ? masprintf(" inprogress=%ld", ipf_xtra->inprogress)
+    : masprintf("%s",""); /* GCC produces a stupid warning for printf("") ! */
   char *autodefer= ipf_xtra && ipf_xtra->autodefer >= 0
-    ? xasprintf(" autodeferred=%ld", ipf_xtra->autodefer)
-    : xasprintf("%s","");
+    ? masprintf(" autodeferred=%ld", ipf_xtra->autodefer)
+    : masprintf("%s","");
 
-  info("%s %s read=%d (+bl=%d,+err=%d)%s%s"
-       " missing=%d offered=%d (ch=%d,nc=%d) accepted=%d (ch=%d,nc=%d)"
+  notice("%s %s read=%d (+bl=%d,+err=%d)%s%s missing=%d"
+        " offered=%d (ch=%d,nc=%d)"
+        " accepted=%d (ch=%d,nc=%d)"
        RESULT_COUNTS(RCI_NOTHING, RCI_TRIPLE_FMT)
        ,
        completed?"completed":"processed", what,
@@ -244,8 +233,8 @@ static void notice_processed_counts(Counts *counts, int completed,
        inprog, autodefer, counts->events[nooffer_missing],
        CNT(Unchecked,sent) + CNT(Unsolicited,sent)
        , CNT(Unchecked,sent), CNT(Unsolicited,sent),
-       CNT(Wanted,accepted) + CNT(Unsolicited,accepted)
-       , CNT(Wanted,accepted), CNT(Unsolicited,accepted)
+       CNT(Wanted,accepted) + CNT(Wanted,accepted)
+       , CNT(Wanted,accepted), CNT(Wanted,accepted)
        RESULT_COUNTS(RCI_NOTHING,  RCI_TRIPLE_VALS)
        );
 
@@ -282,6 +271,7 @@ void statemc_check_backlog_done(void) {
   InputFile *ipf= backlog_input_file;
   if (!inputfile_is_done(ipf)) return;
 
+  dbg("backlog file %p %s complete", ipf, ipf->path);
   backlog_accumulate_counts(ipf);
   close_input_file(ipf);
   if (unlink(ipf->path)) {
@@ -303,7 +293,7 @@ void statemc_check_flushing_done(void) {
 
   assert(sms==sm_SEPARATED || sms==sm_DROPPING);
 
-  notice_processed_inputfile(ipf,1,"feedfile");
+  notice_processed_inputfile(ipf,1,"batch");
 
   close_defer();
 
@@ -500,6 +490,7 @@ void preterminate(void) {
 }
 
 void showstats(void) {
+  notice_conns_stats();
   notice_processed_inputfile(main_input_file,     0, "feedfile");
   notice_processed_inputfile(flushing_input_file, 0, "flushing");
 
@@ -515,7 +506,7 @@ void showstats(void) {
 static int signal_self_pipe[2];
 
 static void *sigarrived_event(oop_source *lp, int fd, oop_event e, void *u) {
-  assert(fd=signal_self_pipe[0]);
+  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))
@@ -530,6 +521,7 @@ static void *sigarrived_event(oop_source *lp, int fd, oop_event e, void *u) {
 }
 
 static void sigarrived_handler(int signum) {
+  int esave = errno;
   static char x;
   switch (signum) {
   case SIGTERM:
@@ -539,7 +531,9 @@ static void sigarrived_handler(int signum) {
   default:
     abort();
   }
-  write(signal_self_pipe[1],&x,1);
+  int r = write(signal_self_pipe[1],&x,1);
+  if (!(r==1 || isewouldblock(errno))) abort();
+  errno = esave;
 }
 
 void init_signals(void) {