xref: /csrg-svn/usr.sbin/sendmail/src/queue.c (revision 24954)
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.7 (Berkeley) 09/19/85	(no queueing)";
21 # endif not lint
22 # else QUEUE
23 
24 # ifndef lint
25 static char	SccsId[] = "@(#)queue.c	5.7 (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);
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 		closexscript(CurEnv);
574 		CurEnv->e_flags &= ~EF_FATALERRS;
575 		QueueRun = TRUE;
576 		ErrorMode = EM_MAIL;
577 		CurEnv->e_id = &w->w_name[2];
578 # ifdef LOG
579 		if (LogLevel > 11)
580 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", CurEnv->e_id,
581 			       getpid());
582 # endif LOG
583 
584 		/* don't use the headers from sendmail.cf... */
585 		CurEnv->e_header = NULL;
586 
587 		/* lock the control file during processing */
588 		if (link(w->w_name, queuename(CurEnv, 'l')) < 0)
589 		{
590 			/* being processed by another queuer */
591 # ifdef LOG
592 			if (LogLevel > 4)
593 				syslog(LOG_DEBUG, "%s: locked", CurEnv->e_id);
594 # endif LOG
595 			if (ForkQueueRuns)
596 				exit(EX_OK);
597 			else
598 				return;
599 		}
600 
601 		/* do basic system initialization */
602 		initsys();
603 
604 		/* read the queue control file */
605 		readqf(CurEnv, TRUE);
606 		CurEnv->e_flags |= EF_INQUEUE;
607 		eatheader(CurEnv);
608 
609 		/* do the delivery */
610 		if (!bitset(EF_FATALERRS, CurEnv->e_flags))
611 			sendall(CurEnv, SM_DELIVER);
612 
613 		/* finish up and exit */
614 		if (ForkQueueRuns)
615 			finis();
616 		else
617 			dropenvelope(CurEnv);
618 	}
619 	else
620 	{
621 		/*
622 		**  Parent -- pick up results.
623 		*/
624 
625 		errno = 0;
626 		(void) waitfor(i);
627 	}
628 }
629 /*
630 **  READQF -- read queue file and set up environment.
631 **
632 **	Parameters:
633 **		e -- the envelope of the job to run.
634 **		full -- if set, read in all information.  Otherwise just
635 **			read in info needed for a queue print.
636 **
637 **	Returns:
638 **		none.
639 **
640 **	Side Effects:
641 **		cf is read and created as the current job, as though
642 **		we had been invoked by argument.
643 */
644 
645 readqf(e, full)
646 	register ENVELOPE *e;
647 	bool full;
648 {
649 	char *qf;
650 	register FILE *qfp;
651 	char buf[MAXFIELD];
652 	extern char *fgetfolded();
653 	extern long atol();
654 
655 	/*
656 	**  Read and process the file.
657 	*/
658 
659 	qf = queuename(e, 'q');
660 	qfp = fopen(qf, "r");
661 	if (qfp == NULL)
662 	{
663 		syserr("readqf: no control file %s", qf);
664 		return;
665 	}
666 	FileName = qf;
667 	LineNumber = 0;
668 	if (Verbose && full)
669 		printf("\nRunning %s\n", e->e_id);
670 	while (fgetfolded(buf, sizeof buf, qfp) != NULL)
671 	{
672 		switch (buf[0])
673 		{
674 		  case 'R':		/* specify recipient */
675 			sendtolist(&buf[1], (ADDRESS *) NULL, &e->e_sendqueue);
676 			break;
677 
678 		  case 'H':		/* header */
679 			if (full)
680 				(void) chompheader(&buf[1], FALSE);
681 			break;
682 
683 		  case 'M':		/* message */
684 			e->e_message = newstr(&buf[1]);
685 			break;
686 
687 		  case 'S':		/* sender */
688 			setsender(newstr(&buf[1]));
689 			break;
690 
691 		  case 'D':		/* data file name */
692 			if (!full)
693 				break;
694 			e->e_df = newstr(&buf[1]);
695 			e->e_dfp = fopen(e->e_df, "r");
696 			if (e->e_dfp == NULL)
697 				syserr("readqf: cannot open %s", e->e_df);
698 			break;
699 
700 		  case 'T':		/* init time */
701 			e->e_ctime = atol(&buf[1]);
702 			break;
703 
704 		  case 'P':		/* message priority */
705 			e->e_msgpriority = atol(&buf[1]);
706 
707 			/* make sure that big things get sent eventually */
708 			e->e_msgpriority -= WKTIMEFACT;
709 			break;
710 
711 		  case '\0':		/* blank line; ignore */
712 			break;
713 
714 		  default:
715 			syserr("readqf(%s:%d): bad line \"%s\"", e->e_id,
716 				LineNumber, buf);
717 			break;
718 		}
719 	}
720 
721 	(void) fclose(qfp);
722 	FileName = NULL;
723 
724 	/*
725 	**  If we haven't read any lines, this queue file is empty.
726 	**  Arrange to remove it without referencing any null pointers.
727 	*/
728 
729 	if (LineNumber == 0)
730 	{
731 		errno = 0;
732 		e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE;
733 	}
734 }
735 /*
736 **  PRINTQUEUE -- print out a representation of the mail queue
737 **
738 **	Parameters:
739 **		none.
740 **
741 **	Returns:
742 **		none.
743 **
744 **	Side Effects:
745 **		Prints a listing of the mail queue on the standard output.
746 */
747 
748 printqueue()
749 {
750 	register WORK *w;
751 	FILE *f;
752 	int nrequests;
753 	char buf[MAXLINE];
754 
755 	/*
756 	**  Read and order the queue.
757 	*/
758 
759 	nrequests = orderq(TRUE);
760 
761 	/*
762 	**  Print the work list that we have read.
763 	*/
764 
765 	/* first see if there is anything */
766 	if (nrequests <= 0)
767 	{
768 		printf("Mail queue is empty\n");
769 		return;
770 	}
771 
772 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
773 	if (nrequests > WLSIZE)
774 		printf(", only %d printed", WLSIZE);
775 	printf(")\n--QID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
776 	for (w = WorkQ; w != NULL; w = w->w_next)
777 	{
778 		struct stat st;
779 		auto time_t submittime = 0;
780 		long dfsize = -1;
781 		char lf[20];
782 		char message[MAXLINE];
783 		extern bool shouldqueue();
784 
785 		f = fopen(w->w_name, "r");
786 		if (f == NULL)
787 		{
788 			errno = 0;
789 			continue;
790 		}
791 		printf("%7s", w->w_name + 2);
792 		(void) strcpy(lf, w->w_name);
793 		lf[0] = 'l';
794 		if (stat(lf, &st) >= 0)
795 			printf("*");
796 		else if (shouldqueue(w->w_pri))
797 			printf("X");
798 		else
799 			printf(" ");
800 		errno = 0;
801 
802 		message[0] = '\0';
803 		while (fgets(buf, sizeof buf, f) != NULL)
804 		{
805 			fixcrlf(buf, TRUE);
806 			switch (buf[0])
807 			{
808 			  case 'M':	/* error message */
809 				(void) strcpy(message, &buf[1]);
810 				break;
811 
812 			  case 'S':	/* sender name */
813 				printf("%8ld %.16s %.45s", dfsize,
814 					ctime(&submittime), &buf[1]);
815 				if (message[0] != '\0')
816 					printf("\n\t\t (%.62s)", message);
817 				break;
818 
819 			  case 'R':	/* recipient name */
820 				printf("\n\t\t\t\t  %.45s", &buf[1]);
821 				break;
822 
823 			  case 'T':	/* creation time */
824 				submittime = atol(&buf[1]);
825 				break;
826 
827 			  case 'D':	/* data file name */
828 				if (stat(&buf[1], &st) >= 0)
829 					dfsize = st.st_size;
830 				break;
831 			}
832 		}
833 		if (submittime == (time_t) 0)
834 			printf(" (no control file)");
835 		printf("\n");
836 		(void) fclose(f);
837 	}
838 }
839 
840 # endif QUEUE
841 /*
842 **  QUEUENAME -- build a file name in the queue directory for this envelope.
843 **
844 **	Assigns an id code if one does not already exist.
845 **	This code is very careful to avoid trashing existing files
846 **	under any circumstances.
847 **		We first create an nf file that is only used when
848 **		assigning an id.  This file is always empty, so that
849 **		we can never accidently truncate an lf file.
850 **
851 **	Parameters:
852 **		e -- envelope to build it in/from.
853 **		type -- the file type, used as the first character
854 **			of the file name.
855 **
856 **	Returns:
857 **		a pointer to the new file name (in a static buffer).
858 **
859 **	Side Effects:
860 **		Will create the lf and qf files if no id code is
861 **		already assigned.  This will cause the envelope
862 **		to be modified.
863 */
864 
865 char *
866 queuename(e, type)
867 	register ENVELOPE *e;
868 	char type;
869 {
870 	static char buf[MAXNAME];
871 	static int pid = -1;
872 	char c1 = 'A';
873 	char c2 = 'A';
874 
875 	if (e->e_id == NULL)
876 	{
877 		char qf[20];
878 		char nf[20];
879 		char lf[20];
880 
881 		/* find a unique id */
882 		if (pid != getpid())
883 		{
884 			/* new process -- start back at "AA" */
885 			pid = getpid();
886 			c1 = 'A';
887 			c2 = 'A' - 1;
888 		}
889 		(void) sprintf(qf, "qfAA%05d", pid);
890 		(void) strcpy(lf, qf);
891 		lf[0] = 'l';
892 		(void) strcpy(nf, qf);
893 		nf[0] = 'n';
894 
895 		while (c1 < '~' || c2 < 'Z')
896 		{
897 			int i;
898 
899 			if (c2 >= 'Z')
900 			{
901 				c1++;
902 				c2 = 'A' - 1;
903 			}
904 			lf[2] = nf[2] = qf[2] = c1;
905 			lf[3] = nf[3] = qf[3] = ++c2;
906 # ifdef DEBUG
907 			if (tTd(7, 20))
908 				printf("queuename: trying \"%s\"\n", nf);
909 # endif DEBUG
910 
911 # ifdef QUEUE
912 			if (access(lf, 0) >= 0 || access(qf, 0) >= 0)
913 				continue;
914 			errno = 0;
915 			i = creat(nf, FileMode);
916 			if (i < 0)
917 			{
918 				(void) unlink(nf);	/* kernel bug */
919 				continue;
920 			}
921 			(void) close(i);
922 			i = link(nf, lf);
923 			(void) unlink(nf);
924 			if (i < 0)
925 				continue;
926 			if (link(lf, qf) >= 0)
927 				break;
928 			(void) unlink(lf);
929 # else QUEUE
930 			if (close(creat(qf, FileMode)) >= 0)
931 				break;
932 # endif QUEUE
933 		}
934 		if (c1 >= '~' && c2 >= 'Z')
935 		{
936 			syserr("queuename: Cannot create \"%s\" in \"%s\"",
937 				qf, QueueDir);
938 			exit(EX_OSERR);
939 		}
940 		e->e_id = newstr(&qf[2]);
941 		define('i', e->e_id, e);
942 # ifdef DEBUG
943 		if (tTd(7, 1))
944 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
945 # ifdef LOG
946 		if (LogLevel > 16)
947 			syslog(LOG_DEBUG, "%s: assigned id", e->e_id);
948 # endif LOG
949 # endif DEBUG
950 	}
951 
952 	if (type == '\0')
953 		return (NULL);
954 	(void) sprintf(buf, "%cf%s", type, e->e_id);
955 # ifdef DEBUG
956 	if (tTd(7, 2))
957 		printf("queuename: %s\n", buf);
958 # endif DEBUG
959 	return (buf);
960 }
961 /*
962 **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
963 **
964 **	Parameters:
965 **		e -- the envelope to unlock.
966 **
967 **	Returns:
968 **		none
969 **
970 **	Side Effects:
971 **		unlocks the queue for `e'.
972 */
973 
974 unlockqueue(e)
975 	ENVELOPE *e;
976 {
977 	/* remove the transcript */
978 #ifdef DEBUG
979 # ifdef LOG
980 	if (LogLevel > 19)
981 		syslog(LOG_DEBUG, "%s: unlock", e->e_id);
982 # endif LOG
983 	if (!tTd(51, 4))
984 #endif DEBUG
985 		xunlink(queuename(e, 'x'));
986 
987 # ifdef QUEUE
988 	/* last but not least, remove the lock */
989 	xunlink(queuename(e, 'l'));
990 # endif QUEUE
991 }
992