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