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