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