122708Sdist /* 2*33731Sbostic * Copyright (c) 1988 Regents of the University of California. 3*33731Sbostic * All rights reserved. 4*33731Sbostic * 5*33731Sbostic * Redistribution and use in source and binary forms are permitted 6*33731Sbostic * provided that this notice is preserved and that due credit is given 7*33731Sbostic * to the University of California at Berkeley. The name of the University 8*33731Sbostic * may not be used to endorse or promote products derived from this 9*33731Sbostic * software without specific prior written permission. This software 10*33731Sbostic * is provided ``as is'' without express or implied warranty. 11*33731Sbostic * 12*33731Sbostic * Sendmail 13*33731Sbostic * Copyright (c) 1983 Eric P. Allman 14*33731Sbostic * Berkeley, California 15*33731Sbostic */ 1622708Sdist 17*33731Sbostic # include "sendmail.h" 1822708Sdist 19*33731Sbostic #ifndef lint 20*33731Sbostic #ifdef QUEUE 21*33731Sbostic static char sccsid[] = "@(#)queue.c 5.23 (Berkeley) 03/13/88 (with queueing)"; 22*33731Sbostic #else 23*33731Sbostic static char sccsid[] = "@(#)queue.c 5.23 (Berkeley) 03/13/88 (without queueing)"; 24*33731Sbostic #endif 25*33731Sbostic #endif /* not lint */ 26*33731Sbostic 274632Seric # include <sys/stat.h> 2813707Ssam # include <sys/dir.h> 294634Seric # include <signal.h> 304632Seric # include <errno.h> 314632Seric 32*33731Sbostic # ifdef QUEUE 334632Seric 344632Seric /* 359377Seric ** Work queue. 369377Seric */ 379377Seric 389377Seric struct work 399377Seric { 409377Seric char *w_name; /* name of control file */ 419377Seric long w_pri; /* priority of message, see below */ 4225013Seric time_t w_ctime; /* creation time of message */ 439377Seric struct work *w_next; /* next in queue */ 449377Seric }; 459377Seric 469377Seric typedef struct work WORK; 479377Seric 489377Seric WORK *WorkQ; /* queue of things to be done */ 499377Seric /* 504632Seric ** QUEUEUP -- queue a message up for future transmission. 514632Seric ** 524632Seric ** Parameters: 536980Seric ** e -- the envelope to queue up. 546999Seric ** queueall -- if TRUE, queue all addresses, rather than 556999Seric ** just those with the QQUEUEUP flag set. 569377Seric ** announce -- if TRUE, tell when you are queueing up. 574632Seric ** 584632Seric ** Returns: 594632Seric ** none. 604632Seric ** 614632Seric ** Side Effects: 629377Seric ** The current request are saved in a control file. 634632Seric */ 644632Seric 659377Seric queueup(e, queueall, announce) 666980Seric register ENVELOPE *e; 676999Seric bool queueall; 689377Seric bool announce; 694632Seric { 707812Seric char *tf; 717812Seric char *qf; 727763Seric char buf[MAXLINE]; 737812Seric register FILE *tfp; 744632Seric register HDR *h; 755007Seric register ADDRESS *q; 7610173Seric MAILER nullmailer; 774632Seric 785037Seric /* 7917477Seric ** Create control file. 805037Seric */ 814632Seric 8217477Seric tf = newstr(queuename(e, 't')); 8317477Seric tfp = fopen(tf, "w"); 847812Seric if (tfp == NULL) 854632Seric { 8617477Seric syserr("queueup: cannot create temp file %s", tf); 8717477Seric return; 884632Seric } 8917477Seric (void) chmod(tf, FileMode); 904632Seric 914632Seric # ifdef DEBUG 927677Seric if (tTd(40, 1)) 9317468Seric printf("queueing %s\n", e->e_id); 944632Seric # endif DEBUG 954632Seric 964632Seric /* 976980Seric ** If there is no data file yet, create one. 986980Seric */ 996980Seric 1006980Seric if (e->e_df == NULL) 1016980Seric { 1026980Seric register FILE *dfp; 1039389Seric extern putbody(); 1046980Seric 1057812Seric e->e_df = newstr(queuename(e, 'd')); 1066980Seric dfp = fopen(e->e_df, "w"); 1076980Seric if (dfp == NULL) 1086980Seric { 1096980Seric syserr("queueup: cannot create %s", e->e_df); 1107812Seric (void) fclose(tfp); 1116980Seric return; 1126980Seric } 1139048Seric (void) chmod(e->e_df, FileMode); 11410173Seric (*e->e_putbody)(dfp, ProgMailer, e); 1157009Seric (void) fclose(dfp); 1169389Seric e->e_putbody = putbody; 1176980Seric } 1186980Seric 1196980Seric /* 1204632Seric ** Output future work requests. 12125687Seric ** Priority and creation time should be first, since 12225687Seric ** they are required by orderq. 1234632Seric */ 1244632Seric 1259377Seric /* output message priority */ 1269377Seric fprintf(tfp, "P%ld\n", e->e_msgpriority); 1279377Seric 1289630Seric /* output creation time */ 1299630Seric fprintf(tfp, "T%ld\n", e->e_ctime); 1309630Seric 1314632Seric /* output name of data file */ 1327812Seric fprintf(tfp, "D%s\n", e->e_df); 1334632Seric 13410108Seric /* message from envelope, if it exists */ 13510108Seric if (e->e_message != NULL) 13610108Seric fprintf(tfp, "M%s\n", e->e_message); 13710108Seric 1384632Seric /* output name of sender */ 1397812Seric fprintf(tfp, "S%s\n", e->e_from.q_paddr); 1404632Seric 1414632Seric /* output list of recipient addresses */ 1426980Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1434632Seric { 1447763Seric if (queueall ? !bitset(QDONTSEND, q->q_flags) : 1457763Seric bitset(QQUEUEUP, q->q_flags)) 1468245Seric { 1477812Seric fprintf(tfp, "R%s\n", q->q_paddr); 1489377Seric if (announce) 1499377Seric { 1509377Seric e->e_to = q->q_paddr; 1519377Seric message(Arpa_Info, "queued"); 1529377Seric if (LogLevel > 4) 1539377Seric logdelivery("queued"); 1549377Seric e->e_to = NULL; 1559377Seric } 1569387Seric #ifdef DEBUG 1579387Seric if (tTd(40, 1)) 1589387Seric { 1599387Seric printf("queueing "); 1609387Seric printaddr(q, FALSE); 1619387Seric } 1629387Seric #endif DEBUG 1638245Seric } 1644632Seric } 1654632Seric 16625687Seric /* output list of error recipients */ 16725687Seric for (q = e->e_errorqueue; q != NULL; q = q->q_next) 16825687Seric { 16926504Seric if (!bitset(QDONTSEND, q->q_flags)) 17026504Seric fprintf(tfp, "E%s\n", q->q_paddr); 17125687Seric } 17225687Seric 1739377Seric /* 1749377Seric ** Output headers for this message. 1759377Seric ** Expand macros completely here. Queue run will deal with 1769377Seric ** everything as absolute headers. 1779377Seric ** All headers that must be relative to the recipient 1789377Seric ** can be cracked later. 17910173Seric ** We set up a "null mailer" -- i.e., a mailer that will have 18010173Seric ** no effect on the addresses as they are output. 1819377Seric */ 1829377Seric 18310686Seric bzero((char *) &nullmailer, sizeof nullmailer); 18410173Seric nullmailer.m_r_rwset = nullmailer.m_s_rwset = -1; 18510349Seric nullmailer.m_eol = "\n"; 18610173Seric 18716147Seric define('g', "\001f", e); 1886980Seric for (h = e->e_header; h != NULL; h = h->h_link) 1894632Seric { 19010686Seric extern bool bitzerop(); 19110686Seric 19212015Seric /* don't output null headers */ 1934632Seric if (h->h_value == NULL || h->h_value[0] == '\0') 1944632Seric continue; 19512015Seric 19612015Seric /* don't output resent headers on non-resent messages */ 19712015Seric if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags)) 19812015Seric continue; 19912015Seric 20012015Seric /* output this header */ 2017812Seric fprintf(tfp, "H"); 20212015Seric 20312015Seric /* if conditional, output the set of conditions */ 20410686Seric if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags)) 20510686Seric { 20610686Seric int j; 20710686Seric 20823098Seric (void) putc('?', tfp); 20910686Seric for (j = '\0'; j <= '\177'; j++) 21010686Seric if (bitnset(j, h->h_mflags)) 21123098Seric (void) putc(j, tfp); 21223098Seric (void) putc('?', tfp); 21310686Seric } 21412015Seric 21512015Seric /* output the header: expand macros, convert addresses */ 2167763Seric if (bitset(H_DEFAULT, h->h_flags)) 2177763Seric { 2187763Seric (void) expand(h->h_value, buf, &buf[sizeof buf], e); 2198236Seric fprintf(tfp, "%s: %s\n", h->h_field, buf); 2207763Seric } 2218245Seric else if (bitset(H_FROM|H_RCPT, h->h_flags)) 2229348Seric { 2239348Seric commaize(h, h->h_value, tfp, bitset(EF_OLDSTYLE, e->e_flags), 22410173Seric &nullmailer); 2259348Seric } 2267763Seric else 2278245Seric fprintf(tfp, "%s: %s\n", h->h_field, h->h_value); 2284632Seric } 2294632Seric 2304632Seric /* 2314632Seric ** Clean up. 2324632Seric */ 2334632Seric 23417477Seric (void) fclose(tfp); 23517468Seric qf = queuename(e, 'q'); 23617468Seric if (tf != NULL) 23717468Seric { 23817468Seric (void) unlink(qf); 23924968Seric if (rename(tf, qf) < 0) 24024968Seric syserr("cannot unlink(%s, %s), df=%s", tf, qf, e->e_df); 24124968Seric errno = 0; 24217468Seric } 2437391Seric 2447677Seric # ifdef LOG 2457677Seric /* save log info */ 2467878Seric if (LogLevel > 15) 2477878Seric syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df); 2487677Seric # endif LOG 2494632Seric } 2504632Seric /* 2514632Seric ** RUNQUEUE -- run the jobs in the queue. 2524632Seric ** 2534632Seric ** Gets the stuff out of the queue in some presumably logical 2544632Seric ** order and processes them. 2554632Seric ** 2564632Seric ** Parameters: 25724941Seric ** forkflag -- TRUE if the queue scanning should be done in 25824941Seric ** a child process. We double-fork so it is not our 25924941Seric ** child and we don't have to clean up after it. 2604632Seric ** 2614632Seric ** Returns: 2624632Seric ** none. 2634632Seric ** 2644632Seric ** Side Effects: 2654632Seric ** runs things in the mail queue. 2664632Seric */ 2674632Seric 2684639Seric runqueue(forkflag) 2694639Seric bool forkflag; 2704632Seric { 27124953Seric extern bool shouldqueue(); 27224953Seric 2737466Seric /* 27424953Seric ** If no work will ever be selected, don't even bother reading 27524953Seric ** the queue. 27624953Seric */ 27724953Seric 27824953Seric if (shouldqueue(-100000000L)) 27924953Seric { 28024953Seric if (Verbose) 28124953Seric printf("Skipping queue run -- load average too high\n"); 28224953Seric 28324953Seric if (forkflag) 28424953Seric return; 28524953Seric finis(); 28624953Seric } 28724953Seric 28824953Seric /* 2897466Seric ** See if we want to go off and do other useful work. 2907466Seric */ 2914639Seric 2924639Seric if (forkflag) 2934639Seric { 2947943Seric int pid; 2957943Seric 2967943Seric pid = dofork(); 2977943Seric if (pid != 0) 2984639Seric { 29925184Seric extern reapchild(); 30025184Seric 3017943Seric /* parent -- pick up intermediate zombie */ 30225184Seric #ifndef SIGCHLD 3039377Seric (void) waitfor(pid); 30425184Seric #else SIGCHLD 30525184Seric (void) signal(SIGCHLD, reapchild); 30625184Seric #endif SIGCHLD 3077690Seric if (QueueIntvl != 0) 3089348Seric (void) setevent(QueueIntvl, runqueue, TRUE); 3094639Seric return; 3104639Seric } 3117943Seric /* child -- double fork */ 31225184Seric #ifndef SIGCHLD 3137943Seric if (fork() != 0) 3147943Seric exit(EX_OK); 31525184Seric #else SIGCHLD 31625184Seric (void) signal(SIGCHLD, SIG_DFL); 31725184Seric #endif SIGCHLD 3184639Seric } 31924941Seric 32024941Seric setproctitle("running queue"); 32124941Seric 3227876Seric # ifdef LOG 3237876Seric if (LogLevel > 11) 3247943Seric syslog(LOG_DEBUG, "runqueue %s, pid=%d", QueueDir, getpid()); 3257876Seric # endif LOG 3264639Seric 3277466Seric /* 32810205Seric ** Release any resources used by the daemon code. 32910205Seric */ 33010205Seric 33110205Seric # ifdef DAEMON 33210205Seric clrdaemon(); 33310205Seric # endif DAEMON 33410205Seric 33510205Seric /* 33627175Seric ** Make sure the alias database is open. 33727175Seric */ 33827175Seric 33927175Seric initaliases(AliasFile, FALSE); 34027175Seric 34127175Seric /* 3427466Seric ** Start making passes through the queue. 3437466Seric ** First, read and sort the entire queue. 3447466Seric ** Then, process the work in that order. 3457466Seric ** But if you take too long, start over. 3467466Seric */ 3477466Seric 3487943Seric /* order the existing work requests */ 34924954Seric (void) orderq(FALSE); 3507690Seric 3517943Seric /* process them once at a time */ 3527943Seric while (WorkQ != NULL) 3534639Seric { 3547943Seric WORK *w = WorkQ; 3557881Seric 3567943Seric WorkQ = WorkQ->w_next; 3577943Seric dowork(w); 3587943Seric free(w->w_name); 3597943Seric free((char *) w); 3604639Seric } 36129866Seric 36229866Seric /* exit without the usual cleanup */ 36329866Seric exit(ExitStat); 3644634Seric } 3654634Seric /* 3664632Seric ** ORDERQ -- order the work queue. 3674632Seric ** 3684632Seric ** Parameters: 36924941Seric ** doall -- if set, include everything in the queue (even 37024941Seric ** the jobs that cannot be run because the load 37124941Seric ** average is too high). Otherwise, exclude those 37224941Seric ** jobs. 3734632Seric ** 3744632Seric ** Returns: 37510121Seric ** The number of request in the queue (not necessarily 37610121Seric ** the number of requests in WorkQ however). 3774632Seric ** 3784632Seric ** Side Effects: 3794632Seric ** Sets WorkQ to the queue of available work, in order. 3804632Seric */ 3814632Seric 38225687Seric # define NEED_P 001 38325687Seric # define NEED_T 002 3844632Seric 38524941Seric orderq(doall) 38624941Seric bool doall; 3874632Seric { 3886625Sglickman register struct direct *d; 3894632Seric register WORK *w; 3906625Sglickman DIR *f; 3914632Seric register int i; 39225687Seric WORK wlist[QUEUESIZE+1]; 39310070Seric int wn = -1; 3944632Seric extern workcmpf(); 3954632Seric 3964632Seric /* clear out old WorkQ */ 3974632Seric for (w = WorkQ; w != NULL; ) 3984632Seric { 3994632Seric register WORK *nw = w->w_next; 4004632Seric 4014632Seric WorkQ = nw; 4024632Seric free(w->w_name); 4034632Seric free((char *) w); 4044632Seric w = nw; 4054632Seric } 4064632Seric 4074632Seric /* open the queue directory */ 4088148Seric f = opendir("."); 4094632Seric if (f == NULL) 4104632Seric { 4118148Seric syserr("orderq: cannot open \"%s\" as \".\"", QueueDir); 41210070Seric return (0); 4134632Seric } 4144632Seric 4154632Seric /* 4164632Seric ** Read the work directory. 4174632Seric */ 4184632Seric 41910070Seric while ((d = readdir(f)) != NULL) 4204632Seric { 4219377Seric FILE *cf; 4224632Seric char lbuf[MAXNAME]; 4234632Seric 4244632Seric /* is this an interesting entry? */ 4257812Seric if (d->d_name[0] != 'q' || d->d_name[1] != 'f') 4264632Seric continue; 4274632Seric 42810070Seric /* yes -- open control file (if not too many files) */ 42925687Seric if (++wn >= QUEUESIZE) 43010070Seric continue; 4318148Seric cf = fopen(d->d_name, "r"); 4324632Seric if (cf == NULL) 4334632Seric { 4347055Seric /* this may be some random person sending hir msgs */ 4357055Seric /* syserr("orderq: cannot open %s", cbuf); */ 43610090Seric #ifdef DEBUG 43710090Seric if (tTd(41, 2)) 43810090Seric printf("orderq: cannot open %s (%d)\n", 43910090Seric d->d_name, errno); 44010090Seric #endif DEBUG 4417055Seric errno = 0; 44210090Seric wn--; 4434632Seric continue; 4444632Seric } 44525687Seric w = &wlist[wn]; 44625687Seric w->w_name = newstr(d->d_name); 4474632Seric 44825027Seric /* make sure jobs in creation don't clog queue */ 44925687Seric w->w_pri = 0x7fffffff; 45025687Seric w->w_ctime = 0; 45125027Seric 4524632Seric /* extract useful information */ 45325687Seric i = NEED_P | NEED_T; 45425687Seric while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL) 4554632Seric { 45624954Seric extern long atol(); 45724954Seric 45824941Seric switch (lbuf[0]) 4594632Seric { 46024941Seric case 'P': 46125687Seric w->w_pri = atol(&lbuf[1]); 46225687Seric i &= ~NEED_P; 4634632Seric break; 46425013Seric 46525013Seric case 'T': 46625687Seric w->w_ctime = atol(&lbuf[1]); 46725687Seric i &= ~NEED_T; 46825013Seric break; 4694632Seric } 4704632Seric } 4714632Seric (void) fclose(cf); 47224953Seric 47325687Seric if (!doall && shouldqueue(w->w_pri)) 47424953Seric { 47524953Seric /* don't even bother sorting this job in */ 47624953Seric wn--; 47724953Seric } 4784632Seric } 4796625Sglickman (void) closedir(f); 48010090Seric wn++; 4814632Seric 4824632Seric /* 4834632Seric ** Sort the work directory. 4844632Seric */ 4854632Seric 48625687Seric qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf); 4874632Seric 4884632Seric /* 4894632Seric ** Convert the work list into canonical form. 4909377Seric ** Should be turning it into a list of envelopes here perhaps. 4914632Seric */ 4924632Seric 49324981Seric WorkQ = NULL; 49425687Seric for (i = min(wn, QUEUESIZE); --i >= 0; ) 4954632Seric { 4964632Seric w = (WORK *) xalloc(sizeof *w); 4974632Seric w->w_name = wlist[i].w_name; 4984632Seric w->w_pri = wlist[i].w_pri; 49925013Seric w->w_ctime = wlist[i].w_ctime; 50024981Seric w->w_next = WorkQ; 50124981Seric WorkQ = w; 5024632Seric } 5034632Seric 5044632Seric # ifdef DEBUG 5057677Seric if (tTd(40, 1)) 5064632Seric { 5074632Seric for (w = WorkQ; w != NULL; w = w->w_next) 5085037Seric printf("%32s: pri=%ld\n", w->w_name, w->w_pri); 5094632Seric } 5104632Seric # endif DEBUG 51110070Seric 51210090Seric return (wn); 5134632Seric } 5144632Seric /* 5157677Seric ** WORKCMPF -- compare function for ordering work. 5164632Seric ** 5174632Seric ** Parameters: 5184632Seric ** a -- the first argument. 5194632Seric ** b -- the second argument. 5204632Seric ** 5214632Seric ** Returns: 52224981Seric ** -1 if a < b 52324981Seric ** 0 if a == b 52424981Seric ** +1 if a > b 5254632Seric ** 5264632Seric ** Side Effects: 5274632Seric ** none. 5284632Seric */ 5294632Seric 5304632Seric workcmpf(a, b) 5315037Seric register WORK *a; 5325037Seric register WORK *b; 5334632Seric { 53425013Seric long pa = a->w_pri + a->w_ctime; 53525013Seric long pb = b->w_pri + b->w_ctime; 53624941Seric 53724941Seric if (pa == pb) 5384632Seric return (0); 53924941Seric else if (pa > pb) 54024981Seric return (1); 54124981Seric else 54210121Seric return (-1); 5434632Seric } 5444632Seric /* 5454632Seric ** DOWORK -- do a work request. 5464632Seric ** 5474632Seric ** Parameters: 5484632Seric ** w -- the work request to be satisfied. 5494632Seric ** 5504632Seric ** Returns: 5514632Seric ** none. 5524632Seric ** 5534632Seric ** Side Effects: 5544632Seric ** The work request is satisfied if possible. 5554632Seric */ 5564632Seric 5574632Seric dowork(w) 5584632Seric register WORK *w; 5594632Seric { 5604632Seric register int i; 56124941Seric extern bool shouldqueue(); 5624632Seric 5634632Seric # ifdef DEBUG 5647677Seric if (tTd(40, 1)) 5655037Seric printf("dowork: %s pri %ld\n", w->w_name, w->w_pri); 5664632Seric # endif DEBUG 5674632Seric 5684632Seric /* 56924941Seric ** Ignore jobs that are too expensive for the moment. 5704632Seric */ 5714632Seric 57224941Seric if (shouldqueue(w->w_pri)) 5734632Seric { 57424941Seric if (Verbose) 57524967Seric printf("\nSkipping %s\n", w->w_name + 2); 5764632Seric return; 5774632Seric } 5784632Seric 57924941Seric /* 58024941Seric ** Fork for work. 58124941Seric */ 58224941Seric 58324941Seric if (ForkQueueRuns) 58424941Seric { 58524941Seric i = fork(); 58624941Seric if (i < 0) 58724941Seric { 58824941Seric syserr("dowork: cannot fork"); 58924941Seric return; 59024941Seric } 59124941Seric } 59224941Seric else 59324941Seric { 59424941Seric i = 0; 59524941Seric } 59624941Seric 5974632Seric if (i == 0) 5984632Seric { 5994632Seric /* 6004632Seric ** CHILD 6018148Seric ** Lock the control file to avoid duplicate deliveries. 6028148Seric ** Then run the file as though we had just read it. 6037350Seric ** We save an idea of the temporary name so we 6047350Seric ** can recover on interrupt. 6054632Seric */ 6064632Seric 6077763Seric /* set basic modes, etc. */ 6087356Seric (void) alarm(0); 60925612Seric clearenvelope(CurEnv, FALSE); 6104632Seric QueueRun = TRUE; 6119377Seric ErrorMode = EM_MAIL; 6128148Seric CurEnv->e_id = &w->w_name[2]; 6137876Seric # ifdef LOG 6147876Seric if (LogLevel > 11) 6157881Seric syslog(LOG_DEBUG, "%s: dowork, pid=%d", CurEnv->e_id, 6167881Seric getpid()); 6177876Seric # endif LOG 6187763Seric 6197763Seric /* don't use the headers from sendmail.cf... */ 6207763Seric CurEnv->e_header = NULL; 6217763Seric 62217468Seric /* lock the control file during processing */ 6237812Seric if (link(w->w_name, queuename(CurEnv, 'l')) < 0) 6246980Seric { 6257812Seric /* being processed by another queuer */ 6267881Seric # ifdef LOG 6277881Seric if (LogLevel > 4) 6287881Seric syslog(LOG_DEBUG, "%s: locked", CurEnv->e_id); 6297881Seric # endif LOG 63024941Seric if (ForkQueueRuns) 63124941Seric exit(EX_OK); 63224941Seric else 63324941Seric return; 6346980Seric } 6356980Seric 6366980Seric /* do basic system initialization */ 6374632Seric initsys(); 6386980Seric 6396980Seric /* read the queue control file */ 64017477Seric readqf(CurEnv, TRUE); 6419338Seric CurEnv->e_flags |= EF_INQUEUE; 6429377Seric eatheader(CurEnv); 6436980Seric 6446980Seric /* do the delivery */ 6459338Seric if (!bitset(EF_FATALERRS, CurEnv->e_flags)) 6469282Seric sendall(CurEnv, SM_DELIVER); 6476980Seric 6486980Seric /* finish up and exit */ 64924941Seric if (ForkQueueRuns) 65024941Seric finis(); 65124941Seric else 65224941Seric dropenvelope(CurEnv); 6534632Seric } 65424941Seric else 65524941Seric { 65624941Seric /* 65724941Seric ** Parent -- pick up results. 65824941Seric */ 6594632Seric 66024941Seric errno = 0; 66124941Seric (void) waitfor(i); 66224941Seric } 6634632Seric } 6644632Seric /* 6654632Seric ** READQF -- read queue file and set up environment. 6664632Seric ** 6674632Seric ** Parameters: 6689377Seric ** e -- the envelope of the job to run. 6699630Seric ** full -- if set, read in all information. Otherwise just 6709630Seric ** read in info needed for a queue print. 6714632Seric ** 6724632Seric ** Returns: 6734632Seric ** none. 6744632Seric ** 6754632Seric ** Side Effects: 6764632Seric ** cf is read and created as the current job, as though 6774632Seric ** we had been invoked by argument. 6784632Seric */ 6794632Seric 68017477Seric readqf(e, full) 6819377Seric register ENVELOPE *e; 6829630Seric bool full; 6834632Seric { 68417477Seric char *qf; 68517477Seric register FILE *qfp; 6867785Seric char buf[MAXFIELD]; 6879348Seric extern char *fgetfolded(); 68824954Seric extern long atol(); 6894632Seric 6904632Seric /* 69117468Seric ** Read and process the file. 6924632Seric */ 6934632Seric 69417477Seric qf = queuename(e, 'q'); 69517477Seric qfp = fopen(qf, "r"); 69617477Seric if (qfp == NULL) 69717477Seric { 69817477Seric syserr("readqf: no control file %s", qf); 69917477Seric return; 70017477Seric } 70117477Seric FileName = qf; 7029377Seric LineNumber = 0; 7039630Seric if (Verbose && full) 7049377Seric printf("\nRunning %s\n", e->e_id); 70517468Seric while (fgetfolded(buf, sizeof buf, qfp) != NULL) 7064632Seric { 70726504Seric # ifdef DEBUG 70826504Seric if (tTd(40, 4)) 70926504Seric printf("+++++ %s\n", buf); 71026504Seric # endif DEBUG 7114632Seric switch (buf[0]) 7124632Seric { 7134632Seric case 'R': /* specify recipient */ 7149618Seric sendtolist(&buf[1], (ADDRESS *) NULL, &e->e_sendqueue); 7154632Seric break; 7164632Seric 71725687Seric case 'E': /* specify error recipient */ 71825687Seric sendtolist(&buf[1], (ADDRESS *) NULL, &e->e_errorqueue); 71925687Seric break; 72025687Seric 7214632Seric case 'H': /* header */ 7229630Seric if (full) 7239630Seric (void) chompheader(&buf[1], FALSE); 7244632Seric break; 7254632Seric 72610108Seric case 'M': /* message */ 72710108Seric e->e_message = newstr(&buf[1]); 72810108Seric break; 72910108Seric 7304632Seric case 'S': /* sender */ 7314634Seric setsender(newstr(&buf[1])); 7324632Seric break; 7334632Seric 7344632Seric case 'D': /* data file name */ 7359630Seric if (!full) 7369630Seric break; 7379377Seric e->e_df = newstr(&buf[1]); 7389544Seric e->e_dfp = fopen(e->e_df, "r"); 7399544Seric if (e->e_dfp == NULL) 7409377Seric syserr("readqf: cannot open %s", e->e_df); 7414632Seric break; 7424632Seric 7437860Seric case 'T': /* init time */ 74424941Seric e->e_ctime = atol(&buf[1]); 7454632Seric break; 7464632Seric 7474634Seric case 'P': /* message priority */ 74825008Seric e->e_msgpriority = atol(&buf[1]) + WkTimeFact; 7494634Seric break; 7504634Seric 75124941Seric case '\0': /* blank line; ignore */ 75224941Seric break; 75324941Seric 7544632Seric default: 75524941Seric syserr("readqf(%s:%d): bad line \"%s\"", e->e_id, 75624941Seric LineNumber, buf); 7574632Seric break; 7584632Seric } 7594632Seric } 7609377Seric 76124954Seric (void) fclose(qfp); 7629377Seric FileName = NULL; 76324941Seric 76424941Seric /* 76524941Seric ** If we haven't read any lines, this queue file is empty. 76624941Seric ** Arrange to remove it without referencing any null pointers. 76724941Seric */ 76824941Seric 76924941Seric if (LineNumber == 0) 77024941Seric { 77124941Seric errno = 0; 77224941Seric e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE; 77324941Seric } 7744632Seric } 7754632Seric /* 7769630Seric ** PRINTQUEUE -- print out a representation of the mail queue 7779630Seric ** 7789630Seric ** Parameters: 7799630Seric ** none. 7809630Seric ** 7819630Seric ** Returns: 7829630Seric ** none. 7839630Seric ** 7849630Seric ** Side Effects: 7859630Seric ** Prints a listing of the mail queue on the standard output. 7869630Seric */ 7875182Seric 7889630Seric printqueue() 7899630Seric { 7909630Seric register WORK *w; 7919630Seric FILE *f; 79210070Seric int nrequests; 7939630Seric char buf[MAXLINE]; 7949630Seric 7959630Seric /* 7969630Seric ** Read and order the queue. 7979630Seric */ 7989630Seric 79924941Seric nrequests = orderq(TRUE); 8009630Seric 8019630Seric /* 8029630Seric ** Print the work list that we have read. 8039630Seric */ 8049630Seric 8059630Seric /* first see if there is anything */ 80610070Seric if (nrequests <= 0) 8079630Seric { 80810070Seric printf("Mail queue is empty\n"); 8099630Seric return; 8109630Seric } 8119630Seric 81210096Seric printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s"); 81325687Seric if (nrequests > QUEUESIZE) 81425687Seric printf(", only %d printed", QUEUESIZE); 81524979Seric if (Verbose) 81625032Seric printf(")\n--QID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n"); 81724979Seric else 81824979Seric printf(")\n--QID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n"); 8199630Seric for (w = WorkQ; w != NULL; w = w->w_next) 8209630Seric { 8219630Seric struct stat st; 82210070Seric auto time_t submittime = 0; 82310070Seric long dfsize = -1; 82410108Seric char lf[20]; 82510108Seric char message[MAXLINE]; 82624941Seric extern bool shouldqueue(); 8279630Seric 82817468Seric f = fopen(w->w_name, "r"); 82917468Seric if (f == NULL) 83017468Seric { 83117468Seric errno = 0; 83217468Seric continue; 83317468Seric } 8349630Seric printf("%7s", w->w_name + 2); 83523098Seric (void) strcpy(lf, w->w_name); 83610070Seric lf[0] = 'l'; 83710070Seric if (stat(lf, &st) >= 0) 83810070Seric printf("*"); 83924941Seric else if (shouldqueue(w->w_pri)) 84024941Seric printf("X"); 84110070Seric else 84210070Seric printf(" "); 84310070Seric errno = 0; 84417468Seric 84510108Seric message[0] = '\0'; 8469630Seric while (fgets(buf, sizeof buf, f) != NULL) 8479630Seric { 8489630Seric fixcrlf(buf, TRUE); 8499630Seric switch (buf[0]) 8509630Seric { 85110108Seric case 'M': /* error message */ 85223098Seric (void) strcpy(message, &buf[1]); 85310108Seric break; 85410108Seric 8559630Seric case 'S': /* sender name */ 85624979Seric if (Verbose) 85725027Seric printf("%8ld %10ld %.12s %.38s", dfsize, 85825027Seric w->w_pri, ctime(&submittime) + 4, 85924979Seric &buf[1]); 86024979Seric else 86124979Seric printf("%8ld %.16s %.45s", dfsize, 86224979Seric ctime(&submittime), &buf[1]); 86310108Seric if (message[0] != '\0') 86425027Seric printf("\n\t\t (%.60s)", message); 8659630Seric break; 8669630Seric 8679630Seric case 'R': /* recipient name */ 86824979Seric if (Verbose) 86925027Seric printf("\n\t\t\t\t\t %.38s", &buf[1]); 87024979Seric else 87124979Seric printf("\n\t\t\t\t %.45s", &buf[1]); 8729630Seric break; 8739630Seric 8749630Seric case 'T': /* creation time */ 87524941Seric submittime = atol(&buf[1]); 8769630Seric break; 87710070Seric 87810070Seric case 'D': /* data file name */ 87910070Seric if (stat(&buf[1], &st) >= 0) 88010070Seric dfsize = st.st_size; 88110070Seric break; 8829630Seric } 8839630Seric } 88410070Seric if (submittime == (time_t) 0) 88510070Seric printf(" (no control file)"); 8869630Seric printf("\n"); 88723098Seric (void) fclose(f); 8889630Seric } 8899630Seric } 8909630Seric 8915182Seric # endif QUEUE 89217468Seric /* 89317468Seric ** QUEUENAME -- build a file name in the queue directory for this envelope. 89417468Seric ** 89517468Seric ** Assigns an id code if one does not already exist. 89617468Seric ** This code is very careful to avoid trashing existing files 89717468Seric ** under any circumstances. 89817468Seric ** We first create an nf file that is only used when 89917468Seric ** assigning an id. This file is always empty, so that 90017468Seric ** we can never accidently truncate an lf file. 90117468Seric ** 90217468Seric ** Parameters: 90317468Seric ** e -- envelope to build it in/from. 90417468Seric ** type -- the file type, used as the first character 90517468Seric ** of the file name. 90617468Seric ** 90717468Seric ** Returns: 90817468Seric ** a pointer to the new file name (in a static buffer). 90917468Seric ** 91017468Seric ** Side Effects: 91117468Seric ** Will create the lf and qf files if no id code is 91217468Seric ** already assigned. This will cause the envelope 91317468Seric ** to be modified. 91417468Seric */ 91517468Seric 91617468Seric char * 91717468Seric queuename(e, type) 91817468Seric register ENVELOPE *e; 91917468Seric char type; 92017468Seric { 92117468Seric static char buf[MAXNAME]; 92217468Seric static int pid = -1; 92317468Seric char c1 = 'A'; 92417468Seric char c2 = 'A'; 92517468Seric 92617468Seric if (e->e_id == NULL) 92717468Seric { 92817468Seric char qf[20]; 92917468Seric char nf[20]; 93017468Seric char lf[20]; 93117468Seric 93217468Seric /* find a unique id */ 93317468Seric if (pid != getpid()) 93417468Seric { 93517468Seric /* new process -- start back at "AA" */ 93617468Seric pid = getpid(); 93717468Seric c1 = 'A'; 93817468Seric c2 = 'A' - 1; 93917468Seric } 94017468Seric (void) sprintf(qf, "qfAA%05d", pid); 94123098Seric (void) strcpy(lf, qf); 94217468Seric lf[0] = 'l'; 94323098Seric (void) strcpy(nf, qf); 94417468Seric nf[0] = 'n'; 94517468Seric 94617468Seric while (c1 < '~' || c2 < 'Z') 94717468Seric { 94817468Seric int i; 94917468Seric 95017468Seric if (c2 >= 'Z') 95117468Seric { 95217468Seric c1++; 95317468Seric c2 = 'A' - 1; 95417468Seric } 95517477Seric lf[2] = nf[2] = qf[2] = c1; 95617477Seric lf[3] = nf[3] = qf[3] = ++c2; 95717468Seric # ifdef DEBUG 95817468Seric if (tTd(7, 20)) 95917468Seric printf("queuename: trying \"%s\"\n", nf); 96017468Seric # endif DEBUG 96117468Seric 96217468Seric # ifdef QUEUE 96317468Seric if (access(lf, 0) >= 0 || access(qf, 0) >= 0) 96417468Seric continue; 96517468Seric errno = 0; 96617468Seric i = creat(nf, FileMode); 96717468Seric if (i < 0) 96817468Seric { 96917468Seric (void) unlink(nf); /* kernel bug */ 97017468Seric continue; 97117468Seric } 97217468Seric (void) close(i); 97317468Seric i = link(nf, lf); 97417468Seric (void) unlink(nf); 97517468Seric if (i < 0) 97617468Seric continue; 97717468Seric if (link(lf, qf) >= 0) 97817468Seric break; 97917468Seric (void) unlink(lf); 98017468Seric # else QUEUE 98117982Seric if (close(creat(qf, FileMode)) >= 0) 98217982Seric break; 98317468Seric # endif QUEUE 98417468Seric } 98517468Seric if (c1 >= '~' && c2 >= 'Z') 98617468Seric { 98717468Seric syserr("queuename: Cannot create \"%s\" in \"%s\"", 98817468Seric qf, QueueDir); 98917468Seric exit(EX_OSERR); 99017468Seric } 99117468Seric e->e_id = newstr(&qf[2]); 99217468Seric define('i', e->e_id, e); 99317468Seric # ifdef DEBUG 99417468Seric if (tTd(7, 1)) 99517468Seric printf("queuename: assigned id %s, env=%x\n", e->e_id, e); 99617468Seric # ifdef LOG 99717468Seric if (LogLevel > 16) 99817468Seric syslog(LOG_DEBUG, "%s: assigned id", e->e_id); 99917468Seric # endif LOG 100017468Seric # endif DEBUG 100117468Seric } 100217468Seric 100317468Seric if (type == '\0') 100417468Seric return (NULL); 100517468Seric (void) sprintf(buf, "%cf%s", type, e->e_id); 100617468Seric # ifdef DEBUG 100717468Seric if (tTd(7, 2)) 100817468Seric printf("queuename: %s\n", buf); 100917468Seric # endif DEBUG 101017468Seric return (buf); 101117468Seric } 101217468Seric /* 101317468Seric ** UNLOCKQUEUE -- unlock the queue entry for a specified envelope 101417468Seric ** 101517468Seric ** Parameters: 101617468Seric ** e -- the envelope to unlock. 101717468Seric ** 101817468Seric ** Returns: 101917468Seric ** none 102017468Seric ** 102117468Seric ** Side Effects: 102217468Seric ** unlocks the queue for `e'. 102317468Seric */ 102417468Seric 102517468Seric unlockqueue(e) 102617468Seric ENVELOPE *e; 102717468Seric { 102817468Seric /* remove the transcript */ 102917468Seric #ifdef DEBUG 103017468Seric # ifdef LOG 103117468Seric if (LogLevel > 19) 103217468Seric syslog(LOG_DEBUG, "%s: unlock", e->e_id); 103317468Seric # endif LOG 103417468Seric if (!tTd(51, 4)) 103517468Seric #endif DEBUG 103617468Seric xunlink(queuename(e, 'x')); 103717468Seric 103817468Seric # ifdef QUEUE 103917468Seric /* last but not least, remove the lock */ 104017468Seric xunlink(queuename(e, 'l')); 104117468Seric # endif QUEUE 104217468Seric } 1043