122708Sdist /* 2*34921Sbostic * Copyright (c) 1983 Eric P. Allman 333731Sbostic * Copyright (c) 1988 Regents of the University of California. 433731Sbostic * All rights reserved. 533731Sbostic * 633731Sbostic * Redistribution and use in source and binary forms are permitted 7*34921Sbostic * provided that the above copyright notice and this paragraph are 8*34921Sbostic * duplicated in all such forms and that any documentation, 9*34921Sbostic * advertising materials, and other materials related to such 10*34921Sbostic * distribution and use acknowledge that the software was developed 11*34921Sbostic * by the University of California, Berkeley. The name of the 12*34921Sbostic * University may not be used to endorse or promote products derived 13*34921Sbostic * from this software without specific prior written permission. 14*34921Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 15*34921Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 16*34921Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1733731Sbostic */ 1822708Sdist 1933731Sbostic # include "sendmail.h" 2022708Sdist 2133731Sbostic #ifndef lint 2233731Sbostic #ifdef QUEUE 23*34921Sbostic static char sccsid[] = "@(#)queue.c 5.24 (Berkeley) 06/30/88 (with queueing)"; 2433731Sbostic #else 25*34921Sbostic static char sccsid[] = "@(#)queue.c 5.24 (Berkeley) 06/30/88 (without queueing)"; 2633731Sbostic #endif 2733731Sbostic #endif /* not lint */ 2833731Sbostic 294632Seric # include <sys/stat.h> 3013707Ssam # include <sys/dir.h> 314634Seric # include <signal.h> 324632Seric # include <errno.h> 334632Seric 3433731Sbostic # ifdef QUEUE 354632Seric 364632Seric /* 379377Seric ** Work queue. 389377Seric */ 399377Seric 409377Seric struct work 419377Seric { 429377Seric char *w_name; /* name of control file */ 439377Seric long w_pri; /* priority of message, see below */ 4425013Seric time_t w_ctime; /* creation time of message */ 459377Seric struct work *w_next; /* next in queue */ 469377Seric }; 479377Seric 489377Seric typedef struct work WORK; 499377Seric 509377Seric WORK *WorkQ; /* queue of things to be done */ 519377Seric /* 524632Seric ** QUEUEUP -- queue a message up for future transmission. 534632Seric ** 544632Seric ** Parameters: 556980Seric ** e -- the envelope to queue up. 566999Seric ** queueall -- if TRUE, queue all addresses, rather than 576999Seric ** just those with the QQUEUEUP flag set. 589377Seric ** announce -- if TRUE, tell when you are queueing up. 594632Seric ** 604632Seric ** Returns: 614632Seric ** none. 624632Seric ** 634632Seric ** Side Effects: 649377Seric ** The current request are saved in a control file. 654632Seric */ 664632Seric 679377Seric queueup(e, queueall, announce) 686980Seric register ENVELOPE *e; 696999Seric bool queueall; 709377Seric bool announce; 714632Seric { 727812Seric char *tf; 737812Seric char *qf; 747763Seric char buf[MAXLINE]; 757812Seric register FILE *tfp; 764632Seric register HDR *h; 775007Seric register ADDRESS *q; 7810173Seric MAILER nullmailer; 794632Seric 805037Seric /* 8117477Seric ** Create control file. 825037Seric */ 834632Seric 8417477Seric tf = newstr(queuename(e, 't')); 8517477Seric tfp = fopen(tf, "w"); 867812Seric if (tfp == NULL) 874632Seric { 8817477Seric syserr("queueup: cannot create temp file %s", tf); 8917477Seric return; 904632Seric } 9117477Seric (void) chmod(tf, FileMode); 924632Seric 934632Seric # ifdef DEBUG 947677Seric if (tTd(40, 1)) 9517468Seric printf("queueing %s\n", e->e_id); 964632Seric # endif DEBUG 974632Seric 984632Seric /* 996980Seric ** If there is no data file yet, create one. 1006980Seric */ 1016980Seric 1026980Seric if (e->e_df == NULL) 1036980Seric { 1046980Seric register FILE *dfp; 1059389Seric extern putbody(); 1066980Seric 1077812Seric e->e_df = newstr(queuename(e, 'd')); 1086980Seric dfp = fopen(e->e_df, "w"); 1096980Seric if (dfp == NULL) 1106980Seric { 1116980Seric syserr("queueup: cannot create %s", e->e_df); 1127812Seric (void) fclose(tfp); 1136980Seric return; 1146980Seric } 1159048Seric (void) chmod(e->e_df, FileMode); 11610173Seric (*e->e_putbody)(dfp, ProgMailer, e); 1177009Seric (void) fclose(dfp); 1189389Seric e->e_putbody = putbody; 1196980Seric } 1206980Seric 1216980Seric /* 1224632Seric ** Output future work requests. 12325687Seric ** Priority and creation time should be first, since 12425687Seric ** they are required by orderq. 1254632Seric */ 1264632Seric 1279377Seric /* output message priority */ 1289377Seric fprintf(tfp, "P%ld\n", e->e_msgpriority); 1299377Seric 1309630Seric /* output creation time */ 1319630Seric fprintf(tfp, "T%ld\n", e->e_ctime); 1329630Seric 1334632Seric /* output name of data file */ 1347812Seric fprintf(tfp, "D%s\n", e->e_df); 1354632Seric 13610108Seric /* message from envelope, if it exists */ 13710108Seric if (e->e_message != NULL) 13810108Seric fprintf(tfp, "M%s\n", e->e_message); 13910108Seric 1404632Seric /* output name of sender */ 1417812Seric fprintf(tfp, "S%s\n", e->e_from.q_paddr); 1424632Seric 1434632Seric /* output list of recipient addresses */ 1446980Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1454632Seric { 1467763Seric if (queueall ? !bitset(QDONTSEND, q->q_flags) : 1477763Seric bitset(QQUEUEUP, q->q_flags)) 1488245Seric { 1497812Seric fprintf(tfp, "R%s\n", q->q_paddr); 1509377Seric if (announce) 1519377Seric { 1529377Seric e->e_to = q->q_paddr; 1539377Seric message(Arpa_Info, "queued"); 1549377Seric if (LogLevel > 4) 1559377Seric logdelivery("queued"); 1569377Seric e->e_to = NULL; 1579377Seric } 1589387Seric #ifdef DEBUG 1599387Seric if (tTd(40, 1)) 1609387Seric { 1619387Seric printf("queueing "); 1629387Seric printaddr(q, FALSE); 1639387Seric } 1649387Seric #endif DEBUG 1658245Seric } 1664632Seric } 1674632Seric 16825687Seric /* output list of error recipients */ 16925687Seric for (q = e->e_errorqueue; q != NULL; q = q->q_next) 17025687Seric { 17126504Seric if (!bitset(QDONTSEND, q->q_flags)) 17226504Seric fprintf(tfp, "E%s\n", q->q_paddr); 17325687Seric } 17425687Seric 1759377Seric /* 1769377Seric ** Output headers for this message. 1779377Seric ** Expand macros completely here. Queue run will deal with 1789377Seric ** everything as absolute headers. 1799377Seric ** All headers that must be relative to the recipient 1809377Seric ** can be cracked later. 18110173Seric ** We set up a "null mailer" -- i.e., a mailer that will have 18210173Seric ** no effect on the addresses as they are output. 1839377Seric */ 1849377Seric 18510686Seric bzero((char *) &nullmailer, sizeof nullmailer); 18610173Seric nullmailer.m_r_rwset = nullmailer.m_s_rwset = -1; 18710349Seric nullmailer.m_eol = "\n"; 18810173Seric 18916147Seric define('g', "\001f", e); 1906980Seric for (h = e->e_header; h != NULL; h = h->h_link) 1914632Seric { 19210686Seric extern bool bitzerop(); 19310686Seric 19412015Seric /* don't output null headers */ 1954632Seric if (h->h_value == NULL || h->h_value[0] == '\0') 1964632Seric continue; 19712015Seric 19812015Seric /* don't output resent headers on non-resent messages */ 19912015Seric if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags)) 20012015Seric continue; 20112015Seric 20212015Seric /* output this header */ 2037812Seric fprintf(tfp, "H"); 20412015Seric 20512015Seric /* if conditional, output the set of conditions */ 20610686Seric if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags)) 20710686Seric { 20810686Seric int j; 20910686Seric 21023098Seric (void) putc('?', tfp); 21110686Seric for (j = '\0'; j <= '\177'; j++) 21210686Seric if (bitnset(j, h->h_mflags)) 21323098Seric (void) putc(j, tfp); 21423098Seric (void) putc('?', tfp); 21510686Seric } 21612015Seric 21712015Seric /* output the header: expand macros, convert addresses */ 2187763Seric if (bitset(H_DEFAULT, h->h_flags)) 2197763Seric { 2207763Seric (void) expand(h->h_value, buf, &buf[sizeof buf], e); 2218236Seric fprintf(tfp, "%s: %s\n", h->h_field, buf); 2227763Seric } 2238245Seric else if (bitset(H_FROM|H_RCPT, h->h_flags)) 2249348Seric { 2259348Seric commaize(h, h->h_value, tfp, bitset(EF_OLDSTYLE, e->e_flags), 22610173Seric &nullmailer); 2279348Seric } 2287763Seric else 2298245Seric fprintf(tfp, "%s: %s\n", h->h_field, h->h_value); 2304632Seric } 2314632Seric 2324632Seric /* 2334632Seric ** Clean up. 2344632Seric */ 2354632Seric 23617477Seric (void) fclose(tfp); 23717468Seric qf = queuename(e, 'q'); 23817468Seric if (tf != NULL) 23917468Seric { 24017468Seric (void) unlink(qf); 24124968Seric if (rename(tf, qf) < 0) 24224968Seric syserr("cannot unlink(%s, %s), df=%s", tf, qf, e->e_df); 24324968Seric errno = 0; 24417468Seric } 2457391Seric 2467677Seric # ifdef LOG 2477677Seric /* save log info */ 2487878Seric if (LogLevel > 15) 2497878Seric syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df); 2507677Seric # endif LOG 2514632Seric } 2524632Seric /* 2534632Seric ** RUNQUEUE -- run the jobs in the queue. 2544632Seric ** 2554632Seric ** Gets the stuff out of the queue in some presumably logical 2564632Seric ** order and processes them. 2574632Seric ** 2584632Seric ** Parameters: 25924941Seric ** forkflag -- TRUE if the queue scanning should be done in 26024941Seric ** a child process. We double-fork so it is not our 26124941Seric ** child and we don't have to clean up after it. 2624632Seric ** 2634632Seric ** Returns: 2644632Seric ** none. 2654632Seric ** 2664632Seric ** Side Effects: 2674632Seric ** runs things in the mail queue. 2684632Seric */ 2694632Seric 2704639Seric runqueue(forkflag) 2714639Seric bool forkflag; 2724632Seric { 27324953Seric extern bool shouldqueue(); 27424953Seric 2757466Seric /* 27624953Seric ** If no work will ever be selected, don't even bother reading 27724953Seric ** the queue. 27824953Seric */ 27924953Seric 28024953Seric if (shouldqueue(-100000000L)) 28124953Seric { 28224953Seric if (Verbose) 28324953Seric printf("Skipping queue run -- load average too high\n"); 28424953Seric 28524953Seric if (forkflag) 28624953Seric return; 28724953Seric finis(); 28824953Seric } 28924953Seric 29024953Seric /* 2917466Seric ** See if we want to go off and do other useful work. 2927466Seric */ 2934639Seric 2944639Seric if (forkflag) 2954639Seric { 2967943Seric int pid; 2977943Seric 2987943Seric pid = dofork(); 2997943Seric if (pid != 0) 3004639Seric { 30125184Seric extern reapchild(); 30225184Seric 3037943Seric /* parent -- pick up intermediate zombie */ 30425184Seric #ifndef SIGCHLD 3059377Seric (void) waitfor(pid); 30625184Seric #else SIGCHLD 30725184Seric (void) signal(SIGCHLD, reapchild); 30825184Seric #endif SIGCHLD 3097690Seric if (QueueIntvl != 0) 3109348Seric (void) setevent(QueueIntvl, runqueue, TRUE); 3114639Seric return; 3124639Seric } 3137943Seric /* child -- double fork */ 31425184Seric #ifndef SIGCHLD 3157943Seric if (fork() != 0) 3167943Seric exit(EX_OK); 31725184Seric #else SIGCHLD 31825184Seric (void) signal(SIGCHLD, SIG_DFL); 31925184Seric #endif SIGCHLD 3204639Seric } 32124941Seric 32224941Seric setproctitle("running queue"); 32324941Seric 3247876Seric # ifdef LOG 3257876Seric if (LogLevel > 11) 3267943Seric syslog(LOG_DEBUG, "runqueue %s, pid=%d", QueueDir, getpid()); 3277876Seric # endif LOG 3284639Seric 3297466Seric /* 33010205Seric ** Release any resources used by the daemon code. 33110205Seric */ 33210205Seric 33310205Seric # ifdef DAEMON 33410205Seric clrdaemon(); 33510205Seric # endif DAEMON 33610205Seric 33710205Seric /* 33827175Seric ** Make sure the alias database is open. 33927175Seric */ 34027175Seric 34127175Seric initaliases(AliasFile, FALSE); 34227175Seric 34327175Seric /* 3447466Seric ** Start making passes through the queue. 3457466Seric ** First, read and sort the entire queue. 3467466Seric ** Then, process the work in that order. 3477466Seric ** But if you take too long, start over. 3487466Seric */ 3497466Seric 3507943Seric /* order the existing work requests */ 35124954Seric (void) orderq(FALSE); 3527690Seric 3537943Seric /* process them once at a time */ 3547943Seric while (WorkQ != NULL) 3554639Seric { 3567943Seric WORK *w = WorkQ; 3577881Seric 3587943Seric WorkQ = WorkQ->w_next; 3597943Seric dowork(w); 3607943Seric free(w->w_name); 3617943Seric free((char *) w); 3624639Seric } 36329866Seric 36429866Seric /* exit without the usual cleanup */ 36529866Seric exit(ExitStat); 3664634Seric } 3674634Seric /* 3684632Seric ** ORDERQ -- order the work queue. 3694632Seric ** 3704632Seric ** Parameters: 37124941Seric ** doall -- if set, include everything in the queue (even 37224941Seric ** the jobs that cannot be run because the load 37324941Seric ** average is too high). Otherwise, exclude those 37424941Seric ** jobs. 3754632Seric ** 3764632Seric ** Returns: 37710121Seric ** The number of request in the queue (not necessarily 37810121Seric ** the number of requests in WorkQ however). 3794632Seric ** 3804632Seric ** Side Effects: 3814632Seric ** Sets WorkQ to the queue of available work, in order. 3824632Seric */ 3834632Seric 38425687Seric # define NEED_P 001 38525687Seric # define NEED_T 002 3864632Seric 38724941Seric orderq(doall) 38824941Seric bool doall; 3894632Seric { 3906625Sglickman register struct direct *d; 3914632Seric register WORK *w; 3926625Sglickman DIR *f; 3934632Seric register int i; 39425687Seric WORK wlist[QUEUESIZE+1]; 39510070Seric int wn = -1; 3964632Seric extern workcmpf(); 3974632Seric 3984632Seric /* clear out old WorkQ */ 3994632Seric for (w = WorkQ; w != NULL; ) 4004632Seric { 4014632Seric register WORK *nw = w->w_next; 4024632Seric 4034632Seric WorkQ = nw; 4044632Seric free(w->w_name); 4054632Seric free((char *) w); 4064632Seric w = nw; 4074632Seric } 4084632Seric 4094632Seric /* open the queue directory */ 4108148Seric f = opendir("."); 4114632Seric if (f == NULL) 4124632Seric { 4138148Seric syserr("orderq: cannot open \"%s\" as \".\"", QueueDir); 41410070Seric return (0); 4154632Seric } 4164632Seric 4174632Seric /* 4184632Seric ** Read the work directory. 4194632Seric */ 4204632Seric 42110070Seric while ((d = readdir(f)) != NULL) 4224632Seric { 4239377Seric FILE *cf; 4244632Seric char lbuf[MAXNAME]; 4254632Seric 4264632Seric /* is this an interesting entry? */ 4277812Seric if (d->d_name[0] != 'q' || d->d_name[1] != 'f') 4284632Seric continue; 4294632Seric 43010070Seric /* yes -- open control file (if not too many files) */ 43125687Seric if (++wn >= QUEUESIZE) 43210070Seric continue; 4338148Seric cf = fopen(d->d_name, "r"); 4344632Seric if (cf == NULL) 4354632Seric { 4367055Seric /* this may be some random person sending hir msgs */ 4377055Seric /* syserr("orderq: cannot open %s", cbuf); */ 43810090Seric #ifdef DEBUG 43910090Seric if (tTd(41, 2)) 44010090Seric printf("orderq: cannot open %s (%d)\n", 44110090Seric d->d_name, errno); 44210090Seric #endif DEBUG 4437055Seric errno = 0; 44410090Seric wn--; 4454632Seric continue; 4464632Seric } 44725687Seric w = &wlist[wn]; 44825687Seric w->w_name = newstr(d->d_name); 4494632Seric 45025027Seric /* make sure jobs in creation don't clog queue */ 45125687Seric w->w_pri = 0x7fffffff; 45225687Seric w->w_ctime = 0; 45325027Seric 4544632Seric /* extract useful information */ 45525687Seric i = NEED_P | NEED_T; 45625687Seric while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL) 4574632Seric { 45824954Seric extern long atol(); 45924954Seric 46024941Seric switch (lbuf[0]) 4614632Seric { 46224941Seric case 'P': 46325687Seric w->w_pri = atol(&lbuf[1]); 46425687Seric i &= ~NEED_P; 4654632Seric break; 46625013Seric 46725013Seric case 'T': 46825687Seric w->w_ctime = atol(&lbuf[1]); 46925687Seric i &= ~NEED_T; 47025013Seric break; 4714632Seric } 4724632Seric } 4734632Seric (void) fclose(cf); 47424953Seric 47525687Seric if (!doall && shouldqueue(w->w_pri)) 47624953Seric { 47724953Seric /* don't even bother sorting this job in */ 47824953Seric wn--; 47924953Seric } 4804632Seric } 4816625Sglickman (void) closedir(f); 48210090Seric wn++; 4834632Seric 4844632Seric /* 4854632Seric ** Sort the work directory. 4864632Seric */ 4874632Seric 48825687Seric qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf); 4894632Seric 4904632Seric /* 4914632Seric ** Convert the work list into canonical form. 4929377Seric ** Should be turning it into a list of envelopes here perhaps. 4934632Seric */ 4944632Seric 49524981Seric WorkQ = NULL; 49625687Seric for (i = min(wn, QUEUESIZE); --i >= 0; ) 4974632Seric { 4984632Seric w = (WORK *) xalloc(sizeof *w); 4994632Seric w->w_name = wlist[i].w_name; 5004632Seric w->w_pri = wlist[i].w_pri; 50125013Seric w->w_ctime = wlist[i].w_ctime; 50224981Seric w->w_next = WorkQ; 50324981Seric WorkQ = w; 5044632Seric } 5054632Seric 5064632Seric # ifdef DEBUG 5077677Seric if (tTd(40, 1)) 5084632Seric { 5094632Seric for (w = WorkQ; w != NULL; w = w->w_next) 5105037Seric printf("%32s: pri=%ld\n", w->w_name, w->w_pri); 5114632Seric } 5124632Seric # endif DEBUG 51310070Seric 51410090Seric return (wn); 5154632Seric } 5164632Seric /* 5177677Seric ** WORKCMPF -- compare function for ordering work. 5184632Seric ** 5194632Seric ** Parameters: 5204632Seric ** a -- the first argument. 5214632Seric ** b -- the second argument. 5224632Seric ** 5234632Seric ** Returns: 52424981Seric ** -1 if a < b 52524981Seric ** 0 if a == b 52624981Seric ** +1 if a > b 5274632Seric ** 5284632Seric ** Side Effects: 5294632Seric ** none. 5304632Seric */ 5314632Seric 5324632Seric workcmpf(a, b) 5335037Seric register WORK *a; 5345037Seric register WORK *b; 5354632Seric { 53625013Seric long pa = a->w_pri + a->w_ctime; 53725013Seric long pb = b->w_pri + b->w_ctime; 53824941Seric 53924941Seric if (pa == pb) 5404632Seric return (0); 54124941Seric else if (pa > pb) 54224981Seric return (1); 54324981Seric else 54410121Seric return (-1); 5454632Seric } 5464632Seric /* 5474632Seric ** DOWORK -- do a work request. 5484632Seric ** 5494632Seric ** Parameters: 5504632Seric ** w -- the work request to be satisfied. 5514632Seric ** 5524632Seric ** Returns: 5534632Seric ** none. 5544632Seric ** 5554632Seric ** Side Effects: 5564632Seric ** The work request is satisfied if possible. 5574632Seric */ 5584632Seric 5594632Seric dowork(w) 5604632Seric register WORK *w; 5614632Seric { 5624632Seric register int i; 56324941Seric extern bool shouldqueue(); 5644632Seric 5654632Seric # ifdef DEBUG 5667677Seric if (tTd(40, 1)) 5675037Seric printf("dowork: %s pri %ld\n", w->w_name, w->w_pri); 5684632Seric # endif DEBUG 5694632Seric 5704632Seric /* 57124941Seric ** Ignore jobs that are too expensive for the moment. 5724632Seric */ 5734632Seric 57424941Seric if (shouldqueue(w->w_pri)) 5754632Seric { 57624941Seric if (Verbose) 57724967Seric printf("\nSkipping %s\n", w->w_name + 2); 5784632Seric return; 5794632Seric } 5804632Seric 58124941Seric /* 58224941Seric ** Fork for work. 58324941Seric */ 58424941Seric 58524941Seric if (ForkQueueRuns) 58624941Seric { 58724941Seric i = fork(); 58824941Seric if (i < 0) 58924941Seric { 59024941Seric syserr("dowork: cannot fork"); 59124941Seric return; 59224941Seric } 59324941Seric } 59424941Seric else 59524941Seric { 59624941Seric i = 0; 59724941Seric } 59824941Seric 5994632Seric if (i == 0) 6004632Seric { 6014632Seric /* 6024632Seric ** CHILD 6038148Seric ** Lock the control file to avoid duplicate deliveries. 6048148Seric ** Then run the file as though we had just read it. 6057350Seric ** We save an idea of the temporary name so we 6067350Seric ** can recover on interrupt. 6074632Seric */ 6084632Seric 6097763Seric /* set basic modes, etc. */ 6107356Seric (void) alarm(0); 61125612Seric clearenvelope(CurEnv, FALSE); 6124632Seric QueueRun = TRUE; 6139377Seric ErrorMode = EM_MAIL; 6148148Seric CurEnv->e_id = &w->w_name[2]; 6157876Seric # ifdef LOG 6167876Seric if (LogLevel > 11) 6177881Seric syslog(LOG_DEBUG, "%s: dowork, pid=%d", CurEnv->e_id, 6187881Seric getpid()); 6197876Seric # endif LOG 6207763Seric 6217763Seric /* don't use the headers from sendmail.cf... */ 6227763Seric CurEnv->e_header = NULL; 6237763Seric 62417468Seric /* lock the control file during processing */ 6257812Seric if (link(w->w_name, queuename(CurEnv, 'l')) < 0) 6266980Seric { 6277812Seric /* being processed by another queuer */ 6287881Seric # ifdef LOG 6297881Seric if (LogLevel > 4) 6307881Seric syslog(LOG_DEBUG, "%s: locked", CurEnv->e_id); 6317881Seric # endif LOG 63224941Seric if (ForkQueueRuns) 63324941Seric exit(EX_OK); 63424941Seric else 63524941Seric return; 6366980Seric } 6376980Seric 6386980Seric /* do basic system initialization */ 6394632Seric initsys(); 6406980Seric 6416980Seric /* read the queue control file */ 64217477Seric readqf(CurEnv, TRUE); 6439338Seric CurEnv->e_flags |= EF_INQUEUE; 6449377Seric eatheader(CurEnv); 6456980Seric 6466980Seric /* do the delivery */ 6479338Seric if (!bitset(EF_FATALERRS, CurEnv->e_flags)) 6489282Seric sendall(CurEnv, SM_DELIVER); 6496980Seric 6506980Seric /* finish up and exit */ 65124941Seric if (ForkQueueRuns) 65224941Seric finis(); 65324941Seric else 65424941Seric dropenvelope(CurEnv); 6554632Seric } 65624941Seric else 65724941Seric { 65824941Seric /* 65924941Seric ** Parent -- pick up results. 66024941Seric */ 6614632Seric 66224941Seric errno = 0; 66324941Seric (void) waitfor(i); 66424941Seric } 6654632Seric } 6664632Seric /* 6674632Seric ** READQF -- read queue file and set up environment. 6684632Seric ** 6694632Seric ** Parameters: 6709377Seric ** e -- the envelope of the job to run. 6719630Seric ** full -- if set, read in all information. Otherwise just 6729630Seric ** read in info needed for a queue print. 6734632Seric ** 6744632Seric ** Returns: 6754632Seric ** none. 6764632Seric ** 6774632Seric ** Side Effects: 6784632Seric ** cf is read and created as the current job, as though 6794632Seric ** we had been invoked by argument. 6804632Seric */ 6814632Seric 68217477Seric readqf(e, full) 6839377Seric register ENVELOPE *e; 6849630Seric bool full; 6854632Seric { 68617477Seric char *qf; 68717477Seric register FILE *qfp; 6887785Seric char buf[MAXFIELD]; 6899348Seric extern char *fgetfolded(); 69024954Seric extern long atol(); 6914632Seric 6924632Seric /* 69317468Seric ** Read and process the file. 6944632Seric */ 6954632Seric 69617477Seric qf = queuename(e, 'q'); 69717477Seric qfp = fopen(qf, "r"); 69817477Seric if (qfp == NULL) 69917477Seric { 70017477Seric syserr("readqf: no control file %s", qf); 70117477Seric return; 70217477Seric } 70317477Seric FileName = qf; 7049377Seric LineNumber = 0; 7059630Seric if (Verbose && full) 7069377Seric printf("\nRunning %s\n", e->e_id); 70717468Seric while (fgetfolded(buf, sizeof buf, qfp) != NULL) 7084632Seric { 70926504Seric # ifdef DEBUG 71026504Seric if (tTd(40, 4)) 71126504Seric printf("+++++ %s\n", buf); 71226504Seric # endif DEBUG 7134632Seric switch (buf[0]) 7144632Seric { 7154632Seric case 'R': /* specify recipient */ 7169618Seric sendtolist(&buf[1], (ADDRESS *) NULL, &e->e_sendqueue); 7174632Seric break; 7184632Seric 71925687Seric case 'E': /* specify error recipient */ 72025687Seric sendtolist(&buf[1], (ADDRESS *) NULL, &e->e_errorqueue); 72125687Seric break; 72225687Seric 7234632Seric case 'H': /* header */ 7249630Seric if (full) 7259630Seric (void) chompheader(&buf[1], FALSE); 7264632Seric break; 7274632Seric 72810108Seric case 'M': /* message */ 72910108Seric e->e_message = newstr(&buf[1]); 73010108Seric break; 73110108Seric 7324632Seric case 'S': /* sender */ 7334634Seric setsender(newstr(&buf[1])); 7344632Seric break; 7354632Seric 7364632Seric case 'D': /* data file name */ 7379630Seric if (!full) 7389630Seric break; 7399377Seric e->e_df = newstr(&buf[1]); 7409544Seric e->e_dfp = fopen(e->e_df, "r"); 7419544Seric if (e->e_dfp == NULL) 7429377Seric syserr("readqf: cannot open %s", e->e_df); 7434632Seric break; 7444632Seric 7457860Seric case 'T': /* init time */ 74624941Seric e->e_ctime = atol(&buf[1]); 7474632Seric break; 7484632Seric 7494634Seric case 'P': /* message priority */ 75025008Seric e->e_msgpriority = atol(&buf[1]) + WkTimeFact; 7514634Seric break; 7524634Seric 75324941Seric case '\0': /* blank line; ignore */ 75424941Seric break; 75524941Seric 7564632Seric default: 75724941Seric syserr("readqf(%s:%d): bad line \"%s\"", e->e_id, 75824941Seric LineNumber, buf); 7594632Seric break; 7604632Seric } 7614632Seric } 7629377Seric 76324954Seric (void) fclose(qfp); 7649377Seric FileName = NULL; 76524941Seric 76624941Seric /* 76724941Seric ** If we haven't read any lines, this queue file is empty. 76824941Seric ** Arrange to remove it without referencing any null pointers. 76924941Seric */ 77024941Seric 77124941Seric if (LineNumber == 0) 77224941Seric { 77324941Seric errno = 0; 77424941Seric e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE; 77524941Seric } 7764632Seric } 7774632Seric /* 7789630Seric ** PRINTQUEUE -- print out a representation of the mail queue 7799630Seric ** 7809630Seric ** Parameters: 7819630Seric ** none. 7829630Seric ** 7839630Seric ** Returns: 7849630Seric ** none. 7859630Seric ** 7869630Seric ** Side Effects: 7879630Seric ** Prints a listing of the mail queue on the standard output. 7889630Seric */ 7895182Seric 7909630Seric printqueue() 7919630Seric { 7929630Seric register WORK *w; 7939630Seric FILE *f; 79410070Seric int nrequests; 7959630Seric char buf[MAXLINE]; 7969630Seric 7979630Seric /* 7989630Seric ** Read and order the queue. 7999630Seric */ 8009630Seric 80124941Seric nrequests = orderq(TRUE); 8029630Seric 8039630Seric /* 8049630Seric ** Print the work list that we have read. 8059630Seric */ 8069630Seric 8079630Seric /* first see if there is anything */ 80810070Seric if (nrequests <= 0) 8099630Seric { 81010070Seric printf("Mail queue is empty\n"); 8119630Seric return; 8129630Seric } 8139630Seric 81410096Seric printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s"); 81525687Seric if (nrequests > QUEUESIZE) 81625687Seric printf(", only %d printed", QUEUESIZE); 81724979Seric if (Verbose) 81825032Seric printf(")\n--QID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n"); 81924979Seric else 82024979Seric printf(")\n--QID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n"); 8219630Seric for (w = WorkQ; w != NULL; w = w->w_next) 8229630Seric { 8239630Seric struct stat st; 82410070Seric auto time_t submittime = 0; 82510070Seric long dfsize = -1; 82610108Seric char lf[20]; 82710108Seric char message[MAXLINE]; 82824941Seric extern bool shouldqueue(); 8299630Seric 83017468Seric f = fopen(w->w_name, "r"); 83117468Seric if (f == NULL) 83217468Seric { 83317468Seric errno = 0; 83417468Seric continue; 83517468Seric } 8369630Seric printf("%7s", w->w_name + 2); 83723098Seric (void) strcpy(lf, w->w_name); 83810070Seric lf[0] = 'l'; 83910070Seric if (stat(lf, &st) >= 0) 84010070Seric printf("*"); 84124941Seric else if (shouldqueue(w->w_pri)) 84224941Seric printf("X"); 84310070Seric else 84410070Seric printf(" "); 84510070Seric errno = 0; 84617468Seric 84710108Seric message[0] = '\0'; 8489630Seric while (fgets(buf, sizeof buf, f) != NULL) 8499630Seric { 8509630Seric fixcrlf(buf, TRUE); 8519630Seric switch (buf[0]) 8529630Seric { 85310108Seric case 'M': /* error message */ 85423098Seric (void) strcpy(message, &buf[1]); 85510108Seric break; 85610108Seric 8579630Seric case 'S': /* sender name */ 85824979Seric if (Verbose) 85925027Seric printf("%8ld %10ld %.12s %.38s", dfsize, 86025027Seric w->w_pri, ctime(&submittime) + 4, 86124979Seric &buf[1]); 86224979Seric else 86324979Seric printf("%8ld %.16s %.45s", dfsize, 86424979Seric ctime(&submittime), &buf[1]); 86510108Seric if (message[0] != '\0') 86625027Seric printf("\n\t\t (%.60s)", message); 8679630Seric break; 8689630Seric 8699630Seric case 'R': /* recipient name */ 87024979Seric if (Verbose) 87125027Seric printf("\n\t\t\t\t\t %.38s", &buf[1]); 87224979Seric else 87324979Seric printf("\n\t\t\t\t %.45s", &buf[1]); 8749630Seric break; 8759630Seric 8769630Seric case 'T': /* creation time */ 87724941Seric submittime = atol(&buf[1]); 8789630Seric break; 87910070Seric 88010070Seric case 'D': /* data file name */ 88110070Seric if (stat(&buf[1], &st) >= 0) 88210070Seric dfsize = st.st_size; 88310070Seric break; 8849630Seric } 8859630Seric } 88610070Seric if (submittime == (time_t) 0) 88710070Seric printf(" (no control file)"); 8889630Seric printf("\n"); 88923098Seric (void) fclose(f); 8909630Seric } 8919630Seric } 8929630Seric 8935182Seric # endif QUEUE 89417468Seric /* 89517468Seric ** QUEUENAME -- build a file name in the queue directory for this envelope. 89617468Seric ** 89717468Seric ** Assigns an id code if one does not already exist. 89817468Seric ** This code is very careful to avoid trashing existing files 89917468Seric ** under any circumstances. 90017468Seric ** We first create an nf file that is only used when 90117468Seric ** assigning an id. This file is always empty, so that 90217468Seric ** we can never accidently truncate an lf file. 90317468Seric ** 90417468Seric ** Parameters: 90517468Seric ** e -- envelope to build it in/from. 90617468Seric ** type -- the file type, used as the first character 90717468Seric ** of the file name. 90817468Seric ** 90917468Seric ** Returns: 91017468Seric ** a pointer to the new file name (in a static buffer). 91117468Seric ** 91217468Seric ** Side Effects: 91317468Seric ** Will create the lf and qf files if no id code is 91417468Seric ** already assigned. This will cause the envelope 91517468Seric ** to be modified. 91617468Seric */ 91717468Seric 91817468Seric char * 91917468Seric queuename(e, type) 92017468Seric register ENVELOPE *e; 92117468Seric char type; 92217468Seric { 92317468Seric static char buf[MAXNAME]; 92417468Seric static int pid = -1; 92517468Seric char c1 = 'A'; 92617468Seric char c2 = 'A'; 92717468Seric 92817468Seric if (e->e_id == NULL) 92917468Seric { 93017468Seric char qf[20]; 93117468Seric char nf[20]; 93217468Seric char lf[20]; 93317468Seric 93417468Seric /* find a unique id */ 93517468Seric if (pid != getpid()) 93617468Seric { 93717468Seric /* new process -- start back at "AA" */ 93817468Seric pid = getpid(); 93917468Seric c1 = 'A'; 94017468Seric c2 = 'A' - 1; 94117468Seric } 94217468Seric (void) sprintf(qf, "qfAA%05d", pid); 94323098Seric (void) strcpy(lf, qf); 94417468Seric lf[0] = 'l'; 94523098Seric (void) strcpy(nf, qf); 94617468Seric nf[0] = 'n'; 94717468Seric 94817468Seric while (c1 < '~' || c2 < 'Z') 94917468Seric { 95017468Seric int i; 95117468Seric 95217468Seric if (c2 >= 'Z') 95317468Seric { 95417468Seric c1++; 95517468Seric c2 = 'A' - 1; 95617468Seric } 95717477Seric lf[2] = nf[2] = qf[2] = c1; 95817477Seric lf[3] = nf[3] = qf[3] = ++c2; 95917468Seric # ifdef DEBUG 96017468Seric if (tTd(7, 20)) 96117468Seric printf("queuename: trying \"%s\"\n", nf); 96217468Seric # endif DEBUG 96317468Seric 96417468Seric # ifdef QUEUE 96517468Seric if (access(lf, 0) >= 0 || access(qf, 0) >= 0) 96617468Seric continue; 96717468Seric errno = 0; 96817468Seric i = creat(nf, FileMode); 96917468Seric if (i < 0) 97017468Seric { 97117468Seric (void) unlink(nf); /* kernel bug */ 97217468Seric continue; 97317468Seric } 97417468Seric (void) close(i); 97517468Seric i = link(nf, lf); 97617468Seric (void) unlink(nf); 97717468Seric if (i < 0) 97817468Seric continue; 97917468Seric if (link(lf, qf) >= 0) 98017468Seric break; 98117468Seric (void) unlink(lf); 98217468Seric # else QUEUE 98317982Seric if (close(creat(qf, FileMode)) >= 0) 98417982Seric break; 98517468Seric # endif QUEUE 98617468Seric } 98717468Seric if (c1 >= '~' && c2 >= 'Z') 98817468Seric { 98917468Seric syserr("queuename: Cannot create \"%s\" in \"%s\"", 99017468Seric qf, QueueDir); 99117468Seric exit(EX_OSERR); 99217468Seric } 99317468Seric e->e_id = newstr(&qf[2]); 99417468Seric define('i', e->e_id, e); 99517468Seric # ifdef DEBUG 99617468Seric if (tTd(7, 1)) 99717468Seric printf("queuename: assigned id %s, env=%x\n", e->e_id, e); 99817468Seric # ifdef LOG 99917468Seric if (LogLevel > 16) 100017468Seric syslog(LOG_DEBUG, "%s: assigned id", e->e_id); 100117468Seric # endif LOG 100217468Seric # endif DEBUG 100317468Seric } 100417468Seric 100517468Seric if (type == '\0') 100617468Seric return (NULL); 100717468Seric (void) sprintf(buf, "%cf%s", type, e->e_id); 100817468Seric # ifdef DEBUG 100917468Seric if (tTd(7, 2)) 101017468Seric printf("queuename: %s\n", buf); 101117468Seric # endif DEBUG 101217468Seric return (buf); 101317468Seric } 101417468Seric /* 101517468Seric ** UNLOCKQUEUE -- unlock the queue entry for a specified envelope 101617468Seric ** 101717468Seric ** Parameters: 101817468Seric ** e -- the envelope to unlock. 101917468Seric ** 102017468Seric ** Returns: 102117468Seric ** none 102217468Seric ** 102317468Seric ** Side Effects: 102417468Seric ** unlocks the queue for `e'. 102517468Seric */ 102617468Seric 102717468Seric unlockqueue(e) 102817468Seric ENVELOPE *e; 102917468Seric { 103017468Seric /* remove the transcript */ 103117468Seric #ifdef DEBUG 103217468Seric # ifdef LOG 103317468Seric if (LogLevel > 19) 103417468Seric syslog(LOG_DEBUG, "%s: unlock", e->e_id); 103517468Seric # endif LOG 103617468Seric if (!tTd(51, 4)) 103717468Seric #endif DEBUG 103817468Seric xunlink(queuename(e, 'x')); 103917468Seric 104017468Seric # ifdef QUEUE 104117468Seric /* last but not least, remove the lock */ 104217468Seric xunlink(queuename(e, 'l')); 104317468Seric # endif QUEUE 104417468Seric } 1045