2 * Copyright (c) 1998-2003 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.
16 SM_RCSID("@(#)$Id: queue.c,v 8.863.2.61 2003/09/03 19:58:26 ca Exp $")
20 # define RELEASE_QUEUE (void) 0
21 # define ST_INODE(st) (st).st_ino
23 # define sm_file_exists(errno) ((errno) == EEXIST)
25 # define TF_OPEN_FLAGS (O_CREAT|O_WRONLY|O_EXCL)
29 ** QF_VERSION == 4 was sendmail 8.10/8.11 without _FFR_QUEUEDELAY
30 ** QF_VERSION == 5 was sendmail 8.10/8.11 with _FFR_QUEUEDELAY
31 ** QF_VERSION == 6 is sendmail 8.12 without _FFR_QUEUEDELAY
32 ** QF_VERSION == 7 is sendmail 8.12 with _FFR_QUEUEDELAY
36 # define QF_VERSION 7 /* version number of this queue format */
37 static time_t queuedelay __P((ENVELOPE *));
38 # define queuedelay_qfver_unsupported(qfver) false
39 #else /* _FFR_QUEUEDELAY */
40 # define QF_VERSION 6 /* version number of this queue format */
41 # define queuedelay(e) MinQueueAge
42 # define queuedelay_qfver_unsupported(qfver) ((qfver) == 5 || (qfver) == 7)
43 #endif /* _FFR_QUEUEDELAY */
45 static char queue_letter __P((ENVELOPE *, int));
46 static bool quarantine_queue_item __P((int, int, ENVELOPE *, char *));
47 #endif /* _FFR_QUARANTINE */
49 /* Naming convention: qgrp: index of queue group, qg: QUEUEGROUP */
57 char *w_name; /* name of control file */
58 char *w_host; /* name of recipient host */
59 bool w_lock; /* is message locked? */
60 bool w_tooyoung; /* is it too young to run? */
61 long w_pri; /* priority of message, see below */
62 time_t w_ctime; /* creation time */
63 time_t w_mtime; /* modification time */
64 int w_qgrp; /* queue group located in */
65 int w_qdir; /* queue directory located in */
66 struct work *w_next; /* next in queue */
69 typedef struct work WORK;
71 static WORK *WorkQ; /* queue of things to be done */
72 static int NumWorkGroups; /* number of work groups */
73 static time_t Current_LA_time = 0;
75 /* Get new load average every 30 seconds. */
76 #define GET_NEW_LA_TIME 30
78 #define SM_GET_LA(now) \
82 if (Current_LA_time < now - GET_NEW_LA_TIME) \
85 Current_LA_time = now; \
90 ** DoQueueRun indicates that a queue run is needed.
91 ** Notice: DoQueueRun is modified in a signal handler!
94 static bool volatile DoQueueRun; /* non-interrupt time queue run needed */
97 ** Work group definition structure.
98 ** Each work group contains one or more queue groups. This is done
99 ** to manage the number of queue group runners active at the same time
100 ** to be within the constraints of MaxQueueChildren (if it is set).
101 ** The number of queue groups that can be run on the next work run
102 ** is kept track of. The queue groups are run in a round robin.
107 int wg_numqgrp; /* number of queue groups in work grp */
108 int wg_runners; /* total runners */
109 int wg_curqgrp; /* current queue group */
110 QUEUEGRP **wg_qgs; /* array of queue groups */
111 int wg_maxact; /* max # of active runners */
112 time_t wg_lowqintvl; /* lowest queue interval */
113 int wg_restart; /* needs restarting? */
114 int wg_restartcnt; /* count of times restarted */
117 typedef struct workgrp WORKGRP;
119 static WORKGRP volatile WorkGrp[MAXWORKGROUPS + 1]; /* work groups */
122 static SM_DEBUG_T DebugLeakQ = SM_DEBUG_INITIALIZER("leak_q",
123 "@(#)$Debug: leak_q - trace memory leaks during queue processing $");
124 #endif /* SM_HEAP_CHECK */
127 ** We use EmptyString instead of "" to avoid
128 ** 'zero-length format string' warnings from gcc
131 static const char EmptyString[] = "";
133 static void grow_wlist __P((int, int));
134 static int multiqueue_cache __P((char *, int, QUEUEGRP *, int, unsigned int *));
135 static int gatherq __P((int, int, bool, bool *, bool *));
136 static int sortq __P((int));
137 static void printctladdr __P((ADDRESS *, SM_FILE_T *));
138 static bool readqf __P((ENVELOPE *, bool));
139 static void restart_work_group __P((int));
140 static void runner_work __P((ENVELOPE *, int, bool, int, int));
141 static void schedule_queue_runs __P((bool, int, bool));
142 static char *strrev __P((char *));
143 static ADDRESS *setctluser __P((char *, int, ENVELOPE *));
145 static int sm_strshufflecmp __P((char *, char *));
146 static void init_shuffle_alphabet __P(());
147 #endif /* _FFR_RHS */
148 static int workcmpf0();
149 static int workcmpf1();
150 static int workcmpf2();
151 static int workcmpf3();
152 static int workcmpf4();
153 static int randi = 3; /* index for workcmpf5() */
154 static int workcmpf5();
155 static int workcmpf6();
157 static int workcmpf7();
158 #endif /* _FFR_RHS */
161 # define get_rand_mod(m) ((get_random() >> RANDOMSHIFT) % (m))
162 #else /* RANDOMSHIFT */
163 # define get_rand_mod(m) (get_random() % (m))
164 #endif /* RANDOMSHIFT */
167 ** File system definition.
168 ** Used to keep track of how much free space is available
169 ** on a file system in which one or more queue directories reside.
172 typedef struct filesys_shared FILESYS;
174 struct filesys_shared
176 dev_t fs_dev; /* unique device id */
177 long fs_avail; /* number of free blocks available */
178 long fs_blksize; /* block size, in bytes */
181 /* probably kept in shared memory */
182 static FILESYS FileSys[MAXFILESYS]; /* queue file systems */
183 static char *FSPath[MAXFILESYS]; /* pathnames for file systems */
188 ** Shared memory data
191 ** size -- size of shared memory segment
192 ** pid -- pid of owner, should be a unique id to avoid misinterpretations
193 ** by other processes.
194 ** tag -- should be a unique id to avoid misinterpretations by others.
195 ** idea: hash over configuration data that will be stored here.
196 ** NumFileSys -- number of file systems.
197 ** FileSys -- (arrary of) structure for used file systems.
198 ** RSATmpCnt -- counter for number of uses of ephemeral RSA key.
199 ** QShm -- (array of) structure for information about queue directories.
203 ** Queue data in shared memory
206 typedef struct queue_shared QUEUE_SHM_T;
210 int qs_entries; /* number of entries */
211 /* XXX more to follow? */
214 static void *Pshm; /* pointer to shared memory */
215 static FILESYS *PtrFileSys; /* pointer to queue file system array */
216 int ShmId = SM_SHM_NO_ID; /* shared memory id */
217 static QUEUE_SHM_T *QShm; /* pointer to shared queue data */
220 # define SHM_OFF_PID(p) (((char *) (p)) + sizeof(int))
221 # define SHM_OFF_TAG(p) (((char *) (p)) + sizeof(pid_t) + sizeof(int))
222 # define SHM_OFF_HEAD (sizeof(pid_t) + sizeof(int) * 2)
224 /* how to access FileSys */
225 # define FILE_SYS(i) (PtrFileSys[i])
227 /* first entry is a tag, for now just the size */
228 # define OFF_FILE_SYS(p) (((char *) (p)) + SHM_OFF_HEAD)
230 /* offset for PNumFileSys */
231 # define OFF_NUM_FILE_SYS(p) (((char *) (p)) + SHM_OFF_HEAD + sizeof(FileSys))
233 /* offset for PRSATmpCnt */
234 # define OFF_RSA_TMP_CNT(p) (((char *) (p)) + SHM_OFF_HEAD + sizeof(FileSys) + sizeof(int))
237 /* offset for queue_shm */
238 # define OFF_QUEUE_SHM(p) (((char *) (p)) + SHM_OFF_HEAD + sizeof(FileSys) + sizeof(int) * 2)
240 # define QSHM_ENTRIES(i) QShm[i].qs_entries
242 /* basic size of shared memory segment */
243 # define SM_T_SIZE (SHM_OFF_HEAD + sizeof(FileSys) + sizeof(int) * 2)
245 static unsigned int hash_q __P((char *, unsigned int));
248 ** HASH_Q -- simple hash function
251 ** p -- string to hash.
252 ** h -- hash start value (from previous run).
270 h += (c<<11) ^ (c>>1);
271 h ^= (d<<14) + (d<<7) + (d<<4) + d;
277 #else /* SM_CONF_SHM */
278 # define FILE_SYS(i) FileSys[i]
279 #endif /* SM_CONF_SHM */
281 /* access to the various components of file system data */
282 #define FILE_SYS_NAME(i) FSPath[i]
283 #define FILE_SYS_AVAIL(i) FILE_SYS(i).fs_avail
284 #define FILE_SYS_BLKSIZE(i) FILE_SYS(i).fs_blksize
285 #define FILE_SYS_DEV(i) FILE_SYS(i).fs_dev
289 ** Current qf file field assignments:
293 ** C controlling user
295 ** d data file directory name (added in 8.12)
298 ** G queue delay algorithm (_FFR_QUEUEDELAY)
300 ** I data file's inode number
301 ** K time of last delivery attempt
302 ** L Solaris Content-Length: header (obsolete)
304 ** N number of delivery attempts
305 ** P message priority
306 ** q quarantine reason (_FFR_QUARANTINE)
307 ** Q original recipient (ORCPT=)
308 ** r final recipient (Final-Recipient: DSN field)
312 ** V queue file version
313 ** X free (was: character set if _FFR_SAVE_CHARSET)
314 ** Y current delay (_FFR_QUEUEDELAY)
315 ** Z original envelope id from ESMTP
316 ** ! deliver by (added in 8.12)
322 ** QUEUEUP -- queue a message up for future transmission.
325 ** e -- the envelope to queue up.
326 ** announce -- if true, tell when you are queueing up.
327 ** msync -- if true, then fsync() if SuperSafe interactive mode.
333 ** The current request is saved in a control file.
334 ** The queue file is left locked.
338 queueup(e, announce, msync)
339 register ENVELOPE *e;
343 register SM_FILE_T *tfp;
358 ** Create control file.
361 newid = (e->e_id == NULL) || !bitset(EF_INQUEUE, e->e_flags);
362 (void) sm_strlcpy(tf, queuename(e, NEWQFL_LETTER), sizeof tf);
364 if (tfp == NULL && newid)
367 ** open qf file directly: this will give an error if the file
368 ** already exists and hence prevent problems if a queue-id
369 ** is reused (e.g., because the clock is set back).
372 (void) sm_strlcpy(tf, queuename(e, ANYQFL_LETTER), sizeof tf);
373 tfd = open(tf, TF_OPEN_FLAGS, FileMode);
375 !lockfile(tfd, tf, NULL, LOCK_EX|LOCK_NB) ||
376 (tfp = sm_io_open(SmFtStdiofd, SM_TIME_DEFAULT,
377 (void *) &tfd, SM_IO_WRONLY_B,
380 int save_errno = errno;
384 syserr("!queueup: cannot create queue file %s, euid=%d",
385 tf, (int) geteuid());
391 /* if newid, write the queue file directly (instead of temp file) */
394 /* get a locked tf file */
395 for (i = 0; i < 128; i++)
401 if (bitset(S_IWGRP, QueueFileMode))
402 oldumask = umask(002);
403 tfd = open(tf, TF_OPEN_FLAGS, QueueFileMode);
404 if (bitset(S_IWGRP, QueueFileMode))
405 (void) umask(oldumask);
411 if (LogLevel > 0 && (i % 32) == 0)
412 sm_syslog(LOG_ALERT, e->e_id,
413 "queueup: cannot create %s, uid=%d: %s",
415 sm_errstring(errno));
420 if (lockfile(tfd, tf, NULL, LOCK_EX|LOCK_NB))
422 else if (LogLevel > 0 && (i % 32) == 0)
423 sm_syslog(LOG_ALERT, e->e_id,
424 "queueup: cannot lock %s: %s",
425 tf, sm_errstring(errno));
435 /* save the old temp file away */
436 (void) rename(tf, queuename(e, TEMPQF_LETTER));
439 (void) sleep(i % 32);
441 if (tfd < 0 || (tfp = sm_io_open(SmFtStdiofd, SM_TIME_DEFAULT,
442 (void *) &tfd, SM_IO_WRONLY_B,
445 int save_errno = errno;
449 syserr("!queueup: cannot create queue temp file %s, uid=%d",
450 tf, (int) geteuid());
455 sm_dprintf("\n>>>>> queueing %s/%s%s >>>>>\n",
456 qid_printqueue(e->e_qgrp, e->e_qdir),
457 queuename(e, ANYQFL_LETTER),
458 newid ? " (new id)" : "");
461 sm_dprintf(" e_flags=");
466 sm_dprintf(" sendq=");
467 printaddr(e->e_sendqueue, true);
472 dumpfd(sm_io_getinfo(tfp, SM_IO_WHAT_FD, NULL), true, false);
473 sm_dprintf(" lockfp=");
474 if (e->e_lockfp == NULL)
475 sm_dprintf("NULL\n");
477 dumpfd(sm_io_getinfo(e->e_lockfp, SM_IO_WHAT_FD, NULL),
482 ** If there is no data file yet, create one.
485 (void) sm_strlcpy(df, queuename(e, DATAFL_LETTER), sizeof df);
486 if (bitset(EF_HAS_DF, e->e_flags))
488 if (e->e_dfp != NULL &&
489 SuperSafe != SAFE_REALLY &&
490 sm_io_setinfo(e->e_dfp, SM_BF_COMMIT, NULL) < 0 &&
493 syserr("!queueup: cannot commit data file %s, uid=%d",
494 queuename(e, DATAFL_LETTER), (int) geteuid());
496 if (e->e_dfp != NULL &&
497 SuperSafe == SAFE_INTERACTIVE && msync)
500 sm_syslog(LOG_INFO, e->e_id,
501 "queueup: fsync(e->e_dfp)");
503 if (fsync(sm_io_getinfo(e->e_dfp, SM_IO_WHAT_FD,
507 syserr("!552 Error writing data file %s",
510 syserr("!452 Error writing data file %s",
519 register SM_FILE_T *dfp = NULL;
522 if (e->e_dfp != NULL &&
523 sm_io_getinfo(e->e_dfp, SM_IO_WHAT_ISTYPE, BF_FILE_TYPE))
524 syserr("committing over bf file");
526 if (bitset(S_IWGRP, QueueFileMode))
527 oldumask = umask(002);
528 dfd = open(df, O_WRONLY|O_CREAT|O_TRUNC|QF_O_EXTRA,
530 if (bitset(S_IWGRP, QueueFileMode))
531 (void) umask(oldumask);
532 if (dfd < 0 || (dfp = sm_io_open(SmFtStdiofd, SM_TIME_DEFAULT,
533 (void *) &dfd, SM_IO_WRONLY_B,
535 syserr("!queueup: cannot create data temp file %s, uid=%d",
536 df, (int) geteuid());
537 if (fstat(dfd, &stbuf) < 0)
541 e->e_dfdev = stbuf.st_dev;
542 e->e_dfino = ST_INODE(stbuf);
544 e->e_flags |= EF_HAS_DF;
545 memset(&mcibuf, '\0', sizeof mcibuf);
546 mcibuf.mci_out = dfp;
547 mcibuf.mci_mailer = FileMailer;
548 (*e->e_putbody)(&mcibuf, e, NULL);
550 if (SuperSafe == SAFE_REALLY ||
551 (SuperSafe == SAFE_INTERACTIVE && msync))
554 sm_syslog(LOG_INFO, e->e_id,
555 "queueup: fsync(dfp)");
557 if (fsync(sm_io_getinfo(dfp, SM_IO_WHAT_FD, NULL)) < 0)
560 syserr("!552 Error writing data file %s",
563 syserr("!452 Error writing data file %s",
568 if (sm_io_close(dfp, SM_TIME_DEFAULT) < 0)
569 syserr("!queueup: cannot save data temp file %s, uid=%d",
570 df, (int) geteuid());
571 e->e_putbody = putbody;
575 ** Output future work requests.
576 ** Priority and creation time should be first, since
577 ** they are required by gatherq.
580 /* output queue version number (must be first!) */
581 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "V%d\n", QF_VERSION);
583 /* output creation time */
584 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "T%ld\n", (long) e->e_ctime);
586 /* output last delivery time */
588 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "K%ld\n", (long) e->e_dtime);
589 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "G%d\n", e->e_queuealg);
590 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "Y%ld\n", (long) e->e_queuedelay);
592 sm_syslog(LOG_INFO, e->e_id,
593 "queue alg: %d delay %ld next: %ld (now: %ld)\n",
594 e->e_queuealg, e->e_queuedelay, e->e_dtime, curtime());
595 #else /* _FFR_QUEUEDELAY */
596 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "K%ld\n", (long) e->e_dtime);
597 #endif /* _FFR_QUEUEDELAY */
599 /* output number of delivery attempts */
600 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "N%d\n", e->e_ntries);
602 /* output message priority */
603 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "P%ld\n", e->e_msgpriority);
606 ** If data file is in a different directory than the queue file,
607 ** output a "d" record naming the directory of the data file.
610 if (e->e_dfqgrp != e->e_qgrp)
612 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "d%s\n",
613 Queue[e->e_dfqgrp]->qg_qpaths[e->e_dfqdir].qp_name);
616 /* output inode number of data file */
617 /* XXX should probably include device major/minor too */
618 if (e->e_dfino != -1)
620 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "I%ld/%ld/%llu\n",
621 (long) major(e->e_dfdev),
622 (long) minor(e->e_dfdev),
623 (ULONGLONG_T) e->e_dfino);
626 /* output body type */
627 if (e->e_bodytype != NULL)
628 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "B%s\n",
629 denlstring(e->e_bodytype, true, false));
632 /* quarantine reason */
633 if (e->e_quarmsg != NULL)
634 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "q%s\n",
635 denlstring(e->e_quarmsg, true, false));
636 #endif /* _FFR_QUARANTINE */
638 /* message from envelope, if it exists */
639 if (e->e_message != NULL)
640 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "M%s\n",
641 denlstring(e->e_message, true, false));
643 /* send various flag bits through */
645 if (bitset(EF_WARNING, e->e_flags))
647 if (bitset(EF_RESPONSE, e->e_flags))
649 if (bitset(EF_HAS8BIT, e->e_flags))
651 if (bitset(EF_DELETE_BCC, e->e_flags))
653 if (bitset(EF_RET_PARAM, e->e_flags))
655 if (bitset(EF_NO_BODY_RETN, e->e_flags))
657 if (bitset(EF_SPLIT, e->e_flags))
661 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "F%s\n", buf);
663 /* save $={persistentMacros} macro values */
664 queueup_macros(macid("{persistentMacros}"), tfp, e);
666 /* output name of sender */
667 if (bitnset(M_UDBENVELOPE, e->e_from.q_mailer->m_flags))
670 p = e->e_from.q_paddr;
671 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "S%s\n",
672 denlstring(p, true, false));
674 /* output ESMTP-supplied "original" information */
675 if (e->e_envid != NULL)
676 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "Z%s\n",
677 denlstring(e->e_envid, true, false));
679 /* output AUTH= parameter */
680 if (e->e_auth_param != NULL)
681 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "A%s\n",
682 denlstring(e->e_auth_param, true, false));
683 if (e->e_dlvr_flag != 0)
684 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "!%c %ld\n",
685 (char) e->e_dlvr_flag, e->e_deliver_by);
687 /* output list of recipient addresses */
688 printctladdr(NULL, NULL);
689 for (q = e->e_sendqueue; q != NULL; q = q->q_next)
691 if (!QS_IS_UNDELIVERED(q->q_state))
694 /* message for this recipient, if it exists */
695 if (q->q_message != NULL)
696 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "M%s\n",
697 denlstring(q->q_message, true,
700 printctladdr(q, tfp);
701 if (q->q_orcpt != NULL)
702 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "Q%s\n",
703 denlstring(q->q_orcpt, true,
705 if (q->q_finalrcpt != NULL)
706 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "r%s\n",
707 denlstring(q->q_finalrcpt, true,
709 (void) sm_io_putc(tfp, SM_TIME_DEFAULT, 'R');
710 if (bitset(QPRIMARY, q->q_flags))
711 (void) sm_io_putc(tfp, SM_TIME_DEFAULT, 'P');
712 if (bitset(QHASNOTIFY, q->q_flags))
713 (void) sm_io_putc(tfp, SM_TIME_DEFAULT, 'N');
714 if (bitset(QPINGONSUCCESS, q->q_flags))
715 (void) sm_io_putc(tfp, SM_TIME_DEFAULT, 'S');
716 if (bitset(QPINGONFAILURE, q->q_flags))
717 (void) sm_io_putc(tfp, SM_TIME_DEFAULT, 'F');
718 if (bitset(QPINGONDELAY, q->q_flags))
719 (void) sm_io_putc(tfp, SM_TIME_DEFAULT, 'D');
720 if (q->q_alias != NULL &&
721 bitset(QALIAS, q->q_alias->q_flags))
722 (void) sm_io_putc(tfp, SM_TIME_DEFAULT, 'A');
723 (void) sm_io_putc(tfp, SM_TIME_DEFAULT, ':');
724 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "%s\n",
725 denlstring(q->q_paddr, true, false));
728 char *tag = "queued";
731 if (e->e_quarmsg != NULL)
733 #endif /* _FFR_QUARANTINE */
735 e->e_to = q->q_paddr;
738 logdelivery(q->q_mailer, NULL, q->q_status,
739 tag, NULL, (time_t) 0, e);
744 sm_dprintf("queueing ");
750 ** Output headers for this message.
751 ** Expand macros completely here. Queue run will deal with
752 ** everything as absolute headers.
753 ** All headers that must be relative to the recipient
754 ** can be cracked later.
755 ** We set up a "null mailer" -- i.e., a mailer that will have
756 ** no effect on the addresses as they are output.
759 memset((char *) &nullmailer, '\0', sizeof nullmailer);
760 nullmailer.m_re_rwset = nullmailer.m_rh_rwset =
761 nullmailer.m_se_rwset = nullmailer.m_sh_rwset = -1;
762 nullmailer.m_eol = "\n";
763 memset(&mcibuf, '\0', sizeof mcibuf);
764 mcibuf.mci_mailer = &nullmailer;
765 mcibuf.mci_out = tfp;
767 macdefine(&e->e_macro, A_PERM, 'g', "\201f");
768 for (h = e->e_header; h != NULL; h = h->h_link)
770 if (h->h_value == NULL)
773 /* don't output resent headers on non-resent messages */
774 if (bitset(H_RESENT, h->h_flags) &&
775 !bitset(EF_RESENT, e->e_flags))
778 /* expand macros; if null, don't output header at all */
779 if (bitset(H_DEFAULT, h->h_flags))
781 (void) expand(h->h_value, buf, sizeof buf, e);
786 /* output this header */
787 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "H?");
789 /* output conditional macro if present */
790 if (h->h_macro != '\0')
792 if (bitset(0200, h->h_macro))
793 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT,
795 macname(bitidx(h->h_macro)));
797 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT,
800 else if (!bitzerop(h->h_mflags) &&
801 bitset(H_CHECK|H_ACHECK, h->h_flags))
805 /* if conditional, output the set of conditions */
806 for (j = '\0'; j <= '\177'; j++)
807 if (bitnset(j, h->h_mflags))
808 (void) sm_io_putc(tfp, SM_TIME_DEFAULT,
811 (void) sm_io_putc(tfp, SM_TIME_DEFAULT, '?');
813 /* output the header: expand macros, convert addresses */
814 if (bitset(H_DEFAULT, h->h_flags) &&
815 !bitset(H_BINDLATE, h->h_flags))
817 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "%s: %s\n",
819 denlstring(buf, false, true));
821 else if (bitset(H_FROM|H_RCPT, h->h_flags) &&
822 !bitset(H_BINDLATE, h->h_flags))
824 bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
825 SM_FILE_T *savetrace = TrafficLogFile;
827 TrafficLogFile = NULL;
829 if (bitset(H_FROM, h->h_flags))
832 commaize(h, h->h_value, oldstyle, &mcibuf, e);
834 TrafficLogFile = savetrace;
838 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "%s: %s\n",
840 denlstring(h->h_value, false,
848 ** Write a terminator record -- this is to prevent
849 ** scurrilous crackers from appending any data.
852 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, ".\n");
854 if (sm_io_flush(tfp, SM_TIME_DEFAULT) != 0 ||
855 ((SuperSafe == SAFE_REALLY ||
856 (SuperSafe == SAFE_INTERACTIVE && msync)) &&
857 fsync(sm_io_getinfo(tfp, SM_IO_WHAT_FD, NULL)) < 0) ||
861 syserr("!552 Error writing control file %s", tf);
863 syserr("!452 Error writing control file %s", tf);
869 char new = queue_letter(e, ANYQFL_LETTER);
870 #endif /* _FFR_QUARANTINE */
872 /* rename (locked) tf to be (locked) [qh]f */
873 (void) sm_strlcpy(qf, queuename(e, ANYQFL_LETTER),
875 if (rename(tf, qf) < 0)
876 syserr("cannot rename(%s, %s), uid=%d",
877 tf, qf, (int) geteuid());
882 ** Check if type has changed and only
883 ** remove the old item if the rename above
887 if (e->e_qfletter != '\0' &&
888 e->e_qfletter != new)
892 sm_dprintf("type changed from %c to %c\n",
896 if (unlink(queuename(e, e->e_qfletter)) < 0)
898 /* XXX: something more drastic? */
900 sm_syslog(LOG_ERR, e->e_id,
901 "queueup: unlink(%s) failed: %s",
902 queuename(e, e->e_qfletter),
903 sm_errstring(errno));
908 # endif /* _FFR_QUARANTINE */
911 ** fsync() after renaming to make sure metadata is
912 ** written to disk on filesystems in which renames are
916 if (SuperSafe != SAFE_NO)
918 /* for softupdates */
919 if (tfd >= 0 && fsync(tfd) < 0)
921 syserr("!queueup: cannot fsync queue temp file %s",
927 /* close and unlock old (locked) queue file */
928 if (e->e_lockfp != NULL)
929 (void) sm_io_close(e->e_lockfp, SM_TIME_DEFAULT);
934 sm_syslog(LOG_DEBUG, e->e_id, "queueup %s", qf);
940 sm_syslog(LOG_DEBUG, e->e_id, "queueup %s", tf);
943 e->e_qfletter = queue_letter(e, ANYQFL_LETTER);
944 #endif /* _FFR_QUARANTINE */
948 e->e_flags |= EF_INQUEUE;
951 sm_dprintf("<<<<< done queueing %s <<<<<\n\n", e->e_id);
956 ** PRINTCTLADDR -- print control address to file.
960 ** tfp -- file pointer.
966 ** The control address (if changed) is printed to the file.
967 ** The last control address and uid are saved.
979 static ADDRESS *lastctladdr = NULL;
980 static uid_t lastuid;
983 if (a == NULL || a->q_alias == NULL || tfp == NULL)
985 if (lastctladdr != NULL && tfp != NULL)
986 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "C\n");
992 /* find the active uid */
1002 user = q->q_ruser != NULL ? q->q_ruser : q->q_user;
1008 /* check to see if this is the same as last time */
1009 if (lastctladdr != NULL && uid == lastuid &&
1010 strcmp(lastctladdr->q_paddr, a->q_paddr) == 0)
1015 if (uid == 0 || user == NULL || user[0] == '\0')
1016 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "C");
1018 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "C%s:%ld:%ld",
1019 denlstring(user, true, false), (long) uid,
1021 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, ":%s\n",
1022 denlstring(a->q_paddr, true, false));
1026 ** RUNNERS_SIGTERM -- propagate a SIGTERM to queue runner process
1028 ** This propagates the signal to the child processes that are queue
1029 ** runners. This is for a queue runner "cleanup". After all of the
1030 ** child queue runner processes are signaled (it should be SIGTERM
1031 ** being the sig) then the old signal handler (Oldsh) is called
1032 ** to handle any cleanup set for this process (provided it is not
1033 ** SIG_DFL or SIG_IGN). The signal may not be handled immediately
1034 ** if the BlockOldsh flag is set. If the current process doesn't
1035 ** have a parent then handle the signal immediately, regardless of
1039 ** sig -- the signal number being sent
1045 ** Sets the NoMoreRunners boolean to true to stop more runners
1046 ** from being started in runqueue().
1048 ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
1049 ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
1053 static bool volatile NoMoreRunners = false;
1054 static sigfunc_t Oldsh_term = SIG_DFL;
1055 static sigfunc_t Oldsh_hup = SIG_DFL;
1056 static sigfunc_t volatile Oldsh = SIG_DFL;
1057 static bool BlockOldsh = false;
1058 static int volatile Oldsig = 0;
1059 static SIGFUNC_DECL runners_sigterm __P((int));
1060 static SIGFUNC_DECL runners_sighup __P((int));
1063 runners_sigterm(sig)
1066 int save_errno = errno;
1068 FIX_SYSV_SIGNAL(sig, runners_sigterm);
1070 CHECK_CRITICAL(sig);
1071 NoMoreRunners = true;
1074 proc_list_signal(PROC_QUEUE, sig);
1076 if (!BlockOldsh || getppid() <= 1)
1078 /* Check that a valid 'old signal handler' is callable */
1079 if (Oldsh_term != SIG_DFL && Oldsh_term != SIG_IGN &&
1080 Oldsh_term != runners_sigterm)
1084 return SIGFUNC_RETURN;
1087 ** RUNNERS_SIGHUP -- propagate a SIGHUP to queue runner process
1089 ** This propagates the signal to the child processes that are queue
1090 ** runners. This is for a queue runner "cleanup". After all of the
1091 ** child queue runner processes are signaled (it should be SIGHUP
1092 ** being the sig) then the old signal handler (Oldsh) is called to
1093 ** handle any cleanup set for this process (provided it is not SIG_DFL
1094 ** or SIG_IGN). The signal may not be handled immediately if the
1095 ** BlockOldsh flag is set. If the current process doesn't have
1096 ** a parent then handle the signal immediately, regardless of
1100 ** sig -- the signal number being sent
1106 ** Sets the NoMoreRunners boolean to true to stop more runners
1107 ** from being started in runqueue().
1109 ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
1110 ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
1118 int save_errno = errno;
1120 FIX_SYSV_SIGNAL(sig, runners_sighup);
1122 CHECK_CRITICAL(sig);
1123 NoMoreRunners = true;
1126 proc_list_signal(PROC_QUEUE, sig);
1128 if (!BlockOldsh || getppid() <= 1)
1130 /* Check that a valid 'old signal handler' is callable */
1131 if (Oldsh_hup != SIG_DFL && Oldsh_hup != SIG_IGN &&
1132 Oldsh_hup != runners_sighup)
1136 return SIGFUNC_RETURN;
1139 ** MARK_WORK_GROUP_RESTART -- mark a work group as needing a restart
1141 ** Sets a workgroup for restarting.
1144 ** wgrp -- the work group id to restart.
1145 ** reason -- why (signal?), -1 to turn off restart
1151 ** May set global RestartWorkGroup to true.
1153 ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
1154 ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
1159 mark_work_group_restart(wgrp, reason)
1163 if (wgrp < 0 || wgrp > NumWorkGroups)
1166 WorkGrp[wgrp].wg_restart = reason;
1168 RestartWorkGroup = true;
1171 ** RESTART_MARKED_WORK_GROUPS -- restart work groups marked as needing restart
1173 ** Restart any workgroup marked as needing a restart provided more
1174 ** runners are allowed.
1183 ** Sets global RestartWorkGroup to false.
1187 restart_marked_work_groups()
1195 /* Block SIGCHLD so reapchild() doesn't mess with us */
1196 wasblocked = sm_blocksignal(SIGCHLD);
1198 for (i = 0; i < NumWorkGroups; i++)
1200 if (WorkGrp[i].wg_restart >= 0)
1203 sm_syslog(LOG_ERR, NOQID,
1204 "restart queue runner=%d due to signal 0x%x",
1205 i, WorkGrp[i].wg_restart);
1206 restart_work_group(i);
1209 RestartWorkGroup = false;
1211 if (wasblocked == 0)
1212 (void) sm_releasesignal(SIGCHLD);
1215 ** RESTART_WORK_GROUP -- restart a specific work group
1217 ** Restart a specific workgroup provided more runners are allowed.
1218 ** If the requested work group has been restarted too many times log
1219 ** this and refuse to restart.
1222 ** wgrp -- the work group id to restart
1228 ** starts another process doing the work of wgrp
1231 #define MAX_PERSIST_RESTART 10 /* max allowed number of restarts */
1234 restart_work_group(wgrp)
1237 if (NoMoreRunners ||
1238 wgrp < 0 || wgrp > NumWorkGroups)
1241 WorkGrp[wgrp].wg_restart = -1;
1242 if (WorkGrp[wgrp].wg_restartcnt < MAX_PERSIST_RESTART)
1244 /* avoid overflow; increment here */
1245 WorkGrp[wgrp].wg_restartcnt++;
1246 (void) run_work_group(wgrp, RWG_FORK|RWG_PERSISTENT|RWG_RUNALL);
1250 sm_syslog(LOG_ERR, NOQID,
1251 "ERROR: persistent queue runner=%d restarted too many times, queue runner lost",
1256 ** SCHEDULE_QUEUE_RUNS -- schedule the next queue run for a work group.
1259 ** runall -- schedule even if individual bit is not set.
1260 ** wgrp -- the work group id to schedule.
1261 ** didit -- the queue run was performed for this work group.
1267 #define INCR_MOD(v, m) if (++v >= m) \
1272 schedule_queue_runs(runall, wgrp, didit)
1277 int qgrp, cgrp, endgrp;
1278 #if _FFR_QUEUE_SCHED_DBG
1281 #endif /* _FFR_QUEUE_SCHED_DBG */
1286 ** This is a bit ugly since we have to duplicate the
1287 ** code that "walks" through a work queue group.
1292 cgrp = endgrp = WorkGrp[wgrp].wg_curqgrp;
1297 #if _FFR_QUEUE_SCHED_DBG
1300 #endif /* _FFR_QUEUE_SCHED_DBG */
1301 qgrp = WorkGrp[wgrp].wg_qgs[cgrp]->qg_index;
1302 if (Queue[qgrp]->qg_queueintvl > 0)
1303 qintvl = Queue[qgrp]->qg_queueintvl;
1304 else if (QueueIntvl > 0)
1305 qintvl = QueueIntvl;
1307 qintvl = (time_t) 0;
1308 #if _FFR_QUEUE_SCHED_DBG
1309 lastsched = Queue[qgrp]->qg_nextrun;
1310 #endif /* _FFR_QUEUE_SCHED_DBG */
1311 if ((runall || Queue[qgrp]->qg_nextrun <= now) && qintvl > 0)
1313 #if _FFR_QUEUE_SCHED_DBG
1315 #endif /* _FFR_QUEUE_SCHED_DBG */
1316 if (minqintvl == 0 || qintvl < minqintvl)
1320 ** Only set a new time if a queue run was performed
1321 ** for this queue group. If the queue was not run,
1322 ** we could starve it by setting a new time on each
1327 Queue[qgrp]->qg_nextrun += qintvl;
1329 #if _FFR_QUEUE_SCHED_DBG
1331 sm_syslog(LOG_INFO, NOQID,
1332 "sqr: wgrp=%d, cgrp=%d, qgrp=%d, intvl=%ld, QI=%ld, runall=%d, lastrun=%ld, nextrun=%ld, sched=%d",
1333 wgrp, cgrp, qgrp, Queue[qgrp]->qg_queueintvl,
1334 QueueIntvl, runall, lastsched,
1335 Queue[qgrp]->qg_nextrun, sched);
1336 #endif /* _FFR_QUEUE_SCHED_DBG */
1337 INCR_MOD(cgrp, WorkGrp[wgrp].wg_numqgrp);
1338 } while (endgrp != cgrp);
1340 (void) sm_setevent(minqintvl, runqueueevent, 0);
1343 #if _FFR_QUEUE_RUN_PARANOIA
1345 ** CHECKQUEUERUNNER -- check whether a queue group hasn't been run.
1347 ** Use this if events may get lost and hence queue runners may not
1348 ** be started and mail will pile up in a queue.
1354 ** true if a queue run is necessary.
1357 ** may schedule a queue run.
1364 time_t now, minqintvl;
1368 for (qgrp = 0; qgrp < NumQueue && Queue[qgrp] != NULL; qgrp++)
1372 if (Queue[qgrp]->qg_queueintvl > 0)
1373 qintvl = Queue[qgrp]->qg_queueintvl;
1374 else if (QueueIntvl > 0)
1375 qintvl = QueueIntvl;
1377 qintvl = (time_t) 0;
1378 if (Queue[qgrp]->qg_nextrun <= now - qintvl)
1380 if (minqintvl == 0 || qintvl < minqintvl)
1383 sm_syslog(LOG_WARNING, NOQID,
1384 "checkqueuerunner: queue %d should have been run at %s, queue interval %ld",
1386 arpadate(ctime(&Queue[qgrp]->qg_nextrun)),
1392 (void) sm_setevent(minqintvl, runqueueevent, 0);
1397 #endif /* _FFR_QUEUE_RUN_PARANOIA */
1400 ** RUNQUEUE -- run the jobs in the queue.
1402 ** Gets the stuff out of the queue in some presumably logical
1403 ** order and processes them.
1406 ** forkflag -- true if the queue scanning should be done in
1407 ** a child process. We double-fork so it is not our
1408 ** child and we don't have to clean up after it.
1409 ** false can be ignored if we have multiple queues.
1410 ** verbose -- if true, print out status information.
1411 ** persistent -- persistent queue runner?
1412 ** runall -- run all groups or only a subset (DoQueueRun)?
1415 ** true if the queue run successfully began.
1418 ** runs things in the mail queue using run_work_group().
1419 ** maybe schedules next queue run.
1422 static ENVELOPE QueueEnvelope; /* the queue run envelope */
1423 static time_t LastQueueTime = 0; /* last time a queue ID assigned */
1424 static pid_t LastQueuePid = -1; /* last PID which had a queue ID */
1426 /* values for qp_supdirs */
1427 #define QP_NOSUB 0x0000 /* No subdirectories */
1428 #define QP_SUBDF 0x0001 /* "df" subdirectory */
1429 #define QP_SUBQF 0x0002 /* "qf" subdirectory */
1430 #define QP_SUBXF 0x0004 /* "xf" subdirectory */
1433 runqueue(forkflag, verbose, persistent, runall)
1441 static int curnum = 0;
1444 SM_NONVOLATILE int oldgroup = 0;
1446 if (sm_debug_active(&DebugLeakQ, 1))
1448 oldgroup = sm_heap_group();
1450 sm_dprintf("runqueue() heap group #%d\n", sm_heap_group());
1452 #endif /* SM_HEAP_CHECK */
1454 /* queue run has been started, don't do any more this time */
1457 /* more than one queue or more than one directory per queue */
1458 if (!forkflag && !verbose &&
1459 (WorkGrp[0].wg_qgs[0]->qg_numqueues > 1 || NumWorkGroups > 1 ||
1460 WorkGrp[0].wg_numqgrp > 1))
1464 ** For controlling queue runners via signals sent to this process.
1465 ** Oldsh* will get called too by runners_sig* (if it is not SIG_IGN
1466 ** or SIG_DFL) to preserve cleanup behavior. Now that this process
1467 ** will have children (and perhaps grandchildren) this handler will
1468 ** be left in place. This is because this process, once it has
1469 ** finished spinning off queue runners, may go back to doing something
1470 ** else (like being a daemon). And we still want on a SIG{TERM,HUP} to
1471 ** clean up the child queue runners. Only install 'runners_sig*' once
1472 ** else we'll get stuck looping forever.
1475 cursh = sm_signal(SIGTERM, runners_sigterm);
1476 if (cursh != runners_sigterm)
1478 cursh = sm_signal(SIGHUP, runners_sighup);
1479 if (cursh != runners_sighup)
1482 for (i = 0; i < NumWorkGroups && !NoMoreRunners; i++)
1484 int rwgflags = RWG_NONE;
1487 ** If MaxQueueChildren active then test whether the start
1488 ** of the next queue group's additional queue runners (maximum)
1489 ** will result in MaxQueueChildren being exceeded.
1491 ** Note: do not use continue; even though another workgroup
1492 ** may have fewer queue runners, this would be "unfair",
1493 ** i.e., this work group might "starve" then.
1496 #if _FFR_QUEUE_SCHED_DBG
1498 sm_syslog(LOG_INFO, NOQID,
1499 "rq: curnum=%d, MaxQueueChildren=%d, CurRunners=%d, WorkGrp[curnum].wg_maxact=%d",
1500 curnum, MaxQueueChildren, CurRunners,
1501 WorkGrp[curnum].wg_maxact);
1502 #endif /* _FFR_QUEUE_SCHED_DBG */
1503 if (MaxQueueChildren > 0 &&
1504 CurRunners + WorkGrp[curnum].wg_maxact > MaxQueueChildren)
1508 ** Pick up where we left off (curnum), in case we
1509 ** used up all the children last time without finishing.
1510 ** This give a round-robin fairness to queue runs.
1512 ** Increment CurRunners before calling run_work_group()
1513 ** to avoid a "race condition" with proc_list_drop() which
1514 ** decrements CurRunners if the queue runners terminate.
1515 ** This actually doesn't cause any harm, but CurRunners
1516 ** might become negative which is at least confusing.
1518 ** Notice: CurRunners is an upper limit, in some cases
1519 ** (too few jobs in the queue) this value is larger than
1520 ** the actual number of queue runners. The discrepancy can
1521 ** increase if some queue runners "hang" for a long time.
1524 CurRunners += WorkGrp[curnum].wg_maxact;
1526 rwgflags |= RWG_FORK;
1528 rwgflags |= RWG_VERBOSE;
1530 rwgflags |= RWG_PERSISTENT;
1532 rwgflags |= RWG_RUNALL;
1533 ret = run_work_group(curnum, rwgflags);
1536 ** Failure means a message was printed for ETRN
1537 ** and subsequent queues are likely to fail as well.
1538 ** Decrement CurRunners in that case because
1539 ** none have been started.
1544 CurRunners -= WorkGrp[curnum].wg_maxact;
1549 schedule_queue_runs(runall, curnum, true);
1550 INCR_MOD(curnum, NumWorkGroups);
1553 /* schedule left over queue runs */
1554 if (i < NumWorkGroups && !NoMoreRunners && !persistent)
1558 for (h = curnum; i < NumWorkGroups; i++)
1560 schedule_queue_runs(runall, h, false);
1561 INCR_MOD(h, NumWorkGroups);
1567 if (sm_debug_active(&DebugLeakQ, 1))
1568 sm_heap_setgroup(oldgroup);
1569 #endif /* SM_HEAP_CHECK */
1573 ** RUNNER_WORK -- have a queue runner do its work
1575 ** Have a queue runner do its work a list of entries.
1576 ** When work isn't directly being done then this process can take a signal
1577 ** and terminate immediately (in a clean fashion of course).
1578 ** When work is directly being done, it's not to be interrupted
1579 ** immediately: the work should be allowed to finish at a clean point
1580 ** before termination (in a clean fashion of course).
1584 ** sequenceno -- 'th process to run WorkQ.
1585 ** didfork -- did the calling process fork()?
1586 ** skip -- process only each skip'th item.
1587 ** njobs -- number of jobs in WorkQ.
1593 ** runs things in the mail queue.
1597 runner_work(e, sequenceno, didfork, skip, njobs)
1598 register ENVELOPE *e;
1611 ** Here we temporarily block the second calling of the handlers.
1612 ** This allows us to handle the signal without terminating in the
1613 ** middle of direct work. If a signal does come, the test for
1614 ** NoMoreRunners will find it.
1619 /* process them once at a time */
1620 while (WorkQ != NULL)
1623 SM_NONVOLATILE int oldgroup = 0;
1625 if (sm_debug_active(&DebugLeakQ, 1))
1627 oldgroup = sm_heap_group();
1629 sm_dprintf("run_queue_group() heap group #%d\n",
1632 #endif /* SM_HEAP_CHECK */
1634 /* do no more work */
1637 /* Check that a valid signal handler is callable */
1638 if (Oldsh != SIG_DFL && Oldsh != SIG_IGN &&
1639 Oldsh != runners_sighup &&
1640 Oldsh != runners_sigterm)
1645 w = WorkQ; /* assign current work item */
1648 ** Set the head of the WorkQ to the next work item.
1649 ** It is set 'skip' ahead (the number of parallel queue
1650 ** runners working on WorkQ together) since each runner
1651 ** works on every 'skip'th (N-th) item.
1654 for (n = 0; n < skip && WorkQ != NULL; n++)
1655 WorkQ = WorkQ->w_next;
1659 ** Ignore jobs that are too expensive for the moment.
1661 ** Get new load average every GET_NEW_LA_TIME seconds.
1665 if (shouldqueue(WkRecipFact, Current_LA_time))
1667 char *msg = "Aborting queue run: load average too high";
1672 sm_syslog(LOG_INFO, NOQID, "runqueue: %s", msg);
1675 if (shouldqueue(w->w_pri, w->w_ctime))
1678 message(EmptyString);
1679 if (QueueSortOrder == QSO_BYPRIORITY)
1682 message("Skipping %s/%s (sequence %d of %d) and flushing rest of queue",
1683 qid_printqueue(w->w_qgrp,
1685 w->w_name + 2, sequenceno,
1688 sm_syslog(LOG_INFO, NOQID,
1689 "runqueue: Flushing queue from %s/%s (pri %ld, LA %d, %d of %d)",
1690 qid_printqueue(w->w_qgrp,
1692 w->w_name + 2, w->w_pri,
1693 CurrentLA, sequenceno,
1698 message("Skipping %s/%s (sequence %d of %d)",
1699 qid_printqueue(w->w_qgrp, w->w_qdir),
1700 w->w_name + 2, sequenceno, njobs);
1706 message(EmptyString);
1707 message("Running %s/%s (sequence %d of %d)",
1708 qid_printqueue(w->w_qgrp, w->w_qdir),
1709 w->w_name + 2, sequenceno, njobs);
1711 if (didfork && MaxQueueChildren > 0)
1713 sm_blocksignal(SIGCHLD);
1714 (void) sm_signal(SIGCHLD, reapchild);
1717 sm_syslog(LOG_DEBUG, NOQID,
1718 "runqueue %s dowork(%s)",
1719 qid_printqueue(w->w_qgrp, w->w_qdir),
1722 (void) dowork(w->w_qgrp, w->w_qdir, w->w_name + 2,
1723 ForkQueueRuns, false, e);
1726 sm_free(w->w_name); /* XXX */
1727 if (w->w_host != NULL)
1728 sm_free(w->w_host); /* XXX */
1729 sm_free((char *) w); /* XXX */
1730 sequenceno += skip; /* next sequence number */
1732 if (sm_debug_active(&DebugLeakQ, 1))
1733 sm_heap_setgroup(oldgroup);
1734 #endif /* SM_HEAP_CHECK */
1739 /* check the signals didn't happen during the revert */
1742 /* Check that a valid signal handler is callable */
1743 if (Oldsh != SIG_DFL && Oldsh != SIG_IGN &&
1744 Oldsh != runners_sighup && Oldsh != runners_sigterm)
1748 Oldsh = SIG_DFL; /* after the NoMoreRunners check */
1751 ** RUN_WORK_GROUP -- run the jobs in a queue group from a work group.
1753 ** Gets the stuff out of the queue in some presumably logical
1754 ** order and processes them.
1757 ** wgrp -- work group to process.
1758 ** flags -- RWG_* flags
1761 ** true if the queue run successfully began.
1764 ** runs things in the mail queue.
1767 /* Minimum sleep time for persistent queue runners */
1768 #define MIN_SLEEP_TIME 5
1771 run_work_group(wgrp, flags)
1775 register ENVELOPE *e;
1778 int qgrp, endgrp, h, i;
1782 extern void rmexpstab __P((void));
1783 extern ENVELOPE BlankEnvelope;
1784 extern SIGFUNC_DECL reapchild __P((int));
1790 ** If no work will ever be selected, don't even bother reading
1796 if (!bitset(RWG_PERSISTENT, flags) &&
1797 shouldqueue(WkRecipFact, Current_LA_time))
1799 char *msg = "Skipping queue run -- load average too high";
1801 if (bitset(RWG_VERBOSE, flags))
1802 message("458 %s\n", msg);
1804 sm_syslog(LOG_INFO, NOQID, "runqueue: %s", msg);
1809 ** See if we already have too many children.
1812 if (bitset(RWG_FORK, flags) &&
1813 WorkGrp[wgrp].wg_lowqintvl > 0 &&
1814 !bitset(RWG_PERSISTENT, flags) &&
1815 MaxChildren > 0 && CurChildren >= MaxChildren)
1817 char *msg = "Skipping queue run -- too many children";
1819 if (bitset(RWG_VERBOSE, flags))
1820 message("458 %s (%d)\n", msg, CurChildren);
1822 sm_syslog(LOG_INFO, NOQID, "runqueue: %s (%d)",
1828 ** See if we want to go off and do other useful work.
1831 if (bitset(RWG_FORK, flags))
1835 (void) sm_blocksignal(SIGCHLD);
1836 (void) sm_signal(SIGCHLD, reapchild);
1841 const char *msg = "Skipping queue run -- fork() failed";
1842 const char *err = sm_errstring(errno);
1844 if (bitset(RWG_VERBOSE, flags))
1845 message("458 %s: %s\n", msg, err);
1847 sm_syslog(LOG_INFO, NOQID, "runqueue: %s: %s",
1849 (void) sm_releasesignal(SIGCHLD);
1854 /* parent -- pick up intermediate zombie */
1855 (void) sm_blocksignal(SIGALRM);
1857 /* wgrp only used when queue runners are persistent */
1858 proc_list_add(pid, "Queue runner", PROC_QUEUE,
1859 WorkGrp[wgrp].wg_maxact,
1860 bitset(RWG_PERSISTENT, flags) ? wgrp : -1);
1861 (void) sm_releasesignal(SIGALRM);
1862 (void) sm_releasesignal(SIGCHLD);
1866 /* child -- clean up signals */
1868 /* Reset global flags */
1869 RestartRequest = NULL;
1870 RestartWorkGroup = false;
1871 ShutdownRequest = NULL;
1873 CurrentPid = getpid();
1876 ** Initialize exception stack and default exception
1877 ** handler for child process.
1880 sm_exc_newthread(fatal_error);
1884 /* Add parent process as first child item */
1885 proc_list_add(CurrentPid, "Queue runner child process",
1886 PROC_QUEUE_CHILD, 0, -1);
1887 (void) sm_releasesignal(SIGCHLD);
1888 (void) sm_signal(SIGCHLD, SIG_DFL);
1889 (void) sm_signal(SIGHUP, SIG_DFL);
1890 (void) sm_signal(SIGTERM, intsig);
1894 ** Release any resources used by the daemon code.
1899 /* force it to run expensive jobs */
1902 /* drop privileges */
1903 if (geteuid() == (uid_t) 0)
1904 (void) drop_privileges(false);
1907 ** Create ourselves an envelope
1910 CurEnv = &QueueEnvelope;
1911 rpool = sm_rpool_new_x(NULL);
1912 e = newenvelope(&QueueEnvelope, CurEnv, rpool);
1913 e->e_flags = BlankEnvelope.e_flags;
1916 /* make sure we have disconnected from parent */
1917 if (bitset(RWG_FORK, flags))
1924 ** If we are running part of the queue, always ignore stored
1928 if (QueueLimitId != NULL || QueueLimitSender != NULL ||
1930 QueueLimitQuarantine != NULL ||
1931 #endif /* _FFR_QUARANTINE */
1932 QueueLimitRecipient != NULL)
1934 IgnoreHostStatus = true;
1939 ** Here is where we choose the queue group from the work group.
1940 ** The caller of the "domorework" label must setup a new envelope.
1943 endgrp = WorkGrp[wgrp].wg_curqgrp; /* to not spin endlessly */
1948 ** Run a queue group if:
1949 ** RWG_RUNALL bit is set or the bit for this group is set.
1956 ** Find the next queue group within the work group that
1957 ** has been marked as needing a run.
1960 qgrp = WorkGrp[wgrp].wg_qgs[WorkGrp[wgrp].wg_curqgrp]->qg_index;
1961 WorkGrp[wgrp].wg_curqgrp++; /* advance */
1962 WorkGrp[wgrp].wg_curqgrp %= WorkGrp[wgrp].wg_numqgrp; /* wrap */
1963 if (bitset(RWG_RUNALL, flags) ||
1964 (Queue[qgrp]->qg_nextrun <= now &&
1965 Queue[qgrp]->qg_nextrun != (time_t) -1))
1967 if (endgrp == WorkGrp[wgrp].wg_curqgrp)
1970 if (bitset(RWG_FORK, flags))
1971 finis(true, true, ExitStat);
1972 return true; /* we're done */
1976 qdir = Queue[qgrp]->qg_curnum; /* round-robin init of queue position */
1977 #if _FFR_QUEUE_SCHED_DBG
1979 sm_syslog(LOG_INFO, NOQID,
1980 "rwg: wgrp=%d, qgrp=%d, qdir=%d, name=%s, curqgrp=%d, numgrps=%d",
1981 wgrp, qgrp, qdir, qid_printqueue(qgrp, qdir),
1982 WorkGrp[wgrp].wg_curqgrp, WorkGrp[wgrp].wg_numqgrp);
1983 #endif /* _FFR_QUEUE_SCHED_DBG */
1986 /* tweak niceness of queue runs */
1987 if (Queue[qgrp]->qg_nice > 0)
1988 (void) nice(Queue[qgrp]->qg_nice);
1989 #endif /* HASNICE */
1991 /* XXX running queue group... */
1992 sm_setproctitle(true, CurEnv, "running queue: %s",
1993 qid_printqueue(qgrp, qdir));
1995 if (LogLevel > 69 || tTd(63, 99))
1996 sm_syslog(LOG_DEBUG, NOQID,
1997 "runqueue %s, pid=%d, forkflag=%d",
1998 qid_printqueue(qgrp, qdir), (int) CurrentPid,
1999 bitset(RWG_FORK, flags));
2002 ** Start making passes through the queue.
2003 ** First, read and sort the entire queue.
2004 ** Then, process the work in that order.
2005 ** But if you take too long, start over.
2008 for (i = 0; i < Queue[qgrp]->qg_numqueues; i++)
2010 h = gatherq(qgrp, qdir, false, &full, &more);
2012 if (ShmId != SM_SHM_NO_ID)
2013 QSHM_ENTRIES(Queue[qgrp]->qg_qpaths[qdir].qp_idx) = h;
2014 #endif /* SM_CONF_SHM */
2015 /* If there are no more items in this queue advance */
2018 /* A round-robin advance */
2020 qdir %= Queue[qgrp]->qg_numqueues;
2023 /* Has the WorkList reached the limit? */
2025 break; /* don't try to gather more */
2028 /* order the existing work requests */
2029 njobs = sortq(Queue[qgrp]->qg_maxlist);
2030 Queue[qgrp]->qg_curnum = qdir; /* update */
2033 if (!Verbose && bitnset(QD_FORK, Queue[qgrp]->qg_flags))
2035 int loop, maxrunners;
2039 ** For this WorkQ we want to fork off N children (maxrunners)
2040 ** at this point. Each child has a copy of WorkQ. Each child
2041 ** will process every N-th item. The parent will wait for all
2042 ** of the children to finish before moving on to the next
2043 ** queue group within the work group. This saves us forking
2044 ** a new runner-child for each work item.
2045 ** It's valid for qg_maxqrun == 0 since this may be an
2046 ** explicit "don't run this queue" setting.
2049 maxrunners = Queue[qgrp]->qg_maxqrun;
2051 /* No need to have more runners then there are jobs */
2052 if (maxrunners > njobs)
2054 for (loop = 0; loop < maxrunners; loop++)
2057 ** Since the delivery may happen in a child and the
2058 ** parent does not wait, the parent may close the
2059 ** maps thereby removing any shared memory used by
2060 ** the map. Therefore, close the maps now so the
2061 ** child will dynamically open them if necessary.
2069 syserr("run_work_group: cannot fork");
2074 /* parent -- clean out connection cache */
2075 mci_flush(false, NULL);
2076 WorkQ = WorkQ->w_next; /* for the skip */
2078 proc_list_add(pid, "Queue child runner process",
2079 PROC_QUEUE_CHILD, 0, -1);
2081 /* No additional work, no additional runners */
2087 /* child -- Reset global flags */
2088 RestartRequest = NULL;
2089 RestartWorkGroup = false;
2090 ShutdownRequest = NULL;
2092 CurrentPid = getpid();
2095 ** Initialize exception stack and default
2096 ** exception handler for child process.
2097 ** When fork()'d the child now has a private
2098 ** copy of WorkQ at its current position.
2101 sm_exc_newthread(fatal_error);
2104 ** SMTP processes (whether -bd or -bs) set
2105 ** SIGCHLD to reapchild to collect
2106 ** children status. However, at delivery
2107 ** time, that status must be collected
2108 ** by sm_wait() to be dealt with properly
2109 ** (check success of delivery based
2110 ** on status code, etc). Therefore, if we
2111 ** are an SMTP process, reset SIGCHLD
2112 ** back to the default so reapchild
2113 ** doesn't collect status before
2117 if (OpMode == MD_SMTP ||
2118 OpMode == MD_DAEMON ||
2119 MaxQueueChildren > 0)
2122 sm_releasesignal(SIGCHLD);
2123 (void) sm_signal(SIGCHLD, SIG_DFL);
2126 /* child -- error messages to the transcript */
2127 QuickAbort = OnlyOneError = false;
2128 runner_work(e, sequenceno, true,
2131 /* This child is done */
2132 finis(true, true, ExitStat);
2137 sm_releasesignal(SIGCHLD);
2140 ** Wait until all of the runners have completed before
2141 ** seeing if there is another queue group in the
2142 ** work group to process.
2143 ** XXX Future enhancement: don't wait() for all children
2144 ** here, just go ahead and make sure that overall the number
2145 ** of children is not exceeded.
2148 while (CurChildren > 0)
2153 while ((ret = sm_wait(&status)) <= 0)
2155 proc_list_drop(ret, status, NULL);
2158 else if (Queue[qgrp]->qg_maxqrun > 0 || bitset(RWG_FORCE, flags))
2161 ** When current process will not fork children to do the work,
2162 ** it will do the work itself. The 'skip' will be 1 since
2163 ** there are no child runners to divide the work across.
2166 runner_work(e, sequenceno, false, 1, njobs);
2169 /* free memory allocated by newenvelope() above */
2170 sm_rpool_free(rpool);
2171 QueueEnvelope.e_rpool = NULL;
2173 /* Are there still more queues in the work group to process? */
2174 if (endgrp != WorkGrp[wgrp].wg_curqgrp)
2176 rpool = sm_rpool_new_x(NULL);
2177 e = newenvelope(&QueueEnvelope, CurEnv, rpool);
2178 e->e_flags = BlankEnvelope.e_flags;
2182 /* No more queues in work group to process. Now check persistent. */
2183 if (bitset(RWG_PERSISTENT, flags))
2186 sm_setproctitle(true, CurEnv, "running queue: %s",
2187 qid_printqueue(qgrp, qdir));
2190 ** close bogus maps, i.e., maps which caused a tempfail,
2191 ** so we get fresh map connections on the next lookup.
2192 ** closemaps() is also called when children are started.
2197 /* Close any cached connections. */
2198 mci_flush(true, NULL);
2200 /* Clean out expired related entries. */
2204 /* Update MX records for FallBackMX. */
2205 if (FallBackMX != NULL)
2206 (void) getfallbackmxrr(FallBackMX);
2207 #endif /* NAMED_BIND */
2210 /* close UserDatabase */
2215 if (sm_debug_active(&SmHeapCheck, 2)
2216 && access("memdump", F_OK) == 0
2222 out = sm_io_open(SmFtStdio, SM_TIME_DEFAULT,
2223 "memdump.out", SM_IO_APPEND, NULL);
2226 (void) sm_io_fprintf(out, SM_TIME_DEFAULT, "----------------------\n");
2228 sm_debug_level(&SmHeapCheck) - 1);
2229 (void) sm_io_close(out, SM_TIME_DEFAULT);
2232 #endif /* SM_HEAP_CHECK */
2234 /* let me rest for a second to catch my breath */
2235 if (njobs == 0 && WorkGrp[wgrp].wg_lowqintvl < MIN_SLEEP_TIME)
2236 sleep(MIN_SLEEP_TIME);
2237 else if (WorkGrp[wgrp].wg_lowqintvl <= 0)
2238 sleep(QueueIntvl > 0 ? QueueIntvl : MIN_SLEEP_TIME);
2240 sleep(WorkGrp[wgrp].wg_lowqintvl);
2243 ** Get the LA outside the WorkQ loop if necessary.
2244 ** In a persistent queue runner the code is repeated over
2245 ** and over but gatherq() may ignore entries due to
2246 ** shouldqueue() (do we really have to do this twice?).
2247 ** Hence the queue runners would just idle around when once
2248 ** CurrentLA caused all entries in a queue to be ignored.
2253 rpool = sm_rpool_new_x(NULL);
2254 e = newenvelope(&QueueEnvelope, CurEnv, rpool);
2255 e->e_flags = BlankEnvelope.e_flags;
2259 /* exit without the usual cleanup */
2261 if (bitset(RWG_FORK, flags))
2262 finis(true, true, ExitStat);
2268 ** DOQUEUERUN -- do a queue run?
2278 ** RUNQUEUEEVENT -- Sets a flag to indicate that a queue run should be done.
2287 ** The invocation of this function via an alarm may interrupt
2288 ** a set of actions. Thus errno may be set in that context.
2289 ** We need to restore errno at the end of this function to ensure
2290 ** that any work done here that sets errno doesn't return a
2291 ** misleading/false errno value. Errno may be EINTR upon entry to
2292 ** this function because of non-restartable/continuable system
2293 ** API was active. Iff this is true we will override errno as
2294 ** a timeout (as a more accurate error message).
2296 ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
2297 ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
2304 int save_errno = errno;
2307 ** Set the general bit that we want a queue run,
2308 ** tested in doqueuerun()
2312 #if _FFR_QUEUE_SCHED_DBG
2314 sm_syslog(LOG_INFO, NOQID, "rqe: done");
2315 #endif /* _FFR_QUEUE_SCHED_DBG */
2322 ** GATHERQ -- gather messages from the message queue(s) the work queue.
2325 ** qgrp -- the index of the queue group.
2326 ** qdir -- the index of the queue directory.
2327 ** doall -- if set, include everything in the queue (even
2328 ** the jobs that cannot be run because the load
2329 ** average is too high, or MaxQueueRun is reached).
2330 ** Otherwise, exclude those jobs.
2331 ** full -- (optional) to be set 'true' if WorkList is full
2332 ** more -- (optional) to be set 'true' if there are still more
2333 ** messages in this queue not added to WorkList
2336 ** The number of request in the queue (not necessarily
2337 ** the number of requests in WorkList however).
2340 ** prepares available work into WorkList
2343 #define NEED_P 0001 /* 'P': priority */
2344 #define NEED_T 0002 /* 'T': time */
2345 #define NEED_R 0004 /* 'R': recipient */
2346 #define NEED_S 0010 /* 'S': sender */
2347 #define NEED_H 0020 /* host */
2349 # define HAS_QUARANTINE 0040 /* has an unexpected 'q' line */
2350 # define NEED_QUARANTINE 0100 /* 'q': reason */
2351 #endif /* _FFR_QUARANTINE */
2353 static WORK *WorkList = NULL; /* list of unsort work */
2354 static int WorkListSize = 0; /* current max size of WorkList */
2355 static int WorkListCount = 0; /* # of work items in WorkList */
2358 gatherq(qgrp, qdir, doall, full, more)
2365 register struct dirent *d;
2372 char qd[MAXPATHLEN];
2373 char qf[MAXPATHLEN];
2375 wn = WorkListCount - 1;
2378 (void) sm_strlcpy(qd, ".", sizeof qd);
2380 (void) sm_strlcpyn(qd, sizeof qd, 2,
2381 Queue[qgrp]->qg_qpaths[qdir].qp_name,
2383 Queue[qgrp]->qg_qpaths[qdir].qp_subdirs)
2388 sm_dprintf("gatherq:\n");
2390 check = QueueLimitId;
2391 while (check != NULL)
2393 sm_dprintf("\tQueueLimitId = %s%s\n",
2394 check->queue_negate ? "!" : "",
2395 check->queue_match);
2396 check = check->queue_next;
2399 check = QueueLimitSender;
2400 while (check != NULL)
2402 sm_dprintf("\tQueueLimitSender = %s%s\n",
2403 check->queue_negate ? "!" : "",
2404 check->queue_match);
2405 check = check->queue_next;
2408 check = QueueLimitRecipient;
2409 while (check != NULL)
2411 sm_dprintf("\tQueueLimitRecipient = %s%s\n",
2412 check->queue_negate ? "!" : "",
2413 check->queue_match);
2414 check = check->queue_next;
2418 if (QueueMode == QM_QUARANTINE)
2420 check = QueueLimitQuarantine;
2421 while (check != NULL)
2423 sm_dprintf("\tQueueLimitQuarantine = %s%s\n",
2424 check->queue_negate ? "!" : "",
2425 check->queue_match);
2426 check = check->queue_next;
2429 #endif /* _FFR_QUARANTINE */
2432 /* open the queue directory */
2436 syserr("gatherq: cannot open \"%s\"",
2437 qid_printqueue(qgrp, qdir));
2439 *full = WorkListCount >= MaxQueueRun && MaxQueueRun > 0;
2446 ** Read the work directory.
2449 while ((d = readdir(f)) != NULL)
2453 char lbuf[MAXNAME + 1];
2457 sm_dprintf("gatherq: checking %s..", d->d_name);
2459 /* is this an interesting entry? */
2461 if (!(((QueueMode == QM_NORMAL &&
2462 d->d_name[0] == NORMQF_LETTER) ||
2463 (QueueMode == QM_QUARANTINE &&
2464 d->d_name[0] == QUARQF_LETTER) ||
2465 (QueueMode == QM_LOST &&
2466 d->d_name[0] == LOSEQF_LETTER)) &&
2467 d->d_name[1] == 'f'))
2468 #else /* _FFR_QUARANTINE */
2469 if (d->d_name[0] != NORMQF_LETTER || d->d_name[1] != 'f')
2470 #endif /* _FFR_QUARANTINE */
2473 sm_dprintf(" skipping\n");
2479 if (strlen(d->d_name) >= MAXQFNAME)
2482 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
2483 "gatherq: %s too long, %d max characters\n",
2484 d->d_name, MAXQFNAME);
2486 sm_syslog(LOG_ALERT, NOQID,
2487 "gatherq: %s too long, %d max characters",
2488 d->d_name, MAXQFNAME);
2492 check = QueueLimitId;
2493 while (check != NULL)
2495 if (strcontainedin(false, check->queue_match,
2496 d->d_name) != check->queue_negate)
2499 check = check->queue_next;
2501 if (QueueLimitId != NULL && check == NULL)
2504 /* grow work list if necessary */
2505 if (++wn >= MaxQueueRun && MaxQueueRun > 0)
2507 if (wn == MaxQueueRun && LogLevel > 0)
2508 sm_syslog(LOG_WARNING, NOQID,
2509 "WorkList for %s maxed out at %d",
2510 qid_printqueue(qgrp, qdir),
2513 continue; /* just count entries */
2516 if (wn >= WorkListSize)
2518 grow_wlist(qgrp, qdir);
2519 if (wn >= WorkListSize)
2525 (void) sm_strlcpyn(qf, sizeof qf, 3, qd, "/", d->d_name);
2526 if (stat(qf, &sbuf) < 0)
2528 if (errno != ENOENT)
2529 sm_syslog(LOG_INFO, NOQID,
2530 "gatherq: can't stat %s/%s",
2531 qid_printqueue(qgrp, qdir),
2536 if (!bitset(S_IFREG, sbuf.st_mode))
2538 /* Yikes! Skip it or we will hang on open! */
2539 if (!((d->d_name[0] == DATAFL_LETTER ||
2540 d->d_name[0] == NORMQF_LETTER ||
2542 d->d_name[0] == QUARQF_LETTER ||
2543 d->d_name[0] == LOSEQF_LETTER ||
2544 #endif /* _FFR_QUARANTINE */
2545 d->d_name[0] == XSCRPT_LETTER) &&
2546 d->d_name[1] == 'f' && d->d_name[2] == '\0'))
2547 syserr("gatherq: %s/%s is not a regular file",
2548 qid_printqueue(qgrp, qdir), d->d_name);
2553 /* avoid work if possible */
2554 if ((QueueSortOrder == QSO_BYFILENAME ||
2555 QueueSortOrder == QSO_BYMODTIME ||
2556 QueueSortOrder == QSO_RANDOM) &&
2558 QueueLimitQuarantine == NULL &&
2559 #endif /* _FFR_QUARANTINE */
2560 QueueLimitSender == NULL &&
2561 QueueLimitRecipient == NULL)
2565 w->w_name = newstr(d->d_name);
2567 w->w_lock = w->w_tooyoung = false;
2570 w->w_mtime = sbuf.st_mtime;
2575 /* open control file */
2576 cf = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, qf, SM_IO_RDONLY_B,
2578 if (cf == NULL && OpMode != MD_PRINT)
2580 /* this may be some random person sending hir msgs */
2582 sm_dprintf("gatherq: cannot open %s: %s\n",
2583 d->d_name, sm_errstring(errno));
2590 w->w_name = newstr(d->d_name);
2594 w->w_lock = !lockfile(sm_io_getinfo(cf, SM_IO_WHAT_FD,
2599 w->w_tooyoung = false;
2601 /* make sure jobs in creation don't clog queue */
2602 w->w_pri = 0x7fffffff;
2604 w->w_mtime = sbuf.st_mtime;
2606 /* extract useful information */
2608 if (QueueSortOrder == QSO_BYHOST
2610 || QueueSortOrder == QSO_BYSHUFFLE
2611 #endif /* _FFR_RHS */
2614 /* need w_host set for host sort order */
2617 if (QueueLimitSender != NULL)
2619 if (QueueLimitRecipient != NULL)
2622 if (QueueLimitQuarantine != NULL)
2623 i |= NEED_QUARANTINE;
2624 #endif /* _FFR_QUARANTINE */
2625 while (cf != NULL && i != 0 &&
2626 sm_io_fgets(cf, SM_TIME_DEFAULT, lbuf,
2627 sizeof lbuf) != NULL)
2632 p = strchr(lbuf, '\n');
2637 /* flush rest of overly long line */
2638 while ((c = sm_io_getc(cf, SM_TIME_DEFAULT))
2639 != SM_IO_EOF && c != '\n')
2646 qfver = atoi(&lbuf[1]);
2650 w->w_pri = atol(&lbuf[1]);
2655 w->w_ctime = atol(&lbuf[1]);
2661 if (QueueMode != QM_QUARANTINE &&
2662 QueueMode != QM_LOST)
2665 sm_dprintf("%s not marked as quarantined but has a 'q' line\n",
2667 i |= HAS_QUARANTINE;
2669 else if (QueueMode == QM_QUARANTINE)
2671 if (QueueLimitQuarantine == NULL)
2673 i &= ~NEED_QUARANTINE;
2677 check = QueueLimitQuarantine;
2678 while (check != NULL)
2680 if (strcontainedin(false,
2683 check->queue_negate)
2686 check = check->queue_next;
2689 i &= ~NEED_QUARANTINE;
2692 #endif /* _FFR_QUARANTINE */
2695 if (w->w_host == NULL &&
2696 (p = strrchr(&lbuf[1], '@')) != NULL)
2699 if (QueueSortOrder == QSO_BYSHUFFLE)
2700 w->w_host = newstr(&p[1]);
2702 #endif /* _FFR_RHS */
2703 w->w_host = strrev(&p[1]);
2704 makelower(w->w_host);
2707 if (QueueLimitRecipient == NULL)
2714 p = strchr(&lbuf[1], ':');
2718 ++p; /* skip over ':' */
2722 check = QueueLimitRecipient;
2723 while (check != NULL)
2725 if (strcontainedin(true,
2728 check->queue_negate)
2731 check = check->queue_next;
2738 check = QueueLimitSender;
2739 while (check != NULL)
2741 if (strcontainedin(true,
2744 check->queue_negate)
2747 check = check->queue_next;
2754 age = curtime() - (time_t) atol(&lbuf[1]);
2755 if (age >= 0 && MinQueueAge > 0 &&
2757 w->w_tooyoung = true;
2761 if (atol(&lbuf[1]) == 0)
2762 w->w_tooyoung = false;
2768 queuealg = atoi(lbuf[1]);
2771 queuedelay = (time_t) atol(&lbuf[1]);
2774 #endif /* _FFR_QUEUEDELAY */
2778 (void) sm_io_close(cf, SM_TIME_DEFAULT);
2780 if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) ||
2782 bitset(HAS_QUARANTINE, i) ||
2783 bitset(NEED_QUARANTINE, i) ||
2784 #endif /* _FFR_QUARANTINE */
2785 bitset(NEED_R|NEED_S, i))
2787 /* don't even bother sorting this job in */
2789 sm_dprintf("skipping %s (%x)\n", w->w_name, i);
2790 sm_free(w->w_name); /* XXX */
2791 if (w->w_host != NULL)
2792 sm_free(w->w_host); /* XXX */
2801 i = wn - WorkListCount;
2802 WorkListCount += SM_MIN(num_ent, WorkListSize);
2805 *more = WorkListCount < wn;
2808 *full = (wn >= MaxQueueRun && MaxQueueRun > 0) ||
2809 (WorkList == NULL && wn > 0);
2814 ** SORTQ -- sort the work list
2816 ** First the old WorkQ is cleared away. Then the WorkList is sorted
2817 ** for all items so that important (higher sorting value) items are not
2818 ** trunctated off. Then the most important items are moved from
2819 ** WorkList to WorkQ. The lower count of 'max' or MaxListCount items
2823 ** max -- maximum number of items to be placed in WorkQ
2826 ** the number of items in WorkQ
2829 ** WorkQ gets released and filled with new work. WorkList
2830 ** gets released. Work items get sorted in order.
2837 register int i; /* local counter */
2838 register WORK *w; /* tmp item pointer */
2839 int wc = WorkListCount; /* trim size for WorkQ */
2843 /* Clear out old WorkQ. */
2844 for (w = WorkQ; w != NULL; )
2846 register WORK *nw = w->w_next;
2849 sm_free(w->w_name); /* XXX */
2850 if (w->w_host != NULL)
2851 sm_free(w->w_host); /* XXX */
2852 sm_free((char *) w); /* XXX */
2855 sm_free((char *) WorkQ);
2859 if (WorkList == NULL || wc <= 0)
2862 /* Check if the per queue group item limit will be exceeded */
2863 if (wc > max && max > 0)
2867 ** The sort now takes place using all of the items in WorkList.
2868 ** The list gets trimmed to the most important items after the sort.
2869 ** If the trim were to happen before the sort then one or more
2870 ** important items might get truncated off -- not what we want.
2873 if (QueueSortOrder == QSO_BYHOST)
2876 ** Sort the work directory for the first time,
2877 ** based on host name, lock status, and priority.
2880 qsort((char *) WorkList, wc, sizeof *WorkList, workcmpf1);
2883 ** If one message to host is locked, "lock" all messages
2890 if (!WorkList[i].w_lock)
2898 if (WorkList[i].w_host == NULL &&
2900 WorkList[i].w_lock = true;
2901 else if (WorkList[i].w_host != NULL &&
2902 w->w_host != NULL &&
2903 sm_strcasecmp(WorkList[i].w_host,
2905 WorkList[i].w_lock = true;
2912 ** Sort the work directory for the second time,
2913 ** based on lock status, host name, and priority.
2916 qsort((char *) WorkList, wc, sizeof *WorkList, workcmpf2);
2918 else if (QueueSortOrder == QSO_BYTIME)
2921 ** Simple sort based on submission time only.
2924 qsort((char *) WorkList, wc, sizeof *WorkList, workcmpf3);
2926 else if (QueueSortOrder == QSO_BYFILENAME)
2929 ** Sort based on queue filename.
2932 qsort((char *) WorkList, wc, sizeof *WorkList, workcmpf4);
2934 else if (QueueSortOrder == QSO_RANDOM)
2937 ** Sort randomly. To avoid problems with an instable sort,
2938 ** use a random index into the queue file name to start
2942 randi = get_rand_mod(MAXQFNAME);
2945 qsort((char *) WorkList, wc, sizeof *WorkList, workcmpf5);
2947 else if (QueueSortOrder == QSO_BYMODTIME)
2950 ** Simple sort based on modification time of queue file.
2951 ** This puts the oldest items first.
2954 qsort((char *) WorkList, wc, sizeof *WorkList, workcmpf6);
2957 else if (QueueSortOrder == QSO_BYSHUFFLE)
2960 ** Simple sort based on shuffled host name.
2963 init_shuffle_alphabet();
2964 qsort((char *) WorkList, wc, sizeof *WorkList, workcmpf7);
2966 #endif /* _FFR_RHS */
2970 ** Simple sort based on queue priority only.
2973 qsort((char *) WorkList, wc, sizeof *WorkList, workcmpf0);
2977 ** Convert the work list into canonical form.
2978 ** Should be turning it into a list of envelopes here perhaps.
2979 ** Only take the most important items up to the per queue group
2983 for (i = wc; --i >= 0; )
2985 w = (WORK *) xalloc(sizeof *w);
2986 w->w_qgrp = WorkList[i].w_qgrp;
2987 w->w_qdir = WorkList[i].w_qdir;
2988 w->w_name = WorkList[i].w_name;
2989 w->w_host = WorkList[i].w_host;
2990 w->w_lock = WorkList[i].w_lock;
2991 w->w_tooyoung = WorkList[i].w_tooyoung;
2992 w->w_pri = WorkList[i].w_pri;
2993 w->w_ctime = WorkList[i].w_ctime;
2994 w->w_mtime = WorkList[i].w_mtime;
2998 if (WorkList != NULL)
2999 sm_free(WorkList); /* XXX */
3006 for (w = WorkQ; w != NULL; w = w->w_next)
3008 if (w->w_host != NULL)
3009 sm_dprintf("%22s: pri=%ld %s\n",
3010 w->w_name, w->w_pri, w->w_host);
3012 sm_dprintf("%32s: pri=%ld\n",
3013 w->w_name, w->w_pri);
3017 return wc; /* return number of WorkQ items */
3020 ** GROW_WLIST -- make the work list larger
3023 ** qgrp -- the index for the queue group.
3024 ** qdir -- the index for the queue directory.
3030 ** Adds another QUEUESEGSIZE entries to WorkList if possible.
3031 ** It can fail if there isn't enough memory, so WorkListSize
3032 ** should be checked again upon return.
3036 grow_wlist(qgrp, qdir)
3041 sm_dprintf("grow_wlist: WorkListSize=%d\n", WorkListSize);
3042 if (WorkList == NULL)
3044 WorkList = (WORK *) xalloc((sizeof *WorkList) *
3045 (QUEUESEGSIZE + 1));
3046 WorkListSize = QUEUESEGSIZE;
3050 int newsize = WorkListSize + QUEUESEGSIZE;
3051 WORK *newlist = (WORK *) sm_realloc((char *) WorkList,
3052 (unsigned) sizeof(WORK) * (newsize + 1));
3054 if (newlist != NULL)
3056 WorkListSize = newsize;
3060 sm_syslog(LOG_INFO, NOQID,
3061 "grew WorkList for %s to %d",
3062 qid_printqueue(qgrp, qdir),
3066 else if (LogLevel > 0)
3068 sm_syslog(LOG_ALERT, NOQID,
3069 "FAILED to grow WorkList for %s to %d",
3070 qid_printqueue(qgrp, qdir), newsize);
3074 sm_dprintf("grow_wlist: WorkListSize now %d\n", WorkListSize);
3077 ** WORKCMPF0 -- simple priority-only compare function.
3080 ** a -- the first argument.
3081 ** b -- the second argument.
3106 ** WORKCMPF1 -- first compare function for ordering work based on host name.
3108 ** Sorts on host name, lock status, and priority in that order.
3111 ** a -- the first argument.
3112 ** b -- the second argument.
3129 if (a->w_host != NULL && b->w_host == NULL)
3131 else if (a->w_host == NULL && b->w_host != NULL)
3133 if (a->w_host != NULL && b->w_host != NULL &&
3134 (i = sm_strcasecmp(a->w_host, b->w_host)) != 0)
3138 if (a->w_lock != b->w_lock)
3139 return b->w_lock - a->w_lock;
3142 return workcmpf0(a, b);
3145 ** WORKCMPF2 -- second compare function for ordering work based on host name.
3147 ** Sorts on lock status, host name, and priority in that order.
3150 ** a -- the first argument.
3151 ** b -- the second argument.
3168 if (a->w_lock != b->w_lock)
3169 return a->w_lock - b->w_lock;
3172 if (a->w_host != NULL && b->w_host == NULL)
3174 else if (a->w_host == NULL && b->w_host != NULL)
3176 if (a->w_host != NULL && b->w_host != NULL &&
3177 (i = sm_strcasecmp(a->w_host, b->w_host)) != 0)
3181 return workcmpf0(a, b);
3184 ** WORKCMPF3 -- simple submission-time-only compare function.
3187 ** a -- the first argument.
3188 ** b -- the second argument.
3202 if (a->w_ctime > b->w_ctime)
3204 else if (a->w_ctime < b->w_ctime)
3210 ** WORKCMPF4 -- compare based on file name
3213 ** a -- the first argument.
3214 ** b -- the second argument.
3228 return strcmp(a->w_name, b->w_name);
3231 ** WORKCMPF5 -- compare based on assigned random number
3234 ** a -- the first argument (ignored).
3235 ** b -- the second argument (ignored).
3247 if (strlen(a->w_name) < randi || strlen(b->w_name) < randi)
3249 return a->w_name[randi] - b->w_name[randi];
3252 ** WORKCMPF6 -- simple modification-time-only compare function.
3255 ** a -- the first argument.
3256 ** b -- the second argument.
3270 if (a->w_mtime > b->w_mtime)
3272 else if (a->w_mtime < b->w_mtime)
3279 ** WORKCMPF7 -- compare function for ordering work based on shuffled host name.
3281 ** Sorts on lock status, host name, and priority in that order.
3284 ** a -- the first argument.
3285 ** b -- the second argument.
3302 if (a->w_lock != b->w_lock)
3303 return a->w_lock - b->w_lock;
3306 if (a->w_host != NULL && b->w_host == NULL)
3308 else if (a->w_host == NULL && b->w_host != NULL)
3310 if (a->w_host != NULL && b->w_host != NULL &&
3311 (i = sm_strshufflecmp(a->w_host, b->w_host)) != 0)
3315 return workcmpf0(a, b);
3317 #endif /* _FFR_RHS */
3319 ** STRREV -- reverse string
3321 ** Returns a pointer to a new string that is the reverse of
3322 ** the string pointed to by fwd. The space for the new
3323 ** string is obtained using xalloc().
3326 ** fwd -- the string to reverse.
3329 ** the reversed string.
3340 rev = xalloc(len + 1);
3341 for (cnt = 0; cnt < len; ++cnt)
3342 rev[cnt] = fwd[len - cnt - 1];
3352 static unsigned char ShuffledAlphabet[NCHAR];
3355 init_shuffle_alphabet()
3357 static bool init = false;
3363 /* fill the ShuffledAlphabet */
3364 for (i = 0; i < NCHAR; i++)
3365 ShuffledAlphabet[i] = i;
3368 for (i = 1; i < NCHAR; i++)
3370 register int j = get_random() % NCHAR;
3373 tmp = ShuffledAlphabet[j];
3374 ShuffledAlphabet[j] = ShuffledAlphabet[i];
3375 ShuffledAlphabet[i] = tmp;
3378 /* make it case insensitive */
3379 for (i = 'A'; i <= 'Z'; i++)
3380 ShuffledAlphabet[i] = ShuffledAlphabet[i + 'a' - 'A'];
3382 /* fill the upper part */
3383 for (i = 0; i < NCHAR; i++)
3384 ShuffledAlphabet[i + NCHAR] = ShuffledAlphabet[i];
3389 sm_strshufflecmp(a, b)
3393 const unsigned char *us1 = (const unsigned char *) a;
3394 const unsigned char *us2 = (const unsigned char *) b;
3396 while (ShuffledAlphabet[*us1] == ShuffledAlphabet[*us2++])
3401 return (ShuffledAlphabet[*us1] - ShuffledAlphabet[*--us2]);
3403 #endif /* _FFR_RHS */
3406 ** DOWORK -- do a work request.
3409 ** qgrp -- the index of the queue group for the job.
3410 ** qdir -- the index of the queue directory for the job.
3411 ** id -- the ID of the job to run.
3412 ** forkflag -- if set, run this in background.
3413 ** requeueflag -- if set, reinstantiate the queue quickly.
3414 ** This is used when expanding aliases in the queue.
3415 ** If forkflag is also set, it doesn't wait for the
3417 ** e - the envelope in which to run it.
3420 ** process id of process that is running the queue job.
3423 ** The work request is satisfied if possible.
3427 dowork(qgrp, qdir, id, forkflag, requeueflag, e)
3433 register ENVELOPE *e;
3439 sm_dprintf("dowork(%s/%s)\n", qid_printqueue(qgrp, qdir), id);
3448 ** Since the delivery may happen in a child and the
3449 ** parent does not wait, the parent may close the
3450 ** maps thereby removing any shared memory used by
3451 ** the map. Therefore, close the maps now so the
3452 ** child will dynamically open them if necessary.
3460 syserr("dowork: cannot fork");
3465 /* parent -- clean out connection cache */
3466 mci_flush(false, NULL);
3471 ** Initialize exception stack and default exception
3472 ** handler for child process.
3475 /* Reset global flags */
3476 RestartRequest = NULL;
3477 RestartWorkGroup = false;
3478 ShutdownRequest = NULL;
3480 CurrentPid = getpid();
3481 sm_exc_newthread(fatal_error);
3484 ** See note above about SMTP processes and SIGCHLD.
3487 if (OpMode == MD_SMTP ||
3488 OpMode == MD_DAEMON ||
3489 MaxQueueChildren > 0)
3492 sm_releasesignal(SIGCHLD);
3493 (void) sm_signal(SIGCHLD, SIG_DFL);
3496 /* child -- error messages to the transcript */
3497 QuickAbort = OnlyOneError = false;
3509 ** Lock the control file to avoid duplicate deliveries.
3510 ** Then run the file as though we had just read it.
3511 ** We save an idea of the temporary name so we
3512 ** can recover on interrupt.
3517 /* Reset global flags */
3518 RestartRequest = NULL;
3519 RestartWorkGroup = false;
3520 ShutdownRequest = NULL;
3524 /* set basic modes, etc. */
3527 rpool = sm_rpool_new_x(NULL);
3528 clearenvelope(e, false, rpool);
3529 e->e_flags |= EF_QUEUERUN|EF_GLOBALERRS;
3530 set_delivery_mode(SM_DELIVER, e);
3531 e->e_errormode = EM_MAIL;
3535 GrabTo = UseErrorsTo = false;
3540 set_op_mode(MD_QUEUERUN);
3542 sm_setproctitle(true, e, "%s from queue", qid_printname(e));
3544 sm_syslog(LOG_DEBUG, e->e_id, "dowork, pid=%d",
3547 /* don't use the headers from sendmail.cf... */
3550 /* read the queue control file -- return if locked */
3551 if (!readqf(e, false))
3553 if (tTd(40, 4) && e->e_id != NULL)
3554 sm_dprintf("readqf(%s) failed\n",
3558 finis(false, true, EX_OK);
3561 /* adding this frees 8 bytes */
3562 clearenvelope(e, false, rpool);
3564 /* adding this frees 12 bytes */
3565 sm_rpool_free(rpool);
3571 e->e_flags |= EF_INQUEUE;
3572 eatheader(e, requeueflag, true);
3575 queueup(e, false, false);
3577 /* do the delivery */
3578 sendall(e, SM_DELIVER);
3580 /* finish up and exit */
3582 finis(true, true, ExitStat);
3585 dropenvelope(e, true, false);
3586 sm_rpool_free(rpool);
3595 ** DOWORKLIST -- process a list of envelopes as work requests
3597 ** Similar to dowork(), except that after forking, it processes an
3598 ** envelope and its siblings, treating each envelope as a work request.
3601 ** el -- envelope to be processed including its siblings.
3602 ** forkflag -- if set, run this in background.
3603 ** requeueflag -- if set, reinstantiate the queue quickly.
3604 ** This is used when expanding aliases in the queue.
3605 ** If forkflag is also set, it doesn't wait for the
3609 ** process id of process that is running the queue job.
3612 ** The work request is satisfied if possible.
3616 doworklist(el, forkflag, requeueflag)
3625 sm_dprintf("doworklist()\n");
3634 ** Since the delivery may happen in a child and the
3635 ** parent does not wait, the parent may close the
3636 ** maps thereby removing any shared memory used by
3637 ** the map. Therefore, close the maps now so the
3638 ** child will dynamically open them if necessary.
3646 syserr("doworklist: cannot fork");
3651 /* parent -- clean out connection cache */
3652 mci_flush(false, NULL);
3657 ** Initialize exception stack and default exception
3658 ** handler for child process.
3661 /* Reset global flags */
3662 RestartRequest = NULL;
3663 RestartWorkGroup = false;
3664 ShutdownRequest = NULL;
3666 CurrentPid = getpid();
3667 sm_exc_newthread(fatal_error);
3670 ** See note above about SMTP processes and SIGCHLD.
3673 if (OpMode == MD_SMTP ||
3674 OpMode == MD_DAEMON ||
3675 MaxQueueChildren > 0)
3678 sm_releasesignal(SIGCHLD);
3679 (void) sm_signal(SIGCHLD, SIG_DFL);
3682 /* child -- error messages to the transcript */
3683 QuickAbort = OnlyOneError = false;
3696 ** Lock the control file to avoid duplicate deliveries.
3697 ** Then run the file as though we had just read it.
3698 ** We save an idea of the temporary name so we
3699 ** can recover on interrupt.
3704 /* Reset global flags */
3705 RestartRequest = NULL;
3706 RestartWorkGroup = false;
3707 ShutdownRequest = NULL;
3711 /* set basic modes, etc. */
3714 GrabTo = UseErrorsTo = false;
3719 set_op_mode(MD_QUEUERUN);
3722 sm_syslog(LOG_DEBUG, el->e_id, "doworklist, pid=%d",
3725 for (ei = el; ei != NULL; ei = ei->e_sibling)
3730 if (WILL_BE_QUEUED(ei->e_sendmode))
3733 else if (QueueMode != QM_QUARANTINE &&
3734 ei->e_quarmsg != NULL)
3736 #endif /* _FFR_QUARANTINE */
3738 rpool = sm_rpool_new_x(NULL);
3739 clearenvelope(&e, true, rpool);
3740 e.e_flags |= EF_QUEUERUN|EF_GLOBALERRS;
3741 set_delivery_mode(SM_DELIVER, &e);
3742 e.e_errormode = EM_MAIL;
3744 e.e_qgrp = ei->e_qgrp;
3745 e.e_qdir = ei->e_qdir;
3747 sm_setproctitle(true, &e, "%s from queue", qid_printname(&e));
3749 /* don't use the headers from sendmail.cf... */
3753 /* read the queue control file -- return if locked */
3754 if (readqf(&e, false))
3756 e.e_flags |= EF_INQUEUE;
3757 eatheader(&e, requeueflag, true);
3760 queueup(&e, false, false);
3762 /* do the delivery */
3763 sendall(&e, SM_DELIVER);
3764 dropenvelope(&e, true, false);
3768 if (tTd(40, 4) && e.e_id != NULL)
3769 sm_dprintf("readqf(%s) failed\n",
3772 sm_rpool_free(rpool);
3776 /* restore CurEnv */
3779 /* finish up and exit */
3781 finis(true, true, ExitStat);
3785 ** READQF -- read queue file and set up environment.
3788 ** e -- the envelope of the job to run.
3789 ** openonly -- only open the qf (returned as e_lockfp)
3792 ** true if it successfully read the queue file.
3796 ** The queue file is returned locked.
3801 register ENVELOPE *e;
3804 register SM_FILE_T *qfp;
3806 struct stat st, stf;
3813 bool nomore = false;
3817 char qf[MAXPATHLEN];
3821 ** Read and process the file.
3824 (void) sm_strlcpy(qf, queuename(e, ANYQFL_LETTER), sizeof qf);
3825 qfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, qf, SM_IO_RDWR_B, NULL);
3828 int save_errno = errno;
3831 sm_dprintf("readqf(%s): sm_io_open failure (%s)\n",
3832 qf, sm_errstring(errno));
3836 syserr("readqf: no control file %s", qf);
3841 if (!lockfile(sm_io_getinfo(qfp, SM_IO_WHAT_FD, NULL), qf, NULL,
3844 /* being processed by another queuer */
3846 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
3847 "%s: locked\n", e->e_id);
3849 sm_dprintf("%s: locked\n", e->e_id);
3851 sm_syslog(LOG_DEBUG, e->e_id, "locked");
3852 (void) sm_io_close(qfp, SM_TIME_DEFAULT);
3860 ** Prevent locking race condition.
3862 ** Process A: readqf(): qfp = fopen(qffile)
3863 ** Process B: queueup(): rename(tf, qf)
3864 ** Process B: unlocks(tf)
3865 ** Process A: lockfile(qf);
3867 ** Process A (us) has the old qf file (before the rename deleted
3868 ** the directory entry) and will be delivering based on old data.
3869 ** This can lead to multiple deliveries of the same recipients.
3871 ** Catch this by checking if the underlying qf file has changed
3872 ** *after* acquiring our lock and if so, act as though the file
3873 ** was still locked (i.e., just return like the lockfile() case
3877 if (stat(qf, &stf) < 0 ||
3878 fstat(sm_io_getinfo(qfp, SM_IO_WHAT_FD, NULL), &st) < 0)
3880 /* must have been being processed by someone else */
3882 sm_dprintf("readqf(%s): [f]stat failure (%s)\n",
3883 qf, sm_errstring(errno));
3884 (void) sm_io_close(qfp, SM_TIME_DEFAULT);
3888 if (st.st_nlink != stf.st_nlink ||
3889 st.st_dev != stf.st_dev ||
3890 ST_INODE(st) != ST_INODE(stf) ||
3891 #if HAS_ST_GEN && 0 /* AFS returns garbage in st_gen */
3892 st.st_gen != stf.st_gen ||
3893 #endif /* HAS_ST_GEN && 0 */
3894 st.st_uid != stf.st_uid ||
3895 st.st_gid != stf.st_gid ||
3896 st.st_size != stf.st_size)
3898 /* changed after opened */
3900 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
3901 "%s: changed\n", e->e_id);
3903 sm_dprintf("%s: changed\n", e->e_id);
3905 sm_syslog(LOG_DEBUG, e->e_id, "changed");
3906 (void) sm_io_close(qfp, SM_TIME_DEFAULT);
3911 ** Check the queue file for plausibility to avoid attacks.
3914 qsafe = S_IWOTH|S_IWGRP;
3915 if (bitset(S_IWGRP, QueueFileMode))
3918 bogus = st.st_uid != geteuid() &&
3919 st.st_uid != TrustedUid &&
3920 geteuid() != RealUid;
3923 ** If this qf file results from a set-group-ID binary, then
3924 ** we check whether the directory is group-writable,
3925 ** the queue file mode contains the group-writable bit, and
3926 ** the groups are the same.
3927 ** Notice: this requires that the set-group-ID binary is used to
3931 if (bogus && st.st_gid == getegid() && UseMSP)
3936 bp = SM_LAST_DIR_DELIM(qf);
3944 if (stat(delim == '\0' ? "." : qf, &dst) < 0)
3945 syserr("readqf: cannot stat directory %s",
3946 delim == '\0' ? "." : qf);
3949 bogus = !(bitset(S_IWGRP, QueueFileMode) &&
3950 bitset(S_IWGRP, dst.st_mode) &&
3951 dst.st_gid == st.st_gid);
3957 bogus = bitset(qsafe, st.st_mode);
3962 sm_syslog(LOG_ALERT, e->e_id,
3963 "bogus queue file, uid=%d, gid=%d, mode=%o",
3964 st.st_uid, st.st_gid, st.st_mode);
3967 sm_dprintf("readqf(%s): bogus file\n", qf);
3968 e->e_flags |= EF_INQUEUE;
3970 loseqfile(e, "bogus file uid/gid in mqueue");
3971 (void) sm_io_close(qfp, SM_TIME_DEFAULT);
3975 if (st.st_size == 0)
3977 /* must be a bogus file -- if also old, just remove it */
3978 if (!openonly && st.st_ctime + 10 * 60 < curtime())
3980 (void) xunlink(queuename(e, DATAFL_LETTER));
3981 (void) xunlink(queuename(e, ANYQFL_LETTER));
3983 (void) sm_io_close(qfp, SM_TIME_DEFAULT);
3987 if (st.st_nlink == 0)
3990 ** Race condition -- we got a file just as it was being
3991 ** unlinked. Just assume it is zero length.
3994 (void) sm_io_close(qfp, SM_TIME_DEFAULT);
4000 ** If we don't own the file mark it as unsafe.
4001 ** However, allow TrustedUser to own it as well
4002 ** in case TrustedUser manipulates the queue.
4005 if (st.st_uid != geteuid() && st.st_uid != TrustedUid)
4006 e->e_flags |= EF_UNSAFE;
4007 #else /* _FFR_TRUSTED_QF */
4008 /* If we don't own the file mark it as unsafe */
4009 if (st.st_uid != geteuid())
4010 e->e_flags |= EF_UNSAFE;
4011 #endif /* _FFR_TRUSTED_QF */
4013 /* good file -- save this lock */
4016 /* Just wanted the open file */
4020 /* do basic system initialization */
4022 macdefine(&e->e_macro, A_PERM, 'i', e->e_id);
4025 e->e_flags |= EF_GLOBALERRS;
4026 set_op_mode(MD_QUEUERUN);
4029 e->e_qfletter = queue_letter(e, ANYQFL_LETTER);
4030 #endif /* _FFR_QUARANTINE */
4031 e->e_dfqgrp = e->e_qgrp;
4032 e->e_dfqdir = e->e_qdir;
4033 #if _FFR_QUEUE_MACRO
4034 macdefine(&e->e_macro, A_TEMP, macid("{queue}"),
4035 qid_printqueue(e->e_qgrp, e->e_qdir));
4036 #endif /* _FFR_QUEUE_MACRO */
4040 e->e_queuealg = QD_LINEAR;
4041 e->e_queuedelay = (time_t) 0;
4042 #endif /* _FFR_QUEUEDELAY */
4043 while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL)
4045 unsigned long qflags;
4052 sm_dprintf("+++++ %s\n", bp);
4057 syserr("SECURITY ALERT: extra or bogus data in queue file: %s",
4059 err = "bogus queue line";
4064 case 'A': /* AUTH= parameter */
4065 if (!xtextok(&bp[1]))
4067 e->e_auth_param = sm_rpool_strdup_x(e->e_rpool, &bp[1]);
4070 case 'B': /* body type */
4071 r = check_bodytype(&bp[1]);
4072 if (!BODYTYPE_VALID(r))
4074 e->e_bodytype = sm_rpool_strdup_x(e->e_rpool, &bp[1]);
4077 case 'C': /* specify controlling user */
4078 ctladdr = setctluser(&bp[1], qfver, e);
4081 case 'D': /* data file name */
4082 /* obsolete -- ignore */
4085 case 'd': /* data file directory name */
4089 #if _FFR_MSP_PARANOIA
4090 /* forbid queue groups in MSP? */
4093 #endif /* _FFR_MSP_PARANOIA */
4095 qgrp < NumQueue && Queue[qgrp] != NULL;
4099 qdir < Queue[qgrp]->qg_numqueues;
4103 Queue[qgrp]->qg_qpaths[qdir].qp_name)
4112 err = "bogus queue file directory";
4118 case 'E': /* specify error recipient */
4119 /* no longer used */
4122 case 'F': /* flag bits */
4123 if (strncmp(bp, "From ", 5) == 0)
4125 /* we are being spoofed! */
4126 syserr("SECURITY ALERT: bogus qf line %s", bp);
4127 err = "bogus queue line";
4130 for (p = &bp[1]; *p != '\0'; p++)
4134 case '8': /* has 8 bit data */
4135 e->e_flags |= EF_HAS8BIT;
4138 case 'b': /* delete Bcc: header */
4139 e->e_flags |= EF_DELETE_BCC;
4142 case 'd': /* envelope has DSN RET= */
4143 e->e_flags |= EF_RET_PARAM;
4146 case 'n': /* don't return body */
4147 e->e_flags |= EF_NO_BODY_RETN;
4150 case 'r': /* response */
4151 e->e_flags |= EF_RESPONSE;
4154 case 's': /* split */
4155 e->e_flags |= EF_SPLIT;
4158 case 'w': /* warning sent */
4159 e->e_flags |= EF_WARNING;
4166 case 'G': /* queue delay algorithm */
4167 e->e_queuealg = atoi(&buf[1]);
4169 #endif /* _FFR_QUEUEDELAY */
4172 case 'q': /* quarantine reason */
4173 e->e_quarmsg = sm_rpool_strdup_x(e->e_rpool, &bp[1]);
4174 macdefine(&e->e_macro, A_PERM,
4175 macid("{quarantine}"), e->e_quarmsg);
4177 #endif /* _FFR_QUARANTINE */
4179 case 'H': /* header */
4182 ** count size before chompheader() destroys the line.
4183 ** this isn't accurate due to macro expansion, but
4184 ** better than before. "-3" to skip H?? at least.
4187 hdrsize += strlen(bp) - 3;
4188 (void) chompheader(&bp[1], CHHDR_QUEUE, NULL, e);
4191 case 'I': /* data file's inode number */
4192 /* regenerated below */
4195 case 'K': /* time of last delivery attempt */
4196 e->e_dtime = atol(&buf[1]);
4199 case 'L': /* Solaris Content-Length: */
4200 case 'M': /* message */
4201 /* ignore this; we want a new message next time */
4204 case 'N': /* number of delivery attempts */
4205 e->e_ntries = atoi(&buf[1]);
4207 /* if this has been tried recently, let it be */
4209 if (e->e_ntries > 0 && e->e_dtime <= now &&
4210 now < e->e_dtime + queuedelay(e))
4214 howlong = pintvl(now - e->e_dtime, true);
4216 (void) sm_io_fprintf(smioout,
4218 "%s: too young (%s)\n",
4221 sm_dprintf("%s: too young (%s)\n",
4224 sm_syslog(LOG_DEBUG, e->e_id,
4231 macdefine(&e->e_macro, A_TEMP,
4232 macid("{ntries}"), &buf[1]);
4235 /* adjust BIND parameters immediately */
4236 if (e->e_ntries == 0)
4238 _res.retry = TimeOuts.res_retry[RES_TO_FIRST];
4239 _res.retrans = TimeOuts.res_retrans[RES_TO_FIRST];
4243 _res.retry = TimeOuts.res_retry[RES_TO_NORMAL];
4244 _res.retrans = TimeOuts.res_retrans[RES_TO_NORMAL];
4246 #endif /* NAMED_BIND */
4249 case 'P': /* message priority */
4250 e->e_msgpriority = atol(&bp[1]) + WkTimeFact;
4253 case 'Q': /* original recipient */
4254 orcpt = sm_rpool_strdup_x(e->e_rpool, &bp[1]);
4257 case 'r': /* final recipient */
4258 frcpt = sm_rpool_strdup_x(e->e_rpool, &bp[1]);
4261 case 'R': /* specify recipient */
4267 while (*++p != '\0' && *p != ':')
4272 qflags |= QHASNOTIFY;
4276 qflags |= QPINGONSUCCESS;
4280 qflags |= QPINGONFAILURE;
4284 qflags |= QPINGONDELAY;
4292 if (ctladdr != NULL)
4293 ctladdr->q_flags |= QALIAS;
4296 default: /* ignore or complain? */
4303 macdefine(&e->e_macro, A_PERM, macid("{addr_type}"),
4306 q = parseaddr(++p, NULLADDR, RF_COPYALL, '\0',
4312 /* make sure we keep the current qgrp */
4313 if (ISVALIDQGRP(e->e_qgrp))
4314 q->q_qgrp = e->e_qgrp;
4315 q->q_alias = ctladdr;
4317 q->q_flags &= ~Q_PINGFLAGS;
4318 q->q_flags |= qflags;
4319 q->q_finalrcpt = frcpt;
4321 (void) recipient(q, &e->e_sendqueue, 0, e);
4325 macdefine(&e->e_macro, A_PERM, macid("{addr_type}"),
4329 case 'S': /* sender */
4330 setsender(sm_rpool_strdup_x(e->e_rpool, &bp[1]),
4331 e, NULL, '\0', true);
4334 case 'T': /* init time */
4335 e->e_ctime = atol(&bp[1]);
4338 case 'V': /* queue file version number */
4339 qfver = atoi(&bp[1]);
4340 if (queuedelay_qfver_unsupported(qfver))
4341 syserr("queue file version %d not supported: %s",
4343 "sendmail not compiled with _FFR_QUEUEDELAY");
4344 if (qfver <= QF_VERSION)
4346 syserr("Version number in queue file (%d) greater than max (%d)",
4348 err = "unsupported queue file version";
4354 case 'Y': /* current delay */
4355 e->e_queuedelay = (time_t) atol(&buf[1]);
4357 #endif /* _FFR_QUEUEDELAY */
4359 case 'Z': /* original envelope id from ESMTP */
4360 e->e_envid = sm_rpool_strdup_x(e->e_rpool, &bp[1]);
4361 macdefine(&e->e_macro, A_PERM,
4362 macid("{dsn_envid}"), e->e_envid);
4365 case '!': /* deliver by */
4367 /* format: flag (1 char) space long-integer */
4368 e->e_dlvr_flag = buf[1];
4369 e->e_deliver_by = strtol(&buf[3], NULL, 10);
4371 case '$': /* define macro */
4375 /* XXX elimate p? */
4376 r = macid_parse(&bp[1], &ep);
4379 p = sm_rpool_strdup_x(e->e_rpool, ep);
4380 macdefine(&e->e_macro, A_PERM, r, p);
4384 case '.': /* terminate file */
4389 syserr("readqf: %s: line %d: bad line \"%s\"",
4390 qf, LineNumber, shortenstring(bp, MAXSHORTSTR));
4391 err = "unrecognized line";
4396 sm_free(bp); /* XXX */
4400 ** If we haven't read any lines, this queue file is empty.
4401 ** Arrange to remove it without referencing any null pointers.
4404 if (LineNumber == 0)
4407 e->e_flags |= EF_CLRQUEUE|EF_FATALERRS|EF_RESPONSE;
4411 /* Check to make sure we have a complete queue file read */
4414 syserr("readqf: %s: incomplete queue file read", qf);
4415 (void) sm_io_close(qfp, SM_TIME_DEFAULT);
4419 /* possibly set ${dsn_ret} macro */
4420 if (bitset(EF_RET_PARAM, e->e_flags))
4422 if (bitset(EF_NO_BODY_RETN, e->e_flags))
4423 macdefine(&e->e_macro, A_PERM,
4424 macid("{dsn_ret}"), "hdrs");
4426 macdefine(&e->e_macro, A_PERM,
4427 macid("{dsn_ret}"), "full");
4431 ** Arrange to read the data file.
4434 p = queuename(e, DATAFL_LETTER);
4435 e->e_dfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, p, SM_IO_RDONLY_B,
4437 if (e->e_dfp == NULL)
4439 syserr("readqf: cannot open %s", p);
4443 e->e_flags |= EF_HAS_DF;
4444 if (fstat(sm_io_getinfo(e->e_dfp, SM_IO_WHAT_FD, NULL), &st)
4447 e->e_msgsize = st.st_size + hdrsize;
4448 e->e_dfdev = st.st_dev;
4449 e->e_dfino = ST_INODE(st);
4450 (void) sm_snprintf(buf, sizeof buf, "%ld",
4452 macdefine(&e->e_macro, A_TEMP, macid("{msg_size}"),
4461 ** There was some error reading the qf file (reason is in err var.)
4463 ** close file; clear e_lockfp since it is the same as qfp,
4464 ** hence it is invalid (as file) after qfp is closed;
4465 ** the qf file is on disk, so set the flag to avoid calling
4466 ** queueup() with bogus data.
4470 (void) sm_io_close(qfp, SM_TIME_DEFAULT);
4472 e->e_flags |= EF_INQUEUE;
4477 ** PRTSTR -- print a string, "unprintable" characters are shown as \oct
4480 ** s -- string to print
4481 ** ml -- maximum length of output
4484 ** number of entries
4487 ** Prints a string on stdout.
4499 while (ml-- > 0 && ((c = *s++) != '\0'))
4505 (void) sm_io_putc(smioout, SM_TIME_DEFAULT, c);
4506 (void) sm_io_putc(smioout, SM_TIME_DEFAULT, c);
4509 else if (isascii(c) && isprint(c))
4510 (void) sm_io_putc(smioout, SM_TIME_DEFAULT, c);
4514 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
4515 "\\%03o", c & 0xFF);
4520 ** PRINTNQE -- print out number of entries in the mail queue
4523 ** out -- output file pointer.
4524 ** prefix -- string to output in front of each line.
4531 printnqe(out, prefix)
4536 int i, k = 0, nrequests = 0;
4537 bool unknown = false;
4539 if (ShmId == SM_SHM_NO_ID)
4542 (void) sm_io_fprintf(out, SM_TIME_DEFAULT,
4543 "Data unavailable: shared memory not updated\n");
4545 (void) sm_io_fprintf(out, SM_TIME_DEFAULT,
4546 "%sNOTCONFIGURED:-1\r\n", prefix);
4549 for (i = 0; i < NumQueue && Queue[i] != NULL; i++)
4554 for (j = 0; j < Queue[i]->qg_numqueues; j++)
4561 n = QSHM_ENTRIES(Queue[i]->qg_qpaths[j].qp_idx);
4563 (void) sm_io_fprintf(out, SM_TIME_DEFAULT,
4565 prefix, qid_printqueue(i, j), n);
4568 (void) sm_io_fprintf(out, SM_TIME_DEFAULT,
4569 "%s: unknown number of entries\n",
4570 qid_printqueue(i, j));
4575 (void) sm_io_fprintf(out, SM_TIME_DEFAULT,
4577 qid_printqueue(i, j));
4581 (void) sm_io_fprintf(out, SM_TIME_DEFAULT,
4583 qid_printqueue(i, j), n);
4589 if (prefix == NULL && k > 1)
4590 (void) sm_io_fprintf(out, SM_TIME_DEFAULT,
4591 "\t\tTotal requests: %d%s\n",
4592 nrequests, unknown ? " (about)" : "");
4593 #else /* SM_CONF_SHM */
4595 (void) sm_io_fprintf(out, SM_TIME_DEFAULT,
4596 "Data unavailable without shared memory support\n");
4598 (void) sm_io_fprintf(out, SM_TIME_DEFAULT,
4599 "%sNOTAVAILABLE:-1\r\n", prefix);
4600 #endif /* SM_CONF_SHM */
4603 ** PRINTQUEUE -- print out a representation of the mail queue
4612 ** Prints a listing of the mail queue on the standard output.
4618 int i, k = 0, nrequests = 0;
4620 for (i = 0; i < NumQueue && Queue[i] != NULL; i++)
4625 for (j = 0; j < Queue[i]->qg_numqueues; j++)
4629 nrequests += print_single_queue(i, j);
4634 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
4635 "\t\tTotal requests: %d\n",
4639 ** PRINT_SINGLE_QUEUE -- print out a representation of a single mail queue
4642 ** qgrp -- the index of the queue group.
4643 ** qdir -- the queue directory.
4646 ** number of requests in mail queue.
4649 ** Prints a listing of the mail queue on the standard output.
4653 print_single_queue(qgrp, qdir)
4660 char qd[MAXPATHLEN];
4661 char qddf[MAXPATHLEN];
4666 (void) sm_strlcpy(qd, ".", sizeof qd);
4667 (void) sm_strlcpy(qddf, ".", sizeof qddf);
4671 (void) sm_strlcpyn(qd, sizeof qd, 2,
4672 Queue[qgrp]->qg_qpaths[qdir].qp_name,
4674 Queue[qgrp]->qg_qpaths[qdir].qp_subdirs)
4676 (void) sm_strlcpyn(qddf, sizeof qddf, 2,
4677 Queue[qgrp]->qg_qpaths[qdir].qp_name,
4679 Queue[qgrp]->qg_qpaths[qdir].qp_subdirs)
4684 ** Check for permission to print the queue
4687 if (bitset(PRIV_RESTRICTMAILQ, PrivacyFlags) && RealUid != 0)
4692 extern GIDSET_T InitialGidSet[NGROUPS_MAX];
4693 #endif /* NGROUPS_MAX */
4695 if (stat(qd, &st) < 0)
4697 syserr("Cannot stat %s",
4698 qid_printqueue(qgrp, qdir));
4705 if (InitialGidSet[n] == st.st_gid)
4708 if (n < 0 && RealGid != st.st_gid)
4709 #else /* NGROUPS_MAX */
4710 if (RealGid != st.st_gid)
4711 #endif /* NGROUPS_MAX */
4713 usrerr("510 You are not permitted to see the queue");
4720 ** Read and order the queue.
4723 nrequests = gatherq(qgrp, qdir, true, NULL, NULL);
4724 (void) sortq(Queue[qgrp]->qg_maxlist);
4727 ** Print the work list that we have read.
4730 /* first see if there is anything */
4733 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "%s is empty\n",
4734 qid_printqueue(qgrp, qdir));
4738 sm_getla(); /* get load average */
4740 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "\t\t%s (%d request%s",
4741 qid_printqueue(qgrp, qdir),
4742 nrequests, nrequests == 1 ? "" : "s");
4743 if (MaxQueueRun > 0 && nrequests > MaxQueueRun)
4744 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
4745 ", only %d printed", MaxQueueRun);
4747 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
4748 ")\n-----Q-ID----- --Size-- -Priority- ---Q-Time--- --------Sender/Recipient--------\n");
4750 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
4751 ")\n-----Q-ID----- --Size-- -----Q-Time----- ------------Sender/Recipient-----------\n");
4752 for (w = WorkQ; w != NULL; w = w->w_next)
4755 auto time_t submittime = 0;
4760 char quarmsg[MAXLINE];
4761 #endif /* _FFR_QUARANTINE */
4762 char statmsg[MAXLINE];
4763 char bodytype[MAXNAME + 1];
4764 char qf[MAXPATHLEN];
4769 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "%13s",
4771 (void) sm_strlcpyn(qf, sizeof qf, 3, qd, "/", w->w_name);
4772 f = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, qf, SM_IO_RDONLY_B,
4777 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
4778 " (permission denied)\n");
4779 else if (errno == ENOENT)
4780 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
4781 " (job completed)\n");
4783 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
4785 sm_errstring(errno));
4789 w->w_name[0] = DATAFL_LETTER;
4790 (void) sm_strlcpyn(qf, sizeof qf, 3, qddf, "/", w->w_name);
4791 if (stat(qf, &st) >= 0)
4792 dfsize = st.st_size;
4798 ** Maybe the df file can't be statted because
4799 ** it is in a different directory than the qf file.
4800 ** In order to find out, we must read the qf file.
4803 newenvelope(&e, &BlankEnvelope, sm_rpool_new_x(NULL));
4804 e.e_id = w->w_name + 2;
4808 if (readqf(&e, false))
4810 char *df = queuename(&e, DATAFL_LETTER);
4811 if (stat(df, &st) >= 0)
4812 dfsize = st.st_size;
4814 if (e.e_lockfp != NULL)
4816 (void) sm_io_close(e.e_lockfp, SM_TIME_DEFAULT);
4819 clearenvelope(&e, false, e.e_rpool);
4820 sm_rpool_free(e.e_rpool);
4823 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "*");
4825 else if (QueueMode == QM_LOST)
4826 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "?");
4827 #endif /* _FFR_QUARANTINE */
4828 else if (w->w_tooyoung)
4829 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "-");
4830 else if (shouldqueue(w->w_pri, w->w_ctime))
4831 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "X");
4833 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, " ");
4839 #endif /* _FFR_QUARANTINE */
4840 statmsg[0] = bodytype[0] = '\0';
4842 while (sm_io_fgets(f, SM_TIME_DEFAULT, buf, sizeof buf) != NULL)
4853 case 'V': /* queue file version */
4854 qfver = atoi(&buf[1]);
4857 case 'M': /* error message */
4858 if ((i = strlen(&buf[1])) >= sizeof statmsg)
4859 i = sizeof statmsg - 1;
4860 memmove(statmsg, &buf[1], i);
4865 case 'q': /* quarantine reason */
4866 if ((i = strlen(&buf[1])) >= sizeof quarmsg)
4867 i = sizeof quarmsg - 1;
4868 memmove(quarmsg, &buf[1], i);
4871 #endif /* _FFR_QUARANTINE */
4873 case 'B': /* body type */
4874 if ((i = strlen(&buf[1])) >= sizeof bodytype)
4875 i = sizeof bodytype - 1;
4876 memmove(bodytype, &buf[1], i);
4880 case 'S': /* sender name */
4883 (void) sm_io_fprintf(smioout,
4885 "%8ld %10ld%c%.12s ",
4888 bitset(EF_WARNING, flags)
4890 ctime(&submittime) + 4);
4891 prtstr(&buf[1], 78);
4895 (void) sm_io_fprintf(smioout,
4899 ctime(&submittime));
4900 prtstr(&buf[1], 39);
4903 if (quarmsg[0] != '\0')
4905 (void) sm_io_fprintf(smioout,
4907 "\n QUARANTINE: %.*s",
4912 #endif /* _FFR_QUARANTINE */
4913 if (statmsg[0] != '\0' || bodytype[0] != '\0')
4915 (void) sm_io_fprintf(smioout,
4919 if (statmsg[0] != '\0')
4920 (void) sm_io_fprintf(smioout,
4929 case 'C': /* controlling user */
4931 (void) sm_io_fprintf(smioout,
4933 "\n\t\t\t\t\t\t(---%.64s---)",
4937 case 'R': /* recipient name */
4948 (void) sm_io_fprintf(smioout,
4955 (void) sm_io_fprintf(smioout,
4960 if (Verbose && statmsg[0] != '\0')
4962 (void) sm_io_fprintf(smioout,
4970 case 'T': /* creation time */
4971 submittime = atol(&buf[1]);
4974 case 'F': /* flag bits */
4975 for (p = &buf[1]; *p != '\0'; p++)
4980 flags |= EF_WARNING;
4986 if (submittime == (time_t) 0)
4987 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
4988 " (no control file)");
4989 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "\n");
4990 (void) sm_io_close(f, SM_TIME_DEFAULT);
4997 ** QUEUE_LETTER -- get the proper queue letter for the current QueueMode.
5000 ** e -- envelope to build it in/from.
5001 ** type -- the file type, used as the first character
5002 ** of the file name.
5005 ** the letter to use
5009 queue_letter(e, type)
5013 /* Change type according to QueueMode */
5014 if (type == ANYQFL_LETTER)
5016 if (e->e_quarmsg != NULL)
5017 type = QUARQF_LETTER;
5023 type = NORMQF_LETTER;
5027 type = QUARQF_LETTER;
5031 type = LOSEQF_LETTER;
5035 /* should never happen */
5043 #endif /* _FFR_QUARANTINE */
5046 ** QUEUENAME -- build a file name in the queue directory for this envelope.
5049 ** e -- envelope to build it in/from.
5050 ** type -- the file type, used as the first character
5051 ** of the file name.
5054 ** a pointer to the queue name (in a static buffer).
5057 ** If no id code is already assigned, queuename() will
5058 ** assign an id code with assign_queueid(). If no queue
5059 ** directory is assigned, one will be set with setnewqueue().
5064 register ENVELOPE *e;
5070 static char buf[MAXPATHLEN];
5072 /* Assign an ID if needed */
5073 if (e->e_id == NULL)
5077 type = queue_letter(e, type);
5078 #endif /* _FFR_QUARANTINE */
5080 /* begin of filename */
5081 pref[0] = (char) type;
5085 /* Assign a queue group/directory if needed */
5086 if (type == XSCRPT_LETTER)
5089 ** We don't want to call setnewqueue() if we are fetching
5090 ** the pathname of the transcript file, because setnewqueue
5091 ** chooses a queue, and sometimes we need to write to the
5092 ** transcript file before we have gathered enough information
5093 ** to choose a queue.
5096 if (e->e_xfqgrp == NOQGRP || e->e_xfqdir == NOQDIR)
5098 if (e->e_qgrp != NOQGRP && e->e_qdir != NOQDIR)
5100 e->e_xfqgrp = e->e_qgrp;
5101 e->e_xfqdir = e->e_qdir;
5106 if (Queue[e->e_xfqgrp]->qg_numqueues <= 1)
5110 e->e_xfqdir = get_rand_mod(
5111 Queue[e->e_xfqgrp]->qg_numqueues);
5120 if (e->e_qgrp == NOQGRP || e->e_qdir == NOQDIR)
5122 if (type == DATAFL_LETTER)
5134 /* xf files always have a valid qd and qg picked above */
5135 if (e->e_qdir == NOQDIR && type != XSCRPT_LETTER)
5136 (void) sm_strlcpyn(buf, sizeof buf, 2, pref, e->e_id);
5142 if (bitset(QP_SUBDF, Queue[qg]->qg_qpaths[qd].qp_subdirs))
5148 #endif /* _FFR_QUARANTINE */
5153 if (bitset(QP_SUBQF, Queue[qg]->qg_qpaths[qd].qp_subdirs))
5158 if (bitset(QP_SUBXF, Queue[qg]->qg_qpaths[qd].qp_subdirs))
5163 sm_abort("queuename: bad queue file type %d", type);
5166 (void) sm_strlcpyn(buf, sizeof buf, 4,
5167 Queue[qg]->qg_qpaths[qd].qp_name,
5168 sub, pref, e->e_id);
5172 sm_dprintf("queuename: %s\n", buf);
5176 ** ASSIGN_QUEUEID -- assign a queue ID for this envelope.
5178 ** Assigns an id code if one does not already exist.
5179 ** This code assumes that nothing will remain in the queue for
5180 ** longer than 60 years. It is critical that files with the given
5181 ** name do not already exist in the queue.
5182 ** [No longer initializes e_qdir to NOQDIR.]
5185 ** e -- envelope to set it in.
5191 static const char QueueIdChars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwx";
5193 # define queuenextid() CurrentPid
5198 register ENVELOPE *e;
5200 pid_t pid = queuenextid();
5202 static long random_offset;
5204 char idbuf[MAXQFNAME - 2];
5207 if (e->e_id != NULL)
5210 /* see if we need to get a new base time/pid */
5211 if (cX >= QIC_LEN * QIC_LEN || LastQueueTime == 0 ||
5212 LastQueuePid != pid)
5214 time_t then = LastQueueTime;
5216 /* if the first time through, pick a random offset */
5217 if (LastQueueTime == 0)
5218 random_offset = get_random();
5220 while ((LastQueueTime = curtime()) == then &&
5221 LastQueuePid == pid)
5225 LastQueuePid = queuenextid();
5230 ** Generate a new sequence number between 0 and QIC_LEN*QIC_LEN-1.
5231 ** This lets us generate up to QIC_LEN*QIC_LEN unique queue ids
5232 ** per second, per process. With envelope splitting,
5233 ** a single message can consume many queue ids.
5236 seq = (int)((cX + random_offset) % (QIC_LEN * QIC_LEN));
5239 sm_dprintf("assign_queueid: random_offset = %ld (%d)\n",
5240 random_offset, seq);
5242 tm = gmtime(&LastQueueTime);
5243 idbuf[0] = QueueIdChars[tm->tm_year % QIC_LEN];
5244 idbuf[1] = QueueIdChars[tm->tm_mon];
5245 idbuf[2] = QueueIdChars[tm->tm_mday];
5246 idbuf[3] = QueueIdChars[tm->tm_hour];
5247 idbuf[4] = QueueIdChars[tm->tm_min];
5248 idbuf[5] = QueueIdChars[tm->tm_sec];
5249 idbuf[6] = QueueIdChars[seq / QIC_LEN];
5250 idbuf[7] = QueueIdChars[seq % QIC_LEN];
5251 (void) sm_snprintf(&idbuf[8], sizeof idbuf - 8, "%06d",
5252 (int) LastQueuePid);
5253 e->e_id = sm_rpool_strdup_x(e->e_rpool, idbuf);
5254 macdefine(&e->e_macro, A_PERM, 'i', e->e_id);
5256 /* XXX: inherited from MainEnvelope */
5257 e->e_qgrp = NOQGRP; /* too early to do anything else */
5259 e->e_xfqgrp = NOQGRP;
5262 /* New ID means it's not on disk yet */
5263 e->e_qfletter = '\0';
5264 #endif /* _FFR_QUARANTINE */
5266 sm_dprintf("assign_queueid: assigned id %s, e=%p\n",
5269 sm_syslog(LOG_DEBUG, e->e_id, "assigned id");
5272 ** SYNC_QUEUE_TIME -- Assure exclusive PID in any given second
5274 ** Make sure one PID can't be used by two processes in any one second.
5276 ** If the system rotates PIDs fast enough, may get the
5277 ** same pid in the same second for two distinct processes.
5278 ** This will interfere with the queue file naming system.
5290 #if FAST_PID_RECYCLE
5291 if (OpMode != MD_TEST &&
5292 OpMode != MD_VERIFY &&
5293 LastQueueTime > 0 &&
5294 LastQueuePid == CurrentPid &&
5295 curtime() == LastQueueTime)
5297 #endif /* FAST_PID_RECYCLE */
5300 ** UNLOCKQUEUE -- unlock the queue entry for a specified envelope
5303 ** e -- the envelope to unlock.
5309 ** unlocks the queue for `e'.
5317 sm_dprintf("unlockqueue(%s)\n",
5318 e->e_id == NULL ? "NOQUEUE" : e->e_id);
5321 /* if there is a lock file in the envelope, close it */
5322 if (e->e_lockfp != NULL)
5323 (void) sm_io_close(e->e_lockfp, SM_TIME_DEFAULT);
5326 /* don't create a queue id if we don't already have one */
5327 if (e->e_id == NULL)
5330 /* remove the transcript */
5332 sm_syslog(LOG_DEBUG, e->e_id, "unlock");
5334 (void) xunlink(queuename(e, XSCRPT_LETTER));
5337 ** SETCTLUSER -- create a controlling address
5339 ** Create a fake "address" given only a local login name; this is
5340 ** used as a "controlling user" for future recipient addresses.
5343 ** user -- the user name of the controlling user.
5344 ** qfver -- the version stamp of this queue file.
5348 ** An address descriptor for the controlling user,
5349 ** using storage allocated from e->e_rpool.
5354 setctluser(user, qfver, e)
5359 register ADDRESS *a;
5364 ** See if this clears our concept of controlling user.
5367 if (user == NULL || *user == '\0')
5371 ** Set up addr fields for controlling user.
5374 a = (ADDRESS *) sm_rpool_malloc_x(e->e_rpool, sizeof *a);
5375 memset((char *) a, '\0', sizeof *a);
5380 a->q_user = sm_rpool_strdup_x(e->e_rpool, p);
5384 p = strtok(user, ":");
5385 a->q_user = sm_rpool_strdup_x(e->e_rpool, user);
5388 if ((p = strtok(NULL, ":")) != NULL)
5390 if ((p = strtok(NULL, ":")) != NULL)
5392 if ((p = strtok(NULL, ":")) != NULL)
5396 a->q_flags |= QGOODUID;
5398 /* if there is another ':': restore it */
5399 if ((o = strtok(NULL, ":")) != NULL && o > p)
5403 else if ((pw = sm_getpwnam(user)) != NULL)
5405 if (*pw->pw_dir == '\0')
5407 else if (strcmp(pw->pw_dir, "/") == 0)
5410 a->q_home = sm_rpool_strdup_x(e->e_rpool, pw->pw_dir);
5411 a->q_uid = pw->pw_uid;
5412 a->q_gid = pw->pw_gid;
5413 a->q_flags |= QGOODUID;
5417 a->q_flags |= QPRIMARY; /* flag as a "ctladdr" */
5418 a->q_mailer = LocalMailer;
5420 a->q_paddr = sm_rpool_strdup_x(e->e_rpool, a->q_user);
5422 a->q_paddr = sm_rpool_strdup_x(e->e_rpool, p);
5426 ** LOSEQFILE -- rename queue file with LOSEQF_LETTER & try to let someone know
5429 ** e -- the envelope (e->e_id will be used).
5430 ** why -- reported to whomever can hear.
5438 register ENVELOPE *e;
5443 char buf[MAXPATHLEN];
5445 if (e == NULL || e->e_id == NULL)
5447 p = queuename(e, ANYQFL_LETTER);
5448 if (sm_strlcpy(buf, p, sizeof buf) >= sizeof buf)
5450 if (!bitset(EF_INQUEUE, e->e_flags))
5451 queueup(e, false, true);
5453 else if (QueueMode == QM_LOST)
5455 #endif /* _FFR_QUARANTINE */
5457 /* if already lost, no need to re-lose */
5460 p = queuename(e, LOSEQF_LETTER);
5461 if (rename(buf, p) < 0)
5462 syserr("cannot rename(%s, %s), uid=%d",
5463 buf, p, (int) geteuid());
5464 else if (LogLevel > 0)
5465 sm_syslog(LOG_ALERT, e->e_id,
5466 "Losing %s: %s", buf, why);
5468 if (e->e_dfp != NULL)
5470 (void) sm_io_close(e->e_dfp, SM_TIME_DEFAULT);
5473 e->e_flags &= ~EF_HAS_DF;
5476 ** NAME2QID -- translate a queue group name to a queue group id
5479 ** queuename -- name of queue group.
5482 ** queue group id if found.
5483 ** NOQGRP otherwise.
5492 s = stab(queuename, ST_QUEUE, ST_FIND);
5495 return s->s_quegrp->qg_index;
5498 ** QID_PRINTNAME -- create externally printable version of queue id
5501 ** e -- the envelope.
5504 ** a printable version
5512 static char idbuf[MAXQFNAME + 34];
5517 if (e->e_id == NULL)
5522 if (e->e_qdir == NOQDIR)
5525 (void) sm_snprintf(idbuf, sizeof idbuf, "%.32s/%s",
5526 Queue[e->e_qgrp]->qg_qpaths[e->e_qdir].qp_name,
5531 ** QID_PRINTQUEUE -- create full version of queue directory for data files
5534 ** qgrp -- index in queue group.
5535 ** qdir -- the short version of the queue directory
5538 ** the full pathname to the queue (might point to a static var)
5542 qid_printqueue(qgrp, qdir)
5547 static char dir[MAXPATHLEN];
5550 return Queue[qgrp]->qg_qdir;
5552 if (strcmp(Queue[qgrp]->qg_qpaths[qdir].qp_name, ".") == 0)
5555 subdir = Queue[qgrp]->qg_qpaths[qdir].qp_name;
5557 (void) sm_strlcpyn(dir, sizeof dir, 4,
5558 Queue[qgrp]->qg_qdir,
5559 subdir == NULL ? "" : "/",
5560 subdir == NULL ? "" : subdir,
5562 Queue[qgrp]->qg_qpaths[qdir].qp_subdirs)
5568 ** PICKQDIR -- Pick a queue directory from a queue group
5571 ** qg -- queue group
5572 ** fsize -- file size in bytes
5573 ** e -- envelope, or NULL
5576 ** NOQDIR if no queue directory in qg has enough free space to
5577 ** hold a file of size 'fsize', otherwise the index of
5578 ** a randomly selected queue directory which resides on a
5579 ** file system with enough disk space.
5580 ** XXX This could be extended to select a queuedir with
5581 ** a few (the fewest?) number of entries. That data
5582 ** is available if shared memory is used.
5585 ** If the request fails and e != NULL then sm_syslog is called.
5589 pickqdir(qg, fsize, e)
5598 /* Pick a random directory, as a starting point. */
5599 if (qg->qg_numqueues <= 1)
5602 qdir = get_rand_mod(qg->qg_numqueues);
5604 if (MinBlocksFree <= 0 && fsize <= 0)
5608 ** Now iterate over the queue directories,
5609 ** looking for a directory with enough space for this message.
5615 QPATHS *qp = &qg->qg_qpaths[i];
5620 needed += fsize / FILE_SYS_BLKSIZE(qp->qp_fsysidx)
5621 + ((fsize % FILE_SYS_BLKSIZE(qp->qp_fsysidx)
5623 if (MinBlocksFree > 0)
5624 needed += MinBlocksFree;
5625 fsavail = FILE_SYS_AVAIL(qp->qp_fsysidx);
5632 ** might be not correctly updated,
5633 ** let's try to get the info directly.
5636 fsavail = freediskspace(FILE_SYS_NAME(qp->qp_fsysidx),
5641 #endif /* SM_CONF_SHM */
5642 if (needed <= fsavail)
5644 if (avail < fsavail)
5647 if (qg->qg_numqueues > 0)
5648 i = (i + 1) % qg->qg_numqueues;
5649 } while (i != qdir);
5651 if (e != NULL && LogLevel > 0)
5652 sm_syslog(LOG_ALERT, e->e_id,
5653 "low on space (%s needs %ld bytes + %ld blocks in %s), max avail: %ld",
5654 CurHostName == NULL ? "SMTP-DAEMON" : CurHostName,
5655 fsize, MinBlocksFree,
5656 qg->qg_qdir, avail);
5660 ** SETNEWQUEUE -- Sets a new queue group and directory
5662 ** Assign a queue group and directory to an envelope and store the
5663 ** directory in e->e_qdir.
5666 ** e -- envelope to assign a queue for.
5669 ** true if successful
5673 ** On success, e->e_qgrp and e->e_qdir are non-negative.
5674 ** On failure (not enough disk space),
5675 ** e->qgrp = NOQGRP, e->e_qdir = NOQDIR
5676 ** and usrerr() is invoked (which could raise an exception).
5684 sm_dprintf("setnewqueue: called\n");
5686 /* not set somewhere else */
5687 if (e->e_qgrp == NOQGRP)
5692 ** Use the queue group of the "first" recipient, as set by
5693 ** the "queuegroup" rule set. If that is not defined, then
5694 ** use the queue group of the mailer of the first recipient.
5695 ** If that is not defined either, then use the default
5697 ** Notice: "first" depends on the sorting of sendqueue
5699 ** To avoid problems with "bad" recipients look
5700 ** for a valid address first.
5705 (QS_IS_BADADDR(q->q_state) || QS_IS_DEAD(q->q_state)))
5711 else if (q->q_qgrp >= 0)
5712 e->e_qgrp = q->q_qgrp;
5713 else if (q->q_mailer != NULL &&
5714 ISVALIDQGRP(q->q_mailer->m_qgrp))
5715 e->e_qgrp = q->q_mailer->m_qgrp;
5718 e->e_dfqgrp = e->e_qgrp;
5721 if (ISVALIDQDIR(e->e_qdir) && ISVALIDQDIR(e->e_dfqdir))
5724 sm_dprintf("setnewqueue: e_qdir already assigned (%s)\n",
5725 qid_printqueue(e->e_qgrp, e->e_qdir));
5730 e->e_qdir = pickqdir(Queue[e->e_qgrp], e->e_msgsize, e);
5731 if (e->e_qdir == NOQDIR)
5734 if (!bitset(EF_FATALERRS, e->e_flags))
5735 usrerr("452 4.4.5 Insufficient disk space; try again later");
5736 e->e_flags |= EF_FATALERRS;
5741 sm_dprintf("setnewqueue: Assigned queue directory %s\n",
5742 qid_printqueue(e->e_qgrp, e->e_qdir));
5744 if (e->e_xfqgrp == NOQGRP || e->e_xfqdir == NOQDIR)
5746 e->e_xfqgrp = e->e_qgrp;
5747 e->e_xfqdir = e->e_qdir;
5749 e->e_dfqdir = e->e_qdir;
5753 ** CHKQDIR -- check a queue directory
5756 ** name -- name of queue directory
5757 ** sff -- flags for safefile()
5760 ** is it a queue directory?
5771 /* skip over . and .. directories */
5772 if (name[0] == '.' &&
5773 (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')))
5776 if (lstat(name, &statb) < 0)
5777 #else /* HASLSTAT */
5778 if (stat(name, &statb) < 0)
5779 #endif /* HASLSTAT */
5782 sm_dprintf("chkqdir: stat(\"%s\"): %s\n",
5783 name, sm_errstring(errno));
5787 if (S_ISLNK(statb.st_mode))
5790 ** For a symlink we need to make sure the
5791 ** target is a directory
5794 if (stat(name, &statb) < 0)
5797 sm_dprintf("chkqdir: stat(\"%s\"): %s\n",
5798 name, sm_errstring(errno));
5802 #endif /* HASLSTAT */
5804 if (!S_ISDIR(statb.st_mode))
5807 sm_dprintf("chkqdir: \"%s\": Not a directory\n",
5812 /* Print a warning if unsafe (but still use it) */
5813 /* XXX do this only if we want the warning? */
5814 i = safedirpath(name, RunAsUid, RunAsGid, NULL, sff, 0, 0);
5818 sm_dprintf("chkqdir: \"%s\": Not safe: %s\n",
5819 name, sm_errstring(i));
5822 sm_syslog(LOG_WARNING, NOQID,
5823 "queue directory \"%s\": Not safe: %s",
5824 name, sm_errstring(i));
5825 #endif /* _FFR_CHK_QUEUE */
5830 ** MULTIQUEUE_CACHE -- cache a list of paths to queues.
5832 ** Each potential queue is checked as the cache is built.
5833 ** Thereafter, each is blindly trusted.
5834 ** Note that we can be called again after a timeout to rebuild
5835 ** (although code for that is not ready yet).
5838 ** basedir -- base of all queue directories.
5839 ** blen -- strlen(basedir).
5840 ** qg -- queue group.
5841 ** qn -- number of queue directories already cached.
5842 ** phash -- pointer to hash value over queue dirs.
5844 ** only used if shared memory is active.
5845 #endif * SM_CONF_SHM *
5848 ** new number of queue directories.
5851 #define INITIAL_SLOTS 20
5852 #define ADD_SLOTS 10
5855 multiqueue_cache(basedir, blen, qg, qn, phash)
5860 unsigned int *phash;
5865 long sff = SFF_ANYFILE;
5866 char qpath[MAXPATHLEN];
5867 char subdir[MAXPATHLEN];
5868 char prefix[MAXPATHLEN]; /* dir relative to basedir */
5871 sm_dprintf("multiqueue_cache: called\n");
5873 /* Initialize to current directory */
5876 if (qg->qg_numqueues != 0 && qg->qg_qpaths != NULL)
5878 for (i = 0; i < qg->qg_numqueues; i++)
5880 if (qg->qg_qpaths[i].qp_name != NULL)
5881 (void) sm_free(qg->qg_qpaths[i].qp_name); /* XXX */
5883 (void) sm_free((char *) qg->qg_qpaths); /* XXX */
5884 qg->qg_qpaths = NULL;
5885 qg->qg_numqueues = 0;
5888 /* If running as root, allow safedirpath() checks to use privs */
5892 sff |= SFF_SAFEDIRPATH|SFF_NOWWFILES;
5894 sff |= SFF_NOGWFILES;
5895 #endif /* _FFR_CHK_QUEUE */
5897 if (!SM_IS_DIR_START(qg->qg_qdir))
5900 ** XXX we could add basedir, but then we have to realloc()
5901 ** the string... Maybe another time.
5904 syserr("QueuePath %s not absolute", qg->qg_qdir);
5905 ExitStat = EX_CONFIG;
5909 /* qpath: directory of current workgroup */
5910 len = sm_strlcpy(qpath, qg->qg_qdir, sizeof qpath);
5911 if (len >= sizeof qpath)
5913 syserr("QueuePath %.256s too long (%d max)",
5914 qg->qg_qdir, (int) sizeof qpath);
5915 ExitStat = EX_CONFIG;
5919 /* begin of qpath must be same as basedir */
5920 if (strncmp(basedir, qpath, blen) != 0 &&
5921 (strncmp(basedir, qpath, blen - 1) != 0 || len != blen - 1))
5923 syserr("QueuePath %s not subpath of QueueDirectory %s",
5925 ExitStat = EX_CONFIG;
5929 /* Do we have a nested subdirectory? */
5930 if (blen < len && SM_FIRST_DIR_DELIM(qg->qg_qdir + blen) != NULL)
5933 /* Copy subdirectory into prefix for later use */
5934 if (sm_strlcpy(prefix, qg->qg_qdir + blen, sizeof prefix) >=
5937 syserr("QueuePath %.256s too long (%d max)",
5938 qg->qg_qdir, (int) sizeof qpath);
5939 ExitStat = EX_CONFIG;
5942 cp = SM_LAST_DIR_DELIM(prefix);
5943 SM_ASSERT(cp != NULL);
5944 *cp = '\0'; /* cut off trailing / */
5947 /* This is guaranteed by the basedir check above */
5948 SM_ASSERT(len >= blen - 1);
5949 cp = &qpath[len - 1];
5953 register struct dirent *d;
5956 char relpath[MAXPATHLEN];
5958 *cp = '\0'; /* Overwrite wildcard */
5959 if ((cp = SM_LAST_DIR_DELIM(qpath)) == NULL)
5961 syserr("QueueDirectory: can not wildcard relative path");
5963 sm_dprintf("multiqueue_cache: \"%s*\": Can not wildcard relative path.\n",
5965 ExitStat = EX_CONFIG;
5971 ** Special case of top level wildcard, like /foo*
5975 (void) sm_strlcpy(qpath + 1, qpath, sizeof qpath - 1);
5979 *(cp++) = '\0'; /* Replace / with \0 */
5980 len = strlen(cp); /* Last component of queue directory */
5983 ** Path relative to basedir, with trailing /
5984 ** It will be modified below to specify the subdirectories
5985 ** so they can be opened without chdir().
5988 off = sm_strlcpyn(relpath, sizeof relpath, 2, prefix, "/");
5989 SM_ASSERT(off < sizeof relpath);
5992 sm_dprintf("multiqueue_cache: prefix=\"%s%s\"\n",
5995 /* It is always basedir: we don't need to store it per group */
5996 /* XXX: optimize this! -> one more global? */
5997 qg->qg_qdir = newstr(basedir);
5998 qg->qg_qdir[blen - 1] = '\0'; /* cut off trailing / */
6001 ** XXX Should probably wrap this whole loop in a timeout
6002 ** in case some wag decides to NFS mount the queues.
6005 /* Test path to get warning messages. */
6008 /* XXX qg_runasuid and qg_runasgid for specials? */
6009 i = safedirpath(basedir, RunAsUid, RunAsGid, NULL,
6011 if (i != 0 && tTd(41, 2))
6012 sm_dprintf("multiqueue_cache: \"%s\": Not safe: %s\n",
6013 basedir, sm_errstring(i));
6016 if ((dp = opendir(prefix)) == NULL)
6018 syserr("can not opendir(%s/%s)", qg->qg_qdir, prefix);
6020 sm_dprintf("multiqueue_cache: opendir(\"%s/%s\"): %s\n",
6021 qg->qg_qdir, prefix,
6022 sm_errstring(errno));
6023 ExitStat = EX_CONFIG;
6026 while ((d = readdir(dp)) != NULL)
6028 i = strlen(d->d_name);
6029 if (i < len || strncmp(d->d_name, cp, len) != 0)
6032 sm_dprintf("multiqueue_cache: \"%s\", skipped\n",
6037 /* Create relative pathname: prefix + local directory */
6038 i = sizeof(relpath) - off;
6039 if (sm_strlcpy(relpath + off, d->d_name, i) >= i)
6040 continue; /* way too long */
6042 if (!chkqdir(relpath, sff))
6045 if (qg->qg_qpaths == NULL)
6047 slotsleft = INITIAL_SLOTS;
6048 qg->qg_qpaths = (QPATHS *)xalloc((sizeof *qg->qg_qpaths) *
6050 qg->qg_numqueues = 0;
6052 else if (slotsleft < 1)
6054 qg->qg_qpaths = (QPATHS *)sm_realloc((char *)qg->qg_qpaths,
6055 (sizeof *qg->qg_qpaths) *
6058 if (qg->qg_qpaths == NULL)
6060 (void) closedir(dp);
6063 slotsleft += ADD_SLOTS;
6067 qg->qg_qpaths[qg->qg_numqueues].qp_subdirs = QP_NOSUB;
6069 #define CHKRSUBDIR(name, flag) \
6070 (void) sm_strlcpyn(subdir, sizeof subdir, 3, relpath, "/", name); \
6071 if (chkqdir(subdir, sff)) \
6072 qg->qg_qpaths[qg->qg_numqueues].qp_subdirs |= flag; \
6076 CHKRSUBDIR("qf", QP_SUBQF);
6077 CHKRSUBDIR("df", QP_SUBDF);
6078 CHKRSUBDIR("xf", QP_SUBXF);
6080 /* assert(strlen(d->d_name) < MAXPATHLEN - 14) */
6081 /* maybe even - 17 (subdirs) */
6083 if (prefix[0] != '.')
6084 qg->qg_qpaths[qg->qg_numqueues].qp_name =
6087 qg->qg_qpaths[qg->qg_numqueues].qp_name =
6091 sm_dprintf("multiqueue_cache: %d: \"%s\" cached (%x).\n",
6092 qg->qg_numqueues, relpath,
6093 qg->qg_qpaths[qg->qg_numqueues].qp_subdirs);
6095 qg->qg_qpaths[qg->qg_numqueues].qp_idx = qn;
6096 *phash = hash_q(relpath, *phash);
6097 #endif /* SM_CONF_SHM */
6102 (void) closedir(dp);
6107 if (qg->qg_numqueues == 0)
6109 qg->qg_qpaths = (QPATHS *) xalloc(sizeof *qg->qg_qpaths);
6111 /* test path to get warning messages */
6112 i = safedirpath(qpath, RunAsUid, RunAsGid, NULL, sff, 0, 0);
6115 syserr("can not opendir(%s)", qpath);
6117 sm_dprintf("multiqueue_cache: opendir(\"%s\"): %s\n",
6118 qpath, sm_errstring(i));
6119 ExitStat = EX_CONFIG;
6123 qg->qg_qpaths[0].qp_subdirs = QP_NOSUB;
6124 qg->qg_numqueues = 1;
6127 #define CHKSUBDIR(name, flag) \
6128 (void) sm_strlcpyn(subdir, sizeof subdir, 3, qg->qg_qdir, "/", name); \
6129 if (chkqdir(subdir, sff)) \
6130 qg->qg_qpaths[0].qp_subdirs |= flag; \
6133 CHKSUBDIR("qf", QP_SUBQF);
6134 CHKSUBDIR("df", QP_SUBDF);
6135 CHKSUBDIR("xf", QP_SUBXF);
6137 if (qg->qg_qdir[blen - 1] != '\0' &&
6138 qg->qg_qdir[blen] != '\0')
6141 ** Copy the last component into qpaths and
6145 qg->qg_qpaths[0].qp_name = newstr(qg->qg_qdir + blen);
6146 qg->qg_qdir[blen - 1] = '\0';
6149 qg->qg_qpaths[0].qp_name = newstr(".");
6152 qg->qg_qpaths[0].qp_idx = qn;
6153 *phash = hash_q(qg->qg_qpaths[0].qp_name, *phash);
6154 #endif /* SM_CONF_SHM */
6161 ** FILESYS_FIND -- find entry in FileSys table, or add new one
6163 ** Given the pathname of a directory, determine the file system
6164 ** in which that directory resides, and return a pointer to the
6165 ** entry in the FileSys table that describes the file system.
6166 ** A new entry is added if necessary (and requested).
6167 ** If the directory does not exist, -1 is returned.
6170 ** path -- pathname of directory
6171 ** add -- add to structure if not found.
6174 ** >=0: found: index in file system table
6175 ** <0: some error, i.e.,
6176 ** FSF_TOO_MANY: too many filesystems (-> syserr())
6177 ** FSF_STAT_FAIL: can't stat() filesystem (-> syserr())
6178 ** FSF_NOT_FOUND: not in list
6181 static short filesys_find __P((char *, bool));
6183 #define FSF_NOT_FOUND (-1)
6184 #define FSF_STAT_FAIL (-2)
6185 #define FSF_TOO_MANY (-3)
6188 filesys_find(path, add)
6195 if (stat(path, &st) < 0)
6197 syserr("cannot stat queue directory %s", path);
6198 return FSF_STAT_FAIL;
6200 for (i = 0; i < NumFileSys; ++i)
6202 if (FILE_SYS_DEV(i) == st.st_dev)
6205 if (i >= MAXFILESYS)
6207 syserr("too many queue file systems (%d max)", MAXFILESYS);
6208 return FSF_TOO_MANY;
6211 return FSF_NOT_FOUND;
6214 FILE_SYS_NAME(i) = path;
6215 FILE_SYS_DEV(i) = st.st_dev;
6216 FILE_SYS_AVAIL(i) = 0;
6217 FILE_SYS_BLKSIZE(i) = 1024; /* avoid divide by zero */
6222 ** FILESYS_SETUP -- set up mapping from queue directories to file systems
6224 ** This data structure is used to efficiently check the amount of
6225 ** free space available in a set of queue directories.
6228 ** add -- initialize structure if necessary.
6232 ** <0: some error, i.e.,
6233 ** FSF_NOT_FOUND: not in list
6234 ** FSF_STAT_FAIL: can't stat() filesystem (-> syserr())
6235 ** FSF_TOO_MANY: too many filesystems (-> syserr())
6238 static int filesys_setup __P((bool));
6249 for (i = 0; i < NumQueue && Queue[i] != NULL; i++)
6251 for (j = 0; j < Queue[i]->qg_numqueues; ++j)
6253 QPATHS *qp = &Queue[i]->qg_qpaths[j];
6255 fs = filesys_find(qp->qp_name, add);
6257 qp->qp_fsysidx = fs;
6268 ** FILESYS_UPDATE -- update amount of free space on all file systems
6270 ** The FileSys table is used to cache the amount of free space
6271 ** available on all queue directory file systems.
6272 ** This function updates the cached information if it has expired.
6281 ** Updates FileSys table.
6288 long avail, blksize;
6290 static time_t nextupdate = 0;
6293 /* only the daemon updates this structure */
6294 if (ShmId != SM_SHM_NO_ID && DaemonPid != CurrentPid)
6296 #endif /* SM_CONF_SHM */
6298 if (now < nextupdate)
6300 nextupdate = now + FILESYS_UPDATE_INTERVAL;
6301 for (i = 0; i < NumFileSys; ++i)
6303 FILESYS *fs = &FILE_SYS(i);
6305 avail = freediskspace(FILE_SYS_NAME(i), &blksize);
6306 if (avail < 0 || blksize <= 0)
6309 sm_syslog(LOG_ERR, NOQID,
6310 "filesys_update failed: %s, fs=%s, avail=%ld, blocksize=%ld",
6311 sm_errstring(errno),
6312 FILE_SYS_NAME(i), avail, blksize);
6314 fs->fs_blksize = 1024; /* avoid divide by zero */
6315 nextupdate = now + 2; /* let's do this soon again */
6319 fs->fs_avail = avail;
6320 fs->fs_blksize = blksize;
6325 #if _FFR_ANY_FREE_FS
6327 ** FILESYS_FREE -- check whether there is at least one fs with enough space.
6330 ** fsize -- file size in bytes
6333 ** true iff there is one fs with more than fsize bytes free.
6344 for (i = 0; i < NumFileSys; ++i)
6348 if (FILE_SYS_AVAIL(i) < 0 || FILE_SYS_BLKSIZE(i) <= 0)
6350 needed += fsize / FILE_SYS_BLKSIZE(i)
6351 + ((fsize % FILE_SYS_BLKSIZE(i)
6354 if (needed <= FILE_SYS_AVAIL(i))
6359 #endif /* _FFR_ANY_FREE_FS */
6361 #if _FFR_CONTROL_MSTAT
6363 ** DISK_STATUS -- show amount of free space in queue directories
6366 ** out -- output file pointer.
6367 ** prefix -- string to output in front of each line.
6374 disk_status(out, prefix)
6379 long avail, blksize;
6382 for (i = 0; i < NumFileSys; ++i)
6384 avail = freediskspace(FILE_SYS_NAME(i), &blksize);
6385 if (avail >= 0 && blksize > 0)
6387 free = (long)((double) avail *
6388 ((double) blksize / 1024));
6392 (void) sm_io_fprintf(out, SM_TIME_DEFAULT,
6399 #endif /* _FFR_CONTROL_MSTAT */
6403 ** UPD_QS -- update information about queue when adding/deleting an entry
6407 ** delete -- delete/add entry.
6408 ** avail -- update the space available as well.
6414 ** Modifies available space in filesystem.
6415 ** Changes number of entries in queue directory.
6419 upd_qs(e, delete, avail)
6428 if (ShmId == SM_SHM_NO_ID || e == NULL)
6430 if (e->e_qgrp == NOQGRP || e->e_qdir == NOQDIR)
6432 idx = Queue[e->e_qgrp]->qg_qpaths[e->e_qdir].qp_idx;
6434 /* XXX in theory this needs to be protected with a mutex */
6435 if (QSHM_ENTRIES(idx) >= 0)
6438 --QSHM_ENTRIES(idx);
6440 ++QSHM_ENTRIES(idx);
6443 fidx = Queue[e->e_qgrp]->qg_qpaths[e->e_qdir].qp_fsysidx;
6447 /* update available space also? (might be loseqfile) */
6451 /* convert size to blocks; this causes rounding errors */
6452 s = e->e_msgsize / FILE_SYS_BLKSIZE(fidx);
6456 /* XXX in theory this needs to be protected with a mutex */
6458 FILE_SYS_AVAIL(fidx) += s;
6460 FILE_SYS_AVAIL(fidx) -= s;
6466 static bool write_key_file __P((char *, long));
6467 static long read_key_file __P((char *, long));
6470 ** WRITE_KEY_FILE -- record some key into a file.
6473 ** keypath -- file name.
6474 ** key -- key to write.
6477 ** true iff file could be written.
6484 write_key_file(keypath, key)
6493 if (keypath == NULL || *keypath == '\0')
6495 sff = SFF_NOLINK|SFF_ROOTOK|SFF_REGONLY|SFF_CREAT;
6496 if (TrustedUid != 0 && RealUid == TrustedUid)
6497 sff |= SFF_OPENASROOT;
6498 keyf = safefopen(keypath, O_WRONLY|O_TRUNC, FileMode, sff);
6501 sm_syslog(LOG_ERR, NOQID, "unable to write %s: %s",
6502 keypath, sm_errstring(errno));
6506 ok = sm_io_fprintf(keyf, SM_TIME_DEFAULT, "%ld\n", key) !=
6508 ok = (sm_io_close(keyf, SM_TIME_DEFAULT) != SM_IO_EOF) && ok;
6514 ** READ_KEY_FILE -- read a key from a file.
6517 ** keypath -- file name.
6518 ** key -- default key.
6525 read_key_file(keypath, key)
6533 if (keypath == NULL || *keypath == '\0')
6535 sff = SFF_NOLINK|SFF_ROOTOK|SFF_REGONLY;
6536 if (RealUid == 0 || (TrustedUid != 0 && RealUid == TrustedUid))
6537 sff |= SFF_OPENASROOT;
6538 keyf = safefopen(keypath, O_RDONLY, FileMode, sff);
6541 sm_syslog(LOG_ERR, NOQID, "unable to read %s: %s",
6542 keypath, sm_errstring(errno));
6546 r = sm_io_fscanf(keyf, SM_TIME_DEFAULT, "%ld", &n);
6549 (void) sm_io_close(keyf, SM_TIME_DEFAULT);
6553 #endif /* _FFR_SELECT_SHM */
6556 ** INIT_SHM -- initialize shared memory structure
6558 ** Initialize or attach to shared memory segment.
6559 ** Currently it is not a fatal error if this doesn't work.
6560 ** However, it causes us to have a "fallback" storage location
6561 ** for everything that is supposed to be in the shared memory,
6562 ** which makes the code slightly ugly.
6565 ** qn -- number of queue directories.
6566 ** owner -- owner of shared memory.
6567 ** hash -- identifies data that is stored in shared memory.
6573 static void init_shm __P((int, bool, unsigned int));
6576 init_shm(qn, owner, hash)
6584 #endif /* _FFR_SELECT_SHM */
6586 PtrFileSys = &FileSys[0];
6587 PNumFileSys = &Numfilesys;
6589 /* if this "key" is specified: select one yourself */
6590 # define SEL_SHM_KEY ((key_t) -1)
6591 # define FIRST_SHM_KEY 25
6592 #endif /* _FFR_SELECT_SHM */
6594 /* This allows us to disable shared memory at runtime. */
6601 shms = SM_T_SIZE + qn * sizeof(QUEUE_SHM_T);
6603 keyselect = ShmKey == SEL_SHM_KEY;
6607 ShmKey = FIRST_SHM_KEY;
6610 ShmKey = read_key_file(ShmKeyFile, ShmKey);
6612 if (ShmKey == SEL_SHM_KEY)
6616 #endif /* _FFR_SELECT_SHM */
6619 /* XXX: maybe allow read access for group? */
6620 Pshm = sm_shmstart(ShmKey, shms, SHM_R|SHM_W, &ShmId,
6623 if (Pshm != NULL || !sm_file_exists(save_errno))
6632 /* back where we started? */
6633 if (ShmKey == SEL_SHM_KEY)
6637 #endif /* _FFR_SELECT_SHM */
6641 /* only sleep if we are at the first key */
6642 if (!keyselect || ShmKey == SEL_SHM_KEY)
6643 #endif /* _FFR_SELECT_SHM */
6652 (void) write_key_file(ShmKeyFile, (long) ShmKey);
6653 #endif /* _FFR_SELECT_SHM */
6658 *((pid_t *) SHM_OFF_PID(Pshm)) = CurrentPid;
6659 p = (int *) SHM_OFF_TAG(Pshm);
6664 if (*p != (int) shms)
6666 save_errno = EINVAL;
6670 p = (int *) SHM_OFF_TAG(Pshm);
6671 if (*p != (int) hash)
6673 save_errno = EINVAL;
6679 ** XXX how to check the pid?
6680 ** Read it from the pid-file? That does
6681 ** not need to exist.
6682 ** We could disable shm if we can't confirm
6683 ** that it is the right one.
6687 PtrFileSys = (FILESYS *) OFF_FILE_SYS(Pshm);
6688 PNumFileSys = (int *) OFF_NUM_FILE_SYS(Pshm);
6689 QShm = (QUEUE_SHM_T *) OFF_QUEUE_SHM(Pshm);
6690 PRSATmpCnt = (int *) OFF_RSA_TMP_CNT(Pshm);
6694 /* initialize values in shared memory */
6696 for (i = 0; i < qn; i++)
6697 QShm[i].qs_entries = -1;
6702 if (LogLevel > (owner ? 8 : 11))
6704 sm_syslog(owner ? LOG_ERR : LOG_NOTICE, NOQID,
6705 "can't %s shared memory, key=%ld: %s",
6706 owner ? "initialize" : "attach to",
6707 (long) ShmKey, sm_errstring(save_errno));
6711 #endif /* SM_CONF_SHM */
6715 ** SETUP_QUEUES -- setup all queue groups
6718 ** owner -- owner of shared memory.
6725 ** attaches shared memory.
6726 #endif * SM_CONF_SHM *
6734 unsigned int hashval;
6736 char basedir[MAXPATHLEN];
6740 ** Determine basedir for all queue directories.
6741 ** All queue directories must be (first level) subdirectories
6742 ** of the basedir. The basedir is the QueueDir
6743 ** without wildcards, but with trailing /
6748 len = sm_strlcpy(basedir, QueueDir, sizeof basedir);
6750 /* Provide space for trailing '/' */
6751 if (len >= sizeof basedir - 1)
6753 syserr("QueueDirectory: path too long: %d, max %d",
6754 len, (int) sizeof basedir - 1);
6755 ExitStat = EX_CONFIG;
6759 if (basedir[len - 1] == '*')
6763 cp = SM_LAST_DIR_DELIM(basedir);
6766 syserr("QueueDirectory: can not wildcard relative path \"%s\"",
6769 sm_dprintf("setup_queues: \"%s\": Can not wildcard relative path.\n",
6771 ExitStat = EX_CONFIG;
6775 /* cut off wildcard pattern */
6779 else if (!SM_IS_DIR_DELIM(basedir[len - 1]))
6781 /* append trailing slash since it is a directory */
6783 basedir[++len] = '\0';
6786 /* len counts up to the last directory delimiter */
6787 SM_ASSERT(basedir[len - 1] == '/');
6789 if (chdir(basedir) < 0)
6791 int save_errno = errno;
6793 syserr("can not chdir(%s)", basedir);
6794 if (save_errno == EACCES)
6795 (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
6796 "Program mode requires special privileges, e.g., root or TrustedUser.\n");
6798 sm_dprintf("setup_queues: \"%s\": %s\n",
6799 basedir, sm_errstring(errno));
6800 ExitStat = EX_CONFIG;
6804 hashval = hash_q(basedir, hashval);
6805 #endif /* SM_CONF_SHM */
6807 /* initialize for queue runs */
6810 for (i = 0; i < NumQueue && Queue[i] != NULL; i++)
6811 Queue[i]->qg_nextrun = now;
6814 if (UseMSP && OpMode != MD_TEST)
6816 long sff = SFF_CREAT;
6818 if (stat(".", &st) < 0)
6820 syserr("can not stat(%s)", basedir);
6822 sm_dprintf("setup_queues: \"%s\": %s\n",
6823 basedir, sm_errstring(errno));
6824 ExitStat = EX_CONFIG;
6831 ** Check queue directory permissions.
6832 ** Can we write to a group writable queue directory?
6835 if (bitset(S_IWGRP, QueueFileMode) &&
6836 bitset(S_IWGRP, st.st_mode) &&
6837 safefile(" ", RunAsUid, RunAsGid, RunAsUserName, sff,
6838 QueueFileMode, NULL) != 0)
6840 syserr("can not write to queue directory %s (RunAsGid=%d, required=%d)",
6841 basedir, (int) RunAsGid, (int) st.st_gid);
6843 if (bitset(S_IWOTH|S_IXOTH, st.st_mode))
6845 #if _FFR_MSP_PARANOIA
6846 syserr("dangerous permissions=%o on queue directory %s",
6847 (int) st.st_mode, basedir);
6848 #else /* _FFR_MSP_PARANOIA */
6850 sm_syslog(LOG_ERR, NOQID,
6851 "dangerous permissions=%o on queue directory %s",
6852 (int) st.st_mode, basedir);
6853 #endif /* _FFR_MSP_PARANOIA */
6855 #if _FFR_MSP_PARANOIA
6857 syserr("can not use multiple queues for MSP");
6858 #endif /* _FFR_MSP_PARANOIA */
6861 /* initial number of queue directories */
6863 for (i = 0; i < NumQueue && Queue[i] != NULL; i++)
6864 qn = multiqueue_cache(basedir, len, Queue[i], qn, &hashval);
6867 init_shm(qn, owner, hashval);
6868 i = filesys_setup(owner || ShmId == SM_SHM_NO_ID);
6869 if (i == FSF_NOT_FOUND)
6872 ** We didn't get the right filesystem data
6873 ** This may happen if we don't have the right shared memory.
6874 ** So let's do this without shared memory.
6878 cleanup_shm(false); /* release shared memory */
6879 i = filesys_setup(false);
6881 syserr("filesys_setup failed twice, result=%d", i);
6882 else if (LogLevel > 8)
6883 sm_syslog(LOG_WARNING, NOQID,
6884 "shared memory does not contain expected data, ignored");
6886 #else /* SM_CONF_SHM */
6887 i = filesys_setup(true);
6888 #endif /* SM_CONF_SHM */
6890 ExitStat = EX_CONFIG;
6895 ** CLEANUP_SHM -- do some cleanup work for shared memory etc
6898 ** owner -- owner of shared memory?
6904 ** detaches shared memory.
6911 if (ShmId != SM_SHM_NO_ID)
6913 if (sm_shmstop(Pshm, ShmId, owner) < 0 && LogLevel > 8)
6914 sm_syslog(LOG_INFO, NOQID, "sm_shmstop failed=%s",
6915 sm_errstring(errno));
6917 ShmId = SM_SHM_NO_ID;
6920 #endif /* SM_CONF_SHM */
6923 ** CLEANUP_QUEUES -- do some cleanup work for queues
6939 ** SET_DEF_QUEUEVAL -- set default values for a queue group.
6942 ** qg -- queue group
6943 ** all -- set all values (true for default group)?
6949 ** sets default values for the queue group.
6953 set_def_queueval(qg, all)
6957 if (bitnset(QD_DEFINED, qg->qg_flags))
6960 qg->qg_qdir = QueueDir;
6961 #if _FFR_QUEUE_GROUP_SORTORDER
6962 qg->qg_sortorder = QueueSortOrder;
6963 #endif /* _FFR_QUEUE_GROUP_SORTORDER */
6964 qg->qg_maxqrun = all ? MaxRunnersPerQueue : -1;
6965 qg->qg_nice = NiceQueueRun;
6968 ** MAKEQUEUE -- define a new queue.
6971 ** line -- description of queue. This is in labeled fields.
6973 ** F -- the flags associated with the queue
6974 ** I -- the interval between running the queue
6975 ** J -- the maximum # of jobs in work list
6976 ** [M -- the maximum # of jobs in a queue run]
6977 ** N -- the niceness at which to run
6978 ** P -- the path to the queue
6979 ** S -- the queue sorting order
6980 ** R -- number of parallel queue runners
6981 ** r -- max recipients per envelope
6982 ** The first word is the canonical name of the queue.
6983 ** qdef -- this is a 'Q' definition from .cf
6989 ** enters the queue into the queue table.
6993 makequeue(line, qdef)
6998 register QUEUEGRP *qg;
7003 /* allocate a queue and set up defaults */
7004 qg = (QUEUEGRP *) xalloc(sizeof *qg);
7005 memset((char *) qg, '\0', sizeof *qg);
7007 if (line[0] == '\0')
7009 syserr("name required for queue");
7013 /* collect the queue name */
7015 *p != '\0' && *p != ',' && !(isascii(*p) && isspace(*p));
7020 qg->qg_name = newstr(line);
7022 /* set default values, can be overridden below */
7023 set_def_queueval(qg, false);
7025 /* now scan through and assign info from the fields */
7028 auto char *delimptr;
7030 while (*p != '\0' &&
7031 (*p == ',' || (isascii(*p) && isspace(*p))))
7034 /* p now points to field code */
7036 while (*p != '\0' && *p != '=' && *p != ',')
7040 syserr("queue %s: `=' expected", qg->qg_name);
7043 while (isascii(*p) && isspace(*p))
7046 /* p now points to the field body */
7047 p = munchstring(p, &delimptr, ',');
7049 /* install the field into the queue struct */
7052 case 'P': /* pathname */
7054 syserr("queue %s: empty path name",
7057 qg->qg_qdir = newstr(p);
7060 case 'F': /* flags */
7061 for (; *p != '\0'; p++)
7062 if (!(isascii(*p) && isspace(*p)))
7063 setbitn(*p, qg->qg_flags);
7067 ** Do we need two intervals here:
7068 ** One for persistent queue runners,
7069 ** one for "normal" queue runs?
7072 case 'I': /* interval between running the queue */
7073 qg->qg_queueintvl = convtime(p, 'm');
7076 case 'N': /* run niceness */
7077 qg->qg_nice = atoi(p);
7080 case 'R': /* maximum # of runners for the group */
7083 /* can't have more runners than allowed total */
7084 if (MaxQueueChildren > 0 && i > MaxQueueChildren)
7086 qg->qg_maxqrun = MaxQueueChildren;
7087 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
7088 "Q=%s: R=%d exceeds MaxQueueChildren=%d, set to MaxQueueChildren\n",
7096 case 'J': /* maximum # of jobs in work list */
7097 qg->qg_maxlist = atoi(p);
7100 case 'r': /* max recipients per envelope */
7101 qg->qg_maxrcpt = atoi(p);
7104 #if _FFR_QUEUE_GROUP_SORTORDER
7105 case 'S': /* queue sorting order */
7108 case 'h': /* Host first */
7110 qg->qg_sortorder = QSO_BYHOST;
7113 case 'p': /* Priority order */
7115 qg->qg_sortorder = QSO_BYPRIORITY;
7118 case 't': /* Submission time */
7120 qg->qg_sortorder = QSO_BYTIME;
7123 case 'f': /* File name */
7125 qg->qg_sortorder = QSO_BYFILENAME;
7128 case 'm': /* Modification time */
7130 qg->qg_sortorder = QSO_BYMODTIME;
7133 case 'r': /* Random */
7135 qg->qg_sortorder = QSO_RANDOM;
7139 case 's': /* Shuffled host name */
7141 qg->qg_sortorder = QSO_BYSHUFFLE;
7143 # endif /* _FFR_RHS */
7146 syserr("Invalid queue sort order \"%s\"", p);
7149 #endif /* _FFR_QUEUE_GROUP_SORTORDER */
7152 syserr("Q%s: unknown queue equate %c=",
7153 qg->qg_name, fcode);
7161 if (qg->qg_nice != NiceQueueRun)
7163 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
7164 "Q%s: Warning: N= set on system that doesn't support nice()\n",
7167 #endif /* !HASNICE */
7169 /* do some rationality checking */
7170 if (NumQueue >= MAXQUEUEGROUPS)
7172 syserr("too many queue groups defined (%d max)",
7177 if (qg->qg_qdir == NULL)
7179 if (QueueDir == NULL || *QueueDir == '\0')
7181 syserr("QueueDir must be defined before queue groups");
7184 qg->qg_qdir = newstr(QueueDir);
7187 if (qg->qg_maxqrun > 1 && !bitnset(QD_FORK, qg->qg_flags))
7189 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
7190 "Warning: Q=%s: R=%d: multiple queue runners specified\n\tbut flag '%c' is not set\n",
7191 qg->qg_name, qg->qg_maxqrun, QD_FORK);
7194 /* enter the queue into the symbol table */
7196 sm_syslog(LOG_INFO, NOQID,
7197 "Adding %s to stab, path: %s", qg->qg_name,
7199 s = stab(qg->qg_name, ST_QUEUE, ST_ENTER);
7200 if (s->s_quegrp != NULL)
7202 i = s->s_quegrp->qg_index;
7204 /* XXX what about the pointers inside this struct? */
7205 sm_free(s->s_quegrp); /* XXX */
7209 Queue[i] = s->s_quegrp = qg;
7212 /* set default value for max queue runners */
7213 if (qg->qg_maxqrun < 0)
7215 if (MaxRunnersPerQueue > 0)
7216 qg->qg_maxqrun = MaxRunnersPerQueue;
7221 setbitn(QD_DEFINED, qg->qg_flags);
7225 ** HASHFQN -- calculate a hash value for a fully qualified host name
7228 ** fqn -- an all lower-case host.domain string
7229 ** buckets -- the number of buckets (queue directories)
7232 ** a bucket number (signed integer)
7235 ** Contributed by Exactis.com, Inc.
7239 hashfqn(fqn, buckets)
7244 register int h = 0, hash, cnt;
7250 ** A variation on the gdb hash
7251 ** This is the best as of Feb 19, 1996 --bcx
7255 h = 0x238F13AF * strlen(p);
7256 for (cnt = 0; *p != 0; ++p, cnt++)
7258 h = (h + (*p << (cnt * 5 % 24))) & 0x7FFFFFFF;
7260 h = (1103515243 * h + 12345) & 0x7FFFFFFF;
7264 hash = (h % buckets);
7272 ** QUEUEDELAY -- compute queue delay time
7275 ** e -- the envelope to queue up.
7281 ** may change e_queuedelay
7290 if (e->e_queuealg == QD_EXP)
7292 if (e->e_queuedelay == 0)
7293 e->e_queuedelay = QueueInitDelay;
7296 e->e_queuedelay *= 2;
7297 if (e->e_queuedelay > QueueMaxDelay)
7298 e->e_queuedelay = QueueMaxDelay;
7300 qd = e->e_queuedelay;
7306 #endif /* _FFR_QUEUEDELAY */
7309 ** A structure for sorting Queue according to maxqrun without
7310 ** screwing up Queue itself.
7315 int sg_idx; /* original index */
7316 int sg_maxqrun; /* max queue runners */
7318 typedef struct sortqgrp SORTQGRP_T;
7319 static int cmpidx __P((const void *, const void *));
7326 /* The sort is highest to lowest, so the comparison is reversed */
7327 if (((SORTQGRP_T *)a)->sg_maxqrun < ((SORTQGRP_T *)b)->sg_maxqrun)
7329 else if (((SORTQGRP_T *)a)->sg_maxqrun > ((SORTQGRP_T *)b)->sg_maxqrun)
7336 ** MAKEWORKGROUP -- balance queue groups into work groups per MaxQueueChildren
7338 ** Take the now defined queue groups and assign them to work groups.
7339 ** This is done to balance out the number of concurrently active
7340 ** queue runners such that MaxQueueChildren is not exceeded. This may
7341 ** result in more than one queue group per work group. In such a case
7342 ** the number of running queue groups in that work group will have no
7343 ** more than the work group maximum number of runners (a "fair" portion
7344 ** of MaxQueueRunners). All queue groups within a work group will get a
7345 ** chance at running.
7354 ** Sets up WorkGrp structure.
7360 int i, j, total_runners = 0;
7362 SORTQGRP_T si[MAXQUEUEGROUPS + 1];
7364 if (NumQueue == 1 && strcmp(Queue[0]->qg_name, "mqueue") == 0)
7367 ** There is only the "mqueue" queue group (a default)
7368 ** containing all of the queues. We want to provide to
7369 ** this queue group the maximum allowable queue runners.
7370 ** To match older behavior (8.10/8.11) we'll try for
7371 ** 1 runner per queue capping it at MaxQueueChildren.
7372 ** So if there are N queues, then there will be N runners
7373 ** for the "mqueue" queue group (where N is kept less than
7374 ** MaxQueueChildren).
7378 WorkGrp[0].wg_numqgrp = 1;
7379 WorkGrp[0].wg_qgs = (QUEUEGRP **) xalloc(sizeof(QUEUEGRP *));
7380 WorkGrp[0].wg_qgs[0] = Queue[0];
7381 if (MaxQueueChildren > 0 &&
7382 Queue[0]->qg_numqueues > MaxQueueChildren)
7383 WorkGrp[0].wg_runners = MaxQueueChildren;
7385 WorkGrp[0].wg_runners = Queue[0]->qg_numqueues;
7387 Queue[0]->qg_wgrp = 0;
7389 /* can't have more runners than allowed total */
7390 if (MaxQueueChildren > 0 &&
7391 Queue[0]->qg_maxqrun > MaxQueueChildren)
7392 Queue[0]->qg_maxqrun = MaxQueueChildren;
7393 WorkGrp[0].wg_maxact = Queue[0]->qg_maxqrun;
7394 WorkGrp[0].wg_lowqintvl = Queue[0]->qg_queueintvl;
7398 for (i = 0; i < NumQueue; i++)
7400 si[i].sg_maxqrun = Queue[i]->qg_maxqrun;
7403 qsort(si, NumQueue, sizeof(si[0]), cmpidx);
7406 for (i = 0; i < NumQueue; i++)
7408 total_runners += si[i].sg_maxqrun;
7409 if (MaxQueueChildren <= 0 || total_runners <= MaxQueueChildren)
7415 if (NumWorkGroups < 1)
7416 NumWorkGroups = 1; /* gotta have one at least */
7417 else if (NumWorkGroups > MAXWORKGROUPS)
7418 NumWorkGroups = MAXWORKGROUPS; /* the limit */
7421 ** We now know the number of work groups to pack the queue groups
7422 ** into. The queue groups in 'Queue' are sorted from highest
7423 ** to lowest for the number of runners per queue group.
7424 ** We put the queue groups with the largest number of runners
7425 ** into work groups first. Then the smaller ones are fitted in
7426 ** where it looks best.
7431 for (i = 0; i < NumQueue; i++)
7433 /* a to-and-fro packing scheme, continue from last position */
7434 if (j >= NumWorkGroups)
7437 j = NumWorkGroups - 1;
7445 if (WorkGrp[j].wg_qgs == NULL)
7446 WorkGrp[j].wg_qgs = (QUEUEGRP **)sm_malloc(sizeof(QUEUEGRP *) *
7447 (WorkGrp[j].wg_numqgrp + 1));
7449 WorkGrp[j].wg_qgs = (QUEUEGRP **)sm_realloc(WorkGrp[j].wg_qgs,
7450 sizeof(QUEUEGRP *) *
7451 (WorkGrp[j].wg_numqgrp + 1));
7452 if (WorkGrp[j].wg_qgs == NULL)
7454 syserr("!cannot allocate memory for work queues, need %d bytes",
7455 (int) (sizeof(QUEUEGRP *) *
7456 (WorkGrp[j].wg_numqgrp + 1)));
7459 WorkGrp[j].wg_qgs[WorkGrp[j].wg_numqgrp] = Queue[si[i].sg_idx];
7460 WorkGrp[j].wg_numqgrp++;
7461 WorkGrp[j].wg_runners += Queue[i]->qg_maxqrun;
7462 Queue[si[i].sg_idx]->qg_wgrp = j;
7464 if (WorkGrp[j].wg_maxact == 0)
7466 /* can't have more runners than allowed total */
7467 if (MaxQueueChildren > 0 &&
7468 Queue[i]->qg_maxqrun > MaxQueueChildren)
7469 Queue[i]->qg_maxqrun = MaxQueueChildren;
7470 WorkGrp[j].wg_maxact = Queue[i]->qg_maxqrun;
7474 ** XXX: must wg_lowqintvl be the GCD?
7475 ** qg1: 2m, qg2: 3m, minimum: 2m, when do queue runs for
7479 /* keep track of the lowest interval for a persistent runner */
7480 if (Queue[si[i].sg_idx]->qg_queueintvl > 0 &&
7481 WorkGrp[j].wg_lowqintvl < Queue[si[i].sg_idx]->qg_queueintvl)
7482 WorkGrp[j].wg_lowqintvl = Queue[si[i].sg_idx]->qg_queueintvl;
7487 for (i = 0; i < NumWorkGroups; i++)
7489 sm_dprintf("Workgroup[%d]=", i);
7490 for (j = 0; j < WorkGrp[i].wg_numqgrp; j++)
7493 WorkGrp[i].wg_qgs[j]->qg_name);
7501 ** DUP_DF -- duplicate envelope data file
7503 ** Copy the data file from the 'old' envelope to the 'new' envelope
7504 ** in the most efficient way possible.
7506 ** Create a hard link from the 'old' data file to the 'new' data file.
7507 ** If the old and new queue directories are on different file systems,
7508 ** then the new data file link is created in the old queue directory,
7509 ** and the new queue file will contain a 'd' record pointing to the
7510 ** directory containing the new data file.
7513 ** old -- old envelope.
7514 ** new -- new envelope.
7517 ** Returns true on success, false on failure.
7520 ** On success, the new data file is created.
7521 ** On fatal failure, EF_FATALERRS is set in old->e_flags.
7524 static bool dup_df __P((ENVELOPE *, ENVELOPE *));
7532 char opath[MAXPATHLEN];
7533 char npath[MAXPATHLEN];
7535 if (!bitset(EF_HAS_DF, old->e_flags))
7538 ** this can happen if: SuperSafe != True
7539 ** and a bounce mail is sent that is split.
7542 queueup(old, false, true);
7544 SM_REQUIRE(ISVALIDQGRP(old->e_qgrp) && ISVALIDQDIR(old->e_qdir));
7545 SM_REQUIRE(ISVALIDQGRP(new->e_qgrp) && ISVALIDQDIR(new->e_qdir));
7547 (void) sm_strlcpy(opath, queuename(old, DATAFL_LETTER), sizeof opath);
7548 (void) sm_strlcpy(npath, queuename(new, DATAFL_LETTER), sizeof npath);
7550 if (old->e_dfp != NULL)
7552 r = sm_io_setinfo(old->e_dfp, SM_BF_COMMIT, NULL);
7553 if (r < 0 && errno != EINVAL)
7555 syserr("@can't commit %s", opath);
7556 old->e_flags |= EF_FATALERRS;
7562 ** Attempt to create a hard link, if we think both old and new
7563 ** are on the same file system, otherwise copy the file.
7565 ** Don't waste time attempting a hard link unless old and new
7566 ** are on the same file system.
7569 ofs = Queue[old->e_qgrp]->qg_qpaths[old->e_qdir].qp_fsysidx;
7570 nfs = Queue[new->e_qgrp]->qg_qpaths[new->e_qdir].qp_fsysidx;
7571 if (FILE_SYS_DEV(ofs) == FILE_SYS_DEV(nfs))
7573 if (link(opath, npath) == 0)
7575 new->e_flags |= EF_HAS_DF;
7576 SYNC_DIR(npath, true);
7583 ** Can't link across queue directories, so try to create a hard
7584 ** link in the same queue directory as the old df file.
7585 ** The qf file will refer to the new df file using a 'd' record.
7588 new->e_dfqgrp = old->e_dfqgrp;
7589 new->e_dfqdir = old->e_dfqdir;
7590 (void) sm_strlcpy(npath, queuename(new, DATAFL_LETTER), sizeof npath);
7591 if (link(opath, npath) == 0)
7593 new->e_flags |= EF_HAS_DF;
7594 SYNC_DIR(npath, true);
7600 sm_syslog(LOG_ERR, old->e_id,
7601 "dup_df: can't link %s to %s, error=%s, envelope splitting failed",
7602 opath, npath, sm_errstring(errno));
7607 ** SPLIT_ENV -- Allocate a new envelope based on a given envelope.
7611 ** sendqueue -- sendqueue for new envelope.
7612 ** qgrp -- index of queue group.
7613 ** qdir -- queue directory.
7620 static ENVELOPE *split_env __P((ENVELOPE *, ADDRESS *, int, int));
7623 split_env(e, sendqueue, qgrp, qdir)
7631 ee = (ENVELOPE *) sm_rpool_malloc_x(e->e_rpool, sizeof *ee);
7632 STRUCTCOPY(*e, *ee);
7633 ee->e_message = NULL; /* XXX use original message? */
7636 ee->e_sendqueue = sendqueue;
7637 ee->e_flags &= ~(EF_INQUEUE|EF_CLRQUEUE|EF_FATALERRS
7638 |EF_SENDRECEIPT|EF_RET_PARAM|EF_HAS_DF);
7639 ee->e_flags |= EF_NORECEIPT; /* XXX really? */
7640 ee->e_from.q_state = QS_SENDER;
7642 ee->e_lockfp = NULL;
7643 if (e->e_xfp != NULL)
7644 ee->e_xfp = sm_io_dup(e->e_xfp);
7646 /* failed to dup e->e_xfp, start a new transcript */
7647 if (ee->e_xfp == NULL)
7650 ee->e_qgrp = ee->e_dfqgrp = qgrp;
7651 ee->e_qdir = ee->e_dfqdir = qdir;
7652 ee->e_errormode = EM_MAIL;
7653 ee->e_statmsg = NULL;
7655 if (e->e_quarmsg != NULL)
7656 ee->e_quarmsg = sm_rpool_strdup_x(ee->e_rpool,
7658 #endif /* _FFR_QUARANTINE */
7661 ** XXX Not sure if this copying is necessary.
7662 ** sendall() does this copying, but I (dm) don't know if that is
7663 ** because of the storage management discipline we were using
7664 ** before rpools were introduced, or if it is because these lists
7665 ** can be modified later.
7668 ee->e_header = copyheader(e->e_header, ee->e_rpool);
7669 ee->e_errorqueue = copyqueue(e->e_errorqueue, ee->e_rpool);
7674 /* return values from split functions, check also below! */
7675 #define SM_SPLIT_FAIL (0)
7676 #define SM_SPLIT_NONE (1)
7677 #define SM_SPLIT_NEW(n) (1 + (n))
7680 ** SPLIT_ACROSS_QUEUE_GROUPS
7682 ** This function splits an envelope across multiple queue groups
7683 ** based on the queue group of each recipient.
7689 ** SM_SPLIT_FAIL on failure
7690 ** SM_SPLIT_NONE if no splitting occurred,
7691 ** or 1 + the number of additional envelopes created.
7694 ** On success, e->e_sibling points to a list of zero or more
7695 ** additional envelopes, and the associated data files exist
7696 ** on disk. But the queue files are not created.
7698 ** On failure, e->e_sibling is not changed.
7699 ** The order of recipients in e->e_sendqueue is permuted.
7700 ** Abandoned data files for additional envelopes that failed
7701 ** to be created may exist on disk.
7704 static int q_qgrp_compare __P((const void *, const void *));
7705 static int e_filesys_compare __P((const void *, const void *));
7708 q_qgrp_compare(p1, p2)
7712 ADDRESS **pq1 = (ADDRESS **) p1;
7713 ADDRESS **pq2 = (ADDRESS **) p2;
7715 return (*pq1)->q_qgrp - (*pq2)->q_qgrp;
7719 e_filesys_compare(p1, p2)
7723 ENVELOPE **pe1 = (ENVELOPE **) p1;
7724 ENVELOPE **pe2 = (ENVELOPE **) p2;
7727 fs1 = Queue[(*pe1)->e_qgrp]->qg_qpaths[(*pe1)->e_qdir].qp_fsysidx;
7728 fs2 = Queue[(*pe2)->e_qgrp]->qg_qpaths[(*pe2)->e_qdir].qp_fsysidx;
7729 if (FILE_SYS_DEV(fs1) < FILE_SYS_DEV(fs2))
7731 if (FILE_SYS_DEV(fs1) > FILE_SYS_DEV(fs2))
7737 split_across_queue_groups(e)
7740 int naddrs, nsplits, i;
7743 ADDRESS *q, **addrs;
7745 ENVELOPE *splits[MAXQUEUEGROUPS];
7746 char pvpbuf[PSBUFSIZE];
7748 SM_REQUIRE(ISVALIDQGRP(e->e_qgrp));
7750 /* Count addresses and assign queue groups. */
7753 for (q = e->e_sendqueue; q != NULL; q = q->q_next)
7755 if (QS_IS_DEAD(q->q_state))
7759 /* bad addresses and those already sent stay put */
7760 if (QS_IS_BADADDR(q->q_state) ||
7761 QS_IS_SENT(q->q_state))
7762 q->q_qgrp = e->e_qgrp;
7763 else if (!ISVALIDQGRP(q->q_qgrp))
7765 /* call ruleset which should return a queue group */
7766 i = rscap(RS_QUEUEGROUP, q->q_user, NULL, e, &pvp,
7767 pvpbuf, sizeof(pvpbuf));
7769 pvp != NULL && pvp[0] != NULL &&
7770 (pvp[0][0] & 0377) == CANONNET &&
7771 pvp[1] != NULL && pvp[1][0] != '\0')
7773 i = name2qid(pvp[1]);
7779 sm_syslog(LOG_INFO, NOQID,
7780 "queue group name %s -> %d",
7784 else if (LogLevel > 10)
7785 sm_syslog(LOG_INFO, NOQID,
7786 "can't find queue group name %s, selection ignored",
7789 if (q->q_mailer != NULL &&
7790 ISVALIDQGRP(q->q_mailer->m_qgrp))
7793 q->q_qgrp = q->q_mailer->m_qgrp;
7795 else if (ISVALIDQGRP(e->e_qgrp))
7796 q->q_qgrp = e->e_qgrp;
7802 /* only one address? nothing to split. */
7803 if (naddrs <= 1 && !changed)
7804 return SM_SPLIT_NONE;
7806 /* sort the addresses by queue group */
7807 addrs = sm_rpool_malloc_x(e->e_rpool, naddrs * sizeof(ADDRESS *));
7808 for (i = 0, q = e->e_sendqueue; q != NULL; q = q->q_next)
7810 if (QS_IS_DEAD(q->q_state))
7814 qsort(addrs, naddrs, sizeof(ADDRESS *), q_qgrp_compare);
7816 /* split into multiple envelopes, by queue group */
7819 e->e_sendqueue = NULL;
7820 for (i = 0; i < naddrs; ++i)
7822 if (i == naddrs - 1 || addrs[i]->q_qgrp != addrs[i + 1]->q_qgrp)
7823 addrs[i]->q_next = NULL;
7825 addrs[i]->q_next = addrs[i + 1];
7827 /* same queue group as original envelope? */
7828 if (addrs[i]->q_qgrp == e->e_qgrp)
7830 if (e->e_sendqueue == NULL)
7831 e->e_sendqueue = addrs[i];
7835 /* different queue group than original envelope */
7836 if (es == NULL || addrs[i]->q_qgrp != es->e_qgrp)
7838 ee = split_env(e, addrs[i], addrs[i]->q_qgrp, NOQDIR);
7840 splits[nsplits++] = ee;
7844 /* no splits? return right now. */
7846 return SM_SPLIT_NONE;
7848 /* assign a queue directory to each additional envelope */
7849 for (i = 0; i < nsplits; ++i)
7853 es->e_qdir = pickqdir(Queue[es->e_qgrp], es->e_msgsize, es);
7855 if (!setnewqueue(es))
7859 /* sort the additional envelopes by queue file system */
7860 qsort(splits, nsplits, sizeof(ENVELOPE *), e_filesys_compare);
7862 /* create data files for each additional envelope */
7863 if (!dup_df(e, splits[0]))
7868 for (i = 1; i < nsplits; ++i)
7870 /* copy or link to the previous data file */
7871 if (!dup_df(splits[i - 1], splits[i]))
7875 /* success: prepend the new envelopes to the e->e_sibling list */
7876 for (i = 0; i < nsplits; ++i)
7879 es->e_sibling = e->e_sibling;
7882 return SM_SPLIT_NEW(nsplits);
7884 /* failure: clean up */
7890 for (j = 0; j < i; j++)
7891 (void) unlink(queuename(splits[j], DATAFL_LETTER));
7893 e->e_sendqueue = addrs[0];
7894 for (i = 0; i < naddrs - 1; ++i)
7895 addrs[i]->q_next = addrs[i + 1];
7896 addrs[naddrs - 1]->q_next = NULL;
7897 return SM_SPLIT_FAIL;
7901 ** SPLIT_WITHIN_QUEUE
7903 ** Split an envelope with multiple recipients into several
7904 ** envelopes within the same queue directory, if the number of
7905 ** recipients exceeds the limit for the queue group.
7911 ** SM_SPLIT_FAIL on failure
7912 ** SM_SPLIT_NONE if no splitting occurred,
7913 ** or 1 + the number of additional envelopes created.
7916 #define SPLIT_LOG_LEVEL 8
7918 static int split_within_queue __P((ENVELOPE *));
7921 split_within_queue(e)
7924 int maxrcpt, nrcpt, ndead, nsplit, i;
7927 ADDRESS *q, **addrs;
7928 ENVELOPE *ee, *firstsibling;
7930 if (!ISVALIDQGRP(e->e_qgrp) || bitset(EF_SPLIT, e->e_flags))
7931 return SM_SPLIT_NONE;
7933 /* don't bother if there is no recipient limit */
7934 maxrcpt = Queue[e->e_qgrp]->qg_maxrcpt;
7936 return SM_SPLIT_NONE;
7938 /* count recipients */
7940 for (q = e->e_sendqueue; q != NULL; q = q->q_next)
7942 if (QS_IS_DEAD(q->q_state))
7946 if (nrcpt <= maxrcpt)
7947 return SM_SPLIT_NONE;
7950 ** Preserve the recipient list
7951 ** so that we can restore it in case of error.
7952 ** (But we discard dead addresses.)
7955 addrs = sm_rpool_malloc_x(e->e_rpool, nrcpt * sizeof(ADDRESS *));
7956 for (i = 0, q = e->e_sendqueue; q != NULL; q = q->q_next)
7958 if (QS_IS_DEAD(q->q_state))
7964 ** Partition the recipient list so that bad and sent addresses
7965 ** come first. These will go with the original envelope, and
7966 ** do not count towards the maxrcpt limit.
7967 ** addrs[] does not contain QS_IS_DEAD() addresses.
7971 for (i = 0; i < nrcpt; ++i)
7973 if (QS_IS_BADADDR(addrs[i]->q_state) ||
7974 QS_IS_SENT(addrs[i]->q_state) ||
7975 QS_IS_DEAD(addrs[i]->q_state)) /* for paranoia's sake */
7979 ADDRESS *tmp = addrs[i];
7981 addrs[i] = addrs[ndead];
7988 /* Check if no splitting required. */
7989 if (nrcpt - ndead <= maxrcpt)
7990 return SM_SPLIT_NONE;
7993 for (i = 0; i < nrcpt - 1; ++i)
7994 addrs[i]->q_next = addrs[i + 1];
7995 addrs[nrcpt - 1]->q_next = NULL;
7996 e->e_sendqueue = addrs[0];
7998 /* prepare buffer for logging */
7999 if (LogLevel > SPLIT_LOG_LEVEL)
8002 lsplits = sm_malloc(l);
8003 if (lsplits != NULL)
8009 /* get rid of stupid compiler warnings */
8014 /* split the envelope */
8015 firstsibling = e->e_sibling;
8016 i = maxrcpt + ndead;
8020 addrs[i - 1]->q_next = NULL;
8021 ee = split_env(e, addrs[i], e->e_qgrp, e->e_qdir);
8028 (void) unlink(queuename(ee, DATAFL_LETTER));
8032 /* Error. Restore e's sibling & recipient lists. */
8033 e->e_sibling = firstsibling;
8034 for (i = 0; i < nrcpt - 1; ++i)
8035 addrs[i]->q_next = addrs[i + 1];
8036 if (lsplits != NULL)
8038 return SM_SPLIT_FAIL;
8041 /* prepend the new envelope to e->e_sibling */
8042 ee->e_sibling = e->e_sibling;
8045 if (LogLevel > SPLIT_LOG_LEVEL && lsplits != NULL)
8047 if (j >= l - strlen(ee->e_id) - 3)
8052 p = sm_realloc(lsplits, l);
8055 /* let's try to get this done */
8062 if (lsplits != NULL)
8065 j += sm_strlcat(lsplits + j,
8069 j += sm_strlcat2(lsplits + j,
8076 if (nrcpt - i <= maxrcpt)
8080 if (LogLevel > SPLIT_LOG_LEVEL && lsplits != NULL)
8084 sm_syslog(LOG_NOTICE, e->e_id,
8085 "split: maxrcpts=%d, rcpts=%d, count=%d, id%s=%s",
8086 maxrcpt, nrcpt - ndead, nsplit,
8087 nsplit > 1 ? "s" : "", lsplits);
8091 return SM_SPLIT_NEW(nsplit);
8094 ** SPLIT_BY_RECIPIENT
8096 ** Split an envelope with multiple recipients into multiple
8097 ** envelopes as required by the sendmail configuration.
8103 ** Returns true on success, false on failure.
8106 ** see split_across_queue_groups(), split_within_queue(e)
8110 split_by_recipient(e)
8113 int split, n, i, j, l;
8115 ENVELOPE *ee, *next, *firstsibling;
8117 if (OpMode == SM_VERIFY || !ISVALIDQGRP(e->e_qgrp) ||
8118 bitset(EF_SPLIT, e->e_flags))
8120 n = split_across_queue_groups(e);
8121 if (n == SM_SPLIT_FAIL)
8123 firstsibling = ee = e->e_sibling;
8124 if (n > 1 && LogLevel > SPLIT_LOG_LEVEL)
8127 lsplits = sm_malloc(l);
8128 if (lsplits != NULL)
8134 /* get rid of stupid compiler warnings */
8138 for (i = 1; i < n; ++i)
8140 next = ee->e_sibling;
8141 if (split_within_queue(ee) == SM_SPLIT_FAIL)
8143 e->e_sibling = firstsibling;
8146 ee->e_flags |= EF_SPLIT;
8147 if (LogLevel > SPLIT_LOG_LEVEL && lsplits != NULL)
8149 if (j >= l - strlen(ee->e_id) - 3)
8154 p = sm_realloc(lsplits, l);
8157 /* let's try to get this done */
8164 if (lsplits != NULL)
8167 j += sm_strlcat(lsplits + j,
8170 j += sm_strlcat2(lsplits + j, "; ",
8177 if (LogLevel > SPLIT_LOG_LEVEL && lsplits != NULL && n > 1)
8179 sm_syslog(LOG_NOTICE, e->e_id, "split: count=%d, id%s=%s",
8180 n - 1, n > 2 ? "s" : "", lsplits);
8183 split = split_within_queue(e) != SM_SPLIT_FAIL;
8185 e->e_flags |= EF_SPLIT;
8191 ** QUARANTINE_QUEUE_ITEM -- {un,}quarantine a single envelope
8193 ** Add/remove quarantine reason and requeue appropriately.
8196 ** qgrp -- queue group for the item
8197 ** qdir -- queue directory in the given queue group
8198 ** e -- envelope information for the item
8199 ** reason -- quarantine reason, NULL means unquarantine.
8202 ** true if item changed, false otherwise
8205 ** Changes quarantine tag in queue file and renames it.
8209 quarantine_queue_item(qgrp, qdir, e, reason)
8216 bool failing = false;
8217 bool foundq = false;
8218 bool finished = false;
8224 MODE_T oldumask = 0;
8225 SM_FILE_T *oldqfp, *tempqfp;
8227 char oldqf[MAXPATHLEN];
8228 char tempqf[MAXPATHLEN];
8229 char newqf[MAXPATHLEN];
8232 oldtype = queue_letter(e, ANYQFL_LETTER);
8233 (void) sm_strlcpy(oldqf, queuename(e, ANYQFL_LETTER), sizeof oldqf);
8234 (void) sm_strlcpy(tempqf, queuename(e, NEWQFL_LETTER), sizeof tempqf);
8237 ** Instead of duplicating all the open
8238 ** and lock code here, tell readqf() to
8239 ** do that work and return the open
8240 ** file pointer in e_lockfp. Note that
8241 ** we must release the locks properly when
8245 if (!readqf(e, true))
8247 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8248 "Skipping %s\n", qid_printname(e));
8251 oldqfp = e->e_lockfp;
8253 /* open the new queue file */
8254 flags = O_CREAT|O_WRONLY|O_EXCL;
8255 if (bitset(S_IWGRP, QueueFileMode))
8256 oldumask = umask(002);
8257 fd = open(tempqf, flags, QueueFileMode);
8258 if (bitset(S_IWGRP, QueueFileMode))
8259 (void) umask(oldumask);
8265 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8266 "Skipping %s: Could not open %s: %s\n",
8267 qid_printname(e), tempqf,
8268 sm_errstring(save_errno));
8269 (void) sm_io_close(oldqfp, SM_TIME_DEFAULT);
8272 if (!lockfile(fd, tempqf, NULL, LOCK_EX|LOCK_NB))
8274 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8275 "Skipping %s: Could not lock %s\n",
8276 qid_printname(e), tempqf);
8278 (void) sm_io_close(oldqfp, SM_TIME_DEFAULT);
8282 tempqfp = sm_io_open(SmFtStdiofd, SM_TIME_DEFAULT, (void *) &fd,
8283 SM_IO_WRONLY_B, NULL);
8284 if (tempqfp == NULL)
8286 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8287 "Skipping %s: Could not lock %s\n",
8288 qid_printname(e), tempqf);
8290 (void) sm_io_close(oldqfp, SM_TIME_DEFAULT);
8294 /* Copy the data over, changing the quarantine reason */
8295 while ((bp = fgetfolded(buf, sizeof buf, oldqfp)) != NULL)
8298 sm_dprintf("+++++ %s\n", bp);
8301 case 'q': /* quarantine reason */
8307 (void) sm_io_fprintf(smioout,
8309 "%s: Removed quarantine of \"%s\"\n",
8312 sm_syslog(LOG_INFO, e->e_id, "unquarantine");
8316 else if (strcmp(reason, &bp[1]) == 0)
8320 (void) sm_io_fprintf(smioout,
8322 "%s: Already quarantined with \"%s\"\n",
8325 (void) sm_io_fprintf(tempqfp, SM_TIME_DEFAULT,
8332 (void) sm_io_fprintf(smioout,
8334 "%s: Quarantine changed from \"%s\" to \"%s\"\n",
8338 (void) sm_io_fprintf(tempqfp, SM_TIME_DEFAULT,
8340 sm_syslog(LOG_INFO, e->e_id, "quarantine=%s",
8348 ** If we are quarantining an unquarantined item,
8349 ** need to put in a new 'q' line before it's
8353 if (!foundq && reason != NULL)
8357 (void) sm_io_fprintf(smioout,
8359 "%s: Quarantined with \"%s\"\n",
8362 (void) sm_io_fprintf(tempqfp, SM_TIME_DEFAULT,
8364 sm_syslog(LOG_INFO, e->e_id, "quarantine=%s",
8370 /* Copy the line to the new file */
8371 (void) sm_io_fprintf(tempqfp, SM_TIME_DEFAULT,
8380 /* Copy the line to the new file */
8381 (void) sm_io_fprintf(tempqfp, SM_TIME_DEFAULT,
8387 /* Make sure we read the whole old file */
8388 errno = sm_io_error(tempqfp);
8389 if (errno != 0 && errno != SM_IO_EOF)
8392 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8393 "Skipping %s: Error reading %s: %s\n",
8394 qid_printname(e), oldqf,
8395 sm_errstring(save_errno));
8399 if (!failing && !finished)
8401 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8402 "Skipping %s: Incomplete file: %s\n",
8403 qid_printname(e), oldqf);
8407 /* Check if we actually changed anything or we can just bail now */
8410 /* pretend we failed, even though we technically didn't */
8414 /* Make sure we wrote things out safely */
8416 (sm_io_flush(tempqfp, SM_TIME_DEFAULT) != 0 ||
8417 ((SuperSafe == SAFE_REALLY || SuperSafe == SAFE_INTERACTIVE) &&
8418 fsync(sm_io_getinfo(tempqfp, SM_IO_WHAT_FD, NULL)) < 0) ||
8419 ((errno = sm_io_error(tempqfp)) != 0)))
8422 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8423 "Skipping %s: Error writing %s: %s\n",
8424 qid_printname(e), tempqf,
8425 sm_errstring(save_errno));
8430 /* Figure out the new filename */
8431 newtype = (reason == NULL ? NORMQF_LETTER : QUARQF_LETTER);
8432 if (oldtype == newtype)
8434 /* going to rename tempqf to oldqf */
8435 (void) sm_strlcpy(newqf, oldqf, sizeof newqf);
8439 /* going to rename tempqf to new name based on newtype */
8440 (void) sm_strlcpy(newqf, queuename(e, newtype), sizeof newqf);
8445 /* rename tempqf to newqf */
8447 rename(tempqf, newqf) < 0)
8448 save_errno = (errno == 0) ? EINVAL : errno;
8450 /* Check rename() success */
8451 if (!failing && save_errno != 0)
8453 sm_syslog(LOG_DEBUG, e->e_id,
8454 "quarantine_queue_item: rename(%s, %s): %s",
8455 tempqf, newqf, sm_errstring(save_errno));
8457 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8458 "Error renaming %s to %s: %s\n",
8460 sm_errstring(save_errno));
8461 if (oldtype == newtype)
8464 ** Bail here since we don't know the state of
8465 ** the filesystem and may need to keep tempqf
8466 ** for the user to rescue us.
8471 syserr("!452 Error renaming control file %s", tempqf);
8476 /* remove new file (if rename() half completed) */
8477 if (xunlink(newqf) < 0)
8480 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8481 "Error removing %s: %s\n",
8483 sm_errstring(save_errno));
8486 /* tempqf removed below */
8492 /* If changing file types, need to remove old type */
8493 if (!failing && oldtype != newtype)
8495 if (xunlink(oldqf) < 0)
8498 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8499 "Error removing %s: %s\n",
8500 oldqf, sm_errstring(save_errno));
8504 /* see if anything above failed */
8507 /* Something failed: remove new file, old file still there */
8508 (void) xunlink(tempqf);
8512 ** fsync() after file operations to make sure metadata is
8513 ** written to disk on filesystems in which renames are
8514 ** not guaranteed. It's ok if they fail, mail won't be lost.
8517 if (SuperSafe != SAFE_NO)
8519 /* for soft-updates */
8520 (void) fsync(sm_io_getinfo(tempqfp,
8521 SM_IO_WHAT_FD, NULL));
8525 /* for soft-updates */
8526 (void) fsync(sm_io_getinfo(oldqfp,
8527 SM_IO_WHAT_FD, NULL));
8530 /* for other odd filesystems */
8531 SYNC_DIR(tempqf, false);
8536 if (tempqfp != NULL)
8537 (void) sm_io_close(tempqfp, SM_TIME_DEFAULT);
8539 (void) sm_io_close(oldqfp, SM_TIME_DEFAULT);
8546 ** QUARANTINE_QUEUE -- {un,}quarantine matching items in the queue
8548 ** Read all matching queue items, add/remove quarantine
8549 ** reason, and requeue appropriately.
8552 ** reason -- quarantine reason, "." means unquarantine.
8553 ** qgrplimit -- limit to single queue group unless NOQGRP
8559 ** Lots of changes to the queue.
8563 quarantine_queue(reason, qgrplimit)
8570 /* Convert internal representation of unquarantine */
8571 if (reason != NULL && reason[0] == '.' && reason[1] == '\0')
8577 reason = newstr(denlstring(reason, true, true));
8580 for (qgrp = 0; qgrp < NumQueue && Queue[qgrp] != NULL; qgrp++)
8584 if (qgrplimit != NOQGRP && qgrplimit != qgrp)
8587 for (qdir = 0; qdir < Queue[qgrp]->qg_numqueues; qdir++)
8595 nrequests = gatherq(qgrp, qdir, true, NULL, NULL);
8597 /* first see if there is anything */
8602 (void) sm_io_fprintf(smioout,
8603 SM_TIME_DEFAULT, "%s: no matches\n",
8604 qid_printqueue(qgrp, qdir));
8611 (void) sm_io_fprintf(smioout,
8612 SM_TIME_DEFAULT, "Processing %s:\n",
8613 qid_printqueue(qgrp, qdir));
8616 for (i = 0; i < WorkListCount; i++)
8623 /* setup envelope */
8624 clearenvelope(&e, true, sm_rpool_new_x(NULL));
8625 e.e_id = WorkList[i].w_name + 2;
8631 sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8632 "Would do %s\n", e.e_id);
8635 else if (quarantine_queue_item(qgrp, qdir,
8640 sm_rpool_free(e.e_rpool);
8643 if (WorkList != NULL)
8644 sm_free(WorkList); /* XXX */
8653 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8656 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8659 changed == 1 ? "" : "s");
8662 #endif /* _FFR_QUARANTINE */