xref: /csrg-svn/usr.sbin/sendmail/src/queue.c (revision 17468)
14632Seric # include "sendmail.h"
24632Seric # include <sys/stat.h>
313707Ssam # include <sys/dir.h>
44634Seric # include <signal.h>
54632Seric # include <errno.h>
6*17468Seric # ifdef FLOCK
7*17468Seric # include <sys/file.h>
8*17468Seric # endif FLOCK
94632Seric 
105182Seric # ifndef QUEUE
11*17468Seric SCCSID(@(#)queue.c	4.3		12/05/84	(no queueing));
125182Seric # else QUEUE
134632Seric 
14*17468Seric SCCSID(@(#)queue.c	4.3		12/05/84);
155182Seric 
164632Seric /*
179377Seric **  Work queue.
189377Seric */
199377Seric 
209377Seric struct work
219377Seric {
229377Seric 	char		*w_name;	/* name of control file */
239377Seric 	long		w_pri;		/* priority of message, see below */
249377Seric 	struct work	*w_next;	/* next in queue */
259377Seric };
269377Seric 
279377Seric typedef struct work	WORK;
289377Seric 
299377Seric WORK	*WorkQ;			/* queue of things to be done */
309377Seric /*
314632Seric **  QUEUEUP -- queue a message up for future transmission.
324632Seric **
334632Seric **	Parameters:
346980Seric **		e -- the envelope to queue up.
356999Seric **		queueall -- if TRUE, queue all addresses, rather than
366999Seric **			just those with the QQUEUEUP flag set.
379377Seric **		announce -- if TRUE, tell when you are queueing up.
384632Seric **
394632Seric **	Returns:
404632Seric **		none.
414632Seric **
424632Seric **	Side Effects:
439377Seric **		The current request are saved in a control file.
444632Seric */
454632Seric 
469377Seric queueup(e, queueall, announce)
476980Seric 	register ENVELOPE *e;
486999Seric 	bool queueall;
499377Seric 	bool announce;
504632Seric {
517812Seric 	char *tf;
527812Seric 	char *qf;
537763Seric 	char buf[MAXLINE];
547812Seric 	register FILE *tfp;
554632Seric 	register HDR *h;
565007Seric 	register ADDRESS *q;
5710173Seric 	MAILER nullmailer;
584632Seric 
595037Seric 	/*
60*17468Seric 	**  Create control file if necessary.
615037Seric 	*/
624632Seric 
63*17468Seric 	tfp = e->e_qfp;
647812Seric 	if (tfp == NULL)
654632Seric 	{
66*17468Seric 		tf = newstr(queuename(e, 't'));
67*17468Seric 		tfp = fopen(tf, "w");
68*17468Seric 		if (tfp == NULL)
69*17468Seric 		{
70*17468Seric 			syserr("queueup: cannot create temp file %s", tf);
71*17468Seric 			return;
72*17468Seric 		}
73*17468Seric 		(void) chmod(tf, FileMode);
744632Seric 	}
75*17468Seric 	else
76*17468Seric 		tf = NULL;
774632Seric 
784632Seric # ifdef DEBUG
797677Seric 	if (tTd(40, 1))
80*17468Seric 		printf("queueing %s\n", e->e_id);
814632Seric # endif DEBUG
824632Seric 
834632Seric 	/*
846980Seric 	**  If there is no data file yet, create one.
856980Seric 	*/
866980Seric 
876980Seric 	if (e->e_df == NULL)
886980Seric 	{
896980Seric 		register FILE *dfp;
909389Seric 		extern putbody();
916980Seric 
927812Seric 		e->e_df = newstr(queuename(e, 'd'));
936980Seric 		dfp = fopen(e->e_df, "w");
946980Seric 		if (dfp == NULL)
956980Seric 		{
966980Seric 			syserr("queueup: cannot create %s", e->e_df);
977812Seric 			(void) fclose(tfp);
986980Seric 			return;
996980Seric 		}
1009048Seric 		(void) chmod(e->e_df, FileMode);
10110173Seric 		(*e->e_putbody)(dfp, ProgMailer, e);
1027009Seric 		(void) fclose(dfp);
1039389Seric 		e->e_putbody = putbody;
1046980Seric 	}
1056980Seric 
1066980Seric 	/*
1074632Seric 	**  Output future work requests.
1089377Seric 	**	Priority should be first, since it is read by orderq.
1094632Seric 	*/
1104632Seric 
1119377Seric 	/* output message priority */
1129377Seric 	fprintf(tfp, "P%ld\n", e->e_msgpriority);
1139377Seric 
1149630Seric 	/* output creation time */
1159630Seric 	fprintf(tfp, "T%ld\n", e->e_ctime);
1169630Seric 
1174632Seric 	/* output name of data file */
1187812Seric 	fprintf(tfp, "D%s\n", e->e_df);
1194632Seric 
12010108Seric 	/* message from envelope, if it exists */
12110108Seric 	if (e->e_message != NULL)
12210108Seric 		fprintf(tfp, "M%s\n", e->e_message);
12310108Seric 
1244632Seric 	/* output name of sender */
1257812Seric 	fprintf(tfp, "S%s\n", e->e_from.q_paddr);
1264632Seric 
1274632Seric 	/* output list of recipient addresses */
1286980Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1294632Seric 	{
1307763Seric 		if (queueall ? !bitset(QDONTSEND, q->q_flags) :
1317763Seric 			       bitset(QQUEUEUP, q->q_flags))
1328245Seric 		{
1337812Seric 			fprintf(tfp, "R%s\n", q->q_paddr);
1349377Seric 			if (announce)
1359377Seric 			{
1369377Seric 				e->e_to = q->q_paddr;
1379377Seric 				message(Arpa_Info, "queued");
1389377Seric 				if (LogLevel > 4)
1399377Seric 					logdelivery("queued");
1409377Seric 				e->e_to = NULL;
1419377Seric 			}
1429387Seric #ifdef DEBUG
1439387Seric 			if (tTd(40, 1))
1449387Seric 			{
1459387Seric 				printf("queueing ");
1469387Seric 				printaddr(q, FALSE);
1479387Seric 			}
1489387Seric #endif DEBUG
1498245Seric 		}
1504632Seric 	}
1514632Seric 
1529377Seric 	/*
1539377Seric 	**  Output headers for this message.
1549377Seric 	**	Expand macros completely here.  Queue run will deal with
1559377Seric 	**	everything as absolute headers.
1569377Seric 	**		All headers that must be relative to the recipient
1579377Seric 	**		can be cracked later.
15810173Seric 	**	We set up a "null mailer" -- i.e., a mailer that will have
15910173Seric 	**	no effect on the addresses as they are output.
1609377Seric 	*/
1619377Seric 
16210686Seric 	bzero((char *) &nullmailer, sizeof nullmailer);
16310173Seric 	nullmailer.m_r_rwset = nullmailer.m_s_rwset = -1;
16410349Seric 	nullmailer.m_eol = "\n";
16510173Seric 
16616147Seric 	define('g', "\001f", e);
1676980Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
1684632Seric 	{
16910686Seric 		extern bool bitzerop();
17010686Seric 
17112015Seric 		/* don't output null headers */
1724632Seric 		if (h->h_value == NULL || h->h_value[0] == '\0')
1734632Seric 			continue;
17412015Seric 
17512015Seric 		/* don't output resent headers on non-resent messages */
17612015Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
17712015Seric 			continue;
17812015Seric 
17912015Seric 		/* output this header */
1807812Seric 		fprintf(tfp, "H");
18112015Seric 
18212015Seric 		/* if conditional, output the set of conditions */
18310686Seric 		if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags))
18410686Seric 		{
18510686Seric 			int j;
18610686Seric 
18710686Seric 			putc('?', tfp);
18810686Seric 			for (j = '\0'; j <= '\177'; j++)
18910686Seric 				if (bitnset(j, h->h_mflags))
19010686Seric 					putc(j, tfp);
19110686Seric 			putc('?', tfp);
19210686Seric 		}
19312015Seric 
19412015Seric 		/* output the header: expand macros, convert addresses */
1957763Seric 		if (bitset(H_DEFAULT, h->h_flags))
1967763Seric 		{
1977763Seric 			(void) expand(h->h_value, buf, &buf[sizeof buf], e);
1988236Seric 			fprintf(tfp, "%s: %s\n", h->h_field, buf);
1997763Seric 		}
2008245Seric 		else if (bitset(H_FROM|H_RCPT, h->h_flags))
2019348Seric 		{
2029348Seric 			commaize(h, h->h_value, tfp, bitset(EF_OLDSTYLE, e->e_flags),
20310173Seric 				 &nullmailer);
2049348Seric 		}
2057763Seric 		else
2068245Seric 			fprintf(tfp, "%s: %s\n", h->h_field, h->h_value);
2074632Seric 	}
2084632Seric 
2094632Seric 	/*
2104632Seric 	**  Clean up.
2114632Seric 	*/
2124632Seric 
213*17468Seric 	qf = queuename(e, 'q');
214*17468Seric 	if (tf != NULL)
215*17468Seric 	{
216*17468Seric # ifdef FLOCK
217*17468Seric 		(void) flock(fileno(tfp), LOCK_EX|LOCK_NB);
218*17468Seric 		if (rename(tf, qf) < 0)
219*17468Seric 			syserr("cannot rename(%s, %s), df=%s", tf, qf, e->e_df);
220*17468Seric # else FLOCK
221*17468Seric 		holdsigs();
222*17468Seric 		(void) unlink(qf);
223*17468Seric 		if (link(tf, qf) < 0)
224*17468Seric 			syserr("cannot link(%s, %s), df=%s", tf, qf, e->e_df);
225*17468Seric 		else
226*17468Seric 			(void) unlink(tf);
227*17468Seric 		rlsesigs();
228*17468Seric # endif FLOCK
229*17468Seric 	}
2307812Seric 	(void) fclose(tfp);
231*17468Seric 	e->e_qfp = NULL;
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:
2464632Seric **		none.
2474632Seric **
2484632Seric **	Returns:
2494632Seric **		none.
2504632Seric **
2514632Seric **	Side Effects:
2524632Seric **		runs things in the mail queue.
2534632Seric */
2544632Seric 
2554639Seric runqueue(forkflag)
2564639Seric 	bool forkflag;
2574632Seric {
2587466Seric 	/*
2597466Seric 	**  See if we want to go off and do other useful work.
2607466Seric 	*/
2614639Seric 
2624639Seric 	if (forkflag)
2634639Seric 	{
2647943Seric 		int pid;
2657943Seric 
2667943Seric 		pid = dofork();
2677943Seric 		if (pid != 0)
2684639Seric 		{
2697943Seric 			/* parent -- pick up intermediate zombie */
2709377Seric 			(void) waitfor(pid);
2717690Seric 			if (QueueIntvl != 0)
2729348Seric 				(void) setevent(QueueIntvl, runqueue, TRUE);
2734639Seric 			return;
2744639Seric 		}
2757943Seric 		/* child -- double fork */
2767943Seric 		if (fork() != 0)
2777943Seric 			exit(EX_OK);
2784639Seric 	}
2797876Seric # ifdef LOG
2807876Seric 	if (LogLevel > 11)
2817943Seric 		syslog(LOG_DEBUG, "runqueue %s, pid=%d", QueueDir, getpid());
2827876Seric # endif LOG
2834639Seric 
2847466Seric 	/*
28510205Seric 	**  Release any resources used by the daemon code.
28610205Seric 	*/
28710205Seric 
28810205Seric # ifdef DAEMON
28910205Seric 	clrdaemon();
29010205Seric # endif DAEMON
29110205Seric 
29210205Seric 	/*
2937466Seric 	**  Start making passes through the queue.
2947466Seric 	**	First, read and sort the entire queue.
2957466Seric 	**	Then, process the work in that order.
2967466Seric 	**		But if you take too long, start over.
2977466Seric 	*/
2987466Seric 
2997943Seric 	/* order the existing work requests */
30010070Seric 	(void) orderq();
3017690Seric 
3027943Seric 	/* process them once at a time */
3037943Seric 	while (WorkQ != NULL)
3044639Seric 	{
3057943Seric 		WORK *w = WorkQ;
3067881Seric 
3077943Seric 		WorkQ = WorkQ->w_next;
3087943Seric 		dowork(w);
3097943Seric 		free(w->w_name);
3107943Seric 		free((char *) w);
3114639Seric 	}
3127943Seric 	finis();
3134634Seric }
3144634Seric /*
3154632Seric **  ORDERQ -- order the work queue.
3164632Seric **
3174632Seric **	Parameters:
3184632Seric **		none.
3194632Seric **
3204632Seric **	Returns:
32110121Seric **		The number of request in the queue (not necessarily
32210121Seric **		the number of requests in WorkQ however).
3234632Seric **
3244632Seric **	Side Effects:
3254632Seric **		Sets WorkQ to the queue of available work, in order.
3264632Seric */
3274632Seric 
3284632Seric # define WLSIZE		120	/* max size of worklist per sort */
3294632Seric 
3304632Seric orderq()
3314632Seric {
3326625Sglickman 	register struct direct *d;
3334632Seric 	register WORK *w;
3344632Seric 	register WORK **wp;		/* parent of w */
3356625Sglickman 	DIR *f;
3364632Seric 	register int i;
33710121Seric 	WORK wlist[WLSIZE+1];
33810070Seric 	int wn = -1;
3394632Seric 	extern workcmpf();
3404632Seric 
3414632Seric 	/* clear out old WorkQ */
3424632Seric 	for (w = WorkQ; w != NULL; )
3434632Seric 	{
3444632Seric 		register WORK *nw = w->w_next;
3454632Seric 
3464632Seric 		WorkQ = nw;
3474632Seric 		free(w->w_name);
3484632Seric 		free((char *) w);
3494632Seric 		w = nw;
3504632Seric 	}
3514632Seric 
3524632Seric 	/* open the queue directory */
3538148Seric 	f = opendir(".");
3544632Seric 	if (f == NULL)
3554632Seric 	{
3568148Seric 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
35710070Seric 		return (0);
3584632Seric 	}
3594632Seric 
3604632Seric 	/*
3614632Seric 	**  Read the work directory.
3624632Seric 	*/
3634632Seric 
36410070Seric 	while ((d = readdir(f)) != NULL)
3654632Seric 	{
3669377Seric 		FILE *cf;
3674632Seric 		char lbuf[MAXNAME];
3684632Seric 
3694632Seric 		/* is this an interesting entry? */
3707812Seric 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
3714632Seric 			continue;
3724632Seric 
37310070Seric 		/* yes -- open control file (if not too many files) */
37410070Seric 		if (++wn >= WLSIZE)
37510070Seric 			continue;
3768148Seric 		cf = fopen(d->d_name, "r");
3774632Seric 		if (cf == NULL)
3784632Seric 		{
3797055Seric 			/* this may be some random person sending hir msgs */
3807055Seric 			/* syserr("orderq: cannot open %s", cbuf); */
38110090Seric #ifdef DEBUG
38210090Seric 			if (tTd(41, 2))
38310090Seric 				printf("orderq: cannot open %s (%d)\n",
38410090Seric 					d->d_name, errno);
38510090Seric #endif DEBUG
3867055Seric 			errno = 0;
38710090Seric 			wn--;
3884632Seric 			continue;
3894632Seric 		}
3908148Seric 		wlist[wn].w_name = newstr(d->d_name);
3914632Seric 
3924632Seric 		/* extract useful information */
3934632Seric 		while (fgets(lbuf, sizeof lbuf, cf) != NULL)
3944632Seric 		{
3959377Seric 			if (lbuf[0] == 'P')
3964632Seric 			{
3975037Seric 				(void) sscanf(&lbuf[1], "%ld", &wlist[wn].w_pri);
3984632Seric 				break;
3994632Seric 			}
4004632Seric 		}
4014632Seric 		(void) fclose(cf);
4024632Seric 	}
4036625Sglickman 	(void) closedir(f);
40410090Seric 	wn++;
4054632Seric 
4064632Seric 	/*
4074632Seric 	**  Sort the work directory.
4084632Seric 	*/
4094632Seric 
41010121Seric 	qsort(wlist, min(wn, WLSIZE), sizeof *wlist, workcmpf);
4114632Seric 
4124632Seric 	/*
4134632Seric 	**  Convert the work list into canonical form.
4149377Seric 	**	Should be turning it into a list of envelopes here perhaps.
4154632Seric 	*/
4164632Seric 
4174632Seric 	wp = &WorkQ;
41810121Seric 	for (i = min(wn, WLSIZE); --i >= 0; )
4194632Seric 	{
4204632Seric 		w = (WORK *) xalloc(sizeof *w);
4214632Seric 		w->w_name = wlist[i].w_name;
4224632Seric 		w->w_pri = wlist[i].w_pri;
4234632Seric 		w->w_next = NULL;
4244632Seric 		*wp = w;
4254632Seric 		wp = &w->w_next;
4264632Seric 	}
4274632Seric 
4284632Seric # ifdef DEBUG
4297677Seric 	if (tTd(40, 1))
4304632Seric 	{
4314632Seric 		for (w = WorkQ; w != NULL; w = w->w_next)
4325037Seric 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
4334632Seric 	}
4344632Seric # endif DEBUG
43510070Seric 
43610090Seric 	return (wn);
4374632Seric }
4384632Seric /*
4397677Seric **  WORKCMPF -- compare function for ordering work.
4404632Seric **
4414632Seric **	Parameters:
4424632Seric **		a -- the first argument.
4434632Seric **		b -- the second argument.
4444632Seric **
4454632Seric **	Returns:
44610121Seric **		1 if a < b
4474632Seric **		0 if a == b
44810121Seric **		-1 if a > b
4494632Seric **
4504632Seric **	Side Effects:
4514632Seric **		none.
4524632Seric */
4534632Seric 
4544632Seric workcmpf(a, b)
4555037Seric 	register WORK *a;
4565037Seric 	register WORK *b;
4574632Seric {
4585037Seric 	if (a->w_pri == b->w_pri)
4594632Seric 		return (0);
4605037Seric 	else if (a->w_pri > b->w_pri)
46110121Seric 		return (-1);
46210121Seric 	else
4634632Seric 		return (1);
4644632Seric }
4654632Seric /*
4664632Seric **  DOWORK -- do a work request.
4674632Seric **
4684632Seric **	Parameters:
4694632Seric **		w -- the work request to be satisfied.
4704632Seric **
4714632Seric **	Returns:
4724632Seric **		none.
4734632Seric **
4744632Seric **	Side Effects:
4754632Seric **		The work request is satisfied if possible.
4764632Seric */
4774632Seric 
4784632Seric dowork(w)
4794632Seric 	register WORK *w;
4804632Seric {
4814632Seric 	register int i;
4824632Seric 
4834632Seric # ifdef DEBUG
4847677Seric 	if (tTd(40, 1))
4855037Seric 		printf("dowork: %s pri %ld\n", w->w_name, w->w_pri);
4864632Seric # endif DEBUG
4874632Seric 
4884632Seric 	/*
4894632Seric 	**  Fork for work.
4904632Seric 	*/
4914632Seric 
4924632Seric 	i = fork();
4934632Seric 	if (i < 0)
4944632Seric 	{
4954632Seric 		syserr("dowork: cannot fork");
4964632Seric 		return;
4974632Seric 	}
4984632Seric 
4994632Seric 	if (i == 0)
5004632Seric 	{
501*17468Seric 		FILE *qfp;
502*17468Seric 
5034632Seric 		/*
5044632Seric 		**  CHILD
5058148Seric 		**	Lock the control file to avoid duplicate deliveries.
5068148Seric 		**		Then run the file as though we had just read it.
5077350Seric 		**	We save an idea of the temporary name so we
5087350Seric 		**		can recover on interrupt.
5094632Seric 		*/
5104632Seric 
5117763Seric 		/* set basic modes, etc. */
5127356Seric 		(void) alarm(0);
51310195Seric 		closexscript(CurEnv);
5149338Seric 		CurEnv->e_flags &= ~EF_FATALERRS;
5154632Seric 		QueueRun = TRUE;
5169377Seric 		ErrorMode = EM_MAIL;
5178148Seric 		CurEnv->e_id = &w->w_name[2];
5187876Seric # ifdef LOG
5197876Seric 		if (LogLevel > 11)
5207881Seric 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", CurEnv->e_id,
5217881Seric 			       getpid());
5227876Seric # endif LOG
5237763Seric 
5247763Seric 		/* don't use the headers from sendmail.cf... */
5257763Seric 		CurEnv->e_header = NULL;
5267763Seric 
527*17468Seric 		FileName = queuename(CurEnv, 'q');
528*17468Seric 		qfp = fopen(FileName, "r");
529*17468Seric 		if (qfp == NULL)
530*17468Seric 			exit(EX_OK);
531*17468Seric 
532*17468Seric 		/* lock the control file during processing */
533*17468Seric # ifdef FLOCK
534*17468Seric 		if (flock(fileno(qfp), LOCK_EX|LOCK_NB) < 0)
535*17468Seric # else FLOCK
5367812Seric 		if (link(w->w_name, queuename(CurEnv, 'l')) < 0)
537*17468Seric # endif FLOCK
5386980Seric 		{
5397812Seric 			/* being processed by another queuer */
5407881Seric # ifdef LOG
5417881Seric 			if (LogLevel > 4)
5427881Seric 				syslog(LOG_DEBUG, "%s: locked", CurEnv->e_id);
5437881Seric # endif LOG
5446980Seric 			exit(EX_OK);
5456980Seric 		}
5466980Seric 
5476980Seric 		/* do basic system initialization */
5484632Seric 		initsys();
5496980Seric 
5506980Seric 		/* read the queue control file */
551*17468Seric 		readqf(qfp, CurEnv, TRUE);
552*17468Seric 		(void) fclose(qfp);
5539338Seric 		CurEnv->e_flags |= EF_INQUEUE;
5549377Seric 		eatheader(CurEnv);
5556980Seric 
5566980Seric 		/* do the delivery */
5579338Seric 		if (!bitset(EF_FATALERRS, CurEnv->e_flags))
5589282Seric 			sendall(CurEnv, SM_DELIVER);
5596980Seric 
5606980Seric 		/* finish up and exit */
5614632Seric 		finis();
5624632Seric 	}
5634632Seric 
5644632Seric 	/*
5654632Seric 	**  Parent -- pick up results.
5664632Seric 	*/
5674632Seric 
5684632Seric 	errno = 0;
5699377Seric 	(void) waitfor(i);
5704632Seric }
5714632Seric /*
5724632Seric **  READQF -- read queue file and set up environment.
5734632Seric **
5744632Seric **	Parameters:
575*17468Seric **		qfp -- the file pointer for the qf file.
5769377Seric **		e -- the envelope of the job to run.
5779630Seric **		full -- if set, read in all information.  Otherwise just
5789630Seric **			read in info needed for a queue print.
5794632Seric **
5804632Seric **	Returns:
5814632Seric **		none.
5824632Seric **
5834632Seric **	Side Effects:
5844632Seric **		cf is read and created as the current job, as though
5854632Seric **		we had been invoked by argument.
5864632Seric */
5874632Seric 
588*17468Seric readqf(qfp, e, full)
589*17468Seric 	register FILE *qfp;
5909377Seric 	register ENVELOPE *e;
5919630Seric 	bool full;
5924632Seric {
5937785Seric 	char buf[MAXFIELD];
5949348Seric 	extern char *fgetfolded();
5954632Seric 
5964632Seric 	/*
597*17468Seric 	**  Read and process the file.
5984632Seric 	*/
5994632Seric 
6009377Seric 	LineNumber = 0;
6019630Seric 	if (Verbose && full)
6029377Seric 		printf("\nRunning %s\n", e->e_id);
603*17468Seric 	while (fgetfolded(buf, sizeof buf, qfp) != NULL)
6044632Seric 	{
6054632Seric 		switch (buf[0])
6064632Seric 		{
6074632Seric 		  case 'R':		/* specify recipient */
6089618Seric 			sendtolist(&buf[1], (ADDRESS *) NULL, &e->e_sendqueue);
6094632Seric 			break;
6104632Seric 
6114632Seric 		  case 'H':		/* header */
6129630Seric 			if (full)
6139630Seric 				(void) chompheader(&buf[1], FALSE);
6144632Seric 			break;
6154632Seric 
61610108Seric 		  case 'M':		/* message */
61710108Seric 			e->e_message = newstr(&buf[1]);
61810108Seric 			break;
61910108Seric 
6204632Seric 		  case 'S':		/* sender */
6214634Seric 			setsender(newstr(&buf[1]));
6224632Seric 			break;
6234632Seric 
6244632Seric 		  case 'D':		/* data file name */
6259630Seric 			if (!full)
6269630Seric 				break;
6279377Seric 			e->e_df = newstr(&buf[1]);
6289544Seric 			e->e_dfp = fopen(e->e_df, "r");
6299544Seric 			if (e->e_dfp == NULL)
6309377Seric 				syserr("readqf: cannot open %s", e->e_df);
6314632Seric 			break;
6324632Seric 
6337860Seric 		  case 'T':		/* init time */
6349377Seric 			(void) sscanf(&buf[1], "%ld", &e->e_ctime);
6354632Seric 			break;
6364632Seric 
6374634Seric 		  case 'P':		/* message priority */
6389377Seric 			(void) sscanf(&buf[1], "%ld", &e->e_msgpriority);
6395037Seric 
6405037Seric 			/* make sure that big things get sent eventually */
6419377Seric 			e->e_msgpriority -= WKTIMEFACT;
6424634Seric 			break;
6434634Seric 
6444632Seric 		  default:
6459377Seric 			syserr("readqf(%s): bad line \"%s\"", e->e_id, buf);
6464632Seric 			break;
6474632Seric 		}
6484632Seric 	}
6499377Seric 
6509377Seric 	FileName = NULL;
6514632Seric }
6524632Seric /*
6539630Seric **  PRINTQUEUE -- print out a representation of the mail queue
6549630Seric **
6559630Seric **	Parameters:
6569630Seric **		none.
6579630Seric **
6589630Seric **	Returns:
6599630Seric **		none.
6609630Seric **
6619630Seric **	Side Effects:
6629630Seric **		Prints a listing of the mail queue on the standard output.
6639630Seric */
6645182Seric 
6659630Seric printqueue()
6669630Seric {
6679630Seric 	register WORK *w;
6689630Seric 	FILE *f;
66910070Seric 	int nrequests;
6709630Seric 	char buf[MAXLINE];
6719630Seric 
6729630Seric 	/*
6739630Seric 	**  Read and order the queue.
6749630Seric 	*/
6759630Seric 
67610070Seric 	nrequests = orderq();
6779630Seric 
6789630Seric 	/*
6799630Seric 	**  Print the work list that we have read.
6809630Seric 	*/
6819630Seric 
6829630Seric 	/* first see if there is anything */
68310070Seric 	if (nrequests <= 0)
6849630Seric 	{
68510070Seric 		printf("Mail queue is empty\n");
6869630Seric 		return;
6879630Seric 	}
6889630Seric 
68910096Seric 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
69010070Seric 	if (nrequests > WLSIZE)
69110070Seric 		printf(", only %d printed", WLSIZE);
69210070Seric 	printf(")\n--QID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
6939630Seric 	for (w = WorkQ; w != NULL; w = w->w_next)
6949630Seric 	{
6959630Seric 		struct stat st;
69610070Seric 		auto time_t submittime = 0;
69710070Seric 		long dfsize = -1;
698*17468Seric 		int fd;
699*17468Seric # ifndef FLOCK
70010108Seric 		char lf[20];
701*17468Seric # endif FLOCK
70210108Seric 		char message[MAXLINE];
7039630Seric 
704*17468Seric 		f = fopen(w->w_name, "r");
705*17468Seric 		if (f == NULL)
706*17468Seric 		{
707*17468Seric 			errno = 0;
708*17468Seric 			continue;
709*17468Seric 		}
7109630Seric 		printf("%7s", w->w_name + 2);
711*17468Seric # ifdef FLOCK
712*17468Seric 		fd = fileno(f);
713*17468Seric 
714*17468Seric 		if (flock(fd, LOCK_EX|LOCK_NB) < 0)
715*17468Seric 		{
716*17468Seric 			printf("*");
717*17468Seric 		}
718*17468Seric 		else
719*17468Seric 		{
720*17468Seric 			flock(fd, LOCK_UN);
721*17468Seric 			printf(" ");
722*17468Seric 		}
723*17468Seric # else FLOCK
72410070Seric 		strcpy(lf, w->w_name);
72510070Seric 		lf[0] = 'l';
72610070Seric 		if (stat(lf, &st) >= 0)
72710070Seric 			printf("*");
72810070Seric 		else
72910070Seric 			printf(" ");
730*17468Seric # endif FLOCK
73110070Seric 		errno = 0;
732*17468Seric 
73310108Seric 		message[0] = '\0';
7349630Seric 		while (fgets(buf, sizeof buf, f) != NULL)
7359630Seric 		{
7369630Seric 			fixcrlf(buf, TRUE);
7379630Seric 			switch (buf[0])
7389630Seric 			{
73910108Seric 			  case 'M':	/* error message */
74010108Seric 				strcpy(message, &buf[1]);
74110108Seric 				break;
74210108Seric 
7439630Seric 			  case 'S':	/* sender name */
74413015Seric 				printf("%8ld %.16s %.45s", dfsize,
74512517Seric 					ctime(&submittime), &buf[1]);
74610108Seric 				if (message[0] != '\0')
74712517Seric 					printf("\n\t\t\t\t  (%.43s)", message);
7489630Seric 				break;
7499630Seric 
7509630Seric 			  case 'R':	/* recipient name */
75112517Seric 				printf("\n\t\t\t\t  %.45s", &buf[1]);
7529630Seric 				break;
7539630Seric 
7549630Seric 			  case 'T':	/* creation time */
75510070Seric 				sscanf(&buf[1], "%ld", &submittime);
7569630Seric 				break;
75710070Seric 
75810070Seric 			  case 'D':	/* data file name */
75910070Seric 				if (stat(&buf[1], &st) >= 0)
76010070Seric 					dfsize = st.st_size;
76110070Seric 				break;
7629630Seric 			}
7639630Seric 		}
76410070Seric 		if (submittime == (time_t) 0)
76510070Seric 			printf(" (no control file)");
7669630Seric 		printf("\n");
7679630Seric 		fclose(f);
7689630Seric 	}
7699630Seric }
7709630Seric 
7715182Seric # endif QUEUE
772*17468Seric /*
773*17468Seric **  QUEUENAME -- build a file name in the queue directory for this envelope.
774*17468Seric **
775*17468Seric **	Assigns an id code if one does not already exist.
776*17468Seric **	This code is very careful to avoid trashing existing files
777*17468Seric **	under any circumstances.
778*17468Seric **		We first create an nf file that is only used when
779*17468Seric **		assigning an id.  This file is always empty, so that
780*17468Seric **		we can never accidently truncate an lf file.
781*17468Seric **
782*17468Seric **	Parameters:
783*17468Seric **		e -- envelope to build it in/from.
784*17468Seric **		type -- the file type, used as the first character
785*17468Seric **			of the file name.
786*17468Seric **
787*17468Seric **	Returns:
788*17468Seric **		a pointer to the new file name (in a static buffer).
789*17468Seric **
790*17468Seric **	Side Effects:
791*17468Seric **		Will create the lf and qf files if no id code is
792*17468Seric **		already assigned.  This will cause the envelope
793*17468Seric **		to be modified.
794*17468Seric */
795*17468Seric 
796*17468Seric char *
797*17468Seric queuename(e, type)
798*17468Seric 	register ENVELOPE *e;
799*17468Seric 	char type;
800*17468Seric {
801*17468Seric 	static char buf[MAXNAME];
802*17468Seric 	static int pid = -1;
803*17468Seric 	char c1 = 'A';
804*17468Seric 	char c2 = 'A';
805*17468Seric 
806*17468Seric 	if (e->e_id == NULL)
807*17468Seric 	{
808*17468Seric 		char qf[20];
809*17468Seric 		char nf[20];
810*17468Seric # ifndef FLOCK
811*17468Seric 		char lf[20];
812*17468Seric # endif FLOCK
813*17468Seric 
814*17468Seric 		/* find a unique id */
815*17468Seric 		if (pid != getpid())
816*17468Seric 		{
817*17468Seric 			/* new process -- start back at "AA" */
818*17468Seric 			pid = getpid();
819*17468Seric 			c1 = 'A';
820*17468Seric 			c2 = 'A' - 1;
821*17468Seric 		}
822*17468Seric 		(void) sprintf(qf, "qfAA%05d", pid);
823*17468Seric # ifndef FLOCK
824*17468Seric 		strcpy(lf, qf);
825*17468Seric 		lf[0] = 'l';
826*17468Seric # endif FLOCK
827*17468Seric 		strcpy(nf, qf);
828*17468Seric 		nf[0] = 'n';
829*17468Seric 
830*17468Seric 		while (c1 < '~' || c2 < 'Z')
831*17468Seric 		{
832*17468Seric 			int i;
833*17468Seric 
834*17468Seric 			if (c2 >= 'Z')
835*17468Seric 			{
836*17468Seric 				c1++;
837*17468Seric 				c2 = 'A' - 1;
838*17468Seric 			}
839*17468Seric 			nf[2] = qf[2] = c1;
840*17468Seric 			nf[3] = qf[3] = ++c2;
841*17468Seric # ifndef FLOCK
842*17468Seric 			lf[2] = c1;
843*17468Seric 			lf[3] = c2;
844*17468Seric # endif FLOCK
845*17468Seric # ifdef DEBUG
846*17468Seric 			if (tTd(7, 20))
847*17468Seric 				printf("queuename: trying \"%s\"\n", nf);
848*17468Seric # endif DEBUG
849*17468Seric 
850*17468Seric # ifdef FLOCK
851*17468Seric 			i = open(nf, O_WRONLY|O_CREAT|O_EXCL, FileMode);
852*17468Seric 			if (i >= 0)
853*17468Seric 			{
854*17468Seric 				(void) flock(i, LOCK_EX|LOCK_NB);
855*17468Seric 				if (link(nf, qf) < 0)
856*17468Seric 				{
857*17468Seric 					(void) close(i);
858*17468Seric 					(void) unlink(nf);
859*17468Seric 					continue;
860*17468Seric 				}
861*17468Seric 				e->e_qfp = fdopen(i, "w");
862*17468Seric 				(void) unlink(nf);
863*17468Seric 				break;
864*17468Seric 			}
865*17468Seric # else FLOCK
866*17468Seric # ifdef QUEUE
867*17468Seric 			if (access(lf, 0) >= 0 || access(qf, 0) >= 0)
868*17468Seric 				continue;
869*17468Seric 			errno = 0;
870*17468Seric 			i = creat(nf, FileMode);
871*17468Seric 			if (i < 0)
872*17468Seric 			{
873*17468Seric 				(void) unlink(nf);	/* kernel bug */
874*17468Seric 				continue;
875*17468Seric 			}
876*17468Seric 			(void) close(i);
877*17468Seric 			i = link(nf, lf);
878*17468Seric 			(void) unlink(nf);
879*17468Seric 			if (i < 0)
880*17468Seric 				continue;
881*17468Seric 			if (link(lf, qf) >= 0)
882*17468Seric 				break;
883*17468Seric 			(void) unlink(lf);
884*17468Seric # else QUEUE
885*17468Seric 			if (close(creat(qf, FileMode)) < 0)
886*17468Seric 				continue;
887*17468Seric # endif QUEUE
888*17468Seric # endif FLOCK
889*17468Seric 		}
890*17468Seric 		if (c1 >= '~' && c2 >= 'Z')
891*17468Seric 		{
892*17468Seric 			syserr("queuename: Cannot create \"%s\" in \"%s\"",
893*17468Seric 				qf, QueueDir);
894*17468Seric 			exit(EX_OSERR);
895*17468Seric 		}
896*17468Seric 		e->e_id = newstr(&qf[2]);
897*17468Seric 		define('i', e->e_id, e);
898*17468Seric # ifdef DEBUG
899*17468Seric 		if (tTd(7, 1))
900*17468Seric 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
901*17468Seric # ifdef LOG
902*17468Seric 		if (LogLevel > 16)
903*17468Seric 			syslog(LOG_DEBUG, "%s: assigned id", e->e_id);
904*17468Seric # endif LOG
905*17468Seric # endif DEBUG
906*17468Seric 	}
907*17468Seric 
908*17468Seric 	if (type == '\0')
909*17468Seric 		return (NULL);
910*17468Seric 	(void) sprintf(buf, "%cf%s", type, e->e_id);
911*17468Seric # ifdef DEBUG
912*17468Seric 	if (tTd(7, 2))
913*17468Seric 		printf("queuename: %s\n", buf);
914*17468Seric # endif DEBUG
915*17468Seric 	return (buf);
916*17468Seric }
917*17468Seric /*
918*17468Seric **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
919*17468Seric **
920*17468Seric **	Parameters:
921*17468Seric **		e -- the envelope to unlock.
922*17468Seric **
923*17468Seric **	Returns:
924*17468Seric **		none
925*17468Seric **
926*17468Seric **	Side Effects:
927*17468Seric **		unlocks the queue for `e'.
928*17468Seric */
929*17468Seric 
930*17468Seric unlockqueue(e)
931*17468Seric 	ENVELOPE *e;
932*17468Seric {
933*17468Seric 	/* remove the transcript */
934*17468Seric #ifdef DEBUG
935*17468Seric # ifdef LOG
936*17468Seric 	if (LogLevel > 19)
937*17468Seric 		syslog(LOG_DEBUG, "%s: unlock", e->e_id);
938*17468Seric # endif LOG
939*17468Seric 	if (!tTd(51, 4))
940*17468Seric #endif DEBUG
941*17468Seric 		xunlink(queuename(e, 'x'));
942*17468Seric 
943*17468Seric # ifdef QUEUE
944*17468Seric # ifndef FLOCK
945*17468Seric 	/* last but not least, remove the lock */
946*17468Seric 	xunlink(queuename(e, 'l'));
947*17468Seric # endif FLOCK
948*17468Seric # endif QUEUE
949*17468Seric }
950