24 #include "auto_qmail.h"
33 #include "readsubdir.h"
35 /* critical timing feature #1: if not triggered, do not busy-loop */
36 /* critical timing feature #2: if triggered, respond within fixed time */
37 /* important timing feature: when triggered, respond instantly */
38 #define SLEEP_TODO 1500 /* check todo/ every 25 minutes in any case */
39 #define SLEEP_FUZZ 1 /* slop a bit on sleeps to avoid zeno effect */
40 #define SLEEP_FOREVER 86400 /* absolute maximum time spent in select() */
41 #define SLEEP_CLEANUP 76431 /* time between cleanups */
42 #define SLEEP_SYSFAIL 123
43 #define OSSIFIED 129600 /* 36 hours; _must_ exceed q-q's DEATH (24 hours) */
45 int lifetime = 604800;
47 stralloc percenthack = {0};
48 struct constmap mappercenthack;
49 stralloc locals = {0};
50 struct constmap maplocals;
52 struct constmap mapvdoms;
53 stralloc envnoathost = {0};
54 stralloc bouncefrom = {0};
55 stralloc bouncehost = {0};
56 stralloc doublebounceto = {0};
57 stralloc doublebouncehost = {0};
59 char strnum2[FMT_ULONG];
60 char strnum3[FMT_ULONG];
63 char *chanaddr[CHANNELS] = { "local/", "remote/" };
64 char *chanstatusmsg[CHANNELS] = { " local ", " remote " };
65 char *tochan[CHANNELS] = { " to local ", " to remote " };
66 int chanfdout[CHANNELS] = { 1, 3 };
67 int chanfdin[CHANNELS] = { 2, 4 };
68 int chanskip[CHANNELS] = { 10, 20 };
70 int flagexitasap = 0; void sigterm() { flagexitasap = 1; }
71 int flagrunasap = 0; void sigalrm() { flagrunasap = 1; }
72 int flagreadasap = 0; void sighup() { flagreadasap = 1; }
74 void cleandied() { log1("alert: oh no! lost qmail-clean connection! dying...\n");
77 int flagspawnalive[CHANNELS];
78 void spawndied(c) int c; { log1("alert: oh no! lost spawn connection! dying...\n");
79 flagspawnalive[c] = 0; flagexitasap = 1; }
81 #define REPORTMAX 10000
86 /* this file is too long ----------------------------------------- FILENAMES */
90 char fnmake_strnum[FMT_ULONG];
94 while (!stralloc_ready(&fn,FMTQFN)) nomem();
95 while (!stralloc_ready(&fn2,FMTQFN)) nomem();
98 void fnmake_info(id) unsigned long id; { fn.len = fmtqfn(fn.s,"info/",id,1); }
99 void fnmake_todo(id) unsigned long id; { fn.len = fmtqfn(fn.s,"todo/",id,0); }
100 void fnmake_mess(id) unsigned long id; { fn.len = fmtqfn(fn.s,"mess/",id,1); }
101 void fnmake_foop(id) unsigned long id; { fn.len = fmtqfn(fn.s,"foop/",id,0); }
102 void fnmake_split(id) unsigned long id; { fn.len = fmtqfn(fn.s,"",id,1); }
103 void fnmake2_bounce(id) unsigned long id;
104 { fn2.len = fmtqfn(fn2.s,"bounce/",id,0); }
105 void fnmake_chanaddr(id,c) unsigned long id; int c;
106 { fn.len = fmtqfn(fn.s,chanaddr[c],id,1); }
109 /* this file is too long ----------------------------------------- REWRITING */
111 stralloc rwline = {0};
113 /* 1 if by land, 2 if by sea, 0 if out of memory. not allowed to barf. */
114 /* may trash recip. must set up rwline, between a T and a \0. */
121 static stralloc addr = {0};
124 if (!stralloc_copys(&rwline,"T")) return 0;
125 if (!stralloc_copys(&addr,recip)) return 0;
127 i = byte_rchr(addr.s,addr.len,'@');
129 if (!stralloc_cats(&addr,"@")) return 0;
130 if (!stralloc_cat(&addr,&envnoathost)) return 0;
133 while (constmap(&mappercenthack,addr.s + i + 1,addr.len - i - 1)) {
134 j = byte_rchr(addr.s,i,'%');
141 at = byte_rchr(addr.s,addr.len,'@');
143 if (constmap(&maplocals,addr.s + at + 1,addr.len - at - 1)) {
144 if (!stralloc_cat(&rwline,&addr)) return 0;
145 if (!stralloc_0(&rwline)) return 0;
149 for (i = 0;i <= addr.len;++i)
150 if (!i || (i == at + 1) || (i == addr.len) || ((i > at) && (addr.s[i] == '.')))
151 if (x = constmap(&mapvdoms,addr.s + i,addr.len - i)) {
153 if (!stralloc_cats(&rwline,x)) return 0;
154 if (!stralloc_cats(&rwline,"-")) return 0;
155 if (!stralloc_cat(&rwline,&addr)) return 0;
156 if (!stralloc_0(&rwline)) return 0;
160 if (!stralloc_cat(&rwline,&addr)) return 0;
161 if (!stralloc_0(&rwline)) return 0;
165 void senderadd(sa,sender,recip)
176 if (str_equal(sender + i - 4,"-@[]"))
178 j = byte_rchr(sender,i - 4,'@');
179 k = str_rchr(recip,'@');
180 if (recip[k] && (j + 5 <= i))
182 /* owner-@host-@[] -> owner-recipbox=reciphost@host */
183 while (!stralloc_catb(sa,sender,j)) nomem();
184 while (!stralloc_catb(sa,recip,k)) nomem();
185 while (!stralloc_cats(sa,"=")) nomem();
186 while (!stralloc_cats(sa,recip + k + 1)) nomem();
187 while (!stralloc_cats(sa,"@")) nomem();
188 while (!stralloc_catb(sa,sender + j + 1,i - 5 - j)) nomem();
192 while (!stralloc_cats(sa,sender)) nomem();
196 /* this file is too long ---------------------------------------------- INFO */
198 int getinfo(sa,dt,id)
205 static stralloc line = {0};
211 fdinfo = open_read(fn.s);
212 if (fdinfo == -1) return 0;
213 if (fstat(fdinfo,&st) == -1) { close(fdinfo); return 0; }
214 substdio_fdbuf(&ss,read,fdinfo,buf,sizeof(buf));
215 if (getln(&ss,&line,&match,'\0') == -1) { close(fdinfo); return 0; }
217 if (!match) return 0;
218 if (line.s[0] != 'F') return 0;
221 while (!stralloc_copys(sa,line.s + 1)) nomem();
222 while (!stralloc_0(sa)) nomem();
227 /* this file is too long ------------------------------------- COMMUNICATION */
229 substdio sstoqc; char sstoqcbuf[1024];
230 substdio ssfromqc; char ssfromqcbuf[1024];
231 stralloc comm_buf[CHANNELS] = { {0}, {0} };
232 int comm_pos[CHANNELS];
237 substdio_fdbuf(&sstoqc,write,5,sstoqcbuf,sizeof(sstoqcbuf));
238 substdio_fdbuf(&ssfromqc,read,6,ssfromqcbuf,sizeof(ssfromqcbuf));
239 for (c = 0;c < CHANNELS;++c)
240 if (ndelay_on(chanfdout[c]) == -1)
241 /* this is so stupid: NDELAY semantics should be default on write */
242 spawndied(c); /* drastic, but better than risking deadlock */
248 /* XXX: could allow a bigger buffer; say 10 recipients */
249 if (comm_buf[c].s && comm_buf[c].len) return 0;
253 void comm_write(c,delnum,id,sender,recip)
261 if (comm_buf[c].s && comm_buf[c].len) return;
262 while (!stralloc_copys(&comm_buf[c],"")) nomem();
264 while (!stralloc_append(&comm_buf[c],&ch)) nomem();
266 while (!stralloc_cats(&comm_buf[c],fn.s)) nomem();
267 while (!stralloc_0(&comm_buf[c])) nomem();
268 senderadd(&comm_buf[c],sender,recip);
269 while (!stralloc_0(&comm_buf[c])) nomem();
270 while (!stralloc_cats(&comm_buf[c],recip)) nomem();
271 while (!stralloc_0(&comm_buf[c])) nomem();
275 void comm_selprep(nfds,wfds)
280 for (c = 0;c < CHANNELS;++c)
281 if (flagspawnalive[c])
282 if (comm_buf[c].s && comm_buf[c].len)
284 FD_SET(chanfdout[c],wfds);
285 if (*nfds <= chanfdout[c])
286 *nfds = chanfdout[c] + 1;
294 for (c = 0;c < CHANNELS;++c)
295 if (flagspawnalive[c])
296 if (comm_buf[c].s && comm_buf[c].len)
297 if (FD_ISSET(chanfdout[c],wfds))
301 len = comm_buf[c].len;
302 w = write(chanfdout[c],comm_buf[c].s + comm_pos[c],len - comm_pos[c]);
305 if ((w == -1) && (errno == error_pipe))
308 continue; /* kernel select() bug; can't avoid busy-looping */
313 if (comm_pos[c] == len)
320 /* this file is too long ------------------------------------------ CLEANUPS */
322 int flagcleanup; /* if 1, cleanupdir is initialized and ready */
323 readsubdir cleanupdir;
324 datetime_sec cleanuptime;
332 void cleanup_selprep(wakeup)
333 datetime_sec *wakeup;
335 if (flagcleanup) *wakeup = 0;
336 if (*wakeup > cleanuptime) *wakeup = cleanuptime;
347 if (recent < cleanuptime) return;
348 readsubdir_init(&cleanupdir,"mess",pausedir);
352 switch(readsubdir_next(&cleanupdir,&id))
358 cleanuptime = recent + SLEEP_CLEANUP;
364 if (stat(fn.s,&st) == -1) return; /* probably qmail-queue deleted it */
365 if (recent <= st.st_atime + OSSIFIED) return;
368 if (stat(fn.s,&st) == 0) return;
369 if (errno != error_noent) return;
371 if (stat(fn.s,&st) == 0) return;
372 if (errno != error_noent) return;
375 if (substdio_putflush(&sstoqc,fn.s,fn.len) == -1) { cleandied(); return; }
376 if (substdio_get(&ssfromqc,&ch,1) != 1) { cleandied(); return; }
378 log3("warning: qmail-clean unable to clean up ",fn.s,"\n");
382 /* this file is too long ----------------------------------- PRIORITY QUEUES */
384 prioq pqdone = {0}; /* -todo +info; HOPEFULLY -local -remote */
385 prioq pqchan[CHANNELS] = { {0}, {0} };
386 /* pqchan 0: -todo +info +local ?remote */
387 /* pqchan 1: -todo +info ?local +remote */
388 prioq pqfail = {0}; /* stat() failure; has to be pqadded again */
394 struct prioq_elt pechan[CHANNELS];
395 int flagchan[CHANNELS];
399 #define CHECKSTAT if (errno != error_noent) goto fail;
402 if (stat(fn.s,&st) == -1)
405 return; /* someone yanking our chain */
409 if (stat(fn.s,&st) != -1) return; /* look, ma, dad crashed writing info! */
412 for (c = 0;c < CHANNELS;++c)
414 fnmake_chanaddr(id,c);
415 if (stat(fn.s,&st) == -1) { flagchan[c] = 0; CHECKSTAT }
416 else { flagchan[c] = 1; pechan[c].id = id; pechan[c].dt = st.st_mtime; }
419 for (c = 0;c < CHANNELS;++c)
421 while (!prioq_insert(&pqchan[c],&pechan[c])) nomem();
423 for (c = 0;c < CHANNELS;++c) if (flagchan[c]) break;
426 pe.id = id; pe.dt = now();
427 while (!prioq_insert(&pqdone,&pe)) nomem();
433 log3("warning: unable to stat ",fn.s,"; will try again later\n");
434 pe.id = id; pe.dt = now() + SLEEP_SYSFAIL;
435 while (!prioq_insert(&pqfail,&pe)) nomem();
444 readsubdir_init(&rs,"info",pausedir);
446 while (x = readsubdir_next(&rs,&id))
455 time_t ut[2]; /* XXX: more portable than utimbuf, but still worrisome */
457 for (c = 0;c < CHANNELS;++c)
458 while (prioq_min(&pqchan[c],&pe))
460 prioq_delmin(&pqchan[c]);
461 fnmake_chanaddr(pe.id,c);
462 ut[0] = ut[1] = pe.dt;
463 if (utime(fn.s,ut) == -1)
464 log3("warning: unable to utime ",fn.s,"; message will be retried too soon\n");
472 for (c = 0;c < CHANNELS;++c)
475 for (i = 0;i < pqchan[c].len;++i)
476 pqchan[c].p[i].dt = recent;
480 /* this file is too long ---------------------------------------------- JOBS */
484 int refs; /* if 0, this struct is unused */
501 while (!(jo = (struct job *) alloc(numjobs * sizeof(struct job)))) nomem();
502 for (j = 0;j < numjobs;++j)
512 for (j = 0;j < numjobs;++j) if (!jo[j].refs) return 1;
516 int job_open(id,channel)
521 for (j = 0;j < numjobs;++j) if (!jo[j].refs) break;
522 if (j == numjobs) return -1;
525 jo[j].channel = channel;
527 jo[j].flaghiteof = 0;
537 if (0 < --jo[j].refs) return;
541 if (jo[j].flaghiteof && !jo[j].numtodo)
543 fnmake_chanaddr(jo[j].id,jo[j].channel);
544 if (unlink(fn.s) == -1)
546 log3("warning: unable to unlink ",fn.s,"; will try again later\n");
547 pe.dt = now() + SLEEP_SYSFAIL;
552 for (c = 0;c < CHANNELS;++c) if (c != jo[j].channel)
554 fnmake_chanaddr(jo[j].id,c);
555 if (stat(fn.s,&st) == 0) return; /* more channels going */
556 if (errno != error_noent)
558 log3("warning: unable to stat ",fn.s,"\n");
559 break; /* this is the only reason for HOPEFULLY */
563 while (!prioq_insert(&pqdone,&pe)) nomem();
568 while (!prioq_insert(&pqchan[jo[j].channel],&pe)) nomem();
572 /* this file is too long ------------------------------------------- BOUNCES */
574 char *stripvdomprepend(recip)
582 i = str_rchr(recip,'@');
583 if (!recip[i]) return recip;
584 domain = recip + i + 1;
585 domainlen = str_len(domain);
587 for (i = 0;i <= domainlen;++i)
588 if ((i == 0) || (i == domainlen) || (domain[i] == '.'))
589 if (prepend = constmap(&mapvdoms,domain + i,domainlen - i))
591 if (!*prepend) break;
592 i = str_len(prepend);
593 if (str_diffn(recip,prepend,i)) break;
594 if (recip[i] != '-') break;
595 return recip + i + 1;
600 stralloc bouncetext = {0};
602 void addbounce(id,recip,report)
610 while (!stralloc_copys(&bouncetext,"<")) nomem();
611 while (!stralloc_cats(&bouncetext,stripvdomprepend(recip))) nomem();
612 for (pos = 0;pos < bouncetext.len;++pos)
613 if (bouncetext.s[pos] == '\n')
614 bouncetext.s[pos] = '_';
615 while (!stralloc_cats(&bouncetext,">:\n")) nomem();
616 while (!stralloc_cats(&bouncetext,report)) nomem();
618 if (report[str_len(report) - 1] != '\n')
619 while (!stralloc_cats(&bouncetext,"\n")) nomem();
620 for (pos = bouncetext.len - 2;pos > 0;--pos)
621 if (bouncetext.s[pos] == '\n')
622 if (bouncetext.s[pos - 1] == '\n')
623 bouncetext.s[pos] = '/';
624 while (!stralloc_cats(&bouncetext,"\n")) nomem();
628 fd = open_append(fn2.s);
630 log1("alert: unable to append to bounce message; HELP! sleeping...\n");
634 while (pos < bouncetext.len)
636 w = write(fd,bouncetext.s + pos,bouncetext.len - pos);
639 log1("alert: unable to append to bounce message; HELP! sleeping...\n");
660 static stralloc sender = {0};
661 static stralloc quoted = {0};
665 if (!getinfo(&sender,&birth,id)) return 0; /* XXX: print warning */
667 /* owner-@host-@[] -> owner-@host */
669 if (str_equal(sender.s + sender.len - 5,"-@[]"))
672 sender.s[sender.len - 1] = 0;
677 if (stat(fn2.s,&st) == -1)
679 if (errno == error_noent)
681 log3("warning: unable to stat ",fn2.s,"\n");
684 if (str_equal(sender.s,"#@[]"))
685 log3("triple bounce: discarding ",fn2.s,"\n");
688 if (qmail_open(&qqt) == -1)
689 { log1("warning: unable to start qmail-queue, will try later\n"); return 0; }
692 if (*sender.s) { bouncesender = ""; bouncerecip = sender.s; }
693 else { bouncesender = "#@[]"; bouncerecip = doublebounceto.s; }
695 while (!newfield_datemake(now())) nomem();
696 qmail_put(&qqt,newfield_date.s,newfield_date.len);
697 qmail_puts(&qqt,"From: ");
698 while (!quote("ed,&bouncefrom)) nomem();
699 qmail_put(&qqt,quoted.s,quoted.len);
700 qmail_puts(&qqt,"@");
701 qmail_put(&qqt,bouncehost.s,bouncehost.len);
702 qmail_puts(&qqt,"\nTo: ");
703 while (!quote2("ed,bouncerecip)) nomem();
704 qmail_put(&qqt,quoted.s,quoted.len);
706 Subject: failure notice\n\
708 Hi. This is the qmail-send program at ");
709 qmail_put(&qqt,bouncehost.s,bouncehost.len);
710 qmail_puts(&qqt,*sender.s ? ".\n\
711 I'm afraid I wasn't able to deliver your message to the following addresses.\n\
712 This is a permanent error; I've given up. Sorry it didn't work out.\n\
715 I tried to deliver a bounce message to this address, but the bounce bounced!\n\
719 fd = open_read(fn2.s);
724 substdio_fdbuf(&ssread,read,fd,inbuf,sizeof(inbuf));
725 while ((r = substdio_get(&ssread,buf,sizeof(buf))) > 0)
726 qmail_put(&qqt,buf,r);
732 qmail_puts(&qqt,*sender.s ? "--- Below this line is a copy of the message.\n\n" : "--- Below this line is the original bounce.\n\n");
733 qmail_puts(&qqt,"Return-Path: <");
734 while (!quote2("ed,sender.s)) nomem();
735 qmail_put(&qqt,quoted.s,quoted.len);
736 qmail_puts(&qqt,">\n");
738 fd = open_read(fn.s);
743 substdio_fdbuf(&ssread,read,fd,inbuf,sizeof(inbuf));
744 while ((r = substdio_get(&ssread,buf,sizeof(buf))) > 0)
745 qmail_put(&qqt,buf,r);
751 qmail_from(&qqt,bouncesender);
752 qmail_to(&qqt,bouncerecip);
753 if (*qmail_close(&qqt))
754 { log1("warning: trouble injecting bounce message, will try later\n"); return 0; }
756 strnum2[fmt_ulong(strnum2,id)] = 0;
757 log2("bounce msg ",strnum2);
758 strnum2[fmt_ulong(strnum2,qp)] = 0;
759 log3(" qp ",strnum2,"\n");
761 if (unlink(fn2.s) == -1)
763 log3("warning: unable to unlink ",fn2.s,"\n");
770 /* this file is too long ---------------------------------------- DELIVERIES */
782 unsigned long masterdelid = 1;
783 unsigned int concurrency[CHANNELS] = { 10, 20 };
784 unsigned int concurrencyused[CHANNELS] = { 0, 0 };
785 struct del *d[CHANNELS];
786 stralloc dline[CHANNELS];
794 for (c = 0;c < CHANNELS;++c) {
795 strnum2[fmt_ulong(strnum2,(unsigned long) concurrencyused[c])] = 0;
796 strnum3[fmt_ulong(strnum3,(unsigned long) concurrency[c])] = 0;
797 log2(chanstatusmsg[c],strnum2);
800 if (flagexitasap) log1(" exitasap");
808 for (c = 0;c < CHANNELS;++c)
810 flagspawnalive[c] = 1;
811 while (!(d[c] = (struct del *) alloc(concurrency[c] * sizeof(struct del))))
813 for (i = 0;i < concurrency[c];++i)
814 { d[c][i].used = 0; d[c][i].recip.s = 0; }
816 while (!stralloc_copys(&dline[c],"")) nomem();
824 for (c = 0;c < CHANNELS;++c)
825 if (flagspawnalive[c]) /* if dead, nothing we can do about its jobs */
826 if (concurrencyused[c]) return 0;
833 return flagspawnalive[c] && comm_canwrite(c) && (concurrencyused[c] < concurrency[c]);
836 void del_start(j,mpos,recip)
845 if (!flagspawnalive[c]) return;
846 if (!comm_canwrite(c)) return;
848 for (i = 0;i < concurrency[c];++i) if (!d[c][i].used) break;
849 if (i == concurrency[c]) return;
851 if (!stralloc_copys(&d[c][i].recip,recip)) { nomem(); return; }
852 if (!stralloc_0(&d[c][i].recip)) { nomem(); return; }
853 d[c][i].j = j; ++jo[j].refs;
854 d[c][i].delid = masterdelid++;
856 d[c][i].used = 1; ++concurrencyused[c];
858 comm_write(c,i,jo[j].id,jo[j].sender.s,recip);
860 strnum2[fmt_ulong(strnum2,d[c][i].delid)] = 0;
861 strnum3[fmt_ulong(strnum3,jo[j].id)] = 0;
862 log2("starting delivery ",strnum2);
863 log3(": msg ",strnum3,tochan[c]);
869 void markdone(c,id,pos)
876 fnmake_chanaddr(id,c);
879 fd = open_write(fn.s);
881 if (fstat(fd,&st) == -1) { close(fd); break; }
882 if (seek_set(fd,pos) == -1) { close(fd); break; }
883 if (write(fd,"D",1) != 1) { close(fd); break; }
884 /* further errors -> double delivery without us knowing about it, oh well */
888 log3("warning: trouble marking ",fn.s,"; message will be delivered twice!\n");
898 r = read(chanfdin[c],delbuf,sizeof(delbuf));
900 if (r == 0) { spawndied(c); return; }
901 for (i = 0;i < r;++i)
904 while (!stralloc_append(&dline[c],&ch)) nomem();
905 if (dline[c].len > REPORTMAX)
906 dline[c].len = REPORTMAX;
907 /* qmail-lspawn and qmail-rspawn are responsible for keeping it short */
908 /* but from a security point of view, we don't trust rspawn */
909 if (!ch && (dline[c].len > 1))
911 delnum = (unsigned int) (unsigned char) dline[c].s[0];
912 if ((delnum < 0) || (delnum >= concurrency[c]) || !d[c][delnum].used)
913 log1("warning: internal error: delivery report out of range\n");
916 strnum3[fmt_ulong(strnum3,d[c][delnum].delid)] = 0;
917 if (dline[c].s[1] == 'Z')
918 if (jo[d[c][delnum].j].flagdying)
922 while (!stralloc_cats(&dline[c],"I'm not going to try again; this message has been in the queue too long.\n")) nomem();
923 while (!stralloc_0(&dline[c])) nomem();
925 switch(dline[c].s[1])
928 log3("delivery ",strnum3,": success: ");
929 logsafe(dline[c].s + 2);
931 markdone(c,jo[d[c][delnum].j].id,d[c][delnum].mpos);
932 --jo[d[c][delnum].j].numtodo;
935 log3("delivery ",strnum3,": deferral: ");
936 logsafe(dline[c].s + 2);
940 log3("delivery ",strnum3,": failure: ");
941 logsafe(dline[c].s + 2);
943 addbounce(jo[d[c][delnum].j].id,d[c][delnum].recip.s,dline[c].s + 2);
944 markdone(c,jo[d[c][delnum].j].id,d[c][delnum].mpos);
945 --jo[d[c][delnum].j].numtodo;
948 log3("delivery ",strnum3,": report mangled, will defer\n");
950 job_close(d[c][delnum].j);
951 d[c][delnum].used = 0; --concurrencyused[c];
959 void del_selprep(nfds,rfds)
964 for (c = 0;c < CHANNELS;++c)
965 if (flagspawnalive[c])
967 FD_SET(chanfdin[c],rfds);
968 if (*nfds <= chanfdin[c])
969 *nfds = chanfdin[c] + 1;
977 for (c = 0;c < CHANNELS;++c)
978 if (flagspawnalive[c])
979 if (FD_ISSET(chanfdin[c],rfds))
984 /* this file is too long -------------------------------------------- PASSES */
988 unsigned long id; /* if 0, need a new pass */
989 int j; /* defined if id; job number */
990 int fd; /* defined if id; reading from {local,remote} */
991 seek_pos mpos; /* defined if id; mark position */
1000 for (c = 0;c < CHANNELS;++c) pass[c].id = 0;
1003 void pass_selprep(wakeup)
1004 datetime_sec *wakeup;
1007 struct prioq_elt pe;
1008 if (flagexitasap) return;
1009 for (c = 0;c < CHANNELS;++c)
1012 { *wakeup = 0; return; }
1014 for (c = 0;c < CHANNELS;++c)
1016 if (prioq_min(&pqchan[c],&pe))
1017 if (*wakeup > pe.dt)
1019 if (prioq_min(&pqfail,&pe))
1020 if (*wakeup > pe.dt)
1022 if (prioq_min(&pqdone,&pe))
1023 if (*wakeup > pe.dt)
1027 static datetime_sec squareroot(x) /* result^2 <= x < (result + 1)^2 */
1028 datetime_sec x; /* assuming: >= 0 */
1036 for (j = 15;j >= 0;--j)
1038 y21 = (y << (j + 1)) + (1 << (j + j));
1039 if (y21 <= x - yy) { y += (1 << j); yy += y21; }
1044 datetime_sec nextretry(birth,c)
1050 if (birth > recent) n = 0;
1051 else n = squareroot(recent - birth); /* no need to add fuzz to recent */
1053 return birth + n * n;
1060 struct prioq_elt pe;
1061 static stralloc line = {0};
1064 if (flagexitasap) return;
1068 if (!job_avail()) return;
1069 if (!prioq_min(&pqchan[c],&pe)) return;
1070 if (pe.dt > recent) return;
1071 fnmake_chanaddr(pe.id,c);
1073 prioq_delmin(&pqchan[c]);
1075 pass[c].fd = open_read(fn.s);
1076 if (pass[c].fd == -1) goto trouble;
1077 if (!getinfo(&line,&birth,pe.id)) { close(pass[c].fd); goto trouble; }
1079 substdio_fdbuf(&pass[c].ss,read,pass[c].fd,pass[c].buf,sizeof(pass[c].buf));
1080 pass[c].j = job_open(pe.id,c);
1081 jo[pass[c].j].retry = nextretry(birth,c);
1082 jo[pass[c].j].flagdying = (recent > birth + lifetime);
1083 while (!stralloc_copy(&jo[pass[c].j].sender,&line)) nomem();
1086 if (!del_avail(c)) return;
1088 if (getln(&pass[c].ss,&line,&match,'\0') == -1)
1090 fnmake_chanaddr(pass[c].id,c);
1091 log3("warning: trouble reading ",fn.s,"; will try again later\n");
1093 job_close(pass[c].j);
1100 jo[pass[c].j].flaghiteof = 1;
1101 job_close(pass[c].j);
1108 ++jo[pass[c].j].numtodo;
1109 del_start(pass[c].j,pass[c].mpos,line.s + 1);
1114 fnmake_chanaddr(pass[c].id,c);
1115 log3("warning: unknown record type in ",fn.s,"!\n");
1117 job_close(pass[c].j);
1122 pass[c].mpos += line.len;
1126 log3("warning: trouble opening ",fn.s,"; will try again later\n");
1127 pe.dt = recent + SLEEP_SYSFAIL;
1128 while (!prioq_insert(&pqchan[c],&pe)) nomem();
1136 struct prioq_elt pe;
1139 for (c = 0;c < CHANNELS;++c)
1141 fnmake_chanaddr(id,c);
1142 if (stat(fn.s,&st) == 0) return; /* false alarm; consequence of HOPEFULLY */
1143 if (errno != error_noent)
1145 log3("warning: unable to stat ",fn.s,"; will try again later\n");
1151 if (stat(fn.s,&st) == 0) return;
1152 if (errno != error_noent)
1154 log3("warning: unable to stat ",fn.s,"; will try again later\n");
1159 if (stat(fn.s,&st) == -1)
1161 if (errno == error_noent) return;
1162 log3("warning: unable to stat ",fn.s,"; will try again later\n");
1166 /* -todo +info -local -remote ?bounce */
1167 if (!injectbounce(id))
1168 goto fail; /* injectbounce() produced error message */
1170 strnum3[fmt_ulong(strnum3,id)] = 0;
1171 log3("end msg ",strnum3,"\n");
1173 /* -todo +info -local -remote -bounce */
1175 if (unlink(fn.s) == -1)
1177 log3("warning: unable to unlink ",fn.s,"; will try again later\n");
1181 /* -todo -info -local -remote -bounce; we can relax */
1183 if (substdio_putflush(&sstoqc,fn.s,fn.len) == -1) { cleandied(); return; }
1184 if (substdio_get(&ssfromqc,&ch,1) != 1) { cleandied(); return; }
1186 log3("warning: qmail-clean unable to clean up ",fn.s,"\n");
1191 pe.id = id; pe.dt = now() + SLEEP_SYSFAIL;
1192 while (!prioq_insert(&pqdone,&pe)) nomem();
1198 struct prioq_elt pe;
1200 for (c = 0;c < CHANNELS;++c) pass_dochan(c);
1201 if (prioq_min(&pqfail,&pe))
1202 if (pe.dt <= recent)
1204 prioq_delmin(&pqfail);
1207 if (prioq_min(&pqdone,&pe))
1208 if (pe.dt <= recent)
1210 prioq_delmin(&pqdone);
1216 /* this file is too long ---------------------------------------------- TODO */
1218 datetime_sec nexttodorun;
1219 DIR *tododir; /* if 0, have to opendir again */
1220 stralloc todoline = {0};
1221 char todobuf[SUBSTDIO_INSIZE];
1222 char todobufinfo[512];
1223 char todobufchan[CHANNELS][1024];
1228 nexttodorun = now();
1232 void todo_selprep(nfds,rfds,wakeup)
1235 datetime_sec *wakeup;
1237 if (flagexitasap) return;
1238 trigger_selprep(nfds,rfds);
1239 if (tododir) *wakeup = 0;
1240 if (*wakeup > nexttodorun) *wakeup = nexttodorun;
1247 substdio ss; int fd;
1248 substdio ssinfo; int fdinfo;
1249 substdio sschan[CHANNELS];
1250 int fdchan[CHANNELS];
1251 int flagchan[CHANNELS];
1252 struct prioq_elt pe;
1264 for (c = 0;c < CHANNELS;++c) fdchan[c] = -1;
1266 if (flagexitasap) return;
1270 if (!trigger_pulled(rfds))
1271 if (recent < nexttodorun)
1274 tododir = opendir("todo");
1280 nexttodorun = recent + SLEEP_TODO;
1283 d = readdir(tododir);
1290 if (str_equal(d->d_name,".")) return;
1291 if (str_equal(d->d_name,"..")) return;
1292 len = scan_ulong(d->d_name,&id);
1293 if (!len || d->d_name[len]) return;
1297 fd = open_read(fn.s);
1298 if (fd == -1) { log3("warning: unable to open ",fn.s,"\n"); return; }
1301 /* just for the statistics */
1302 if (stat(fn.s,&st) == -1)
1303 { log3("warning: unable to stat ",fn.s,"\n"); goto fail; }
1305 for (c = 0;c < CHANNELS;++c)
1307 fnmake_chanaddr(id,c);
1308 if (unlink(fn.s) == -1) if (errno != error_noent)
1309 { log3("warning: unable to unlink ",fn.s,"\n"); goto fail; }
1313 if (unlink(fn.s) == -1) if (errno != error_noent)
1314 { log3("warning: unable to unlink ",fn.s,"\n"); goto fail; }
1316 fdinfo = open_excl(fn.s);
1318 { log3("warning: unable to create ",fn.s,"\n"); goto fail; }
1320 strnum3[fmt_ulong(strnum3,id)] = 0;
1321 log3("new msg ",strnum3,"\n");
1323 for (c = 0;c < CHANNELS;++c) flagchan[c] = 0;
1325 substdio_fdbuf(&ss,read,fd,todobuf,sizeof(todobuf));
1326 substdio_fdbuf(&ssinfo,write,fdinfo,todobufinfo,sizeof(todobufinfo));
1333 if (getln(&ss,&todoline,&match,'\0') == -1)
1335 /* perhaps we're out of memory, perhaps an I/O error */
1337 log3("warning: trouble reading ",fn.s,"\n"); goto fail;
1341 switch(todoline.s[0])
1344 scan_ulong(todoline.s + 1,&uid);
1347 scan_ulong(todoline.s + 1,&pid);
1350 if (substdio_putflush(&ssinfo,todoline.s,todoline.len) == -1)
1353 log3("warning: trouble writing to ",fn.s,"\n"); goto fail;
1355 log2("info msg ",strnum3);
1356 strnum2[fmt_ulong(strnum2,(unsigned long) st.st_size)] = 0;
1357 log2(": bytes ",strnum2);
1358 log1(" from <"); logsafe(todoline.s + 1);
1359 strnum2[fmt_ulong(strnum2,pid)] = 0;
1360 log2("> qp ",strnum2);
1361 strnum2[fmt_ulong(strnum2,uid)] = 0;
1362 log2(" uid ",strnum2);
1366 switch(rewrite(todoline.s + 1))
1368 case 0: nomem(); goto fail;
1369 case 2: c = 1; break;
1370 default: c = 0; break;
1372 if (fdchan[c] == -1)
1374 fnmake_chanaddr(id,c);
1375 fdchan[c] = open_excl(fn.s);
1376 if (fdchan[c] == -1)
1377 { log3("warning: unable to create ",fn.s,"\n"); goto fail; }
1378 substdio_fdbuf(&sschan[c]
1379 ,write,fdchan[c],todobufchan[c],sizeof(todobufchan[c]));
1382 if (substdio_bput(&sschan[c],rwline.s,rwline.len) == -1)
1384 fnmake_chanaddr(id,c);
1385 log3("warning: trouble writing to ",fn.s,"\n"); goto fail;
1390 log3("warning: unknown record type in ",fn.s,"\n"); goto fail;
1397 if (substdio_flush(&ssinfo) == -1)
1398 { log3("warning: trouble writing to ",fn.s,"\n"); goto fail; }
1399 if (fsync(fdinfo) == -1)
1400 { log3("warning: trouble fsyncing ",fn.s,"\n"); goto fail; }
1401 close(fdinfo); fdinfo = -1;
1403 for (c = 0;c < CHANNELS;++c)
1404 if (fdchan[c] != -1)
1406 fnmake_chanaddr(id,c);
1407 if (substdio_flush(&sschan[c]) == -1)
1408 { log3("warning: trouble writing to ",fn.s,"\n"); goto fail; }
1409 if (fsync(fdchan[c]) == -1)
1410 { log3("warning: trouble fsyncing ",fn.s,"\n"); goto fail; }
1411 close(fdchan[c]); fdchan[c] = -1;
1415 if (substdio_putflush(&sstoqc,fn.s,fn.len) == -1) { cleandied(); return; }
1416 if (substdio_get(&ssfromqc,&ch,1) != 1) { cleandied(); return; }
1419 log3("warning: qmail-clean unable to clean up ",fn.s,"\n");
1423 pe.id = id; pe.dt = now();
1424 for (c = 0;c < CHANNELS;++c)
1426 while (!prioq_insert(&pqchan[c],&pe)) nomem();
1428 for (c = 0;c < CHANNELS;++c) if (flagchan[c]) break;
1430 while (!prioq_insert(&pqdone,&pe)) nomem();
1435 if (fd != -1) close(fd);
1436 if (fdinfo != -1) close(fdinfo);
1437 for (c = 0;c < CHANNELS;++c)
1438 if (fdchan[c] != -1) close(fdchan[c]);
1442 /* this file is too long ---------------------------------------------- MAIN */
1444 int getcontrols() { if (control_init() == -1) return 0;
1445 if (control_readint(&lifetime,"control/queuelifetime") == -1) return 0;
1446 if (control_readint(&concurrency[0],"control/concurrencylocal") == -1) return 0;
1447 if (control_readint(&concurrency[1],"control/concurrencyremote") == -1) return 0;
1448 if (control_rldef(&envnoathost,"control/envnoathost",1,"envnoathost") != 1) return 0;
1449 if (control_rldef(&bouncefrom,"control/bouncefrom",0,"MAILER-DAEMON") != 1) return 0;
1450 if (control_rldef(&bouncehost,"control/bouncehost",1,"bouncehost") != 1) return 0;
1451 if (control_rldef(&doublebouncehost,"control/doublebouncehost",1,"doublebouncehost") != 1) return 0;
1452 if (control_rldef(&doublebounceto,"control/doublebounceto",0,"postmaster") != 1) return 0;
1453 if (!stralloc_cats(&doublebounceto,"@")) return 0;
1454 if (!stralloc_cat(&doublebounceto,&doublebouncehost)) return 0;
1455 if (!stralloc_0(&doublebounceto)) return 0;
1456 if (control_readfile(&locals,"control/locals",1) != 1) return 0;
1457 if (!constmap_init(&maplocals,locals.s,locals.len,0)) return 0;
1458 switch(control_readfile(&percenthack,"control/percenthack",0))
1461 case 0: if (!constmap_init(&mappercenthack,"",0,0)) return 0; break;
1462 case 1: if (!constmap_init(&mappercenthack,percenthack.s,percenthack.len,0)) return 0; break;
1464 switch(control_readfile(&vdoms,"control/virtualdomains",0))
1467 case 0: if (!constmap_init(&mapvdoms,"",0,1)) return 0; break;
1468 case 1: if (!constmap_init(&mapvdoms,vdoms.s,vdoms.len,1)) return 0; break;
1472 stralloc newlocals = {0};
1473 stralloc newvdoms = {0};
1475 void regetcontrols()
1479 if (control_readfile(&newlocals,"control/locals",1) != 1)
1480 { log1("alert: unable to reread control/locals\n"); return; }
1481 r = control_readfile(&newvdoms,"control/virtualdomains",0);
1483 { log1("alert: unable to reread control/virtualdomains\n"); return; }
1485 constmap_free(&maplocals);
1486 constmap_free(&mapvdoms);
1488 while (!stralloc_copy(&locals,&newlocals)) nomem();
1489 while (!constmap_init(&maplocals,locals.s,locals.len,0)) nomem();
1493 while (!stralloc_copy(&vdoms,&newvdoms)) nomem();
1494 while (!constmap_init(&mapvdoms,vdoms.s,vdoms.len,1)) nomem();
1497 while (!constmap_init(&mapvdoms,"",0,1)) nomem();
1502 if (chdir(auto_qmail) == -1)
1504 log1("alert: unable to reread controls: unable to switch to home directory\n");
1508 while (chdir("queue") == -1)
1510 log1("alert: unable to switch back to queue directory; HELP! sleeping...\n");
1518 datetime_sec wakeup;
1525 if (chdir(auto_qmail) == -1)
1526 { log1("alert: cannot start: unable to switch to home directory\n"); _exit(111); }
1528 { log1("alert: cannot start: unable to read controls\n"); _exit(111); }
1529 if (chdir("queue") == -1)
1530 { log1("alert: cannot start: unable to switch to queue directory\n"); _exit(111); }
1532 sig_termcatch(sigterm);
1533 sig_alarmcatch(sigalrm);
1534 sig_hangupcatch(sighup);
1538 fd = open_write("lock/sendmutex");
1540 { log1("alert: cannot start: unable to open mutex\n"); _exit(111); }
1541 if (lock_exnb(fd) == -1)
1542 { log1("alert: cannot start: qmail-send is already running\n"); _exit(111); }
1545 for (c = 0;c < CHANNELS;++c)
1551 r = read(chanfdin[c],&ch,1);
1552 while ((r == -1) && (errno == error_intr));
1554 { log1("alert: cannot start: hath the daemon spawn no fire?\n"); _exit(111); }
1555 u = (unsigned int) (unsigned char) ch;
1556 if (concurrency[c] > u) concurrency[c] = u;
1557 numjobs += concurrency[c];
1571 while (!flagexitasap || !del_canexit())
1575 if (flagrunasap) { flagrunasap = 0; pqrun(); }
1576 if (flagreadasap) { flagreadasap = 0; reread(); }
1578 wakeup = recent + SLEEP_FOREVER;
1583 comm_selprep(&nfds,&wfds);
1584 del_selprep(&nfds,&rfds);
1585 pass_selprep(&wakeup);
1586 todo_selprep(&nfds,&rfds,&wakeup);
1587 cleanup_selprep(&wakeup);
1589 if (wakeup <= recent) tv.tv_sec = 0;
1590 else tv.tv_sec = wakeup - recent + SLEEP_FUZZ;
1593 if (select(nfds,&rfds,&wfds,(fd_set *) 0,&tv) == -1)
1594 if (errno == error_intr)
1597 log1("warning: trouble in select\n");
1610 log1("status: exiting\n");