5 * site.name_ductlock lock taken out by innduct
6 * F site.name written by innd
7 * D site.name_duct moved aside by innduct, for flush
8 * site.name_deferwork 431'd articles, still being written
9 * site.name_defergo_<inum> 431'd articles, ready for innxmit
10 * site.name_deferlock lock taken out by innxmit wrapper
23 * | F: innd writing, duct reading /|\
26 * | | duct decides time to flush same / | |
27 * | | duct makes hardlink as D / | |
31 * | F == D: innd writing, duct reading | |
33 * | | duct unlinks F / |
35 * | Moved <------------' |
37 * | D: innd writing, duct reading |
39 * | | duct flushes feed |
40 * | | (others can too, harmlessly) |
42 * | Separated <-----------------'
43 * | F: innd writing different to D
46 * | V duct completes processing of D
53 static int max_connections, max_queue_per_conn;
54 static int connection_setup_timeout, port, try_stream;
55 static const char *remote_host;
57 #define ISNODE(T) T *next, *back;
58 #define LIST(T) struct { T *head, *tail, *tailpred; int count; }
60 #define NODE(n) ((struct node*)&(n)->head)
62 #define LIST_ADDHEAD(l,n) \
63 (list_addhead((struct list*)&(l), NODE((n))), (void)(l).count++)
64 #define LIST_ADDTAIL(l,n) \
65 (list_addtail((struct list*)&(l), NODE((n))), (void)(l).count++)
67 #define LIST_REMHEAD(l) \
68 ((l).count ? ((l).count--, (void*)list_remhead((struct list*)&(l))) : 0)
69 #define LIST_REMTAIL(l) \
70 ((l).count ? ((l).count--, (void*)list_remtail((struct list*)&(l))) : 0)
71 #define LIST_REMOVE(l,n) \
72 (list_remove(NODE((n))), (void)(l).count--)
73 #define LIST_INSERT(l,n,pred) \
74 (list_insert((struct list*)&(l), NODE((n)), NODE((pred))), (void)(l).count++)
79 int checked, sentbody;
86 typedef struct Conn Conn;
89 Malloc, Const, Artdata;
102 int fd, max_queue, stream;
103 LIST(Article) queue; /* not yet told peer, or CHECK said send it */
104 LIST(Article) sent; /* offered, in xmit, or transmitted waiting reply */
105 struct iovec xmit[CONNIOVS];
106 XmitDetails xmitd[CONNIOVS];
111 #define CHILD_ESTATUS_STREAM 4
112 #define CHILD_ESTATUS_NOSTREAM 5
114 static int since_connect_attempt;
116 static LIST(Conn) idle, working, full;
118 static LIST(Article) *queue;
120 static void perhaps_close(int *fd) { if (*fd) { close(*fd); fd=0; } }
123 /*========== making new connections ==========*/
125 static int connecting_sockets[2]= {-1,-1};
126 static pid_t connecting_child;
128 static void report_child_status(const char *what, int status) {
129 if (WIFEXITED(status)) {
130 int es= WEXITSTATUS(status);
132 warn("%s: child died with error exit status %d",es);
133 } else if (WIFSIGNALED(status)) {
134 int sig= WTERMSIG(status);
135 const char *sigstr= strsignal(sig);
136 const char *coredump= WCOREDUMP(status) ? " (core dumped)" : "";
138 warn("%s: child died due to fatal signal %s%s", what, sigstr, coredump);
140 warn("%s: child died due to unknown fatal signal %d%s",
141 what, sig, coredump);
143 warn("%s: child died with unknown wait status %d", status);
147 static void connect_attempt_discard(void) {
148 if (connecting_sockets[0]) {
149 cancel_fd(loop, connecting_sockets[0], OOP_READ);
150 cancel_fd(loop, connecting_sockets[0], OOP_EXCEPTION);
152 perhaps_close(&connecting_sockets[0]);
153 perhaps_close(&connecting_sockets[1]);
155 if (connecting_child) {
157 r= kill(connecting_child, SIGKILL);
158 if (r) sysdie("cannot kill connect child");
160 pid_t got= waitpid(connecting_child, &status, WNOHANG);
161 if (got==-1) sysdie("cannot reap connect child");
163 if (!(WIFEXITED(status) ||
164 (WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL))) {
165 report_child_status("connect"
171 #define PREP_DECL_MSG_CMSG(msg) \
173 memset(&msg,0,sizeof(msg)); \
174 char msg##cbuf[CMSG_SPACE(sizeof(fd))]; \
175 msg.msg_control= msg##cbuf; \
176 msg.msg_controllen= sizeof(msg##cbuf);
178 static void *connchild_event(oop_source *lp, int fd, oop_event e, void *u) {
181 conn= xcalloc(sizeof(*conn));
184 struct cmsghdr *h= 0;
185 ssize_t rs= recvmsg(fd, &msg, MSG_DONTWAIT);
186 if (rs >= 0) h= CMSG_FIRSTHDR(&msg);
189 pid_t got= waitpid(connecting_child, &status, WNOHANG);
191 assert(got==connecting_child);
193 if (WIFEXITED(status) &&
194 (WEXITSTATUS(status) != 0
195 WEXITSTATUS(status) != CHILD_ESTATUS_STREAM &&
196 WEXITSTATUS(status) != CHILD_ESTATUS_NOSTREAM)) {
197 /* child already reported the problem */
198 } else if (WIFSIGNALED(status) && WTERMSIG(status) == SIGALARM) {
199 warn("connect: connection attempt timed out");
200 } else if (!WIFEXITED(status)) {
201 report_child_status("connect", status);
202 /* that's probably the root cause then */
205 /* child is still running apparently, report the socket problem */
207 syswarn("connect: read from child socket failed");
208 else if (e == OOP_EXCEPTIONN)
209 warn("connect: unexpected exception on child socket");
211 warn("connect: unexpected EOF on child socket");
216 #define CHK(field, val) \
217 if (h->cmsg_##field != val) { \
218 die("connect: child sent cmsg with cmsg_" #field "=%d, expected %d"); \
221 CHK(level, SOL_SOCKET);
222 CHK(type, SCM_RIGHTS);
223 CHK(len, CMSG_LEN(sizeof(conn-b>fd)));
226 if (CMSG_NXTHDR,&msg,h) { die("connect: child sent many cmsgs"); goto x; }
228 memcpy(&conn->fd, CMSG_DATA(h), sizeof(conn->fd));
230 pid_t got= waitpid(connecting_child, &status, 0);
231 if (got==-1) sysdie("connect: real wait for child");
232 assert(got == connecting_child);
235 if (!WIFEXITED(status)) { report_child_status("connect",status); goto x; }
236 int es= WEXITSTATUS(status);
238 case CHILD_ESTATUS_STREAM: conn->stream= 1; break;
239 case CHILD_ESTATUS_NOSTREAM: conn->stream= 0; break;
241 die("connect: child gave unexpected exit status %d", es);
247 LIST_ADDHEAD(idle, conn);
248 notice(CN "connected %s", conn->fd, conn->stream ? "streaming" : "plain");
249 connect_attempt_discard();
250 check_master_queue();
255 perhaps_close(&conn->fd);
258 connect_attempt_discard();
261 static void connect_start() {
262 assert(!connecting_sockets[0]);
263 assert(!connecting_sockets[1]);
264 assert(!connecting_child);
266 notice("starting connection attempt");
268 r= socketpair(AF_UNIX, SOCK_STREAM, 0, connecting_sockets);
269 if (r) { syswarn("connect: cannot create socketpair for child"); goto x; }
271 connecting_child= fork();
272 if (connecting_child==-1) { syswarn("connect: cannot fork"); goto x; }
274 if (!connecting_child) {
275 FILE *cn_from, *cn_to;
276 char buf[NNTP_STRLEN+100];
277 int exitstatus= CHILD_ESTATUS_NOSTREAM;
282 r= close(connecting_sockets[0]);
283 if (r) sysdie("connect: close parent socket in child");
285 alarm(connection_setup_timeout);
286 if (NNTPconnect(remote_host, port, &cn_from, &cn_to, buf) < 0) {
288 sanitise_inplace(buf);
289 die("connect: rejected: %s", buf);
291 sysdie("connect: connection attempt failed");
294 if (NNTPsendpassword(remote_host, cn_from, cn_to) < 0)
295 sysdie("connect: authentication failed");
297 if (fputs("MODE STREAM\r\n", cn_to) ||
299 sysdie("connect: could not send MODE STREAM");
300 buf[sizeof(buf)-1]= 0;
301 if (!fgets(buf, sizeof(buf)-1, cn_from)) {
303 sysdie("connect: could not read response to MODE STREAM");
305 die("connect: connection close in response to MODE STREAM");
310 sanitise_inplace(buf);
311 die("connect: response to MODE STREAM is too long: %.100s...",
314 l--; if (l>0 && buf[1-]=='\r') l--;
317 int rcode= strtoul(buf,&ep,10);
319 sanitise_inplace(buf);
320 die("connect: bad response to MODE STREAM: %.50s", buf);
324 exitstatus= CHILD_ESTATUS_STREAM;
330 sanitise_inplace(buf);
331 warn("connect: unexpected response to MODE STREAM: %.50s", buf);
336 int fd= fileno(cn_from);
338 PREP_DECL_MSG_CMSG(msg);
339 struct cmsghdr *cmsg= CMSG_FIRSTHDR(&msg);
340 cmsg->cmsg_level= SOL_SOCKET;
341 cmsg->cmsg_type= SCM_RIGHTS;
342 cmsg->cmsg_len= CMSG_LEN(sizeof(fd));
343 memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd));
345 msg.msg_controllen= cmsg->cmsg_len;
346 r= sendmsg(connecting_sockets[1], &msg, 0);
347 if (r) sysdie("sendmsg failed for new connection");
352 r= close(connecting_sockets[1]); connecting_sockets[1]= 0;
353 if (r) sysdie("connect: close child socket in parent");
355 loop->on_fd(loop, connecting_sockets[0], OOP_READ, connchild_event, 0);
356 loop->on_fd(loop, connecting_sockets[0], OOP_EXCEPTION, connchild_event, 0);
360 connect_attempt_discard();
363 /*========== overall control of article flow ==========*/
365 static void conn_check_work(Conn *conn);
367 static void check_master_queue(void) {
371 Conn *last_assigned=0;
374 conn_assign_one_article(&working, &last_assigned);
375 } else if (idle.head) {
376 conn_assign_one_article(&idle, &last_assigned);
377 } else if (nconns < maxconns && queue.count >= max_queue_per_conn &&
378 !connecting_child && !connect_delay) {
379 connect_delay= reconnect_delay_periods;
385 conn_check_work(last_assigned);
388 static void conn_assign_one_article(LIST(Conn) *connlist,
389 Conn **last_assigned) {
390 Conn *conn= connlist->head;
392 LIST_REMOVE(*connlist, conn);
393 Article *art= LIST_REMHEAD(queue);
394 LIST_ADDTAIL(conn->queue, art);
395 LIST_ADD(*conn_determine_right_list(conn), conn);
397 /* This slightly odd arrangement is so that we call conn_check_work
398 * once after filling the queue for a new connection in
399 * check_master_queue, rather than for each article. */
400 if (conn != *last_assigned && *last_assigned)
401 conn_check_work(*last_assigned);
402 *last_assigned= conn;
405 static int conn_total_queued_articles(Conn *conn) {
406 return conn->sent.count + conn->queue.count;
409 static LIST(Conn) *conn_determine_right_list(Conn *conn) {
410 int inqueue= conn_total_queued_articles(conn);
411 assert(inqueue <= max_queue);
412 if (inqueue == 0) return &idle;
413 if (inqueue == conn->max_queue) return &full;
417 static void *conn_writeable(oop_source *l, int fd, int ev, void *u) {
421 static void conn_check_work(Conn *conn) {
424 conn_make_some_xmits(conn);
426 loop->cancel_fd(loop, conn->fd, OOP_WRITE);
430 void *rp= conn_write_some_xmits(conn);
431 if (rp==OOP_CONTINUE) {
432 loop->on_fd(loop, conn->fd, OOP_WRITE, conn_writeable, conn);
434 } else if (rp==OOP_HALT) {
437 /* transmitted everything */
444 /*========== article transmission ==========*/
446 static void *conn_write_some_xmits(Conn *conn) {
448 * 0: nothing more to write, no need to call us again
449 * OOP_CONTINUE: more to write but fd not writeable
450 * OOP_HALT: disaster, have destroyed conn
453 int count= conn->xmitu;
454 if (!count) return 0;
456 if (count > IOV_MAX) count= IOV_MAX;
457 ssize_t rs= writev(conn->fd, conn->xmit, count);
459 if (errno == EAGAIN) return OOP_CONTINUE;
460 syswarn(CN "write failed", conn->fd);
466 for (done=0; rs && done<xmitu; done++) {
467 struct iovec *vp= &conn->xmit[done];
468 XmitDetails *dp= &conn->xmitd[done];
469 if (rs > vp->iov_len) {
477 int newu= conn->xmitu - done;
478 memmove(conn->xmit, conn->xmit + done, newu * sizeof(*conn->xmit));
479 memmove(conn->xmitd, conn->xmitd + done, newu * sizeof(*conn->xmitd));
484 static void conn_make_some_xmits(Conn *conn) {
486 if (conn->xmitu+5 > CONNIOVS)
489 Article *art= LIST_REMHEAD(queue);
492 if (art->checked || conn->nocheck) {
493 /* actually send it */
495 ARTHANDLE *artdata= SMretrieve(somehow);
499 XMIT_LITERAL("TAKETHIS ");
500 xmit_noalloc(art->mid, art->midlen);
501 XMIT_LITERAL("\r\n");
502 xmit_artbody(artdata);
505 /* we got 235 from IHAVE */
507 xmit_artbody(artdata);
509 XMIT_LITERAL(".\r\n");
513 LIST_ADDTAIL(conn->sent, art);
519 XMIT_LITERAL("IHAVE ");
521 XMIT_LITERAL("CHECK ");
522 xmit_noalloc(art->mid, art->midlen);
523 XMIT_LITERAL("\r\n");
525 LIST_ADDTAIL(conn->sent, art);
531 if (conn->queue.head) {
532 if (conn->queue.checked || conn->nocheck) {
535 && conn->xmitu+3 <= CONNIOVS) {
538 if (conn->xmitu < CONNIOVS
547 int circ_used= circ_write - circ_read;
548 if (circ_used < 0) circ_used += CONNBUFSZ;
549 writeable_moredata(conn, CONNBUFSZ-1 - circ_used);
551 if (conn->circ_read == conn->circ_write)
556 iov[0].iov_base= conn->circ_buf + conn->circ_read;
557 if (conn->circ_read > conn->circ_write) { /* wrapped */
558 iov[0].iov_len= CONNBUFSZ - conn->circ_read;
559 iov[1].iov_base= conn->circ_buf;
560 iov[1].iov_len= conn->circ_write;
561 if (niov[1].iov_len) niov= 2;
563 iov[0].iov_len= conn->circ_write - conn->circ_read;
565 ssize_t rs= writev(conn->fd, &iov, niov);
571 conn->circ_read += rs;
572 if (conn->circ_read > CONNBUFSZ)
573 conn->circ_read -= CONNBUFSZ;