xref: /csrg-svn/usr.sbin/sendmail/src/queue.c (revision 24953)
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.6 (Berkeley) 09/19/85	(no queueing)";
21 # endif not lint
22 # else QUEUE
23 
24 # ifndef lint
25 static char	SccsId[] = "@(#)queue.c	5.6 (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();
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 			switch (lbuf[0])
422 			{
423 			  case 'P':
424 				wlist[wn].w_pri = atol(&lbuf[1]);
425 				break;
426 
427 			  case 'T':
428 				wlist[wn].w_ctime = atol(&lbuf[1]);
429 				break;
430 			}
431 		}
432 		(void) fclose(cf);
433 
434 		if (!doall && shouldqueue(wlist[wn].w_pri))
435 		{
436 			/* don't even bother sorting this job in */
437 			wn--;
438 		}
439 	}
440 	(void) closedir(f);
441 	wn++;
442 
443 	/*
444 	**  Sort the work directory.
445 	*/
446 
447 	qsort((char *) wlist, min(wn, WLSIZE), sizeof *wlist, workcmpf);
448 
449 	/*
450 	**  Convert the work list into canonical form.
451 	**	Should be turning it into a list of envelopes here perhaps.
452 	*/
453 
454 	wp = &WorkQ;
455 	for (i = min(wn, WLSIZE); --i >= 0; )
456 	{
457 		w = (WORK *) xalloc(sizeof *w);
458 		w->w_name = wlist[i].w_name;
459 		w->w_pri = wlist[i].w_pri;
460 		w->w_ctime = wlist[i].w_ctime;
461 		w->w_next = NULL;
462 		*wp = w;
463 		wp = &w->w_next;
464 	}
465 
466 # ifdef DEBUG
467 	if (tTd(40, 1))
468 	{
469 		for (w = WorkQ; w != NULL; w = w->w_next)
470 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
471 	}
472 # endif DEBUG
473 
474 	return (wn);
475 }
476 /*
477 **  WORKCMPF -- compare function for ordering work.
478 **
479 **	Parameters:
480 **		a -- the first argument.
481 **		b -- the second argument.
482 **
483 **	Returns:
484 **		1 if a < b
485 **		0 if a == b
486 **		-1 if a > b
487 **
488 **	Side Effects:
489 **		none.
490 */
491 
492 workcmpf(a, b)
493 	register WORK *a;
494 	register WORK *b;
495 {
496 	long pa = a->w_pri + a->w_ctime;
497 	long pb = b->w_pri + b->w_ctime;
498 
499 	if (pa == pb)
500 		return (0);
501 	else if (pa > pb)
502 		return (-1);
503 	else
504 		return (1);
505 }
506 /*
507 **  DOWORK -- do a work request.
508 **
509 **	Parameters:
510 **		w -- the work request to be satisfied.
511 **
512 **	Returns:
513 **		none.
514 **
515 **	Side Effects:
516 **		The work request is satisfied if possible.
517 */
518 
519 dowork(w)
520 	register WORK *w;
521 {
522 	register int i;
523 	extern bool shouldqueue();
524 
525 # ifdef DEBUG
526 	if (tTd(40, 1))
527 		printf("dowork: %s pri %ld\n", w->w_name, w->w_pri);
528 # endif DEBUG
529 
530 	/*
531 	**  Ignore jobs that are too expensive for the moment.
532 	*/
533 
534 	if (shouldqueue(w->w_pri))
535 	{
536 		if (Verbose)
537 			printf("\nSkipping %s\n", w->w_name);
538 		return;
539 	}
540 
541 	/*
542 	**  Fork for work.
543 	*/
544 
545 	if (ForkQueueRuns)
546 	{
547 		i = fork();
548 		if (i < 0)
549 		{
550 			syserr("dowork: cannot fork");
551 			return;
552 		}
553 	}
554 	else
555 	{
556 		i = 0;
557 	}
558 
559 	if (i == 0)
560 	{
561 		/*
562 		**  CHILD
563 		**	Lock the control file to avoid duplicate deliveries.
564 		**		Then run the file as though we had just read it.
565 		**	We save an idea of the temporary name so we
566 		**		can recover on interrupt.
567 		*/
568 
569 		/* set basic modes, etc. */
570 		(void) alarm(0);
571 		closexscript(CurEnv);
572 		CurEnv->e_flags &= ~EF_FATALERRS;
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 
652 	/*
653 	**  Read and process the file.
654 	*/
655 
656 	qf = queuename(e, 'q');
657 	qfp = fopen(qf, "r");
658 	if (qfp == NULL)
659 	{
660 		syserr("readqf: no control file %s", qf);
661 		return;
662 	}
663 	FileName = qf;
664 	LineNumber = 0;
665 	if (Verbose && full)
666 		printf("\nRunning %s\n", e->e_id);
667 	while (fgetfolded(buf, sizeof buf, qfp) != NULL)
668 	{
669 		switch (buf[0])
670 		{
671 		  case 'R':		/* specify recipient */
672 			sendtolist(&buf[1], (ADDRESS *) NULL, &e->e_sendqueue);
673 			break;
674 
675 		  case 'H':		/* header */
676 			if (full)
677 				(void) chompheader(&buf[1], FALSE);
678 			break;
679 
680 		  case 'M':		/* message */
681 			e->e_message = newstr(&buf[1]);
682 			break;
683 
684 		  case 'S':		/* sender */
685 			setsender(newstr(&buf[1]));
686 			break;
687 
688 		  case 'D':		/* data file name */
689 			if (!full)
690 				break;
691 			e->e_df = newstr(&buf[1]);
692 			e->e_dfp = fopen(e->e_df, "r");
693 			if (e->e_dfp == NULL)
694 				syserr("readqf: cannot open %s", e->e_df);
695 			break;
696 
697 		  case 'T':		/* init time */
698 			e->e_ctime = atol(&buf[1]);
699 			break;
700 
701 		  case 'P':		/* message priority */
702 			e->e_msgpriority = atol(&buf[1]);
703 
704 			/* make sure that big things get sent eventually */
705 			e->e_msgpriority -= WKTIMEFACT;
706 			break;
707 
708 		  case '\0':		/* blank line; ignore */
709 			break;
710 
711 		  default:
712 			syserr("readqf(%s:%d): bad line \"%s\"", e->e_id,
713 				LineNumber, buf);
714 			break;
715 		}
716 	}
717 
718 	fclose(qfp);
719 	FileName = NULL;
720 
721 	/*
722 	**  If we haven't read any lines, this queue file is empty.
723 	**  Arrange to remove it without referencing any null pointers.
724 	*/
725 
726 	if (LineNumber == 0)
727 	{
728 		errno = 0;
729 		e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE;
730 	}
731 }
732 /*
733 **  PRINTQUEUE -- print out a representation of the mail queue
734 **
735 **	Parameters:
736 **		none.
737 **
738 **	Returns:
739 **		none.
740 **
741 **	Side Effects:
742 **		Prints a listing of the mail queue on the standard output.
743 */
744 
745 printqueue()
746 {
747 	register WORK *w;
748 	FILE *f;
749 	int nrequests;
750 	char buf[MAXLINE];
751 
752 	/*
753 	**  Read and order the queue.
754 	*/
755 
756 	nrequests = orderq(TRUE);
757 
758 	/*
759 	**  Print the work list that we have read.
760 	*/
761 
762 	/* first see if there is anything */
763 	if (nrequests <= 0)
764 	{
765 		printf("Mail queue is empty\n");
766 		return;
767 	}
768 
769 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
770 	if (nrequests > WLSIZE)
771 		printf(", only %d printed", WLSIZE);
772 	printf(")\n--QID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
773 	for (w = WorkQ; w != NULL; w = w->w_next)
774 	{
775 		struct stat st;
776 		auto time_t submittime = 0;
777 		long dfsize = -1;
778 		char lf[20];
779 		char message[MAXLINE];
780 		extern bool shouldqueue();
781 
782 		f = fopen(w->w_name, "r");
783 		if (f == NULL)
784 		{
785 			errno = 0;
786 			continue;
787 		}
788 		printf("%7s", w->w_name + 2);
789 		(void) strcpy(lf, w->w_name);
790 		lf[0] = 'l';
791 		if (stat(lf, &st) >= 0)
792 			printf("*");
793 		else if (shouldqueue(w->w_pri))
794 			printf("X");
795 		else
796 			printf(" ");
797 		errno = 0;
798 
799 		message[0] = '\0';
800 		while (fgets(buf, sizeof buf, f) != NULL)
801 		{
802 			fixcrlf(buf, TRUE);
803 			switch (buf[0])
804 			{
805 			  case 'M':	/* error message */
806 				(void) strcpy(message, &buf[1]);
807 				break;
808 
809 			  case 'S':	/* sender name */
810 				printf("%8ld %.16s %.45s", dfsize,
811 					ctime(&submittime), &buf[1]);
812 				if (message[0] != '\0')
813 					printf("\n\t\t (%.62s)", message);
814 				break;
815 
816 			  case 'R':	/* recipient name */
817 				printf("\n\t\t\t\t  %.45s", &buf[1]);
818 				break;
819 
820 			  case 'T':	/* creation time */
821 				submittime = atol(&buf[1]);
822 				break;
823 
824 			  case 'D':	/* data file name */
825 				if (stat(&buf[1], &st) >= 0)
826 					dfsize = st.st_size;
827 				break;
828 			}
829 		}
830 		if (submittime == (time_t) 0)
831 			printf(" (no control file)");
832 		printf("\n");
833 		(void) fclose(f);
834 	}
835 }
836 
837 # endif QUEUE
838 /*
839 **  QUEUENAME -- build a file name in the queue directory for this envelope.
840 **
841 **	Assigns an id code if one does not already exist.
842 **	This code is very careful to avoid trashing existing files
843 **	under any circumstances.
844 **		We first create an nf file that is only used when
845 **		assigning an id.  This file is always empty, so that
846 **		we can never accidently truncate an lf file.
847 **
848 **	Parameters:
849 **		e -- envelope to build it in/from.
850 **		type -- the file type, used as the first character
851 **			of the file name.
852 **
853 **	Returns:
854 **		a pointer to the new file name (in a static buffer).
855 **
856 **	Side Effects:
857 **		Will create the lf and qf files if no id code is
858 **		already assigned.  This will cause the envelope
859 **		to be modified.
860 */
861 
862 char *
863 queuename(e, type)
864 	register ENVELOPE *e;
865 	char type;
866 {
867 	static char buf[MAXNAME];
868 	static int pid = -1;
869 	char c1 = 'A';
870 	char c2 = 'A';
871 
872 	if (e->e_id == NULL)
873 	{
874 		char qf[20];
875 		char nf[20];
876 		char lf[20];
877 
878 		/* find a unique id */
879 		if (pid != getpid())
880 		{
881 			/* new process -- start back at "AA" */
882 			pid = getpid();
883 			c1 = 'A';
884 			c2 = 'A' - 1;
885 		}
886 		(void) sprintf(qf, "qfAA%05d", pid);
887 		(void) strcpy(lf, qf);
888 		lf[0] = 'l';
889 		(void) strcpy(nf, qf);
890 		nf[0] = 'n';
891 
892 		while (c1 < '~' || c2 < 'Z')
893 		{
894 			int i;
895 
896 			if (c2 >= 'Z')
897 			{
898 				c1++;
899 				c2 = 'A' - 1;
900 			}
901 			lf[2] = nf[2] = qf[2] = c1;
902 			lf[3] = nf[3] = qf[3] = ++c2;
903 # ifdef DEBUG
904 			if (tTd(7, 20))
905 				printf("queuename: trying \"%s\"\n", nf);
906 # endif DEBUG
907 
908 # ifdef QUEUE
909 			if (access(lf, 0) >= 0 || access(qf, 0) >= 0)
910 				continue;
911 			errno = 0;
912 			i = creat(nf, FileMode);
913 			if (i < 0)
914 			{
915 				(void) unlink(nf);	/* kernel bug */
916 				continue;
917 			}
918 			(void) close(i);
919 			i = link(nf, lf);
920 			(void) unlink(nf);
921 			if (i < 0)
922 				continue;
923 			if (link(lf, qf) >= 0)
924 				break;
925 			(void) unlink(lf);
926 # else QUEUE
927 			if (close(creat(qf, FileMode)) >= 0)
928 				break;
929 # endif QUEUE
930 		}
931 		if (c1 >= '~' && c2 >= 'Z')
932 		{
933 			syserr("queuename: Cannot create \"%s\" in \"%s\"",
934 				qf, QueueDir);
935 			exit(EX_OSERR);
936 		}
937 		e->e_id = newstr(&qf[2]);
938 		define('i', e->e_id, e);
939 # ifdef DEBUG
940 		if (tTd(7, 1))
941 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
942 # ifdef LOG
943 		if (LogLevel > 16)
944 			syslog(LOG_DEBUG, "%s: assigned id", e->e_id);
945 # endif LOG
946 # endif DEBUG
947 	}
948 
949 	if (type == '\0')
950 		return (NULL);
951 	(void) sprintf(buf, "%cf%s", type, e->e_id);
952 # ifdef DEBUG
953 	if (tTd(7, 2))
954 		printf("queuename: %s\n", buf);
955 # endif DEBUG
956 	return (buf);
957 }
958 /*
959 **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
960 **
961 **	Parameters:
962 **		e -- the envelope to unlock.
963 **
964 **	Returns:
965 **		none
966 **
967 **	Side Effects:
968 **		unlocks the queue for `e'.
969 */
970 
971 unlockqueue(e)
972 	ENVELOPE *e;
973 {
974 	/* remove the transcript */
975 #ifdef DEBUG
976 # ifdef LOG
977 	if (LogLevel > 19)
978 		syslog(LOG_DEBUG, "%s: unlock", e->e_id);
979 # endif LOG
980 	if (!tTd(51, 4))
981 #endif DEBUG
982 		xunlink(queuename(e, 'x'));
983 
984 # ifdef QUEUE
985 	/* last but not least, remove the lock */
986 	xunlink(queuename(e, 'l'));
987 # endif QUEUE
988 }
989