xref: /csrg-svn/usr.sbin/sendmail/src/queue.c (revision 26504)
1 /*
2 **  Sendmail
3 **  Copyright (c) 1983  Eric P. Allman
4 **  Berkeley, California
5 **
6 **  Copyright (c) 1983 Regents of the University of California.
7 **  All rights reserved.  The Berkeley software License Agreement
8 **  specifies the terms and conditions for redistribution.
9 */
10 
11 
12 # include "sendmail.h"
13 # include <sys/stat.h>
14 # include <sys/dir.h>
15 # include <signal.h>
16 # include <errno.h>
17 
18 # ifndef QUEUE
19 # ifndef lint
20 static char	SccsId[] = "@(#)queue.c	5.20 (Berkeley) 03/08/86	(no queueing)";
21 # endif not lint
22 # else QUEUE
23 
24 # ifndef lint
25 static char	SccsId[] = "@(#)queue.c	5.20 (Berkeley) 03/08/86";
26 # endif not lint
27 
28 /*
29 **  Work queue.
30 */
31 
32 struct work
33 {
34 	char		*w_name;	/* name of control file */
35 	long		w_pri;		/* priority of message, see below */
36 	time_t		w_ctime;	/* creation time of message */
37 	struct work	*w_next;	/* next in queue */
38 };
39 
40 typedef struct work	WORK;
41 
42 WORK	*WorkQ;			/* queue of things to be done */
43 /*
44 **  QUEUEUP -- queue a message up for future transmission.
45 **
46 **	Parameters:
47 **		e -- the envelope to queue up.
48 **		queueall -- if TRUE, queue all addresses, rather than
49 **			just those with the QQUEUEUP flag set.
50 **		announce -- if TRUE, tell when you are queueing up.
51 **
52 **	Returns:
53 **		none.
54 **
55 **	Side Effects:
56 **		The current request are saved in a control file.
57 */
58 
59 queueup(e, queueall, announce)
60 	register ENVELOPE *e;
61 	bool queueall;
62 	bool announce;
63 {
64 	char *tf;
65 	char *qf;
66 	char buf[MAXLINE];
67 	register FILE *tfp;
68 	register HDR *h;
69 	register ADDRESS *q;
70 	MAILER nullmailer;
71 
72 	/*
73 	**  Create control file.
74 	*/
75 
76 	tf = newstr(queuename(e, 't'));
77 	tfp = fopen(tf, "w");
78 	if (tfp == NULL)
79 	{
80 		syserr("queueup: cannot create temp file %s", tf);
81 		return;
82 	}
83 	(void) chmod(tf, FileMode);
84 
85 # ifdef DEBUG
86 	if (tTd(40, 1))
87 		printf("queueing %s\n", e->e_id);
88 # endif DEBUG
89 
90 	/*
91 	**  If there is no data file yet, create one.
92 	*/
93 
94 	if (e->e_df == NULL)
95 	{
96 		register FILE *dfp;
97 		extern putbody();
98 
99 		e->e_df = newstr(queuename(e, 'd'));
100 		dfp = fopen(e->e_df, "w");
101 		if (dfp == NULL)
102 		{
103 			syserr("queueup: cannot create %s", e->e_df);
104 			(void) fclose(tfp);
105 			return;
106 		}
107 		(void) chmod(e->e_df, FileMode);
108 		(*e->e_putbody)(dfp, ProgMailer, e);
109 		(void) fclose(dfp);
110 		e->e_putbody = putbody;
111 	}
112 
113 	/*
114 	**  Output future work requests.
115 	**	Priority and creation time should be first, since
116 	**	they are required by orderq.
117 	*/
118 
119 	/* output message priority */
120 	fprintf(tfp, "P%ld\n", e->e_msgpriority);
121 
122 	/* output creation time */
123 	fprintf(tfp, "T%ld\n", e->e_ctime);
124 
125 	/* output name of data file */
126 	fprintf(tfp, "D%s\n", e->e_df);
127 
128 	/* message from envelope, if it exists */
129 	if (e->e_message != NULL)
130 		fprintf(tfp, "M%s\n", e->e_message);
131 
132 	/* output name of sender */
133 	fprintf(tfp, "S%s\n", e->e_from.q_paddr);
134 
135 	/* output list of recipient addresses */
136 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
137 	{
138 		if (queueall ? !bitset(QDONTSEND, q->q_flags) :
139 			       bitset(QQUEUEUP, q->q_flags))
140 		{
141 			fprintf(tfp, "R%s\n", q->q_paddr);
142 			if (announce)
143 			{
144 				e->e_to = q->q_paddr;
145 				message(Arpa_Info, "queued");
146 				if (LogLevel > 4)
147 					logdelivery("queued");
148 				e->e_to = NULL;
149 			}
150 #ifdef DEBUG
151 			if (tTd(40, 1))
152 			{
153 				printf("queueing ");
154 				printaddr(q, FALSE);
155 			}
156 #endif DEBUG
157 		}
158 	}
159 
160 	/* output list of error recipients */
161 	for (q = e->e_errorqueue; q != NULL; q = q->q_next)
162 	{
163 		if (!bitset(QDONTSEND, q->q_flags))
164 			fprintf(tfp, "E%s\n", q->q_paddr);
165 	}
166 
167 	/*
168 	**  Output headers for this message.
169 	**	Expand macros completely here.  Queue run will deal with
170 	**	everything as absolute headers.
171 	**		All headers that must be relative to the recipient
172 	**		can be cracked later.
173 	**	We set up a "null mailer" -- i.e., a mailer that will have
174 	**	no effect on the addresses as they are output.
175 	*/
176 
177 	bzero((char *) &nullmailer, sizeof nullmailer);
178 	nullmailer.m_r_rwset = nullmailer.m_s_rwset = -1;
179 	nullmailer.m_eol = "\n";
180 
181 	define('g', "\001f", e);
182 	for (h = e->e_header; h != NULL; h = h->h_link)
183 	{
184 		extern bool bitzerop();
185 
186 		/* don't output null headers */
187 		if (h->h_value == NULL || h->h_value[0] == '\0')
188 			continue;
189 
190 		/* don't output resent headers on non-resent messages */
191 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
192 			continue;
193 
194 		/* output this header */
195 		fprintf(tfp, "H");
196 
197 		/* if conditional, output the set of conditions */
198 		if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags))
199 		{
200 			int j;
201 
202 			(void) putc('?', tfp);
203 			for (j = '\0'; j <= '\177'; j++)
204 				if (bitnset(j, h->h_mflags))
205 					(void) putc(j, tfp);
206 			(void) putc('?', tfp);
207 		}
208 
209 		/* output the header: expand macros, convert addresses */
210 		if (bitset(H_DEFAULT, h->h_flags))
211 		{
212 			(void) expand(h->h_value, buf, &buf[sizeof buf], e);
213 			fprintf(tfp, "%s: %s\n", h->h_field, buf);
214 		}
215 		else if (bitset(H_FROM|H_RCPT, h->h_flags))
216 		{
217 			commaize(h, h->h_value, tfp, bitset(EF_OLDSTYLE, e->e_flags),
218 				 &nullmailer);
219 		}
220 		else
221 			fprintf(tfp, "%s: %s\n", h->h_field, h->h_value);
222 	}
223 
224 	/*
225 	**  Clean up.
226 	*/
227 
228 	(void) fclose(tfp);
229 	qf = queuename(e, 'q');
230 	if (tf != NULL)
231 	{
232 		(void) unlink(qf);
233 		if (rename(tf, qf) < 0)
234 			syserr("cannot unlink(%s, %s), df=%s", tf, qf, e->e_df);
235 		errno = 0;
236 	}
237 
238 # ifdef LOG
239 	/* save log info */
240 	if (LogLevel > 15)
241 		syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df);
242 # endif LOG
243 }
244 /*
245 **  RUNQUEUE -- run the jobs in the queue.
246 **
247 **	Gets the stuff out of the queue in some presumably logical
248 **	order and processes them.
249 **
250 **	Parameters:
251 **		forkflag -- TRUE if the queue scanning should be done in
252 **			a child process.  We double-fork so it is not our
253 **			child and we don't have to clean up after it.
254 **
255 **	Returns:
256 **		none.
257 **
258 **	Side Effects:
259 **		runs things in the mail queue.
260 */
261 
262 runqueue(forkflag)
263 	bool forkflag;
264 {
265 	extern bool shouldqueue();
266 
267 	/*
268 	**  If no work will ever be selected, don't even bother reading
269 	**  the queue.
270 	*/
271 
272 	if (shouldqueue(-100000000L))
273 	{
274 		if (Verbose)
275 			printf("Skipping queue run -- load average too high\n");
276 
277 		if (forkflag)
278 			return;
279 		finis();
280 	}
281 
282 	/*
283 	**  See if we want to go off and do other useful work.
284 	*/
285 
286 	if (forkflag)
287 	{
288 		int pid;
289 
290 		pid = dofork();
291 		if (pid != 0)
292 		{
293 			extern reapchild();
294 
295 			/* parent -- pick up intermediate zombie */
296 #ifndef SIGCHLD
297 			(void) waitfor(pid);
298 #else SIGCHLD
299 			(void) signal(SIGCHLD, reapchild);
300 #endif SIGCHLD
301 			if (QueueIntvl != 0)
302 				(void) setevent(QueueIntvl, runqueue, TRUE);
303 			return;
304 		}
305 		/* child -- double fork */
306 #ifndef SIGCHLD
307 		if (fork() != 0)
308 			exit(EX_OK);
309 #else SIGCHLD
310 		(void) signal(SIGCHLD, SIG_DFL);
311 #endif SIGCHLD
312 	}
313 
314 	setproctitle("running queue");
315 
316 # ifdef LOG
317 	if (LogLevel > 11)
318 		syslog(LOG_DEBUG, "runqueue %s, pid=%d", QueueDir, getpid());
319 # endif LOG
320 
321 	/*
322 	**  Release any resources used by the daemon code.
323 	*/
324 
325 # ifdef DAEMON
326 	clrdaemon();
327 # endif DAEMON
328 
329 	/*
330 	**  Start making passes through the queue.
331 	**	First, read and sort the entire queue.
332 	**	Then, process the work in that order.
333 	**		But if you take too long, start over.
334 	*/
335 
336 	/* order the existing work requests */
337 	(void) orderq(FALSE);
338 
339 	/* process them once at a time */
340 	while (WorkQ != NULL)
341 	{
342 		WORK *w = WorkQ;
343 
344 		WorkQ = WorkQ->w_next;
345 		dowork(w);
346 		free(w->w_name);
347 		free((char *) w);
348 	}
349 	finis();
350 }
351 /*
352 **  ORDERQ -- order the work queue.
353 **
354 **	Parameters:
355 **		doall -- if set, include everything in the queue (even
356 **			the jobs that cannot be run because the load
357 **			average is too high).  Otherwise, exclude those
358 **			jobs.
359 **
360 **	Returns:
361 **		The number of request in the queue (not necessarily
362 **		the number of requests in WorkQ however).
363 **
364 **	Side Effects:
365 **		Sets WorkQ to the queue of available work, in order.
366 */
367 
368 # define NEED_P		001
369 # define NEED_T		002
370 
371 orderq(doall)
372 	bool doall;
373 {
374 	register struct direct *d;
375 	register WORK *w;
376 	DIR *f;
377 	register int i;
378 	WORK wlist[QUEUESIZE+1];
379 	int wn = -1;
380 	extern workcmpf();
381 
382 	/* clear out old WorkQ */
383 	for (w = WorkQ; w != NULL; )
384 	{
385 		register WORK *nw = w->w_next;
386 
387 		WorkQ = nw;
388 		free(w->w_name);
389 		free((char *) w);
390 		w = nw;
391 	}
392 
393 	/* open the queue directory */
394 	f = opendir(".");
395 	if (f == NULL)
396 	{
397 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
398 		return (0);
399 	}
400 
401 	/*
402 	**  Read the work directory.
403 	*/
404 
405 	while ((d = readdir(f)) != NULL)
406 	{
407 		FILE *cf;
408 		char lbuf[MAXNAME];
409 
410 		/* is this an interesting entry? */
411 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
412 			continue;
413 
414 		/* yes -- open control file (if not too many files) */
415 		if (++wn >= QUEUESIZE)
416 			continue;
417 		cf = fopen(d->d_name, "r");
418 		if (cf == NULL)
419 		{
420 			/* this may be some random person sending hir msgs */
421 			/* syserr("orderq: cannot open %s", cbuf); */
422 #ifdef DEBUG
423 			if (tTd(41, 2))
424 				printf("orderq: cannot open %s (%d)\n",
425 					d->d_name, errno);
426 #endif DEBUG
427 			errno = 0;
428 			wn--;
429 			continue;
430 		}
431 		w = &wlist[wn];
432 		w->w_name = newstr(d->d_name);
433 
434 		/* make sure jobs in creation don't clog queue */
435 		w->w_pri = 0x7fffffff;
436 		w->w_ctime = 0;
437 
438 		/* extract useful information */
439 		i = NEED_P | NEED_T;
440 		while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL)
441 		{
442 			extern long atol();
443 
444 			switch (lbuf[0])
445 			{
446 			  case 'P':
447 				w->w_pri = atol(&lbuf[1]);
448 				i &= ~NEED_P;
449 				break;
450 
451 			  case 'T':
452 				w->w_ctime = atol(&lbuf[1]);
453 				i &= ~NEED_T;
454 				break;
455 			}
456 		}
457 		(void) fclose(cf);
458 
459 		if (!doall && shouldqueue(w->w_pri))
460 		{
461 			/* don't even bother sorting this job in */
462 			wn--;
463 		}
464 	}
465 	(void) closedir(f);
466 	wn++;
467 
468 	/*
469 	**  Sort the work directory.
470 	*/
471 
472 	qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf);
473 
474 	/*
475 	**  Convert the work list into canonical form.
476 	**	Should be turning it into a list of envelopes here perhaps.
477 	*/
478 
479 	WorkQ = NULL;
480 	for (i = min(wn, QUEUESIZE); --i >= 0; )
481 	{
482 		w = (WORK *) xalloc(sizeof *w);
483 		w->w_name = wlist[i].w_name;
484 		w->w_pri = wlist[i].w_pri;
485 		w->w_ctime = wlist[i].w_ctime;
486 		w->w_next = WorkQ;
487 		WorkQ = w;
488 	}
489 
490 # ifdef DEBUG
491 	if (tTd(40, 1))
492 	{
493 		for (w = WorkQ; w != NULL; w = w->w_next)
494 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
495 	}
496 # endif DEBUG
497 
498 	return (wn);
499 }
500 /*
501 **  WORKCMPF -- compare function for ordering work.
502 **
503 **	Parameters:
504 **		a -- the first argument.
505 **		b -- the second argument.
506 **
507 **	Returns:
508 **		-1 if a < b
509 **		 0 if a == b
510 **		+1 if a > b
511 **
512 **	Side Effects:
513 **		none.
514 */
515 
516 workcmpf(a, b)
517 	register WORK *a;
518 	register WORK *b;
519 {
520 	long pa = a->w_pri + a->w_ctime;
521 	long pb = b->w_pri + b->w_ctime;
522 
523 	if (pa == pb)
524 		return (0);
525 	else if (pa > pb)
526 		return (1);
527 	else
528 		return (-1);
529 }
530 /*
531 **  DOWORK -- do a work request.
532 **
533 **	Parameters:
534 **		w -- the work request to be satisfied.
535 **
536 **	Returns:
537 **		none.
538 **
539 **	Side Effects:
540 **		The work request is satisfied if possible.
541 */
542 
543 dowork(w)
544 	register WORK *w;
545 {
546 	register int i;
547 	extern bool shouldqueue();
548 
549 # ifdef DEBUG
550 	if (tTd(40, 1))
551 		printf("dowork: %s pri %ld\n", w->w_name, w->w_pri);
552 # endif DEBUG
553 
554 	/*
555 	**  Ignore jobs that are too expensive for the moment.
556 	*/
557 
558 	if (shouldqueue(w->w_pri))
559 	{
560 		if (Verbose)
561 			printf("\nSkipping %s\n", w->w_name + 2);
562 		return;
563 	}
564 
565 	/*
566 	**  Fork for work.
567 	*/
568 
569 	if (ForkQueueRuns)
570 	{
571 		i = fork();
572 		if (i < 0)
573 		{
574 			syserr("dowork: cannot fork");
575 			return;
576 		}
577 	}
578 	else
579 	{
580 		i = 0;
581 	}
582 
583 	if (i == 0)
584 	{
585 		/*
586 		**  CHILD
587 		**	Lock the control file to avoid duplicate deliveries.
588 		**		Then run the file as though we had just read it.
589 		**	We save an idea of the temporary name so we
590 		**		can recover on interrupt.
591 		*/
592 
593 		/* set basic modes, etc. */
594 		(void) alarm(0);
595 		clearenvelope(CurEnv, FALSE);
596 		QueueRun = TRUE;
597 		ErrorMode = EM_MAIL;
598 		CurEnv->e_id = &w->w_name[2];
599 # ifdef LOG
600 		if (LogLevel > 11)
601 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", CurEnv->e_id,
602 			       getpid());
603 # endif LOG
604 
605 		/* don't use the headers from sendmail.cf... */
606 		CurEnv->e_header = NULL;
607 
608 		/* lock the control file during processing */
609 		if (link(w->w_name, queuename(CurEnv, 'l')) < 0)
610 		{
611 			/* being processed by another queuer */
612 # ifdef LOG
613 			if (LogLevel > 4)
614 				syslog(LOG_DEBUG, "%s: locked", CurEnv->e_id);
615 # endif LOG
616 			if (ForkQueueRuns)
617 				exit(EX_OK);
618 			else
619 				return;
620 		}
621 
622 		/* do basic system initialization */
623 		initsys();
624 
625 		/* read the queue control file */
626 		readqf(CurEnv, TRUE);
627 		CurEnv->e_flags |= EF_INQUEUE;
628 		eatheader(CurEnv);
629 
630 		/* do the delivery */
631 		if (!bitset(EF_FATALERRS, CurEnv->e_flags))
632 			sendall(CurEnv, SM_DELIVER);
633 
634 		/* finish up and exit */
635 		if (ForkQueueRuns)
636 			finis();
637 		else
638 			dropenvelope(CurEnv);
639 	}
640 	else
641 	{
642 		/*
643 		**  Parent -- pick up results.
644 		*/
645 
646 		errno = 0;
647 		(void) waitfor(i);
648 	}
649 }
650 /*
651 **  READQF -- read queue file and set up environment.
652 **
653 **	Parameters:
654 **		e -- the envelope of the job to run.
655 **		full -- if set, read in all information.  Otherwise just
656 **			read in info needed for a queue print.
657 **
658 **	Returns:
659 **		none.
660 **
661 **	Side Effects:
662 **		cf is read and created as the current job, as though
663 **		we had been invoked by argument.
664 */
665 
666 readqf(e, full)
667 	register ENVELOPE *e;
668 	bool full;
669 {
670 	char *qf;
671 	register FILE *qfp;
672 	char buf[MAXFIELD];
673 	extern char *fgetfolded();
674 	extern long atol();
675 
676 	/*
677 	**  Read and process the file.
678 	*/
679 
680 	qf = queuename(e, 'q');
681 	qfp = fopen(qf, "r");
682 	if (qfp == NULL)
683 	{
684 		syserr("readqf: no control file %s", qf);
685 		return;
686 	}
687 	FileName = qf;
688 	LineNumber = 0;
689 	if (Verbose && full)
690 		printf("\nRunning %s\n", e->e_id);
691 	while (fgetfolded(buf, sizeof buf, qfp) != NULL)
692 	{
693 # ifdef DEBUG
694 		if (tTd(40, 4))
695 			printf("+++++ %s\n", buf);
696 # endif DEBUG
697 		switch (buf[0])
698 		{
699 		  case 'R':		/* specify recipient */
700 			sendtolist(&buf[1], (ADDRESS *) NULL, &e->e_sendqueue);
701 			break;
702 
703 		  case 'E':		/* specify error recipient */
704 			sendtolist(&buf[1], (ADDRESS *) NULL, &e->e_errorqueue);
705 			break;
706 
707 		  case 'H':		/* header */
708 			if (full)
709 				(void) chompheader(&buf[1], FALSE);
710 			break;
711 
712 		  case 'M':		/* message */
713 			e->e_message = newstr(&buf[1]);
714 			break;
715 
716 		  case 'S':		/* sender */
717 			setsender(newstr(&buf[1]));
718 			break;
719 
720 		  case 'D':		/* data file name */
721 			if (!full)
722 				break;
723 			e->e_df = newstr(&buf[1]);
724 			e->e_dfp = fopen(e->e_df, "r");
725 			if (e->e_dfp == NULL)
726 				syserr("readqf: cannot open %s", e->e_df);
727 			break;
728 
729 		  case 'T':		/* init time */
730 			e->e_ctime = atol(&buf[1]);
731 			break;
732 
733 		  case 'P':		/* message priority */
734 			e->e_msgpriority = atol(&buf[1]) + WkTimeFact;
735 			break;
736 
737 		  case '\0':		/* blank line; ignore */
738 			break;
739 
740 		  default:
741 			syserr("readqf(%s:%d): bad line \"%s\"", e->e_id,
742 				LineNumber, buf);
743 			break;
744 		}
745 	}
746 
747 	(void) fclose(qfp);
748 	FileName = NULL;
749 
750 	/*
751 	**  If we haven't read any lines, this queue file is empty.
752 	**  Arrange to remove it without referencing any null pointers.
753 	*/
754 
755 	if (LineNumber == 0)
756 	{
757 		errno = 0;
758 		e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE;
759 	}
760 }
761 /*
762 **  PRINTQUEUE -- print out a representation of the mail queue
763 **
764 **	Parameters:
765 **		none.
766 **
767 **	Returns:
768 **		none.
769 **
770 **	Side Effects:
771 **		Prints a listing of the mail queue on the standard output.
772 */
773 
774 printqueue()
775 {
776 	register WORK *w;
777 	FILE *f;
778 	int nrequests;
779 	char buf[MAXLINE];
780 
781 	/*
782 	**  Read and order the queue.
783 	*/
784 
785 	nrequests = orderq(TRUE);
786 
787 	/*
788 	**  Print the work list that we have read.
789 	*/
790 
791 	/* first see if there is anything */
792 	if (nrequests <= 0)
793 	{
794 		printf("Mail queue is empty\n");
795 		return;
796 	}
797 
798 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
799 	if (nrequests > QUEUESIZE)
800 		printf(", only %d printed", QUEUESIZE);
801 	if (Verbose)
802 		printf(")\n--QID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n");
803 	else
804 		printf(")\n--QID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
805 	for (w = WorkQ; w != NULL; w = w->w_next)
806 	{
807 		struct stat st;
808 		auto time_t submittime = 0;
809 		long dfsize = -1;
810 		char lf[20];
811 		char message[MAXLINE];
812 		extern bool shouldqueue();
813 
814 		f = fopen(w->w_name, "r");
815 		if (f == NULL)
816 		{
817 			errno = 0;
818 			continue;
819 		}
820 		printf("%7s", w->w_name + 2);
821 		(void) strcpy(lf, w->w_name);
822 		lf[0] = 'l';
823 		if (stat(lf, &st) >= 0)
824 			printf("*");
825 		else if (shouldqueue(w->w_pri))
826 			printf("X");
827 		else
828 			printf(" ");
829 		errno = 0;
830 
831 		message[0] = '\0';
832 		while (fgets(buf, sizeof buf, f) != NULL)
833 		{
834 			fixcrlf(buf, TRUE);
835 			switch (buf[0])
836 			{
837 			  case 'M':	/* error message */
838 				(void) strcpy(message, &buf[1]);
839 				break;
840 
841 			  case 'S':	/* sender name */
842 				if (Verbose)
843 					printf("%8ld %10ld %.12s %.38s", dfsize,
844 					    w->w_pri, ctime(&submittime) + 4,
845 					    &buf[1]);
846 				else
847 					printf("%8ld %.16s %.45s", dfsize,
848 					    ctime(&submittime), &buf[1]);
849 				if (message[0] != '\0')
850 					printf("\n\t\t (%.60s)", message);
851 				break;
852 
853 			  case 'R':	/* recipient name */
854 				if (Verbose)
855 					printf("\n\t\t\t\t\t %.38s", &buf[1]);
856 				else
857 					printf("\n\t\t\t\t  %.45s", &buf[1]);
858 				break;
859 
860 			  case 'T':	/* creation time */
861 				submittime = atol(&buf[1]);
862 				break;
863 
864 			  case 'D':	/* data file name */
865 				if (stat(&buf[1], &st) >= 0)
866 					dfsize = st.st_size;
867 				break;
868 			}
869 		}
870 		if (submittime == (time_t) 0)
871 			printf(" (no control file)");
872 		printf("\n");
873 		(void) fclose(f);
874 	}
875 }
876 
877 # endif QUEUE
878 /*
879 **  QUEUENAME -- build a file name in the queue directory for this envelope.
880 **
881 **	Assigns an id code if one does not already exist.
882 **	This code is very careful to avoid trashing existing files
883 **	under any circumstances.
884 **		We first create an nf file that is only used when
885 **		assigning an id.  This file is always empty, so that
886 **		we can never accidently truncate an lf file.
887 **
888 **	Parameters:
889 **		e -- envelope to build it in/from.
890 **		type -- the file type, used as the first character
891 **			of the file name.
892 **
893 **	Returns:
894 **		a pointer to the new file name (in a static buffer).
895 **
896 **	Side Effects:
897 **		Will create the lf and qf files if no id code is
898 **		already assigned.  This will cause the envelope
899 **		to be modified.
900 */
901 
902 char *
903 queuename(e, type)
904 	register ENVELOPE *e;
905 	char type;
906 {
907 	static char buf[MAXNAME];
908 	static int pid = -1;
909 	char c1 = 'A';
910 	char c2 = 'A';
911 
912 	if (e->e_id == NULL)
913 	{
914 		char qf[20];
915 		char nf[20];
916 		char lf[20];
917 
918 		/* find a unique id */
919 		if (pid != getpid())
920 		{
921 			/* new process -- start back at "AA" */
922 			pid = getpid();
923 			c1 = 'A';
924 			c2 = 'A' - 1;
925 		}
926 		(void) sprintf(qf, "qfAA%05d", pid);
927 		(void) strcpy(lf, qf);
928 		lf[0] = 'l';
929 		(void) strcpy(nf, qf);
930 		nf[0] = 'n';
931 
932 		while (c1 < '~' || c2 < 'Z')
933 		{
934 			int i;
935 
936 			if (c2 >= 'Z')
937 			{
938 				c1++;
939 				c2 = 'A' - 1;
940 			}
941 			lf[2] = nf[2] = qf[2] = c1;
942 			lf[3] = nf[3] = qf[3] = ++c2;
943 # ifdef DEBUG
944 			if (tTd(7, 20))
945 				printf("queuename: trying \"%s\"\n", nf);
946 # endif DEBUG
947 
948 # ifdef QUEUE
949 			if (access(lf, 0) >= 0 || access(qf, 0) >= 0)
950 				continue;
951 			errno = 0;
952 			i = creat(nf, FileMode);
953 			if (i < 0)
954 			{
955 				(void) unlink(nf);	/* kernel bug */
956 				continue;
957 			}
958 			(void) close(i);
959 			i = link(nf, lf);
960 			(void) unlink(nf);
961 			if (i < 0)
962 				continue;
963 			if (link(lf, qf) >= 0)
964 				break;
965 			(void) unlink(lf);
966 # else QUEUE
967 			if (close(creat(qf, FileMode)) >= 0)
968 				break;
969 # endif QUEUE
970 		}
971 		if (c1 >= '~' && c2 >= 'Z')
972 		{
973 			syserr("queuename: Cannot create \"%s\" in \"%s\"",
974 				qf, QueueDir);
975 			exit(EX_OSERR);
976 		}
977 		e->e_id = newstr(&qf[2]);
978 		define('i', e->e_id, e);
979 # ifdef DEBUG
980 		if (tTd(7, 1))
981 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
982 # ifdef LOG
983 		if (LogLevel > 16)
984 			syslog(LOG_DEBUG, "%s: assigned id", e->e_id);
985 # endif LOG
986 # endif DEBUG
987 	}
988 
989 	if (type == '\0')
990 		return (NULL);
991 	(void) sprintf(buf, "%cf%s", type, e->e_id);
992 # ifdef DEBUG
993 	if (tTd(7, 2))
994 		printf("queuename: %s\n", buf);
995 # endif DEBUG
996 	return (buf);
997 }
998 /*
999 **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
1000 **
1001 **	Parameters:
1002 **		e -- the envelope to unlock.
1003 **
1004 **	Returns:
1005 **		none
1006 **
1007 **	Side Effects:
1008 **		unlocks the queue for `e'.
1009 */
1010 
1011 unlockqueue(e)
1012 	ENVELOPE *e;
1013 {
1014 	/* remove the transcript */
1015 #ifdef DEBUG
1016 # ifdef LOG
1017 	if (LogLevel > 19)
1018 		syslog(LOG_DEBUG, "%s: unlock", e->e_id);
1019 # endif LOG
1020 	if (!tTd(51, 4))
1021 #endif DEBUG
1022 		xunlink(queuename(e, 'x'));
1023 
1024 # ifdef QUEUE
1025 	/* last but not least, remove the lock */
1026 	xunlink(queuename(e, 'l'));
1027 # endif QUEUE
1028 }
1029