xref: /csrg-svn/usr.sbin/sendmail/src/queue.c (revision 24981)
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*24981Seric static char	SccsId[] = "@(#)queue.c	5.12 (Berkeley) 09/21/85	(no queueing)";
2123116Seric # endif not lint
225182Seric # else QUEUE
234632Seric 
2423116Seric # ifndef lint
25*24981Seric static char	SccsId[] = "@(#)queue.c	5.12 (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 */
369377Seric 	struct work	*w_next;	/* next in queue */
379377Seric };
389377Seric 
399377Seric typedef struct work	WORK;
409377Seric 
419377Seric WORK	*WorkQ;			/* queue of things to be done */
429377Seric /*
434632Seric **  QUEUEUP -- queue a message up for future transmission.
444632Seric **
454632Seric **	Parameters:
466980Seric **		e -- the envelope to queue up.
476999Seric **		queueall -- if TRUE, queue all addresses, rather than
486999Seric **			just those with the QQUEUEUP flag set.
499377Seric **		announce -- if TRUE, tell when you are queueing up.
504632Seric **
514632Seric **	Returns:
524632Seric **		none.
534632Seric **
544632Seric **	Side Effects:
559377Seric **		The current request are saved in a control file.
564632Seric */
574632Seric 
589377Seric queueup(e, queueall, announce)
596980Seric 	register ENVELOPE *e;
606999Seric 	bool queueall;
619377Seric 	bool announce;
624632Seric {
637812Seric 	char *tf;
647812Seric 	char *qf;
657763Seric 	char buf[MAXLINE];
667812Seric 	register FILE *tfp;
674632Seric 	register HDR *h;
685007Seric 	register ADDRESS *q;
6910173Seric 	MAILER nullmailer;
704632Seric 
715037Seric 	/*
7217477Seric 	**  Create control file.
735037Seric 	*/
744632Seric 
7517477Seric 	tf = newstr(queuename(e, 't'));
7617477Seric 	tfp = fopen(tf, "w");
777812Seric 	if (tfp == NULL)
784632Seric 	{
7917477Seric 		syserr("queueup: cannot create temp file %s", tf);
8017477Seric 		return;
814632Seric 	}
8217477Seric 	(void) chmod(tf, FileMode);
834632Seric 
844632Seric # ifdef DEBUG
857677Seric 	if (tTd(40, 1))
8617468Seric 		printf("queueing %s\n", e->e_id);
874632Seric # endif DEBUG
884632Seric 
894632Seric 	/*
906980Seric 	**  If there is no data file yet, create one.
916980Seric 	*/
926980Seric 
936980Seric 	if (e->e_df == NULL)
946980Seric 	{
956980Seric 		register FILE *dfp;
969389Seric 		extern putbody();
976980Seric 
987812Seric 		e->e_df = newstr(queuename(e, 'd'));
996980Seric 		dfp = fopen(e->e_df, "w");
1006980Seric 		if (dfp == NULL)
1016980Seric 		{
1026980Seric 			syserr("queueup: cannot create %s", e->e_df);
1037812Seric 			(void) fclose(tfp);
1046980Seric 			return;
1056980Seric 		}
1069048Seric 		(void) chmod(e->e_df, FileMode);
10710173Seric 		(*e->e_putbody)(dfp, ProgMailer, e);
1087009Seric 		(void) fclose(dfp);
1099389Seric 		e->e_putbody = putbody;
1106980Seric 	}
1116980Seric 
1126980Seric 	/*
1134632Seric 	**  Output future work requests.
1149377Seric 	**	Priority should be first, since it is read by orderq.
1154632Seric 	*/
1164632Seric 
1179377Seric 	/* output message priority */
1189377Seric 	fprintf(tfp, "P%ld\n", e->e_msgpriority);
1199377Seric 
1209630Seric 	/* output creation time */
1219630Seric 	fprintf(tfp, "T%ld\n", e->e_ctime);
1229630Seric 
1234632Seric 	/* output name of data file */
1247812Seric 	fprintf(tfp, "D%s\n", e->e_df);
1254632Seric 
12610108Seric 	/* message from envelope, if it exists */
12710108Seric 	if (e->e_message != NULL)
12810108Seric 		fprintf(tfp, "M%s\n", e->e_message);
12910108Seric 
1304632Seric 	/* output name of sender */
1317812Seric 	fprintf(tfp, "S%s\n", e->e_from.q_paddr);
1324632Seric 
1334632Seric 	/* output list of recipient addresses */
1346980Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1354632Seric 	{
1367763Seric 		if (queueall ? !bitset(QDONTSEND, q->q_flags) :
1377763Seric 			       bitset(QQUEUEUP, q->q_flags))
1388245Seric 		{
1397812Seric 			fprintf(tfp, "R%s\n", q->q_paddr);
1409377Seric 			if (announce)
1419377Seric 			{
1429377Seric 				e->e_to = q->q_paddr;
1439377Seric 				message(Arpa_Info, "queued");
1449377Seric 				if (LogLevel > 4)
1459377Seric 					logdelivery("queued");
1469377Seric 				e->e_to = NULL;
1479377Seric 			}
1489387Seric #ifdef DEBUG
1499387Seric 			if (tTd(40, 1))
1509387Seric 			{
1519387Seric 				printf("queueing ");
1529387Seric 				printaddr(q, FALSE);
1539387Seric 			}
1549387Seric #endif DEBUG
1558245Seric 		}
1564632Seric 	}
1574632Seric 
1589377Seric 	/*
1599377Seric 	**  Output headers for this message.
1609377Seric 	**	Expand macros completely here.  Queue run will deal with
1619377Seric 	**	everything as absolute headers.
1629377Seric 	**		All headers that must be relative to the recipient
1639377Seric 	**		can be cracked later.
16410173Seric 	**	We set up a "null mailer" -- i.e., a mailer that will have
16510173Seric 	**	no effect on the addresses as they are output.
1669377Seric 	*/
1679377Seric 
16810686Seric 	bzero((char *) &nullmailer, sizeof nullmailer);
16910173Seric 	nullmailer.m_r_rwset = nullmailer.m_s_rwset = -1;
17010349Seric 	nullmailer.m_eol = "\n";
17110173Seric 
17216147Seric 	define('g', "\001f", e);
1736980Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
1744632Seric 	{
17510686Seric 		extern bool bitzerop();
17610686Seric 
17712015Seric 		/* don't output null headers */
1784632Seric 		if (h->h_value == NULL || h->h_value[0] == '\0')
1794632Seric 			continue;
18012015Seric 
18112015Seric 		/* don't output resent headers on non-resent messages */
18212015Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
18312015Seric 			continue;
18412015Seric 
18512015Seric 		/* output this header */
1867812Seric 		fprintf(tfp, "H");
18712015Seric 
18812015Seric 		/* if conditional, output the set of conditions */
18910686Seric 		if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags))
19010686Seric 		{
19110686Seric 			int j;
19210686Seric 
19323098Seric 			(void) putc('?', tfp);
19410686Seric 			for (j = '\0'; j <= '\177'; j++)
19510686Seric 				if (bitnset(j, h->h_mflags))
19623098Seric 					(void) putc(j, tfp);
19723098Seric 			(void) putc('?', tfp);
19810686Seric 		}
19912015Seric 
20012015Seric 		/* output the header: expand macros, convert addresses */
2017763Seric 		if (bitset(H_DEFAULT, h->h_flags))
2027763Seric 		{
2037763Seric 			(void) expand(h->h_value, buf, &buf[sizeof buf], e);
2048236Seric 			fprintf(tfp, "%s: %s\n", h->h_field, buf);
2057763Seric 		}
2068245Seric 		else if (bitset(H_FROM|H_RCPT, h->h_flags))
2079348Seric 		{
2089348Seric 			commaize(h, h->h_value, tfp, bitset(EF_OLDSTYLE, e->e_flags),
20910173Seric 				 &nullmailer);
2109348Seric 		}
2117763Seric 		else
2128245Seric 			fprintf(tfp, "%s: %s\n", h->h_field, h->h_value);
2134632Seric 	}
2144632Seric 
2154632Seric 	/*
2164632Seric 	**  Clean up.
2174632Seric 	*/
2184632Seric 
21917477Seric 	(void) fclose(tfp);
22017468Seric 	qf = queuename(e, 'q');
22117468Seric 	if (tf != NULL)
22217468Seric 	{
22317468Seric 		(void) unlink(qf);
22424968Seric 		if (rename(tf, qf) < 0)
22524968Seric 			syserr("cannot unlink(%s, %s), df=%s", tf, qf, e->e_df);
22624968Seric 		errno = 0;
22717468Seric 	}
2287391Seric 
2297677Seric # ifdef LOG
2307677Seric 	/* save log info */
2317878Seric 	if (LogLevel > 15)
2327878Seric 		syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df);
2337677Seric # endif LOG
2344632Seric }
2354632Seric /*
2364632Seric **  RUNQUEUE -- run the jobs in the queue.
2374632Seric **
2384632Seric **	Gets the stuff out of the queue in some presumably logical
2394632Seric **	order and processes them.
2404632Seric **
2414632Seric **	Parameters:
24224941Seric **		forkflag -- TRUE if the queue scanning should be done in
24324941Seric **			a child process.  We double-fork so it is not our
24424941Seric **			child and we don't have to clean up after it.
2454632Seric **
2464632Seric **	Returns:
2474632Seric **		none.
2484632Seric **
2494632Seric **	Side Effects:
2504632Seric **		runs things in the mail queue.
2514632Seric */
2524632Seric 
2534639Seric runqueue(forkflag)
2544639Seric 	bool forkflag;
2554632Seric {
25624953Seric 	extern bool shouldqueue();
25724953Seric 
2587466Seric 	/*
25924953Seric 	**  If no work will ever be selected, don't even bother reading
26024953Seric 	**  the queue.
26124953Seric 	*/
26224953Seric 
26324953Seric 	if (shouldqueue(-100000000L))
26424953Seric 	{
26524953Seric 		if (Verbose)
26624953Seric 			printf("Skipping queue run -- load average too high\n");
26724953Seric 
26824953Seric 		if (forkflag)
26924953Seric 			return;
27024953Seric 		finis();
27124953Seric 	}
27224953Seric 
27324953Seric 	/*
2747466Seric 	**  See if we want to go off and do other useful work.
2757466Seric 	*/
2764639Seric 
2774639Seric 	if (forkflag)
2784639Seric 	{
2797943Seric 		int pid;
2807943Seric 
2817943Seric 		pid = dofork();
2827943Seric 		if (pid != 0)
2834639Seric 		{
2847943Seric 			/* parent -- pick up intermediate zombie */
2859377Seric 			(void) waitfor(pid);
2867690Seric 			if (QueueIntvl != 0)
2879348Seric 				(void) setevent(QueueIntvl, runqueue, TRUE);
2884639Seric 			return;
2894639Seric 		}
2907943Seric 		/* child -- double fork */
2917943Seric 		if (fork() != 0)
2927943Seric 			exit(EX_OK);
2934639Seric 	}
29424941Seric 
29524941Seric 	setproctitle("running queue");
29624941Seric 
2977876Seric # ifdef LOG
2987876Seric 	if (LogLevel > 11)
2997943Seric 		syslog(LOG_DEBUG, "runqueue %s, pid=%d", QueueDir, getpid());
3007876Seric # endif LOG
3014639Seric 
3027466Seric 	/*
30310205Seric 	**  Release any resources used by the daemon code.
30410205Seric 	*/
30510205Seric 
30610205Seric # ifdef DAEMON
30710205Seric 	clrdaemon();
30810205Seric # endif DAEMON
30910205Seric 
31010205Seric 	/*
3117466Seric 	**  Start making passes through the queue.
3127466Seric 	**	First, read and sort the entire queue.
3137466Seric 	**	Then, process the work in that order.
3147466Seric 	**		But if you take too long, start over.
3157466Seric 	*/
3167466Seric 
3177943Seric 	/* order the existing work requests */
31824954Seric 	(void) orderq(FALSE);
3197690Seric 
3207943Seric 	/* process them once at a time */
3217943Seric 	while (WorkQ != NULL)
3224639Seric 	{
3237943Seric 		WORK *w = WorkQ;
3247881Seric 
3257943Seric 		WorkQ = WorkQ->w_next;
3267943Seric 		dowork(w);
3277943Seric 		free(w->w_name);
3287943Seric 		free((char *) w);
3294639Seric 	}
3307943Seric 	finis();
3314634Seric }
3324634Seric /*
3334632Seric **  ORDERQ -- order the work queue.
3344632Seric **
3354632Seric **	Parameters:
33624941Seric **		doall -- if set, include everything in the queue (even
33724941Seric **			the jobs that cannot be run because the load
33824941Seric **			average is too high).  Otherwise, exclude those
33924941Seric **			jobs.
3404632Seric **
3414632Seric **	Returns:
34210121Seric **		The number of request in the queue (not necessarily
34310121Seric **		the number of requests in WorkQ however).
3444632Seric **
3454632Seric **	Side Effects:
3464632Seric **		Sets WorkQ to the queue of available work, in order.
3474632Seric */
3484632Seric 
3494632Seric # define WLSIZE		120	/* max size of worklist per sort */
3504632Seric 
35124941Seric orderq(doall)
35224941Seric 	bool doall;
3534632Seric {
3546625Sglickman 	register struct direct *d;
3554632Seric 	register WORK *w;
3566625Sglickman 	DIR *f;
3574632Seric 	register int i;
35810121Seric 	WORK wlist[WLSIZE+1];
35910070Seric 	int wn = -1;
3604632Seric 	extern workcmpf();
3614632Seric 
3624632Seric 	/* clear out old WorkQ */
3634632Seric 	for (w = WorkQ; w != NULL; )
3644632Seric 	{
3654632Seric 		register WORK *nw = w->w_next;
3664632Seric 
3674632Seric 		WorkQ = nw;
3684632Seric 		free(w->w_name);
3694632Seric 		free((char *) w);
3704632Seric 		w = nw;
3714632Seric 	}
3724632Seric 
3734632Seric 	/* open the queue directory */
3748148Seric 	f = opendir(".");
3754632Seric 	if (f == NULL)
3764632Seric 	{
3778148Seric 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
37810070Seric 		return (0);
3794632Seric 	}
3804632Seric 
3814632Seric 	/*
3824632Seric 	**  Read the work directory.
3834632Seric 	*/
3844632Seric 
38510070Seric 	while ((d = readdir(f)) != NULL)
3864632Seric 	{
3879377Seric 		FILE *cf;
3884632Seric 		char lbuf[MAXNAME];
3894632Seric 
3904632Seric 		/* is this an interesting entry? */
3917812Seric 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
3924632Seric 			continue;
3934632Seric 
39410070Seric 		/* yes -- open control file (if not too many files) */
39510070Seric 		if (++wn >= WLSIZE)
39610070Seric 			continue;
3978148Seric 		cf = fopen(d->d_name, "r");
3984632Seric 		if (cf == NULL)
3994632Seric 		{
4007055Seric 			/* this may be some random person sending hir msgs */
4017055Seric 			/* syserr("orderq: cannot open %s", cbuf); */
40210090Seric #ifdef DEBUG
40310090Seric 			if (tTd(41, 2))
40410090Seric 				printf("orderq: cannot open %s (%d)\n",
40510090Seric 					d->d_name, errno);
40610090Seric #endif DEBUG
4077055Seric 			errno = 0;
40810090Seric 			wn--;
4094632Seric 			continue;
4104632Seric 		}
4118148Seric 		wlist[wn].w_name = newstr(d->d_name);
4124632Seric 
4134632Seric 		/* extract useful information */
4144632Seric 		while (fgets(lbuf, sizeof lbuf, cf) != NULL)
4154632Seric 		{
41624954Seric 			extern long atol();
41724954Seric 
41824941Seric 			switch (lbuf[0])
4194632Seric 			{
42024941Seric 			  case 'P':
42124941Seric 				wlist[wn].w_pri = atol(&lbuf[1]);
4224632Seric 				break;
4234632Seric 			}
4244632Seric 		}
4254632Seric 		(void) fclose(cf);
42624953Seric 
42724953Seric 		if (!doall && shouldqueue(wlist[wn].w_pri))
42824953Seric 		{
42924953Seric 			/* don't even bother sorting this job in */
43024953Seric 			wn--;
43124953Seric 		}
4324632Seric 	}
4336625Sglickman 	(void) closedir(f);
43410090Seric 	wn++;
4354632Seric 
4364632Seric 	/*
4374632Seric 	**  Sort the work directory.
4384632Seric 	*/
4394632Seric 
44023116Seric 	qsort((char *) wlist, min(wn, WLSIZE), sizeof *wlist, workcmpf);
4414632Seric 
4424632Seric 	/*
4434632Seric 	**  Convert the work list into canonical form.
4449377Seric 	**	Should be turning it into a list of envelopes here perhaps.
4454632Seric 	*/
4464632Seric 
447*24981Seric 	WorkQ = NULL;
44810121Seric 	for (i = min(wn, WLSIZE); --i >= 0; )
4494632Seric 	{
4504632Seric 		w = (WORK *) xalloc(sizeof *w);
4514632Seric 		w->w_name = wlist[i].w_name;
4524632Seric 		w->w_pri = wlist[i].w_pri;
453*24981Seric 		w->w_next = WorkQ;
454*24981Seric 		WorkQ = w;
4554632Seric 	}
4564632Seric 
4574632Seric # ifdef DEBUG
4587677Seric 	if (tTd(40, 1))
4594632Seric 	{
4604632Seric 		for (w = WorkQ; w != NULL; w = w->w_next)
4615037Seric 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
4624632Seric 	}
4634632Seric # endif DEBUG
46410070Seric 
46510090Seric 	return (wn);
4664632Seric }
4674632Seric /*
4687677Seric **  WORKCMPF -- compare function for ordering work.
4694632Seric **
4704632Seric **	Parameters:
4714632Seric **		a -- the first argument.
4724632Seric **		b -- the second argument.
4734632Seric **
4744632Seric **	Returns:
475*24981Seric **		-1 if a < b
476*24981Seric **		 0 if a == b
477*24981Seric **		+1 if a > b
4784632Seric **
4794632Seric **	Side Effects:
4804632Seric **		none.
4814632Seric */
4824632Seric 
4834632Seric workcmpf(a, b)
4845037Seric 	register WORK *a;
4855037Seric 	register WORK *b;
4864632Seric {
487*24981Seric 	long pa = a->w_pri;
488*24981Seric 	long pb = b->w_pri;
48924941Seric 
49024941Seric 	if (pa == pb)
4914632Seric 		return (0);
49224941Seric 	else if (pa > pb)
493*24981Seric 		return (1);
494*24981Seric 	else
49510121Seric 		return (-1);
4964632Seric }
4974632Seric /*
4984632Seric **  DOWORK -- do a work request.
4994632Seric **
5004632Seric **	Parameters:
5014632Seric **		w -- the work request to be satisfied.
5024632Seric **
5034632Seric **	Returns:
5044632Seric **		none.
5054632Seric **
5064632Seric **	Side Effects:
5074632Seric **		The work request is satisfied if possible.
5084632Seric */
5094632Seric 
5104632Seric dowork(w)
5114632Seric 	register WORK *w;
5124632Seric {
5134632Seric 	register int i;
51424941Seric 	extern bool shouldqueue();
5154632Seric 
5164632Seric # ifdef DEBUG
5177677Seric 	if (tTd(40, 1))
5185037Seric 		printf("dowork: %s pri %ld\n", w->w_name, w->w_pri);
5194632Seric # endif DEBUG
5204632Seric 
5214632Seric 	/*
52224941Seric 	**  Ignore jobs that are too expensive for the moment.
5234632Seric 	*/
5244632Seric 
52524941Seric 	if (shouldqueue(w->w_pri))
5264632Seric 	{
52724941Seric 		if (Verbose)
52824967Seric 			printf("\nSkipping %s\n", w->w_name + 2);
5294632Seric 		return;
5304632Seric 	}
5314632Seric 
53224941Seric 	/*
53324941Seric 	**  Fork for work.
53424941Seric 	*/
53524941Seric 
53624941Seric 	if (ForkQueueRuns)
53724941Seric 	{
53824941Seric 		i = fork();
53924941Seric 		if (i < 0)
54024941Seric 		{
54124941Seric 			syserr("dowork: cannot fork");
54224941Seric 			return;
54324941Seric 		}
54424941Seric 	}
54524941Seric 	else
54624941Seric 	{
54724941Seric 		i = 0;
54824941Seric 	}
54924941Seric 
5504632Seric 	if (i == 0)
5514632Seric 	{
5524632Seric 		/*
5534632Seric 		**  CHILD
5548148Seric 		**	Lock the control file to avoid duplicate deliveries.
5558148Seric 		**		Then run the file as though we had just read it.
5567350Seric 		**	We save an idea of the temporary name so we
5577350Seric 		**		can recover on interrupt.
5584632Seric 		*/
5594632Seric 
5607763Seric 		/* set basic modes, etc. */
5617356Seric 		(void) alarm(0);
56224961Seric 		clearenvelope(CurEnv);
5634632Seric 		QueueRun = TRUE;
5649377Seric 		ErrorMode = EM_MAIL;
5658148Seric 		CurEnv->e_id = &w->w_name[2];
5667876Seric # ifdef LOG
5677876Seric 		if (LogLevel > 11)
5687881Seric 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", CurEnv->e_id,
5697881Seric 			       getpid());
5707876Seric # endif LOG
5717763Seric 
5727763Seric 		/* don't use the headers from sendmail.cf... */
5737763Seric 		CurEnv->e_header = NULL;
5747763Seric 
57517468Seric 		/* lock the control file during processing */
5767812Seric 		if (link(w->w_name, queuename(CurEnv, 'l')) < 0)
5776980Seric 		{
5787812Seric 			/* being processed by another queuer */
5797881Seric # ifdef LOG
5807881Seric 			if (LogLevel > 4)
5817881Seric 				syslog(LOG_DEBUG, "%s: locked", CurEnv->e_id);
5827881Seric # endif LOG
58324941Seric 			if (ForkQueueRuns)
58424941Seric 				exit(EX_OK);
58524941Seric 			else
58624941Seric 				return;
5876980Seric 		}
5886980Seric 
5896980Seric 		/* do basic system initialization */
5904632Seric 		initsys();
5916980Seric 
5926980Seric 		/* read the queue control file */
59317477Seric 		readqf(CurEnv, TRUE);
5949338Seric 		CurEnv->e_flags |= EF_INQUEUE;
5959377Seric 		eatheader(CurEnv);
5966980Seric 
5976980Seric 		/* do the delivery */
5989338Seric 		if (!bitset(EF_FATALERRS, CurEnv->e_flags))
5999282Seric 			sendall(CurEnv, SM_DELIVER);
6006980Seric 
6016980Seric 		/* finish up and exit */
60224941Seric 		if (ForkQueueRuns)
60324941Seric 			finis();
60424941Seric 		else
60524941Seric 			dropenvelope(CurEnv);
6064632Seric 	}
60724941Seric 	else
60824941Seric 	{
60924941Seric 		/*
61024941Seric 		**  Parent -- pick up results.
61124941Seric 		*/
6124632Seric 
61324941Seric 		errno = 0;
61424941Seric 		(void) waitfor(i);
61524941Seric 	}
6164632Seric }
6174632Seric /*
6184632Seric **  READQF -- read queue file and set up environment.
6194632Seric **
6204632Seric **	Parameters:
6219377Seric **		e -- the envelope of the job to run.
6229630Seric **		full -- if set, read in all information.  Otherwise just
6239630Seric **			read in info needed for a queue print.
6244632Seric **
6254632Seric **	Returns:
6264632Seric **		none.
6274632Seric **
6284632Seric **	Side Effects:
6294632Seric **		cf is read and created as the current job, as though
6304632Seric **		we had been invoked by argument.
6314632Seric */
6324632Seric 
63317477Seric readqf(e, full)
6349377Seric 	register ENVELOPE *e;
6359630Seric 	bool full;
6364632Seric {
63717477Seric 	char *qf;
63817477Seric 	register FILE *qfp;
6397785Seric 	char buf[MAXFIELD];
6409348Seric 	extern char *fgetfolded();
64124954Seric 	extern long atol();
6424632Seric 
6434632Seric 	/*
64417468Seric 	**  Read and process the file.
6454632Seric 	*/
6464632Seric 
64717477Seric 	qf = queuename(e, 'q');
64817477Seric 	qfp = fopen(qf, "r");
64917477Seric 	if (qfp == NULL)
65017477Seric 	{
65117477Seric 		syserr("readqf: no control file %s", qf);
65217477Seric 		return;
65317477Seric 	}
65417477Seric 	FileName = qf;
6559377Seric 	LineNumber = 0;
6569630Seric 	if (Verbose && full)
6579377Seric 		printf("\nRunning %s\n", e->e_id);
65817468Seric 	while (fgetfolded(buf, sizeof buf, qfp) != NULL)
6594632Seric 	{
6604632Seric 		switch (buf[0])
6614632Seric 		{
6624632Seric 		  case 'R':		/* specify recipient */
6639618Seric 			sendtolist(&buf[1], (ADDRESS *) NULL, &e->e_sendqueue);
6644632Seric 			break;
6654632Seric 
6664632Seric 		  case 'H':		/* header */
6679630Seric 			if (full)
6689630Seric 				(void) chompheader(&buf[1], FALSE);
6694632Seric 			break;
6704632Seric 
67110108Seric 		  case 'M':		/* message */
67210108Seric 			e->e_message = newstr(&buf[1]);
67310108Seric 			break;
67410108Seric 
6754632Seric 		  case 'S':		/* sender */
6764634Seric 			setsender(newstr(&buf[1]));
6774632Seric 			break;
6784632Seric 
6794632Seric 		  case 'D':		/* data file name */
6809630Seric 			if (!full)
6819630Seric 				break;
6829377Seric 			e->e_df = newstr(&buf[1]);
6839544Seric 			e->e_dfp = fopen(e->e_df, "r");
6849544Seric 			if (e->e_dfp == NULL)
6859377Seric 				syserr("readqf: cannot open %s", e->e_df);
6864632Seric 			break;
6874632Seric 
6887860Seric 		  case 'T':		/* init time */
68924941Seric 			e->e_ctime = atol(&buf[1]);
6904632Seric 			break;
6914632Seric 
6924634Seric 		  case 'P':		/* message priority */
693*24981Seric 			e->e_msgpriority = atol(&buf[1]) - WkTimeFact;
6944634Seric 			break;
6954634Seric 
69624941Seric 		  case '\0':		/* blank line; ignore */
69724941Seric 			break;
69824941Seric 
6994632Seric 		  default:
70024941Seric 			syserr("readqf(%s:%d): bad line \"%s\"", e->e_id,
70124941Seric 				LineNumber, buf);
7024632Seric 			break;
7034632Seric 		}
7044632Seric 	}
7059377Seric 
70624954Seric 	(void) fclose(qfp);
7079377Seric 	FileName = NULL;
70824941Seric 
70924941Seric 	/*
71024941Seric 	**  If we haven't read any lines, this queue file is empty.
71124941Seric 	**  Arrange to remove it without referencing any null pointers.
71224941Seric 	*/
71324941Seric 
71424941Seric 	if (LineNumber == 0)
71524941Seric 	{
71624941Seric 		errno = 0;
71724941Seric 		e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE;
71824941Seric 	}
7194632Seric }
7204632Seric /*
7219630Seric **  PRINTQUEUE -- print out a representation of the mail queue
7229630Seric **
7239630Seric **	Parameters:
7249630Seric **		none.
7259630Seric **
7269630Seric **	Returns:
7279630Seric **		none.
7289630Seric **
7299630Seric **	Side Effects:
7309630Seric **		Prints a listing of the mail queue on the standard output.
7319630Seric */
7325182Seric 
7339630Seric printqueue()
7349630Seric {
7359630Seric 	register WORK *w;
7369630Seric 	FILE *f;
73710070Seric 	int nrequests;
7389630Seric 	char buf[MAXLINE];
7399630Seric 
7409630Seric 	/*
7419630Seric 	**  Read and order the queue.
7429630Seric 	*/
7439630Seric 
74424941Seric 	nrequests = orderq(TRUE);
7459630Seric 
7469630Seric 	/*
7479630Seric 	**  Print the work list that we have read.
7489630Seric 	*/
7499630Seric 
7509630Seric 	/* first see if there is anything */
75110070Seric 	if (nrequests <= 0)
7529630Seric 	{
75310070Seric 		printf("Mail queue is empty\n");
7549630Seric 		return;
7559630Seric 	}
7569630Seric 
75710096Seric 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
75810070Seric 	if (nrequests > WLSIZE)
75910070Seric 		printf(", only %d printed", WLSIZE);
76024979Seric 	if (Verbose)
76124979Seric 		printf(")\n--QID-- --Size-- -Priority- -----Q-Time----- --------Sender/Recipient--------\n");
76224979Seric 	else
76324979Seric 		printf(")\n--QID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
7649630Seric 	for (w = WorkQ; w != NULL; w = w->w_next)
7659630Seric 	{
7669630Seric 		struct stat st;
76710070Seric 		auto time_t submittime = 0;
76810070Seric 		long dfsize = -1;
76910108Seric 		char lf[20];
77010108Seric 		char message[MAXLINE];
77124941Seric 		extern bool shouldqueue();
7729630Seric 
77317468Seric 		f = fopen(w->w_name, "r");
77417468Seric 		if (f == NULL)
77517468Seric 		{
77617468Seric 			errno = 0;
77717468Seric 			continue;
77817468Seric 		}
7799630Seric 		printf("%7s", w->w_name + 2);
78023098Seric 		(void) strcpy(lf, w->w_name);
78110070Seric 		lf[0] = 'l';
78210070Seric 		if (stat(lf, &st) >= 0)
78310070Seric 			printf("*");
78424941Seric 		else if (shouldqueue(w->w_pri))
78524941Seric 			printf("X");
78610070Seric 		else
78710070Seric 			printf(" ");
78810070Seric 		errno = 0;
78917468Seric 
79010108Seric 		message[0] = '\0';
7919630Seric 		while (fgets(buf, sizeof buf, f) != NULL)
7929630Seric 		{
7939630Seric 			fixcrlf(buf, TRUE);
7949630Seric 			switch (buf[0])
7959630Seric 			{
79610108Seric 			  case 'M':	/* error message */
79723098Seric 				(void) strcpy(message, &buf[1]);
79810108Seric 				break;
79910108Seric 
8009630Seric 			  case 'S':	/* sender name */
80124979Seric 				if (Verbose)
80224979Seric 					printf("%8ld %10ld %.16s %.37s", dfsize,
80324979Seric 					    w->w_pri, ctime(&submittime),
80424979Seric 					    &buf[1]);
80524979Seric 				else
80624979Seric 					printf("%8ld %.16s %.45s", dfsize,
80724979Seric 					    ctime(&submittime), &buf[1]);
80810108Seric 				if (message[0] != '\0')
80924941Seric 					printf("\n\t\t (%.62s)", message);
8109630Seric 				break;
8119630Seric 
8129630Seric 			  case 'R':	/* recipient name */
81324979Seric 				if (Verbose)
81424979Seric 					printf("\n\t\t\t\t\t     %.37s", &buf[1]);
81524979Seric 				else
81624979Seric 					printf("\n\t\t\t\t  %.45s", &buf[1]);
8179630Seric 				break;
8189630Seric 
8199630Seric 			  case 'T':	/* creation time */
82024941Seric 				submittime = atol(&buf[1]);
8219630Seric 				break;
82210070Seric 
82310070Seric 			  case 'D':	/* data file name */
82410070Seric 				if (stat(&buf[1], &st) >= 0)
82510070Seric 					dfsize = st.st_size;
82610070Seric 				break;
8279630Seric 			}
8289630Seric 		}
82910070Seric 		if (submittime == (time_t) 0)
83010070Seric 			printf(" (no control file)");
8319630Seric 		printf("\n");
83223098Seric 		(void) fclose(f);
8339630Seric 	}
8349630Seric }
8359630Seric 
8365182Seric # endif QUEUE
83717468Seric /*
83817468Seric **  QUEUENAME -- build a file name in the queue directory for this envelope.
83917468Seric **
84017468Seric **	Assigns an id code if one does not already exist.
84117468Seric **	This code is very careful to avoid trashing existing files
84217468Seric **	under any circumstances.
84317468Seric **		We first create an nf file that is only used when
84417468Seric **		assigning an id.  This file is always empty, so that
84517468Seric **		we can never accidently truncate an lf file.
84617468Seric **
84717468Seric **	Parameters:
84817468Seric **		e -- envelope to build it in/from.
84917468Seric **		type -- the file type, used as the first character
85017468Seric **			of the file name.
85117468Seric **
85217468Seric **	Returns:
85317468Seric **		a pointer to the new file name (in a static buffer).
85417468Seric **
85517468Seric **	Side Effects:
85617468Seric **		Will create the lf and qf files if no id code is
85717468Seric **		already assigned.  This will cause the envelope
85817468Seric **		to be modified.
85917468Seric */
86017468Seric 
86117468Seric char *
86217468Seric queuename(e, type)
86317468Seric 	register ENVELOPE *e;
86417468Seric 	char type;
86517468Seric {
86617468Seric 	static char buf[MAXNAME];
86717468Seric 	static int pid = -1;
86817468Seric 	char c1 = 'A';
86917468Seric 	char c2 = 'A';
87017468Seric 
87117468Seric 	if (e->e_id == NULL)
87217468Seric 	{
87317468Seric 		char qf[20];
87417468Seric 		char nf[20];
87517468Seric 		char lf[20];
87617468Seric 
87717468Seric 		/* find a unique id */
87817468Seric 		if (pid != getpid())
87917468Seric 		{
88017468Seric 			/* new process -- start back at "AA" */
88117468Seric 			pid = getpid();
88217468Seric 			c1 = 'A';
88317468Seric 			c2 = 'A' - 1;
88417468Seric 		}
88517468Seric 		(void) sprintf(qf, "qfAA%05d", pid);
88623098Seric 		(void) strcpy(lf, qf);
88717468Seric 		lf[0] = 'l';
88823098Seric 		(void) strcpy(nf, qf);
88917468Seric 		nf[0] = 'n';
89017468Seric 
89117468Seric 		while (c1 < '~' || c2 < 'Z')
89217468Seric 		{
89317468Seric 			int i;
89417468Seric 
89517468Seric 			if (c2 >= 'Z')
89617468Seric 			{
89717468Seric 				c1++;
89817468Seric 				c2 = 'A' - 1;
89917468Seric 			}
90017477Seric 			lf[2] = nf[2] = qf[2] = c1;
90117477Seric 			lf[3] = nf[3] = qf[3] = ++c2;
90217468Seric # ifdef DEBUG
90317468Seric 			if (tTd(7, 20))
90417468Seric 				printf("queuename: trying \"%s\"\n", nf);
90517468Seric # endif DEBUG
90617468Seric 
90717468Seric # ifdef QUEUE
90817468Seric 			if (access(lf, 0) >= 0 || access(qf, 0) >= 0)
90917468Seric 				continue;
91017468Seric 			errno = 0;
91117468Seric 			i = creat(nf, FileMode);
91217468Seric 			if (i < 0)
91317468Seric 			{
91417468Seric 				(void) unlink(nf);	/* kernel bug */
91517468Seric 				continue;
91617468Seric 			}
91717468Seric 			(void) close(i);
91817468Seric 			i = link(nf, lf);
91917468Seric 			(void) unlink(nf);
92017468Seric 			if (i < 0)
92117468Seric 				continue;
92217468Seric 			if (link(lf, qf) >= 0)
92317468Seric 				break;
92417468Seric 			(void) unlink(lf);
92517468Seric # else QUEUE
92617982Seric 			if (close(creat(qf, FileMode)) >= 0)
92717982Seric 				break;
92817468Seric # endif QUEUE
92917468Seric 		}
93017468Seric 		if (c1 >= '~' && c2 >= 'Z')
93117468Seric 		{
93217468Seric 			syserr("queuename: Cannot create \"%s\" in \"%s\"",
93317468Seric 				qf, QueueDir);
93417468Seric 			exit(EX_OSERR);
93517468Seric 		}
93617468Seric 		e->e_id = newstr(&qf[2]);
93717468Seric 		define('i', e->e_id, e);
93817468Seric # ifdef DEBUG
93917468Seric 		if (tTd(7, 1))
94017468Seric 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
94117468Seric # ifdef LOG
94217468Seric 		if (LogLevel > 16)
94317468Seric 			syslog(LOG_DEBUG, "%s: assigned id", e->e_id);
94417468Seric # endif LOG
94517468Seric # endif DEBUG
94617468Seric 	}
94717468Seric 
94817468Seric 	if (type == '\0')
94917468Seric 		return (NULL);
95017468Seric 	(void) sprintf(buf, "%cf%s", type, e->e_id);
95117468Seric # ifdef DEBUG
95217468Seric 	if (tTd(7, 2))
95317468Seric 		printf("queuename: %s\n", buf);
95417468Seric # endif DEBUG
95517468Seric 	return (buf);
95617468Seric }
95717468Seric /*
95817468Seric **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
95917468Seric **
96017468Seric **	Parameters:
96117468Seric **		e -- the envelope to unlock.
96217468Seric **
96317468Seric **	Returns:
96417468Seric **		none
96517468Seric **
96617468Seric **	Side Effects:
96717468Seric **		unlocks the queue for `e'.
96817468Seric */
96917468Seric 
97017468Seric unlockqueue(e)
97117468Seric 	ENVELOPE *e;
97217468Seric {
97317468Seric 	/* remove the transcript */
97417468Seric #ifdef DEBUG
97517468Seric # ifdef LOG
97617468Seric 	if (LogLevel > 19)
97717468Seric 		syslog(LOG_DEBUG, "%s: unlock", e->e_id);
97817468Seric # endif LOG
97917468Seric 	if (!tTd(51, 4))
98017468Seric #endif DEBUG
98117468Seric 		xunlink(queuename(e, 'x'));
98217468Seric 
98317468Seric # ifdef QUEUE
98417468Seric 	/* last but not least, remove the lock */
98517468Seric 	xunlink(queuename(e, 'l'));
98617468Seric # endif QUEUE
98717468Seric }
988