xref: /csrg-svn/usr.sbin/sendmail/src/queue.c (revision 25013)
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*25013Seric static char	SccsId[] = "@(#)queue.c	5.14 (Berkeley) 09/21/85	(no queueing)";
2123116Seric # endif not lint
225182Seric # else QUEUE
234632Seric 
2423116Seric # ifndef lint
25*25013Seric static char	SccsId[] = "@(#)queue.c	5.14 (Berkeley) 09/21/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 */
36*25013Seric 	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 		{
2857943Seric 			/* parent -- pick up intermediate zombie */
2869377Seric 			(void) waitfor(pid);
2877690Seric 			if (QueueIntvl != 0)
2889348Seric 				(void) setevent(QueueIntvl, runqueue, TRUE);
2894639Seric 			return;
2904639Seric 		}
2917943Seric 		/* child -- double fork */
2927943Seric 		if (fork() != 0)
2937943Seric 			exit(EX_OK);
2944639Seric 	}
29524941Seric 
29624941Seric 	setproctitle("running queue");
29724941Seric 
2987876Seric # ifdef LOG
2997876Seric 	if (LogLevel > 11)
3007943Seric 		syslog(LOG_DEBUG, "runqueue %s, pid=%d", QueueDir, getpid());
3017876Seric # endif LOG
3024639Seric 
3037466Seric 	/*
30410205Seric 	**  Release any resources used by the daemon code.
30510205Seric 	*/
30610205Seric 
30710205Seric # ifdef DAEMON
30810205Seric 	clrdaemon();
30910205Seric # endif DAEMON
31010205Seric 
31110205Seric 	/*
3127466Seric 	**  Start making passes through the queue.
3137466Seric 	**	First, read and sort the entire queue.
3147466Seric 	**	Then, process the work in that order.
3157466Seric 	**		But if you take too long, start over.
3167466Seric 	*/
3177466Seric 
3187943Seric 	/* order the existing work requests */
31924954Seric 	(void) orderq(FALSE);
3207690Seric 
3217943Seric 	/* process them once at a time */
3227943Seric 	while (WorkQ != NULL)
3234639Seric 	{
3247943Seric 		WORK *w = WorkQ;
3257881Seric 
3267943Seric 		WorkQ = WorkQ->w_next;
3277943Seric 		dowork(w);
3287943Seric 		free(w->w_name);
3297943Seric 		free((char *) w);
3304639Seric 	}
3317943Seric 	finis();
3324634Seric }
3334634Seric /*
3344632Seric **  ORDERQ -- order the work queue.
3354632Seric **
3364632Seric **	Parameters:
33724941Seric **		doall -- if set, include everything in the queue (even
33824941Seric **			the jobs that cannot be run because the load
33924941Seric **			average is too high).  Otherwise, exclude those
34024941Seric **			jobs.
3414632Seric **
3424632Seric **	Returns:
34310121Seric **		The number of request in the queue (not necessarily
34410121Seric **		the number of requests in WorkQ however).
3454632Seric **
3464632Seric **	Side Effects:
3474632Seric **		Sets WorkQ to the queue of available work, in order.
3484632Seric */
3494632Seric 
3504632Seric # define WLSIZE		120	/* max size of worklist per sort */
3514632Seric 
35224941Seric orderq(doall)
35324941Seric 	bool doall;
3544632Seric {
3556625Sglickman 	register struct direct *d;
3564632Seric 	register WORK *w;
3576625Sglickman 	DIR *f;
3584632Seric 	register int i;
35910121Seric 	WORK wlist[WLSIZE+1];
36010070Seric 	int wn = -1;
3614632Seric 	extern workcmpf();
3624632Seric 
3634632Seric 	/* clear out old WorkQ */
3644632Seric 	for (w = WorkQ; w != NULL; )
3654632Seric 	{
3664632Seric 		register WORK *nw = w->w_next;
3674632Seric 
3684632Seric 		WorkQ = nw;
3694632Seric 		free(w->w_name);
3704632Seric 		free((char *) w);
3714632Seric 		w = nw;
3724632Seric 	}
3734632Seric 
3744632Seric 	/* open the queue directory */
3758148Seric 	f = opendir(".");
3764632Seric 	if (f == NULL)
3774632Seric 	{
3788148Seric 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
37910070Seric 		return (0);
3804632Seric 	}
3814632Seric 
3824632Seric 	/*
3834632Seric 	**  Read the work directory.
3844632Seric 	*/
3854632Seric 
38610070Seric 	while ((d = readdir(f)) != NULL)
3874632Seric 	{
3889377Seric 		FILE *cf;
3894632Seric 		char lbuf[MAXNAME];
3904632Seric 
3914632Seric 		/* is this an interesting entry? */
3927812Seric 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
3934632Seric 			continue;
3944632Seric 
39510070Seric 		/* yes -- open control file (if not too many files) */
39610070Seric 		if (++wn >= WLSIZE)
39710070Seric 			continue;
3988148Seric 		cf = fopen(d->d_name, "r");
3994632Seric 		if (cf == NULL)
4004632Seric 		{
4017055Seric 			/* this may be some random person sending hir msgs */
4027055Seric 			/* syserr("orderq: cannot open %s", cbuf); */
40310090Seric #ifdef DEBUG
40410090Seric 			if (tTd(41, 2))
40510090Seric 				printf("orderq: cannot open %s (%d)\n",
40610090Seric 					d->d_name, errno);
40710090Seric #endif DEBUG
4087055Seric 			errno = 0;
40910090Seric 			wn--;
4104632Seric 			continue;
4114632Seric 		}
4128148Seric 		wlist[wn].w_name = newstr(d->d_name);
4134632Seric 
4144632Seric 		/* extract useful information */
4154632Seric 		while (fgets(lbuf, sizeof lbuf, cf) != NULL)
4164632Seric 		{
41724954Seric 			extern long atol();
41824954Seric 
41924941Seric 			switch (lbuf[0])
4204632Seric 			{
42124941Seric 			  case 'P':
42224941Seric 				wlist[wn].w_pri = atol(&lbuf[1]);
4234632Seric 				break;
424*25013Seric 
425*25013Seric 			  case 'T':
426*25013Seric 				wlist[wn].w_ctime = atol(&lbuf[1]);
427*25013Seric 				break;
4284632Seric 			}
4294632Seric 		}
4304632Seric 		(void) fclose(cf);
43124953Seric 
43224953Seric 		if (!doall && shouldqueue(wlist[wn].w_pri))
43324953Seric 		{
43424953Seric 			/* don't even bother sorting this job in */
43524953Seric 			wn--;
43624953Seric 		}
4374632Seric 	}
4386625Sglickman 	(void) closedir(f);
43910090Seric 	wn++;
4404632Seric 
4414632Seric 	/*
4424632Seric 	**  Sort the work directory.
4434632Seric 	*/
4444632Seric 
44523116Seric 	qsort((char *) wlist, min(wn, WLSIZE), sizeof *wlist, workcmpf);
4464632Seric 
4474632Seric 	/*
4484632Seric 	**  Convert the work list into canonical form.
4499377Seric 	**	Should be turning it into a list of envelopes here perhaps.
4504632Seric 	*/
4514632Seric 
45224981Seric 	WorkQ = NULL;
45310121Seric 	for (i = min(wn, WLSIZE); --i >= 0; )
4544632Seric 	{
4554632Seric 		w = (WORK *) xalloc(sizeof *w);
4564632Seric 		w->w_name = wlist[i].w_name;
4574632Seric 		w->w_pri = wlist[i].w_pri;
458*25013Seric 		w->w_ctime = wlist[i].w_ctime;
45924981Seric 		w->w_next = WorkQ;
46024981Seric 		WorkQ = w;
4614632Seric 	}
4624632Seric 
4634632Seric # ifdef DEBUG
4647677Seric 	if (tTd(40, 1))
4654632Seric 	{
4664632Seric 		for (w = WorkQ; w != NULL; w = w->w_next)
4675037Seric 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
4684632Seric 	}
4694632Seric # endif DEBUG
47010070Seric 
47110090Seric 	return (wn);
4724632Seric }
4734632Seric /*
4747677Seric **  WORKCMPF -- compare function for ordering work.
4754632Seric **
4764632Seric **	Parameters:
4774632Seric **		a -- the first argument.
4784632Seric **		b -- the second argument.
4794632Seric **
4804632Seric **	Returns:
48124981Seric **		-1 if a < b
48224981Seric **		 0 if a == b
48324981Seric **		+1 if a > b
4844632Seric **
4854632Seric **	Side Effects:
4864632Seric **		none.
4874632Seric */
4884632Seric 
4894632Seric workcmpf(a, b)
4905037Seric 	register WORK *a;
4915037Seric 	register WORK *b;
4924632Seric {
493*25013Seric 	long pa = a->w_pri + a->w_ctime;
494*25013Seric 	long pb = b->w_pri + b->w_ctime;
49524941Seric 
49624941Seric 	if (pa == pb)
4974632Seric 		return (0);
49824941Seric 	else if (pa > pb)
49924981Seric 		return (1);
50024981Seric 	else
50110121Seric 		return (-1);
5024632Seric }
5034632Seric /*
5044632Seric **  DOWORK -- do a work request.
5054632Seric **
5064632Seric **	Parameters:
5074632Seric **		w -- the work request to be satisfied.
5084632Seric **
5094632Seric **	Returns:
5104632Seric **		none.
5114632Seric **
5124632Seric **	Side Effects:
5134632Seric **		The work request is satisfied if possible.
5144632Seric */
5154632Seric 
5164632Seric dowork(w)
5174632Seric 	register WORK *w;
5184632Seric {
5194632Seric 	register int i;
52024941Seric 	extern bool shouldqueue();
5214632Seric 
5224632Seric # ifdef DEBUG
5237677Seric 	if (tTd(40, 1))
5245037Seric 		printf("dowork: %s pri %ld\n", w->w_name, w->w_pri);
5254632Seric # endif DEBUG
5264632Seric 
5274632Seric 	/*
52824941Seric 	**  Ignore jobs that are too expensive for the moment.
5294632Seric 	*/
5304632Seric 
53124941Seric 	if (shouldqueue(w->w_pri))
5324632Seric 	{
53324941Seric 		if (Verbose)
53424967Seric 			printf("\nSkipping %s\n", w->w_name + 2);
5354632Seric 		return;
5364632Seric 	}
5374632Seric 
53824941Seric 	/*
53924941Seric 	**  Fork for work.
54024941Seric 	*/
54124941Seric 
54224941Seric 	if (ForkQueueRuns)
54324941Seric 	{
54424941Seric 		i = fork();
54524941Seric 		if (i < 0)
54624941Seric 		{
54724941Seric 			syserr("dowork: cannot fork");
54824941Seric 			return;
54924941Seric 		}
55024941Seric 	}
55124941Seric 	else
55224941Seric 	{
55324941Seric 		i = 0;
55424941Seric 	}
55524941Seric 
5564632Seric 	if (i == 0)
5574632Seric 	{
5584632Seric 		/*
5594632Seric 		**  CHILD
5608148Seric 		**	Lock the control file to avoid duplicate deliveries.
5618148Seric 		**		Then run the file as though we had just read it.
5627350Seric 		**	We save an idea of the temporary name so we
5637350Seric 		**		can recover on interrupt.
5644632Seric 		*/
5654632Seric 
5667763Seric 		/* set basic modes, etc. */
5677356Seric 		(void) alarm(0);
56824961Seric 		clearenvelope(CurEnv);
5694632Seric 		QueueRun = TRUE;
5709377Seric 		ErrorMode = EM_MAIL;
5718148Seric 		CurEnv->e_id = &w->w_name[2];
5727876Seric # ifdef LOG
5737876Seric 		if (LogLevel > 11)
5747881Seric 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", CurEnv->e_id,
5757881Seric 			       getpid());
5767876Seric # endif LOG
5777763Seric 
5787763Seric 		/* don't use the headers from sendmail.cf... */
5797763Seric 		CurEnv->e_header = NULL;
5807763Seric 
58117468Seric 		/* lock the control file during processing */
5827812Seric 		if (link(w->w_name, queuename(CurEnv, 'l')) < 0)
5836980Seric 		{
5847812Seric 			/* being processed by another queuer */
5857881Seric # ifdef LOG
5867881Seric 			if (LogLevel > 4)
5877881Seric 				syslog(LOG_DEBUG, "%s: locked", CurEnv->e_id);
5887881Seric # endif LOG
58924941Seric 			if (ForkQueueRuns)
59024941Seric 				exit(EX_OK);
59124941Seric 			else
59224941Seric 				return;
5936980Seric 		}
5946980Seric 
5956980Seric 		/* do basic system initialization */
5964632Seric 		initsys();
5976980Seric 
5986980Seric 		/* read the queue control file */
59917477Seric 		readqf(CurEnv, TRUE);
6009338Seric 		CurEnv->e_flags |= EF_INQUEUE;
6019377Seric 		eatheader(CurEnv);
6026980Seric 
6036980Seric 		/* do the delivery */
6049338Seric 		if (!bitset(EF_FATALERRS, CurEnv->e_flags))
6059282Seric 			sendall(CurEnv, SM_DELIVER);
6066980Seric 
6076980Seric 		/* finish up and exit */
60824941Seric 		if (ForkQueueRuns)
60924941Seric 			finis();
61024941Seric 		else
61124941Seric 			dropenvelope(CurEnv);
6124632Seric 	}
61324941Seric 	else
61424941Seric 	{
61524941Seric 		/*
61624941Seric 		**  Parent -- pick up results.
61724941Seric 		*/
6184632Seric 
61924941Seric 		errno = 0;
62024941Seric 		(void) waitfor(i);
62124941Seric 	}
6224632Seric }
6234632Seric /*
6244632Seric **  READQF -- read queue file and set up environment.
6254632Seric **
6264632Seric **	Parameters:
6279377Seric **		e -- the envelope of the job to run.
6289630Seric **		full -- if set, read in all information.  Otherwise just
6299630Seric **			read in info needed for a queue print.
6304632Seric **
6314632Seric **	Returns:
6324632Seric **		none.
6334632Seric **
6344632Seric **	Side Effects:
6354632Seric **		cf is read and created as the current job, as though
6364632Seric **		we had been invoked by argument.
6374632Seric */
6384632Seric 
63917477Seric readqf(e, full)
6409377Seric 	register ENVELOPE *e;
6419630Seric 	bool full;
6424632Seric {
64317477Seric 	char *qf;
64417477Seric 	register FILE *qfp;
6457785Seric 	char buf[MAXFIELD];
6469348Seric 	extern char *fgetfolded();
64724954Seric 	extern long atol();
6484632Seric 
6494632Seric 	/*
65017468Seric 	**  Read and process the file.
6514632Seric 	*/
6524632Seric 
65317477Seric 	qf = queuename(e, 'q');
65417477Seric 	qfp = fopen(qf, "r");
65517477Seric 	if (qfp == NULL)
65617477Seric 	{
65717477Seric 		syserr("readqf: no control file %s", qf);
65817477Seric 		return;
65917477Seric 	}
66017477Seric 	FileName = qf;
6619377Seric 	LineNumber = 0;
6629630Seric 	if (Verbose && full)
6639377Seric 		printf("\nRunning %s\n", e->e_id);
66417468Seric 	while (fgetfolded(buf, sizeof buf, qfp) != NULL)
6654632Seric 	{
6664632Seric 		switch (buf[0])
6674632Seric 		{
6684632Seric 		  case 'R':		/* specify recipient */
6699618Seric 			sendtolist(&buf[1], (ADDRESS *) NULL, &e->e_sendqueue);
6704632Seric 			break;
6714632Seric 
6724632Seric 		  case 'H':		/* header */
6739630Seric 			if (full)
6749630Seric 				(void) chompheader(&buf[1], FALSE);
6754632Seric 			break;
6764632Seric 
67710108Seric 		  case 'M':		/* message */
67810108Seric 			e->e_message = newstr(&buf[1]);
67910108Seric 			break;
68010108Seric 
6814632Seric 		  case 'S':		/* sender */
6824634Seric 			setsender(newstr(&buf[1]));
6834632Seric 			break;
6844632Seric 
6854632Seric 		  case 'D':		/* data file name */
6869630Seric 			if (!full)
6879630Seric 				break;
6889377Seric 			e->e_df = newstr(&buf[1]);
6899544Seric 			e->e_dfp = fopen(e->e_df, "r");
6909544Seric 			if (e->e_dfp == NULL)
6919377Seric 				syserr("readqf: cannot open %s", e->e_df);
6924632Seric 			break;
6934632Seric 
6947860Seric 		  case 'T':		/* init time */
69524941Seric 			e->e_ctime = atol(&buf[1]);
6964632Seric 			break;
6974632Seric 
6984634Seric 		  case 'P':		/* message priority */
69925008Seric 			e->e_msgpriority = atol(&buf[1]) + WkTimeFact;
7004634Seric 			break;
7014634Seric 
70224941Seric 		  case '\0':		/* blank line; ignore */
70324941Seric 			break;
70424941Seric 
7054632Seric 		  default:
70624941Seric 			syserr("readqf(%s:%d): bad line \"%s\"", e->e_id,
70724941Seric 				LineNumber, buf);
7084632Seric 			break;
7094632Seric 		}
7104632Seric 	}
7119377Seric 
71224954Seric 	(void) fclose(qfp);
7139377Seric 	FileName = NULL;
71424941Seric 
71524941Seric 	/*
71624941Seric 	**  If we haven't read any lines, this queue file is empty.
71724941Seric 	**  Arrange to remove it without referencing any null pointers.
71824941Seric 	*/
71924941Seric 
72024941Seric 	if (LineNumber == 0)
72124941Seric 	{
72224941Seric 		errno = 0;
72324941Seric 		e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE;
72424941Seric 	}
7254632Seric }
7264632Seric /*
7279630Seric **  PRINTQUEUE -- print out a representation of the mail queue
7289630Seric **
7299630Seric **	Parameters:
7309630Seric **		none.
7319630Seric **
7329630Seric **	Returns:
7339630Seric **		none.
7349630Seric **
7359630Seric **	Side Effects:
7369630Seric **		Prints a listing of the mail queue on the standard output.
7379630Seric */
7385182Seric 
7399630Seric printqueue()
7409630Seric {
7419630Seric 	register WORK *w;
7429630Seric 	FILE *f;
74310070Seric 	int nrequests;
7449630Seric 	char buf[MAXLINE];
7459630Seric 
7469630Seric 	/*
7479630Seric 	**  Read and order the queue.
7489630Seric 	*/
7499630Seric 
75024941Seric 	nrequests = orderq(TRUE);
7519630Seric 
7529630Seric 	/*
7539630Seric 	**  Print the work list that we have read.
7549630Seric 	*/
7559630Seric 
7569630Seric 	/* first see if there is anything */
75710070Seric 	if (nrequests <= 0)
7589630Seric 	{
75910070Seric 		printf("Mail queue is empty\n");
7609630Seric 		return;
7619630Seric 	}
7629630Seric 
76310096Seric 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
76410070Seric 	if (nrequests > WLSIZE)
76510070Seric 		printf(", only %d printed", WLSIZE);
76624979Seric 	if (Verbose)
76724979Seric 		printf(")\n--QID-- --Size-- -Priority- -----Q-Time----- --------Sender/Recipient--------\n");
76824979Seric 	else
76924979Seric 		printf(")\n--QID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
7709630Seric 	for (w = WorkQ; w != NULL; w = w->w_next)
7719630Seric 	{
7729630Seric 		struct stat st;
77310070Seric 		auto time_t submittime = 0;
77410070Seric 		long dfsize = -1;
77510108Seric 		char lf[20];
77610108Seric 		char message[MAXLINE];
77724941Seric 		extern bool shouldqueue();
7789630Seric 
77917468Seric 		f = fopen(w->w_name, "r");
78017468Seric 		if (f == NULL)
78117468Seric 		{
78217468Seric 			errno = 0;
78317468Seric 			continue;
78417468Seric 		}
7859630Seric 		printf("%7s", w->w_name + 2);
78623098Seric 		(void) strcpy(lf, w->w_name);
78710070Seric 		lf[0] = 'l';
78810070Seric 		if (stat(lf, &st) >= 0)
78910070Seric 			printf("*");
79024941Seric 		else if (shouldqueue(w->w_pri))
79124941Seric 			printf("X");
79210070Seric 		else
79310070Seric 			printf(" ");
79410070Seric 		errno = 0;
79517468Seric 
79610108Seric 		message[0] = '\0';
7979630Seric 		while (fgets(buf, sizeof buf, f) != NULL)
7989630Seric 		{
7999630Seric 			fixcrlf(buf, TRUE);
8009630Seric 			switch (buf[0])
8019630Seric 			{
80210108Seric 			  case 'M':	/* error message */
80323098Seric 				(void) strcpy(message, &buf[1]);
80410108Seric 				break;
80510108Seric 
8069630Seric 			  case 'S':	/* sender name */
80724979Seric 				if (Verbose)
80824979Seric 					printf("%8ld %10ld %.16s %.37s", dfsize,
80924979Seric 					    w->w_pri, ctime(&submittime),
81024979Seric 					    &buf[1]);
81124979Seric 				else
81224979Seric 					printf("%8ld %.16s %.45s", dfsize,
81324979Seric 					    ctime(&submittime), &buf[1]);
81410108Seric 				if (message[0] != '\0')
81524941Seric 					printf("\n\t\t (%.62s)", message);
8169630Seric 				break;
8179630Seric 
8189630Seric 			  case 'R':	/* recipient name */
81924979Seric 				if (Verbose)
82024979Seric 					printf("\n\t\t\t\t\t     %.37s", &buf[1]);
82124979Seric 				else
82224979Seric 					printf("\n\t\t\t\t  %.45s", &buf[1]);
8239630Seric 				break;
8249630Seric 
8259630Seric 			  case 'T':	/* creation time */
82624941Seric 				submittime = atol(&buf[1]);
8279630Seric 				break;
82810070Seric 
82910070Seric 			  case 'D':	/* data file name */
83010070Seric 				if (stat(&buf[1], &st) >= 0)
83110070Seric 					dfsize = st.st_size;
83210070Seric 				break;
8339630Seric 			}
8349630Seric 		}
83510070Seric 		if (submittime == (time_t) 0)
83610070Seric 			printf(" (no control file)");
8379630Seric 		printf("\n");
83823098Seric 		(void) fclose(f);
8399630Seric 	}
8409630Seric }
8419630Seric 
8425182Seric # endif QUEUE
84317468Seric /*
84417468Seric **  QUEUENAME -- build a file name in the queue directory for this envelope.
84517468Seric **
84617468Seric **	Assigns an id code if one does not already exist.
84717468Seric **	This code is very careful to avoid trashing existing files
84817468Seric **	under any circumstances.
84917468Seric **		We first create an nf file that is only used when
85017468Seric **		assigning an id.  This file is always empty, so that
85117468Seric **		we can never accidently truncate an lf file.
85217468Seric **
85317468Seric **	Parameters:
85417468Seric **		e -- envelope to build it in/from.
85517468Seric **		type -- the file type, used as the first character
85617468Seric **			of the file name.
85717468Seric **
85817468Seric **	Returns:
85917468Seric **		a pointer to the new file name (in a static buffer).
86017468Seric **
86117468Seric **	Side Effects:
86217468Seric **		Will create the lf and qf files if no id code is
86317468Seric **		already assigned.  This will cause the envelope
86417468Seric **		to be modified.
86517468Seric */
86617468Seric 
86717468Seric char *
86817468Seric queuename(e, type)
86917468Seric 	register ENVELOPE *e;
87017468Seric 	char type;
87117468Seric {
87217468Seric 	static char buf[MAXNAME];
87317468Seric 	static int pid = -1;
87417468Seric 	char c1 = 'A';
87517468Seric 	char c2 = 'A';
87617468Seric 
87717468Seric 	if (e->e_id == NULL)
87817468Seric 	{
87917468Seric 		char qf[20];
88017468Seric 		char nf[20];
88117468Seric 		char lf[20];
88217468Seric 
88317468Seric 		/* find a unique id */
88417468Seric 		if (pid != getpid())
88517468Seric 		{
88617468Seric 			/* new process -- start back at "AA" */
88717468Seric 			pid = getpid();
88817468Seric 			c1 = 'A';
88917468Seric 			c2 = 'A' - 1;
89017468Seric 		}
89117468Seric 		(void) sprintf(qf, "qfAA%05d", pid);
89223098Seric 		(void) strcpy(lf, qf);
89317468Seric 		lf[0] = 'l';
89423098Seric 		(void) strcpy(nf, qf);
89517468Seric 		nf[0] = 'n';
89617468Seric 
89717468Seric 		while (c1 < '~' || c2 < 'Z')
89817468Seric 		{
89917468Seric 			int i;
90017468Seric 
90117468Seric 			if (c2 >= 'Z')
90217468Seric 			{
90317468Seric 				c1++;
90417468Seric 				c2 = 'A' - 1;
90517468Seric 			}
90617477Seric 			lf[2] = nf[2] = qf[2] = c1;
90717477Seric 			lf[3] = nf[3] = qf[3] = ++c2;
90817468Seric # ifdef DEBUG
90917468Seric 			if (tTd(7, 20))
91017468Seric 				printf("queuename: trying \"%s\"\n", nf);
91117468Seric # endif DEBUG
91217468Seric 
91317468Seric # ifdef QUEUE
91417468Seric 			if (access(lf, 0) >= 0 || access(qf, 0) >= 0)
91517468Seric 				continue;
91617468Seric 			errno = 0;
91717468Seric 			i = creat(nf, FileMode);
91817468Seric 			if (i < 0)
91917468Seric 			{
92017468Seric 				(void) unlink(nf);	/* kernel bug */
92117468Seric 				continue;
92217468Seric 			}
92317468Seric 			(void) close(i);
92417468Seric 			i = link(nf, lf);
92517468Seric 			(void) unlink(nf);
92617468Seric 			if (i < 0)
92717468Seric 				continue;
92817468Seric 			if (link(lf, qf) >= 0)
92917468Seric 				break;
93017468Seric 			(void) unlink(lf);
93117468Seric # else QUEUE
93217982Seric 			if (close(creat(qf, FileMode)) >= 0)
93317982Seric 				break;
93417468Seric # endif QUEUE
93517468Seric 		}
93617468Seric 		if (c1 >= '~' && c2 >= 'Z')
93717468Seric 		{
93817468Seric 			syserr("queuename: Cannot create \"%s\" in \"%s\"",
93917468Seric 				qf, QueueDir);
94017468Seric 			exit(EX_OSERR);
94117468Seric 		}
94217468Seric 		e->e_id = newstr(&qf[2]);
94317468Seric 		define('i', e->e_id, e);
94417468Seric # ifdef DEBUG
94517468Seric 		if (tTd(7, 1))
94617468Seric 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
94717468Seric # ifdef LOG
94817468Seric 		if (LogLevel > 16)
94917468Seric 			syslog(LOG_DEBUG, "%s: assigned id", e->e_id);
95017468Seric # endif LOG
95117468Seric # endif DEBUG
95217468Seric 	}
95317468Seric 
95417468Seric 	if (type == '\0')
95517468Seric 		return (NULL);
95617468Seric 	(void) sprintf(buf, "%cf%s", type, e->e_id);
95717468Seric # ifdef DEBUG
95817468Seric 	if (tTd(7, 2))
95917468Seric 		printf("queuename: %s\n", buf);
96017468Seric # endif DEBUG
96117468Seric 	return (buf);
96217468Seric }
96317468Seric /*
96417468Seric **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
96517468Seric **
96617468Seric **	Parameters:
96717468Seric **		e -- the envelope to unlock.
96817468Seric **
96917468Seric **	Returns:
97017468Seric **		none
97117468Seric **
97217468Seric **	Side Effects:
97317468Seric **		unlocks the queue for `e'.
97417468Seric */
97517468Seric 
97617468Seric unlockqueue(e)
97717468Seric 	ENVELOPE *e;
97817468Seric {
97917468Seric 	/* remove the transcript */
98017468Seric #ifdef DEBUG
98117468Seric # ifdef LOG
98217468Seric 	if (LogLevel > 19)
98317468Seric 		syslog(LOG_DEBUG, "%s: unlock", e->e_id);
98417468Seric # endif LOG
98517468Seric 	if (!tTd(51, 4))
98617468Seric #endif DEBUG
98717468Seric 		xunlink(queuename(e, 'x'));
98817468Seric 
98917468Seric # ifdef QUEUE
99017468Seric 	/* last but not least, remove the lock */
99117468Seric 	xunlink(queuename(e, 'l'));
99217468Seric # endif QUEUE
99317468Seric }
994