+ info("CTRL%d %s ready", cc->fd, how);
+ cli_prompt(cc);
+ return 0;
+}
+
+static void cli_stdio_destroy(CliConn *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 cli_stdio(void) {
+ NEW_DECL(CliConn *,cc);
+ cc->destroy= cli_stdio_destroy;
+
+ cc->fd= 0;
+ cc->out= stdout;
+ int r= cli_conn_startup(cc,"stdio");
+ if (r) cc->destroy(cc);
+}
+
+static void cli_accepted_destroy(CliConn *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 *cli_master_readable(oop_source *lp, int master,
+ oop_event ev, void *u) {
+ NEW_DECL(CliConn *,cc);
+ cc->destroy= cli_accepted_destroy;
+
+ cc->salen= sizeof(cc->sa);
+ cc->fd= accept(master, &cc->sa.sa, &cc->salen);
+ if (cc->fd<0) { syswarn("error accepting cli connection"); goto x; }
+
+ cc->out= fdopen(cc->fd, "w");
+ if (!cc->out) { syswarn("error fdopening accepted cli connection"); goto x; }
+
+ int r= cli_conn_startup(cc, "accepted");
+ if (r) goto x;
+
+ return OOP_CONTINUE;
+
+ x:
+ cc->destroy(cc);
+ return OOP_CONTINUE;
+}
+
+#define NOCLI(...) do{ \
+ syswarn("no cli listener, because failed to " __VA_ARGS__); \
+ goto nocli; \
+ }while(0)
+
+static void cli_init(void) {
+ union {
+ struct sockaddr sa;
+ struct sockaddr_un un;
+ } sa;
+
+ memset(&sa,0,sizeof(sa));
+ int maxlen= sizeof(sa.un.sun_path);
+
+ if (!path_cli) {
+ info("control command line disabled");
+ return;
+ }
+
+ int pathlen= strlen(path_cli);
+ if (pathlen > maxlen) {
+ warn("no cli listener, because cli socket path %s too long (%d>%d)",
+ path_cli, pathlen, maxlen);
+ return;
+ }
+
+ if (path_cli_dir) {
+ int r= mkdir(path_cli_dir, 0700);
+ if (r && errno!=EEXIST)
+ NOCLI("create cli socket directory %s", path_cli_dir);
+ }
+
+ int r= unlink(path_cli);
+ if (r && errno!=ENOENT)
+ NOCLI("remove old cli socket %s", path_cli);
+
+ cli_master= socket(PF_UNIX, SOCK_STREAM, 0);
+ if (cli_master<0) NOCLI("create new cli master socket");
+
+ int sl= pathlen + offsetof(struct sockaddr_un, sun_path);
+ sa.un.sun_family= AF_UNIX;
+ memcpy(sa.un.sun_path, path_cli, pathlen);
+
+ r= bind(cli_master, &sa.sa, sl);
+ if (r) NOCLI("bind to cli socket path %s", sa.un.sun_path);
+
+ r= listen(cli_master, 5);
+ if (r) NOCLI("listen to cli master socket");
+
+ xsetnonblock(cli_master, 1);
+
+ loop->on_fd(loop, cli_master, OOP_READ, cli_master_readable, 0);
+ info("cli ready, listening on %s", path_cli);
+
+ return;
+
+ nocli:
+ xclose_perhaps(&cli_master, "cli master",0);
+ return;
+}
+
+/*========== management of connections ==========*/
+
+static void reconnect_blocking_event(void) {
+ until_connect= reconnect_delay_periods;
+}
+
+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 int conn_busy(Conn *conn) {
+ return
+ conn->waiting.count ||
+ conn->priority.count ||
+ conn->sent.count ||
+ conn->xmitu;
+}
+
+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);
+}
+
+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(art->ipf->queue, art);
+
+ while ((art= LIST_REMHEAD(conn->waiting)))
+ LIST_ADDTAIL(art->ipf->queue, art);
+
+ while ((art= LIST_REMHEAD(conn->sent))) {
+ requeue[art->state]++;
+ if (art->state==art_Unsolicited) art->state= art_Unchecked;
+ LIST_ADDTAIL(art->ipf->queue,art);
+ check_reading_pause_resume(art->ipf);
+ }
+
+ int i;
+ XmitDetails *d;
+ for (i=0, d=conn->xmitd; i<conn->xmitu; i++, d++)
+ xmit_free(d);
+
+ LIST_REMOVE(conns,conn);
+
+ char *m= xvasprintf(fmt,al);
+ warn("C%d (now %d) connection failed requeueing " RCI_TRIPLE_FMT_BASE ": %s",
+ conn->fd, conns.count, RCI_TRIPLE_VALS_BASE(requeue, /*nothing*/), m);
+ free(m);
+
+ reconnect_blocking_event();
+ 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 conn_idle_close(Conn *conn, const char *why) {
+ 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= why;
+ conn->since_activity= 0;
+ debug("C%d is idle (%s), quitting", conn->fd, why);
+ break;
+ }
+ }
+}
+
+/*
+ * For our last connection, we also shut it down if we have had
+ * less than K in the last L
+ */
+static void check_idle_conns(void) {
+ Conn *conn;
+
+ int volthisperiod= lowvol_perperiod[lowvol_circptr];
+ lowvol_circptr++;
+ lowvol_circptr %= lowvol_periods;
+ lowvol_total += volthisperiod;
+ lowvol_total -= lowvol_perperiod[lowvol_circptr];
+ lowvol_perperiod[lowvol_circptr]= 0;
+
+ FOR_CONN(conn)
+ conn->since_activity++;
+
+ search_again:
+ FOR_CONN(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 (%s)",
+ conn->quitting);
+ 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
+ conn_idle_close(conn, "no activity");
+
+ goto search_again;
+ }
+
+ conn= LIST_HEAD(conns);
+ if (!volthisperiod &&
+ conns.count==1 &&
+ lowvol_total < lowvol_thresh &&
+ !conn_busy(conn))
+ conn_idle_close(conn, "low volume");
+}
+
+/*---------- making new connections ----------*/
+
+static pid_t connecting_child;
+static int connecting_fdpass_sock;
+
+static void connect_attempt_discard(void) {
+ if (connecting_child) {
+ int status= xwaitpid(&connecting_child, "connect");
+ if (!(WIFEXITED(status) ||
+ (WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL)))
+ report_child_status("connect", status);
+ }
+ if (connecting_fdpass_sock) {
+ cancel_fd_read_except(connecting_fdpass_sock);
+ xclose_perhaps(&connecting_fdpass_sock, "connecting fdpass socket",0);
+ }
+}
+
+#define PREP_DECL_MSG_CMSG(msg) \
+ char msgbyte= 0; \
+ struct iovec msgiov; \
+ msgiov.iov_base= &msgbyte; \
+ msgiov.iov_len= 1; \
+ struct msghdr msg; \
+ memset(&msg,0,sizeof(msg)); \
+ char msg##cbuf[CMSG_SPACE(sizeof(int))]; \
+ msg.msg_iov= &msgiov; \
+ msg.msg_iovlen= 1; \
+ msg.msg_control= msg##cbuf; \
+ msg.msg_controllen= sizeof(msg##cbuf);
+
+static void *connchild_event(oop_source *lp, int fd, oop_event e, void *u) {
+ Conn *conn= 0;
+
+ assert(fd == connecting_fdpass_sock);
+
+ PREP_DECL_MSG_CMSG(msg);
+
+ ssize_t rs= recvmsg(fd, &msg, 0);
+ if (rs<0) {
+ if (isewouldblock(errno)) return OOP_CONTINUE;
+ syswarn("failed to read socket from connecting child");
+ goto x;
+ }
+
+ NEW(conn);
+ LIST_INIT(conn->waiting);
+ LIST_INIT(conn->priority);
+ LIST_INIT(conn->sent);
+
+ struct cmsghdr *h= 0;
+ if (rs >= 0) h= CMSG_FIRSTHDR(&msg);
+ if (!h) {
+ int status= xwaitpid(&connecting_child, "connect child (broken)");
+
+ if (WIFEXITED(status)) {
+ if (WEXITSTATUS(status) != 0 &&
+ WEXITSTATUS(status) != CONNCHILD_ESTATUS_STREAM &&
+ WEXITSTATUS(status) != CONNCHILD_ESTATUS_NOSTREAM)
+ /* child already reported the problem */;
+ else {
+ if (e == OOP_EXCEPTION)
+ warn("connect: connection child exited code %d but"
+ " unexpected exception on fdpass socket",
+ WEXITSTATUS(status));
+ else
+ warn("connect: connection child exited code %d but"
+ " no cmsg (rs=%d)",
+ WEXITSTATUS(status), (int)rs);
+ }
+ } else if (WIFSIGNALED(status) && WTERMSIG(status) == SIGALRM) {
+ warn("connect: connection attempt timed out");
+ } else {
+ report_child_status("connect", status);
+ }
+ goto x;
+ }
+
+#define CHK(field, val) \
+ if (h->cmsg_##field != val) { \
+ die("connect: child sent cmsg with cmsg_" #field "=%d, expected %d", \
+ h->cmsg_##field, val); \
+ goto x; \
+ }
+ CHK(level, SOL_SOCKET);
+ CHK(type, SCM_RIGHTS);
+ CHK(len, CMSG_LEN(sizeof(conn->fd)));
+#undef CHK
+
+ if (CMSG_NXTHDR(&msg,h)) die("connect: child sent many cmsgs");
+
+ memcpy(&conn->fd, CMSG_DATA(h), sizeof(conn->fd));
+
+ int status;
+ pid_t got= waitpid(connecting_child, &status, 0);
+ if (got==-1) sysdie("connect: real wait for child");
+ assert(got == connecting_child);
+ connecting_child= 0;
+
+ if (!WIFEXITED(status)) { report_child_status("connect",status); goto x; }
+ int es= WEXITSTATUS(status);
+ switch (es) {
+ case CONNCHILD_ESTATUS_STREAM: conn->stream= 1; break;
+ case CONNCHILD_ESTATUS_NOSTREAM: conn->stream= 0; break;
+ default:
+ fatal("connect: child gave unexpected exit status %d", es);
+ }
+
+ /* Phew! */
+ conn->max_queue= conn->stream ? max_queue_per_conn : 1;
+
+ loop->on_fd(loop, conn->fd, OOP_EXCEPTION, conn_exception, conn);
+ conn->rd= oop_rd_new_fd(loop,conn->fd, 0, 0); /* sets nonblocking, too */
+ if (!conn->fd) die("oop_rd_new_fd conn failed (fd=%d)",conn->fd);
+ int r= oop_rd_read(conn->rd, &peer_rd_style, NNTP_STRLEN,
+ &peer_rd_ok, conn,
+ &peer_rd_err, conn);
+ if (r) sysdie("oop_rd_read for peer (fd=%d)",conn->fd);
+
+ LIST_ADDHEAD(conns, conn);
+ notice("C%d (now %d) connected %s",
+ conn->fd, conns.count, conn->stream ? "streaming" : "plain");
+
+ connect_attempt_discard();
+ check_assign_articles();
+ return OOP_CONTINUE;
+
+ x:
+ conn_dispose(conn);
+ connect_attempt_discard();
+ reconnect_blocking_event();
+ return OOP_CONTINUE;
+}
+
+static int allow_connect_start(void) {
+ return conns.count < max_connections
+ && !connecting_child
+ && !until_connect;
+}
+
+static void connect_start(void) {
+ assert(!connecting_child);
+ assert(!connecting_fdpass_sock);
+
+ info("starting connection attempt");
+ int ok_until_connect= until_connect;
+ reconnect_blocking_event();
+
+ int socks[2];
+ int r= socketpair(AF_UNIX, SOCK_STREAM, 0, socks);
+ if (r) { syswarn("connect: cannot create socketpair for child"); return; }
+
+ connecting_child= xfork("connection");
+
+ if (!connecting_child) {
+ FILE *cn_from, *cn_to;
+ char buf[NNTP_STRLEN+100];
+ int exitstatus= CONNCHILD_ESTATUS_NOSTREAM;
+
+ xclose(socks[0], "(in child) parent's connection fdpass socket",0);