xref: /csrg-svn/usr.sbin/sendmail/src/queue.c (revision 24941)
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*24941Seric static char	SccsId[] = "@(#)queue.c	5.5 (Berkeley) 09/19/85	(no queueing)";
2123116Seric # endif not lint
225182Seric # else QUEUE
234632Seric 
2423116Seric # ifndef lint
25*24941Seric static char	SccsId[] = "@(#)queue.c	5.5 (Berkeley) 09/19/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*24941Seric 	long		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 		holdsigs();
22517468Seric 		(void) unlink(qf);
22617468Seric 		if (link(tf, qf) < 0)
22717468Seric 			syserr("cannot link(%s, %s), df=%s", tf, qf, e->e_df);
22817468Seric 		else
22917468Seric 			(void) unlink(tf);
23017468Seric 		rlsesigs();
23117468Seric 	}
2327391Seric 
2337677Seric # ifdef LOG
2347677Seric 	/* save log info */
2357878Seric 	if (LogLevel > 15)
2367878Seric 		syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df);
2377677Seric # endif LOG
2384632Seric }
2394632Seric /*
2404632Seric **  RUNQUEUE -- run the jobs in the queue.
2414632Seric **
2424632Seric **	Gets the stuff out of the queue in some presumably logical
2434632Seric **	order and processes them.
2444632Seric **
2454632Seric **	Parameters:
246*24941Seric **		forkflag -- TRUE if the queue scanning should be done in
247*24941Seric **			a child process.  We double-fork so it is not our
248*24941Seric **			child and we don't have to clean up after it.
2494632Seric **
2504632Seric **	Returns:
2514632Seric **		none.
2524632Seric **
2534632Seric **	Side Effects:
2544632Seric **		runs things in the mail queue.
2554632Seric */
2564632Seric 
2574639Seric runqueue(forkflag)
2584639Seric 	bool forkflag;
2594632Seric {
2607466Seric 	/*
2617466Seric 	**  See if we want to go off and do other useful work.
2627466Seric 	*/
2634639Seric 
2644639Seric 	if (forkflag)
2654639Seric 	{
2667943Seric 		int pid;
2677943Seric 
2687943Seric 		pid = dofork();
2697943Seric 		if (pid != 0)
2704639Seric 		{
2717943Seric 			/* parent -- pick up intermediate zombie */
2729377Seric 			(void) waitfor(pid);
2737690Seric 			if (QueueIntvl != 0)
2749348Seric 				(void) setevent(QueueIntvl, runqueue, TRUE);
2754639Seric 			return;
2764639Seric 		}
2777943Seric 		/* child -- double fork */
2787943Seric 		if (fork() != 0)
2797943Seric 			exit(EX_OK);
2804639Seric 	}
281*24941Seric 
282*24941Seric 	setproctitle("running queue");
283*24941Seric 
2847876Seric # ifdef LOG
2857876Seric 	if (LogLevel > 11)
2867943Seric 		syslog(LOG_DEBUG, "runqueue %s, pid=%d", QueueDir, getpid());
2877876Seric # endif LOG
2884639Seric 
2897466Seric 	/*
29010205Seric 	**  Release any resources used by the daemon code.
29110205Seric 	*/
29210205Seric 
29310205Seric # ifdef DAEMON
29410205Seric 	clrdaemon();
29510205Seric # endif DAEMON
29610205Seric 
29710205Seric 	/*
2987466Seric 	**  Start making passes through the queue.
2997466Seric 	**	First, read and sort the entire queue.
3007466Seric 	**	Then, process the work in that order.
3017466Seric 	**		But if you take too long, start over.
3027466Seric 	*/
3037466Seric 
3047943Seric 	/* order the existing work requests */
30510070Seric 	(void) orderq();
3067690Seric 
3077943Seric 	/* process them once at a time */
3087943Seric 	while (WorkQ != NULL)
3094639Seric 	{
3107943Seric 		WORK *w = WorkQ;
3117881Seric 
3127943Seric 		WorkQ = WorkQ->w_next;
3137943Seric 		dowork(w);
3147943Seric 		free(w->w_name);
3157943Seric 		free((char *) w);
3164639Seric 	}
3177943Seric 	finis();
3184634Seric }
3194634Seric /*
3204632Seric **  ORDERQ -- order the work queue.
3214632Seric **
3224632Seric **	Parameters:
323*24941Seric **		doall -- if set, include everything in the queue (even
324*24941Seric **			the jobs that cannot be run because the load
325*24941Seric **			average is too high).  Otherwise, exclude those
326*24941Seric **			jobs.
3274632Seric **
3284632Seric **	Returns:
32910121Seric **		The number of request in the queue (not necessarily
33010121Seric **		the number of requests in WorkQ however).
3314632Seric **
3324632Seric **	Side Effects:
3334632Seric **		Sets WorkQ to the queue of available work, in order.
3344632Seric */
3354632Seric 
3364632Seric # define WLSIZE		120	/* max size of worklist per sort */
3374632Seric 
338*24941Seric orderq(doall)
339*24941Seric 	bool doall;
3404632Seric {
3416625Sglickman 	register struct direct *d;
3424632Seric 	register WORK *w;
3434632Seric 	register WORK **wp;		/* parent of w */
3446625Sglickman 	DIR *f;
3454632Seric 	register int i;
34610121Seric 	WORK wlist[WLSIZE+1];
34710070Seric 	int wn = -1;
3484632Seric 	extern workcmpf();
3494632Seric 
3504632Seric 	/* clear out old WorkQ */
3514632Seric 	for (w = WorkQ; w != NULL; )
3524632Seric 	{
3534632Seric 		register WORK *nw = w->w_next;
3544632Seric 
3554632Seric 		WorkQ = nw;
3564632Seric 		free(w->w_name);
3574632Seric 		free((char *) w);
3584632Seric 		w = nw;
3594632Seric 	}
3604632Seric 
3614632Seric 	/* open the queue directory */
3628148Seric 	f = opendir(".");
3634632Seric 	if (f == NULL)
3644632Seric 	{
3658148Seric 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
36610070Seric 		return (0);
3674632Seric 	}
3684632Seric 
3694632Seric 	/*
3704632Seric 	**  Read the work directory.
3714632Seric 	*/
3724632Seric 
37310070Seric 	while ((d = readdir(f)) != NULL)
3744632Seric 	{
3759377Seric 		FILE *cf;
3764632Seric 		char lbuf[MAXNAME];
3774632Seric 
3784632Seric 		/* is this an interesting entry? */
3797812Seric 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
3804632Seric 			continue;
3814632Seric 
38210070Seric 		/* yes -- open control file (if not too many files) */
38310070Seric 		if (++wn >= WLSIZE)
38410070Seric 			continue;
3858148Seric 		cf = fopen(d->d_name, "r");
3864632Seric 		if (cf == NULL)
3874632Seric 		{
3887055Seric 			/* this may be some random person sending hir msgs */
3897055Seric 			/* syserr("orderq: cannot open %s", cbuf); */
39010090Seric #ifdef DEBUG
39110090Seric 			if (tTd(41, 2))
39210090Seric 				printf("orderq: cannot open %s (%d)\n",
39310090Seric 					d->d_name, errno);
39410090Seric #endif DEBUG
3957055Seric 			errno = 0;
39610090Seric 			wn--;
3974632Seric 			continue;
3984632Seric 		}
3998148Seric 		wlist[wn].w_name = newstr(d->d_name);
4004632Seric 
4014632Seric 		/* extract useful information */
4024632Seric 		while (fgets(lbuf, sizeof lbuf, cf) != NULL)
4034632Seric 		{
404*24941Seric 			switch (lbuf[0])
4054632Seric 			{
406*24941Seric 			  case 'P':
407*24941Seric 				wlist[wn].w_pri = atol(&lbuf[1]);
4084632Seric 				break;
409*24941Seric 
410*24941Seric 			  case 'T':
411*24941Seric 				wlist[wn].w_ctime = atol(&lbuf[1]);
412*24941Seric 				break;
4134632Seric 			}
4144632Seric 		}
4154632Seric 		(void) fclose(cf);
4164632Seric 	}
4176625Sglickman 	(void) closedir(f);
41810090Seric 	wn++;
4194632Seric 
4204632Seric 	/*
4214632Seric 	**  Sort the work directory.
4224632Seric 	*/
4234632Seric 
42423116Seric 	qsort((char *) wlist, min(wn, WLSIZE), sizeof *wlist, workcmpf);
4254632Seric 
4264632Seric 	/*
4274632Seric 	**  Convert the work list into canonical form.
4289377Seric 	**	Should be turning it into a list of envelopes here perhaps.
4294632Seric 	*/
4304632Seric 
4314632Seric 	wp = &WorkQ;
43210121Seric 	for (i = min(wn, WLSIZE); --i >= 0; )
4334632Seric 	{
4344632Seric 		w = (WORK *) xalloc(sizeof *w);
4354632Seric 		w->w_name = wlist[i].w_name;
4364632Seric 		w->w_pri = wlist[i].w_pri;
437*24941Seric 		w->w_ctime = wlist[i].w_ctime;
4384632Seric 		w->w_next = NULL;
4394632Seric 		*wp = w;
4404632Seric 		wp = &w->w_next;
4414632Seric 	}
4424632Seric 
4434632Seric # ifdef DEBUG
4447677Seric 	if (tTd(40, 1))
4454632Seric 	{
4464632Seric 		for (w = WorkQ; w != NULL; w = w->w_next)
4475037Seric 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
4484632Seric 	}
4494632Seric # endif DEBUG
45010070Seric 
45110090Seric 	return (wn);
4524632Seric }
4534632Seric /*
4547677Seric **  WORKCMPF -- compare function for ordering work.
4554632Seric **
4564632Seric **	Parameters:
4574632Seric **		a -- the first argument.
4584632Seric **		b -- the second argument.
4594632Seric **
4604632Seric **	Returns:
46110121Seric **		1 if a < b
4624632Seric **		0 if a == b
46310121Seric **		-1 if a > b
4644632Seric **
4654632Seric **	Side Effects:
4664632Seric **		none.
4674632Seric */
4684632Seric 
4694632Seric workcmpf(a, b)
4705037Seric 	register WORK *a;
4715037Seric 	register WORK *b;
4724632Seric {
473*24941Seric 	long pa = a->w_pri + a->w_ctime;
474*24941Seric 	long pb = b->w_pri + b->w_ctime;
475*24941Seric 
476*24941Seric 	if (pa == pb)
4774632Seric 		return (0);
478*24941Seric 	else if (pa > pb)
47910121Seric 		return (-1);
48010121Seric 	else
4814632Seric 		return (1);
4824632Seric }
4834632Seric /*
4844632Seric **  DOWORK -- do a work request.
4854632Seric **
4864632Seric **	Parameters:
4874632Seric **		w -- the work request to be satisfied.
4884632Seric **
4894632Seric **	Returns:
4904632Seric **		none.
4914632Seric **
4924632Seric **	Side Effects:
4934632Seric **		The work request is satisfied if possible.
4944632Seric */
4954632Seric 
4964632Seric dowork(w)
4974632Seric 	register WORK *w;
4984632Seric {
4994632Seric 	register int i;
500*24941Seric 	extern bool shouldqueue();
5014632Seric 
5024632Seric # ifdef DEBUG
5037677Seric 	if (tTd(40, 1))
5045037Seric 		printf("dowork: %s pri %ld\n", w->w_name, w->w_pri);
5054632Seric # endif DEBUG
5064632Seric 
5074632Seric 	/*
508*24941Seric 	**  Ignore jobs that are too expensive for the moment.
5094632Seric 	*/
5104632Seric 
511*24941Seric 	if (shouldqueue(w->w_pri))
5124632Seric 	{
513*24941Seric 		if (Verbose)
514*24941Seric 			printf("\nSkipping %s\n", w->w_name);
5154632Seric 		return;
5164632Seric 	}
5174632Seric 
518*24941Seric 	/*
519*24941Seric 	**  Fork for work.
520*24941Seric 	*/
521*24941Seric 
522*24941Seric 	if (ForkQueueRuns)
523*24941Seric 	{
524*24941Seric 		i = fork();
525*24941Seric 		if (i < 0)
526*24941Seric 		{
527*24941Seric 			syserr("dowork: cannot fork");
528*24941Seric 			return;
529*24941Seric 		}
530*24941Seric 	}
531*24941Seric 	else
532*24941Seric 	{
533*24941Seric 		i = 0;
534*24941Seric 	}
535*24941Seric 
5364632Seric 	if (i == 0)
5374632Seric 	{
5384632Seric 		/*
5394632Seric 		**  CHILD
5408148Seric 		**	Lock the control file to avoid duplicate deliveries.
5418148Seric 		**		Then run the file as though we had just read it.
5427350Seric 		**	We save an idea of the temporary name so we
5437350Seric 		**		can recover on interrupt.
5444632Seric 		*/
5454632Seric 
5467763Seric 		/* set basic modes, etc. */
5477356Seric 		(void) alarm(0);
54810195Seric 		closexscript(CurEnv);
5499338Seric 		CurEnv->e_flags &= ~EF_FATALERRS;
5504632Seric 		QueueRun = TRUE;
5519377Seric 		ErrorMode = EM_MAIL;
5528148Seric 		CurEnv->e_id = &w->w_name[2];
5537876Seric # ifdef LOG
5547876Seric 		if (LogLevel > 11)
5557881Seric 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", CurEnv->e_id,
5567881Seric 			       getpid());
5577876Seric # endif LOG
5587763Seric 
5597763Seric 		/* don't use the headers from sendmail.cf... */
5607763Seric 		CurEnv->e_header = NULL;
5617763Seric 
56217468Seric 		/* lock the control file during processing */
5637812Seric 		if (link(w->w_name, queuename(CurEnv, 'l')) < 0)
5646980Seric 		{
5657812Seric 			/* being processed by another queuer */
5667881Seric # ifdef LOG
5677881Seric 			if (LogLevel > 4)
5687881Seric 				syslog(LOG_DEBUG, "%s: locked", CurEnv->e_id);
5697881Seric # endif LOG
570*24941Seric 			if (ForkQueueRuns)
571*24941Seric 				exit(EX_OK);
572*24941Seric 			else
573*24941Seric 				return;
5746980Seric 		}
5756980Seric 
5766980Seric 		/* do basic system initialization */
5774632Seric 		initsys();
5786980Seric 
5796980Seric 		/* read the queue control file */
58017477Seric 		readqf(CurEnv, TRUE);
5819338Seric 		CurEnv->e_flags |= EF_INQUEUE;
5829377Seric 		eatheader(CurEnv);
5836980Seric 
5846980Seric 		/* do the delivery */
5859338Seric 		if (!bitset(EF_FATALERRS, CurEnv->e_flags))
5869282Seric 			sendall(CurEnv, SM_DELIVER);
5876980Seric 
5886980Seric 		/* finish up and exit */
589*24941Seric 		if (ForkQueueRuns)
590*24941Seric 			finis();
591*24941Seric 		else
592*24941Seric 			dropenvelope(CurEnv);
5934632Seric 	}
594*24941Seric 	else
595*24941Seric 	{
596*24941Seric 		/*
597*24941Seric 		**  Parent -- pick up results.
598*24941Seric 		*/
5994632Seric 
600*24941Seric 		errno = 0;
601*24941Seric 		(void) waitfor(i);
602*24941Seric 	}
6034632Seric }
6044632Seric /*
6054632Seric **  READQF -- read queue file and set up environment.
6064632Seric **
6074632Seric **	Parameters:
6089377Seric **		e -- the envelope of the job to run.
6099630Seric **		full -- if set, read in all information.  Otherwise just
6109630Seric **			read in info needed for a queue print.
6114632Seric **
6124632Seric **	Returns:
6134632Seric **		none.
6144632Seric **
6154632Seric **	Side Effects:
6164632Seric **		cf is read and created as the current job, as though
6174632Seric **		we had been invoked by argument.
6184632Seric */
6194632Seric 
62017477Seric readqf(e, full)
6219377Seric 	register ENVELOPE *e;
6229630Seric 	bool full;
6234632Seric {
62417477Seric 	char *qf;
62517477Seric 	register FILE *qfp;
6267785Seric 	char buf[MAXFIELD];
6279348Seric 	extern char *fgetfolded();
6284632Seric 
6294632Seric 	/*
63017468Seric 	**  Read and process the file.
6314632Seric 	*/
6324632Seric 
63317477Seric 	qf = queuename(e, 'q');
63417477Seric 	qfp = fopen(qf, "r");
63517477Seric 	if (qfp == NULL)
63617477Seric 	{
63717477Seric 		syserr("readqf: no control file %s", qf);
63817477Seric 		return;
63917477Seric 	}
64017477Seric 	FileName = qf;
6419377Seric 	LineNumber = 0;
6429630Seric 	if (Verbose && full)
6439377Seric 		printf("\nRunning %s\n", e->e_id);
64417468Seric 	while (fgetfolded(buf, sizeof buf, qfp) != NULL)
6454632Seric 	{
6464632Seric 		switch (buf[0])
6474632Seric 		{
6484632Seric 		  case 'R':		/* specify recipient */
6499618Seric 			sendtolist(&buf[1], (ADDRESS *) NULL, &e->e_sendqueue);
6504632Seric 			break;
6514632Seric 
6524632Seric 		  case 'H':		/* header */
6539630Seric 			if (full)
6549630Seric 				(void) chompheader(&buf[1], FALSE);
6554632Seric 			break;
6564632Seric 
65710108Seric 		  case 'M':		/* message */
65810108Seric 			e->e_message = newstr(&buf[1]);
65910108Seric 			break;
66010108Seric 
6614632Seric 		  case 'S':		/* sender */
6624634Seric 			setsender(newstr(&buf[1]));
6634632Seric 			break;
6644632Seric 
6654632Seric 		  case 'D':		/* data file name */
6669630Seric 			if (!full)
6679630Seric 				break;
6689377Seric 			e->e_df = newstr(&buf[1]);
6699544Seric 			e->e_dfp = fopen(e->e_df, "r");
6709544Seric 			if (e->e_dfp == NULL)
6719377Seric 				syserr("readqf: cannot open %s", e->e_df);
6724632Seric 			break;
6734632Seric 
6747860Seric 		  case 'T':		/* init time */
675*24941Seric 			e->e_ctime = atol(&buf[1]);
6764632Seric 			break;
6774632Seric 
6784634Seric 		  case 'P':		/* message priority */
679*24941Seric 			e->e_msgpriority = atol(&buf[1]);
6805037Seric 
6815037Seric 			/* make sure that big things get sent eventually */
6829377Seric 			e->e_msgpriority -= WKTIMEFACT;
6834634Seric 			break;
6844634Seric 
685*24941Seric 		  case '\0':		/* blank line; ignore */
686*24941Seric 			break;
687*24941Seric 
6884632Seric 		  default:
689*24941Seric 			syserr("readqf(%s:%d): bad line \"%s\"", e->e_id,
690*24941Seric 				LineNumber, buf);
6914632Seric 			break;
6924632Seric 		}
6934632Seric 	}
6949377Seric 
695*24941Seric 	fclose(qfp);
6969377Seric 	FileName = NULL;
697*24941Seric 
698*24941Seric 	/*
699*24941Seric 	**  If we haven't read any lines, this queue file is empty.
700*24941Seric 	**  Arrange to remove it without referencing any null pointers.
701*24941Seric 	*/
702*24941Seric 
703*24941Seric 	if (LineNumber == 0)
704*24941Seric 	{
705*24941Seric 		errno = 0;
706*24941Seric 		e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE;
707*24941Seric 	}
7084632Seric }
7094632Seric /*
7109630Seric **  PRINTQUEUE -- print out a representation of the mail queue
7119630Seric **
7129630Seric **	Parameters:
7139630Seric **		none.
7149630Seric **
7159630Seric **	Returns:
7169630Seric **		none.
7179630Seric **
7189630Seric **	Side Effects:
7199630Seric **		Prints a listing of the mail queue on the standard output.
7209630Seric */
7215182Seric 
7229630Seric printqueue()
7239630Seric {
7249630Seric 	register WORK *w;
7259630Seric 	FILE *f;
72610070Seric 	int nrequests;
7279630Seric 	char buf[MAXLINE];
7289630Seric 
7299630Seric 	/*
7309630Seric 	**  Read and order the queue.
7319630Seric 	*/
7329630Seric 
733*24941Seric 	nrequests = orderq(TRUE);
7349630Seric 
7359630Seric 	/*
7369630Seric 	**  Print the work list that we have read.
7379630Seric 	*/
7389630Seric 
7399630Seric 	/* first see if there is anything */
74010070Seric 	if (nrequests <= 0)
7419630Seric 	{
74210070Seric 		printf("Mail queue is empty\n");
7439630Seric 		return;
7449630Seric 	}
7459630Seric 
74610096Seric 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
74710070Seric 	if (nrequests > WLSIZE)
74810070Seric 		printf(", only %d printed", WLSIZE);
74910070Seric 	printf(")\n--QID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
7509630Seric 	for (w = WorkQ; w != NULL; w = w->w_next)
7519630Seric 	{
7529630Seric 		struct stat st;
75310070Seric 		auto time_t submittime = 0;
75410070Seric 		long dfsize = -1;
75510108Seric 		char lf[20];
75610108Seric 		char message[MAXLINE];
757*24941Seric 		extern bool shouldqueue();
7589630Seric 
75917468Seric 		f = fopen(w->w_name, "r");
76017468Seric 		if (f == NULL)
76117468Seric 		{
76217468Seric 			errno = 0;
76317468Seric 			continue;
76417468Seric 		}
7659630Seric 		printf("%7s", w->w_name + 2);
76623098Seric 		(void) strcpy(lf, w->w_name);
76710070Seric 		lf[0] = 'l';
76810070Seric 		if (stat(lf, &st) >= 0)
76910070Seric 			printf("*");
770*24941Seric 		else if (shouldqueue(w->w_pri))
771*24941Seric 			printf("X");
77210070Seric 		else
77310070Seric 			printf(" ");
77410070Seric 		errno = 0;
77517468Seric 
77610108Seric 		message[0] = '\0';
7779630Seric 		while (fgets(buf, sizeof buf, f) != NULL)
7789630Seric 		{
7799630Seric 			fixcrlf(buf, TRUE);
7809630Seric 			switch (buf[0])
7819630Seric 			{
78210108Seric 			  case 'M':	/* error message */
78323098Seric 				(void) strcpy(message, &buf[1]);
78410108Seric 				break;
78510108Seric 
7869630Seric 			  case 'S':	/* sender name */
78713015Seric 				printf("%8ld %.16s %.45s", dfsize,
78812517Seric 					ctime(&submittime), &buf[1]);
78910108Seric 				if (message[0] != '\0')
790*24941Seric 					printf("\n\t\t (%.62s)", message);
7919630Seric 				break;
7929630Seric 
7939630Seric 			  case 'R':	/* recipient name */
79412517Seric 				printf("\n\t\t\t\t  %.45s", &buf[1]);
7959630Seric 				break;
7969630Seric 
7979630Seric 			  case 'T':	/* creation time */
798*24941Seric 				submittime = atol(&buf[1]);
7999630Seric 				break;
80010070Seric 
80110070Seric 			  case 'D':	/* data file name */
80210070Seric 				if (stat(&buf[1], &st) >= 0)
80310070Seric 					dfsize = st.st_size;
80410070Seric 				break;
8059630Seric 			}
8069630Seric 		}
80710070Seric 		if (submittime == (time_t) 0)
80810070Seric 			printf(" (no control file)");
8099630Seric 		printf("\n");
81023098Seric 		(void) fclose(f);
8119630Seric 	}
8129630Seric }
8139630Seric 
8145182Seric # endif QUEUE
81517468Seric /*
81617468Seric **  QUEUENAME -- build a file name in the queue directory for this envelope.
81717468Seric **
81817468Seric **	Assigns an id code if one does not already exist.
81917468Seric **	This code is very careful to avoid trashing existing files
82017468Seric **	under any circumstances.
82117468Seric **		We first create an nf file that is only used when
82217468Seric **		assigning an id.  This file is always empty, so that
82317468Seric **		we can never accidently truncate an lf file.
82417468Seric **
82517468Seric **	Parameters:
82617468Seric **		e -- envelope to build it in/from.
82717468Seric **		type -- the file type, used as the first character
82817468Seric **			of the file name.
82917468Seric **
83017468Seric **	Returns:
83117468Seric **		a pointer to the new file name (in a static buffer).
83217468Seric **
83317468Seric **	Side Effects:
83417468Seric **		Will create the lf and qf files if no id code is
83517468Seric **		already assigned.  This will cause the envelope
83617468Seric **		to be modified.
83717468Seric */
83817468Seric 
83917468Seric char *
84017468Seric queuename(e, type)
84117468Seric 	register ENVELOPE *e;
84217468Seric 	char type;
84317468Seric {
84417468Seric 	static char buf[MAXNAME];
84517468Seric 	static int pid = -1;
84617468Seric 	char c1 = 'A';
84717468Seric 	char c2 = 'A';
84817468Seric 
84917468Seric 	if (e->e_id == NULL)
85017468Seric 	{
85117468Seric 		char qf[20];
85217468Seric 		char nf[20];
85317468Seric 		char lf[20];
85417468Seric 
85517468Seric 		/* find a unique id */
85617468Seric 		if (pid != getpid())
85717468Seric 		{
85817468Seric 			/* new process -- start back at "AA" */
85917468Seric 			pid = getpid();
86017468Seric 			c1 = 'A';
86117468Seric 			c2 = 'A' - 1;
86217468Seric 		}
86317468Seric 		(void) sprintf(qf, "qfAA%05d", pid);
86423098Seric 		(void) strcpy(lf, qf);
86517468Seric 		lf[0] = 'l';
86623098Seric 		(void) strcpy(nf, qf);
86717468Seric 		nf[0] = 'n';
86817468Seric 
86917468Seric 		while (c1 < '~' || c2 < 'Z')
87017468Seric 		{
87117468Seric 			int i;
87217468Seric 
87317468Seric 			if (c2 >= 'Z')
87417468Seric 			{
87517468Seric 				c1++;
87617468Seric 				c2 = 'A' - 1;
87717468Seric 			}
87817477Seric 			lf[2] = nf[2] = qf[2] = c1;
87917477Seric 			lf[3] = nf[3] = qf[3] = ++c2;
88017468Seric # ifdef DEBUG
88117468Seric 			if (tTd(7, 20))
88217468Seric 				printf("queuename: trying \"%s\"\n", nf);
88317468Seric # endif DEBUG
88417468Seric 
88517468Seric # ifdef QUEUE
88617468Seric 			if (access(lf, 0) >= 0 || access(qf, 0) >= 0)
88717468Seric 				continue;
88817468Seric 			errno = 0;
88917468Seric 			i = creat(nf, FileMode);
89017468Seric 			if (i < 0)
89117468Seric 			{
89217468Seric 				(void) unlink(nf);	/* kernel bug */
89317468Seric 				continue;
89417468Seric 			}
89517468Seric 			(void) close(i);
89617468Seric 			i = link(nf, lf);
89717468Seric 			(void) unlink(nf);
89817468Seric 			if (i < 0)
89917468Seric 				continue;
90017468Seric 			if (link(lf, qf) >= 0)
90117468Seric 				break;
90217468Seric 			(void) unlink(lf);
90317468Seric # else QUEUE
90417982Seric 			if (close(creat(qf, FileMode)) >= 0)
90517982Seric 				break;
90617468Seric # endif QUEUE
90717468Seric 		}
90817468Seric 		if (c1 >= '~' && c2 >= 'Z')
90917468Seric 		{
91017468Seric 			syserr("queuename: Cannot create \"%s\" in \"%s\"",
91117468Seric 				qf, QueueDir);
91217468Seric 			exit(EX_OSERR);
91317468Seric 		}
91417468Seric 		e->e_id = newstr(&qf[2]);
91517468Seric 		define('i', e->e_id, e);
91617468Seric # ifdef DEBUG
91717468Seric 		if (tTd(7, 1))
91817468Seric 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
91917468Seric # ifdef LOG
92017468Seric 		if (LogLevel > 16)
92117468Seric 			syslog(LOG_DEBUG, "%s: assigned id", e->e_id);
92217468Seric # endif LOG
92317468Seric # endif DEBUG
92417468Seric 	}
92517468Seric 
92617468Seric 	if (type == '\0')
92717468Seric 		return (NULL);
92817468Seric 	(void) sprintf(buf, "%cf%s", type, e->e_id);
92917468Seric # ifdef DEBUG
93017468Seric 	if (tTd(7, 2))
93117468Seric 		printf("queuename: %s\n", buf);
93217468Seric # endif DEBUG
93317468Seric 	return (buf);
93417468Seric }
93517468Seric /*
93617468Seric **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
93717468Seric **
93817468Seric **	Parameters:
93917468Seric **		e -- the envelope to unlock.
94017468Seric **
94117468Seric **	Returns:
94217468Seric **		none
94317468Seric **
94417468Seric **	Side Effects:
94517468Seric **		unlocks the queue for `e'.
94617468Seric */
94717468Seric 
94817468Seric unlockqueue(e)
94917468Seric 	ENVELOPE *e;
95017468Seric {
95117468Seric 	/* remove the transcript */
95217468Seric #ifdef DEBUG
95317468Seric # ifdef LOG
95417468Seric 	if (LogLevel > 19)
95517468Seric 		syslog(LOG_DEBUG, "%s: unlock", e->e_id);
95617468Seric # endif LOG
95717468Seric 	if (!tTd(51, 4))
95817468Seric #endif DEBUG
95917468Seric 		xunlink(queuename(e, 'x'));
96017468Seric 
96117468Seric # ifdef QUEUE
96217468Seric 	/* last but not least, remove the lock */
96317468Seric 	xunlink(queuename(e, 'l'));
96417468Seric # endif QUEUE
96517468Seric }
966