xref: /csrg-svn/usr.sbin/sendmail/src/queue.c (revision 22708)
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 #ifndef lint
12 static char	SccsId[] = "@(#)queue.c	5.1 (Berkeley) 06/07/85";
13 #endif not lint
14 
15 # include "sendmail.h"
16 # include <sys/stat.h>
17 # include <sys/dir.h>
18 # include <signal.h>
19 # include <errno.h>
20 
21 # ifndef QUEUE
22 SCCSID(@(#)queue.c	5.1		06/07/85	(no queueing));
23 # else QUEUE
24 
25 SCCSID(@(#)queue.c	5.1		06/07/85);
26 
27 /*
28 **  Work queue.
29 */
30 
31 struct work
32 {
33 	char		*w_name;	/* name of control file */
34 	long		w_pri;		/* priority of message, see below */
35 	struct work	*w_next;	/* next in queue */
36 };
37 
38 typedef struct work	WORK;
39 
40 WORK	*WorkQ;			/* queue of things to be done */
41 /*
42 **  QUEUEUP -- queue a message up for future transmission.
43 **
44 **	Parameters:
45 **		e -- the envelope to queue up.
46 **		queueall -- if TRUE, queue all addresses, rather than
47 **			just those with the QQUEUEUP flag set.
48 **		announce -- if TRUE, tell when you are queueing up.
49 **
50 **	Returns:
51 **		none.
52 **
53 **	Side Effects:
54 **		The current request are saved in a control file.
55 */
56 
57 queueup(e, queueall, announce)
58 	register ENVELOPE *e;
59 	bool queueall;
60 	bool announce;
61 {
62 	char *tf;
63 	char *qf;
64 	char buf[MAXLINE];
65 	register FILE *tfp;
66 	register HDR *h;
67 	register ADDRESS *q;
68 	MAILER nullmailer;
69 
70 	/*
71 	**  Create control file.
72 	*/
73 
74 	tf = newstr(queuename(e, 't'));
75 	tfp = fopen(tf, "w");
76 	if (tfp == NULL)
77 	{
78 		syserr("queueup: cannot create temp file %s", tf);
79 		return;
80 	}
81 	(void) chmod(tf, FileMode);
82 
83 # ifdef DEBUG
84 	if (tTd(40, 1))
85 		printf("queueing %s\n", e->e_id);
86 # endif DEBUG
87 
88 	/*
89 	**  If there is no data file yet, create one.
90 	*/
91 
92 	if (e->e_df == NULL)
93 	{
94 		register FILE *dfp;
95 		extern putbody();
96 
97 		e->e_df = newstr(queuename(e, 'd'));
98 		dfp = fopen(e->e_df, "w");
99 		if (dfp == NULL)
100 		{
101 			syserr("queueup: cannot create %s", e->e_df);
102 			(void) fclose(tfp);
103 			return;
104 		}
105 		(void) chmod(e->e_df, FileMode);
106 		(*e->e_putbody)(dfp, ProgMailer, e);
107 		(void) fclose(dfp);
108 		e->e_putbody = putbody;
109 	}
110 
111 	/*
112 	**  Output future work requests.
113 	**	Priority should be first, since it is read by orderq.
114 	*/
115 
116 	/* output message priority */
117 	fprintf(tfp, "P%ld\n", e->e_msgpriority);
118 
119 	/* output creation time */
120 	fprintf(tfp, "T%ld\n", e->e_ctime);
121 
122 	/* output name of data file */
123 	fprintf(tfp, "D%s\n", e->e_df);
124 
125 	/* message from envelope, if it exists */
126 	if (e->e_message != NULL)
127 		fprintf(tfp, "M%s\n", e->e_message);
128 
129 	/* output name of sender */
130 	fprintf(tfp, "S%s\n", e->e_from.q_paddr);
131 
132 	/* output list of recipient addresses */
133 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
134 	{
135 		if (queueall ? !bitset(QDONTSEND, q->q_flags) :
136 			       bitset(QQUEUEUP, q->q_flags))
137 		{
138 			fprintf(tfp, "R%s\n", q->q_paddr);
139 			if (announce)
140 			{
141 				e->e_to = q->q_paddr;
142 				message(Arpa_Info, "queued");
143 				if (LogLevel > 4)
144 					logdelivery("queued");
145 				e->e_to = NULL;
146 			}
147 #ifdef DEBUG
148 			if (tTd(40, 1))
149 			{
150 				printf("queueing ");
151 				printaddr(q, FALSE);
152 			}
153 #endif DEBUG
154 		}
155 	}
156 
157 	/*
158 	**  Output headers for this message.
159 	**	Expand macros completely here.  Queue run will deal with
160 	**	everything as absolute headers.
161 	**		All headers that must be relative to the recipient
162 	**		can be cracked later.
163 	**	We set up a "null mailer" -- i.e., a mailer that will have
164 	**	no effect on the addresses as they are output.
165 	*/
166 
167 	bzero((char *) &nullmailer, sizeof nullmailer);
168 	nullmailer.m_r_rwset = nullmailer.m_s_rwset = -1;
169 	nullmailer.m_eol = "\n";
170 
171 	define('g', "\001f", e);
172 	for (h = e->e_header; h != NULL; h = h->h_link)
173 	{
174 		extern bool bitzerop();
175 
176 		/* don't output null headers */
177 		if (h->h_value == NULL || h->h_value[0] == '\0')
178 			continue;
179 
180 		/* don't output resent headers on non-resent messages */
181 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
182 			continue;
183 
184 		/* output this header */
185 		fprintf(tfp, "H");
186 
187 		/* if conditional, output the set of conditions */
188 		if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags))
189 		{
190 			int j;
191 
192 			putc('?', tfp);
193 			for (j = '\0'; j <= '\177'; j++)
194 				if (bitnset(j, h->h_mflags))
195 					putc(j, tfp);
196 			putc('?', tfp);
197 		}
198 
199 		/* output the header: expand macros, convert addresses */
200 		if (bitset(H_DEFAULT, h->h_flags))
201 		{
202 			(void) expand(h->h_value, buf, &buf[sizeof buf], e);
203 			fprintf(tfp, "%s: %s\n", h->h_field, buf);
204 		}
205 		else if (bitset(H_FROM|H_RCPT, h->h_flags))
206 		{
207 			commaize(h, h->h_value, tfp, bitset(EF_OLDSTYLE, e->e_flags),
208 				 &nullmailer);
209 		}
210 		else
211 			fprintf(tfp, "%s: %s\n", h->h_field, h->h_value);
212 	}
213 
214 	/*
215 	**  Clean up.
216 	*/
217 
218 	(void) fclose(tfp);
219 	qf = queuename(e, 'q');
220 	if (tf != NULL)
221 	{
222 		holdsigs();
223 		(void) unlink(qf);
224 		if (link(tf, qf) < 0)
225 			syserr("cannot link(%s, %s), df=%s", tf, qf, e->e_df);
226 		else
227 			(void) unlink(tf);
228 		rlsesigs();
229 	}
230 
231 # ifdef LOG
232 	/* save log info */
233 	if (LogLevel > 15)
234 		syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df);
235 # endif LOG
236 }
237 /*
238 **  RUNQUEUE -- run the jobs in the queue.
239 **
240 **	Gets the stuff out of the queue in some presumably logical
241 **	order and processes them.
242 **
243 **	Parameters:
244 **		none.
245 **
246 **	Returns:
247 **		none.
248 **
249 **	Side Effects:
250 **		runs things in the mail queue.
251 */
252 
253 runqueue(forkflag)
254 	bool forkflag;
255 {
256 	/*
257 	**  See if we want to go off and do other useful work.
258 	*/
259 
260 	if (forkflag)
261 	{
262 		int pid;
263 
264 		pid = dofork();
265 		if (pid != 0)
266 		{
267 			/* parent -- pick up intermediate zombie */
268 			(void) waitfor(pid);
269 			if (QueueIntvl != 0)
270 				(void) setevent(QueueIntvl, runqueue, TRUE);
271 			return;
272 		}
273 		/* child -- double fork */
274 		if (fork() != 0)
275 			exit(EX_OK);
276 	}
277 # ifdef LOG
278 	if (LogLevel > 11)
279 		syslog(LOG_DEBUG, "runqueue %s, pid=%d", QueueDir, getpid());
280 # endif LOG
281 
282 	/*
283 	**  Release any resources used by the daemon code.
284 	*/
285 
286 # ifdef DAEMON
287 	clrdaemon();
288 # endif DAEMON
289 
290 	/*
291 	**  Start making passes through the queue.
292 	**	First, read and sort the entire queue.
293 	**	Then, process the work in that order.
294 	**		But if you take too long, start over.
295 	*/
296 
297 	/* order the existing work requests */
298 	(void) orderq();
299 
300 	/* process them once at a time */
301 	while (WorkQ != NULL)
302 	{
303 		WORK *w = WorkQ;
304 
305 		WorkQ = WorkQ->w_next;
306 		dowork(w);
307 		free(w->w_name);
308 		free((char *) w);
309 	}
310 	finis();
311 }
312 /*
313 **  ORDERQ -- order the work queue.
314 **
315 **	Parameters:
316 **		none.
317 **
318 **	Returns:
319 **		The number of request in the queue (not necessarily
320 **		the number of requests in WorkQ however).
321 **
322 **	Side Effects:
323 **		Sets WorkQ to the queue of available work, in order.
324 */
325 
326 # define WLSIZE		120	/* max size of worklist per sort */
327 
328 orderq()
329 {
330 	register struct direct *d;
331 	register WORK *w;
332 	register WORK **wp;		/* parent of w */
333 	DIR *f;
334 	register int i;
335 	WORK wlist[WLSIZE+1];
336 	int wn = -1;
337 	extern workcmpf();
338 
339 	/* clear out old WorkQ */
340 	for (w = WorkQ; w != NULL; )
341 	{
342 		register WORK *nw = w->w_next;
343 
344 		WorkQ = nw;
345 		free(w->w_name);
346 		free((char *) w);
347 		w = nw;
348 	}
349 
350 	/* open the queue directory */
351 	f = opendir(".");
352 	if (f == NULL)
353 	{
354 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
355 		return (0);
356 	}
357 
358 	/*
359 	**  Read the work directory.
360 	*/
361 
362 	while ((d = readdir(f)) != NULL)
363 	{
364 		FILE *cf;
365 		char lbuf[MAXNAME];
366 
367 		/* is this an interesting entry? */
368 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
369 			continue;
370 
371 		/* yes -- open control file (if not too many files) */
372 		if (++wn >= WLSIZE)
373 			continue;
374 		cf = fopen(d->d_name, "r");
375 		if (cf == NULL)
376 		{
377 			/* this may be some random person sending hir msgs */
378 			/* syserr("orderq: cannot open %s", cbuf); */
379 #ifdef DEBUG
380 			if (tTd(41, 2))
381 				printf("orderq: cannot open %s (%d)\n",
382 					d->d_name, errno);
383 #endif DEBUG
384 			errno = 0;
385 			wn--;
386 			continue;
387 		}
388 		wlist[wn].w_name = newstr(d->d_name);
389 
390 		/* extract useful information */
391 		while (fgets(lbuf, sizeof lbuf, cf) != NULL)
392 		{
393 			if (lbuf[0] == 'P')
394 			{
395 				(void) sscanf(&lbuf[1], "%ld", &wlist[wn].w_pri);
396 				break;
397 			}
398 		}
399 		(void) fclose(cf);
400 	}
401 	(void) closedir(f);
402 	wn++;
403 
404 	/*
405 	**  Sort the work directory.
406 	*/
407 
408 	qsort(wlist, min(wn, WLSIZE), sizeof *wlist, workcmpf);
409 
410 	/*
411 	**  Convert the work list into canonical form.
412 	**	Should be turning it into a list of envelopes here perhaps.
413 	*/
414 
415 	wp = &WorkQ;
416 	for (i = min(wn, WLSIZE); --i >= 0; )
417 	{
418 		w = (WORK *) xalloc(sizeof *w);
419 		w->w_name = wlist[i].w_name;
420 		w->w_pri = wlist[i].w_pri;
421 		w->w_next = NULL;
422 		*wp = w;
423 		wp = &w->w_next;
424 	}
425 
426 # ifdef DEBUG
427 	if (tTd(40, 1))
428 	{
429 		for (w = WorkQ; w != NULL; w = w->w_next)
430 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
431 	}
432 # endif DEBUG
433 
434 	return (wn);
435 }
436 /*
437 **  WORKCMPF -- compare function for ordering work.
438 **
439 **	Parameters:
440 **		a -- the first argument.
441 **		b -- the second argument.
442 **
443 **	Returns:
444 **		1 if a < b
445 **		0 if a == b
446 **		-1 if a > b
447 **
448 **	Side Effects:
449 **		none.
450 */
451 
452 workcmpf(a, b)
453 	register WORK *a;
454 	register WORK *b;
455 {
456 	if (a->w_pri == b->w_pri)
457 		return (0);
458 	else if (a->w_pri > b->w_pri)
459 		return (-1);
460 	else
461 		return (1);
462 }
463 /*
464 **  DOWORK -- do a work request.
465 **
466 **	Parameters:
467 **		w -- the work request to be satisfied.
468 **
469 **	Returns:
470 **		none.
471 **
472 **	Side Effects:
473 **		The work request is satisfied if possible.
474 */
475 
476 dowork(w)
477 	register WORK *w;
478 {
479 	register int i;
480 
481 # ifdef DEBUG
482 	if (tTd(40, 1))
483 		printf("dowork: %s pri %ld\n", w->w_name, w->w_pri);
484 # endif DEBUG
485 
486 	/*
487 	**  Fork for work.
488 	*/
489 
490 	i = fork();
491 	if (i < 0)
492 	{
493 		syserr("dowork: cannot fork");
494 		return;
495 	}
496 
497 	if (i == 0)
498 	{
499 		/*
500 		**  CHILD
501 		**	Lock the control file to avoid duplicate deliveries.
502 		**		Then run the file as though we had just read it.
503 		**	We save an idea of the temporary name so we
504 		**		can recover on interrupt.
505 		*/
506 
507 		/* set basic modes, etc. */
508 		(void) alarm(0);
509 		closexscript(CurEnv);
510 		CurEnv->e_flags &= ~EF_FATALERRS;
511 		QueueRun = TRUE;
512 		ErrorMode = EM_MAIL;
513 		CurEnv->e_id = &w->w_name[2];
514 # ifdef LOG
515 		if (LogLevel > 11)
516 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", CurEnv->e_id,
517 			       getpid());
518 # endif LOG
519 
520 		/* don't use the headers from sendmail.cf... */
521 		CurEnv->e_header = NULL;
522 
523 		/* lock the control file during processing */
524 		if (link(w->w_name, queuename(CurEnv, 'l')) < 0)
525 		{
526 			/* being processed by another queuer */
527 # ifdef LOG
528 			if (LogLevel > 4)
529 				syslog(LOG_DEBUG, "%s: locked", CurEnv->e_id);
530 # endif LOG
531 			exit(EX_OK);
532 		}
533 
534 		/* do basic system initialization */
535 		initsys();
536 
537 		/* read the queue control file */
538 		readqf(CurEnv, TRUE);
539 		CurEnv->e_flags |= EF_INQUEUE;
540 		eatheader(CurEnv);
541 
542 		/* do the delivery */
543 		if (!bitset(EF_FATALERRS, CurEnv->e_flags))
544 			sendall(CurEnv, SM_DELIVER);
545 
546 		/* finish up and exit */
547 		finis();
548 	}
549 
550 	/*
551 	**  Parent -- pick up results.
552 	*/
553 
554 	errno = 0;
555 	(void) waitfor(i);
556 }
557 /*
558 **  READQF -- read queue file and set up environment.
559 **
560 **	Parameters:
561 **		e -- the envelope of the job to run.
562 **		full -- if set, read in all information.  Otherwise just
563 **			read in info needed for a queue print.
564 **
565 **	Returns:
566 **		none.
567 **
568 **	Side Effects:
569 **		cf is read and created as the current job, as though
570 **		we had been invoked by argument.
571 */
572 
573 readqf(e, full)
574 	register ENVELOPE *e;
575 	bool full;
576 {
577 	char *qf;
578 	register FILE *qfp;
579 	char buf[MAXFIELD];
580 	extern char *fgetfolded();
581 
582 	/*
583 	**  Read and process the file.
584 	*/
585 
586 	qf = queuename(e, 'q');
587 	qfp = fopen(qf, "r");
588 	if (qfp == NULL)
589 	{
590 		syserr("readqf: no control file %s", qf);
591 		return;
592 	}
593 	FileName = qf;
594 	LineNumber = 0;
595 	if (Verbose && full)
596 		printf("\nRunning %s\n", e->e_id);
597 	while (fgetfolded(buf, sizeof buf, qfp) != NULL)
598 	{
599 		switch (buf[0])
600 		{
601 		  case 'R':		/* specify recipient */
602 			sendtolist(&buf[1], (ADDRESS *) NULL, &e->e_sendqueue);
603 			break;
604 
605 		  case 'H':		/* header */
606 			if (full)
607 				(void) chompheader(&buf[1], FALSE);
608 			break;
609 
610 		  case 'M':		/* message */
611 			e->e_message = newstr(&buf[1]);
612 			break;
613 
614 		  case 'S':		/* sender */
615 			setsender(newstr(&buf[1]));
616 			break;
617 
618 		  case 'D':		/* data file name */
619 			if (!full)
620 				break;
621 			e->e_df = newstr(&buf[1]);
622 			e->e_dfp = fopen(e->e_df, "r");
623 			if (e->e_dfp == NULL)
624 				syserr("readqf: cannot open %s", e->e_df);
625 			break;
626 
627 		  case 'T':		/* init time */
628 			(void) sscanf(&buf[1], "%ld", &e->e_ctime);
629 			break;
630 
631 		  case 'P':		/* message priority */
632 			(void) sscanf(&buf[1], "%ld", &e->e_msgpriority);
633 
634 			/* make sure that big things get sent eventually */
635 			e->e_msgpriority -= WKTIMEFACT;
636 			break;
637 
638 		  default:
639 			syserr("readqf(%s): bad line \"%s\"", e->e_id, buf);
640 			break;
641 		}
642 	}
643 
644 	FileName = NULL;
645 }
646 /*
647 **  PRINTQUEUE -- print out a representation of the mail queue
648 **
649 **	Parameters:
650 **		none.
651 **
652 **	Returns:
653 **		none.
654 **
655 **	Side Effects:
656 **		Prints a listing of the mail queue on the standard output.
657 */
658 
659 printqueue()
660 {
661 	register WORK *w;
662 	FILE *f;
663 	int nrequests;
664 	char buf[MAXLINE];
665 
666 	/*
667 	**  Read and order the queue.
668 	*/
669 
670 	nrequests = orderq();
671 
672 	/*
673 	**  Print the work list that we have read.
674 	*/
675 
676 	/* first see if there is anything */
677 	if (nrequests <= 0)
678 	{
679 		printf("Mail queue is empty\n");
680 		return;
681 	}
682 
683 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
684 	if (nrequests > WLSIZE)
685 		printf(", only %d printed", WLSIZE);
686 	printf(")\n--QID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
687 	for (w = WorkQ; w != NULL; w = w->w_next)
688 	{
689 		struct stat st;
690 		auto time_t submittime = 0;
691 		long dfsize = -1;
692 		int fd;
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 		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 				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 				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 		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 		strcpy(lf, qf);
800 		lf[0] = 'l';
801 		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