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