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
39 * | V <----------+--<----------------------' |
45 * | open F ------->-' |
51 * | F: innd writing, duct reading / editing /|\
54 * | | duct decides time to flush same / | |
55 * | | duct makes hardlink as D / | |
59 * | F == D: innd writing, duct reading / editing | |
61 * | | duct unlinks F / |
63 * | Moved <------------' |
65 * | D: innd writing, duct reading / editing |
67 * | | duct requests flush of feed |
68 * | | (others can too, harmlessly) |
70 * | Separated <-----------------'
71 * | F: innd writing different to D
72 * | D: duct reading / editing
74 * | V duct completes processing of D
81 #define PERIOD_SECONDS 60
83 static char *feedfile;
84 static int max_connections, max_queue_per_conn;
85 static int connection_setup_timeout, port, try_stream;
86 static const char *remote_host;
88 #define ISNODE(T) T *next, *back;
89 #define LIST(T) struct { T *head, *tail, *tailpred; int count; }
91 #define NODE(n) ((struct node*)&(n)->head)
93 #define LIST_ADDHEAD(l,n) \
94 (list_addhead((struct list*)&(l), NODE((n))), (void)(l).count++)
95 #define LIST_ADDTAIL(l,n) \
96 (list_addtail((struct list*)&(l), NODE((n))), (void)(l).count++)
98 #define LIST_REMHEAD(l) \
99 ((l).count ? ((l).count--, (void*)list_remhead((struct list*)&(l))) : 0)
100 #define LIST_REMTAIL(l) \
101 ((l).count ? ((l).count--, (void*)list_remtail((struct list*)&(l))) : 0)
102 #define LIST_REMOVE(l,n) \
103 (list_remove(NODE((n))), (void)(l).count--)
104 #define LIST_INSERT(l,n,pred) \
105 (list_insert((struct list*)&(l), NODE((n)), NODE((pred))), (void)(l).count++)
110 int checked, sentbody;
111 fd and offset for blanking token or mid;
118 typedef struct Conn Conn;
121 Malloc, Const, Artdata;
134 int fd, max_queue, stream;
135 LIST(Article) queue; /* not yet told peer, or CHECK said send it */
136 LIST(Article) sent; /* offered/transmitted - in xmit or waiting reply */
137 struct iovec xmit[CONNIOVS];
138 XmitDetails xmitd[CONNIOVS];
142 static int filemon_init(void);
143 static void filemon_setfile(int mainfeed_fd, const char *mainfeed_path);
144 static void filemon_callback(void);
147 #define CHILD_ESTATUS_STREAM 4
148 #define CHILD_ESTATUS_NOSTREAM 5
150 static int since_connect_attempt;
152 static LIST(Conn) idle, working, full;
154 static LIST(Article) *queue;
156 static void perhaps_close(int *fd) { if (*fd) { close(*fd); fd=0; } }
158 /*========== making new connections ==========*/
160 static int connecting_sockets[2]= {-1,-1};
161 static pid_t connecting_child;
163 static void report_child_status(const char *what, int status) {
164 if (WIFEXITED(status)) {
165 int es= WEXITSTATUS(status);
167 warn("%s: child died with error exit status %d",es);
168 } else if (WIFSIGNALED(status)) {
169 int sig= WTERMSIG(status);
170 const char *sigstr= strsignal(sig);
171 const char *coredump= WCOREDUMP(status) ? " (core dumped)" : "";
173 warn("%s: child died due to fatal signal %s%s", what, sigstr, coredump);
175 warn("%s: child died due to unknown fatal signal %d%s",
176 what, sig, coredump);
178 warn("%s: child died with unknown wait status %d", status);
182 static void connect_attempt_discard(void) {
183 if (connecting_sockets[0]) {
184 cancel_fd(loop, connecting_sockets[0], OOP_READ);
185 cancel_fd(loop, connecting_sockets[0], OOP_EXCEPTION);
187 perhaps_close(&connecting_sockets[0]);
188 perhaps_close(&connecting_sockets[1]);
190 if (connecting_child) {
192 r= kill(connecting_child, SIGKILL);
193 if (r) sysdie("cannot kill connect child");
195 pid_t got= waitpid(connecting_child, &status, WNOHANG);
196 if (got==-1) sysdie("cannot reap connect child");
198 if (!(WIFEXITED(status) ||
199 (WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL))) {
200 report_child_status("connect"
206 #define PREP_DECL_MSG_CMSG(msg) \
208 memset(&msg,0,sizeof(msg)); \
209 char msg##cbuf[CMSG_SPACE(sizeof(fd))]; \
210 msg.msg_control= msg##cbuf; \
211 msg.msg_controllen= sizeof(msg##cbuf);
213 static void *connchild_event(oop_source *lp, int fd, oop_event e, void *u) {
216 conn= xcalloc(sizeof(*conn));
219 struct cmsghdr *h= 0;
220 ssize_t rs= recvmsg(fd, &msg, MSG_DONTWAIT);
221 if (rs >= 0) h= CMSG_FIRSTHDR(&msg);
224 pid_t got= waitpid(connecting_child, &status, WNOHANG);
226 assert(got==connecting_child);
228 if (WIFEXITED(status) &&
229 (WEXITSTATUS(status) != 0
230 WEXITSTATUS(status) != CHILD_ESTATUS_STREAM &&
231 WEXITSTATUS(status) != CHILD_ESTATUS_NOSTREAM)) {
232 /* child already reported the problem */
233 } else if (WIFSIGNALED(status) && WTERMSIG(status) == SIGALARM) {
234 warn("connect: connection attempt timed out");
235 } else if (!WIFEXITED(status)) {
236 report_child_status("connect", status);
237 /* that's probably the root cause then */
240 /* child is still running apparently, report the socket problem */
242 syswarn("connect: read from child socket failed");
243 else if (e == OOP_EXCEPTIONN)
244 warn("connect: unexpected exception on child socket");
246 warn("connect: unexpected EOF on child socket");
251 #define CHK(field, val) \
252 if (h->cmsg_##field != val) { \
253 die("connect: child sent cmsg with cmsg_" #field "=%d, expected %d"); \
256 CHK(level, SOL_SOCKET);
257 CHK(type, SCM_RIGHTS);
258 CHK(len, CMSG_LEN(sizeof(conn-b>fd)));
261 if (CMSG_NXTHDR,&msg,h) { die("connect: child sent many cmsgs"); goto x; }
263 memcpy(&conn->fd, CMSG_DATA(h), sizeof(conn->fd));
265 pid_t got= waitpid(connecting_child, &status, 0);
266 if (got==-1) sysdie("connect: real wait for child");
267 assert(got == connecting_child);
270 if (!WIFEXITED(status)) { report_child_status("connect",status); goto x; }
271 int es= WEXITSTATUS(status);
273 case CHILD_ESTATUS_STREAM: conn->stream= 1; break;
274 case CHILD_ESTATUS_NOSTREAM: conn->stream= 0; break;
276 die("connect: child gave unexpected exit status %d", es);
282 LIST_ADDHEAD(idle, conn);
283 notice(CN "connected %s", conn->fd, conn->stream ? "streaming" : "plain");
284 connect_attempt_discard();
285 check_master_queue();
290 perhaps_close(&conn->fd);
293 connect_attempt_discard();
296 static void connect_start() {
297 assert(!connecting_sockets[0]);
298 assert(!connecting_sockets[1]);
299 assert(!connecting_child);
301 notice("starting connection attempt");
303 r= socketpair(AF_UNIX, SOCK_STREAM, 0, connecting_sockets);
304 if (r) { syswarn("connect: cannot create socketpair for child"); goto x; }
306 connecting_child= fork();
307 if (connecting_child==-1) { syswarn("connect: cannot fork"); goto x; }
309 if (!connecting_child) {
310 FILE *cn_from, *cn_to;
311 char buf[NNTP_STRLEN+100];
312 int exitstatus= CHILD_ESTATUS_NOSTREAM;
317 r= close(connecting_sockets[0]);
318 if (r) sysdie("connect: close parent socket in child");
320 alarm(connection_setup_timeout);
321 if (NNTPconnect(remote_host, port, &cn_from, &cn_to, buf) < 0) {
323 sanitise_inplace(buf);
324 die("connect: rejected: %s", buf);
326 sysdie("connect: connection attempt failed");
329 if (NNTPsendpassword(remote_host, cn_from, cn_to) < 0)
330 sysdie("connect: authentication failed");
332 if (fputs("MODE STREAM\r\n", cn_to) ||
334 sysdie("connect: could not send MODE STREAM");
335 buf[sizeof(buf)-1]= 0;
336 if (!fgets(buf, sizeof(buf)-1, cn_from)) {
338 sysdie("connect: could not read response to MODE STREAM");
340 die("connect: connection close in response to MODE STREAM");
345 sanitise_inplace(buf);
346 die("connect: response to MODE STREAM is too long: %.100s...",
349 l--; if (l>0 && buf[1-]=='\r') l--;
352 int rcode= strtoul(buf,&ep,10);
354 sanitise_inplace(buf);
355 die("connect: bad response to MODE STREAM: %.50s", buf);
359 exitstatus= CHILD_ESTATUS_STREAM;
365 sanitise_inplace(buf);
366 warn("connect: unexpected response to MODE STREAM: %.50s", buf);
371 int fd= fileno(cn_from);
373 PREP_DECL_MSG_CMSG(msg);
374 struct cmsghdr *cmsg= CMSG_FIRSTHDR(&msg);
375 cmsg->cmsg_level= SOL_SOCKET;
376 cmsg->cmsg_type= SCM_RIGHTS;
377 cmsg->cmsg_len= CMSG_LEN(sizeof(fd));
378 memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd));
380 msg.msg_controllen= cmsg->cmsg_len;
381 r= sendmsg(connecting_sockets[1], &msg, 0);
382 if (r) sysdie("sendmsg failed for new connection");
387 r= close(connecting_sockets[1]); connecting_sockets[1]= 0;
388 if (r) sysdie("connect: close child socket in parent");
390 loop->on_fd(loop, connecting_sockets[0], OOP_READ, connchild_event, 0);
391 loop->on_fd(loop, connecting_sockets[0], OOP_EXCEPTION, connchild_event, 0);
395 connect_attempt_discard();
398 /*========== overall control of article flow ==========*/
400 static void conn_check_work(Conn *conn);
402 static void check_master_queue(void) {
403 try reading current feed file;
408 Conn *last_assigned=0;
411 conn_assign_one_article(&working, &last_assigned);
412 } else if (idle.head) {
413 conn_assign_one_article(&idle, &last_assigned);
414 } else if (nconns < maxconns && queue.count >= max_queue_per_conn &&
415 !connecting_child && !connect_delay) {
416 connect_delay= reconnect_delay_periods;
422 conn_check_work(last_assigned);
425 static void conn_assign_one_article(LIST(Conn) *connlist,
426 Conn **last_assigned) {
427 Conn *conn= connlist->head;
429 LIST_REMOVE(*connlist, conn);
430 Article *art= LIST_REMHEAD(queue);
431 LIST_ADDTAIL(conn->queue, art);
432 LIST_ADD(*conn_determine_right_list(conn), conn);
434 /* This slightly odd arrangement is so that we call conn_check_work
435 * once after filling the queue for a new connection in
436 * check_master_queue, rather than for each article. */
437 if (conn != *last_assigned && *last_assigned)
438 conn_check_work(*last_assigned);
439 *last_assigned= conn;
442 static int conn_total_queued_articles(Conn *conn) {
443 return conn->sent.count + conn->queue.count;
446 static LIST(Conn) *conn_determine_right_list(Conn *conn) {
447 int inqueue= conn_total_queued_articles(conn);
448 assert(inqueue <= max_queue);
449 if (inqueue == 0) return &idle;
450 if (inqueue == conn->max_queue) return &full;
454 static void *conn_writeable(oop_source *l, int fd, int ev, void *u) {
459 static void conn_check_work(Conn *conn) {
462 conn_make_some_xmits(conn);
464 loop->cancel_fd(loop, conn->fd, OOP_WRITE);
468 void *rp= conn_write_some_xmits(conn);
469 if (rp==OOP_CONTINUE) {
470 loop->on_fd(loop, conn->fd, OOP_WRITE, conn_writeable, conn);
472 } else if (rp==OOP_HALT) {
475 /* transmitted everything */
482 /*========== article transmission ==========*/
484 static void *conn_write_some_xmits(Conn *conn) {
486 * 0: nothing more to write, no need to call us again
487 * OOP_CONTINUE: more to write but fd not writeable
488 * OOP_HALT: disaster, have destroyed conn
491 int count= conn->xmitu;
492 if (!count) return 0;
494 if (count > IOV_MAX) count= IOV_MAX;
495 ssize_t rs= writev(conn->fd, conn->xmit, count);
497 if (errno == EAGAIN) return OOP_CONTINUE;
498 syswarn(CN "write failed", conn->fd);
504 for (done=0; rs && done<xmitu; done++) {
505 struct iovec *vp= &conn->xmit[done];
506 XmitDetails *dp= &conn->xmitd[done];
507 if (rs > vp->iov_len) {
515 int newu= conn->xmitu - done;
516 memmove(conn->xmit, conn->xmit + done, newu * sizeof(*conn->xmit));
517 memmove(conn->xmitd, conn->xmitd + done, newu * sizeof(*conn->xmitd));
522 static void conn_make_some_xmits(Conn *conn) {
524 if (conn->xmitu+5 > CONNIOVS)
527 Article *art= LIST_REMHEAD(queue);
530 if (art->checked || conn->nocheck) {
531 /* actually send it */
533 ARTHANDLE *artdata= SMretrieve(somehow);
537 XMIT_LITERAL("TAKETHIS ");
538 xmit_noalloc(art->mid, art->midlen);
539 XMIT_LITERAL("\r\n");
540 xmit_artbody(artdata);
543 /* we got 235 from IHAVE */
545 xmit_artbody(artdata);
547 XMIT_LITERAL(".\r\n");
551 LIST_ADDTAIL(conn->sent, art);
557 XMIT_LITERAL("IHAVE ");
559 XMIT_LITERAL("CHECK ");
560 xmit_noalloc(art->mid, art->midlen);
561 XMIT_LITERAL("\r\n");
563 LIST_ADDTAIL(conn->sent, art);
568 /*========== responses from peer ==========*/
570 static const oop_rd_style peer_rd_style= {
571 OOP_RD_DELIM_STRIP, '\n',
573 OOP_RD_SHORTREC_FORBID
576 static void *peer_rd_ok(oop_source *lp, oop_read *oread, oop_event ev,
577 const char *errmsg, int errnoval,
578 const char *data, size_t recsz, void *conn_v) {
581 if (ev == OOP_RD_EOF) {
582 warn("unexpected EOF from peer");
586 assert(ev == OOP_RD_OK);
589 unsigned long code= strtoul(data, &ep, 10);
590 if (ep != data+3 || *ep != ' ' || data[0]=='0') {
596 if (q > sanibuf+sizeof(sanibuf)-8) { strcpy(q,"..."); break; }
598 if (!c) { *q++= '\''; break; }
599 if (c>=' ' && c<=126 && c!='\\') { *q++= c; continue; }
600 sprintf(q,"\\x%02x",c);
603 warn("badly formatted response from peer: %s", sanibuf);
608 if (conn->quitting) {
610 warn("peer gave failure response to QUIT: %s", sani);
619 case 438: /* CHECK says they have it */
620 case 435: /* IHAVE says they have it */
621 ARTICLE_DEALTWITH(1,unwanted);
624 case 238: /* CHECK says send it */
625 case 335: /* IHAVE says send it */
626 count_checkedwanted++;
627 Article *art= LIST_REMHEAD(conn->sent);
629 LIST_ADDTAIL(conn->queue);
632 case 235: /* IHAVE says thanks */
633 case 239: /* TAKETHIS says thanks */
634 ARTICLE_DEALTWITH(1,accepted);
637 case 439: /* TAKETHIS says rejected */
638 case 437: /* IHAVE says rejected */
639 ARTICLE_DEALTWITH(1,rejected);
642 case 431: /* CHECK or TAKETHIS says try later */
643 case 436: /* IHAVE says try later */
644 ARTICLE_DEALTWITH(0,deferred);
647 case 400: warn("peer has stopped accepting articles: %s", sani); goto failed;
648 case 503: warn("peer timed us out: %s", sani); goto failed;
649 default: warn("peer sent unexpected message: %s", sani);
652 return OOP_CONTINUE;;
658 /*========== monitoring of input file ==========*/
660 /*---------- tailing input file ----------*/
664 /*---------- filemon implemented with inotify ----------*/
666 #if defined(HAVE_INOTIFY) && !defined(HAVE_FILEMON)
669 #include <linux/inotify.h>
671 static int filemon_inotify_fd;
672 static int filemon_inotify_wd= -1;
674 static void *filemon_inotify_readable(oop_source *lp, int fd,
675 oop_event e, void *u) {
676 struct inotify_event iev;
678 int r= read(filemon_inotify_fd, &iev, sizeof(iev));
680 if (errno==EAGAIN) break;
681 sysdie("read from inotify master");
682 } else if (r==sizeof(iev)) {
683 assert(wd == filemon_inotify_wd);
685 die("inotify read %d bytes wanted struct of %d", r, (int)sizeof(iev));
688 /* Technically speaking the select might fire early in which case
689 * we'll read no events and then call filemon_callback
690 * unnecessarily. We don't care about that.
696 static int filemon_init(void) {
697 filemon_inotify_fd= inotify_init();
698 if (filemon_inotify_fd<0) {
699 syswarn("could not initialise inotify: inotify_init failed");
703 loop->on_fd(loop, filemon_inotify_fd, OOP_READ, filemon_inotify_readable);
708 static void filemon_setfile(int mainfeed_fd, const char *mainfeed_path) {
709 if (filemon_inotify_wd >= 0) {
710 int r= inotify_rm_watch(filemon_inotify_fd, filemon_inotify_wd);
711 if (r) sysdie("inotify_rm_watch");
713 filemon_inotify_wd= inotify_add_watch(filemon_inotify_fd, path, IN_MODIFY);
714 if (filemon_inotify_wd < 0) sysdie("inotify_add_watch");
717 #endif /* HAVE_INOTIFY && !HAVE_FILEMON *//
719 /*---------- filemon dummy implementation ----------*/
721 #if !defined(HAVE_FILEMON)
723 static int filemon_init(void) { return 0; }
724 static void filemon_setfile(int mainfeed_fd, const char *mainfeed_path) { }
728 /*========== interaction with innd ==========*/
730 /* see state diagram at top of file */
732 static char *path_ductlock, *path_duct, *path_ductdefer;
733 static int tailing_fd= -1, flushing_fd= -1;
735 static void statemc_init(void) {
736 path_ductlock= xasprintf("%s_duct.lock", feedfile);
737 path_duct= xasprintf("%s_duct", feedfile);
738 path_ductdefer= xasprintf("%s_duct.defer", feedfile);
740 int lockfd= open(path_ductlock, O_CREAT|O_RDWR, 0600);
741 if (lockfd<0) sysdie("open lockfile %s", path_ductlock);
744 memset(&fl,0,sizeof(fl));
746 fl.l_whence= SEEK_SET;
747 r= fcntl(lockfd, F_SETLK, &fl);
749 if (errno==EACCES || errno==EAGAIN)
750 die("another duct holds the lockfile");
751 sysdie("fcntl F_SETLK lockfile %s", path_ductlock);
755 static void statemc_poll(void) {
756 if (tailing_fd>=0) return;
758 int d_fd= open(path_duct, O_RDWR);
760 if (errno!=ENOENT) sysdie("open duct file %s", path_duct);
762 int f_fd= open(feedfile, O_RDWR);
764 if (errno!=ENOENT) sysdie("open feed file %s", feedfile);
774 /*========== main program ==========*/
776 #define EVERY(what, interval, body) \
777 static const struct timeval what##_timeout = { 5, 0 }; \
778 static void what##_schedule(void); \
779 static void *what##_timedout(oop_source *lp, struct timeval tv, void *u) { \
783 static void what##_schedule(void) { \
784 loop->on_time(loop, what##_timeout, what##_timedout, 0); \
787 EVERY(filepoll, {5,0}, { check_master_queue(); })
789 EVERY(period, {PERIOD_SECONDS,0}, {
790 if (connect_delay) connect_delay--;
792 check_master_queue();