xref: /original-bsd/usr.sbin/sendmail/src/queue.c (revision 0997b878)
1 /*
2  * Copyright (c) 1983 Eric P. Allman
3  * Copyright (c) 1988, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * %sccs.include.redist.c%
7  */
8 
9 # include "sendmail.h"
10 
11 #ifndef lint
12 #ifdef QUEUE
13 static char sccsid[] = "@(#)queue.c	8.41.1.1 (Berkeley) 02/10/95 (with queueing)";
14 #else
15 static char sccsid[] = "@(#)queue.c	8.41.1.1 (Berkeley) 02/10/95 (without queueing)";
16 #endif
17 #endif /* not lint */
18 
19 # include <errno.h>
20 # include <pwd.h>
21 # include <dirent.h>
22 
23 # ifdef QUEUE
24 
25 /*
26 **  Work queue.
27 */
28 
29 struct work
30 {
31 	char		*w_name;	/* name of control file */
32 	long		w_pri;		/* priority of message, see below */
33 	time_t		w_ctime;	/* creation time of message */
34 	struct work	*w_next;	/* next in queue */
35 };
36 
37 typedef struct work	WORK;
38 
39 WORK	*WorkQ;			/* queue of things to be done */
40 /*
41 **  QUEUEUP -- queue a message up for future transmission.
42 **
43 **	Parameters:
44 **		e -- the envelope to queue up.
45 **		queueall -- if TRUE, queue all addresses, rather than
46 **			just those with the QQUEUEUP flag set.
47 **		announce -- if TRUE, tell when you are queueing up.
48 **
49 **	Returns:
50 **		none.
51 **
52 **	Side Effects:
53 **		The current request are saved in a control file.
54 **		The queue file is left locked.
55 */
56 
57 queueup(e, queueall, announce)
58 	register ENVELOPE *e;
59 	bool queueall;
60 	bool announce;
61 {
62 	char *qf;
63 	register FILE *tfp;
64 	register HDR *h;
65 	register ADDRESS *q;
66 	int fd;
67 	int i;
68 	bool newid;
69 	register char *p;
70 	MAILER nullmailer;
71 	MCI mcibuf;
72 	char buf[MAXLINE], tf[MAXLINE];
73 
74 	/*
75 	**  Create control file.
76 	*/
77 
78 	newid = (e->e_id == NULL) || !bitset(EF_INQUEUE, e->e_flags);
79 
80 	/* if newid, queuename will create a locked qf file in e->lockfp */
81 	strcpy(tf, queuename(e, 't'));
82 	tfp = e->e_lockfp;
83 	if (tfp == NULL)
84 		newid = FALSE;
85 
86 	/* if newid, just write the qf file directly (instead of tf file) */
87 	if (!newid)
88 	{
89 		/* get a locked tf file */
90 		for (i = 0; i < 128; i++)
91 		{
92 			fd = open(tf, O_CREAT|O_WRONLY|O_EXCL, FileMode);
93 			if (fd < 0)
94 			{
95 				if (errno != EEXIST)
96 					break;
97 #ifdef LOG
98 				if (LogLevel > 0 && (i % 32) == 0)
99 					syslog(LOG_ALERT, "queueup: cannot create %s, uid=%d: %s",
100 						tf, geteuid(), errstring(errno));
101 #endif
102 			}
103 			else
104 			{
105 				if (lockfile(fd, tf, NULL, LOCK_EX|LOCK_NB))
106 					break;
107 #ifdef LOG
108 				else if (LogLevel > 0 && (i % 32) == 0)
109 					syslog(LOG_ALERT, "queueup: cannot lock %s: %s",
110 						tf, errstring(errno));
111 #endif
112 				close(fd);
113 			}
114 
115 			if ((i % 32) == 31)
116 			{
117 				/* save the old temp file away */
118 				(void) rename(tf, queuename(e, 'T'));
119 			}
120 			else
121 				sleep(i % 32);
122 		}
123 		if (fd < 0 || (tfp = fdopen(fd, "w")) == NULL)
124 		{
125 			printopenfds(TRUE);
126 			syserr("!queueup: cannot create queue temp file %s, uid=%d",
127 				tf, geteuid());
128 		}
129 	}
130 
131 	if (tTd(40, 1))
132 		printf("\n>>>>> queueing %s%s >>>>>\n", e->e_id,
133 			newid ? " (new id)" : "");
134 	if (tTd(40, 9))
135 	{
136 		printf("  tfp=");
137 		dumpfd(fileno(tfp), TRUE, FALSE);
138 		printf("  lockfp=");
139 		if (e->e_lockfp == NULL)
140 			printf("NULL\n");
141 		else
142 			dumpfd(fileno(e->e_lockfp), TRUE, FALSE);
143 	}
144 
145 	/*
146 	**  If there is no data file yet, create one.
147 	*/
148 
149 	if (e->e_df == NULL)
150 	{
151 		register FILE *dfp;
152 		extern putbody();
153 
154 		e->e_df = queuename(e, 'd');
155 		e->e_df = newstr(e->e_df);
156 		fd = open(e->e_df, O_WRONLY|O_CREAT|O_TRUNC, FileMode);
157 		if (fd < 0 || (dfp = fdopen(fd, "w")) == NULL)
158 			syserr("!queueup: cannot create data temp file %s, uid=%d",
159 				e->e_df, geteuid());
160 		bzero(&mcibuf, sizeof mcibuf);
161 		mcibuf.mci_out = dfp;
162 		mcibuf.mci_mailer = FileMailer;
163 		(*e->e_putbody)(&mcibuf, e, NULL);
164 		(void) xfclose(dfp, "queueup dfp", e->e_id);
165 		e->e_putbody = putbody;
166 	}
167 
168 	/*
169 	**  Output future work requests.
170 	**	Priority and creation time should be first, since
171 	**	they are required by orderq.
172 	*/
173 
174 	/* output message priority */
175 	fprintf(tfp, "P%ld\n", e->e_msgpriority);
176 
177 	/* output creation time */
178 	fprintf(tfp, "T%ld\n", e->e_ctime);
179 
180 	/* output type and name of data file */
181 	if (e->e_bodytype != NULL)
182 		fprintf(tfp, "B%s\n", e->e_bodytype);
183 	fprintf(tfp, "D%s\n", e->e_df);
184 
185 	/* message from envelope, if it exists */
186 	if (e->e_message != NULL)
187 		fprintf(tfp, "M%s\n", denlstring(e->e_message));
188 
189 	/* send various flag bits through */
190 	p = buf;
191 	if (bitset(EF_WARNING, e->e_flags))
192 		*p++ = 'w';
193 	if (bitset(EF_RESPONSE, e->e_flags))
194 		*p++ = 'r';
195 	*p++ = '\0';
196 	if (buf[0] != '\0')
197 		fprintf(tfp, "F%s\n", buf);
198 
199 	/* $r and $s and $_ macro values */
200 	if ((p = macvalue('r', e)) != NULL)
201 		fprintf(tfp, "$r%s\n", denlstring(p));
202 	if ((p = macvalue('s', e)) != NULL)
203 		fprintf(tfp, "$s%s\n", denlstring(p));
204 	if ((p = macvalue('_', e)) != NULL)
205 		fprintf(tfp, "$_%s\n", denlstring(p));
206 
207 	/* output name of sender */
208 	fprintf(tfp, "S%s\n", denlstring(e->e_from.q_paddr));
209 
210 	/* output list of error recipients */
211 	printctladdr(NULL, NULL);
212 	for (q = e->e_errorqueue; q != NULL; q = q->q_next)
213 	{
214 		if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
215 		{
216 			printctladdr(q, tfp);
217 			fprintf(tfp, "E%s\n", denlstring(q->q_paddr));
218 		}
219 	}
220 
221 	/* output list of recipient addresses */
222 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
223 	{
224 		if (bitset(QQUEUEUP, q->q_flags) ||
225 		    (queueall && !bitset(QDONTSEND|QBADADDR|QSENT, q->q_flags)))
226 		{
227 			printctladdr(q, tfp);
228 			fprintf(tfp, "R%s\n", denlstring(q->q_paddr));
229 			if (announce)
230 			{
231 				e->e_to = q->q_paddr;
232 				message("queued");
233 				if (LogLevel > 8)
234 					logdelivery(NULL, NULL, "queued", NULL, e);
235 				e->e_to = NULL;
236 			}
237 			if (tTd(40, 1))
238 			{
239 				printf("queueing ");
240 				printaddr(q, FALSE);
241 			}
242 		}
243 	}
244 
245 	/*
246 	**  Output headers for this message.
247 	**	Expand macros completely here.  Queue run will deal with
248 	**	everything as absolute headers.
249 	**		All headers that must be relative to the recipient
250 	**		can be cracked later.
251 	**	We set up a "null mailer" -- i.e., a mailer that will have
252 	**	no effect on the addresses as they are output.
253 	*/
254 
255 	bzero((char *) &nullmailer, sizeof nullmailer);
256 	nullmailer.m_re_rwset = nullmailer.m_rh_rwset =
257 			nullmailer.m_se_rwset = nullmailer.m_sh_rwset = -1;
258 	nullmailer.m_eol = "\n";
259 	bzero(&mcibuf, sizeof mcibuf);
260 	mcibuf.mci_mailer = &nullmailer;
261 	mcibuf.mci_out = tfp;
262 
263 	define('g', "\201f", e);
264 	for (h = e->e_header; h != NULL; h = h->h_link)
265 	{
266 		extern bool bitzerop();
267 
268 		/* don't output null headers */
269 		if (h->h_value == NULL || h->h_value[0] == '\0')
270 			continue;
271 
272 		/* don't output resent headers on non-resent messages */
273 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
274 			continue;
275 
276 		/* expand macros; if null, don't output header at all */
277 		if (bitset(H_DEFAULT, h->h_flags))
278 		{
279 			(void) expand(h->h_value, buf, &buf[sizeof buf], e);
280 			if (buf[0] == '\0')
281 				continue;
282 		}
283 
284 		/* output this header */
285 		fprintf(tfp, "H");
286 
287 		/* if conditional, output the set of conditions */
288 		if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags))
289 		{
290 			int j;
291 
292 			(void) putc('?', tfp);
293 			for (j = '\0'; j <= '\177'; j++)
294 				if (bitnset(j, h->h_mflags))
295 					(void) putc(j, tfp);
296 			(void) putc('?', tfp);
297 		}
298 
299 		/* output the header: expand macros, convert addresses */
300 		if (bitset(H_DEFAULT, h->h_flags))
301 		{
302 			fprintf(tfp, "%s: %s\n", h->h_field, buf);
303 		}
304 		else if (bitset(H_FROM|H_RCPT, h->h_flags))
305 		{
306 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
307 			FILE *savetrace = TrafficLogFile;
308 
309 			TrafficLogFile = NULL;
310 
311 			if (bitset(H_FROM, h->h_flags))
312 				oldstyle = FALSE;
313 
314 			commaize(h, h->h_value, oldstyle, &mcibuf, e);
315 
316 			TrafficLogFile = savetrace;
317 		}
318 		else
319 			fprintf(tfp, "%s: %s\n", h->h_field, h->h_value);
320 	}
321 
322 	/*
323 	**  Clean up.
324 	*/
325 
326 	if (fflush(tfp) < 0 || fsync(fileno(tfp)) < 0 || ferror(tfp))
327 	{
328 		if (newid)
329 			syserr("!552 Error writing control file %s", tf);
330 		else
331 			syserr("!452 Error writing control file %s", tf);
332 	}
333 
334 	if (!newid)
335 	{
336 		/* rename (locked) tf to be (locked) qf */
337 		qf = queuename(e, 'q');
338 		if (rename(tf, qf) < 0)
339 			syserr("cannot rename(%s, %s), df=%s, uid=%d",
340 				tf, qf, e->e_df, geteuid());
341 
342 		/* close and unlock old (locked) qf */
343 		if (e->e_lockfp != NULL)
344 			(void) xfclose(e->e_lockfp, "queueup lockfp", e->e_id);
345 		e->e_lockfp = tfp;
346 	}
347 	else
348 		qf = tf;
349 	errno = 0;
350 	e->e_flags |= EF_INQUEUE;
351 
352 # ifdef LOG
353 	/* save log info */
354 	if (LogLevel > 79)
355 		syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df);
356 # endif /* LOG */
357 
358 	if (tTd(40, 1))
359 		printf("<<<<< done queueing %s <<<<<\n\n", e->e_id);
360 	return;
361 }
362 
363 printctladdr(a, tfp)
364 	register ADDRESS *a;
365 	FILE *tfp;
366 {
367 	char *uname;
368 	register struct passwd *pw;
369 	register ADDRESS *q;
370 	uid_t uid;
371 	static ADDRESS *lastctladdr;
372 	static uid_t lastuid;
373 
374 	/* initialization */
375 	if (a == NULL || a->q_alias == NULL || tfp == NULL)
376 	{
377 		if (lastctladdr != NULL && tfp != NULL)
378 			fprintf(tfp, "C\n");
379 		lastctladdr = NULL;
380 		lastuid = 0;
381 		return;
382 	}
383 
384 	/* find the active uid */
385 	q = getctladdr(a);
386 	if (q == NULL)
387 		uid = 0;
388 	else
389 		uid = q->q_uid;
390 	a = a->q_alias;
391 
392 	/* check to see if this is the same as last time */
393 	if (lastctladdr != NULL && uid == lastuid &&
394 	    strcmp(lastctladdr->q_paddr, a->q_paddr) == 0)
395 		return;
396 	lastuid = uid;
397 	lastctladdr = a;
398 
399 	if (uid == 0 || (pw = getpwuid(uid)) == NULL)
400 		uname = "";
401 	else
402 		uname = pw->pw_name;
403 
404 	fprintf(tfp, "C%s:%s\n", uname, denlstring(a->q_paddr));
405 }
406 
407 /*
408 **  RUNQUEUE -- run the jobs in the queue.
409 **
410 **	Gets the stuff out of the queue in some presumably logical
411 **	order and processes them.
412 **
413 **	Parameters:
414 **		forkflag -- TRUE if the queue scanning should be done in
415 **			a child process.  We double-fork so it is not our
416 **			child and we don't have to clean up after it.
417 **
418 **	Returns:
419 **		none.
420 **
421 **	Side Effects:
422 **		runs things in the mail queue.
423 */
424 
425 ENVELOPE	QueueEnvelope;		/* the queue run envelope */
426 
427 runqueue(forkflag)
428 	bool forkflag;
429 {
430 	register ENVELOPE *e;
431 	extern ENVELOPE BlankEnvelope;
432 
433 	/*
434 	**  If no work will ever be selected, don't even bother reading
435 	**  the queue.
436 	*/
437 
438 	CurrentLA = getla();	/* get load average */
439 
440 	if (shouldqueue(0L, curtime()))
441 	{
442 		if (Verbose)
443 			printf("Skipping queue run -- load average too high\n");
444 		if (forkflag && QueueIntvl != 0)
445 			(void) setevent(QueueIntvl, runqueue, TRUE);
446 		return;
447 	}
448 
449 	/*
450 	**  See if we want to go off and do other useful work.
451 	*/
452 
453 	if (forkflag)
454 	{
455 		int pid;
456 #ifdef SIGCHLD
457 		extern void reapchild();
458 
459 		(void) setsignal(SIGCHLD, reapchild);
460 #endif
461 
462 		pid = dofork();
463 		if (pid != 0)
464 		{
465 			/* parent -- pick up intermediate zombie */
466 #ifndef SIGCHLD
467 			(void) waitfor(pid);
468 #endif /* SIGCHLD */
469 			if (QueueIntvl != 0)
470 				(void) setevent(QueueIntvl, runqueue, TRUE);
471 			return;
472 		}
473 		/* child -- double fork */
474 #ifndef SIGCHLD
475 		if (fork() != 0)
476 			exit(EX_OK);
477 #else /* SIGCHLD */
478 		(void) setsignal(SIGCHLD, SIG_DFL);
479 #endif /* SIGCHLD */
480 	}
481 
482 	setproctitle("running queue: %s", QueueDir);
483 
484 # ifdef LOG
485 	if (LogLevel > 69)
486 		syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d",
487 			QueueDir, getpid(), forkflag);
488 # endif /* LOG */
489 
490 	/*
491 	**  Release any resources used by the daemon code.
492 	*/
493 
494 # ifdef DAEMON
495 	clrdaemon();
496 # endif /* DAEMON */
497 
498 	/* force it to run expensive jobs */
499 	NoConnect = FALSE;
500 
501 	/*
502 	**  Create ourselves an envelope
503 	*/
504 
505 	CurEnv = &QueueEnvelope;
506 	e = newenvelope(&QueueEnvelope, CurEnv);
507 	e->e_flags = BlankEnvelope.e_flags;
508 
509 	/*
510 	**  Make sure the alias database is open.
511 	*/
512 
513 	initmaps(FALSE, e);
514 
515 	/*
516 	**  Start making passes through the queue.
517 	**	First, read and sort the entire queue.
518 	**	Then, process the work in that order.
519 	**		But if you take too long, start over.
520 	*/
521 
522 	/* order the existing work requests */
523 	(void) orderq(FALSE);
524 
525 	/* process them once at a time */
526 	while (WorkQ != NULL)
527 	{
528 		WORK *w = WorkQ;
529 
530 		WorkQ = WorkQ->w_next;
531 
532 		/*
533 		**  Ignore jobs that are too expensive for the moment.
534 		*/
535 
536 		if (shouldqueue(w->w_pri, w->w_ctime))
537 		{
538 			if (Verbose)
539 				printf("\nSkipping %s\n", w->w_name + 2);
540 		}
541 		else
542 		{
543 			pid_t pid;
544 			extern pid_t dowork();
545 
546 			pid = dowork(w->w_name + 2, ForkQueueRuns, FALSE, e);
547 			errno = 0;
548 			if (pid != 0)
549 				(void) waitfor(pid);
550 		}
551 		free(w->w_name);
552 		free((char *) w);
553 	}
554 
555 	/* exit without the usual cleanup */
556 	e->e_id = NULL;
557 	finis();
558 }
559 /*
560 **  ORDERQ -- order the work queue.
561 **
562 **	Parameters:
563 **		doall -- if set, include everything in the queue (even
564 **			the jobs that cannot be run because the load
565 **			average is too high).  Otherwise, exclude those
566 **			jobs.
567 **
568 **	Returns:
569 **		The number of request in the queue (not necessarily
570 **		the number of requests in WorkQ however).
571 **
572 **	Side Effects:
573 **		Sets WorkQ to the queue of available work, in order.
574 */
575 
576 # define NEED_P		001
577 # define NEED_T		002
578 # define NEED_R		004
579 # define NEED_S		010
580 
581 orderq(doall)
582 	bool doall;
583 {
584 	register struct dirent *d;
585 	register WORK *w;
586 	DIR *f;
587 	register int i;
588 	WORK wlist[QUEUESIZE+1];
589 	int wn = -1;
590 	extern workcmpf();
591 
592 	if (tTd(41, 1))
593 	{
594 		printf("orderq:\n");
595 		if (QueueLimitId != NULL)
596 			printf("\tQueueLimitId = %s\n", QueueLimitId);
597 		if (QueueLimitSender != NULL)
598 			printf("\tQueueLimitSender = %s\n", QueueLimitSender);
599 		if (QueueLimitRecipient != NULL)
600 			printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient);
601 	}
602 
603 	/* clear out old WorkQ */
604 	for (w = WorkQ; w != NULL; )
605 	{
606 		register WORK *nw = w->w_next;
607 
608 		WorkQ = nw;
609 		free(w->w_name);
610 		free((char *) w);
611 		w = nw;
612 	}
613 
614 	/* open the queue directory */
615 	f = opendir(".");
616 	if (f == NULL)
617 	{
618 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
619 		return (0);
620 	}
621 
622 	/*
623 	**  Read the work directory.
624 	*/
625 
626 	while ((d = readdir(f)) != NULL)
627 	{
628 		FILE *cf;
629 		register char *p;
630 		char lbuf[MAXNAME];
631 		extern bool strcontainedin();
632 
633 		/* is this an interesting entry? */
634 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
635 			continue;
636 
637 		if (QueueLimitId != NULL &&
638 		    !strcontainedin(QueueLimitId, d->d_name))
639 			continue;
640 
641 		/*
642 		**  Check queue name for plausibility.  This handles
643 		**  both old and new type ids.
644 		*/
645 
646 		p = d->d_name + 2;
647 		if (isupper(p[0]) && isupper(p[2]))
648 			p += 3;
649 		else if (isupper(p[1]))
650 			p += 2;
651 		else
652 			p = d->d_name;
653 		for (i = 0; isdigit(*p); p++)
654 			i++;
655 		if (i < 5 || *p != '\0')
656 		{
657 			if (Verbose)
658 				printf("orderq: bogus qf name %s\n", d->d_name);
659 #ifdef LOG
660 			if (LogLevel > 3)
661 				syslog(LOG_CRIT, "orderq: bogus qf name %s",
662 					d->d_name);
663 #endif
664 			if (strlen(d->d_name) >= MAXNAME)
665 				d->d_name[MAXNAME - 1] = '\0';
666 			strcpy(lbuf, d->d_name);
667 			lbuf[0] = 'Q';
668 			(void) rename(d->d_name, lbuf);
669 			continue;
670 		}
671 
672 		/* yes -- open control file (if not too many files) */
673 		if (++wn >= QUEUESIZE)
674 			continue;
675 
676 		cf = fopen(d->d_name, "r");
677 		if (cf == NULL)
678 		{
679 			/* this may be some random person sending hir msgs */
680 			/* syserr("orderq: cannot open %s", cbuf); */
681 			if (tTd(41, 2))
682 				printf("orderq: cannot open %s (%d)\n",
683 					d->d_name, errno);
684 			errno = 0;
685 			wn--;
686 			continue;
687 		}
688 		w = &wlist[wn];
689 		w->w_name = newstr(d->d_name);
690 
691 		/* make sure jobs in creation don't clog queue */
692 		w->w_pri = 0x7fffffff;
693 		w->w_ctime = 0;
694 
695 		/* extract useful information */
696 		i = NEED_P | NEED_T;
697 		if (QueueLimitSender != NULL)
698 			i |= NEED_S;
699 		if (QueueLimitRecipient != NULL)
700 			i |= NEED_R;
701 		while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL)
702 		{
703 			extern long atol();
704 			extern bool strcontainedin();
705 
706 			switch (lbuf[0])
707 			{
708 			  case 'P':
709 				w->w_pri = atol(&lbuf[1]);
710 				i &= ~NEED_P;
711 				break;
712 
713 			  case 'T':
714 				w->w_ctime = atol(&lbuf[1]);
715 				i &= ~NEED_T;
716 				break;
717 
718 			  case 'R':
719 				if (QueueLimitRecipient != NULL &&
720 				    strcontainedin(QueueLimitRecipient, &lbuf[1]))
721 					i &= ~NEED_R;
722 				break;
723 
724 			  case 'S':
725 				if (QueueLimitSender != NULL &&
726 				    strcontainedin(QueueLimitSender, &lbuf[1]))
727 					i &= ~NEED_S;
728 				break;
729 			}
730 		}
731 		(void) fclose(cf);
732 
733 		if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) ||
734 		    bitset(NEED_R|NEED_S, i))
735 		{
736 			/* don't even bother sorting this job in */
737 			wn--;
738 		}
739 	}
740 	(void) closedir(f);
741 	wn++;
742 
743 	/*
744 	**  Sort the work directory.
745 	*/
746 
747 	qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf);
748 
749 	/*
750 	**  Convert the work list into canonical form.
751 	**	Should be turning it into a list of envelopes here perhaps.
752 	*/
753 
754 	WorkQ = NULL;
755 	for (i = min(wn, QUEUESIZE); --i >= 0; )
756 	{
757 		w = (WORK *) xalloc(sizeof *w);
758 		w->w_name = wlist[i].w_name;
759 		w->w_pri = wlist[i].w_pri;
760 		w->w_ctime = wlist[i].w_ctime;
761 		w->w_next = WorkQ;
762 		WorkQ = w;
763 	}
764 
765 	if (tTd(40, 1))
766 	{
767 		for (w = WorkQ; w != NULL; w = w->w_next)
768 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
769 	}
770 
771 	return (wn);
772 }
773 /*
774 **  WORKCMPF -- compare function for ordering work.
775 **
776 **	Parameters:
777 **		a -- the first argument.
778 **		b -- the second argument.
779 **
780 **	Returns:
781 **		-1 if a < b
782 **		 0 if a == b
783 **		+1 if a > b
784 **
785 **	Side Effects:
786 **		none.
787 */
788 
789 workcmpf(a, b)
790 	register WORK *a;
791 	register WORK *b;
792 {
793 	long pa = a->w_pri;
794 	long pb = b->w_pri;
795 
796 	if (pa == pb)
797 		return (0);
798 	else if (pa > pb)
799 		return (1);
800 	else
801 		return (-1);
802 }
803 /*
804 **  DOWORK -- do a work request.
805 **
806 **	Parameters:
807 **		id -- the ID of the job to run.
808 **		forkflag -- if set, run this in background.
809 **		requeueflag -- if set, reinstantiate the queue quickly.
810 **			This is used when expanding aliases in the queue.
811 **			If forkflag is also set, it doesn't wait for the
812 **			child.
813 **		e - the envelope in which to run it.
814 **
815 **	Returns:
816 **		process id of process that is running the queue job.
817 **
818 **	Side Effects:
819 **		The work request is satisfied if possible.
820 */
821 
822 pid_t
823 dowork(id, forkflag, requeueflag, e)
824 	char *id;
825 	bool forkflag;
826 	bool requeueflag;
827 	register ENVELOPE *e;
828 {
829 	register pid_t pid;
830 	extern bool readqf();
831 
832 	if (tTd(40, 1))
833 		printf("dowork(%s)\n", id);
834 
835 	/*
836 	**  Fork for work.
837 	*/
838 
839 	if (forkflag)
840 	{
841 		pid = fork();
842 		if (pid < 0)
843 		{
844 			syserr("dowork: cannot fork");
845 			return 0;
846 		}
847 		else if (pid > 0)
848 		{
849 			/* parent -- clean out connection cache */
850 			mci_flush(FALSE, NULL);
851 		}
852 	}
853 	else
854 	{
855 		pid = 0;
856 	}
857 
858 	if (pid == 0)
859 	{
860 		/*
861 		**  CHILD
862 		**	Lock the control file to avoid duplicate deliveries.
863 		**		Then run the file as though we had just read it.
864 		**	We save an idea of the temporary name so we
865 		**		can recover on interrupt.
866 		*/
867 
868 		/* set basic modes, etc. */
869 		(void) alarm(0);
870 		clearenvelope(e, FALSE);
871 		e->e_flags |= EF_QUEUERUN|EF_GLOBALERRS;
872 		e->e_errormode = EM_MAIL;
873 		e->e_id = id;
874 		GrabTo = UseErrorsTo = FALSE;
875 		ExitStat = EX_OK;
876 		if (forkflag)
877 		{
878 			disconnect(1, e);
879 			OpMode = MD_DELIVER;
880 		}
881 # ifdef LOG
882 		if (LogLevel > 76)
883 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id,
884 			       getpid());
885 # endif /* LOG */
886 
887 		/* don't use the headers from sendmail.cf... */
888 		e->e_header = NULL;
889 
890 		/* read the queue control file -- return if locked */
891 		if (!readqf(e))
892 		{
893 			if (tTd(40, 4))
894 				printf("readqf(%s) failed\n", e->e_id);
895 			if (forkflag)
896 				exit(EX_OK);
897 			else
898 				return 0;
899 		}
900 
901 		e->e_flags |= EF_INQUEUE;
902 		eatheader(e, requeueflag);
903 
904 		if (requeueflag)
905 			queueup(e, TRUE, FALSE);
906 
907 		/* do the delivery */
908 		sendall(e, SM_DELIVER);
909 
910 		/* finish up and exit */
911 		if (forkflag)
912 			finis();
913 		else
914 			dropenvelope(e);
915 	}
916 	e->e_id = NULL;
917 	return pid;
918 }
919 /*
920 **  READQF -- read queue file and set up environment.
921 **
922 **	Parameters:
923 **		e -- the envelope of the job to run.
924 **
925 **	Returns:
926 **		TRUE if it successfully read the queue file.
927 **		FALSE otherwise.
928 **
929 **	Side Effects:
930 **		The queue file is returned locked.
931 */
932 
933 bool
934 readqf(e)
935 	register ENVELOPE *e;
936 {
937 	register FILE *qfp;
938 	ADDRESS *ctladdr;
939 	struct stat st;
940 	char *bp;
941 	char qf[20];
942 	char buf[MAXLINE];
943 	extern long atol();
944 	extern ADDRESS *setctluser();
945 
946 	/*
947 	**  Read and process the file.
948 	*/
949 
950 	strcpy(qf, queuename(e, 'q'));
951 	qfp = fopen(qf, "r+");
952 	if (qfp == NULL)
953 	{
954 		if (tTd(40, 8))
955 			printf("readqf(%s): fopen failure (%s)\n",
956 				qf, errstring(errno));
957 		if (errno != ENOENT)
958 			syserr("readqf: no control file %s", qf);
959 		return FALSE;
960 	}
961 
962 	if (!lockfile(fileno(qfp), qf, NULL, LOCK_EX|LOCK_NB))
963 	{
964 		/* being processed by another queuer */
965 		if (tTd(40, 8))
966 			printf("readqf(%s): locked\n", qf);
967 		if (Verbose)
968 			printf("%s: locked\n", e->e_id);
969 # ifdef LOG
970 		if (LogLevel > 19)
971 			syslog(LOG_DEBUG, "%s: locked", e->e_id);
972 # endif /* LOG */
973 		(void) fclose(qfp);
974 		return FALSE;
975 	}
976 
977 	/*
978 	**  Check the queue file for plausibility to avoid attacks.
979 	*/
980 
981 	if (fstat(fileno(qfp), &st) < 0)
982 	{
983 		/* must have been being processed by someone else */
984 		if (tTd(40, 8))
985 			printf("readqf(%s): fstat failure (%s)\n",
986 				qf, errstring(errno));
987 		fclose(qfp);
988 		return FALSE;
989 	}
990 
991 	if (st.st_uid != geteuid())
992 	{
993 # ifdef LOG
994 		if (LogLevel > 0)
995 		{
996 			syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o",
997 				e->e_id, st.st_uid, st.st_mode);
998 		}
999 # endif /* LOG */
1000 		if (tTd(40, 8))
1001 			printf("readqf(%s): bogus file\n", qf);
1002 		rename(qf, queuename(e, 'Q'));
1003 		fclose(qfp);
1004 		return FALSE;
1005 	}
1006 
1007 	if (st.st_size == 0)
1008 	{
1009 		/* must be a bogus file -- just remove it */
1010 		(void) unlink(qf);
1011 		fclose(qfp);
1012 		return FALSE;
1013 	}
1014 
1015 	if (st.st_nlink == 0)
1016 	{
1017 		/*
1018 		**  Race condition -- we got a file just as it was being
1019 		**  unlinked.  Just assume it is zero length.
1020 		*/
1021 
1022 		fclose(qfp);
1023 		return FALSE;
1024 	}
1025 
1026 	/* good file -- save this lock */
1027 	e->e_lockfp = qfp;
1028 
1029 	/* do basic system initialization */
1030 	initsys(e);
1031 	define('i', e->e_id, e);
1032 
1033 	LineNumber = 0;
1034 	e->e_flags |= EF_GLOBALERRS;
1035 	OpMode = MD_DELIVER;
1036 	if (Verbose)
1037 		printf("\nRunning %s\n", e->e_id);
1038 	ctladdr = NULL;
1039 	while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL)
1040 	{
1041 		register char *p;
1042 		struct stat st;
1043 
1044 		if (tTd(40, 4))
1045 			printf("+++++ %s\n", bp);
1046 		switch (bp[0])
1047 		{
1048 		  case 'C':		/* specify controlling user */
1049 			ctladdr = setctluser(&bp[1]);
1050 			break;
1051 
1052 		  case 'R':		/* specify recipient */
1053 			(void) sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e);
1054 			break;
1055 
1056 		  case 'E':		/* specify error recipient */
1057 			(void) sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e);
1058 			break;
1059 
1060 		  case 'H':		/* header */
1061 			(void) chompheader(&bp[1], FALSE, e);
1062 			break;
1063 
1064 		  case 'M':		/* message */
1065 			/* ignore this; we want a new message next time */
1066 			break;
1067 
1068 		  case 'S':		/* sender */
1069 			setsender(newstr(&bp[1]), e, NULL, TRUE);
1070 			break;
1071 
1072 		  case 'B':		/* body type */
1073 			e->e_bodytype = newstr(&bp[1]);
1074 			break;
1075 
1076 		  case 'D':		/* data file name */
1077 			e->e_df = newstr(&bp[1]);
1078 			e->e_dfp = fopen(e->e_df, "r");
1079 			if (e->e_dfp == NULL)
1080 			{
1081 				syserr("readqf: cannot open %s", e->e_df);
1082 				e->e_msgsize = -1;
1083 			}
1084 			else if (fstat(fileno(e->e_dfp), &st) >= 0)
1085 				e->e_msgsize = st.st_size;
1086 			break;
1087 
1088 		  case 'T':		/* init time */
1089 			e->e_ctime = atol(&bp[1]);
1090 			break;
1091 
1092 		  case 'P':		/* message priority */
1093 			e->e_msgpriority = atol(&bp[1]) + WkTimeFact;
1094 			break;
1095 
1096 		  case 'F':		/* flag bits */
1097 			for (p = &bp[1]; *p != '\0'; p++)
1098 			{
1099 				switch (*p)
1100 				{
1101 				  case 'w':	/* warning sent */
1102 					e->e_flags |= EF_WARNING;
1103 					break;
1104 
1105 				  case 'r':	/* response */
1106 					e->e_flags |= EF_RESPONSE;
1107 					break;
1108 				}
1109 			}
1110 			break;
1111 
1112 		  case '$':		/* define macro */
1113 			define(bp[1], newstr(&bp[2]), e);
1114 			break;
1115 
1116 		  case '\0':		/* blank line; ignore */
1117 			break;
1118 
1119 		  default:
1120 			syserr("readqf: %s: line %d: bad line \"%s\"",
1121 				qf, LineNumber, bp);
1122 			fclose(qfp);
1123 			rename(qf, queuename(e, 'Q'));
1124 			return FALSE;
1125 		}
1126 
1127 		if (bp != buf)
1128 			free(bp);
1129 	}
1130 
1131 	/*
1132 	**  If we haven't read any lines, this queue file is empty.
1133 	**  Arrange to remove it without referencing any null pointers.
1134 	*/
1135 
1136 	if (LineNumber == 0)
1137 	{
1138 		errno = 0;
1139 		e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE;
1140 	}
1141 	return TRUE;
1142 }
1143 /*
1144 **  PRINTQUEUE -- print out a representation of the mail queue
1145 **
1146 **	Parameters:
1147 **		none.
1148 **
1149 **	Returns:
1150 **		none.
1151 **
1152 **	Side Effects:
1153 **		Prints a listing of the mail queue on the standard output.
1154 */
1155 
1156 printqueue()
1157 {
1158 	register WORK *w;
1159 	FILE *f;
1160 	int nrequests;
1161 	char buf[MAXLINE];
1162 
1163 	/*
1164 	**  Check for permission to print the queue
1165 	*/
1166 
1167 	if (bitset(PRIV_RESTRICTMAILQ, PrivacyFlags) && RealUid != 0)
1168 	{
1169 		struct stat st;
1170 # ifdef NGROUPS
1171 		int n;
1172 		GIDSET_T gidset[NGROUPS];
1173 # endif
1174 
1175 		if (stat(QueueDir, &st) < 0)
1176 		{
1177 			syserr("Cannot stat %s", QueueDir);
1178 			return;
1179 		}
1180 # ifdef NGROUPS
1181 		n = getgroups(NGROUPS, gidset);
1182 		while (--n >= 0)
1183 		{
1184 			if (gidset[n] == st.st_gid)
1185 				break;
1186 		}
1187 		if (n < 0)
1188 # else
1189 		if (RealGid != st.st_gid)
1190 # endif
1191 		{
1192 			usrerr("510 You are not permitted to see the queue");
1193 			setstat(EX_NOPERM);
1194 			return;
1195 		}
1196 	}
1197 
1198 	/*
1199 	**  Read and order the queue.
1200 	*/
1201 
1202 	nrequests = orderq(TRUE);
1203 
1204 	/*
1205 	**  Print the work list that we have read.
1206 	*/
1207 
1208 	/* first see if there is anything */
1209 	if (nrequests <= 0)
1210 	{
1211 		printf("Mail queue is empty\n");
1212 		return;
1213 	}
1214 
1215 	CurrentLA = getla();	/* get load average */
1216 
1217 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
1218 	if (nrequests > QUEUESIZE)
1219 		printf(", only %d printed", QUEUESIZE);
1220 	if (Verbose)
1221 		printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n");
1222 	else
1223 		printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
1224 	for (w = WorkQ; w != NULL; w = w->w_next)
1225 	{
1226 		struct stat st;
1227 		auto time_t submittime = 0;
1228 		long dfsize = -1;
1229 		int flags = 0;
1230 		char message[MAXLINE];
1231 		char bodytype[MAXNAME];
1232 
1233 		printf("%8s", w->w_name + 2);
1234 		f = fopen(w->w_name, "r");
1235 		if (f == NULL)
1236 		{
1237 			printf(" (job completed)\n");
1238 			errno = 0;
1239 			continue;
1240 		}
1241 		if (!lockfile(fileno(f), w->w_name, NULL, LOCK_SH|LOCK_NB))
1242 			printf("*");
1243 		else if (shouldqueue(w->w_pri, w->w_ctime))
1244 			printf("X");
1245 		else
1246 			printf(" ");
1247 		errno = 0;
1248 
1249 		message[0] = bodytype[0] = '\0';
1250 		while (fgets(buf, sizeof buf, f) != NULL)
1251 		{
1252 			register int i;
1253 			register char *p;
1254 
1255 			fixcrlf(buf, TRUE);
1256 			switch (buf[0])
1257 			{
1258 			  case 'M':	/* error message */
1259 				if ((i = strlen(&buf[1])) >= sizeof message)
1260 					i = sizeof message - 1;
1261 				bcopy(&buf[1], message, i);
1262 				message[i] = '\0';
1263 				break;
1264 
1265 			  case 'B':	/* body type */
1266 				if ((i = strlen(&buf[1])) >= sizeof bodytype)
1267 					i = sizeof bodytype - 1;
1268 				bcopy(&buf[1], bodytype, i);
1269 				bodytype[i] = '\0';
1270 				break;
1271 
1272 			  case 'S':	/* sender name */
1273 				if (Verbose)
1274 					printf("%8ld %10ld%c%.12s %.38s",
1275 					    dfsize,
1276 					    w->w_pri,
1277 					    bitset(EF_WARNING, flags) ? '+' : ' ',
1278 					    ctime(&submittime) + 4,
1279 					    &buf[1]);
1280 				else
1281 					printf("%8ld %.16s %.45s", dfsize,
1282 					    ctime(&submittime), &buf[1]);
1283 				if (message[0] != '\0' || bodytype[0] != '\0')
1284 				{
1285 					printf("\n    %10.10s", bodytype);
1286 					if (message[0] != '\0')
1287 						printf("   (%.60s)", message);
1288 				}
1289 				break;
1290 
1291 			  case 'C':	/* controlling user */
1292 				if (Verbose)
1293 					printf("\n\t\t\t\t      (---%.34s---)",
1294 						&buf[1]);
1295 				break;
1296 
1297 			  case 'R':	/* recipient name */
1298 				if (Verbose)
1299 					printf("\n\t\t\t\t\t  %.38s", &buf[1]);
1300 				else
1301 					printf("\n\t\t\t\t   %.45s", &buf[1]);
1302 				break;
1303 
1304 			  case 'T':	/* creation time */
1305 				submittime = atol(&buf[1]);
1306 				break;
1307 
1308 			  case 'D':	/* data file name */
1309 				if (stat(&buf[1], &st) >= 0)
1310 					dfsize = st.st_size;
1311 				break;
1312 
1313 			  case 'F':	/* flag bits */
1314 				for (p = &buf[1]; *p != '\0'; p++)
1315 				{
1316 					switch (*p)
1317 					{
1318 					  case 'w':
1319 						flags |= EF_WARNING;
1320 						break;
1321 					}
1322 				}
1323 			}
1324 		}
1325 		if (submittime == (time_t) 0)
1326 			printf(" (no control file)");
1327 		printf("\n");
1328 		(void) fclose(f);
1329 	}
1330 }
1331 
1332 # endif /* QUEUE */
1333 /*
1334 **  QUEUENAME -- build a file name in the queue directory for this envelope.
1335 **
1336 **	Assigns an id code if one does not already exist.
1337 **	This code is very careful to avoid trashing existing files
1338 **	under any circumstances.
1339 **
1340 **	Parameters:
1341 **		e -- envelope to build it in/from.
1342 **		type -- the file type, used as the first character
1343 **			of the file name.
1344 **
1345 **	Returns:
1346 **		a pointer to the new file name (in a static buffer).
1347 **
1348 **	Side Effects:
1349 **		If no id code is already assigned, queuename will
1350 **		assign an id code, create a qf file, and leave a
1351 **		locked, open-for-write file pointer in the envelope.
1352 */
1353 
1354 char *
1355 queuename(e, type)
1356 	register ENVELOPE *e;
1357 	int type;
1358 {
1359 	static int pid = -1;
1360 	static char c0;
1361 	static char c1;
1362 	static char c2;
1363 	time_t now;
1364 	struct tm *tm;
1365 	static char buf[MAXNAME];
1366 
1367 	if (e->e_id == NULL)
1368 	{
1369 		char qf[20];
1370 
1371 		/* find a unique id */
1372 		if (pid != getpid())
1373 		{
1374 			/* new process -- start back at "AA" */
1375 			pid = getpid();
1376 			now = curtime();
1377 			tm = localtime(&now);
1378 			c0 = 'A' + tm->tm_hour;
1379 			c1 = 'A';
1380 			c2 = 'A' - 1;
1381 		}
1382 		(void) sprintf(qf, "qf%cAA%05d", c0, pid);
1383 
1384 		while (c1 < '~' || c2 < 'Z')
1385 		{
1386 			int i;
1387 
1388 			if (c2 >= 'Z')
1389 			{
1390 				c1++;
1391 				c2 = 'A' - 1;
1392 			}
1393 			qf[3] = c1;
1394 			qf[4] = ++c2;
1395 			if (tTd(7, 20))
1396 				printf("queuename: trying \"%s\"\n", qf);
1397 
1398 			i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode);
1399 			if (i < 0)
1400 			{
1401 				if (errno == EEXIST)
1402 					continue;
1403 				syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)",
1404 					qf, QueueDir, geteuid());
1405 				exit(EX_UNAVAILABLE);
1406 			}
1407 			if (lockfile(i, qf, NULL, LOCK_EX|LOCK_NB))
1408 			{
1409 				e->e_lockfp = fdopen(i, "w");
1410 				break;
1411 			}
1412 
1413 			/* a reader got the file; abandon it and try again */
1414 			(void) close(i);
1415 		}
1416 		if (c1 >= '~' && c2 >= 'Z')
1417 		{
1418 			syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)",
1419 				qf, QueueDir, geteuid());
1420 			exit(EX_OSERR);
1421 		}
1422 		e->e_id = newstr(&qf[2]);
1423 		define('i', e->e_id, e);
1424 		if (tTd(7, 1))
1425 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
1426 		if (tTd(7, 9))
1427 		{
1428 			printf("  lockfd=");
1429 			dumpfd(fileno(e->e_lockfp), TRUE, FALSE);
1430 		}
1431 # ifdef LOG
1432 		if (LogLevel > 93)
1433 			syslog(LOG_DEBUG, "%s: assigned id", e->e_id);
1434 # endif /* LOG */
1435 	}
1436 
1437 	if (type == '\0')
1438 		return (NULL);
1439 	(void) sprintf(buf, "%cf%s", type, e->e_id);
1440 	if (tTd(7, 2))
1441 		printf("queuename: %s\n", buf);
1442 	return (buf);
1443 }
1444 /*
1445 **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
1446 **
1447 **	Parameters:
1448 **		e -- the envelope to unlock.
1449 **
1450 **	Returns:
1451 **		none
1452 **
1453 **	Side Effects:
1454 **		unlocks the queue for `e'.
1455 */
1456 
1457 unlockqueue(e)
1458 	ENVELOPE *e;
1459 {
1460 	if (tTd(51, 4))
1461 		printf("unlockqueue(%s)\n", e->e_id);
1462 
1463 	/* if there is a lock file in the envelope, close it */
1464 	if (e->e_lockfp != NULL)
1465 		xfclose(e->e_lockfp, "unlockqueue", e->e_id);
1466 	e->e_lockfp = NULL;
1467 
1468 	/* don't create a queue id if we don't already have one */
1469 	if (e->e_id == NULL)
1470 		return;
1471 
1472 	/* remove the transcript */
1473 # ifdef LOG
1474 	if (LogLevel > 87)
1475 		syslog(LOG_DEBUG, "%s: unlock", e->e_id);
1476 # endif /* LOG */
1477 	if (!tTd(51, 104))
1478 		xunlink(queuename(e, 'x'));
1479 
1480 }
1481 /*
1482 **  SETCTLUSER -- create a controlling address
1483 **
1484 **	Create a fake "address" given only a local login name; this is
1485 **	used as a "controlling user" for future recipient addresses.
1486 **
1487 **	Parameters:
1488 **		user -- the user name of the controlling user.
1489 **
1490 **	Returns:
1491 **		An address descriptor for the controlling user.
1492 **
1493 **	Side Effects:
1494 **		none.
1495 */
1496 
1497 ADDRESS *
1498 setctluser(user)
1499 	char *user;
1500 {
1501 	register ADDRESS *a;
1502 	struct passwd *pw;
1503 	char *p;
1504 
1505 	/*
1506 	**  See if this clears our concept of controlling user.
1507 	*/
1508 
1509 	if (user == NULL || *user == '\0')
1510 		return NULL;
1511 
1512 	/*
1513 	**  Set up addr fields for controlling user.
1514 	*/
1515 
1516 	a = (ADDRESS *) xalloc(sizeof *a);
1517 	bzero((char *) a, sizeof *a);
1518 
1519 	p = strchr(user, ':');
1520 	if (p != NULL)
1521 		*p++ = '\0';
1522 	if (*user != '\0' && (pw = getpwnam(user)) != NULL)
1523 	{
1524 		if (strcmp(pw->pw_dir, "/") == 0)
1525 			a->q_home = "";
1526 		else
1527 			a->q_home = newstr(pw->pw_dir);
1528 		a->q_uid = pw->pw_uid;
1529 		a->q_gid = pw->pw_gid;
1530 		a->q_user = newstr(user);
1531 		a->q_flags |= QGOODUID;
1532 	}
1533 	else
1534 	{
1535 		a->q_user = newstr(DefUser);
1536 	}
1537 
1538 	a->q_flags |= QPRIMARY;		/* flag as a "ctladdr"  */
1539 	a->q_mailer = LocalMailer;
1540 	if (p == NULL)
1541 		a->q_paddr = a->q_user;
1542 	else
1543 		a->q_paddr = newstr(p);
1544 	return a;
1545 }
1546