xref: /csrg-svn/usr.sbin/sendmail/src/queue.c (revision 23116)
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.4 (Berkeley) 06/08/85	(no queueing)";
21 # endif not lint
22 # else QUEUE
23 
24 # ifndef lint
25 static char	SccsId[] = "@(#)queue.c	5.4 (Berkeley) 06/08/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 	struct work	*w_next;	/* next in queue */
37 };
38 
39 typedef struct work	WORK;
40 
41 WORK	*WorkQ;			/* queue of things to be done */
42 /*
43 **  QUEUEUP -- queue a message up for future transmission.
44 **
45 **	Parameters:
46 **		e -- the envelope to queue up.
47 **		queueall -- if TRUE, queue all addresses, rather than
48 **			just those with the QQUEUEUP flag set.
49 **		announce -- if TRUE, tell when you are queueing up.
50 **
51 **	Returns:
52 **		none.
53 **
54 **	Side Effects:
55 **		The current request are saved in a control file.
56 */
57 
58 queueup(e, queueall, announce)
59 	register ENVELOPE *e;
60 	bool queueall;
61 	bool announce;
62 {
63 	char *tf;
64 	char *qf;
65 	char buf[MAXLINE];
66 	register FILE *tfp;
67 	register HDR *h;
68 	register ADDRESS *q;
69 	MAILER nullmailer;
70 
71 	/*
72 	**  Create control file.
73 	*/
74 
75 	tf = newstr(queuename(e, 't'));
76 	tfp = fopen(tf, "w");
77 	if (tfp == NULL)
78 	{
79 		syserr("queueup: cannot create temp file %s", tf);
80 		return;
81 	}
82 	(void) chmod(tf, FileMode);
83 
84 # ifdef DEBUG
85 	if (tTd(40, 1))
86 		printf("queueing %s\n", e->e_id);
87 # endif DEBUG
88 
89 	/*
90 	**  If there is no data file yet, create one.
91 	*/
92 
93 	if (e->e_df == NULL)
94 	{
95 		register FILE *dfp;
96 		extern putbody();
97 
98 		e->e_df = newstr(queuename(e, 'd'));
99 		dfp = fopen(e->e_df, "w");
100 		if (dfp == NULL)
101 		{
102 			syserr("queueup: cannot create %s", e->e_df);
103 			(void) fclose(tfp);
104 			return;
105 		}
106 		(void) chmod(e->e_df, FileMode);
107 		(*e->e_putbody)(dfp, ProgMailer, e);
108 		(void) fclose(dfp);
109 		e->e_putbody = putbody;
110 	}
111 
112 	/*
113 	**  Output future work requests.
114 	**	Priority should be first, since it is read by orderq.
115 	*/
116 
117 	/* output message priority */
118 	fprintf(tfp, "P%ld\n", e->e_msgpriority);
119 
120 	/* output creation time */
121 	fprintf(tfp, "T%ld\n", e->e_ctime);
122 
123 	/* output name of data file */
124 	fprintf(tfp, "D%s\n", e->e_df);
125 
126 	/* message from envelope, if it exists */
127 	if (e->e_message != NULL)
128 		fprintf(tfp, "M%s\n", e->e_message);
129 
130 	/* output name of sender */
131 	fprintf(tfp, "S%s\n", e->e_from.q_paddr);
132 
133 	/* output list of recipient addresses */
134 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
135 	{
136 		if (queueall ? !bitset(QDONTSEND, q->q_flags) :
137 			       bitset(QQUEUEUP, q->q_flags))
138 		{
139 			fprintf(tfp, "R%s\n", q->q_paddr);
140 			if (announce)
141 			{
142 				e->e_to = q->q_paddr;
143 				message(Arpa_Info, "queued");
144 				if (LogLevel > 4)
145 					logdelivery("queued");
146 				e->e_to = NULL;
147 			}
148 #ifdef DEBUG
149 			if (tTd(40, 1))
150 			{
151 				printf("queueing ");
152 				printaddr(q, FALSE);
153 			}
154 #endif DEBUG
155 		}
156 	}
157 
158 	/*
159 	**  Output headers for this message.
160 	**	Expand macros completely here.  Queue run will deal with
161 	**	everything as absolute headers.
162 	**		All headers that must be relative to the recipient
163 	**		can be cracked later.
164 	**	We set up a "null mailer" -- i.e., a mailer that will have
165 	**	no effect on the addresses as they are output.
166 	*/
167 
168 	bzero((char *) &nullmailer, sizeof nullmailer);
169 	nullmailer.m_r_rwset = nullmailer.m_s_rwset = -1;
170 	nullmailer.m_eol = "\n";
171 
172 	define('g', "\001f", e);
173 	for (h = e->e_header; h != NULL; h = h->h_link)
174 	{
175 		extern bool bitzerop();
176 
177 		/* don't output null headers */
178 		if (h->h_value == NULL || h->h_value[0] == '\0')
179 			continue;
180 
181 		/* don't output resent headers on non-resent messages */
182 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
183 			continue;
184 
185 		/* output this header */
186 		fprintf(tfp, "H");
187 
188 		/* if conditional, output the set of conditions */
189 		if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags))
190 		{
191 			int j;
192 
193 			(void) putc('?', tfp);
194 			for (j = '\0'; j <= '\177'; j++)
195 				if (bitnset(j, h->h_mflags))
196 					(void) putc(j, tfp);
197 			(void) putc('?', tfp);
198 		}
199 
200 		/* output the header: expand macros, convert addresses */
201 		if (bitset(H_DEFAULT, h->h_flags))
202 		{
203 			(void) expand(h->h_value, buf, &buf[sizeof buf], e);
204 			fprintf(tfp, "%s: %s\n", h->h_field, buf);
205 		}
206 		else if (bitset(H_FROM|H_RCPT, h->h_flags))
207 		{
208 			commaize(h, h->h_value, tfp, bitset(EF_OLDSTYLE, e->e_flags),
209 				 &nullmailer);
210 		}
211 		else
212 			fprintf(tfp, "%s: %s\n", h->h_field, h->h_value);
213 	}
214 
215 	/*
216 	**  Clean up.
217 	*/
218 
219 	(void) fclose(tfp);
220 	qf = queuename(e, 'q');
221 	if (tf != NULL)
222 	{
223 		holdsigs();
224 		(void) unlink(qf);
225 		if (link(tf, qf) < 0)
226 			syserr("cannot link(%s, %s), df=%s", tf, qf, e->e_df);
227 		else
228 			(void) unlink(tf);
229 		rlsesigs();
230 	}
231 
232 # ifdef LOG
233 	/* save log info */
234 	if (LogLevel > 15)
235 		syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df);
236 # endif LOG
237 }
238 /*
239 **  RUNQUEUE -- run the jobs in the queue.
240 **
241 **	Gets the stuff out of the queue in some presumably logical
242 **	order and processes them.
243 **
244 **	Parameters:
245 **		none.
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 	/*
258 	**  See if we want to go off and do other useful work.
259 	*/
260 
261 	if (forkflag)
262 	{
263 		int pid;
264 
265 		pid = dofork();
266 		if (pid != 0)
267 		{
268 			/* parent -- pick up intermediate zombie */
269 			(void) waitfor(pid);
270 			if (QueueIntvl != 0)
271 				(void) setevent(QueueIntvl, runqueue, TRUE);
272 			return;
273 		}
274 		/* child -- double fork */
275 		if (fork() != 0)
276 			exit(EX_OK);
277 	}
278 # ifdef LOG
279 	if (LogLevel > 11)
280 		syslog(LOG_DEBUG, "runqueue %s, pid=%d", QueueDir, getpid());
281 # endif LOG
282 
283 	/*
284 	**  Release any resources used by the daemon code.
285 	*/
286 
287 # ifdef DAEMON
288 	clrdaemon();
289 # endif DAEMON
290 
291 	/*
292 	**  Start making passes through the queue.
293 	**	First, read and sort the entire queue.
294 	**	Then, process the work in that order.
295 	**		But if you take too long, start over.
296 	*/
297 
298 	/* order the existing work requests */
299 	(void) orderq();
300 
301 	/* process them once at a time */
302 	while (WorkQ != NULL)
303 	{
304 		WORK *w = WorkQ;
305 
306 		WorkQ = WorkQ->w_next;
307 		dowork(w);
308 		free(w->w_name);
309 		free((char *) w);
310 	}
311 	finis();
312 }
313 /*
314 **  ORDERQ -- order the work queue.
315 **
316 **	Parameters:
317 **		none.
318 **
319 **	Returns:
320 **		The number of request in the queue (not necessarily
321 **		the number of requests in WorkQ however).
322 **
323 **	Side Effects:
324 **		Sets WorkQ to the queue of available work, in order.
325 */
326 
327 # define WLSIZE		120	/* max size of worklist per sort */
328 
329 orderq()
330 {
331 	register struct direct *d;
332 	register WORK *w;
333 	register WORK **wp;		/* parent of w */
334 	DIR *f;
335 	register int i;
336 	WORK wlist[WLSIZE+1];
337 	int wn = -1;
338 	extern workcmpf();
339 
340 	/* clear out old WorkQ */
341 	for (w = WorkQ; w != NULL; )
342 	{
343 		register WORK *nw = w->w_next;
344 
345 		WorkQ = nw;
346 		free(w->w_name);
347 		free((char *) w);
348 		w = nw;
349 	}
350 
351 	/* open the queue directory */
352 	f = opendir(".");
353 	if (f == NULL)
354 	{
355 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
356 		return (0);
357 	}
358 
359 	/*
360 	**  Read the work directory.
361 	*/
362 
363 	while ((d = readdir(f)) != NULL)
364 	{
365 		FILE *cf;
366 		char lbuf[MAXNAME];
367 
368 		/* is this an interesting entry? */
369 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
370 			continue;
371 
372 		/* yes -- open control file (if not too many files) */
373 		if (++wn >= WLSIZE)
374 			continue;
375 		cf = fopen(d->d_name, "r");
376 		if (cf == NULL)
377 		{
378 			/* this may be some random person sending hir msgs */
379 			/* syserr("orderq: cannot open %s", cbuf); */
380 #ifdef DEBUG
381 			if (tTd(41, 2))
382 				printf("orderq: cannot open %s (%d)\n",
383 					d->d_name, errno);
384 #endif DEBUG
385 			errno = 0;
386 			wn--;
387 			continue;
388 		}
389 		wlist[wn].w_name = newstr(d->d_name);
390 
391 		/* extract useful information */
392 		while (fgets(lbuf, sizeof lbuf, cf) != NULL)
393 		{
394 			if (lbuf[0] == 'P')
395 			{
396 				(void) sscanf(&lbuf[1], "%ld", &wlist[wn].w_pri);
397 				break;
398 			}
399 		}
400 		(void) fclose(cf);
401 	}
402 	(void) closedir(f);
403 	wn++;
404 
405 	/*
406 	**  Sort the work directory.
407 	*/
408 
409 	qsort((char *) wlist, min(wn, WLSIZE), sizeof *wlist, workcmpf);
410 
411 	/*
412 	**  Convert the work list into canonical form.
413 	**	Should be turning it into a list of envelopes here perhaps.
414 	*/
415 
416 	wp = &WorkQ;
417 	for (i = min(wn, WLSIZE); --i >= 0; )
418 	{
419 		w = (WORK *) xalloc(sizeof *w);
420 		w->w_name = wlist[i].w_name;
421 		w->w_pri = wlist[i].w_pri;
422 		w->w_next = NULL;
423 		*wp = w;
424 		wp = &w->w_next;
425 	}
426 
427 # ifdef DEBUG
428 	if (tTd(40, 1))
429 	{
430 		for (w = WorkQ; w != NULL; w = w->w_next)
431 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
432 	}
433 # endif DEBUG
434 
435 	return (wn);
436 }
437 /*
438 **  WORKCMPF -- compare function for ordering work.
439 **
440 **	Parameters:
441 **		a -- the first argument.
442 **		b -- the second argument.
443 **
444 **	Returns:
445 **		1 if a < b
446 **		0 if a == b
447 **		-1 if a > b
448 **
449 **	Side Effects:
450 **		none.
451 */
452 
453 workcmpf(a, b)
454 	register WORK *a;
455 	register WORK *b;
456 {
457 	if (a->w_pri == b->w_pri)
458 		return (0);
459 	else if (a->w_pri > b->w_pri)
460 		return (-1);
461 	else
462 		return (1);
463 }
464 /*
465 **  DOWORK -- do a work request.
466 **
467 **	Parameters:
468 **		w -- the work request to be satisfied.
469 **
470 **	Returns:
471 **		none.
472 **
473 **	Side Effects:
474 **		The work request is satisfied if possible.
475 */
476 
477 dowork(w)
478 	register WORK *w;
479 {
480 	register int i;
481 
482 # ifdef DEBUG
483 	if (tTd(40, 1))
484 		printf("dowork: %s pri %ld\n", w->w_name, w->w_pri);
485 # endif DEBUG
486 
487 	/*
488 	**  Fork for work.
489 	*/
490 
491 	i = fork();
492 	if (i < 0)
493 	{
494 		syserr("dowork: cannot fork");
495 		return;
496 	}
497 
498 	if (i == 0)
499 	{
500 		/*
501 		**  CHILD
502 		**	Lock the control file to avoid duplicate deliveries.
503 		**		Then run the file as though we had just read it.
504 		**	We save an idea of the temporary name so we
505 		**		can recover on interrupt.
506 		*/
507 
508 		/* set basic modes, etc. */
509 		(void) alarm(0);
510 		closexscript(CurEnv);
511 		CurEnv->e_flags &= ~EF_FATALERRS;
512 		QueueRun = TRUE;
513 		ErrorMode = EM_MAIL;
514 		CurEnv->e_id = &w->w_name[2];
515 # ifdef LOG
516 		if (LogLevel > 11)
517 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", CurEnv->e_id,
518 			       getpid());
519 # endif LOG
520 
521 		/* don't use the headers from sendmail.cf... */
522 		CurEnv->e_header = NULL;
523 
524 		/* lock the control file during processing */
525 		if (link(w->w_name, queuename(CurEnv, 'l')) < 0)
526 		{
527 			/* being processed by another queuer */
528 # ifdef LOG
529 			if (LogLevel > 4)
530 				syslog(LOG_DEBUG, "%s: locked", CurEnv->e_id);
531 # endif LOG
532 			exit(EX_OK);
533 		}
534 
535 		/* do basic system initialization */
536 		initsys();
537 
538 		/* read the queue control file */
539 		readqf(CurEnv, TRUE);
540 		CurEnv->e_flags |= EF_INQUEUE;
541 		eatheader(CurEnv);
542 
543 		/* do the delivery */
544 		if (!bitset(EF_FATALERRS, CurEnv->e_flags))
545 			sendall(CurEnv, SM_DELIVER);
546 
547 		/* finish up and exit */
548 		finis();
549 	}
550 
551 	/*
552 	**  Parent -- pick up results.
553 	*/
554 
555 	errno = 0;
556 	(void) waitfor(i);
557 }
558 /*
559 **  READQF -- read queue file and set up environment.
560 **
561 **	Parameters:
562 **		e -- the envelope of the job to run.
563 **		full -- if set, read in all information.  Otherwise just
564 **			read in info needed for a queue print.
565 **
566 **	Returns:
567 **		none.
568 **
569 **	Side Effects:
570 **		cf is read and created as the current job, as though
571 **		we had been invoked by argument.
572 */
573 
574 readqf(e, full)
575 	register ENVELOPE *e;
576 	bool full;
577 {
578 	char *qf;
579 	register FILE *qfp;
580 	char buf[MAXFIELD];
581 	extern char *fgetfolded();
582 
583 	/*
584 	**  Read and process the file.
585 	*/
586 
587 	qf = queuename(e, 'q');
588 	qfp = fopen(qf, "r");
589 	if (qfp == NULL)
590 	{
591 		syserr("readqf: no control file %s", qf);
592 		return;
593 	}
594 	FileName = qf;
595 	LineNumber = 0;
596 	if (Verbose && full)
597 		printf("\nRunning %s\n", e->e_id);
598 	while (fgetfolded(buf, sizeof buf, qfp) != NULL)
599 	{
600 		switch (buf[0])
601 		{
602 		  case 'R':		/* specify recipient */
603 			sendtolist(&buf[1], (ADDRESS *) NULL, &e->e_sendqueue);
604 			break;
605 
606 		  case 'H':		/* header */
607 			if (full)
608 				(void) chompheader(&buf[1], FALSE);
609 			break;
610 
611 		  case 'M':		/* message */
612 			e->e_message = newstr(&buf[1]);
613 			break;
614 
615 		  case 'S':		/* sender */
616 			setsender(newstr(&buf[1]));
617 			break;
618 
619 		  case 'D':		/* data file name */
620 			if (!full)
621 				break;
622 			e->e_df = newstr(&buf[1]);
623 			e->e_dfp = fopen(e->e_df, "r");
624 			if (e->e_dfp == NULL)
625 				syserr("readqf: cannot open %s", e->e_df);
626 			break;
627 
628 		  case 'T':		/* init time */
629 			(void) sscanf(&buf[1], "%ld", &e->e_ctime);
630 			break;
631 
632 		  case 'P':		/* message priority */
633 			(void) sscanf(&buf[1], "%ld", &e->e_msgpriority);
634 
635 			/* make sure that big things get sent eventually */
636 			e->e_msgpriority -= WKTIMEFACT;
637 			break;
638 
639 		  default:
640 			syserr("readqf(%s): bad line \"%s\"", e->e_id, buf);
641 			break;
642 		}
643 	}
644 
645 	FileName = NULL;
646 }
647 /*
648 **  PRINTQUEUE -- print out a representation of the mail queue
649 **
650 **	Parameters:
651 **		none.
652 **
653 **	Returns:
654 **		none.
655 **
656 **	Side Effects:
657 **		Prints a listing of the mail queue on the standard output.
658 */
659 
660 printqueue()
661 {
662 	register WORK *w;
663 	FILE *f;
664 	int nrequests;
665 	char buf[MAXLINE];
666 
667 	/*
668 	**  Read and order the queue.
669 	*/
670 
671 	nrequests = orderq();
672 
673 	/*
674 	**  Print the work list that we have read.
675 	*/
676 
677 	/* first see if there is anything */
678 	if (nrequests <= 0)
679 	{
680 		printf("Mail queue is empty\n");
681 		return;
682 	}
683 
684 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
685 	if (nrequests > WLSIZE)
686 		printf(", only %d printed", WLSIZE);
687 	printf(")\n--QID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
688 	for (w = WorkQ; w != NULL; w = w->w_next)
689 	{
690 		struct stat st;
691 		auto time_t submittime = 0;
692 		long dfsize = -1;
693 		char lf[20];
694 		char message[MAXLINE];
695 
696 		f = fopen(w->w_name, "r");
697 		if (f == NULL)
698 		{
699 			errno = 0;
700 			continue;
701 		}
702 		printf("%7s", w->w_name + 2);
703 		(void) strcpy(lf, w->w_name);
704 		lf[0] = 'l';
705 		if (stat(lf, &st) >= 0)
706 			printf("*");
707 		else
708 			printf(" ");
709 		errno = 0;
710 
711 		message[0] = '\0';
712 		while (fgets(buf, sizeof buf, f) != NULL)
713 		{
714 			fixcrlf(buf, TRUE);
715 			switch (buf[0])
716 			{
717 			  case 'M':	/* error message */
718 				(void) strcpy(message, &buf[1]);
719 				break;
720 
721 			  case 'S':	/* sender name */
722 				printf("%8ld %.16s %.45s", dfsize,
723 					ctime(&submittime), &buf[1]);
724 				if (message[0] != '\0')
725 					printf("\n\t\t\t\t  (%.43s)", message);
726 				break;
727 
728 			  case 'R':	/* recipient name */
729 				printf("\n\t\t\t\t  %.45s", &buf[1]);
730 				break;
731 
732 			  case 'T':	/* creation time */
733 				(void) sscanf(&buf[1], "%ld", &submittime);
734 				break;
735 
736 			  case 'D':	/* data file name */
737 				if (stat(&buf[1], &st) >= 0)
738 					dfsize = st.st_size;
739 				break;
740 			}
741 		}
742 		if (submittime == (time_t) 0)
743 			printf(" (no control file)");
744 		printf("\n");
745 		(void) fclose(f);
746 	}
747 }
748 
749 # endif QUEUE
750 /*
751 **  QUEUENAME -- build a file name in the queue directory for this envelope.
752 **
753 **	Assigns an id code if one does not already exist.
754 **	This code is very careful to avoid trashing existing files
755 **	under any circumstances.
756 **		We first create an nf file that is only used when
757 **		assigning an id.  This file is always empty, so that
758 **		we can never accidently truncate an lf file.
759 **
760 **	Parameters:
761 **		e -- envelope to build it in/from.
762 **		type -- the file type, used as the first character
763 **			of the file name.
764 **
765 **	Returns:
766 **		a pointer to the new file name (in a static buffer).
767 **
768 **	Side Effects:
769 **		Will create the lf and qf files if no id code is
770 **		already assigned.  This will cause the envelope
771 **		to be modified.
772 */
773 
774 char *
775 queuename(e, type)
776 	register ENVELOPE *e;
777 	char type;
778 {
779 	static char buf[MAXNAME];
780 	static int pid = -1;
781 	char c1 = 'A';
782 	char c2 = 'A';
783 
784 	if (e->e_id == NULL)
785 	{
786 		char qf[20];
787 		char nf[20];
788 		char lf[20];
789 
790 		/* find a unique id */
791 		if (pid != getpid())
792 		{
793 			/* new process -- start back at "AA" */
794 			pid = getpid();
795 			c1 = 'A';
796 			c2 = 'A' - 1;
797 		}
798 		(void) sprintf(qf, "qfAA%05d", pid);
799 		(void) strcpy(lf, qf);
800 		lf[0] = 'l';
801 		(void) strcpy(nf, qf);
802 		nf[0] = 'n';
803 
804 		while (c1 < '~' || c2 < 'Z')
805 		{
806 			int i;
807 
808 			if (c2 >= 'Z')
809 			{
810 				c1++;
811 				c2 = 'A' - 1;
812 			}
813 			lf[2] = nf[2] = qf[2] = c1;
814 			lf[3] = nf[3] = qf[3] = ++c2;
815 # ifdef DEBUG
816 			if (tTd(7, 20))
817 				printf("queuename: trying \"%s\"\n", nf);
818 # endif DEBUG
819 
820 # ifdef QUEUE
821 			if (access(lf, 0) >= 0 || access(qf, 0) >= 0)
822 				continue;
823 			errno = 0;
824 			i = creat(nf, FileMode);
825 			if (i < 0)
826 			{
827 				(void) unlink(nf);	/* kernel bug */
828 				continue;
829 			}
830 			(void) close(i);
831 			i = link(nf, lf);
832 			(void) unlink(nf);
833 			if (i < 0)
834 				continue;
835 			if (link(lf, qf) >= 0)
836 				break;
837 			(void) unlink(lf);
838 # else QUEUE
839 			if (close(creat(qf, FileMode)) >= 0)
840 				break;
841 # endif QUEUE
842 		}
843 		if (c1 >= '~' && c2 >= 'Z')
844 		{
845 			syserr("queuename: Cannot create \"%s\" in \"%s\"",
846 				qf, QueueDir);
847 			exit(EX_OSERR);
848 		}
849 		e->e_id = newstr(&qf[2]);
850 		define('i', e->e_id, e);
851 # ifdef DEBUG
852 		if (tTd(7, 1))
853 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
854 # ifdef LOG
855 		if (LogLevel > 16)
856 			syslog(LOG_DEBUG, "%s: assigned id", e->e_id);
857 # endif LOG
858 # endif DEBUG
859 	}
860 
861 	if (type == '\0')
862 		return (NULL);
863 	(void) sprintf(buf, "%cf%s", type, e->e_id);
864 # ifdef DEBUG
865 	if (tTd(7, 2))
866 		printf("queuename: %s\n", buf);
867 # endif DEBUG
868 	return (buf);
869 }
870 /*
871 **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
872 **
873 **	Parameters:
874 **		e -- the envelope to unlock.
875 **
876 **	Returns:
877 **		none
878 **
879 **	Side Effects:
880 **		unlocks the queue for `e'.
881 */
882 
883 unlockqueue(e)
884 	ENVELOPE *e;
885 {
886 	/* remove the transcript */
887 #ifdef DEBUG
888 # ifdef LOG
889 	if (LogLevel > 19)
890 		syslog(LOG_DEBUG, "%s: unlock", e->e_id);
891 # endif LOG
892 	if (!tTd(51, 4))
893 #endif DEBUG
894 		xunlink(queuename(e, 'x'));
895 
896 # ifdef QUEUE
897 	/* last but not least, remove the lock */
898 	xunlink(queuename(e, 'l'));
899 # endif QUEUE
900 }
901