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