+static void vconnfail(Conn *conn, const char *fmt, va_list al) {
+ int requeue[art_MaxState];
+
+ 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;
+ }
+}