xref: /original-bsd/usr.sbin/sendmail/src/queue.c (revision 57124d5e)
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.22 (Berkeley) 10/14/86	(no queueing)";
21 # endif not lint
22 # else QUEUE
23 
24 # ifndef lint
25 static char	SccsId[] = "@(#)queue.c	5.22 (Berkeley) 10/14/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 	**  Make sure the alias database is open.
331 	*/
332 
333 	initaliases(AliasFile, FALSE);
334 
335 	/*
336 	**  Start making passes through the queue.
337 	**	First, read and sort the entire queue.
338 	**	Then, process the work in that order.
339 	**		But if you take too long, start over.
340 	*/
341 
342 	/* order the existing work requests */
343 	(void) orderq(FALSE);
344 
345 	/* process them once at a time */
346 	while (WorkQ != NULL)
347 	{
348 		WORK *w = WorkQ;
349 
350 		WorkQ = WorkQ->w_next;
351 		dowork(w);
352 		free(w->w_name);
353 		free((char *) w);
354 	}
355 
356 	/* exit without the usual cleanup */
357 	exit(ExitStat);
358 }
359 /*
360 **  ORDERQ -- order the work queue.
361 **
362 **	Parameters:
363 **		doall -- if set, include everything in the queue (even
364 **			the jobs that cannot be run because the load
365 **			average is too high).  Otherwise, exclude those
366 **			jobs.
367 **
368 **	Returns:
369 **		The number of request in the queue (not necessarily
370 **		the number of requests in WorkQ however).
371 **
372 **	Side Effects:
373 **		Sets WorkQ to the queue of available work, in order.
374 */
375 
376 # define NEED_P		001
377 # define NEED_T		002
378 
379 orderq(doall)
380 	bool doall;
381 {
382 	register struct direct *d;
383 	register WORK *w;
384 	DIR *f;
385 	register int i;
386 	WORK wlist[QUEUESIZE+1];
387 	int wn = -1;
388 	extern workcmpf();
389 
390 	/* clear out old WorkQ */
391 	for (w = WorkQ; w != NULL; )
392 	{
393 		register WORK *nw = w->w_next;
394 
395 		WorkQ = nw;
396 		free(w->w_name);
397 		free((char *) w);
398 		w = nw;
399 	}
400 
401 	/* open the queue directory */
402 	f = opendir(".");
403 	if (f == NULL)
404 	{
405 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
406 		return (0);
407 	}
408 
409 	/*
410 	**  Read the work directory.
411 	*/
412 
413 	while ((d = readdir(f)) != NULL)
414 	{
415 		FILE *cf;
416 		char lbuf[MAXNAME];
417 
418 		/* is this an interesting entry? */
419 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
420 			continue;
421 
422 		/* yes -- open control file (if not too many files) */
423 		if (++wn >= QUEUESIZE)
424 			continue;
425 		cf = fopen(d->d_name, "r");
426 		if (cf == NULL)
427 		{
428 			/* this may be some random person sending hir msgs */
429 			/* syserr("orderq: cannot open %s", cbuf); */
430 #ifdef DEBUG
431 			if (tTd(41, 2))
432 				printf("orderq: cannot open %s (%d)\n",
433 					d->d_name, errno);
434 #endif DEBUG
435 			errno = 0;
436 			wn--;
437 			continue;
438 		}
439 		w = &wlist[wn];
440 		w->w_name = newstr(d->d_name);
441 
442 		/* make sure jobs in creation don't clog queue */
443 		w->w_pri = 0x7fffffff;
444 		w->w_ctime = 0;
445 
446 		/* extract useful information */
447 		i = NEED_P | NEED_T;
448 		while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL)
449 		{
450 			extern long atol();
451 
452 			switch (lbuf[0])
453 			{
454 			  case 'P':
455 				w->w_pri = atol(&lbuf[1]);
456 				i &= ~NEED_P;
457 				break;
458 
459 			  case 'T':
460 				w->w_ctime = atol(&lbuf[1]);
461 				i &= ~NEED_T;
462 				break;
463 			}
464 		}
465 		(void) fclose(cf);
466 
467 		if (!doall && shouldqueue(w->w_pri))
468 		{
469 			/* don't even bother sorting this job in */
470 			wn--;
471 		}
472 	}
473 	(void) closedir(f);
474 	wn++;
475 
476 	/*
477 	**  Sort the work directory.
478 	*/
479 
480 	qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf);
481 
482 	/*
483 	**  Convert the work list into canonical form.
484 	**	Should be turning it into a list of envelopes here perhaps.
485 	*/
486 
487 	WorkQ = NULL;
488 	for (i = min(wn, QUEUESIZE); --i >= 0; )
489 	{
490 		w = (WORK *) xalloc(sizeof *w);
491 		w->w_name = wlist[i].w_name;
492 		w->w_pri = wlist[i].w_pri;
493 		w->w_ctime = wlist[i].w_ctime;
494 		w->w_next = WorkQ;
495 		WorkQ = w;
496 	}
497 
498 # ifdef DEBUG
499 	if (tTd(40, 1))
500 	{
501 		for (w = WorkQ; w != NULL; w = w->w_next)
502 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
503 	}
504 # endif DEBUG
505 
506 	return (wn);
507 }
508 /*
509 **  WORKCMPF -- compare function for ordering work.
510 **
511 **	Parameters:
512 **		a -- the first argument.
513 **		b -- the second argument.
514 **
515 **	Returns:
516 **		-1 if a < b
517 **		 0 if a == b
518 **		+1 if a > b
519 **
520 **	Side Effects:
521 **		none.
522 */
523 
524 workcmpf(a, b)
525 	register WORK *a;
526 	register WORK *b;
527 {
528 	long pa = a->w_pri + a->w_ctime;
529 	long pb = b->w_pri + b->w_ctime;
530 
531 	if (pa == pb)
532 		return (0);
533 	else if (pa > pb)
534 		return (1);
535 	else
536 		return (-1);
537 }
538 /*
539 **  DOWORK -- do a work request.
540 **
541 **	Parameters:
542 **		w -- the work request to be satisfied.
543 **
544 **	Returns:
545 **		none.
546 **
547 **	Side Effects:
548 **		The work request is satisfied if possible.
549 */
550 
551 dowork(w)
552 	register WORK *w;
553 {
554 	register int i;
555 	extern bool shouldqueue();
556 
557 # ifdef DEBUG
558 	if (tTd(40, 1))
559 		printf("dowork: %s pri %ld\n", w->w_name, w->w_pri);
560 # endif DEBUG
561 
562 	/*
563 	**  Ignore jobs that are too expensive for the moment.
564 	*/
565 
566 	if (shouldqueue(w->w_pri))
567 	{
568 		if (Verbose)
569 			printf("\nSkipping %s\n", w->w_name + 2);
570 		return;
571 	}
572 
573 	/*
574 	**  Fork for work.
575 	*/
576 
577 	if (ForkQueueRuns)
578 	{
579 		i = fork();
580 		if (i < 0)
581 		{
582 			syserr("dowork: cannot fork");
583 			return;
584 		}
585 	}
586 	else
587 	{
588 		i = 0;
589 	}
590 
591 	if (i == 0)
592 	{
593 		/*
594 		**  CHILD
595 		**	Lock the control file to avoid duplicate deliveries.
596 		**		Then run the file as though we had just read it.
597 		**	We save an idea of the temporary name so we
598 		**		can recover on interrupt.
599 		*/
600 
601 		/* set basic modes, etc. */
602 		(void) alarm(0);
603 		clearenvelope(CurEnv, FALSE);
604 		QueueRun = TRUE;
605 		ErrorMode = EM_MAIL;
606 		CurEnv->e_id = &w->w_name[2];
607 # ifdef LOG
608 		if (LogLevel > 11)
609 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", CurEnv->e_id,
610 			       getpid());
611 # endif LOG
612 
613 		/* don't use the headers from sendmail.cf... */
614 		CurEnv->e_header = NULL;
615 
616 		/* lock the control file during processing */
617 		if (link(w->w_name, queuename(CurEnv, 'l')) < 0)
618 		{
619 			/* being processed by another queuer */
620 # ifdef LOG
621 			if (LogLevel > 4)
622 				syslog(LOG_DEBUG, "%s: locked", CurEnv->e_id);
623 # endif LOG
624 			if (ForkQueueRuns)
625 				exit(EX_OK);
626 			else
627 				return;
628 		}
629 
630 		/* do basic system initialization */
631 		initsys();
632 
633 		/* read the queue control file */
634 		readqf(CurEnv, TRUE);
635 		CurEnv->e_flags |= EF_INQUEUE;
636 		eatheader(CurEnv);
637 
638 		/* do the delivery */
639 		if (!bitset(EF_FATALERRS, CurEnv->e_flags))
640 			sendall(CurEnv, SM_DELIVER);
641 
642 		/* finish up and exit */
643 		if (ForkQueueRuns)
644 			finis();
645 		else
646 			dropenvelope(CurEnv);
647 	}
648 	else
649 	{
650 		/*
651 		**  Parent -- pick up results.
652 		*/
653 
654 		errno = 0;
655 		(void) waitfor(i);
656 	}
657 }
658 /*
659 **  READQF -- read queue file and set up environment.
660 **
661 **	Parameters:
662 **		e -- the envelope of the job to run.
663 **		full -- if set, read in all information.  Otherwise just
664 **			read in info needed for a queue print.
665 **
666 **	Returns:
667 **		none.
668 **
669 **	Side Effects:
670 **		cf is read and created as the current job, as though
671 **		we had been invoked by argument.
672 */
673 
674 readqf(e, full)
675 	register ENVELOPE *e;
676 	bool full;
677 {
678 	char *qf;
679 	register FILE *qfp;
680 	char buf[MAXFIELD];
681 	extern char *fgetfolded();
682 	extern long atol();
683 
684 	/*
685 	**  Read and process the file.
686 	*/
687 
688 	qf = queuename(e, 'q');
689 	qfp = fopen(qf, "r");
690 	if (qfp == NULL)
691 	{
692 		syserr("readqf: no control file %s", qf);
693 		return;
694 	}
695 	FileName = qf;
696 	LineNumber = 0;
697 	if (Verbose && full)
698 		printf("\nRunning %s\n", e->e_id);
699 	while (fgetfolded(buf, sizeof buf, qfp) != NULL)
700 	{
701 # ifdef DEBUG
702 		if (tTd(40, 4))
703 			printf("+++++ %s\n", buf);
704 # endif DEBUG
705 		switch (buf[0])
706 		{
707 		  case 'R':		/* specify recipient */
708 			sendtolist(&buf[1], (ADDRESS *) NULL, &e->e_sendqueue);
709 			break;
710 
711 		  case 'E':		/* specify error recipient */
712 			sendtolist(&buf[1], (ADDRESS *) NULL, &e->e_errorqueue);
713 			break;
714 
715 		  case 'H':		/* header */
716 			if (full)
717 				(void) chompheader(&buf[1], FALSE);
718 			break;
719 
720 		  case 'M':		/* message */
721 			e->e_message = newstr(&buf[1]);
722 			break;
723 
724 		  case 'S':		/* sender */
725 			setsender(newstr(&buf[1]));
726 			break;
727 
728 		  case 'D':		/* data file name */
729 			if (!full)
730 				break;
731 			e->e_df = newstr(&buf[1]);
732 			e->e_dfp = fopen(e->e_df, "r");
733 			if (e->e_dfp == NULL)
734 				syserr("readqf: cannot open %s", e->e_df);
735 			break;
736 
737 		  case 'T':		/* init time */
738 			e->e_ctime = atol(&buf[1]);
739 			break;
740 
741 		  case 'P':		/* message priority */
742 			e->e_msgpriority = atol(&buf[1]) + WkTimeFact;
743 			break;
744 
745 		  case '\0':		/* blank line; ignore */
746 			break;
747 
748 		  default:
749 			syserr("readqf(%s:%d): bad line \"%s\"", e->e_id,
750 				LineNumber, buf);
751 			break;
752 		}
753 	}
754 
755 	(void) fclose(qfp);
756 	FileName = NULL;
757 
758 	/*
759 	**  If we haven't read any lines, this queue file is empty.
760 	**  Arrange to remove it without referencing any null pointers.
761 	*/
762 
763 	if (LineNumber == 0)
764 	{
765 		errno = 0;
766 		e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE;
767 	}
768 }
769 /*
770 **  PRINTQUEUE -- print out a representation of the mail queue
771 **
772 **	Parameters:
773 **		none.
774 **
775 **	Returns:
776 **		none.
777 **
778 **	Side Effects:
779 **		Prints a listing of the mail queue on the standard output.
780 */
781 
782 printqueue()
783 {
784 	register WORK *w;
785 	FILE *f;
786 	int nrequests;
787 	char buf[MAXLINE];
788 
789 	/*
790 	**  Read and order the queue.
791 	*/
792 
793 	nrequests = orderq(TRUE);
794 
795 	/*
796 	**  Print the work list that we have read.
797 	*/
798 
799 	/* first see if there is anything */
800 	if (nrequests <= 0)
801 	{
802 		printf("Mail queue is empty\n");
803 		return;
804 	}
805 
806 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
807 	if (nrequests > QUEUESIZE)
808 		printf(", only %d printed", QUEUESIZE);
809 	if (Verbose)
810 		printf(")\n--QID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n");
811 	else
812 		printf(")\n--QID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
813 	for (w = WorkQ; w != NULL; w = w->w_next)
814 	{
815 		struct stat st;
816 		auto time_t submittime = 0;
817 		long dfsize = -1;
818 		char lf[20];
819 		char message[MAXLINE];
820 		extern bool shouldqueue();
821 
822 		f = fopen(w->w_name, "r");
823 		if (f == NULL)
824 		{
825 			errno = 0;
826 			continue;
827 		}
828 		printf("%7s", w->w_name + 2);
829 		(void) strcpy(lf, w->w_name);
830 		lf[0] = 'l';
831 		if (stat(lf, &st) >= 0)
832 			printf("*");
833 		else if (shouldqueue(w->w_pri))
834 			printf("X");
835 		else
836 			printf(" ");
837 		errno = 0;
838 
839 		message[0] = '\0';
840 		while (fgets(buf, sizeof buf, f) != NULL)
841 		{
842 			fixcrlf(buf, TRUE);
843 			switch (buf[0])
844 			{
845 			  case 'M':	/* error message */
846 				(void) strcpy(message, &buf[1]);
847 				break;
848 
849 			  case 'S':	/* sender name */
850 				if (Verbose)
851 					printf("%8ld %10ld %.12s %.38s", dfsize,
852 					    w->w_pri, ctime(&submittime) + 4,
853 					    &buf[1]);
854 				else
855 					printf("%8ld %.16s %.45s", dfsize,
856 					    ctime(&submittime), &buf[1]);
857 				if (message[0] != '\0')
858 					printf("\n\t\t (%.60s)", message);
859 				break;
860 
861 			  case 'R':	/* recipient name */
862 				if (Verbose)
863 					printf("\n\t\t\t\t\t %.38s", &buf[1]);
864 				else
865 					printf("\n\t\t\t\t  %.45s", &buf[1]);
866 				break;
867 
868 			  case 'T':	/* creation time */
869 				submittime = atol(&buf[1]);
870 				break;
871 
872 			  case 'D':	/* data file name */
873 				if (stat(&buf[1], &st) >= 0)
874 					dfsize = st.st_size;
875 				break;
876 			}
877 		}
878 		if (submittime == (time_t) 0)
879 			printf(" (no control file)");
880 		printf("\n");
881 		(void) fclose(f);
882 	}
883 }
884 
885 # endif QUEUE
886 /*
887 **  QUEUENAME -- build a file name in the queue directory for this envelope.
888 **
889 **	Assigns an id code if one does not already exist.
890 **	This code is very careful to avoid trashing existing files
891 **	under any circumstances.
892 **		We first create an nf file that is only used when
893 **		assigning an id.  This file is always empty, so that
894 **		we can never accidently truncate an lf file.
895 **
896 **	Parameters:
897 **		e -- envelope to build it in/from.
898 **		type -- the file type, used as the first character
899 **			of the file name.
900 **
901 **	Returns:
902 **		a pointer to the new file name (in a static buffer).
903 **
904 **	Side Effects:
905 **		Will create the lf and qf files if no id code is
906 **		already assigned.  This will cause the envelope
907 **		to be modified.
908 */
909 
910 char *
911 queuename(e, type)
912 	register ENVELOPE *e;
913 	char type;
914 {
915 	static char buf[MAXNAME];
916 	static int pid = -1;
917 	char c1 = 'A';
918 	char c2 = 'A';
919 
920 	if (e->e_id == NULL)
921 	{
922 		char qf[20];
923 		char nf[20];
924 		char lf[20];
925 
926 		/* find a unique id */
927 		if (pid != getpid())
928 		{
929 			/* new process -- start back at "AA" */
930 			pid = getpid();
931 			c1 = 'A';
932 			c2 = 'A' - 1;
933 		}
934 		(void) sprintf(qf, "qfAA%05d", pid);
935 		(void) strcpy(lf, qf);
936 		lf[0] = 'l';
937 		(void) strcpy(nf, qf);
938 		nf[0] = 'n';
939 
940 		while (c1 < '~' || c2 < 'Z')
941 		{
942 			int i;
943 
944 			if (c2 >= 'Z')
945 			{
946 				c1++;
947 				c2 = 'A' - 1;
948 			}
949 			lf[2] = nf[2] = qf[2] = c1;
950 			lf[3] = nf[3] = qf[3] = ++c2;
951 # ifdef DEBUG
952 			if (tTd(7, 20))
953 				printf("queuename: trying \"%s\"\n", nf);
954 # endif DEBUG
955 
956 # ifdef QUEUE
957 			if (access(lf, 0) >= 0 || access(qf, 0) >= 0)
958 				continue;
959 			errno = 0;
960 			i = creat(nf, FileMode);
961 			if (i < 0)
962 			{
963 				(void) unlink(nf);	/* kernel bug */
964 				continue;
965 			}
966 			(void) close(i);
967 			i = link(nf, lf);
968 			(void) unlink(nf);
969 			if (i < 0)
970 				continue;
971 			if (link(lf, qf) >= 0)
972 				break;
973 			(void) unlink(lf);
974 # else QUEUE
975 			if (close(creat(qf, FileMode)) >= 0)
976 				break;
977 # endif QUEUE
978 		}
979 		if (c1 >= '~' && c2 >= 'Z')
980 		{
981 			syserr("queuename: Cannot create \"%s\" in \"%s\"",
982 				qf, QueueDir);
983 			exit(EX_OSERR);
984 		}
985 		e->e_id = newstr(&qf[2]);
986 		define('i', e->e_id, e);
987 # ifdef DEBUG
988 		if (tTd(7, 1))
989 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
990 # ifdef LOG
991 		if (LogLevel > 16)
992 			syslog(LOG_DEBUG, "%s: assigned id", e->e_id);
993 # endif LOG
994 # endif DEBUG
995 	}
996 
997 	if (type == '\0')
998 		return (NULL);
999 	(void) sprintf(buf, "%cf%s", type, e->e_id);
1000 # ifdef DEBUG
1001 	if (tTd(7, 2))
1002 		printf("queuename: %s\n", buf);
1003 # endif DEBUG
1004 	return (buf);
1005 }
1006 /*
1007 **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
1008 **
1009 **	Parameters:
1010 **		e -- the envelope to unlock.
1011 **
1012 **	Returns:
1013 **		none
1014 **
1015 **	Side Effects:
1016 **		unlocks the queue for `e'.
1017 */
1018 
1019 unlockqueue(e)
1020 	ENVELOPE *e;
1021 {
1022 	/* remove the transcript */
1023 #ifdef DEBUG
1024 # ifdef LOG
1025 	if (LogLevel > 19)
1026 		syslog(LOG_DEBUG, "%s: unlock", e->e_id);
1027 # endif LOG
1028 	if (!tTd(51, 4))
1029 #endif DEBUG
1030 		xunlink(queuename(e, 'x'));
1031 
1032 # ifdef QUEUE
1033 	/* last but not least, remove the lock */
1034 	xunlink(queuename(e, 'l'));
1035 # endif QUEUE
1036 }
1037