14632Seric # include "sendmail.h" 24632Seric # include <sys/stat.h> 36625Sglickman # include <ndir.h> 44634Seric # include <signal.h> 54632Seric # include <errno.h> 64632Seric 75182Seric # ifndef QUEUE 8*6980Seric SCCSID(@(#)queue.c 3.13.1.1 05/29/82 (no queueing)); 95182Seric # else QUEUE 104632Seric 11*6980Seric SCCSID(@(#)queue.c 3.13.1.1 05/29/82); 125182Seric 134632Seric /* 144632Seric ** QUEUEUP -- queue a message up for future transmission. 154632Seric ** 164632Seric ** The queued message should already be in the correct place. 174632Seric ** This routine just outputs the control file as appropriate. 184632Seric ** 194632Seric ** Parameters: 20*6980Seric ** e -- the envelope to queue up. 214632Seric ** 224632Seric ** Returns: 234632Seric ** none. 244632Seric ** 254632Seric ** Side Effects: 264632Seric ** The current request (only unsatisfied addresses) 274632Seric ** are saved in a control file. 284632Seric */ 294632Seric 30*6980Seric queueup(e) 31*6980Seric register ENVELOPE *e; 324632Seric { 334632Seric char cf[MAXNAME]; 34*6980Seric char buf[MAXNAME]; 35*6980Seric register FILE *cfp; 364632Seric register HDR *h; 375007Seric register ADDRESS *q; 385199Seric extern char *mktemp(); 395902Seric register int i; 404632Seric 415037Seric /* 425037Seric ** Create control file. 435037Seric */ 444632Seric 455037Seric strcpy(cf, QueueDir); 46*6980Seric strcat(cf, "/tfXXXXXX"); 475037Seric (void) mktemp(cf); 48*6980Seric cfp = fopen(cf, "w"); 49*6980Seric if (cfp == NULL) 504632Seric { 514632Seric syserr("queueup: cannot create control file %s", cf); 524632Seric return; 534632Seric } 544632Seric 554632Seric # ifdef DEBUG 564632Seric if (Debug) 57*6980Seric printf("queueing in %s\n", cf); 584632Seric # endif DEBUG 594632Seric 604632Seric /* 61*6980Seric ** If there is no data file yet, create one. 62*6980Seric */ 63*6980Seric 64*6980Seric if (e->e_df == NULL) 65*6980Seric { 66*6980Seric register FILE *dfp; 67*6980Seric 68*6980Seric strcpy(buf, QueueDir); 69*6980Seric strcat(buf, "/dfXXXXXX"); 70*6980Seric e->e_df = newstr(mktemp(buf)); 71*6980Seric dfp = fopen(e->e_df, "w"); 72*6980Seric if (dfp == NULL) 73*6980Seric { 74*6980Seric syserr("queueup: cannot create %s", e->e_df); 75*6980Seric fclose(cfp); 76*6980Seric return; 77*6980Seric } 78*6980Seric (*e->e_putbody)(dfp, Mailer[1], FALSE); 79*6980Seric fclose(dfp); 80*6980Seric } 81*6980Seric 82*6980Seric /* 834632Seric ** Output future work requests. 844632Seric */ 854632Seric 864632Seric /* output name of data file */ 87*6980Seric fprintf(cfp, "D%s\n", e->e_df); 884632Seric 894632Seric /* output name of sender */ 90*6980Seric fprintf(cfp, "S%s\n", e->e_from.q_paddr); 914632Seric 924632Seric /* output timeout */ 93*6980Seric fprintf(cfp, "T%ld\n", TimeOut); 944632Seric 954634Seric /* output message priority */ 96*6980Seric fprintf(cfp, "P%ld\n", e->e_msgpriority); 974634Seric 98*6980Seric /* output message class */ 99*6980Seric fprintf(cfp, "C%d\n", e->e_class); 100*6980Seric 1015902Seric /* output macro definitions */ 1025902Seric for (i = 0; i < 128; i++) 1035902Seric { 104*6980Seric register char *p = e->e_macro[i]; 1055902Seric 1065902Seric if (p != NULL && i != (int) 'b') 107*6980Seric fprintf(cfp, "M%c%s\n", i, p); 1085902Seric } 1095902Seric 1104632Seric /* output list of recipient addresses */ 111*6980Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1124632Seric { 1135037Seric # ifdef DEBUG 1145037Seric if (Debug > 0) 1155037Seric { 1165037Seric printf("queueing "); 1175037Seric printaddr(q, FALSE); 1185037Seric } 1195037Seric # endif DEBUG 1205037Seric if (bitset(QQUEUEUP, q->q_flags)) 121*6980Seric fprintf(cfp, "R%s\n", q->q_paddr); 1224632Seric } 1234632Seric 1244632Seric /* output headers for this message */ 125*6980Seric for (h = e->e_header; h != NULL; h = h->h_link) 1264632Seric { 1274632Seric if (h->h_value == NULL || h->h_value[0] == '\0') 1284632Seric continue; 129*6980Seric fprintf(cfp, "H"); 1304632Seric if (h->h_mflags != 0 && bitset(H_CHECK|H_ACHECK, h->h_flags)) 131*6980Seric mfdecode(h->h_mflags, cfp); 132*6980Seric fprintf(cfp, "%s: %s\n", h->h_field, h->h_value); 1334632Seric } 1344632Seric 1354632Seric /* 1364632Seric ** Clean up. 1374632Seric */ 1384632Seric 139*6980Seric (void) fclose(cfp); 140*6980Seric (void) strcpy(buf, QueueDir); 141*6980Seric (void) strcat(buf, "/cfXXXXXX"); 142*6980Seric (void) mktemp(buf); 143*6980Seric if (link(cf, buf) < 0) 144*6980Seric syserr("cannot link(%s, %s), df=%s", cf, buf, e->e_df); 145*6980Seric else 146*6980Seric unlink(cf); 1474632Seric } 1484632Seric /* 1494632Seric ** RUNQUEUE -- run the jobs in the queue. 1504632Seric ** 1514632Seric ** Gets the stuff out of the queue in some presumably logical 1524632Seric ** order and processes them. 1534632Seric ** 1544632Seric ** Parameters: 1554632Seric ** none. 1564632Seric ** 1574632Seric ** Returns: 1584632Seric ** none. 1594632Seric ** 1604632Seric ** Side Effects: 1614632Seric ** runs things in the mail queue. 1624632Seric */ 1634632Seric 1644639Seric bool ReorderQueue; /* if set, reorder the send queue */ 1654639Seric int QueuePid; /* pid of child running queue */ 1664634Seric 1674639Seric runqueue(forkflag) 1684639Seric bool forkflag; 1694632Seric { 1704634Seric extern reordersig(); 1714632Seric 1724639Seric if (QueueIntvl != 0) 1734639Seric { 1744836Seric (void) signal(SIGALRM, reordersig); 1754836Seric (void) alarm((unsigned) QueueIntvl); 1764639Seric } 1774639Seric 1784639Seric if (forkflag) 1794639Seric { 1804639Seric QueuePid = dofork(); 1814639Seric if (QueuePid > 0) 1824639Seric { 1834639Seric /* parent */ 1844639Seric return; 1854639Seric } 1864639Seric else 1874836Seric (void) alarm((unsigned) 0); 1884639Seric } 1894639Seric 1904634Seric for (;;) 1914634Seric { 1924634Seric /* 1934634Seric ** Order the existing work requests. 1944634Seric */ 1954632Seric 1964634Seric orderq(); 1974632Seric 1984634Seric if (WorkQ == NULL) 1994634Seric { 2004634Seric /* no work? well, maybe later */ 2014634Seric if (QueueIntvl == 0) 2024634Seric break; 2034639Seric pause(); 2044634Seric continue; 2054634Seric } 2064632Seric 2074634Seric ReorderQueue = FALSE; 2084634Seric 2094634Seric /* 2104634Seric ** Process them once at a time. 2114634Seric ** The queue could be reordered while we do this to take 2124634Seric ** new requests into account. If so, the existing job 2134634Seric ** will be finished but the next thing taken off WorkQ 2144634Seric ** may be something else. 2154634Seric */ 2164634Seric 2174634Seric while (WorkQ != NULL) 2184634Seric { 2194634Seric WORK *w = WorkQ; 2204634Seric 2214634Seric WorkQ = WorkQ->w_next; 2224634Seric dowork(w); 2234634Seric free(w->w_name); 2244634Seric free((char *) w); 2254634Seric if (ReorderQueue) 2264634Seric break; 2274634Seric } 2284634Seric 2294634Seric if (QueueIntvl == 0) 2304634Seric break; 2314632Seric } 2325978Seric 2335978Seric /* no work to do -- just exit */ 2345978Seric finis(); 2354632Seric } 2364632Seric /* 2374634Seric ** REORDERSIG -- catch the alarm signal and tell sendmail to reorder queue. 2384634Seric ** 2394634Seric ** Parameters: 2404634Seric ** none. 2414634Seric ** 2424634Seric ** Returns: 2434634Seric ** none. 2444634Seric ** 2454634Seric ** Side Effects: 2464634Seric ** sets the "reorder work queue" flag. 2474634Seric */ 2484634Seric 2494634Seric reordersig() 2504634Seric { 2514639Seric if (QueuePid == 0) 2524639Seric { 2534639Seric /* we are in a child doing queueing */ 2544639Seric ReorderQueue = TRUE; 2554639Seric } 2564639Seric else 2574639Seric { 2584639Seric /* we are in a parent -- poke child or start new one */ 2594639Seric if (kill(QueuePid, SIGALRM) < 0) 2604639Seric { 2614639Seric /* no child -- get zombie & start new one */ 2624639Seric static int st; 2634639Seric 2644836Seric (void) wait(&st); 2654639Seric QueuePid = dofork(); 2664639Seric if (QueuePid == 0) 2674639Seric { 2684639Seric /* new child; run queue */ 2694836Seric runqueue(FALSE); 2704639Seric finis(); 2714639Seric } 2724639Seric } 2734639Seric } 2744639Seric 2754639Seric /* 2764639Seric ** Arrange to get this signal again. 2774639Seric */ 2784639Seric 2796065Seric (void) signal(SIGALRM, reordersig); 2804836Seric (void) alarm((unsigned) QueueIntvl); 2814634Seric } 2824634Seric /* 2834632Seric ** ORDERQ -- order the work queue. 2844632Seric ** 2854632Seric ** Parameters: 2864632Seric ** none. 2874632Seric ** 2884632Seric ** Returns: 2894632Seric ** none. 2904632Seric ** 2914632Seric ** Side Effects: 2924632Seric ** Sets WorkQ to the queue of available work, in order. 2934632Seric */ 2944632Seric 2954632Seric # define WLSIZE 120 /* max size of worklist per sort */ 2964632Seric 2974632Seric orderq() 2984632Seric { 2996625Sglickman register struct direct *d; 3004632Seric register WORK *w; 3014632Seric register WORK **wp; /* parent of w */ 3026625Sglickman DIR *f; 3034632Seric register int i; 3044632Seric WORK wlist[WLSIZE]; 3054632Seric int wn = 0; 3064632Seric extern workcmpf(); 3074632Seric extern char *QueueDir; 3084632Seric 3094632Seric /* clear out old WorkQ */ 3104632Seric for (w = WorkQ; w != NULL; ) 3114632Seric { 3124632Seric register WORK *nw = w->w_next; 3134632Seric 3144632Seric WorkQ = nw; 3154632Seric free(w->w_name); 3164632Seric free((char *) w); 3174632Seric w = nw; 3184632Seric } 3194632Seric 3204632Seric /* open the queue directory */ 3216625Sglickman f = opendir(QueueDir); 3224632Seric if (f == NULL) 3234632Seric { 3244632Seric syserr("orderq: cannot open %s", QueueDir); 3254632Seric return; 3264632Seric } 3274632Seric 3284632Seric /* 3294632Seric ** Read the work directory. 3304632Seric */ 3314632Seric 3326625Sglickman while (wn < WLSIZE && (d = readdir(f)) != NULL) 3334632Seric { 3344632Seric char cbuf[MAXNAME]; 3354632Seric char lbuf[MAXNAME]; 3364632Seric FILE *cf; 3374632Seric register char *p; 3384632Seric 3394632Seric /* is this an interesting entry? */ 3406625Sglickman if (d->d_name[0] != 'c') 3414632Seric continue; 3424632Seric 3434632Seric /* yes -- find the control file location */ 3444632Seric strcpy(cbuf, QueueDir); 3454632Seric strcat(cbuf, "/"); 3464632Seric p = &cbuf[strlen(cbuf)]; 3476625Sglickman strcpy(p, d->d_name); 3484632Seric 3494632Seric /* open control file */ 3504632Seric cf = fopen(cbuf, "r"); 3514632Seric if (cf == NULL) 3524632Seric { 3534632Seric syserr("orderq: cannot open %s", cbuf); 3544632Seric continue; 3554632Seric } 3565037Seric wlist[wn].w_name = newstr(cbuf); 3574632Seric 3584632Seric /* extract useful information */ 3594632Seric while (fgets(lbuf, sizeof lbuf, cf) != NULL) 3604632Seric { 3614632Seric fixcrlf(lbuf, TRUE); 3624632Seric 3634632Seric switch (lbuf[0]) 3644632Seric { 3654632Seric case 'P': /* message priority */ 3665037Seric (void) sscanf(&lbuf[1], "%ld", &wlist[wn].w_pri); 3674632Seric break; 3684632Seric } 3694632Seric } 3704632Seric wn++; 3714632Seric (void) fclose(cf); 3724632Seric } 3736625Sglickman (void) closedir(f); 3744632Seric 3754632Seric /* 3764632Seric ** Sort the work directory. 3774632Seric */ 3784632Seric 3794632Seric qsort(wlist, wn, sizeof *wlist, workcmpf); 3804632Seric 3814632Seric /* 3824632Seric ** Convert the work list into canonical form. 3834632Seric */ 3844632Seric 3854632Seric wp = &WorkQ; 3864632Seric for (i = 0; i < wn; i++) 3874632Seric { 3884632Seric w = (WORK *) xalloc(sizeof *w); 3894632Seric w->w_name = wlist[i].w_name; 3904632Seric w->w_pri = wlist[i].w_pri; 3914632Seric w->w_next = NULL; 3924632Seric *wp = w; 3934632Seric wp = &w->w_next; 3944632Seric } 3954632Seric 3964632Seric # ifdef DEBUG 3974632Seric if (Debug) 3984632Seric { 3994632Seric for (w = WorkQ; w != NULL; w = w->w_next) 4005037Seric printf("%32s: pri=%ld\n", w->w_name, w->w_pri); 4014632Seric } 4024632Seric # endif DEBUG 4034632Seric } 4044632Seric /* 4054632Seric ** WORKCMPF -- compare function for ordering work. 4064632Seric ** 4074632Seric ** Parameters: 4084632Seric ** a -- the first argument. 4094632Seric ** b -- the second argument. 4104632Seric ** 4114632Seric ** Returns: 4124632Seric ** -1 if a < b 4134632Seric ** 0 if a == b 4144632Seric ** 1 if a > b 4154632Seric ** 4164632Seric ** Side Effects: 4174632Seric ** none. 4184632Seric */ 4194632Seric 4204632Seric # define PRIFACT 1800 /* bytes each priority point is worth */ 4214632Seric 4224632Seric workcmpf(a, b) 4235037Seric register WORK *a; 4245037Seric register WORK *b; 4254632Seric { 4265037Seric if (a->w_pri == b->w_pri) 4274632Seric return (0); 4285037Seric else if (a->w_pri > b->w_pri) 4294632Seric return (1); 4304632Seric else 4314632Seric return (-1); 4324632Seric } 4334632Seric /* 4344632Seric ** DOWORK -- do a work request. 4354632Seric ** 4364632Seric ** Parameters: 4374632Seric ** w -- the work request to be satisfied. 4384632Seric ** 4394632Seric ** Returns: 4404632Seric ** none. 4414632Seric ** 4424632Seric ** Side Effects: 4434632Seric ** The work request is satisfied if possible. 4444632Seric */ 4454632Seric 4464632Seric dowork(w) 4474632Seric register WORK *w; 4484632Seric { 4494632Seric register int i; 4504632Seric auto int xstat; 4514632Seric 4524632Seric # ifdef DEBUG 4534632Seric if (Debug) 4545037Seric printf("dowork: %s pri %ld\n", w->w_name, w->w_pri); 4554632Seric # endif DEBUG 4564632Seric 4574632Seric /* 4584632Seric ** Fork for work. 4594632Seric */ 4604632Seric 4614632Seric i = fork(); 4624632Seric if (i < 0) 4634632Seric { 4644632Seric syserr("dowork: cannot fork"); 4654632Seric return; 4664632Seric } 4674632Seric 4684632Seric if (i == 0) 4694632Seric { 470*6980Seric char buf[MAXNAME]; 471*6980Seric 4724632Seric /* 4734632Seric ** CHILD 474*6980Seric ** Change the name of the control file to avoid 475*6980Seric ** duplicate deliveries. Then run the file as 476*6980Seric ** though we had just read it. 4774632Seric */ 4784632Seric 4794632Seric QueueRun = TRUE; 480*6980Seric (void) strcpy(buf, QueueDir); 481*6980Seric (void) strcat(buf, "/tfXXXXXX"); 482*6980Seric (void) mktemp(buf); 483*6980Seric if (link(w->w_name, buf) < 0) 484*6980Seric { 485*6980Seric syserr("dowork: link(%s, %s)", w->w_name, buf); 486*6980Seric 487*6980Seric /* it's ok to lie -- it will be run later */ 488*6980Seric exit(EX_OK); 489*6980Seric } 490*6980Seric (void) unlink(w->w_name); 491*6980Seric 492*6980Seric /* create ourselves a transcript file */ 4934634Seric openxscrpt(); 494*6980Seric 495*6980Seric /* do basic system initialization */ 4964632Seric initsys(); 497*6980Seric 498*6980Seric /* read the queue control file */ 499*6980Seric readqf(buf); 500*6980Seric 501*6980Seric /* do the delivery */ 5024632Seric sendall(FALSE); 503*6980Seric 504*6980Seric /* if still not sent, perhaps we should time out.... */ 5054634Seric # ifdef DEBUG 5064634Seric if (Debug > 2) 5074634Seric printf("CurTime=%ld, TimeOut=%ld\n", CurTime, TimeOut); 5084634Seric # endif DEBUG 5096908Seric if (CurEnv->e_queueup && CurTime > TimeOut) 5104634Seric timeout(w); 511*6980Seric 512*6980Seric /* get rid of the temporary file -- a new cf will be made */ 513*6980Seric (void) unlink(buf); 514*6980Seric 515*6980Seric /* finish up and exit */ 5164632Seric finis(); 5174632Seric } 5184632Seric 5194632Seric /* 5204632Seric ** Parent -- pick up results. 5214632Seric */ 5224632Seric 5234632Seric errno = 0; 5244632Seric while ((i = wait(&xstat)) > 0 && errno != EINTR) 5254632Seric { 5264632Seric if (errno == EINTR) 5274632Seric { 5284632Seric errno = 0; 5294632Seric } 5304632Seric } 5314632Seric } 5324632Seric /* 5334632Seric ** READQF -- read queue file and set up environment. 5344632Seric ** 5354632Seric ** Parameters: 5364632Seric ** cf -- name of queue control file. 5374632Seric ** 5384632Seric ** Returns: 5394632Seric ** none. 5404632Seric ** 5414632Seric ** Side Effects: 5424632Seric ** cf is read and created as the current job, as though 5434632Seric ** we had been invoked by argument. 5444632Seric */ 5454632Seric 5464632Seric readqf(cf) 5474632Seric char *cf; 5484632Seric { 5494632Seric register FILE *f; 5504632Seric char buf[MAXLINE]; 5514632Seric 5524632Seric /* 5534632Seric ** Open the file created by queueup. 5544632Seric */ 5554632Seric 5564632Seric f = fopen(cf, "r"); 5574632Seric if (f == NULL) 5584632Seric { 5594632Seric syserr("readqf: no cf file %s", cf); 5604632Seric return; 5614632Seric } 5624632Seric 5634632Seric /* 5644632Seric ** Read and process the file. 5654632Seric */ 5664632Seric 5675037Seric if (Verbose) 5686908Seric message(Arpa_Info, "Running %s", cf); 5695037Seric 5704632Seric while (fgets(buf, sizeof buf, f) != NULL) 5714632Seric { 5724632Seric fixcrlf(buf, TRUE); 5734632Seric 5744632Seric switch (buf[0]) 5754632Seric { 5764632Seric case 'R': /* specify recipient */ 5776908Seric sendto(&buf[1], 1, (ADDRESS *) NULL, &CurEnv->e_sendqueue); 5784632Seric break; 5794632Seric 5804632Seric case 'H': /* header */ 5814632Seric (void) chompheader(&buf[1], FALSE); 5824632Seric break; 5834632Seric 5844632Seric case 'S': /* sender */ 5854634Seric setsender(newstr(&buf[1])); 5864632Seric break; 5874632Seric 5884632Seric case 'D': /* data file name */ 5894632Seric InFileName = newstr(&buf[1]); 5904632Seric TempFile = fopen(InFileName, "r"); 5914632Seric if (TempFile == NULL) 5924632Seric syserr("readqf: cannot open %s", InFileName); 5934632Seric break; 5944632Seric 5954632Seric case 'T': /* timeout */ 5964632Seric (void) sscanf(&buf[1], "%ld", &TimeOut); 5974632Seric break; 5984632Seric 5994634Seric case 'P': /* message priority */ 6006908Seric (void) sscanf(&buf[1], "%ld", &CurEnv->e_msgpriority); 6015037Seric 6025037Seric /* make sure that big things get sent eventually */ 6036908Seric CurEnv->e_msgpriority -= WKTIMEFACT; 6044634Seric break; 6054634Seric 606*6980Seric case 'C': /* message class */ 607*6980Seric (void) sscanf(&buf[1], "%hd", &CurEnv->e_class); 608*6980Seric break; 609*6980Seric 6105902Seric case 'M': /* define macro */ 6115902Seric define(buf[1], newstr(&buf[2])); 6125902Seric break; 6135902Seric 6144632Seric default: 6154632Seric syserr("readqf(%s): bad line \"%s\"", cf, buf); 6164632Seric break; 6174632Seric } 6184632Seric } 6194632Seric } 6204632Seric /* 6214632Seric ** TIMEOUT -- process timeout on queue file. 6224632Seric ** 6234632Seric ** Parameters: 6244632Seric ** w -- pointer to work request that timed out. 6254632Seric ** 6264632Seric ** Returns: 6274632Seric ** none. 6284632Seric ** 6294632Seric ** Side Effects: 6304632Seric ** Returns a message to the sender saying that this 6314632Seric ** message has timed out. 6324632Seric */ 6334632Seric 6344632Seric timeout(w) 6354632Seric register WORK *w; 6364632Seric { 6374634Seric # ifdef DEBUG 6384634Seric if (Debug > 0) 6394634Seric printf("timeout(%s)\n", w->w_name); 6404634Seric # endif DEBUG 6414634Seric 6424634Seric /* return message to sender */ 643*6980Seric (void) returntosender("Cannot send mail for three days", 644*6980Seric &CurEnv->e_from, TRUE); 6454634Seric 6464634Seric /* arrange to remove files from queue */ 6476908Seric CurEnv->e_queueup = FALSE; 6484632Seric } 6495182Seric 6505182Seric # endif QUEUE 651