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