1 diff -u --new-file qmail-1.03-orig/EXTTODO qmail-1.03/EXTTODO
2 --- qmail-1.03-orig/EXTTODO 1969-12-31 21:00:00.000000000 -0300
3 +++ qmail-1.03/EXTTODO 2006-03-21 17:00:59.000000000 -0300
5 +EXTTODO by Claudio Jeker <jeker@n-r-g.com> and
6 +Andre Oppermann <opi@nrg4u.com>
7 +(c) 1998,1999,2000,2001,2002 Internet Business Solutions Ltd.
9 +The EXTTODO patch is a part of the qmail-ldap patch.
10 +This patches for qmail come with NO WARRANTY.
12 +These patches are under the BSD license.
14 +RELEASE: 5. Jan. 2003
17 +======================
28 + This is the first release of the EXTTODO patch.
30 +================================================================================
34 + The exttodo patch addresses a problem known as the silly qmail (queue)
35 + problem. This problem is found only on system with high injection rates.
37 + qmail with a big local and remote concurrency could deliver a tremendous
38 + amount of messages but normally this can not be achieved because qmail-send
39 + becomes a bottleneck on those high volumes servers.
40 + qmail-send preprocesses all new messages before distributing them for local
41 + or remote delivering. In one run qmail-send does one todo run but has the
42 + ability to close multiple jobs. Because of this layout qmail-send can not
43 + feed all the new available (local/remote) delivery slots and therefor it is
44 + not possible to achieve the maximum throughput.
45 + This would be a minor problem if one qmail-send run could be done in extreme
46 + short time but because of many file system calls (fsync and (un)link) a todo
47 + run is expensive and throttles the throughput.
49 + The exttodo patch tries to solve the problem by moving the todo routine into
50 + an external program. This reduces the run time in qmail-send.
52 + exttodo adds a new program to qmail called qmail-todo. qmail-todo prepares
53 + incoming messages for local and remote delivering (by creating info/<messid>
54 + local/<messid> and remote/<messid> and removing todo/<messid>). See also
55 + INTERNALS. As next qmail-todo transmits the <messid> to qmail-send which will
56 + add this message into the priority queue which schedules the message for
61 + To enable the exttodo patch you need to define EXTERNAL_TODO while compiling
62 + qmail(-ldap) this can be done with the -D flag of cc (e.g. cc -DEXTERNAL_TODO).
64 + NOTE: the exttodo patch can also be used on qmail systems without the
67 +================================================================================
71 + No additional control files are used or needed.
73 +================================================================================
77 + qmail-todo will be started by qmail-start and therefor no additional setup
80 + To verify that exttodo is running just check if qmail-todo is running.
82 +================================================================================
88 + +--0-1--+ +--0-1--+ +-----------+
89 + trigger ^ | ^ | +->0,1 lspawn |
90 + | | v | v / +-----------+
91 + +-------+ v +--2-3--+ +--5-6--+ /
92 + | | | | 0<--7 1,2<-+
93 + | queue |--+--| todo | | send |
94 + | | | | 1-->8 3,4<-+
95 + +-------+ +-------+ +---0---+ \
98 + +---0---+ +-----------+
102 +Communication between qmail-send and qmail-todo
106 + Start delivery for new message with id <messid>.
107 + the character L, R or B defines the type
108 + of delivery, local, remote or both respectively.
110 + Dump string to the logger without adding additional \n or similar.
112 + H Got a SIGHUP reread ~/control/locals and ~/control/virtualdomains
115 +qmail-todo sends "\0" terminated messages whereas qmail-send just send one
116 +character to qmail-todo.
119 diff -u --new-file qmail-1.03-orig/EXTTODO-INFO qmail-1.03/EXTTODO-INFO
120 --- qmail-1.03-orig/EXTTODO-INFO 1969-12-31 21:00:00.000000000 -0300
121 +++ qmail-1.03/EXTTODO-INFO 2006-03-21 17:01:26.000000000 -0300
134 diff -u --new-file qmail-1.03-orig/FILES qmail-1.03/FILES
135 --- qmail-1.03-orig/FILES 2006-03-21 16:58:51.000000000 -0300
136 +++ qmail-1.03/FILES 2006-03-21 17:01:51.000000000 -0300
142 diff -u --new-file qmail-1.03-orig/hier.c qmail-1.03/hier.c
143 --- qmail-1.03-orig/hier.c 2006-03-21 16:58:51.000000000 -0300
144 +++ qmail-1.03/hier.c 2006-03-21 17:05:28.000000000 -0300
146 c(auto_qmail,"bin","qmail-rspawn",auto_uido,auto_gidq,0711);
147 c(auto_qmail,"bin","qmail-clean",auto_uido,auto_gidq,0711);
148 c(auto_qmail,"bin","qmail-send",auto_uido,auto_gidq,0711);
149 +#ifdef EXTERNAL_TODO
150 + c(auto_qmail,"bin","qmail-todo",auto_uido,auto_gidq,0711);
152 c(auto_qmail,"bin","splogger",auto_uido,auto_gidq,0711);
153 c(auto_qmail,"bin","qmail-newu",auto_uido,auto_gidq,0700);
154 c(auto_qmail,"bin","qmail-newmrh",auto_uido,auto_gidq,0700);
155 diff -u --new-file qmail-1.03-orig/install-big.c qmail-1.03/install-big.c
156 --- qmail-1.03-orig/install-big.c 2006-03-21 16:58:51.000000000 -0300
157 +++ qmail-1.03/install-big.c 2006-03-21 17:05:53.000000000 -0300
159 c(auto_qmail,"bin","qmail-rspawn",auto_uido,auto_gidq,0711);
160 c(auto_qmail,"bin","qmail-clean",auto_uido,auto_gidq,0711);
161 c(auto_qmail,"bin","qmail-send",auto_uido,auto_gidq,0711);
162 +#ifdef EXTERNAL_TODO
163 + c(auto_qmail,"bin","qmail-todo",auto_uido,auto_gidq,0711);
165 c(auto_qmail,"bin","splogger",auto_uido,auto_gidq,0711);
166 c(auto_qmail,"bin","qmail-newu",auto_uido,auto_gidq,0700);
167 c(auto_qmail,"bin","qmail-newmrh",auto_uido,auto_gidq,0700);
168 diff -u --new-file qmail-1.03-orig/Makefile qmail-1.03/Makefile
169 --- qmail-1.03-orig/Makefile 2006-03-21 16:58:51.000000000 -0300
170 +++ qmail-1.03/Makefile 2006-03-21 17:04:19.000000000 -0300
172 # Don't edit Makefile! Use conf-* for configuration.
174 +DEFINES=-DEXTERNAL_TODO # use to enable external todo
182 compile hier.c auto_split.h auto_uids.h fmt.h fifo.h
184 + ./compile $(DEFINES) hier.c
189 forward preline condredirect bouncesaying except maildirmake make-owners \
190 maildir2mbox maildirwatch qail elq pinq idedit install-big install \
191 instcheck home home+df proc proc+df binm1 binm1+df binm2 binm2+df \
193 +binm3 binm3+df qmail-todo
196 make-load warn-auto.sh systype
197 @@ -1482,7 +1484,7 @@
198 scan.h case.h auto_qmail.h trigger.h newfield.h stralloc.h quote.h \
199 qmail.h substdio.h qsutil.h prioq.h datetime.h gen_alloc.h constmap.h \
200 fmtqfn.h readsubdir.h direntry.h
201 - ./compile qmail-send.c
202 + ./compile $(DEFINES) qmail-send.c
205 load qmail-showctl.o auto_uids.o control.o open.a getln.a stralloc.a \
206 @@ -1549,7 +1551,7 @@
209 compile qmail-start.c fd.h prot.h exit.h fork.h auto_uids.h
210 - ./compile qmail-start.c
211 + ./compile $(DEFINES) qmail-start.c
214 load qmail-tcpok.o open.a lock.a strerr.a substdio.a error.a str.a \
215 @@ -1581,6 +1583,21 @@
216 fmt.h ip.h lock.h error.h exit.h datetime.h now.h datetime.h
217 ./compile qmail-tcpto.c
221 +load qmail-todo.o control.o constmap.o trigger.o fmtqfn.o now.o \
222 +readsubdir.o case.a ndelay.a getln.a sig.a open.a stralloc.a alloc.a \
223 +substdio.a error.a str.a fs.a auto_qmail.o auto_split.o
224 + ./load qmail-todo control.o constmap.o trigger.o fmtqfn.o now.o \
225 + readsubdir.o case.a ndelay.a getln.a sig.a open.a stralloc.a \
226 + alloc.a substdio.a error.a str.a fs.a auto_qmail.o auto_split.o
229 +compile alloc.h auto_qmail.h byte.h constmap.h control.h direntry.h error.h \
230 +exit.h fmt.h fmtqfn.h getln.h open.h ndelay.h now.h readsubdir.h readwrite.h \
231 +scan.h select.h str.h stralloc.h substdio.h trigger.h
232 + ./compile $(DEFINES) qmail-todo.c
235 warn-auto.sh qmail-upq.sh conf-qmail conf-break conf-split
236 cat warn-auto.sh qmail-upq.sh \
237 Subdirectorios comunes: qmail-1.03-orig/qmail-rhinit y qmail-1.03/qmail-rhinit
238 diff -u --new-file qmail-1.03-orig/qmail-send.c qmail-1.03/qmail-send.c
239 --- qmail-1.03-orig/qmail-send.c 2006-03-21 16:58:51.000000000 -0300
240 +++ qmail-1.03/qmail-send.c 2006-03-21 17:12:01.000000000 -0300
241 @@ -1218,6 +1218,7 @@
243 /* this file is too long ---------------------------------------------- TODO */
245 +#ifndef EXTERNAL_TODO
246 datetime_sec nexttodorun;
247 int flagtododir = 0; /* if 0, have to readsubdir_init again */
248 readsubdir todosubdir;
249 @@ -1435,6 +1436,143 @@
250 if (fdchan[c] != -1) close(fdchan[c]);
255 +/* this file is too long ------------------------------------- EXTERNAL TODO */
257 +#ifdef EXTERNAL_TODO
258 +stralloc todoline = {0};
264 +void tododied() { log1("alert: oh no! lost qmail-todo connection! dying...\n");
265 + flagexitasap = 1; flagtodoalive = 0; }
272 + /* sync with external todo */
273 + if (write(todofdout, "S", 1) != 1) tododied();
278 +void todo_selprep(nfds,rfds,wakeup)
281 +datetime_sec *wakeup;
283 + if (flagexitasap) {
284 + if (flagtodoalive) {
285 + write(todofdout, "X", 1);
288 + if (flagtodoalive) {
289 + FD_SET(todofdin,rfds);
290 + if (*nfds <= todofdin)
291 + *nfds = todofdin + 1;
295 +void todo_del(char* s)
297 + int flagchan[CHANNELS];
298 + struct prioq_elt pe;
303 + for (c = 0;c < CHANNELS;++c) flagchan[c] = 0;
318 + log1("warning: qmail-send unable to understand qmail-todo\n");
322 + len = scan_ulong(s,&id);
323 + if (!len || s[len]) {
324 + log1("warning: qmail-send unable to understand qmail-todo\n");
328 + pe.id = id; pe.dt = now();
329 + for (c = 0;c < CHANNELS;++c)
331 + while (!prioq_insert(&pqchan[c],&pe)) nomem();
333 + for (c = 0;c < CHANNELS;++c) if (flagchan[c]) break;
335 + while (!prioq_insert(&pqdone,&pe)) nomem();
347 + if (!flagtodoalive) return;
348 + if (!FD_ISSET(todofdin,rfds)) return;
350 + r = read(todofdin,todobuf,sizeof(todobuf));
351 + if (r == -1) return;
359 + for (i = 0;i < r;++i) {
361 + while (!stralloc_append(&todoline,&ch)) nomem();
362 + if (todoline.len > REPORTMAX)
363 + todoline.len = REPORTMAX;
364 + /* qmail-todo is responsible for keeping it short */
365 + if (!ch && (todoline.len > 1)) {
366 + switch (todoline.s[0]) {
368 + if (flagexitasap) break;
369 + todo_del(todoline.s + 1);
372 + log1(todoline.s + 1);
381 + log1("warning: qmail-send unable to understand qmail-todo: report mangled\n");
391 /* this file is too long ---------------------------------------------- MAIN */
393 @@ -1501,6 +1639,9 @@
394 log1("alert: unable to reread controls: unable to switch to home directory\n");
397 +#ifdef EXTERNAL_TODO
398 + write(todofdout, "H", 1);
401 while (chdir("queue") == -1)
403 @@ -1571,7 +1712,11 @@
407 +#ifdef EXTERNAL_TODO
408 + while (!flagexitasap || !del_canexit() || flagtodoalive)
410 while (!flagexitasap || !del_canexit())
415 diff -u --new-file qmail-1.03-orig/qmail-start.c qmail-1.03/qmail-start.c
416 --- qmail-1.03-orig/qmail-start.c 2006-03-21 16:58:51.000000000 -0300
417 +++ qmail-1.03/qmail-start.c 2006-03-21 17:14:50.000000000 -0300
419 char *(qcargs[]) = { "qmail-clean", 0 };
420 char *(qlargs[]) = { "qmail-lspawn", "./Mailbox", 0 };
421 char *(qrargs[]) = { "qmail-rspawn", 0 };
422 +#ifdef EXTERNAL_TODO
423 +char *(qtargs[]) = { "qmail-todo", 0};
426 void die() { _exit(111); }
432 -void close23456() { close(2); close(3); close(4); close(5); close(6); }
434 +#ifdef EXTERNAL_TODO
442 + close(2); close(3); close(4); close(5); close(6);
443 +#ifdef EXTERNAL_TODO
444 + close(7); close(8);
448 close(pi1[0]); close(pi1[1]); close(pi2[0]); close(pi2[1]);
449 close(pi3[0]); close(pi3[1]); close(pi4[0]); close(pi4[1]);
450 close(pi5[0]); close(pi5[1]); close(pi6[0]); close(pi6[1]);
451 +#ifdef EXTERNAL_TODO
452 + close(pi7[0]); close(pi7[1]); close(pi8[0]); close(pi8[1]);
453 + close(pi9[0]); close(pi9[1]); close(pi10[0]); close(pi10[1]);
459 if (fd_copy(4,0) == -1) die();
460 if (fd_copy(5,0) == -1) die();
461 if (fd_copy(6,0) == -1) die();
463 +#ifdef EXTERNAL_TODO
464 + if (fd_copy(7,0) == -1) die();
465 + if (fd_copy(8,0) == -1) die();
471 if (pipe(pi4) == -1) die();
472 if (pipe(pi5) == -1) die();
473 if (pipe(pi6) == -1) die();
475 +#ifdef EXTERNAL_TODO
476 + if (pipe(pi7) == -1) die();
477 + if (pipe(pi8) == -1) die();
478 + if (pipe(pi9) == -1) die();
479 + if (pipe(pi10) == -1) die();
485 execvp(*qcargs,qcargs);
489 +#ifdef EXTERNAL_TODO
493 + if (prot_uid(auto_uids) == -1) die();
494 + if (fd_copy(0,pi7[0]) == -1) die();
495 + if (fd_copy(1,pi8[1]) == -1) die();
497 + if (fd_copy(2,pi9[1]) == -1) die();
498 + if (fd_copy(3,pi10[0]) == -1) die();
500 + execvp(*qtargs,qtargs);
507 + if (prot_uid(auto_uidq) == -1) die();
508 + if (fd_copy(0,pi9[0]) == -1) die();
509 + if (fd_copy(1,pi10[1]) == -1) die();
512 + execvp(*qcargs,qcargs);
517 if (prot_uid(auto_uids) == -1) die();
518 if (fd_copy(0,1) == -1) die();
520 if (fd_copy(4,pi4[0]) == -1) die();
521 if (fd_copy(5,pi5[1]) == -1) die();
522 if (fd_copy(6,pi6[0]) == -1) die();
523 +#ifdef EXTERNAL_TODO
524 + if (fd_copy(7,pi7[1]) == -1) die();
525 + if (fd_copy(8,pi8[0]) == -1) die();
528 execvp(*qsargs,qsargs);
530 diff -u --new-file qmail-1.03-orig/qmail-todo.c qmail-1.03/qmail-todo.c
531 --- qmail-1.03-orig/qmail-todo.c 1969-12-31 21:00:00.000000000 -0300
532 +++ qmail-1.03/qmail-todo.c 2006-03-21 17:15:17.000000000 -0300
534 +#include <sys/types.h>
535 +#include <sys/stat.h>
537 +#include "auto_qmail.h"
539 +#include "constmap.h"
540 +#include "control.h"
541 +#include "direntry.h"
550 +#include "readsubdir.h"
551 +#include "readwrite.h"
555 +#include "stralloc.h"
556 +#include "substdio.h"
557 +#include "trigger.h"
559 +/* critical timing feature #1: if not triggered, do not busy-loop */
560 +/* critical timing feature #2: if triggered, respond within fixed time */
561 +/* important timing feature: when triggered, respond instantly */
562 +#define SLEEP_TODO 1500 /* check todo/ every 25 minutes in any case */
563 +#define SLEEP_FUZZ 1 /* slop a bit on sleeps to avoid zeno effect */
564 +#define SLEEP_FOREVER 86400 /* absolute maximum time spent in select() */
565 +#define SLEEP_SYSFAIL 123
567 +stralloc percenthack = {0};
568 +struct constmap mappercenthack;
569 +stralloc locals = {0};
570 +struct constmap maplocals;
571 +stralloc vdoms = {0};
572 +struct constmap mapvdoms;
573 +stralloc envnoathost = {0};
575 +char strnum[FMT_ULONG];
577 +/* XXX not good, if qmail-send.c changes this has to be updated */
579 +char *chanaddr[CHANNELS] = { "local/", "remote/" };
581 +datetime_sec recent;
584 +void log3(char* x, char* y, char* z);
586 +int flagstopasap = 0;
589 + if (flagstopasap == 0)
590 + log1("status: qmail-todo stop processing asap\n");
594 +int flagreadasap = 0; void sighup(void) { flagreadasap = 1; }
595 +int flagsendalive = 1; void senddied(void) { flagsendalive = 0; }
597 +void nomem() { log1("alert: out of memory, sleeping...\n"); sleep(10); }
598 +void pausedir(dir) char *dir;
599 +{ log3("alert: unable to opendir ",dir,", sleeping...\n"); sleep(10); }
603 + log1("alert: qmail-todo: oh no! lost qmail-clean connection! dying...\n");
608 +/* this file is not so long ------------------------------------- FILENAMES */
612 +void fnmake_init(void)
614 + while (!stralloc_ready(&fn,FMTQFN)) nomem();
617 +void fnmake_info(unsigned long id) { fn.len = fmtqfn(fn.s,"info/",id,1); }
618 +void fnmake_todo(unsigned long id) { fn.len = fmtqfn(fn.s,"todo/",id,0); }
619 +void fnmake_mess(unsigned long id) { fn.len = fmtqfn(fn.s,"mess/",id,1); }
620 +void fnmake_chanaddr(unsigned long id, int c)
621 +{ fn.len = fmtqfn(fn.s,chanaddr[c],id,1); }
624 +/* this file is not so long ------------------------------------- REWRITING */
626 +stralloc rwline = {0};
628 +/* 1 if by land, 2 if by sea, 0 if out of memory. not allowed to barf. */
629 +/* may trash recip. must set up rwline, between a T and a \0. */
630 +int rewrite(char *recip)
635 + static stralloc addr = {0};
638 + if (!stralloc_copys(&rwline,"T")) return 0;
639 + if (!stralloc_copys(&addr,recip)) return 0;
641 + i = byte_rchr(addr.s,addr.len,'@');
642 + if (i == addr.len) {
643 + if (!stralloc_cats(&addr,"@")) return 0;
644 + if (!stralloc_cat(&addr,&envnoathost)) return 0;
647 + while (constmap(&mappercenthack,addr.s + i + 1,addr.len - i - 1)) {
648 + j = byte_rchr(addr.s,i,'%');
655 + at = byte_rchr(addr.s,addr.len,'@');
657 + if (constmap(&maplocals,addr.s + at + 1,addr.len - at - 1)) {
658 + if (!stralloc_cat(&rwline,&addr)) return 0;
659 + if (!stralloc_0(&rwline)) return 0;
663 + for (i = 0;i <= addr.len;++i)
664 + if (!i || (i == at + 1) || (i == addr.len) || ((i > at) && (addr.s[i] == '.')))
665 + if (x = constmap(&mapvdoms,addr.s + i,addr.len - i)) {
667 + if (!stralloc_cats(&rwline,x)) return 0;
668 + if (!stralloc_cats(&rwline,"-")) return 0;
669 + if (!stralloc_cat(&rwline,&addr)) return 0;
670 + if (!stralloc_0(&rwline)) return 0;
674 + if (!stralloc_cat(&rwline,&addr)) return 0;
675 + if (!stralloc_0(&rwline)) return 0;
679 +/* this file is not so long --------------------------------- COMMUNICATION */
681 +substdio sstoqc; char sstoqcbuf[1024];
682 +substdio ssfromqc; char ssfromqcbuf[1024];
683 +stralloc comm_buf = {0};
688 +void comm_init(void)
690 + substdio_fdbuf(&sstoqc,write,2,sstoqcbuf,sizeof(sstoqcbuf));
691 + substdio_fdbuf(&ssfromqc,read,3,ssfromqcbuf,sizeof(ssfromqcbuf));
693 + fdout = 1; /* stdout */
694 + fdin = 0; /* stdin */
695 + if (ndelay_on(fdout) == -1)
696 + /* this is so stupid: NDELAY semantics should be default on write */
697 + senddied(); /* drastic, but better than risking deadlock */
699 + while (!stralloc_ready(&comm_buf,1024)) nomem();
702 +int comm_canwrite(void)
704 + /* XXX: could allow a bigger buffer; say 10 recipients */
705 + /* XXX: returns true if there is something in the buffer */
706 + if (!flagsendalive) return 0;
707 + if (comm_buf.s && comm_buf.len) return 1;
715 + pos = comm_buf.len;
716 + if (!stralloc_cats(&comm_buf,"L")) goto fail;
717 + if (!stralloc_cats(&comm_buf,x)) goto fail;
718 + if (!stralloc_0(&comm_buf)) goto fail;
722 + /* either all or nothing */
723 + comm_buf.len = pos;
726 +void log3(char* x, char *y, char *z)
730 + pos = comm_buf.len;
731 + if (!stralloc_cats(&comm_buf,"L")) goto fail;
732 + if (!stralloc_cats(&comm_buf,x)) goto fail;
733 + if (!stralloc_cats(&comm_buf,y)) goto fail;
734 + if (!stralloc_cats(&comm_buf,z)) goto fail;
735 + if (!stralloc_0(&comm_buf)) goto fail;
739 + /* either all or nothing */
740 + comm_buf.len = pos;
743 +void comm_write(unsigned long id, int local, int remote)
748 + if(local && remote) s="B";
749 + else if(local) s="L";
750 + else if(remote) s="R";
753 + pos = comm_buf.len;
754 + strnum[fmt_ulong(strnum,id)] = 0;
755 + if (!stralloc_cats(&comm_buf,"D")) goto fail;
756 + if (!stralloc_cats(&comm_buf,s)) goto fail;
757 + if (!stralloc_cats(&comm_buf,strnum)) goto fail;
758 + if (!stralloc_0(&comm_buf)) goto fail;
762 + /* either all or nothing */
763 + comm_buf.len = pos;
766 +static int issafe(char ch)
768 + if (ch == '%') return 0; /* general principle: allman's code is crap */
769 + if (ch < 33) return 0;
770 + if (ch > 126) return 0;
774 +void comm_info(unsigned long id, unsigned long size, char* from, unsigned long pid, unsigned long uid)
779 + pos = comm_buf.len;
780 + if (!stralloc_cats(&comm_buf,"Linfo msg ")) goto fail;
781 + strnum[fmt_ulong(strnum,id)] = 0;
782 + if (!stralloc_cats(&comm_buf,strnum)) goto fail;
783 + if (!stralloc_cats(&comm_buf,": bytes ")) goto fail;
784 + strnum[fmt_ulong(strnum,size)] = 0;
785 + if (!stralloc_cats(&comm_buf,strnum)) goto fail;
786 + if (!stralloc_cats(&comm_buf," from <")) goto fail;
788 + if (!stralloc_cats(&comm_buf,from)) goto fail;
789 + for (;i < comm_buf.len;++i)
790 + if (comm_buf.s[i] == '\n')
791 + comm_buf.s[i] = '/';
793 + if (!issafe(comm_buf.s[i]))
794 + comm_buf.s[i] = '_';
795 + if (!stralloc_cats(&comm_buf,"> qp ")) goto fail;
796 + strnum[fmt_ulong(strnum,pid)] = 0;
797 + if (!stralloc_cats(&comm_buf,strnum)) goto fail;
798 + if (!stralloc_cats(&comm_buf," uid ")) goto fail;
799 + strnum[fmt_ulong(strnum,uid)] = 0;
800 + if (!stralloc_cats(&comm_buf,strnum)) goto fail;
801 + if (!stralloc_cats(&comm_buf,"\n")) goto fail;
802 + if (!stralloc_0(&comm_buf)) goto fail;
806 + /* either all or nothing */
807 + comm_buf.len = pos;
810 +void comm_exit(void)
814 + /* if it fails exit, we have already stoped */
815 + if (!stralloc_cats(&comm_buf,"X")) _exit(1);
816 + if (!stralloc_0(&comm_buf)) _exit(1);
819 +void comm_selprep(int *nfds, fd_set *wfds, fd_set *rfds)
821 + if (flagsendalive) {
822 + if (flagstopasap && comm_canwrite() == 0)
824 + if (comm_canwrite()) {
825 + FD_SET(fdout,wfds);
826 + if (*nfds <= fdout)
835 +void comm_do(fd_set *wfds, fd_set *rfds)
837 + /* first write then read */
839 + if (comm_canwrite())
840 + if (FD_ISSET(fdout,wfds)) {
843 + len = comm_buf.len;
844 + w = write(fdout,comm_buf.s + comm_pos,len - comm_pos);
846 + if ((w == -1) && (errno == error_pipe))
850 + if (comm_pos == len) {
857 + if (FD_ISSET(fdin,rfds)) {
858 + /* there are only two messages 'H' and 'X' */
861 + r = read(fdin, &c, 1);
863 + if ((r == -1) && (errno != error_intr))
874 + log1("warning: qmail-todo: qmail-send speaks an obscure dialect\n");
881 +/* this file is not so long ------------------------------------------ TODO */
883 +datetime_sec nexttodorun;
884 +DIR *tododir; /* if 0, have to opendir again */
885 +stralloc todoline = {0};
886 +char todobuf[SUBSTDIO_INSIZE];
887 +char todobufinfo[512];
888 +char todobufchan[CHANNELS][1024];
890 +void todo_init(void)
893 + nexttodorun = now();
897 +void todo_selprep(int *nfds, fd_set *rfds, datetime_sec *wakeup)
899 + if (flagstopasap) return;
900 + trigger_selprep(nfds,rfds);
901 + if (tododir) *wakeup = 0;
902 + if (*wakeup > nexttodorun) *wakeup = nexttodorun;
905 +void todo_do(fd_set *rfds)
908 + substdio ss; int fd;
909 + substdio ssinfo; int fdinfo;
910 + substdio sschan[CHANNELS];
911 + int fdchan[CHANNELS];
912 + int flagchan[CHANNELS];
924 + for (c = 0;c < CHANNELS;++c) fdchan[c] = -1;
926 + if (flagstopasap) return;
930 + if (!trigger_pulled(rfds))
931 + if (recent < nexttodorun)
934 + tododir = opendir("todo");
940 + nexttodorun = recent + SLEEP_TODO;
943 + d = readdir(tododir);
950 + if (str_equal(d->d_name,".")) return;
951 + if (str_equal(d->d_name,"..")) return;
952 + len = scan_ulong(d->d_name,&id);
953 + if (!len || d->d_name[len]) return;
957 + fd = open_read(fn.s);
958 + if (fd == -1) { log3("warning: qmail-todo: unable to open ",fn.s,"\n"); return; }
961 + /* just for the statistics */
962 + if (stat(fn.s,&st) == -1)
963 + { log3("warning: qmail-todo: unable to stat ",fn.s,"\n"); goto fail; }
965 + for (c = 0;c < CHANNELS;++c)
967 + fnmake_chanaddr(id,c);
968 + if (unlink(fn.s) == -1) if (errno != error_noent)
969 + { log3("warning: qmail-todo: unable to unlink ",fn.s,"\n"); goto fail; }
973 + if (unlink(fn.s) == -1) if (errno != error_noent)
974 + { log3("warning: qmail-todo: unable to unlink ",fn.s,"\n"); goto fail; }
976 + fdinfo = open_excl(fn.s);
978 + { log3("warning: qmail-todo: unable to create ",fn.s,"\n"); goto fail; }
980 + strnum[fmt_ulong(strnum,id)] = 0;
981 + log3("new msg ",strnum,"\n");
983 + for (c = 0;c < CHANNELS;++c) flagchan[c] = 0;
985 + substdio_fdbuf(&ss,read,fd,todobuf,sizeof(todobuf));
986 + substdio_fdbuf(&ssinfo,write,fdinfo,todobufinfo,sizeof(todobufinfo));
993 + if (getln(&ss,&todoline,&match,'\0') == -1)
995 + /* perhaps we're out of memory, perhaps an I/O error */
997 + log3("warning: qmail-todo: trouble reading ",fn.s,"\n"); goto fail;
1001 + switch(todoline.s[0])
1004 + scan_ulong(todoline.s + 1,&uid);
1007 + scan_ulong(todoline.s + 1,&pid);
1010 + if (substdio_putflush(&ssinfo,todoline.s,todoline.len) == -1)
1013 + log3("warning: qmail-todo: trouble writing to ",fn.s,"\n"); goto fail;
1015 + comm_info(id, (unsigned long) st.st_size, todoline.s + 1, pid, uid);
1018 + switch(rewrite(todoline.s + 1))
1020 + case 0: nomem(); goto fail;
1021 + case 2: c = 1; break;
1022 + default: c = 0; break;
1024 + if (fdchan[c] == -1)
1026 + fnmake_chanaddr(id,c);
1027 + fdchan[c] = open_excl(fn.s);
1028 + if (fdchan[c] == -1)
1029 + { log3("warning: qmail-todo: unable to create ",fn.s,"\n"); goto fail; }
1030 + substdio_fdbuf(&sschan[c]
1031 + ,write,fdchan[c],todobufchan[c],sizeof(todobufchan[c]));
1034 + if (substdio_bput(&sschan[c],rwline.s,rwline.len) == -1)
1036 + fnmake_chanaddr(id,c);
1037 + log3("warning: qmail-todo: trouble writing to ",fn.s,"\n"); goto fail;
1042 + log3("warning: qmail-todo: unknown record type in ",fn.s,"\n"); goto fail;
1046 + close(fd); fd = -1;
1049 + if (substdio_flush(&ssinfo) == -1)
1050 + { log3("warning: qmail-todo: trouble writing to ",fn.s,"\n"); goto fail; }
1051 + if (fsync(fdinfo) == -1)
1052 + { log3("warning: qmail-todo: trouble fsyncing ",fn.s,"\n"); goto fail; }
1053 + close(fdinfo); fdinfo = -1;
1055 + for (c = 0;c < CHANNELS;++c)
1056 + if (fdchan[c] != -1)
1058 + fnmake_chanaddr(id,c);
1059 + if (substdio_flush(&sschan[c]) == -1)
1060 + { log3("warning: qmail-todo: trouble writing to ",fn.s,"\n"); goto fail; }
1061 + if (fsync(fdchan[c]) == -1)
1062 + { log3("warning: qmail-todo: trouble fsyncing ",fn.s,"\n"); goto fail; }
1063 + close(fdchan[c]); fdchan[c] = -1;
1067 + if (substdio_putflush(&sstoqc,fn.s,fn.len) == -1) { cleandied(); return; }
1068 + if (substdio_get(&ssfromqc,&ch,1) != 1) { cleandied(); return; }
1071 + log3("warning: qmail-clean unable to clean up ",fn.s,"\n");
1075 + comm_write(id, flagchan[0], flagchan[1]);
1080 + if (fd != -1) close(fd);
1081 + if (fdinfo != -1) close(fdinfo);
1082 + for (c = 0;c < CHANNELS;++c)
1083 + if (fdchan[c] != -1) close(fdchan[c]);
1086 +/* this file is too long ---------------------------------------------- MAIN */
1088 +int getcontrols(void)
1090 + if (control_init() == -1) return 0;
1091 + if (control_rldef(&envnoathost,"control/envnoathost",1,"envnoathost") != 1) return 0;
1092 + if (control_readfile(&locals,"control/locals",1) != 1) return 0;
1093 + if (!constmap_init(&maplocals,locals.s,locals.len,0)) return 0;
1094 + switch(control_readfile(&percenthack,"control/percenthack",0))
1096 + case -1: return 0;
1097 + case 0: if (!constmap_init(&mappercenthack,"",0,0)) return 0; break;
1098 + case 1: if (!constmap_init(&mappercenthack,percenthack.s,percenthack.len,0)) return 0; break;
1100 + switch(control_readfile(&vdoms,"control/virtualdomains",0))
1102 + case -1: return 0;
1103 + case 0: if (!constmap_init(&mapvdoms,"",0,1)) return 0; break;
1104 + case 1: if (!constmap_init(&mapvdoms,vdoms.s,vdoms.len,1)) return 0; break;
1109 +stralloc newlocals = {0};
1110 +stralloc newvdoms = {0};
1112 +void regetcontrols(void)
1116 + if (control_readfile(&newlocals,"control/locals",1) != 1)
1117 + { log1("alert: qmail-todo: unable to reread control/locals\n"); return; }
1118 + r = control_readfile(&newvdoms,"control/virtualdomains",0);
1120 + { log1("alert: qmail-todo: unable to reread control/virtualdomains\n"); return; }
1122 + constmap_free(&maplocals);
1123 + constmap_free(&mapvdoms);
1125 + while (!stralloc_copy(&locals,&newlocals)) nomem();
1126 + while (!constmap_init(&maplocals,locals.s,locals.len,0)) nomem();
1130 + while (!stralloc_copy(&vdoms,&newvdoms)) nomem();
1131 + while (!constmap_init(&mapvdoms,vdoms.s,vdoms.len,1)) nomem();
1134 + while (!constmap_init(&mapvdoms,"",0,1)) nomem();
1139 + if (chdir(auto_qmail) == -1)
1141 + log1("alert: qmail-todo: unable to reread controls: unable to switch to home directory\n");
1145 + while (chdir("queue") == -1)
1147 + log1("alert: qmail-todo: unable to switch back to queue directory; HELP! sleeping...\n");
1154 + datetime_sec wakeup;
1158 + struct timeval tv;
1162 + if (chdir(auto_qmail) == -1)
1163 + { log1("alert: qmail-todo: cannot start: unable to switch to home directory\n"); _exit(111); }
1164 + if (!getcontrols())
1165 + { log1("alert: qmail-todo: cannot start: unable to read controls\n"); _exit(111); }
1166 + if (chdir("queue") == -1)
1167 + { log1("alert: qmail-todo: cannot start: unable to switch to queue directory\n"); _exit(111); }
1177 + r = read(fdin, &c, 1);
1178 + if ((r == -1) && (errno != error_intr))
1179 + _exit(100); /* read failed probably qmail-send died */
1180 + } while (r =! 1); /* we assume it is a 'S' */
1186 + if (flagreadasap) { flagreadasap = 0; reread(); }
1187 + if (!flagsendalive) {
1188 + /* qmail-send finaly exited, so do the same. */
1189 + if (flagstopasap) _exit(0);
1190 + /* qmail-send died. We can not log and we can not work therefor _exit(1). */
1194 + wakeup = recent + SLEEP_FOREVER;
1199 + todo_selprep(&nfds,&rfds,&wakeup);
1200 + comm_selprep(&nfds,&wfds,&rfds);
1202 + if (wakeup <= recent) tv.tv_sec = 0;
1203 + else tv.tv_sec = wakeup - recent + SLEEP_FUZZ;
1206 + if (select(nfds,&rfds,&wfds,(fd_set *) 0,&tv) == -1)
1207 + if (errno == error_intr)
1210 + log1("warning: qmail-todo: trouble in select\n");
1216 + comm_do(&wfds, &rfds);
1223 diff -u --new-file qmail-1.03-orig/TARGETS qmail-1.03/TARGETS
1224 --- qmail-1.03-orig/TARGETS 2006-03-21 16:58:51.000000000 -0300
1225 +++ qmail-1.03/TARGETS 2006-03-21 17:04:41.000000000 -0300