2 * Newsfeeds file entries should look like this:
3 * host.name.of.site[/exclude,exclude,...]\
4 * :pattern,pattern...[/distribution,distribution...]\
8 * sitename[/exclude,exclude,...]\
9 * :pattern,pattern...[/distribution,distribution...]\
15 * or might be blanked out
16 * <spc><spc><spc><spc>....
18 * site.name_duct.lock lock preventing multiple ducts
19 * holder of this lock is "duct"
20 * F site.name main feed file
21 * opened/created, then written, by innd
24 * tokens blanked out by duct when processed
25 * D site.name_duct temporary feed file during flush (or crash)
26 * hardlink created by duct
28 * site.name_duct.defer 431'd articles, still being written,
29 * created, written, used by duct
30 * site.name_backlog.lock lock taken out by innxmit wrapper
31 * holder and its child are "xmit"
32 * site.name_backlog_<date>.<inum>
33 * 431'd articles, ready for innxmit
34 * created (link/mv) by duct
37 * site.name_backlog_<letters> eg
38 * site.name_backlog_manual
39 * anything the sysadmin likes (eg, feed files
40 * from old feeds to be merged into this one)
41 * created (link/mv) by admin
52 <--------------------------------------'|
53 Nothing F, D both ENOENT |
56 duct: not not reading anything |
58 |`---------------------. |
59 | | duct times out waiting for F |
60 V innd creates F | duct exits |
62 Noduct GO TO Dropped |
65 duct: not running or not reading anything |
68 ,-->--+ <---------------------------------'|
69 | | duct opens F F exists |
73 | F: innd writing, duct reading |
76 | | duct decides time to flush |
77 | | duct makes hardlink |
79 | V <------------------------'|
81 | F == D: innd writing, duct reading both exist |
85 | Moved <----+------------<--'|
86 | F: ENOENT | F ENOENT |
87 | D: innd writing, duct reading | D exists |
89 | | duct requests flush of feed | |
90 | | (others can too, harmlessly) | |
94 | D: innd flushing, duct reading | |
96 | | inndcomm flush fails | |
97 | |`-------------------------->---------' |
99 | | inndcomm reports no such site |
100 | |`---------------------------------------------------- | -.
102 | | innd finishes writing D, creates F | |
103 | | inndcomm reports flush successful | |
106 | Separated <----------------' |
107 | F: innd writing F!=D /
108 | D: duct reading both exist /
110 | | duct gets to the end of D /
111 | | duct opens F too /
114 | F: innd writing, duct reading |
115 | D: duct finishing V
117 | | duct finishes processing D F: ENOENT
118 | V duct unlinks D D: duct reading
120 `--<--' | duct finishes
130 "duct reading" means innduct is reading the file but also
131 overwriting processed tokens.
137 #define PERIOD_SECONDS 60
139 static char *feedfile;
140 static int max_connections, max_queue_per_conn;
141 static int connection_setup_timeout, port, try_stream;
142 static const char *remote_host;
144 static double accept_proportion;
145 static double nocheck_thresh= 0.95;
146 static double nocheck_decay= 1-1/100;
147 static int nocheck, nocheck_reported;
149 #define ISNODE(T) T *next, *back;
150 #define LIST(T) struct { T *head, *tail, *tailpred; int count; }
152 #define NODE(n) ((struct node*)&(n)->head)
154 #define LIST_ADDHEAD(l,n) \
155 (list_addhead((struct list*)&(l), NODE((n))), (void)(l).count++)
156 #define LIST_ADDTAIL(l,n) \
157 (list_addtail((struct list*)&(l), NODE((n))), (void)(l).count++)
159 #define LIST_REMHEAD(l) \
160 ((l).count ? ((l).count--, (void*)list_remhead((struct list*)&(l))) : 0)
161 #define LIST_REMTAIL(l) \
162 ((l).count ? ((l).count--, (void*)list_remtail((struct list*)&(l))) : 0)
163 #define LIST_REMOVE(l,n) \
164 (list_remove(NODE((n))), (void)(l).count--)
165 #define LIST_INSERT(l,n,pred) \
166 (list_insert((struct list*)&(l), NODE((n)), NODE((pred))), (void)(l).count++)
169 #define RESULT_COUNTS \
178 #define RC_INDEX(x) RCI_##x
184 int articles[2 /* checked */][RCI_max];
189 int checked, sentbody;
201 typedef struct Conn Conn;
204 xk_Malloc, xk_Const, xk_Artdata;
217 int fd, max_queue, stream;
218 LIST(Article) queue; /* not yet told peer, or CHECK said send it */
219 LIST(Article) sent; /* offered/transmitted - in xmit or waiting reply */
220 struct iovec xmit[CONNIOVS];
221 XmitDetails xmitd[CONNIOVS];
227 static int filemon_init(void);
228 static void filemon_setfile(int mainfeed_fd, const char *mainfeed_path);
229 static void filemon_callback(void);
232 #define CHILD_ESTATUS_STREAM 4
233 #define CHILD_ESTATUS_NOSTREAM 5
235 static int since_connect_attempt;
237 static LIST(Conn) idle, working, full;
239 static LIST(Article) *queue;
241 static void perhaps_close(int *fd) { if (*fd) { close(*fd); fd=0; } }
243 /*========== making new connections ==========*/
245 static int connecting_sockets[2]= {-1,-1};
246 static pid_t connecting_child;
248 static void report_child_status(const char *what, int status) {
249 if (WIFEXITED(status)) {
250 int es= WEXITSTATUS(status);
252 warn("%s: child died with error exit status %d",es);
253 } else if (WIFSIGNALED(status)) {
254 int sig= WTERMSIG(status);
255 const char *sigstr= strsignal(sig);
256 const char *coredump= WCOREDUMP(status) ? " (core dumped)" : "";
258 warn("%s: child died due to fatal signal %s%s", what, sigstr, coredump);
260 warn("%s: child died due to unknown fatal signal %d%s",
261 what, sig, coredump);
263 warn("%s: child died with unknown wait status %d", status);
267 static void connect_attempt_discard(void) {
268 if (connecting_sockets[0]) {
269 cancel_fd(loop, connecting_sockets[0], OOP_READ);
270 cancel_fd(loop, connecting_sockets[0], OOP_EXCEPTION);
272 perhaps_close(&connecting_sockets[0]);
273 perhaps_close(&connecting_sockets[1]);
275 if (connecting_child) {
277 r= kill(connecting_child, SIGKILL);
278 if (r) sysdie("cannot kill connect child");
280 pid_t got= waitpid(connecting_child, &status, WNOHANG);
281 if (got==-1) sysdie("cannot reap connect child");
283 if (!(WIFEXITED(status) ||
284 (WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL))) {
285 report_child_status("connect"
291 #define PREP_DECL_MSG_CMSG(msg) \
293 memset(&msg,0,sizeof(msg)); \
294 char msg##cbuf[CMSG_SPACE(sizeof(fd))]; \
295 msg.msg_control= msg##cbuf; \
296 msg.msg_controllen= sizeof(msg##cbuf);
298 static void *connchild_event(oop_source *lp, int fd, oop_event e, void *u) {
301 conn= xcalloc(sizeof(*conn));
304 struct cmsghdr *h= 0;
305 ssize_t rs= recvmsg(fd, &msg, MSG_DONTWAIT);
306 if (rs >= 0) h= CMSG_FIRSTHDR(&msg);
309 pid_t got= waitpid(connecting_child, &status, WNOHANG);
311 assert(got==connecting_child);
313 if (WIFEXITED(status) &&
314 (WEXITSTATUS(status) != 0
315 WEXITSTATUS(status) != CHILD_ESTATUS_STREAM &&
316 WEXITSTATUS(status) != CHILD_ESTATUS_NOSTREAM)) {
317 /* child already reported the problem */
318 } else if (WIFSIGNALED(status) && WTERMSIG(status) == SIGALARM) {
319 warn("connect: connection attempt timed out");
320 } else if (!WIFEXITED(status)) {
321 report_child_status("connect", status);
322 /* that's probably the root cause then */
325 /* child is still running apparently, report the socket problem */
327 syswarn("connect: read from child socket failed");
328 else if (e == OOP_EXCEPTIONN)
329 warn("connect: unexpected exception on child socket");
331 warn("connect: unexpected EOF on child socket");
336 #define CHK(field, val) \
337 if (h->cmsg_##field != val) { \
338 die("connect: child sent cmsg with cmsg_" #field "=%d, expected %d"); \
341 CHK(level, SOL_SOCKET);
342 CHK(type, SCM_RIGHTS);
343 CHK(len, CMSG_LEN(sizeof(conn-b>fd)));
346 if (CMSG_NXTHDR,&msg,h) { die("connect: child sent many cmsgs"); goto x; }
348 memcpy(&conn->fd, CMSG_DATA(h), sizeof(conn->fd));
350 pid_t got= waitpid(connecting_child, &status, 0);
351 if (got==-1) sysdie("connect: real wait for child");
352 assert(got == connecting_child);
355 if (!WIFEXITED(status)) { report_child_status("connect",status); goto x; }
356 int es= WEXITSTATUS(status);
358 case CHILD_ESTATUS_STREAM: conn->stream= 1; break;
359 case CHILD_ESTATUS_NOSTREAM: conn->stream= 0; break;
361 die("connect: child gave unexpected exit status %d", es);
367 LIST_ADDHEAD(idle, conn);
368 notice(CN "connected %s", conn->fd, conn->stream ? "streaming" : "plain");
369 connect_attempt_discard();
370 check_master_queue();
375 perhaps_close(&conn->fd);
378 connect_attempt_discard();
381 static void connect_start() {
382 assert(!connecting_sockets[0]);
383 assert(!connecting_sockets[1]);
384 assert(!connecting_child);
386 notice("starting connection attempt");
388 r= socketpair(AF_UNIX, SOCK_STREAM, 0, connecting_sockets);
389 if (r) { syswarn("connect: cannot create socketpair for child"); goto x; }
391 connecting_child= fork();
392 if (connecting_child==-1) { syswarn("connect: cannot fork"); goto x; }
394 if (!connecting_child) {
395 FILE *cn_from, *cn_to;
396 char buf[NNTP_STRLEN+100];
397 int exitstatus= CHILD_ESTATUS_NOSTREAM;
402 r= close(connecting_sockets[0]);
403 if (r) sysdie("connect: close parent socket in child");
405 alarm(connection_setup_timeout);
406 if (NNTPconnect(remote_host, port, &cn_from, &cn_to, buf) < 0) {
408 sanitise_inplace(buf);
409 die("connect: rejected: %s", buf);
411 sysdie("connect: connection attempt failed");
414 if (NNTPsendpassword(remote_host, cn_from, cn_to) < 0)
415 sysdie("connect: authentication failed");
417 if (fputs("MODE STREAM\r\n", cn_to) ||
419 sysdie("connect: could not send MODE STREAM");
420 buf[sizeof(buf)-1]= 0;
421 if (!fgets(buf, sizeof(buf)-1, cn_from)) {
423 sysdie("connect: could not read response to MODE STREAM");
425 die("connect: connection close in response to MODE STREAM");
430 sanitise_inplace(buf);
431 die("connect: response to MODE STREAM is too long: %.100s...",
434 l--; if (l>0 && buf[1-]=='\r') l--;
437 int rcode= strtoul(buf,&ep,10);
439 sanitise_inplace(buf);
440 die("connect: bad response to MODE STREAM: %.50s", buf);
444 exitstatus= CHILD_ESTATUS_STREAM;
450 sanitise_inplace(buf);
451 warn("connect: unexpected response to MODE STREAM: %.50s", buf);
456 int fd= fileno(cn_from);
458 PREP_DECL_MSG_CMSG(msg);
459 struct cmsghdr *cmsg= CMSG_FIRSTHDR(&msg);
460 cmsg->cmsg_level= SOL_SOCKET;
461 cmsg->cmsg_type= SCM_RIGHTS;
462 cmsg->cmsg_len= CMSG_LEN(sizeof(fd));
463 memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd));
465 msg.msg_controllen= cmsg->cmsg_len;
466 r= sendmsg(connecting_sockets[1], &msg, 0);
467 if (r) sysdie("sendmsg failed for new connection");
472 r= close(connecting_sockets[1]); connecting_sockets[1]= 0;
473 if (r) sysdie("connect: close child socket in parent");
475 loop->on_fd(loop, connecting_sockets[0], OOP_READ, connchild_event, 0);
476 loop->on_fd(loop, connecting_sockets[0], OOP_EXCEPTION, connchild_event, 0);
480 connect_attempt_discard();
483 /*========== overall control of article flow ==========*/
485 static void conn_check_work(Conn *conn);
487 static void check_master_queue(void) {
488 try reading current feed file;
493 Conn *last_assigned=0;
496 conn_assign_one_article(&working, &last_assigned);
497 } else if (idle.head) {
498 conn_assign_one_article(&idle, &last_assigned);
499 } else if (nconns < maxconns && queue.count >= max_queue_per_conn &&
500 !connecting_child && !connect_delay) {
501 connect_delay= reconnect_delay_periods;
507 conn_check_work(last_assigned);
510 static void conn_assign_one_article(LIST(Conn) *connlist,
511 Conn **last_assigned) {
512 Conn *conn= connlist->head;
514 LIST_REMOVE(*connlist, conn);
515 Article *art= LIST_REMHEAD(queue);
516 LIST_ADDTAIL(conn->queue, art);
517 LIST_ADD(*conn_determine_right_list(conn), conn);
519 /* This slightly odd arrangement is so that we call conn_check_work
520 * once after filling the queue for a new connection in
521 * check_master_queue, rather than for each article. */
522 if (conn != *last_assigned && *last_assigned)
523 conn_check_work(*last_assigned);
524 *last_assigned= conn;
527 static int conn_total_queued_articles(Conn *conn) {
528 return conn->sent.count + conn->queue.count;
531 static LIST(Conn) *conn_determine_right_list(Conn *conn) {
532 int inqueue= conn_total_queued_articles(conn);
533 assert(inqueue <= max_queue);
534 if (inqueue == 0) return &idle;
535 if (inqueue == conn->max_queue) return &full;
539 static void *conn_writeable(oop_source *l, int fd, int ev, void *u) {
544 static void conn_check_work(Conn *conn) {
547 conn_make_some_xmits(conn);
549 loop->cancel_fd(loop, conn->fd, OOP_WRITE);
553 void *rp= conn_write_some_xmits(conn);
554 if (rp==OOP_CONTINUE) {
555 loop->on_fd(loop, conn->fd, OOP_WRITE, conn_writeable, conn);
557 } else if (rp==OOP_HALT) {
560 /* transmitted everything */
567 /*========== article transmission ==========*/
570 static XmitDetails *xmit_core(Conn *conn, const char *data, int len,
571 XmitKind kind) { /* caller must then fill in details */
572 struct iovec *v= &conn->xmit[conn->xmitu];
573 XmitDetails *d= &conn->xmitd[conn->xmitu++];
580 static void xmit_noalloc(Conn *conn, const char *data, int len) {
581 xmit_core(conn,data,len, xk_Const);
583 #define XMIT_LITERAL(lit) (xmit_noalloc(conn, (lit), sizeof(lit)-1))
585 static void xmit_artbody(Conn *conn, ARTHANDLE *ah /* consumed */) {
586 XmitDetails *d= xmit_core(conn, ah->data, ah->len, sk_Artdata);
590 static void xmit_free(XmitDetails *d) {
592 case xk_Malloc: free(d->info.malloc_tofree); break;
593 case xk_Artdata: SMfreearticle(d->info.sm_art); break;
594 case xk_Const: break;
599 static void *conn_write_some_xmits(Conn *conn) {
601 * 0: nothing more to write, no need to call us again
602 * OOP_CONTINUE: more to write but fd not writeable
603 * OOP_HALT: disaster, have destroyed conn
606 int count= conn->xmitu;
607 if (!count) return 0;
609 if (count > IOV_MAX) count= IOV_MAX;
610 ssize_t rs= writev(conn->fd, conn->xmit, count);
612 if (errno == EAGAIN) return OOP_CONTINUE;
613 syswarn(CN "write failed", conn->fd);
619 for (done=0; rs && done<xmitu; done++) {
620 struct iovec *vp= &conn->xmit[done];
621 XmitDetails *dp= &conn->xmitd[done];
622 if (rs > vp->iov_len) {
630 int newu= conn->xmitu - done;
631 memmove(conn->xmit, conn->xmit + done, newu * sizeof(*conn->xmit));
632 memmove(conn->xmitd, conn->xmitd + done, newu * sizeof(*conn->xmitd));
637 static void conn_make_some_xmits(Conn *conn) {
639 if (conn->xmitu+5 > CONNIOVS)
642 Article *art= LIST_REMHEAD(queue);
645 if (art->checked || (conn->stream && nocheck)) {
646 /* actually send it */
648 ARTHANDLE *artdata= SMretrieve();
652 XMIT_LITERAL("TAKETHIS ");
653 xmit_noalloc(conn, art->mid, art->midlen);
654 XMIT_LITERAL("\r\n");
655 xmit_artbody(conn, artdata);
658 /* we got 235 from IHAVE */
660 xmit_artbody(conn, artdata);
662 XMIT_LITERAL(".\r\n");
667 LIST_ADDTAIL(conn->sent, art);
669 counts[art->checked].sent++;
675 XMIT_LITERAL("IHAVE ");
677 XMIT_LITERAL("CHECK ");
678 xmit_noalloc(art->mid, art->midlen);
679 XMIT_LITERAL("\r\n");
681 LIST_ADDTAIL(conn->sent, art);
682 counts[art->checked].offered++;
687 /*========== responses from peer ==========*/
689 static const oop_rd_style peer_rd_style= {
690 OOP_RD_DELIM_STRIP, '\n',
692 OOP_RD_SHORTREC_FORBID
695 static Article *article_reply_check(Connection *conn, const char *response,
696 int code_indicates_streaming,
697 const char *sanitised_response) {
698 Article *art= LIST_REMHEAD(conn->sent);
701 warn("peer gave unexpected response when no commands outstanding: %s",
706 if (code_indicates_streaming) {
707 assert(!memchr(response, 0, 4)); /* ensured by peer_rd_ok */
709 warn("peer gave streaming response code "
710 " to IHAVE or subsequent body: %s", sanitised_response);
713 const char *got_mid= response+4;
714 int got_midlen= strcspn(got_mid, " \n\r");
715 if (got_midlen<3 || got_mid[0]!='<' || got_mid[got_midlen-1]!='>') {
716 warn("peer gave streaming response with syntactically invalid"
717 " messageid: %s", sanitised_response);
720 if (got_midlen != art->midlen ||
721 memcmp(got_mid, art->messageid, got_midlen)) {
722 peer("peer gave streaming response code to wrong article -"
723 " probable synchronisation problem; we offered: %s; peer said: %s",
724 art->messageid, sanitised_response);
729 warn("peer gave non-streaming response code to CHECK/TAKETHIS: %s",
742 static void update_nocheck(int accepted) {
743 accept_proportion *= accept_decay;
744 accept_proportion += accepted;
745 nocheck= accept_proportion >= nocheck_thresh;
746 if (nocheck && !nocheck_reported) {
747 notice("entering nocheck mode for the first time");
752 static void article_done(Connection *conn, Article *art, int whichcount) {
754 counts.articles[art->checked][whichcount]++;
755 if (whichcount == RC_accepted) update_nocheck(1);
756 else if (whichcount == RC_unwanted) update_nocheck(0);
758 InputFile *ipf= art->ipf;
759 while (art->blanklen) {
760 static const char spaces[]=
765 int w= art->blanklen; if (w >= sizeof(spaces)) w= sizeof(spaces)-1;
766 int r= pwrite(ipf->fd, spaces, w, art->offset);
768 if (errno==EINTR) continue;
769 sysdie("failed to blank entry for %s (length %d at offset %lu) in %s",
770 art->messageid, art->blanklen, art->offset, ipf->path);
772 assert(r>=0 && r<=w);
778 assert(ipf->inprogress >= 0);
783 static void *peer_rd_ok(oop_source *lp, oop_read *oread, oop_event ev,
784 const char *errmsg, int errnoval,
785 const char *data, size_t recsz, void *conn_v) {
788 if (ev == OOP_RD_EOF) {
789 warn("unexpected EOF from peer");
793 assert(ev == OOP_RD_OK);
796 unsigned long code= strtoul(data, &ep, 10);
797 if (ep != data+3 || *ep != ' ' || data[0]=='0') {
803 if (q > sanibuf+sizeof(sanibuf)-8) { strcpy(q,"..."); break; }
805 if (!c) { *q++= '\''; break; }
806 if (c>=' ' && c<=126 && c!='\\') { *q++= c; continue; }
807 sprintf(q,"\\x%02x",c);
810 warn("badly formatted response from peer: %s", sanibuf);
815 if (conn->quitting) {
817 warn("peer gave failure response to QUIT: %s", sani);
827 #define GET_ARTICLE \
828 art= article_reply_check(conn, data, code_streaming, sani); \
829 if (art) ; else return OOP_CONTINUE /* reply_check has failed the conn */
831 #define ARTICLE_DEALTWITH(streaming,how) \
832 code_streaming= (streaming) \
834 article_done(conn, art, RC_##how); break;
836 int code_streaming= 0;
840 case 435: ARTICLE_DEALTWITH(0,unwanted); /* IHAVE says they have it */
841 case 438: ARTICLE_DEALTWITH(1,unwanted); /* CHECK/TAKETHIS: they have it */
843 case 235: ARTICLE_DEALTWITH(0,accepted); /* IHAVE says thanks */
844 case 239: ARTICLE_DEALTWITH(1,accepted); /* TAKETHIS says thanks */
846 case 437: ARTICLE_DEALTWITH(0,rejected); /* IHAVE says rejected */
847 case 439: ARTICLE_DEALTWITH(1,rejected); /* TAKETHIS says rejected */
849 case 238: /* CHECK says send it */
851 case 335: /* IHAVE says send it */
853 count_checkedwanted++;
854 LIST_ADDTAIL(conn->queue);
856 warn("peer gave %d response to article body",code);
862 case 431: /* CHECK or TAKETHIS says try later */
864 case 436: /* IHAVE says try later */
866 if (fprintf(defer, "%s %s\n", TokenToText(art->token), art->messageid) <0
868 sysdie("write to defer file %s",path_ductdefer);
869 article_done(conn, art, RC_deferred);
872 case 400: warn("peer has stopped accepting articles: %s", sani); goto failed;
873 case 503: warn("peer timed us out: %s", sani); goto failed;
874 default: warn("peer sent unexpected message: %s", sani); goto failed;
878 return OOP_CONTINUE;;
881 check_check_work(conn);
885 /*========== monitoring of input file ==========*/
887 static void feedfile_eof(InputFile *ipf) {
888 assert(ipf != main_input_file); /* promised by tailing_try_read */
889 assert(ipf == old_input_file);
890 assert(sms == sm_SEPARATED);
892 inputfile_tailing_stop(ipf);
893 inputfile_tailing_start(main_input_file);
896 static void statmc_finishdone(void) {
900 assert(sms == sm_FINISHING);
902 r= fstat(fileno(defer), &stab);
903 if (r) sysdie("check defer file %s", path_defer);
905 if (fclose(defer)) sysdie("could not close defer file %s", path_defer);
909 if (now==-1) sysdie("could not get current time for backlog filename");
911 char *backlog= xasprintf("%s_backlog_%lu.%lu", feedfile,
913 (unsigned long)stab.st_ino);
914 if (link(path_defer, path_backlog))
915 sysdie("could not install defer file %s as backlog file %s",
916 path_defer, backlog);
917 if (unlink(path_defer))
918 sysdie("could not unlink old defer link %s to backlog file %s",
919 path_defer, backlog);
922 close_input_file(old_input_file);
925 if (unlink(path_duct))
926 sysdie("could not unlink old duct file %s", path_duct);
931 static InputFile *open_input_file(const char *path) {
932 int fd= open(path, O_RDONLY);
934 if (errno==ENOENT) return 0;
935 sysdie("unable to open input file %s", path);
938 InputFile *ipf= xmalloc(sizeof(InputFile));
939 memset(ipf,0,sizeof(*ipf));
941 ipf->readable.on_readable= tailing_on_readable;
942 ipf->readable.on_cancel= tailing_on_cancel;
943 ipf->readable.try_read= tailing_try_read;
951 static void close_input_file(InputFile *ipf) {
952 assert(!ipf->readable_callback); /* must have had ->on_cancel */
953 assert(!ipf->filemon); /* must have had inputfile_tailing_stop */
954 assert(!ipf->rd); /* must have had inputfile_tailing_stop */
955 assert(!ipf->inprogress); /* no dangling pointers pointing here */
957 if (close(ipf->fd)) sysdie("could not close input file %s", ipf->path);
961 /*---------- dealing with articles read in the input file ----------*/
963 typedef void *feedfile_got_article(oop_source *lp, oop_read *rd,
964 oop_rd_event ev, const char *errmsg,
966 const char *data, size_t recsz,
968 InputFile *ipf= ipf_v;
970 char tokentextbuf[sizeof(TOKEN)*2+3];
972 if (!data) { feedfile_eof(ipf); return OOP_CONTINUE; }
974 if (data[0] && data[0]!=' ') {
975 char *space= strchr(data,' ');
976 int tokenlen= space-data;
977 int midlen= (int)recsz-tokenlen-1;
978 if (midlen < 0) goto bad_data;
980 if (tokenlen != sizeof(TOKEN)*2+2) goto bad_data;
981 memcpy(tokentextbuf, data, tokenlen);
982 tokentextbuf[tokenlen]= 0;
983 if (!IsToken(tokentextbuf)) goto bad_data;
985 art= xmalloc(sizeof(*art) - 1 + midlen + 1);
986 art->offset= ipf->offset;
987 art->blanklen= recsz;
989 art->checked= art->sentbody= 0;
990 art->ipf= ipf; ipf->inprogress++;
991 art->token= TextToToken(tokentextbuf);
992 strcpy(art->messageid, space+1);
993 LIST_ADDTAIL(queue, art);
995 ipf->offset += recsz + 1;
997 if (sms==sm_NORMAL && ipf->offset >= flush_threshold) {
998 int r= link(feedfile, duct_path);
999 if (r) sysdie("link feedfile %s to ductfile %s", feedfile, dut_path);
1002 r= unlink(feedfile);
1003 if (r) sysdie("unlink old feedfile link %s", feedfile);
1006 spawn_inndcomm_flush(); /* => Flushing, sets sms to sm_FLUSHING */
1009 check_master_queue();
1012 /*---------- tailing input file ----------*/
1014 static void filemon_start(InputFile *ipf) {
1015 assert(!ipf->filemon);
1017 ipf->filemon= xmalloc(sizeof(*ipf->filemon));
1018 memset(ipf->filemon, 0, sizeof(*ipf->filemon));
1019 filemon_method_startfile(ipf, ipf->filemon);
1022 static void filemon_stop(InputFile *ipf) {
1023 if (!ipf->filemon) return;
1024 filemon_method_stopfile(ipf, ipf->filemon);
1029 static void filemon_callback(InputFile *ipf) {
1030 ipf->readable_callback(ipf->readable_callback_user);
1033 static void *tailing_rable_call_time(oop_source *loop, struct timeval tv,
1035 InputFile *ipf= user;
1036 return ipf->readable_callback(ipf->readable_callback_user);
1039 static void on_cancel(struct oop_readable *rable) {
1040 InputFile *ipf= (void*)rable;
1042 if (ipf->filemon) filemon_stopfile(ipf);
1043 loop->cancel_time(loop, OOP_TIME_NOW, tailing_rable_call_time, ipf);
1044 ipf->readable_callback= 0;
1047 static int tailing_on_readable(struct oop_readable *rable,
1048 oop_readable_call *cb, void *user) {
1049 InputFile *ipf= (void*)rable;
1051 tailing_on_cancel(rable);
1052 ipf->readable_callback= cb;
1053 ipf->readable_callback_user= user;
1054 filemon_startfile(ipf);
1056 loop->on_time(loop, OOP_TIME_NOW, tailing_rable_call_time, ipf);
1060 static ssize_t tailing_try_read(struct oop_readable *rable, void *buffer,
1062 InputFile *ipf= (void*)rable;
1064 ssize_t r= read(ipf->fd, buffer, length);
1065 if (!r && ipf==main_input_file) { errno=EAGAIN; return -1; }
1066 if (r==-1 && errno==EINTR) continue;
1071 /*---------- filemon implemented with inotify ----------*/
1073 #if defined(HAVE_INOTIFY) && !defined(HAVE_FILEMON)
1074 #define HAVE_FILEMON
1076 #include <linux/inotify.h>
1078 static int filemon_inotify_fd;
1079 static int filemon_inotify_wdmax;
1080 static InputFile **filemon_inotify_wd2ipf;
1082 typedef struct Filemon_Perfile {
1084 } Filemon_Inotify_Perfile;
1086 static void filemon_method_startfile(InputFile *ipf, Filemon_Perfile *pf) {
1087 int wd= inotify_add_watch(filemon_inotify_fd, ipf->path, IN_MODIFY);
1088 if (wd < 0) sysdie("inotify_add_watch %s", ipf->path);
1090 if (wd >= filemon_inotify_wdmax) {
1092 filemon_inotify_wd= xrealloc(filemon_inotify_wd2ipf,
1093 sizeof(*filemon_inotify_wd2ipf) * newmax);
1094 memset(filemon_inotify_wd2ipf + filemon_inotify_wdmax, 0,
1095 sizeof(*filemon_inotify_wd2ipf) * (newmax - filemon_inotify_wdmax));
1096 filemon_inotify_wdmax= newmax;
1099 assert(!filemon_inotify_wd2ipf[wd]);
1100 filemon_inotify_wd2ipf[wd]= ipf;
1105 static void filemon_method_stopfile(InputFile *ipf, Filemon_Perfile *pf) {
1107 int r= inotify_rm_watch(filemon_inotify_fd, filemon_inotify_wd);
1108 if (r) sysdie("inotify_rm_watch");
1109 filemon_inotify_wd2ipf[wd]= 0;
1112 static void *filemon_inotify_readable(oop_source *lp, int fd,
1113 oop_event e, void *u) {
1114 struct inotify_event iev;
1116 int r= read(filemon_inotify_fd, &iev, sizeof(iev));
1118 if (errno==EAGAIN) break;
1119 sysdie("read from inotify master");
1120 } else if (r==sizeof(iev)) {
1121 assert(iev.wd >= 0 && iev.wd < filemon_inotify_wdmax);
1123 die("inotify read %d bytes wanted struct of %d", r, (int)sizeof(iev));
1125 InputFile *ipf= filemon_inotify_wd2ipf[iev.wd];
1126 filemon_callback(ipf);
1128 return OOP_CONTINUE;
1131 static int filemon_method_init(void) {
1132 filemon_inotify_fd= inotify_init();
1133 if (filemon_inotify_fd<0) {
1134 syswarn("could not initialise inotify: inotify_init failed");
1138 loop->on_fd(loop, filemon_inotify_fd, OOP_READ, filemon_inotify_readable);
1143 #endif /* HAVE_INOTIFY && !HAVE_FILEMON *//
1145 /*---------- filemon dummy implementation ----------*/
1147 #if !defined(HAVE_FILEMON)
1149 typedef struct Filemon_Perfile { int dummy; } Filemon_Dummy_Perfile;
1151 static int filemon_method_init(void) { return 0; }
1152 static void filemon_method_startfile(InputFile *ipf, Filemon_Perfile *pf) { }
1153 static void filemon_method_stopfile(InputFile *ipf, Filemon_Perfile *pf) { }
1155 #endif /* !HAVE_FILEMON */
1157 /*---------- interface to start and stop an input file ----------*/
1159 static const oop_rd_style feedfile_rdstyle= {
1160 OOP_RD_DELIM_STRIP, '\n',
1162 OOP_RD_SHORTREC_EOF,
1165 static void inputfile_tailing_start(InputFile *ipf) {
1167 ipf->readable->on_readable= tailing_on_readable;
1168 ipf->readable->on_cancel= tailing_on_cancel;
1169 ipf->readable->try_read= tailing_try_read;
1170 ipf->readable->delete_tidy= 0; /* we never call oop_rd_delete_{tidy,kill} */
1171 ipf->readable->delete_kill= 0;
1173 ipf->readable_callback= 0;
1174 ipf->readable_callback_user= 0;
1176 ipf->rd= oop_rd_new(loop, &ipf->readable, 0,0);
1179 int r= oop_rd_read(ipf->rd, &feedfile_rdstyle, MAX_LINE_FEEDFILE,
1180 feedfile_got_article,ipf, feedfile_problem,ipf);
1181 if (r) sysdie("unable start reading feedfile %s",ipf->path);
1184 static void inputfile_tailing_stop(InputFile *ipf) {
1186 oop_rd_delete(ipf->rd);
1188 assert(!ipf->filemon); /* we shouldn't be monitoring it now */
1191 /*========== interaction with innd ==========*/
1193 /* See official state diagram at top of file. We implement
1203 |`--------------------------.
1204 | | install defer as backlog
1205 | OPEN F SUCCEEDS | exit
1210 | [Normal] =========
1214 | | F IS SO BIG WE SHOULD FLUSH
1218 | | our handle onto F is now onto D
1221 | |<---------------------------------------------------.
1223 | | spawn inndcomm flush |
1231 | | INNDCOMM FLUSH FAILS ^
1232 | |`----------------------->--------. |
1234 | | NO SUCH SITE V |
1235 ^ |`----------------. ========= |
1238 | | ========== read D |
1239 | | DROPPING ========= |
1241 | | read D | TIME TO RETRY |
1242 | | ========== `------------------'
1244 | | open F | AT EOF OF D AND ALL PROCESSED
1245 | V | install defer as backlog
1246 | =========== | unlink D
1250 | =========== (ESRCH)
1264 | | install defer as backlog
1274 static char *path_ductlock, *path_duct, *path_ductdefer;
1277 /* This is an instance of struct oop_readable */
1278 struct oop_readable readable; /* first */
1279 oop_readable_call *readable_callback;
1280 void *readable_callback_user;
1283 const char *path; /* ptr copy of path_<foo> or feedfile */
1284 struct Filemon_Perfile *filemon;
1287 long inprogress; /* no. of articles read but not processed */
1299 } StateMachineState;
1301 static InputFile *main_input_file, *old_input_file;
1302 static StateMachineState sms;
1303 static int waiting_periods_sofar;
1305 static void open_defer(void) {
1309 defer= fopen(path_ductdefer, "a+");
1310 if (!defer) sysdie("could not open defer file %s", path_ductdefer);
1312 /* truncate away any half-written records */
1314 r= fstat(fileno(defer), &stab);
1315 if (r) sysdie("could not stat newly opened defer file %s", path_ductdefer);
1317 if (stab.st_size > LONG_MAX)
1318 die("defer file %s size is far too large", path_ductdefer);
1323 long orgsize= stab.st_size;
1324 long truncto= stab.st_size;
1326 if (!truncto) break; /* was only (if anything) one half-truncated record */
1327 if (fseek(defer, truncto-1, SEEK_SET) < 0)
1328 sysdie("seek in defer file %s while truncating partial", path_ductdefer);
1333 sysdie("failed read from defer file %s", path_ductdefer);
1335 die("defer file %s shrank while we were checking it!", path_ductdefer);
1341 if (stab.st_size != truncto) {
1342 warn("truncating half-record at end of defer file %s -"
1343 " shrinking by %ld bytes from %ld to %ld",
1344 path_ductdefer, orgsize - truncto, orgsize, truncto);
1346 if (fflush(defer)) sysdie("could not flush defer file %s", path_ductdefer);
1347 if (ftruncate(fileno(defer), truncto))
1348 sysdie("could not truncate defer file %s", path_ductdefer);
1351 info("continuing existing defer file %s (%ld bytes)",
1352 path_ductdefer, orgsize);
1354 if (fseek(defer, truncto, SEEK_SET))
1355 sysdie("could not seek to new end of defer file %s", path_ductdefer);
1358 static void statemc_init(void) {
1361 path_ductlock= xasprintf("%s_duct.lock", feedfile);
1362 path_duct= xasprintf("%s_duct", feedfile);
1363 path_ductdefer= xasprintf("%s_duct.defer", feedfile);
1365 if (lstat(path_ductdefer, &stab)) {
1366 if (errno!=ENOENT) sysdie("could not check defer file %s", path_defer);
1368 if (!S_ISREG(stab.st_mode))
1369 die("defer file %s not a plain file (mode 0%lo)",
1370 path_defer, (unsigned long)stab.st_mode);
1371 switch (stab.st_nlink==1) {
1372 case 1: /* ok */ break;
1374 if (unlink(path_defer))
1375 sysdie("could not unlink stale defer file link %s (presumably"
1376 " hardlink to backlog file)", path_defer);
1379 die("defer file %s has unexpected link count %d",
1380 path_defer, stab.st_nlink);
1385 int lockfd= open(path_ductlock, O_CREAT|O_RDWR, 0600);
1386 if (lockfd<0) sysdie("open lockfile %s", path_ductlock);
1389 memset(&fl,0,sizeof(fl));
1391 fl.l_whence= SEEK_SET;
1392 r= fcntl(lockfd, F_SETLK, &fl);
1394 if (errno==EACCES || errno==EAGAIN)
1395 die("another duct holds the lockfile");
1396 sysdie("fcntl F_SETLK lockfile %s", path_ductlock);
1399 InputFile *file_d= open_input_file(path_duct);
1402 struct stat stab_f, stab_d;
1404 r= stat(feedfile, &stab_f);
1406 if (errno!=ENOENT) sysdie("check feed file %s", feedfile);
1407 /* D exists, F ENOENT => Moved */
1411 /* F and D both exist */
1413 r= fstat(file_d->fd, &stab_d);
1414 if (r) sysdie("check duct file %s", ductfile);
1416 if (stab_d.st_ino == stab_f.st_ino &&
1417 stab_d.st_dev == stab_f.st_dev) {
1418 /* F==D => Hardlinked*/
1419 r= unlink(path_duct);
1420 if (r) sysdie("unlink feed file %s during startup", feedfile);
1423 startup_set_input_file(file_d);
1424 spawn_inndcomm_flush(); /* => Flushing, sets sms to sm_FLUSHING */
1426 /* F!=D => Separated */
1428 startup_set_input_file(file_d);
1430 } else { /*!file_d*/
1432 statemc_waiting_poll();
1436 static void statemc_poll(void) {
1437 if (sms == sm_WAITING) statemc_waiting_poll();
1438 if (sms == sm_FINISHING && !old_input_file->inprogress) statemc_finishdone();
1441 static void statemc_waiting_poll(void) {
1442 InputFile *file_f= open_input_file(feedfile);
1444 if (waiting_periods_sofar++ > waiting_timeout_periods)
1445 die("timed out waiting for innd to create feed file %s", feedfile);
1448 startup_set_input_file(file_d);
1452 static void startup_set_input_file(InputFile *f) {
1453 assert(!main_input_file);
1455 inputfile_tailing_start(f);
1458 /*========== flushing the feed ==========*/
1462 /*========== main program ==========*/
1464 #define EVERY(what, interval, body) \
1465 static const struct timeval what##_timeout = { 5, 0 }; \
1466 static void what##_schedule(void); \
1467 static void *what##_timedout(oop_source *lp, struct timeval tv, void *u) { \
1469 what##_schedule(); \
1471 static void what##_schedule(void) { \
1472 loop->on_time(loop, what##_timeout, what##_timedout, 0); \
1475 EVERY(filepoll, {5,0}, { check_master_queue(); })
1477 EVERY(period, {PERIOD_SECONDS,0}, {
1478 if (connect_delay) connect_delay--;
1480 check_master_queue();
1485 if (!filemon_init())
1486 filepoll_schedule();