chiark / gitweb /
8b588039b9e4f45e427f93a4e1da8a85dec9dda0
[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.counts, [RC_##x])
226
227 #define CNT(art,rc) (ipf->counts.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->counts.read_ok, ipf->counts.read_blank, ipf->counts.read_err,
242        inprog, autodefer, ipf->counts.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   memset(&ipf->counts, 0, sizeof(ipf->counts));
251
252   free(inprog);
253   free(autodefer);
254
255 #undef CNT
256 }
257
258 void statemc_check_backlog_done(void) {
259   InputFile *ipf= backlog_input_file;
260   if (!inputfile_is_done(ipf)) return;
261
262   const char *slash= strrchr(ipf->path, '/');
263   const char *leaf= slash ? slash+1 : ipf->path;
264   const char *under= strchr(slash, '_');
265   const char *rest= under ? under+1 : leaf;
266   if (!strncmp(rest,"backlog",7)) rest += 7;
267   notice_processed(ipf,1,"backlog ",rest);
268
269   close_input_file(ipf);
270   if (unlink(ipf->path)) {
271     if (errno != ENOENT)
272       syscrash("could not unlink processed backlog file %s", ipf->path);
273     warn("backlog file %s vanished while we were reading it"
274          " so we couldn't remove it (but it's done now, anyway)",
275          ipf->path);
276   }
277   free(ipf);
278   backlog_input_file= 0;
279   search_backlog_file();
280   return;
281 }
282
283 void statemc_check_flushing_done(void) {
284   InputFile *ipf= flushing_input_file;
285   if (!inputfile_is_done(ipf)) return;
286
287   assert(sms==sm_SEPARATED || sms==sm_DROPPING);
288
289   notice_processed(ipf,1,"feedfile","");
290
291   close_defer();
292
293   xunlink(path_flushing, "old flushing file");
294
295   close_input_file(flushing_input_file);
296   free(flushing_input_file);
297   flushing_input_file= 0;
298
299   if (sms==sm_SEPARATED) {
300     notice("flush complete");
301     SMS(NORMAL, spontaneous_flush_periods, "flush complete");
302   } else if (sms==sm_DROPPING) {
303     SMS(DROPPED, max_separated_periods, "old flush complete");
304     search_backlog_file();
305     notice("feed dropped, but will continue until backlog is finished");
306   }
307 }
308
309 static void *statemc_check_input_done(oop_source *lp, struct timeval now,
310                                       void *u) {
311   /* main input file may be idle but if so that's because
312    * we haven't got to it yet, but that doesn't mean it's really done */
313   statemc_check_flushing_done();
314   statemc_check_backlog_done();
315   return OOP_CONTINUE;
316 }
317
318 void queue_check_input_done(void) {
319   loop->on_time(loop, OOP_TIME_NOW, statemc_check_input_done, 0);
320 }
321
322 void statemc_setstate(StateMachineState newsms, int periods,
323                       const char *forlog, const char *why) {
324   sms= newsms;
325   until_flush= periods;
326
327   const char *xtra= "";
328   switch (sms) {
329   case sm_FLUSHING:
330   case sm_FLUSHFAILED:
331     if (!main_input_file) xtra= "-ABSENT";
332     break;
333   case sm_SEPARATED:
334   case sm_DROPPING:
335     xtra= flushing_input_file->rd ? "-1" : "-2";
336     break;
337   default:;
338   }
339
340   if (periods) {
341     info("state %s%s[%d] %s",forlog,xtra,periods,why);
342   } else {
343     info("state %s%s %s",forlog,xtra,why);
344   }
345 }
346
347 /*========== flushing the feed ==========*/
348
349 pid_t inndcomm_child;
350 static int inndcomm_sentinel_fd;
351
352 static void *inndcomm_event(oop_source *lp, int fd, oop_event e, void *u) {
353   assert(inndcomm_child);
354   assert(fd == inndcomm_sentinel_fd);
355   int status= xwaitpid(&inndcomm_child, "inndcomm");
356   inndcomm_child= 0;
357   
358   cancel_fd_read_except(fd);
359   xclose_perhaps(&fd, "inndcomm sentinel pipe",0);
360   inndcomm_sentinel_fd= 0;
361
362   assert(!flushing_input_file);
363
364   if (WIFEXITED(status)) {
365     switch (WEXITSTATUS(status)) {
366
367     case INNDCOMMCHILD_ESTATUS_FAIL:
368       goto failed;
369
370     case INNDCOMMCHILD_ESTATUS_NONESUCH:
371       notice("feed has been dropped by innd, finishing up");
372       flushing_input_file= main_input_file;
373       tailing_make_readable(flushing_input_file);
374         /* we probably previously returned EAGAIN from our fake read method
375          * when in fact we were at EOF, so signal another readable event
376          * so we actually see the EOF */
377
378       main_input_file= 0;
379
380       if (flushing_input_file) {
381         SMS(DROPPING, max_separated_periods,
382             "feed dropped by innd, but must finish last flush");
383       } else {
384         close_defer();
385         SMS(DROPPED, 0, "feed dropped by innd");
386         search_backlog_file();
387       }
388       return OOP_CONTINUE;
389
390     case 0:
391       /* as above */
392       flushing_input_file= main_input_file;
393       tailing_make_readable(flushing_input_file);
394
395       main_input_file= open_input_file(feedfile);
396       if (!main_input_file)
397         crash("flush succeeded but feedfile %s does not exist!"
398               " (this probably means feedfile does not correspond"
399               " to site %s in newsfeeds)", feedfile, sitename);
400
401       if (flushing_input_file) {
402         SMS(SEPARATED, max_separated_periods, "flush complete");
403       } else {
404         close_defer();
405         SMS(NORMAL, spontaneous_flush_periods, "recovery flush complete");
406       }
407       return OOP_CONTINUE;
408
409     default:
410       goto unexpected_exitstatus;
411
412     }
413   } else if (WIFSIGNALED(status) && WTERMSIG(status) == SIGALRM) {
414     warn("flush timed out trying to talk to innd");
415     goto failed;
416   } else {
417   unexpected_exitstatus:
418     report_child_status("inndcomm child", status);
419   }
420
421  failed:
422   SMS(FLUSHFAILED, flushfail_retry_periods, "flush failed, will retry");
423   return OOP_CONTINUE;
424 }
425
426 static void inndcommfail(const char *what) {
427   syswarn("error communicating with innd: %s failed: %s", what, ICCfailure);
428   exit(INNDCOMMCHILD_ESTATUS_FAIL);
429 }
430
431 void spawn_inndcomm_flush(const char *why) { /* Moved => Flushing */
432   int pipefds[2];
433
434   notice("flushing %s",why);
435
436   assert(sms==sm_NORMAL || sms==sm_FLUSHFAILED);
437   assert(!inndcomm_child);
438   assert(!inndcomm_sentinel_fd);
439
440   if (pipe(pipefds)) sysdie("create pipe for inndcomm child sentinel");
441
442   inndcomm_child= xfork("inndcomm child");
443
444   if (!inndcomm_child) {
445     const char *flushargv[2]= { sitename, 0 };
446     char *reply;
447     int r;
448
449     xclose(pipefds[0], "(in child) inndcomm sentinel parent's end",0);
450     /* parent spots the autoclose of pipefds[1] when we die or exit */
451
452     if (simulate_flush>=0) {
453       warn("SIMULATING flush child status %d", simulate_flush);
454       if (simulate_flush>128) raise(simulate_flush-128);
455       else exit(simulate_flush);
456     }
457
458     alarm(inndcomm_flush_timeout);
459     r= ICCopen();                         if (r)   inndcommfail("connect");
460     r= ICCcommand('f',flushargv,&reply);  if (r<0) inndcommfail("transmit");
461     if (!r) exit(0); /* yay! */
462
463     if (!strcmp(reply, "1 No such site")) exit(INNDCOMMCHILD_ESTATUS_NONESUCH);
464     syswarn("innd ctlinnd flush failed: innd said %s", reply);
465     exit(INNDCOMMCHILD_ESTATUS_FAIL);
466   }
467
468   simulate_flush= -1;
469
470   xclose(pipefds[1], "inndcomm sentinel child's end",0);
471   inndcomm_sentinel_fd= pipefds[0];
472   assert(inndcomm_sentinel_fd);
473   on_fd_read_except(inndcomm_sentinel_fd, inndcomm_event);
474
475   SMS(FLUSHING, 0, why);
476 }
477
478 /*---------- shutdown and signal handling ----------*/
479
480 void preterminate(void) {
481   if (in_child) return;
482   showstats();
483 }
484
485 void showstats(void) {
486   notice_processed(main_input_file,0,"feedfile","");
487   notice_processed(flushing_input_file,0,"flushing","");
488   if (backlog_input_file)
489     notice_processed(backlog_input_file,0, "backlog file ",
490                      backlog_input_file->path);
491   until_stats_log= stats_log_periods;
492 }
493
494 static int signal_self_pipe[2];
495
496 static void *sigarrived_event(oop_source *lp, int fd, oop_event e, void *u) {
497   assert(fd=signal_self_pipe[0]);
498   char buf[PIPE_BUF];
499   int r= read(signal_self_pipe[0], buf, sizeof(buf));
500   if (r<0 && !isewouldblock(errno))
501     syscrash("failed to read signal self pipe");
502   if (r==0) crash("eof on signal self pipe");
503   if (terminate_sig_flag) {
504     preterminate();
505     notice("terminating (%s)", strsignal(terminate_sig_flag));
506     raise_default(terminate_sig_flag);
507   }
508   return OOP_CONTINUE;
509 }
510
511 static void sigarrived_handler(int signum) {
512   static char x;
513   switch (signum) {
514   case SIGTERM:
515   case SIGINT:
516     if (!terminate_sig_flag) terminate_sig_flag= signum;
517     break;
518   default:
519     abort();
520   }
521   write(signal_self_pipe[1],&x,1);
522 }
523
524 void init_signals(void) {
525   if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
526     syscrash("could not ignore SIGPIPE");
527
528   if (pipe(signal_self_pipe)) sysdie("create self-pipe for signals");
529
530   xsetnonblock(signal_self_pipe[0],1);
531   xsetnonblock(signal_self_pipe[1],1);
532
533   struct sigaction sa;
534   memset(&sa,0,sizeof(sa));
535   sa.sa_handler= sigarrived_handler;
536   sa.sa_flags= SA_RESTART;
537   xsigaction(SIGTERM,&sa);
538   xsigaction(SIGINT,&sa);
539
540   on_fd_read_except(signal_self_pipe[0], sigarrived_event);
541 }
542