+static const oop_rd_style control_rd_style= {
+ OOP_RD_DELIM_STRIP, '\n',
+ OOP_RD_NUL_FORBID,
+ OOP_RD_SHORTREC_FORBID
+};
+
+static void control_destroy(ControlConn *cc) {
+ cc->destroy(cc);
+}
+
+static void control_checkouterr(ControlConn *cc /* may destroy*/) {
+ if (ferror(cc->out) | fflush(cc->out)) {
+ info("CTRL%d write error %s", cc->fd, strerror(errno));
+ control_destroy(cc);
+ }
+}
+
+static void control_prompt(ControlConn *cc /* may destroy*/) {
+ fprintf(cc->out, "%s| ", sitename);
+ control_checkouterr(cc);
+}
+
+typedef struct ControlCommand ControlCommand;
+struct ControlCommand {
+ const char *cmd;
+ void (*f)(ControlConn *cc, const ControlCommand *ccmd,
+ const char *arg, size_t argsz);
+ void *xdata;
+ int xval;
+};
+
+static const ControlCommand control_commands[];
+
+#define CCMD(wh) \
+ static void ccmd_##wh(ControlConn *cc, const ControlCommand *c, \
+ const char *arg, size_t argsz)
+
+CCMD(help) {
+ fputs("commands:\n", cc->out);
+ const ControlCommand *ccmd;
+ for (ccmd=control_commands; ccmd->cmd; ccmd++)
+ fprintf(cc->out, " %s\n", ccmd->cmd);
+}
+
+CCMD(period) { period(); }
+CCMD(setintarg) { *(int*)c->xdata= atoi(arg); }
+CCMD(setint) { *(int*)c->xdata= c->xval; }
+
+static const ControlCommand control_commands[]= {
+ { "h", ccmd_help },
+ { "p", ccmd_period },
+ { "pretend flush", ccmd_setintarg, &simulate_flush },
+ { "poke sm", ccmd_setint, &sm_period_counter, 1 },
+ { "poke conn", ccmd_setint, &until_connect, 0 },
+ { "poke blscan", ccmd_setint, &until_backlog_nextscan, 0 },
+ { "wedge blscan", ccmd_setint, &until_backlog_nextscan, -1 },
+ { 0 }
+};
+
+static void *control_rd_ok(oop_source *lp, oop_read *oread, oop_rd_event ev,
+ const char *errmsg, int errnoval,
+ const char *data, size_t recsz, void *cc_v) {
+ ControlConn *cc= cc_v;
+
+ if (!data) {
+ info("CTRL%d closed", cc->fd);
+ cc->destroy(cc);
+ return OOP_CONTINUE;
+ }
+
+ if (recsz == 0) goto prompt;
+
+ const ControlCommand *ccmd;
+ for (ccmd=control_commands; ccmd->cmd; ccmd++) {
+ int l= strlen(ccmd->cmd);
+ if (recsz < l) continue;
+ if (recsz > l && data[l] != ' ') continue;
+ if (memcmp(data, ccmd->cmd, l)) continue;
+
+ int argl= (int)recsz - (l+1);
+ ccmd->f(cc, ccmd, argl>=0 ? data+l+1 : 0, argl);
+ goto prompt;
+ }
+
+ fputs("unknown command; h for help\n", cc->out);
+
+ prompt:
+ control_prompt(cc);
+ return OOP_CONTINUE;
+}
+
+static void *control_rd_err(oop_source *lp, oop_read *oread, oop_rd_event ev,
+ const char *errmsg, int errnoval,
+ const char *data, size_t recsz, void *cc_v) {
+ ControlConn *cc= cc_v;
+
+ info("CTRL%d read error %s", cc->fd, errmsg);
+ cc->destroy(cc);
+ return OOP_CONTINUE;
+}
+
+static int control_conn_startup(ControlConn *cc /* may destroy*/,
+ const char *how) {
+ cc->rd= oop_rd_new_fd(loop, cc->fd, 0,0);
+ if (!cc->rd) { warn("oop_rd_new_fd control failed"); return -1; }
+
+ int er= oop_rd_read(cc->rd, &control_rd_style, MAX_CONTROL_COMMAND,
+ control_rd_ok, cc,
+ control_rd_err, cc);
+ if (er) { errno= er; syswarn("oop_rd_read control failed"); return -1; }
+
+ info("CTRL%d %s ready", cc->fd, how);
+ control_prompt(cc);
+ return 0;
+}
+
+static void control_stdio_destroy(ControlConn *cc) {
+ if (cc->rd) {
+ oop_rd_cancel(cc->rd);
+ errno= oop_rd_delete_tidy(cc->rd);
+ if (errno) syswarn("oop_rd_delete tidy failed (no-nonblock stdin?)");
+ }
+ free(cc);
+}
+
+static void control_stdio(void) {
+ ControlConn *cc= xmalloc(sizeof(*cc));
+ memset(cc,0,sizeof(*cc));
+ cc->destroy= control_stdio_destroy;
+
+ cc->fd= 0;
+ cc->out= stdout;
+ int r= control_conn_startup(cc,"stdio");
+ if (r) cc->destroy(cc);
+}
+
+static void control_accepted_destroy(ControlConn *cc) {
+ if (cc->rd) {
+ oop_rd_cancel(cc->rd);
+ oop_rd_delete_kill(cc->rd);
+ }
+ if (cc->out) { fclose(cc->out); cc->fd=0; }
+ close_perhaps(&cc->fd);
+ free(cc);
+}
+
+static void *control_master_readable(oop_source *lp, int master,
+ oop_event ev, void *u) {
+ ControlConn *cc= xmalloc(sizeof(*cc));
+ memset(cc,0,sizeof(*cc));
+ cc->destroy= control_accepted_destroy;
+
+ cc->salen= sizeof(cc->sa);
+ cc->fd= accept(master, &cc->sa.sa, &cc->salen);
+ if (cc->fd<0) { syswarn("error accepting control connection"); goto x; }
+
+ cc->out= fdopen(cc->fd, "w");
+ if (!cc->out) { syswarn("error fdopening accepted control conn"); goto x; }
+
+ int r= control_conn_startup(cc, "accepted");
+ if (r) goto x;
+
+ return OOP_CONTINUE;
+
+ x:
+ cc->destroy(cc);
+ return OOP_CONTINUE;
+}
+
+#define NOCONTROL(...) do{ \
+ syswarn("no control socket, because failed to " __VA_ARGS__); \
+ goto nocontrol; \
+ }while(0)
+
+static void control_init(void) {
+ char *real=0;
+
+ union {
+ struct sockaddr sa;
+ struct sockaddr_un un;
+ } sa;
+
+ memset(&sa,0,sizeof(sa));
+ int maxlen= sizeof(sa.un.sun_path);
+
+ int reallen= readlink(path_control, sa.un.sun_path, maxlen);
+ if (reallen<0) {
+ if (errno != ENOENT)
+ NOCONTROL("readlink control socket symlink path %s", path_control);
+ }
+ if (reallen >= maxlen) {
+ debug("control socket symlink path too long (r=%d)",reallen);
+ xunlink(path_control, "old (overlong) control socket symlink");
+ reallen= -1;
+ }
+
+ if (reallen<0) {
+ struct stat stab;
+ int r= lstat(realsockdir,&stab);
+ if (r) {
+ if (errno != ENOENT) NOCONTROL("lstat real socket dir %s", realsockdir);
+
+ r= mkdir(realsockdir, 0700);
+ if (r) NOCONTROL("mkdir real socket dir %s", realsockdir);
+
+ } else {
+ uid_t self= geteuid();
+ if (!S_ISDIR(stab.st_mode) ||
+ stab.st_uid != self ||
+ stab.st_mode & 0077) {
+ warn("no control socket, because real socket directory"
+ " is somehow wrong (ISDIR=%d, uid=%lu (exp.%lu), mode %lo)",
+ !!S_ISDIR(stab.st_mode),
+ (unsigned long)stab.st_uid, (unsigned long)self,
+ (unsigned long)stab.st_mode & 0777UL);
+ goto nocontrol;
+ }
+ }
+
+ real= xasprintf("%s/s%lx.%lx", realsockdir,
+ (unsigned long)xtime(), (unsigned long)self_pid);
+ int reallen= strlen(real);
+
+ if (reallen >= maxlen) {
+ warn("no control socket, because tmpnam gave overly-long path"
+ " %s", real);
+ goto nocontrol;
+ }
+ r= symlink(real, path_control);
+ if (r) NOCONTROL("make control socket path %s a symlink to real"
+ " socket path %s", path_control, real);
+ memcpy(sa.un.sun_path, real, reallen);
+ }
+
+ int r= unlink(sa.un.sun_path);
+ if (r && errno!=ENOENT)
+ NOCONTROL("remove old real socket %s", sa.un.sun_path);
+
+ control_master= socket(PF_UNIX, SOCK_STREAM, 0);
+ if (control_master<0) NOCONTROL("create new control socket");
+
+ sa.un.sun_family= AF_UNIX;
+ int sl= strlen(sa.un.sun_path) + offsetof(struct sockaddr_un, sun_path);
+ r= bind(control_master, &sa.sa, sl);
+ if (r) NOCONTROL("bind to real socket path %s", sa.un.sun_path);
+
+ r= listen(control_master, 5);
+ if (r) NOCONTROL("listen");
+
+ xsetnonblock(control_master, 1);
+
+ loop->on_fd(loop, control_master, OOP_READ, control_master_readable, 0);
+ info("control socket ok, real path %s", sa.un.sun_path);
+
+ return;
+
+ nocontrol:
+ free(real);
+ xclose_perhaps(&control_master, "control master",0);
+ return;
+}
+
+/*========== management of connections ==========*/
+
+static void conn_closefd(Conn *conn, const char *msgprefix) {
+ int r= close_perhaps(&conn->fd);
+ if (r) info("C%d %serror closing socket: %s",
+ conn->fd, msgprefix, strerror(errno));
+}
+
+static void conn_dispose(Conn *conn) {
+ if (!conn) return;
+ if (conn->rd) {
+ oop_rd_cancel(conn->rd);
+ oop_rd_delete_kill(conn->rd);
+ conn->rd= 0;
+ }
+ if (conn->fd) {
+ loop->cancel_fd(loop, conn->fd, OOP_WRITE);
+ loop->cancel_fd(loop, conn->fd, OOP_EXCEPTION);
+ }
+ conn_closefd(conn,"");
+ free(conn);
+ until_connect= reconnect_delay_periods;
+}
+
+static void *conn_exception(oop_source *lp, int fd,
+ oop_event ev, void *conn_v) {
+ Conn *conn= conn_v;
+ unsigned char ch;
+ assert(fd == conn->fd);
+ assert(ev == OOP_EXCEPTION);
+ int r= read(conn->fd, &ch, 1);
+ if (r<0) connfail(conn,"read failed: %s",strerror(errno));
+ else connfail(conn,"exceptional condition on socket (peer sent urgent"
+ " data? read(,&ch,1)=%d,ch='\\x%02x')",r,ch);
+ return OOP_CONTINUE;
+}
+
+static void vconnfail(Conn *conn, const char *fmt, va_list al) {
+ int requeue[art_MaxState];
+ memset(requeue,0,sizeof(requeue));
+
+ Article *art;
+ while ((art= LIST_REMHEAD(conn->priority))) LIST_ADDTAIL(queue, art);
+ while ((art= LIST_REMHEAD(conn->waiting))) LIST_ADDTAIL(queue, art);
+ while ((art= LIST_REMHEAD(conn->sent))) {
+ requeue[art->state]++;
+ if (art->state==art_Unsolicited) art->state= art_Unchecked;
+ LIST_ADDTAIL(queue,art);
+ }
+
+ int i;
+ XmitDetails *d;
+ for (i=0, d=conn->xmitd; i<conn->xmitu; i++, d++)
+ xmit_free(d);
+
+ char *m= xvasprintf(fmt,al);
+ warn("C%d connection failed (requeueing " RCI_TRIPLE_FMT_BASE "): %s",
+ conn->fd, RCI_TRIPLE_VALS_BASE(requeue, /*nothing*/), m);
+ free(m);
+
+ LIST_REMOVE(conns,conn);
+ conn_dispose(conn);
+ check_assign_articles();
+}
+
+static void connfail(Conn *conn, const char *fmt, ...) {
+ va_list al;
+ va_start(al,fmt);
+ vconnfail(conn,fmt,al);
+ va_end(al);
+}
+
+static void check_idle_conns(void) {
+ Conn *conn;
+ for (conn=LIST_HEAD(conns); conn; conn=LIST_NEXT(conn))
+ conn->since_activity++;
+ search_again:
+ for (conn=LIST_HEAD(conns); conn; conn=LIST_NEXT(conn)) {
+ if (conn->since_activity <= need_activity_periods) continue;
+
+ /* We need to shut this down */
+ if (conn->quitting)
+ connfail(conn,"timed out waiting for response to QUIT");
+ else if (conn->sent.count)
+ connfail(conn,"timed out waiting for responses");
+ else if (conn->waiting.count || conn->priority.count)
+ connfail(conn,"BUG IN INNDUCT conn has queue but nothing sent");
+ else if (conn->xmitu)
+ connfail(conn,"peer has been sending responses"
+ " before receiving our commands!");
+ else {
+ static const char quitcmd[]= "QUIT\r\n";
+ int todo= sizeof(quitcmd)-1;
+ const char *p= quitcmd;
+ for (;;) {
+ int r= write(conn->fd, p, todo);
+ if (r<0) {
+ if (isewouldblock(errno))
+ connfail(conn, "blocked writing QUIT to idle connection");
+ else
+ connfail(conn, "failed to write QUIT to idle connection: %s",
+ strerror(errno));
+ break;
+ }
+ assert(r<=todo);
+ todo -= r;
+ if (!todo) {
+ conn->quitting= 1;
+ conn->since_activity= 0;
+ debug("C%d is idle, quitting", conn->fd);
+ break;
+ }
+ }
+ }
+ goto search_again;
+ }
+}
+
+/*---------- making new connections ----------*/
+
+static pid_t connecting_child;
+static int connecting_fdpass_sock;
+
+static void connect_attempt_discard(void) {