5 * site.name_duct.lock lock preventing multiple ducts
6 * holder of this lock is "duct"
7 * F site.name main feed file
8 * opened/created, then written, by innd
11 * tokens blanked out by duct when processed
12 * D site.name_duct temporary feed file during flush (or crash)
13 * hardlink created by duct
15 * site.name_duct.defer 431'd articles, still being written,
16 * created, written, used by duct
17 * site.name_backlog.lock lock taken out by innxmit wrapper
18 * holder and its child are "xmit"
19 * site.name_backlog_<inum> 431'd articles, ready for innxmit
20 * created (link/mv) by duct
23 * site.name_backlog_<letters> eg
24 * site.name_backlog_manual
25 * anything the sysadmin likes (eg, feed files
26 * from old feeds to be merged into this one)
27 * created (link/mv) by admin
38 <--------------------------------------'|
39 Nothing F, D both ENOENT |
42 duct: not not reading anything |
44 |`---------------------. |
45 | | duct times out waiting for F |
46 V innd creates F | duct exits |
48 Noduct GO TO Dropped |
51 duct: not running or not reading anything |
54 ,-->--+ <---------------------------------'|
55 | | duct opens F F exists |
59 | F: innd writing, duct reading |
62 | | duct decides time to flush |
63 | | duct makes hardlink |
65 | V <------------------------'|
67 | F == D: innd writing, duct reading both exist |
71 | Moved <----+------------<--'|
72 | F: ENOENT | F ENOENT |
73 | D: innd writing, duct reading | D exists |
75 | | duct requests flush of feed | |
76 | | (others can too, harmlessly) | |
80 | D: innd flushing, duct reading | |
82 | | inndcomm flush fails | |
83 | |`-------------------------->---------' |
85 | | inndcomm reports no such site |
86 | |`---------------------------------------------------- | -.
88 | | innd finishes writing D, creates F | |
89 | | inndcomm reports flush successful | |
90 | | duct opens F too | |
92 | Flushed <----------------' |
93 | F: innd writing, duct reading F!=D /
94 | D: duct reading both exist /
96 | | duct gets to the end of D /
97 | V duct finishes processing D /
115 "duct reading" means innduct is reading the file but also
116 overwriting processed tokens.
122 #define PERIOD_SECONDS 60
124 static char *feedfile;
125 static int max_connections, max_queue_per_conn;
126 static int connection_setup_timeout, port, try_stream;
127 static const char *remote_host;
129 #define ISNODE(T) T *next, *back;
130 #define LIST(T) struct { T *head, *tail, *tailpred; int count; }
132 #define NODE(n) ((struct node*)&(n)->head)
134 #define LIST_ADDHEAD(l,n) \
135 (list_addhead((struct list*)&(l), NODE((n))), (void)(l).count++)
136 #define LIST_ADDTAIL(l,n) \
137 (list_addtail((struct list*)&(l), NODE((n))), (void)(l).count++)
139 #define LIST_REMHEAD(l) \
140 ((l).count ? ((l).count--, (void*)list_remhead((struct list*)&(l))) : 0)
141 #define LIST_REMTAIL(l) \
142 ((l).count ? ((l).count--, (void*)list_remtail((struct list*)&(l))) : 0)
143 #define LIST_REMOVE(l,n) \
144 (list_remove(NODE((n))), (void)(l).count--)
145 #define LIST_INSERT(l,n,pred) \
146 (list_insert((struct list*)&(l), NODE((n)), NODE((pred))), (void)(l).count++)
151 int checked, sentbody;
152 fd and offset for blanking token or mid;
159 typedef struct Conn Conn;
162 Malloc, Const, Artdata;
175 int fd, max_queue, stream;
176 LIST(Article) queue; /* not yet told peer, or CHECK said send it */
177 LIST(Article) sent; /* offered/transmitted - in xmit or waiting reply */
178 struct iovec xmit[CONNIOVS];
179 XmitDetails xmitd[CONNIOVS];
183 static int filemon_init(void);
184 static void filemon_setfile(int mainfeed_fd, const char *mainfeed_path);
185 static void filemon_callback(void);
188 #define CHILD_ESTATUS_STREAM 4
189 #define CHILD_ESTATUS_NOSTREAM 5
191 static int since_connect_attempt;
193 static LIST(Conn) idle, working, full;
195 static LIST(Article) *queue;
197 static void perhaps_close(int *fd) { if (*fd) { close(*fd); fd=0; } }
199 /*========== making new connections ==========*/
201 static int connecting_sockets[2]= {-1,-1};
202 static pid_t connecting_child;
204 static void report_child_status(const char *what, int status) {
205 if (WIFEXITED(status)) {
206 int es= WEXITSTATUS(status);
208 warn("%s: child died with error exit status %d",es);
209 } else if (WIFSIGNALED(status)) {
210 int sig= WTERMSIG(status);
211 const char *sigstr= strsignal(sig);
212 const char *coredump= WCOREDUMP(status) ? " (core dumped)" : "";
214 warn("%s: child died due to fatal signal %s%s", what, sigstr, coredump);
216 warn("%s: child died due to unknown fatal signal %d%s",
217 what, sig, coredump);
219 warn("%s: child died with unknown wait status %d", status);
223 static void connect_attempt_discard(void) {
224 if (connecting_sockets[0]) {
225 cancel_fd(loop, connecting_sockets[0], OOP_READ);
226 cancel_fd(loop, connecting_sockets[0], OOP_EXCEPTION);
228 perhaps_close(&connecting_sockets[0]);
229 perhaps_close(&connecting_sockets[1]);
231 if (connecting_child) {
233 r= kill(connecting_child, SIGKILL);
234 if (r) sysdie("cannot kill connect child");
236 pid_t got= waitpid(connecting_child, &status, WNOHANG);
237 if (got==-1) sysdie("cannot reap connect child");
239 if (!(WIFEXITED(status) ||
240 (WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL))) {
241 report_child_status("connect"
247 #define PREP_DECL_MSG_CMSG(msg) \
249 memset(&msg,0,sizeof(msg)); \
250 char msg##cbuf[CMSG_SPACE(sizeof(fd))]; \
251 msg.msg_control= msg##cbuf; \
252 msg.msg_controllen= sizeof(msg##cbuf);
254 static void *connchild_event(oop_source *lp, int fd, oop_event e, void *u) {
257 conn= xcalloc(sizeof(*conn));
260 struct cmsghdr *h= 0;
261 ssize_t rs= recvmsg(fd, &msg, MSG_DONTWAIT);
262 if (rs >= 0) h= CMSG_FIRSTHDR(&msg);
265 pid_t got= waitpid(connecting_child, &status, WNOHANG);
267 assert(got==connecting_child);
269 if (WIFEXITED(status) &&
270 (WEXITSTATUS(status) != 0
271 WEXITSTATUS(status) != CHILD_ESTATUS_STREAM &&
272 WEXITSTATUS(status) != CHILD_ESTATUS_NOSTREAM)) {
273 /* child already reported the problem */
274 } else if (WIFSIGNALED(status) && WTERMSIG(status) == SIGALARM) {
275 warn("connect: connection attempt timed out");
276 } else if (!WIFEXITED(status)) {
277 report_child_status("connect", status);
278 /* that's probably the root cause then */
281 /* child is still running apparently, report the socket problem */
283 syswarn("connect: read from child socket failed");
284 else if (e == OOP_EXCEPTIONN)
285 warn("connect: unexpected exception on child socket");
287 warn("connect: unexpected EOF on child socket");
292 #define CHK(field, val) \
293 if (h->cmsg_##field != val) { \
294 die("connect: child sent cmsg with cmsg_" #field "=%d, expected %d"); \
297 CHK(level, SOL_SOCKET);
298 CHK(type, SCM_RIGHTS);
299 CHK(len, CMSG_LEN(sizeof(conn-b>fd)));
302 if (CMSG_NXTHDR,&msg,h) { die("connect: child sent many cmsgs"); goto x; }
304 memcpy(&conn->fd, CMSG_DATA(h), sizeof(conn->fd));
306 pid_t got= waitpid(connecting_child, &status, 0);
307 if (got==-1) sysdie("connect: real wait for child");
308 assert(got == connecting_child);
311 if (!WIFEXITED(status)) { report_child_status("connect",status); goto x; }
312 int es= WEXITSTATUS(status);
314 case CHILD_ESTATUS_STREAM: conn->stream= 1; break;
315 case CHILD_ESTATUS_NOSTREAM: conn->stream= 0; break;
317 die("connect: child gave unexpected exit status %d", es);
323 LIST_ADDHEAD(idle, conn);
324 notice(CN "connected %s", conn->fd, conn->stream ? "streaming" : "plain");
325 connect_attempt_discard();
326 check_master_queue();
331 perhaps_close(&conn->fd);
334 connect_attempt_discard();
337 static void connect_start() {
338 assert(!connecting_sockets[0]);
339 assert(!connecting_sockets[1]);
340 assert(!connecting_child);
342 notice("starting connection attempt");
344 r= socketpair(AF_UNIX, SOCK_STREAM, 0, connecting_sockets);
345 if (r) { syswarn("connect: cannot create socketpair for child"); goto x; }
347 connecting_child= fork();
348 if (connecting_child==-1) { syswarn("connect: cannot fork"); goto x; }
350 if (!connecting_child) {
351 FILE *cn_from, *cn_to;
352 char buf[NNTP_STRLEN+100];
353 int exitstatus= CHILD_ESTATUS_NOSTREAM;
358 r= close(connecting_sockets[0]);
359 if (r) sysdie("connect: close parent socket in child");
361 alarm(connection_setup_timeout);
362 if (NNTPconnect(remote_host, port, &cn_from, &cn_to, buf) < 0) {
364 sanitise_inplace(buf);
365 die("connect: rejected: %s", buf);
367 sysdie("connect: connection attempt failed");
370 if (NNTPsendpassword(remote_host, cn_from, cn_to) < 0)
371 sysdie("connect: authentication failed");
373 if (fputs("MODE STREAM\r\n", cn_to) ||
375 sysdie("connect: could not send MODE STREAM");
376 buf[sizeof(buf)-1]= 0;
377 if (!fgets(buf, sizeof(buf)-1, cn_from)) {
379 sysdie("connect: could not read response to MODE STREAM");
381 die("connect: connection close in response to MODE STREAM");
386 sanitise_inplace(buf);
387 die("connect: response to MODE STREAM is too long: %.100s...",
390 l--; if (l>0 && buf[1-]=='\r') l--;
393 int rcode= strtoul(buf,&ep,10);
395 sanitise_inplace(buf);
396 die("connect: bad response to MODE STREAM: %.50s", buf);
400 exitstatus= CHILD_ESTATUS_STREAM;
406 sanitise_inplace(buf);
407 warn("connect: unexpected response to MODE STREAM: %.50s", buf);
412 int fd= fileno(cn_from);
414 PREP_DECL_MSG_CMSG(msg);
415 struct cmsghdr *cmsg= CMSG_FIRSTHDR(&msg);
416 cmsg->cmsg_level= SOL_SOCKET;
417 cmsg->cmsg_type= SCM_RIGHTS;
418 cmsg->cmsg_len= CMSG_LEN(sizeof(fd));
419 memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd));
421 msg.msg_controllen= cmsg->cmsg_len;
422 r= sendmsg(connecting_sockets[1], &msg, 0);
423 if (r) sysdie("sendmsg failed for new connection");
428 r= close(connecting_sockets[1]); connecting_sockets[1]= 0;
429 if (r) sysdie("connect: close child socket in parent");
431 loop->on_fd(loop, connecting_sockets[0], OOP_READ, connchild_event, 0);
432 loop->on_fd(loop, connecting_sockets[0], OOP_EXCEPTION, connchild_event, 0);
436 connect_attempt_discard();
439 /*========== overall control of article flow ==========*/
441 static void conn_check_work(Conn *conn);
443 static void check_master_queue(void) {
444 try reading current feed file;
449 Conn *last_assigned=0;
452 conn_assign_one_article(&working, &last_assigned);
453 } else if (idle.head) {
454 conn_assign_one_article(&idle, &last_assigned);
455 } else if (nconns < maxconns && queue.count >= max_queue_per_conn &&
456 !connecting_child && !connect_delay) {
457 connect_delay= reconnect_delay_periods;
463 conn_check_work(last_assigned);
466 static void conn_assign_one_article(LIST(Conn) *connlist,
467 Conn **last_assigned) {
468 Conn *conn= connlist->head;
470 LIST_REMOVE(*connlist, conn);
471 Article *art= LIST_REMHEAD(queue);
472 LIST_ADDTAIL(conn->queue, art);
473 LIST_ADD(*conn_determine_right_list(conn), conn);
475 /* This slightly odd arrangement is so that we call conn_check_work
476 * once after filling the queue for a new connection in
477 * check_master_queue, rather than for each article. */
478 if (conn != *last_assigned && *last_assigned)
479 conn_check_work(*last_assigned);
480 *last_assigned= conn;
483 static int conn_total_queued_articles(Conn *conn) {
484 return conn->sent.count + conn->queue.count;
487 static LIST(Conn) *conn_determine_right_list(Conn *conn) {
488 int inqueue= conn_total_queued_articles(conn);
489 assert(inqueue <= max_queue);
490 if (inqueue == 0) return &idle;
491 if (inqueue == conn->max_queue) return &full;
495 static void *conn_writeable(oop_source *l, int fd, int ev, void *u) {
500 static void conn_check_work(Conn *conn) {
503 conn_make_some_xmits(conn);
505 loop->cancel_fd(loop, conn->fd, OOP_WRITE);
509 void *rp= conn_write_some_xmits(conn);
510 if (rp==OOP_CONTINUE) {
511 loop->on_fd(loop, conn->fd, OOP_WRITE, conn_writeable, conn);
513 } else if (rp==OOP_HALT) {
516 /* transmitted everything */
523 /*========== article transmission ==========*/
525 static void *conn_write_some_xmits(Conn *conn) {
527 * 0: nothing more to write, no need to call us again
528 * OOP_CONTINUE: more to write but fd not writeable
529 * OOP_HALT: disaster, have destroyed conn
532 int count= conn->xmitu;
533 if (!count) return 0;
535 if (count > IOV_MAX) count= IOV_MAX;
536 ssize_t rs= writev(conn->fd, conn->xmit, count);
538 if (errno == EAGAIN) return OOP_CONTINUE;
539 syswarn(CN "write failed", conn->fd);
545 for (done=0; rs && done<xmitu; done++) {
546 struct iovec *vp= &conn->xmit[done];
547 XmitDetails *dp= &conn->xmitd[done];
548 if (rs > vp->iov_len) {
556 int newu= conn->xmitu - done;
557 memmove(conn->xmit, conn->xmit + done, newu * sizeof(*conn->xmit));
558 memmove(conn->xmitd, conn->xmitd + done, newu * sizeof(*conn->xmitd));
563 static void conn_make_some_xmits(Conn *conn) {
565 if (conn->xmitu+5 > CONNIOVS)
568 Article *art= LIST_REMHEAD(queue);
571 if (art->checked || conn->nocheck) {
572 /* actually send it */
574 ARTHANDLE *artdata= SMretrieve(somehow);
578 XMIT_LITERAL("TAKETHIS ");
579 xmit_noalloc(art->mid, art->midlen);
580 XMIT_LITERAL("\r\n");
581 xmit_artbody(artdata);
584 /* we got 235 from IHAVE */
586 xmit_artbody(artdata);
588 XMIT_LITERAL(".\r\n");
592 LIST_ADDTAIL(conn->sent, art);
598 XMIT_LITERAL("IHAVE ");
600 XMIT_LITERAL("CHECK ");
601 xmit_noalloc(art->mid, art->midlen);
602 XMIT_LITERAL("\r\n");
604 LIST_ADDTAIL(conn->sent, art);
609 /*========== responses from peer ==========*/
611 static const oop_rd_style peer_rd_style= {
612 OOP_RD_DELIM_STRIP, '\n',
614 OOP_RD_SHORTREC_FORBID
617 static void *peer_rd_ok(oop_source *lp, oop_read *oread, oop_event ev,
618 const char *errmsg, int errnoval,
619 const char *data, size_t recsz, void *conn_v) {
622 if (ev == OOP_RD_EOF) {
623 warn("unexpected EOF from peer");
627 assert(ev == OOP_RD_OK);
630 unsigned long code= strtoul(data, &ep, 10);
631 if (ep != data+3 || *ep != ' ' || data[0]=='0') {
637 if (q > sanibuf+sizeof(sanibuf)-8) { strcpy(q,"..."); break; }
639 if (!c) { *q++= '\''; break; }
640 if (c>=' ' && c<=126 && c!='\\') { *q++= c; continue; }
641 sprintf(q,"\\x%02x",c);
644 warn("badly formatted response from peer: %s", sanibuf);
649 if (conn->quitting) {
651 warn("peer gave failure response to QUIT: %s", sani);
660 case 438: /* CHECK says they have it */
661 case 435: /* IHAVE says they have it */
662 ARTICLE_DEALTWITH(1,unwanted);
665 case 238: /* CHECK says send it */
666 case 335: /* IHAVE says send it */
667 count_checkedwanted++;
668 Article *art= LIST_REMHEAD(conn->sent);
670 LIST_ADDTAIL(conn->queue);
673 case 235: /* IHAVE says thanks */
674 case 239: /* TAKETHIS says thanks */
675 ARTICLE_DEALTWITH(1,accepted);
678 case 439: /* TAKETHIS says rejected */
679 case 437: /* IHAVE says rejected */
680 ARTICLE_DEALTWITH(1,rejected);
683 case 431: /* CHECK or TAKETHIS says try later */
684 case 436: /* IHAVE says try later */
685 ARTICLE_DEALTWITH(0,deferred);
688 case 400: warn("peer has stopped accepting articles: %s", sani); goto failed;
689 case 503: warn("peer timed us out: %s", sani); goto failed;
690 default: warn("peer sent unexpected message: %s", sani);
693 return OOP_CONTINUE;;
699 /*========== monitoring of input file ==========*/
701 /*---------- tailing input file ----------*/
705 /*---------- filemon implemented with inotify ----------*/
707 #if defined(HAVE_INOTIFY) && !defined(HAVE_FILEMON)
710 #include <linux/inotify.h>
712 static int filemon_inotify_fd;
713 static int filemon_inotify_wdmax;
714 static InputFile **filemon_inotify_wd2ipf;
716 typedef struct Filemon_Perfile {
718 } Filemon_Inotify_Perfile;
720 static void filemon_startfile(InputFile *ipf) {
721 int wd= inotify_add_watch(filemon_inotify_fd, ipf->path, IN_MODIFY);
722 if (wd < 0) sysdie("inotify_add_watch %s", ipf->path);
724 if (wd >= filemon_inotify_wdmax) {
726 filemon_inotify_wd= xrealloc(filemon_inotify_wd2ipf,
727 sizeof(*filemon_inotify_wd2ipf) * newmax);
728 memset(filemon_inotify_wd2ipf + filemon_inotify_wdmax, 0,
729 sizeof(*filemon_inotify_wd2ipf) * (newmax - filemon_inotify_wdmax));
730 filemon_inotify_wdmax= newmax;
733 assert(!filemon_inotify_wd2ipf[wd]);
734 filemon_inotify_wd2ipf[wd]= ipf;
736 assert(!ipf->filemon);
737 ipf->filemon= xmalloc(sizeof(*ipf->filemon));
738 ipf->filemon->wd= wd;
741 static void filemon_stopfile(InputFile *ipf) {
742 int wd= ipf->filemon->wd;
743 int r= inotify_rm_watch(filemon_inotify_fd, filemon_inotify_wd);
744 if (r) sysdie("inotify_rm_watch");
745 filemon_inotify_wd2ipf[wd]= 0;
750 static void *filemon_inotify_readable(oop_source *lp, int fd,
751 oop_event e, void *u) {
752 struct inotify_event iev;
754 int r= read(filemon_inotify_fd, &iev, sizeof(iev));
756 if (errno==EAGAIN) break;
757 sysdie("read from inotify master");
758 } else if (r==sizeof(iev)) {
759 assert(iev.wd >= 0 && iev.wd < filemon_inotify_wdmax);
761 die("inotify read %d bytes wanted struct of %d", r, (int)sizeof(iev));
763 filemon_callback(filemon_inotify_wd2ipf[iev.wd]);
768 static int filemon_init(void) {
769 filemon_inotify_fd= inotify_init();
770 if (filemon_inotify_fd<0) {
771 syswarn("could not initialise inotify: inotify_init failed");
775 loop->on_fd(loop, filemon_inotify_fd, OOP_READ, filemon_inotify_readable);
780 #endif /* HAVE_INOTIFY && !HAVE_FILEMON *//
782 /*---------- filemon dummy implementation ----------*/
784 #if !defined(HAVE_FILEMON)
786 static int filemon_init(void) { return 0; }
787 static void filemon_startfile(InputFile *ipf) { }
788 static void filemon_stopfile(InputFile *ipf) { }
792 /*========== interaction with innd ==========*/
794 /* See official state diagram at top of file. We implement
804 |`--------------------------.
805 | | install defer as backlog
807 | | OPEN F SUCCEEDS V
815 | | F IS SO BIG WE SHOULD FLUSH
819 | | our handle onto F is now onto D
822 | |<---------------------------------------------------.
824 | | spawn inndcomm flush |
832 | | INNDCOMM FLUSH FAILS ^
833 | |`----------------------->--------. |
836 ^ |`----------------. ========= |
839 | | ========== read D |
840 | | DROPPING ========= |
842 | | read D | TIME TO RETRY |
843 | | ========== `------------------'
845 | | open F | AT EOF OF D AND ALL PROCESSED
846 | V | install defer as backlog
847 | ========== | unlink D
850 | read D, F ==========
855 | | AT EOF OF D AND ALL D PROCESSED
858 | | install defer as backlog
873 static char *path_ductlock, *path_duct, *path_ductdefer;
876 /* This is an instance of struct oop_readable */
877 struct oop_readable readable;
878 oop_readable_call *readable_callback;
881 const char *path; /* ptr copy of path_<foo> or feedfile */
882 struct Filemon_Perfile *filemon;
885 long inprogress; /* no. of articles read but not processed */
888 static void statemc_init(void) {
889 path_ductlock= xasprintf("%s_duct.lock", feedfile);
890 path_duct= xasprintf("%s_duct", feedfile);
891 path_ductdefer= xasprintf("%s_duct.defer", feedfile);
893 int lockfd= open(path_ductlock, O_CREAT|O_RDWR, 0600);
894 if (lockfd<0) sysdie("open lockfile %s", path_ductlock);
897 memset(&fl,0,sizeof(fl));
899 fl.l_whence= SEEK_SET;
900 r= fcntl(lockfd, F_SETLK, &fl);
902 if (errno==EACCES || errno==EAGAIN)
903 die("another duct holds the lockfile");
904 sysdie("fcntl F_SETLK lockfile %s", path_ductlock);
908 static void statemc_poll(void) {
909 if (tailing_fd>=0) return;
911 int d_fd= open(path_duct, O_RDWR);
913 if (errno!=ENOENT) sysdie("open duct file %s", path_duct);
915 int f_fd= open(feedfile, O_RDWR);
917 if (errno!=ENOENT) sysdie("open feed file %s", feedfile);
927 /*========== main program ==========*/
929 #define EVERY(what, interval, body) \
930 static const struct timeval what##_timeout = { 5, 0 }; \
931 static void what##_schedule(void); \
932 static void *what##_timedout(oop_source *lp, struct timeval tv, void *u) { \
936 static void what##_schedule(void) { \
937 loop->on_time(loop, what##_timeout, what##_timedout, 0); \
940 EVERY(filepoll, {5,0}, { check_master_queue(); })
942 EVERY(period, {PERIOD_SECONDS,0}, {
943 if (connect_delay) connect_delay--;
945 check_master_queue();