xref: /csrg-svn/usr.sbin/sendmail/src/queue.c (revision 25687)
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*25687Seric static char	SccsId[] = "@(#)queue.c	5.19 (Berkeley) 01/05/86	(no queueing)";
2123116Seric # endif not lint
225182Seric # else QUEUE
234632Seric 
2423116Seric # ifndef lint
25*25687Seric static char	SccsId[] = "@(#)queue.c	5.19 (Berkeley) 01/05/86";
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.
115*25687Seric 	**	Priority and creation time should be first, since
116*25687Seric 	**	they are required by orderq.
1174632Seric 	*/
1184632Seric 
1199377Seric 	/* output message priority */
1209377Seric 	fprintf(tfp, "P%ld\n", e->e_msgpriority);
1219377Seric 
1229630Seric 	/* output creation time */
1239630Seric 	fprintf(tfp, "T%ld\n", e->e_ctime);
1249630Seric 
1254632Seric 	/* output name of data file */
1267812Seric 	fprintf(tfp, "D%s\n", e->e_df);
1274632Seric 
12810108Seric 	/* message from envelope, if it exists */
12910108Seric 	if (e->e_message != NULL)
13010108Seric 		fprintf(tfp, "M%s\n", e->e_message);
13110108Seric 
1324632Seric 	/* output name of sender */
1337812Seric 	fprintf(tfp, "S%s\n", e->e_from.q_paddr);
1344632Seric 
1354632Seric 	/* output list of recipient addresses */
1366980Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1374632Seric 	{
1387763Seric 		if (queueall ? !bitset(QDONTSEND, q->q_flags) :
1397763Seric 			       bitset(QQUEUEUP, q->q_flags))
1408245Seric 		{
1417812Seric 			fprintf(tfp, "R%s\n", q->q_paddr);
1429377Seric 			if (announce)
1439377Seric 			{
1449377Seric 				e->e_to = q->q_paddr;
1459377Seric 				message(Arpa_Info, "queued");
1469377Seric 				if (LogLevel > 4)
1479377Seric 					logdelivery("queued");
1489377Seric 				e->e_to = NULL;
1499377Seric 			}
1509387Seric #ifdef DEBUG
1519387Seric 			if (tTd(40, 1))
1529387Seric 			{
1539387Seric 				printf("queueing ");
1549387Seric 				printaddr(q, FALSE);
1559387Seric 			}
1569387Seric #endif DEBUG
1578245Seric 		}
1584632Seric 	}
1594632Seric 
160*25687Seric 	/* output list of error recipients */
161*25687Seric 	for (q = e->e_errorqueue; q != NULL; q = q->q_next)
162*25687Seric 	{
163*25687Seric 		fprintf(tfp, "E%s\n", q->q_paddr);
164*25687Seric 	}
165*25687Seric 
1669377Seric 	/*
1679377Seric 	**  Output headers for this message.
1689377Seric 	**	Expand macros completely here.  Queue run will deal with
1699377Seric 	**	everything as absolute headers.
1709377Seric 	**		All headers that must be relative to the recipient
1719377Seric 	**		can be cracked later.
17210173Seric 	**	We set up a "null mailer" -- i.e., a mailer that will have
17310173Seric 	**	no effect on the addresses as they are output.
1749377Seric 	*/
1759377Seric 
17610686Seric 	bzero((char *) &nullmailer, sizeof nullmailer);
17710173Seric 	nullmailer.m_r_rwset = nullmailer.m_s_rwset = -1;
17810349Seric 	nullmailer.m_eol = "\n";
17910173Seric 
18016147Seric 	define('g', "\001f", e);
1816980Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
1824632Seric 	{
18310686Seric 		extern bool bitzerop();
18410686Seric 
18512015Seric 		/* don't output null headers */
1864632Seric 		if (h->h_value == NULL || h->h_value[0] == '\0')
1874632Seric 			continue;
18812015Seric 
18912015Seric 		/* don't output resent headers on non-resent messages */
19012015Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
19112015Seric 			continue;
19212015Seric 
19312015Seric 		/* output this header */
1947812Seric 		fprintf(tfp, "H");
19512015Seric 
19612015Seric 		/* if conditional, output the set of conditions */
19710686Seric 		if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags))
19810686Seric 		{
19910686Seric 			int j;
20010686Seric 
20123098Seric 			(void) putc('?', tfp);
20210686Seric 			for (j = '\0'; j <= '\177'; j++)
20310686Seric 				if (bitnset(j, h->h_mflags))
20423098Seric 					(void) putc(j, tfp);
20523098Seric 			(void) putc('?', tfp);
20610686Seric 		}
20712015Seric 
20812015Seric 		/* output the header: expand macros, convert addresses */
2097763Seric 		if (bitset(H_DEFAULT, h->h_flags))
2107763Seric 		{
2117763Seric 			(void) expand(h->h_value, buf, &buf[sizeof buf], e);
2128236Seric 			fprintf(tfp, "%s: %s\n", h->h_field, buf);
2137763Seric 		}
2148245Seric 		else if (bitset(H_FROM|H_RCPT, h->h_flags))
2159348Seric 		{
2169348Seric 			commaize(h, h->h_value, tfp, bitset(EF_OLDSTYLE, e->e_flags),
21710173Seric 				 &nullmailer);
2189348Seric 		}
2197763Seric 		else
2208245Seric 			fprintf(tfp, "%s: %s\n", h->h_field, h->h_value);
2214632Seric 	}
2224632Seric 
2234632Seric 	/*
2244632Seric 	**  Clean up.
2254632Seric 	*/
2264632Seric 
22717477Seric 	(void) fclose(tfp);
22817468Seric 	qf = queuename(e, 'q');
22917468Seric 	if (tf != NULL)
23017468Seric 	{
23117468Seric 		(void) unlink(qf);
23224968Seric 		if (rename(tf, qf) < 0)
23324968Seric 			syserr("cannot unlink(%s, %s), df=%s", tf, qf, e->e_df);
23424968Seric 		errno = 0;
23517468Seric 	}
2367391Seric 
2377677Seric # ifdef LOG
2387677Seric 	/* save log info */
2397878Seric 	if (LogLevel > 15)
2407878Seric 		syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df);
2417677Seric # endif LOG
2424632Seric }
2434632Seric /*
2444632Seric **  RUNQUEUE -- run the jobs in the queue.
2454632Seric **
2464632Seric **	Gets the stuff out of the queue in some presumably logical
2474632Seric **	order and processes them.
2484632Seric **
2494632Seric **	Parameters:
25024941Seric **		forkflag -- TRUE if the queue scanning should be done in
25124941Seric **			a child process.  We double-fork so it is not our
25224941Seric **			child and we don't have to clean up after it.
2534632Seric **
2544632Seric **	Returns:
2554632Seric **		none.
2564632Seric **
2574632Seric **	Side Effects:
2584632Seric **		runs things in the mail queue.
2594632Seric */
2604632Seric 
2614639Seric runqueue(forkflag)
2624639Seric 	bool forkflag;
2634632Seric {
26424953Seric 	extern bool shouldqueue();
26524953Seric 
2667466Seric 	/*
26724953Seric 	**  If no work will ever be selected, don't even bother reading
26824953Seric 	**  the queue.
26924953Seric 	*/
27024953Seric 
27124953Seric 	if (shouldqueue(-100000000L))
27224953Seric 	{
27324953Seric 		if (Verbose)
27424953Seric 			printf("Skipping queue run -- load average too high\n");
27524953Seric 
27624953Seric 		if (forkflag)
27724953Seric 			return;
27824953Seric 		finis();
27924953Seric 	}
28024953Seric 
28124953Seric 	/*
2827466Seric 	**  See if we want to go off and do other useful work.
2837466Seric 	*/
2844639Seric 
2854639Seric 	if (forkflag)
2864639Seric 	{
2877943Seric 		int pid;
2887943Seric 
2897943Seric 		pid = dofork();
2907943Seric 		if (pid != 0)
2914639Seric 		{
29225184Seric 			extern reapchild();
29325184Seric 
2947943Seric 			/* parent -- pick up intermediate zombie */
29525184Seric #ifndef SIGCHLD
2969377Seric 			(void) waitfor(pid);
29725184Seric #else SIGCHLD
29825184Seric 			(void) signal(SIGCHLD, reapchild);
29925184Seric #endif SIGCHLD
3007690Seric 			if (QueueIntvl != 0)
3019348Seric 				(void) setevent(QueueIntvl, runqueue, TRUE);
3024639Seric 			return;
3034639Seric 		}
3047943Seric 		/* child -- double fork */
30525184Seric #ifndef SIGCHLD
3067943Seric 		if (fork() != 0)
3077943Seric 			exit(EX_OK);
30825184Seric #else SIGCHLD
30925184Seric 		(void) signal(SIGCHLD, SIG_DFL);
31025184Seric #endif SIGCHLD
3114639Seric 	}
31224941Seric 
31324941Seric 	setproctitle("running queue");
31424941Seric 
3157876Seric # ifdef LOG
3167876Seric 	if (LogLevel > 11)
3177943Seric 		syslog(LOG_DEBUG, "runqueue %s, pid=%d", QueueDir, getpid());
3187876Seric # endif LOG
3194639Seric 
3207466Seric 	/*
32110205Seric 	**  Release any resources used by the daemon code.
32210205Seric 	*/
32310205Seric 
32410205Seric # ifdef DAEMON
32510205Seric 	clrdaemon();
32610205Seric # endif DAEMON
32710205Seric 
32810205Seric 	/*
3297466Seric 	**  Start making passes through the queue.
3307466Seric 	**	First, read and sort the entire queue.
3317466Seric 	**	Then, process the work in that order.
3327466Seric 	**		But if you take too long, start over.
3337466Seric 	*/
3347466Seric 
3357943Seric 	/* order the existing work requests */
33624954Seric 	(void) orderq(FALSE);
3377690Seric 
3387943Seric 	/* process them once at a time */
3397943Seric 	while (WorkQ != NULL)
3404639Seric 	{
3417943Seric 		WORK *w = WorkQ;
3427881Seric 
3437943Seric 		WorkQ = WorkQ->w_next;
3447943Seric 		dowork(w);
3457943Seric 		free(w->w_name);
3467943Seric 		free((char *) w);
3474639Seric 	}
3487943Seric 	finis();
3494634Seric }
3504634Seric /*
3514632Seric **  ORDERQ -- order the work queue.
3524632Seric **
3534632Seric **	Parameters:
35424941Seric **		doall -- if set, include everything in the queue (even
35524941Seric **			the jobs that cannot be run because the load
35624941Seric **			average is too high).  Otherwise, exclude those
35724941Seric **			jobs.
3584632Seric **
3594632Seric **	Returns:
36010121Seric **		The number of request in the queue (not necessarily
36110121Seric **		the number of requests in WorkQ however).
3624632Seric **
3634632Seric **	Side Effects:
3644632Seric **		Sets WorkQ to the queue of available work, in order.
3654632Seric */
3664632Seric 
367*25687Seric # define NEED_P		001
368*25687Seric # define NEED_T		002
3694632Seric 
37024941Seric orderq(doall)
37124941Seric 	bool doall;
3724632Seric {
3736625Sglickman 	register struct direct *d;
3744632Seric 	register WORK *w;
3756625Sglickman 	DIR *f;
3764632Seric 	register int i;
377*25687Seric 	WORK wlist[QUEUESIZE+1];
37810070Seric 	int wn = -1;
3794632Seric 	extern workcmpf();
3804632Seric 
3814632Seric 	/* clear out old WorkQ */
3824632Seric 	for (w = WorkQ; w != NULL; )
3834632Seric 	{
3844632Seric 		register WORK *nw = w->w_next;
3854632Seric 
3864632Seric 		WorkQ = nw;
3874632Seric 		free(w->w_name);
3884632Seric 		free((char *) w);
3894632Seric 		w = nw;
3904632Seric 	}
3914632Seric 
3924632Seric 	/* open the queue directory */
3938148Seric 	f = opendir(".");
3944632Seric 	if (f == NULL)
3954632Seric 	{
3968148Seric 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
39710070Seric 		return (0);
3984632Seric 	}
3994632Seric 
4004632Seric 	/*
4014632Seric 	**  Read the work directory.
4024632Seric 	*/
4034632Seric 
40410070Seric 	while ((d = readdir(f)) != NULL)
4054632Seric 	{
4069377Seric 		FILE *cf;
4074632Seric 		char lbuf[MAXNAME];
4084632Seric 
4094632Seric 		/* is this an interesting entry? */
4107812Seric 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
4114632Seric 			continue;
4124632Seric 
41310070Seric 		/* yes -- open control file (if not too many files) */
414*25687Seric 		if (++wn >= QUEUESIZE)
41510070Seric 			continue;
4168148Seric 		cf = fopen(d->d_name, "r");
4174632Seric 		if (cf == NULL)
4184632Seric 		{
4197055Seric 			/* this may be some random person sending hir msgs */
4207055Seric 			/* syserr("orderq: cannot open %s", cbuf); */
42110090Seric #ifdef DEBUG
42210090Seric 			if (tTd(41, 2))
42310090Seric 				printf("orderq: cannot open %s (%d)\n",
42410090Seric 					d->d_name, errno);
42510090Seric #endif DEBUG
4267055Seric 			errno = 0;
42710090Seric 			wn--;
4284632Seric 			continue;
4294632Seric 		}
430*25687Seric 		w = &wlist[wn];
431*25687Seric 		w->w_name = newstr(d->d_name);
4324632Seric 
43325027Seric 		/* make sure jobs in creation don't clog queue */
434*25687Seric 		w->w_pri = 0x7fffffff;
435*25687Seric 		w->w_ctime = 0;
43625027Seric 
4374632Seric 		/* extract useful information */
438*25687Seric 		i = NEED_P | NEED_T;
439*25687Seric 		while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL)
4404632Seric 		{
44124954Seric 			extern long atol();
44224954Seric 
44324941Seric 			switch (lbuf[0])
4444632Seric 			{
44524941Seric 			  case 'P':
446*25687Seric 				w->w_pri = atol(&lbuf[1]);
447*25687Seric 				i &= ~NEED_P;
4484632Seric 				break;
44925013Seric 
45025013Seric 			  case 'T':
451*25687Seric 				w->w_ctime = atol(&lbuf[1]);
452*25687Seric 				i &= ~NEED_T;
45325013Seric 				break;
4544632Seric 			}
4554632Seric 		}
4564632Seric 		(void) fclose(cf);
45724953Seric 
458*25687Seric 		if (!doall && shouldqueue(w->w_pri))
45924953Seric 		{
46024953Seric 			/* don't even bother sorting this job in */
46124953Seric 			wn--;
46224953Seric 		}
4634632Seric 	}
4646625Sglickman 	(void) closedir(f);
46510090Seric 	wn++;
4664632Seric 
4674632Seric 	/*
4684632Seric 	**  Sort the work directory.
4694632Seric 	*/
4704632Seric 
471*25687Seric 	qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf);
4724632Seric 
4734632Seric 	/*
4744632Seric 	**  Convert the work list into canonical form.
4759377Seric 	**	Should be turning it into a list of envelopes here perhaps.
4764632Seric 	*/
4774632Seric 
47824981Seric 	WorkQ = NULL;
479*25687Seric 	for (i = min(wn, QUEUESIZE); --i >= 0; )
4804632Seric 	{
4814632Seric 		w = (WORK *) xalloc(sizeof *w);
4824632Seric 		w->w_name = wlist[i].w_name;
4834632Seric 		w->w_pri = wlist[i].w_pri;
48425013Seric 		w->w_ctime = wlist[i].w_ctime;
48524981Seric 		w->w_next = WorkQ;
48624981Seric 		WorkQ = w;
4874632Seric 	}
4884632Seric 
4894632Seric # ifdef DEBUG
4907677Seric 	if (tTd(40, 1))
4914632Seric 	{
4924632Seric 		for (w = WorkQ; w != NULL; w = w->w_next)
4935037Seric 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
4944632Seric 	}
4954632Seric # endif DEBUG
49610070Seric 
49710090Seric 	return (wn);
4984632Seric }
4994632Seric /*
5007677Seric **  WORKCMPF -- compare function for ordering work.
5014632Seric **
5024632Seric **	Parameters:
5034632Seric **		a -- the first argument.
5044632Seric **		b -- the second argument.
5054632Seric **
5064632Seric **	Returns:
50724981Seric **		-1 if a < b
50824981Seric **		 0 if a == b
50924981Seric **		+1 if a > b
5104632Seric **
5114632Seric **	Side Effects:
5124632Seric **		none.
5134632Seric */
5144632Seric 
5154632Seric workcmpf(a, b)
5165037Seric 	register WORK *a;
5175037Seric 	register WORK *b;
5184632Seric {
51925013Seric 	long pa = a->w_pri + a->w_ctime;
52025013Seric 	long pb = b->w_pri + b->w_ctime;
52124941Seric 
52224941Seric 	if (pa == pb)
5234632Seric 		return (0);
52424941Seric 	else if (pa > pb)
52524981Seric 		return (1);
52624981Seric 	else
52710121Seric 		return (-1);
5284632Seric }
5294632Seric /*
5304632Seric **  DOWORK -- do a work request.
5314632Seric **
5324632Seric **	Parameters:
5334632Seric **		w -- the work request to be satisfied.
5344632Seric **
5354632Seric **	Returns:
5364632Seric **		none.
5374632Seric **
5384632Seric **	Side Effects:
5394632Seric **		The work request is satisfied if possible.
5404632Seric */
5414632Seric 
5424632Seric dowork(w)
5434632Seric 	register WORK *w;
5444632Seric {
5454632Seric 	register int i;
54624941Seric 	extern bool shouldqueue();
5474632Seric 
5484632Seric # ifdef DEBUG
5497677Seric 	if (tTd(40, 1))
5505037Seric 		printf("dowork: %s pri %ld\n", w->w_name, w->w_pri);
5514632Seric # endif DEBUG
5524632Seric 
5534632Seric 	/*
55424941Seric 	**  Ignore jobs that are too expensive for the moment.
5554632Seric 	*/
5564632Seric 
55724941Seric 	if (shouldqueue(w->w_pri))
5584632Seric 	{
55924941Seric 		if (Verbose)
56024967Seric 			printf("\nSkipping %s\n", w->w_name + 2);
5614632Seric 		return;
5624632Seric 	}
5634632Seric 
56424941Seric 	/*
56524941Seric 	**  Fork for work.
56624941Seric 	*/
56724941Seric 
56824941Seric 	if (ForkQueueRuns)
56924941Seric 	{
57024941Seric 		i = fork();
57124941Seric 		if (i < 0)
57224941Seric 		{
57324941Seric 			syserr("dowork: cannot fork");
57424941Seric 			return;
57524941Seric 		}
57624941Seric 	}
57724941Seric 	else
57824941Seric 	{
57924941Seric 		i = 0;
58024941Seric 	}
58124941Seric 
5824632Seric 	if (i == 0)
5834632Seric 	{
5844632Seric 		/*
5854632Seric 		**  CHILD
5868148Seric 		**	Lock the control file to avoid duplicate deliveries.
5878148Seric 		**		Then run the file as though we had just read it.
5887350Seric 		**	We save an idea of the temporary name so we
5897350Seric 		**		can recover on interrupt.
5904632Seric 		*/
5914632Seric 
5927763Seric 		/* set basic modes, etc. */
5937356Seric 		(void) alarm(0);
59425612Seric 		clearenvelope(CurEnv, FALSE);
5954632Seric 		QueueRun = TRUE;
5969377Seric 		ErrorMode = EM_MAIL;
5978148Seric 		CurEnv->e_id = &w->w_name[2];
5987876Seric # ifdef LOG
5997876Seric 		if (LogLevel > 11)
6007881Seric 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", CurEnv->e_id,
6017881Seric 			       getpid());
6027876Seric # endif LOG
6037763Seric 
6047763Seric 		/* don't use the headers from sendmail.cf... */
6057763Seric 		CurEnv->e_header = NULL;
6067763Seric 
60717468Seric 		/* lock the control file during processing */
6087812Seric 		if (link(w->w_name, queuename(CurEnv, 'l')) < 0)
6096980Seric 		{
6107812Seric 			/* being processed by another queuer */
6117881Seric # ifdef LOG
6127881Seric 			if (LogLevel > 4)
6137881Seric 				syslog(LOG_DEBUG, "%s: locked", CurEnv->e_id);
6147881Seric # endif LOG
61524941Seric 			if (ForkQueueRuns)
61624941Seric 				exit(EX_OK);
61724941Seric 			else
61824941Seric 				return;
6196980Seric 		}
6206980Seric 
6216980Seric 		/* do basic system initialization */
6224632Seric 		initsys();
6236980Seric 
6246980Seric 		/* read the queue control file */
62517477Seric 		readqf(CurEnv, TRUE);
6269338Seric 		CurEnv->e_flags |= EF_INQUEUE;
6279377Seric 		eatheader(CurEnv);
6286980Seric 
6296980Seric 		/* do the delivery */
6309338Seric 		if (!bitset(EF_FATALERRS, CurEnv->e_flags))
6319282Seric 			sendall(CurEnv, SM_DELIVER);
6326980Seric 
6336980Seric 		/* finish up and exit */
63424941Seric 		if (ForkQueueRuns)
63524941Seric 			finis();
63624941Seric 		else
63724941Seric 			dropenvelope(CurEnv);
6384632Seric 	}
63924941Seric 	else
64024941Seric 	{
64124941Seric 		/*
64224941Seric 		**  Parent -- pick up results.
64324941Seric 		*/
6444632Seric 
64524941Seric 		errno = 0;
64624941Seric 		(void) waitfor(i);
64724941Seric 	}
6484632Seric }
6494632Seric /*
6504632Seric **  READQF -- read queue file and set up environment.
6514632Seric **
6524632Seric **	Parameters:
6539377Seric **		e -- the envelope of the job to run.
6549630Seric **		full -- if set, read in all information.  Otherwise just
6559630Seric **			read in info needed for a queue print.
6564632Seric **
6574632Seric **	Returns:
6584632Seric **		none.
6594632Seric **
6604632Seric **	Side Effects:
6614632Seric **		cf is read and created as the current job, as though
6624632Seric **		we had been invoked by argument.
6634632Seric */
6644632Seric 
66517477Seric readqf(e, full)
6669377Seric 	register ENVELOPE *e;
6679630Seric 	bool full;
6684632Seric {
66917477Seric 	char *qf;
67017477Seric 	register FILE *qfp;
6717785Seric 	char buf[MAXFIELD];
6729348Seric 	extern char *fgetfolded();
67324954Seric 	extern long atol();
6744632Seric 
6754632Seric 	/*
67617468Seric 	**  Read and process the file.
6774632Seric 	*/
6784632Seric 
67917477Seric 	qf = queuename(e, 'q');
68017477Seric 	qfp = fopen(qf, "r");
68117477Seric 	if (qfp == NULL)
68217477Seric 	{
68317477Seric 		syserr("readqf: no control file %s", qf);
68417477Seric 		return;
68517477Seric 	}
68617477Seric 	FileName = qf;
6879377Seric 	LineNumber = 0;
6889630Seric 	if (Verbose && full)
6899377Seric 		printf("\nRunning %s\n", e->e_id);
69017468Seric 	while (fgetfolded(buf, sizeof buf, qfp) != NULL)
6914632Seric 	{
6924632Seric 		switch (buf[0])
6934632Seric 		{
6944632Seric 		  case 'R':		/* specify recipient */
6959618Seric 			sendtolist(&buf[1], (ADDRESS *) NULL, &e->e_sendqueue);
6964632Seric 			break;
6974632Seric 
698*25687Seric 		  case 'E':		/* specify error recipient */
699*25687Seric 			sendtolist(&buf[1], (ADDRESS *) NULL, &e->e_errorqueue);
700*25687Seric 			break;
701*25687Seric 
7024632Seric 		  case 'H':		/* header */
7039630Seric 			if (full)
7049630Seric 				(void) chompheader(&buf[1], FALSE);
7054632Seric 			break;
7064632Seric 
70710108Seric 		  case 'M':		/* message */
70810108Seric 			e->e_message = newstr(&buf[1]);
70910108Seric 			break;
71010108Seric 
7114632Seric 		  case 'S':		/* sender */
7124634Seric 			setsender(newstr(&buf[1]));
7134632Seric 			break;
7144632Seric 
7154632Seric 		  case 'D':		/* data file name */
7169630Seric 			if (!full)
7179630Seric 				break;
7189377Seric 			e->e_df = newstr(&buf[1]);
7199544Seric 			e->e_dfp = fopen(e->e_df, "r");
7209544Seric 			if (e->e_dfp == NULL)
7219377Seric 				syserr("readqf: cannot open %s", e->e_df);
7224632Seric 			break;
7234632Seric 
7247860Seric 		  case 'T':		/* init time */
72524941Seric 			e->e_ctime = atol(&buf[1]);
7264632Seric 			break;
7274632Seric 
7284634Seric 		  case 'P':		/* message priority */
72925008Seric 			e->e_msgpriority = atol(&buf[1]) + WkTimeFact;
7304634Seric 			break;
7314634Seric 
73224941Seric 		  case '\0':		/* blank line; ignore */
73324941Seric 			break;
73424941Seric 
7354632Seric 		  default:
73624941Seric 			syserr("readqf(%s:%d): bad line \"%s\"", e->e_id,
73724941Seric 				LineNumber, buf);
7384632Seric 			break;
7394632Seric 		}
7404632Seric 	}
7419377Seric 
74224954Seric 	(void) fclose(qfp);
7439377Seric 	FileName = NULL;
74424941Seric 
74524941Seric 	/*
74624941Seric 	**  If we haven't read any lines, this queue file is empty.
74724941Seric 	**  Arrange to remove it without referencing any null pointers.
74824941Seric 	*/
74924941Seric 
75024941Seric 	if (LineNumber == 0)
75124941Seric 	{
75224941Seric 		errno = 0;
75324941Seric 		e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE;
75424941Seric 	}
7554632Seric }
7564632Seric /*
7579630Seric **  PRINTQUEUE -- print out a representation of the mail queue
7589630Seric **
7599630Seric **	Parameters:
7609630Seric **		none.
7619630Seric **
7629630Seric **	Returns:
7639630Seric **		none.
7649630Seric **
7659630Seric **	Side Effects:
7669630Seric **		Prints a listing of the mail queue on the standard output.
7679630Seric */
7685182Seric 
7699630Seric printqueue()
7709630Seric {
7719630Seric 	register WORK *w;
7729630Seric 	FILE *f;
77310070Seric 	int nrequests;
7749630Seric 	char buf[MAXLINE];
7759630Seric 
7769630Seric 	/*
7779630Seric 	**  Read and order the queue.
7789630Seric 	*/
7799630Seric 
78024941Seric 	nrequests = orderq(TRUE);
7819630Seric 
7829630Seric 	/*
7839630Seric 	**  Print the work list that we have read.
7849630Seric 	*/
7859630Seric 
7869630Seric 	/* first see if there is anything */
78710070Seric 	if (nrequests <= 0)
7889630Seric 	{
78910070Seric 		printf("Mail queue is empty\n");
7909630Seric 		return;
7919630Seric 	}
7929630Seric 
79310096Seric 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
794*25687Seric 	if (nrequests > QUEUESIZE)
795*25687Seric 		printf(", only %d printed", QUEUESIZE);
79624979Seric 	if (Verbose)
79725032Seric 		printf(")\n--QID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n");
79824979Seric 	else
79924979Seric 		printf(")\n--QID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
8009630Seric 	for (w = WorkQ; w != NULL; w = w->w_next)
8019630Seric 	{
8029630Seric 		struct stat st;
80310070Seric 		auto time_t submittime = 0;
80410070Seric 		long dfsize = -1;
80510108Seric 		char lf[20];
80610108Seric 		char message[MAXLINE];
80724941Seric 		extern bool shouldqueue();
8089630Seric 
80917468Seric 		f = fopen(w->w_name, "r");
81017468Seric 		if (f == NULL)
81117468Seric 		{
81217468Seric 			errno = 0;
81317468Seric 			continue;
81417468Seric 		}
8159630Seric 		printf("%7s", w->w_name + 2);
81623098Seric 		(void) strcpy(lf, w->w_name);
81710070Seric 		lf[0] = 'l';
81810070Seric 		if (stat(lf, &st) >= 0)
81910070Seric 			printf("*");
82024941Seric 		else if (shouldqueue(w->w_pri))
82124941Seric 			printf("X");
82210070Seric 		else
82310070Seric 			printf(" ");
82410070Seric 		errno = 0;
82517468Seric 
82610108Seric 		message[0] = '\0';
8279630Seric 		while (fgets(buf, sizeof buf, f) != NULL)
8289630Seric 		{
8299630Seric 			fixcrlf(buf, TRUE);
8309630Seric 			switch (buf[0])
8319630Seric 			{
83210108Seric 			  case 'M':	/* error message */
83323098Seric 				(void) strcpy(message, &buf[1]);
83410108Seric 				break;
83510108Seric 
8369630Seric 			  case 'S':	/* sender name */
83724979Seric 				if (Verbose)
83825027Seric 					printf("%8ld %10ld %.12s %.38s", dfsize,
83925027Seric 					    w->w_pri, ctime(&submittime) + 4,
84024979Seric 					    &buf[1]);
84124979Seric 				else
84224979Seric 					printf("%8ld %.16s %.45s", dfsize,
84324979Seric 					    ctime(&submittime), &buf[1]);
84410108Seric 				if (message[0] != '\0')
84525027Seric 					printf("\n\t\t (%.60s)", message);
8469630Seric 				break;
8479630Seric 
8489630Seric 			  case 'R':	/* recipient name */
84924979Seric 				if (Verbose)
85025027Seric 					printf("\n\t\t\t\t\t %.38s", &buf[1]);
85124979Seric 				else
85224979Seric 					printf("\n\t\t\t\t  %.45s", &buf[1]);
8539630Seric 				break;
8549630Seric 
8559630Seric 			  case 'T':	/* creation time */
85624941Seric 				submittime = atol(&buf[1]);
8579630Seric 				break;
85810070Seric 
85910070Seric 			  case 'D':	/* data file name */
86010070Seric 				if (stat(&buf[1], &st) >= 0)
86110070Seric 					dfsize = st.st_size;
86210070Seric 				break;
8639630Seric 			}
8649630Seric 		}
86510070Seric 		if (submittime == (time_t) 0)
86610070Seric 			printf(" (no control file)");
8679630Seric 		printf("\n");
86823098Seric 		(void) fclose(f);
8699630Seric 	}
8709630Seric }
8719630Seric 
8725182Seric # endif QUEUE
87317468Seric /*
87417468Seric **  QUEUENAME -- build a file name in the queue directory for this envelope.
87517468Seric **
87617468Seric **	Assigns an id code if one does not already exist.
87717468Seric **	This code is very careful to avoid trashing existing files
87817468Seric **	under any circumstances.
87917468Seric **		We first create an nf file that is only used when
88017468Seric **		assigning an id.  This file is always empty, so that
88117468Seric **		we can never accidently truncate an lf file.
88217468Seric **
88317468Seric **	Parameters:
88417468Seric **		e -- envelope to build it in/from.
88517468Seric **		type -- the file type, used as the first character
88617468Seric **			of the file name.
88717468Seric **
88817468Seric **	Returns:
88917468Seric **		a pointer to the new file name (in a static buffer).
89017468Seric **
89117468Seric **	Side Effects:
89217468Seric **		Will create the lf and qf files if no id code is
89317468Seric **		already assigned.  This will cause the envelope
89417468Seric **		to be modified.
89517468Seric */
89617468Seric 
89717468Seric char *
89817468Seric queuename(e, type)
89917468Seric 	register ENVELOPE *e;
90017468Seric 	char type;
90117468Seric {
90217468Seric 	static char buf[MAXNAME];
90317468Seric 	static int pid = -1;
90417468Seric 	char c1 = 'A';
90517468Seric 	char c2 = 'A';
90617468Seric 
90717468Seric 	if (e->e_id == NULL)
90817468Seric 	{
90917468Seric 		char qf[20];
91017468Seric 		char nf[20];
91117468Seric 		char lf[20];
91217468Seric 
91317468Seric 		/* find a unique id */
91417468Seric 		if (pid != getpid())
91517468Seric 		{
91617468Seric 			/* new process -- start back at "AA" */
91717468Seric 			pid = getpid();
91817468Seric 			c1 = 'A';
91917468Seric 			c2 = 'A' - 1;
92017468Seric 		}
92117468Seric 		(void) sprintf(qf, "qfAA%05d", pid);
92223098Seric 		(void) strcpy(lf, qf);
92317468Seric 		lf[0] = 'l';
92423098Seric 		(void) strcpy(nf, qf);
92517468Seric 		nf[0] = 'n';
92617468Seric 
92717468Seric 		while (c1 < '~' || c2 < 'Z')
92817468Seric 		{
92917468Seric 			int i;
93017468Seric 
93117468Seric 			if (c2 >= 'Z')
93217468Seric 			{
93317468Seric 				c1++;
93417468Seric 				c2 = 'A' - 1;
93517468Seric 			}
93617477Seric 			lf[2] = nf[2] = qf[2] = c1;
93717477Seric 			lf[3] = nf[3] = qf[3] = ++c2;
93817468Seric # ifdef DEBUG
93917468Seric 			if (tTd(7, 20))
94017468Seric 				printf("queuename: trying \"%s\"\n", nf);
94117468Seric # endif DEBUG
94217468Seric 
94317468Seric # ifdef QUEUE
94417468Seric 			if (access(lf, 0) >= 0 || access(qf, 0) >= 0)
94517468Seric 				continue;
94617468Seric 			errno = 0;
94717468Seric 			i = creat(nf, FileMode);
94817468Seric 			if (i < 0)
94917468Seric 			{
95017468Seric 				(void) unlink(nf);	/* kernel bug */
95117468Seric 				continue;
95217468Seric 			}
95317468Seric 			(void) close(i);
95417468Seric 			i = link(nf, lf);
95517468Seric 			(void) unlink(nf);
95617468Seric 			if (i < 0)
95717468Seric 				continue;
95817468Seric 			if (link(lf, qf) >= 0)
95917468Seric 				break;
96017468Seric 			(void) unlink(lf);
96117468Seric # else QUEUE
96217982Seric 			if (close(creat(qf, FileMode)) >= 0)
96317982Seric 				break;
96417468Seric # endif QUEUE
96517468Seric 		}
96617468Seric 		if (c1 >= '~' && c2 >= 'Z')
96717468Seric 		{
96817468Seric 			syserr("queuename: Cannot create \"%s\" in \"%s\"",
96917468Seric 				qf, QueueDir);
97017468Seric 			exit(EX_OSERR);
97117468Seric 		}
97217468Seric 		e->e_id = newstr(&qf[2]);
97317468Seric 		define('i', e->e_id, e);
97417468Seric # ifdef DEBUG
97517468Seric 		if (tTd(7, 1))
97617468Seric 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
97717468Seric # ifdef LOG
97817468Seric 		if (LogLevel > 16)
97917468Seric 			syslog(LOG_DEBUG, "%s: assigned id", e->e_id);
98017468Seric # endif LOG
98117468Seric # endif DEBUG
98217468Seric 	}
98317468Seric 
98417468Seric 	if (type == '\0')
98517468Seric 		return (NULL);
98617468Seric 	(void) sprintf(buf, "%cf%s", type, e->e_id);
98717468Seric # ifdef DEBUG
98817468Seric 	if (tTd(7, 2))
98917468Seric 		printf("queuename: %s\n", buf);
99017468Seric # endif DEBUG
99117468Seric 	return (buf);
99217468Seric }
99317468Seric /*
99417468Seric **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
99517468Seric **
99617468Seric **	Parameters:
99717468Seric **		e -- the envelope to unlock.
99817468Seric **
99917468Seric **	Returns:
100017468Seric **		none
100117468Seric **
100217468Seric **	Side Effects:
100317468Seric **		unlocks the queue for `e'.
100417468Seric */
100517468Seric 
100617468Seric unlockqueue(e)
100717468Seric 	ENVELOPE *e;
100817468Seric {
100917468Seric 	/* remove the transcript */
101017468Seric #ifdef DEBUG
101117468Seric # ifdef LOG
101217468Seric 	if (LogLevel > 19)
101317468Seric 		syslog(LOG_DEBUG, "%s: unlock", e->e_id);
101417468Seric # endif LOG
101517468Seric 	if (!tTd(51, 4))
101617468Seric #endif DEBUG
101717468Seric 		xunlink(queuename(e, 'x'));
101817468Seric 
101917468Seric # ifdef QUEUE
102017468Seric 	/* last but not least, remove the lock */
102117468Seric 	xunlink(queuename(e, 'l'));
102217468Seric # endif QUEUE
102317468Seric }
1024