+ }
+
+ check_check_work(conn);
+ return OOP_CONTINUE;
+}
+
+
+/*========== monitoring of input files ==========*/
+
+static void feedfile_eof(InputFile *ipf) {
+ assert(ipf != main_input_file); /* promised by tailing_try_read */
+
+ inputfile_tailing_stop(ipf);
+
+ if (ipf == backlog_input_file) {
+ assert(ipf->fd >= 0);
+ if (close(ipf->fd)) sysdie("could not close backlog file %s", ipf->path);
+ ipf->fd= -1;
+ return;
+ }
+
+ assert(ipf == flushing_input_file);
+
+ inputfile_tailing_stop(ipf);
+ assert(ipf->fd >= 0);
+ if (close(ipf->fd)) sysdie("could not close input file %s", ipf->path);
+ ipf->fd= -1;
+
+ assert(sms==sm_SEPARATED || sms==sm_DROPPING);
+
+ if (main_input_file)
+ inputfile_tailing_start(main_input_file);
+}
+
+static InputFile *open_input_file(const char *path) {
+ int fd= open(path, O_RDONLY);
+ if (fd<0) {
+ if (errno==ENOENT) return 0;
+ sysfatal("unable to open input file %s", path);
+ }
+
+ InputFile *ipf= xmalloc(sizeof(*ipf) + strlen(path) + 1);
+ memset(ipf,0,sizeof(*ipf));
+
+ ipf->readable.on_readable= tailing_on_readable;
+ ipf->readable.on_cancel= tailing_on_cancel;
+ ipf->readable.try_read= tailing_try_read;
+
+ ipf->fd= fd;
+ strcpy(ipf->path, path);
+
+ return ipf;
+}
+
+static void close_input_file(InputFile *ipf) {
+ assert(!ipf->readable_callback); /* must have had ->on_cancel */
+ assert(!ipf->filemon); /* must have had inputfile_tailing_stop */
+ assert(!ipf->rd); /* must have had inputfile_tailing_stop */
+ assert(!ipf->inprogress); /* no dangling pointers pointing here */
+
+ if (ipf->fd >= 0)
+ if (close(ipf->fd)) sysdie("could not close input file %s", ipf->path);
+
+ free(ipf);
+}
+
+
+/*---------- dealing with articles read in the input file ----------*/
+
+typedef void *feedfile_got_article(oop_source *lp, oop_read *rd,
+ oop_rd_event ev, const char *errmsg,
+ int errnoval,
+ const char *data, size_t recsz,
+ void *ipf_v) {
+ InputFile *ipf= ipf_v;
+ Article *art;
+ char tokentextbuf[sizeof(TOKEN)*2+3];
+
+ if (!data) { feedfile_eof(ipf); return OOP_CONTINUE; }
+
+ if (data[0] && data[0]!=' ') {
+ char *space= strchr(data,' ');
+ int tokenlen= space-data;
+ int midlen= (int)recsz-tokenlen-1;
+ if (midlen < 0) goto bad_data;
+
+ if (tokenlen != sizeof(TOKEN)*2+2) goto bad_data;
+ memcpy(tokentextbuf, data, tokenlen);
+ tokentextbuf[tokenlen]= 0;
+ if (!IsToken(tokentextbuf)) goto bad_data;
+
+ art= xmalloc(sizeof(*art) - 1 + midlen + 1);
+ art->offset= ipf->offset;
+ art->blanklen= recsz;
+ art->midlen= midlen;
+ art->checked= art->sentbody= 0;
+ art->ipf= ipf; ipf->inprogress++;
+ art->token= TextToToken(tokentextbuf);
+ strcpy(art->messageid, space+1);
+ LIST_ADDTAIL(queue, art);
+ }
+ ipf->offset += recsz + 1;
+
+ if (sms==sm_NORMAL && ipf==main_input_file &&
+ (ipf->offset >= flush_threshold || !until_spontaneous_flush) {
+
+ notice("starting flush (%lu >= %lu)",
+ (unsigned long)ipf->offset, (unsigned long)flush_threshold);
+
+ int r= link(feedfile, duct_path);
+ if (r) sysdie("link feedfile %s to flushing file %s", feedfile, dut_path);
+ /* => Hardlinked */
+
+ r= unlink(feedfile);
+ if (r) sysdie("unlink old feedfile link %s", feedfile);
+ /* => Moved */
+
+ spawn_inndcomm_flush(); /* => Flushing, sets sms to sm_FLUSHING */
+ }
+
+ check_master_queue();
+}
+
+
+/*========== tailing input file ==========*/
+
+static void filemon_start(InputFile *ipf) {
+ assert(!ipf->filemon);
+
+ ipf->filemon= xmalloc(sizeof(*ipf->filemon));
+ memset(ipf->filemon, 0, sizeof(*ipf->filemon));
+ filemon_method_startfile(ipf, ipf->filemon);
+}
+
+static void filemon_stop(InputFile *ipf) {
+ if (!ipf->filemon) return;
+ filemon_method_stopfile(ipf, ipf->filemon);
+ free(ipf->filemon);
+ ipf->filemon= 0;
+}
+
+static void filemon_callback(InputFile *ipf) {
+ ipf->readable_callback(ipf->readable_callback_user);
+}
+
+static void *tailing_rable_call_time(oop_source *loop, struct timeval tv,
+ void *user) {
+ InputFile *ipf= user;
+ return ipf->readable_callback(ipf->readable_callback_user);
+}
+
+static void on_cancel(struct oop_readable *rable) {
+ InputFile *ipf= (void*)rable;
+
+ if (ipf->filemon) filemon_stopfile(ipf);
+ loop->cancel_time(loop, OOP_TIME_NOW, tailing_rable_call_time, ipf);
+ ipf->readable_callback= 0;
+}
+
+static int tailing_on_readable(struct oop_readable *rable,
+ oop_readable_call *cb, void *user) {
+ InputFile *ipf= (void*)rable;
+
+ tailing_on_cancel(rable);
+ ipf->readable_callback= cb;
+ ipf->readable_callback_user= user;
+ filemon_startfile(ipf);
+
+ loop->on_time(loop, OOP_TIME_NOW, tailing_rable_call_time, ipf);
+ return 0;
+}
+
+static ssize_t tailing_try_read(struct oop_readable *rable, void *buffer,
+ size_t length) {
+ InputFile *ipf= (void*)rable;
+ for (;;) {
+ ssize_t r= read(ipf->fd, buffer, length);
+ if (r==-1) {
+ if (errno==EINTR) continue;
+ return r;
+ }
+ if (!r) {
+ if (ipf==main_input_file) {
+ errno=EAGAIN;
+ return -1;
+ } else if (ipf==flushing_input_file) {
+ assert(ipf->fd>=0);
+ assert(sms==sm_SEPARATED || sms==sm_DROPPING);
+ } else if (ipf==backlog_input_file) {
+ assert(ipf->fd>=0);
+ } else {
+ abort();
+ }
+ }
+ return r;
+ }
+}
+
+/*---------- filemon implemented with inotify ----------*/
+
+#if defined(HAVE_INOTIFY) && !defined(HAVE_FILEMON)
+#define HAVE_FILEMON
+
+#include <linux/inotify.h>
+
+static int filemon_inotify_fd;
+static int filemon_inotify_wdmax;
+static InputFile **filemon_inotify_wd2ipf;
+
+typedef struct Filemon_Perfile {
+ int wd;
+} Filemon_Inotify_Perfile;
+
+static void filemon_method_startfile(InputFile *ipf, Filemon_Perfile *pf) {
+ int wd= inotify_add_watch(filemon_inotify_fd, ipf->path, IN_MODIFY);
+ if (wd < 0) sysdie("inotify_add_watch %s", ipf->path);
+
+ if (wd >= filemon_inotify_wdmax) {
+ int newmax= wd+2;
+ filemon_inotify_wd= xrealloc(filemon_inotify_wd2ipf,
+ sizeof(*filemon_inotify_wd2ipf) * newmax);
+ memset(filemon_inotify_wd2ipf + filemon_inotify_wdmax, 0,
+ sizeof(*filemon_inotify_wd2ipf) * (newmax - filemon_inotify_wdmax));
+ filemon_inotify_wdmax= newmax;
+ }
+
+ assert(!filemon_inotify_wd2ipf[wd]);
+ filemon_inotify_wd2ipf[wd]= ipf;
+
+ debug("filemon inotify startfile %p wd=%d wdmax=%d",
+ ipf, wd, filemon_inotify_wdmax);
+
+ pf->wd= wd;
+}
+
+static void filemon_method_stopfile(InputFile *ipf, Filemon_Perfile *pf) {
+ int wd= pf->wd;
+ debug("filemon inotify stopfile %p wd=%d", ipf, wd);
+ int r= inotify_rm_watch(filemon_inotify_fd, filemon_inotify_wd);
+ if (r) sysdie("inotify_rm_watch");
+ filemon_inotify_wd2ipf[wd]= 0;
+}
+
+static void *filemon_inotify_readable(oop_source *lp, int fd,
+ oop_event e, void *u) {
+ struct inotify_event iev;
+ for (;;) {
+ int r= read(filemon_inotify_fd, &iev, sizeof(iev));
+ if (r==-1) {
+ if (errno==EAGAIN) break;
+ sysdie("read from inotify master");
+ } else if (r==sizeof(iev)) {
+ assert(iev.wd >= 0 && iev.wd < filemon_inotify_wdmax);
+ } else {
+ die("inotify read %d bytes wanted struct of %d", r, (int)sizeof(iev));
+ }
+ InputFile *ipf= filemon_inotify_wd2ipf[iev.wd];
+ debug("filemon inotify readable read %p wd=%p", iev.wd, ipf);
+ filemon_callback(ipf);
+ }
+ return OOP_CONTINUE;
+}
+
+static int filemon_method_init(void) {
+ filemon_inotify_fd= inotify_init();
+ if (filemon_inotify_fd<0) {
+ syswarn("could not initialise inotify: inotify_init failed");
+ return 0;
+ }
+ set nonblock;
+ loop->on_fd(loop, filemon_inotify_fd, OOP_READ, filemon_inotify_readable);
+
+ debug("filemon inotify init filemon_inotify_fd=%d", filemon_inotify_fd);
+ return 1;
+}
+
+#endif /* HAVE_INOTIFY && !HAVE_FILEMON *//
+
+/*---------- filemon dummy implementation ----------*/
+
+#if !defined(HAVE_FILEMON)
+
+typedef struct Filemon_Perfile { int dummy; } Filemon_Dummy_Perfile;
+
+static int filemon_method_init(void) { return 0; }
+static void filemon_method_startfile(InputFile *ipf, Filemon_Perfile *pf) { }
+static void filemon_method_stopfile(InputFile *ipf, Filemon_Perfile *pf) { }
+
+#endif /* !HAVE_FILEMON */
+
+/*---------- interface to start and stop an input file ----------*/
+
+static const oop_rd_style feedfile_rdstyle= {
+ OOP_RD_DELIM_STRIP, '\n',
+ OOP_RD_NUL_FORBID,
+ OOP_RD_SHORTREC_EOF,
+};
+
+static void inputfile_tailing_start(InputFile *ipf) {
+ assert(!ipf->fd);
+ ipf->readable->on_readable= tailing_on_readable;
+ ipf->readable->on_cancel= tailing_on_cancel;
+ ipf->readable->try_read= tailing_try_read;
+ ipf->readable->delete_tidy= 0; /* we never call oop_rd_delete_{tidy,kill} */
+ ipf->readable->delete_kill= 0;
+
+ ipf->readable_callback= 0;
+ ipf->readable_callback_user= 0;
+
+ ipf->rd= oop_rd_new(loop, &ipf->readable, 0,0);
+ assert(ipf->fd);
+
+ int r= oop_rd_read(ipf->rd, &feedfile_rdstyle, MAX_LINE_FEEDFILE,
+ feedfile_got_article,ipf, feedfile_problem,ipf);
+ if (r) sysdie("unable start reading feedfile %s",ipf->path);
+}
+
+static void inputfile_tailing_stop(InputFile *ipf) {
+ assert(ipf->fd);
+ oop_rd_cancel(ipf->rd);
+ oop_rd_delete(ipf->rd);
+ ipf->rd= 0;
+ assert(!ipf->filemon); /* we shouldn't be monitoring it now */
+}
+
+
+/*========== interaction with innd - state machine ==========*/
+
+/* See official state diagram at top of file. We implement
+ * this as follows:
+
+ ================
+ WAITING
+ [Nothing/Noduct]
+ poll for F
+ ================
+ |
+ | TIMEOUT and no defer, no backlog
+ |`--------------------------.
+ | |
+ | OPEN F SUCCEEDS | exit
+ ,--------->| V
+ | V =========
+ | ======== (ESRCH)
+ | NORMAL [Dropped]
+ | [Normal] =========
+ | main F tail
+ | ========
+ | |
+ | | F IS SO BIG WE SHOULD FLUSH
+ ^ | hardlink F to D
+ | [Hardlinked]
+ | | unlink F
+ | | our handle onto F is now onto D
+ | [Moved]
+ | |
+ | |<---------------------------------------------------.
+ | | |
+ | | spawn inndcomm flush |
+ | V |
+ | ========== |
+ | FLUSHING |
+ | [Flushing] |
+ | main D tail |
+ | ========== |
+ | | |
+ | | INNDCOMM FLUSH FAILS ^
+ | |`----------------------->----------. |
+ | | | |
+ | | NO SUCH SITE V |
+ ^ |`--------------->----. =========== |
+ | | \ FLUSHFAIL |
+ | | \ [Moved] |
+ | | \ main D tail |
+ | | \ =========== |
+ | | \ | |
+ | | \ | TIME TO RETRY |
+ | | \ `----------------'
+ | | FLUSH OK \
+ | | open F \
+ | V V
+ | ============= ============
+ | SEPARATED/ DROPPING/
+ | old->fd>=0 old->fd>=0
+ | [Separated] [Dropping]
+ | main F idle main none
+ | old D tail old D tail
+ | ============= ============
+ | | |
+ ^ | EOF ON D | EOF ON D
+ | V V
+ | =============== ===============
+ | SEPARATED/ DROPPING/
+ | old->fd==-1 old->fd==-1
+ | [Finishing] [Dropping]
+ | main F tail main none
+ | old D closed old D closed
+ | =============== ===============
+ | | |
+ | | ALL D PROCESSED | ALL D PROCESSED
+ | V install defer as backlog V install defer as backlog
+ ^ | close D | close D
+ | | unlink D | unlink D
+ | | | exit
+ `----------' V
+ ==========
+ (ESRCH)
+ [Droppped]
+ ==========
+ */
+
+static void statemc_init(void) {
+ struct stat stab, stabf;
+ int noent;
+
+ path_lock= xasprintf("%s_lock", feedfile);
+ path_flushing= xasprintf("%s_flushing", feedfile);
+ path_defer= xasprintf("%s_defer", feedfile);
+ globpat_backlog= xasprintf("%s_backlog*", feedfile);
+
+ for (;;) {
+ int lockfd= open(path_lock, O_CREAT|O_RDWR, 0600);
+ if (lockfd<0) sysfatal("open lockfile %s", path_lock);
+
+ struct flock fl;
+ memset(&fl,0,sizeof(fl));
+ fl.l_type= F_WRLCK;
+ fl.l_whence= SEEK_SET;
+ r= fcntl(lockfd, F_SETLK, &fl);
+ if (r==-1) {
+ if (errno==EACCES || errno==EAGAIN) {
+ if (quiet_if_locked) exit(0);
+ fatal("another duct holds the lockfile");
+ }
+ sysdie("fcntl F_SETLK lockfile %s", path_lock);
+ }
+
+ xfstat_isreg(lockfd, &stabf, "lockfile");
+ xlstat_isreg(path_lock, &stab, &noent, "lockfile");
+
+ if (!noent && samefile(&stab, &stabf))
+ break;
+
+ if (close(lockfd))
+ sysdie("could not close stale lockfile %s", path_lock);
+ }
+ debug("startup: locked");
+
+ search_backlog_file();
+
+ xlstat_isreg(path_defer, &stab, &noent, "defer file");
+ if (noent) {
+ debug("startup: ductdefer ENOENT");
+ } else {
+ debug("startup: ductdefer nlink=%ld", (long)stab.st_nlink);
+ switch (stab.st_nlink==1) {
+ case 1:
+ open_defer(); /* so that we will later close it and rename it */
+ break;
+ case 2:
+ if (unlink(path_defer))
+ sysdie("could not unlink stale defer file link %s (presumably"
+ " hardlink to backlog file)", path_defer);
+ break;
+ default:
+ die("defer file %s has unexpected link count %d",
+ path_defer, stab.st_nlink);
+ }
+ }
+
+ InputFile *file_d= open_input_file(path_flushing);
+
+ if (file_d) {
+ struct stat stab_f, stab_d;
+
+ xlstat_isreg(feedfile, &stab_f, &noent, "feed file");
+ if (noent) {
+ debug("startup: D exists, F ENOENT => Moved");
+ goto found_moved;
+ }
+
+ debug("startup: F and D both exist");
+
+ xfstat_isreg(file_d->fd, &stab_d, "flushing file");
+
+ if (samefile(&stab_d, &stab_f)) {
+ debug("startup: F==D => Hardlinked");
+ r= unlink(path_flushing);
+ if (r) sysdie("unlink feed file %s during startup", feedfile);
+ found_moved:
+ debug(" => Moved");
+ startup_set_input_file(file_d);
+ spawn_inndcomm_flush(); /* => Flushing, sets sms to sm_FLUSHING */
+ } else {
+ debug("F!=D => Separated");
+ SMS(SEPARATED, 0, "found both old and current feed files");
+ startup_set_input_file(file_d);
+ }
+ } else {
+ debug("startup: D ENOENT => Nothing");
+ fixme need to try flushing innd here - needs state diagram changes;
+ SMS(WAITING, open_wait_periods, "no feed file currently exists");
+ }
+}
+
+static void statemc_poll(void) {
+ if (sms==sm_WAITING) { statemc_waiting_poll(); return; }
+
+ if (!sm_period_counter) return;
+ sm_period_counter--;
+ assert(sm_period_counter>=0);
+
+ if (sm_period_counter) return;
+ switch (sms) {
+ case sm_WAITING:
+ fatal("timed out waiting for innd to create feed file %s", feedfile);
+ case sm_FLUSHFAIL:
+ spawn_inndcomm_flush(void);