xref: /illumos-gate/usr/src/cmd/sendmail/src/queue.c (revision 4aac33d3)
1 /*
2  * Copyright (c) 1998-2007 Sendmail, Inc. and its suppliers.
3  *	All rights reserved.
4  * Copyright (c) 1983, 1995-1997 Eric P. Allman.  All rights reserved.
5  * Copyright (c) 1988, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * By using this file, you agree to the terms and conditions set
9  * forth in the LICENSE file which can be found at the top level of
10  * the sendmail distribution.
11  *
12  */
13 
14 #pragma ident	"%Z%%M%	%I%	%E% SMI"
15 
16 #include <sendmail.h>
17 #include <sm/sem.h>
18 
19 SM_RCSID("@(#)$Id: queue.c,v 8.972 2007/03/29 22:55:17 ca Exp $")
20 
21 #include <dirent.h>
22 
23 # define RELEASE_QUEUE	(void) 0
24 # define ST_INODE(st)	(st).st_ino
25 
26 #  define sm_file_exists(errno) ((errno) == EEXIST)
27 
28 # if HASFLOCK && defined(O_EXLOCK)
29 #   define SM_OPEN_EXLOCK 1
30 #   define TF_OPEN_FLAGS (O_CREAT|O_WRONLY|O_EXCL|O_EXLOCK)
31 # else /* HASFLOCK && defined(O_EXLOCK) */
32 #  define TF_OPEN_FLAGS (O_CREAT|O_WRONLY|O_EXCL)
33 # endif /* HASFLOCK && defined(O_EXLOCK) */
34 
35 #ifndef SM_OPEN_EXLOCK
36 # define SM_OPEN_EXLOCK 0
37 #endif /* ! SM_OPEN_EXLOCK */
38 
39 /*
40 **  Historical notes:
41 **	QF_VERSION == 4 was sendmail 8.10/8.11 without _FFR_QUEUEDELAY
42 **	QF_VERSION == 5 was sendmail 8.10/8.11 with    _FFR_QUEUEDELAY
43 **	QF_VERSION == 6 was sendmail 8.12      without _FFR_QUEUEDELAY
44 **	QF_VERSION == 7 was sendmail 8.12      with    _FFR_QUEUEDELAY
45 **	QF_VERSION == 8 is  sendmail 8.13
46 */
47 
48 #define QF_VERSION	8	/* version number of this queue format */
49 
50 static char	queue_letter __P((ENVELOPE *, int));
51 static bool	quarantine_queue_item __P((int, int, ENVELOPE *, char *));
52 
53 /* Naming convention: qgrp: index of queue group, qg: QUEUEGROUP */
54 
55 /*
56 **  Work queue.
57 */
58 
59 struct work
60 {
61 	char		*w_name;	/* name of control file */
62 	char		*w_host;	/* name of recipient host */
63 	bool		w_lock;		/* is message locked? */
64 	bool		w_tooyoung;	/* is it too young to run? */
65 	long		w_pri;		/* priority of message, see below */
66 	time_t		w_ctime;	/* creation time */
67 	time_t		w_mtime;	/* modification time */
68 	int		w_qgrp;		/* queue group located in */
69 	int		w_qdir;		/* queue directory located in */
70 	struct work	*w_next;	/* next in queue */
71 };
72 
73 typedef struct work	WORK;
74 
75 static WORK	*WorkQ;		/* queue of things to be done */
76 static int	NumWorkGroups;	/* number of work groups */
77 static time_t	Current_LA_time = 0;
78 
79 /* Get new load average every 30 seconds. */
80 #define GET_NEW_LA_TIME	30
81 
82 #define SM_GET_LA(now)	\
83 	do							\
84 	{							\
85 		now = curtime();				\
86 		if (Current_LA_time < now - GET_NEW_LA_TIME)	\
87 		{						\
88 			sm_getla();				\
89 			Current_LA_time = now;			\
90 		}						\
91 	} while (0)
92 
93 /*
94 **  DoQueueRun indicates that a queue run is needed.
95 **	Notice: DoQueueRun is modified in a signal handler!
96 */
97 
98 static bool	volatile DoQueueRun; /* non-interrupt time queue run needed */
99 
100 /*
101 **  Work group definition structure.
102 **	Each work group contains one or more queue groups. This is done
103 **	to manage the number of queue group runners active at the same time
104 **	to be within the constraints of MaxQueueChildren (if it is set).
105 **	The number of queue groups that can be run on the next work run
106 **	is kept track of. The queue groups are run in a round robin.
107 */
108 
109 struct workgrp
110 {
111 	int		wg_numqgrp;	/* number of queue groups in work grp */
112 	int		wg_runners;	/* total runners */
113 	int		wg_curqgrp;	/* current queue group */
114 	QUEUEGRP	**wg_qgs;	/* array of queue groups */
115 	int		wg_maxact;	/* max # of active runners */
116 	time_t		wg_lowqintvl;	/* lowest queue interval */
117 	int		wg_restart;	/* needs restarting? */
118 	int		wg_restartcnt;	/* count of times restarted */
119 };
120 
121 typedef struct workgrp WORKGRP;
122 
123 static WORKGRP	volatile WorkGrp[MAXWORKGROUPS + 1];	/* work groups */
124 
125 #if SM_HEAP_CHECK
126 static SM_DEBUG_T DebugLeakQ = SM_DEBUG_INITIALIZER("leak_q",
127 	"@(#)$Debug: leak_q - trace memory leaks during queue processing $");
128 #endif /* SM_HEAP_CHECK */
129 
130 /*
131 **  We use EmptyString instead of "" to avoid
132 **  'zero-length format string' warnings from gcc
133 */
134 
135 static const char EmptyString[] = "";
136 
137 static void	grow_wlist __P((int, int));
138 static int	multiqueue_cache __P((char *, int, QUEUEGRP *, int, unsigned int *));
139 static int	gatherq __P((int, int, bool, bool *, bool *));
140 static int	sortq __P((int));
141 static void	printctladdr __P((ADDRESS *, SM_FILE_T *));
142 static bool	readqf __P((ENVELOPE *, bool));
143 static void	restart_work_group __P((int));
144 static void	runner_work __P((ENVELOPE *, int, bool, int, int));
145 static void	schedule_queue_runs __P((bool, int, bool));
146 static char	*strrev __P((char *));
147 static ADDRESS	*setctluser __P((char *, int, ENVELOPE *));
148 #if _FFR_RHS
149 static int	sm_strshufflecmp __P((char *, char *));
150 static void	init_shuffle_alphabet __P(());
151 #endif /* _FFR_RHS */
152 
153 /*
154 **  Note: workcmpf?() don't use a prototype because it will cause a conflict
155 **  with the qsort() call (which expects something like
156 **  int (*compar)(const void *, const void *), not (WORK *, WORK *))
157 */
158 
159 static int	workcmpf0();
160 static int	workcmpf1();
161 static int	workcmpf2();
162 static int	workcmpf3();
163 static int	workcmpf4();
164 static int	randi = 3;	/* index for workcmpf5() */
165 static int	workcmpf5();
166 static int	workcmpf6();
167 #if _FFR_RHS
168 static int	workcmpf7();
169 #endif /* _FFR_RHS */
170 
171 #if RANDOMSHIFT
172 # define get_rand_mod(m)	((get_random() >> RANDOMSHIFT) % (m))
173 #else /* RANDOMSHIFT */
174 # define get_rand_mod(m)	(get_random() % (m))
175 #endif /* RANDOMSHIFT */
176 
177 /*
178 **  File system definition.
179 **	Used to keep track of how much free space is available
180 **	on a file system in which one or more queue directories reside.
181 */
182 
183 typedef struct filesys_shared	FILESYS;
184 
185 struct filesys_shared
186 {
187 	dev_t	fs_dev;		/* unique device id */
188 	long	fs_avail;	/* number of free blocks available */
189 	long	fs_blksize;	/* block size, in bytes */
190 };
191 
192 /* probably kept in shared memory */
193 static FILESYS	FileSys[MAXFILESYS];	/* queue file systems */
194 static const char *FSPath[MAXFILESYS];	/* pathnames for file systems */
195 
196 #if SM_CONF_SHM
197 
198 /*
199 **  Shared memory data
200 **
201 **  Current layout:
202 **	size -- size of shared memory segment
203 **	pid -- pid of owner, should be a unique id to avoid misinterpretations
204 **		by other processes.
205 **	tag -- should be a unique id to avoid misinterpretations by others.
206 **		idea: hash over configuration data that will be stored here.
207 **	NumFileSys -- number of file systems.
208 **	FileSys -- (arrary of) structure for used file systems.
209 **	RSATmpCnt -- counter for number of uses of ephemeral RSA key.
210 **	QShm -- (array of) structure for information about queue directories.
211 */
212 
213 /*
214 **  Queue data in shared memory
215 */
216 
217 typedef struct queue_shared	QUEUE_SHM_T;
218 
219 struct queue_shared
220 {
221 	int	qs_entries;	/* number of entries */
222 	/* XXX more to follow? */
223 };
224 
225 static void	*Pshm;		/* pointer to shared memory */
226 static FILESYS	*PtrFileSys;	/* pointer to queue file system array */
227 int		ShmId = SM_SHM_NO_ID;	/* shared memory id */
228 static QUEUE_SHM_T	*QShm;		/* pointer to shared queue data */
229 static size_t shms;
230 
231 # define SHM_OFF_PID(p)	(((char *) (p)) + sizeof(int))
232 # define SHM_OFF_TAG(p)	(((char *) (p)) + sizeof(pid_t) + sizeof(int))
233 # define SHM_OFF_HEAD	(sizeof(pid_t) + sizeof(int) * 2)
234 
235 /* how to access FileSys */
236 # define FILE_SYS(i)	(PtrFileSys[i])
237 
238 /* first entry is a tag, for now just the size */
239 # define OFF_FILE_SYS(p)	(((char *) (p)) + SHM_OFF_HEAD)
240 
241 /* offset for PNumFileSys */
242 # define OFF_NUM_FILE_SYS(p)	(((char *) (p)) + SHM_OFF_HEAD + sizeof(FileSys))
243 
244 /* offset for PRSATmpCnt */
245 # define OFF_RSA_TMP_CNT(p) (((char *) (p)) + SHM_OFF_HEAD + sizeof(FileSys) + sizeof(int))
246 int	*PRSATmpCnt;
247 
248 /* offset for queue_shm */
249 # define OFF_QUEUE_SHM(p) (((char *) (p)) + SHM_OFF_HEAD + sizeof(FileSys) + sizeof(int) * 2)
250 
251 # define QSHM_ENTRIES(i)	QShm[i].qs_entries
252 
253 /* basic size of shared memory segment */
254 # define SM_T_SIZE	(SHM_OFF_HEAD + sizeof(FileSys) + sizeof(int) * 2)
255 
256 static unsigned int	hash_q __P((char *, unsigned int));
257 
258 /*
259 **  HASH_Q -- simple hash function
260 **
261 **	Parameters:
262 **		p -- string to hash.
263 **		h -- hash start value (from previous run).
264 **
265 **	Returns:
266 **		hash value.
267 */
268 
269 static unsigned int
270 hash_q(p, h)
271 	char *p;
272 	unsigned int h;
273 {
274 	int c, d;
275 
276 	while (*p != '\0')
277 	{
278 		d = *p++;
279 		c = d;
280 		c ^= c<<6;
281 		h += (c<<11) ^ (c>>1);
282 		h ^= (d<<14) + (d<<7) + (d<<4) + d;
283 	}
284 	return h;
285 }
286 
287 
288 #else /* SM_CONF_SHM */
289 # define FILE_SYS(i)	FileSys[i]
290 #endif /* SM_CONF_SHM */
291 
292 /* access to the various components of file system data */
293 #define FILE_SYS_NAME(i)	FSPath[i]
294 #define FILE_SYS_AVAIL(i)	FILE_SYS(i).fs_avail
295 #define FILE_SYS_BLKSIZE(i)	FILE_SYS(i).fs_blksize
296 #define FILE_SYS_DEV(i)	FILE_SYS(i).fs_dev
297 
298 
299 /*
300 **  Current qf file field assignments:
301 **
302 **	A	AUTH= parameter
303 **	B	body type
304 **	C	controlling user
305 **	D	data file name
306 **	d	data file directory name (added in 8.12)
307 **	E	error recipient
308 **	F	flag bits
309 **	G	free (was: queue delay algorithm if _FFR_QUEUEDELAY)
310 **	H	header
311 **	I	data file's inode number
312 **	K	time of last delivery attempt
313 **	L	Solaris Content-Length: header (obsolete)
314 **	M	message
315 **	N	number of delivery attempts
316 **	P	message priority
317 **	q	quarantine reason
318 **	Q	original recipient (ORCPT=)
319 **	r	final recipient (Final-Recipient: DSN field)
320 **	R	recipient
321 **	S	sender
322 **	T	init time
323 **	V	queue file version
324 **	X	free (was: character set if _FFR_SAVE_CHARSET)
325 **	Y	free (was: current delay if _FFR_QUEUEDELAY)
326 **	Z	original envelope id from ESMTP
327 **	!	deliver by (added in 8.12)
328 **	$	define macro
329 **	.	terminate file
330 */
331 
332 /*
333 **  QUEUEUP -- queue a message up for future transmission.
334 **
335 **	Parameters:
336 **		e -- the envelope to queue up.
337 **		announce -- if true, tell when you are queueing up.
338 **		msync -- if true, then fsync() if SuperSafe interactive mode.
339 **
340 **	Returns:
341 **		none.
342 **
343 **	Side Effects:
344 **		The current request is saved in a control file.
345 **		The queue file is left locked.
346 */
347 
348 void
349 queueup(e, announce, msync)
350 	register ENVELOPE *e;
351 	bool announce;
352 	bool msync;
353 {
354 	register SM_FILE_T *tfp;
355 	register HDR *h;
356 	register ADDRESS *q;
357 	int tfd = -1;
358 	int i;
359 	bool newid;
360 	register char *p;
361 	MAILER nullmailer;
362 	MCI mcibuf;
363 	char qf[MAXPATHLEN];
364 	char tf[MAXPATHLEN];
365 	char df[MAXPATHLEN];
366 	char buf[MAXLINE];
367 
368 	/*
369 	**  Create control file.
370 	*/
371 
372 #define OPEN_TF	do							\
373 		{							\
374 			MODE_T oldumask = 0;				\
375 									\
376 			if (bitset(S_IWGRP, QueueFileMode))		\
377 				oldumask = umask(002);			\
378 			tfd = open(tf, TF_OPEN_FLAGS, QueueFileMode);	\
379 			if (bitset(S_IWGRP, QueueFileMode))		\
380 				(void) umask(oldumask);			\
381 		} while (0)
382 
383 
384 	newid = (e->e_id == NULL) || !bitset(EF_INQUEUE, e->e_flags);
385 	(void) sm_strlcpy(tf, queuename(e, NEWQFL_LETTER), sizeof(tf));
386 	tfp = e->e_lockfp;
387 	if (tfp == NULL && newid)
388 	{
389 		/*
390 		**  open qf file directly: this will give an error if the file
391 		**  already exists and hence prevent problems if a queue-id
392 		**  is reused (e.g., because the clock is set back).
393 		*/
394 
395 		(void) sm_strlcpy(tf, queuename(e, ANYQFL_LETTER), sizeof(tf));
396 		OPEN_TF;
397 		if (tfd < 0 ||
398 #if !SM_OPEN_EXLOCK
399 		    !lockfile(tfd, tf, NULL, LOCK_EX|LOCK_NB) ||
400 #endif /* !SM_OPEN_EXLOCK */
401 		    (tfp = sm_io_open(SmFtStdiofd, SM_TIME_DEFAULT,
402 					 (void *) &tfd, SM_IO_WRONLY,
403 					 NULL)) == NULL)
404 		{
405 			int save_errno = errno;
406 
407 			printopenfds(true);
408 			errno = save_errno;
409 			syserr("!queueup: cannot create queue file %s, euid=%d, fd=%d, fp=%p",
410 				tf, (int) geteuid(), tfd, tfp);
411 			/* NOTREACHED */
412 		}
413 		e->e_lockfp = tfp;
414 		upd_qs(e, 1, 0, "queueup");
415 	}
416 
417 	/* if newid, write the queue file directly (instead of temp file) */
418 	if (!newid)
419 	{
420 		/* get a locked tf file */
421 		for (i = 0; i < 128; i++)
422 		{
423 			if (tfd < 0)
424 			{
425 				OPEN_TF;
426 				if (tfd < 0)
427 				{
428 					if (errno != EEXIST)
429 						break;
430 					if (LogLevel > 0 && (i % 32) == 0)
431 						sm_syslog(LOG_ALERT, e->e_id,
432 							  "queueup: cannot create %s, uid=%d: %s",
433 							  tf, (int) geteuid(),
434 							  sm_errstring(errno));
435 				}
436 #if SM_OPEN_EXLOCK
437 				else
438 					break;
439 #endif /* SM_OPEN_EXLOCK */
440 			}
441 			if (tfd >= 0)
442 			{
443 #if SM_OPEN_EXLOCK
444 				/* file is locked by open() */
445 				break;
446 #else /* SM_OPEN_EXLOCK */
447 				if (lockfile(tfd, tf, NULL, LOCK_EX|LOCK_NB))
448 					break;
449 				else
450 #endif /* SM_OPEN_EXLOCK */
451 				if (LogLevel > 0 && (i % 32) == 0)
452 					sm_syslog(LOG_ALERT, e->e_id,
453 						  "queueup: cannot lock %s: %s",
454 						  tf, sm_errstring(errno));
455 				if ((i % 32) == 31)
456 				{
457 					(void) close(tfd);
458 					tfd = -1;
459 				}
460 			}
461 
462 			if ((i % 32) == 31)
463 			{
464 				/* save the old temp file away */
465 				(void) rename(tf, queuename(e, TEMPQF_LETTER));
466 			}
467 			else
468 				(void) sleep(i % 32);
469 		}
470 		if (tfd < 0 || (tfp = sm_io_open(SmFtStdiofd, SM_TIME_DEFAULT,
471 						 (void *) &tfd, SM_IO_WRONLY_B,
472 						 NULL)) == NULL)
473 		{
474 			int save_errno = errno;
475 
476 			printopenfds(true);
477 			errno = save_errno;
478 			syserr("!queueup: cannot create queue temp file %s, uid=%d",
479 				tf, (int) geteuid());
480 		}
481 	}
482 
483 	if (tTd(40, 1))
484 		sm_dprintf("\n>>>>> queueing %s/%s%s >>>>>\n",
485 			   qid_printqueue(e->e_qgrp, e->e_qdir),
486 			   queuename(e, ANYQFL_LETTER),
487 			   newid ? " (new id)" : "");
488 	if (tTd(40, 3))
489 	{
490 		sm_dprintf("  e_flags=");
491 		printenvflags(e);
492 	}
493 	if (tTd(40, 32))
494 	{
495 		sm_dprintf("  sendq=");
496 		printaddr(sm_debug_file(), e->e_sendqueue, true);
497 	}
498 	if (tTd(40, 9))
499 	{
500 		sm_dprintf("  tfp=");
501 		dumpfd(sm_io_getinfo(tfp, SM_IO_WHAT_FD, NULL), true, false);
502 		sm_dprintf("  lockfp=");
503 		if (e->e_lockfp == NULL)
504 			sm_dprintf("NULL\n");
505 		else
506 			dumpfd(sm_io_getinfo(e->e_lockfp, SM_IO_WHAT_FD, NULL),
507 			       true, false);
508 	}
509 
510 	/*
511 	**  If there is no data file yet, create one.
512 	*/
513 
514 	(void) sm_strlcpy(df, queuename(e, DATAFL_LETTER), sizeof(df));
515 	if (bitset(EF_HAS_DF, e->e_flags))
516 	{
517 		if (e->e_dfp != NULL &&
518 		    SuperSafe != SAFE_REALLY &&
519 		    SuperSafe != SAFE_REALLY_POSTMILTER &&
520 		    sm_io_setinfo(e->e_dfp, SM_BF_COMMIT, NULL) < 0 &&
521 		    errno != EINVAL)
522 		{
523 			syserr("!queueup: cannot commit data file %s, uid=%d",
524 			       queuename(e, DATAFL_LETTER), (int) geteuid());
525 		}
526 		if (e->e_dfp != NULL &&
527 		    SuperSafe == SAFE_INTERACTIVE && msync)
528 		{
529 			if (tTd(40,32))
530 				sm_syslog(LOG_INFO, e->e_id,
531 					  "queueup: fsync(e->e_dfp)");
532 
533 			if (fsync(sm_io_getinfo(e->e_dfp, SM_IO_WHAT_FD,
534 						NULL)) < 0)
535 			{
536 				if (newid)
537 					syserr("!552 Error writing data file %s",
538 					       df);
539 				else
540 					syserr("!452 Error writing data file %s",
541 					       df);
542 			}
543 		}
544 	}
545 	else
546 	{
547 		int dfd;
548 		MODE_T oldumask = 0;
549 		register SM_FILE_T *dfp = NULL;
550 		struct stat stbuf;
551 
552 		if (e->e_dfp != NULL &&
553 		    sm_io_getinfo(e->e_dfp, SM_IO_WHAT_ISTYPE, BF_FILE_TYPE))
554 			syserr("committing over bf file");
555 
556 		if (bitset(S_IWGRP, QueueFileMode))
557 			oldumask = umask(002);
558 		dfd = open(df, O_WRONLY|O_CREAT|O_TRUNC|QF_O_EXTRA,
559 			   QueueFileMode);
560 		if (bitset(S_IWGRP, QueueFileMode))
561 			(void) umask(oldumask);
562 		if (dfd < 0 || (dfp = sm_io_open(SmFtStdiofd, SM_TIME_DEFAULT,
563 						 (void *) &dfd, SM_IO_WRONLY_B,
564 						 NULL)) == NULL)
565 			syserr("!queueup: cannot create data temp file %s, uid=%d",
566 				df, (int) geteuid());
567 		if (fstat(dfd, &stbuf) < 0)
568 			e->e_dfino = -1;
569 		else
570 		{
571 			e->e_dfdev = stbuf.st_dev;
572 			e->e_dfino = ST_INODE(stbuf);
573 		}
574 		e->e_flags |= EF_HAS_DF;
575 		memset(&mcibuf, '\0', sizeof(mcibuf));
576 		mcibuf.mci_out = dfp;
577 		mcibuf.mci_mailer = FileMailer;
578 		(*e->e_putbody)(&mcibuf, e, NULL);
579 
580 		if (SuperSafe == SAFE_REALLY ||
581 		    SuperSafe == SAFE_REALLY_POSTMILTER ||
582 		    (SuperSafe == SAFE_INTERACTIVE && msync))
583 		{
584 			if (tTd(40,32))
585 				sm_syslog(LOG_INFO, e->e_id,
586 					  "queueup: fsync(dfp)");
587 
588 			if (fsync(sm_io_getinfo(dfp, SM_IO_WHAT_FD, NULL)) < 0)
589 			{
590 				if (newid)
591 					syserr("!552 Error writing data file %s",
592 					       df);
593 				else
594 					syserr("!452 Error writing data file %s",
595 					       df);
596 			}
597 		}
598 
599 		if (sm_io_close(dfp, SM_TIME_DEFAULT) < 0)
600 			syserr("!queueup: cannot save data temp file %s, uid=%d",
601 				df, (int) geteuid());
602 		e->e_putbody = putbody;
603 	}
604 
605 	/*
606 	**  Output future work requests.
607 	**	Priority and creation time should be first, since
608 	**	they are required by gatherq.
609 	*/
610 
611 	/* output queue version number (must be first!) */
612 	(void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "V%d\n", QF_VERSION);
613 
614 	/* output creation time */
615 	(void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "T%ld\n", (long) e->e_ctime);
616 
617 	/* output last delivery time */
618 	(void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "K%ld\n", (long) e->e_dtime);
619 
620 	/* output number of delivery attempts */
621 	(void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "N%d\n", e->e_ntries);
622 
623 	/* output message priority */
624 	(void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "P%ld\n", e->e_msgpriority);
625 
626 	/*
627 	**  If data file is in a different directory than the queue file,
628 	**  output a "d" record naming the directory of the data file.
629 	*/
630 
631 	if (e->e_dfqgrp != e->e_qgrp)
632 	{
633 		(void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "d%s\n",
634 			Queue[e->e_dfqgrp]->qg_qpaths[e->e_dfqdir].qp_name);
635 	}
636 
637 	/* output inode number of data file */
638 	/* XXX should probably include device major/minor too */
639 	if (e->e_dfino != -1)
640 	{
641 		(void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "I%ld/%ld/%llu\n",
642 				     (long) major(e->e_dfdev),
643 				     (long) minor(e->e_dfdev),
644 				     (ULONGLONG_T) e->e_dfino);
645 	}
646 
647 	/* output body type */
648 	if (e->e_bodytype != NULL)
649 		(void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "B%s\n",
650 				     denlstring(e->e_bodytype, true, false));
651 
652 	/* quarantine reason */
653 	if (e->e_quarmsg != NULL)
654 		(void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "q%s\n",
655 				     denlstring(e->e_quarmsg, true, false));
656 
657 	/* message from envelope, if it exists */
658 	if (e->e_message != NULL)
659 		(void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "M%s\n",
660 				     denlstring(e->e_message, true, false));
661 
662 	/* send various flag bits through */
663 	p = buf;
664 	if (bitset(EF_WARNING, e->e_flags))
665 		*p++ = 'w';
666 	if (bitset(EF_RESPONSE, e->e_flags))
667 		*p++ = 'r';
668 	if (bitset(EF_HAS8BIT, e->e_flags))
669 		*p++ = '8';
670 	if (bitset(EF_DELETE_BCC, e->e_flags))
671 		*p++ = 'b';
672 	if (bitset(EF_RET_PARAM, e->e_flags))
673 		*p++ = 'd';
674 	if (bitset(EF_NO_BODY_RETN, e->e_flags))
675 		*p++ = 'n';
676 	if (bitset(EF_SPLIT, e->e_flags))
677 		*p++ = 's';
678 	*p++ = '\0';
679 	if (buf[0] != '\0')
680 		(void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "F%s\n", buf);
681 
682 	/* save $={persistentMacros} macro values */
683 	queueup_macros(macid("{persistentMacros}"), tfp, e);
684 
685 	/* output name of sender */
686 	if (bitnset(M_UDBENVELOPE, e->e_from.q_mailer->m_flags))
687 		p = e->e_sender;
688 	else
689 		p = e->e_from.q_paddr;
690 	(void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "S%s\n",
691 			     denlstring(p, true, false));
692 
693 	/* output ESMTP-supplied "original" information */
694 	if (e->e_envid != NULL)
695 		(void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "Z%s\n",
696 				     denlstring(e->e_envid, true, false));
697 
698 	/* output AUTH= parameter */
699 	if (e->e_auth_param != NULL)
700 		(void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "A%s\n",
701 				     denlstring(e->e_auth_param, true, false));
702 	if (e->e_dlvr_flag != 0)
703 		(void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "!%c %ld\n",
704 				     (char) e->e_dlvr_flag, e->e_deliver_by);
705 
706 	/* output list of recipient addresses */
707 	printctladdr(NULL, NULL);
708 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
709 	{
710 		if (!QS_IS_UNDELIVERED(q->q_state))
711 			continue;
712 
713 		/* message for this recipient, if it exists */
714 		if (q->q_message != NULL)
715 			(void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "M%s\n",
716 					     denlstring(q->q_message, true,
717 							false));
718 
719 		printctladdr(q, tfp);
720 		if (q->q_orcpt != NULL)
721 			(void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "Q%s\n",
722 					     denlstring(q->q_orcpt, true,
723 							false));
724 		if (q->q_finalrcpt != NULL)
725 			(void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "r%s\n",
726 					     denlstring(q->q_finalrcpt, true,
727 							false));
728 		(void) sm_io_putc(tfp, SM_TIME_DEFAULT, 'R');
729 		if (bitset(QPRIMARY, q->q_flags))
730 			(void) sm_io_putc(tfp, SM_TIME_DEFAULT, 'P');
731 		if (bitset(QHASNOTIFY, q->q_flags))
732 			(void) sm_io_putc(tfp, SM_TIME_DEFAULT, 'N');
733 		if (bitset(QPINGONSUCCESS, q->q_flags))
734 			(void) sm_io_putc(tfp, SM_TIME_DEFAULT, 'S');
735 		if (bitset(QPINGONFAILURE, q->q_flags))
736 			(void) sm_io_putc(tfp, SM_TIME_DEFAULT, 'F');
737 		if (bitset(QPINGONDELAY, q->q_flags))
738 			(void) sm_io_putc(tfp, SM_TIME_DEFAULT, 'D');
739 		if (q->q_alias != NULL &&
740 		    bitset(QALIAS, q->q_alias->q_flags))
741 			(void) sm_io_putc(tfp, SM_TIME_DEFAULT, 'A');
742 		(void) sm_io_putc(tfp, SM_TIME_DEFAULT, ':');
743 		(void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "%s\n",
744 				     denlstring(q->q_paddr, true, false));
745 		if (announce)
746 		{
747 			char *tag = "queued";
748 
749 			if (e->e_quarmsg != NULL)
750 				tag = "quarantined";
751 
752 			e->e_to = q->q_paddr;
753 			message(tag);
754 			if (LogLevel > 8)
755 				logdelivery(q->q_mailer, NULL, q->q_status,
756 					    tag, NULL, (time_t) 0, e);
757 			e->e_to = NULL;
758 		}
759 		if (tTd(40, 1))
760 		{
761 			sm_dprintf("queueing ");
762 			printaddr(sm_debug_file(), q, false);
763 		}
764 	}
765 
766 	/*
767 	**  Output headers for this message.
768 	**	Expand macros completely here.  Queue run will deal with
769 	**	everything as absolute headers.
770 	**		All headers that must be relative to the recipient
771 	**		can be cracked later.
772 	**	We set up a "null mailer" -- i.e., a mailer that will have
773 	**	no effect on the addresses as they are output.
774 	*/
775 
776 	memset((char *) &nullmailer, '\0', sizeof(nullmailer));
777 	nullmailer.m_re_rwset = nullmailer.m_rh_rwset =
778 			nullmailer.m_se_rwset = nullmailer.m_sh_rwset = -1;
779 	nullmailer.m_eol = "\n";
780 	memset(&mcibuf, '\0', sizeof(mcibuf));
781 	mcibuf.mci_mailer = &nullmailer;
782 	mcibuf.mci_out = tfp;
783 
784 	macdefine(&e->e_macro, A_PERM, 'g', "\201f");
785 	for (h = e->e_header; h != NULL; h = h->h_link)
786 	{
787 		if (h->h_value == NULL)
788 			continue;
789 
790 		/* don't output resent headers on non-resent messages */
791 		if (bitset(H_RESENT, h->h_flags) &&
792 		    !bitset(EF_RESENT, e->e_flags))
793 			continue;
794 
795 		/* expand macros; if null, don't output header at all */
796 		if (bitset(H_DEFAULT, h->h_flags))
797 		{
798 			(void) expand(h->h_value, buf, sizeof(buf), e);
799 			if (buf[0] == '\0')
800 				continue;
801 			if (buf[0] == ' ' && buf[1] == '\0')
802 				continue;
803 		}
804 
805 		/* output this header */
806 		(void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "H?");
807 
808 		/* output conditional macro if present */
809 		if (h->h_macro != '\0')
810 		{
811 			if (bitset(0200, h->h_macro))
812 				(void) sm_io_fprintf(tfp, SM_TIME_DEFAULT,
813 						     "${%s}",
814 						      macname(bitidx(h->h_macro)));
815 			else
816 				(void) sm_io_fprintf(tfp, SM_TIME_DEFAULT,
817 						     "$%c", h->h_macro);
818 		}
819 		else if (!bitzerop(h->h_mflags) &&
820 			 bitset(H_CHECK|H_ACHECK, h->h_flags))
821 		{
822 			int j;
823 
824 			/* if conditional, output the set of conditions */
825 			for (j = '\0'; j <= '\177'; j++)
826 				if (bitnset(j, h->h_mflags))
827 					(void) sm_io_putc(tfp, SM_TIME_DEFAULT,
828 							  j);
829 		}
830 		(void) sm_io_putc(tfp, SM_TIME_DEFAULT, '?');
831 
832 		/* output the header: expand macros, convert addresses */
833 		if (bitset(H_DEFAULT, h->h_flags) &&
834 		    !bitset(H_BINDLATE, h->h_flags))
835 		{
836 			(void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "%s:%s\n",
837 					     h->h_field,
838 					     denlstring(buf, false, true));
839 		}
840 		else if (bitset(H_FROM|H_RCPT, h->h_flags) &&
841 			 !bitset(H_BINDLATE, h->h_flags))
842 		{
843 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
844 			SM_FILE_T *savetrace = TrafficLogFile;
845 
846 			TrafficLogFile = NULL;
847 
848 			if (bitset(H_FROM, h->h_flags))
849 				oldstyle = false;
850 
851 			commaize(h, h->h_value, oldstyle, &mcibuf, e);
852 
853 			TrafficLogFile = savetrace;
854 		}
855 		else
856 		{
857 			(void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "%s:%s\n",
858 					     h->h_field,
859 					     denlstring(h->h_value, false,
860 							true));
861 		}
862 	}
863 
864 	/*
865 	**  Clean up.
866 	**
867 	**	Write a terminator record -- this is to prevent
868 	**	scurrilous crackers from appending any data.
869 	*/
870 
871 	(void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, ".\n");
872 
873 	if (sm_io_flush(tfp, SM_TIME_DEFAULT) != 0 ||
874 	    ((SuperSafe == SAFE_REALLY ||
875 	      SuperSafe == SAFE_REALLY_POSTMILTER ||
876 	      (SuperSafe == SAFE_INTERACTIVE && msync)) &&
877 	     fsync(sm_io_getinfo(tfp, SM_IO_WHAT_FD, NULL)) < 0) ||
878 	    sm_io_error(tfp))
879 	{
880 		if (newid)
881 			syserr("!552 Error writing control file %s", tf);
882 		else
883 			syserr("!452 Error writing control file %s", tf);
884 	}
885 
886 	if (!newid)
887 	{
888 		char new = queue_letter(e, ANYQFL_LETTER);
889 
890 		/* rename (locked) tf to be (locked) [qh]f */
891 		(void) sm_strlcpy(qf, queuename(e, ANYQFL_LETTER),
892 				  sizeof(qf));
893 		if (rename(tf, qf) < 0)
894 			syserr("cannot rename(%s, %s), uid=%d",
895 				tf, qf, (int) geteuid());
896 		else
897 		{
898 			/*
899 			**  Check if type has changed and only
900 			**  remove the old item if the rename above
901 			**  succeeded.
902 			*/
903 
904 			if (e->e_qfletter != '\0' &&
905 			    e->e_qfletter != new)
906 			{
907 				if (tTd(40, 5))
908 				{
909 					sm_dprintf("type changed from %c to %c\n",
910 						   e->e_qfletter, new);
911 				}
912 
913 				if (unlink(queuename(e, e->e_qfletter)) < 0)
914 				{
915 					/* XXX: something more drastic? */
916 					if (LogLevel > 0)
917 						sm_syslog(LOG_ERR, e->e_id,
918 							  "queueup: unlink(%s) failed: %s",
919 							  queuename(e, e->e_qfletter),
920 							  sm_errstring(errno));
921 				}
922 			}
923 		}
924 		e->e_qfletter = new;
925 
926 		/*
927 		**  fsync() after renaming to make sure metadata is
928 		**  written to disk on filesystems in which renames are
929 		**  not guaranteed.
930 		*/
931 
932 		if (SuperSafe != SAFE_NO)
933 		{
934 			/* for softupdates */
935 			if (tfd >= 0 && fsync(tfd) < 0)
936 			{
937 				syserr("!queueup: cannot fsync queue temp file %s",
938 				       tf);
939 			}
940 			SYNC_DIR(qf, true);
941 		}
942 
943 		/* close and unlock old (locked) queue file */
944 		if (e->e_lockfp != NULL)
945 			(void) sm_io_close(e->e_lockfp, SM_TIME_DEFAULT);
946 		e->e_lockfp = tfp;
947 
948 		/* save log info */
949 		if (LogLevel > 79)
950 			sm_syslog(LOG_DEBUG, e->e_id, "queueup %s", qf);
951 	}
952 	else
953 	{
954 		/* save log info */
955 		if (LogLevel > 79)
956 			sm_syslog(LOG_DEBUG, e->e_id, "queueup %s", tf);
957 
958 		e->e_qfletter = queue_letter(e, ANYQFL_LETTER);
959 	}
960 
961 	errno = 0;
962 	e->e_flags |= EF_INQUEUE;
963 
964 	if (tTd(40, 1))
965 		sm_dprintf("<<<<< done queueing %s <<<<<\n\n", e->e_id);
966 	return;
967 }
968 
969 /*
970 **  PRINTCTLADDR -- print control address to file.
971 **
972 **	Parameters:
973 **		a -- address.
974 **		tfp -- file pointer.
975 **
976 **	Returns:
977 **		none.
978 **
979 **	Side Effects:
980 **		The control address (if changed) is printed to the file.
981 **		The last control address and uid are saved.
982 */
983 
984 static void
985 printctladdr(a, tfp)
986 	register ADDRESS *a;
987 	SM_FILE_T *tfp;
988 {
989 	char *user;
990 	register ADDRESS *q;
991 	uid_t uid;
992 	gid_t gid;
993 	static ADDRESS *lastctladdr = NULL;
994 	static uid_t lastuid;
995 
996 	/* initialization */
997 	if (a == NULL || a->q_alias == NULL || tfp == NULL)
998 	{
999 		if (lastctladdr != NULL && tfp != NULL)
1000 			(void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "C\n");
1001 		lastctladdr = NULL;
1002 		lastuid = 0;
1003 		return;
1004 	}
1005 
1006 	/* find the active uid */
1007 	q = getctladdr(a);
1008 	if (q == NULL)
1009 	{
1010 		user = NULL;
1011 		uid = 0;
1012 		gid = 0;
1013 	}
1014 	else
1015 	{
1016 		user = q->q_ruser != NULL ? q->q_ruser : q->q_user;
1017 		uid = q->q_uid;
1018 		gid = q->q_gid;
1019 	}
1020 	a = a->q_alias;
1021 
1022 	/* check to see if this is the same as last time */
1023 	if (lastctladdr != NULL && uid == lastuid &&
1024 	    strcmp(lastctladdr->q_paddr, a->q_paddr) == 0)
1025 		return;
1026 	lastuid = uid;
1027 	lastctladdr = a;
1028 
1029 	if (uid == 0 || user == NULL || user[0] == '\0')
1030 		(void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "C");
1031 	else
1032 		(void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "C%s:%ld:%ld",
1033 				     denlstring(user, true, false), (long) uid,
1034 				     (long) gid);
1035 	(void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, ":%s\n",
1036 			     denlstring(a->q_paddr, true, false));
1037 }
1038 
1039 /*
1040 **  RUNNERS_SIGTERM -- propagate a SIGTERM to queue runner process
1041 **
1042 **	This propagates the signal to the child processes that are queue
1043 **	runners. This is for a queue runner "cleanup". After all of the
1044 **	child queue runner processes are signaled (it should be SIGTERM
1045 **	being the sig) then the old signal handler (Oldsh) is called
1046 **	to handle any cleanup set for this process (provided it is not
1047 **	SIG_DFL or SIG_IGN). The signal may not be handled immediately
1048 **	if the BlockOldsh flag is set. If the current process doesn't
1049 **	have a parent then handle the signal immediately, regardless of
1050 **	BlockOldsh.
1051 **
1052 **	Parameters:
1053 **		sig -- the signal number being sent
1054 **
1055 **	Returns:
1056 **		none.
1057 **
1058 **	Side Effects:
1059 **		Sets the NoMoreRunners boolean to true to stop more runners
1060 **		from being started in runqueue().
1061 **
1062 **	NOTE:	THIS CAN BE CALLED FROM A SIGNAL HANDLER.  DO NOT ADD
1063 **		ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
1064 **		DOING.
1065 */
1066 
1067 static bool		volatile NoMoreRunners = false;
1068 static sigfunc_t	Oldsh_term = SIG_DFL;
1069 static sigfunc_t	Oldsh_hup = SIG_DFL;
1070 static sigfunc_t	volatile Oldsh = SIG_DFL;
1071 static bool		BlockOldsh = false;
1072 static int		volatile Oldsig = 0;
1073 static SIGFUNC_DECL	runners_sigterm __P((int));
1074 static SIGFUNC_DECL	runners_sighup __P((int));
1075 
1076 static SIGFUNC_DECL
1077 runners_sigterm(sig)
1078 	int sig;
1079 {
1080 	int save_errno = errno;
1081 
1082 	FIX_SYSV_SIGNAL(sig, runners_sigterm);
1083 	errno = save_errno;
1084 	CHECK_CRITICAL(sig);
1085 	NoMoreRunners = true;
1086 	Oldsh = Oldsh_term;
1087 	Oldsig = sig;
1088 	proc_list_signal(PROC_QUEUE, sig);
1089 
1090 	if (!BlockOldsh || getppid() <= 1)
1091 	{
1092 		/* Check that a valid 'old signal handler' is callable */
1093 		if (Oldsh_term != SIG_DFL && Oldsh_term != SIG_IGN &&
1094 		    Oldsh_term != runners_sigterm)
1095 			(*Oldsh_term)(sig);
1096 	}
1097 	errno = save_errno;
1098 	return SIGFUNC_RETURN;
1099 }
1100 /*
1101 **  RUNNERS_SIGHUP -- propagate a SIGHUP to queue runner process
1102 **
1103 **	This propagates the signal to the child processes that are queue
1104 **	runners. This is for a queue runner "cleanup". After all of the
1105 **	child queue runner processes are signaled (it should be SIGHUP
1106 **	being the sig) then the old signal handler (Oldsh) is called to
1107 **	handle any cleanup set for this process (provided it is not SIG_DFL
1108 **	or SIG_IGN). The signal may not be handled immediately if the
1109 **	BlockOldsh flag is set. If the current process doesn't have
1110 **	a parent then handle the signal immediately, regardless of
1111 **	BlockOldsh.
1112 **
1113 **	Parameters:
1114 **		sig -- the signal number being sent
1115 **
1116 **	Returns:
1117 **		none.
1118 **
1119 **	Side Effects:
1120 **		Sets the NoMoreRunners boolean to true to stop more runners
1121 **		from being started in runqueue().
1122 **
1123 **	NOTE:	THIS CAN BE CALLED FROM A SIGNAL HANDLER.  DO NOT ADD
1124 **		ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
1125 **		DOING.
1126 */
1127 
1128 static SIGFUNC_DECL
1129 runners_sighup(sig)
1130 	int sig;
1131 {
1132 	int save_errno = errno;
1133 
1134 	FIX_SYSV_SIGNAL(sig, runners_sighup);
1135 	errno = save_errno;
1136 	CHECK_CRITICAL(sig);
1137 	NoMoreRunners = true;
1138 	Oldsh = Oldsh_hup;
1139 	Oldsig = sig;
1140 	proc_list_signal(PROC_QUEUE, sig);
1141 
1142 	if (!BlockOldsh || getppid() <= 1)
1143 	{
1144 		/* Check that a valid 'old signal handler' is callable */
1145 		if (Oldsh_hup != SIG_DFL && Oldsh_hup != SIG_IGN &&
1146 		    Oldsh_hup != runners_sighup)
1147 			(*Oldsh_hup)(sig);
1148 	}
1149 	errno = save_errno;
1150 	return SIGFUNC_RETURN;
1151 }
1152 /*
1153 **  MARK_WORK_GROUP_RESTART -- mark a work group as needing a restart
1154 **
1155 **  Sets a workgroup for restarting.
1156 **
1157 **	Parameters:
1158 **		wgrp -- the work group id to restart.
1159 **		reason -- why (signal?), -1 to turn off restart
1160 **
1161 **	Returns:
1162 **		none.
1163 **
1164 **	Side effects:
1165 **		May set global RestartWorkGroup to true.
1166 **
1167 **	NOTE:	THIS CAN BE CALLED FROM A SIGNAL HANDLER.  DO NOT ADD
1168 **		ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
1169 **		DOING.
1170 */
1171 
1172 void
1173 mark_work_group_restart(wgrp, reason)
1174 	int wgrp;
1175 	int reason;
1176 {
1177 	if (wgrp < 0 || wgrp > NumWorkGroups)
1178 		return;
1179 
1180 	WorkGrp[wgrp].wg_restart = reason;
1181 	if (reason >= 0)
1182 		RestartWorkGroup = true;
1183 }
1184 /*
1185 **  RESTART_MARKED_WORK_GROUPS -- restart work groups marked as needing restart
1186 **
1187 **  Restart any workgroup marked as needing a restart provided more
1188 **  runners are allowed.
1189 **
1190 **	Parameters:
1191 **		none.
1192 **
1193 **	Returns:
1194 **		none.
1195 **
1196 **	Side effects:
1197 **		Sets global RestartWorkGroup to false.
1198 */
1199 
1200 void
1201 restart_marked_work_groups()
1202 {
1203 	int i;
1204 	int wasblocked;
1205 
1206 	if (NoMoreRunners)
1207 		return;
1208 
1209 	/* Block SIGCHLD so reapchild() doesn't mess with us */
1210 	wasblocked = sm_blocksignal(SIGCHLD);
1211 
1212 	for (i = 0; i < NumWorkGroups; i++)
1213 	{
1214 		if (WorkGrp[i].wg_restart >= 0)
1215 		{
1216 			if (LogLevel > 8)
1217 				sm_syslog(LOG_ERR, NOQID,
1218 					  "restart queue runner=%d due to signal 0x%x",
1219 					  i, WorkGrp[i].wg_restart);
1220 			restart_work_group(i);
1221 		}
1222 	}
1223 	RestartWorkGroup = false;
1224 
1225 	if (wasblocked == 0)
1226 		(void) sm_releasesignal(SIGCHLD);
1227 }
1228 /*
1229 **  RESTART_WORK_GROUP -- restart a specific work group
1230 **
1231 **  Restart a specific workgroup provided more runners are allowed.
1232 **  If the requested work group has been restarted too many times log
1233 **  this and refuse to restart.
1234 **
1235 **	Parameters:
1236 **		wgrp -- the work group id to restart
1237 **
1238 **	Returns:
1239 **		none.
1240 **
1241 **	Side Effects:
1242 **		starts another process doing the work of wgrp
1243 */
1244 
1245 #define MAX_PERSIST_RESTART	10	/* max allowed number of restarts */
1246 
1247 static void
1248 restart_work_group(wgrp)
1249 	int wgrp;
1250 {
1251 	if (NoMoreRunners ||
1252 	    wgrp < 0 || wgrp > NumWorkGroups)
1253 		return;
1254 
1255 	WorkGrp[wgrp].wg_restart = -1;
1256 	if (WorkGrp[wgrp].wg_restartcnt < MAX_PERSIST_RESTART)
1257 	{
1258 		/* avoid overflow; increment here */
1259 		WorkGrp[wgrp].wg_restartcnt++;
1260 		(void) run_work_group(wgrp, RWG_FORK|RWG_PERSISTENT|RWG_RUNALL);
1261 	}
1262 	else
1263 	{
1264 		sm_syslog(LOG_ERR, NOQID,
1265 			  "ERROR: persistent queue runner=%d restarted too many times, queue runner lost",
1266 			  wgrp);
1267 	}
1268 }
1269 /*
1270 **  SCHEDULE_QUEUE_RUNS -- schedule the next queue run for a work group.
1271 **
1272 **	Parameters:
1273 **		runall -- schedule even if individual bit is not set.
1274 **		wgrp -- the work group id to schedule.
1275 **		didit -- the queue run was performed for this work group.
1276 **
1277 **	Returns:
1278 **		nothing
1279 */
1280 
1281 #define INCR_MOD(v, m)	if (++v >= m)	\
1282 				v = 0;	\
1283 			else
1284 
1285 static void
1286 schedule_queue_runs(runall, wgrp, didit)
1287 	bool runall;
1288 	int wgrp;
1289 	bool didit;
1290 {
1291 	int qgrp, cgrp, endgrp;
1292 #if _FFR_QUEUE_SCHED_DBG
1293 	time_t lastsched;
1294 	bool sched;
1295 #endif /* _FFR_QUEUE_SCHED_DBG */
1296 	time_t now;
1297 	time_t minqintvl;
1298 
1299 	/*
1300 	**  This is a bit ugly since we have to duplicate the
1301 	**  code that "walks" through a work queue group.
1302 	*/
1303 
1304 	now = curtime();
1305 	minqintvl = 0;
1306 	cgrp = endgrp = WorkGrp[wgrp].wg_curqgrp;
1307 	do
1308 	{
1309 		time_t qintvl;
1310 
1311 #if _FFR_QUEUE_SCHED_DBG
1312 		lastsched = 0;
1313 		sched = false;
1314 #endif /* _FFR_QUEUE_SCHED_DBG */
1315 		qgrp = WorkGrp[wgrp].wg_qgs[cgrp]->qg_index;
1316 		if (Queue[qgrp]->qg_queueintvl > 0)
1317 			qintvl = Queue[qgrp]->qg_queueintvl;
1318 		else if (QueueIntvl > 0)
1319 			qintvl = QueueIntvl;
1320 		else
1321 			qintvl = (time_t) 0;
1322 #if _FFR_QUEUE_SCHED_DBG
1323 		lastsched = Queue[qgrp]->qg_nextrun;
1324 #endif /* _FFR_QUEUE_SCHED_DBG */
1325 		if ((runall || Queue[qgrp]->qg_nextrun <= now) && qintvl > 0)
1326 		{
1327 #if _FFR_QUEUE_SCHED_DBG
1328 			sched = true;
1329 #endif /* _FFR_QUEUE_SCHED_DBG */
1330 			if (minqintvl == 0 || qintvl < minqintvl)
1331 				minqintvl = qintvl;
1332 
1333 			/*
1334 			**  Only set a new time if a queue run was performed
1335 			**  for this queue group.  If the queue was not run,
1336 			**  we could starve it by setting a new time on each
1337 			**  call.
1338 			*/
1339 
1340 			if (didit)
1341 				Queue[qgrp]->qg_nextrun += qintvl;
1342 		}
1343 #if _FFR_QUEUE_SCHED_DBG
1344 		if (tTd(69, 10))
1345 			sm_syslog(LOG_INFO, NOQID,
1346 				"sqr: wgrp=%d, cgrp=%d, qgrp=%d, intvl=%ld, QI=%ld, runall=%d, lastrun=%ld, nextrun=%ld, sched=%d",
1347 				wgrp, cgrp, qgrp, Queue[qgrp]->qg_queueintvl,
1348 				QueueIntvl, runall, lastsched,
1349 				Queue[qgrp]->qg_nextrun, sched);
1350 #endif /* _FFR_QUEUE_SCHED_DBG */
1351 		INCR_MOD(cgrp, WorkGrp[wgrp].wg_numqgrp);
1352 	} while (endgrp != cgrp);
1353 	if (minqintvl > 0)
1354 		(void) sm_setevent(minqintvl, runqueueevent, 0);
1355 }
1356 
1357 #if _FFR_QUEUE_RUN_PARANOIA
1358 /*
1359 **  CHECKQUEUERUNNER -- check whether a queue group hasn't been run.
1360 **
1361 **	Use this if events may get lost and hence queue runners may not
1362 **	be started and mail will pile up in a queue.
1363 **
1364 **	Parameters:
1365 **		none.
1366 **
1367 **	Returns:
1368 **		true if a queue run is necessary.
1369 **
1370 **	Side Effects:
1371 **		may schedule a queue run.
1372 */
1373 
1374 bool
1375 checkqueuerunner()
1376 {
1377 	int qgrp;
1378 	time_t now, minqintvl;
1379 
1380 	now = curtime();
1381 	minqintvl = 0;
1382 	for (qgrp = 0; qgrp < NumQueue && Queue[qgrp] != NULL; qgrp++)
1383 	{
1384 		time_t qintvl;
1385 
1386 		if (Queue[qgrp]->qg_queueintvl > 0)
1387 			qintvl = Queue[qgrp]->qg_queueintvl;
1388 		else if (QueueIntvl > 0)
1389 			qintvl = QueueIntvl;
1390 		else
1391 			qintvl = (time_t) 0;
1392 		if (Queue[qgrp]->qg_nextrun <= now - qintvl)
1393 		{
1394 			if (minqintvl == 0 || qintvl < minqintvl)
1395 				minqintvl = qintvl;
1396 			if (LogLevel > 1)
1397 				sm_syslog(LOG_WARNING, NOQID,
1398 					"checkqueuerunner: queue %d should have been run at %s, queue interval %ld",
1399 					qgrp,
1400 					arpadate(ctime(&Queue[qgrp]->qg_nextrun)),
1401 					qintvl);
1402 		}
1403 	}
1404 	if (minqintvl > 0)
1405 	{
1406 		(void) sm_setevent(minqintvl, runqueueevent, 0);
1407 		return true;
1408 	}
1409 	return false;
1410 }
1411 #endif /* _FFR_QUEUE_RUN_PARANOIA */
1412 
1413 /*
1414 **  RUNQUEUE -- run the jobs in the queue.
1415 **
1416 **	Gets the stuff out of the queue in some presumably logical
1417 **	order and processes them.
1418 **
1419 **	Parameters:
1420 **		forkflag -- true if the queue scanning should be done in
1421 **			a child process.  We double-fork so it is not our
1422 **			child and we don't have to clean up after it.
1423 **			false can be ignored if we have multiple queues.
1424 **		verbose -- if true, print out status information.
1425 **		persistent -- persistent queue runner?
1426 **		runall -- run all groups or only a subset (DoQueueRun)?
1427 **
1428 **	Returns:
1429 **		true if the queue run successfully began.
1430 **
1431 **	Side Effects:
1432 **		runs things in the mail queue using run_work_group().
1433 **		maybe schedules next queue run.
1434 */
1435 
1436 static ENVELOPE	QueueEnvelope;		/* the queue run envelope */
1437 static time_t	LastQueueTime = 0;	/* last time a queue ID assigned */
1438 static pid_t	LastQueuePid = -1;	/* last PID which had a queue ID */
1439 
1440 /* values for qp_supdirs */
1441 #define QP_NOSUB	0x0000	/* No subdirectories */
1442 #define QP_SUBDF	0x0001	/* "df" subdirectory */
1443 #define QP_SUBQF	0x0002	/* "qf" subdirectory */
1444 #define QP_SUBXF	0x0004	/* "xf" subdirectory */
1445 
1446 bool
1447 runqueue(forkflag, verbose, persistent, runall)
1448 	bool forkflag;
1449 	bool verbose;
1450 	bool persistent;
1451 	bool runall;
1452 {
1453 	int i;
1454 	bool ret = true;
1455 	static int curnum = 0;
1456 	sigfunc_t cursh;
1457 #if SM_HEAP_CHECK
1458 	SM_NONVOLATILE int oldgroup = 0;
1459 
1460 	if (sm_debug_active(&DebugLeakQ, 1))
1461 	{
1462 		oldgroup = sm_heap_group();
1463 		sm_heap_newgroup();
1464 		sm_dprintf("runqueue() heap group #%d\n", sm_heap_group());
1465 	}
1466 #endif /* SM_HEAP_CHECK */
1467 
1468 	/* queue run has been started, don't do any more this time */
1469 	DoQueueRun = false;
1470 
1471 	/* more than one queue or more than one directory per queue */
1472 	if (!forkflag && !verbose &&
1473 	    (WorkGrp[0].wg_qgs[0]->qg_numqueues > 1 || NumWorkGroups > 1 ||
1474 	     WorkGrp[0].wg_numqgrp > 1))
1475 		forkflag = true;
1476 
1477 	/*
1478 	**  For controlling queue runners via signals sent to this process.
1479 	**  Oldsh* will get called too by runners_sig* (if it is not SIG_IGN
1480 	**  or SIG_DFL) to preserve cleanup behavior. Now that this process
1481 	**  will have children (and perhaps grandchildren) this handler will
1482 	**  be left in place. This is because this process, once it has
1483 	**  finished spinning off queue runners, may go back to doing something
1484 	**  else (like being a daemon). And we still want on a SIG{TERM,HUP} to
1485 	**  clean up the child queue runners. Only install 'runners_sig*' once
1486 	**  else we'll get stuck looping forever.
1487 	*/
1488 
1489 	cursh = sm_signal(SIGTERM, runners_sigterm);
1490 	if (cursh != runners_sigterm)
1491 		Oldsh_term = cursh;
1492 	cursh = sm_signal(SIGHUP, runners_sighup);
1493 	if (cursh != runners_sighup)
1494 		Oldsh_hup = cursh;
1495 
1496 	for (i = 0; i < NumWorkGroups && !NoMoreRunners; i++)
1497 	{
1498 		int rwgflags = RWG_NONE;
1499 
1500 		/*
1501 		**  If MaxQueueChildren active then test whether the start
1502 		**  of the next queue group's additional queue runners (maximum)
1503 		**  will result in MaxQueueChildren being exceeded.
1504 		**
1505 		**  Note: do not use continue; even though another workgroup
1506 		**	may have fewer queue runners, this would be "unfair",
1507 		**	i.e., this work group might "starve" then.
1508 		*/
1509 
1510 #if _FFR_QUEUE_SCHED_DBG
1511 		if (tTd(69, 10))
1512 			sm_syslog(LOG_INFO, NOQID,
1513 				"rq: curnum=%d, MaxQueueChildren=%d, CurRunners=%d, WorkGrp[curnum].wg_maxact=%d",
1514 				curnum, MaxQueueChildren, CurRunners,
1515 				WorkGrp[curnum].wg_maxact);
1516 #endif /* _FFR_QUEUE_SCHED_DBG */
1517 		if (MaxQueueChildren > 0 &&
1518 		    CurRunners + WorkGrp[curnum].wg_maxact > MaxQueueChildren)
1519 			break;
1520 
1521 		/*
1522 		**  Pick up where we left off (curnum), in case we
1523 		**  used up all the children last time without finishing.
1524 		**  This give a round-robin fairness to queue runs.
1525 		**
1526 		**  Increment CurRunners before calling run_work_group()
1527 		**  to avoid a "race condition" with proc_list_drop() which
1528 		**  decrements CurRunners if the queue runners terminate.
1529 		**  Notice: CurRunners is an upper limit, in some cases
1530 		**  (too few jobs in the queue) this value is larger than
1531 		**  the actual number of queue runners. The discrepancy can
1532 		**  increase if some queue runners "hang" for a long time.
1533 		*/
1534 
1535 		CurRunners += WorkGrp[curnum].wg_maxact;
1536 		if (forkflag)
1537 			rwgflags |= RWG_FORK;
1538 		if (verbose)
1539 			rwgflags |= RWG_VERBOSE;
1540 		if (persistent)
1541 			rwgflags |= RWG_PERSISTENT;
1542 		if (runall)
1543 			rwgflags |= RWG_RUNALL;
1544 		ret = run_work_group(curnum, rwgflags);
1545 
1546 		/*
1547 		**  Failure means a message was printed for ETRN
1548 		**  and subsequent queues are likely to fail as well.
1549 		**  Decrement CurRunners in that case because
1550 		**  none have been started.
1551 		*/
1552 
1553 		if (!ret)
1554 		{
1555 			CurRunners -= WorkGrp[curnum].wg_maxact;
1556 			break;
1557 		}
1558 
1559 		if (!persistent)
1560 			schedule_queue_runs(runall, curnum, true);
1561 		INCR_MOD(curnum, NumWorkGroups);
1562 	}
1563 
1564 	/* schedule left over queue runs */
1565 	if (i < NumWorkGroups && !NoMoreRunners && !persistent)
1566 	{
1567 		int h;
1568 
1569 		for (h = curnum; i < NumWorkGroups; i++)
1570 		{
1571 			schedule_queue_runs(runall, h, false);
1572 			INCR_MOD(h, NumWorkGroups);
1573 		}
1574 	}
1575 
1576 
1577 #if SM_HEAP_CHECK
1578 	if (sm_debug_active(&DebugLeakQ, 1))
1579 		sm_heap_setgroup(oldgroup);
1580 #endif /* SM_HEAP_CHECK */
1581 	return ret;
1582 }
1583 
1584 #if _FFR_SKIP_DOMAINS
1585 /*
1586 **  SKIP_DOMAINS -- Skip 'skip' number of domains in the WorkQ.
1587 **
1588 **  Added by Stephen Frost <sfrost@snowman.net> to support
1589 **  having each runner process every N'th domain instead of
1590 **  every N'th message.
1591 **
1592 **	Parameters:
1593 **		skip -- number of domains in WorkQ to skip.
1594 **
1595 **	Returns:
1596 **		total number of messages skipped.
1597 **
1598 **	Side Effects:
1599 **		may change WorkQ
1600 */
1601 
1602 static int
1603 skip_domains(skip)
1604 	int skip;
1605 {
1606 	int n, seqjump;
1607 
1608 	for (n = 0, seqjump = 0; n < skip && WorkQ != NULL; seqjump++)
1609 	{
1610 		if (WorkQ->w_next != NULL)
1611 		{
1612 			if (WorkQ->w_host != NULL &&
1613 			    WorkQ->w_next->w_host != NULL)
1614 			{
1615 				if (sm_strcasecmp(WorkQ->w_host,
1616 						WorkQ->w_next->w_host) != 0)
1617 					n++;
1618 			}
1619 			else
1620 			{
1621 				if ((WorkQ->w_host != NULL &&
1622 				     WorkQ->w_next->w_host == NULL) ||
1623 				    (WorkQ->w_host == NULL &&
1624 				     WorkQ->w_next->w_host != NULL))
1625 					     n++;
1626 			}
1627 		}
1628 		WorkQ = WorkQ->w_next;
1629 	}
1630 	return seqjump;
1631 }
1632 #endif /* _FFR_SKIP_DOMAINS */
1633 
1634 /*
1635 **  RUNNER_WORK -- have a queue runner do its work
1636 **
1637 **  Have a queue runner do its work a list of entries.
1638 **  When work isn't directly being done then this process can take a signal
1639 **  and terminate immediately (in a clean fashion of course).
1640 **  When work is directly being done, it's not to be interrupted
1641 **  immediately: the work should be allowed to finish at a clean point
1642 **  before termination (in a clean fashion of course).
1643 **
1644 **	Parameters:
1645 **		e -- envelope.
1646 **		sequenceno -- 'th process to run WorkQ.
1647 **		didfork -- did the calling process fork()?
1648 **		skip -- process only each skip'th item.
1649 **		njobs -- number of jobs in WorkQ.
1650 **
1651 **	Returns:
1652 **		none.
1653 **
1654 **	Side Effects:
1655 **		runs things in the mail queue.
1656 */
1657 
1658 static void
1659 runner_work(e, sequenceno, didfork, skip, njobs)
1660 	register ENVELOPE *e;
1661 	int sequenceno;
1662 	bool didfork;
1663 	int skip;
1664 	int njobs;
1665 {
1666 	int n, seqjump;
1667 	WORK *w;
1668 	time_t now;
1669 
1670 	SM_GET_LA(now);
1671 
1672 	/*
1673 	**  Here we temporarily block the second calling of the handlers.
1674 	**  This allows us to handle the signal without terminating in the
1675 	**  middle of direct work. If a signal does come, the test for
1676 	**  NoMoreRunners will find it.
1677 	*/
1678 
1679 	BlockOldsh = true;
1680 	seqjump = skip;
1681 
1682 	/* process them once at a time */
1683 	while (WorkQ != NULL)
1684 	{
1685 #if SM_HEAP_CHECK
1686 		SM_NONVOLATILE int oldgroup = 0;
1687 
1688 		if (sm_debug_active(&DebugLeakQ, 1))
1689 		{
1690 			oldgroup = sm_heap_group();
1691 			sm_heap_newgroup();
1692 			sm_dprintf("run_queue_group() heap group #%d\n",
1693 				sm_heap_group());
1694 		}
1695 #endif /* SM_HEAP_CHECK */
1696 
1697 		/* do no more work */
1698 		if (NoMoreRunners)
1699 		{
1700 			/* Check that a valid signal handler is callable */
1701 			if (Oldsh != SIG_DFL && Oldsh != SIG_IGN &&
1702 			    Oldsh != runners_sighup &&
1703 			    Oldsh != runners_sigterm)
1704 				(*Oldsh)(Oldsig);
1705 			break;
1706 		}
1707 
1708 		w = WorkQ; /* assign current work item */
1709 
1710 		/*
1711 		**  Set the head of the WorkQ to the next work item.
1712 		**  It is set 'skip' ahead (the number of parallel queue
1713 		**  runners working on WorkQ together) since each runner
1714 		**  works on every 'skip'th (N-th) item.
1715 #if _FFR_SKIP_DOMAINS
1716 		**  In the case of the BYHOST Queue Sort Order, the 'item'
1717 		**  is a domain, so we work on every 'skip'th (N-th) domain.
1718 #endif * _FFR_SKIP_DOMAINS *
1719 		*/
1720 
1721 #if _FFR_SKIP_DOMAINS
1722 		if (QueueSortOrder == QSO_BYHOST)
1723 		{
1724 			seqjump = 1;
1725 			if (WorkQ->w_next != NULL)
1726 			{
1727 				if (WorkQ->w_host != NULL &&
1728 				    WorkQ->w_next->w_host != NULL)
1729 				{
1730 					if (sm_strcasecmp(WorkQ->w_host,
1731 							WorkQ->w_next->w_host)
1732 								!= 0)
1733 						seqjump = skip_domains(skip);
1734 					else
1735 						WorkQ = WorkQ->w_next;
1736 				}
1737 				else
1738 				{
1739 					if ((WorkQ->w_host != NULL &&
1740 					     WorkQ->w_next->w_host == NULL) ||
1741 					    (WorkQ->w_host == NULL &&
1742 					     WorkQ->w_next->w_host != NULL))
1743 						seqjump = skip_domains(skip);
1744 					else
1745 						WorkQ = WorkQ->w_next;
1746 				}
1747 			}
1748 			else
1749 				WorkQ = WorkQ->w_next;
1750 		}
1751 		else
1752 #endif /* _FFR_SKIP_DOMAINS */
1753 		{
1754 			for (n = 0; n < skip && WorkQ != NULL; n++)
1755 				WorkQ = WorkQ->w_next;
1756 		}
1757 
1758 		e->e_to = NULL;
1759 
1760 		/*
1761 		**  Ignore jobs that are too expensive for the moment.
1762 		**
1763 		**	Get new load average every GET_NEW_LA_TIME seconds.
1764 		*/
1765 
1766 		SM_GET_LA(now);
1767 		if (shouldqueue(WkRecipFact, Current_LA_time))
1768 		{
1769 			char *msg = "Aborting queue run: load average too high";
1770 
1771 			if (Verbose)
1772 				message("%s", msg);
1773 			if (LogLevel > 8)
1774 				sm_syslog(LOG_INFO, NOQID, "runqueue: %s", msg);
1775 			break;
1776 		}
1777 		if (shouldqueue(w->w_pri, w->w_ctime))
1778 		{
1779 			if (Verbose)
1780 				message(EmptyString);
1781 			if (QueueSortOrder == QSO_BYPRIORITY)
1782 			{
1783 				if (Verbose)
1784 					message("Skipping %s/%s (sequence %d of %d) and flushing rest of queue",
1785 						qid_printqueue(w->w_qgrp,
1786 							       w->w_qdir),
1787 						w->w_name + 2, sequenceno,
1788 						njobs);
1789 				if (LogLevel > 8)
1790 					sm_syslog(LOG_INFO, NOQID,
1791 						  "runqueue: Flushing queue from %s/%s (pri %ld, LA %d, %d of %d)",
1792 						  qid_printqueue(w->w_qgrp,
1793 								 w->w_qdir),
1794 						  w->w_name + 2, w->w_pri,
1795 						  CurrentLA, sequenceno,
1796 						  njobs);
1797 				break;
1798 			}
1799 			else if (Verbose)
1800 				message("Skipping %s/%s (sequence %d of %d)",
1801 					qid_printqueue(w->w_qgrp, w->w_qdir),
1802 					w->w_name + 2, sequenceno, njobs);
1803 		}
1804 		else
1805 		{
1806 			if (Verbose)
1807 			{
1808 				message(EmptyString);
1809 				message("Running %s/%s (sequence %d of %d)",
1810 					qid_printqueue(w->w_qgrp, w->w_qdir),
1811 					w->w_name + 2, sequenceno, njobs);
1812 			}
1813 			if (didfork && MaxQueueChildren > 0)
1814 			{
1815 				sm_blocksignal(SIGCHLD);
1816 				(void) sm_signal(SIGCHLD, reapchild);
1817 			}
1818 			if (tTd(63, 100))
1819 				sm_syslog(LOG_DEBUG, NOQID,
1820 					  "runqueue %s dowork(%s)",
1821 					  qid_printqueue(w->w_qgrp, w->w_qdir),
1822 					  w->w_name + 2);
1823 
1824 			(void) dowork(w->w_qgrp, w->w_qdir, w->w_name + 2,
1825 				      ForkQueueRuns, false, e);
1826 			errno = 0;
1827 		}
1828 		sm_free(w->w_name); /* XXX */
1829 		if (w->w_host != NULL)
1830 			sm_free(w->w_host); /* XXX */
1831 		sm_free((char *) w); /* XXX */
1832 		sequenceno += seqjump; /* next sequence number */
1833 #if SM_HEAP_CHECK
1834 		if (sm_debug_active(&DebugLeakQ, 1))
1835 			sm_heap_setgroup(oldgroup);
1836 #endif /* SM_HEAP_CHECK */
1837 	}
1838 
1839 	BlockOldsh = false;
1840 
1841 	/* check the signals didn't happen during the revert */
1842 	if (NoMoreRunners)
1843 	{
1844 		/* Check that a valid signal handler is callable */
1845 		if (Oldsh != SIG_DFL && Oldsh != SIG_IGN &&
1846 		    Oldsh != runners_sighup && Oldsh != runners_sigterm)
1847 			(*Oldsh)(Oldsig);
1848 	}
1849 
1850 	Oldsh = SIG_DFL; /* after the NoMoreRunners check */
1851 }
1852 /*
1853 **  RUN_WORK_GROUP -- run the jobs in a queue group from a work group.
1854 **
1855 **	Gets the stuff out of the queue in some presumably logical
1856 **	order and processes them.
1857 **
1858 **	Parameters:
1859 **		wgrp -- work group to process.
1860 **		flags -- RWG_* flags
1861 **
1862 **	Returns:
1863 **		true if the queue run successfully began.
1864 **
1865 **	Side Effects:
1866 **		runs things in the mail queue.
1867 */
1868 
1869 /* Minimum sleep time for persistent queue runners */
1870 #define MIN_SLEEP_TIME	5
1871 
1872 bool
1873 run_work_group(wgrp, flags)
1874 	int wgrp;
1875 	int flags;
1876 {
1877 	register ENVELOPE *e;
1878 	int njobs, qdir;
1879 	int sequenceno = 1;
1880 	int qgrp, endgrp, h, i;
1881 	time_t now;
1882 	bool full, more;
1883 	SM_RPOOL_T *rpool;
1884 	extern ENVELOPE BlankEnvelope;
1885 	extern SIGFUNC_DECL reapchild __P((int));
1886 
1887 	if (wgrp < 0)
1888 		return false;
1889 
1890 	/*
1891 	**  If no work will ever be selected, don't even bother reading
1892 	**  the queue.
1893 	*/
1894 
1895 	SM_GET_LA(now);
1896 
1897 	if (!bitset(RWG_PERSISTENT, flags) &&
1898 	    shouldqueue(WkRecipFact, Current_LA_time))
1899 	{
1900 		char *msg = "Skipping queue run -- load average too high";
1901 
1902 		if (bitset(RWG_VERBOSE, flags))
1903 			message("458 %s\n", msg);
1904 		if (LogLevel > 8)
1905 			sm_syslog(LOG_INFO, NOQID, "runqueue: %s", msg);
1906 		return false;
1907 	}
1908 
1909 	/*
1910 	**  See if we already have too many children.
1911 	*/
1912 
1913 	if (bitset(RWG_FORK, flags) &&
1914 	    WorkGrp[wgrp].wg_lowqintvl > 0 &&
1915 	    !bitset(RWG_PERSISTENT, flags) &&
1916 	    MaxChildren > 0 && CurChildren >= MaxChildren)
1917 	{
1918 		char *msg = "Skipping queue run -- too many children";
1919 
1920 		if (bitset(RWG_VERBOSE, flags))
1921 			message("458 %s (%d)\n", msg, CurChildren);
1922 		if (LogLevel > 8)
1923 			sm_syslog(LOG_INFO, NOQID, "runqueue: %s (%d)",
1924 				  msg, CurChildren);
1925 		return false;
1926 	}
1927 
1928 	/*
1929 	**  See if we want to go off and do other useful work.
1930 	*/
1931 
1932 	if (bitset(RWG_FORK, flags))
1933 	{
1934 		pid_t pid;
1935 
1936 		(void) sm_blocksignal(SIGCHLD);
1937 		(void) sm_signal(SIGCHLD, reapchild);
1938 
1939 		pid = dofork();
1940 		if (pid == -1)
1941 		{
1942 			const char *msg = "Skipping queue run -- fork() failed";
1943 			const char *err = sm_errstring(errno);
1944 
1945 			if (bitset(RWG_VERBOSE, flags))
1946 				message("458 %s: %s\n", msg, err);
1947 			if (LogLevel > 8)
1948 				sm_syslog(LOG_INFO, NOQID, "runqueue: %s: %s",
1949 					  msg, err);
1950 			(void) sm_releasesignal(SIGCHLD);
1951 			return false;
1952 		}
1953 		if (pid != 0)
1954 		{
1955 			/* parent -- pick up intermediate zombie */
1956 			(void) sm_blocksignal(SIGALRM);
1957 
1958 			/* wgrp only used when queue runners are persistent */
1959 			proc_list_add(pid, "Queue runner", PROC_QUEUE,
1960 				      WorkGrp[wgrp].wg_maxact,
1961 				      bitset(RWG_PERSISTENT, flags) ? wgrp : -1,
1962 				      NULL);
1963 			(void) sm_releasesignal(SIGALRM);
1964 			(void) sm_releasesignal(SIGCHLD);
1965 			return true;
1966 		}
1967 
1968 		/* child -- clean up signals */
1969 
1970 		/* Reset global flags */
1971 		RestartRequest = NULL;
1972 		RestartWorkGroup = false;
1973 		ShutdownRequest = NULL;
1974 		PendingSignal = 0;
1975 		CurrentPid = getpid();
1976 		close_sendmail_pid();
1977 
1978 		/*
1979 		**  Initialize exception stack and default exception
1980 		**  handler for child process.
1981 		*/
1982 
1983 		sm_exc_newthread(fatal_error);
1984 		clrcontrol();
1985 		proc_list_clear();
1986 
1987 		/* Add parent process as first child item */
1988 		proc_list_add(CurrentPid, "Queue runner child process",
1989 			      PROC_QUEUE_CHILD, 0, -1, NULL);
1990 		(void) sm_releasesignal(SIGCHLD);
1991 		(void) sm_signal(SIGCHLD, SIG_DFL);
1992 		(void) sm_signal(SIGHUP, SIG_DFL);
1993 		(void) sm_signal(SIGTERM, intsig);
1994 	}
1995 
1996 	/*
1997 	**  Release any resources used by the daemon code.
1998 	*/
1999 
2000 	clrdaemon();
2001 
2002 	/* force it to run expensive jobs */
2003 	NoConnect = false;
2004 
2005 	/* drop privileges */
2006 	if (geteuid() == (uid_t) 0)
2007 		(void) drop_privileges(false);
2008 
2009 	/*
2010 	**  Create ourselves an envelope
2011 	*/
2012 
2013 	CurEnv = &QueueEnvelope;
2014 	rpool = sm_rpool_new_x(NULL);
2015 	e = newenvelope(&QueueEnvelope, CurEnv, rpool);
2016 	e->e_flags = BlankEnvelope.e_flags;
2017 	e->e_parent = NULL;
2018 
2019 	/* make sure we have disconnected from parent */
2020 	if (bitset(RWG_FORK, flags))
2021 	{
2022 		disconnect(1, e);
2023 		QuickAbort = false;
2024 	}
2025 
2026 	/*
2027 	**  If we are running part of the queue, always ignore stored
2028 	**  host status.
2029 	*/
2030 
2031 	if (QueueLimitId != NULL || QueueLimitSender != NULL ||
2032 	    QueueLimitQuarantine != NULL ||
2033 	    QueueLimitRecipient != NULL)
2034 	{
2035 		IgnoreHostStatus = true;
2036 		MinQueueAge = 0;
2037 	}
2038 
2039 	/*
2040 	**  Here is where we choose the queue group from the work group.
2041 	**  The caller of the "domorework" label must setup a new envelope.
2042 	*/
2043 
2044 	endgrp = WorkGrp[wgrp].wg_curqgrp; /* to not spin endlessly */
2045 
2046   domorework:
2047 
2048 	/*
2049 	**  Run a queue group if:
2050 	**  RWG_RUNALL bit is set or the bit for this group is set.
2051 	*/
2052 
2053 	now = curtime();
2054 	for (;;)
2055 	{
2056 		/*
2057 		**  Find the next queue group within the work group that
2058 		**  has been marked as needing a run.
2059 		*/
2060 
2061 		qgrp = WorkGrp[wgrp].wg_qgs[WorkGrp[wgrp].wg_curqgrp]->qg_index;
2062 		WorkGrp[wgrp].wg_curqgrp++; /* advance */
2063 		WorkGrp[wgrp].wg_curqgrp %= WorkGrp[wgrp].wg_numqgrp; /* wrap */
2064 		if (bitset(RWG_RUNALL, flags) ||
2065 		    (Queue[qgrp]->qg_nextrun <= now &&
2066 		     Queue[qgrp]->qg_nextrun != (time_t) -1))
2067 			break;
2068 		if (endgrp == WorkGrp[wgrp].wg_curqgrp)
2069 		{
2070 			e->e_id = NULL;
2071 			if (bitset(RWG_FORK, flags))
2072 				finis(true, true, ExitStat);
2073 			return true; /* we're done */
2074 		}
2075 	}
2076 
2077 	qdir = Queue[qgrp]->qg_curnum; /* round-robin init of queue position */
2078 #if _FFR_QUEUE_SCHED_DBG
2079 	if (tTd(69, 12))
2080 		sm_syslog(LOG_INFO, NOQID,
2081 			"rwg: wgrp=%d, qgrp=%d, qdir=%d, name=%s, curqgrp=%d, numgrps=%d",
2082 			wgrp, qgrp, qdir, qid_printqueue(qgrp, qdir),
2083 			WorkGrp[wgrp].wg_curqgrp, WorkGrp[wgrp].wg_numqgrp);
2084 #endif /* _FFR_QUEUE_SCHED_DBG */
2085 
2086 #if HASNICE
2087 	/* tweak niceness of queue runs */
2088 	if (Queue[qgrp]->qg_nice > 0)
2089 		(void) nice(Queue[qgrp]->qg_nice);
2090 #endif /* HASNICE */
2091 
2092 	/* XXX running queue group... */
2093 	sm_setproctitle(true, CurEnv, "running queue: %s",
2094 			qid_printqueue(qgrp, qdir));
2095 
2096 	if (LogLevel > 69 || tTd(63, 99))
2097 		sm_syslog(LOG_DEBUG, NOQID,
2098 			  "runqueue %s, pid=%d, forkflag=%d",
2099 			  qid_printqueue(qgrp, qdir), (int) CurrentPid,
2100 			  bitset(RWG_FORK, flags));
2101 
2102 	/*
2103 	**  Start making passes through the queue.
2104 	**	First, read and sort the entire queue.
2105 	**	Then, process the work in that order.
2106 	**		But if you take too long, start over.
2107 	*/
2108 
2109 	for (i = 0; i < Queue[qgrp]->qg_numqueues; i++)
2110 	{
2111 		h = gatherq(qgrp, qdir, false, &full, &more);
2112 #if SM_CONF_SHM
2113 		if (ShmId != SM_SHM_NO_ID)
2114 			QSHM_ENTRIES(Queue[qgrp]->qg_qpaths[qdir].qp_idx) = h;
2115 #endif /* SM_CONF_SHM */
2116 		/* If there are no more items in this queue advance */
2117 		if (!more)
2118 		{
2119 			/* A round-robin advance */
2120 			qdir++;
2121 			qdir %= Queue[qgrp]->qg_numqueues;
2122 		}
2123 
2124 		/* Has the WorkList reached the limit? */
2125 		if (full)
2126 			break; /* don't try to gather more */
2127 	}
2128 
2129 	/* order the existing work requests */
2130 	njobs = sortq(Queue[qgrp]->qg_maxlist);
2131 	Queue[qgrp]->qg_curnum = qdir; /* update */
2132 
2133 
2134 	if (!Verbose && bitnset(QD_FORK, Queue[qgrp]->qg_flags))
2135 	{
2136 		int loop, maxrunners;
2137 		pid_t pid;
2138 
2139 		/*
2140 		**  For this WorkQ we want to fork off N children (maxrunners)
2141 		**  at this point. Each child has a copy of WorkQ. Each child
2142 		**  will process every N-th item. The parent will wait for all
2143 		**  of the children to finish before moving on to the next
2144 		**  queue group within the work group. This saves us forking
2145 		**  a new runner-child for each work item.
2146 		**  It's valid for qg_maxqrun == 0 since this may be an
2147 		**  explicit "don't run this queue" setting.
2148 		*/
2149 
2150 		maxrunners = Queue[qgrp]->qg_maxqrun;
2151 
2152 		/* No need to have more runners then there are jobs */
2153 		if (maxrunners > njobs)
2154 			maxrunners = njobs;
2155 		for (loop = 0; loop < maxrunners; loop++)
2156 		{
2157 			/*
2158 			**  Since the delivery may happen in a child and the
2159 			**  parent does not wait, the parent may close the
2160 			**  maps thereby removing any shared memory used by
2161 			**  the map.  Therefore, close the maps now so the
2162 			**  child will dynamically open them if necessary.
2163 			*/
2164 
2165 			closemaps(false);
2166 
2167 			pid = fork();
2168 			if (pid < 0)
2169 			{
2170 				syserr("run_work_group: cannot fork");
2171 				return false;
2172 			}
2173 			else if (pid > 0)
2174 			{
2175 				/* parent -- clean out connection cache */
2176 				mci_flush(false, NULL);
2177 #if _FFR_SKIP_DOMAINS
2178 				if (QueueSortOrder == QSO_BYHOST)
2179 				{
2180 					sequenceno += skip_domains(1);
2181 				}
2182 				else
2183 #endif /* _FFR_SKIP_DOMAINS */
2184 				{
2185 					/* for the skip */
2186 					WorkQ = WorkQ->w_next;
2187 					sequenceno++;
2188 				}
2189 				proc_list_add(pid, "Queue child runner process",
2190 					      PROC_QUEUE_CHILD, 0, -1, NULL);
2191 
2192 				/* No additional work, no additional runners */
2193 				if (WorkQ == NULL)
2194 					break;
2195 			}
2196 			else
2197 			{
2198 				/* child -- Reset global flags */
2199 				RestartRequest = NULL;
2200 				RestartWorkGroup = false;
2201 				ShutdownRequest = NULL;
2202 				PendingSignal = 0;
2203 				CurrentPid = getpid();
2204 				close_sendmail_pid();
2205 
2206 				/*
2207 				**  Initialize exception stack and default
2208 				**  exception handler for child process.
2209 				**  When fork()'d the child now has a private
2210 				**  copy of WorkQ at its current position.
2211 				*/
2212 
2213 				sm_exc_newthread(fatal_error);
2214 
2215 				/*
2216 				**  SMTP processes (whether -bd or -bs) set
2217 				**  SIGCHLD to reapchild to collect
2218 				**  children status.  However, at delivery
2219 				**  time, that status must be collected
2220 				**  by sm_wait() to be dealt with properly
2221 				**  (check success of delivery based
2222 				**  on status code, etc).  Therefore, if we
2223 				**  are an SMTP process, reset SIGCHLD
2224 				**  back to the default so reapchild
2225 				**  doesn't collect status before
2226 				**  sm_wait().
2227 				*/
2228 
2229 				if (OpMode == MD_SMTP ||
2230 				    OpMode == MD_DAEMON ||
2231 				    MaxQueueChildren > 0)
2232 				{
2233 					proc_list_clear();
2234 					sm_releasesignal(SIGCHLD);
2235 					(void) sm_signal(SIGCHLD, SIG_DFL);
2236 				}
2237 
2238 				/* child -- error messages to the transcript */
2239 				QuickAbort = OnlyOneError = false;
2240 				runner_work(e, sequenceno, true,
2241 					    maxrunners, njobs);
2242 
2243 				/* This child is done */
2244 				finis(true, true, ExitStat);
2245 				/* NOTREACHED */
2246 			}
2247 		}
2248 
2249 		sm_releasesignal(SIGCHLD);
2250 
2251 		/*
2252 		**  Wait until all of the runners have completed before
2253 		**  seeing if there is another queue group in the
2254 		**  work group to process.
2255 		**  XXX Future enhancement: don't wait() for all children
2256 		**  here, just go ahead and make sure that overall the number
2257 		**  of children is not exceeded.
2258 		*/
2259 
2260 		while (CurChildren > 0)
2261 		{
2262 			int status;
2263 			pid_t ret;
2264 
2265 			while ((ret = sm_wait(&status)) <= 0)
2266 				continue;
2267 			proc_list_drop(ret, status, NULL);
2268 		}
2269 	}
2270 	else if (Queue[qgrp]->qg_maxqrun > 0 || bitset(RWG_FORCE, flags))
2271 	{
2272 		/*
2273 		**  When current process will not fork children to do the work,
2274 		**  it will do the work itself. The 'skip' will be 1 since
2275 		**  there are no child runners to divide the work across.
2276 		*/
2277 
2278 		runner_work(e, sequenceno, false, 1, njobs);
2279 	}
2280 
2281 	/* free memory allocated by newenvelope() above */
2282 	sm_rpool_free(rpool);
2283 	QueueEnvelope.e_rpool = NULL;
2284 
2285 	/* Are there still more queues in the work group to process? */
2286 	if (endgrp != WorkGrp[wgrp].wg_curqgrp)
2287 	{
2288 		rpool = sm_rpool_new_x(NULL);
2289 		e = newenvelope(&QueueEnvelope, CurEnv, rpool);
2290 		e->e_flags = BlankEnvelope.e_flags;
2291 		goto domorework;
2292 	}
2293 
2294 	/* No more queues in work group to process. Now check persistent. */
2295 	if (bitset(RWG_PERSISTENT, flags))
2296 	{
2297 		sequenceno = 1;
2298 		sm_setproctitle(true, CurEnv, "running queue: %s",
2299 				qid_printqueue(qgrp, qdir));
2300 
2301 		/*
2302 		**  close bogus maps, i.e., maps which caused a tempfail,
2303 		**	so we get fresh map connections on the next lookup.
2304 		**  closemaps() is also called when children are started.
2305 		*/
2306 
2307 		closemaps(true);
2308 
2309 		/* Close any cached connections. */
2310 		mci_flush(true, NULL);
2311 
2312 		/* Clean out expired related entries. */
2313 		rmexpstab();
2314 
2315 #if NAMED_BIND
2316 		/* Update MX records for FallbackMX. */
2317 		if (FallbackMX != NULL)
2318 			(void) getfallbackmxrr(FallbackMX);
2319 #endif /* NAMED_BIND */
2320 
2321 #if USERDB
2322 		/* close UserDatabase */
2323 		_udbx_close();
2324 #endif /* USERDB */
2325 
2326 #if SM_HEAP_CHECK
2327 		if (sm_debug_active(&SmHeapCheck, 2)
2328 		    && access("memdump", F_OK) == 0
2329 		   )
2330 		{
2331 			SM_FILE_T *out;
2332 
2333 			remove("memdump");
2334 			out = sm_io_open(SmFtStdio, SM_TIME_DEFAULT,
2335 					 "memdump.out", SM_IO_APPEND, NULL);
2336 			if (out != NULL)
2337 			{
2338 				(void) sm_io_fprintf(out, SM_TIME_DEFAULT, "----------------------\n");
2339 				sm_heap_report(out,
2340 					sm_debug_level(&SmHeapCheck) - 1);
2341 				(void) sm_io_close(out, SM_TIME_DEFAULT);
2342 			}
2343 		}
2344 #endif /* SM_HEAP_CHECK */
2345 
2346 		/* let me rest for a second to catch my breath */
2347 		if (njobs == 0 && WorkGrp[wgrp].wg_lowqintvl < MIN_SLEEP_TIME)
2348 			sleep(MIN_SLEEP_TIME);
2349 		else if (WorkGrp[wgrp].wg_lowqintvl <= 0)
2350 			sleep(QueueIntvl > 0 ? QueueIntvl : MIN_SLEEP_TIME);
2351 		else
2352 			sleep(WorkGrp[wgrp].wg_lowqintvl);
2353 
2354 		/*
2355 		**  Get the LA outside the WorkQ loop if necessary.
2356 		**  In a persistent queue runner the code is repeated over
2357 		**  and over but gatherq() may ignore entries due to
2358 		**  shouldqueue() (do we really have to do this twice?).
2359 		**  Hence the queue runners would just idle around when once
2360 		**  CurrentLA caused all entries in a queue to be ignored.
2361 		*/
2362 
2363 		if (njobs == 0)
2364 			SM_GET_LA(now);
2365 		rpool = sm_rpool_new_x(NULL);
2366 		e = newenvelope(&QueueEnvelope, CurEnv, rpool);
2367 		e->e_flags = BlankEnvelope.e_flags;
2368 		goto domorework;
2369 	}
2370 
2371 	/* exit without the usual cleanup */
2372 	e->e_id = NULL;
2373 	if (bitset(RWG_FORK, flags))
2374 		finis(true, true, ExitStat);
2375 	/* NOTREACHED */
2376 	return true;
2377 }
2378 
2379 /*
2380 **  DOQUEUERUN -- do a queue run?
2381 */
2382 
2383 bool
2384 doqueuerun()
2385 {
2386 	return DoQueueRun;
2387 }
2388 
2389 /*
2390 **  RUNQUEUEEVENT -- Sets a flag to indicate that a queue run should be done.
2391 **
2392 **	Parameters:
2393 **		none.
2394 **
2395 **	Returns:
2396 **		none.
2397 **
2398 **	Side Effects:
2399 **		The invocation of this function via an alarm may interrupt
2400 **		a set of actions. Thus errno may be set in that context.
2401 **		We need to restore errno at the end of this function to ensure
2402 **		that any work done here that sets errno doesn't return a
2403 **		misleading/false errno value. Errno may	be EINTR upon entry to
2404 **		this function because of non-restartable/continuable system
2405 **		API was active. Iff this is true we will override errno as
2406 **		a timeout (as a more accurate error message).
2407 **
2408 **	NOTE:	THIS CAN BE CALLED FROM A SIGNAL HANDLER.  DO NOT ADD
2409 **		ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
2410 **		DOING.
2411 */
2412 
2413 void
2414 runqueueevent(ignore)
2415 	int ignore;
2416 {
2417 	int save_errno = errno;
2418 
2419 	/*
2420 	**  Set the general bit that we want a queue run,
2421 	**  tested in doqueuerun()
2422 	*/
2423 
2424 	DoQueueRun = true;
2425 #if _FFR_QUEUE_SCHED_DBG
2426 	if (tTd(69, 10))
2427 		sm_syslog(LOG_INFO, NOQID, "rqe: done");
2428 #endif /* _FFR_QUEUE_SCHED_DBG */
2429 
2430 	errno = save_errno;
2431 	if (errno == EINTR)
2432 		errno = ETIMEDOUT;
2433 }
2434 /*
2435 **  GATHERQ -- gather messages from the message queue(s) the work queue.
2436 **
2437 **	Parameters:
2438 **		qgrp -- the index of the queue group.
2439 **		qdir -- the index of the queue directory.
2440 **		doall -- if set, include everything in the queue (even
2441 **			the jobs that cannot be run because the load
2442 **			average is too high, or MaxQueueRun is reached).
2443 **			Otherwise, exclude those jobs.
2444 **		full -- (optional) to be set 'true' if WorkList is full
2445 **		more -- (optional) to be set 'true' if there are still more
2446 **			messages in this queue not added to WorkList
2447 **
2448 **	Returns:
2449 **		The number of request in the queue (not necessarily
2450 **		the number of requests in WorkList however).
2451 **
2452 **	Side Effects:
2453 **		prepares available work into WorkList
2454 */
2455 
2456 #define NEED_P		0001	/* 'P': priority */
2457 #define NEED_T		0002	/* 'T': time */
2458 #define NEED_R		0004	/* 'R': recipient */
2459 #define NEED_S		0010	/* 'S': sender */
2460 #define NEED_H		0020	/* host */
2461 #define HAS_QUARANTINE	0040	/* has an unexpected 'q' line */
2462 #define NEED_QUARANTINE	0100	/* 'q': reason */
2463 
2464 static WORK	*WorkList = NULL;	/* list of unsort work */
2465 static int	WorkListSize = 0;	/* current max size of WorkList */
2466 static int	WorkListCount = 0;	/* # of work items in WorkList */
2467 
2468 static int
2469 gatherq(qgrp, qdir, doall, full, more)
2470 	int qgrp;
2471 	int qdir;
2472 	bool doall;
2473 	bool *full;
2474 	bool *more;
2475 {
2476 	register struct dirent *d;
2477 	register WORK *w;
2478 	register char *p;
2479 	DIR *f;
2480 	int i, num_ent;
2481 	int wn;
2482 	QUEUE_CHAR *check;
2483 	char qd[MAXPATHLEN];
2484 	char qf[MAXPATHLEN];
2485 
2486 	wn = WorkListCount - 1;
2487 	num_ent = 0;
2488 	if (qdir == NOQDIR)
2489 		(void) sm_strlcpy(qd, ".", sizeof(qd));
2490 	else
2491 		(void) sm_strlcpyn(qd, sizeof(qd), 2,
2492 			Queue[qgrp]->qg_qpaths[qdir].qp_name,
2493 			(bitset(QP_SUBQF,
2494 				Queue[qgrp]->qg_qpaths[qdir].qp_subdirs)
2495 					? "/qf" : ""));
2496 
2497 	if (tTd(41, 1))
2498 	{
2499 		sm_dprintf("gatherq:\n");
2500 
2501 		check = QueueLimitId;
2502 		while (check != NULL)
2503 		{
2504 			sm_dprintf("\tQueueLimitId = %s%s\n",
2505 				check->queue_negate ? "!" : "",
2506 				check->queue_match);
2507 			check = check->queue_next;
2508 		}
2509 
2510 		check = QueueLimitSender;
2511 		while (check != NULL)
2512 		{
2513 			sm_dprintf("\tQueueLimitSender = %s%s\n",
2514 				check->queue_negate ? "!" : "",
2515 				check->queue_match);
2516 			check = check->queue_next;
2517 		}
2518 
2519 		check = QueueLimitRecipient;
2520 		while (check != NULL)
2521 		{
2522 			sm_dprintf("\tQueueLimitRecipient = %s%s\n",
2523 				check->queue_negate ? "!" : "",
2524 				check->queue_match);
2525 			check = check->queue_next;
2526 		}
2527 
2528 		if (QueueMode == QM_QUARANTINE)
2529 		{
2530 			check = QueueLimitQuarantine;
2531 			while (check != NULL)
2532 			{
2533 				sm_dprintf("\tQueueLimitQuarantine = %s%s\n",
2534 					   check->queue_negate ? "!" : "",
2535 					   check->queue_match);
2536 				check = check->queue_next;
2537 			}
2538 		}
2539 	}
2540 
2541 	/* open the queue directory */
2542 	f = opendir(qd);
2543 	if (f == NULL)
2544 	{
2545 		syserr("gatherq: cannot open \"%s\"",
2546 			qid_printqueue(qgrp, qdir));
2547 		if (full != NULL)
2548 			*full = WorkListCount >= MaxQueueRun && MaxQueueRun > 0;
2549 		if (more != NULL)
2550 			*more = false;
2551 		return 0;
2552 	}
2553 
2554 	/*
2555 	**  Read the work directory.
2556 	*/
2557 
2558 	while ((d = readdir(f)) != NULL)
2559 	{
2560 		SM_FILE_T *cf;
2561 		int qfver = 0;
2562 		char lbuf[MAXNAME + 1];
2563 		struct stat sbuf;
2564 
2565 		if (tTd(41, 50))
2566 			sm_dprintf("gatherq: checking %s..", d->d_name);
2567 
2568 		/* is this an interesting entry? */
2569 		if (!(((QueueMode == QM_NORMAL &&
2570 			d->d_name[0] == NORMQF_LETTER) ||
2571 		       (QueueMode == QM_QUARANTINE &&
2572 			d->d_name[0] == QUARQF_LETTER) ||
2573 		       (QueueMode == QM_LOST &&
2574 			d->d_name[0] == LOSEQF_LETTER)) &&
2575 		      d->d_name[1] == 'f'))
2576 		{
2577 			if (tTd(41, 50))
2578 				sm_dprintf("  skipping\n");
2579 			continue;
2580 		}
2581 		if (tTd(41, 50))
2582 			sm_dprintf("\n");
2583 
2584 		if (strlen(d->d_name) >= MAXQFNAME)
2585 		{
2586 			if (Verbose)
2587 				(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
2588 						     "gatherq: %s too long, %d max characters\n",
2589 						     d->d_name, MAXQFNAME);
2590 			if (LogLevel > 0)
2591 				sm_syslog(LOG_ALERT, NOQID,
2592 					  "gatherq: %s too long, %d max characters",
2593 					  d->d_name, MAXQFNAME);
2594 			continue;
2595 		}
2596 
2597 		check = QueueLimitId;
2598 		while (check != NULL)
2599 		{
2600 			if (strcontainedin(false, check->queue_match,
2601 					   d->d_name) != check->queue_negate)
2602 				break;
2603 			else
2604 				check = check->queue_next;
2605 		}
2606 		if (QueueLimitId != NULL && check == NULL)
2607 			continue;
2608 
2609 		/* grow work list if necessary */
2610 		if (++wn >= MaxQueueRun && MaxQueueRun > 0)
2611 		{
2612 			if (wn == MaxQueueRun && LogLevel > 0)
2613 				sm_syslog(LOG_WARNING, NOQID,
2614 					  "WorkList for %s maxed out at %d",
2615 					  qid_printqueue(qgrp, qdir),
2616 					  MaxQueueRun);
2617 			if (doall)
2618 				continue;	/* just count entries */
2619 			break;
2620 		}
2621 		if (wn >= WorkListSize)
2622 		{
2623 			grow_wlist(qgrp, qdir);
2624 			if (wn >= WorkListSize)
2625 				continue;
2626 		}
2627 		SM_ASSERT(wn >= 0);
2628 		w = &WorkList[wn];
2629 
2630 		(void) sm_strlcpyn(qf, sizeof(qf), 3, qd, "/", d->d_name);
2631 		if (stat(qf, &sbuf) < 0)
2632 		{
2633 			if (errno != ENOENT)
2634 				sm_syslog(LOG_INFO, NOQID,
2635 					  "gatherq: can't stat %s/%s",
2636 					  qid_printqueue(qgrp, qdir),
2637 					  d->d_name);
2638 			wn--;
2639 			continue;
2640 		}
2641 		if (!bitset(S_IFREG, sbuf.st_mode))
2642 		{
2643 			/* Yikes!  Skip it or we will hang on open! */
2644 			if (!((d->d_name[0] == DATAFL_LETTER ||
2645 			       d->d_name[0] == NORMQF_LETTER ||
2646 			       d->d_name[0] == QUARQF_LETTER ||
2647 			       d->d_name[0] == LOSEQF_LETTER ||
2648 			       d->d_name[0] == XSCRPT_LETTER) &&
2649 			      d->d_name[1] == 'f' && d->d_name[2] == '\0'))
2650 				syserr("gatherq: %s/%s is not a regular file",
2651 				       qid_printqueue(qgrp, qdir), d->d_name);
2652 			wn--;
2653 			continue;
2654 		}
2655 
2656 		/* avoid work if possible */
2657 		if ((QueueSortOrder == QSO_BYFILENAME ||
2658 		     QueueSortOrder == QSO_BYMODTIME ||
2659 		     QueueSortOrder == QSO_NONE ||
2660 		     QueueSortOrder == QSO_RANDOM) &&
2661 		    QueueLimitQuarantine == NULL &&
2662 		    QueueLimitSender == NULL &&
2663 		    QueueLimitRecipient == NULL)
2664 		{
2665 			w->w_qgrp = qgrp;
2666 			w->w_qdir = qdir;
2667 			w->w_name = newstr(d->d_name);
2668 			w->w_host = NULL;
2669 			w->w_lock = w->w_tooyoung = false;
2670 			w->w_pri = 0;
2671 			w->w_ctime = 0;
2672 			w->w_mtime = sbuf.st_mtime;
2673 			++num_ent;
2674 			continue;
2675 		}
2676 
2677 		/* open control file */
2678 		cf = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, qf, SM_IO_RDONLY_B,
2679 				NULL);
2680 		if (cf == NULL && OpMode != MD_PRINT)
2681 		{
2682 			/* this may be some random person sending hir msgs */
2683 			if (tTd(41, 2))
2684 				sm_dprintf("gatherq: cannot open %s: %s\n",
2685 					d->d_name, sm_errstring(errno));
2686 			errno = 0;
2687 			wn--;
2688 			continue;
2689 		}
2690 		w->w_qgrp = qgrp;
2691 		w->w_qdir = qdir;
2692 		w->w_name = newstr(d->d_name);
2693 		w->w_host = NULL;
2694 		if (cf != NULL)
2695 		{
2696 			w->w_lock = !lockfile(sm_io_getinfo(cf, SM_IO_WHAT_FD,
2697 							    NULL),
2698 					      w->w_name, NULL,
2699 					      LOCK_SH|LOCK_NB);
2700 		}
2701 		w->w_tooyoung = false;
2702 
2703 		/* make sure jobs in creation don't clog queue */
2704 		w->w_pri = 0x7fffffff;
2705 		w->w_ctime = 0;
2706 		w->w_mtime = sbuf.st_mtime;
2707 
2708 		/* extract useful information */
2709 		i = NEED_P|NEED_T;
2710 		if (QueueSortOrder == QSO_BYHOST
2711 #if _FFR_RHS
2712 		    || QueueSortOrder == QSO_BYSHUFFLE
2713 #endif /* _FFR_RHS */
2714 		   )
2715 		{
2716 			/* need w_host set for host sort order */
2717 			i |= NEED_H;
2718 		}
2719 		if (QueueLimitSender != NULL)
2720 			i |= NEED_S;
2721 		if (QueueLimitRecipient != NULL)
2722 			i |= NEED_R;
2723 		if (QueueLimitQuarantine != NULL)
2724 			i |= NEED_QUARANTINE;
2725 		while (cf != NULL && i != 0 &&
2726 		       sm_io_fgets(cf, SM_TIME_DEFAULT, lbuf,
2727 				   sizeof(lbuf)) != NULL)
2728 		{
2729 			int c;
2730 			time_t age;
2731 
2732 			p = strchr(lbuf, '\n');
2733 			if (p != NULL)
2734 				*p = '\0';
2735 			else
2736 			{
2737 				/* flush rest of overly long line */
2738 				while ((c = sm_io_getc(cf, SM_TIME_DEFAULT))
2739 				       != SM_IO_EOF && c != '\n')
2740 					continue;
2741 			}
2742 
2743 			switch (lbuf[0])
2744 			{
2745 			  case 'V':
2746 				qfver = atoi(&lbuf[1]);
2747 				break;
2748 
2749 			  case 'P':
2750 				w->w_pri = atol(&lbuf[1]);
2751 				i &= ~NEED_P;
2752 				break;
2753 
2754 			  case 'T':
2755 				w->w_ctime = atol(&lbuf[1]);
2756 				i &= ~NEED_T;
2757 				break;
2758 
2759 			  case 'q':
2760 				if (QueueMode != QM_QUARANTINE &&
2761 				    QueueMode != QM_LOST)
2762 				{
2763 					if (tTd(41, 49))
2764 						sm_dprintf("%s not marked as quarantined but has a 'q' line\n",
2765 							   w->w_name);
2766 					i |= HAS_QUARANTINE;
2767 				}
2768 				else if (QueueMode == QM_QUARANTINE)
2769 				{
2770 					if (QueueLimitQuarantine == NULL)
2771 					{
2772 						i &= ~NEED_QUARANTINE;
2773 						break;
2774 					}
2775 					p = &lbuf[1];
2776 					check = QueueLimitQuarantine;
2777 					while (check != NULL)
2778 					{
2779 						if (strcontainedin(false,
2780 								   check->queue_match,
2781 								   p) !=
2782 						    check->queue_negate)
2783 							break;
2784 						else
2785 							check = check->queue_next;
2786 					}
2787 					if (check != NULL)
2788 						i &= ~NEED_QUARANTINE;
2789 				}
2790 				break;
2791 
2792 			  case 'R':
2793 				if (w->w_host == NULL &&
2794 				    (p = strrchr(&lbuf[1], '@')) != NULL)
2795 				{
2796 #if _FFR_RHS
2797 					if (QueueSortOrder == QSO_BYSHUFFLE)
2798 						w->w_host = newstr(&p[1]);
2799 					else
2800 #endif /* _FFR_RHS */
2801 						w->w_host = strrev(&p[1]);
2802 					makelower(w->w_host);
2803 					i &= ~NEED_H;
2804 				}
2805 				if (QueueLimitRecipient == NULL)
2806 				{
2807 					i &= ~NEED_R;
2808 					break;
2809 				}
2810 				if (qfver > 0)
2811 				{
2812 					p = strchr(&lbuf[1], ':');
2813 					if (p == NULL)
2814 						p = &lbuf[1];
2815 					else
2816 						++p; /* skip over ':' */
2817 				}
2818 				else
2819 					p = &lbuf[1];
2820 				check = QueueLimitRecipient;
2821 				while (check != NULL)
2822 				{
2823 					if (strcontainedin(true,
2824 							   check->queue_match,
2825 							   p) !=
2826 					    check->queue_negate)
2827 						break;
2828 					else
2829 						check = check->queue_next;
2830 				}
2831 				if (check != NULL)
2832 					i &= ~NEED_R;
2833 				break;
2834 
2835 			  case 'S':
2836 				check = QueueLimitSender;
2837 				while (check != NULL)
2838 				{
2839 					if (strcontainedin(true,
2840 							   check->queue_match,
2841 							   &lbuf[1]) !=
2842 					    check->queue_negate)
2843 						break;
2844 					else
2845 						check = check->queue_next;
2846 				}
2847 				if (check != NULL)
2848 					i &= ~NEED_S;
2849 				break;
2850 
2851 			  case 'K':
2852 				age = curtime() - (time_t) atol(&lbuf[1]);
2853 				if (age >= 0 && MinQueueAge > 0 &&
2854 				    age < MinQueueAge)
2855 					w->w_tooyoung = true;
2856 				break;
2857 
2858 			  case 'N':
2859 				if (atol(&lbuf[1]) == 0)
2860 					w->w_tooyoung = false;
2861 				break;
2862 			}
2863 		}
2864 		if (cf != NULL)
2865 			(void) sm_io_close(cf, SM_TIME_DEFAULT);
2866 
2867 		if ((!doall && (shouldqueue(w->w_pri, w->w_ctime) ||
2868 		    w->w_tooyoung)) ||
2869 		    bitset(HAS_QUARANTINE, i) ||
2870 		    bitset(NEED_QUARANTINE, i) ||
2871 		    bitset(NEED_R|NEED_S, i))
2872 		{
2873 			/* don't even bother sorting this job in */
2874 			if (tTd(41, 49))
2875 				sm_dprintf("skipping %s (%x)\n", w->w_name, i);
2876 			sm_free(w->w_name); /* XXX */
2877 			if (w->w_host != NULL)
2878 				sm_free(w->w_host); /* XXX */
2879 			wn--;
2880 		}
2881 		else
2882 			++num_ent;
2883 	}
2884 	(void) closedir(f);
2885 	wn++;
2886 
2887 	i = wn - WorkListCount;
2888 	WorkListCount += SM_MIN(num_ent, WorkListSize);
2889 
2890 	if (more != NULL)
2891 		*more = WorkListCount < wn;
2892 
2893 	if (full != NULL)
2894 		*full = (wn >= MaxQueueRun && MaxQueueRun > 0) ||
2895 			(WorkList == NULL && wn > 0);
2896 
2897 	return i;
2898 }
2899 /*
2900 **  SORTQ -- sort the work list
2901 **
2902 **	First the old WorkQ is cleared away. Then the WorkList is sorted
2903 **	for all items so that important (higher sorting value) items are not
2904 **	trunctated off. Then the most important items are moved from
2905 **	WorkList to WorkQ. The lower count of 'max' or MaxListCount items
2906 **	are moved.
2907 **
2908 **	Parameters:
2909 **		max -- maximum number of items to be placed in WorkQ
2910 **
2911 **	Returns:
2912 **		the number of items in WorkQ
2913 **
2914 **	Side Effects:
2915 **		WorkQ gets released and filled with new work. WorkList
2916 **		gets released. Work items get sorted in order.
2917 */
2918 
2919 static int
2920 sortq(max)
2921 	int max;
2922 {
2923 	register int i;			/* local counter */
2924 	register WORK *w;		/* tmp item pointer */
2925 	int wc = WorkListCount;		/* trim size for WorkQ */
2926 
2927 	if (WorkQ != NULL)
2928 	{
2929 		WORK *nw;
2930 
2931 		/* Clear out old WorkQ. */
2932 		for (w = WorkQ; w != NULL; w = nw)
2933 		{
2934 			nw = w->w_next;
2935 			sm_free(w->w_name); /* XXX */
2936 			if (w->w_host != NULL)
2937 				sm_free(w->w_host); /* XXX */
2938 			sm_free((char *) w); /* XXX */
2939 		}
2940 		WorkQ = NULL;
2941 	}
2942 
2943 	if (WorkList == NULL || wc <= 0)
2944 		return 0;
2945 
2946 	/*
2947 	**  The sort now takes place using all of the items in WorkList.
2948 	**  The list gets trimmed to the most important items after the sort.
2949 	**  If the trim were to happen before the sort then one or more
2950 	**  important items might get truncated off -- not what we want.
2951 	*/
2952 
2953 	if (QueueSortOrder == QSO_BYHOST)
2954 	{
2955 		/*
2956 		**  Sort the work directory for the first time,
2957 		**  based on host name, lock status, and priority.
2958 		*/
2959 
2960 		qsort((char *) WorkList, wc, sizeof(*WorkList), workcmpf1);
2961 
2962 		/*
2963 		**  If one message to host is locked, "lock" all messages
2964 		**  to that host.
2965 		*/
2966 
2967 		i = 0;
2968 		while (i < wc)
2969 		{
2970 			if (!WorkList[i].w_lock)
2971 			{
2972 				i++;
2973 				continue;
2974 			}
2975 			w = &WorkList[i];
2976 			while (++i < wc)
2977 			{
2978 				if (WorkList[i].w_host == NULL &&
2979 				    w->w_host == NULL)
2980 					WorkList[i].w_lock = true;
2981 				else if (WorkList[i].w_host != NULL &&
2982 					 w->w_host != NULL &&
2983 					 sm_strcasecmp(WorkList[i].w_host,
2984 						       w->w_host) == 0)
2985 					WorkList[i].w_lock = true;
2986 				else
2987 					break;
2988 			}
2989 		}
2990 
2991 		/*
2992 		**  Sort the work directory for the second time,
2993 		**  based on lock status, host name, and priority.
2994 		*/
2995 
2996 		qsort((char *) WorkList, wc, sizeof(*WorkList), workcmpf2);
2997 	}
2998 	else if (QueueSortOrder == QSO_BYTIME)
2999 	{
3000 		/*
3001 		**  Simple sort based on submission time only.
3002 		*/
3003 
3004 		qsort((char *) WorkList, wc, sizeof(*WorkList), workcmpf3);
3005 	}
3006 	else if (QueueSortOrder == QSO_BYFILENAME)
3007 	{
3008 		/*
3009 		**  Sort based on queue filename.
3010 		*/
3011 
3012 		qsort((char *) WorkList, wc, sizeof(*WorkList), workcmpf4);
3013 	}
3014 	else if (QueueSortOrder == QSO_RANDOM)
3015 	{
3016 		/*
3017 		**  Sort randomly.  To avoid problems with an instable sort,
3018 		**  use a random index into the queue file name to start
3019 		**  comparison.
3020 		*/
3021 
3022 		randi = get_rand_mod(MAXQFNAME);
3023 		if (randi < 2)
3024 			randi = 3;
3025 		qsort((char *) WorkList, wc, sizeof(*WorkList), workcmpf5);
3026 	}
3027 	else if (QueueSortOrder == QSO_BYMODTIME)
3028 	{
3029 		/*
3030 		**  Simple sort based on modification time of queue file.
3031 		**  This puts the oldest items first.
3032 		*/
3033 
3034 		qsort((char *) WorkList, wc, sizeof(*WorkList), workcmpf6);
3035 	}
3036 #if _FFR_RHS
3037 	else if (QueueSortOrder == QSO_BYSHUFFLE)
3038 	{
3039 		/*
3040 		**  Simple sort based on shuffled host name.
3041 		*/
3042 
3043 		init_shuffle_alphabet();
3044 		qsort((char *) WorkList, wc, sizeof(*WorkList), workcmpf7);
3045 	}
3046 #endif /* _FFR_RHS */
3047 	else if (QueueSortOrder == QSO_BYPRIORITY)
3048 	{
3049 		/*
3050 		**  Simple sort based on queue priority only.
3051 		*/
3052 
3053 		qsort((char *) WorkList, wc, sizeof(*WorkList), workcmpf0);
3054 	}
3055 	/* else don't sort at all */
3056 
3057 	/* Check if the per queue group item limit will be exceeded */
3058 	if (wc > max && max > 0)
3059 		wc = max;
3060 
3061 	/*
3062 	**  Convert the work list into canonical form.
3063 	**	Should be turning it into a list of envelopes here perhaps.
3064 	**  Only take the most important items up to the per queue group
3065 	**  maximum.
3066 	*/
3067 
3068 	for (i = wc; --i >= 0; )
3069 	{
3070 		w = (WORK *) xalloc(sizeof(*w));
3071 		w->w_qgrp = WorkList[i].w_qgrp;
3072 		w->w_qdir = WorkList[i].w_qdir;
3073 		w->w_name = WorkList[i].w_name;
3074 		w->w_host = WorkList[i].w_host;
3075 		w->w_lock = WorkList[i].w_lock;
3076 		w->w_tooyoung = WorkList[i].w_tooyoung;
3077 		w->w_pri = WorkList[i].w_pri;
3078 		w->w_ctime = WorkList[i].w_ctime;
3079 		w->w_mtime = WorkList[i].w_mtime;
3080 		w->w_next = WorkQ;
3081 		WorkQ = w;
3082 	}
3083 
3084 	/* free the rest of the list */
3085 	for (i = WorkListCount; --i >= wc; )
3086 	{
3087 		sm_free(WorkList[i].w_name);
3088 		if (WorkList[i].w_host != NULL)
3089 			sm_free(WorkList[i].w_host);
3090 	}
3091 
3092 	if (WorkList != NULL)
3093 		sm_free(WorkList); /* XXX */
3094 	WorkList = NULL;
3095 	WorkListSize = 0;
3096 	WorkListCount = 0;
3097 
3098 	if (tTd(40, 1))
3099 	{
3100 		for (w = WorkQ; w != NULL; w = w->w_next)
3101 		{
3102 			if (w->w_host != NULL)
3103 				sm_dprintf("%22s: pri=%ld %s\n",
3104 					w->w_name, w->w_pri, w->w_host);
3105 			else
3106 				sm_dprintf("%32s: pri=%ld\n",
3107 					w->w_name, w->w_pri);
3108 		}
3109 	}
3110 
3111 	return wc; /* return number of WorkQ items */
3112 }
3113 /*
3114 **  GROW_WLIST -- make the work list larger
3115 **
3116 **	Parameters:
3117 **		qgrp -- the index for the queue group.
3118 **		qdir -- the index for the queue directory.
3119 **
3120 **	Returns:
3121 **		none.
3122 **
3123 **	Side Effects:
3124 **		Adds another QUEUESEGSIZE entries to WorkList if possible.
3125 **		It can fail if there isn't enough memory, so WorkListSize
3126 **		should be checked again upon return.
3127 */
3128 
3129 static void
3130 grow_wlist(qgrp, qdir)
3131 	int qgrp;
3132 	int qdir;
3133 {
3134 	if (tTd(41, 1))
3135 		sm_dprintf("grow_wlist: WorkListSize=%d\n", WorkListSize);
3136 	if (WorkList == NULL)
3137 	{
3138 		WorkList = (WORK *) xalloc((sizeof(*WorkList)) *
3139 					   (QUEUESEGSIZE + 1));
3140 		WorkListSize = QUEUESEGSIZE;
3141 	}
3142 	else
3143 	{
3144 		int newsize = WorkListSize + QUEUESEGSIZE;
3145 		WORK *newlist = (WORK *) sm_realloc((char *) WorkList,
3146 					  (unsigned) sizeof(WORK) * (newsize + 1));
3147 
3148 		if (newlist != NULL)
3149 		{
3150 			WorkListSize = newsize;
3151 			WorkList = newlist;
3152 			if (LogLevel > 1)
3153 			{
3154 				sm_syslog(LOG_INFO, NOQID,
3155 					  "grew WorkList for %s to %d",
3156 					  qid_printqueue(qgrp, qdir),
3157 					  WorkListSize);
3158 			}
3159 		}
3160 		else if (LogLevel > 0)
3161 		{
3162 			sm_syslog(LOG_ALERT, NOQID,
3163 				  "FAILED to grow WorkList for %s to %d",
3164 				  qid_printqueue(qgrp, qdir), newsize);
3165 		}
3166 	}
3167 	if (tTd(41, 1))
3168 		sm_dprintf("grow_wlist: WorkListSize now %d\n", WorkListSize);
3169 }
3170 /*
3171 **  WORKCMPF0 -- simple priority-only compare function.
3172 **
3173 **	Parameters:
3174 **		a -- the first argument.
3175 **		b -- the second argument.
3176 **
3177 **	Returns:
3178 **		-1 if a < b
3179 **		 0 if a == b
3180 **		+1 if a > b
3181 **
3182 */
3183 
3184 static int
3185 workcmpf0(a, b)
3186 	register WORK *a;
3187 	register WORK *b;
3188 {
3189 	long pa = a->w_pri;
3190 	long pb = b->w_pri;
3191 
3192 	if (pa == pb)
3193 		return 0;
3194 	else if (pa > pb)
3195 		return 1;
3196 	else
3197 		return -1;
3198 }
3199 /*
3200 **  WORKCMPF1 -- first compare function for ordering work based on host name.
3201 **
3202 **	Sorts on host name, lock status, and priority in that order.
3203 **
3204 **	Parameters:
3205 **		a -- the first argument.
3206 **		b -- the second argument.
3207 **
3208 **	Returns:
3209 **		<0 if a < b
3210 **		 0 if a == b
3211 **		>0 if a > b
3212 **
3213 */
3214 
3215 static int
3216 workcmpf1(a, b)
3217 	register WORK *a;
3218 	register WORK *b;
3219 {
3220 	int i;
3221 
3222 	/* host name */
3223 	if (a->w_host != NULL && b->w_host == NULL)
3224 		return 1;
3225 	else if (a->w_host == NULL && b->w_host != NULL)
3226 		return -1;
3227 	if (a->w_host != NULL && b->w_host != NULL &&
3228 	    (i = sm_strcasecmp(a->w_host, b->w_host)) != 0)
3229 		return i;
3230 
3231 	/* lock status */
3232 	if (a->w_lock != b->w_lock)
3233 		return b->w_lock - a->w_lock;
3234 
3235 	/* job priority */
3236 	return workcmpf0(a, b);
3237 }
3238 /*
3239 **  WORKCMPF2 -- second compare function for ordering work based on host name.
3240 **
3241 **	Sorts on lock status, host name, and priority in that order.
3242 **
3243 **	Parameters:
3244 **		a -- the first argument.
3245 **		b -- the second argument.
3246 **
3247 **	Returns:
3248 **		<0 if a < b
3249 **		 0 if a == b
3250 **		>0 if a > b
3251 **
3252 */
3253 
3254 static int
3255 workcmpf2(a, b)
3256 	register WORK *a;
3257 	register WORK *b;
3258 {
3259 	int i;
3260 
3261 	/* lock status */
3262 	if (a->w_lock != b->w_lock)
3263 		return a->w_lock - b->w_lock;
3264 
3265 	/* host name */
3266 	if (a->w_host != NULL && b->w_host == NULL)
3267 		return 1;
3268 	else if (a->w_host == NULL && b->w_host != NULL)
3269 		return -1;
3270 	if (a->w_host != NULL && b->w_host != NULL &&
3271 	    (i = sm_strcasecmp(a->w_host, b->w_host)) != 0)
3272 		return i;
3273 
3274 	/* job priority */
3275 	return workcmpf0(a, b);
3276 }
3277 /*
3278 **  WORKCMPF3 -- simple submission-time-only compare function.
3279 **
3280 **	Parameters:
3281 **		a -- the first argument.
3282 **		b -- the second argument.
3283 **
3284 **	Returns:
3285 **		-1 if a < b
3286 **		 0 if a == b
3287 **		+1 if a > b
3288 **
3289 */
3290 
3291 static int
3292 workcmpf3(a, b)
3293 	register WORK *a;
3294 	register WORK *b;
3295 {
3296 	if (a->w_ctime > b->w_ctime)
3297 		return 1;
3298 	else if (a->w_ctime < b->w_ctime)
3299 		return -1;
3300 	else
3301 		return 0;
3302 }
3303 /*
3304 **  WORKCMPF4 -- compare based on file name
3305 **
3306 **	Parameters:
3307 **		a -- the first argument.
3308 **		b -- the second argument.
3309 **
3310 **	Returns:
3311 **		-1 if a < b
3312 **		 0 if a == b
3313 **		+1 if a > b
3314 **
3315 */
3316 
3317 static int
3318 workcmpf4(a, b)
3319 	register WORK *a;
3320 	register WORK *b;
3321 {
3322 	return strcmp(a->w_name, b->w_name);
3323 }
3324 /*
3325 **  WORKCMPF5 -- compare based on assigned random number
3326 **
3327 **	Parameters:
3328 **		a -- the first argument (ignored).
3329 **		b -- the second argument (ignored).
3330 **
3331 **	Returns:
3332 **		randomly 1/-1
3333 */
3334 
3335 /* ARGSUSED0 */
3336 static int
3337 workcmpf5(a, b)
3338 	register WORK *a;
3339 	register WORK *b;
3340 {
3341 	if (strlen(a->w_name) < randi || strlen(b->w_name) < randi)
3342 		return -1;
3343 	return a->w_name[randi] - b->w_name[randi];
3344 }
3345 /*
3346 **  WORKCMPF6 -- simple modification-time-only compare function.
3347 **
3348 **	Parameters:
3349 **		a -- the first argument.
3350 **		b -- the second argument.
3351 **
3352 **	Returns:
3353 **		-1 if a < b
3354 **		 0 if a == b
3355 **		+1 if a > b
3356 **
3357 */
3358 
3359 static int
3360 workcmpf6(a, b)
3361 	register WORK *a;
3362 	register WORK *b;
3363 {
3364 	if (a->w_mtime > b->w_mtime)
3365 		return 1;
3366 	else if (a->w_mtime < b->w_mtime)
3367 		return -1;
3368 	else
3369 		return 0;
3370 }
3371 #if _FFR_RHS
3372 /*
3373 **  WORKCMPF7 -- compare function for ordering work based on shuffled host name.
3374 **
3375 **	Sorts on lock status, host name, and priority in that order.
3376 **
3377 **	Parameters:
3378 **		a -- the first argument.
3379 **		b -- the second argument.
3380 **
3381 **	Returns:
3382 **		<0 if a < b
3383 **		 0 if a == b
3384 **		>0 if a > b
3385 **
3386 */
3387 
3388 static int
3389 workcmpf7(a, b)
3390 	register WORK *a;
3391 	register WORK *b;
3392 {
3393 	int i;
3394 
3395 	/* lock status */
3396 	if (a->w_lock != b->w_lock)
3397 		return a->w_lock - b->w_lock;
3398 
3399 	/* host name */
3400 	if (a->w_host != NULL && b->w_host == NULL)
3401 		return 1;
3402 	else if (a->w_host == NULL && b->w_host != NULL)
3403 		return -1;
3404 	if (a->w_host != NULL && b->w_host != NULL &&
3405 	    (i = sm_strshufflecmp(a->w_host, b->w_host)) != 0)
3406 		return i;
3407 
3408 	/* job priority */
3409 	return workcmpf0(a, b);
3410 }
3411 #endif /* _FFR_RHS */
3412 /*
3413 **  STRREV -- reverse string
3414 **
3415 **	Returns a pointer to a new string that is the reverse of
3416 **	the string pointed to by fwd.  The space for the new
3417 **	string is obtained using xalloc().
3418 **
3419 **	Parameters:
3420 **		fwd -- the string to reverse.
3421 **
3422 **	Returns:
3423 **		the reversed string.
3424 */
3425 
3426 static char *
3427 strrev(fwd)
3428 	char *fwd;
3429 {
3430 	char *rev = NULL;
3431 	int len, cnt;
3432 
3433 	len = strlen(fwd);
3434 	rev = xalloc(len + 1);
3435 	for (cnt = 0; cnt < len; ++cnt)
3436 		rev[cnt] = fwd[len - cnt - 1];
3437 	rev[len] = '\0';
3438 	return rev;
3439 }
3440 
3441 #if _FFR_RHS
3442 
3443 # define NASCII	128
3444 # define NCHAR	256
3445 
3446 static unsigned char ShuffledAlphabet[NCHAR];
3447 
3448 void
3449 init_shuffle_alphabet()
3450 {
3451 	static bool init = false;
3452 	int i;
3453 
3454 	if (init)
3455 		return;
3456 
3457 	/* fill the ShuffledAlphabet */
3458 	for (i = 0; i < NASCII; i++)
3459 		ShuffledAlphabet[i] = i;
3460 
3461 	/* mix it */
3462 	for (i = 1; i < NASCII; i++)
3463 	{
3464 		register int j = get_random() % NASCII;
3465 		register int tmp;
3466 
3467 		tmp = ShuffledAlphabet[j];
3468 		ShuffledAlphabet[j] = ShuffledAlphabet[i];
3469 		ShuffledAlphabet[i] = tmp;
3470 	}
3471 
3472 	/* make it case insensitive */
3473 	for (i = 'A'; i <= 'Z'; i++)
3474 		ShuffledAlphabet[i] = ShuffledAlphabet[i + 'a' - 'A'];
3475 
3476 	/* fill the upper part */
3477 	for (i = 0; i < NASCII; i++)
3478 		ShuffledAlphabet[i + NASCII] = ShuffledAlphabet[i];
3479 	init = true;
3480 }
3481 
3482 static int
3483 sm_strshufflecmp(a, b)
3484 	char *a;
3485 	char *b;
3486 {
3487 	const unsigned char *us1 = (const unsigned char *) a;
3488 	const unsigned char *us2 = (const unsigned char *) b;
3489 
3490 	while (ShuffledAlphabet[*us1] == ShuffledAlphabet[*us2++])
3491 	{
3492 		if (*us1++ == '\0')
3493 			return 0;
3494 	}
3495 	return (ShuffledAlphabet[*us1] - ShuffledAlphabet[*--us2]);
3496 }
3497 #endif /* _FFR_RHS */
3498 
3499 /*
3500 **  DOWORK -- do a work request.
3501 **
3502 **	Parameters:
3503 **		qgrp -- the index of the queue group for the job.
3504 **		qdir -- the index of the queue directory for the job.
3505 **		id -- the ID of the job to run.
3506 **		forkflag -- if set, run this in background.
3507 **		requeueflag -- if set, reinstantiate the queue quickly.
3508 **			This is used when expanding aliases in the queue.
3509 **			If forkflag is also set, it doesn't wait for the
3510 **			child.
3511 **		e - the envelope in which to run it.
3512 **
3513 **	Returns:
3514 **		process id of process that is running the queue job.
3515 **
3516 **	Side Effects:
3517 **		The work request is satisfied if possible.
3518 */
3519 
3520 pid_t
3521 dowork(qgrp, qdir, id, forkflag, requeueflag, e)
3522 	int qgrp;
3523 	int qdir;
3524 	char *id;
3525 	bool forkflag;
3526 	bool requeueflag;
3527 	register ENVELOPE *e;
3528 {
3529 	register pid_t pid;
3530 	SM_RPOOL_T *rpool;
3531 
3532 	if (tTd(40, 1))
3533 		sm_dprintf("dowork(%s/%s)\n", qid_printqueue(qgrp, qdir), id);
3534 
3535 	/*
3536 	**  Fork for work.
3537 	*/
3538 
3539 	if (forkflag)
3540 	{
3541 		/*
3542 		**  Since the delivery may happen in a child and the
3543 		**  parent does not wait, the parent may close the
3544 		**  maps thereby removing any shared memory used by
3545 		**  the map.  Therefore, close the maps now so the
3546 		**  child will dynamically open them if necessary.
3547 		*/
3548 
3549 		closemaps(false);
3550 
3551 		pid = fork();
3552 		if (pid < 0)
3553 		{
3554 			syserr("dowork: cannot fork");
3555 			return 0;
3556 		}
3557 		else if (pid > 0)
3558 		{
3559 			/* parent -- clean out connection cache */
3560 			mci_flush(false, NULL);
3561 		}
3562 		else
3563 		{
3564 			/*
3565 			**  Initialize exception stack and default exception
3566 			**  handler for child process.
3567 			*/
3568 
3569 			/* Reset global flags */
3570 			RestartRequest = NULL;
3571 			RestartWorkGroup = false;
3572 			ShutdownRequest = NULL;
3573 			PendingSignal = 0;
3574 			CurrentPid = getpid();
3575 			sm_exc_newthread(fatal_error);
3576 
3577 			/*
3578 			**  See note above about SMTP processes and SIGCHLD.
3579 			*/
3580 
3581 			if (OpMode == MD_SMTP ||
3582 			    OpMode == MD_DAEMON ||
3583 			    MaxQueueChildren > 0)
3584 			{
3585 				proc_list_clear();
3586 				sm_releasesignal(SIGCHLD);
3587 				(void) sm_signal(SIGCHLD, SIG_DFL);
3588 			}
3589 
3590 			/* child -- error messages to the transcript */
3591 			QuickAbort = OnlyOneError = false;
3592 		}
3593 	}
3594 	else
3595 	{
3596 		pid = 0;
3597 	}
3598 
3599 	if (pid == 0)
3600 	{
3601 		/*
3602 		**  CHILD
3603 		**	Lock the control file to avoid duplicate deliveries.
3604 		**		Then run the file as though we had just read it.
3605 		**	We save an idea of the temporary name so we
3606 		**		can recover on interrupt.
3607 		*/
3608 
3609 		if (forkflag)
3610 		{
3611 			/* Reset global flags */
3612 			RestartRequest = NULL;
3613 			RestartWorkGroup = false;
3614 			ShutdownRequest = NULL;
3615 			PendingSignal = 0;
3616 		}
3617 
3618 		/* set basic modes, etc. */
3619 		sm_clear_events();
3620 		clearstats();
3621 		rpool = sm_rpool_new_x(NULL);
3622 		clearenvelope(e, false, rpool);
3623 		e->e_flags |= EF_QUEUERUN|EF_GLOBALERRS;
3624 		set_delivery_mode(SM_DELIVER, e);
3625 		e->e_errormode = EM_MAIL;
3626 		e->e_id = id;
3627 		e->e_qgrp = qgrp;
3628 		e->e_qdir = qdir;
3629 		GrabTo = UseErrorsTo = false;
3630 		ExitStat = EX_OK;
3631 		if (forkflag)
3632 		{
3633 			disconnect(1, e);
3634 			set_op_mode(MD_QUEUERUN);
3635 		}
3636 		sm_setproctitle(true, e, "%s from queue", qid_printname(e));
3637 		if (LogLevel > 76)
3638 			sm_syslog(LOG_DEBUG, e->e_id, "dowork, pid=%d",
3639 				  (int) CurrentPid);
3640 
3641 		/* don't use the headers from sendmail.cf... */
3642 		e->e_header = NULL;
3643 
3644 		/* read the queue control file -- return if locked */
3645 		if (!readqf(e, false))
3646 		{
3647 			if (tTd(40, 4) && e->e_id != NULL)
3648 				sm_dprintf("readqf(%s) failed\n",
3649 					qid_printname(e));
3650 			e->e_id = NULL;
3651 			if (forkflag)
3652 				finis(false, true, EX_OK);
3653 			else
3654 			{
3655 				/* adding this frees 8 bytes */
3656 				clearenvelope(e, false, rpool);
3657 
3658 				/* adding this frees 12 bytes */
3659 				sm_rpool_free(rpool);
3660 				e->e_rpool = NULL;
3661 				return 0;
3662 			}
3663 		}
3664 
3665 		e->e_flags |= EF_INQUEUE;
3666 		eatheader(e, requeueflag, true);
3667 
3668 		if (requeueflag)
3669 			queueup(e, false, false);
3670 
3671 		/* do the delivery */
3672 		sendall(e, SM_DELIVER);
3673 
3674 		/* finish up and exit */
3675 		if (forkflag)
3676 			finis(true, true, ExitStat);
3677 		else
3678 		{
3679 			dropenvelope(e, true, false);
3680 			sm_rpool_free(rpool);
3681 			e->e_rpool = NULL;
3682 		}
3683 	}
3684 	e->e_id = NULL;
3685 	return pid;
3686 }
3687 
3688 /*
3689 **  DOWORKLIST -- process a list of envelopes as work requests
3690 **
3691 **	Similar to dowork(), except that after forking, it processes an
3692 **	envelope and its siblings, treating each envelope as a work request.
3693 **
3694 **	Parameters:
3695 **		el -- envelope to be processed including its siblings.
3696 **		forkflag -- if set, run this in background.
3697 **		requeueflag -- if set, reinstantiate the queue quickly.
3698 **			This is used when expanding aliases in the queue.
3699 **			If forkflag is also set, it doesn't wait for the
3700 **			child.
3701 **
3702 **	Returns:
3703 **		process id of process that is running the queue job.
3704 **
3705 **	Side Effects:
3706 **		The work request is satisfied if possible.
3707 */
3708 
3709 pid_t
3710 doworklist(el, forkflag, requeueflag)
3711 	ENVELOPE *el;
3712 	bool forkflag;
3713 	bool requeueflag;
3714 {
3715 	register pid_t pid;
3716 	ENVELOPE *ei;
3717 
3718 	if (tTd(40, 1))
3719 		sm_dprintf("doworklist()\n");
3720 
3721 	/*
3722 	**  Fork for work.
3723 	*/
3724 
3725 	if (forkflag)
3726 	{
3727 		/*
3728 		**  Since the delivery may happen in a child and the
3729 		**  parent does not wait, the parent may close the
3730 		**  maps thereby removing any shared memory used by
3731 		**  the map.  Therefore, close the maps now so the
3732 		**  child will dynamically open them if necessary.
3733 		*/
3734 
3735 		closemaps(false);
3736 
3737 		pid = fork();
3738 		if (pid < 0)
3739 		{
3740 			syserr("doworklist: cannot fork");
3741 			return 0;
3742 		}
3743 		else if (pid > 0)
3744 		{
3745 			/* parent -- clean out connection cache */
3746 			mci_flush(false, NULL);
3747 		}
3748 		else
3749 		{
3750 			/*
3751 			**  Initialize exception stack and default exception
3752 			**  handler for child process.
3753 			*/
3754 
3755 			/* Reset global flags */
3756 			RestartRequest = NULL;
3757 			RestartWorkGroup = false;
3758 			ShutdownRequest = NULL;
3759 			PendingSignal = 0;
3760 			CurrentPid = getpid();
3761 			sm_exc_newthread(fatal_error);
3762 
3763 			/*
3764 			**  See note above about SMTP processes and SIGCHLD.
3765 			*/
3766 
3767 			if (OpMode == MD_SMTP ||
3768 			    OpMode == MD_DAEMON ||
3769 			    MaxQueueChildren > 0)
3770 			{
3771 				proc_list_clear();
3772 				sm_releasesignal(SIGCHLD);
3773 				(void) sm_signal(SIGCHLD, SIG_DFL);
3774 			}
3775 
3776 			/* child -- error messages to the transcript */
3777 			QuickAbort = OnlyOneError = false;
3778 		}
3779 	}
3780 	else
3781 	{
3782 		pid = 0;
3783 	}
3784 
3785 	if (pid != 0)
3786 		return pid;
3787 
3788 	/*
3789 	**  IN CHILD
3790 	**	Lock the control file to avoid duplicate deliveries.
3791 	**		Then run the file as though we had just read it.
3792 	**	We save an idea of the temporary name so we
3793 	**		can recover on interrupt.
3794 	*/
3795 
3796 	if (forkflag)
3797 	{
3798 		/* Reset global flags */
3799 		RestartRequest = NULL;
3800 		RestartWorkGroup = false;
3801 		ShutdownRequest = NULL;
3802 		PendingSignal = 0;
3803 	}
3804 
3805 	/* set basic modes, etc. */
3806 	sm_clear_events();
3807 	clearstats();
3808 	GrabTo = UseErrorsTo = false;
3809 	ExitStat = EX_OK;
3810 	if (forkflag)
3811 	{
3812 		disconnect(1, el);
3813 		set_op_mode(MD_QUEUERUN);
3814 	}
3815 	if (LogLevel > 76)
3816 		sm_syslog(LOG_DEBUG, el->e_id, "doworklist, pid=%d",
3817 			  (int) CurrentPid);
3818 
3819 	for (ei = el; ei != NULL; ei = ei->e_sibling)
3820 	{
3821 		ENVELOPE e;
3822 		SM_RPOOL_T *rpool;
3823 
3824 		if (WILL_BE_QUEUED(ei->e_sendmode))
3825 			continue;
3826 		else if (QueueMode != QM_QUARANTINE &&
3827 			 ei->e_quarmsg != NULL)
3828 			continue;
3829 
3830 		rpool = sm_rpool_new_x(NULL);
3831 		clearenvelope(&e, true, rpool);
3832 		e.e_flags |= EF_QUEUERUN|EF_GLOBALERRS;
3833 		set_delivery_mode(SM_DELIVER, &e);
3834 		e.e_errormode = EM_MAIL;
3835 		e.e_id = ei->e_id;
3836 		e.e_qgrp = ei->e_qgrp;
3837 		e.e_qdir = ei->e_qdir;
3838 		openxscript(&e);
3839 		sm_setproctitle(true, &e, "%s from queue", qid_printname(&e));
3840 
3841 		/* don't use the headers from sendmail.cf... */
3842 		e.e_header = NULL;
3843 		CurEnv = &e;
3844 
3845 		/* read the queue control file -- return if locked */
3846 		if (readqf(&e, false))
3847 		{
3848 			e.e_flags |= EF_INQUEUE;
3849 			eatheader(&e, requeueflag, true);
3850 
3851 			if (requeueflag)
3852 				queueup(&e, false, false);
3853 
3854 			/* do the delivery */
3855 			sendall(&e, SM_DELIVER);
3856 			dropenvelope(&e, true, false);
3857 		}
3858 		else
3859 		{
3860 			if (tTd(40, 4) && e.e_id != NULL)
3861 				sm_dprintf("readqf(%s) failed\n",
3862 					qid_printname(&e));
3863 		}
3864 		sm_rpool_free(rpool);
3865 		ei->e_id = NULL;
3866 	}
3867 
3868 	/* restore CurEnv */
3869 	CurEnv = el;
3870 
3871 	/* finish up and exit */
3872 	if (forkflag)
3873 		finis(true, true, ExitStat);
3874 	return 0;
3875 }
3876 /*
3877 **  READQF -- read queue file and set up environment.
3878 **
3879 **	Parameters:
3880 **		e -- the envelope of the job to run.
3881 **		openonly -- only open the qf (returned as e_lockfp)
3882 **
3883 **	Returns:
3884 **		true if it successfully read the queue file.
3885 **		false otherwise.
3886 **
3887 **	Side Effects:
3888 **		The queue file is returned locked.
3889 */
3890 
3891 static bool
3892 readqf(e, openonly)
3893 	register ENVELOPE *e;
3894 	bool openonly;
3895 {
3896 	register SM_FILE_T *qfp;
3897 	ADDRESS *ctladdr;
3898 	struct stat st, stf;
3899 	char *bp;
3900 	int qfver = 0;
3901 	long hdrsize = 0;
3902 	register char *p;
3903 	char *frcpt = NULL;
3904 	char *orcpt = NULL;
3905 	bool nomore = false;
3906 	bool bogus = false;
3907 	MODE_T qsafe;
3908 	char *err;
3909 	char qf[MAXPATHLEN];
3910 	char buf[MAXLINE];
3911 	int bufsize;
3912 
3913 	/*
3914 	**  Read and process the file.
3915 	*/
3916 
3917 	SM_REQUIRE(e != NULL);
3918 	bp = NULL;
3919 	(void) sm_strlcpy(qf, queuename(e, ANYQFL_LETTER), sizeof(qf));
3920 	qfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, qf, SM_IO_RDWR_B, NULL);
3921 	if (qfp == NULL)
3922 	{
3923 		int save_errno = errno;
3924 
3925 		if (tTd(40, 8))
3926 			sm_dprintf("readqf(%s): sm_io_open failure (%s)\n",
3927 				qf, sm_errstring(errno));
3928 		errno = save_errno;
3929 		if (errno != ENOENT
3930 		    )
3931 			syserr("readqf: no control file %s", qf);
3932 		RELEASE_QUEUE;
3933 		return false;
3934 	}
3935 
3936 	if (!lockfile(sm_io_getinfo(qfp, SM_IO_WHAT_FD, NULL), qf, NULL,
3937 		      LOCK_EX|LOCK_NB))
3938 	{
3939 		/* being processed by another queuer */
3940 		if (Verbose)
3941 			(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
3942 					     "%s: locked\n", e->e_id);
3943 		if (tTd(40, 8))
3944 			sm_dprintf("%s: locked\n", e->e_id);
3945 		if (LogLevel > 19)
3946 			sm_syslog(LOG_DEBUG, e->e_id, "locked");
3947 		(void) sm_io_close(qfp, SM_TIME_DEFAULT);
3948 		RELEASE_QUEUE;
3949 		return false;
3950 	}
3951 
3952 	RELEASE_QUEUE;
3953 
3954 	/*
3955 	**  Prevent locking race condition.
3956 	**
3957 	**  Process A: readqf(): qfp = fopen(qffile)
3958 	**  Process B: queueup(): rename(tf, qf)
3959 	**  Process B: unlocks(tf)
3960 	**  Process A: lockfile(qf);
3961 	**
3962 	**  Process A (us) has the old qf file (before the rename deleted
3963 	**  the directory entry) and will be delivering based on old data.
3964 	**  This can lead to multiple deliveries of the same recipients.
3965 	**
3966 	**  Catch this by checking if the underlying qf file has changed
3967 	**  *after* acquiring our lock and if so, act as though the file
3968 	**  was still locked (i.e., just return like the lockfile() case
3969 	**  above.
3970 	*/
3971 
3972 	if (stat(qf, &stf) < 0 ||
3973 	    fstat(sm_io_getinfo(qfp, SM_IO_WHAT_FD, NULL), &st) < 0)
3974 	{
3975 		/* must have been being processed by someone else */
3976 		if (tTd(40, 8))
3977 			sm_dprintf("readqf(%s): [f]stat failure (%s)\n",
3978 				qf, sm_errstring(errno));
3979 		(void) sm_io_close(qfp, SM_TIME_DEFAULT);
3980 		return false;
3981 	}
3982 
3983 	if (st.st_nlink != stf.st_nlink ||
3984 	    st.st_dev != stf.st_dev ||
3985 	    ST_INODE(st) != ST_INODE(stf) ||
3986 #if HAS_ST_GEN && 0		/* AFS returns garbage in st_gen */
3987 	    st.st_gen != stf.st_gen ||
3988 #endif /* HAS_ST_GEN && 0 */
3989 	    st.st_uid != stf.st_uid ||
3990 	    st.st_gid != stf.st_gid ||
3991 	    st.st_size != stf.st_size)
3992 	{
3993 		/* changed after opened */
3994 		if (Verbose)
3995 			(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
3996 					     "%s: changed\n", e->e_id);
3997 		if (tTd(40, 8))
3998 			sm_dprintf("%s: changed\n", e->e_id);
3999 		if (LogLevel > 19)
4000 			sm_syslog(LOG_DEBUG, e->e_id, "changed");
4001 		(void) sm_io_close(qfp, SM_TIME_DEFAULT);
4002 		return false;
4003 	}
4004 
4005 	/*
4006 	**  Check the queue file for plausibility to avoid attacks.
4007 	*/
4008 
4009 	qsafe = S_IWOTH|S_IWGRP;
4010 	if (bitset(S_IWGRP, QueueFileMode))
4011 		qsafe &= ~S_IWGRP;
4012 
4013 	bogus = st.st_uid != geteuid() &&
4014 		st.st_uid != TrustedUid &&
4015 		geteuid() != RealUid;
4016 
4017 	/*
4018 	**  If this qf file results from a set-group-ID binary, then
4019 	**  we check whether the directory is group-writable,
4020 	**  the queue file mode contains the group-writable bit, and
4021 	**  the groups are the same.
4022 	**  Notice: this requires that the set-group-ID binary is used to
4023 	**  run the queue!
4024 	*/
4025 
4026 	if (bogus && st.st_gid == getegid() && UseMSP)
4027 	{
4028 		char delim;
4029 		struct stat dst;
4030 
4031 		bp = SM_LAST_DIR_DELIM(qf);
4032 		if (bp == NULL)
4033 			delim = '\0';
4034 		else
4035 		{
4036 			delim = *bp;
4037 			*bp = '\0';
4038 		}
4039 		if (stat(delim == '\0' ? "." : qf, &dst) < 0)
4040 			syserr("readqf: cannot stat directory %s",
4041 				delim == '\0' ? "." : qf);
4042 		else
4043 		{
4044 			bogus = !(bitset(S_IWGRP, QueueFileMode) &&
4045 				  bitset(S_IWGRP, dst.st_mode) &&
4046 				  dst.st_gid == st.st_gid);
4047 		}
4048 		if (delim != '\0')
4049 			*bp = delim;
4050 		bp = NULL;
4051 	}
4052 	if (!bogus)
4053 		bogus = bitset(qsafe, st.st_mode);
4054 	if (bogus)
4055 	{
4056 		if (LogLevel > 0)
4057 		{
4058 			sm_syslog(LOG_ALERT, e->e_id,
4059 				  "bogus queue file, uid=%d, gid=%d, mode=%o",
4060 				  st.st_uid, st.st_gid, st.st_mode);
4061 		}
4062 		if (tTd(40, 8))
4063 			sm_dprintf("readqf(%s): bogus file\n", qf);
4064 		e->e_flags |= EF_INQUEUE;
4065 		if (!openonly)
4066 			loseqfile(e, "bogus file uid/gid in mqueue");
4067 		(void) sm_io_close(qfp, SM_TIME_DEFAULT);
4068 		return false;
4069 	}
4070 
4071 	if (st.st_size == 0)
4072 	{
4073 		/* must be a bogus file -- if also old, just remove it */
4074 		if (!openonly && st.st_ctime + 10 * 60 < curtime())
4075 		{
4076 			(void) xunlink(queuename(e, DATAFL_LETTER));
4077 			(void) xunlink(queuename(e, ANYQFL_LETTER));
4078 		}
4079 		(void) sm_io_close(qfp, SM_TIME_DEFAULT);
4080 		return false;
4081 	}
4082 
4083 	if (st.st_nlink == 0)
4084 	{
4085 		/*
4086 		**  Race condition -- we got a file just as it was being
4087 		**  unlinked.  Just assume it is zero length.
4088 		*/
4089 
4090 		(void) sm_io_close(qfp, SM_TIME_DEFAULT);
4091 		return false;
4092 	}
4093 
4094 #if _FFR_TRUSTED_QF
4095 	/*
4096 	**  If we don't own the file mark it as unsafe.
4097 	**  However, allow TrustedUser to own it as well
4098 	**  in case TrustedUser manipulates the queue.
4099 	*/
4100 
4101 	if (st.st_uid != geteuid() && st.st_uid != TrustedUid)
4102 		e->e_flags |= EF_UNSAFE;
4103 #else /* _FFR_TRUSTED_QF */
4104 	/* If we don't own the file mark it as unsafe */
4105 	if (st.st_uid != geteuid())
4106 		e->e_flags |= EF_UNSAFE;
4107 #endif /* _FFR_TRUSTED_QF */
4108 
4109 	/* good file -- save this lock */
4110 	e->e_lockfp = qfp;
4111 
4112 	/* Just wanted the open file */
4113 	if (openonly)
4114 		return true;
4115 
4116 	/* do basic system initialization */
4117 	initsys(e);
4118 	macdefine(&e->e_macro, A_PERM, 'i', e->e_id);
4119 
4120 	LineNumber = 0;
4121 	e->e_flags |= EF_GLOBALERRS;
4122 	set_op_mode(MD_QUEUERUN);
4123 	ctladdr = NULL;
4124 	e->e_qfletter = queue_letter(e, ANYQFL_LETTER);
4125 	e->e_dfqgrp = e->e_qgrp;
4126 	e->e_dfqdir = e->e_qdir;
4127 #if _FFR_QUEUE_MACRO
4128 	macdefine(&e->e_macro, A_TEMP, macid("{queue}"),
4129 		  qid_printqueue(e->e_qgrp, e->e_qdir));
4130 #endif /* _FFR_QUEUE_MACRO */
4131 	e->e_dfino = -1;
4132 	e->e_msgsize = -1;
4133 	while (bufsize = sizeof(buf),
4134 	       (bp = fgetfolded(buf, &bufsize, qfp)) != NULL)
4135 	{
4136 		unsigned long qflags;
4137 		ADDRESS *q;
4138 		int r;
4139 		time_t now;
4140 		auto char *ep;
4141 
4142 		if (tTd(40, 4))
4143 			sm_dprintf("+++++ %s\n", bp);
4144 		if (nomore)
4145 		{
4146 			/* hack attack */
4147   hackattack:
4148 			syserr("SECURITY ALERT: extra or bogus data in queue file: %s",
4149 			       bp);
4150 			err = "bogus queue line";
4151 			goto fail;
4152 		}
4153 		switch (bp[0])
4154 		{
4155 		  case 'A':		/* AUTH= parameter */
4156 			if (!xtextok(&bp[1]))
4157 				goto hackattack;
4158 			e->e_auth_param = sm_rpool_strdup_x(e->e_rpool, &bp[1]);
4159 			break;
4160 
4161 		  case 'B':		/* body type */
4162 			r = check_bodytype(&bp[1]);
4163 			if (!BODYTYPE_VALID(r))
4164 				goto hackattack;
4165 			e->e_bodytype = sm_rpool_strdup_x(e->e_rpool, &bp[1]);
4166 			break;
4167 
4168 		  case 'C':		/* specify controlling user */
4169 			ctladdr = setctluser(&bp[1], qfver, e);
4170 			break;
4171 
4172 		  case 'D':		/* data file name */
4173 			/* obsolete -- ignore */
4174 			break;
4175 
4176 		  case 'd':		/* data file directory name */
4177 			{
4178 				int qgrp, qdir;
4179 
4180 #if _FFR_MSP_PARANOIA
4181 				/* forbid queue groups in MSP? */
4182 				if (UseMSP)
4183 					goto hackattack;
4184 #endif /* _FFR_MSP_PARANOIA */
4185 				for (qgrp = 0;
4186 				     qgrp < NumQueue && Queue[qgrp] != NULL;
4187 				     ++qgrp)
4188 				{
4189 					for (qdir = 0;
4190 					     qdir < Queue[qgrp]->qg_numqueues;
4191 					     ++qdir)
4192 					{
4193 						if (strcmp(&bp[1],
4194 							   Queue[qgrp]->qg_qpaths[qdir].qp_name)
4195 						    == 0)
4196 						{
4197 							e->e_dfqgrp = qgrp;
4198 							e->e_dfqdir = qdir;
4199 							goto done;
4200 						}
4201 					}
4202 				}
4203 				err = "bogus queue file directory";
4204 				goto fail;
4205 			  done:
4206 				break;
4207 			}
4208 
4209 		  case 'E':		/* specify error recipient */
4210 			/* no longer used */
4211 			break;
4212 
4213 		  case 'F':		/* flag bits */
4214 			if (strncmp(bp, "From ", 5) == 0)
4215 			{
4216 				/* we are being spoofed! */
4217 				syserr("SECURITY ALERT: bogus qf line %s", bp);
4218 				err = "bogus queue line";
4219 				goto fail;
4220 			}
4221 			for (p = &bp[1]; *p != '\0'; p++)
4222 			{
4223 				switch (*p)
4224 				{
4225 				  case '8':	/* has 8 bit data */
4226 					e->e_flags |= EF_HAS8BIT;
4227 					break;
4228 
4229 				  case 'b':	/* delete Bcc: header */
4230 					e->e_flags |= EF_DELETE_BCC;
4231 					break;
4232 
4233 				  case 'd':	/* envelope has DSN RET= */
4234 					e->e_flags |= EF_RET_PARAM;
4235 					break;
4236 
4237 				  case 'n':	/* don't return body */
4238 					e->e_flags |= EF_NO_BODY_RETN;
4239 					break;
4240 
4241 				  case 'r':	/* response */
4242 					e->e_flags |= EF_RESPONSE;
4243 					break;
4244 
4245 				  case 's':	/* split */
4246 					e->e_flags |= EF_SPLIT;
4247 					break;
4248 
4249 				  case 'w':	/* warning sent */
4250 					e->e_flags |= EF_WARNING;
4251 					break;
4252 				}
4253 			}
4254 			break;
4255 
4256 		  case 'q':		/* quarantine reason */
4257 			e->e_quarmsg = sm_rpool_strdup_x(e->e_rpool, &bp[1]);
4258 			macdefine(&e->e_macro, A_PERM,
4259 				  macid("{quarantine}"), e->e_quarmsg);
4260 			break;
4261 
4262 		  case 'H':		/* header */
4263 
4264 			/*
4265 			**  count size before chompheader() destroys the line.
4266 			**  this isn't accurate due to macro expansion, but
4267 			**  better than before. "-3" to skip H?? at least.
4268 			*/
4269 
4270 			hdrsize += strlen(bp) - 3;
4271 			(void) chompheader(&bp[1], CHHDR_QUEUE, NULL, e);
4272 			break;
4273 
4274 		  case 'I':		/* data file's inode number */
4275 			/* regenerated below */
4276 			break;
4277 
4278 		  case 'K':		/* time of last delivery attempt */
4279 			e->e_dtime = atol(&buf[1]);
4280 			break;
4281 
4282 		  case 'L':		/* Solaris Content-Length: */
4283 		  case 'M':		/* message */
4284 			/* ignore this; we want a new message next time */
4285 			break;
4286 
4287 		  case 'N':		/* number of delivery attempts */
4288 			e->e_ntries = atoi(&buf[1]);
4289 
4290 			/* if this has been tried recently, let it be */
4291 			now = curtime();
4292 			if (e->e_ntries > 0 && e->e_dtime <= now &&
4293 			    now < e->e_dtime + MinQueueAge)
4294 			{
4295 				char *howlong;
4296 
4297 				howlong = pintvl(now - e->e_dtime, true);
4298 				if (Verbose)
4299 					(void) sm_io_fprintf(smioout,
4300 							     SM_TIME_DEFAULT,
4301 							     "%s: too young (%s)\n",
4302 							     e->e_id, howlong);
4303 				if (tTd(40, 8))
4304 					sm_dprintf("%s: too young (%s)\n",
4305 						e->e_id, howlong);
4306 				if (LogLevel > 19)
4307 					sm_syslog(LOG_DEBUG, e->e_id,
4308 						  "too young (%s)",
4309 						  howlong);
4310 				e->e_id = NULL;
4311 				unlockqueue(e);
4312 				if (bp != buf)
4313 					sm_free(bp);
4314 				return false;
4315 			}
4316 			macdefine(&e->e_macro, A_TEMP,
4317 				macid("{ntries}"), &buf[1]);
4318 
4319 #if NAMED_BIND
4320 			/* adjust BIND parameters immediately */
4321 			if (e->e_ntries == 0)
4322 			{
4323 				_res.retry = TimeOuts.res_retry[RES_TO_FIRST];
4324 				_res.retrans = TimeOuts.res_retrans[RES_TO_FIRST];
4325 			}
4326 			else
4327 			{
4328 				_res.retry = TimeOuts.res_retry[RES_TO_NORMAL];
4329 				_res.retrans = TimeOuts.res_retrans[RES_TO_NORMAL];
4330 			}
4331 #endif /* NAMED_BIND */
4332 			break;
4333 
4334 		  case 'P':		/* message priority */
4335 			e->e_msgpriority = atol(&bp[1]) + WkTimeFact;
4336 			break;
4337 
4338 		  case 'Q':		/* original recipient */
4339 			orcpt = sm_rpool_strdup_x(e->e_rpool, &bp[1]);
4340 			break;
4341 
4342 		  case 'r':		/* final recipient */
4343 			frcpt = sm_rpool_strdup_x(e->e_rpool, &bp[1]);
4344 			break;
4345 
4346 		  case 'R':		/* specify recipient */
4347 			p = bp;
4348 			qflags = 0;
4349 			if (qfver >= 1)
4350 			{
4351 				/* get flag bits */
4352 				while (*++p != '\0' && *p != ':')
4353 				{
4354 					switch (*p)
4355 					{
4356 					  case 'N':
4357 						qflags |= QHASNOTIFY;
4358 						break;
4359 
4360 					  case 'S':
4361 						qflags |= QPINGONSUCCESS;
4362 						break;
4363 
4364 					  case 'F':
4365 						qflags |= QPINGONFAILURE;
4366 						break;
4367 
4368 					  case 'D':
4369 						qflags |= QPINGONDELAY;
4370 						break;
4371 
4372 					  case 'P':
4373 						qflags |= QPRIMARY;
4374 						break;
4375 
4376 					  case 'A':
4377 						if (ctladdr != NULL)
4378 							ctladdr->q_flags |= QALIAS;
4379 						break;
4380 
4381 					  default: /* ignore or complain? */
4382 						break;
4383 					}
4384 				}
4385 			}
4386 			else
4387 				qflags |= QPRIMARY;
4388 			macdefine(&e->e_macro, A_PERM, macid("{addr_type}"),
4389 				"e r");
4390 			if (*p != '\0')
4391 				q = parseaddr(++p, NULLADDR, RF_COPYALL, '\0',
4392 						NULL, e, true);
4393 			else
4394 				q = NULL;
4395 			if (q != NULL)
4396 			{
4397 				/* make sure we keep the current qgrp */
4398 				if (ISVALIDQGRP(e->e_qgrp))
4399 					q->q_qgrp = e->e_qgrp;
4400 				q->q_alias = ctladdr;
4401 				if (qfver >= 1)
4402 					q->q_flags &= ~Q_PINGFLAGS;
4403 				q->q_flags |= qflags;
4404 				q->q_finalrcpt = frcpt;
4405 				q->q_orcpt = orcpt;
4406 				(void) recipient(q, &e->e_sendqueue, 0, e);
4407 			}
4408 			frcpt = NULL;
4409 			orcpt = NULL;
4410 			macdefine(&e->e_macro, A_PERM, macid("{addr_type}"),
4411 				NULL);
4412 			break;
4413 
4414 		  case 'S':		/* sender */
4415 			setsender(sm_rpool_strdup_x(e->e_rpool, &bp[1]),
4416 				  e, NULL, '\0', true);
4417 			break;
4418 
4419 		  case 'T':		/* init time */
4420 			e->e_ctime = atol(&bp[1]);
4421 			break;
4422 
4423 		  case 'V':		/* queue file version number */
4424 			qfver = atoi(&bp[1]);
4425 			if (qfver <= QF_VERSION)
4426 				break;
4427 			syserr("Version number in queue file (%d) greater than max (%d)",
4428 				qfver, QF_VERSION);
4429 			err = "unsupported queue file version";
4430 			goto fail;
4431 			/* NOTREACHED */
4432 			break;
4433 
4434 		  case 'Z':		/* original envelope id from ESMTP */
4435 			e->e_envid = sm_rpool_strdup_x(e->e_rpool, &bp[1]);
4436 			macdefine(&e->e_macro, A_PERM,
4437 				macid("{dsn_envid}"), e->e_envid);
4438 			break;
4439 
4440 		  case '!':		/* deliver by */
4441 
4442 			/* format: flag (1 char) space long-integer */
4443 			e->e_dlvr_flag = buf[1];
4444 			e->e_deliver_by = strtol(&buf[3], NULL, 10);
4445 
4446 		  case '$':		/* define macro */
4447 			{
4448 				char *p;
4449 
4450 				/* XXX elimate p? */
4451 				r = macid_parse(&bp[1], &ep);
4452 				if (r == 0)
4453 					break;
4454 				p = sm_rpool_strdup_x(e->e_rpool, ep);
4455 				macdefine(&e->e_macro, A_PERM, r, p);
4456 			}
4457 			break;
4458 
4459 		  case '.':		/* terminate file */
4460 			nomore = true;
4461 			break;
4462 
4463 #if _FFR_QUEUEDELAY
4464 		  case 'G':
4465 		  case 'Y':
4466 
4467 			/*
4468 			**  Maintain backward compatibility for
4469 			**  users who defined _FFR_QUEUEDELAY in
4470 			**  previous releases.  Remove this
4471 			**  code in 8.14 or 8.15.
4472 			*/
4473 
4474 			if (qfver == 5 || qfver == 7)
4475 				break;
4476 
4477 			/* If not qfver 5 or 7, then 'G' or 'Y' is invalid */
4478 			/* FALLTHROUGH */
4479 #endif /* _FFR_QUEUEDELAY */
4480 
4481 		  default:
4482 			syserr("readqf: %s: line %d: bad line \"%s\"",
4483 				qf, LineNumber, shortenstring(bp, MAXSHORTSTR));
4484 			err = "unrecognized line";
4485 			goto fail;
4486 		}
4487 
4488 		if (bp != buf)
4489 			SM_FREE(bp);
4490 	}
4491 
4492 	/*
4493 	**  If we haven't read any lines, this queue file is empty.
4494 	**  Arrange to remove it without referencing any null pointers.
4495 	*/
4496 
4497 	if (LineNumber == 0)
4498 	{
4499 		errno = 0;
4500 		e->e_flags |= EF_CLRQUEUE|EF_FATALERRS|EF_RESPONSE;
4501 		return true;
4502 	}
4503 
4504 	/* Check to make sure we have a complete queue file read */
4505 	if (!nomore)
4506 	{
4507 		syserr("readqf: %s: incomplete queue file read", qf);
4508 		(void) sm_io_close(qfp, SM_TIME_DEFAULT);
4509 		return false;
4510 	}
4511 
4512 #if _FFR_QF_PARANOIA
4513 	/* Check to make sure key fields were read */
4514 	if (e->e_from.q_mailer == NULL)
4515 	{
4516 		syserr("readqf: %s: sender not specified in queue file", qf);
4517 		(void) sm_io_close(qfp, SM_TIME_DEFAULT);
4518 		return false;
4519 	}
4520 	/* other checks? */
4521 #endif /* _FFR_QF_PARANOIA */
4522 
4523 	/* possibly set ${dsn_ret} macro */
4524 	if (bitset(EF_RET_PARAM, e->e_flags))
4525 	{
4526 		if (bitset(EF_NO_BODY_RETN, e->e_flags))
4527 			macdefine(&e->e_macro, A_PERM,
4528 				macid("{dsn_ret}"), "hdrs");
4529 		else
4530 			macdefine(&e->e_macro, A_PERM,
4531 				macid("{dsn_ret}"), "full");
4532 	}
4533 
4534 	/*
4535 	**  Arrange to read the data file.
4536 	*/
4537 
4538 	p = queuename(e, DATAFL_LETTER);
4539 	e->e_dfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, p, SM_IO_RDONLY_B,
4540 			      NULL);
4541 	if (e->e_dfp == NULL)
4542 	{
4543 		syserr("readqf: cannot open %s", p);
4544 	}
4545 	else
4546 	{
4547 		e->e_flags |= EF_HAS_DF;
4548 		if (fstat(sm_io_getinfo(e->e_dfp, SM_IO_WHAT_FD, NULL), &st)
4549 		    >= 0)
4550 		{
4551 			e->e_msgsize = st.st_size + hdrsize;
4552 			e->e_dfdev = st.st_dev;
4553 			e->e_dfino = ST_INODE(st);
4554 			(void) sm_snprintf(buf, sizeof(buf), "%ld",
4555 					   e->e_msgsize);
4556 			macdefine(&e->e_macro, A_TEMP, macid("{msg_size}"),
4557 				  buf);
4558 		}
4559 	}
4560 
4561 	return true;
4562 
4563   fail:
4564 	/*
4565 	**  There was some error reading the qf file (reason is in err var.)
4566 	**  Cleanup:
4567 	**	close file; clear e_lockfp since it is the same as qfp,
4568 	**	hence it is invalid (as file) after qfp is closed;
4569 	**	the qf file is on disk, so set the flag to avoid calling
4570 	**	queueup() with bogus data.
4571 	*/
4572 
4573 	if (bp != buf)
4574 		SM_FREE(bp);
4575 	if (qfp != NULL)
4576 		(void) sm_io_close(qfp, SM_TIME_DEFAULT);
4577 	e->e_lockfp = NULL;
4578 	e->e_flags |= EF_INQUEUE;
4579 	loseqfile(e, err);
4580 	return false;
4581 }
4582 /*
4583 **  PRTSTR -- print a string, "unprintable" characters are shown as \oct
4584 **
4585 **	Parameters:
4586 **		s -- string to print
4587 **		ml -- maximum length of output
4588 **
4589 **	Returns:
4590 **		number of entries
4591 **
4592 **	Side Effects:
4593 **		Prints a string on stdout.
4594 */
4595 
4596 static void prtstr __P((char *, int));
4597 
4598 static void
4599 prtstr(s, ml)
4600 	char *s;
4601 	int ml;
4602 {
4603 	int c;
4604 
4605 	if (s == NULL)
4606 		return;
4607 	while (ml-- > 0 && ((c = *s++) != '\0'))
4608 	{
4609 		if (c == '\\')
4610 		{
4611 			if (ml-- > 0)
4612 			{
4613 				(void) sm_io_putc(smioout, SM_TIME_DEFAULT, c);
4614 				(void) sm_io_putc(smioout, SM_TIME_DEFAULT, c);
4615 			}
4616 		}
4617 		else if (isascii(c) && isprint(c))
4618 			(void) sm_io_putc(smioout, SM_TIME_DEFAULT, c);
4619 		else
4620 		{
4621 			if ((ml -= 3) > 0)
4622 				(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
4623 						     "\\%03o", c & 0xFF);
4624 		}
4625 	}
4626 }
4627 /*
4628 **  PRINTNQE -- print out number of entries in the mail queue
4629 **
4630 **	Parameters:
4631 **		out -- output file pointer.
4632 **		prefix -- string to output in front of each line.
4633 **
4634 **	Returns:
4635 **		none.
4636 */
4637 
4638 void
4639 printnqe(out, prefix)
4640 	SM_FILE_T *out;
4641 	char *prefix;
4642 {
4643 #if SM_CONF_SHM
4644 	int i, k = 0, nrequests = 0;
4645 	bool unknown = false;
4646 
4647 	if (ShmId == SM_SHM_NO_ID)
4648 	{
4649 		if (prefix == NULL)
4650 			(void) sm_io_fprintf(out, SM_TIME_DEFAULT,
4651 					"Data unavailable: shared memory not updated\n");
4652 		else
4653 			(void) sm_io_fprintf(out, SM_TIME_DEFAULT,
4654 					"%sNOTCONFIGURED:-1\r\n", prefix);
4655 		return;
4656 	}
4657 	for (i = 0; i < NumQueue && Queue[i] != NULL; i++)
4658 	{
4659 		int j;
4660 
4661 		k++;
4662 		for (j = 0; j < Queue[i]->qg_numqueues; j++)
4663 		{
4664 			int n;
4665 
4666 			if (StopRequest)
4667 				stop_sendmail();
4668 
4669 			n = QSHM_ENTRIES(Queue[i]->qg_qpaths[j].qp_idx);
4670 			if (prefix != NULL)
4671 				(void) sm_io_fprintf(out, SM_TIME_DEFAULT,
4672 					"%s%s:%d\r\n",
4673 					prefix, qid_printqueue(i, j), n);
4674 			else if (n < 0)
4675 			{
4676 				(void) sm_io_fprintf(out, SM_TIME_DEFAULT,
4677 					"%s: unknown number of entries\n",
4678 					qid_printqueue(i, j));
4679 				unknown = true;
4680 			}
4681 			else if (n == 0)
4682 			{
4683 				(void) sm_io_fprintf(out, SM_TIME_DEFAULT,
4684 					"%s is empty\n",
4685 					qid_printqueue(i, j));
4686 			}
4687 			else if (n > 0)
4688 			{
4689 				(void) sm_io_fprintf(out, SM_TIME_DEFAULT,
4690 					"%s: entries=%d\n",
4691 					qid_printqueue(i, j), n);
4692 				nrequests += n;
4693 				k++;
4694 			}
4695 		}
4696 	}
4697 	if (prefix == NULL && k > 1)
4698 		(void) sm_io_fprintf(out, SM_TIME_DEFAULT,
4699 				     "\t\tTotal requests: %d%s\n",
4700 				     nrequests, unknown ? " (about)" : "");
4701 #else /* SM_CONF_SHM */
4702 	if (prefix == NULL)
4703 		(void) sm_io_fprintf(out, SM_TIME_DEFAULT,
4704 			     "Data unavailable without shared memory support\n");
4705 	else
4706 		(void) sm_io_fprintf(out, SM_TIME_DEFAULT,
4707 			     "%sNOTAVAILABLE:-1\r\n", prefix);
4708 #endif /* SM_CONF_SHM */
4709 }
4710 /*
4711 **  PRINTQUEUE -- print out a representation of the mail queue
4712 **
4713 **	Parameters:
4714 **		none.
4715 **
4716 **	Returns:
4717 **		none.
4718 **
4719 **	Side Effects:
4720 **		Prints a listing of the mail queue on the standard output.
4721 */
4722 
4723 void
4724 printqueue()
4725 {
4726 	int i, k = 0, nrequests = 0;
4727 
4728 	for (i = 0; i < NumQueue && Queue[i] != NULL; i++)
4729 	{
4730 		int j;
4731 
4732 		k++;
4733 		for (j = 0; j < Queue[i]->qg_numqueues; j++)
4734 		{
4735 			if (StopRequest)
4736 				stop_sendmail();
4737 			nrequests += print_single_queue(i, j);
4738 			k++;
4739 		}
4740 	}
4741 	if (k > 1)
4742 		(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
4743 				     "\t\tTotal requests: %d\n",
4744 				     nrequests);
4745 }
4746 /*
4747 **  PRINT_SINGLE_QUEUE -- print out a representation of a single mail queue
4748 **
4749 **	Parameters:
4750 **		qgrp -- the index of the queue group.
4751 **		qdir -- the queue directory.
4752 **
4753 **	Returns:
4754 **		number of requests in mail queue.
4755 **
4756 **	Side Effects:
4757 **		Prints a listing of the mail queue on the standard output.
4758 */
4759 
4760 int
4761 print_single_queue(qgrp, qdir)
4762 	int qgrp;
4763 	int qdir;
4764 {
4765 	register WORK *w;
4766 	SM_FILE_T *f;
4767 	int nrequests;
4768 	char qd[MAXPATHLEN];
4769 	char qddf[MAXPATHLEN];
4770 	char buf[MAXLINE];
4771 
4772 	if (qdir == NOQDIR)
4773 	{
4774 		(void) sm_strlcpy(qd, ".", sizeof(qd));
4775 		(void) sm_strlcpy(qddf, ".", sizeof(qddf));
4776 	}
4777 	else
4778 	{
4779 		(void) sm_strlcpyn(qd, sizeof(qd), 2,
4780 			Queue[qgrp]->qg_qpaths[qdir].qp_name,
4781 			(bitset(QP_SUBQF,
4782 				Queue[qgrp]->qg_qpaths[qdir].qp_subdirs)
4783 					? "/qf" : ""));
4784 		(void) sm_strlcpyn(qddf, sizeof(qddf), 2,
4785 			Queue[qgrp]->qg_qpaths[qdir].qp_name,
4786 			(bitset(QP_SUBDF,
4787 				Queue[qgrp]->qg_qpaths[qdir].qp_subdirs)
4788 					? "/df" : ""));
4789 	}
4790 
4791 	/*
4792 	**  Check for permission to print the queue
4793 	*/
4794 
4795 	if (bitset(PRIV_RESTRICTMAILQ, PrivacyFlags) && RealUid != 0)
4796 	{
4797 		struct stat st;
4798 #ifdef NGROUPS_MAX
4799 		int n;
4800 		extern GIDSET_T InitialGidSet[NGROUPS_MAX];
4801 #endif /* NGROUPS_MAX */
4802 
4803 		if (stat(qd, &st) < 0)
4804 		{
4805 			syserr("Cannot stat %s",
4806 				qid_printqueue(qgrp, qdir));
4807 			return 0;
4808 		}
4809 #ifdef NGROUPS_MAX
4810 		n = NGROUPS_MAX;
4811 		while (--n >= 0)
4812 		{
4813 			if (InitialGidSet[n] == st.st_gid)
4814 				break;
4815 		}
4816 		if (n < 0 && RealGid != st.st_gid)
4817 #else /* NGROUPS_MAX */
4818 		if (RealGid != st.st_gid)
4819 #endif /* NGROUPS_MAX */
4820 		{
4821 			usrerr("510 You are not permitted to see the queue");
4822 			setstat(EX_NOPERM);
4823 			return 0;
4824 		}
4825 	}
4826 
4827 	/*
4828 	**  Read and order the queue.
4829 	*/
4830 
4831 	nrequests = gatherq(qgrp, qdir, true, NULL, NULL);
4832 	(void) sortq(Queue[qgrp]->qg_maxlist);
4833 
4834 	/*
4835 	**  Print the work list that we have read.
4836 	*/
4837 
4838 	/* first see if there is anything */
4839 	if (nrequests <= 0)
4840 	{
4841 		(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "%s is empty\n",
4842 				     qid_printqueue(qgrp, qdir));
4843 		return 0;
4844 	}
4845 
4846 	sm_getla();	/* get load average */
4847 
4848 	(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "\t\t%s (%d request%s",
4849 			     qid_printqueue(qgrp, qdir),
4850 			     nrequests, nrequests == 1 ? "" : "s");
4851 	if (MaxQueueRun > 0 && nrequests > MaxQueueRun)
4852 		(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
4853 				     ", only %d printed", MaxQueueRun);
4854 	if (Verbose)
4855 		(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
4856 			")\n-----Q-ID----- --Size-- -Priority- ---Q-Time--- --------Sender/Recipient--------\n");
4857 	else
4858 		(void) sm_io_fprintf(smioout,  SM_TIME_DEFAULT,
4859 			")\n-----Q-ID----- --Size-- -----Q-Time----- ------------Sender/Recipient-----------\n");
4860 	for (w = WorkQ; w != NULL; w = w->w_next)
4861 	{
4862 		struct stat st;
4863 		auto time_t submittime = 0;
4864 		long dfsize;
4865 		int flags = 0;
4866 		int qfver;
4867 		char quarmsg[MAXLINE];
4868 		char statmsg[MAXLINE];
4869 		char bodytype[MAXNAME + 1];
4870 		char qf[MAXPATHLEN];
4871 
4872 		if (StopRequest)
4873 			stop_sendmail();
4874 
4875 		(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "%13s",
4876 				     w->w_name + 2);
4877 		(void) sm_strlcpyn(qf, sizeof(qf), 3, qd, "/", w->w_name);
4878 		f = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, qf, SM_IO_RDONLY_B,
4879 			       NULL);
4880 		if (f == NULL)
4881 		{
4882 			if (errno == EPERM)
4883 				(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
4884 						     " (permission denied)\n");
4885 			else if (errno == ENOENT)
4886 				(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
4887 						     " (job completed)\n");
4888 			else
4889 				(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
4890 						     " (%s)\n",
4891 						     sm_errstring(errno));
4892 			errno = 0;
4893 			continue;
4894 		}
4895 		w->w_name[0] = DATAFL_LETTER;
4896 		(void) sm_strlcpyn(qf, sizeof(qf), 3, qddf, "/", w->w_name);
4897 		if (stat(qf, &st) >= 0)
4898 			dfsize = st.st_size;
4899 		else
4900 		{
4901 			ENVELOPE e;
4902 
4903 			/*
4904 			**  Maybe the df file can't be statted because
4905 			**  it is in a different directory than the qf file.
4906 			**  In order to find out, we must read the qf file.
4907 			*/
4908 
4909 			newenvelope(&e, &BlankEnvelope, sm_rpool_new_x(NULL));
4910 			e.e_id = w->w_name + 2;
4911 			e.e_qgrp = qgrp;
4912 			e.e_qdir = qdir;
4913 			dfsize = -1;
4914 			if (readqf(&e, false))
4915 			{
4916 				char *df = queuename(&e, DATAFL_LETTER);
4917 				if (stat(df, &st) >= 0)
4918 					dfsize = st.st_size;
4919 			}
4920 			if (e.e_lockfp != NULL)
4921 			{
4922 				(void) sm_io_close(e.e_lockfp, SM_TIME_DEFAULT);
4923 				e.e_lockfp = NULL;
4924 			}
4925 			clearenvelope(&e, false, e.e_rpool);
4926 			sm_rpool_free(e.e_rpool);
4927 		}
4928 		if (w->w_lock)
4929 			(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "*");
4930 		else if (QueueMode == QM_LOST)
4931 			(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "?");
4932 		else if (w->w_tooyoung)
4933 			(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "-");
4934 		else if (shouldqueue(w->w_pri, w->w_ctime))
4935 			(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "X");
4936 		else
4937 			(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, " ");
4938 
4939 		errno = 0;
4940 
4941 		quarmsg[0] = '\0';
4942 		statmsg[0] = bodytype[0] = '\0';
4943 		qfver = 0;
4944 		while (sm_io_fgets(f, SM_TIME_DEFAULT, buf, sizeof(buf)) != NULL)
4945 		{
4946 			register int i;
4947 			register char *p;
4948 
4949 			if (StopRequest)
4950 				stop_sendmail();
4951 
4952 			fixcrlf(buf, true);
4953 			switch (buf[0])
4954 			{
4955 			  case 'V':	/* queue file version */
4956 				qfver = atoi(&buf[1]);
4957 				break;
4958 
4959 			  case 'M':	/* error message */
4960 				if ((i = strlen(&buf[1])) >= sizeof(statmsg))
4961 					i = sizeof(statmsg) - 1;
4962 				memmove(statmsg, &buf[1], i);
4963 				statmsg[i] = '\0';
4964 				break;
4965 
4966 			  case 'q':	/* quarantine reason */
4967 				if ((i = strlen(&buf[1])) >= sizeof(quarmsg))
4968 					i = sizeof(quarmsg) - 1;
4969 				memmove(quarmsg, &buf[1], i);
4970 				quarmsg[i] = '\0';
4971 				break;
4972 
4973 			  case 'B':	/* body type */
4974 				if ((i = strlen(&buf[1])) >= sizeof(bodytype))
4975 					i = sizeof(bodytype) - 1;
4976 				memmove(bodytype, &buf[1], i);
4977 				bodytype[i] = '\0';
4978 				break;
4979 
4980 			  case 'S':	/* sender name */
4981 				if (Verbose)
4982 				{
4983 					(void) sm_io_fprintf(smioout,
4984 						SM_TIME_DEFAULT,
4985 						"%8ld %10ld%c%.12s ",
4986 						dfsize,
4987 						w->w_pri,
4988 						bitset(EF_WARNING, flags)
4989 							? '+' : ' ',
4990 						ctime(&submittime) + 4);
4991 					prtstr(&buf[1], 78);
4992 				}
4993 				else
4994 				{
4995 					(void) sm_io_fprintf(smioout,
4996 						SM_TIME_DEFAULT,
4997 						"%8ld %.16s ",
4998 						dfsize,
4999 						ctime(&submittime));
5000 					prtstr(&buf[1], 39);
5001 				}
5002 
5003 				if (quarmsg[0] != '\0')
5004 				{
5005 					(void) sm_io_fprintf(smioout,
5006 							     SM_TIME_DEFAULT,
5007 							     "\n     QUARANTINE: %.*s",
5008 							     Verbose ? 100 : 60,
5009 							     quarmsg);
5010 					quarmsg[0] = '\0';
5011 				}
5012 
5013 				if (statmsg[0] != '\0' || bodytype[0] != '\0')
5014 				{
5015 					(void) sm_io_fprintf(smioout,
5016 						SM_TIME_DEFAULT,
5017 						"\n    %10.10s",
5018 						bodytype);
5019 					if (statmsg[0] != '\0')
5020 						(void) sm_io_fprintf(smioout,
5021 							SM_TIME_DEFAULT,
5022 							"   (%.*s)",
5023 							Verbose ? 100 : 60,
5024 							statmsg);
5025 					statmsg[0] = '\0';
5026 				}
5027 				break;
5028 
5029 			  case 'C':	/* controlling user */
5030 				if (Verbose)
5031 					(void) sm_io_fprintf(smioout,
5032 						SM_TIME_DEFAULT,
5033 						"\n\t\t\t\t\t\t(---%.64s---)",
5034 						&buf[1]);
5035 				break;
5036 
5037 			  case 'R':	/* recipient name */
5038 				p = &buf[1];
5039 				if (qfver >= 1)
5040 				{
5041 					p = strchr(p, ':');
5042 					if (p == NULL)
5043 						break;
5044 					p++;
5045 				}
5046 				if (Verbose)
5047 				{
5048 					(void) sm_io_fprintf(smioout,
5049 							SM_TIME_DEFAULT,
5050 							"\n\t\t\t\t\t\t");
5051 					prtstr(p, 71);
5052 				}
5053 				else
5054 				{
5055 					(void) sm_io_fprintf(smioout,
5056 							SM_TIME_DEFAULT,
5057 							"\n\t\t\t\t\t ");
5058 					prtstr(p, 38);
5059 				}
5060 				if (Verbose && statmsg[0] != '\0')
5061 				{
5062 					(void) sm_io_fprintf(smioout,
5063 							SM_TIME_DEFAULT,
5064 							"\n\t\t (%.100s)",
5065 							statmsg);
5066 					statmsg[0] = '\0';
5067 				}
5068 				break;
5069 
5070 			  case 'T':	/* creation time */
5071 				submittime = atol(&buf[1]);
5072 				break;
5073 
5074 			  case 'F':	/* flag bits */
5075 				for (p = &buf[1]; *p != '\0'; p++)
5076 				{
5077 					switch (*p)
5078 					{
5079 					  case 'w':
5080 						flags |= EF_WARNING;
5081 						break;
5082 					}
5083 				}
5084 			}
5085 		}
5086 		if (submittime == (time_t) 0)
5087 			(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
5088 					     " (no control file)");
5089 		(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "\n");
5090 		(void) sm_io_close(f, SM_TIME_DEFAULT);
5091 	}
5092 	return nrequests;
5093 }
5094 
5095 /*
5096 **  QUEUE_LETTER -- get the proper queue letter for the current QueueMode.
5097 **
5098 **	Parameters:
5099 **		e -- envelope to build it in/from.
5100 **		type -- the file type, used as the first character
5101 **			of the file name.
5102 **
5103 **	Returns:
5104 **		the letter to use
5105 */
5106 
5107 static char
5108 queue_letter(e, type)
5109 	ENVELOPE *e;
5110 	int type;
5111 {
5112 	/* Change type according to QueueMode */
5113 	if (type == ANYQFL_LETTER)
5114 	{
5115 		if (e->e_quarmsg != NULL)
5116 			type = QUARQF_LETTER;
5117 		else
5118 		{
5119 			switch (QueueMode)
5120 			{
5121 			  case QM_NORMAL:
5122 				type = NORMQF_LETTER;
5123 				break;
5124 
5125 			  case QM_QUARANTINE:
5126 				type = QUARQF_LETTER;
5127 				break;
5128 
5129 			  case QM_LOST:
5130 				type = LOSEQF_LETTER;
5131 				break;
5132 
5133 			  default:
5134 				/* should never happen */
5135 				abort();
5136 				/* NOTREACHED */
5137 			}
5138 		}
5139 	}
5140 	return type;
5141 }
5142 
5143 /*
5144 **  QUEUENAME -- build a file name in the queue directory for this envelope.
5145 **
5146 **	Parameters:
5147 **		e -- envelope to build it in/from.
5148 **		type -- the file type, used as the first character
5149 **			of the file name.
5150 **
5151 **	Returns:
5152 **		a pointer to the queue name (in a static buffer).
5153 **
5154 **	Side Effects:
5155 **		If no id code is already assigned, queuename() will
5156 **		assign an id code with assign_queueid().  If no queue
5157 **		directory is assigned, one will be set with setnewqueue().
5158 */
5159 
5160 char *
5161 queuename(e, type)
5162 	register ENVELOPE *e;
5163 	int type;
5164 {
5165 	int qd, qg;
5166 	char *sub = "/";
5167 	char pref[3];
5168 	static char buf[MAXPATHLEN];
5169 
5170 	/* Assign an ID if needed */
5171 	if (e->e_id == NULL)
5172 		assign_queueid(e);
5173 	type = queue_letter(e, type);
5174 
5175 	/* begin of filename */
5176 	pref[0] = (char) type;
5177 	pref[1] = 'f';
5178 	pref[2] = '\0';
5179 
5180 	/* Assign a queue group/directory if needed */
5181 	if (type == XSCRPT_LETTER)
5182 	{
5183 		/*
5184 		**  We don't want to call setnewqueue() if we are fetching
5185 		**  the pathname of the transcript file, because setnewqueue
5186 		**  chooses a queue, and sometimes we need to write to the
5187 		**  transcript file before we have gathered enough information
5188 		**  to choose a queue.
5189 		*/
5190 
5191 		if (e->e_xfqgrp == NOQGRP || e->e_xfqdir == NOQDIR)
5192 		{
5193 			if (e->e_qgrp != NOQGRP && e->e_qdir != NOQDIR)
5194 			{
5195 				e->e_xfqgrp = e->e_qgrp;
5196 				e->e_xfqdir = e->e_qdir;
5197 			}
5198 			else
5199 			{
5200 				e->e_xfqgrp = 0;
5201 				if (Queue[e->e_xfqgrp]->qg_numqueues <= 1)
5202 					e->e_xfqdir = 0;
5203 				else
5204 				{
5205 					e->e_xfqdir = get_rand_mod(
5206 					      Queue[e->e_xfqgrp]->qg_numqueues);
5207 				}
5208 			}
5209 		}
5210 		qd = e->e_xfqdir;
5211 		qg = e->e_xfqgrp;
5212 	}
5213 	else
5214 	{
5215 		if (e->e_qgrp == NOQGRP || e->e_qdir == NOQDIR)
5216 			(void) setnewqueue(e);
5217 		if (type ==  DATAFL_LETTER)
5218 		{
5219 			qd = e->e_dfqdir;
5220 			qg = e->e_dfqgrp;
5221 		}
5222 		else
5223 		{
5224 			qd = e->e_qdir;
5225 			qg = e->e_qgrp;
5226 		}
5227 	}
5228 
5229 	/* xf files always have a valid qd and qg picked above */
5230 	if ((qd == NOQDIR || qg == NOQGRP) && type != XSCRPT_LETTER)
5231 		(void) sm_strlcpyn(buf, sizeof(buf), 2, pref, e->e_id);
5232 	else
5233 	{
5234 		switch (type)
5235 		{
5236 		  case DATAFL_LETTER:
5237 			if (bitset(QP_SUBDF, Queue[qg]->qg_qpaths[qd].qp_subdirs))
5238 				sub = "/df/";
5239 			break;
5240 
5241 		  case QUARQF_LETTER:
5242 		  case TEMPQF_LETTER:
5243 		  case NEWQFL_LETTER:
5244 		  case LOSEQF_LETTER:
5245 		  case NORMQF_LETTER:
5246 			if (bitset(QP_SUBQF, Queue[qg]->qg_qpaths[qd].qp_subdirs))
5247 				sub = "/qf/";
5248 			break;
5249 
5250 		  case XSCRPT_LETTER:
5251 			if (bitset(QP_SUBXF, Queue[qg]->qg_qpaths[qd].qp_subdirs))
5252 				sub = "/xf/";
5253 			break;
5254 
5255 		  default:
5256 			sm_abort("queuename: bad queue file type %d", type);
5257 		}
5258 
5259 		(void) sm_strlcpyn(buf, sizeof(buf), 4,
5260 				Queue[qg]->qg_qpaths[qd].qp_name,
5261 				sub, pref, e->e_id);
5262 	}
5263 
5264 	if (tTd(7, 2))
5265 		sm_dprintf("queuename: %s\n", buf);
5266 	return buf;
5267 }
5268 
5269 /*
5270 **  INIT_QID_ALG -- Initialize the (static) parameters that are used to
5271 **	generate a queue ID.
5272 **
5273 **	This function is called by the daemon to reset
5274 **	LastQueueTime and LastQueuePid which are used by assign_queueid().
5275 **	Otherwise the algorithm may cause problems because
5276 **	LastQueueTime and LastQueuePid are set indirectly by main()
5277 **	before the daemon process is started, hence LastQueuePid is not
5278 **	the pid of the daemon and therefore a child of the daemon can
5279 **	actually have the same pid as LastQueuePid which means the section
5280 **	in  assign_queueid():
5281 **	* see if we need to get a new base time/pid *
5282 **	is NOT triggered which will cause the same queue id to be generated.
5283 **
5284 **	Parameters:
5285 **		none
5286 **
5287 **	Returns:
5288 **		none.
5289 */
5290 
5291 void
5292 init_qid_alg()
5293 {
5294 	LastQueueTime = 0;
5295 	LastQueuePid = -1;
5296 }
5297 
5298 /*
5299 **  ASSIGN_QUEUEID -- assign a queue ID for this envelope.
5300 **
5301 **	Assigns an id code if one does not already exist.
5302 **	This code assumes that nothing will remain in the queue for
5303 **	longer than 60 years.  It is critical that files with the given
5304 **	name do not already exist in the queue.
5305 **	[No longer initializes e_qdir to NOQDIR.]
5306 **
5307 **	Parameters:
5308 **		e -- envelope to set it in.
5309 **
5310 **	Returns:
5311 **		none.
5312 */
5313 
5314 static const char QueueIdChars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
5315 # define QIC_LEN	60
5316 # define QIC_LEN_R	62
5317 
5318 /*
5319 **  Note: the length is "officially" 60 because minutes and seconds are
5320 **	usually only 0-59.  However (Linux):
5321 **       tm_sec The number of seconds after the minute, normally in
5322 **		the range 0 to 59, but can be up to 61 to allow for
5323 **		leap seconds.
5324 **	Hence the real length of the string is 62 to take this into account.
5325 **	Alternatively % QIC_LEN can (should) be used for access everywhere.
5326 */
5327 
5328 # define queuenextid() CurrentPid
5329 
5330 
5331 void
5332 assign_queueid(e)
5333 	register ENVELOPE *e;
5334 {
5335 	pid_t pid = queuenextid();
5336 	static int cX = 0;
5337 	static long random_offset;
5338 	struct tm *tm;
5339 	char idbuf[MAXQFNAME - 2];
5340 	int seq;
5341 
5342 	if (e->e_id != NULL)
5343 		return;
5344 
5345 	/* see if we need to get a new base time/pid */
5346 	if (cX >= QIC_LEN * QIC_LEN || LastQueueTime == 0 ||
5347 	    LastQueuePid != pid)
5348 	{
5349 		time_t then = LastQueueTime;
5350 
5351 		/* if the first time through, pick a random offset */
5352 		if (LastQueueTime == 0)
5353 			random_offset = get_random();
5354 
5355 		while ((LastQueueTime = curtime()) == then &&
5356 		       LastQueuePid == pid)
5357 		{
5358 			(void) sleep(1);
5359 		}
5360 		LastQueuePid = queuenextid();
5361 		cX = 0;
5362 	}
5363 
5364 	/*
5365 	**  Generate a new sequence number between 0 and QIC_LEN*QIC_LEN-1.
5366 	**  This lets us generate up to QIC_LEN*QIC_LEN unique queue ids
5367 	**  per second, per process.  With envelope splitting,
5368 	**  a single message can consume many queue ids.
5369 	*/
5370 
5371 	seq = (int)((cX + random_offset) % (QIC_LEN * QIC_LEN));
5372 	++cX;
5373 	if (tTd(7, 50))
5374 		sm_dprintf("assign_queueid: random_offset = %ld (%d)\n",
5375 			random_offset, seq);
5376 
5377 	tm = gmtime(&LastQueueTime);
5378 	idbuf[0] = QueueIdChars[tm->tm_year % QIC_LEN];
5379 	idbuf[1] = QueueIdChars[tm->tm_mon];
5380 	idbuf[2] = QueueIdChars[tm->tm_mday];
5381 	idbuf[3] = QueueIdChars[tm->tm_hour];
5382 	idbuf[4] = QueueIdChars[tm->tm_min % QIC_LEN_R];
5383 	idbuf[5] = QueueIdChars[tm->tm_sec % QIC_LEN_R];
5384 	idbuf[6] = QueueIdChars[seq / QIC_LEN];
5385 	idbuf[7] = QueueIdChars[seq % QIC_LEN];
5386 	(void) sm_snprintf(&idbuf[8], sizeof(idbuf) - 8, "%06d",
5387 			   (int) LastQueuePid);
5388 	e->e_id = sm_rpool_strdup_x(e->e_rpool, idbuf);
5389 	macdefine(&e->e_macro, A_PERM, 'i', e->e_id);
5390 #if 0
5391 	/* XXX: inherited from MainEnvelope */
5392 	e->e_qgrp = NOQGRP;  /* too early to do anything else */
5393 	e->e_qdir = NOQDIR;
5394 	e->e_xfqgrp = NOQGRP;
5395 #endif /* 0 */
5396 
5397 	/* New ID means it's not on disk yet */
5398 	e->e_qfletter = '\0';
5399 
5400 	if (tTd(7, 1))
5401 		sm_dprintf("assign_queueid: assigned id %s, e=%p\n",
5402 			e->e_id, e);
5403 	if (LogLevel > 93)
5404 		sm_syslog(LOG_DEBUG, e->e_id, "assigned id");
5405 }
5406 /*
5407 **  SYNC_QUEUE_TIME -- Assure exclusive PID in any given second
5408 **
5409 **	Make sure one PID can't be used by two processes in any one second.
5410 **
5411 **		If the system rotates PIDs fast enough, may get the
5412 **		same pid in the same second for two distinct processes.
5413 **		This will interfere with the queue file naming system.
5414 **
5415 **	Parameters:
5416 **		none
5417 **
5418 **	Returns:
5419 **		none
5420 */
5421 
5422 void
5423 sync_queue_time()
5424 {
5425 #if FAST_PID_RECYCLE
5426 	if (OpMode != MD_TEST &&
5427 	    OpMode != MD_VERIFY &&
5428 	    LastQueueTime > 0 &&
5429 	    LastQueuePid == CurrentPid &&
5430 	    curtime() == LastQueueTime)
5431 		(void) sleep(1);
5432 #endif /* FAST_PID_RECYCLE */
5433 }
5434 /*
5435 **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
5436 **
5437 **	Parameters:
5438 **		e -- the envelope to unlock.
5439 **
5440 **	Returns:
5441 **		none
5442 **
5443 **	Side Effects:
5444 **		unlocks the queue for `e'.
5445 */
5446 
5447 void
5448 unlockqueue(e)
5449 	ENVELOPE *e;
5450 {
5451 	if (tTd(51, 4))
5452 		sm_dprintf("unlockqueue(%s)\n",
5453 			e->e_id == NULL ? "NOQUEUE" : e->e_id);
5454 
5455 
5456 	/* if there is a lock file in the envelope, close it */
5457 	if (e->e_lockfp != NULL)
5458 		(void) sm_io_close(e->e_lockfp, SM_TIME_DEFAULT);
5459 	e->e_lockfp = NULL;
5460 
5461 	/* don't create a queue id if we don't already have one */
5462 	if (e->e_id == NULL)
5463 		return;
5464 
5465 	/* remove the transcript */
5466 	if (LogLevel > 87)
5467 		sm_syslog(LOG_DEBUG, e->e_id, "unlock");
5468 	if (!tTd(51, 104))
5469 		(void) xunlink(queuename(e, XSCRPT_LETTER));
5470 }
5471 /*
5472 **  SETCTLUSER -- create a controlling address
5473 **
5474 **	Create a fake "address" given only a local login name; this is
5475 **	used as a "controlling user" for future recipient addresses.
5476 **
5477 **	Parameters:
5478 **		user -- the user name of the controlling user.
5479 **		qfver -- the version stamp of this queue file.
5480 **		e -- envelope
5481 **
5482 **	Returns:
5483 **		An address descriptor for the controlling user,
5484 **		using storage allocated from e->e_rpool.
5485 **
5486 */
5487 
5488 static ADDRESS *
5489 setctluser(user, qfver, e)
5490 	char *user;
5491 	int qfver;
5492 	ENVELOPE *e;
5493 {
5494 	register ADDRESS *a;
5495 	struct passwd *pw;
5496 	char *p;
5497 
5498 	/*
5499 	**  See if this clears our concept of controlling user.
5500 	*/
5501 
5502 	if (user == NULL || *user == '\0')
5503 		return NULL;
5504 
5505 	/*
5506 	**  Set up addr fields for controlling user.
5507 	*/
5508 
5509 	a = (ADDRESS *) sm_rpool_malloc_x(e->e_rpool, sizeof(*a));
5510 	memset((char *) a, '\0', sizeof(*a));
5511 
5512 	if (*user == ':')
5513 	{
5514 		p = &user[1];
5515 		a->q_user = sm_rpool_strdup_x(e->e_rpool, p);
5516 	}
5517 	else
5518 	{
5519 		p = strtok(user, ":");
5520 		a->q_user = sm_rpool_strdup_x(e->e_rpool, user);
5521 		if (qfver >= 2)
5522 		{
5523 			if ((p = strtok(NULL, ":")) != NULL)
5524 				a->q_uid = atoi(p);
5525 			if ((p = strtok(NULL, ":")) != NULL)
5526 				a->q_gid = atoi(p);
5527 			if ((p = strtok(NULL, ":")) != NULL)
5528 			{
5529 				char *o;
5530 
5531 				a->q_flags |= QGOODUID;
5532 
5533 				/* if there is another ':': restore it */
5534 				if ((o = strtok(NULL, ":")) != NULL && o > p)
5535 					o[-1] = ':';
5536 			}
5537 		}
5538 		else if ((pw = sm_getpwnam(user)) != NULL)
5539 		{
5540 			if (*pw->pw_dir == '\0')
5541 				a->q_home = NULL;
5542 			else if (strcmp(pw->pw_dir, "/") == 0)
5543 				a->q_home = "";
5544 			else
5545 				a->q_home = sm_rpool_strdup_x(e->e_rpool, pw->pw_dir);
5546 			a->q_uid = pw->pw_uid;
5547 			a->q_gid = pw->pw_gid;
5548 			a->q_flags |= QGOODUID;
5549 		}
5550 	}
5551 
5552 	a->q_flags |= QPRIMARY;		/* flag as a "ctladdr" */
5553 	a->q_mailer = LocalMailer;
5554 	if (p == NULL)
5555 		a->q_paddr = sm_rpool_strdup_x(e->e_rpool, a->q_user);
5556 	else
5557 		a->q_paddr = sm_rpool_strdup_x(e->e_rpool, p);
5558 	return a;
5559 }
5560 /*
5561 **  LOSEQFILE -- rename queue file with LOSEQF_LETTER & try to let someone know
5562 **
5563 **	Parameters:
5564 **		e -- the envelope (e->e_id will be used).
5565 **		why -- reported to whomever can hear.
5566 **
5567 **	Returns:
5568 **		none.
5569 */
5570 
5571 void
5572 loseqfile(e, why)
5573 	register ENVELOPE *e;
5574 	char *why;
5575 {
5576 	bool loseit = true;
5577 	char *p;
5578 	char buf[MAXPATHLEN];
5579 
5580 	if (e == NULL || e->e_id == NULL)
5581 		return;
5582 	p = queuename(e, ANYQFL_LETTER);
5583 	if (sm_strlcpy(buf, p, sizeof(buf)) >= sizeof(buf))
5584 		return;
5585 	if (!bitset(EF_INQUEUE, e->e_flags))
5586 		queueup(e, false, true);
5587 	else if (QueueMode == QM_LOST)
5588 		loseit = false;
5589 
5590 	/* if already lost, no need to re-lose */
5591 	if (loseit)
5592 	{
5593 		p = queuename(e, LOSEQF_LETTER);
5594 		if (rename(buf, p) < 0)
5595 			syserr("cannot rename(%s, %s), uid=%d",
5596 			       buf, p, (int) geteuid());
5597 		else if (LogLevel > 0)
5598 			sm_syslog(LOG_ALERT, e->e_id,
5599 				  "Losing %s: %s", buf, why);
5600 	}
5601 	if (e->e_dfp != NULL)
5602 	{
5603 		(void) sm_io_close(e->e_dfp, SM_TIME_DEFAULT);
5604 		e->e_dfp = NULL;
5605 	}
5606 	e->e_flags &= ~EF_HAS_DF;
5607 }
5608 /*
5609 **  NAME2QID -- translate a queue group name to a queue group id
5610 **
5611 **	Parameters:
5612 **		queuename -- name of queue group.
5613 **
5614 **	Returns:
5615 **		queue group id if found.
5616 **		NOQGRP otherwise.
5617 */
5618 
5619 int
5620 name2qid(queuename)
5621 	char *queuename;
5622 {
5623 	register STAB *s;
5624 
5625 	s = stab(queuename, ST_QUEUE, ST_FIND);
5626 	if (s == NULL)
5627 		return NOQGRP;
5628 	return s->s_quegrp->qg_index;
5629 }
5630 /*
5631 **  QID_PRINTNAME -- create externally printable version of queue id
5632 **
5633 **	Parameters:
5634 **		e -- the envelope.
5635 **
5636 **	Returns:
5637 **		a printable version
5638 */
5639 
5640 char *
5641 qid_printname(e)
5642 	ENVELOPE *e;
5643 {
5644 	char *id;
5645 	static char idbuf[MAXQFNAME + 34];
5646 
5647 	if (e == NULL)
5648 		return "";
5649 
5650 	if (e->e_id == NULL)
5651 		id = "";
5652 	else
5653 		id = e->e_id;
5654 
5655 	if (e->e_qdir == NOQDIR)
5656 		return id;
5657 
5658 	(void) sm_snprintf(idbuf, sizeof(idbuf), "%.32s/%s",
5659 			   Queue[e->e_qgrp]->qg_qpaths[e->e_qdir].qp_name,
5660 			   id);
5661 	return idbuf;
5662 }
5663 /*
5664 **  QID_PRINTQUEUE -- create full version of queue directory for data files
5665 **
5666 **	Parameters:
5667 **		qgrp -- index in queue group.
5668 **		qdir -- the short version of the queue directory
5669 **
5670 **	Returns:
5671 **		the full pathname to the queue (might point to a static var)
5672 */
5673 
5674 char *
5675 qid_printqueue(qgrp, qdir)
5676 	int qgrp;
5677 	int qdir;
5678 {
5679 	char *subdir;
5680 	static char dir[MAXPATHLEN];
5681 
5682 	if (qdir == NOQDIR)
5683 		return Queue[qgrp]->qg_qdir;
5684 
5685 	if (strcmp(Queue[qgrp]->qg_qpaths[qdir].qp_name, ".") == 0)
5686 		subdir = NULL;
5687 	else
5688 		subdir = Queue[qgrp]->qg_qpaths[qdir].qp_name;
5689 
5690 	(void) sm_strlcpyn(dir, sizeof(dir), 4,
5691 			Queue[qgrp]->qg_qdir,
5692 			subdir == NULL ? "" : "/",
5693 			subdir == NULL ? "" : subdir,
5694 			(bitset(QP_SUBDF,
5695 				Queue[qgrp]->qg_qpaths[qdir].qp_subdirs)
5696 					? "/df" : ""));
5697 	return dir;
5698 }
5699 
5700 /*
5701 **  PICKQDIR -- Pick a queue directory from a queue group
5702 **
5703 **	Parameters:
5704 **		qg -- queue group
5705 **		fsize -- file size in bytes
5706 **		e -- envelope, or NULL
5707 **
5708 **	Result:
5709 **		NOQDIR if no queue directory in qg has enough free space to
5710 **		hold a file of size 'fsize', otherwise the index of
5711 **		a randomly selected queue directory which resides on a
5712 **		file system with enough disk space.
5713 **		XXX This could be extended to select a queuedir with
5714 **			a few (the fewest?) number of entries. That data
5715 **			is available if shared memory is used.
5716 **
5717 **	Side Effects:
5718 **		If the request fails and e != NULL then sm_syslog is called.
5719 */
5720 
5721 int
5722 pickqdir(qg, fsize, e)
5723 	QUEUEGRP *qg;
5724 	long fsize;
5725 	ENVELOPE *e;
5726 {
5727 	int qdir;
5728 	int i;
5729 	long avail = 0;
5730 
5731 	/* Pick a random directory, as a starting point. */
5732 	if (qg->qg_numqueues <= 1)
5733 		qdir = 0;
5734 	else
5735 		qdir = get_rand_mod(qg->qg_numqueues);
5736 
5737 	if (MinBlocksFree <= 0 && fsize <= 0)
5738 		return qdir;
5739 
5740 	/*
5741 	**  Now iterate over the queue directories,
5742 	**  looking for a directory with enough space for this message.
5743 	*/
5744 
5745 	i = qdir;
5746 	do
5747 	{
5748 		QPATHS *qp = &qg->qg_qpaths[i];
5749 		long needed = 0;
5750 		long fsavail = 0;
5751 
5752 		if (fsize > 0)
5753 			needed += fsize / FILE_SYS_BLKSIZE(qp->qp_fsysidx)
5754 				  + ((fsize % FILE_SYS_BLKSIZE(qp->qp_fsysidx)
5755 				      > 0) ? 1 : 0);
5756 		if (MinBlocksFree > 0)
5757 			needed += MinBlocksFree;
5758 		fsavail = FILE_SYS_AVAIL(qp->qp_fsysidx);
5759 #if SM_CONF_SHM
5760 		if (fsavail <= 0)
5761 		{
5762 			long blksize;
5763 
5764 			/*
5765 			**  might be not correctly updated,
5766 			**  let's try to get the info directly.
5767 			*/
5768 
5769 			fsavail = freediskspace(FILE_SYS_NAME(qp->qp_fsysidx),
5770 						&blksize);
5771 			if (fsavail < 0)
5772 				fsavail = 0;
5773 		}
5774 #endif /* SM_CONF_SHM */
5775 		if (needed <= fsavail)
5776 			return i;
5777 		if (avail < fsavail)
5778 			avail = fsavail;
5779 
5780 		if (qg->qg_numqueues > 0)
5781 			i = (i + 1) % qg->qg_numqueues;
5782 	} while (i != qdir);
5783 
5784 	if (e != NULL && LogLevel > 0)
5785 		sm_syslog(LOG_ALERT, e->e_id,
5786 			"low on space (%s needs %ld bytes + %ld blocks in %s), max avail: %ld",
5787 			CurHostName == NULL ? "SMTP-DAEMON" : CurHostName,
5788 			fsize, MinBlocksFree,
5789 			qg->qg_qdir, avail);
5790 	return NOQDIR;
5791 }
5792 /*
5793 **  SETNEWQUEUE -- Sets a new queue group and directory
5794 **
5795 **	Assign a queue group and directory to an envelope and store the
5796 **	directory in e->e_qdir.
5797 **
5798 **	Parameters:
5799 **		e -- envelope to assign a queue for.
5800 **
5801 **	Returns:
5802 **		true if successful
5803 **		false otherwise
5804 **
5805 **	Side Effects:
5806 **		On success, e->e_qgrp and e->e_qdir are non-negative.
5807 **		On failure (not enough disk space),
5808 **		e->qgrp = NOQGRP, e->e_qdir = NOQDIR
5809 **		and usrerr() is invoked (which could raise an exception).
5810 */
5811 
5812 bool
5813 setnewqueue(e)
5814 	ENVELOPE *e;
5815 {
5816 	if (tTd(41, 20))
5817 		sm_dprintf("setnewqueue: called\n");
5818 
5819 	/* not set somewhere else */
5820 	if (e->e_qgrp == NOQGRP)
5821 	{
5822 		ADDRESS *q;
5823 
5824 		/*
5825 		**  Use the queue group of the "first" recipient, as set by
5826 		**  the "queuegroup" rule set.  If that is not defined, then
5827 		**  use the queue group of the mailer of the first recipient.
5828 		**  If that is not defined either, then use the default
5829 		**  queue group.
5830 		**  Notice: "first" depends on the sorting of sendqueue
5831 		**  in recipient().
5832 		**  To avoid problems with "bad" recipients look
5833 		**  for a valid address first.
5834 		*/
5835 
5836 		q = e->e_sendqueue;
5837 		while (q != NULL &&
5838 		       (QS_IS_BADADDR(q->q_state) || QS_IS_DEAD(q->q_state)))
5839 		{
5840 			q = q->q_next;
5841 		}
5842 		if (q == NULL)
5843 			e->e_qgrp = 0;
5844 		else if (q->q_qgrp >= 0)
5845 			e->e_qgrp = q->q_qgrp;
5846 		else if (q->q_mailer != NULL &&
5847 			 ISVALIDQGRP(q->q_mailer->m_qgrp))
5848 			e->e_qgrp = q->q_mailer->m_qgrp;
5849 		else
5850 			e->e_qgrp = 0;
5851 		e->e_dfqgrp = e->e_qgrp;
5852 	}
5853 
5854 	if (ISVALIDQDIR(e->e_qdir) && ISVALIDQDIR(e->e_dfqdir))
5855 	{
5856 		if (tTd(41, 20))
5857 			sm_dprintf("setnewqueue: e_qdir already assigned (%s)\n",
5858 				qid_printqueue(e->e_qgrp, e->e_qdir));
5859 		return true;
5860 	}
5861 
5862 	filesys_update();
5863 	e->e_qdir = pickqdir(Queue[e->e_qgrp], e->e_msgsize, e);
5864 	if (e->e_qdir == NOQDIR)
5865 	{
5866 		e->e_qgrp = NOQGRP;
5867 		if (!bitset(EF_FATALERRS, e->e_flags))
5868 			usrerr("452 4.4.5 Insufficient disk space; try again later");
5869 		e->e_flags |= EF_FATALERRS;
5870 		return false;
5871 	}
5872 
5873 	if (tTd(41, 3))
5874 		sm_dprintf("setnewqueue: Assigned queue directory %s\n",
5875 			qid_printqueue(e->e_qgrp, e->e_qdir));
5876 
5877 	if (e->e_xfqgrp == NOQGRP || e->e_xfqdir == NOQDIR)
5878 	{
5879 		e->e_xfqgrp = e->e_qgrp;
5880 		e->e_xfqdir = e->e_qdir;
5881 	}
5882 	e->e_dfqdir = e->e_qdir;
5883 	return true;
5884 }
5885 /*
5886 **  CHKQDIR -- check a queue directory
5887 **
5888 **	Parameters:
5889 **		name -- name of queue directory
5890 **		sff -- flags for safefile()
5891 **
5892 **	Returns:
5893 **		is it a queue directory?
5894 */
5895 
5896 static bool chkqdir __P((char *, long));
5897 
5898 static bool
5899 chkqdir(name, sff)
5900 	char *name;
5901 	long sff;
5902 {
5903 	struct stat statb;
5904 	int i;
5905 
5906 	/* skip over . and .. directories */
5907 	if (name[0] == '.' &&
5908 	    (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')))
5909 		return false;
5910 #if HASLSTAT
5911 	if (lstat(name, &statb) < 0)
5912 #else /* HASLSTAT */
5913 	if (stat(name, &statb) < 0)
5914 #endif /* HASLSTAT */
5915 	{
5916 		if (tTd(41, 2))
5917 			sm_dprintf("chkqdir: stat(\"%s\"): %s\n",
5918 				   name, sm_errstring(errno));
5919 		return false;
5920 	}
5921 #if HASLSTAT
5922 	if (S_ISLNK(statb.st_mode))
5923 	{
5924 		/*
5925 		**  For a symlink we need to make sure the
5926 		**  target is a directory
5927 		*/
5928 
5929 		if (stat(name, &statb) < 0)
5930 		{
5931 			if (tTd(41, 2))
5932 				sm_dprintf("chkqdir: stat(\"%s\"): %s\n",
5933 					   name, sm_errstring(errno));
5934 			return false;
5935 		}
5936 	}
5937 #endif /* HASLSTAT */
5938 
5939 	if (!S_ISDIR(statb.st_mode))
5940 	{
5941 		if (tTd(41, 2))
5942 			sm_dprintf("chkqdir: \"%s\": Not a directory\n",
5943 				name);
5944 		return false;
5945 	}
5946 
5947 	/* Print a warning if unsafe (but still use it) */
5948 	/* XXX do this only if we want the warning? */
5949 	i = safedirpath(name, RunAsUid, RunAsGid, NULL, sff, 0, 0);
5950 	if (i != 0)
5951 	{
5952 		if (tTd(41, 2))
5953 			sm_dprintf("chkqdir: \"%s\": Not safe: %s\n",
5954 				   name, sm_errstring(i));
5955 #if _FFR_CHK_QUEUE
5956 		if (LogLevel > 8)
5957 			sm_syslog(LOG_WARNING, NOQID,
5958 				  "queue directory \"%s\": Not safe: %s",
5959 				  name, sm_errstring(i));
5960 #endif /* _FFR_CHK_QUEUE */
5961 	}
5962 	return true;
5963 }
5964 /*
5965 **  MULTIQUEUE_CACHE -- cache a list of paths to queues.
5966 **
5967 **	Each potential queue is checked as the cache is built.
5968 **	Thereafter, each is blindly trusted.
5969 **	Note that we can be called again after a timeout to rebuild
5970 **	(although code for that is not ready yet).
5971 **
5972 **	Parameters:
5973 **		basedir -- base of all queue directories.
5974 **		blen -- strlen(basedir).
5975 **		qg -- queue group.
5976 **		qn -- number of queue directories already cached.
5977 **		phash -- pointer to hash value over queue dirs.
5978 #if SM_CONF_SHM
5979 **			only used if shared memory is active.
5980 #endif * SM_CONF_SHM *
5981 **
5982 **	Returns:
5983 **		new number of queue directories.
5984 */
5985 
5986 #define INITIAL_SLOTS	20
5987 #define ADD_SLOTS	10
5988 
5989 static int
5990 multiqueue_cache(basedir, blen, qg, qn, phash)
5991 	char *basedir;
5992 	int blen;
5993 	QUEUEGRP *qg;
5994 	int qn;
5995 	unsigned int *phash;
5996 {
5997 	char *cp;
5998 	int i, len;
5999 	int slotsleft = 0;
6000 	long sff = SFF_ANYFILE;
6001 	char qpath[MAXPATHLEN];
6002 	char subdir[MAXPATHLEN];
6003 	char prefix[MAXPATHLEN];	/* dir relative to basedir */
6004 
6005 	if (tTd(41, 20))
6006 		sm_dprintf("multiqueue_cache: called\n");
6007 
6008 	/* Initialize to current directory */
6009 	prefix[0] = '.';
6010 	prefix[1] = '\0';
6011 	if (qg->qg_numqueues != 0 && qg->qg_qpaths != NULL)
6012 	{
6013 		for (i = 0; i < qg->qg_numqueues; i++)
6014 		{
6015 			if (qg->qg_qpaths[i].qp_name != NULL)
6016 				(void) sm_free(qg->qg_qpaths[i].qp_name); /* XXX */
6017 		}
6018 		(void) sm_free((char *) qg->qg_qpaths); /* XXX */
6019 		qg->qg_qpaths = NULL;
6020 		qg->qg_numqueues = 0;
6021 	}
6022 
6023 	/* If running as root, allow safedirpath() checks to use privs */
6024 	if (RunAsUid == 0)
6025 		sff |= SFF_ROOTOK;
6026 #if _FFR_CHK_QUEUE
6027 	sff |= SFF_SAFEDIRPATH|SFF_NOWWFILES;
6028 	if (!UseMSP)
6029 		sff |= SFF_NOGWFILES;
6030 #endif /* _FFR_CHK_QUEUE */
6031 
6032 	if (!SM_IS_DIR_START(qg->qg_qdir))
6033 	{
6034 		/*
6035 		**  XXX we could add basedir, but then we have to realloc()
6036 		**  the string... Maybe another time.
6037 		*/
6038 
6039 		syserr("QueuePath %s not absolute", qg->qg_qdir);
6040 		ExitStat = EX_CONFIG;
6041 		return qn;
6042 	}
6043 
6044 	/* qpath: directory of current workgroup */
6045 	len = sm_strlcpy(qpath, qg->qg_qdir, sizeof(qpath));
6046 	if (len >= sizeof(qpath))
6047 	{
6048 		syserr("QueuePath %.256s too long (%d max)",
6049 		       qg->qg_qdir, (int) sizeof(qpath));
6050 		ExitStat = EX_CONFIG;
6051 		return qn;
6052 	}
6053 
6054 	/* begin of qpath must be same as basedir */
6055 	if (strncmp(basedir, qpath, blen) != 0 &&
6056 	    (strncmp(basedir, qpath, blen - 1) != 0 || len != blen - 1))
6057 	{
6058 		syserr("QueuePath %s not subpath of QueueDirectory %s",
6059 			qpath, basedir);
6060 		ExitStat = EX_CONFIG;
6061 		return qn;
6062 	}
6063 
6064 	/* Do we have a nested subdirectory? */
6065 	if (blen < len && SM_FIRST_DIR_DELIM(qg->qg_qdir + blen) != NULL)
6066 	{
6067 
6068 		/* Copy subdirectory into prefix for later use */
6069 		if (sm_strlcpy(prefix, qg->qg_qdir + blen, sizeof(prefix)) >=
6070 		    sizeof(prefix))
6071 		{
6072 			syserr("QueuePath %.256s too long (%d max)",
6073 				qg->qg_qdir, (int) sizeof(qpath));
6074 			ExitStat = EX_CONFIG;
6075 			return qn;
6076 		}
6077 		cp = SM_LAST_DIR_DELIM(prefix);
6078 		SM_ASSERT(cp != NULL);
6079 		*cp = '\0';	/* cut off trailing / */
6080 	}
6081 
6082 	/* This is guaranteed by the basedir check above */
6083 	SM_ASSERT(len >= blen - 1);
6084 	cp = &qpath[len - 1];
6085 	if (*cp == '*')
6086 	{
6087 		register DIR *dp;
6088 		register struct dirent *d;
6089 		int off;
6090 		char *delim;
6091 		char relpath[MAXPATHLEN];
6092 
6093 		*cp = '\0';	/* Overwrite wildcard */
6094 		if ((cp = SM_LAST_DIR_DELIM(qpath)) == NULL)
6095 		{
6096 			syserr("QueueDirectory: can not wildcard relative path");
6097 			if (tTd(41, 2))
6098 				sm_dprintf("multiqueue_cache: \"%s*\": Can not wildcard relative path.\n",
6099 					qpath);
6100 			ExitStat = EX_CONFIG;
6101 			return qn;
6102 		}
6103 		if (cp == qpath)
6104 		{
6105 			/*
6106 			**  Special case of top level wildcard, like /foo*
6107 			**	Change to //foo*
6108 			*/
6109 
6110 			(void) sm_strlcpy(qpath + 1, qpath, sizeof(qpath) - 1);
6111 			++cp;
6112 		}
6113 		delim = cp;
6114 		*(cp++) = '\0';		/* Replace / with \0 */
6115 		len = strlen(cp);	/* Last component of queue directory */
6116 
6117 		/*
6118 		**  Path relative to basedir, with trailing /
6119 		**  It will be modified below to specify the subdirectories
6120 		**  so they can be opened without chdir().
6121 		*/
6122 
6123 		off = sm_strlcpyn(relpath, sizeof(relpath), 2, prefix, "/");
6124 		SM_ASSERT(off < sizeof(relpath));
6125 
6126 		if (tTd(41, 2))
6127 			sm_dprintf("multiqueue_cache: prefix=\"%s%s\"\n",
6128 				   relpath, cp);
6129 
6130 		/* It is always basedir: we don't need to store it per group */
6131 		/* XXX: optimize this! -> one more global? */
6132 		qg->qg_qdir = newstr(basedir);
6133 		qg->qg_qdir[blen - 1] = '\0';	/* cut off trailing / */
6134 
6135 		/*
6136 		**  XXX Should probably wrap this whole loop in a timeout
6137 		**  in case some wag decides to NFS mount the queues.
6138 		*/
6139 
6140 		/* Test path to get warning messages. */
6141 		if (qn == 0)
6142 		{
6143 			/*  XXX qg_runasuid and qg_runasgid for specials? */
6144 			i = safedirpath(basedir, RunAsUid, RunAsGid, NULL,
6145 					sff, 0, 0);
6146 			if (i != 0 && tTd(41, 2))
6147 				sm_dprintf("multiqueue_cache: \"%s\": Not safe: %s\n",
6148 					   basedir, sm_errstring(i));
6149 		}
6150 
6151 		if ((dp = opendir(prefix)) == NULL)
6152 		{
6153 			syserr("can not opendir(%s/%s)", qg->qg_qdir, prefix);
6154 			if (tTd(41, 2))
6155 				sm_dprintf("multiqueue_cache: opendir(\"%s/%s\"): %s\n",
6156 					   qg->qg_qdir, prefix,
6157 					   sm_errstring(errno));
6158 			ExitStat = EX_CONFIG;
6159 			return qn;
6160 		}
6161 		while ((d = readdir(dp)) != NULL)
6162 		{
6163 			/* Skip . and .. directories */
6164 			if (strcmp(d->d_name, ".") == 0 ||
6165 			    strcmp(d->d_name, "..") == 0)
6166 				continue;
6167 
6168 			i = strlen(d->d_name);
6169 			if (i < len || strncmp(d->d_name, cp, len) != 0)
6170 			{
6171 				if (tTd(41, 5))
6172 					sm_dprintf("multiqueue_cache: \"%s\", skipped\n",
6173 						d->d_name);
6174 				continue;
6175 			}
6176 
6177 			/* Create relative pathname: prefix + local directory */
6178 			i = sizeof(relpath) - off;
6179 			if (sm_strlcpy(relpath + off, d->d_name, i) >= i)
6180 				continue;	/* way too long */
6181 
6182 			if (!chkqdir(relpath, sff))
6183 				continue;
6184 
6185 			if (qg->qg_qpaths == NULL)
6186 			{
6187 				slotsleft = INITIAL_SLOTS;
6188 				qg->qg_qpaths = (QPATHS *)xalloc((sizeof(*qg->qg_qpaths)) *
6189 								slotsleft);
6190 				qg->qg_numqueues = 0;
6191 			}
6192 			else if (slotsleft < 1)
6193 			{
6194 				qg->qg_qpaths = (QPATHS *)sm_realloc((char *)qg->qg_qpaths,
6195 							  (sizeof(*qg->qg_qpaths)) *
6196 							  (qg->qg_numqueues +
6197 							   ADD_SLOTS));
6198 				if (qg->qg_qpaths == NULL)
6199 				{
6200 					(void) closedir(dp);
6201 					return qn;
6202 				}
6203 				slotsleft += ADD_SLOTS;
6204 			}
6205 
6206 			/* check subdirs */
6207 			qg->qg_qpaths[qg->qg_numqueues].qp_subdirs = QP_NOSUB;
6208 
6209 #define CHKRSUBDIR(name, flag)	\
6210 	(void) sm_strlcpyn(subdir, sizeof(subdir), 3, relpath, "/", name); \
6211 	if (chkqdir(subdir, sff))	\
6212 		qg->qg_qpaths[qg->qg_numqueues].qp_subdirs |= flag;	\
6213 	else
6214 
6215 
6216 			CHKRSUBDIR("qf", QP_SUBQF);
6217 			CHKRSUBDIR("df", QP_SUBDF);
6218 			CHKRSUBDIR("xf", QP_SUBXF);
6219 
6220 			/* assert(strlen(d->d_name) < MAXPATHLEN - 14) */
6221 			/* maybe even - 17 (subdirs) */
6222 
6223 			if (prefix[0] != '.')
6224 				qg->qg_qpaths[qg->qg_numqueues].qp_name =
6225 					newstr(relpath);
6226 			else
6227 				qg->qg_qpaths[qg->qg_numqueues].qp_name =
6228 					newstr(d->d_name);
6229 
6230 			if (tTd(41, 2))
6231 				sm_dprintf("multiqueue_cache: %d: \"%s\" cached (%x).\n",
6232 					qg->qg_numqueues, relpath,
6233 					qg->qg_qpaths[qg->qg_numqueues].qp_subdirs);
6234 #if SM_CONF_SHM
6235 			qg->qg_qpaths[qg->qg_numqueues].qp_idx = qn;
6236 			*phash = hash_q(relpath, *phash);
6237 #endif /* SM_CONF_SHM */
6238 			qg->qg_numqueues++;
6239 			++qn;
6240 			slotsleft--;
6241 		}
6242 		(void) closedir(dp);
6243 
6244 		/* undo damage */
6245 		*delim = '/';
6246 	}
6247 	if (qg->qg_numqueues == 0)
6248 	{
6249 		qg->qg_qpaths = (QPATHS *) xalloc(sizeof(*qg->qg_qpaths));
6250 
6251 		/* test path to get warning messages */
6252 		i = safedirpath(qpath, RunAsUid, RunAsGid, NULL, sff, 0, 0);
6253 		if (i == ENOENT)
6254 		{
6255 			syserr("can not opendir(%s)", qpath);
6256 			if (tTd(41, 2))
6257 				sm_dprintf("multiqueue_cache: opendir(\"%s\"): %s\n",
6258 					   qpath, sm_errstring(i));
6259 			ExitStat = EX_CONFIG;
6260 			return qn;
6261 		}
6262 
6263 		qg->qg_qpaths[0].qp_subdirs = QP_NOSUB;
6264 		qg->qg_numqueues = 1;
6265 
6266 		/* check subdirs */
6267 #define CHKSUBDIR(name, flag)	\
6268 	(void) sm_strlcpyn(subdir, sizeof(subdir), 3, qg->qg_qdir, "/", name); \
6269 	if (chkqdir(subdir, sff))	\
6270 		qg->qg_qpaths[0].qp_subdirs |= flag;	\
6271 	else
6272 
6273 		CHKSUBDIR("qf", QP_SUBQF);
6274 		CHKSUBDIR("df", QP_SUBDF);
6275 		CHKSUBDIR("xf", QP_SUBXF);
6276 
6277 		if (qg->qg_qdir[blen - 1] != '\0' &&
6278 		    qg->qg_qdir[blen] != '\0')
6279 		{
6280 			/*
6281 			**  Copy the last component into qpaths and
6282 			**  cut off qdir
6283 			*/
6284 
6285 			qg->qg_qpaths[0].qp_name = newstr(qg->qg_qdir + blen);
6286 			qg->qg_qdir[blen - 1] = '\0';
6287 		}
6288 		else
6289 			qg->qg_qpaths[0].qp_name = newstr(".");
6290 
6291 #if SM_CONF_SHM
6292 		qg->qg_qpaths[0].qp_idx = qn;
6293 		*phash = hash_q(qg->qg_qpaths[0].qp_name, *phash);
6294 #endif /* SM_CONF_SHM */
6295 		++qn;
6296 	}
6297 	return qn;
6298 }
6299 
6300 /*
6301 **  FILESYS_FIND -- find entry in FileSys table, or add new one
6302 **
6303 **	Given the pathname of a directory, determine the file system
6304 **	in which that directory resides, and return a pointer to the
6305 **	entry in the FileSys table that describes the file system.
6306 **	A new entry is added if necessary (and requested).
6307 **	If the directory does not exist, -1 is returned.
6308 **
6309 **	Parameters:
6310 **		name -- name of directory (must be persistent!)
6311 **		path -- pathname of directory (name plus maybe "/df")
6312 **		add -- add to structure if not found.
6313 **
6314 **	Returns:
6315 **		>=0: found: index in file system table
6316 **		<0: some error, i.e.,
6317 **		FSF_TOO_MANY: too many filesystems (-> syserr())
6318 **		FSF_STAT_FAIL: can't stat() filesystem (-> syserr())
6319 **		FSF_NOT_FOUND: not in list
6320 */
6321 
6322 static short filesys_find __P((const char *, const char *, bool));
6323 
6324 #define FSF_NOT_FOUND	(-1)
6325 #define FSF_STAT_FAIL	(-2)
6326 #define FSF_TOO_MANY	(-3)
6327 
6328 static short
6329 filesys_find(name, path, add)
6330 	const char *name;
6331 	const char *path;
6332 	bool add;
6333 {
6334 	struct stat st;
6335 	short i;
6336 
6337 	if (stat(path, &st) < 0)
6338 	{
6339 		syserr("cannot stat queue directory %s", path);
6340 		return FSF_STAT_FAIL;
6341 	}
6342 	for (i = 0; i < NumFileSys; ++i)
6343 	{
6344 		if (FILE_SYS_DEV(i) == st.st_dev)
6345 		{
6346 			/*
6347 			**  Make sure the file system (FS) name is set:
6348 			**  even though the source code indicates that
6349 			**  FILE_SYS_DEV() is only set below, it could be
6350 			**  set via shared memory, hence we need to perform
6351 			**  this check/assignment here.
6352 			*/
6353 
6354 			if (NULL == FILE_SYS_NAME(i))
6355 				FILE_SYS_NAME(i) = name;
6356 			return i;
6357 		}
6358 	}
6359 	if (i >= MAXFILESYS)
6360 	{
6361 		syserr("too many queue file systems (%d max)", MAXFILESYS);
6362 		return FSF_TOO_MANY;
6363 	}
6364 	if (!add)
6365 		return FSF_NOT_FOUND;
6366 
6367 	++NumFileSys;
6368 	FILE_SYS_NAME(i) = name;
6369 	FILE_SYS_DEV(i) = st.st_dev;
6370 	FILE_SYS_AVAIL(i) = 0;
6371 	FILE_SYS_BLKSIZE(i) = 1024; /* avoid divide by zero */
6372 	return i;
6373 }
6374 
6375 /*
6376 **  FILESYS_SETUP -- set up mapping from queue directories to file systems
6377 **
6378 **	This data structure is used to efficiently check the amount of
6379 **	free space available in a set of queue directories.
6380 **
6381 **	Parameters:
6382 **		add -- initialize structure if necessary.
6383 **
6384 **	Returns:
6385 **		0: success
6386 **		<0: some error, i.e.,
6387 **		FSF_NOT_FOUND: not in list
6388 **		FSF_STAT_FAIL: can't stat() filesystem (-> syserr())
6389 **		FSF_TOO_MANY: too many filesystems (-> syserr())
6390 */
6391 
6392 static int filesys_setup __P((bool));
6393 
6394 static int
6395 filesys_setup(add)
6396 	bool add;
6397 {
6398 	int i, j;
6399 	short fs;
6400 	int ret;
6401 
6402 	ret = 0;
6403 	for (i = 0; i < NumQueue && Queue[i] != NULL; i++)
6404 	{
6405 		for (j = 0; j < Queue[i]->qg_numqueues; ++j)
6406 		{
6407 			QPATHS *qp = &Queue[i]->qg_qpaths[j];
6408 			char qddf[MAXPATHLEN];
6409 
6410 			(void) sm_strlcpyn(qddf, sizeof(qddf), 2, qp->qp_name,
6411 					(bitset(QP_SUBDF, qp->qp_subdirs)
6412 						? "/df" : ""));
6413 			fs = filesys_find(qp->qp_name, qddf, add);
6414 			if (fs >= 0)
6415 				qp->qp_fsysidx = fs;
6416 			else
6417 				qp->qp_fsysidx = 0;
6418 			if (fs < ret)
6419 				ret = fs;
6420 		}
6421 	}
6422 	return ret;
6423 }
6424 
6425 /*
6426 **  FILESYS_UPDATE -- update amount of free space on all file systems
6427 **
6428 **	The FileSys table is used to cache the amount of free space
6429 **	available on all queue directory file systems.
6430 **	This function updates the cached information if it has expired.
6431 **
6432 **	Parameters:
6433 **		none.
6434 **
6435 **	Returns:
6436 **		none.
6437 **
6438 **	Side Effects:
6439 **		Updates FileSys table.
6440 */
6441 
6442 void
6443 filesys_update()
6444 {
6445 	int i;
6446 	long avail, blksize;
6447 	time_t now;
6448 	static time_t nextupdate = 0;
6449 
6450 #if SM_CONF_SHM
6451 	/*
6452 	**  Only the daemon updates the shared memory, i.e.,
6453 	**  if shared memory is available but the pid is not the
6454 	**  one of the daemon, then don't do anything.
6455 	*/
6456 
6457 	if (ShmId != SM_SHM_NO_ID && DaemonPid != CurrentPid)
6458 		return;
6459 #endif /* SM_CONF_SHM */
6460 	now = curtime();
6461 	if (now < nextupdate)
6462 		return;
6463 	nextupdate = now + FILESYS_UPDATE_INTERVAL;
6464 	for (i = 0; i < NumFileSys; ++i)
6465 	{
6466 		FILESYS *fs = &FILE_SYS(i);
6467 
6468 		avail = freediskspace(FILE_SYS_NAME(i), &blksize);
6469 		if (avail < 0 || blksize <= 0)
6470 		{
6471 			if (LogLevel > 5)
6472 				sm_syslog(LOG_ERR, NOQID,
6473 					"filesys_update failed: %s, fs=%s, avail=%ld, blocksize=%ld",
6474 					sm_errstring(errno),
6475 					FILE_SYS_NAME(i), avail, blksize);
6476 			fs->fs_avail = 0;
6477 			fs->fs_blksize = 1024; /* avoid divide by zero */
6478 			nextupdate = now + 2; /* let's do this soon again */
6479 		}
6480 		else
6481 		{
6482 			fs->fs_avail = avail;
6483 			fs->fs_blksize = blksize;
6484 		}
6485 	}
6486 }
6487 
6488 #if _FFR_ANY_FREE_FS
6489 /*
6490 **  FILESYS_FREE -- check whether there is at least one fs with enough space.
6491 **
6492 **	Parameters:
6493 **		fsize -- file size in bytes
6494 **
6495 **	Returns:
6496 **		true iff there is one fs with more than fsize bytes free.
6497 */
6498 
6499 bool
6500 filesys_free(fsize)
6501 	long fsize;
6502 {
6503 	int i;
6504 
6505 	if (fsize <= 0)
6506 		return true;
6507 	for (i = 0; i < NumFileSys; ++i)
6508 	{
6509 		long needed = 0;
6510 
6511 		if (FILE_SYS_AVAIL(i) < 0 || FILE_SYS_BLKSIZE(i) <= 0)
6512 			continue;
6513 		needed += fsize / FILE_SYS_BLKSIZE(i)
6514 			  + ((fsize % FILE_SYS_BLKSIZE(i)
6515 			      > 0) ? 1 : 0)
6516 			  + MinBlocksFree;
6517 		if (needed <= FILE_SYS_AVAIL(i))
6518 			return true;
6519 	}
6520 	return false;
6521 }
6522 #endif /* _FFR_ANY_FREE_FS */
6523 
6524 /*
6525 **  DISK_STATUS -- show amount of free space in queue directories
6526 **
6527 **	Parameters:
6528 **		out -- output file pointer.
6529 **		prefix -- string to output in front of each line.
6530 **
6531 **	Returns:
6532 **		none.
6533 */
6534 
6535 void
6536 disk_status(out, prefix)
6537 	SM_FILE_T *out;
6538 	char *prefix;
6539 {
6540 	int i;
6541 	long avail, blksize;
6542 	long free;
6543 
6544 	for (i = 0; i < NumFileSys; ++i)
6545 	{
6546 		avail = freediskspace(FILE_SYS_NAME(i), &blksize);
6547 		if (avail >= 0 && blksize > 0)
6548 		{
6549 			free = (long)((double) avail *
6550 				((double) blksize / 1024));
6551 		}
6552 		else
6553 			free = -1;
6554 		(void) sm_io_fprintf(out, SM_TIME_DEFAULT,
6555 				"%s%d/%s/%ld\r\n",
6556 				prefix, i,
6557 				FILE_SYS_NAME(i),
6558 					free);
6559 	}
6560 }
6561 
6562 #if SM_CONF_SHM
6563 
6564 /*
6565 **  INIT_SEM -- initialize semaphore system
6566 **
6567 **	Parameters:
6568 **		owner -- is this the owner of semaphores?
6569 **
6570 **	Returns:
6571 **		none.
6572 */
6573 
6574 #if _FFR_USE_SEM_LOCKING
6575 #if SM_CONF_SEM
6576 static int SemId = -1;		/* Semaphore Id */
6577 int SemKey = SM_SEM_KEY;
6578 #endif /* SM_CONF_SEM */
6579 #endif /* _FFR_USE_SEM_LOCKING */
6580 
6581 static void init_sem __P((bool));
6582 
6583 static void
6584 init_sem(owner)
6585 	bool owner;
6586 {
6587 #if _FFR_USE_SEM_LOCKING
6588 #if SM_CONF_SEM
6589 	SemId = sm_sem_start(SemKey, 1, 0, owner);
6590 	if (SemId < 0)
6591 	{
6592 		sm_syslog(LOG_ERR, NOQID,
6593 			"func=init_sem, sem_key=%ld, sm_sem_start=%d",
6594 			(long) SemKey, SemId);
6595 		return;
6596 	}
6597 #endif /* SM_CONF_SEM */
6598 #endif /* _FFR_USE_SEM_LOCKING */
6599 	return;
6600 }
6601 
6602 /*
6603 **  STOP_SEM -- stop semaphore system
6604 **
6605 **	Parameters:
6606 **		owner -- is this the owner of semaphores?
6607 **
6608 **	Returns:
6609 **		none.
6610 */
6611 
6612 static void stop_sem __P((bool));
6613 
6614 static void
6615 stop_sem(owner)
6616 	bool owner;
6617 {
6618 #if _FFR_USE_SEM_LOCKING
6619 #if SM_CONF_SEM
6620 	if (owner && SemId >= 0)
6621 		sm_sem_stop(SemId);
6622 #endif /* SM_CONF_SEM */
6623 #endif /* _FFR_USE_SEM_LOCKING */
6624 	return;
6625 }
6626 
6627 /*
6628 **  UPD_QS -- update information about queue when adding/deleting an entry
6629 **
6630 **	Parameters:
6631 **		e -- envelope.
6632 **		count -- add/remove entry (+1/0/-1: add/no change/remove)
6633 **		space -- update the space available as well.
6634 **			(>0/0/<0: add/no change/remove)
6635 **		where -- caller (for logging)
6636 **
6637 **	Returns:
6638 **		none.
6639 **
6640 **	Side Effects:
6641 **		Modifies available space in filesystem.
6642 **		Changes number of entries in queue directory.
6643 */
6644 
6645 void
6646 upd_qs(e, count, space, where)
6647 	ENVELOPE *e;
6648 	int count;
6649 	int space;
6650 	char *where;
6651 {
6652 	short fidx;
6653 	int idx;
6654 # if _FFR_USE_SEM_LOCKING
6655 	int r;
6656 # endif /* _FFR_USE_SEM_LOCKING */
6657 	long s;
6658 
6659 	if (ShmId == SM_SHM_NO_ID || e == NULL)
6660 		return;
6661 	if (e->e_qgrp == NOQGRP || e->e_qdir == NOQDIR)
6662 		return;
6663 	idx = Queue[e->e_qgrp]->qg_qpaths[e->e_qdir].qp_idx;
6664 	if (tTd(73,2))
6665 		sm_dprintf("func=upd_qs, count=%d, space=%d, where=%s, idx=%d, entries=%d\n",
6666 			count, space, where, idx, QSHM_ENTRIES(idx));
6667 
6668 	/* XXX in theory this needs to be protected with a mutex */
6669 	if (QSHM_ENTRIES(idx) >= 0 && count != 0)
6670 	{
6671 # if _FFR_USE_SEM_LOCKING
6672 		r = sm_sem_acq(SemId, 0, 1);
6673 # endif /* _FFR_USE_SEM_LOCKING */
6674 		QSHM_ENTRIES(idx) += count;
6675 # if _FFR_USE_SEM_LOCKING
6676 		if (r >= 0)
6677 			r = sm_sem_rel(SemId, 0, 1);
6678 # endif /* _FFR_USE_SEM_LOCKING */
6679 	}
6680 
6681 	fidx = Queue[e->e_qgrp]->qg_qpaths[e->e_qdir].qp_fsysidx;
6682 	if (fidx < 0)
6683 		return;
6684 
6685 	/* update available space also?  (might be loseqfile) */
6686 	if (space == 0)
6687 		return;
6688 
6689 	/* convert size to blocks; this causes rounding errors */
6690 	s = e->e_msgsize / FILE_SYS_BLKSIZE(fidx);
6691 	if (s == 0)
6692 		return;
6693 
6694 	/* XXX in theory this needs to be protected with a mutex */
6695 	if (space > 0)
6696 		FILE_SYS_AVAIL(fidx) += s;
6697 	else
6698 		FILE_SYS_AVAIL(fidx) -= s;
6699 
6700 }
6701 
6702 static bool write_key_file __P((char *, long));
6703 static long read_key_file __P((char *, long));
6704 
6705 /*
6706 **  WRITE_KEY_FILE -- record some key into a file.
6707 **
6708 **	Parameters:
6709 **		keypath -- file name.
6710 **		key -- key to write.
6711 **
6712 **	Returns:
6713 **		true iff file could be written.
6714 **
6715 **	Side Effects:
6716 **		writes file.
6717 */
6718 
6719 static bool
6720 write_key_file(keypath, key)
6721 	char *keypath;
6722 	long key;
6723 {
6724 	bool ok;
6725 	long sff;
6726 	SM_FILE_T *keyf;
6727 
6728 	ok = false;
6729 	if (keypath == NULL || *keypath == '\0')
6730 		return ok;
6731 	sff = SFF_NOLINK|SFF_ROOTOK|SFF_REGONLY|SFF_CREAT;
6732 	if (TrustedUid != 0 && RealUid == TrustedUid)
6733 		sff |= SFF_OPENASROOT;
6734 	keyf = safefopen(keypath, O_WRONLY|O_TRUNC, FileMode, sff);
6735 	if (keyf == NULL)
6736 	{
6737 		sm_syslog(LOG_ERR, NOQID, "unable to write %s: %s",
6738 			  keypath, sm_errstring(errno));
6739 	}
6740 	else
6741 	{
6742 		if (geteuid() == 0 && RunAsUid != 0)
6743 		{
6744 #  if HASFCHOWN
6745 			int fd;
6746 
6747 			fd = keyf->f_file;
6748 			if (fd >= 0 && fchown(fd, RunAsUid, -1) < 0)
6749 			{
6750 				int err = errno;
6751 
6752 				sm_syslog(LOG_ALERT, NOQID,
6753 					  "ownership change on %s to %d failed: %s",
6754 					  keypath, RunAsUid, sm_errstring(err));
6755 			}
6756 #  endif /* HASFCHOWN */
6757 		}
6758 		ok = sm_io_fprintf(keyf, SM_TIME_DEFAULT, "%ld\n", key) !=
6759 		     SM_IO_EOF;
6760 		ok = (sm_io_close(keyf, SM_TIME_DEFAULT) != SM_IO_EOF) && ok;
6761 	}
6762 	return ok;
6763 }
6764 
6765 /*
6766 **  READ_KEY_FILE -- read a key from a file.
6767 **
6768 **	Parameters:
6769 **		keypath -- file name.
6770 **		key -- default key.
6771 **
6772 **	Returns:
6773 **		key.
6774 */
6775 
6776 static long
6777 read_key_file(keypath, key)
6778 	char *keypath;
6779 	long key;
6780 {
6781 	int r;
6782 	long sff, n;
6783 	SM_FILE_T *keyf;
6784 
6785 	if (keypath == NULL || *keypath == '\0')
6786 		return key;
6787 	sff = SFF_NOLINK|SFF_ROOTOK|SFF_REGONLY;
6788 	if (RealUid == 0 || (TrustedUid != 0 && RealUid == TrustedUid))
6789 		sff |= SFF_OPENASROOT;
6790 	keyf = safefopen(keypath, O_RDONLY, FileMode, sff);
6791 	if (keyf == NULL)
6792 	{
6793 		sm_syslog(LOG_ERR, NOQID, "unable to read %s: %s",
6794 			  keypath, sm_errstring(errno));
6795 	}
6796 	else
6797 	{
6798 		r = sm_io_fscanf(keyf, SM_TIME_DEFAULT, "%ld", &n);
6799 		if (r == 1)
6800 			key = n;
6801 		(void) sm_io_close(keyf, SM_TIME_DEFAULT);
6802 	}
6803 	return key;
6804 }
6805 
6806 /*
6807 **  INIT_SHM -- initialize shared memory structure
6808 **
6809 **	Initialize or attach to shared memory segment.
6810 **	Currently it is not a fatal error if this doesn't work.
6811 **	However, it causes us to have a "fallback" storage location
6812 **	for everything that is supposed to be in the shared memory,
6813 **	which makes the code slightly ugly.
6814 **
6815 **	Parameters:
6816 **		qn -- number of queue directories.
6817 **		owner -- owner of shared memory.
6818 **		hash -- identifies data that is stored in shared memory.
6819 **
6820 **	Returns:
6821 **		none.
6822 */
6823 
6824 static void init_shm __P((int, bool, unsigned int));
6825 
6826 static void
6827 init_shm(qn, owner, hash)
6828 	int qn;
6829 	bool owner;
6830 	unsigned int hash;
6831 {
6832 	int i;
6833 	int count;
6834 	int save_errno;
6835 	bool keyselect;
6836 
6837 	PtrFileSys = &FileSys[0];
6838 	PNumFileSys = &Numfilesys;
6839 /* if this "key" is specified: select one yourself */
6840 #define SEL_SHM_KEY	((key_t) -1)
6841 #define FIRST_SHM_KEY	25
6842 
6843 	/* This allows us to disable shared memory at runtime. */
6844 	if (ShmKey == 0)
6845 		return;
6846 
6847 	count = 0;
6848 	shms = SM_T_SIZE + qn * sizeof(QUEUE_SHM_T);
6849 	keyselect = ShmKey == SEL_SHM_KEY;
6850 	if (keyselect)
6851 	{
6852 		if (owner)
6853 			ShmKey = FIRST_SHM_KEY;
6854 		else
6855 		{
6856 			errno = 0;
6857 			ShmKey = read_key_file(ShmKeyFile, ShmKey);
6858 			keyselect = false;
6859 			if (ShmKey == SEL_SHM_KEY)
6860 			{
6861 				save_errno = (errno != 0) ? errno : EINVAL;
6862 				goto error;
6863 			}
6864 		}
6865 	}
6866 	for (;;)
6867 	{
6868 		/* allow read/write access for group? */
6869 		Pshm = sm_shmstart(ShmKey, shms,
6870 				SHM_R|SHM_W|(SHM_R>>3)|(SHM_W>>3),
6871 				&ShmId, owner);
6872 		save_errno = errno;
6873 		if (Pshm != NULL || !sm_file_exists(save_errno))
6874 			break;
6875 		if (++count >= 3)
6876 		{
6877 			if (keyselect)
6878 			{
6879 				++ShmKey;
6880 
6881 				/* back where we started? */
6882 				if (ShmKey == SEL_SHM_KEY)
6883 					break;
6884 				continue;
6885 			}
6886 			break;
6887 		}
6888 
6889 		/* only sleep if we are at the first key */
6890 		if (!keyselect || ShmKey == SEL_SHM_KEY)
6891 			sleep(count);
6892 	}
6893 	if (Pshm != NULL)
6894 	{
6895 		int *p;
6896 
6897 		if (keyselect)
6898 			(void) write_key_file(ShmKeyFile, (long) ShmKey);
6899 		if (owner && RunAsUid != 0)
6900 		{
6901 			i = sm_shmsetowner(ShmId, RunAsUid, RunAsGid, 0660);
6902 			if (i != 0)
6903 				sm_syslog(LOG_ERR, NOQID,
6904 					"key=%ld, sm_shmsetowner=%d, RunAsUid=%d, RunAsGid=%d",
6905 					(long) ShmKey, i, RunAsUid, RunAsGid);
6906 		}
6907 		p = (int *) Pshm;
6908 		if (owner)
6909 		{
6910 			*p = (int) shms;
6911 			*((pid_t *) SHM_OFF_PID(Pshm)) = CurrentPid;
6912 			p = (int *) SHM_OFF_TAG(Pshm);
6913 			*p = hash;
6914 		}
6915 		else
6916 		{
6917 			if (*p != (int) shms)
6918 			{
6919 				save_errno = EINVAL;
6920 				cleanup_shm(false);
6921 				goto error;
6922 			}
6923 			p = (int *) SHM_OFF_TAG(Pshm);
6924 			if (*p != (int) hash)
6925 			{
6926 				save_errno = EINVAL;
6927 				cleanup_shm(false);
6928 				goto error;
6929 			}
6930 
6931 			/*
6932 			**  XXX how to check the pid?
6933 			**  Read it from the pid-file? That does
6934 			**  not need to exist.
6935 			**  We could disable shm if we can't confirm
6936 			**  that it is the right one.
6937 			*/
6938 		}
6939 
6940 		PtrFileSys = (FILESYS *) OFF_FILE_SYS(Pshm);
6941 		PNumFileSys = (int *) OFF_NUM_FILE_SYS(Pshm);
6942 		QShm = (QUEUE_SHM_T *) OFF_QUEUE_SHM(Pshm);
6943 		PRSATmpCnt = (int *) OFF_RSA_TMP_CNT(Pshm);
6944 		*PRSATmpCnt = 0;
6945 		if (owner)
6946 		{
6947 			/* initialize values in shared memory */
6948 			NumFileSys = 0;
6949 			for (i = 0; i < qn; i++)
6950 				QShm[i].qs_entries = -1;
6951 		}
6952 		init_sem(owner);
6953 		return;
6954 	}
6955   error:
6956 	if (LogLevel > (owner ? 8 : 11))
6957 	{
6958 		sm_syslog(owner ? LOG_ERR : LOG_NOTICE, NOQID,
6959 			  "can't %s shared memory, key=%ld: %s",
6960 			  owner ? "initialize" : "attach to",
6961 			  (long) ShmKey, sm_errstring(save_errno));
6962 	}
6963 }
6964 #endif /* SM_CONF_SHM */
6965 
6966 
6967 /*
6968 **  SETUP_QUEUES -- set up all queue groups
6969 **
6970 **	Parameters:
6971 **		owner -- owner of shared memory?
6972 **
6973 **	Returns:
6974 **		none.
6975 **
6976 #if SM_CONF_SHM
6977 **	Side Effects:
6978 **		attaches shared memory.
6979 #endif * SM_CONF_SHM *
6980 */
6981 
6982 void
6983 setup_queues(owner)
6984 	bool owner;
6985 {
6986 	int i, qn, len;
6987 	unsigned int hashval;
6988 	time_t now;
6989 	char basedir[MAXPATHLEN];
6990 	struct stat st;
6991 
6992 	/*
6993 	**  Determine basedir for all queue directories.
6994 	**  All queue directories must be (first level) subdirectories
6995 	**  of the basedir.  The basedir is the QueueDir
6996 	**  without wildcards, but with trailing /
6997 	*/
6998 
6999 	hashval = 0;
7000 	errno = 0;
7001 	len = sm_strlcpy(basedir, QueueDir, sizeof(basedir));
7002 
7003 	/* Provide space for trailing '/' */
7004 	if (len >= sizeof(basedir) - 1)
7005 	{
7006 		syserr("QueueDirectory: path too long: %d,  max %d",
7007 			len, (int) sizeof(basedir) - 1);
7008 		ExitStat = EX_CONFIG;
7009 		return;
7010 	}
7011 	SM_ASSERT(len > 0);
7012 	if (basedir[len - 1] == '*')
7013 	{
7014 		char *cp;
7015 
7016 		cp = SM_LAST_DIR_DELIM(basedir);
7017 		if (cp == NULL)
7018 		{
7019 			syserr("QueueDirectory: can not wildcard relative path \"%s\"",
7020 				QueueDir);
7021 			if (tTd(41, 2))
7022 				sm_dprintf("setup_queues: \"%s\": Can not wildcard relative path.\n",
7023 					QueueDir);
7024 			ExitStat = EX_CONFIG;
7025 			return;
7026 		}
7027 
7028 		/* cut off wildcard pattern */
7029 		*++cp = '\0';
7030 		len = cp - basedir;
7031 	}
7032 	else if (!SM_IS_DIR_DELIM(basedir[len - 1]))
7033 	{
7034 		/* append trailing slash since it is a directory */
7035 		basedir[len] = '/';
7036 		basedir[++len] = '\0';
7037 	}
7038 
7039 	/* len counts up to the last directory delimiter */
7040 	SM_ASSERT(basedir[len - 1] == '/');
7041 
7042 	if (chdir(basedir) < 0)
7043 	{
7044 		int save_errno = errno;
7045 
7046 		syserr("can not chdir(%s)", basedir);
7047 		if (save_errno == EACCES)
7048 			(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
7049 				"Program mode requires special privileges, e.g., root or TrustedUser.\n");
7050 		if (tTd(41, 2))
7051 			sm_dprintf("setup_queues: \"%s\": %s\n",
7052 				   basedir, sm_errstring(errno));
7053 		ExitStat = EX_CONFIG;
7054 		return;
7055 	}
7056 #if SM_CONF_SHM
7057 	hashval = hash_q(basedir, hashval);
7058 #endif /* SM_CONF_SHM */
7059 
7060 	/* initialize for queue runs */
7061 	DoQueueRun = false;
7062 	now = curtime();
7063 	for (i = 0; i < NumQueue && Queue[i] != NULL; i++)
7064 		Queue[i]->qg_nextrun = now;
7065 
7066 
7067 	if (UseMSP && OpMode != MD_TEST)
7068 	{
7069 		long sff = SFF_CREAT;
7070 
7071 		if (stat(".", &st) < 0)
7072 		{
7073 			syserr("can not stat(%s)", basedir);
7074 			if (tTd(41, 2))
7075 				sm_dprintf("setup_queues: \"%s\": %s\n",
7076 					   basedir, sm_errstring(errno));
7077 			ExitStat = EX_CONFIG;
7078 			return;
7079 		}
7080 		if (RunAsUid == 0)
7081 			sff |= SFF_ROOTOK;
7082 
7083 		/*
7084 		**  Check queue directory permissions.
7085 		**	Can we write to a group writable queue directory?
7086 		*/
7087 
7088 		if (bitset(S_IWGRP, QueueFileMode) &&
7089 		    bitset(S_IWGRP, st.st_mode) &&
7090 		    safefile(" ", RunAsUid, RunAsGid, RunAsUserName, sff,
7091 			     QueueFileMode, NULL) != 0)
7092 		{
7093 			syserr("can not write to queue directory %s (RunAsGid=%d, required=%d)",
7094 				basedir, (int) RunAsGid, (int) st.st_gid);
7095 		}
7096 		if (bitset(S_IWOTH|S_IXOTH, st.st_mode))
7097 		{
7098 #if _FFR_MSP_PARANOIA
7099 			syserr("dangerous permissions=%o on queue directory %s",
7100 				(int) st.st_mode, basedir);
7101 #else /* _FFR_MSP_PARANOIA */
7102 			if (LogLevel > 0)
7103 				sm_syslog(LOG_ERR, NOQID,
7104 					  "dangerous permissions=%o on queue directory %s",
7105 					  (int) st.st_mode, basedir);
7106 #endif /* _FFR_MSP_PARANOIA */
7107 		}
7108 #if _FFR_MSP_PARANOIA
7109 		if (NumQueue > 1)
7110 			syserr("can not use multiple queues for MSP");
7111 #endif /* _FFR_MSP_PARANOIA */
7112 	}
7113 
7114 	/* initial number of queue directories */
7115 	qn = 0;
7116 	for (i = 0; i < NumQueue && Queue[i] != NULL; i++)
7117 		qn = multiqueue_cache(basedir, len, Queue[i], qn, &hashval);
7118 
7119 #if SM_CONF_SHM
7120 	init_shm(qn, owner, hashval);
7121 	i = filesys_setup(owner || ShmId == SM_SHM_NO_ID);
7122 	if (i == FSF_NOT_FOUND)
7123 	{
7124 		/*
7125 		**  We didn't get the right filesystem data
7126 		**  This may happen if we don't have the right shared memory.
7127 		**  So let's do this without shared memory.
7128 		*/
7129 
7130 		SM_ASSERT(!owner);
7131 		cleanup_shm(false);	/* release shared memory */
7132 		i = filesys_setup(false);
7133 		if (i < 0)
7134 			syserr("filesys_setup failed twice, result=%d", i);
7135 		else if (LogLevel > 8)
7136 			sm_syslog(LOG_WARNING, NOQID,
7137 				  "shared memory does not contain expected data, ignored");
7138 	}
7139 #else /* SM_CONF_SHM */
7140 	i = filesys_setup(true);
7141 #endif /* SM_CONF_SHM */
7142 	if (i < 0)
7143 		ExitStat = EX_CONFIG;
7144 }
7145 
7146 #if SM_CONF_SHM
7147 /*
7148 **  CLEANUP_SHM -- do some cleanup work for shared memory etc
7149 **
7150 **	Parameters:
7151 **		owner -- owner of shared memory?
7152 **
7153 **	Returns:
7154 **		none.
7155 **
7156 **	Side Effects:
7157 **		detaches shared memory.
7158 */
7159 
7160 void
7161 cleanup_shm(owner)
7162 	bool owner;
7163 {
7164 	if (ShmId != SM_SHM_NO_ID)
7165 	{
7166 		if (sm_shmstop(Pshm, ShmId, owner) < 0 && LogLevel > 8)
7167 			sm_syslog(LOG_INFO, NOQID, "sm_shmstop failed=%s",
7168 				  sm_errstring(errno));
7169 		Pshm = NULL;
7170 		ShmId = SM_SHM_NO_ID;
7171 	}
7172 	stop_sem(owner);
7173 }
7174 #endif /* SM_CONF_SHM */
7175 
7176 /*
7177 **  CLEANUP_QUEUES -- do some cleanup work for queues
7178 **
7179 **	Parameters:
7180 **		none.
7181 **
7182 **	Returns:
7183 **		none.
7184 **
7185 */
7186 
7187 void
7188 cleanup_queues()
7189 {
7190 	sync_queue_time();
7191 }
7192 /*
7193 **  SET_DEF_QUEUEVAL -- set default values for a queue group.
7194 **
7195 **	Parameters:
7196 **		qg -- queue group
7197 **		all -- set all values (true for default group)?
7198 **
7199 **	Returns:
7200 **		none.
7201 **
7202 **	Side Effects:
7203 **		sets default values for the queue group.
7204 */
7205 
7206 void
7207 set_def_queueval(qg, all)
7208 	QUEUEGRP *qg;
7209 	bool all;
7210 {
7211 	if (bitnset(QD_DEFINED, qg->qg_flags))
7212 		return;
7213 	if (all)
7214 		qg->qg_qdir = QueueDir;
7215 #if _FFR_QUEUE_GROUP_SORTORDER
7216 	qg->qg_sortorder = QueueSortOrder;
7217 #endif /* _FFR_QUEUE_GROUP_SORTORDER */
7218 	qg->qg_maxqrun = all ? MaxRunnersPerQueue : -1;
7219 	qg->qg_nice = NiceQueueRun;
7220 }
7221 /*
7222 **  MAKEQUEUE -- define a new queue.
7223 **
7224 **	Parameters:
7225 **		line -- description of queue.  This is in labeled fields.
7226 **			The fields are:
7227 **			   F -- the flags associated with the queue
7228 **			   I -- the interval between running the queue
7229 **			   J -- the maximum # of jobs in work list
7230 **			   [M -- the maximum # of jobs in a queue run]
7231 **			   N -- the niceness at which to run
7232 **			   P -- the path to the queue
7233 **			   S -- the queue sorting order
7234 **			   R -- number of parallel queue runners
7235 **			   r -- max recipients per envelope
7236 **			The first word is the canonical name of the queue.
7237 **		qdef -- this is a 'Q' definition from .cf
7238 **
7239 **	Returns:
7240 **		none.
7241 **
7242 **	Side Effects:
7243 **		enters the queue into the queue table.
7244 */
7245 
7246 void
7247 makequeue(line, qdef)
7248 	char *line;
7249 	bool qdef;
7250 {
7251 	register char *p;
7252 	register QUEUEGRP *qg;
7253 	register STAB *s;
7254 	int i;
7255 	char fcode;
7256 
7257 	/* allocate a queue and set up defaults */
7258 	qg = (QUEUEGRP *) xalloc(sizeof(*qg));
7259 	memset((char *) qg, '\0', sizeof(*qg));
7260 
7261 	if (line[0] == '\0')
7262 	{
7263 		syserr("name required for queue");
7264 		return;
7265 	}
7266 
7267 	/* collect the queue name */
7268 	for (p = line;
7269 	     *p != '\0' && *p != ',' && !(isascii(*p) && isspace(*p));
7270 	     p++)
7271 		continue;
7272 	if (*p != '\0')
7273 		*p++ = '\0';
7274 	qg->qg_name = newstr(line);
7275 
7276 	/* set default values, can be overridden below */
7277 	set_def_queueval(qg, false);
7278 
7279 	/* now scan through and assign info from the fields */
7280 	while (*p != '\0')
7281 	{
7282 		auto char *delimptr;
7283 
7284 		while (*p != '\0' &&
7285 		       (*p == ',' || (isascii(*p) && isspace(*p))))
7286 			p++;
7287 
7288 		/* p now points to field code */
7289 		fcode = *p;
7290 		while (*p != '\0' && *p != '=' && *p != ',')
7291 			p++;
7292 		if (*p++ != '=')
7293 		{
7294 			syserr("queue %s: `=' expected", qg->qg_name);
7295 			return;
7296 		}
7297 		while (isascii(*p) && isspace(*p))
7298 			p++;
7299 
7300 		/* p now points to the field body */
7301 		p = munchstring(p, &delimptr, ',');
7302 
7303 		/* install the field into the queue struct */
7304 		switch (fcode)
7305 		{
7306 		  case 'P':		/* pathname */
7307 			if (*p == '\0')
7308 				syserr("queue %s: empty path name",
7309 					qg->qg_name);
7310 			else
7311 				qg->qg_qdir = newstr(p);
7312 			break;
7313 
7314 		  case 'F':		/* flags */
7315 			for (; *p != '\0'; p++)
7316 				if (!(isascii(*p) && isspace(*p)))
7317 					setbitn(*p, qg->qg_flags);
7318 			break;
7319 
7320 			/*
7321 			**  Do we need two intervals here:
7322 			**  One for persistent queue runners,
7323 			**  one for "normal" queue runs?
7324 			*/
7325 
7326 		  case 'I':	/* interval between running the queue */
7327 			qg->qg_queueintvl = convtime(p, 'm');
7328 			break;
7329 
7330 		  case 'N':		/* run niceness */
7331 			qg->qg_nice = atoi(p);
7332 			break;
7333 
7334 		  case 'R':		/* maximum # of runners for the group */
7335 			i = atoi(p);
7336 
7337 			/* can't have more runners than allowed total */
7338 			if (MaxQueueChildren > 0 && i > MaxQueueChildren)
7339 			{
7340 				qg->qg_maxqrun = MaxQueueChildren;
7341 				(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
7342 						     "Q=%s: R=%d exceeds MaxQueueChildren=%d, set to MaxQueueChildren\n",
7343 						     qg->qg_name, i,
7344 						     MaxQueueChildren);
7345 			}
7346 			else
7347 				qg->qg_maxqrun = i;
7348 			break;
7349 
7350 		  case 'J':		/* maximum # of jobs in work list */
7351 			qg->qg_maxlist = atoi(p);
7352 			break;
7353 
7354 		  case 'r':		/* max recipients per envelope */
7355 			qg->qg_maxrcpt = atoi(p);
7356 			break;
7357 
7358 #if _FFR_QUEUE_GROUP_SORTORDER
7359 		  case 'S':		/* queue sorting order */
7360 			switch (*p)
7361 			{
7362 			  case 'h':	/* Host first */
7363 			  case 'H':
7364 				qg->qg_sortorder = QSO_BYHOST;
7365 				break;
7366 
7367 			  case 'p':	/* Priority order */
7368 			  case 'P':
7369 				qg->qg_sortorder = QSO_BYPRIORITY;
7370 				break;
7371 
7372 			  case 't':	/* Submission time */
7373 			  case 'T':
7374 				qg->qg_sortorder = QSO_BYTIME;
7375 				break;
7376 
7377 			  case 'f':	/* File name */
7378 			  case 'F':
7379 				qg->qg_sortorder = QSO_BYFILENAME;
7380 				break;
7381 
7382 			  case 'm':	/* Modification time */
7383 			  case 'M':
7384 				qg->qg_sortorder = QSO_BYMODTIME;
7385 				break;
7386 
7387 			  case 'r':	/* Random */
7388 			  case 'R':
7389 				qg->qg_sortorder = QSO_RANDOM;
7390 				break;
7391 
7392 # if _FFR_RHS
7393 			  case 's':	/* Shuffled host name */
7394 			  case 'S':
7395 				qg->qg_sortorder = QSO_BYSHUFFLE;
7396 				break;
7397 # endif /* _FFR_RHS */
7398 
7399 			  case 'n':	/* none */
7400 			  case 'N':
7401 				qg->qg_sortorder = QSO_NONE;
7402 				break;
7403 
7404 			  default:
7405 				syserr("Invalid queue sort order \"%s\"", p);
7406 			}
7407 			break;
7408 #endif /* _FFR_QUEUE_GROUP_SORTORDER */
7409 
7410 		  default:
7411 			syserr("Q%s: unknown queue equate %c=",
7412 			       qg->qg_name, fcode);
7413 			break;
7414 		}
7415 
7416 		p = delimptr;
7417 	}
7418 
7419 #if !HASNICE
7420 	if (qg->qg_nice != NiceQueueRun)
7421 	{
7422 		(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
7423 				     "Q%s: Warning: N= set on system that doesn't support nice()\n",
7424 				     qg->qg_name);
7425 	}
7426 #endif /* !HASNICE */
7427 
7428 	/* do some rationality checking */
7429 	if (NumQueue >= MAXQUEUEGROUPS)
7430 	{
7431 		syserr("too many queue groups defined (%d max)",
7432 			MAXQUEUEGROUPS);
7433 		return;
7434 	}
7435 
7436 	if (qg->qg_qdir == NULL)
7437 	{
7438 		if (QueueDir == NULL || *QueueDir == '\0')
7439 		{
7440 			syserr("QueueDir must be defined before queue groups");
7441 			return;
7442 		}
7443 		qg->qg_qdir = newstr(QueueDir);
7444 	}
7445 
7446 	if (qg->qg_maxqrun > 1 && !bitnset(QD_FORK, qg->qg_flags))
7447 	{
7448 		(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
7449 				     "Warning: Q=%s: R=%d: multiple queue runners specified\n\tbut flag '%c' is not set\n",
7450 				     qg->qg_name, qg->qg_maxqrun, QD_FORK);
7451 	}
7452 
7453 	/* enter the queue into the symbol table */
7454 	if (tTd(37, 8))
7455 		sm_syslog(LOG_INFO, NOQID,
7456 			  "Adding %s to stab, path: %s", qg->qg_name,
7457 			  qg->qg_qdir);
7458 	s = stab(qg->qg_name, ST_QUEUE, ST_ENTER);
7459 	if (s->s_quegrp != NULL)
7460 	{
7461 		i = s->s_quegrp->qg_index;
7462 
7463 		/* XXX what about the pointers inside this struct? */
7464 		sm_free(s->s_quegrp); /* XXX */
7465 	}
7466 	else
7467 		i = NumQueue++;
7468 	Queue[i] = s->s_quegrp = qg;
7469 	qg->qg_index = i;
7470 
7471 	/* set default value for max queue runners */
7472 	if (qg->qg_maxqrun < 0)
7473 	{
7474 		if (MaxRunnersPerQueue > 0)
7475 			qg->qg_maxqrun = MaxRunnersPerQueue;
7476 		else
7477 			qg->qg_maxqrun = 1;
7478 	}
7479 	if (qdef)
7480 		setbitn(QD_DEFINED, qg->qg_flags);
7481 }
7482 #if 0
7483 /*
7484 **  HASHFQN -- calculate a hash value for a fully qualified host name
7485 **
7486 **	Arguments:
7487 **		fqn -- an all lower-case host.domain string
7488 **		buckets -- the number of buckets (queue directories)
7489 **
7490 **	Returns:
7491 **		a bucket number (signed integer)
7492 **		-1 on error
7493 **
7494 **	Contributed by Exactis.com, Inc.
7495 */
7496 
7497 int
7498 hashfqn(fqn, buckets)
7499 	register char *fqn;
7500 	int buckets;
7501 {
7502 	register char *p;
7503 	register int h = 0, hash, cnt;
7504 
7505 	if (fqn == NULL)
7506 		return -1;
7507 
7508 	/*
7509 	**  A variation on the gdb hash
7510 	**  This is the best as of Feb 19, 1996 --bcx
7511 	*/
7512 
7513 	p = fqn;
7514 	h = 0x238F13AF * strlen(p);
7515 	for (cnt = 0; *p != 0; ++p, cnt++)
7516 	{
7517 		h = (h + (*p << (cnt * 5 % 24))) & 0x7FFFFFFF;
7518 	}
7519 	h = (1103515243 * h + 12345) & 0x7FFFFFFF;
7520 	if (buckets < 2)
7521 		hash = 0;
7522 	else
7523 		hash = (h % buckets);
7524 
7525 	return hash;
7526 }
7527 #endif /* 0 */
7528 
7529 /*
7530 **  A structure for sorting Queue according to maxqrun without
7531 **	screwing up Queue itself.
7532 */
7533 
7534 struct sortqgrp
7535 {
7536 	int sg_idx;		/* original index */
7537 	int sg_maxqrun;		/* max queue runners */
7538 };
7539 typedef struct sortqgrp	SORTQGRP_T;
7540 static int cmpidx __P((const void *, const void *));
7541 
7542 static int
7543 cmpidx(a, b)
7544 	const void *a;
7545 	const void *b;
7546 {
7547 	/* The sort is highest to lowest, so the comparison is reversed */
7548 	if (((SORTQGRP_T *)a)->sg_maxqrun < ((SORTQGRP_T *)b)->sg_maxqrun)
7549 		return 1;
7550 	else if (((SORTQGRP_T *)a)->sg_maxqrun > ((SORTQGRP_T *)b)->sg_maxqrun)
7551 		return -1;
7552 	else
7553 		return 0;
7554 }
7555 
7556 /*
7557 **  MAKEWORKGROUP -- balance queue groups into work groups per MaxQueueChildren
7558 **
7559 **  Take the now defined queue groups and assign them to work groups.
7560 **  This is done to balance out the number of concurrently active
7561 **  queue runners such that MaxQueueChildren is not exceeded. This may
7562 **  result in more than one queue group per work group. In such a case
7563 **  the number of running queue groups in that work group will have no
7564 **  more than the work group maximum number of runners (a "fair" portion
7565 **  of MaxQueueRunners). All queue groups within a work group will get a
7566 **  chance at running.
7567 **
7568 **	Parameters:
7569 **		none.
7570 **
7571 **	Returns:
7572 **		nothing.
7573 **
7574 **	Side Effects:
7575 **		Sets up WorkGrp structure.
7576 */
7577 
7578 void
7579 makeworkgroups()
7580 {
7581 	int i, j, total_runners, dir, h;
7582 	SORTQGRP_T si[MAXQUEUEGROUPS + 1];
7583 
7584 	total_runners = 0;
7585 	if (NumQueue == 1 && strcmp(Queue[0]->qg_name, "mqueue") == 0)
7586 	{
7587 		/*
7588 		**  There is only the "mqueue" queue group (a default)
7589 		**  containing all of the queues. We want to provide to
7590 		**  this queue group the maximum allowable queue runners.
7591 		**  To match older behavior (8.10/8.11) we'll try for
7592 		**  1 runner per queue capping it at MaxQueueChildren.
7593 		**  So if there are N queues, then there will be N runners
7594 		**  for the "mqueue" queue group (where N is kept less than
7595 		**  MaxQueueChildren).
7596 		*/
7597 
7598 		NumWorkGroups = 1;
7599 		WorkGrp[0].wg_numqgrp = 1;
7600 		WorkGrp[0].wg_qgs = (QUEUEGRP **) xalloc(sizeof(QUEUEGRP *));
7601 		WorkGrp[0].wg_qgs[0] = Queue[0];
7602 		if (MaxQueueChildren > 0 &&
7603 		    Queue[0]->qg_numqueues > MaxQueueChildren)
7604 			WorkGrp[0].wg_runners = MaxQueueChildren;
7605 		else
7606 			WorkGrp[0].wg_runners = Queue[0]->qg_numqueues;
7607 
7608 		Queue[0]->qg_wgrp = 0;
7609 
7610 		/* can't have more runners than allowed total */
7611 		if (MaxQueueChildren > 0 &&
7612 		    Queue[0]->qg_maxqrun > MaxQueueChildren)
7613 			Queue[0]->qg_maxqrun = MaxQueueChildren;
7614 		WorkGrp[0].wg_maxact = Queue[0]->qg_maxqrun;
7615 		WorkGrp[0].wg_lowqintvl = Queue[0]->qg_queueintvl;
7616 		return;
7617 	}
7618 
7619 	for (i = 0; i < NumQueue; i++)
7620 	{
7621 		si[i].sg_maxqrun = Queue[i]->qg_maxqrun;
7622 		si[i].sg_idx = i;
7623 	}
7624 	qsort(si, NumQueue, sizeof(si[0]), cmpidx);
7625 
7626 	NumWorkGroups = 0;
7627 	for (i = 0; i < NumQueue; i++)
7628 	{
7629 		total_runners += si[i].sg_maxqrun;
7630 		if (MaxQueueChildren <= 0 || total_runners <= MaxQueueChildren)
7631 			NumWorkGroups++;
7632 		else
7633 			break;
7634 	}
7635 
7636 	if (NumWorkGroups < 1)
7637 		NumWorkGroups = 1; /* gotta have one at least */
7638 	else if (NumWorkGroups > MAXWORKGROUPS)
7639 		NumWorkGroups = MAXWORKGROUPS; /* the limit */
7640 
7641 	/*
7642 	**  We now know the number of work groups to pack the queue groups
7643 	**  into. The queue groups in 'Queue' are sorted from highest
7644 	**  to lowest for the number of runners per queue group.
7645 	**  We put the queue groups with the largest number of runners
7646 	**  into work groups first. Then the smaller ones are fitted in
7647 	**  where it looks best.
7648 	*/
7649 
7650 	j = 0;
7651 	dir = 1;
7652 	for (i = 0; i < NumQueue; i++)
7653 	{
7654 		/* a to-and-fro packing scheme, continue from last position */
7655 		if (j >= NumWorkGroups)
7656 		{
7657 			dir = -1;
7658 			j = NumWorkGroups - 1;
7659 		}
7660 		else if (j < 0)
7661 		{
7662 			j = 0;
7663 			dir = 1;
7664 		}
7665 
7666 		if (WorkGrp[j].wg_qgs == NULL)
7667 			WorkGrp[j].wg_qgs = (QUEUEGRP **)sm_malloc(sizeof(QUEUEGRP *) *
7668 							(WorkGrp[j].wg_numqgrp + 1));
7669 		else
7670 			WorkGrp[j].wg_qgs = (QUEUEGRP **)sm_realloc(WorkGrp[j].wg_qgs,
7671 							sizeof(QUEUEGRP *) *
7672 							(WorkGrp[j].wg_numqgrp + 1));
7673 		if (WorkGrp[j].wg_qgs == NULL)
7674 		{
7675 			syserr("!cannot allocate memory for work queues, need %d bytes",
7676 			       (int) (sizeof(QUEUEGRP *) *
7677 				      (WorkGrp[j].wg_numqgrp + 1)));
7678 		}
7679 
7680 		h = si[i].sg_idx;
7681 		WorkGrp[j].wg_qgs[WorkGrp[j].wg_numqgrp] = Queue[h];
7682 		WorkGrp[j].wg_numqgrp++;
7683 		WorkGrp[j].wg_runners += Queue[h]->qg_maxqrun;
7684 		Queue[h]->qg_wgrp = j;
7685 
7686 		if (WorkGrp[j].wg_maxact == 0)
7687 		{
7688 			/* can't have more runners than allowed total */
7689 			if (MaxQueueChildren > 0 &&
7690 			    Queue[h]->qg_maxqrun > MaxQueueChildren)
7691 				Queue[h]->qg_maxqrun = MaxQueueChildren;
7692 			WorkGrp[j].wg_maxact = Queue[h]->qg_maxqrun;
7693 		}
7694 
7695 		/*
7696 		**  XXX: must wg_lowqintvl be the GCD?
7697 		**  qg1: 2m, qg2: 3m, minimum: 2m, when do queue runs for
7698 		**  qg2 occur?
7699 		*/
7700 
7701 		/* keep track of the lowest interval for a persistent runner */
7702 		if (Queue[h]->qg_queueintvl > 0 &&
7703 		    WorkGrp[j].wg_lowqintvl < Queue[h]->qg_queueintvl)
7704 			WorkGrp[j].wg_lowqintvl = Queue[h]->qg_queueintvl;
7705 		j += dir;
7706 	}
7707 	if (tTd(41, 9))
7708 	{
7709 		for (i = 0; i < NumWorkGroups; i++)
7710 		{
7711 			sm_dprintf("Workgroup[%d]=", i);
7712 			for (j = 0; j < WorkGrp[i].wg_numqgrp; j++)
7713 			{
7714 				sm_dprintf("%s, ",
7715 					WorkGrp[i].wg_qgs[j]->qg_name);
7716 			}
7717 			sm_dprintf("\n");
7718 		}
7719 	}
7720 }
7721 
7722 /*
7723 **  DUP_DF -- duplicate envelope data file
7724 **
7725 **	Copy the data file from the 'old' envelope to the 'new' envelope
7726 **	in the most efficient way possible.
7727 **
7728 **	Create a hard link from the 'old' data file to the 'new' data file.
7729 **	If the old and new queue directories are on different file systems,
7730 **	then the new data file link is created in the old queue directory,
7731 **	and the new queue file will contain a 'd' record pointing to the
7732 **	directory containing the new data file.
7733 **
7734 **	Parameters:
7735 **		old -- old envelope.
7736 **		new -- new envelope.
7737 **
7738 **	Results:
7739 **		Returns true on success, false on failure.
7740 **
7741 **	Side Effects:
7742 **		On success, the new data file is created.
7743 **		On fatal failure, EF_FATALERRS is set in old->e_flags.
7744 */
7745 
7746 static bool	dup_df __P((ENVELOPE *, ENVELOPE *));
7747 
7748 static bool
7749 dup_df(old, new)
7750 	ENVELOPE *old;
7751 	ENVELOPE *new;
7752 {
7753 	int ofs, nfs, r;
7754 	char opath[MAXPATHLEN];
7755 	char npath[MAXPATHLEN];
7756 
7757 	if (!bitset(EF_HAS_DF, old->e_flags))
7758 	{
7759 		/*
7760 		**  this can happen if: SuperSafe != True
7761 		**  and a bounce mail is sent that is split.
7762 		*/
7763 
7764 		queueup(old, false, true);
7765 	}
7766 	SM_REQUIRE(ISVALIDQGRP(old->e_qgrp) && ISVALIDQDIR(old->e_qdir));
7767 	SM_REQUIRE(ISVALIDQGRP(new->e_qgrp) && ISVALIDQDIR(new->e_qdir));
7768 
7769 	(void) sm_strlcpy(opath, queuename(old, DATAFL_LETTER), sizeof(opath));
7770 	(void) sm_strlcpy(npath, queuename(new, DATAFL_LETTER), sizeof(npath));
7771 
7772 	if (old->e_dfp != NULL)
7773 	{
7774 		r = sm_io_setinfo(old->e_dfp, SM_BF_COMMIT, NULL);
7775 		if (r < 0 && errno != EINVAL)
7776 		{
7777 			syserr("@can't commit %s", opath);
7778 			old->e_flags |= EF_FATALERRS;
7779 			return false;
7780 		}
7781 	}
7782 
7783 	/*
7784 	**  Attempt to create a hard link, if we think both old and new
7785 	**  are on the same file system, otherwise copy the file.
7786 	**
7787 	**  Don't waste time attempting a hard link unless old and new
7788 	**  are on the same file system.
7789 	*/
7790 
7791 	SM_REQUIRE(ISVALIDQGRP(old->e_dfqgrp) && ISVALIDQDIR(old->e_dfqdir));
7792 	SM_REQUIRE(ISVALIDQGRP(new->e_dfqgrp) && ISVALIDQDIR(new->e_dfqdir));
7793 
7794 	ofs = Queue[old->e_dfqgrp]->qg_qpaths[old->e_dfqdir].qp_fsysidx;
7795 	nfs = Queue[new->e_dfqgrp]->qg_qpaths[new->e_dfqdir].qp_fsysidx;
7796 	if (FILE_SYS_DEV(ofs) == FILE_SYS_DEV(nfs))
7797 	{
7798 		if (link(opath, npath) == 0)
7799 		{
7800 			new->e_flags |= EF_HAS_DF;
7801 			SYNC_DIR(npath, true);
7802 			return true;
7803 		}
7804 		goto error;
7805 	}
7806 
7807 	/*
7808 	**  Can't link across queue directories, so try to create a hard
7809 	**  link in the same queue directory as the old df file.
7810 	**  The qf file will refer to the new df file using a 'd' record.
7811 	*/
7812 
7813 	new->e_dfqgrp = old->e_dfqgrp;
7814 	new->e_dfqdir = old->e_dfqdir;
7815 	(void) sm_strlcpy(npath, queuename(new, DATAFL_LETTER), sizeof(npath));
7816 	if (link(opath, npath) == 0)
7817 	{
7818 		new->e_flags |= EF_HAS_DF;
7819 		SYNC_DIR(npath, true);
7820 		return true;
7821 	}
7822 
7823   error:
7824 	if (LogLevel > 0)
7825 		sm_syslog(LOG_ERR, old->e_id,
7826 			  "dup_df: can't link %s to %s, error=%s, envelope splitting failed",
7827 			  opath, npath, sm_errstring(errno));
7828 	return false;
7829 }
7830 
7831 /*
7832 **  SPLIT_ENV -- Allocate a new envelope based on a given envelope.
7833 **
7834 **	Parameters:
7835 **		e -- envelope.
7836 **		sendqueue -- sendqueue for new envelope.
7837 **		qgrp -- index of queue group.
7838 **		qdir -- queue directory.
7839 **
7840 **	Results:
7841 **		new envelope.
7842 **
7843 */
7844 
7845 static ENVELOPE	*split_env __P((ENVELOPE *, ADDRESS *, int, int));
7846 
7847 static ENVELOPE *
7848 split_env(e, sendqueue, qgrp, qdir)
7849 	ENVELOPE *e;
7850 	ADDRESS *sendqueue;
7851 	int qgrp;
7852 	int qdir;
7853 {
7854 	ENVELOPE *ee;
7855 
7856 	ee = (ENVELOPE *) sm_rpool_malloc_x(e->e_rpool, sizeof(*ee));
7857 	STRUCTCOPY(*e, *ee);
7858 	ee->e_message = NULL;	/* XXX use original message? */
7859 	ee->e_id = NULL;
7860 	assign_queueid(ee);
7861 	ee->e_sendqueue = sendqueue;
7862 	ee->e_flags &= ~(EF_INQUEUE|EF_CLRQUEUE|EF_FATALERRS
7863 			 |EF_SENDRECEIPT|EF_RET_PARAM|EF_HAS_DF);
7864 	ee->e_flags |= EF_NORECEIPT;	/* XXX really? */
7865 	ee->e_from.q_state = QS_SENDER;
7866 	ee->e_dfp = NULL;
7867 	ee->e_lockfp = NULL;
7868 	if (e->e_xfp != NULL)
7869 		ee->e_xfp = sm_io_dup(e->e_xfp);
7870 
7871 	/* failed to dup e->e_xfp, start a new transcript */
7872 	if (ee->e_xfp == NULL)
7873 		openxscript(ee);
7874 
7875 	ee->e_qgrp = ee->e_dfqgrp = qgrp;
7876 	ee->e_qdir = ee->e_dfqdir = qdir;
7877 	ee->e_errormode = EM_MAIL;
7878 	ee->e_statmsg = NULL;
7879 	if (e->e_quarmsg != NULL)
7880 		ee->e_quarmsg = sm_rpool_strdup_x(ee->e_rpool,
7881 						  e->e_quarmsg);
7882 
7883 	/*
7884 	**  XXX Not sure if this copying is necessary.
7885 	**  sendall() does this copying, but I (dm) don't know if that is
7886 	**  because of the storage management discipline we were using
7887 	**  before rpools were introduced, or if it is because these lists
7888 	**  can be modified later.
7889 	*/
7890 
7891 	ee->e_header = copyheader(e->e_header, ee->e_rpool);
7892 	ee->e_errorqueue = copyqueue(e->e_errorqueue, ee->e_rpool);
7893 
7894 	return ee;
7895 }
7896 
7897 /* return values from split functions, check also below! */
7898 #define SM_SPLIT_FAIL	(0)
7899 #define SM_SPLIT_NONE	(1)
7900 #define SM_SPLIT_NEW(n)	(1 + (n))
7901 
7902 /*
7903 **  SPLIT_ACROSS_QUEUE_GROUPS
7904 **
7905 **	This function splits an envelope across multiple queue groups
7906 **	based on the queue group of each recipient.
7907 **
7908 **	Parameters:
7909 **		e -- envelope.
7910 **
7911 **	Results:
7912 **		SM_SPLIT_FAIL on failure
7913 **		SM_SPLIT_NONE if no splitting occurred,
7914 **		or 1 + the number of additional envelopes created.
7915 **
7916 **	Side Effects:
7917 **		On success, e->e_sibling points to a list of zero or more
7918 **		additional envelopes, and the associated data files exist
7919 **		on disk.  But the queue files are not created.
7920 **
7921 **		On failure, e->e_sibling is not changed.
7922 **		The order of recipients in e->e_sendqueue is permuted.
7923 **		Abandoned data files for additional envelopes that failed
7924 **		to be created may exist on disk.
7925 */
7926 
7927 static int	q_qgrp_compare __P((const void *, const void *));
7928 static int	e_filesys_compare __P((const void *, const void *));
7929 
7930 static int
7931 q_qgrp_compare(p1, p2)
7932 	const void *p1;
7933 	const void *p2;
7934 {
7935 	ADDRESS **pq1 = (ADDRESS **) p1;
7936 	ADDRESS **pq2 = (ADDRESS **) p2;
7937 
7938 	return (*pq1)->q_qgrp - (*pq2)->q_qgrp;
7939 }
7940 
7941 static int
7942 e_filesys_compare(p1, p2)
7943 	const void *p1;
7944 	const void *p2;
7945 {
7946 	ENVELOPE **pe1 = (ENVELOPE **) p1;
7947 	ENVELOPE **pe2 = (ENVELOPE **) p2;
7948 	int fs1, fs2;
7949 
7950 	fs1 = Queue[(*pe1)->e_qgrp]->qg_qpaths[(*pe1)->e_qdir].qp_fsysidx;
7951 	fs2 = Queue[(*pe2)->e_qgrp]->qg_qpaths[(*pe2)->e_qdir].qp_fsysidx;
7952 	if (FILE_SYS_DEV(fs1) < FILE_SYS_DEV(fs2))
7953 		return -1;
7954 	if (FILE_SYS_DEV(fs1) > FILE_SYS_DEV(fs2))
7955 		return 1;
7956 	return 0;
7957 }
7958 
7959 static int split_across_queue_groups __P((ENVELOPE *));
7960 static int
7961 split_across_queue_groups(e)
7962 	ENVELOPE *e;
7963 {
7964 	int naddrs, nsplits, i;
7965 	bool changed;
7966 	char **pvp;
7967 	ADDRESS *q, **addrs;
7968 	ENVELOPE *ee, *es;
7969 	ENVELOPE *splits[MAXQUEUEGROUPS];
7970 	char pvpbuf[PSBUFSIZE];
7971 
7972 	SM_REQUIRE(ISVALIDQGRP(e->e_qgrp));
7973 
7974 	/* Count addresses and assign queue groups. */
7975 	naddrs = 0;
7976 	changed = false;
7977 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
7978 	{
7979 		if (QS_IS_DEAD(q->q_state))
7980 			continue;
7981 		++naddrs;
7982 
7983 		/* bad addresses and those already sent stay put */
7984 		if (QS_IS_BADADDR(q->q_state) ||
7985 		    QS_IS_SENT(q->q_state))
7986 			q->q_qgrp = e->e_qgrp;
7987 		else if (!ISVALIDQGRP(q->q_qgrp))
7988 		{
7989 			/* call ruleset which should return a queue group */
7990 			i = rscap(RS_QUEUEGROUP, q->q_user, NULL, e, &pvp,
7991 				  pvpbuf, sizeof(pvpbuf));
7992 			if (i == EX_OK &&
7993 			    pvp != NULL && pvp[0] != NULL &&
7994 			    (pvp[0][0] & 0377) == CANONNET &&
7995 			    pvp[1] != NULL && pvp[1][0] != '\0')
7996 			{
7997 				i = name2qid(pvp[1]);
7998 				if (ISVALIDQGRP(i))
7999 				{
8000 					q->q_qgrp = i;
8001 					changed = true;
8002 					if (tTd(20, 4))
8003 						sm_syslog(LOG_INFO, NOQID,
8004 							"queue group name %s -> %d",
8005 							pvp[1], i);
8006 					continue;
8007 				}
8008 				else if (LogLevel > 10)
8009 					sm_syslog(LOG_INFO, NOQID,
8010 						"can't find queue group name %s, selection ignored",
8011 						pvp[1]);
8012 			}
8013 			if (q->q_mailer != NULL &&
8014 			    ISVALIDQGRP(q->q_mailer->m_qgrp))
8015 			{
8016 				changed = true;
8017 				q->q_qgrp = q->q_mailer->m_qgrp;
8018 			}
8019 			else if (ISVALIDQGRP(e->e_qgrp))
8020 				q->q_qgrp = e->e_qgrp;
8021 			else
8022 				q->q_qgrp = 0;
8023 		}
8024 	}
8025 
8026 	/* only one address? nothing to split. */
8027 	if (naddrs <= 1 && !changed)
8028 		return SM_SPLIT_NONE;
8029 
8030 	/* sort the addresses by queue group */
8031 	addrs = sm_rpool_malloc_x(e->e_rpool, naddrs * sizeof(ADDRESS *));
8032 	for (i = 0, q = e->e_sendqueue; q != NULL; q = q->q_next)
8033 	{
8034 		if (QS_IS_DEAD(q->q_state))
8035 			continue;
8036 		addrs[i++] = q;
8037 	}
8038 	qsort(addrs, naddrs, sizeof(ADDRESS *), q_qgrp_compare);
8039 
8040 	/* split into multiple envelopes, by queue group */
8041 	nsplits = 0;
8042 	es = NULL;
8043 	e->e_sendqueue = NULL;
8044 	for (i = 0; i < naddrs; ++i)
8045 	{
8046 		if (i == naddrs - 1 || addrs[i]->q_qgrp != addrs[i + 1]->q_qgrp)
8047 			addrs[i]->q_next = NULL;
8048 		else
8049 			addrs[i]->q_next = addrs[i + 1];
8050 
8051 		/* same queue group as original envelope? */
8052 		if (addrs[i]->q_qgrp == e->e_qgrp)
8053 		{
8054 			if (e->e_sendqueue == NULL)
8055 				e->e_sendqueue = addrs[i];
8056 			continue;
8057 		}
8058 
8059 		/* different queue group than original envelope */
8060 		if (es == NULL || addrs[i]->q_qgrp != es->e_qgrp)
8061 		{
8062 			ee = split_env(e, addrs[i], addrs[i]->q_qgrp, NOQDIR);
8063 			es = ee;
8064 			splits[nsplits++] = ee;
8065 		}
8066 	}
8067 
8068 	/* no splits? return right now. */
8069 	if (nsplits <= 0)
8070 		return SM_SPLIT_NONE;
8071 
8072 	/* assign a queue directory to each additional envelope */
8073 	for (i = 0; i < nsplits; ++i)
8074 	{
8075 		es = splits[i];
8076 #if 0
8077 		es->e_qdir = pickqdir(Queue[es->e_qgrp], es->e_msgsize, es);
8078 #endif /* 0 */
8079 		if (!setnewqueue(es))
8080 			goto failure;
8081 	}
8082 
8083 	/* sort the additional envelopes by queue file system */
8084 	qsort(splits, nsplits, sizeof(ENVELOPE *), e_filesys_compare);
8085 
8086 	/* create data files for each additional envelope */
8087 	if (!dup_df(e, splits[0]))
8088 	{
8089 		i = 0;
8090 		goto failure;
8091 	}
8092 	for (i = 1; i < nsplits; ++i)
8093 	{
8094 		/* copy or link to the previous data file */
8095 		if (!dup_df(splits[i - 1], splits[i]))
8096 			goto failure;
8097 	}
8098 
8099 	/* success: prepend the new envelopes to the e->e_sibling list */
8100 	for (i = 0; i < nsplits; ++i)
8101 	{
8102 		es = splits[i];
8103 		es->e_sibling = e->e_sibling;
8104 		e->e_sibling = es;
8105 	}
8106 	return SM_SPLIT_NEW(nsplits);
8107 
8108 	/* failure: clean up */
8109   failure:
8110 	if (i > 0)
8111 	{
8112 		int j;
8113 
8114 		for (j = 0; j < i; j++)
8115 			(void) unlink(queuename(splits[j], DATAFL_LETTER));
8116 	}
8117 	e->e_sendqueue = addrs[0];
8118 	for (i = 0; i < naddrs - 1; ++i)
8119 		addrs[i]->q_next = addrs[i + 1];
8120 	addrs[naddrs - 1]->q_next = NULL;
8121 	return SM_SPLIT_FAIL;
8122 }
8123 
8124 /*
8125 **  SPLIT_WITHIN_QUEUE
8126 **
8127 **	Split an envelope with multiple recipients into several
8128 **	envelopes within the same queue directory, if the number of
8129 **	recipients exceeds the limit for the queue group.
8130 **
8131 **	Parameters:
8132 **		e -- envelope.
8133 **
8134 **	Results:
8135 **		SM_SPLIT_FAIL on failure
8136 **		SM_SPLIT_NONE if no splitting occurred,
8137 **		or 1 + the number of additional envelopes created.
8138 */
8139 
8140 #define SPLIT_LOG_LEVEL	8
8141 
8142 static int	split_within_queue __P((ENVELOPE *));
8143 
8144 static int
8145 split_within_queue(e)
8146 	ENVELOPE *e;
8147 {
8148 	int maxrcpt, nrcpt, ndead, nsplit, i;
8149 	int j, l;
8150 	char *lsplits;
8151 	ADDRESS *q, **addrs;
8152 	ENVELOPE *ee, *firstsibling;
8153 
8154 	if (!ISVALIDQGRP(e->e_qgrp) || bitset(EF_SPLIT, e->e_flags))
8155 		return SM_SPLIT_NONE;
8156 
8157 	/* don't bother if there is no recipient limit */
8158 	maxrcpt = Queue[e->e_qgrp]->qg_maxrcpt;
8159 	if (maxrcpt <= 0)
8160 		return SM_SPLIT_NONE;
8161 
8162 	/* count recipients */
8163 	nrcpt = 0;
8164 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
8165 	{
8166 		if (QS_IS_DEAD(q->q_state))
8167 			continue;
8168 		++nrcpt;
8169 	}
8170 	if (nrcpt <= maxrcpt)
8171 		return SM_SPLIT_NONE;
8172 
8173 	/*
8174 	**  Preserve the recipient list
8175 	**  so that we can restore it in case of error.
8176 	**  (But we discard dead addresses.)
8177 	*/
8178 
8179 	addrs = sm_rpool_malloc_x(e->e_rpool, nrcpt * sizeof(ADDRESS *));
8180 	for (i = 0, q = e->e_sendqueue; q != NULL; q = q->q_next)
8181 	{
8182 		if (QS_IS_DEAD(q->q_state))
8183 			continue;
8184 		addrs[i++] = q;
8185 	}
8186 
8187 	/*
8188 	**  Partition the recipient list so that bad and sent addresses
8189 	**  come first. These will go with the original envelope, and
8190 	**  do not count towards the maxrcpt limit.
8191 	**  addrs[] does not contain QS_IS_DEAD() addresses.
8192 	*/
8193 
8194 	ndead = 0;
8195 	for (i = 0; i < nrcpt; ++i)
8196 	{
8197 		if (QS_IS_BADADDR(addrs[i]->q_state) ||
8198 		    QS_IS_SENT(addrs[i]->q_state) ||
8199 		    QS_IS_DEAD(addrs[i]->q_state)) /* for paranoia's sake */
8200 		{
8201 			if (i > ndead)
8202 			{
8203 				ADDRESS *tmp = addrs[i];
8204 
8205 				addrs[i] = addrs[ndead];
8206 				addrs[ndead] = tmp;
8207 			}
8208 			++ndead;
8209 		}
8210 	}
8211 
8212 	/* Check if no splitting required. */
8213 	if (nrcpt - ndead <= maxrcpt)
8214 		return SM_SPLIT_NONE;
8215 
8216 	/* fix links */
8217 	for (i = 0; i < nrcpt - 1; ++i)
8218 		addrs[i]->q_next = addrs[i + 1];
8219 	addrs[nrcpt - 1]->q_next = NULL;
8220 	e->e_sendqueue = addrs[0];
8221 
8222 	/* prepare buffer for logging */
8223 	if (LogLevel > SPLIT_LOG_LEVEL)
8224 	{
8225 		l = MAXLINE;
8226 		lsplits = sm_malloc(l);
8227 		if (lsplits != NULL)
8228 			*lsplits = '\0';
8229 		j = 0;
8230 	}
8231 	else
8232 	{
8233 		/* get rid of stupid compiler warnings */
8234 		lsplits = NULL;
8235 		j = l = 0;
8236 	}
8237 
8238 	/* split the envelope */
8239 	firstsibling = e->e_sibling;
8240 	i = maxrcpt + ndead;
8241 	nsplit = 0;
8242 	for (;;)
8243 	{
8244 		addrs[i - 1]->q_next = NULL;
8245 		ee = split_env(e, addrs[i], e->e_qgrp, e->e_qdir);
8246 		if (!dup_df(e, ee))
8247 		{
8248 
8249 			ee = firstsibling;
8250 			while (ee != NULL)
8251 			{
8252 				(void) unlink(queuename(ee, DATAFL_LETTER));
8253 				ee = ee->e_sibling;
8254 			}
8255 
8256 			/* Error.  Restore e's sibling & recipient lists. */
8257 			e->e_sibling = firstsibling;
8258 			for (i = 0; i < nrcpt - 1; ++i)
8259 				addrs[i]->q_next = addrs[i + 1];
8260 			if (lsplits != NULL)
8261 				sm_free(lsplits);
8262 			return SM_SPLIT_FAIL;
8263 		}
8264 
8265 		/* prepend the new envelope to e->e_sibling */
8266 		ee->e_sibling = e->e_sibling;
8267 		e->e_sibling = ee;
8268 		++nsplit;
8269 		if (LogLevel > SPLIT_LOG_LEVEL && lsplits != NULL)
8270 		{
8271 			if (j >= l - strlen(ee->e_id) - 3)
8272 			{
8273 				char *p;
8274 
8275 				l += MAXLINE;
8276 				p = sm_realloc(lsplits, l);
8277 				if (p == NULL)
8278 				{
8279 					/* let's try to get this done */
8280 					sm_free(lsplits);
8281 					lsplits = NULL;
8282 				}
8283 				else
8284 					lsplits = p;
8285 			}
8286 			if (lsplits != NULL)
8287 			{
8288 				if (j == 0)
8289 					j += sm_strlcat(lsplits + j,
8290 							ee->e_id,
8291 							l - j);
8292 				else
8293 					j += sm_strlcat2(lsplits + j,
8294 							 "; ",
8295 							 ee->e_id,
8296 							 l - j);
8297 				SM_ASSERT(j < l);
8298 			}
8299 		}
8300 		if (nrcpt - i <= maxrcpt)
8301 			break;
8302 		i += maxrcpt;
8303 	}
8304 	if (LogLevel > SPLIT_LOG_LEVEL && lsplits != NULL)
8305 	{
8306 		if (nsplit > 0)
8307 		{
8308 			sm_syslog(LOG_NOTICE, e->e_id,
8309 				  "split: maxrcpts=%d, rcpts=%d, count=%d, id%s=%s",
8310 				  maxrcpt, nrcpt - ndead, nsplit,
8311 				  nsplit > 1 ? "s" : "", lsplits);
8312 		}
8313 		sm_free(lsplits);
8314 	}
8315 	return SM_SPLIT_NEW(nsplit);
8316 }
8317 /*
8318 **  SPLIT_BY_RECIPIENT
8319 **
8320 **	Split an envelope with multiple recipients into multiple
8321 **	envelopes as required by the sendmail configuration.
8322 **
8323 **	Parameters:
8324 **		e -- envelope.
8325 **
8326 **	Results:
8327 **		Returns true on success, false on failure.
8328 **
8329 **	Side Effects:
8330 **		see split_across_queue_groups(), split_within_queue(e)
8331 */
8332 
8333 bool
8334 split_by_recipient(e)
8335 	ENVELOPE *e;
8336 {
8337 	int split, n, i, j, l;
8338 	char *lsplits;
8339 	ENVELOPE *ee, *next, *firstsibling;
8340 
8341 	if (OpMode == SM_VERIFY || !ISVALIDQGRP(e->e_qgrp) ||
8342 	    bitset(EF_SPLIT, e->e_flags))
8343 		return true;
8344 	n = split_across_queue_groups(e);
8345 	if (n == SM_SPLIT_FAIL)
8346 		return false;
8347 	firstsibling = ee = e->e_sibling;
8348 	if (n > 1 && LogLevel > SPLIT_LOG_LEVEL)
8349 	{
8350 		l = MAXLINE;
8351 		lsplits = sm_malloc(l);
8352 		if (lsplits != NULL)
8353 			*lsplits = '\0';
8354 		j = 0;
8355 	}
8356 	else
8357 	{
8358 		/* get rid of stupid compiler warnings */
8359 		lsplits = NULL;
8360 		j = l = 0;
8361 	}
8362 	for (i = 1; i < n; ++i)
8363 	{
8364 		next = ee->e_sibling;
8365 		if (split_within_queue(ee) == SM_SPLIT_FAIL)
8366 		{
8367 			e->e_sibling = firstsibling;
8368 			return false;
8369 		}
8370 		ee->e_flags |= EF_SPLIT;
8371 		if (LogLevel > SPLIT_LOG_LEVEL && lsplits != NULL)
8372 		{
8373 			if (j >= l - strlen(ee->e_id) - 3)
8374 			{
8375 				char *p;
8376 
8377 				l += MAXLINE;
8378 				p = sm_realloc(lsplits, l);
8379 				if (p == NULL)
8380 				{
8381 					/* let's try to get this done */
8382 					sm_free(lsplits);
8383 					lsplits = NULL;
8384 				}
8385 				else
8386 					lsplits = p;
8387 			}
8388 			if (lsplits != NULL)
8389 			{
8390 				if (j == 0)
8391 					j += sm_strlcat(lsplits + j,
8392 							ee->e_id, l - j);
8393 				else
8394 					j += sm_strlcat2(lsplits + j, "; ",
8395 							 ee->e_id, l - j);
8396 				SM_ASSERT(j < l);
8397 			}
8398 		}
8399 		ee = next;
8400 	}
8401 	if (LogLevel > SPLIT_LOG_LEVEL && lsplits != NULL && n > 1)
8402 	{
8403 		sm_syslog(LOG_NOTICE, e->e_id, "split: count=%d, id%s=%s",
8404 			  n - 1, n > 2 ? "s" : "", lsplits);
8405 		sm_free(lsplits);
8406 	}
8407 	split = split_within_queue(e) != SM_SPLIT_FAIL;
8408 	if (split)
8409 		e->e_flags |= EF_SPLIT;
8410 	return split;
8411 }
8412 
8413 /*
8414 **  QUARANTINE_QUEUE_ITEM -- {un,}quarantine a single envelope
8415 **
8416 **	Add/remove quarantine reason and requeue appropriately.
8417 **
8418 **	Parameters:
8419 **		qgrp -- queue group for the item
8420 **		qdir -- queue directory in the given queue group
8421 **		e -- envelope information for the item
8422 **		reason -- quarantine reason, NULL means unquarantine.
8423 **
8424 **	Results:
8425 **		true if item changed, false otherwise
8426 **
8427 **	Side Effects:
8428 **		Changes quarantine tag in queue file and renames it.
8429 */
8430 
8431 static bool
8432 quarantine_queue_item(qgrp, qdir, e, reason)
8433 	int qgrp;
8434 	int qdir;
8435 	ENVELOPE *e;
8436 	char *reason;
8437 {
8438 	bool dirty = false;
8439 	bool failing = false;
8440 	bool foundq = false;
8441 	bool finished = false;
8442 	int fd;
8443 	int flags;
8444 	int oldtype;
8445 	int newtype;
8446 	int save_errno;
8447 	MODE_T oldumask = 0;
8448 	SM_FILE_T *oldqfp, *tempqfp;
8449 	char *bp;
8450 	int bufsize;
8451 	char oldqf[MAXPATHLEN];
8452 	char tempqf[MAXPATHLEN];
8453 	char newqf[MAXPATHLEN];
8454 	char buf[MAXLINE];
8455 
8456 	oldtype = queue_letter(e, ANYQFL_LETTER);
8457 	(void) sm_strlcpy(oldqf, queuename(e, ANYQFL_LETTER), sizeof(oldqf));
8458 	(void) sm_strlcpy(tempqf, queuename(e, NEWQFL_LETTER), sizeof(tempqf));
8459 
8460 	/*
8461 	**  Instead of duplicating all the open
8462 	**  and lock code here, tell readqf() to
8463 	**  do that work and return the open
8464 	**  file pointer in e_lockfp.  Note that
8465 	**  we must release the locks properly when
8466 	**  we are done.
8467 	*/
8468 
8469 	if (!readqf(e, true))
8470 	{
8471 		(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8472 				     "Skipping %s\n", qid_printname(e));
8473 		return false;
8474 	}
8475 	oldqfp = e->e_lockfp;
8476 
8477 	/* open the new queue file */
8478 	flags = O_CREAT|O_WRONLY|O_EXCL;
8479 	if (bitset(S_IWGRP, QueueFileMode))
8480 		oldumask = umask(002);
8481 	fd = open(tempqf, flags, QueueFileMode);
8482 	if (bitset(S_IWGRP, QueueFileMode))
8483 		(void) umask(oldumask);
8484 	RELEASE_QUEUE;
8485 
8486 	if (fd < 0)
8487 	{
8488 		save_errno = errno;
8489 		(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8490 				     "Skipping %s: Could not open %s: %s\n",
8491 				     qid_printname(e), tempqf,
8492 				     sm_errstring(save_errno));
8493 		(void) sm_io_close(oldqfp, SM_TIME_DEFAULT);
8494 		return false;
8495 	}
8496 	if (!lockfile(fd, tempqf, NULL, LOCK_EX|LOCK_NB))
8497 	{
8498 		(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8499 				     "Skipping %s: Could not lock %s\n",
8500 				     qid_printname(e), tempqf);
8501 		(void) close(fd);
8502 		(void) sm_io_close(oldqfp, SM_TIME_DEFAULT);
8503 		return false;
8504 	}
8505 
8506 	tempqfp = sm_io_open(SmFtStdiofd, SM_TIME_DEFAULT, (void *) &fd,
8507 			     SM_IO_WRONLY_B, NULL);
8508 	if (tempqfp == NULL)
8509 	{
8510 		(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8511 				     "Skipping %s: Could not lock %s\n",
8512 				     qid_printname(e), tempqf);
8513 		(void) close(fd);
8514 		(void) sm_io_close(oldqfp, SM_TIME_DEFAULT);
8515 		return false;
8516 	}
8517 
8518 	/* Copy the data over, changing the quarantine reason */
8519 	while (bufsize = sizeof(buf),
8520 	       (bp = fgetfolded(buf, &bufsize, oldqfp)) != NULL)
8521 	{
8522 		if (tTd(40, 4))
8523 			sm_dprintf("+++++ %s\n", bp);
8524 		switch (bp[0])
8525 		{
8526 		  case 'q':		/* quarantine reason */
8527 			foundq = true;
8528 			if (reason == NULL)
8529 			{
8530 				if (Verbose)
8531 				{
8532 					(void) sm_io_fprintf(smioout,
8533 							     SM_TIME_DEFAULT,
8534 							     "%s: Removed quarantine of \"%s\"\n",
8535 							     e->e_id, &bp[1]);
8536 				}
8537 				sm_syslog(LOG_INFO, e->e_id, "unquarantine");
8538 				dirty = true;
8539 			}
8540 			else if (strcmp(reason, &bp[1]) == 0)
8541 			{
8542 				if (Verbose)
8543 				{
8544 					(void) sm_io_fprintf(smioout,
8545 							     SM_TIME_DEFAULT,
8546 							     "%s: Already quarantined with \"%s\"\n",
8547 							     e->e_id, reason);
8548 				}
8549 				(void) sm_io_fprintf(tempqfp, SM_TIME_DEFAULT,
8550 						     "q%s\n", reason);
8551 			}
8552 			else
8553 			{
8554 				if (Verbose)
8555 				{
8556 					(void) sm_io_fprintf(smioout,
8557 							     SM_TIME_DEFAULT,
8558 							     "%s: Quarantine changed from \"%s\" to \"%s\"\n",
8559 							     e->e_id, &bp[1],
8560 							     reason);
8561 				}
8562 				(void) sm_io_fprintf(tempqfp, SM_TIME_DEFAULT,
8563 						     "q%s\n", reason);
8564 				sm_syslog(LOG_INFO, e->e_id, "quarantine=%s",
8565 					  reason);
8566 				dirty = true;
8567 			}
8568 			break;
8569 
8570 		  case 'S':
8571 			/*
8572 			**  If we are quarantining an unquarantined item,
8573 			**  need to put in a new 'q' line before it's
8574 			**  too late.
8575 			*/
8576 
8577 			if (!foundq && reason != NULL)
8578 			{
8579 				if (Verbose)
8580 				{
8581 					(void) sm_io_fprintf(smioout,
8582 							     SM_TIME_DEFAULT,
8583 							     "%s: Quarantined with \"%s\"\n",
8584 							     e->e_id, reason);
8585 				}
8586 				(void) sm_io_fprintf(tempqfp, SM_TIME_DEFAULT,
8587 						     "q%s\n", reason);
8588 				sm_syslog(LOG_INFO, e->e_id, "quarantine=%s",
8589 					  reason);
8590 				foundq = true;
8591 				dirty = true;
8592 			}
8593 
8594 			/* Copy the line to the new file */
8595 			(void) sm_io_fprintf(tempqfp, SM_TIME_DEFAULT,
8596 					     "%s\n", bp);
8597 			break;
8598 
8599 		  case '.':
8600 			finished = true;
8601 			/* FALLTHROUGH */
8602 
8603 		  default:
8604 			/* Copy the line to the new file */
8605 			(void) sm_io_fprintf(tempqfp, SM_TIME_DEFAULT,
8606 					     "%s\n", bp);
8607 			break;
8608 		}
8609 		if (bp != buf)
8610 			sm_free(bp);
8611 	}
8612 
8613 	/* Make sure we read the whole old file */
8614 	errno = sm_io_error(tempqfp);
8615 	if (errno != 0 && errno != SM_IO_EOF)
8616 	{
8617 		save_errno = errno;
8618 		(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8619 				     "Skipping %s: Error reading %s: %s\n",
8620 				     qid_printname(e), oldqf,
8621 				     sm_errstring(save_errno));
8622 		failing = true;
8623 	}
8624 
8625 	if (!failing && !finished)
8626 	{
8627 		(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8628 				     "Skipping %s: Incomplete file: %s\n",
8629 				     qid_printname(e), oldqf);
8630 		failing = true;
8631 	}
8632 
8633 	/* Check if we actually changed anything or we can just bail now */
8634 	if (!dirty)
8635 	{
8636 		/* pretend we failed, even though we technically didn't */
8637 		failing = true;
8638 	}
8639 
8640 	/* Make sure we wrote things out safely */
8641 	if (!failing &&
8642 	    (sm_io_flush(tempqfp, SM_TIME_DEFAULT) != 0 ||
8643 	     ((SuperSafe == SAFE_REALLY ||
8644 	       SuperSafe == SAFE_REALLY_POSTMILTER ||
8645 	       SuperSafe == SAFE_INTERACTIVE) &&
8646 	      fsync(sm_io_getinfo(tempqfp, SM_IO_WHAT_FD, NULL)) < 0) ||
8647 	     ((errno = sm_io_error(tempqfp)) != 0)))
8648 	{
8649 		save_errno = errno;
8650 		(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8651 				     "Skipping %s: Error writing %s: %s\n",
8652 				     qid_printname(e), tempqf,
8653 				     sm_errstring(save_errno));
8654 		failing = true;
8655 	}
8656 
8657 
8658 	/* Figure out the new filename */
8659 	newtype = (reason == NULL ? NORMQF_LETTER : QUARQF_LETTER);
8660 	if (oldtype == newtype)
8661 	{
8662 		/* going to rename tempqf to oldqf */
8663 		(void) sm_strlcpy(newqf, oldqf, sizeof(newqf));
8664 	}
8665 	else
8666 	{
8667 		/* going to rename tempqf to new name based on newtype */
8668 		(void) sm_strlcpy(newqf, queuename(e, newtype), sizeof(newqf));
8669 	}
8670 
8671 	save_errno = 0;
8672 
8673 	/* rename tempqf to newqf */
8674 	if (!failing &&
8675 	    rename(tempqf, newqf) < 0)
8676 		save_errno = (errno == 0) ? EINVAL : errno;
8677 
8678 	/* Check rename() success */
8679 	if (!failing && save_errno != 0)
8680 	{
8681 		sm_syslog(LOG_DEBUG, e->e_id,
8682 			  "quarantine_queue_item: rename(%s, %s): %s",
8683 			  tempqf, newqf, sm_errstring(save_errno));
8684 
8685 		(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8686 				     "Error renaming %s to %s: %s\n",
8687 				     tempqf, newqf,
8688 				     sm_errstring(save_errno));
8689 		if (oldtype == newtype)
8690 		{
8691 			/*
8692 			**  Bail here since we don't know the state of
8693 			**  the filesystem and may need to keep tempqf
8694 			**  for the user to rescue us.
8695 			*/
8696 
8697 			RELEASE_QUEUE;
8698 			errno = save_errno;
8699 			syserr("!452 Error renaming control file %s", tempqf);
8700 			/* NOTREACHED */
8701 		}
8702 		else
8703 		{
8704 			/* remove new file (if rename() half completed) */
8705 			if (xunlink(newqf) < 0)
8706 			{
8707 				save_errno = errno;
8708 				(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8709 						     "Error removing %s: %s\n",
8710 						     newqf,
8711 						     sm_errstring(save_errno));
8712 			}
8713 
8714 			/* tempqf removed below */
8715 			failing = true;
8716 		}
8717 
8718 	}
8719 
8720 	/* If changing file types, need to remove old type */
8721 	if (!failing && oldtype != newtype)
8722 	{
8723 		if (xunlink(oldqf) < 0)
8724 		{
8725 			save_errno = errno;
8726 			(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8727 					     "Error removing %s: %s\n",
8728 					     oldqf, sm_errstring(save_errno));
8729 		}
8730 	}
8731 
8732 	/* see if anything above failed */
8733 	if (failing)
8734 	{
8735 		/* Something failed: remove new file, old file still there */
8736 		(void) xunlink(tempqf);
8737 	}
8738 
8739 	/*
8740 	**  fsync() after file operations to make sure metadata is
8741 	**  written to disk on filesystems in which renames are
8742 	**  not guaranteed.  It's ok if they fail, mail won't be lost.
8743 	*/
8744 
8745 	if (SuperSafe != SAFE_NO)
8746 	{
8747 		/* for soft-updates */
8748 		(void) fsync(sm_io_getinfo(tempqfp,
8749 					   SM_IO_WHAT_FD, NULL));
8750 
8751 		if (!failing)
8752 		{
8753 			/* for soft-updates */
8754 			(void) fsync(sm_io_getinfo(oldqfp,
8755 						   SM_IO_WHAT_FD, NULL));
8756 		}
8757 
8758 		/* for other odd filesystems */
8759 		SYNC_DIR(tempqf, false);
8760 	}
8761 
8762 	/* Close up shop */
8763 	RELEASE_QUEUE;
8764 	if (tempqfp != NULL)
8765 		(void) sm_io_close(tempqfp, SM_TIME_DEFAULT);
8766 	if (oldqfp != NULL)
8767 		(void) sm_io_close(oldqfp, SM_TIME_DEFAULT);
8768 
8769 	/* All went well */
8770 	return !failing;
8771 }
8772 
8773 /*
8774 **  QUARANTINE_QUEUE -- {un,}quarantine matching items in the queue
8775 **
8776 **	Read all matching queue items, add/remove quarantine
8777 **	reason, and requeue appropriately.
8778 **
8779 **	Parameters:
8780 **		reason -- quarantine reason, "." means unquarantine.
8781 **		qgrplimit -- limit to single queue group unless NOQGRP
8782 **
8783 **	Results:
8784 **		none.
8785 **
8786 **	Side Effects:
8787 **		Lots of changes to the queue.
8788 */
8789 
8790 void
8791 quarantine_queue(reason, qgrplimit)
8792 	char *reason;
8793 	int qgrplimit;
8794 {
8795 	int changed = 0;
8796 	int qgrp;
8797 
8798 	/* Convert internal representation of unquarantine */
8799 	if (reason != NULL && reason[0] == '.' && reason[1] == '\0')
8800 		reason = NULL;
8801 
8802 	if (reason != NULL)
8803 	{
8804 		/* clean it */
8805 		reason = newstr(denlstring(reason, true, true));
8806 	}
8807 
8808 	for (qgrp = 0; qgrp < NumQueue && Queue[qgrp] != NULL; qgrp++)
8809 	{
8810 		int qdir;
8811 
8812 		if (qgrplimit != NOQGRP && qgrplimit != qgrp)
8813 			continue;
8814 
8815 		for (qdir = 0; qdir < Queue[qgrp]->qg_numqueues; qdir++)
8816 		{
8817 			int i;
8818 			int nrequests;
8819 
8820 			if (StopRequest)
8821 				stop_sendmail();
8822 
8823 			nrequests = gatherq(qgrp, qdir, true, NULL, NULL);
8824 
8825 			/* first see if there is anything */
8826 			if (nrequests <= 0)
8827 			{
8828 				if (Verbose)
8829 				{
8830 					(void) sm_io_fprintf(smioout,
8831 							     SM_TIME_DEFAULT, "%s: no matches\n",
8832 							     qid_printqueue(qgrp, qdir));
8833 				}
8834 				continue;
8835 			}
8836 
8837 			if (Verbose)
8838 			{
8839 				(void) sm_io_fprintf(smioout,
8840 						     SM_TIME_DEFAULT, "Processing %s:\n",
8841 						     qid_printqueue(qgrp, qdir));
8842 			}
8843 
8844 			for (i = 0; i < WorkListCount; i++)
8845 			{
8846 				ENVELOPE e;
8847 
8848 				if (StopRequest)
8849 					stop_sendmail();
8850 
8851 				/* setup envelope */
8852 				clearenvelope(&e, true, sm_rpool_new_x(NULL));
8853 				e.e_id = WorkList[i].w_name + 2;
8854 				e.e_qgrp = qgrp;
8855 				e.e_qdir = qdir;
8856 
8857 				if (tTd(70, 101))
8858 				{
8859 					sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8860 						      "Would do %s\n", e.e_id);
8861 					changed++;
8862 				}
8863 				else if (quarantine_queue_item(qgrp, qdir,
8864 							       &e, reason))
8865 					changed++;
8866 
8867 				/* clean up */
8868 				sm_rpool_free(e.e_rpool);
8869 				e.e_rpool = NULL;
8870 			}
8871 			if (WorkList != NULL)
8872 				sm_free(WorkList); /* XXX */
8873 			WorkList = NULL;
8874 			WorkListSize = 0;
8875 			WorkListCount = 0;
8876 		}
8877 	}
8878 	if (Verbose)
8879 	{
8880 		if (changed == 0)
8881 			(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8882 					     "No changes\n");
8883 		else
8884 			(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8885 					     "%d change%s\n",
8886 					     changed,
8887 					     changed == 1 ? "" : "s");
8888 	}
8889 }
8890