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