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*25184Seric static char SccsId[] = "@(#)queue.c 5.17 (Berkeley) 10/13/85 (no queueing)"; 2123116Seric # endif not lint 225182Seric # else QUEUE 234632Seric 2423116Seric # ifndef lint 25*25184Seric static char SccsId[] = "@(#)queue.c 5.17 (Berkeley) 10/13/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 */ 3625013Seric time_t 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 (void) unlink(qf); 22524968Seric if (rename(tf, qf) < 0) 22624968Seric syserr("cannot unlink(%s, %s), df=%s", tf, qf, e->e_df); 22724968Seric errno = 0; 22817468Seric } 2297391Seric 2307677Seric # ifdef LOG 2317677Seric /* save log info */ 2327878Seric if (LogLevel > 15) 2337878Seric syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df); 2347677Seric # endif LOG 2354632Seric } 2364632Seric /* 2374632Seric ** RUNQUEUE -- run the jobs in the queue. 2384632Seric ** 2394632Seric ** Gets the stuff out of the queue in some presumably logical 2404632Seric ** order and processes them. 2414632Seric ** 2424632Seric ** Parameters: 24324941Seric ** forkflag -- TRUE if the queue scanning should be done in 24424941Seric ** a child process. We double-fork so it is not our 24524941Seric ** child and we don't have to clean up after it. 2464632Seric ** 2474632Seric ** Returns: 2484632Seric ** none. 2494632Seric ** 2504632Seric ** Side Effects: 2514632Seric ** runs things in the mail queue. 2524632Seric */ 2534632Seric 2544639Seric runqueue(forkflag) 2554639Seric bool forkflag; 2564632Seric { 25724953Seric extern bool shouldqueue(); 25824953Seric 2597466Seric /* 26024953Seric ** If no work will ever be selected, don't even bother reading 26124953Seric ** the queue. 26224953Seric */ 26324953Seric 26424953Seric if (shouldqueue(-100000000L)) 26524953Seric { 26624953Seric if (Verbose) 26724953Seric printf("Skipping queue run -- load average too high\n"); 26824953Seric 26924953Seric if (forkflag) 27024953Seric return; 27124953Seric finis(); 27224953Seric } 27324953Seric 27424953Seric /* 2757466Seric ** See if we want to go off and do other useful work. 2767466Seric */ 2774639Seric 2784639Seric if (forkflag) 2794639Seric { 2807943Seric int pid; 2817943Seric 2827943Seric pid = dofork(); 2837943Seric if (pid != 0) 2844639Seric { 285*25184Seric extern reapchild(); 286*25184Seric 2877943Seric /* parent -- pick up intermediate zombie */ 288*25184Seric #ifndef SIGCHLD 2899377Seric (void) waitfor(pid); 290*25184Seric #else SIGCHLD 291*25184Seric (void) signal(SIGCHLD, reapchild); 292*25184Seric #endif SIGCHLD 2937690Seric if (QueueIntvl != 0) 2949348Seric (void) setevent(QueueIntvl, runqueue, TRUE); 2954639Seric return; 2964639Seric } 2977943Seric /* child -- double fork */ 298*25184Seric #ifndef SIGCHLD 2997943Seric if (fork() != 0) 3007943Seric exit(EX_OK); 301*25184Seric #else SIGCHLD 302*25184Seric (void) signal(SIGCHLD, SIG_DFL); 303*25184Seric #endif SIGCHLD 3044639Seric } 30524941Seric 30624941Seric setproctitle("running queue"); 30724941Seric 3087876Seric # ifdef LOG 3097876Seric if (LogLevel > 11) 3107943Seric syslog(LOG_DEBUG, "runqueue %s, pid=%d", QueueDir, getpid()); 3117876Seric # endif LOG 3124639Seric 3137466Seric /* 31410205Seric ** Release any resources used by the daemon code. 31510205Seric */ 31610205Seric 31710205Seric # ifdef DAEMON 31810205Seric clrdaemon(); 31910205Seric # endif DAEMON 32010205Seric 32110205Seric /* 3227466Seric ** Start making passes through the queue. 3237466Seric ** First, read and sort the entire queue. 3247466Seric ** Then, process the work in that order. 3257466Seric ** But if you take too long, start over. 3267466Seric */ 3277466Seric 3287943Seric /* order the existing work requests */ 32924954Seric (void) orderq(FALSE); 3307690Seric 3317943Seric /* process them once at a time */ 3327943Seric while (WorkQ != NULL) 3334639Seric { 3347943Seric WORK *w = WorkQ; 3357881Seric 3367943Seric WorkQ = WorkQ->w_next; 3377943Seric dowork(w); 3387943Seric free(w->w_name); 3397943Seric free((char *) w); 3404639Seric } 3417943Seric finis(); 3424634Seric } 3434634Seric /* 3444632Seric ** ORDERQ -- order the work queue. 3454632Seric ** 3464632Seric ** Parameters: 34724941Seric ** doall -- if set, include everything in the queue (even 34824941Seric ** the jobs that cannot be run because the load 34924941Seric ** average is too high). Otherwise, exclude those 35024941Seric ** jobs. 3514632Seric ** 3524632Seric ** Returns: 35310121Seric ** The number of request in the queue (not necessarily 35410121Seric ** the number of requests in WorkQ however). 3554632Seric ** 3564632Seric ** Side Effects: 3574632Seric ** Sets WorkQ to the queue of available work, in order. 3584632Seric */ 3594632Seric 3604632Seric # define WLSIZE 120 /* max size of worklist per sort */ 3614632Seric 36224941Seric orderq(doall) 36324941Seric bool doall; 3644632Seric { 3656625Sglickman register struct direct *d; 3664632Seric register WORK *w; 3676625Sglickman DIR *f; 3684632Seric register int i; 36910121Seric WORK wlist[WLSIZE+1]; 37010070Seric int wn = -1; 3714632Seric extern workcmpf(); 3724632Seric 3734632Seric /* clear out old WorkQ */ 3744632Seric for (w = WorkQ; w != NULL; ) 3754632Seric { 3764632Seric register WORK *nw = w->w_next; 3774632Seric 3784632Seric WorkQ = nw; 3794632Seric free(w->w_name); 3804632Seric free((char *) w); 3814632Seric w = nw; 3824632Seric } 3834632Seric 3844632Seric /* open the queue directory */ 3858148Seric f = opendir("."); 3864632Seric if (f == NULL) 3874632Seric { 3888148Seric syserr("orderq: cannot open \"%s\" as \".\"", QueueDir); 38910070Seric return (0); 3904632Seric } 3914632Seric 3924632Seric /* 3934632Seric ** Read the work directory. 3944632Seric */ 3954632Seric 39610070Seric while ((d = readdir(f)) != NULL) 3974632Seric { 3989377Seric FILE *cf; 3994632Seric char lbuf[MAXNAME]; 4004632Seric 4014632Seric /* is this an interesting entry? */ 4027812Seric if (d->d_name[0] != 'q' || d->d_name[1] != 'f') 4034632Seric continue; 4044632Seric 40510070Seric /* yes -- open control file (if not too many files) */ 40610070Seric if (++wn >= WLSIZE) 40710070Seric continue; 4088148Seric cf = fopen(d->d_name, "r"); 4094632Seric if (cf == NULL) 4104632Seric { 4117055Seric /* this may be some random person sending hir msgs */ 4127055Seric /* syserr("orderq: cannot open %s", cbuf); */ 41310090Seric #ifdef DEBUG 41410090Seric if (tTd(41, 2)) 41510090Seric printf("orderq: cannot open %s (%d)\n", 41610090Seric d->d_name, errno); 41710090Seric #endif DEBUG 4187055Seric errno = 0; 41910090Seric wn--; 4204632Seric continue; 4214632Seric } 4228148Seric wlist[wn].w_name = newstr(d->d_name); 4234632Seric 42425027Seric /* make sure jobs in creation don't clog queue */ 42525027Seric wlist[wn].w_pri = 0x7fffffff; 42625027Seric wlist[wn].w_ctime = 0; 42725027Seric 4284632Seric /* extract useful information */ 4294632Seric while (fgets(lbuf, sizeof lbuf, cf) != NULL) 4304632Seric { 43124954Seric extern long atol(); 43224954Seric 43324941Seric switch (lbuf[0]) 4344632Seric { 43524941Seric case 'P': 43624941Seric wlist[wn].w_pri = atol(&lbuf[1]); 4374632Seric break; 43825013Seric 43925013Seric case 'T': 44025013Seric wlist[wn].w_ctime = atol(&lbuf[1]); 44125013Seric break; 4424632Seric } 4434632Seric } 4444632Seric (void) fclose(cf); 44524953Seric 44624953Seric if (!doall && shouldqueue(wlist[wn].w_pri)) 44724953Seric { 44824953Seric /* don't even bother sorting this job in */ 44924953Seric wn--; 45024953Seric } 4514632Seric } 4526625Sglickman (void) closedir(f); 45310090Seric wn++; 4544632Seric 4554632Seric /* 4564632Seric ** Sort the work directory. 4574632Seric */ 4584632Seric 45923116Seric qsort((char *) wlist, min(wn, WLSIZE), sizeof *wlist, workcmpf); 4604632Seric 4614632Seric /* 4624632Seric ** Convert the work list into canonical form. 4639377Seric ** Should be turning it into a list of envelopes here perhaps. 4644632Seric */ 4654632Seric 46624981Seric WorkQ = NULL; 46710121Seric for (i = min(wn, WLSIZE); --i >= 0; ) 4684632Seric { 4694632Seric w = (WORK *) xalloc(sizeof *w); 4704632Seric w->w_name = wlist[i].w_name; 4714632Seric w->w_pri = wlist[i].w_pri; 47225013Seric w->w_ctime = wlist[i].w_ctime; 47324981Seric w->w_next = WorkQ; 47424981Seric WorkQ = w; 4754632Seric } 4764632Seric 4774632Seric # ifdef DEBUG 4787677Seric if (tTd(40, 1)) 4794632Seric { 4804632Seric for (w = WorkQ; w != NULL; w = w->w_next) 4815037Seric printf("%32s: pri=%ld\n", w->w_name, w->w_pri); 4824632Seric } 4834632Seric # endif DEBUG 48410070Seric 48510090Seric return (wn); 4864632Seric } 4874632Seric /* 4887677Seric ** WORKCMPF -- compare function for ordering work. 4894632Seric ** 4904632Seric ** Parameters: 4914632Seric ** a -- the first argument. 4924632Seric ** b -- the second argument. 4934632Seric ** 4944632Seric ** Returns: 49524981Seric ** -1 if a < b 49624981Seric ** 0 if a == b 49724981Seric ** +1 if a > b 4984632Seric ** 4994632Seric ** Side Effects: 5004632Seric ** none. 5014632Seric */ 5024632Seric 5034632Seric workcmpf(a, b) 5045037Seric register WORK *a; 5055037Seric register WORK *b; 5064632Seric { 50725013Seric long pa = a->w_pri + a->w_ctime; 50825013Seric long pb = b->w_pri + b->w_ctime; 50924941Seric 51024941Seric if (pa == pb) 5114632Seric return (0); 51224941Seric else if (pa > pb) 51324981Seric return (1); 51424981Seric else 51510121Seric return (-1); 5164632Seric } 5174632Seric /* 5184632Seric ** DOWORK -- do a work request. 5194632Seric ** 5204632Seric ** Parameters: 5214632Seric ** w -- the work request to be satisfied. 5224632Seric ** 5234632Seric ** Returns: 5244632Seric ** none. 5254632Seric ** 5264632Seric ** Side Effects: 5274632Seric ** The work request is satisfied if possible. 5284632Seric */ 5294632Seric 5304632Seric dowork(w) 5314632Seric register WORK *w; 5324632Seric { 5334632Seric register int i; 53424941Seric extern bool shouldqueue(); 5354632Seric 5364632Seric # ifdef DEBUG 5377677Seric if (tTd(40, 1)) 5385037Seric printf("dowork: %s pri %ld\n", w->w_name, w->w_pri); 5394632Seric # endif DEBUG 5404632Seric 5414632Seric /* 54224941Seric ** Ignore jobs that are too expensive for the moment. 5434632Seric */ 5444632Seric 54524941Seric if (shouldqueue(w->w_pri)) 5464632Seric { 54724941Seric if (Verbose) 54824967Seric printf("\nSkipping %s\n", w->w_name + 2); 5494632Seric return; 5504632Seric } 5514632Seric 55224941Seric /* 55324941Seric ** Fork for work. 55424941Seric */ 55524941Seric 55624941Seric if (ForkQueueRuns) 55724941Seric { 55824941Seric i = fork(); 55924941Seric if (i < 0) 56024941Seric { 56124941Seric syserr("dowork: cannot fork"); 56224941Seric return; 56324941Seric } 56424941Seric } 56524941Seric else 56624941Seric { 56724941Seric i = 0; 56824941Seric } 56924941Seric 5704632Seric if (i == 0) 5714632Seric { 5724632Seric /* 5734632Seric ** CHILD 5748148Seric ** Lock the control file to avoid duplicate deliveries. 5758148Seric ** Then run the file as though we had just read it. 5767350Seric ** We save an idea of the temporary name so we 5777350Seric ** can recover on interrupt. 5784632Seric */ 5794632Seric 5807763Seric /* set basic modes, etc. */ 5817356Seric (void) alarm(0); 58224961Seric clearenvelope(CurEnv); 5834632Seric QueueRun = TRUE; 5849377Seric ErrorMode = EM_MAIL; 5858148Seric CurEnv->e_id = &w->w_name[2]; 5867876Seric # ifdef LOG 5877876Seric if (LogLevel > 11) 5887881Seric syslog(LOG_DEBUG, "%s: dowork, pid=%d", CurEnv->e_id, 5897881Seric getpid()); 5907876Seric # endif LOG 5917763Seric 5927763Seric /* don't use the headers from sendmail.cf... */ 5937763Seric CurEnv->e_header = NULL; 5947763Seric 59517468Seric /* lock the control file during processing */ 5967812Seric if (link(w->w_name, queuename(CurEnv, 'l')) < 0) 5976980Seric { 5987812Seric /* being processed by another queuer */ 5997881Seric # ifdef LOG 6007881Seric if (LogLevel > 4) 6017881Seric syslog(LOG_DEBUG, "%s: locked", CurEnv->e_id); 6027881Seric # endif LOG 60324941Seric if (ForkQueueRuns) 60424941Seric exit(EX_OK); 60524941Seric else 60624941Seric return; 6076980Seric } 6086980Seric 6096980Seric /* do basic system initialization */ 6104632Seric initsys(); 6116980Seric 6126980Seric /* read the queue control file */ 61317477Seric readqf(CurEnv, TRUE); 6149338Seric CurEnv->e_flags |= EF_INQUEUE; 6159377Seric eatheader(CurEnv); 6166980Seric 6176980Seric /* do the delivery */ 6189338Seric if (!bitset(EF_FATALERRS, CurEnv->e_flags)) 6199282Seric sendall(CurEnv, SM_DELIVER); 6206980Seric 6216980Seric /* finish up and exit */ 62224941Seric if (ForkQueueRuns) 62324941Seric finis(); 62424941Seric else 62524941Seric dropenvelope(CurEnv); 6264632Seric } 62724941Seric else 62824941Seric { 62924941Seric /* 63024941Seric ** Parent -- pick up results. 63124941Seric */ 6324632Seric 63324941Seric errno = 0; 63424941Seric (void) waitfor(i); 63524941Seric } 6364632Seric } 6374632Seric /* 6384632Seric ** READQF -- read queue file and set up environment. 6394632Seric ** 6404632Seric ** Parameters: 6419377Seric ** e -- the envelope of the job to run. 6429630Seric ** full -- if set, read in all information. Otherwise just 6439630Seric ** read in info needed for a queue print. 6444632Seric ** 6454632Seric ** Returns: 6464632Seric ** none. 6474632Seric ** 6484632Seric ** Side Effects: 6494632Seric ** cf is read and created as the current job, as though 6504632Seric ** we had been invoked by argument. 6514632Seric */ 6524632Seric 65317477Seric readqf(e, full) 6549377Seric register ENVELOPE *e; 6559630Seric bool full; 6564632Seric { 65717477Seric char *qf; 65817477Seric register FILE *qfp; 6597785Seric char buf[MAXFIELD]; 6609348Seric extern char *fgetfolded(); 66124954Seric extern long atol(); 6624632Seric 6634632Seric /* 66417468Seric ** Read and process the file. 6654632Seric */ 6664632Seric 66717477Seric qf = queuename(e, 'q'); 66817477Seric qfp = fopen(qf, "r"); 66917477Seric if (qfp == NULL) 67017477Seric { 67117477Seric syserr("readqf: no control file %s", qf); 67217477Seric return; 67317477Seric } 67417477Seric FileName = qf; 6759377Seric LineNumber = 0; 6769630Seric if (Verbose && full) 6779377Seric printf("\nRunning %s\n", e->e_id); 67817468Seric while (fgetfolded(buf, sizeof buf, qfp) != NULL) 6794632Seric { 6804632Seric switch (buf[0]) 6814632Seric { 6824632Seric case 'R': /* specify recipient */ 6839618Seric sendtolist(&buf[1], (ADDRESS *) NULL, &e->e_sendqueue); 6844632Seric break; 6854632Seric 6864632Seric case 'H': /* header */ 6879630Seric if (full) 6889630Seric (void) chompheader(&buf[1], FALSE); 6894632Seric break; 6904632Seric 69110108Seric case 'M': /* message */ 69210108Seric e->e_message = newstr(&buf[1]); 69310108Seric break; 69410108Seric 6954632Seric case 'S': /* sender */ 6964634Seric setsender(newstr(&buf[1])); 6974632Seric break; 6984632Seric 6994632Seric case 'D': /* data file name */ 7009630Seric if (!full) 7019630Seric break; 7029377Seric e->e_df = newstr(&buf[1]); 7039544Seric e->e_dfp = fopen(e->e_df, "r"); 7049544Seric if (e->e_dfp == NULL) 7059377Seric syserr("readqf: cannot open %s", e->e_df); 7064632Seric break; 7074632Seric 7087860Seric case 'T': /* init time */ 70924941Seric e->e_ctime = atol(&buf[1]); 7104632Seric break; 7114632Seric 7124634Seric case 'P': /* message priority */ 71325008Seric e->e_msgpriority = atol(&buf[1]) + WkTimeFact; 7144634Seric break; 7154634Seric 71624941Seric case '\0': /* blank line; ignore */ 71724941Seric break; 71824941Seric 7194632Seric default: 72024941Seric syserr("readqf(%s:%d): bad line \"%s\"", e->e_id, 72124941Seric LineNumber, buf); 7224632Seric break; 7234632Seric } 7244632Seric } 7259377Seric 72624954Seric (void) fclose(qfp); 7279377Seric FileName = NULL; 72824941Seric 72924941Seric /* 73024941Seric ** If we haven't read any lines, this queue file is empty. 73124941Seric ** Arrange to remove it without referencing any null pointers. 73224941Seric */ 73324941Seric 73424941Seric if (LineNumber == 0) 73524941Seric { 73624941Seric errno = 0; 73724941Seric e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE; 73824941Seric } 7394632Seric } 7404632Seric /* 7419630Seric ** PRINTQUEUE -- print out a representation of the mail queue 7429630Seric ** 7439630Seric ** Parameters: 7449630Seric ** none. 7459630Seric ** 7469630Seric ** Returns: 7479630Seric ** none. 7489630Seric ** 7499630Seric ** Side Effects: 7509630Seric ** Prints a listing of the mail queue on the standard output. 7519630Seric */ 7525182Seric 7539630Seric printqueue() 7549630Seric { 7559630Seric register WORK *w; 7569630Seric FILE *f; 75710070Seric int nrequests; 7589630Seric char buf[MAXLINE]; 7599630Seric 7609630Seric /* 7619630Seric ** Read and order the queue. 7629630Seric */ 7639630Seric 76424941Seric nrequests = orderq(TRUE); 7659630Seric 7669630Seric /* 7679630Seric ** Print the work list that we have read. 7689630Seric */ 7699630Seric 7709630Seric /* first see if there is anything */ 77110070Seric if (nrequests <= 0) 7729630Seric { 77310070Seric printf("Mail queue is empty\n"); 7749630Seric return; 7759630Seric } 7769630Seric 77710096Seric printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s"); 77810070Seric if (nrequests > WLSIZE) 77910070Seric printf(", only %d printed", WLSIZE); 78024979Seric if (Verbose) 78125032Seric printf(")\n--QID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n"); 78224979Seric else 78324979Seric printf(")\n--QID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n"); 7849630Seric for (w = WorkQ; w != NULL; w = w->w_next) 7859630Seric { 7869630Seric struct stat st; 78710070Seric auto time_t submittime = 0; 78810070Seric long dfsize = -1; 78910108Seric char lf[20]; 79010108Seric char message[MAXLINE]; 79124941Seric extern bool shouldqueue(); 7929630Seric 79317468Seric f = fopen(w->w_name, "r"); 79417468Seric if (f == NULL) 79517468Seric { 79617468Seric errno = 0; 79717468Seric continue; 79817468Seric } 7999630Seric printf("%7s", w->w_name + 2); 80023098Seric (void) strcpy(lf, w->w_name); 80110070Seric lf[0] = 'l'; 80210070Seric if (stat(lf, &st) >= 0) 80310070Seric printf("*"); 80424941Seric else if (shouldqueue(w->w_pri)) 80524941Seric printf("X"); 80610070Seric else 80710070Seric printf(" "); 80810070Seric errno = 0; 80917468Seric 81010108Seric message[0] = '\0'; 8119630Seric while (fgets(buf, sizeof buf, f) != NULL) 8129630Seric { 8139630Seric fixcrlf(buf, TRUE); 8149630Seric switch (buf[0]) 8159630Seric { 81610108Seric case 'M': /* error message */ 81723098Seric (void) strcpy(message, &buf[1]); 81810108Seric break; 81910108Seric 8209630Seric case 'S': /* sender name */ 82124979Seric if (Verbose) 82225027Seric printf("%8ld %10ld %.12s %.38s", dfsize, 82325027Seric w->w_pri, ctime(&submittime) + 4, 82424979Seric &buf[1]); 82524979Seric else 82624979Seric printf("%8ld %.16s %.45s", dfsize, 82724979Seric ctime(&submittime), &buf[1]); 82810108Seric if (message[0] != '\0') 82925027Seric printf("\n\t\t (%.60s)", message); 8309630Seric break; 8319630Seric 8329630Seric case 'R': /* recipient name */ 83324979Seric if (Verbose) 83425027Seric printf("\n\t\t\t\t\t %.38s", &buf[1]); 83524979Seric else 83624979Seric printf("\n\t\t\t\t %.45s", &buf[1]); 8379630Seric break; 8389630Seric 8399630Seric case 'T': /* creation time */ 84024941Seric submittime = atol(&buf[1]); 8419630Seric break; 84210070Seric 84310070Seric case 'D': /* data file name */ 84410070Seric if (stat(&buf[1], &st) >= 0) 84510070Seric dfsize = st.st_size; 84610070Seric break; 8479630Seric } 8489630Seric } 84910070Seric if (submittime == (time_t) 0) 85010070Seric printf(" (no control file)"); 8519630Seric printf("\n"); 85223098Seric (void) fclose(f); 8539630Seric } 8549630Seric } 8559630Seric 8565182Seric # endif QUEUE 85717468Seric /* 85817468Seric ** QUEUENAME -- build a file name in the queue directory for this envelope. 85917468Seric ** 86017468Seric ** Assigns an id code if one does not already exist. 86117468Seric ** This code is very careful to avoid trashing existing files 86217468Seric ** under any circumstances. 86317468Seric ** We first create an nf file that is only used when 86417468Seric ** assigning an id. This file is always empty, so that 86517468Seric ** we can never accidently truncate an lf file. 86617468Seric ** 86717468Seric ** Parameters: 86817468Seric ** e -- envelope to build it in/from. 86917468Seric ** type -- the file type, used as the first character 87017468Seric ** of the file name. 87117468Seric ** 87217468Seric ** Returns: 87317468Seric ** a pointer to the new file name (in a static buffer). 87417468Seric ** 87517468Seric ** Side Effects: 87617468Seric ** Will create the lf and qf files if no id code is 87717468Seric ** already assigned. This will cause the envelope 87817468Seric ** to be modified. 87917468Seric */ 88017468Seric 88117468Seric char * 88217468Seric queuename(e, type) 88317468Seric register ENVELOPE *e; 88417468Seric char type; 88517468Seric { 88617468Seric static char buf[MAXNAME]; 88717468Seric static int pid = -1; 88817468Seric char c1 = 'A'; 88917468Seric char c2 = 'A'; 89017468Seric 89117468Seric if (e->e_id == NULL) 89217468Seric { 89317468Seric char qf[20]; 89417468Seric char nf[20]; 89517468Seric char lf[20]; 89617468Seric 89717468Seric /* find a unique id */ 89817468Seric if (pid != getpid()) 89917468Seric { 90017468Seric /* new process -- start back at "AA" */ 90117468Seric pid = getpid(); 90217468Seric c1 = 'A'; 90317468Seric c2 = 'A' - 1; 90417468Seric } 90517468Seric (void) sprintf(qf, "qfAA%05d", pid); 90623098Seric (void) strcpy(lf, qf); 90717468Seric lf[0] = 'l'; 90823098Seric (void) strcpy(nf, qf); 90917468Seric nf[0] = 'n'; 91017468Seric 91117468Seric while (c1 < '~' || c2 < 'Z') 91217468Seric { 91317468Seric int i; 91417468Seric 91517468Seric if (c2 >= 'Z') 91617468Seric { 91717468Seric c1++; 91817468Seric c2 = 'A' - 1; 91917468Seric } 92017477Seric lf[2] = nf[2] = qf[2] = c1; 92117477Seric lf[3] = nf[3] = qf[3] = ++c2; 92217468Seric # ifdef DEBUG 92317468Seric if (tTd(7, 20)) 92417468Seric printf("queuename: trying \"%s\"\n", nf); 92517468Seric # endif DEBUG 92617468Seric 92717468Seric # ifdef QUEUE 92817468Seric if (access(lf, 0) >= 0 || access(qf, 0) >= 0) 92917468Seric continue; 93017468Seric errno = 0; 93117468Seric i = creat(nf, FileMode); 93217468Seric if (i < 0) 93317468Seric { 93417468Seric (void) unlink(nf); /* kernel bug */ 93517468Seric continue; 93617468Seric } 93717468Seric (void) close(i); 93817468Seric i = link(nf, lf); 93917468Seric (void) unlink(nf); 94017468Seric if (i < 0) 94117468Seric continue; 94217468Seric if (link(lf, qf) >= 0) 94317468Seric break; 94417468Seric (void) unlink(lf); 94517468Seric # else QUEUE 94617982Seric if (close(creat(qf, FileMode)) >= 0) 94717982Seric break; 94817468Seric # endif QUEUE 94917468Seric } 95017468Seric if (c1 >= '~' && c2 >= 'Z') 95117468Seric { 95217468Seric syserr("queuename: Cannot create \"%s\" in \"%s\"", 95317468Seric qf, QueueDir); 95417468Seric exit(EX_OSERR); 95517468Seric } 95617468Seric e->e_id = newstr(&qf[2]); 95717468Seric define('i', e->e_id, e); 95817468Seric # ifdef DEBUG 95917468Seric if (tTd(7, 1)) 96017468Seric printf("queuename: assigned id %s, env=%x\n", e->e_id, e); 96117468Seric # ifdef LOG 96217468Seric if (LogLevel > 16) 96317468Seric syslog(LOG_DEBUG, "%s: assigned id", e->e_id); 96417468Seric # endif LOG 96517468Seric # endif DEBUG 96617468Seric } 96717468Seric 96817468Seric if (type == '\0') 96917468Seric return (NULL); 97017468Seric (void) sprintf(buf, "%cf%s", type, e->e_id); 97117468Seric # ifdef DEBUG 97217468Seric if (tTd(7, 2)) 97317468Seric printf("queuename: %s\n", buf); 97417468Seric # endif DEBUG 97517468Seric return (buf); 97617468Seric } 97717468Seric /* 97817468Seric ** UNLOCKQUEUE -- unlock the queue entry for a specified envelope 97917468Seric ** 98017468Seric ** Parameters: 98117468Seric ** e -- the envelope to unlock. 98217468Seric ** 98317468Seric ** Returns: 98417468Seric ** none 98517468Seric ** 98617468Seric ** Side Effects: 98717468Seric ** unlocks the queue for `e'. 98817468Seric */ 98917468Seric 99017468Seric unlockqueue(e) 99117468Seric ENVELOPE *e; 99217468Seric { 99317468Seric /* remove the transcript */ 99417468Seric #ifdef DEBUG 99517468Seric # ifdef LOG 99617468Seric if (LogLevel > 19) 99717468Seric syslog(LOG_DEBUG, "%s: unlock", e->e_id); 99817468Seric # endif LOG 99917468Seric if (!tTd(51, 4)) 100017468Seric #endif DEBUG 100117468Seric xunlink(queuename(e, 'x')); 100217468Seric 100317468Seric # ifdef QUEUE 100417468Seric /* last but not least, remove the lock */ 100517468Seric xunlink(queuename(e, 'l')); 100617468Seric # endif QUEUE 100717468Seric } 1008