2 * Copyright (c) 1998-2007 Sendmail, Inc. and its suppliers.
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.
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.
17 SM_RCSID("@(#)$Id: queue.c,v 8.972 2007/03/29 22:55:17 ca Exp $")
21 # define RELEASE_QUEUE (void) 0
22 # define ST_INODE(st) (st).st_ino
24 # define sm_file_exists(errno) ((errno) == EEXIST)
26 # if HASFLOCK && defined(O_EXLOCK)
27 # define SM_OPEN_EXLOCK 1
28 # define TF_OPEN_FLAGS (O_CREAT|O_WRONLY|O_EXCL|O_EXLOCK)
29 # else /* HASFLOCK && defined(O_EXLOCK) */
30 # define TF_OPEN_FLAGS (O_CREAT|O_WRONLY|O_EXCL)
31 # endif /* HASFLOCK && defined(O_EXLOCK) */
33 #ifndef SM_OPEN_EXLOCK
34 # define SM_OPEN_EXLOCK 0
35 #endif /* ! SM_OPEN_EXLOCK */
39 ** QF_VERSION == 4 was sendmail 8.10/8.11 without _FFR_QUEUEDELAY
40 ** QF_VERSION == 5 was sendmail 8.10/8.11 with _FFR_QUEUEDELAY
41 ** QF_VERSION == 6 was sendmail 8.12 without _FFR_QUEUEDELAY
42 ** QF_VERSION == 7 was sendmail 8.12 with _FFR_QUEUEDELAY
43 ** QF_VERSION == 8 is sendmail 8.13
46 #define QF_VERSION 8 /* version number of this queue format */
48 static char queue_letter __P((ENVELOPE *, int));
49 static bool quarantine_queue_item __P((int, int, ENVELOPE *, char *));
51 /* Naming convention: qgrp: index of queue group, qg: QUEUEGROUP */
59 char *w_name; /* name of control file */
60 char *w_host; /* name of recipient host */
61 bool w_lock; /* is message locked? */
62 bool w_tooyoung; /* is it too young to run? */
63 long w_pri; /* priority of message, see below */
64 time_t w_ctime; /* creation time */
65 time_t w_mtime; /* modification time */
66 int w_qgrp; /* queue group located in */
67 int w_qdir; /* queue directory located in */
68 struct work *w_next; /* next in queue */
71 typedef struct work WORK;
73 static WORK *WorkQ; /* queue of things to be done */
74 static int NumWorkGroups; /* number of work groups */
75 static time_t Current_LA_time = 0;
77 /* Get new load average every 30 seconds. */
78 #define GET_NEW_LA_TIME 30
80 #define SM_GET_LA(now) \
84 if (Current_LA_time < now - GET_NEW_LA_TIME) \
87 Current_LA_time = now; \
92 ** DoQueueRun indicates that a queue run is needed.
93 ** Notice: DoQueueRun is modified in a signal handler!
96 static bool volatile DoQueueRun; /* non-interrupt time queue run needed */
99 ** Work group definition structure.
100 ** Each work group contains one or more queue groups. This is done
101 ** to manage the number of queue group runners active at the same time
102 ** to be within the constraints of MaxQueueChildren (if it is set).
103 ** The number of queue groups that can be run on the next work run
104 ** is kept track of. The queue groups are run in a round robin.
109 int wg_numqgrp; /* number of queue groups in work grp */
110 int wg_runners; /* total runners */
111 int wg_curqgrp; /* current queue group */
112 QUEUEGRP **wg_qgs; /* array of queue groups */
113 int wg_maxact; /* max # of active runners */
114 time_t wg_lowqintvl; /* lowest queue interval */
115 int wg_restart; /* needs restarting? */
116 int wg_restartcnt; /* count of times restarted */
119 typedef struct workgrp WORKGRP;
121 static WORKGRP volatile WorkGrp[MAXWORKGROUPS + 1]; /* work groups */
124 static SM_DEBUG_T DebugLeakQ = SM_DEBUG_INITIALIZER("leak_q",
125 "@(#)$Debug: leak_q - trace memory leaks during queue processing $");
126 #endif /* SM_HEAP_CHECK */
129 ** We use EmptyString instead of "" to avoid
130 ** 'zero-length format string' warnings from gcc
133 static const char EmptyString[] = "";
135 static void grow_wlist __P((int, int));
136 static int multiqueue_cache __P((char *, int, QUEUEGRP *, int, unsigned int *));
137 static int gatherq __P((int, int, bool, bool *, bool *));
138 static int sortq __P((int));
139 static void printctladdr __P((ADDRESS *, SM_FILE_T *));
140 static bool readqf __P((ENVELOPE *, bool));
141 static void restart_work_group __P((int));
142 static void runner_work __P((ENVELOPE *, int, bool, int, int));
143 static void schedule_queue_runs __P((bool, int, bool));
144 static char *strrev __P((char *));
145 static ADDRESS *setctluser __P((char *, int, ENVELOPE *));
147 static int sm_strshufflecmp __P((char *, char *));
148 static void init_shuffle_alphabet __P(());
149 #endif /* _FFR_RHS */
152 ** Note: workcmpf?() don't use a prototype because it will cause a conflict
153 ** with the qsort() call (which expects something like
154 ** int (*compar)(const void *, const void *), not (WORK *, WORK *))
157 static int workcmpf0();
158 static int workcmpf1();
159 static int workcmpf2();
160 static int workcmpf3();
161 static int workcmpf4();
162 static int randi = 3; /* index for workcmpf5() */
163 static int workcmpf5();
164 static int workcmpf6();
166 static int workcmpf7();
167 #endif /* _FFR_RHS */
170 # define get_rand_mod(m) ((get_random() >> RANDOMSHIFT) % (m))
171 #else /* RANDOMSHIFT */
172 # define get_rand_mod(m) (get_random() % (m))
173 #endif /* RANDOMSHIFT */
176 ** File system definition.
177 ** Used to keep track of how much free space is available
178 ** on a file system in which one or more queue directories reside.
181 typedef struct filesys_shared FILESYS;
183 struct filesys_shared
185 dev_t fs_dev; /* unique device id */
186 long fs_avail; /* number of free blocks available */
187 long fs_blksize; /* block size, in bytes */
190 /* probably kept in shared memory */
191 static FILESYS FileSys[MAXFILESYS]; /* queue file systems */
192 static const char *FSPath[MAXFILESYS]; /* pathnames for file systems */
197 ** Shared memory data
200 ** size -- size of shared memory segment
201 ** pid -- pid of owner, should be a unique id to avoid misinterpretations
202 ** by other processes.
203 ** tag -- should be a unique id to avoid misinterpretations by others.
204 ** idea: hash over configuration data that will be stored here.
205 ** NumFileSys -- number of file systems.
206 ** FileSys -- (arrary of) structure for used file systems.
207 ** RSATmpCnt -- counter for number of uses of ephemeral RSA key.
208 ** QShm -- (array of) structure for information about queue directories.
212 ** Queue data in shared memory
215 typedef struct queue_shared QUEUE_SHM_T;
219 int qs_entries; /* number of entries */
220 /* XXX more to follow? */
223 static void *Pshm; /* pointer to shared memory */
224 static FILESYS *PtrFileSys; /* pointer to queue file system array */
225 int ShmId = SM_SHM_NO_ID; /* shared memory id */
226 static QUEUE_SHM_T *QShm; /* pointer to shared queue data */
229 # define SHM_OFF_PID(p) (((char *) (p)) + sizeof(int))
230 # define SHM_OFF_TAG(p) (((char *) (p)) + sizeof(pid_t) + sizeof(int))
231 # define SHM_OFF_HEAD (sizeof(pid_t) + sizeof(int) * 2)
233 /* how to access FileSys */
234 # define FILE_SYS(i) (PtrFileSys[i])
236 /* first entry is a tag, for now just the size */
237 # define OFF_FILE_SYS(p) (((char *) (p)) + SHM_OFF_HEAD)
239 /* offset for PNumFileSys */
240 # define OFF_NUM_FILE_SYS(p) (((char *) (p)) + SHM_OFF_HEAD + sizeof(FileSys))
242 /* offset for PRSATmpCnt */
243 # define OFF_RSA_TMP_CNT(p) (((char *) (p)) + SHM_OFF_HEAD + sizeof(FileSys) + sizeof(int))
246 /* offset for queue_shm */
247 # define OFF_QUEUE_SHM(p) (((char *) (p)) + SHM_OFF_HEAD + sizeof(FileSys) + sizeof(int) * 2)
249 # define QSHM_ENTRIES(i) QShm[i].qs_entries
251 /* basic size of shared memory segment */
252 # define SM_T_SIZE (SHM_OFF_HEAD + sizeof(FileSys) + sizeof(int) * 2)
254 static unsigned int hash_q __P((char *, unsigned int));
257 ** HASH_Q -- simple hash function
260 ** p -- string to hash.
261 ** h -- hash start value (from previous run).
279 h += (c<<11) ^ (c>>1);
280 h ^= (d<<14) + (d<<7) + (d<<4) + d;
286 #else /* SM_CONF_SHM */
287 # define FILE_SYS(i) FileSys[i]
288 #endif /* SM_CONF_SHM */
290 /* access to the various components of file system data */
291 #define FILE_SYS_NAME(i) FSPath[i]
292 #define FILE_SYS_AVAIL(i) FILE_SYS(i).fs_avail
293 #define FILE_SYS_BLKSIZE(i) FILE_SYS(i).fs_blksize
294 #define FILE_SYS_DEV(i) FILE_SYS(i).fs_dev
298 ** Current qf file field assignments:
302 ** C controlling user
304 ** d data file directory name (added in 8.12)
307 ** G free (was: queue delay algorithm if _FFR_QUEUEDELAY)
309 ** I data file's inode number
310 ** K time of last delivery attempt
311 ** L Solaris Content-Length: header (obsolete)
313 ** N number of delivery attempts
314 ** P message priority
315 ** q quarantine reason
316 ** Q original recipient (ORCPT=)
317 ** r final recipient (Final-Recipient: DSN field)
321 ** V queue file version
322 ** X free (was: character set if _FFR_SAVE_CHARSET)
323 ** Y free (was: current delay if _FFR_QUEUEDELAY)
324 ** Z original envelope id from ESMTP
325 ** ! deliver by (added in 8.12)
331 ** QUEUEUP -- queue a message up for future transmission.
334 ** e -- the envelope to queue up.
335 ** announce -- if true, tell when you are queueing up.
336 ** msync -- if true, then fsync() if SuperSafe interactive mode.
342 ** The current request is saved in a control file.
343 ** The queue file is left locked.
347 queueup(e, announce, msync)
348 register ENVELOPE *e;
352 register SM_FILE_T *tfp;
367 ** Create control file.
372 MODE_T oldumask = 0; \
374 if (bitset(S_IWGRP, QueueFileMode)) \
375 oldumask = umask(002); \
376 tfd = open(tf, TF_OPEN_FLAGS, QueueFileMode); \
377 if (bitset(S_IWGRP, QueueFileMode)) \
378 (void) umask(oldumask); \
382 newid = (e->e_id == NULL) || !bitset(EF_INQUEUE, e->e_flags);
383 (void) sm_strlcpy(tf, queuename(e, NEWQFL_LETTER), sizeof(tf));
385 if (tfp == NULL && newid)
388 ** open qf file directly: this will give an error if the file
389 ** already exists and hence prevent problems if a queue-id
390 ** is reused (e.g., because the clock is set back).
393 (void) sm_strlcpy(tf, queuename(e, ANYQFL_LETTER), sizeof(tf));
397 !lockfile(tfd, tf, NULL, LOCK_EX|LOCK_NB) ||
398 #endif /* !SM_OPEN_EXLOCK */
399 (tfp = sm_io_open(SmFtStdiofd, SM_TIME_DEFAULT,
400 (void *) &tfd, SM_IO_WRONLY,
403 int save_errno = errno;
407 syserr("!queueup: cannot create queue file %s, euid=%d, fd=%d, fp=%p",
408 tf, (int) geteuid(), tfd, tfp);
412 upd_qs(e, 1, 0, "queueup");
415 /* if newid, write the queue file directly (instead of temp file) */
418 /* get a locked tf file */
419 for (i = 0; i < 128; i++)
428 if (LogLevel > 0 && (i % 32) == 0)
429 sm_syslog(LOG_ALERT, e->e_id,
430 "queueup: cannot create %s, uid=%d: %s",
432 sm_errstring(errno));
437 #endif /* SM_OPEN_EXLOCK */
442 /* file is locked by open() */
444 #else /* SM_OPEN_EXLOCK */
445 if (lockfile(tfd, tf, NULL, LOCK_EX|LOCK_NB))
448 #endif /* SM_OPEN_EXLOCK */
449 if (LogLevel > 0 && (i % 32) == 0)
450 sm_syslog(LOG_ALERT, e->e_id,
451 "queueup: cannot lock %s: %s",
452 tf, sm_errstring(errno));
462 /* save the old temp file away */
463 (void) rename(tf, queuename(e, TEMPQF_LETTER));
466 (void) sleep(i % 32);
468 if (tfd < 0 || (tfp = sm_io_open(SmFtStdiofd, SM_TIME_DEFAULT,
469 (void *) &tfd, SM_IO_WRONLY_B,
472 int save_errno = errno;
476 syserr("!queueup: cannot create queue temp file %s, uid=%d",
477 tf, (int) geteuid());
482 sm_dprintf("\n>>>>> queueing %s/%s%s >>>>>\n",
483 qid_printqueue(e->e_qgrp, e->e_qdir),
484 queuename(e, ANYQFL_LETTER),
485 newid ? " (new id)" : "");
488 sm_dprintf(" e_flags=");
493 sm_dprintf(" sendq=");
494 printaddr(sm_debug_file(), e->e_sendqueue, true);
499 dumpfd(sm_io_getinfo(tfp, SM_IO_WHAT_FD, NULL), true, false);
500 sm_dprintf(" lockfp=");
501 if (e->e_lockfp == NULL)
502 sm_dprintf("NULL\n");
504 dumpfd(sm_io_getinfo(e->e_lockfp, SM_IO_WHAT_FD, NULL),
509 ** If there is no data file yet, create one.
512 (void) sm_strlcpy(df, queuename(e, DATAFL_LETTER), sizeof(df));
513 if (bitset(EF_HAS_DF, e->e_flags))
515 if (e->e_dfp != NULL &&
516 SuperSafe != SAFE_REALLY &&
517 SuperSafe != SAFE_REALLY_POSTMILTER &&
518 sm_io_setinfo(e->e_dfp, SM_BF_COMMIT, NULL) < 0 &&
521 syserr("!queueup: cannot commit data file %s, uid=%d",
522 queuename(e, DATAFL_LETTER), (int) geteuid());
524 if (e->e_dfp != NULL &&
525 SuperSafe == SAFE_INTERACTIVE && msync)
528 sm_syslog(LOG_INFO, e->e_id,
529 "queueup: fsync(e->e_dfp)");
531 if (fsync(sm_io_getinfo(e->e_dfp, SM_IO_WHAT_FD,
535 syserr("!552 Error writing data file %s",
538 syserr("!452 Error writing data file %s",
547 register SM_FILE_T *dfp = NULL;
550 if (e->e_dfp != NULL &&
551 sm_io_getinfo(e->e_dfp, SM_IO_WHAT_ISTYPE, BF_FILE_TYPE))
552 syserr("committing over bf file");
554 if (bitset(S_IWGRP, QueueFileMode))
555 oldumask = umask(002);
556 dfd = open(df, O_WRONLY|O_CREAT|O_TRUNC|QF_O_EXTRA,
558 if (bitset(S_IWGRP, QueueFileMode))
559 (void) umask(oldumask);
560 if (dfd < 0 || (dfp = sm_io_open(SmFtStdiofd, SM_TIME_DEFAULT,
561 (void *) &dfd, SM_IO_WRONLY_B,
563 syserr("!queueup: cannot create data temp file %s, uid=%d",
564 df, (int) geteuid());
565 if (fstat(dfd, &stbuf) < 0)
569 e->e_dfdev = stbuf.st_dev;
570 e->e_dfino = ST_INODE(stbuf);
572 e->e_flags |= EF_HAS_DF;
573 memset(&mcibuf, '\0', sizeof(mcibuf));
574 mcibuf.mci_out = dfp;
575 mcibuf.mci_mailer = FileMailer;
576 (*e->e_putbody)(&mcibuf, e, NULL);
578 if (SuperSafe == SAFE_REALLY ||
579 SuperSafe == SAFE_REALLY_POSTMILTER ||
580 (SuperSafe == SAFE_INTERACTIVE && msync))
583 sm_syslog(LOG_INFO, e->e_id,
584 "queueup: fsync(dfp)");
586 if (fsync(sm_io_getinfo(dfp, SM_IO_WHAT_FD, NULL)) < 0)
589 syserr("!552 Error writing data file %s",
592 syserr("!452 Error writing data file %s",
597 if (sm_io_close(dfp, SM_TIME_DEFAULT) < 0)
598 syserr("!queueup: cannot save data temp file %s, uid=%d",
599 df, (int) geteuid());
600 e->e_putbody = putbody;
604 ** Output future work requests.
605 ** Priority and creation time should be first, since
606 ** they are required by gatherq.
609 /* output queue version number (must be first!) */
610 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "V%d\n", QF_VERSION);
612 /* output creation time */
613 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "T%ld\n", (long) e->e_ctime);
615 /* output last delivery time */
616 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "K%ld\n", (long) e->e_dtime);
618 /* output number of delivery attempts */
619 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "N%d\n", e->e_ntries);
621 /* output message priority */
622 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "P%ld\n", e->e_msgpriority);
625 ** If data file is in a different directory than the queue file,
626 ** output a "d" record naming the directory of the data file.
629 if (e->e_dfqgrp != e->e_qgrp)
631 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "d%s\n",
632 Queue[e->e_dfqgrp]->qg_qpaths[e->e_dfqdir].qp_name);
635 /* output inode number of data file */
636 /* XXX should probably include device major/minor too */
637 if (e->e_dfino != -1)
639 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "I%ld/%ld/%llu\n",
640 (long) major(e->e_dfdev),
641 (long) minor(e->e_dfdev),
642 (ULONGLONG_T) e->e_dfino);
645 /* output body type */
646 if (e->e_bodytype != NULL)
647 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "B%s\n",
648 denlstring(e->e_bodytype, true, false));
650 /* quarantine reason */
651 if (e->e_quarmsg != NULL)
652 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "q%s\n",
653 denlstring(e->e_quarmsg, true, false));
655 /* message from envelope, if it exists */
656 if (e->e_message != NULL)
657 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "M%s\n",
658 denlstring(e->e_message, true, false));
660 /* send various flag bits through */
662 if (bitset(EF_WARNING, e->e_flags))
664 if (bitset(EF_RESPONSE, e->e_flags))
666 if (bitset(EF_HAS8BIT, e->e_flags))
668 if (bitset(EF_DELETE_BCC, e->e_flags))
670 if (bitset(EF_RET_PARAM, e->e_flags))
672 if (bitset(EF_NO_BODY_RETN, e->e_flags))
674 if (bitset(EF_SPLIT, e->e_flags))
678 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "F%s\n", buf);
680 /* save $={persistentMacros} macro values */
681 queueup_macros(macid("{persistentMacros}"), tfp, e);
683 /* output name of sender */
684 if (bitnset(M_UDBENVELOPE, e->e_from.q_mailer->m_flags))
687 p = e->e_from.q_paddr;
688 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "S%s\n",
689 denlstring(p, true, false));
691 /* output ESMTP-supplied "original" information */
692 if (e->e_envid != NULL)
693 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "Z%s\n",
694 denlstring(e->e_envid, true, false));
696 /* output AUTH= parameter */
697 if (e->e_auth_param != NULL)
698 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "A%s\n",
699 denlstring(e->e_auth_param, true, false));
700 if (e->e_dlvr_flag != 0)
701 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "!%c %ld\n",
702 (char) e->e_dlvr_flag, e->e_deliver_by);
704 /* output list of recipient addresses */
705 printctladdr(NULL, NULL);
706 for (q = e->e_sendqueue; q != NULL; q = q->q_next)
708 if (!QS_IS_UNDELIVERED(q->q_state))
711 /* message for this recipient, if it exists */
712 if (q->q_message != NULL)
713 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "M%s\n",
714 denlstring(q->q_message, true,
717 printctladdr(q, tfp);
718 if (q->q_orcpt != NULL)
719 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "Q%s\n",
720 denlstring(q->q_orcpt, true,
722 if (q->q_finalrcpt != NULL)
723 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "r%s\n",
724 denlstring(q->q_finalrcpt, true,
726 (void) sm_io_putc(tfp, SM_TIME_DEFAULT, 'R');
727 if (bitset(QPRIMARY, q->q_flags))
728 (void) sm_io_putc(tfp, SM_TIME_DEFAULT, 'P');
729 if (bitset(QHASNOTIFY, q->q_flags))
730 (void) sm_io_putc(tfp, SM_TIME_DEFAULT, 'N');
731 if (bitset(QPINGONSUCCESS, q->q_flags))
732 (void) sm_io_putc(tfp, SM_TIME_DEFAULT, 'S');
733 if (bitset(QPINGONFAILURE, q->q_flags))
734 (void) sm_io_putc(tfp, SM_TIME_DEFAULT, 'F');
735 if (bitset(QPINGONDELAY, q->q_flags))
736 (void) sm_io_putc(tfp, SM_TIME_DEFAULT, 'D');
737 if (q->q_alias != NULL &&
738 bitset(QALIAS, q->q_alias->q_flags))
739 (void) sm_io_putc(tfp, SM_TIME_DEFAULT, 'A');
740 (void) sm_io_putc(tfp, SM_TIME_DEFAULT, ':');
741 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "%s\n",
742 denlstring(q->q_paddr, true, false));
745 char *tag = "queued";
747 if (e->e_quarmsg != NULL)
750 e->e_to = q->q_paddr;
753 logdelivery(q->q_mailer, NULL, q->q_status,
754 tag, NULL, (time_t) 0, e);
759 sm_dprintf("queueing ");
760 printaddr(sm_debug_file(), q, false);
765 ** Output headers for this message.
766 ** Expand macros completely here. Queue run will deal with
767 ** everything as absolute headers.
768 ** All headers that must be relative to the recipient
769 ** can be cracked later.
770 ** We set up a "null mailer" -- i.e., a mailer that will have
771 ** no effect on the addresses as they are output.
774 memset((char *) &nullmailer, '\0', sizeof(nullmailer));
775 nullmailer.m_re_rwset = nullmailer.m_rh_rwset =
776 nullmailer.m_se_rwset = nullmailer.m_sh_rwset = -1;
777 nullmailer.m_eol = "\n";
778 memset(&mcibuf, '\0', sizeof(mcibuf));
779 mcibuf.mci_mailer = &nullmailer;
780 mcibuf.mci_out = tfp;
782 macdefine(&e->e_macro, A_PERM, 'g', "\201f");
783 for (h = e->e_header; h != NULL; h = h->h_link)
785 if (h->h_value == NULL)
788 /* don't output resent headers on non-resent messages */
789 if (bitset(H_RESENT, h->h_flags) &&
790 !bitset(EF_RESENT, e->e_flags))
793 /* expand macros; if null, don't output header at all */
794 if (bitset(H_DEFAULT, h->h_flags))
796 (void) expand(h->h_value, buf, sizeof(buf), e);
799 if (buf[0] == ' ' && buf[1] == '\0')
803 /* output this header */
804 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "H?");
806 /* output conditional macro if present */
807 if (h->h_macro != '\0')
809 if (bitset(0200, h->h_macro))
810 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT,
812 macname(bitidx(h->h_macro)));
814 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT,
817 else if (!bitzerop(h->h_mflags) &&
818 bitset(H_CHECK|H_ACHECK, h->h_flags))
822 /* if conditional, output the set of conditions */
823 for (j = '\0'; j <= '\177'; j++)
824 if (bitnset(j, h->h_mflags))
825 (void) sm_io_putc(tfp, SM_TIME_DEFAULT,
828 (void) sm_io_putc(tfp, SM_TIME_DEFAULT, '?');
830 /* output the header: expand macros, convert addresses */
831 if (bitset(H_DEFAULT, h->h_flags) &&
832 !bitset(H_BINDLATE, h->h_flags))
834 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "%s:%s\n",
836 denlstring(buf, false, true));
838 else if (bitset(H_FROM|H_RCPT, h->h_flags) &&
839 !bitset(H_BINDLATE, h->h_flags))
841 bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
842 SM_FILE_T *savetrace = TrafficLogFile;
844 TrafficLogFile = NULL;
846 if (bitset(H_FROM, h->h_flags))
849 commaize(h, h->h_value, oldstyle, &mcibuf, e);
851 TrafficLogFile = savetrace;
855 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "%s:%s\n",
857 denlstring(h->h_value, false,
865 ** Write a terminator record -- this is to prevent
866 ** scurrilous crackers from appending any data.
869 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, ".\n");
871 if (sm_io_flush(tfp, SM_TIME_DEFAULT) != 0 ||
872 ((SuperSafe == SAFE_REALLY ||
873 SuperSafe == SAFE_REALLY_POSTMILTER ||
874 (SuperSafe == SAFE_INTERACTIVE && msync)) &&
875 fsync(sm_io_getinfo(tfp, SM_IO_WHAT_FD, NULL)) < 0) ||
879 syserr("!552 Error writing control file %s", tf);
881 syserr("!452 Error writing control file %s", tf);
886 char new = queue_letter(e, ANYQFL_LETTER);
888 /* rename (locked) tf to be (locked) [qh]f */
889 (void) sm_strlcpy(qf, queuename(e, ANYQFL_LETTER),
891 if (rename(tf, qf) < 0)
892 syserr("cannot rename(%s, %s), uid=%d",
893 tf, qf, (int) geteuid());
897 ** Check if type has changed and only
898 ** remove the old item if the rename above
902 if (e->e_qfletter != '\0' &&
903 e->e_qfletter != new)
907 sm_dprintf("type changed from %c to %c\n",
911 if (unlink(queuename(e, e->e_qfletter)) < 0)
913 /* XXX: something more drastic? */
915 sm_syslog(LOG_ERR, e->e_id,
916 "queueup: unlink(%s) failed: %s",
917 queuename(e, e->e_qfletter),
918 sm_errstring(errno));
925 ** fsync() after renaming to make sure metadata is
926 ** written to disk on filesystems in which renames are
930 if (SuperSafe != SAFE_NO)
932 /* for softupdates */
933 if (tfd >= 0 && fsync(tfd) < 0)
935 syserr("!queueup: cannot fsync queue temp file %s",
941 /* close and unlock old (locked) queue file */
942 if (e->e_lockfp != NULL)
943 (void) sm_io_close(e->e_lockfp, SM_TIME_DEFAULT);
948 sm_syslog(LOG_DEBUG, e->e_id, "queueup %s", qf);
954 sm_syslog(LOG_DEBUG, e->e_id, "queueup %s", tf);
956 e->e_qfletter = queue_letter(e, ANYQFL_LETTER);
960 e->e_flags |= EF_INQUEUE;
963 sm_dprintf("<<<<< done queueing %s <<<<<\n\n", e->e_id);
968 ** PRINTCTLADDR -- print control address to file.
972 ** tfp -- file pointer.
978 ** The control address (if changed) is printed to the file.
979 ** The last control address and uid are saved.
991 static ADDRESS *lastctladdr = NULL;
992 static uid_t lastuid;
995 if (a == NULL || a->q_alias == NULL || tfp == NULL)
997 if (lastctladdr != NULL && tfp != NULL)
998 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "C\n");
1004 /* find the active uid */
1014 user = q->q_ruser != NULL ? q->q_ruser : q->q_user;
1020 /* check to see if this is the same as last time */
1021 if (lastctladdr != NULL && uid == lastuid &&
1022 strcmp(lastctladdr->q_paddr, a->q_paddr) == 0)
1027 if (uid == 0 || user == NULL || user[0] == '\0')
1028 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "C");
1030 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "C%s:%ld:%ld",
1031 denlstring(user, true, false), (long) uid,
1033 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, ":%s\n",
1034 denlstring(a->q_paddr, true, false));
1038 ** RUNNERS_SIGTERM -- propagate a SIGTERM to queue runner process
1040 ** This propagates the signal to the child processes that are queue
1041 ** runners. This is for a queue runner "cleanup". After all of the
1042 ** child queue runner processes are signaled (it should be SIGTERM
1043 ** being the sig) then the old signal handler (Oldsh) is called
1044 ** to handle any cleanup set for this process (provided it is not
1045 ** SIG_DFL or SIG_IGN). The signal may not be handled immediately
1046 ** if the BlockOldsh flag is set. If the current process doesn't
1047 ** have a parent then handle the signal immediately, regardless of
1051 ** sig -- the signal number being sent
1057 ** Sets the NoMoreRunners boolean to true to stop more runners
1058 ** from being started in runqueue().
1060 ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
1061 ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
1065 static bool volatile NoMoreRunners = false;
1066 static sigfunc_t Oldsh_term = SIG_DFL;
1067 static sigfunc_t Oldsh_hup = SIG_DFL;
1068 static sigfunc_t volatile Oldsh = SIG_DFL;
1069 static bool BlockOldsh = false;
1070 static int volatile Oldsig = 0;
1071 static SIGFUNC_DECL runners_sigterm __P((int));
1072 static SIGFUNC_DECL runners_sighup __P((int));
1075 runners_sigterm(sig)
1078 int save_errno = errno;
1080 FIX_SYSV_SIGNAL(sig, runners_sigterm);
1082 CHECK_CRITICAL(sig);
1083 NoMoreRunners = true;
1086 proc_list_signal(PROC_QUEUE, sig);
1088 if (!BlockOldsh || getppid() <= 1)
1090 /* Check that a valid 'old signal handler' is callable */
1091 if (Oldsh_term != SIG_DFL && Oldsh_term != SIG_IGN &&
1092 Oldsh_term != runners_sigterm)
1096 return SIGFUNC_RETURN;
1099 ** RUNNERS_SIGHUP -- propagate a SIGHUP to queue runner process
1101 ** This propagates the signal to the child processes that are queue
1102 ** runners. This is for a queue runner "cleanup". After all of the
1103 ** child queue runner processes are signaled (it should be SIGHUP
1104 ** being the sig) then the old signal handler (Oldsh) is called to
1105 ** handle any cleanup set for this process (provided it is not SIG_DFL
1106 ** or SIG_IGN). The signal may not be handled immediately if the
1107 ** BlockOldsh flag is set. If the current process doesn't have
1108 ** a parent then handle the signal immediately, regardless of
1112 ** sig -- the signal number being sent
1118 ** Sets the NoMoreRunners boolean to true to stop more runners
1119 ** from being started in runqueue().
1121 ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
1122 ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
1130 int save_errno = errno;
1132 FIX_SYSV_SIGNAL(sig, runners_sighup);
1134 CHECK_CRITICAL(sig);
1135 NoMoreRunners = true;
1138 proc_list_signal(PROC_QUEUE, sig);
1140 if (!BlockOldsh || getppid() <= 1)
1142 /* Check that a valid 'old signal handler' is callable */
1143 if (Oldsh_hup != SIG_DFL && Oldsh_hup != SIG_IGN &&
1144 Oldsh_hup != runners_sighup)
1148 return SIGFUNC_RETURN;
1151 ** MARK_WORK_GROUP_RESTART -- mark a work group as needing a restart
1153 ** Sets a workgroup for restarting.
1156 ** wgrp -- the work group id to restart.
1157 ** reason -- why (signal?), -1 to turn off restart
1163 ** May set global RestartWorkGroup to true.
1165 ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
1166 ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
1171 mark_work_group_restart(wgrp, reason)
1175 if (wgrp < 0 || wgrp > NumWorkGroups)
1178 WorkGrp[wgrp].wg_restart = reason;
1180 RestartWorkGroup = true;
1183 ** RESTART_MARKED_WORK_GROUPS -- restart work groups marked as needing restart
1185 ** Restart any workgroup marked as needing a restart provided more
1186 ** runners are allowed.
1195 ** Sets global RestartWorkGroup to false.
1199 restart_marked_work_groups()
1207 /* Block SIGCHLD so reapchild() doesn't mess with us */
1208 wasblocked = sm_blocksignal(SIGCHLD);
1210 for (i = 0; i < NumWorkGroups; i++)
1212 if (WorkGrp[i].wg_restart >= 0)
1215 sm_syslog(LOG_ERR, NOQID,
1216 "restart queue runner=%d due to signal 0x%x",
1217 i, WorkGrp[i].wg_restart);
1218 restart_work_group(i);
1221 RestartWorkGroup = false;
1223 if (wasblocked == 0)
1224 (void) sm_releasesignal(SIGCHLD);
1227 ** RESTART_WORK_GROUP -- restart a specific work group
1229 ** Restart a specific workgroup provided more runners are allowed.
1230 ** If the requested work group has been restarted too many times log
1231 ** this and refuse to restart.
1234 ** wgrp -- the work group id to restart
1240 ** starts another process doing the work of wgrp
1243 #define MAX_PERSIST_RESTART 10 /* max allowed number of restarts */
1246 restart_work_group(wgrp)
1249 if (NoMoreRunners ||
1250 wgrp < 0 || wgrp > NumWorkGroups)
1253 WorkGrp[wgrp].wg_restart = -1;
1254 if (WorkGrp[wgrp].wg_restartcnt < MAX_PERSIST_RESTART)
1256 /* avoid overflow; increment here */
1257 WorkGrp[wgrp].wg_restartcnt++;
1258 (void) run_work_group(wgrp, RWG_FORK|RWG_PERSISTENT|RWG_RUNALL);
1262 sm_syslog(LOG_ERR, NOQID,
1263 "ERROR: persistent queue runner=%d restarted too many times, queue runner lost",
1268 ** SCHEDULE_QUEUE_RUNS -- schedule the next queue run for a work group.
1271 ** runall -- schedule even if individual bit is not set.
1272 ** wgrp -- the work group id to schedule.
1273 ** didit -- the queue run was performed for this work group.
1279 #define INCR_MOD(v, m) if (++v >= m) \
1284 schedule_queue_runs(runall, wgrp, didit)
1289 int qgrp, cgrp, endgrp;
1290 #if _FFR_QUEUE_SCHED_DBG
1293 #endif /* _FFR_QUEUE_SCHED_DBG */
1298 ** This is a bit ugly since we have to duplicate the
1299 ** code that "walks" through a work queue group.
1304 cgrp = endgrp = WorkGrp[wgrp].wg_curqgrp;
1309 #if _FFR_QUEUE_SCHED_DBG
1312 #endif /* _FFR_QUEUE_SCHED_DBG */
1313 qgrp = WorkGrp[wgrp].wg_qgs[cgrp]->qg_index;
1314 if (Queue[qgrp]->qg_queueintvl > 0)
1315 qintvl = Queue[qgrp]->qg_queueintvl;
1316 else if (QueueIntvl > 0)
1317 qintvl = QueueIntvl;
1319 qintvl = (time_t) 0;
1320 #if _FFR_QUEUE_SCHED_DBG
1321 lastsched = Queue[qgrp]->qg_nextrun;
1322 #endif /* _FFR_QUEUE_SCHED_DBG */
1323 if ((runall || Queue[qgrp]->qg_nextrun <= now) && qintvl > 0)
1325 #if _FFR_QUEUE_SCHED_DBG
1327 #endif /* _FFR_QUEUE_SCHED_DBG */
1328 if (minqintvl == 0 || qintvl < minqintvl)
1332 ** Only set a new time if a queue run was performed
1333 ** for this queue group. If the queue was not run,
1334 ** we could starve it by setting a new time on each
1339 Queue[qgrp]->qg_nextrun += qintvl;
1341 #if _FFR_QUEUE_SCHED_DBG
1343 sm_syslog(LOG_INFO, NOQID,
1344 "sqr: wgrp=%d, cgrp=%d, qgrp=%d, intvl=%ld, QI=%ld, runall=%d, lastrun=%ld, nextrun=%ld, sched=%d",
1345 wgrp, cgrp, qgrp, Queue[qgrp]->qg_queueintvl,
1346 QueueIntvl, runall, lastsched,
1347 Queue[qgrp]->qg_nextrun, sched);
1348 #endif /* _FFR_QUEUE_SCHED_DBG */
1349 INCR_MOD(cgrp, WorkGrp[wgrp].wg_numqgrp);
1350 } while (endgrp != cgrp);
1352 (void) sm_setevent(minqintvl, runqueueevent, 0);
1355 #if _FFR_QUEUE_RUN_PARANOIA
1357 ** CHECKQUEUERUNNER -- check whether a queue group hasn't been run.
1359 ** Use this if events may get lost and hence queue runners may not
1360 ** be started and mail will pile up in a queue.
1366 ** true if a queue run is necessary.
1369 ** may schedule a queue run.
1376 time_t now, minqintvl;
1380 for (qgrp = 0; qgrp < NumQueue && Queue[qgrp] != NULL; qgrp++)
1384 if (Queue[qgrp]->qg_queueintvl > 0)
1385 qintvl = Queue[qgrp]->qg_queueintvl;
1386 else if (QueueIntvl > 0)
1387 qintvl = QueueIntvl;
1389 qintvl = (time_t) 0;
1390 if (Queue[qgrp]->qg_nextrun <= now - qintvl)
1392 if (minqintvl == 0 || qintvl < minqintvl)
1395 sm_syslog(LOG_WARNING, NOQID,
1396 "checkqueuerunner: queue %d should have been run at %s, queue interval %ld",
1398 arpadate(ctime(&Queue[qgrp]->qg_nextrun)),
1404 (void) sm_setevent(minqintvl, runqueueevent, 0);
1409 #endif /* _FFR_QUEUE_RUN_PARANOIA */
1412 ** RUNQUEUE -- run the jobs in the queue.
1414 ** Gets the stuff out of the queue in some presumably logical
1415 ** order and processes them.
1418 ** forkflag -- true if the queue scanning should be done in
1419 ** a child process. We double-fork so it is not our
1420 ** child and we don't have to clean up after it.
1421 ** false can be ignored if we have multiple queues.
1422 ** verbose -- if true, print out status information.
1423 ** persistent -- persistent queue runner?
1424 ** runall -- run all groups or only a subset (DoQueueRun)?
1427 ** true if the queue run successfully began.
1430 ** runs things in the mail queue using run_work_group().
1431 ** maybe schedules next queue run.
1434 static ENVELOPE QueueEnvelope; /* the queue run envelope */
1435 static time_t LastQueueTime = 0; /* last time a queue ID assigned */
1436 static pid_t LastQueuePid = -1; /* last PID which had a queue ID */
1438 /* values for qp_supdirs */
1439 #define QP_NOSUB 0x0000 /* No subdirectories */
1440 #define QP_SUBDF 0x0001 /* "df" subdirectory */
1441 #define QP_SUBQF 0x0002 /* "qf" subdirectory */
1442 #define QP_SUBXF 0x0004 /* "xf" subdirectory */
1445 runqueue(forkflag, verbose, persistent, runall)
1453 static int curnum = 0;
1456 SM_NONVOLATILE int oldgroup = 0;
1458 if (sm_debug_active(&DebugLeakQ, 1))
1460 oldgroup = sm_heap_group();
1462 sm_dprintf("runqueue() heap group #%d\n", sm_heap_group());
1464 #endif /* SM_HEAP_CHECK */
1466 /* queue run has been started, don't do any more this time */
1469 /* more than one queue or more than one directory per queue */
1470 if (!forkflag && !verbose &&
1471 (WorkGrp[0].wg_qgs[0]->qg_numqueues > 1 || NumWorkGroups > 1 ||
1472 WorkGrp[0].wg_numqgrp > 1))
1476 ** For controlling queue runners via signals sent to this process.
1477 ** Oldsh* will get called too by runners_sig* (if it is not SIG_IGN
1478 ** or SIG_DFL) to preserve cleanup behavior. Now that this process
1479 ** will have children (and perhaps grandchildren) this handler will
1480 ** be left in place. This is because this process, once it has
1481 ** finished spinning off queue runners, may go back to doing something
1482 ** else (like being a daemon). And we still want on a SIG{TERM,HUP} to
1483 ** clean up the child queue runners. Only install 'runners_sig*' once
1484 ** else we'll get stuck looping forever.
1487 cursh = sm_signal(SIGTERM, runners_sigterm);
1488 if (cursh != runners_sigterm)
1490 cursh = sm_signal(SIGHUP, runners_sighup);
1491 if (cursh != runners_sighup)
1494 for (i = 0; i < NumWorkGroups && !NoMoreRunners; i++)
1496 int rwgflags = RWG_NONE;
1499 ** If MaxQueueChildren active then test whether the start
1500 ** of the next queue group's additional queue runners (maximum)
1501 ** will result in MaxQueueChildren being exceeded.
1503 ** Note: do not use continue; even though another workgroup
1504 ** may have fewer queue runners, this would be "unfair",
1505 ** i.e., this work group might "starve" then.
1508 #if _FFR_QUEUE_SCHED_DBG
1510 sm_syslog(LOG_INFO, NOQID,
1511 "rq: curnum=%d, MaxQueueChildren=%d, CurRunners=%d, WorkGrp[curnum].wg_maxact=%d",
1512 curnum, MaxQueueChildren, CurRunners,
1513 WorkGrp[curnum].wg_maxact);
1514 #endif /* _FFR_QUEUE_SCHED_DBG */
1515 if (MaxQueueChildren > 0 &&
1516 CurRunners + WorkGrp[curnum].wg_maxact > MaxQueueChildren)
1520 ** Pick up where we left off (curnum), in case we
1521 ** used up all the children last time without finishing.
1522 ** This give a round-robin fairness to queue runs.
1524 ** Increment CurRunners before calling run_work_group()
1525 ** to avoid a "race condition" with proc_list_drop() which
1526 ** decrements CurRunners if the queue runners terminate.
1527 ** Notice: CurRunners is an upper limit, in some cases
1528 ** (too few jobs in the queue) this value is larger than
1529 ** the actual number of queue runners. The discrepancy can
1530 ** increase if some queue runners "hang" for a long time.
1533 CurRunners += WorkGrp[curnum].wg_maxact;
1535 rwgflags |= RWG_FORK;
1537 rwgflags |= RWG_VERBOSE;
1539 rwgflags |= RWG_PERSISTENT;
1541 rwgflags |= RWG_RUNALL;
1542 ret = run_work_group(curnum, rwgflags);
1545 ** Failure means a message was printed for ETRN
1546 ** and subsequent queues are likely to fail as well.
1547 ** Decrement CurRunners in that case because
1548 ** none have been started.
1553 CurRunners -= WorkGrp[curnum].wg_maxact;
1558 schedule_queue_runs(runall, curnum, true);
1559 INCR_MOD(curnum, NumWorkGroups);
1562 /* schedule left over queue runs */
1563 if (i < NumWorkGroups && !NoMoreRunners && !persistent)
1567 for (h = curnum; i < NumWorkGroups; i++)
1569 schedule_queue_runs(runall, h, false);
1570 INCR_MOD(h, NumWorkGroups);
1576 if (sm_debug_active(&DebugLeakQ, 1))
1577 sm_heap_setgroup(oldgroup);
1578 #endif /* SM_HEAP_CHECK */
1582 #if _FFR_SKIP_DOMAINS
1584 ** SKIP_DOMAINS -- Skip 'skip' number of domains in the WorkQ.
1586 ** Added by Stephen Frost <sfrost@snowman.net> to support
1587 ** having each runner process every N'th domain instead of
1588 ** every N'th message.
1591 ** skip -- number of domains in WorkQ to skip.
1594 ** total number of messages skipped.
1606 for (n = 0, seqjump = 0; n < skip && WorkQ != NULL; seqjump++)
1608 if (WorkQ->w_next != NULL)
1610 if (WorkQ->w_host != NULL &&
1611 WorkQ->w_next->w_host != NULL)
1613 if (sm_strcasecmp(WorkQ->w_host,
1614 WorkQ->w_next->w_host) != 0)
1619 if ((WorkQ->w_host != NULL &&
1620 WorkQ->w_next->w_host == NULL) ||
1621 (WorkQ->w_host == NULL &&
1622 WorkQ->w_next->w_host != NULL))
1626 WorkQ = WorkQ->w_next;
1630 #endif /* _FFR_SKIP_DOMAINS */
1633 ** RUNNER_WORK -- have a queue runner do its work
1635 ** Have a queue runner do its work a list of entries.
1636 ** When work isn't directly being done then this process can take a signal
1637 ** and terminate immediately (in a clean fashion of course).
1638 ** When work is directly being done, it's not to be interrupted
1639 ** immediately: the work should be allowed to finish at a clean point
1640 ** before termination (in a clean fashion of course).
1644 ** sequenceno -- 'th process to run WorkQ.
1645 ** didfork -- did the calling process fork()?
1646 ** skip -- process only each skip'th item.
1647 ** njobs -- number of jobs in WorkQ.
1653 ** runs things in the mail queue.
1657 runner_work(e, sequenceno, didfork, skip, njobs)
1658 register ENVELOPE *e;
1671 ** Here we temporarily block the second calling of the handlers.
1672 ** This allows us to handle the signal without terminating in the
1673 ** middle of direct work. If a signal does come, the test for
1674 ** NoMoreRunners will find it.
1680 /* process them once at a time */
1681 while (WorkQ != NULL)
1684 SM_NONVOLATILE int oldgroup = 0;
1686 if (sm_debug_active(&DebugLeakQ, 1))
1688 oldgroup = sm_heap_group();
1690 sm_dprintf("run_queue_group() heap group #%d\n",
1693 #endif /* SM_HEAP_CHECK */
1695 /* do no more work */
1698 /* Check that a valid signal handler is callable */
1699 if (Oldsh != SIG_DFL && Oldsh != SIG_IGN &&
1700 Oldsh != runners_sighup &&
1701 Oldsh != runners_sigterm)
1706 w = WorkQ; /* assign current work item */
1709 ** Set the head of the WorkQ to the next work item.
1710 ** It is set 'skip' ahead (the number of parallel queue
1711 ** runners working on WorkQ together) since each runner
1712 ** works on every 'skip'th (N-th) item.
1713 #if _FFR_SKIP_DOMAINS
1714 ** In the case of the BYHOST Queue Sort Order, the 'item'
1715 ** is a domain, so we work on every 'skip'th (N-th) domain.
1716 #endif * _FFR_SKIP_DOMAINS *
1719 #if _FFR_SKIP_DOMAINS
1720 if (QueueSortOrder == QSO_BYHOST)
1723 if (WorkQ->w_next != NULL)
1725 if (WorkQ->w_host != NULL &&
1726 WorkQ->w_next->w_host != NULL)
1728 if (sm_strcasecmp(WorkQ->w_host,
1729 WorkQ->w_next->w_host)
1731 seqjump = skip_domains(skip);
1733 WorkQ = WorkQ->w_next;
1737 if ((WorkQ->w_host != NULL &&
1738 WorkQ->w_next->w_host == NULL) ||
1739 (WorkQ->w_host == NULL &&
1740 WorkQ->w_next->w_host != NULL))
1741 seqjump = skip_domains(skip);
1743 WorkQ = WorkQ->w_next;
1747 WorkQ = WorkQ->w_next;
1750 #endif /* _FFR_SKIP_DOMAINS */
1752 for (n = 0; n < skip && WorkQ != NULL; n++)
1753 WorkQ = WorkQ->w_next;
1759 ** Ignore jobs that are too expensive for the moment.
1761 ** Get new load average every GET_NEW_LA_TIME seconds.
1765 if (shouldqueue(WkRecipFact, Current_LA_time))
1767 char *msg = "Aborting queue run: load average too high";
1772 sm_syslog(LOG_INFO, NOQID, "runqueue: %s", msg);
1775 if (shouldqueue(w->w_pri, w->w_ctime))
1778 message(EmptyString);
1779 if (QueueSortOrder == QSO_BYPRIORITY)
1782 message("Skipping %s/%s (sequence %d of %d) and flushing rest of queue",
1783 qid_printqueue(w->w_qgrp,
1785 w->w_name + 2, sequenceno,
1788 sm_syslog(LOG_INFO, NOQID,
1789 "runqueue: Flushing queue from %s/%s (pri %ld, LA %d, %d of %d)",
1790 qid_printqueue(w->w_qgrp,
1792 w->w_name + 2, w->w_pri,
1793 CurrentLA, sequenceno,
1798 message("Skipping %s/%s (sequence %d of %d)",
1799 qid_printqueue(w->w_qgrp, w->w_qdir),
1800 w->w_name + 2, sequenceno, njobs);
1806 message(EmptyString);
1807 message("Running %s/%s (sequence %d of %d)",
1808 qid_printqueue(w->w_qgrp, w->w_qdir),
1809 w->w_name + 2, sequenceno, njobs);
1811 if (didfork && MaxQueueChildren > 0)
1813 sm_blocksignal(SIGCHLD);
1814 (void) sm_signal(SIGCHLD, reapchild);
1817 sm_syslog(LOG_DEBUG, NOQID,
1818 "runqueue %s dowork(%s)",
1819 qid_printqueue(w->w_qgrp, w->w_qdir),
1822 (void) dowork(w->w_qgrp, w->w_qdir, w->w_name + 2,
1823 ForkQueueRuns, false, e);
1826 sm_free(w->w_name); /* XXX */
1827 if (w->w_host != NULL)
1828 sm_free(w->w_host); /* XXX */
1829 sm_free((char *) w); /* XXX */
1830 sequenceno += seqjump; /* next sequence number */
1832 if (sm_debug_active(&DebugLeakQ, 1))
1833 sm_heap_setgroup(oldgroup);
1834 #endif /* SM_HEAP_CHECK */
1839 /* check the signals didn't happen during the revert */
1842 /* Check that a valid signal handler is callable */
1843 if (Oldsh != SIG_DFL && Oldsh != SIG_IGN &&
1844 Oldsh != runners_sighup && Oldsh != runners_sigterm)
1848 Oldsh = SIG_DFL; /* after the NoMoreRunners check */
1851 ** RUN_WORK_GROUP -- run the jobs in a queue group from a work group.
1853 ** Gets the stuff out of the queue in some presumably logical
1854 ** order and processes them.
1857 ** wgrp -- work group to process.
1858 ** flags -- RWG_* flags
1861 ** true if the queue run successfully began.
1864 ** runs things in the mail queue.
1867 /* Minimum sleep time for persistent queue runners */
1868 #define MIN_SLEEP_TIME 5
1871 run_work_group(wgrp, flags)
1875 register ENVELOPE *e;
1878 int qgrp, endgrp, h, i;
1882 extern ENVELOPE BlankEnvelope;
1883 extern SIGFUNC_DECL reapchild __P((int));
1889 ** If no work will ever be selected, don't even bother reading
1895 if (!bitset(RWG_PERSISTENT, flags) &&
1896 shouldqueue(WkRecipFact, Current_LA_time))
1898 char *msg = "Skipping queue run -- load average too high";
1900 if (bitset(RWG_VERBOSE, flags))
1901 message("458 %s\n", msg);
1903 sm_syslog(LOG_INFO, NOQID, "runqueue: %s", msg);
1908 ** See if we already have too many children.
1911 if (bitset(RWG_FORK, flags) &&
1912 WorkGrp[wgrp].wg_lowqintvl > 0 &&
1913 !bitset(RWG_PERSISTENT, flags) &&
1914 MaxChildren > 0 && CurChildren >= MaxChildren)
1916 char *msg = "Skipping queue run -- too many children";
1918 if (bitset(RWG_VERBOSE, flags))
1919 message("458 %s (%d)\n", msg, CurChildren);
1921 sm_syslog(LOG_INFO, NOQID, "runqueue: %s (%d)",
1927 ** See if we want to go off and do other useful work.
1930 if (bitset(RWG_FORK, flags))
1934 (void) sm_blocksignal(SIGCHLD);
1935 (void) sm_signal(SIGCHLD, reapchild);
1940 const char *msg = "Skipping queue run -- fork() failed";
1941 const char *err = sm_errstring(errno);
1943 if (bitset(RWG_VERBOSE, flags))
1944 message("458 %s: %s\n", msg, err);
1946 sm_syslog(LOG_INFO, NOQID, "runqueue: %s: %s",
1948 (void) sm_releasesignal(SIGCHLD);
1953 /* parent -- pick up intermediate zombie */
1954 (void) sm_blocksignal(SIGALRM);
1956 /* wgrp only used when queue runners are persistent */
1957 proc_list_add(pid, "Queue runner", PROC_QUEUE,
1958 WorkGrp[wgrp].wg_maxact,
1959 bitset(RWG_PERSISTENT, flags) ? wgrp : -1,
1961 (void) sm_releasesignal(SIGALRM);
1962 (void) sm_releasesignal(SIGCHLD);
1966 /* child -- clean up signals */
1968 /* Reset global flags */
1969 RestartRequest = NULL;
1970 RestartWorkGroup = false;
1971 ShutdownRequest = NULL;
1973 CurrentPid = getpid();
1974 close_sendmail_pid();
1977 ** Initialize exception stack and default exception
1978 ** handler for child process.
1981 sm_exc_newthread(fatal_error);
1985 /* Add parent process as first child item */
1986 proc_list_add(CurrentPid, "Queue runner child process",
1987 PROC_QUEUE_CHILD, 0, -1, NULL);
1988 (void) sm_releasesignal(SIGCHLD);
1989 (void) sm_signal(SIGCHLD, SIG_DFL);
1990 (void) sm_signal(SIGHUP, SIG_DFL);
1991 (void) sm_signal(SIGTERM, intsig);
1995 ** Release any resources used by the daemon code.
2000 /* force it to run expensive jobs */
2003 /* drop privileges */
2004 if (geteuid() == (uid_t) 0)
2005 (void) drop_privileges(false);
2008 ** Create ourselves an envelope
2011 CurEnv = &QueueEnvelope;
2012 rpool = sm_rpool_new_x(NULL);
2013 e = newenvelope(&QueueEnvelope, CurEnv, rpool);
2014 e->e_flags = BlankEnvelope.e_flags;
2017 /* make sure we have disconnected from parent */
2018 if (bitset(RWG_FORK, flags))
2025 ** If we are running part of the queue, always ignore stored
2029 if (QueueLimitId != NULL || QueueLimitSender != NULL ||
2030 QueueLimitQuarantine != NULL ||
2031 QueueLimitRecipient != NULL)
2033 IgnoreHostStatus = true;
2038 ** Here is where we choose the queue group from the work group.
2039 ** The caller of the "domorework" label must setup a new envelope.
2042 endgrp = WorkGrp[wgrp].wg_curqgrp; /* to not spin endlessly */
2047 ** Run a queue group if:
2048 ** RWG_RUNALL bit is set or the bit for this group is set.
2055 ** Find the next queue group within the work group that
2056 ** has been marked as needing a run.
2059 qgrp = WorkGrp[wgrp].wg_qgs[WorkGrp[wgrp].wg_curqgrp]->qg_index;
2060 WorkGrp[wgrp].wg_curqgrp++; /* advance */
2061 WorkGrp[wgrp].wg_curqgrp %= WorkGrp[wgrp].wg_numqgrp; /* wrap */
2062 if (bitset(RWG_RUNALL, flags) ||
2063 (Queue[qgrp]->qg_nextrun <= now &&
2064 Queue[qgrp]->qg_nextrun != (time_t) -1))
2066 if (endgrp == WorkGrp[wgrp].wg_curqgrp)
2069 if (bitset(RWG_FORK, flags))
2070 finis(true, true, ExitStat);
2071 return true; /* we're done */
2075 qdir = Queue[qgrp]->qg_curnum; /* round-robin init of queue position */
2076 #if _FFR_QUEUE_SCHED_DBG
2078 sm_syslog(LOG_INFO, NOQID,
2079 "rwg: wgrp=%d, qgrp=%d, qdir=%d, name=%s, curqgrp=%d, numgrps=%d",
2080 wgrp, qgrp, qdir, qid_printqueue(qgrp, qdir),
2081 WorkGrp[wgrp].wg_curqgrp, WorkGrp[wgrp].wg_numqgrp);
2082 #endif /* _FFR_QUEUE_SCHED_DBG */
2085 /* tweak niceness of queue runs */
2086 if (Queue[qgrp]->qg_nice > 0)
2087 (void) nice(Queue[qgrp]->qg_nice);
2088 #endif /* HASNICE */
2090 /* XXX running queue group... */
2091 sm_setproctitle(true, CurEnv, "running queue: %s",
2092 qid_printqueue(qgrp, qdir));
2094 if (LogLevel > 69 || tTd(63, 99))
2095 sm_syslog(LOG_DEBUG, NOQID,
2096 "runqueue %s, pid=%d, forkflag=%d",
2097 qid_printqueue(qgrp, qdir), (int) CurrentPid,
2098 bitset(RWG_FORK, flags));
2101 ** Start making passes through the queue.
2102 ** First, read and sort the entire queue.
2103 ** Then, process the work in that order.
2104 ** But if you take too long, start over.
2107 for (i = 0; i < Queue[qgrp]->qg_numqueues; i++)
2109 h = gatherq(qgrp, qdir, false, &full, &more);
2111 if (ShmId != SM_SHM_NO_ID)
2112 QSHM_ENTRIES(Queue[qgrp]->qg_qpaths[qdir].qp_idx) = h;
2113 #endif /* SM_CONF_SHM */
2114 /* If there are no more items in this queue advance */
2117 /* A round-robin advance */
2119 qdir %= Queue[qgrp]->qg_numqueues;
2122 /* Has the WorkList reached the limit? */
2124 break; /* don't try to gather more */
2127 /* order the existing work requests */
2128 njobs = sortq(Queue[qgrp]->qg_maxlist);
2129 Queue[qgrp]->qg_curnum = qdir; /* update */
2132 if (!Verbose && bitnset(QD_FORK, Queue[qgrp]->qg_flags))
2134 int loop, maxrunners;
2138 ** For this WorkQ we want to fork off N children (maxrunners)
2139 ** at this point. Each child has a copy of WorkQ. Each child
2140 ** will process every N-th item. The parent will wait for all
2141 ** of the children to finish before moving on to the next
2142 ** queue group within the work group. This saves us forking
2143 ** a new runner-child for each work item.
2144 ** It's valid for qg_maxqrun == 0 since this may be an
2145 ** explicit "don't run this queue" setting.
2148 maxrunners = Queue[qgrp]->qg_maxqrun;
2150 /* No need to have more runners then there are jobs */
2151 if (maxrunners > njobs)
2153 for (loop = 0; loop < maxrunners; loop++)
2156 ** Since the delivery may happen in a child and the
2157 ** parent does not wait, the parent may close the
2158 ** maps thereby removing any shared memory used by
2159 ** the map. Therefore, close the maps now so the
2160 ** child will dynamically open them if necessary.
2168 syserr("run_work_group: cannot fork");
2173 /* parent -- clean out connection cache */
2174 mci_flush(false, NULL);
2175 #if _FFR_SKIP_DOMAINS
2176 if (QueueSortOrder == QSO_BYHOST)
2178 sequenceno += skip_domains(1);
2181 #endif /* _FFR_SKIP_DOMAINS */
2184 WorkQ = WorkQ->w_next;
2187 proc_list_add(pid, "Queue child runner process",
2188 PROC_QUEUE_CHILD, 0, -1, NULL);
2190 /* No additional work, no additional runners */
2196 /* child -- Reset global flags */
2197 RestartRequest = NULL;
2198 RestartWorkGroup = false;
2199 ShutdownRequest = NULL;
2201 CurrentPid = getpid();
2202 close_sendmail_pid();
2205 ** Initialize exception stack and default
2206 ** exception handler for child process.
2207 ** When fork()'d the child now has a private
2208 ** copy of WorkQ at its current position.
2211 sm_exc_newthread(fatal_error);
2214 ** SMTP processes (whether -bd or -bs) set
2215 ** SIGCHLD to reapchild to collect
2216 ** children status. However, at delivery
2217 ** time, that status must be collected
2218 ** by sm_wait() to be dealt with properly
2219 ** (check success of delivery based
2220 ** on status code, etc). Therefore, if we
2221 ** are an SMTP process, reset SIGCHLD
2222 ** back to the default so reapchild
2223 ** doesn't collect status before
2227 if (OpMode == MD_SMTP ||
2228 OpMode == MD_DAEMON ||
2229 MaxQueueChildren > 0)
2232 sm_releasesignal(SIGCHLD);
2233 (void) sm_signal(SIGCHLD, SIG_DFL);
2236 /* child -- error messages to the transcript */
2237 QuickAbort = OnlyOneError = false;
2238 runner_work(e, sequenceno, true,
2241 /* This child is done */
2242 finis(true, true, ExitStat);
2247 sm_releasesignal(SIGCHLD);
2250 ** Wait until all of the runners have completed before
2251 ** seeing if there is another queue group in the
2252 ** work group to process.
2253 ** XXX Future enhancement: don't wait() for all children
2254 ** here, just go ahead and make sure that overall the number
2255 ** of children is not exceeded.
2258 while (CurChildren > 0)
2263 while ((ret = sm_wait(&status)) <= 0)
2265 proc_list_drop(ret, status, NULL);
2268 else if (Queue[qgrp]->qg_maxqrun > 0 || bitset(RWG_FORCE, flags))
2271 ** When current process will not fork children to do the work,
2272 ** it will do the work itself. The 'skip' will be 1 since
2273 ** there are no child runners to divide the work across.
2276 runner_work(e, sequenceno, false, 1, njobs);
2279 /* free memory allocated by newenvelope() above */
2280 sm_rpool_free(rpool);
2281 QueueEnvelope.e_rpool = NULL;
2283 /* Are there still more queues in the work group to process? */
2284 if (endgrp != WorkGrp[wgrp].wg_curqgrp)
2286 rpool = sm_rpool_new_x(NULL);
2287 e = newenvelope(&QueueEnvelope, CurEnv, rpool);
2288 e->e_flags = BlankEnvelope.e_flags;
2292 /* No more queues in work group to process. Now check persistent. */
2293 if (bitset(RWG_PERSISTENT, flags))
2296 sm_setproctitle(true, CurEnv, "running queue: %s",
2297 qid_printqueue(qgrp, qdir));
2300 ** close bogus maps, i.e., maps which caused a tempfail,
2301 ** so we get fresh map connections on the next lookup.
2302 ** closemaps() is also called when children are started.
2307 /* Close any cached connections. */
2308 mci_flush(true, NULL);
2310 /* Clean out expired related entries. */
2314 /* Update MX records for FallbackMX. */
2315 if (FallbackMX != NULL)
2316 (void) getfallbackmxrr(FallbackMX);
2317 #endif /* NAMED_BIND */
2320 /* close UserDatabase */
2325 if (sm_debug_active(&SmHeapCheck, 2)
2326 && access("memdump", F_OK) == 0
2332 out = sm_io_open(SmFtStdio, SM_TIME_DEFAULT,
2333 "memdump.out", SM_IO_APPEND, NULL);
2336 (void) sm_io_fprintf(out, SM_TIME_DEFAULT, "----------------------\n");
2338 sm_debug_level(&SmHeapCheck) - 1);
2339 (void) sm_io_close(out, SM_TIME_DEFAULT);
2342 #endif /* SM_HEAP_CHECK */
2344 /* let me rest for a second to catch my breath */
2345 if (njobs == 0 && WorkGrp[wgrp].wg_lowqintvl < MIN_SLEEP_TIME)
2346 sleep(MIN_SLEEP_TIME);
2347 else if (WorkGrp[wgrp].wg_lowqintvl <= 0)
2348 sleep(QueueIntvl > 0 ? QueueIntvl : MIN_SLEEP_TIME);
2350 sleep(WorkGrp[wgrp].wg_lowqintvl);
2353 ** Get the LA outside the WorkQ loop if necessary.
2354 ** In a persistent queue runner the code is repeated over
2355 ** and over but gatherq() may ignore entries due to
2356 ** shouldqueue() (do we really have to do this twice?).
2357 ** Hence the queue runners would just idle around when once
2358 ** CurrentLA caused all entries in a queue to be ignored.
2363 rpool = sm_rpool_new_x(NULL);
2364 e = newenvelope(&QueueEnvelope, CurEnv, rpool);
2365 e->e_flags = BlankEnvelope.e_flags;
2369 /* exit without the usual cleanup */
2371 if (bitset(RWG_FORK, flags))
2372 finis(true, true, ExitStat);
2378 ** DOQUEUERUN -- do a queue run?
2388 ** RUNQUEUEEVENT -- Sets a flag to indicate that a queue run should be done.
2397 ** The invocation of this function via an alarm may interrupt
2398 ** a set of actions. Thus errno may be set in that context.
2399 ** We need to restore errno at the end of this function to ensure
2400 ** that any work done here that sets errno doesn't return a
2401 ** misleading/false errno value. Errno may be EINTR upon entry to
2402 ** this function because of non-restartable/continuable system
2403 ** API was active. Iff this is true we will override errno as
2404 ** a timeout (as a more accurate error message).
2406 ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
2407 ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
2412 runqueueevent(ignore)
2415 int save_errno = errno;
2418 ** Set the general bit that we want a queue run,
2419 ** tested in doqueuerun()
2423 #if _FFR_QUEUE_SCHED_DBG
2425 sm_syslog(LOG_INFO, NOQID, "rqe: done");
2426 #endif /* _FFR_QUEUE_SCHED_DBG */
2433 ** GATHERQ -- gather messages from the message queue(s) the work queue.
2436 ** qgrp -- the index of the queue group.
2437 ** qdir -- the index of the queue directory.
2438 ** doall -- if set, include everything in the queue (even
2439 ** the jobs that cannot be run because the load
2440 ** average is too high, or MaxQueueRun is reached).
2441 ** Otherwise, exclude those jobs.
2442 ** full -- (optional) to be set 'true' if WorkList is full
2443 ** more -- (optional) to be set 'true' if there are still more
2444 ** messages in this queue not added to WorkList
2447 ** The number of request in the queue (not necessarily
2448 ** the number of requests in WorkList however).
2451 ** prepares available work into WorkList
2454 #define NEED_P 0001 /* 'P': priority */
2455 #define NEED_T 0002 /* 'T': time */
2456 #define NEED_R 0004 /* 'R': recipient */
2457 #define NEED_S 0010 /* 'S': sender */
2458 #define NEED_H 0020 /* host */
2459 #define HAS_QUARANTINE 0040 /* has an unexpected 'q' line */
2460 #define NEED_QUARANTINE 0100 /* 'q': reason */
2462 static WORK *WorkList = NULL; /* list of unsort work */
2463 static int WorkListSize = 0; /* current max size of WorkList */
2464 static int WorkListCount = 0; /* # of work items in WorkList */
2467 gatherq(qgrp, qdir, doall, full, more)
2474 register struct dirent *d;
2481 char qd[MAXPATHLEN];
2482 char qf[MAXPATHLEN];
2484 wn = WorkListCount - 1;
2487 (void) sm_strlcpy(qd, ".", sizeof(qd));
2489 (void) sm_strlcpyn(qd, sizeof(qd), 2,
2490 Queue[qgrp]->qg_qpaths[qdir].qp_name,
2492 Queue[qgrp]->qg_qpaths[qdir].qp_subdirs)
2497 sm_dprintf("gatherq:\n");
2499 check = QueueLimitId;
2500 while (check != NULL)
2502 sm_dprintf("\tQueueLimitId = %s%s\n",
2503 check->queue_negate ? "!" : "",
2504 check->queue_match);
2505 check = check->queue_next;
2508 check = QueueLimitSender;
2509 while (check != NULL)
2511 sm_dprintf("\tQueueLimitSender = %s%s\n",
2512 check->queue_negate ? "!" : "",
2513 check->queue_match);
2514 check = check->queue_next;
2517 check = QueueLimitRecipient;
2518 while (check != NULL)
2520 sm_dprintf("\tQueueLimitRecipient = %s%s\n",
2521 check->queue_negate ? "!" : "",
2522 check->queue_match);
2523 check = check->queue_next;
2526 if (QueueMode == QM_QUARANTINE)
2528 check = QueueLimitQuarantine;
2529 while (check != NULL)
2531 sm_dprintf("\tQueueLimitQuarantine = %s%s\n",
2532 check->queue_negate ? "!" : "",
2533 check->queue_match);
2534 check = check->queue_next;
2539 /* open the queue directory */
2543 syserr("gatherq: cannot open \"%s\"",
2544 qid_printqueue(qgrp, qdir));
2546 *full = WorkListCount >= MaxQueueRun && MaxQueueRun > 0;
2553 ** Read the work directory.
2556 while ((d = readdir(f)) != NULL)
2560 char lbuf[MAXNAME + 1];
2564 sm_dprintf("gatherq: checking %s..", d->d_name);
2566 /* is this an interesting entry? */
2567 if (!(((QueueMode == QM_NORMAL &&
2568 d->d_name[0] == NORMQF_LETTER) ||
2569 (QueueMode == QM_QUARANTINE &&
2570 d->d_name[0] == QUARQF_LETTER) ||
2571 (QueueMode == QM_LOST &&
2572 d->d_name[0] == LOSEQF_LETTER)) &&
2573 d->d_name[1] == 'f'))
2576 sm_dprintf(" skipping\n");
2582 if (strlen(d->d_name) >= MAXQFNAME)
2585 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
2586 "gatherq: %s too long, %d max characters\n",
2587 d->d_name, MAXQFNAME);
2589 sm_syslog(LOG_ALERT, NOQID,
2590 "gatherq: %s too long, %d max characters",
2591 d->d_name, MAXQFNAME);
2595 check = QueueLimitId;
2596 while (check != NULL)
2598 if (strcontainedin(false, check->queue_match,
2599 d->d_name) != check->queue_negate)
2602 check = check->queue_next;
2604 if (QueueLimitId != NULL && check == NULL)
2607 /* grow work list if necessary */
2608 if (++wn >= MaxQueueRun && MaxQueueRun > 0)
2610 if (wn == MaxQueueRun && LogLevel > 0)
2611 sm_syslog(LOG_WARNING, NOQID,
2612 "WorkList for %s maxed out at %d",
2613 qid_printqueue(qgrp, qdir),
2616 continue; /* just count entries */
2619 if (wn >= WorkListSize)
2621 grow_wlist(qgrp, qdir);
2622 if (wn >= WorkListSize)
2628 (void) sm_strlcpyn(qf, sizeof(qf), 3, qd, "/", d->d_name);
2629 if (stat(qf, &sbuf) < 0)
2631 if (errno != ENOENT)
2632 sm_syslog(LOG_INFO, NOQID,
2633 "gatherq: can't stat %s/%s",
2634 qid_printqueue(qgrp, qdir),
2639 if (!bitset(S_IFREG, sbuf.st_mode))
2641 /* Yikes! Skip it or we will hang on open! */
2642 if (!((d->d_name[0] == DATAFL_LETTER ||
2643 d->d_name[0] == NORMQF_LETTER ||
2644 d->d_name[0] == QUARQF_LETTER ||
2645 d->d_name[0] == LOSEQF_LETTER ||
2646 d->d_name[0] == XSCRPT_LETTER) &&
2647 d->d_name[1] == 'f' && d->d_name[2] == '\0'))
2648 syserr("gatherq: %s/%s is not a regular file",
2649 qid_printqueue(qgrp, qdir), d->d_name);
2654 /* avoid work if possible */
2655 if ((QueueSortOrder == QSO_BYFILENAME ||
2656 QueueSortOrder == QSO_BYMODTIME ||
2657 QueueSortOrder == QSO_NONE ||
2658 QueueSortOrder == QSO_RANDOM) &&
2659 QueueLimitQuarantine == NULL &&
2660 QueueLimitSender == NULL &&
2661 QueueLimitRecipient == NULL)
2665 w->w_name = newstr(d->d_name);
2667 w->w_lock = w->w_tooyoung = false;
2670 w->w_mtime = sbuf.st_mtime;
2675 /* open control file */
2676 cf = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, qf, SM_IO_RDONLY_B,
2678 if (cf == NULL && OpMode != MD_PRINT)
2680 /* this may be some random person sending hir msgs */
2682 sm_dprintf("gatherq: cannot open %s: %s\n",
2683 d->d_name, sm_errstring(errno));
2690 w->w_name = newstr(d->d_name);
2694 w->w_lock = !lockfile(sm_io_getinfo(cf, SM_IO_WHAT_FD,
2699 w->w_tooyoung = false;
2701 /* make sure jobs in creation don't clog queue */
2702 w->w_pri = 0x7fffffff;
2704 w->w_mtime = sbuf.st_mtime;
2706 /* extract useful information */
2708 if (QueueSortOrder == QSO_BYHOST
2710 || QueueSortOrder == QSO_BYSHUFFLE
2711 #endif /* _FFR_RHS */
2714 /* need w_host set for host sort order */
2717 if (QueueLimitSender != NULL)
2719 if (QueueLimitRecipient != NULL)
2721 if (QueueLimitQuarantine != NULL)
2722 i |= NEED_QUARANTINE;
2723 while (cf != NULL && i != 0 &&
2724 sm_io_fgets(cf, SM_TIME_DEFAULT, lbuf,
2725 sizeof(lbuf)) != NULL)
2730 p = strchr(lbuf, '\n');
2735 /* flush rest of overly long line */
2736 while ((c = sm_io_getc(cf, SM_TIME_DEFAULT))
2737 != SM_IO_EOF && c != '\n')
2744 qfver = atoi(&lbuf[1]);
2748 w->w_pri = atol(&lbuf[1]);
2753 w->w_ctime = atol(&lbuf[1]);
2758 if (QueueMode != QM_QUARANTINE &&
2759 QueueMode != QM_LOST)
2762 sm_dprintf("%s not marked as quarantined but has a 'q' line\n",
2764 i |= HAS_QUARANTINE;
2766 else if (QueueMode == QM_QUARANTINE)
2768 if (QueueLimitQuarantine == NULL)
2770 i &= ~NEED_QUARANTINE;
2774 check = QueueLimitQuarantine;
2775 while (check != NULL)
2777 if (strcontainedin(false,
2780 check->queue_negate)
2783 check = check->queue_next;
2786 i &= ~NEED_QUARANTINE;
2791 if (w->w_host == NULL &&
2792 (p = strrchr(&lbuf[1], '@')) != NULL)
2795 if (QueueSortOrder == QSO_BYSHUFFLE)
2796 w->w_host = newstr(&p[1]);
2798 #endif /* _FFR_RHS */
2799 w->w_host = strrev(&p[1]);
2800 makelower(w->w_host);
2803 if (QueueLimitRecipient == NULL)
2810 p = strchr(&lbuf[1], ':');
2814 ++p; /* skip over ':' */
2818 check = QueueLimitRecipient;
2819 while (check != NULL)
2821 if (strcontainedin(true,
2824 check->queue_negate)
2827 check = check->queue_next;
2834 check = QueueLimitSender;
2835 while (check != NULL)
2837 if (strcontainedin(true,
2840 check->queue_negate)
2843 check = check->queue_next;
2850 age = curtime() - (time_t) atol(&lbuf[1]);
2851 if (age >= 0 && MinQueueAge > 0 &&
2853 w->w_tooyoung = true;
2857 if (atol(&lbuf[1]) == 0)
2858 w->w_tooyoung = false;
2863 (void) sm_io_close(cf, SM_TIME_DEFAULT);
2865 if ((!doall && (shouldqueue(w->w_pri, w->w_ctime) ||
2867 bitset(HAS_QUARANTINE, i) ||
2868 bitset(NEED_QUARANTINE, i) ||
2869 bitset(NEED_R|NEED_S, i))
2871 /* don't even bother sorting this job in */
2873 sm_dprintf("skipping %s (%x)\n", w->w_name, i);
2874 sm_free(w->w_name); /* XXX */
2875 if (w->w_host != NULL)
2876 sm_free(w->w_host); /* XXX */
2885 i = wn - WorkListCount;
2886 WorkListCount += SM_MIN(num_ent, WorkListSize);
2889 *more = WorkListCount < wn;
2892 *full = (wn >= MaxQueueRun && MaxQueueRun > 0) ||
2893 (WorkList == NULL && wn > 0);
2898 ** SORTQ -- sort the work list
2900 ** First the old WorkQ is cleared away. Then the WorkList is sorted
2901 ** for all items so that important (higher sorting value) items are not
2902 ** trunctated off. Then the most important items are moved from
2903 ** WorkList to WorkQ. The lower count of 'max' or MaxListCount items
2907 ** max -- maximum number of items to be placed in WorkQ
2910 ** the number of items in WorkQ
2913 ** WorkQ gets released and filled with new work. WorkList
2914 ** gets released. Work items get sorted in order.
2921 register int i; /* local counter */
2922 register WORK *w; /* tmp item pointer */
2923 int wc = WorkListCount; /* trim size for WorkQ */
2929 /* Clear out old WorkQ. */
2930 for (w = WorkQ; w != NULL; w = nw)
2933 sm_free(w->w_name); /* XXX */
2934 if (w->w_host != NULL)
2935 sm_free(w->w_host); /* XXX */
2936 sm_free((char *) w); /* XXX */
2941 if (WorkList == NULL || wc <= 0)
2945 ** The sort now takes place using all of the items in WorkList.
2946 ** The list gets trimmed to the most important items after the sort.
2947 ** If the trim were to happen before the sort then one or more
2948 ** important items might get truncated off -- not what we want.
2951 if (QueueSortOrder == QSO_BYHOST)
2954 ** Sort the work directory for the first time,
2955 ** based on host name, lock status, and priority.
2958 qsort((char *) WorkList, wc, sizeof(*WorkList), workcmpf1);
2961 ** If one message to host is locked, "lock" all messages
2968 if (!WorkList[i].w_lock)
2976 if (WorkList[i].w_host == NULL &&
2978 WorkList[i].w_lock = true;
2979 else if (WorkList[i].w_host != NULL &&
2980 w->w_host != NULL &&
2981 sm_strcasecmp(WorkList[i].w_host,
2983 WorkList[i].w_lock = true;
2990 ** Sort the work directory for the second time,
2991 ** based on lock status, host name, and priority.
2994 qsort((char *) WorkList, wc, sizeof(*WorkList), workcmpf2);
2996 else if (QueueSortOrder == QSO_BYTIME)
2999 ** Simple sort based on submission time only.
3002 qsort((char *) WorkList, wc, sizeof(*WorkList), workcmpf3);
3004 else if (QueueSortOrder == QSO_BYFILENAME)
3007 ** Sort based on queue filename.
3010 qsort((char *) WorkList, wc, sizeof(*WorkList), workcmpf4);
3012 else if (QueueSortOrder == QSO_RANDOM)
3015 ** Sort randomly. To avoid problems with an instable sort,
3016 ** use a random index into the queue file name to start
3020 randi = get_rand_mod(MAXQFNAME);
3023 qsort((char *) WorkList, wc, sizeof(*WorkList), workcmpf5);
3025 else if (QueueSortOrder == QSO_BYMODTIME)
3028 ** Simple sort based on modification time of queue file.
3029 ** This puts the oldest items first.
3032 qsort((char *) WorkList, wc, sizeof(*WorkList), workcmpf6);
3035 else if (QueueSortOrder == QSO_BYSHUFFLE)
3038 ** Simple sort based on shuffled host name.
3041 init_shuffle_alphabet();
3042 qsort((char *) WorkList, wc, sizeof(*WorkList), workcmpf7);
3044 #endif /* _FFR_RHS */
3045 else if (QueueSortOrder == QSO_BYPRIORITY)
3048 ** Simple sort based on queue priority only.
3051 qsort((char *) WorkList, wc, sizeof(*WorkList), workcmpf0);
3053 /* else don't sort at all */
3055 /* Check if the per queue group item limit will be exceeded */
3056 if (wc > max && max > 0)
3060 ** Convert the work list into canonical form.
3061 ** Should be turning it into a list of envelopes here perhaps.
3062 ** Only take the most important items up to the per queue group
3066 for (i = wc; --i >= 0; )
3068 w = (WORK *) xalloc(sizeof(*w));
3069 w->w_qgrp = WorkList[i].w_qgrp;
3070 w->w_qdir = WorkList[i].w_qdir;
3071 w->w_name = WorkList[i].w_name;
3072 w->w_host = WorkList[i].w_host;
3073 w->w_lock = WorkList[i].w_lock;
3074 w->w_tooyoung = WorkList[i].w_tooyoung;
3075 w->w_pri = WorkList[i].w_pri;
3076 w->w_ctime = WorkList[i].w_ctime;
3077 w->w_mtime = WorkList[i].w_mtime;
3082 /* free the rest of the list */
3083 for (i = WorkListCount; --i >= wc; )
3085 sm_free(WorkList[i].w_name);
3086 if (WorkList[i].w_host != NULL)
3087 sm_free(WorkList[i].w_host);
3090 if (WorkList != NULL)
3091 sm_free(WorkList); /* XXX */
3098 for (w = WorkQ; w != NULL; w = w->w_next)
3100 if (w->w_host != NULL)
3101 sm_dprintf("%22s: pri=%ld %s\n",
3102 w->w_name, w->w_pri, w->w_host);
3104 sm_dprintf("%32s: pri=%ld\n",
3105 w->w_name, w->w_pri);
3109 return wc; /* return number of WorkQ items */
3112 ** GROW_WLIST -- make the work list larger
3115 ** qgrp -- the index for the queue group.
3116 ** qdir -- the index for the queue directory.
3122 ** Adds another QUEUESEGSIZE entries to WorkList if possible.
3123 ** It can fail if there isn't enough memory, so WorkListSize
3124 ** should be checked again upon return.
3128 grow_wlist(qgrp, qdir)
3133 sm_dprintf("grow_wlist: WorkListSize=%d\n", WorkListSize);
3134 if (WorkList == NULL)
3136 WorkList = (WORK *) xalloc((sizeof(*WorkList)) *
3137 (QUEUESEGSIZE + 1));
3138 WorkListSize = QUEUESEGSIZE;
3142 int newsize = WorkListSize + QUEUESEGSIZE;
3143 WORK *newlist = (WORK *) sm_realloc((char *) WorkList,
3144 (unsigned) sizeof(WORK) * (newsize + 1));
3146 if (newlist != NULL)
3148 WorkListSize = newsize;
3152 sm_syslog(LOG_INFO, NOQID,
3153 "grew WorkList for %s to %d",
3154 qid_printqueue(qgrp, qdir),
3158 else if (LogLevel > 0)
3160 sm_syslog(LOG_ALERT, NOQID,
3161 "FAILED to grow WorkList for %s to %d",
3162 qid_printqueue(qgrp, qdir), newsize);
3166 sm_dprintf("grow_wlist: WorkListSize now %d\n", WorkListSize);
3169 ** WORKCMPF0 -- simple priority-only compare function.
3172 ** a -- the first argument.
3173 ** b -- the second argument.
3198 ** WORKCMPF1 -- first compare function for ordering work based on host name.
3200 ** Sorts on host name, lock status, and priority in that order.
3203 ** a -- the first argument.
3204 ** b -- the second argument.
3221 if (a->w_host != NULL && b->w_host == NULL)
3223 else if (a->w_host == NULL && b->w_host != NULL)
3225 if (a->w_host != NULL && b->w_host != NULL &&
3226 (i = sm_strcasecmp(a->w_host, b->w_host)) != 0)
3230 if (a->w_lock != b->w_lock)
3231 return b->w_lock - a->w_lock;
3234 return workcmpf0(a, b);
3237 ** WORKCMPF2 -- second compare function for ordering work based on host name.
3239 ** Sorts on lock status, host name, and priority in that order.
3242 ** a -- the first argument.
3243 ** b -- the second argument.
3260 if (a->w_lock != b->w_lock)
3261 return a->w_lock - b->w_lock;
3264 if (a->w_host != NULL && b->w_host == NULL)
3266 else if (a->w_host == NULL && b->w_host != NULL)
3268 if (a->w_host != NULL && b->w_host != NULL &&
3269 (i = sm_strcasecmp(a->w_host, b->w_host)) != 0)
3273 return workcmpf0(a, b);
3276 ** WORKCMPF3 -- simple submission-time-only compare function.
3279 ** a -- the first argument.
3280 ** b -- the second argument.
3294 if (a->w_ctime > b->w_ctime)
3296 else if (a->w_ctime < b->w_ctime)
3302 ** WORKCMPF4 -- compare based on file name
3305 ** a -- the first argument.
3306 ** b -- the second argument.
3320 return strcmp(a->w_name, b->w_name);
3323 ** WORKCMPF5 -- compare based on assigned random number
3326 ** a -- the first argument (ignored).
3327 ** b -- the second argument (ignored).
3339 if (strlen(a->w_name) < randi || strlen(b->w_name) < randi)
3341 return a->w_name[randi] - b->w_name[randi];
3344 ** WORKCMPF6 -- simple modification-time-only compare function.
3347 ** a -- the first argument.
3348 ** b -- the second argument.
3362 if (a->w_mtime > b->w_mtime)
3364 else if (a->w_mtime < b->w_mtime)
3371 ** WORKCMPF7 -- compare function for ordering work based on shuffled host name.
3373 ** Sorts on lock status, host name, and priority in that order.
3376 ** a -- the first argument.
3377 ** b -- the second argument.
3394 if (a->w_lock != b->w_lock)
3395 return a->w_lock - b->w_lock;
3398 if (a->w_host != NULL && b->w_host == NULL)
3400 else if (a->w_host == NULL && b->w_host != NULL)
3402 if (a->w_host != NULL && b->w_host != NULL &&
3403 (i = sm_strshufflecmp(a->w_host, b->w_host)) != 0)
3407 return workcmpf0(a, b);
3409 #endif /* _FFR_RHS */
3411 ** STRREV -- reverse string
3413 ** Returns a pointer to a new string that is the reverse of
3414 ** the string pointed to by fwd. The space for the new
3415 ** string is obtained using xalloc().
3418 ** fwd -- the string to reverse.
3421 ** the reversed string.
3432 rev = xalloc(len + 1);
3433 for (cnt = 0; cnt < len; ++cnt)
3434 rev[cnt] = fwd[len - cnt - 1];
3444 static unsigned char ShuffledAlphabet[NCHAR];
3447 init_shuffle_alphabet()
3449 static bool init = false;
3455 /* fill the ShuffledAlphabet */
3456 for (i = 0; i < NASCII; i++)
3457 ShuffledAlphabet[i] = i;
3460 for (i = 1; i < NASCII; i++)
3462 register int j = get_random() % NASCII;
3465 tmp = ShuffledAlphabet[j];
3466 ShuffledAlphabet[j] = ShuffledAlphabet[i];
3467 ShuffledAlphabet[i] = tmp;
3470 /* make it case insensitive */
3471 for (i = 'A'; i <= 'Z'; i++)
3472 ShuffledAlphabet[i] = ShuffledAlphabet[i + 'a' - 'A'];
3474 /* fill the upper part */
3475 for (i = 0; i < NASCII; i++)
3476 ShuffledAlphabet[i + NASCII] = ShuffledAlphabet[i];
3481 sm_strshufflecmp(a, b)
3485 const unsigned char *us1 = (const unsigned char *) a;
3486 const unsigned char *us2 = (const unsigned char *) b;
3488 while (ShuffledAlphabet[*us1] == ShuffledAlphabet[*us2++])
3493 return (ShuffledAlphabet[*us1] - ShuffledAlphabet[*--us2]);
3495 #endif /* _FFR_RHS */
3498 ** DOWORK -- do a work request.
3501 ** qgrp -- the index of the queue group for the job.
3502 ** qdir -- the index of the queue directory for the job.
3503 ** id -- the ID of the job to run.
3504 ** forkflag -- if set, run this in background.
3505 ** requeueflag -- if set, reinstantiate the queue quickly.
3506 ** This is used when expanding aliases in the queue.
3507 ** If forkflag is also set, it doesn't wait for the
3509 ** e - the envelope in which to run it.
3512 ** process id of process that is running the queue job.
3515 ** The work request is satisfied if possible.
3519 dowork(qgrp, qdir, id, forkflag, requeueflag, e)
3525 register ENVELOPE *e;
3531 sm_dprintf("dowork(%s/%s)\n", qid_printqueue(qgrp, qdir), id);
3540 ** Since the delivery may happen in a child and the
3541 ** parent does not wait, the parent may close the
3542 ** maps thereby removing any shared memory used by
3543 ** the map. Therefore, close the maps now so the
3544 ** child will dynamically open them if necessary.
3552 syserr("dowork: cannot fork");
3557 /* parent -- clean out connection cache */
3558 mci_flush(false, NULL);
3563 ** Initialize exception stack and default exception
3564 ** handler for child process.
3567 /* Reset global flags */
3568 RestartRequest = NULL;
3569 RestartWorkGroup = false;
3570 ShutdownRequest = NULL;
3572 CurrentPid = getpid();
3573 sm_exc_newthread(fatal_error);
3576 ** See note above about SMTP processes and SIGCHLD.
3579 if (OpMode == MD_SMTP ||
3580 OpMode == MD_DAEMON ||
3581 MaxQueueChildren > 0)
3584 sm_releasesignal(SIGCHLD);
3585 (void) sm_signal(SIGCHLD, SIG_DFL);
3588 /* child -- error messages to the transcript */
3589 QuickAbort = OnlyOneError = false;
3601 ** Lock the control file to avoid duplicate deliveries.
3602 ** Then run the file as though we had just read it.
3603 ** We save an idea of the temporary name so we
3604 ** can recover on interrupt.
3609 /* Reset global flags */
3610 RestartRequest = NULL;
3611 RestartWorkGroup = false;
3612 ShutdownRequest = NULL;
3616 /* set basic modes, etc. */
3619 rpool = sm_rpool_new_x(NULL);
3620 clearenvelope(e, false, rpool);
3621 e->e_flags |= EF_QUEUERUN|EF_GLOBALERRS;
3622 set_delivery_mode(SM_DELIVER, e);
3623 e->e_errormode = EM_MAIL;
3627 GrabTo = UseErrorsTo = false;
3632 set_op_mode(MD_QUEUERUN);
3634 sm_setproctitle(true, e, "%s from queue", qid_printname(e));
3636 sm_syslog(LOG_DEBUG, e->e_id, "dowork, pid=%d",
3639 /* don't use the headers from sendmail.cf... */
3642 /* read the queue control file -- return if locked */
3643 if (!readqf(e, false))
3645 if (tTd(40, 4) && e->e_id != NULL)
3646 sm_dprintf("readqf(%s) failed\n",
3650 finis(false, true, EX_OK);
3653 /* adding this frees 8 bytes */
3654 clearenvelope(e, false, rpool);
3656 /* adding this frees 12 bytes */
3657 sm_rpool_free(rpool);
3663 e->e_flags |= EF_INQUEUE;
3664 eatheader(e, requeueflag, true);
3667 queueup(e, false, false);
3669 /* do the delivery */
3670 sendall(e, SM_DELIVER);
3672 /* finish up and exit */
3674 finis(true, true, ExitStat);
3677 dropenvelope(e, true, false);
3678 sm_rpool_free(rpool);
3687 ** DOWORKLIST -- process a list of envelopes as work requests
3689 ** Similar to dowork(), except that after forking, it processes an
3690 ** envelope and its siblings, treating each envelope as a work request.
3693 ** el -- envelope to be processed including its siblings.
3694 ** forkflag -- if set, run this in background.
3695 ** requeueflag -- if set, reinstantiate the queue quickly.
3696 ** This is used when expanding aliases in the queue.
3697 ** If forkflag is also set, it doesn't wait for the
3701 ** process id of process that is running the queue job.
3704 ** The work request is satisfied if possible.
3708 doworklist(el, forkflag, requeueflag)
3717 sm_dprintf("doworklist()\n");
3726 ** Since the delivery may happen in a child and the
3727 ** parent does not wait, the parent may close the
3728 ** maps thereby removing any shared memory used by
3729 ** the map. Therefore, close the maps now so the
3730 ** child will dynamically open them if necessary.
3738 syserr("doworklist: cannot fork");
3743 /* parent -- clean out connection cache */
3744 mci_flush(false, NULL);
3749 ** Initialize exception stack and default exception
3750 ** handler for child process.
3753 /* Reset global flags */
3754 RestartRequest = NULL;
3755 RestartWorkGroup = false;
3756 ShutdownRequest = NULL;
3758 CurrentPid = getpid();
3759 sm_exc_newthread(fatal_error);
3762 ** See note above about SMTP processes and SIGCHLD.
3765 if (OpMode == MD_SMTP ||
3766 OpMode == MD_DAEMON ||
3767 MaxQueueChildren > 0)
3770 sm_releasesignal(SIGCHLD);
3771 (void) sm_signal(SIGCHLD, SIG_DFL);
3774 /* child -- error messages to the transcript */
3775 QuickAbort = OnlyOneError = false;
3788 ** Lock the control file to avoid duplicate deliveries.
3789 ** Then run the file as though we had just read it.
3790 ** We save an idea of the temporary name so we
3791 ** can recover on interrupt.
3796 /* Reset global flags */
3797 RestartRequest = NULL;
3798 RestartWorkGroup = false;
3799 ShutdownRequest = NULL;
3803 /* set basic modes, etc. */
3806 GrabTo = UseErrorsTo = false;
3811 set_op_mode(MD_QUEUERUN);
3814 sm_syslog(LOG_DEBUG, el->e_id, "doworklist, pid=%d",
3817 for (ei = el; ei != NULL; ei = ei->e_sibling)
3822 if (WILL_BE_QUEUED(ei->e_sendmode))
3824 else if (QueueMode != QM_QUARANTINE &&
3825 ei->e_quarmsg != NULL)
3828 rpool = sm_rpool_new_x(NULL);
3829 clearenvelope(&e, true, rpool);
3830 e.e_flags |= EF_QUEUERUN|EF_GLOBALERRS;
3831 set_delivery_mode(SM_DELIVER, &e);
3832 e.e_errormode = EM_MAIL;
3834 e.e_qgrp = ei->e_qgrp;
3835 e.e_qdir = ei->e_qdir;
3837 sm_setproctitle(true, &e, "%s from queue", qid_printname(&e));
3839 /* don't use the headers from sendmail.cf... */
3843 /* read the queue control file -- return if locked */
3844 if (readqf(&e, false))
3846 e.e_flags |= EF_INQUEUE;
3847 eatheader(&e, requeueflag, true);
3850 queueup(&e, false, false);
3852 /* do the delivery */
3853 sendall(&e, SM_DELIVER);
3854 dropenvelope(&e, true, false);
3858 if (tTd(40, 4) && e.e_id != NULL)
3859 sm_dprintf("readqf(%s) failed\n",
3862 sm_rpool_free(rpool);
3866 /* restore CurEnv */
3869 /* finish up and exit */
3871 finis(true, true, ExitStat);
3875 ** READQF -- read queue file and set up environment.
3878 ** e -- the envelope of the job to run.
3879 ** openonly -- only open the qf (returned as e_lockfp)
3882 ** true if it successfully read the queue file.
3886 ** The queue file is returned locked.
3891 register ENVELOPE *e;
3894 register SM_FILE_T *qfp;
3896 struct stat st, stf;
3903 bool nomore = false;
3907 char qf[MAXPATHLEN];
3912 ** Read and process the file.
3915 SM_REQUIRE(e != NULL);
3917 (void) sm_strlcpy(qf, queuename(e, ANYQFL_LETTER), sizeof(qf));
3918 qfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, qf, SM_IO_RDWR_B, NULL);
3921 int save_errno = errno;
3924 sm_dprintf("readqf(%s): sm_io_open failure (%s)\n",
3925 qf, sm_errstring(errno));
3929 syserr("readqf: no control file %s", qf);
3934 if (!lockfile(sm_io_getinfo(qfp, SM_IO_WHAT_FD, NULL), qf, NULL,
3937 /* being processed by another queuer */
3939 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
3940 "%s: locked\n", e->e_id);
3942 sm_dprintf("%s: locked\n", e->e_id);
3944 sm_syslog(LOG_DEBUG, e->e_id, "locked");
3945 (void) sm_io_close(qfp, SM_TIME_DEFAULT);
3953 ** Prevent locking race condition.
3955 ** Process A: readqf(): qfp = fopen(qffile)
3956 ** Process B: queueup(): rename(tf, qf)
3957 ** Process B: unlocks(tf)
3958 ** Process A: lockfile(qf);
3960 ** Process A (us) has the old qf file (before the rename deleted
3961 ** the directory entry) and will be delivering based on old data.
3962 ** This can lead to multiple deliveries of the same recipients.
3964 ** Catch this by checking if the underlying qf file has changed
3965 ** *after* acquiring our lock and if so, act as though the file
3966 ** was still locked (i.e., just return like the lockfile() case
3970 if (stat(qf, &stf) < 0 ||
3971 fstat(sm_io_getinfo(qfp, SM_IO_WHAT_FD, NULL), &st) < 0)
3973 /* must have been being processed by someone else */
3975 sm_dprintf("readqf(%s): [f]stat failure (%s)\n",
3976 qf, sm_errstring(errno));
3977 (void) sm_io_close(qfp, SM_TIME_DEFAULT);
3981 if (st.st_nlink != stf.st_nlink ||
3982 st.st_dev != stf.st_dev ||
3983 ST_INODE(st) != ST_INODE(stf) ||
3984 #if HAS_ST_GEN && 0 /* AFS returns garbage in st_gen */
3985 st.st_gen != stf.st_gen ||
3986 #endif /* HAS_ST_GEN && 0 */
3987 st.st_uid != stf.st_uid ||
3988 st.st_gid != stf.st_gid ||
3989 st.st_size != stf.st_size)
3991 /* changed after opened */
3993 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
3994 "%s: changed\n", e->e_id);
3996 sm_dprintf("%s: changed\n", e->e_id);
3998 sm_syslog(LOG_DEBUG, e->e_id, "changed");
3999 (void) sm_io_close(qfp, SM_TIME_DEFAULT);
4004 ** Check the queue file for plausibility to avoid attacks.
4007 qsafe = S_IWOTH|S_IWGRP;
4008 if (bitset(S_IWGRP, QueueFileMode))
4011 bogus = st.st_uid != geteuid() &&
4012 st.st_uid != TrustedUid &&
4013 geteuid() != RealUid;
4016 ** If this qf file results from a set-group-ID binary, then
4017 ** we check whether the directory is group-writable,
4018 ** the queue file mode contains the group-writable bit, and
4019 ** the groups are the same.
4020 ** Notice: this requires that the set-group-ID binary is used to
4024 if (bogus && st.st_gid == getegid() && UseMSP)
4029 bp = SM_LAST_DIR_DELIM(qf);
4037 if (stat(delim == '\0' ? "." : qf, &dst) < 0)
4038 syserr("readqf: cannot stat directory %s",
4039 delim == '\0' ? "." : qf);
4042 bogus = !(bitset(S_IWGRP, QueueFileMode) &&
4043 bitset(S_IWGRP, dst.st_mode) &&
4044 dst.st_gid == st.st_gid);
4051 bogus = bitset(qsafe, st.st_mode);
4056 sm_syslog(LOG_ALERT, e->e_id,
4057 "bogus queue file, uid=%d, gid=%d, mode=%o",
4058 st.st_uid, st.st_gid, st.st_mode);
4061 sm_dprintf("readqf(%s): bogus file\n", qf);
4062 e->e_flags |= EF_INQUEUE;
4064 loseqfile(e, "bogus file uid/gid in mqueue");
4065 (void) sm_io_close(qfp, SM_TIME_DEFAULT);
4069 if (st.st_size == 0)
4071 /* must be a bogus file -- if also old, just remove it */
4072 if (!openonly && st.st_ctime + 10 * 60 < curtime())
4074 (void) xunlink(queuename(e, DATAFL_LETTER));
4075 (void) xunlink(queuename(e, ANYQFL_LETTER));
4077 (void) sm_io_close(qfp, SM_TIME_DEFAULT);
4081 if (st.st_nlink == 0)
4084 ** Race condition -- we got a file just as it was being
4085 ** unlinked. Just assume it is zero length.
4088 (void) sm_io_close(qfp, SM_TIME_DEFAULT);
4094 ** If we don't own the file mark it as unsafe.
4095 ** However, allow TrustedUser to own it as well
4096 ** in case TrustedUser manipulates the queue.
4099 if (st.st_uid != geteuid() && st.st_uid != TrustedUid)
4100 e->e_flags |= EF_UNSAFE;
4101 #else /* _FFR_TRUSTED_QF */
4102 /* If we don't own the file mark it as unsafe */
4103 if (st.st_uid != geteuid())
4104 e->e_flags |= EF_UNSAFE;
4105 #endif /* _FFR_TRUSTED_QF */
4107 /* good file -- save this lock */
4110 /* Just wanted the open file */
4114 /* do basic system initialization */
4116 macdefine(&e->e_macro, A_PERM, 'i', e->e_id);
4119 e->e_flags |= EF_GLOBALERRS;
4120 set_op_mode(MD_QUEUERUN);
4122 e->e_qfletter = queue_letter(e, ANYQFL_LETTER);
4123 e->e_dfqgrp = e->e_qgrp;
4124 e->e_dfqdir = e->e_qdir;
4125 #if _FFR_QUEUE_MACRO
4126 macdefine(&e->e_macro, A_TEMP, macid("{queue}"),
4127 qid_printqueue(e->e_qgrp, e->e_qdir));
4128 #endif /* _FFR_QUEUE_MACRO */
4131 while (bufsize = sizeof(buf),
4132 (bp = fgetfolded(buf, &bufsize, qfp)) != NULL)
4134 unsigned long qflags;
4141 sm_dprintf("+++++ %s\n", bp);
4146 syserr("SECURITY ALERT: extra or bogus data in queue file: %s",
4148 err = "bogus queue line";
4153 case 'A': /* AUTH= parameter */
4154 if (!xtextok(&bp[1]))
4156 e->e_auth_param = sm_rpool_strdup_x(e->e_rpool, &bp[1]);
4159 case 'B': /* body type */
4160 r = check_bodytype(&bp[1]);
4161 if (!BODYTYPE_VALID(r))
4163 e->e_bodytype = sm_rpool_strdup_x(e->e_rpool, &bp[1]);
4166 case 'C': /* specify controlling user */
4167 ctladdr = setctluser(&bp[1], qfver, e);
4170 case 'D': /* data file name */
4171 /* obsolete -- ignore */
4174 case 'd': /* data file directory name */
4178 #if _FFR_MSP_PARANOIA
4179 /* forbid queue groups in MSP? */
4182 #endif /* _FFR_MSP_PARANOIA */
4184 qgrp < NumQueue && Queue[qgrp] != NULL;
4188 qdir < Queue[qgrp]->qg_numqueues;
4192 Queue[qgrp]->qg_qpaths[qdir].qp_name)
4201 err = "bogus queue file directory";
4207 case 'E': /* specify error recipient */
4208 /* no longer used */
4211 case 'F': /* flag bits */
4212 if (strncmp(bp, "From ", 5) == 0)
4214 /* we are being spoofed! */
4215 syserr("SECURITY ALERT: bogus qf line %s", bp);
4216 err = "bogus queue line";
4219 for (p = &bp[1]; *p != '\0'; p++)
4223 case '8': /* has 8 bit data */
4224 e->e_flags |= EF_HAS8BIT;
4227 case 'b': /* delete Bcc: header */
4228 e->e_flags |= EF_DELETE_BCC;
4231 case 'd': /* envelope has DSN RET= */
4232 e->e_flags |= EF_RET_PARAM;
4235 case 'n': /* don't return body */
4236 e->e_flags |= EF_NO_BODY_RETN;
4239 case 'r': /* response */
4240 e->e_flags |= EF_RESPONSE;
4243 case 's': /* split */
4244 e->e_flags |= EF_SPLIT;
4247 case 'w': /* warning sent */
4248 e->e_flags |= EF_WARNING;
4254 case 'q': /* quarantine reason */
4255 e->e_quarmsg = sm_rpool_strdup_x(e->e_rpool, &bp[1]);
4256 macdefine(&e->e_macro, A_PERM,
4257 macid("{quarantine}"), e->e_quarmsg);
4260 case 'H': /* header */
4263 ** count size before chompheader() destroys the line.
4264 ** this isn't accurate due to macro expansion, but
4265 ** better than before. "-3" to skip H?? at least.
4268 hdrsize += strlen(bp) - 3;
4269 (void) chompheader(&bp[1], CHHDR_QUEUE, NULL, e);
4272 case 'I': /* data file's inode number */
4273 /* regenerated below */
4276 case 'K': /* time of last delivery attempt */
4277 e->e_dtime = atol(&buf[1]);
4280 case 'L': /* Solaris Content-Length: */
4281 case 'M': /* message */
4282 /* ignore this; we want a new message next time */
4285 case 'N': /* number of delivery attempts */
4286 e->e_ntries = atoi(&buf[1]);
4288 /* if this has been tried recently, let it be */
4290 if (e->e_ntries > 0 && e->e_dtime <= now &&
4291 now < e->e_dtime + MinQueueAge)
4295 howlong = pintvl(now - e->e_dtime, true);
4297 (void) sm_io_fprintf(smioout,
4299 "%s: too young (%s)\n",
4302 sm_dprintf("%s: too young (%s)\n",
4305 sm_syslog(LOG_DEBUG, e->e_id,
4314 macdefine(&e->e_macro, A_TEMP,
4315 macid("{ntries}"), &buf[1]);
4318 /* adjust BIND parameters immediately */
4319 if (e->e_ntries == 0)
4321 _res.retry = TimeOuts.res_retry[RES_TO_FIRST];
4322 _res.retrans = TimeOuts.res_retrans[RES_TO_FIRST];
4326 _res.retry = TimeOuts.res_retry[RES_TO_NORMAL];
4327 _res.retrans = TimeOuts.res_retrans[RES_TO_NORMAL];
4329 #endif /* NAMED_BIND */
4332 case 'P': /* message priority */
4333 e->e_msgpriority = atol(&bp[1]) + WkTimeFact;
4336 case 'Q': /* original recipient */
4337 orcpt = sm_rpool_strdup_x(e->e_rpool, &bp[1]);
4340 case 'r': /* final recipient */
4341 frcpt = sm_rpool_strdup_x(e->e_rpool, &bp[1]);
4344 case 'R': /* specify recipient */
4350 while (*++p != '\0' && *p != ':')
4355 qflags |= QHASNOTIFY;
4359 qflags |= QPINGONSUCCESS;
4363 qflags |= QPINGONFAILURE;
4367 qflags |= QPINGONDELAY;
4375 if (ctladdr != NULL)
4376 ctladdr->q_flags |= QALIAS;
4379 default: /* ignore or complain? */
4386 macdefine(&e->e_macro, A_PERM, macid("{addr_type}"),
4389 q = parseaddr(++p, NULLADDR, RF_COPYALL, '\0',
4395 /* make sure we keep the current qgrp */
4396 if (ISVALIDQGRP(e->e_qgrp))
4397 q->q_qgrp = e->e_qgrp;
4398 q->q_alias = ctladdr;
4400 q->q_flags &= ~Q_PINGFLAGS;
4401 q->q_flags |= qflags;
4402 q->q_finalrcpt = frcpt;
4404 (void) recipient(q, &e->e_sendqueue, 0, e);
4408 macdefine(&e->e_macro, A_PERM, macid("{addr_type}"),
4412 case 'S': /* sender */
4413 setsender(sm_rpool_strdup_x(e->e_rpool, &bp[1]),
4414 e, NULL, '\0', true);
4417 case 'T': /* init time */
4418 e->e_ctime = atol(&bp[1]);
4421 case 'V': /* queue file version number */
4422 qfver = atoi(&bp[1]);
4423 if (qfver <= QF_VERSION)
4425 syserr("Version number in queue file (%d) greater than max (%d)",
4427 err = "unsupported queue file version";
4432 case 'Z': /* original envelope id from ESMTP */
4433 e->e_envid = sm_rpool_strdup_x(e->e_rpool, &bp[1]);
4434 macdefine(&e->e_macro, A_PERM,
4435 macid("{dsn_envid}"), e->e_envid);
4438 case '!': /* deliver by */
4440 /* format: flag (1 char) space long-integer */
4441 e->e_dlvr_flag = buf[1];
4442 e->e_deliver_by = strtol(&buf[3], NULL, 10);
4444 case '$': /* define macro */
4448 /* XXX elimate p? */
4449 r = macid_parse(&bp[1], &ep);
4452 p = sm_rpool_strdup_x(e->e_rpool, ep);
4453 macdefine(&e->e_macro, A_PERM, r, p);
4457 case '.': /* terminate file */
4466 ** Maintain backward compatibility for
4467 ** users who defined _FFR_QUEUEDELAY in
4468 ** previous releases. Remove this
4469 ** code in 8.14 or 8.15.
4472 if (qfver == 5 || qfver == 7)
4475 /* If not qfver 5 or 7, then 'G' or 'Y' is invalid */
4477 #endif /* _FFR_QUEUEDELAY */
4480 syserr("readqf: %s: line %d: bad line \"%s\"",
4481 qf, LineNumber, shortenstring(bp, MAXSHORTSTR));
4482 err = "unrecognized line";
4491 ** If we haven't read any lines, this queue file is empty.
4492 ** Arrange to remove it without referencing any null pointers.
4495 if (LineNumber == 0)
4498 e->e_flags |= EF_CLRQUEUE|EF_FATALERRS|EF_RESPONSE;
4502 /* Check to make sure we have a complete queue file read */
4505 syserr("readqf: %s: incomplete queue file read", qf);
4506 (void) sm_io_close(qfp, SM_TIME_DEFAULT);
4510 #if _FFR_QF_PARANOIA
4511 /* Check to make sure key fields were read */
4512 if (e->e_from.q_mailer == NULL)
4514 syserr("readqf: %s: sender not specified in queue file", qf);
4515 (void) sm_io_close(qfp, SM_TIME_DEFAULT);
4519 #endif /* _FFR_QF_PARANOIA */
4521 /* possibly set ${dsn_ret} macro */
4522 if (bitset(EF_RET_PARAM, e->e_flags))
4524 if (bitset(EF_NO_BODY_RETN, e->e_flags))
4525 macdefine(&e->e_macro, A_PERM,
4526 macid("{dsn_ret}"), "hdrs");
4528 macdefine(&e->e_macro, A_PERM,
4529 macid("{dsn_ret}"), "full");
4533 ** Arrange to read the data file.
4536 p = queuename(e, DATAFL_LETTER);
4537 e->e_dfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, p, SM_IO_RDONLY_B,
4539 if (e->e_dfp == NULL)
4541 syserr("readqf: cannot open %s", p);
4545 e->e_flags |= EF_HAS_DF;
4546 if (fstat(sm_io_getinfo(e->e_dfp, SM_IO_WHAT_FD, NULL), &st)
4549 e->e_msgsize = st.st_size + hdrsize;
4550 e->e_dfdev = st.st_dev;
4551 e->e_dfino = ST_INODE(st);
4552 (void) sm_snprintf(buf, sizeof(buf), "%ld",
4554 macdefine(&e->e_macro, A_TEMP, macid("{msg_size}"),
4563 ** There was some error reading the qf file (reason is in err var.)
4565 ** close file; clear e_lockfp since it is the same as qfp,
4566 ** hence it is invalid (as file) after qfp is closed;
4567 ** the qf file is on disk, so set the flag to avoid calling
4568 ** queueup() with bogus data.
4574 (void) sm_io_close(qfp, SM_TIME_DEFAULT);
4576 e->e_flags |= EF_INQUEUE;
4581 ** PRTSTR -- print a string, "unprintable" characters are shown as \oct
4584 ** s -- string to print
4585 ** ml -- maximum length of output
4588 ** number of entries
4591 ** Prints a string on stdout.
4594 static void prtstr __P((char *, int));
4605 while (ml-- > 0 && ((c = *s++) != '\0'))
4611 (void) sm_io_putc(smioout, SM_TIME_DEFAULT, c);
4612 (void) sm_io_putc(smioout, SM_TIME_DEFAULT, c);
4615 else if (isascii(c) && isprint(c))
4616 (void) sm_io_putc(smioout, SM_TIME_DEFAULT, c);
4620 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
4621 "\\%03o", c & 0xFF);
4626 ** PRINTNQE -- print out number of entries in the mail queue
4629 ** out -- output file pointer.
4630 ** prefix -- string to output in front of each line.
4637 printnqe(out, prefix)
4642 int i, k = 0, nrequests = 0;
4643 bool unknown = false;
4645 if (ShmId == SM_SHM_NO_ID)
4648 (void) sm_io_fprintf(out, SM_TIME_DEFAULT,
4649 "Data unavailable: shared memory not updated\n");
4651 (void) sm_io_fprintf(out, SM_TIME_DEFAULT,
4652 "%sNOTCONFIGURED:-1\r\n", prefix);
4655 for (i = 0; i < NumQueue && Queue[i] != NULL; i++)
4660 for (j = 0; j < Queue[i]->qg_numqueues; j++)
4667 n = QSHM_ENTRIES(Queue[i]->qg_qpaths[j].qp_idx);
4669 (void) sm_io_fprintf(out, SM_TIME_DEFAULT,
4671 prefix, qid_printqueue(i, j), n);
4674 (void) sm_io_fprintf(out, SM_TIME_DEFAULT,
4675 "%s: unknown number of entries\n",
4676 qid_printqueue(i, j));
4681 (void) sm_io_fprintf(out, SM_TIME_DEFAULT,
4683 qid_printqueue(i, j));
4687 (void) sm_io_fprintf(out, SM_TIME_DEFAULT,
4689 qid_printqueue(i, j), n);
4695 if (prefix == NULL && k > 1)
4696 (void) sm_io_fprintf(out, SM_TIME_DEFAULT,
4697 "\t\tTotal requests: %d%s\n",
4698 nrequests, unknown ? " (about)" : "");
4699 #else /* SM_CONF_SHM */
4701 (void) sm_io_fprintf(out, SM_TIME_DEFAULT,
4702 "Data unavailable without shared memory support\n");
4704 (void) sm_io_fprintf(out, SM_TIME_DEFAULT,
4705 "%sNOTAVAILABLE:-1\r\n", prefix);
4706 #endif /* SM_CONF_SHM */
4709 ** PRINTQUEUE -- print out a representation of the mail queue
4718 ** Prints a listing of the mail queue on the standard output.
4724 int i, k = 0, nrequests = 0;
4726 for (i = 0; i < NumQueue && Queue[i] != NULL; i++)
4731 for (j = 0; j < Queue[i]->qg_numqueues; j++)
4735 nrequests += print_single_queue(i, j);
4740 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
4741 "\t\tTotal requests: %d\n",
4745 ** PRINT_SINGLE_QUEUE -- print out a representation of a single mail queue
4748 ** qgrp -- the index of the queue group.
4749 ** qdir -- the queue directory.
4752 ** number of requests in mail queue.
4755 ** Prints a listing of the mail queue on the standard output.
4759 print_single_queue(qgrp, qdir)
4766 char qd[MAXPATHLEN];
4767 char qddf[MAXPATHLEN];
4772 (void) sm_strlcpy(qd, ".", sizeof(qd));
4773 (void) sm_strlcpy(qddf, ".", sizeof(qddf));
4777 (void) sm_strlcpyn(qd, sizeof(qd), 2,
4778 Queue[qgrp]->qg_qpaths[qdir].qp_name,
4780 Queue[qgrp]->qg_qpaths[qdir].qp_subdirs)
4782 (void) sm_strlcpyn(qddf, sizeof(qddf), 2,
4783 Queue[qgrp]->qg_qpaths[qdir].qp_name,
4785 Queue[qgrp]->qg_qpaths[qdir].qp_subdirs)
4790 ** Check for permission to print the queue
4793 if (bitset(PRIV_RESTRICTMAILQ, PrivacyFlags) && RealUid != 0)
4798 extern GIDSET_T InitialGidSet[NGROUPS_MAX];
4799 #endif /* NGROUPS_MAX */
4801 if (stat(qd, &st) < 0)
4803 syserr("Cannot stat %s",
4804 qid_printqueue(qgrp, qdir));
4811 if (InitialGidSet[n] == st.st_gid)
4814 if (n < 0 && RealGid != st.st_gid)
4815 #else /* NGROUPS_MAX */
4816 if (RealGid != st.st_gid)
4817 #endif /* NGROUPS_MAX */
4819 usrerr("510 You are not permitted to see the queue");
4826 ** Read and order the queue.
4829 nrequests = gatherq(qgrp, qdir, true, NULL, NULL);
4830 (void) sortq(Queue[qgrp]->qg_maxlist);
4833 ** Print the work list that we have read.
4836 /* first see if there is anything */
4839 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "%s is empty\n",
4840 qid_printqueue(qgrp, qdir));
4844 sm_getla(); /* get load average */
4846 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "\t\t%s (%d request%s",
4847 qid_printqueue(qgrp, qdir),
4848 nrequests, nrequests == 1 ? "" : "s");
4849 if (MaxQueueRun > 0 && nrequests > MaxQueueRun)
4850 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
4851 ", only %d printed", MaxQueueRun);
4853 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
4854 ")\n-----Q-ID----- --Size-- -Priority- ---Q-Time--- --------Sender/Recipient--------\n");
4856 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
4857 ")\n-----Q-ID----- --Size-- -----Q-Time----- ------------Sender/Recipient-----------\n");
4858 for (w = WorkQ; w != NULL; w = w->w_next)
4861 auto time_t submittime = 0;
4865 char quarmsg[MAXLINE];
4866 char statmsg[MAXLINE];
4867 char bodytype[MAXNAME + 1];
4868 char qf[MAXPATHLEN];
4873 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "%13s",
4875 (void) sm_strlcpyn(qf, sizeof(qf), 3, qd, "/", w->w_name);
4876 f = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, qf, SM_IO_RDONLY_B,
4881 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
4882 " (permission denied)\n");
4883 else if (errno == ENOENT)
4884 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
4885 " (job completed)\n");
4887 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
4889 sm_errstring(errno));
4893 w->w_name[0] = DATAFL_LETTER;
4894 (void) sm_strlcpyn(qf, sizeof(qf), 3, qddf, "/", w->w_name);
4895 if (stat(qf, &st) >= 0)
4896 dfsize = st.st_size;
4902 ** Maybe the df file can't be statted because
4903 ** it is in a different directory than the qf file.
4904 ** In order to find out, we must read the qf file.
4907 newenvelope(&e, &BlankEnvelope, sm_rpool_new_x(NULL));
4908 e.e_id = w->w_name + 2;
4912 if (readqf(&e, false))
4914 char *df = queuename(&e, DATAFL_LETTER);
4915 if (stat(df, &st) >= 0)
4916 dfsize = st.st_size;
4918 if (e.e_lockfp != NULL)
4920 (void) sm_io_close(e.e_lockfp, SM_TIME_DEFAULT);
4923 clearenvelope(&e, false, e.e_rpool);
4924 sm_rpool_free(e.e_rpool);
4927 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "*");
4928 else if (QueueMode == QM_LOST)
4929 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "?");
4930 else if (w->w_tooyoung)
4931 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "-");
4932 else if (shouldqueue(w->w_pri, w->w_ctime))
4933 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "X");
4935 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, " ");
4940 statmsg[0] = bodytype[0] = '\0';
4942 while (sm_io_fgets(f, SM_TIME_DEFAULT, buf, sizeof(buf)) != NULL)
4953 case 'V': /* queue file version */
4954 qfver = atoi(&buf[1]);
4957 case 'M': /* error message */
4958 if ((i = strlen(&buf[1])) >= sizeof(statmsg))
4959 i = sizeof(statmsg) - 1;
4960 memmove(statmsg, &buf[1], i);
4964 case 'q': /* quarantine reason */
4965 if ((i = strlen(&buf[1])) >= sizeof(quarmsg))
4966 i = sizeof(quarmsg) - 1;
4967 memmove(quarmsg, &buf[1], i);
4971 case 'B': /* body type */
4972 if ((i = strlen(&buf[1])) >= sizeof(bodytype))
4973 i = sizeof(bodytype) - 1;
4974 memmove(bodytype, &buf[1], i);
4978 case 'S': /* sender name */
4981 (void) sm_io_fprintf(smioout,
4983 "%8ld %10ld%c%.12s ",
4986 bitset(EF_WARNING, flags)
4988 ctime(&submittime) + 4);
4989 prtstr(&buf[1], 78);
4993 (void) sm_io_fprintf(smioout,
4997 ctime(&submittime));
4998 prtstr(&buf[1], 39);
5001 if (quarmsg[0] != '\0')
5003 (void) sm_io_fprintf(smioout,
5005 "\n QUARANTINE: %.*s",
5011 if (statmsg[0] != '\0' || bodytype[0] != '\0')
5013 (void) sm_io_fprintf(smioout,
5017 if (statmsg[0] != '\0')
5018 (void) sm_io_fprintf(smioout,
5027 case 'C': /* controlling user */
5029 (void) sm_io_fprintf(smioout,
5031 "\n\t\t\t\t\t\t(---%.64s---)",
5035 case 'R': /* recipient name */
5046 (void) sm_io_fprintf(smioout,
5053 (void) sm_io_fprintf(smioout,
5058 if (Verbose && statmsg[0] != '\0')
5060 (void) sm_io_fprintf(smioout,
5068 case 'T': /* creation time */
5069 submittime = atol(&buf[1]);
5072 case 'F': /* flag bits */
5073 for (p = &buf[1]; *p != '\0'; p++)
5078 flags |= EF_WARNING;
5084 if (submittime == (time_t) 0)
5085 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
5086 " (no control file)");
5087 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "\n");
5088 (void) sm_io_close(f, SM_TIME_DEFAULT);
5094 ** QUEUE_LETTER -- get the proper queue letter for the current QueueMode.
5097 ** e -- envelope to build it in/from.
5098 ** type -- the file type, used as the first character
5099 ** of the file name.
5102 ** the letter to use
5106 queue_letter(e, type)
5110 /* Change type according to QueueMode */
5111 if (type == ANYQFL_LETTER)
5113 if (e->e_quarmsg != NULL)
5114 type = QUARQF_LETTER;
5120 type = NORMQF_LETTER;
5124 type = QUARQF_LETTER;
5128 type = LOSEQF_LETTER;
5132 /* should never happen */
5142 ** QUEUENAME -- build a file name in the queue directory for this envelope.
5145 ** e -- envelope to build it in/from.
5146 ** type -- the file type, used as the first character
5147 ** of the file name.
5150 ** a pointer to the queue name (in a static buffer).
5153 ** If no id code is already assigned, queuename() will
5154 ** assign an id code with assign_queueid(). If no queue
5155 ** directory is assigned, one will be set with setnewqueue().
5160 register ENVELOPE *e;
5166 static char buf[MAXPATHLEN];
5168 /* Assign an ID if needed */
5169 if (e->e_id == NULL)
5171 type = queue_letter(e, type);
5173 /* begin of filename */
5174 pref[0] = (char) type;
5178 /* Assign a queue group/directory if needed */
5179 if (type == XSCRPT_LETTER)
5182 ** We don't want to call setnewqueue() if we are fetching
5183 ** the pathname of the transcript file, because setnewqueue
5184 ** chooses a queue, and sometimes we need to write to the
5185 ** transcript file before we have gathered enough information
5186 ** to choose a queue.
5189 if (e->e_xfqgrp == NOQGRP || e->e_xfqdir == NOQDIR)
5191 if (e->e_qgrp != NOQGRP && e->e_qdir != NOQDIR)
5193 e->e_xfqgrp = e->e_qgrp;
5194 e->e_xfqdir = e->e_qdir;
5199 if (Queue[e->e_xfqgrp]->qg_numqueues <= 1)
5203 e->e_xfqdir = get_rand_mod(
5204 Queue[e->e_xfqgrp]->qg_numqueues);
5213 if (e->e_qgrp == NOQGRP || e->e_qdir == NOQDIR)
5214 (void) setnewqueue(e);
5215 if (type == DATAFL_LETTER)
5227 /* xf files always have a valid qd and qg picked above */
5228 if ((qd == NOQDIR || qg == NOQGRP) && type != XSCRPT_LETTER)
5229 (void) sm_strlcpyn(buf, sizeof(buf), 2, pref, e->e_id);
5235 if (bitset(QP_SUBDF, Queue[qg]->qg_qpaths[qd].qp_subdirs))
5244 if (bitset(QP_SUBQF, Queue[qg]->qg_qpaths[qd].qp_subdirs))
5249 if (bitset(QP_SUBXF, Queue[qg]->qg_qpaths[qd].qp_subdirs))
5254 sm_abort("queuename: bad queue file type %d", type);
5257 (void) sm_strlcpyn(buf, sizeof(buf), 4,
5258 Queue[qg]->qg_qpaths[qd].qp_name,
5259 sub, pref, e->e_id);
5263 sm_dprintf("queuename: %s\n", buf);
5268 ** INIT_QID_ALG -- Initialize the (static) parameters that are used to
5269 ** generate a queue ID.
5271 ** This function is called by the daemon to reset
5272 ** LastQueueTime and LastQueuePid which are used by assign_queueid().
5273 ** Otherwise the algorithm may cause problems because
5274 ** LastQueueTime and LastQueuePid are set indirectly by main()
5275 ** before the daemon process is started, hence LastQueuePid is not
5276 ** the pid of the daemon and therefore a child of the daemon can
5277 ** actually have the same pid as LastQueuePid which means the section
5278 ** in assign_queueid():
5279 ** * see if we need to get a new base time/pid *
5280 ** is NOT triggered which will cause the same queue id to be generated.
5297 ** ASSIGN_QUEUEID -- assign a queue ID for this envelope.
5299 ** Assigns an id code if one does not already exist.
5300 ** This code assumes that nothing will remain in the queue for
5301 ** longer than 60 years. It is critical that files with the given
5302 ** name do not already exist in the queue.
5303 ** [No longer initializes e_qdir to NOQDIR.]
5306 ** e -- envelope to set it in.
5312 static const char QueueIdChars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
5314 # define QIC_LEN_R 62
5317 ** Note: the length is "officially" 60 because minutes and seconds are
5318 ** usually only 0-59. However (Linux):
5319 ** tm_sec The number of seconds after the minute, normally in
5320 ** the range 0 to 59, but can be up to 61 to allow for
5322 ** Hence the real length of the string is 62 to take this into account.
5323 ** Alternatively % QIC_LEN can (should) be used for access everywhere.
5326 # define queuenextid() CurrentPid
5331 register ENVELOPE *e;
5333 pid_t pid = queuenextid();
5335 static long random_offset;
5337 char idbuf[MAXQFNAME - 2];
5340 if (e->e_id != NULL)
5343 /* see if we need to get a new base time/pid */
5344 if (cX >= QIC_LEN * QIC_LEN || LastQueueTime == 0 ||
5345 LastQueuePid != pid)
5347 time_t then = LastQueueTime;
5349 /* if the first time through, pick a random offset */
5350 if (LastQueueTime == 0)
5351 random_offset = get_random();
5353 while ((LastQueueTime = curtime()) == then &&
5354 LastQueuePid == pid)
5358 LastQueuePid = queuenextid();
5363 ** Generate a new sequence number between 0 and QIC_LEN*QIC_LEN-1.
5364 ** This lets us generate up to QIC_LEN*QIC_LEN unique queue ids
5365 ** per second, per process. With envelope splitting,
5366 ** a single message can consume many queue ids.
5369 seq = (int)((cX + random_offset) % (QIC_LEN * QIC_LEN));
5372 sm_dprintf("assign_queueid: random_offset = %ld (%d)\n",
5373 random_offset, seq);
5375 tm = gmtime(&LastQueueTime);
5376 idbuf[0] = QueueIdChars[tm->tm_year % QIC_LEN];
5377 idbuf[1] = QueueIdChars[tm->tm_mon];
5378 idbuf[2] = QueueIdChars[tm->tm_mday];
5379 idbuf[3] = QueueIdChars[tm->tm_hour];
5380 idbuf[4] = QueueIdChars[tm->tm_min % QIC_LEN_R];
5381 idbuf[5] = QueueIdChars[tm->tm_sec % QIC_LEN_R];
5382 idbuf[6] = QueueIdChars[seq / QIC_LEN];
5383 idbuf[7] = QueueIdChars[seq % QIC_LEN];
5384 (void) sm_snprintf(&idbuf[8], sizeof(idbuf) - 8, "%06d",
5385 (int) LastQueuePid);
5386 e->e_id = sm_rpool_strdup_x(e->e_rpool, idbuf);
5387 macdefine(&e->e_macro, A_PERM, 'i', e->e_id);
5389 /* XXX: inherited from MainEnvelope */
5390 e->e_qgrp = NOQGRP; /* too early to do anything else */
5392 e->e_xfqgrp = NOQGRP;
5395 /* New ID means it's not on disk yet */
5396 e->e_qfletter = '\0';
5399 sm_dprintf("assign_queueid: assigned id %s, e=%p\n",
5402 sm_syslog(LOG_DEBUG, e->e_id, "assigned id");
5405 ** SYNC_QUEUE_TIME -- Assure exclusive PID in any given second
5407 ** Make sure one PID can't be used by two processes in any one second.
5409 ** If the system rotates PIDs fast enough, may get the
5410 ** same pid in the same second for two distinct processes.
5411 ** This will interfere with the queue file naming system.
5423 #if FAST_PID_RECYCLE
5424 if (OpMode != MD_TEST &&
5425 OpMode != MD_VERIFY &&
5426 LastQueueTime > 0 &&
5427 LastQueuePid == CurrentPid &&
5428 curtime() == LastQueueTime)
5430 #endif /* FAST_PID_RECYCLE */
5433 ** UNLOCKQUEUE -- unlock the queue entry for a specified envelope
5436 ** e -- the envelope to unlock.
5442 ** unlocks the queue for `e'.
5450 sm_dprintf("unlockqueue(%s)\n",
5451 e->e_id == NULL ? "NOQUEUE" : e->e_id);
5454 /* if there is a lock file in the envelope, close it */
5455 if (e->e_lockfp != NULL)
5456 (void) sm_io_close(e->e_lockfp, SM_TIME_DEFAULT);
5459 /* don't create a queue id if we don't already have one */
5460 if (e->e_id == NULL)
5463 /* remove the transcript */
5465 sm_syslog(LOG_DEBUG, e->e_id, "unlock");
5467 (void) xunlink(queuename(e, XSCRPT_LETTER));
5470 ** SETCTLUSER -- create a controlling address
5472 ** Create a fake "address" given only a local login name; this is
5473 ** used as a "controlling user" for future recipient addresses.
5476 ** user -- the user name of the controlling user.
5477 ** qfver -- the version stamp of this queue file.
5481 ** An address descriptor for the controlling user,
5482 ** using storage allocated from e->e_rpool.
5487 setctluser(user, qfver, e)
5492 register ADDRESS *a;
5497 ** See if this clears our concept of controlling user.
5500 if (user == NULL || *user == '\0')
5504 ** Set up addr fields for controlling user.
5507 a = (ADDRESS *) sm_rpool_malloc_x(e->e_rpool, sizeof(*a));
5508 memset((char *) a, '\0', sizeof(*a));
5513 a->q_user = sm_rpool_strdup_x(e->e_rpool, p);
5517 p = strtok(user, ":");
5518 a->q_user = sm_rpool_strdup_x(e->e_rpool, user);
5521 if ((p = strtok(NULL, ":")) != NULL)
5523 if ((p = strtok(NULL, ":")) != NULL)
5525 if ((p = strtok(NULL, ":")) != NULL)
5529 a->q_flags |= QGOODUID;
5531 /* if there is another ':': restore it */
5532 if ((o = strtok(NULL, ":")) != NULL && o > p)
5536 else if ((pw = sm_getpwnam(user)) != NULL)
5538 if (*pw->pw_dir == '\0')
5540 else if (strcmp(pw->pw_dir, "/") == 0)
5543 a->q_home = sm_rpool_strdup_x(e->e_rpool, pw->pw_dir);
5544 a->q_uid = pw->pw_uid;
5545 a->q_gid = pw->pw_gid;
5546 a->q_flags |= QGOODUID;
5550 a->q_flags |= QPRIMARY; /* flag as a "ctladdr" */
5551 a->q_mailer = LocalMailer;
5553 a->q_paddr = sm_rpool_strdup_x(e->e_rpool, a->q_user);
5555 a->q_paddr = sm_rpool_strdup_x(e->e_rpool, p);
5559 ** LOSEQFILE -- rename queue file with LOSEQF_LETTER & try to let someone know
5562 ** e -- the envelope (e->e_id will be used).
5563 ** why -- reported to whomever can hear.
5571 register ENVELOPE *e;
5576 char buf[MAXPATHLEN];
5578 if (e == NULL || e->e_id == NULL)
5580 p = queuename(e, ANYQFL_LETTER);
5581 if (sm_strlcpy(buf, p, sizeof(buf)) >= sizeof(buf))
5583 if (!bitset(EF_INQUEUE, e->e_flags))
5584 queueup(e, false, true);
5585 else if (QueueMode == QM_LOST)
5588 /* if already lost, no need to re-lose */
5591 p = queuename(e, LOSEQF_LETTER);
5592 if (rename(buf, p) < 0)
5593 syserr("cannot rename(%s, %s), uid=%d",
5594 buf, p, (int) geteuid());
5595 else if (LogLevel > 0)
5596 sm_syslog(LOG_ALERT, e->e_id,
5597 "Losing %s: %s", buf, why);
5599 if (e->e_dfp != NULL)
5601 (void) sm_io_close(e->e_dfp, SM_TIME_DEFAULT);
5604 e->e_flags &= ~EF_HAS_DF;
5607 ** NAME2QID -- translate a queue group name to a queue group id
5610 ** queuename -- name of queue group.
5613 ** queue group id if found.
5614 ** NOQGRP otherwise.
5623 s = stab(queuename, ST_QUEUE, ST_FIND);
5626 return s->s_quegrp->qg_index;
5629 ** QID_PRINTNAME -- create externally printable version of queue id
5632 ** e -- the envelope.
5635 ** a printable version
5643 static char idbuf[MAXQFNAME + 34];
5648 if (e->e_id == NULL)
5653 if (e->e_qdir == NOQDIR)
5656 (void) sm_snprintf(idbuf, sizeof(idbuf), "%.32s/%s",
5657 Queue[e->e_qgrp]->qg_qpaths[e->e_qdir].qp_name,
5662 ** QID_PRINTQUEUE -- create full version of queue directory for data files
5665 ** qgrp -- index in queue group.
5666 ** qdir -- the short version of the queue directory
5669 ** the full pathname to the queue (might point to a static var)
5673 qid_printqueue(qgrp, qdir)
5678 static char dir[MAXPATHLEN];
5681 return Queue[qgrp]->qg_qdir;
5683 if (strcmp(Queue[qgrp]->qg_qpaths[qdir].qp_name, ".") == 0)
5686 subdir = Queue[qgrp]->qg_qpaths[qdir].qp_name;
5688 (void) sm_strlcpyn(dir, sizeof(dir), 4,
5689 Queue[qgrp]->qg_qdir,
5690 subdir == NULL ? "" : "/",
5691 subdir == NULL ? "" : subdir,
5693 Queue[qgrp]->qg_qpaths[qdir].qp_subdirs)
5699 ** PICKQDIR -- Pick a queue directory from a queue group
5702 ** qg -- queue group
5703 ** fsize -- file size in bytes
5704 ** e -- envelope, or NULL
5707 ** NOQDIR if no queue directory in qg has enough free space to
5708 ** hold a file of size 'fsize', otherwise the index of
5709 ** a randomly selected queue directory which resides on a
5710 ** file system with enough disk space.
5711 ** XXX This could be extended to select a queuedir with
5712 ** a few (the fewest?) number of entries. That data
5713 ** is available if shared memory is used.
5716 ** If the request fails and e != NULL then sm_syslog is called.
5720 pickqdir(qg, fsize, e)
5729 /* Pick a random directory, as a starting point. */
5730 if (qg->qg_numqueues <= 1)
5733 qdir = get_rand_mod(qg->qg_numqueues);
5735 if (MinBlocksFree <= 0 && fsize <= 0)
5739 ** Now iterate over the queue directories,
5740 ** looking for a directory with enough space for this message.
5746 QPATHS *qp = &qg->qg_qpaths[i];
5751 needed += fsize / FILE_SYS_BLKSIZE(qp->qp_fsysidx)
5752 + ((fsize % FILE_SYS_BLKSIZE(qp->qp_fsysidx)
5754 if (MinBlocksFree > 0)
5755 needed += MinBlocksFree;
5756 fsavail = FILE_SYS_AVAIL(qp->qp_fsysidx);
5763 ** might be not correctly updated,
5764 ** let's try to get the info directly.
5767 fsavail = freediskspace(FILE_SYS_NAME(qp->qp_fsysidx),
5772 #endif /* SM_CONF_SHM */
5773 if (needed <= fsavail)
5775 if (avail < fsavail)
5778 if (qg->qg_numqueues > 0)
5779 i = (i + 1) % qg->qg_numqueues;
5780 } while (i != qdir);
5782 if (e != NULL && LogLevel > 0)
5783 sm_syslog(LOG_ALERT, e->e_id,
5784 "low on space (%s needs %ld bytes + %ld blocks in %s), max avail: %ld",
5785 CurHostName == NULL ? "SMTP-DAEMON" : CurHostName,
5786 fsize, MinBlocksFree,
5787 qg->qg_qdir, avail);
5791 ** SETNEWQUEUE -- Sets a new queue group and directory
5793 ** Assign a queue group and directory to an envelope and store the
5794 ** directory in e->e_qdir.
5797 ** e -- envelope to assign a queue for.
5800 ** true if successful
5804 ** On success, e->e_qgrp and e->e_qdir are non-negative.
5805 ** On failure (not enough disk space),
5806 ** e->qgrp = NOQGRP, e->e_qdir = NOQDIR
5807 ** and usrerr() is invoked (which could raise an exception).
5815 sm_dprintf("setnewqueue: called\n");
5817 /* not set somewhere else */
5818 if (e->e_qgrp == NOQGRP)
5823 ** Use the queue group of the "first" recipient, as set by
5824 ** the "queuegroup" rule set. If that is not defined, then
5825 ** use the queue group of the mailer of the first recipient.
5826 ** If that is not defined either, then use the default
5828 ** Notice: "first" depends on the sorting of sendqueue
5830 ** To avoid problems with "bad" recipients look
5831 ** for a valid address first.
5836 (QS_IS_BADADDR(q->q_state) || QS_IS_DEAD(q->q_state)))
5842 else if (q->q_qgrp >= 0)
5843 e->e_qgrp = q->q_qgrp;
5844 else if (q->q_mailer != NULL &&
5845 ISVALIDQGRP(q->q_mailer->m_qgrp))
5846 e->e_qgrp = q->q_mailer->m_qgrp;
5849 e->e_dfqgrp = e->e_qgrp;
5852 if (ISVALIDQDIR(e->e_qdir) && ISVALIDQDIR(e->e_dfqdir))
5855 sm_dprintf("setnewqueue: e_qdir already assigned (%s)\n",
5856 qid_printqueue(e->e_qgrp, e->e_qdir));
5861 e->e_qdir = pickqdir(Queue[e->e_qgrp], e->e_msgsize, e);
5862 if (e->e_qdir == NOQDIR)
5865 if (!bitset(EF_FATALERRS, e->e_flags))
5866 usrerr("452 4.4.5 Insufficient disk space; try again later");
5867 e->e_flags |= EF_FATALERRS;
5872 sm_dprintf("setnewqueue: Assigned queue directory %s\n",
5873 qid_printqueue(e->e_qgrp, e->e_qdir));
5875 if (e->e_xfqgrp == NOQGRP || e->e_xfqdir == NOQDIR)
5877 e->e_xfqgrp = e->e_qgrp;
5878 e->e_xfqdir = e->e_qdir;
5880 e->e_dfqdir = e->e_qdir;
5884 ** CHKQDIR -- check a queue directory
5887 ** name -- name of queue directory
5888 ** sff -- flags for safefile()
5891 ** is it a queue directory?
5894 static bool chkqdir __P((char *, long));
5904 /* skip over . and .. directories */
5905 if (name[0] == '.' &&
5906 (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')))
5909 if (lstat(name, &statb) < 0)
5910 #else /* HASLSTAT */
5911 if (stat(name, &statb) < 0)
5912 #endif /* HASLSTAT */
5915 sm_dprintf("chkqdir: stat(\"%s\"): %s\n",
5916 name, sm_errstring(errno));
5920 if (S_ISLNK(statb.st_mode))
5923 ** For a symlink we need to make sure the
5924 ** target is a directory
5927 if (stat(name, &statb) < 0)
5930 sm_dprintf("chkqdir: stat(\"%s\"): %s\n",
5931 name, sm_errstring(errno));
5935 #endif /* HASLSTAT */
5937 if (!S_ISDIR(statb.st_mode))
5940 sm_dprintf("chkqdir: \"%s\": Not a directory\n",
5945 /* Print a warning if unsafe (but still use it) */
5946 /* XXX do this only if we want the warning? */
5947 i = safedirpath(name, RunAsUid, RunAsGid, NULL, sff, 0, 0);
5951 sm_dprintf("chkqdir: \"%s\": Not safe: %s\n",
5952 name, sm_errstring(i));
5955 sm_syslog(LOG_WARNING, NOQID,
5956 "queue directory \"%s\": Not safe: %s",
5957 name, sm_errstring(i));
5958 #endif /* _FFR_CHK_QUEUE */
5963 ** MULTIQUEUE_CACHE -- cache a list of paths to queues.
5965 ** Each potential queue is checked as the cache is built.
5966 ** Thereafter, each is blindly trusted.
5967 ** Note that we can be called again after a timeout to rebuild
5968 ** (although code for that is not ready yet).
5971 ** basedir -- base of all queue directories.
5972 ** blen -- strlen(basedir).
5973 ** qg -- queue group.
5974 ** qn -- number of queue directories already cached.
5975 ** phash -- pointer to hash value over queue dirs.
5977 ** only used if shared memory is active.
5978 #endif * SM_CONF_SHM *
5981 ** new number of queue directories.
5984 #define INITIAL_SLOTS 20
5985 #define ADD_SLOTS 10
5988 multiqueue_cache(basedir, blen, qg, qn, phash)
5993 unsigned int *phash;
5998 long sff = SFF_ANYFILE;
5999 char qpath[MAXPATHLEN];
6000 char subdir[MAXPATHLEN];
6001 char prefix[MAXPATHLEN]; /* dir relative to basedir */
6004 sm_dprintf("multiqueue_cache: called\n");
6006 /* Initialize to current directory */
6009 if (qg->qg_numqueues != 0 && qg->qg_qpaths != NULL)
6011 for (i = 0; i < qg->qg_numqueues; i++)
6013 if (qg->qg_qpaths[i].qp_name != NULL)
6014 (void) sm_free(qg->qg_qpaths[i].qp_name); /* XXX */
6016 (void) sm_free((char *) qg->qg_qpaths); /* XXX */
6017 qg->qg_qpaths = NULL;
6018 qg->qg_numqueues = 0;
6021 /* If running as root, allow safedirpath() checks to use privs */
6025 sff |= SFF_SAFEDIRPATH|SFF_NOWWFILES;
6027 sff |= SFF_NOGWFILES;
6028 #endif /* _FFR_CHK_QUEUE */
6030 if (!SM_IS_DIR_START(qg->qg_qdir))
6033 ** XXX we could add basedir, but then we have to realloc()
6034 ** the string... Maybe another time.
6037 syserr("QueuePath %s not absolute", qg->qg_qdir);
6038 ExitStat = EX_CONFIG;
6042 /* qpath: directory of current workgroup */
6043 len = sm_strlcpy(qpath, qg->qg_qdir, sizeof(qpath));
6044 if (len >= sizeof(qpath))
6046 syserr("QueuePath %.256s too long (%d max)",
6047 qg->qg_qdir, (int) sizeof(qpath));
6048 ExitStat = EX_CONFIG;
6052 /* begin of qpath must be same as basedir */
6053 if (strncmp(basedir, qpath, blen) != 0 &&
6054 (strncmp(basedir, qpath, blen - 1) != 0 || len != blen - 1))
6056 syserr("QueuePath %s not subpath of QueueDirectory %s",
6058 ExitStat = EX_CONFIG;
6062 /* Do we have a nested subdirectory? */
6063 if (blen < len && SM_FIRST_DIR_DELIM(qg->qg_qdir + blen) != NULL)
6066 /* Copy subdirectory into prefix for later use */
6067 if (sm_strlcpy(prefix, qg->qg_qdir + blen, sizeof(prefix)) >=
6070 syserr("QueuePath %.256s too long (%d max)",
6071 qg->qg_qdir, (int) sizeof(qpath));
6072 ExitStat = EX_CONFIG;
6075 cp = SM_LAST_DIR_DELIM(prefix);
6076 SM_ASSERT(cp != NULL);
6077 *cp = '\0'; /* cut off trailing / */
6080 /* This is guaranteed by the basedir check above */
6081 SM_ASSERT(len >= blen - 1);
6082 cp = &qpath[len - 1];
6086 register struct dirent *d;
6089 char relpath[MAXPATHLEN];
6091 *cp = '\0'; /* Overwrite wildcard */
6092 if ((cp = SM_LAST_DIR_DELIM(qpath)) == NULL)
6094 syserr("QueueDirectory: can not wildcard relative path");
6096 sm_dprintf("multiqueue_cache: \"%s*\": Can not wildcard relative path.\n",
6098 ExitStat = EX_CONFIG;
6104 ** Special case of top level wildcard, like /foo*
6108 (void) sm_strlcpy(qpath + 1, qpath, sizeof(qpath) - 1);
6112 *(cp++) = '\0'; /* Replace / with \0 */
6113 len = strlen(cp); /* Last component of queue directory */
6116 ** Path relative to basedir, with trailing /
6117 ** It will be modified below to specify the subdirectories
6118 ** so they can be opened without chdir().
6121 off = sm_strlcpyn(relpath, sizeof(relpath), 2, prefix, "/");
6122 SM_ASSERT(off < sizeof(relpath));
6125 sm_dprintf("multiqueue_cache: prefix=\"%s%s\"\n",
6128 /* It is always basedir: we don't need to store it per group */
6129 /* XXX: optimize this! -> one more global? */
6130 qg->qg_qdir = newstr(basedir);
6131 qg->qg_qdir[blen - 1] = '\0'; /* cut off trailing / */
6134 ** XXX Should probably wrap this whole loop in a timeout
6135 ** in case some wag decides to NFS mount the queues.
6138 /* Test path to get warning messages. */
6141 /* XXX qg_runasuid and qg_runasgid for specials? */
6142 i = safedirpath(basedir, RunAsUid, RunAsGid, NULL,
6144 if (i != 0 && tTd(41, 2))
6145 sm_dprintf("multiqueue_cache: \"%s\": Not safe: %s\n",
6146 basedir, sm_errstring(i));
6149 if ((dp = opendir(prefix)) == NULL)
6151 syserr("can not opendir(%s/%s)", qg->qg_qdir, prefix);
6153 sm_dprintf("multiqueue_cache: opendir(\"%s/%s\"): %s\n",
6154 qg->qg_qdir, prefix,
6155 sm_errstring(errno));
6156 ExitStat = EX_CONFIG;
6159 while ((d = readdir(dp)) != NULL)
6161 /* Skip . and .. directories */
6162 if (strcmp(d->d_name, ".") == 0 ||
6163 strcmp(d->d_name, "..") == 0)
6166 i = strlen(d->d_name);
6167 if (i < len || strncmp(d->d_name, cp, len) != 0)
6170 sm_dprintf("multiqueue_cache: \"%s\", skipped\n",
6175 /* Create relative pathname: prefix + local directory */
6176 i = sizeof(relpath) - off;
6177 if (sm_strlcpy(relpath + off, d->d_name, i) >= i)
6178 continue; /* way too long */
6180 if (!chkqdir(relpath, sff))
6183 if (qg->qg_qpaths == NULL)
6185 slotsleft = INITIAL_SLOTS;
6186 qg->qg_qpaths = (QPATHS *)xalloc((sizeof(*qg->qg_qpaths)) *
6188 qg->qg_numqueues = 0;
6190 else if (slotsleft < 1)
6192 qg->qg_qpaths = (QPATHS *)sm_realloc((char *)qg->qg_qpaths,
6193 (sizeof(*qg->qg_qpaths)) *
6196 if (qg->qg_qpaths == NULL)
6198 (void) closedir(dp);
6201 slotsleft += ADD_SLOTS;
6205 qg->qg_qpaths[qg->qg_numqueues].qp_subdirs = QP_NOSUB;
6207 #define CHKRSUBDIR(name, flag) \
6208 (void) sm_strlcpyn(subdir, sizeof(subdir), 3, relpath, "/", name); \
6209 if (chkqdir(subdir, sff)) \
6210 qg->qg_qpaths[qg->qg_numqueues].qp_subdirs |= flag; \
6214 CHKRSUBDIR("qf", QP_SUBQF);
6215 CHKRSUBDIR("df", QP_SUBDF);
6216 CHKRSUBDIR("xf", QP_SUBXF);
6218 /* assert(strlen(d->d_name) < MAXPATHLEN - 14) */
6219 /* maybe even - 17 (subdirs) */
6221 if (prefix[0] != '.')
6222 qg->qg_qpaths[qg->qg_numqueues].qp_name =
6225 qg->qg_qpaths[qg->qg_numqueues].qp_name =
6229 sm_dprintf("multiqueue_cache: %d: \"%s\" cached (%x).\n",
6230 qg->qg_numqueues, relpath,
6231 qg->qg_qpaths[qg->qg_numqueues].qp_subdirs);
6233 qg->qg_qpaths[qg->qg_numqueues].qp_idx = qn;
6234 *phash = hash_q(relpath, *phash);
6235 #endif /* SM_CONF_SHM */
6240 (void) closedir(dp);
6245 if (qg->qg_numqueues == 0)
6247 qg->qg_qpaths = (QPATHS *) xalloc(sizeof(*qg->qg_qpaths));
6249 /* test path to get warning messages */
6250 i = safedirpath(qpath, RunAsUid, RunAsGid, NULL, sff, 0, 0);
6253 syserr("can not opendir(%s)", qpath);
6255 sm_dprintf("multiqueue_cache: opendir(\"%s\"): %s\n",
6256 qpath, sm_errstring(i));
6257 ExitStat = EX_CONFIG;
6261 qg->qg_qpaths[0].qp_subdirs = QP_NOSUB;
6262 qg->qg_numqueues = 1;
6265 #define CHKSUBDIR(name, flag) \
6266 (void) sm_strlcpyn(subdir, sizeof(subdir), 3, qg->qg_qdir, "/", name); \
6267 if (chkqdir(subdir, sff)) \
6268 qg->qg_qpaths[0].qp_subdirs |= flag; \
6271 CHKSUBDIR("qf", QP_SUBQF);
6272 CHKSUBDIR("df", QP_SUBDF);
6273 CHKSUBDIR("xf", QP_SUBXF);
6275 if (qg->qg_qdir[blen - 1] != '\0' &&
6276 qg->qg_qdir[blen] != '\0')
6279 ** Copy the last component into qpaths and
6283 qg->qg_qpaths[0].qp_name = newstr(qg->qg_qdir + blen);
6284 qg->qg_qdir[blen - 1] = '\0';
6287 qg->qg_qpaths[0].qp_name = newstr(".");
6290 qg->qg_qpaths[0].qp_idx = qn;
6291 *phash = hash_q(qg->qg_qpaths[0].qp_name, *phash);
6292 #endif /* SM_CONF_SHM */
6299 ** FILESYS_FIND -- find entry in FileSys table, or add new one
6301 ** Given the pathname of a directory, determine the file system
6302 ** in which that directory resides, and return a pointer to the
6303 ** entry in the FileSys table that describes the file system.
6304 ** A new entry is added if necessary (and requested).
6305 ** If the directory does not exist, -1 is returned.
6308 ** name -- name of directory (must be persistent!)
6309 ** path -- pathname of directory (name plus maybe "/df")
6310 ** add -- add to structure if not found.
6313 ** >=0: found: index in file system table
6314 ** <0: some error, i.e.,
6315 ** FSF_TOO_MANY: too many filesystems (-> syserr())
6316 ** FSF_STAT_FAIL: can't stat() filesystem (-> syserr())
6317 ** FSF_NOT_FOUND: not in list
6320 static short filesys_find __P((const char *, const char *, bool));
6322 #define FSF_NOT_FOUND (-1)
6323 #define FSF_STAT_FAIL (-2)
6324 #define FSF_TOO_MANY (-3)
6327 filesys_find(name, path, add)
6335 if (stat(path, &st) < 0)
6337 syserr("cannot stat queue directory %s", path);
6338 return FSF_STAT_FAIL;
6340 for (i = 0; i < NumFileSys; ++i)
6342 if (FILE_SYS_DEV(i) == st.st_dev)
6345 ** Make sure the file system (FS) name is set:
6346 ** even though the source code indicates that
6347 ** FILE_SYS_DEV() is only set below, it could be
6348 ** set via shared memory, hence we need to perform
6349 ** this check/assignment here.
6352 if (NULL == FILE_SYS_NAME(i))
6353 FILE_SYS_NAME(i) = name;
6357 if (i >= MAXFILESYS)
6359 syserr("too many queue file systems (%d max)", MAXFILESYS);
6360 return FSF_TOO_MANY;
6363 return FSF_NOT_FOUND;
6366 FILE_SYS_NAME(i) = name;
6367 FILE_SYS_DEV(i) = st.st_dev;
6368 FILE_SYS_AVAIL(i) = 0;
6369 FILE_SYS_BLKSIZE(i) = 1024; /* avoid divide by zero */
6374 ** FILESYS_SETUP -- set up mapping from queue directories to file systems
6376 ** This data structure is used to efficiently check the amount of
6377 ** free space available in a set of queue directories.
6380 ** add -- initialize structure if necessary.
6384 ** <0: some error, i.e.,
6385 ** FSF_NOT_FOUND: not in list
6386 ** FSF_STAT_FAIL: can't stat() filesystem (-> syserr())
6387 ** FSF_TOO_MANY: too many filesystems (-> syserr())
6390 static int filesys_setup __P((bool));
6401 for (i = 0; i < NumQueue && Queue[i] != NULL; i++)
6403 for (j = 0; j < Queue[i]->qg_numqueues; ++j)
6405 QPATHS *qp = &Queue[i]->qg_qpaths[j];
6406 char qddf[MAXPATHLEN];
6408 (void) sm_strlcpyn(qddf, sizeof(qddf), 2, qp->qp_name,
6409 (bitset(QP_SUBDF, qp->qp_subdirs)
6411 fs = filesys_find(qp->qp_name, qddf, add);
6413 qp->qp_fsysidx = fs;
6424 ** FILESYS_UPDATE -- update amount of free space on all file systems
6426 ** The FileSys table is used to cache the amount of free space
6427 ** available on all queue directory file systems.
6428 ** This function updates the cached information if it has expired.
6437 ** Updates FileSys table.
6444 long avail, blksize;
6446 static time_t nextupdate = 0;
6450 ** Only the daemon updates the shared memory, i.e.,
6451 ** if shared memory is available but the pid is not the
6452 ** one of the daemon, then don't do anything.
6455 if (ShmId != SM_SHM_NO_ID && DaemonPid != CurrentPid)
6457 #endif /* SM_CONF_SHM */
6459 if (now < nextupdate)
6461 nextupdate = now + FILESYS_UPDATE_INTERVAL;
6462 for (i = 0; i < NumFileSys; ++i)
6464 FILESYS *fs = &FILE_SYS(i);
6466 avail = freediskspace(FILE_SYS_NAME(i), &blksize);
6467 if (avail < 0 || blksize <= 0)
6470 sm_syslog(LOG_ERR, NOQID,
6471 "filesys_update failed: %s, fs=%s, avail=%ld, blocksize=%ld",
6472 sm_errstring(errno),
6473 FILE_SYS_NAME(i), avail, blksize);
6475 fs->fs_blksize = 1024; /* avoid divide by zero */
6476 nextupdate = now + 2; /* let's do this soon again */
6480 fs->fs_avail = avail;
6481 fs->fs_blksize = blksize;
6486 #if _FFR_ANY_FREE_FS
6488 ** FILESYS_FREE -- check whether there is at least one fs with enough space.
6491 ** fsize -- file size in bytes
6494 ** true iff there is one fs with more than fsize bytes free.
6505 for (i = 0; i < NumFileSys; ++i)
6509 if (FILE_SYS_AVAIL(i) < 0 || FILE_SYS_BLKSIZE(i) <= 0)
6511 needed += fsize / FILE_SYS_BLKSIZE(i)
6512 + ((fsize % FILE_SYS_BLKSIZE(i)
6515 if (needed <= FILE_SYS_AVAIL(i))
6520 #endif /* _FFR_ANY_FREE_FS */
6523 ** DISK_STATUS -- show amount of free space in queue directories
6526 ** out -- output file pointer.
6527 ** prefix -- string to output in front of each line.
6534 disk_status(out, prefix)
6539 long avail, blksize;
6542 for (i = 0; i < NumFileSys; ++i)
6544 avail = freediskspace(FILE_SYS_NAME(i), &blksize);
6545 if (avail >= 0 && blksize > 0)
6547 free = (long)((double) avail *
6548 ((double) blksize / 1024));
6552 (void) sm_io_fprintf(out, SM_TIME_DEFAULT,
6563 ** INIT_SEM -- initialize semaphore system
6566 ** owner -- is this the owner of semaphores?
6572 #if _FFR_USE_SEM_LOCKING
6574 static int SemId = -1; /* Semaphore Id */
6575 int SemKey = SM_SEM_KEY;
6576 #endif /* SM_CONF_SEM */
6577 #endif /* _FFR_USE_SEM_LOCKING */
6579 static void init_sem __P((bool));
6585 #if _FFR_USE_SEM_LOCKING
6587 SemId = sm_sem_start(SemKey, 1, 0, owner);
6590 sm_syslog(LOG_ERR, NOQID,
6591 "func=init_sem, sem_key=%ld, sm_sem_start=%d",
6592 (long) SemKey, SemId);
6595 #endif /* SM_CONF_SEM */
6596 #endif /* _FFR_USE_SEM_LOCKING */
6601 ** STOP_SEM -- stop semaphore system
6604 ** owner -- is this the owner of semaphores?
6610 static void stop_sem __P((bool));
6616 #if _FFR_USE_SEM_LOCKING
6618 if (owner && SemId >= 0)
6620 #endif /* SM_CONF_SEM */
6621 #endif /* _FFR_USE_SEM_LOCKING */
6626 ** UPD_QS -- update information about queue when adding/deleting an entry
6630 ** count -- add/remove entry (+1/0/-1: add/no change/remove)
6631 ** space -- update the space available as well.
6632 ** (>0/0/<0: add/no change/remove)
6633 ** where -- caller (for logging)
6639 ** Modifies available space in filesystem.
6640 ** Changes number of entries in queue directory.
6644 upd_qs(e, count, space, where)
6652 # if _FFR_USE_SEM_LOCKING
6654 # endif /* _FFR_USE_SEM_LOCKING */
6657 if (ShmId == SM_SHM_NO_ID || e == NULL)
6659 if (e->e_qgrp == NOQGRP || e->e_qdir == NOQDIR)
6661 idx = Queue[e->e_qgrp]->qg_qpaths[e->e_qdir].qp_idx;
6663 sm_dprintf("func=upd_qs, count=%d, space=%d, where=%s, idx=%d, entries=%d\n",
6664 count, space, where, idx, QSHM_ENTRIES(idx));
6666 /* XXX in theory this needs to be protected with a mutex */
6667 if (QSHM_ENTRIES(idx) >= 0 && count != 0)
6669 # if _FFR_USE_SEM_LOCKING
6670 r = sm_sem_acq(SemId, 0, 1);
6671 # endif /* _FFR_USE_SEM_LOCKING */
6672 QSHM_ENTRIES(idx) += count;
6673 # if _FFR_USE_SEM_LOCKING
6675 r = sm_sem_rel(SemId, 0, 1);
6676 # endif /* _FFR_USE_SEM_LOCKING */
6679 fidx = Queue[e->e_qgrp]->qg_qpaths[e->e_qdir].qp_fsysidx;
6683 /* update available space also? (might be loseqfile) */
6687 /* convert size to blocks; this causes rounding errors */
6688 s = e->e_msgsize / FILE_SYS_BLKSIZE(fidx);
6692 /* XXX in theory this needs to be protected with a mutex */
6694 FILE_SYS_AVAIL(fidx) += s;
6696 FILE_SYS_AVAIL(fidx) -= s;
6700 static bool write_key_file __P((char *, long));
6701 static long read_key_file __P((char *, long));
6704 ** WRITE_KEY_FILE -- record some key into a file.
6707 ** keypath -- file name.
6708 ** key -- key to write.
6711 ** true iff file could be written.
6718 write_key_file(keypath, key)
6727 if (keypath == NULL || *keypath == '\0')
6729 sff = SFF_NOLINK|SFF_ROOTOK|SFF_REGONLY|SFF_CREAT;
6730 if (TrustedUid != 0 && RealUid == TrustedUid)
6731 sff |= SFF_OPENASROOT;
6732 keyf = safefopen(keypath, O_WRONLY|O_TRUNC, FileMode, sff);
6735 sm_syslog(LOG_ERR, NOQID, "unable to write %s: %s",
6736 keypath, sm_errstring(errno));
6740 if (geteuid() == 0 && RunAsUid != 0)
6746 if (fd >= 0 && fchown(fd, RunAsUid, -1) < 0)
6750 sm_syslog(LOG_ALERT, NOQID,
6751 "ownership change on %s to %d failed: %s",
6752 keypath, RunAsUid, sm_errstring(err));
6754 # endif /* HASFCHOWN */
6756 ok = sm_io_fprintf(keyf, SM_TIME_DEFAULT, "%ld\n", key) !=
6758 ok = (sm_io_close(keyf, SM_TIME_DEFAULT) != SM_IO_EOF) && ok;
6764 ** READ_KEY_FILE -- read a key from a file.
6767 ** keypath -- file name.
6768 ** key -- default key.
6775 read_key_file(keypath, key)
6783 if (keypath == NULL || *keypath == '\0')
6785 sff = SFF_NOLINK|SFF_ROOTOK|SFF_REGONLY;
6786 if (RealUid == 0 || (TrustedUid != 0 && RealUid == TrustedUid))
6787 sff |= SFF_OPENASROOT;
6788 keyf = safefopen(keypath, O_RDONLY, FileMode, sff);
6791 sm_syslog(LOG_ERR, NOQID, "unable to read %s: %s",
6792 keypath, sm_errstring(errno));
6796 r = sm_io_fscanf(keyf, SM_TIME_DEFAULT, "%ld", &n);
6799 (void) sm_io_close(keyf, SM_TIME_DEFAULT);
6805 ** INIT_SHM -- initialize shared memory structure
6807 ** Initialize or attach to shared memory segment.
6808 ** Currently it is not a fatal error if this doesn't work.
6809 ** However, it causes us to have a "fallback" storage location
6810 ** for everything that is supposed to be in the shared memory,
6811 ** which makes the code slightly ugly.
6814 ** qn -- number of queue directories.
6815 ** owner -- owner of shared memory.
6816 ** hash -- identifies data that is stored in shared memory.
6822 static void init_shm __P((int, bool, unsigned int));
6825 init_shm(qn, owner, hash)
6835 PtrFileSys = &FileSys[0];
6836 PNumFileSys = &Numfilesys;
6837 /* if this "key" is specified: select one yourself */
6838 #define SEL_SHM_KEY ((key_t) -1)
6839 #define FIRST_SHM_KEY 25
6841 /* This allows us to disable shared memory at runtime. */
6846 shms = SM_T_SIZE + qn * sizeof(QUEUE_SHM_T);
6847 keyselect = ShmKey == SEL_SHM_KEY;
6851 ShmKey = FIRST_SHM_KEY;
6855 ShmKey = read_key_file(ShmKeyFile, ShmKey);
6857 if (ShmKey == SEL_SHM_KEY)
6859 save_errno = (errno != 0) ? errno : EINVAL;
6866 /* allow read/write access for group? */
6867 Pshm = sm_shmstart(ShmKey, shms,
6868 SHM_R|SHM_W|(SHM_R>>3)|(SHM_W>>3),
6871 if (Pshm != NULL || !sm_file_exists(save_errno))
6879 /* back where we started? */
6880 if (ShmKey == SEL_SHM_KEY)
6887 /* only sleep if we are at the first key */
6888 if (!keyselect || ShmKey == SEL_SHM_KEY)
6896 (void) write_key_file(ShmKeyFile, (long) ShmKey);
6897 if (owner && RunAsUid != 0)
6899 i = sm_shmsetowner(ShmId, RunAsUid, RunAsGid, 0660);
6901 sm_syslog(LOG_ERR, NOQID,
6902 "key=%ld, sm_shmsetowner=%d, RunAsUid=%d, RunAsGid=%d",
6903 (long) ShmKey, i, RunAsUid, RunAsGid);
6909 *((pid_t *) SHM_OFF_PID(Pshm)) = CurrentPid;
6910 p = (int *) SHM_OFF_TAG(Pshm);
6915 if (*p != (int) shms)
6917 save_errno = EINVAL;
6921 p = (int *) SHM_OFF_TAG(Pshm);
6922 if (*p != (int) hash)
6924 save_errno = EINVAL;
6930 ** XXX how to check the pid?
6931 ** Read it from the pid-file? That does
6932 ** not need to exist.
6933 ** We could disable shm if we can't confirm
6934 ** that it is the right one.
6938 PtrFileSys = (FILESYS *) OFF_FILE_SYS(Pshm);
6939 PNumFileSys = (int *) OFF_NUM_FILE_SYS(Pshm);
6940 QShm = (QUEUE_SHM_T *) OFF_QUEUE_SHM(Pshm);
6941 PRSATmpCnt = (int *) OFF_RSA_TMP_CNT(Pshm);
6945 /* initialize values in shared memory */
6947 for (i = 0; i < qn; i++)
6948 QShm[i].qs_entries = -1;
6954 if (LogLevel > (owner ? 8 : 11))
6956 sm_syslog(owner ? LOG_ERR : LOG_NOTICE, NOQID,
6957 "can't %s shared memory, key=%ld: %s",
6958 owner ? "initialize" : "attach to",
6959 (long) ShmKey, sm_errstring(save_errno));
6962 #endif /* SM_CONF_SHM */
6966 ** SETUP_QUEUES -- set up all queue groups
6969 ** owner -- owner of shared memory?
6976 ** attaches shared memory.
6977 #endif * SM_CONF_SHM *
6985 unsigned int hashval;
6987 char basedir[MAXPATHLEN];
6991 ** Determine basedir for all queue directories.
6992 ** All queue directories must be (first level) subdirectories
6993 ** of the basedir. The basedir is the QueueDir
6994 ** without wildcards, but with trailing /
6999 len = sm_strlcpy(basedir, QueueDir, sizeof(basedir));
7001 /* Provide space for trailing '/' */
7002 if (len >= sizeof(basedir) - 1)
7004 syserr("QueueDirectory: path too long: %d, max %d",
7005 len, (int) sizeof(basedir) - 1);
7006 ExitStat = EX_CONFIG;
7010 if (basedir[len - 1] == '*')
7014 cp = SM_LAST_DIR_DELIM(basedir);
7017 syserr("QueueDirectory: can not wildcard relative path \"%s\"",
7020 sm_dprintf("setup_queues: \"%s\": Can not wildcard relative path.\n",
7022 ExitStat = EX_CONFIG;
7026 /* cut off wildcard pattern */
7030 else if (!SM_IS_DIR_DELIM(basedir[len - 1]))
7032 /* append trailing slash since it is a directory */
7034 basedir[++len] = '\0';
7037 /* len counts up to the last directory delimiter */
7038 SM_ASSERT(basedir[len - 1] == '/');
7040 if (chdir(basedir) < 0)
7042 int save_errno = errno;
7044 syserr("can not chdir(%s)", basedir);
7045 if (save_errno == EACCES)
7046 (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
7047 "Program mode requires special privileges, e.g., root or TrustedUser.\n");
7049 sm_dprintf("setup_queues: \"%s\": %s\n",
7050 basedir, sm_errstring(errno));
7051 ExitStat = EX_CONFIG;
7055 hashval = hash_q(basedir, hashval);
7056 #endif /* SM_CONF_SHM */
7058 /* initialize for queue runs */
7061 for (i = 0; i < NumQueue && Queue[i] != NULL; i++)
7062 Queue[i]->qg_nextrun = now;
7065 if (UseMSP && OpMode != MD_TEST)
7067 long sff = SFF_CREAT;
7069 if (stat(".", &st) < 0)
7071 syserr("can not stat(%s)", basedir);
7073 sm_dprintf("setup_queues: \"%s\": %s\n",
7074 basedir, sm_errstring(errno));
7075 ExitStat = EX_CONFIG;
7082 ** Check queue directory permissions.
7083 ** Can we write to a group writable queue directory?
7086 if (bitset(S_IWGRP, QueueFileMode) &&
7087 bitset(S_IWGRP, st.st_mode) &&
7088 safefile(" ", RunAsUid, RunAsGid, RunAsUserName, sff,
7089 QueueFileMode, NULL) != 0)
7091 syserr("can not write to queue directory %s (RunAsGid=%d, required=%d)",
7092 basedir, (int) RunAsGid, (int) st.st_gid);
7094 if (bitset(S_IWOTH|S_IXOTH, st.st_mode))
7096 #if _FFR_MSP_PARANOIA
7097 syserr("dangerous permissions=%o on queue directory %s",
7098 (int) st.st_mode, basedir);
7099 #else /* _FFR_MSP_PARANOIA */
7101 sm_syslog(LOG_ERR, NOQID,
7102 "dangerous permissions=%o on queue directory %s",
7103 (int) st.st_mode, basedir);
7104 #endif /* _FFR_MSP_PARANOIA */
7106 #if _FFR_MSP_PARANOIA
7108 syserr("can not use multiple queues for MSP");
7109 #endif /* _FFR_MSP_PARANOIA */
7112 /* initial number of queue directories */
7114 for (i = 0; i < NumQueue && Queue[i] != NULL; i++)
7115 qn = multiqueue_cache(basedir, len, Queue[i], qn, &hashval);
7118 init_shm(qn, owner, hashval);
7119 i = filesys_setup(owner || ShmId == SM_SHM_NO_ID);
7120 if (i == FSF_NOT_FOUND)
7123 ** We didn't get the right filesystem data
7124 ** This may happen if we don't have the right shared memory.
7125 ** So let's do this without shared memory.
7129 cleanup_shm(false); /* release shared memory */
7130 i = filesys_setup(false);
7132 syserr("filesys_setup failed twice, result=%d", i);
7133 else if (LogLevel > 8)
7134 sm_syslog(LOG_WARNING, NOQID,
7135 "shared memory does not contain expected data, ignored");
7137 #else /* SM_CONF_SHM */
7138 i = filesys_setup(true);
7139 #endif /* SM_CONF_SHM */
7141 ExitStat = EX_CONFIG;
7146 ** CLEANUP_SHM -- do some cleanup work for shared memory etc
7149 ** owner -- owner of shared memory?
7155 ** detaches shared memory.
7162 if (ShmId != SM_SHM_NO_ID)
7164 if (sm_shmstop(Pshm, ShmId, owner) < 0 && LogLevel > 8)
7165 sm_syslog(LOG_INFO, NOQID, "sm_shmstop failed=%s",
7166 sm_errstring(errno));
7168 ShmId = SM_SHM_NO_ID;
7172 #endif /* SM_CONF_SHM */
7175 ** CLEANUP_QUEUES -- do some cleanup work for queues
7191 ** SET_DEF_QUEUEVAL -- set default values for a queue group.
7194 ** qg -- queue group
7195 ** all -- set all values (true for default group)?
7201 ** sets default values for the queue group.
7205 set_def_queueval(qg, all)
7209 if (bitnset(QD_DEFINED, qg->qg_flags))
7212 qg->qg_qdir = QueueDir;
7213 #if _FFR_QUEUE_GROUP_SORTORDER
7214 qg->qg_sortorder = QueueSortOrder;
7215 #endif /* _FFR_QUEUE_GROUP_SORTORDER */
7216 qg->qg_maxqrun = all ? MaxRunnersPerQueue : -1;
7217 qg->qg_nice = NiceQueueRun;
7220 ** MAKEQUEUE -- define a new queue.
7223 ** line -- description of queue. This is in labeled fields.
7225 ** F -- the flags associated with the queue
7226 ** I -- the interval between running the queue
7227 ** J -- the maximum # of jobs in work list
7228 ** [M -- the maximum # of jobs in a queue run]
7229 ** N -- the niceness at which to run
7230 ** P -- the path to the queue
7231 ** S -- the queue sorting order
7232 ** R -- number of parallel queue runners
7233 ** r -- max recipients per envelope
7234 ** The first word is the canonical name of the queue.
7235 ** qdef -- this is a 'Q' definition from .cf
7241 ** enters the queue into the queue table.
7245 makequeue(line, qdef)
7250 register QUEUEGRP *qg;
7255 /* allocate a queue and set up defaults */
7256 qg = (QUEUEGRP *) xalloc(sizeof(*qg));
7257 memset((char *) qg, '\0', sizeof(*qg));
7259 if (line[0] == '\0')
7261 syserr("name required for queue");
7265 /* collect the queue name */
7267 *p != '\0' && *p != ',' && !(isascii(*p) && isspace(*p));
7272 qg->qg_name = newstr(line);
7274 /* set default values, can be overridden below */
7275 set_def_queueval(qg, false);
7277 /* now scan through and assign info from the fields */
7280 auto char *delimptr;
7282 while (*p != '\0' &&
7283 (*p == ',' || (isascii(*p) && isspace(*p))))
7286 /* p now points to field code */
7288 while (*p != '\0' && *p != '=' && *p != ',')
7292 syserr("queue %s: `=' expected", qg->qg_name);
7295 while (isascii(*p) && isspace(*p))
7298 /* p now points to the field body */
7299 p = munchstring(p, &delimptr, ',');
7301 /* install the field into the queue struct */
7304 case 'P': /* pathname */
7306 syserr("queue %s: empty path name",
7309 qg->qg_qdir = newstr(p);
7312 case 'F': /* flags */
7313 for (; *p != '\0'; p++)
7314 if (!(isascii(*p) && isspace(*p)))
7315 setbitn(*p, qg->qg_flags);
7319 ** Do we need two intervals here:
7320 ** One for persistent queue runners,
7321 ** one for "normal" queue runs?
7324 case 'I': /* interval between running the queue */
7325 qg->qg_queueintvl = convtime(p, 'm');
7328 case 'N': /* run niceness */
7329 qg->qg_nice = atoi(p);
7332 case 'R': /* maximum # of runners for the group */
7335 /* can't have more runners than allowed total */
7336 if (MaxQueueChildren > 0 && i > MaxQueueChildren)
7338 qg->qg_maxqrun = MaxQueueChildren;
7339 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
7340 "Q=%s: R=%d exceeds MaxQueueChildren=%d, set to MaxQueueChildren\n",
7348 case 'J': /* maximum # of jobs in work list */
7349 qg->qg_maxlist = atoi(p);
7352 case 'r': /* max recipients per envelope */
7353 qg->qg_maxrcpt = atoi(p);
7356 #if _FFR_QUEUE_GROUP_SORTORDER
7357 case 'S': /* queue sorting order */
7360 case 'h': /* Host first */
7362 qg->qg_sortorder = QSO_BYHOST;
7365 case 'p': /* Priority order */
7367 qg->qg_sortorder = QSO_BYPRIORITY;
7370 case 't': /* Submission time */
7372 qg->qg_sortorder = QSO_BYTIME;
7375 case 'f': /* File name */
7377 qg->qg_sortorder = QSO_BYFILENAME;
7380 case 'm': /* Modification time */
7382 qg->qg_sortorder = QSO_BYMODTIME;
7385 case 'r': /* Random */
7387 qg->qg_sortorder = QSO_RANDOM;
7391 case 's': /* Shuffled host name */
7393 qg->qg_sortorder = QSO_BYSHUFFLE;
7395 # endif /* _FFR_RHS */
7397 case 'n': /* none */
7399 qg->qg_sortorder = QSO_NONE;
7403 syserr("Invalid queue sort order \"%s\"", p);
7406 #endif /* _FFR_QUEUE_GROUP_SORTORDER */
7409 syserr("Q%s: unknown queue equate %c=",
7410 qg->qg_name, fcode);
7418 if (qg->qg_nice != NiceQueueRun)
7420 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
7421 "Q%s: Warning: N= set on system that doesn't support nice()\n",
7424 #endif /* !HASNICE */
7426 /* do some rationality checking */
7427 if (NumQueue >= MAXQUEUEGROUPS)
7429 syserr("too many queue groups defined (%d max)",
7434 if (qg->qg_qdir == NULL)
7436 if (QueueDir == NULL || *QueueDir == '\0')
7438 syserr("QueueDir must be defined before queue groups");
7441 qg->qg_qdir = newstr(QueueDir);
7444 if (qg->qg_maxqrun > 1 && !bitnset(QD_FORK, qg->qg_flags))
7446 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
7447 "Warning: Q=%s: R=%d: multiple queue runners specified\n\tbut flag '%c' is not set\n",
7448 qg->qg_name, qg->qg_maxqrun, QD_FORK);
7451 /* enter the queue into the symbol table */
7453 sm_syslog(LOG_INFO, NOQID,
7454 "Adding %s to stab, path: %s", qg->qg_name,
7456 s = stab(qg->qg_name, ST_QUEUE, ST_ENTER);
7457 if (s->s_quegrp != NULL)
7459 i = s->s_quegrp->qg_index;
7461 /* XXX what about the pointers inside this struct? */
7462 sm_free(s->s_quegrp); /* XXX */
7466 Queue[i] = s->s_quegrp = qg;
7469 /* set default value for max queue runners */
7470 if (qg->qg_maxqrun < 0)
7472 if (MaxRunnersPerQueue > 0)
7473 qg->qg_maxqrun = MaxRunnersPerQueue;
7478 setbitn(QD_DEFINED, qg->qg_flags);
7482 ** HASHFQN -- calculate a hash value for a fully qualified host name
7485 ** fqn -- an all lower-case host.domain string
7486 ** buckets -- the number of buckets (queue directories)
7489 ** a bucket number (signed integer)
7492 ** Contributed by Exactis.com, Inc.
7496 hashfqn(fqn, buckets)
7501 register int h = 0, hash, cnt;
7507 ** A variation on the gdb hash
7508 ** This is the best as of Feb 19, 1996 --bcx
7512 h = 0x238F13AF * strlen(p);
7513 for (cnt = 0; *p != 0; ++p, cnt++)
7515 h = (h + (*p << (cnt * 5 % 24))) & 0x7FFFFFFF;
7517 h = (1103515243 * h + 12345) & 0x7FFFFFFF;
7521 hash = (h % buckets);
7528 ** A structure for sorting Queue according to maxqrun without
7529 ** screwing up Queue itself.
7534 int sg_idx; /* original index */
7535 int sg_maxqrun; /* max queue runners */
7537 typedef struct sortqgrp SORTQGRP_T;
7538 static int cmpidx __P((const void *, const void *));
7545 /* The sort is highest to lowest, so the comparison is reversed */
7546 if (((SORTQGRP_T *)a)->sg_maxqrun < ((SORTQGRP_T *)b)->sg_maxqrun)
7548 else if (((SORTQGRP_T *)a)->sg_maxqrun > ((SORTQGRP_T *)b)->sg_maxqrun)
7555 ** MAKEWORKGROUP -- balance queue groups into work groups per MaxQueueChildren
7557 ** Take the now defined queue groups and assign them to work groups.
7558 ** This is done to balance out the number of concurrently active
7559 ** queue runners such that MaxQueueChildren is not exceeded. This may
7560 ** result in more than one queue group per work group. In such a case
7561 ** the number of running queue groups in that work group will have no
7562 ** more than the work group maximum number of runners (a "fair" portion
7563 ** of MaxQueueRunners). All queue groups within a work group will get a
7564 ** chance at running.
7573 ** Sets up WorkGrp structure.
7579 int i, j, total_runners, dir, h;
7580 SORTQGRP_T si[MAXQUEUEGROUPS + 1];
7583 if (NumQueue == 1 && strcmp(Queue[0]->qg_name, "mqueue") == 0)
7586 ** There is only the "mqueue" queue group (a default)
7587 ** containing all of the queues. We want to provide to
7588 ** this queue group the maximum allowable queue runners.
7589 ** To match older behavior (8.10/8.11) we'll try for
7590 ** 1 runner per queue capping it at MaxQueueChildren.
7591 ** So if there are N queues, then there will be N runners
7592 ** for the "mqueue" queue group (where N is kept less than
7593 ** MaxQueueChildren).
7597 WorkGrp[0].wg_numqgrp = 1;
7598 WorkGrp[0].wg_qgs = (QUEUEGRP **) xalloc(sizeof(QUEUEGRP *));
7599 WorkGrp[0].wg_qgs[0] = Queue[0];
7600 if (MaxQueueChildren > 0 &&
7601 Queue[0]->qg_numqueues > MaxQueueChildren)
7602 WorkGrp[0].wg_runners = MaxQueueChildren;
7604 WorkGrp[0].wg_runners = Queue[0]->qg_numqueues;
7606 Queue[0]->qg_wgrp = 0;
7608 /* can't have more runners than allowed total */
7609 if (MaxQueueChildren > 0 &&
7610 Queue[0]->qg_maxqrun > MaxQueueChildren)
7611 Queue[0]->qg_maxqrun = MaxQueueChildren;
7612 WorkGrp[0].wg_maxact = Queue[0]->qg_maxqrun;
7613 WorkGrp[0].wg_lowqintvl = Queue[0]->qg_queueintvl;
7617 for (i = 0; i < NumQueue; i++)
7619 si[i].sg_maxqrun = Queue[i]->qg_maxqrun;
7622 qsort(si, NumQueue, sizeof(si[0]), cmpidx);
7625 for (i = 0; i < NumQueue; i++)
7627 total_runners += si[i].sg_maxqrun;
7628 if (MaxQueueChildren <= 0 || total_runners <= MaxQueueChildren)
7634 if (NumWorkGroups < 1)
7635 NumWorkGroups = 1; /* gotta have one at least */
7636 else if (NumWorkGroups > MAXWORKGROUPS)
7637 NumWorkGroups = MAXWORKGROUPS; /* the limit */
7640 ** We now know the number of work groups to pack the queue groups
7641 ** into. The queue groups in 'Queue' are sorted from highest
7642 ** to lowest for the number of runners per queue group.
7643 ** We put the queue groups with the largest number of runners
7644 ** into work groups first. Then the smaller ones are fitted in
7645 ** where it looks best.
7650 for (i = 0; i < NumQueue; i++)
7652 /* a to-and-fro packing scheme, continue from last position */
7653 if (j >= NumWorkGroups)
7656 j = NumWorkGroups - 1;
7664 if (WorkGrp[j].wg_qgs == NULL)
7665 WorkGrp[j].wg_qgs = (QUEUEGRP **)sm_malloc(sizeof(QUEUEGRP *) *
7666 (WorkGrp[j].wg_numqgrp + 1));
7668 WorkGrp[j].wg_qgs = (QUEUEGRP **)sm_realloc(WorkGrp[j].wg_qgs,
7669 sizeof(QUEUEGRP *) *
7670 (WorkGrp[j].wg_numqgrp + 1));
7671 if (WorkGrp[j].wg_qgs == NULL)
7673 syserr("!cannot allocate memory for work queues, need %d bytes",
7674 (int) (sizeof(QUEUEGRP *) *
7675 (WorkGrp[j].wg_numqgrp + 1)));
7679 WorkGrp[j].wg_qgs[WorkGrp[j].wg_numqgrp] = Queue[h];
7680 WorkGrp[j].wg_numqgrp++;
7681 WorkGrp[j].wg_runners += Queue[h]->qg_maxqrun;
7682 Queue[h]->qg_wgrp = j;
7684 if (WorkGrp[j].wg_maxact == 0)
7686 /* can't have more runners than allowed total */
7687 if (MaxQueueChildren > 0 &&
7688 Queue[h]->qg_maxqrun > MaxQueueChildren)
7689 Queue[h]->qg_maxqrun = MaxQueueChildren;
7690 WorkGrp[j].wg_maxact = Queue[h]->qg_maxqrun;
7694 ** XXX: must wg_lowqintvl be the GCD?
7695 ** qg1: 2m, qg2: 3m, minimum: 2m, when do queue runs for
7699 /* keep track of the lowest interval for a persistent runner */
7700 if (Queue[h]->qg_queueintvl > 0 &&
7701 WorkGrp[j].wg_lowqintvl < Queue[h]->qg_queueintvl)
7702 WorkGrp[j].wg_lowqintvl = Queue[h]->qg_queueintvl;
7707 for (i = 0; i < NumWorkGroups; i++)
7709 sm_dprintf("Workgroup[%d]=", i);
7710 for (j = 0; j < WorkGrp[i].wg_numqgrp; j++)
7713 WorkGrp[i].wg_qgs[j]->qg_name);
7721 ** DUP_DF -- duplicate envelope data file
7723 ** Copy the data file from the 'old' envelope to the 'new' envelope
7724 ** in the most efficient way possible.
7726 ** Create a hard link from the 'old' data file to the 'new' data file.
7727 ** If the old and new queue directories are on different file systems,
7728 ** then the new data file link is created in the old queue directory,
7729 ** and the new queue file will contain a 'd' record pointing to the
7730 ** directory containing the new data file.
7733 ** old -- old envelope.
7734 ** new -- new envelope.
7737 ** Returns true on success, false on failure.
7740 ** On success, the new data file is created.
7741 ** On fatal failure, EF_FATALERRS is set in old->e_flags.
7744 static bool dup_df __P((ENVELOPE *, ENVELOPE *));
7752 char opath[MAXPATHLEN];
7753 char npath[MAXPATHLEN];
7755 if (!bitset(EF_HAS_DF, old->e_flags))
7758 ** this can happen if: SuperSafe != True
7759 ** and a bounce mail is sent that is split.
7762 queueup(old, false, true);
7764 SM_REQUIRE(ISVALIDQGRP(old->e_qgrp) && ISVALIDQDIR(old->e_qdir));
7765 SM_REQUIRE(ISVALIDQGRP(new->e_qgrp) && ISVALIDQDIR(new->e_qdir));
7767 (void) sm_strlcpy(opath, queuename(old, DATAFL_LETTER), sizeof(opath));
7768 (void) sm_strlcpy(npath, queuename(new, DATAFL_LETTER), sizeof(npath));
7770 if (old->e_dfp != NULL)
7772 r = sm_io_setinfo(old->e_dfp, SM_BF_COMMIT, NULL);
7773 if (r < 0 && errno != EINVAL)
7775 syserr("@can't commit %s", opath);
7776 old->e_flags |= EF_FATALERRS;
7782 ** Attempt to create a hard link, if we think both old and new
7783 ** are on the same file system, otherwise copy the file.
7785 ** Don't waste time attempting a hard link unless old and new
7786 ** are on the same file system.
7789 SM_REQUIRE(ISVALIDQGRP(old->e_dfqgrp) && ISVALIDQDIR(old->e_dfqdir));
7790 SM_REQUIRE(ISVALIDQGRP(new->e_dfqgrp) && ISVALIDQDIR(new->e_dfqdir));
7792 ofs = Queue[old->e_dfqgrp]->qg_qpaths[old->e_dfqdir].qp_fsysidx;
7793 nfs = Queue[new->e_dfqgrp]->qg_qpaths[new->e_dfqdir].qp_fsysidx;
7794 if (FILE_SYS_DEV(ofs) == FILE_SYS_DEV(nfs))
7796 if (link(opath, npath) == 0)
7798 new->e_flags |= EF_HAS_DF;
7799 SYNC_DIR(npath, true);
7806 ** Can't link across queue directories, so try to create a hard
7807 ** link in the same queue directory as the old df file.
7808 ** The qf file will refer to the new df file using a 'd' record.
7811 new->e_dfqgrp = old->e_dfqgrp;
7812 new->e_dfqdir = old->e_dfqdir;
7813 (void) sm_strlcpy(npath, queuename(new, DATAFL_LETTER), sizeof(npath));
7814 if (link(opath, npath) == 0)
7816 new->e_flags |= EF_HAS_DF;
7817 SYNC_DIR(npath, true);
7823 sm_syslog(LOG_ERR, old->e_id,
7824 "dup_df: can't link %s to %s, error=%s, envelope splitting failed",
7825 opath, npath, sm_errstring(errno));
7830 ** SPLIT_ENV -- Allocate a new envelope based on a given envelope.
7834 ** sendqueue -- sendqueue for new envelope.
7835 ** qgrp -- index of queue group.
7836 ** qdir -- queue directory.
7843 static ENVELOPE *split_env __P((ENVELOPE *, ADDRESS *, int, int));
7846 split_env(e, sendqueue, qgrp, qdir)
7854 ee = (ENVELOPE *) sm_rpool_malloc_x(e->e_rpool, sizeof(*ee));
7855 STRUCTCOPY(*e, *ee);
7856 ee->e_message = NULL; /* XXX use original message? */
7859 ee->e_sendqueue = sendqueue;
7860 ee->e_flags &= ~(EF_INQUEUE|EF_CLRQUEUE|EF_FATALERRS
7861 |EF_SENDRECEIPT|EF_RET_PARAM|EF_HAS_DF);
7862 ee->e_flags |= EF_NORECEIPT; /* XXX really? */
7863 ee->e_from.q_state = QS_SENDER;
7865 ee->e_lockfp = NULL;
7866 if (e->e_xfp != NULL)
7867 ee->e_xfp = sm_io_dup(e->e_xfp);
7869 /* failed to dup e->e_xfp, start a new transcript */
7870 if (ee->e_xfp == NULL)
7873 ee->e_qgrp = ee->e_dfqgrp = qgrp;
7874 ee->e_qdir = ee->e_dfqdir = qdir;
7875 ee->e_errormode = EM_MAIL;
7876 ee->e_statmsg = NULL;
7877 if (e->e_quarmsg != NULL)
7878 ee->e_quarmsg = sm_rpool_strdup_x(ee->e_rpool,
7882 ** XXX Not sure if this copying is necessary.
7883 ** sendall() does this copying, but I (dm) don't know if that is
7884 ** because of the storage management discipline we were using
7885 ** before rpools were introduced, or if it is because these lists
7886 ** can be modified later.
7889 ee->e_header = copyheader(e->e_header, ee->e_rpool);
7890 ee->e_errorqueue = copyqueue(e->e_errorqueue, ee->e_rpool);
7895 /* return values from split functions, check also below! */
7896 #define SM_SPLIT_FAIL (0)
7897 #define SM_SPLIT_NONE (1)
7898 #define SM_SPLIT_NEW(n) (1 + (n))
7901 ** SPLIT_ACROSS_QUEUE_GROUPS
7903 ** This function splits an envelope across multiple queue groups
7904 ** based on the queue group of each recipient.
7910 ** SM_SPLIT_FAIL on failure
7911 ** SM_SPLIT_NONE if no splitting occurred,
7912 ** or 1 + the number of additional envelopes created.
7915 ** On success, e->e_sibling points to a list of zero or more
7916 ** additional envelopes, and the associated data files exist
7917 ** on disk. But the queue files are not created.
7919 ** On failure, e->e_sibling is not changed.
7920 ** The order of recipients in e->e_sendqueue is permuted.
7921 ** Abandoned data files for additional envelopes that failed
7922 ** to be created may exist on disk.
7925 static int q_qgrp_compare __P((const void *, const void *));
7926 static int e_filesys_compare __P((const void *, const void *));
7929 q_qgrp_compare(p1, p2)
7933 ADDRESS **pq1 = (ADDRESS **) p1;
7934 ADDRESS **pq2 = (ADDRESS **) p2;
7936 return (*pq1)->q_qgrp - (*pq2)->q_qgrp;
7940 e_filesys_compare(p1, p2)
7944 ENVELOPE **pe1 = (ENVELOPE **) p1;
7945 ENVELOPE **pe2 = (ENVELOPE **) p2;
7948 fs1 = Queue[(*pe1)->e_qgrp]->qg_qpaths[(*pe1)->e_qdir].qp_fsysidx;
7949 fs2 = Queue[(*pe2)->e_qgrp]->qg_qpaths[(*pe2)->e_qdir].qp_fsysidx;
7950 if (FILE_SYS_DEV(fs1) < FILE_SYS_DEV(fs2))
7952 if (FILE_SYS_DEV(fs1) > FILE_SYS_DEV(fs2))
7957 static int split_across_queue_groups __P((ENVELOPE *));
7959 split_across_queue_groups(e)
7962 int naddrs, nsplits, i;
7965 ADDRESS *q, **addrs;
7967 ENVELOPE *splits[MAXQUEUEGROUPS];
7968 char pvpbuf[PSBUFSIZE];
7970 SM_REQUIRE(ISVALIDQGRP(e->e_qgrp));
7972 /* Count addresses and assign queue groups. */
7975 for (q = e->e_sendqueue; q != NULL; q = q->q_next)
7977 if (QS_IS_DEAD(q->q_state))
7981 /* bad addresses and those already sent stay put */
7982 if (QS_IS_BADADDR(q->q_state) ||
7983 QS_IS_SENT(q->q_state))
7984 q->q_qgrp = e->e_qgrp;
7985 else if (!ISVALIDQGRP(q->q_qgrp))
7987 /* call ruleset which should return a queue group */
7988 i = rscap(RS_QUEUEGROUP, q->q_user, NULL, e, &pvp,
7989 pvpbuf, sizeof(pvpbuf));
7991 pvp != NULL && pvp[0] != NULL &&
7992 (pvp[0][0] & 0377) == CANONNET &&
7993 pvp[1] != NULL && pvp[1][0] != '\0')
7995 i = name2qid(pvp[1]);
8001 sm_syslog(LOG_INFO, NOQID,
8002 "queue group name %s -> %d",
8006 else if (LogLevel > 10)
8007 sm_syslog(LOG_INFO, NOQID,
8008 "can't find queue group name %s, selection ignored",
8011 if (q->q_mailer != NULL &&
8012 ISVALIDQGRP(q->q_mailer->m_qgrp))
8015 q->q_qgrp = q->q_mailer->m_qgrp;
8017 else if (ISVALIDQGRP(e->e_qgrp))
8018 q->q_qgrp = e->e_qgrp;
8024 /* only one address? nothing to split. */
8025 if (naddrs <= 1 && !changed)
8026 return SM_SPLIT_NONE;
8028 /* sort the addresses by queue group */
8029 addrs = sm_rpool_malloc_x(e->e_rpool, naddrs * sizeof(ADDRESS *));
8030 for (i = 0, q = e->e_sendqueue; q != NULL; q = q->q_next)
8032 if (QS_IS_DEAD(q->q_state))
8036 qsort(addrs, naddrs, sizeof(ADDRESS *), q_qgrp_compare);
8038 /* split into multiple envelopes, by queue group */
8041 e->e_sendqueue = NULL;
8042 for (i = 0; i < naddrs; ++i)
8044 if (i == naddrs - 1 || addrs[i]->q_qgrp != addrs[i + 1]->q_qgrp)
8045 addrs[i]->q_next = NULL;
8047 addrs[i]->q_next = addrs[i + 1];
8049 /* same queue group as original envelope? */
8050 if (addrs[i]->q_qgrp == e->e_qgrp)
8052 if (e->e_sendqueue == NULL)
8053 e->e_sendqueue = addrs[i];
8057 /* different queue group than original envelope */
8058 if (es == NULL || addrs[i]->q_qgrp != es->e_qgrp)
8060 ee = split_env(e, addrs[i], addrs[i]->q_qgrp, NOQDIR);
8062 splits[nsplits++] = ee;
8066 /* no splits? return right now. */
8068 return SM_SPLIT_NONE;
8070 /* assign a queue directory to each additional envelope */
8071 for (i = 0; i < nsplits; ++i)
8075 es->e_qdir = pickqdir(Queue[es->e_qgrp], es->e_msgsize, es);
8077 if (!setnewqueue(es))
8081 /* sort the additional envelopes by queue file system */
8082 qsort(splits, nsplits, sizeof(ENVELOPE *), e_filesys_compare);
8084 /* create data files for each additional envelope */
8085 if (!dup_df(e, splits[0]))
8090 for (i = 1; i < nsplits; ++i)
8092 /* copy or link to the previous data file */
8093 if (!dup_df(splits[i - 1], splits[i]))
8097 /* success: prepend the new envelopes to the e->e_sibling list */
8098 for (i = 0; i < nsplits; ++i)
8101 es->e_sibling = e->e_sibling;
8104 return SM_SPLIT_NEW(nsplits);
8106 /* failure: clean up */
8112 for (j = 0; j < i; j++)
8113 (void) unlink(queuename(splits[j], DATAFL_LETTER));
8115 e->e_sendqueue = addrs[0];
8116 for (i = 0; i < naddrs - 1; ++i)
8117 addrs[i]->q_next = addrs[i + 1];
8118 addrs[naddrs - 1]->q_next = NULL;
8119 return SM_SPLIT_FAIL;
8123 ** SPLIT_WITHIN_QUEUE
8125 ** Split an envelope with multiple recipients into several
8126 ** envelopes within the same queue directory, if the number of
8127 ** recipients exceeds the limit for the queue group.
8133 ** SM_SPLIT_FAIL on failure
8134 ** SM_SPLIT_NONE if no splitting occurred,
8135 ** or 1 + the number of additional envelopes created.
8138 #define SPLIT_LOG_LEVEL 8
8140 static int split_within_queue __P((ENVELOPE *));
8143 split_within_queue(e)
8146 int maxrcpt, nrcpt, ndead, nsplit, i;
8149 ADDRESS *q, **addrs;
8150 ENVELOPE *ee, *firstsibling;
8152 if (!ISVALIDQGRP(e->e_qgrp) || bitset(EF_SPLIT, e->e_flags))
8153 return SM_SPLIT_NONE;
8155 /* don't bother if there is no recipient limit */
8156 maxrcpt = Queue[e->e_qgrp]->qg_maxrcpt;
8158 return SM_SPLIT_NONE;
8160 /* count recipients */
8162 for (q = e->e_sendqueue; q != NULL; q = q->q_next)
8164 if (QS_IS_DEAD(q->q_state))
8168 if (nrcpt <= maxrcpt)
8169 return SM_SPLIT_NONE;
8172 ** Preserve the recipient list
8173 ** so that we can restore it in case of error.
8174 ** (But we discard dead addresses.)
8177 addrs = sm_rpool_malloc_x(e->e_rpool, nrcpt * sizeof(ADDRESS *));
8178 for (i = 0, q = e->e_sendqueue; q != NULL; q = q->q_next)
8180 if (QS_IS_DEAD(q->q_state))
8186 ** Partition the recipient list so that bad and sent addresses
8187 ** come first. These will go with the original envelope, and
8188 ** do not count towards the maxrcpt limit.
8189 ** addrs[] does not contain QS_IS_DEAD() addresses.
8193 for (i = 0; i < nrcpt; ++i)
8195 if (QS_IS_BADADDR(addrs[i]->q_state) ||
8196 QS_IS_SENT(addrs[i]->q_state) ||
8197 QS_IS_DEAD(addrs[i]->q_state)) /* for paranoia's sake */
8201 ADDRESS *tmp = addrs[i];
8203 addrs[i] = addrs[ndead];
8210 /* Check if no splitting required. */
8211 if (nrcpt - ndead <= maxrcpt)
8212 return SM_SPLIT_NONE;
8215 for (i = 0; i < nrcpt - 1; ++i)
8216 addrs[i]->q_next = addrs[i + 1];
8217 addrs[nrcpt - 1]->q_next = NULL;
8218 e->e_sendqueue = addrs[0];
8220 /* prepare buffer for logging */
8221 if (LogLevel > SPLIT_LOG_LEVEL)
8224 lsplits = sm_malloc(l);
8225 if (lsplits != NULL)
8231 /* get rid of stupid compiler warnings */
8236 /* split the envelope */
8237 firstsibling = e->e_sibling;
8238 i = maxrcpt + ndead;
8242 addrs[i - 1]->q_next = NULL;
8243 ee = split_env(e, addrs[i], e->e_qgrp, e->e_qdir);
8250 (void) unlink(queuename(ee, DATAFL_LETTER));
8254 /* Error. Restore e's sibling & recipient lists. */
8255 e->e_sibling = firstsibling;
8256 for (i = 0; i < nrcpt - 1; ++i)
8257 addrs[i]->q_next = addrs[i + 1];
8258 if (lsplits != NULL)
8260 return SM_SPLIT_FAIL;
8263 /* prepend the new envelope to e->e_sibling */
8264 ee->e_sibling = e->e_sibling;
8267 if (LogLevel > SPLIT_LOG_LEVEL && lsplits != NULL)
8269 if (j >= l - strlen(ee->e_id) - 3)
8274 p = sm_realloc(lsplits, l);
8277 /* let's try to get this done */
8284 if (lsplits != NULL)
8287 j += sm_strlcat(lsplits + j,
8291 j += sm_strlcat2(lsplits + j,
8298 if (nrcpt - i <= maxrcpt)
8302 if (LogLevel > SPLIT_LOG_LEVEL && lsplits != NULL)
8306 sm_syslog(LOG_NOTICE, e->e_id,
8307 "split: maxrcpts=%d, rcpts=%d, count=%d, id%s=%s",
8308 maxrcpt, nrcpt - ndead, nsplit,
8309 nsplit > 1 ? "s" : "", lsplits);
8313 return SM_SPLIT_NEW(nsplit);
8316 ** SPLIT_BY_RECIPIENT
8318 ** Split an envelope with multiple recipients into multiple
8319 ** envelopes as required by the sendmail configuration.
8325 ** Returns true on success, false on failure.
8328 ** see split_across_queue_groups(), split_within_queue(e)
8332 split_by_recipient(e)
8335 int split, n, i, j, l;
8337 ENVELOPE *ee, *next, *firstsibling;
8339 if (OpMode == SM_VERIFY || !ISVALIDQGRP(e->e_qgrp) ||
8340 bitset(EF_SPLIT, e->e_flags))
8342 n = split_across_queue_groups(e);
8343 if (n == SM_SPLIT_FAIL)
8345 firstsibling = ee = e->e_sibling;
8346 if (n > 1 && LogLevel > SPLIT_LOG_LEVEL)
8349 lsplits = sm_malloc(l);
8350 if (lsplits != NULL)
8356 /* get rid of stupid compiler warnings */
8360 for (i = 1; i < n; ++i)
8362 next = ee->e_sibling;
8363 if (split_within_queue(ee) == SM_SPLIT_FAIL)
8365 e->e_sibling = firstsibling;
8368 ee->e_flags |= EF_SPLIT;
8369 if (LogLevel > SPLIT_LOG_LEVEL && lsplits != NULL)
8371 if (j >= l - strlen(ee->e_id) - 3)
8376 p = sm_realloc(lsplits, l);
8379 /* let's try to get this done */
8386 if (lsplits != NULL)
8389 j += sm_strlcat(lsplits + j,
8392 j += sm_strlcat2(lsplits + j, "; ",
8399 if (LogLevel > SPLIT_LOG_LEVEL && lsplits != NULL && n > 1)
8401 sm_syslog(LOG_NOTICE, e->e_id, "split: count=%d, id%s=%s",
8402 n - 1, n > 2 ? "s" : "", lsplits);
8405 split = split_within_queue(e) != SM_SPLIT_FAIL;
8407 e->e_flags |= EF_SPLIT;
8412 ** QUARANTINE_QUEUE_ITEM -- {un,}quarantine a single envelope
8414 ** Add/remove quarantine reason and requeue appropriately.
8417 ** qgrp -- queue group for the item
8418 ** qdir -- queue directory in the given queue group
8419 ** e -- envelope information for the item
8420 ** reason -- quarantine reason, NULL means unquarantine.
8423 ** true if item changed, false otherwise
8426 ** Changes quarantine tag in queue file and renames it.
8430 quarantine_queue_item(qgrp, qdir, e, reason)
8437 bool failing = false;
8438 bool foundq = false;
8439 bool finished = false;
8445 MODE_T oldumask = 0;
8446 SM_FILE_T *oldqfp, *tempqfp;
8449 char oldqf[MAXPATHLEN];
8450 char tempqf[MAXPATHLEN];
8451 char newqf[MAXPATHLEN];
8454 oldtype = queue_letter(e, ANYQFL_LETTER);
8455 (void) sm_strlcpy(oldqf, queuename(e, ANYQFL_LETTER), sizeof(oldqf));
8456 (void) sm_strlcpy(tempqf, queuename(e, NEWQFL_LETTER), sizeof(tempqf));
8459 ** Instead of duplicating all the open
8460 ** and lock code here, tell readqf() to
8461 ** do that work and return the open
8462 ** file pointer in e_lockfp. Note that
8463 ** we must release the locks properly when
8467 if (!readqf(e, true))
8469 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8470 "Skipping %s\n", qid_printname(e));
8473 oldqfp = e->e_lockfp;
8475 /* open the new queue file */
8476 flags = O_CREAT|O_WRONLY|O_EXCL;
8477 if (bitset(S_IWGRP, QueueFileMode))
8478 oldumask = umask(002);
8479 fd = open(tempqf, flags, QueueFileMode);
8480 if (bitset(S_IWGRP, QueueFileMode))
8481 (void) umask(oldumask);
8487 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8488 "Skipping %s: Could not open %s: %s\n",
8489 qid_printname(e), tempqf,
8490 sm_errstring(save_errno));
8491 (void) sm_io_close(oldqfp, SM_TIME_DEFAULT);
8494 if (!lockfile(fd, tempqf, NULL, LOCK_EX|LOCK_NB))
8496 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8497 "Skipping %s: Could not lock %s\n",
8498 qid_printname(e), tempqf);
8500 (void) sm_io_close(oldqfp, SM_TIME_DEFAULT);
8504 tempqfp = sm_io_open(SmFtStdiofd, SM_TIME_DEFAULT, (void *) &fd,
8505 SM_IO_WRONLY_B, NULL);
8506 if (tempqfp == NULL)
8508 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8509 "Skipping %s: Could not lock %s\n",
8510 qid_printname(e), tempqf);
8512 (void) sm_io_close(oldqfp, SM_TIME_DEFAULT);
8516 /* Copy the data over, changing the quarantine reason */
8517 while (bufsize = sizeof(buf),
8518 (bp = fgetfolded(buf, &bufsize, oldqfp)) != NULL)
8521 sm_dprintf("+++++ %s\n", bp);
8524 case 'q': /* quarantine reason */
8530 (void) sm_io_fprintf(smioout,
8532 "%s: Removed quarantine of \"%s\"\n",
8535 sm_syslog(LOG_INFO, e->e_id, "unquarantine");
8538 else if (strcmp(reason, &bp[1]) == 0)
8542 (void) sm_io_fprintf(smioout,
8544 "%s: Already quarantined with \"%s\"\n",
8547 (void) sm_io_fprintf(tempqfp, SM_TIME_DEFAULT,
8554 (void) sm_io_fprintf(smioout,
8556 "%s: Quarantine changed from \"%s\" to \"%s\"\n",
8560 (void) sm_io_fprintf(tempqfp, SM_TIME_DEFAULT,
8562 sm_syslog(LOG_INFO, e->e_id, "quarantine=%s",
8570 ** If we are quarantining an unquarantined item,
8571 ** need to put in a new 'q' line before it's
8575 if (!foundq && reason != NULL)
8579 (void) sm_io_fprintf(smioout,
8581 "%s: Quarantined with \"%s\"\n",
8584 (void) sm_io_fprintf(tempqfp, SM_TIME_DEFAULT,
8586 sm_syslog(LOG_INFO, e->e_id, "quarantine=%s",
8592 /* Copy the line to the new file */
8593 (void) sm_io_fprintf(tempqfp, SM_TIME_DEFAULT,
8602 /* Copy the line to the new file */
8603 (void) sm_io_fprintf(tempqfp, SM_TIME_DEFAULT,
8611 /* Make sure we read the whole old file */
8612 errno = sm_io_error(tempqfp);
8613 if (errno != 0 && errno != SM_IO_EOF)
8616 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8617 "Skipping %s: Error reading %s: %s\n",
8618 qid_printname(e), oldqf,
8619 sm_errstring(save_errno));
8623 if (!failing && !finished)
8625 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8626 "Skipping %s: Incomplete file: %s\n",
8627 qid_printname(e), oldqf);
8631 /* Check if we actually changed anything or we can just bail now */
8634 /* pretend we failed, even though we technically didn't */
8638 /* Make sure we wrote things out safely */
8640 (sm_io_flush(tempqfp, SM_TIME_DEFAULT) != 0 ||
8641 ((SuperSafe == SAFE_REALLY ||
8642 SuperSafe == SAFE_REALLY_POSTMILTER ||
8643 SuperSafe == SAFE_INTERACTIVE) &&
8644 fsync(sm_io_getinfo(tempqfp, SM_IO_WHAT_FD, NULL)) < 0) ||
8645 ((errno = sm_io_error(tempqfp)) != 0)))
8648 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8649 "Skipping %s: Error writing %s: %s\n",
8650 qid_printname(e), tempqf,
8651 sm_errstring(save_errno));
8656 /* Figure out the new filename */
8657 newtype = (reason == NULL ? NORMQF_LETTER : QUARQF_LETTER);
8658 if (oldtype == newtype)
8660 /* going to rename tempqf to oldqf */
8661 (void) sm_strlcpy(newqf, oldqf, sizeof(newqf));
8665 /* going to rename tempqf to new name based on newtype */
8666 (void) sm_strlcpy(newqf, queuename(e, newtype), sizeof(newqf));
8671 /* rename tempqf to newqf */
8673 rename(tempqf, newqf) < 0)
8674 save_errno = (errno == 0) ? EINVAL : errno;
8676 /* Check rename() success */
8677 if (!failing && save_errno != 0)
8679 sm_syslog(LOG_DEBUG, e->e_id,
8680 "quarantine_queue_item: rename(%s, %s): %s",
8681 tempqf, newqf, sm_errstring(save_errno));
8683 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8684 "Error renaming %s to %s: %s\n",
8686 sm_errstring(save_errno));
8687 if (oldtype == newtype)
8690 ** Bail here since we don't know the state of
8691 ** the filesystem and may need to keep tempqf
8692 ** for the user to rescue us.
8697 syserr("!452 Error renaming control file %s", tempqf);
8702 /* remove new file (if rename() half completed) */
8703 if (xunlink(newqf) < 0)
8706 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8707 "Error removing %s: %s\n",
8709 sm_errstring(save_errno));
8712 /* tempqf removed below */
8718 /* If changing file types, need to remove old type */
8719 if (!failing && oldtype != newtype)
8721 if (xunlink(oldqf) < 0)
8724 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8725 "Error removing %s: %s\n",
8726 oldqf, sm_errstring(save_errno));
8730 /* see if anything above failed */
8733 /* Something failed: remove new file, old file still there */
8734 (void) xunlink(tempqf);
8738 ** fsync() after file operations to make sure metadata is
8739 ** written to disk on filesystems in which renames are
8740 ** not guaranteed. It's ok if they fail, mail won't be lost.
8743 if (SuperSafe != SAFE_NO)
8745 /* for soft-updates */
8746 (void) fsync(sm_io_getinfo(tempqfp,
8747 SM_IO_WHAT_FD, NULL));
8751 /* for soft-updates */
8752 (void) fsync(sm_io_getinfo(oldqfp,
8753 SM_IO_WHAT_FD, NULL));
8756 /* for other odd filesystems */
8757 SYNC_DIR(tempqf, false);
8762 if (tempqfp != NULL)
8763 (void) sm_io_close(tempqfp, SM_TIME_DEFAULT);
8765 (void) sm_io_close(oldqfp, SM_TIME_DEFAULT);
8772 ** QUARANTINE_QUEUE -- {un,}quarantine matching items in the queue
8774 ** Read all matching queue items, add/remove quarantine
8775 ** reason, and requeue appropriately.
8778 ** reason -- quarantine reason, "." means unquarantine.
8779 ** qgrplimit -- limit to single queue group unless NOQGRP
8785 ** Lots of changes to the queue.
8789 quarantine_queue(reason, qgrplimit)
8796 /* Convert internal representation of unquarantine */
8797 if (reason != NULL && reason[0] == '.' && reason[1] == '\0')
8803 reason = newstr(denlstring(reason, true, true));
8806 for (qgrp = 0; qgrp < NumQueue && Queue[qgrp] != NULL; qgrp++)
8810 if (qgrplimit != NOQGRP && qgrplimit != qgrp)
8813 for (qdir = 0; qdir < Queue[qgrp]->qg_numqueues; qdir++)
8821 nrequests = gatherq(qgrp, qdir, true, NULL, NULL);
8823 /* first see if there is anything */
8828 (void) sm_io_fprintf(smioout,
8829 SM_TIME_DEFAULT, "%s: no matches\n",
8830 qid_printqueue(qgrp, qdir));
8837 (void) sm_io_fprintf(smioout,
8838 SM_TIME_DEFAULT, "Processing %s:\n",
8839 qid_printqueue(qgrp, qdir));
8842 for (i = 0; i < WorkListCount; i++)
8849 /* setup envelope */
8850 clearenvelope(&e, true, sm_rpool_new_x(NULL));
8851 e.e_id = WorkList[i].w_name + 2;
8857 sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8858 "Would do %s\n", e.e_id);
8861 else if (quarantine_queue_item(qgrp, qdir,
8866 sm_rpool_free(e.e_rpool);
8869 if (WorkList != NULL)
8870 sm_free(WorkList); /* XXX */
8879 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8882 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8885 changed == 1 ? "" : "s");