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