122708Sdist /* 222708Sdist ** Sendmail 322708Sdist ** Copyright (c) 1983 Eric P. Allman 422708Sdist ** Berkeley, California 522708Sdist ** 622708Sdist ** Copyright (c) 1983 Regents of the University of California. 722708Sdist ** All rights reserved. The Berkeley software License Agreement 822708Sdist ** specifies the terms and conditions for redistribution. 922708Sdist */ 1022708Sdist 1122708Sdist 124632Seric # include "sendmail.h" 134632Seric # include <sys/stat.h> 1413707Ssam # include <sys/dir.h> 154634Seric # include <signal.h> 164632Seric # include <errno.h> 174632Seric 185182Seric # ifndef QUEUE 1923116Seric # ifndef lint 20*24954Seric static char SccsId[] = "@(#)queue.c 5.7 (Berkeley) 09/19/85 (no queueing)"; 2123116Seric # endif not lint 225182Seric # else QUEUE 234632Seric 2423116Seric # ifndef lint 25*24954Seric static char SccsId[] = "@(#)queue.c 5.7 (Berkeley) 09/19/85"; 2623116Seric # endif not lint 275182Seric 284632Seric /* 299377Seric ** Work queue. 309377Seric */ 319377Seric 329377Seric struct work 339377Seric { 349377Seric char *w_name; /* name of control file */ 359377Seric long w_pri; /* priority of message, see below */ 3624941Seric long w_ctime; /* creation time of message */ 379377Seric struct work *w_next; /* next in queue */ 389377Seric }; 399377Seric 409377Seric typedef struct work WORK; 419377Seric 429377Seric WORK *WorkQ; /* queue of things to be done */ 439377Seric /* 444632Seric ** QUEUEUP -- queue a message up for future transmission. 454632Seric ** 464632Seric ** Parameters: 476980Seric ** e -- the envelope to queue up. 486999Seric ** queueall -- if TRUE, queue all addresses, rather than 496999Seric ** just those with the QQUEUEUP flag set. 509377Seric ** announce -- if TRUE, tell when you are queueing up. 514632Seric ** 524632Seric ** Returns: 534632Seric ** none. 544632Seric ** 554632Seric ** Side Effects: 569377Seric ** The current request are saved in a control file. 574632Seric */ 584632Seric 599377Seric queueup(e, queueall, announce) 606980Seric register ENVELOPE *e; 616999Seric bool queueall; 629377Seric bool announce; 634632Seric { 647812Seric char *tf; 657812Seric char *qf; 667763Seric char buf[MAXLINE]; 677812Seric register FILE *tfp; 684632Seric register HDR *h; 695007Seric register ADDRESS *q; 7010173Seric MAILER nullmailer; 714632Seric 725037Seric /* 7317477Seric ** Create control file. 745037Seric */ 754632Seric 7617477Seric tf = newstr(queuename(e, 't')); 7717477Seric tfp = fopen(tf, "w"); 787812Seric if (tfp == NULL) 794632Seric { 8017477Seric syserr("queueup: cannot create temp file %s", tf); 8117477Seric return; 824632Seric } 8317477Seric (void) chmod(tf, FileMode); 844632Seric 854632Seric # ifdef DEBUG 867677Seric if (tTd(40, 1)) 8717468Seric printf("queueing %s\n", e->e_id); 884632Seric # endif DEBUG 894632Seric 904632Seric /* 916980Seric ** If there is no data file yet, create one. 926980Seric */ 936980Seric 946980Seric if (e->e_df == NULL) 956980Seric { 966980Seric register FILE *dfp; 979389Seric extern putbody(); 986980Seric 997812Seric e->e_df = newstr(queuename(e, 'd')); 1006980Seric dfp = fopen(e->e_df, "w"); 1016980Seric if (dfp == NULL) 1026980Seric { 1036980Seric syserr("queueup: cannot create %s", e->e_df); 1047812Seric (void) fclose(tfp); 1056980Seric return; 1066980Seric } 1079048Seric (void) chmod(e->e_df, FileMode); 10810173Seric (*e->e_putbody)(dfp, ProgMailer, e); 1097009Seric (void) fclose(dfp); 1109389Seric e->e_putbody = putbody; 1116980Seric } 1126980Seric 1136980Seric /* 1144632Seric ** Output future work requests. 1159377Seric ** Priority should be first, since it is read by orderq. 1164632Seric */ 1174632Seric 1189377Seric /* output message priority */ 1199377Seric fprintf(tfp, "P%ld\n", e->e_msgpriority); 1209377Seric 1219630Seric /* output creation time */ 1229630Seric fprintf(tfp, "T%ld\n", e->e_ctime); 1239630Seric 1244632Seric /* output name of data file */ 1257812Seric fprintf(tfp, "D%s\n", e->e_df); 1264632Seric 12710108Seric /* message from envelope, if it exists */ 12810108Seric if (e->e_message != NULL) 12910108Seric fprintf(tfp, "M%s\n", e->e_message); 13010108Seric 1314632Seric /* output name of sender */ 1327812Seric fprintf(tfp, "S%s\n", e->e_from.q_paddr); 1334632Seric 1344632Seric /* output list of recipient addresses */ 1356980Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1364632Seric { 1377763Seric if (queueall ? !bitset(QDONTSEND, q->q_flags) : 1387763Seric bitset(QQUEUEUP, q->q_flags)) 1398245Seric { 1407812Seric fprintf(tfp, "R%s\n", q->q_paddr); 1419377Seric if (announce) 1429377Seric { 1439377Seric e->e_to = q->q_paddr; 1449377Seric message(Arpa_Info, "queued"); 1459377Seric if (LogLevel > 4) 1469377Seric logdelivery("queued"); 1479377Seric e->e_to = NULL; 1489377Seric } 1499387Seric #ifdef DEBUG 1509387Seric if (tTd(40, 1)) 1519387Seric { 1529387Seric printf("queueing "); 1539387Seric printaddr(q, FALSE); 1549387Seric } 1559387Seric #endif DEBUG 1568245Seric } 1574632Seric } 1584632Seric 1599377Seric /* 1609377Seric ** Output headers for this message. 1619377Seric ** Expand macros completely here. Queue run will deal with 1629377Seric ** everything as absolute headers. 1639377Seric ** All headers that must be relative to the recipient 1649377Seric ** can be cracked later. 16510173Seric ** We set up a "null mailer" -- i.e., a mailer that will have 16610173Seric ** no effect on the addresses as they are output. 1679377Seric */ 1689377Seric 16910686Seric bzero((char *) &nullmailer, sizeof nullmailer); 17010173Seric nullmailer.m_r_rwset = nullmailer.m_s_rwset = -1; 17110349Seric nullmailer.m_eol = "\n"; 17210173Seric 17316147Seric define('g', "\001f", e); 1746980Seric for (h = e->e_header; h != NULL; h = h->h_link) 1754632Seric { 17610686Seric extern bool bitzerop(); 17710686Seric 17812015Seric /* don't output null headers */ 1794632Seric if (h->h_value == NULL || h->h_value[0] == '\0') 1804632Seric continue; 18112015Seric 18212015Seric /* don't output resent headers on non-resent messages */ 18312015Seric if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags)) 18412015Seric continue; 18512015Seric 18612015Seric /* output this header */ 1877812Seric fprintf(tfp, "H"); 18812015Seric 18912015Seric /* if conditional, output the set of conditions */ 19010686Seric if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags)) 19110686Seric { 19210686Seric int j; 19310686Seric 19423098Seric (void) putc('?', tfp); 19510686Seric for (j = '\0'; j <= '\177'; j++) 19610686Seric if (bitnset(j, h->h_mflags)) 19723098Seric (void) putc(j, tfp); 19823098Seric (void) putc('?', tfp); 19910686Seric } 20012015Seric 20112015Seric /* output the header: expand macros, convert addresses */ 2027763Seric if (bitset(H_DEFAULT, h->h_flags)) 2037763Seric { 2047763Seric (void) expand(h->h_value, buf, &buf[sizeof buf], e); 2058236Seric fprintf(tfp, "%s: %s\n", h->h_field, buf); 2067763Seric } 2078245Seric else if (bitset(H_FROM|H_RCPT, h->h_flags)) 2089348Seric { 2099348Seric commaize(h, h->h_value, tfp, bitset(EF_OLDSTYLE, e->e_flags), 21010173Seric &nullmailer); 2119348Seric } 2127763Seric else 2138245Seric fprintf(tfp, "%s: %s\n", h->h_field, h->h_value); 2144632Seric } 2154632Seric 2164632Seric /* 2174632Seric ** Clean up. 2184632Seric */ 2194632Seric 22017477Seric (void) fclose(tfp); 22117468Seric qf = queuename(e, 'q'); 22217468Seric if (tf != NULL) 22317468Seric { 22417468Seric holdsigs(); 22517468Seric (void) unlink(qf); 22617468Seric if (link(tf, qf) < 0) 22717468Seric syserr("cannot link(%s, %s), df=%s", tf, qf, e->e_df); 22817468Seric else 22917468Seric (void) unlink(tf); 23017468Seric rlsesigs(); 23117468Seric } 2327391Seric 2337677Seric # ifdef LOG 2347677Seric /* save log info */ 2357878Seric if (LogLevel > 15) 2367878Seric syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df); 2377677Seric # endif LOG 2384632Seric } 2394632Seric /* 2404632Seric ** RUNQUEUE -- run the jobs in the queue. 2414632Seric ** 2424632Seric ** Gets the stuff out of the queue in some presumably logical 2434632Seric ** order and processes them. 2444632Seric ** 2454632Seric ** Parameters: 24624941Seric ** forkflag -- TRUE if the queue scanning should be done in 24724941Seric ** a child process. We double-fork so it is not our 24824941Seric ** child and we don't have to clean up after it. 2494632Seric ** 2504632Seric ** Returns: 2514632Seric ** none. 2524632Seric ** 2534632Seric ** Side Effects: 2544632Seric ** runs things in the mail queue. 2554632Seric */ 2564632Seric 2574639Seric runqueue(forkflag) 2584639Seric bool forkflag; 2594632Seric { 26024953Seric extern bool shouldqueue(); 26124953Seric 2627466Seric /* 26324953Seric ** If no work will ever be selected, don't even bother reading 26424953Seric ** the queue. 26524953Seric */ 26624953Seric 26724953Seric if (shouldqueue(-100000000L)) 26824953Seric { 26924953Seric if (Verbose) 27024953Seric printf("Skipping queue run -- load average too high\n"); 27124953Seric 27224953Seric if (forkflag) 27324953Seric return; 27424953Seric finis(); 27524953Seric } 27624953Seric 27724953Seric /* 2787466Seric ** See if we want to go off and do other useful work. 2797466Seric */ 2804639Seric 2814639Seric if (forkflag) 2824639Seric { 2837943Seric int pid; 2847943Seric 2857943Seric pid = dofork(); 2867943Seric if (pid != 0) 2874639Seric { 2887943Seric /* parent -- pick up intermediate zombie */ 2899377Seric (void) waitfor(pid); 2907690Seric if (QueueIntvl != 0) 2919348Seric (void) setevent(QueueIntvl, runqueue, TRUE); 2924639Seric return; 2934639Seric } 2947943Seric /* child -- double fork */ 2957943Seric if (fork() != 0) 2967943Seric exit(EX_OK); 2974639Seric } 29824941Seric 29924941Seric setproctitle("running queue"); 30024941Seric 3017876Seric # ifdef LOG 3027876Seric if (LogLevel > 11) 3037943Seric syslog(LOG_DEBUG, "runqueue %s, pid=%d", QueueDir, getpid()); 3047876Seric # endif LOG 3054639Seric 3067466Seric /* 30710205Seric ** Release any resources used by the daemon code. 30810205Seric */ 30910205Seric 31010205Seric # ifdef DAEMON 31110205Seric clrdaemon(); 31210205Seric # endif DAEMON 31310205Seric 31410205Seric /* 3157466Seric ** Start making passes through the queue. 3167466Seric ** First, read and sort the entire queue. 3177466Seric ** Then, process the work in that order. 3187466Seric ** But if you take too long, start over. 3197466Seric */ 3207466Seric 3217943Seric /* order the existing work requests */ 322*24954Seric (void) orderq(FALSE); 3237690Seric 3247943Seric /* process them once at a time */ 3257943Seric while (WorkQ != NULL) 3264639Seric { 3277943Seric WORK *w = WorkQ; 3287881Seric 3297943Seric WorkQ = WorkQ->w_next; 3307943Seric dowork(w); 3317943Seric free(w->w_name); 3327943Seric free((char *) w); 3334639Seric } 3347943Seric finis(); 3354634Seric } 3364634Seric /* 3374632Seric ** ORDERQ -- order the work queue. 3384632Seric ** 3394632Seric ** Parameters: 34024941Seric ** doall -- if set, include everything in the queue (even 34124941Seric ** the jobs that cannot be run because the load 34224941Seric ** average is too high). Otherwise, exclude those 34324941Seric ** jobs. 3444632Seric ** 3454632Seric ** Returns: 34610121Seric ** The number of request in the queue (not necessarily 34710121Seric ** the number of requests in WorkQ however). 3484632Seric ** 3494632Seric ** Side Effects: 3504632Seric ** Sets WorkQ to the queue of available work, in order. 3514632Seric */ 3524632Seric 3534632Seric # define WLSIZE 120 /* max size of worklist per sort */ 3544632Seric 35524941Seric orderq(doall) 35624941Seric bool doall; 3574632Seric { 3586625Sglickman register struct direct *d; 3594632Seric register WORK *w; 3604632Seric register WORK **wp; /* parent of w */ 3616625Sglickman DIR *f; 3624632Seric register int i; 36310121Seric WORK wlist[WLSIZE+1]; 36410070Seric int wn = -1; 3654632Seric extern workcmpf(); 3664632Seric 3674632Seric /* clear out old WorkQ */ 3684632Seric for (w = WorkQ; w != NULL; ) 3694632Seric { 3704632Seric register WORK *nw = w->w_next; 3714632Seric 3724632Seric WorkQ = nw; 3734632Seric free(w->w_name); 3744632Seric free((char *) w); 3754632Seric w = nw; 3764632Seric } 3774632Seric 3784632Seric /* open the queue directory */ 3798148Seric f = opendir("."); 3804632Seric if (f == NULL) 3814632Seric { 3828148Seric syserr("orderq: cannot open \"%s\" as \".\"", QueueDir); 38310070Seric return (0); 3844632Seric } 3854632Seric 3864632Seric /* 3874632Seric ** Read the work directory. 3884632Seric */ 3894632Seric 39010070Seric while ((d = readdir(f)) != NULL) 3914632Seric { 3929377Seric FILE *cf; 3934632Seric char lbuf[MAXNAME]; 3944632Seric 3954632Seric /* is this an interesting entry? */ 3967812Seric if (d->d_name[0] != 'q' || d->d_name[1] != 'f') 3974632Seric continue; 3984632Seric 39910070Seric /* yes -- open control file (if not too many files) */ 40010070Seric if (++wn >= WLSIZE) 40110070Seric continue; 4028148Seric cf = fopen(d->d_name, "r"); 4034632Seric if (cf == NULL) 4044632Seric { 4057055Seric /* this may be some random person sending hir msgs */ 4067055Seric /* syserr("orderq: cannot open %s", cbuf); */ 40710090Seric #ifdef DEBUG 40810090Seric if (tTd(41, 2)) 40910090Seric printf("orderq: cannot open %s (%d)\n", 41010090Seric d->d_name, errno); 41110090Seric #endif DEBUG 4127055Seric errno = 0; 41310090Seric wn--; 4144632Seric continue; 4154632Seric } 4168148Seric wlist[wn].w_name = newstr(d->d_name); 4174632Seric 4184632Seric /* extract useful information */ 4194632Seric while (fgets(lbuf, sizeof lbuf, cf) != NULL) 4204632Seric { 421*24954Seric extern long atol(); 422*24954Seric 42324941Seric switch (lbuf[0]) 4244632Seric { 42524941Seric case 'P': 42624941Seric wlist[wn].w_pri = atol(&lbuf[1]); 4274632Seric break; 42824941Seric 42924941Seric case 'T': 43024941Seric wlist[wn].w_ctime = atol(&lbuf[1]); 43124941Seric break; 4324632Seric } 4334632Seric } 4344632Seric (void) fclose(cf); 43524953Seric 43624953Seric if (!doall && shouldqueue(wlist[wn].w_pri)) 43724953Seric { 43824953Seric /* don't even bother sorting this job in */ 43924953Seric wn--; 44024953Seric } 4414632Seric } 4426625Sglickman (void) closedir(f); 44310090Seric wn++; 4444632Seric 4454632Seric /* 4464632Seric ** Sort the work directory. 4474632Seric */ 4484632Seric 44923116Seric qsort((char *) wlist, min(wn, WLSIZE), sizeof *wlist, workcmpf); 4504632Seric 4514632Seric /* 4524632Seric ** Convert the work list into canonical form. 4539377Seric ** Should be turning it into a list of envelopes here perhaps. 4544632Seric */ 4554632Seric 4564632Seric wp = &WorkQ; 45710121Seric for (i = min(wn, WLSIZE); --i >= 0; ) 4584632Seric { 4594632Seric w = (WORK *) xalloc(sizeof *w); 4604632Seric w->w_name = wlist[i].w_name; 4614632Seric w->w_pri = wlist[i].w_pri; 46224941Seric w->w_ctime = wlist[i].w_ctime; 4634632Seric w->w_next = NULL; 4644632Seric *wp = w; 4654632Seric wp = &w->w_next; 4664632Seric } 4674632Seric 4684632Seric # ifdef DEBUG 4697677Seric if (tTd(40, 1)) 4704632Seric { 4714632Seric for (w = WorkQ; w != NULL; w = w->w_next) 4725037Seric printf("%32s: pri=%ld\n", w->w_name, w->w_pri); 4734632Seric } 4744632Seric # endif DEBUG 47510070Seric 47610090Seric return (wn); 4774632Seric } 4784632Seric /* 4797677Seric ** WORKCMPF -- compare function for ordering work. 4804632Seric ** 4814632Seric ** Parameters: 4824632Seric ** a -- the first argument. 4834632Seric ** b -- the second argument. 4844632Seric ** 4854632Seric ** Returns: 48610121Seric ** 1 if a < b 4874632Seric ** 0 if a == b 48810121Seric ** -1 if a > b 4894632Seric ** 4904632Seric ** Side Effects: 4914632Seric ** none. 4924632Seric */ 4934632Seric 4944632Seric workcmpf(a, b) 4955037Seric register WORK *a; 4965037Seric register WORK *b; 4974632Seric { 49824941Seric long pa = a->w_pri + a->w_ctime; 49924941Seric long pb = b->w_pri + b->w_ctime; 50024941Seric 50124941Seric if (pa == pb) 5024632Seric return (0); 50324941Seric else if (pa > pb) 50410121Seric return (-1); 50510121Seric else 5064632Seric return (1); 5074632Seric } 5084632Seric /* 5094632Seric ** DOWORK -- do a work request. 5104632Seric ** 5114632Seric ** Parameters: 5124632Seric ** w -- the work request to be satisfied. 5134632Seric ** 5144632Seric ** Returns: 5154632Seric ** none. 5164632Seric ** 5174632Seric ** Side Effects: 5184632Seric ** The work request is satisfied if possible. 5194632Seric */ 5204632Seric 5214632Seric dowork(w) 5224632Seric register WORK *w; 5234632Seric { 5244632Seric register int i; 52524941Seric extern bool shouldqueue(); 5264632Seric 5274632Seric # ifdef DEBUG 5287677Seric if (tTd(40, 1)) 5295037Seric printf("dowork: %s pri %ld\n", w->w_name, w->w_pri); 5304632Seric # endif DEBUG 5314632Seric 5324632Seric /* 53324941Seric ** Ignore jobs that are too expensive for the moment. 5344632Seric */ 5354632Seric 53624941Seric if (shouldqueue(w->w_pri)) 5374632Seric { 53824941Seric if (Verbose) 53924941Seric printf("\nSkipping %s\n", w->w_name); 5404632Seric return; 5414632Seric } 5424632Seric 54324941Seric /* 54424941Seric ** Fork for work. 54524941Seric */ 54624941Seric 54724941Seric if (ForkQueueRuns) 54824941Seric { 54924941Seric i = fork(); 55024941Seric if (i < 0) 55124941Seric { 55224941Seric syserr("dowork: cannot fork"); 55324941Seric return; 55424941Seric } 55524941Seric } 55624941Seric else 55724941Seric { 55824941Seric i = 0; 55924941Seric } 56024941Seric 5614632Seric if (i == 0) 5624632Seric { 5634632Seric /* 5644632Seric ** CHILD 5658148Seric ** Lock the control file to avoid duplicate deliveries. 5668148Seric ** Then run the file as though we had just read it. 5677350Seric ** We save an idea of the temporary name so we 5687350Seric ** can recover on interrupt. 5694632Seric */ 5704632Seric 5717763Seric /* set basic modes, etc. */ 5727356Seric (void) alarm(0); 57310195Seric closexscript(CurEnv); 5749338Seric CurEnv->e_flags &= ~EF_FATALERRS; 5754632Seric QueueRun = TRUE; 5769377Seric ErrorMode = EM_MAIL; 5778148Seric CurEnv->e_id = &w->w_name[2]; 5787876Seric # ifdef LOG 5797876Seric if (LogLevel > 11) 5807881Seric syslog(LOG_DEBUG, "%s: dowork, pid=%d", CurEnv->e_id, 5817881Seric getpid()); 5827876Seric # endif LOG 5837763Seric 5847763Seric /* don't use the headers from sendmail.cf... */ 5857763Seric CurEnv->e_header = NULL; 5867763Seric 58717468Seric /* lock the control file during processing */ 5887812Seric if (link(w->w_name, queuename(CurEnv, 'l')) < 0) 5896980Seric { 5907812Seric /* being processed by another queuer */ 5917881Seric # ifdef LOG 5927881Seric if (LogLevel > 4) 5937881Seric syslog(LOG_DEBUG, "%s: locked", CurEnv->e_id); 5947881Seric # endif LOG 59524941Seric if (ForkQueueRuns) 59624941Seric exit(EX_OK); 59724941Seric else 59824941Seric return; 5996980Seric } 6006980Seric 6016980Seric /* do basic system initialization */ 6024632Seric initsys(); 6036980Seric 6046980Seric /* read the queue control file */ 60517477Seric readqf(CurEnv, TRUE); 6069338Seric CurEnv->e_flags |= EF_INQUEUE; 6079377Seric eatheader(CurEnv); 6086980Seric 6096980Seric /* do the delivery */ 6109338Seric if (!bitset(EF_FATALERRS, CurEnv->e_flags)) 6119282Seric sendall(CurEnv, SM_DELIVER); 6126980Seric 6136980Seric /* finish up and exit */ 61424941Seric if (ForkQueueRuns) 61524941Seric finis(); 61624941Seric else 61724941Seric dropenvelope(CurEnv); 6184632Seric } 61924941Seric else 62024941Seric { 62124941Seric /* 62224941Seric ** Parent -- pick up results. 62324941Seric */ 6244632Seric 62524941Seric errno = 0; 62624941Seric (void) waitfor(i); 62724941Seric } 6284632Seric } 6294632Seric /* 6304632Seric ** READQF -- read queue file and set up environment. 6314632Seric ** 6324632Seric ** Parameters: 6339377Seric ** e -- the envelope of the job to run. 6349630Seric ** full -- if set, read in all information. Otherwise just 6359630Seric ** read in info needed for a queue print. 6364632Seric ** 6374632Seric ** Returns: 6384632Seric ** none. 6394632Seric ** 6404632Seric ** Side Effects: 6414632Seric ** cf is read and created as the current job, as though 6424632Seric ** we had been invoked by argument. 6434632Seric */ 6444632Seric 64517477Seric readqf(e, full) 6469377Seric register ENVELOPE *e; 6479630Seric bool full; 6484632Seric { 64917477Seric char *qf; 65017477Seric register FILE *qfp; 6517785Seric char buf[MAXFIELD]; 6529348Seric extern char *fgetfolded(); 653*24954Seric extern long atol(); 6544632Seric 6554632Seric /* 65617468Seric ** Read and process the file. 6574632Seric */ 6584632Seric 65917477Seric qf = queuename(e, 'q'); 66017477Seric qfp = fopen(qf, "r"); 66117477Seric if (qfp == NULL) 66217477Seric { 66317477Seric syserr("readqf: no control file %s", qf); 66417477Seric return; 66517477Seric } 66617477Seric FileName = qf; 6679377Seric LineNumber = 0; 6689630Seric if (Verbose && full) 6699377Seric printf("\nRunning %s\n", e->e_id); 67017468Seric while (fgetfolded(buf, sizeof buf, qfp) != NULL) 6714632Seric { 6724632Seric switch (buf[0]) 6734632Seric { 6744632Seric case 'R': /* specify recipient */ 6759618Seric sendtolist(&buf[1], (ADDRESS *) NULL, &e->e_sendqueue); 6764632Seric break; 6774632Seric 6784632Seric case 'H': /* header */ 6799630Seric if (full) 6809630Seric (void) chompheader(&buf[1], FALSE); 6814632Seric break; 6824632Seric 68310108Seric case 'M': /* message */ 68410108Seric e->e_message = newstr(&buf[1]); 68510108Seric break; 68610108Seric 6874632Seric case 'S': /* sender */ 6884634Seric setsender(newstr(&buf[1])); 6894632Seric break; 6904632Seric 6914632Seric case 'D': /* data file name */ 6929630Seric if (!full) 6939630Seric break; 6949377Seric e->e_df = newstr(&buf[1]); 6959544Seric e->e_dfp = fopen(e->e_df, "r"); 6969544Seric if (e->e_dfp == NULL) 6979377Seric syserr("readqf: cannot open %s", e->e_df); 6984632Seric break; 6994632Seric 7007860Seric case 'T': /* init time */ 70124941Seric e->e_ctime = atol(&buf[1]); 7024632Seric break; 7034632Seric 7044634Seric case 'P': /* message priority */ 70524941Seric e->e_msgpriority = atol(&buf[1]); 7065037Seric 7075037Seric /* make sure that big things get sent eventually */ 7089377Seric e->e_msgpriority -= WKTIMEFACT; 7094634Seric break; 7104634Seric 71124941Seric case '\0': /* blank line; ignore */ 71224941Seric break; 71324941Seric 7144632Seric default: 71524941Seric syserr("readqf(%s:%d): bad line \"%s\"", e->e_id, 71624941Seric LineNumber, buf); 7174632Seric break; 7184632Seric } 7194632Seric } 7209377Seric 721*24954Seric (void) fclose(qfp); 7229377Seric FileName = NULL; 72324941Seric 72424941Seric /* 72524941Seric ** If we haven't read any lines, this queue file is empty. 72624941Seric ** Arrange to remove it without referencing any null pointers. 72724941Seric */ 72824941Seric 72924941Seric if (LineNumber == 0) 73024941Seric { 73124941Seric errno = 0; 73224941Seric e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE; 73324941Seric } 7344632Seric } 7354632Seric /* 7369630Seric ** PRINTQUEUE -- print out a representation of the mail queue 7379630Seric ** 7389630Seric ** Parameters: 7399630Seric ** none. 7409630Seric ** 7419630Seric ** Returns: 7429630Seric ** none. 7439630Seric ** 7449630Seric ** Side Effects: 7459630Seric ** Prints a listing of the mail queue on the standard output. 7469630Seric */ 7475182Seric 7489630Seric printqueue() 7499630Seric { 7509630Seric register WORK *w; 7519630Seric FILE *f; 75210070Seric int nrequests; 7539630Seric char buf[MAXLINE]; 7549630Seric 7559630Seric /* 7569630Seric ** Read and order the queue. 7579630Seric */ 7589630Seric 75924941Seric nrequests = orderq(TRUE); 7609630Seric 7619630Seric /* 7629630Seric ** Print the work list that we have read. 7639630Seric */ 7649630Seric 7659630Seric /* first see if there is anything */ 76610070Seric if (nrequests <= 0) 7679630Seric { 76810070Seric printf("Mail queue is empty\n"); 7699630Seric return; 7709630Seric } 7719630Seric 77210096Seric printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s"); 77310070Seric if (nrequests > WLSIZE) 77410070Seric printf(", only %d printed", WLSIZE); 77510070Seric printf(")\n--QID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n"); 7769630Seric for (w = WorkQ; w != NULL; w = w->w_next) 7779630Seric { 7789630Seric struct stat st; 77910070Seric auto time_t submittime = 0; 78010070Seric long dfsize = -1; 78110108Seric char lf[20]; 78210108Seric char message[MAXLINE]; 78324941Seric extern bool shouldqueue(); 7849630Seric 78517468Seric f = fopen(w->w_name, "r"); 78617468Seric if (f == NULL) 78717468Seric { 78817468Seric errno = 0; 78917468Seric continue; 79017468Seric } 7919630Seric printf("%7s", w->w_name + 2); 79223098Seric (void) strcpy(lf, w->w_name); 79310070Seric lf[0] = 'l'; 79410070Seric if (stat(lf, &st) >= 0) 79510070Seric printf("*"); 79624941Seric else if (shouldqueue(w->w_pri)) 79724941Seric printf("X"); 79810070Seric else 79910070Seric printf(" "); 80010070Seric errno = 0; 80117468Seric 80210108Seric message[0] = '\0'; 8039630Seric while (fgets(buf, sizeof buf, f) != NULL) 8049630Seric { 8059630Seric fixcrlf(buf, TRUE); 8069630Seric switch (buf[0]) 8079630Seric { 80810108Seric case 'M': /* error message */ 80923098Seric (void) strcpy(message, &buf[1]); 81010108Seric break; 81110108Seric 8129630Seric case 'S': /* sender name */ 81313015Seric printf("%8ld %.16s %.45s", dfsize, 81412517Seric ctime(&submittime), &buf[1]); 81510108Seric if (message[0] != '\0') 81624941Seric printf("\n\t\t (%.62s)", message); 8179630Seric break; 8189630Seric 8199630Seric case 'R': /* recipient name */ 82012517Seric printf("\n\t\t\t\t %.45s", &buf[1]); 8219630Seric break; 8229630Seric 8239630Seric case 'T': /* creation time */ 82424941Seric submittime = atol(&buf[1]); 8259630Seric break; 82610070Seric 82710070Seric case 'D': /* data file name */ 82810070Seric if (stat(&buf[1], &st) >= 0) 82910070Seric dfsize = st.st_size; 83010070Seric break; 8319630Seric } 8329630Seric } 83310070Seric if (submittime == (time_t) 0) 83410070Seric printf(" (no control file)"); 8359630Seric printf("\n"); 83623098Seric (void) fclose(f); 8379630Seric } 8389630Seric } 8399630Seric 8405182Seric # endif QUEUE 84117468Seric /* 84217468Seric ** QUEUENAME -- build a file name in the queue directory for this envelope. 84317468Seric ** 84417468Seric ** Assigns an id code if one does not already exist. 84517468Seric ** This code is very careful to avoid trashing existing files 84617468Seric ** under any circumstances. 84717468Seric ** We first create an nf file that is only used when 84817468Seric ** assigning an id. This file is always empty, so that 84917468Seric ** we can never accidently truncate an lf file. 85017468Seric ** 85117468Seric ** Parameters: 85217468Seric ** e -- envelope to build it in/from. 85317468Seric ** type -- the file type, used as the first character 85417468Seric ** of the file name. 85517468Seric ** 85617468Seric ** Returns: 85717468Seric ** a pointer to the new file name (in a static buffer). 85817468Seric ** 85917468Seric ** Side Effects: 86017468Seric ** Will create the lf and qf files if no id code is 86117468Seric ** already assigned. This will cause the envelope 86217468Seric ** to be modified. 86317468Seric */ 86417468Seric 86517468Seric char * 86617468Seric queuename(e, type) 86717468Seric register ENVELOPE *e; 86817468Seric char type; 86917468Seric { 87017468Seric static char buf[MAXNAME]; 87117468Seric static int pid = -1; 87217468Seric char c1 = 'A'; 87317468Seric char c2 = 'A'; 87417468Seric 87517468Seric if (e->e_id == NULL) 87617468Seric { 87717468Seric char qf[20]; 87817468Seric char nf[20]; 87917468Seric char lf[20]; 88017468Seric 88117468Seric /* find a unique id */ 88217468Seric if (pid != getpid()) 88317468Seric { 88417468Seric /* new process -- start back at "AA" */ 88517468Seric pid = getpid(); 88617468Seric c1 = 'A'; 88717468Seric c2 = 'A' - 1; 88817468Seric } 88917468Seric (void) sprintf(qf, "qfAA%05d", pid); 89023098Seric (void) strcpy(lf, qf); 89117468Seric lf[0] = 'l'; 89223098Seric (void) strcpy(nf, qf); 89317468Seric nf[0] = 'n'; 89417468Seric 89517468Seric while (c1 < '~' || c2 < 'Z') 89617468Seric { 89717468Seric int i; 89817468Seric 89917468Seric if (c2 >= 'Z') 90017468Seric { 90117468Seric c1++; 90217468Seric c2 = 'A' - 1; 90317468Seric } 90417477Seric lf[2] = nf[2] = qf[2] = c1; 90517477Seric lf[3] = nf[3] = qf[3] = ++c2; 90617468Seric # ifdef DEBUG 90717468Seric if (tTd(7, 20)) 90817468Seric printf("queuename: trying \"%s\"\n", nf); 90917468Seric # endif DEBUG 91017468Seric 91117468Seric # ifdef QUEUE 91217468Seric if (access(lf, 0) >= 0 || access(qf, 0) >= 0) 91317468Seric continue; 91417468Seric errno = 0; 91517468Seric i = creat(nf, FileMode); 91617468Seric if (i < 0) 91717468Seric { 91817468Seric (void) unlink(nf); /* kernel bug */ 91917468Seric continue; 92017468Seric } 92117468Seric (void) close(i); 92217468Seric i = link(nf, lf); 92317468Seric (void) unlink(nf); 92417468Seric if (i < 0) 92517468Seric continue; 92617468Seric if (link(lf, qf) >= 0) 92717468Seric break; 92817468Seric (void) unlink(lf); 92917468Seric # else QUEUE 93017982Seric if (close(creat(qf, FileMode)) >= 0) 93117982Seric break; 93217468Seric # endif QUEUE 93317468Seric } 93417468Seric if (c1 >= '~' && c2 >= 'Z') 93517468Seric { 93617468Seric syserr("queuename: Cannot create \"%s\" in \"%s\"", 93717468Seric qf, QueueDir); 93817468Seric exit(EX_OSERR); 93917468Seric } 94017468Seric e->e_id = newstr(&qf[2]); 94117468Seric define('i', e->e_id, e); 94217468Seric # ifdef DEBUG 94317468Seric if (tTd(7, 1)) 94417468Seric printf("queuename: assigned id %s, env=%x\n", e->e_id, e); 94517468Seric # ifdef LOG 94617468Seric if (LogLevel > 16) 94717468Seric syslog(LOG_DEBUG, "%s: assigned id", e->e_id); 94817468Seric # endif LOG 94917468Seric # endif DEBUG 95017468Seric } 95117468Seric 95217468Seric if (type == '\0') 95317468Seric return (NULL); 95417468Seric (void) sprintf(buf, "%cf%s", type, e->e_id); 95517468Seric # ifdef DEBUG 95617468Seric if (tTd(7, 2)) 95717468Seric printf("queuename: %s\n", buf); 95817468Seric # endif DEBUG 95917468Seric return (buf); 96017468Seric } 96117468Seric /* 96217468Seric ** UNLOCKQUEUE -- unlock the queue entry for a specified envelope 96317468Seric ** 96417468Seric ** Parameters: 96517468Seric ** e -- the envelope to unlock. 96617468Seric ** 96717468Seric ** Returns: 96817468Seric ** none 96917468Seric ** 97017468Seric ** Side Effects: 97117468Seric ** unlocks the queue for `e'. 97217468Seric */ 97317468Seric 97417468Seric unlockqueue(e) 97517468Seric ENVELOPE *e; 97617468Seric { 97717468Seric /* remove the transcript */ 97817468Seric #ifdef DEBUG 97917468Seric # ifdef LOG 98017468Seric if (LogLevel > 19) 98117468Seric syslog(LOG_DEBUG, "%s: unlock", e->e_id); 98217468Seric # endif LOG 98317468Seric if (!tTd(51, 4)) 98417468Seric #endif DEBUG 98517468Seric xunlink(queuename(e, 'x')); 98617468Seric 98717468Seric # ifdef QUEUE 98817468Seric /* last but not least, remove the lock */ 98917468Seric xunlink(queuename(e, 'l')); 99017468Seric # endif QUEUE 99117468Seric } 992