chiark / gitweb /
Rationalise docs - get rid of README.notes
[innduct.git] / statemc.c
1 /*
2  *  innduct
3  *  tailing reliable realtime streaming feeder for inn
4  *  statemc.c - state machine core (see README.states).
5  *
6  *  Copyright (C) 2010 Ian Jackson <ijackson@chiark.greenend.org.uk>
7  * 
8  *  This program is free software: you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation, either version 3 of the License, or
11  *  (at your option) any later version.
12  * 
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  * 
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  *
21  *  (I believe that when you compile and link this as part of the inn2
22  *  build, with the Makefile runes I have provided, all the libraries
23  *  and files which end up included in innduct are licence-compatible
24  *  with GPLv3.  If not then please let me know.  -Ian Jackson.)
25  */
26
27 #include "innduct.h"
28
29
30 /* statemc_init initialises */
31 StateMachineState sms;
32 int until_flush;
33 InputFile *main_input_file, *flushing_input_file, *backlog_input_file;
34 FILE *defer;
35
36 /* initialisation to 0 is good */
37 int until_connect, until_backlog_nextscan;
38 double accept_proportion;
39 int nocheck, nocheck_reported, in_child;
40 sig_atomic_t terminate_sig_flag;
41
42
43 static void startup_set_input_file(InputFile *f) {
44   assert(!main_input_file);
45   main_input_file= f;
46   inputfile_reading_start(f);
47 }
48
49 void statemc_lock(void) {
50   int lockfd;
51   struct stat stab, stabf;
52   
53   for (;;) {
54     lockfd= open(path_lock, O_CREAT|O_RDWR, 0600);
55     if (lockfd<0) sysdie("open lockfile %s", path_lock);
56
57     struct flock fl;
58     memset(&fl,0,sizeof(fl));
59     fl.l_type= F_WRLCK;
60     fl.l_whence= SEEK_SET;
61     int r= fcntl(lockfd, F_SETLK, &fl);
62     if (r==-1) {
63       if (errno==EACCES || isewouldblock(errno)) {
64         if (quiet_multiple) exit(0);
65         die("another duct holds the lockfile");
66       }
67       sysdie("fcntl F_SETLK lockfile %s", path_lock);
68     }
69
70     xfstat_isreg(lockfd, &stabf, path_lock, "lockfile");
71     int lock_noent;
72     xlstat_isreg(path_lock, &stab, &lock_noent, "lockfile");
73
74     if (!lock_noent && samefile(&stab, &stabf))
75       break;
76
77     xclose(lockfd, "stale lockfile ", path_lock);
78   }
79
80   FILE *lockfile= fdopen(lockfd, "w");
81   if (!lockfile) syscrash("fdopen lockfile");
82
83   int r= ftruncate(lockfd, 0);
84   if (r) syscrash("truncate lockfile to write new info");
85
86   if (fprintf(lockfile, "pid %ld\nsite %s\nfeedfile %s\nfqdn %s\n",
87               (unsigned long)self_pid,
88               sitename, feedfile, remote_host) == EOF ||
89       fflush(lockfile))
90     sysdie("write info to lockfile %s", path_lock);
91
92   dbg("startup: locked");
93 }
94
95 void statemc_init(void) {
96   struct stat stabdefer;
97
98   search_backlog_file();
99
100   int defer_noent;
101   xlstat_isreg(path_defer, &stabdefer, &defer_noent, "defer file");
102   if (defer_noent) {
103     dbg("startup: ductdefer ENOENT");
104   } else {
105     dbg("startup: ductdefer nlink=%ld", (long)stabdefer.st_nlink);
106     switch (stabdefer.st_nlink==1) {
107     case 1:
108       open_defer(); /* so that we will later close it and rename it */
109       break;
110     case 2:
111       xunlink(path_defer, "stale defer file link"
112               " (presumably hardlink to backlog file)");
113       break;
114     default:
115       crash("defer file %s has unexpected link count %d",
116             path_defer, stabdefer.st_nlink);
117     }
118   }
119
120   struct stat stab_f, stab_d;
121   int noent_f;
122
123   InputFile *file_d= open_input_file(path_flushing);
124   if (file_d) xfstat_isreg(file_d->fd, &stab_d, path_flushing,"flushing file");
125
126   xlstat_isreg(feedfile, &stab_f, &noent_f, "feedfile");
127
128   if (!noent_f && file_d && samefile(&stab_f, &stab_d)) {
129     dbg("startup: F==D => Hardlinked");
130     xunlink(feedfile, "feed file (during startup)"); /* => Moved */
131     noent_f= 1;
132   }
133
134   if (noent_f) {
135     dbg("startup: F ENOENT => Moved");
136     if (file_d) startup_set_input_file(file_d);
137     spawn_inndcomm_flush("feedfile missing at startup");
138     /* => Flushing, sms:=FLUSHING */
139   } else {
140     if (file_d) {
141       dbg("startup: F!=D => Separated");
142       startup_set_input_file(file_d);
143       flushing_input_file= main_input_file;
144       main_input_file= open_input_file(feedfile);
145       if (!main_input_file) crash("feedfile vanished during startup");
146       SMS(SEPARATED, max_separated_periods,
147           "found both old and current feed files");
148     } else {
149       dbg("startup: F exists, D ENOENT => Normal");
150       InputFile *file_f= open_input_file(feedfile);
151       if (!file_f) crash("feed file vanished during startup");
152       startup_set_input_file(file_f);
153       SMS(NORMAL, spontaneous_flush_periods, "normal startup");
154     }
155   }
156 }
157
158 void statemc_start_flush(const char *why) { /* Normal => Flushing */
159   assert(sms == sm_NORMAL);
160
161   dbg("starting flush (%s) (%lu >?= %lu) (%d)",
162         why,
163         (unsigned long)(main_input_file ? main_input_file->offset : 0),
164         (unsigned long)target_max_feedfile_size,
165         until_flush);
166
167   int r= link(feedfile, path_flushing);
168   if (r) sysdie("link feedfile %s to flushing file %s",
169                 feedfile, path_flushing);
170   /* => Hardlinked */
171
172   xunlink(feedfile, "old feedfile link");
173   /* => Moved */
174
175   spawn_inndcomm_flush(why); /* => Flushing FLUSHING */
176 }
177
178 int trigger_flush_ok(const char *why) {
179   switch (sms) {
180
181   case sm_NORMAL:
182     statemc_start_flush(why ? why : "periodic");
183     return 1;                           /* Normal => Flushing; => FLUSHING */
184
185   case sm_FLUSHFAILED:
186     spawn_inndcomm_flush(why ? why : "retry");
187     return 1;                            /* Moved => Flushing; => FLUSHING */
188
189   case sm_SEPARATED:
190   case sm_DROPPING:
191     warn("abandoning old feedfile after flush (%s), autodeferring",
192          why ? why : "took too long to complete");
193     assert(flushing_input_file);
194     autodefer_input_file(flushing_input_file);
195     return 1;
196
197   default:
198     return 0;
199   }
200 }
201
202 void statemc_period_poll(void) {
203   if (!until_flush) return;
204   until_flush--;
205   assert(until_flush>=0);
206
207   if (until_flush) return;
208   int ok= trigger_flush_ok(0);
209   assert(ok);
210 }
211
212 static int inputfile_is_done(InputFile *ipf) {
213   if (!ipf) return 0;
214   if (ipf->inprogress) return 0; /* new article in the meantime */
215   if (ipf->rd) return 0; /* not had EOF */
216   return 1;
217 }
218
219 static void notice_processed(InputFile *ipf, int completed,
220                              const char *what, const char *spec) {
221   if (!ipf) return; /* allows preterminate to be lazy */
222
223 #define RCI_NOTHING(x) /* nothing */
224 #define RCI_TRIPLE_FMT(x) " " #x "=" RCI_TRIPLE_FMT_BASE
225 #define RCI_TRIPLE_VALS(x) , RCI_TRIPLE_VALS_BASE(ipf->counts, [RC_##x])
226
227 #define CNT(art,rc) (ipf->counts[art_##art][RC_##rc])
228
229   char *inprog= completed
230     ? xasprintf("%s","") /* GCC produces a stupid warning for printf("") ! */
231     : xasprintf(" inprogress=%ld", ipf->inprogress);
232   char *autodefer= ipf->autodefer >= 0
233     ? xasprintf(" autodeferred=%ld", ipf->autodefer)
234     : xasprintf("%s","");
235
236   info("%s %s%s read=%d (+bl=%d,+err=%d)%s%s"
237        " missing=%d offered=%d (ch=%d,nc=%d) accepted=%d (ch=%d,nc=%d)"
238        RESULT_COUNTS(RCI_NOTHING, RCI_TRIPLE_FMT)
239        ,
240        completed?"completed":"processed", what, spec,
241        ipf->readcount_ok, ipf->readcount_blank, ipf->readcount_err,
242        inprog, autodefer, ipf->count_nooffer_missing,
243        CNT(Unchecked,sent) + CNT(Unsolicited,sent)
244        , CNT(Unchecked,sent), CNT(Unsolicited,sent),
245        CNT(Wanted,accepted) + CNT(Unsolicited,accepted)
246        , CNT(Wanted,accepted), CNT(Unsolicited,accepted)
247        RESULT_COUNTS(RCI_NOTHING,  RCI_TRIPLE_VALS)
248        );
249
250   free(inprog);
251   free(autodefer);
252
253 #undef CNT
254 }
255
256 void statemc_check_backlog_done(void) {
257   InputFile *ipf= backlog_input_file;
258   if (!inputfile_is_done(ipf)) return;
259
260   const char *slash= strrchr(ipf->path, '/');
261   const char *leaf= slash ? slash+1 : ipf->path;
262   const char *under= strchr(slash, '_');
263   const char *rest= under ? under+1 : leaf;
264   if (!strncmp(rest,"backlog",7)) rest += 7;
265   notice_processed(ipf,1,"backlog ",rest);
266
267   close_input_file(ipf);
268   if (unlink(ipf->path)) {
269     if (errno != ENOENT)
270       syscrash("could not unlink processed backlog file %s", ipf->path);
271     warn("backlog file %s vanished while we were reading it"
272          " so we couldn't remove it (but it's done now, anyway)",
273          ipf->path);
274   }
275   free(ipf);
276   backlog_input_file= 0;
277   search_backlog_file();
278   return;
279 }
280
281 void statemc_check_flushing_done(void) {
282   InputFile *ipf= flushing_input_file;
283   if (!inputfile_is_done(ipf)) return;
284
285   assert(sms==sm_SEPARATED || sms==sm_DROPPING);
286
287   notice_processed(ipf,1,"feedfile","");
288
289   close_defer();
290
291   xunlink(path_flushing, "old flushing file");
292
293   close_input_file(flushing_input_file);
294   free(flushing_input_file);
295   flushing_input_file= 0;
296
297   if (sms==sm_SEPARATED) {
298     notice("flush complete");
299     SMS(NORMAL, spontaneous_flush_periods, "flush complete");
300   } else if (sms==sm_DROPPING) {
301     SMS(DROPPED, max_separated_periods, "old flush complete");
302     search_backlog_file();
303     notice("feed dropped, but will continue until backlog is finished");
304   }
305 }
306
307 static void *statemc_check_input_done(oop_source *lp, struct timeval now,
308                                       void *u) {
309   /* main input file may be idle but if so that's because
310    * we haven't got to it yet, but that doesn't mean it's really done */
311   statemc_check_flushing_done();
312   statemc_check_backlog_done();
313   return OOP_CONTINUE;
314 }
315
316 void queue_check_input_done(void) {
317   loop->on_time(loop, OOP_TIME_NOW, statemc_check_input_done, 0);
318 }
319
320 void statemc_setstate(StateMachineState newsms, int periods,
321                       const char *forlog, const char *why) {
322   sms= newsms;
323   until_flush= periods;
324
325   const char *xtra= "";
326   switch (sms) {
327   case sm_FLUSHING:
328   case sm_FLUSHFAILED:
329     if (!main_input_file) xtra= "-ABSENT";
330     break;
331   case sm_SEPARATED:
332   case sm_DROPPING:
333     xtra= flushing_input_file->rd ? "-1" : "-2";
334     break;
335   default:;
336   }
337
338   if (periods) {
339     info("state %s%s[%d] %s",forlog,xtra,periods,why);
340   } else {
341     info("state %s%s %s",forlog,xtra,why);
342   }
343 }
344
345 /*========== flushing the feed ==========*/
346
347 pid_t inndcomm_child;
348 static int inndcomm_sentinel_fd;
349
350 static void *inndcomm_event(oop_source *lp, int fd, oop_event e, void *u) {
351   assert(inndcomm_child);
352   assert(fd == inndcomm_sentinel_fd);
353   int status= xwaitpid(&inndcomm_child, "inndcomm");
354   inndcomm_child= 0;
355   
356   cancel_fd_read_except(fd);
357   xclose_perhaps(&fd, "inndcomm sentinel pipe",0);
358   inndcomm_sentinel_fd= 0;
359
360   assert(!flushing_input_file);
361
362   if (WIFEXITED(status)) {
363     switch (WEXITSTATUS(status)) {
364
365     case INNDCOMMCHILD_ESTATUS_FAIL:
366       goto failed;
367
368     case INNDCOMMCHILD_ESTATUS_NONESUCH:
369       notice("feed has been dropped by innd, finishing up");
370       flushing_input_file= main_input_file;
371       tailing_make_readable(flushing_input_file);
372         /* we probably previously returned EAGAIN from our fake read method
373          * when in fact we were at EOF, so signal another readable event
374          * so we actually see the EOF */
375
376       main_input_file= 0;
377
378       if (flushing_input_file) {
379         SMS(DROPPING, max_separated_periods,
380             "feed dropped by innd, but must finish last flush");
381       } else {
382         close_defer();
383         SMS(DROPPED, 0, "feed dropped by innd");
384         search_backlog_file();
385       }
386       return OOP_CONTINUE;
387
388     case 0:
389       /* as above */
390       flushing_input_file= main_input_file;
391       tailing_make_readable(flushing_input_file);
392
393       main_input_file= open_input_file(feedfile);
394       if (!main_input_file)
395         crash("flush succeeded but feedfile %s does not exist!"
396               " (this probably means feedfile does not correspond"
397               " to site %s in newsfeeds)", feedfile, sitename);
398
399       if (flushing_input_file) {
400         SMS(SEPARATED, max_separated_periods, "flush complete");
401       } else {
402         close_defer();
403         SMS(NORMAL, spontaneous_flush_periods, "recovery flush complete");
404       }
405       return OOP_CONTINUE;
406
407     default:
408       goto unexpected_exitstatus;
409
410     }
411   } else if (WIFSIGNALED(status) && WTERMSIG(status) == SIGALRM) {
412     warn("flush timed out trying to talk to innd");
413     goto failed;
414   } else {
415   unexpected_exitstatus:
416     report_child_status("inndcomm child", status);
417   }
418
419  failed:
420   SMS(FLUSHFAILED, flushfail_retry_periods, "flush failed, will retry");
421   return OOP_CONTINUE;
422 }
423
424 static void inndcommfail(const char *what) {
425   syswarn("error communicating with innd: %s failed: %s", what, ICCfailure);
426   exit(INNDCOMMCHILD_ESTATUS_FAIL);
427 }
428
429 void spawn_inndcomm_flush(const char *why) { /* Moved => Flushing */
430   int pipefds[2];
431
432   notice("flushing %s",why);
433
434   assert(sms==sm_NORMAL || sms==sm_FLUSHFAILED);
435   assert(!inndcomm_child);
436   assert(!inndcomm_sentinel_fd);
437
438   if (pipe(pipefds)) sysdie("create pipe for inndcomm child sentinel");
439
440   inndcomm_child= xfork("inndcomm child");
441
442   if (!inndcomm_child) {
443     const char *flushargv[2]= { sitename, 0 };
444     char *reply;
445     int r;
446
447     xclose(pipefds[0], "(in child) inndcomm sentinel parent's end",0);
448     /* parent spots the autoclose of pipefds[1] when we die or exit */
449
450     if (simulate_flush>=0) {
451       warn("SIMULATING flush child status %d", simulate_flush);
452       if (simulate_flush>128) raise(simulate_flush-128);
453       else exit(simulate_flush);
454     }
455
456     alarm(inndcomm_flush_timeout);
457     r= ICCopen();                         if (r)   inndcommfail("connect");
458     r= ICCcommand('f',flushargv,&reply);  if (r<0) inndcommfail("transmit");
459     if (!r) exit(0); /* yay! */
460
461     if (!strcmp(reply, "1 No such site")) exit(INNDCOMMCHILD_ESTATUS_NONESUCH);
462     syswarn("innd ctlinnd flush failed: innd said %s", reply);
463     exit(INNDCOMMCHILD_ESTATUS_FAIL);
464   }
465
466   simulate_flush= -1;
467
468   xclose(pipefds[1], "inndcomm sentinel child's end",0);
469   inndcomm_sentinel_fd= pipefds[0];
470   assert(inndcomm_sentinel_fd);
471   on_fd_read_except(inndcomm_sentinel_fd, inndcomm_event);
472
473   SMS(FLUSHING, 0, why);
474 }
475
476 /*---------- shutdown and signal handling ----------*/
477
478 void preterminate(void) {
479   if (in_child) return;
480   notice_processed(main_input_file,0,"feedfile","");
481   notice_processed(flushing_input_file,0,"flushing","");
482   if (backlog_input_file)
483     notice_processed(backlog_input_file,0, "backlog file ",
484                      backlog_input_file->path);
485 }
486
487 static int signal_self_pipe[2];
488
489 static void *sigarrived_event(oop_source *lp, int fd, oop_event e, void *u) {
490   assert(fd=signal_self_pipe[0]);
491   char buf[PIPE_BUF];
492   int r= read(signal_self_pipe[0], buf, sizeof(buf));
493   if (r<0 && !isewouldblock(errno))
494     syscrash("failed to read signal self pipe");
495   if (r==0) crash("eof on signal self pipe");
496   if (terminate_sig_flag) {
497     preterminate();
498     notice("terminating (%s)", strsignal(terminate_sig_flag));
499     raise_default(terminate_sig_flag);
500   }
501   return OOP_CONTINUE;
502 }
503
504 static void sigarrived_handler(int signum) {
505   static char x;
506   switch (signum) {
507   case SIGTERM:
508   case SIGINT:
509     if (!terminate_sig_flag) terminate_sig_flag= signum;
510     break;
511   default:
512     abort();
513   }
514   write(signal_self_pipe[1],&x,1);
515 }
516
517 void init_signals(void) {
518   if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
519     syscrash("could not ignore SIGPIPE");
520
521   if (pipe(signal_self_pipe)) sysdie("create self-pipe for signals");
522
523   xsetnonblock(signal_self_pipe[0],1);
524   xsetnonblock(signal_self_pipe[1],1);
525
526   struct sigaction sa;
527   memset(&sa,0,sizeof(sa));
528   sa.sa_handler= sigarrived_handler;
529   sa.sa_flags= SA_RESTART;
530   xsigaction(SIGTERM,&sa);
531   xsigaction(SIGINT,&sa);
532
533   on_fd_read_except(signal_self_pipe[0], sigarrived_event);
534 }
535