1N/A/*
1N/A * Copyright (c) 1998-2009, 2011 Sendmail, Inc. and its suppliers.
1N/A * All rights reserved.
1N/A * Copyright (c) 1983, 1995-1997 Eric P. Allman. All rights reserved.
1N/A * Copyright (c) 1988, 1993
1N/A * The Regents of the University of California. All rights reserved.
1N/A *
1N/A * By using this file, you agree to the terms and conditions set
1N/A * forth in the LICENSE file which can be found at the top level of
1N/A * the sendmail distribution.
1N/A *
1N/A */
1N/A
1N/A#include <sendmail.h>
1N/A#include <sm/sem.h>
1N/A
1N/ASM_RCSID("@(#)$Id: queue.c,v 8.991 2011/03/15 23:14:36 ca Exp $")
1N/A
1N/A#include <dirent.h>
1N/A
1N/A# define RELEASE_QUEUE (void) 0
1N/A# define ST_INODE(st) (st).st_ino
1N/A
1N/A# define sm_file_exists(errno) ((errno) == EEXIST)
1N/A
1N/A# if HASFLOCK && defined(O_EXLOCK)
1N/A# define SM_OPEN_EXLOCK 1
1N/A# define TF_OPEN_FLAGS (O_CREAT|O_WRONLY|O_EXCL|O_EXLOCK)
1N/A# else /* HASFLOCK && defined(O_EXLOCK) */
1N/A# define TF_OPEN_FLAGS (O_CREAT|O_WRONLY|O_EXCL)
1N/A# endif /* HASFLOCK && defined(O_EXLOCK) */
1N/A
1N/A#ifndef SM_OPEN_EXLOCK
1N/A# define SM_OPEN_EXLOCK 0
1N/A#endif /* ! SM_OPEN_EXLOCK */
1N/A
1N/A/*
1N/A** Historical notes:
1N/A** QF_VERSION == 4 was sendmail 8.10/8.11 without _FFR_QUEUEDELAY
1N/A** QF_VERSION == 5 was sendmail 8.10/8.11 with _FFR_QUEUEDELAY
1N/A** QF_VERSION == 6 was sendmail 8.12 without _FFR_QUEUEDELAY
1N/A** QF_VERSION == 7 was sendmail 8.12 with _FFR_QUEUEDELAY
1N/A** QF_VERSION == 8 is sendmail 8.13
1N/A*/
1N/A
1N/A#define QF_VERSION 8 /* version number of this queue format */
1N/A
1N/Astatic char queue_letter __P((ENVELOPE *, int));
1N/Astatic bool quarantine_queue_item __P((int, int, ENVELOPE *, char *));
1N/A
1N/A/* Naming convention: qgrp: index of queue group, qg: QUEUEGROUP */
1N/A
1N/A/*
1N/A** Work queue.
1N/A*/
1N/A
1N/Astruct work
1N/A{
1N/A char *w_name; /* name of control file */
1N/A char *w_host; /* name of recipient host */
1N/A bool w_lock; /* is message locked? */
1N/A bool w_tooyoung; /* is it too young to run? */
1N/A long w_pri; /* priority of message, see below */
1N/A time_t w_ctime; /* creation time */
1N/A time_t w_mtime; /* modification time */
1N/A int w_qgrp; /* queue group located in */
1N/A int w_qdir; /* queue directory located in */
1N/A struct work *w_next; /* next in queue */
1N/A};
1N/A
1N/Atypedef struct work WORK;
1N/A
1N/Astatic WORK *WorkQ; /* queue of things to be done */
1N/Astatic int NumWorkGroups; /* number of work groups */
1N/Astatic time_t Current_LA_time = 0;
1N/A
1N/A/* Get new load average every 30 seconds. */
1N/A#define GET_NEW_LA_TIME 30
1N/A
1N/A#define SM_GET_LA(now) \
1N/A do \
1N/A { \
1N/A now = curtime(); \
1N/A if (Current_LA_time < now - GET_NEW_LA_TIME) \
1N/A { \
1N/A sm_getla(); \
1N/A Current_LA_time = now; \
1N/A } \
1N/A } while (0)
1N/A
1N/A/*
1N/A** DoQueueRun indicates that a queue run is needed.
1N/A** Notice: DoQueueRun is modified in a signal handler!
1N/A*/
1N/A
1N/Astatic bool volatile DoQueueRun; /* non-interrupt time queue run needed */
1N/A
1N/A/*
1N/A** Work group definition structure.
1N/A** Each work group contains one or more queue groups. This is done
1N/A** to manage the number of queue group runners active at the same time
1N/A** to be within the constraints of MaxQueueChildren (if it is set).
1N/A** The number of queue groups that can be run on the next work run
1N/A** is kept track of. The queue groups are run in a round robin.
1N/A*/
1N/A
1N/Astruct workgrp
1N/A{
1N/A int wg_numqgrp; /* number of queue groups in work grp */
1N/A int wg_runners; /* total runners */
1N/A int wg_curqgrp; /* current queue group */
1N/A QUEUEGRP **wg_qgs; /* array of queue groups */
1N/A int wg_maxact; /* max # of active runners */
1N/A time_t wg_lowqintvl; /* lowest queue interval */
1N/A int wg_restart; /* needs restarting? */
1N/A int wg_restartcnt; /* count of times restarted */
1N/A};
1N/A
1N/Atypedef struct workgrp WORKGRP;
1N/A
1N/Astatic WORKGRP volatile WorkGrp[MAXWORKGROUPS + 1]; /* work groups */
1N/A
1N/A#if SM_HEAP_CHECK
1N/Astatic SM_DEBUG_T DebugLeakQ = SM_DEBUG_INITIALIZER("leak_q",
1N/A "@(#)$Debug: leak_q - trace memory leaks during queue processing $");
1N/A#endif /* SM_HEAP_CHECK */
1N/A
1N/A/*
1N/A** We use EmptyString instead of "" to avoid
1N/A** 'zero-length format string' warnings from gcc
1N/A*/
1N/A
1N/Astatic const char EmptyString[] = "";
1N/A
1N/Astatic void grow_wlist __P((int, int));
1N/Astatic int multiqueue_cache __P((char *, int, QUEUEGRP *, int, unsigned int *));
1N/Astatic int gatherq __P((int, int, bool, bool *, bool *, int *));
1N/Astatic int sortq __P((int));
1N/Astatic void printctladdr __P((ADDRESS *, SM_FILE_T *));
1N/Astatic bool readqf __P((ENVELOPE *, bool));
1N/Astatic void restart_work_group __P((int));
1N/Astatic void runner_work __P((ENVELOPE *, int, bool, int, int));
1N/Astatic void schedule_queue_runs __P((bool, int, bool));
1N/Astatic char *strrev __P((char *));
1N/Astatic ADDRESS *setctluser __P((char *, int, ENVELOPE *));
1N/A#if _FFR_RHS
1N/Astatic int sm_strshufflecmp __P((char *, char *));
1N/Astatic void init_shuffle_alphabet __P(());
1N/A#endif /* _FFR_RHS */
1N/A
1N/A/*
1N/A** Note: workcmpf?() don't use a prototype because it will cause a conflict
1N/A** with the qsort() call (which expects something like
1N/A** int (*compar)(const void *, const void *), not (WORK *, WORK *))
1N/A*/
1N/A
1N/Astatic int workcmpf0();
1N/Astatic int workcmpf1();
1N/Astatic int workcmpf2();
1N/Astatic int workcmpf3();
1N/Astatic int workcmpf4();
1N/Astatic int randi = 3; /* index for workcmpf5() */
1N/Astatic int workcmpf5();
1N/Astatic int workcmpf6();
1N/A#if _FFR_RHS
1N/Astatic int workcmpf7();
1N/A#endif /* _FFR_RHS */
1N/A
1N/A#if RANDOMSHIFT
1N/A# define get_rand_mod(m) ((get_random() >> RANDOMSHIFT) % (m))
1N/A#else /* RANDOMSHIFT */
1N/A# define get_rand_mod(m) (get_random() % (m))
1N/A#endif /* RANDOMSHIFT */
1N/A
1N/A/*
1N/A** File system definition.
1N/A** Used to keep track of how much free space is available
1N/A** on a file system in which one or more queue directories reside.
1N/A*/
1N/A
1N/Atypedef struct filesys_shared FILESYS;
1N/A
1N/Astruct filesys_shared
1N/A{
1N/A dev_t fs_dev; /* unique device id */
1N/A long fs_avail; /* number of free blocks available */
1N/A long fs_blksize; /* block size, in bytes */
1N/A};
1N/A
1N/A/* probably kept in shared memory */
1N/Astatic FILESYS FileSys[MAXFILESYS]; /* queue file systems */
1N/Astatic const char *FSPath[MAXFILESYS]; /* pathnames for file systems */
1N/A
1N/A#if SM_CONF_SHM
1N/A
1N/A/*
1N/A** Shared memory data
1N/A**
1N/A** Current layout:
1N/A** size -- size of shared memory segment
1N/A** pid -- pid of owner, should be a unique id to avoid misinterpretations
1N/A** by other processes.
1N/A** tag -- should be a unique id to avoid misinterpretations by others.
1N/A** idea: hash over configuration data that will be stored here.
1N/A** NumFileSys -- number of file systems.
1N/A** FileSys -- (array of) structure for used file systems.
1N/A** RSATmpCnt -- counter for number of uses of ephemeral RSA key.
1N/A** QShm -- (array of) structure for information about queue directories.
1N/A*/
1N/A
1N/A/*
1N/A** Queue data in shared memory
1N/A*/
1N/A
1N/Atypedef struct queue_shared QUEUE_SHM_T;
1N/A
1N/Astruct queue_shared
1N/A{
1N/A int qs_entries; /* number of entries */
1N/A /* XXX more to follow? */
1N/A};
1N/A
1N/Astatic void *Pshm; /* pointer to shared memory */
1N/Astatic FILESYS *PtrFileSys; /* pointer to queue file system array */
1N/Aint ShmId = SM_SHM_NO_ID; /* shared memory id */
1N/Astatic QUEUE_SHM_T *QShm; /* pointer to shared queue data */
1N/Astatic size_t shms;
1N/A
1N/A# define SHM_OFF_PID(p) (((char *) (p)) + sizeof(int))
1N/A# define SHM_OFF_TAG(p) (((char *) (p)) + sizeof(pid_t) + sizeof(int))
1N/A# define SHM_OFF_HEAD (sizeof(pid_t) + sizeof(int) * 2)
1N/A
1N/A/* how to access FileSys */
1N/A# define FILE_SYS(i) (PtrFileSys[i])
1N/A
1N/A/* first entry is a tag, for now just the size */
1N/A# define OFF_FILE_SYS(p) (((char *) (p)) + SHM_OFF_HEAD)
1N/A
1N/A/* offset for PNumFileSys */
1N/A# define OFF_NUM_FILE_SYS(p) (((char *) (p)) + SHM_OFF_HEAD + sizeof(FileSys))
1N/A
1N/A/* offset for PRSATmpCnt */
1N/A# define OFF_RSA_TMP_CNT(p) (((char *) (p)) + SHM_OFF_HEAD + sizeof(FileSys) + sizeof(int))
1N/Aint *PRSATmpCnt;
1N/A
1N/A/* offset for queue_shm */
1N/A# define OFF_QUEUE_SHM(p) (((char *) (p)) + SHM_OFF_HEAD + sizeof(FileSys) + sizeof(int) * 2)
1N/A
1N/A# define QSHM_ENTRIES(i) QShm[i].qs_entries
1N/A
1N/A/* basic size of shared memory segment */
1N/A# define SM_T_SIZE (SHM_OFF_HEAD + sizeof(FileSys) + sizeof(int) * 2)
1N/A
1N/Astatic unsigned int hash_q __P((char *, unsigned int));
1N/A
1N/A/*
1N/A** HASH_Q -- simple hash function
1N/A**
1N/A** Parameters:
1N/A** p -- string to hash.
1N/A** h -- hash start value (from previous run).
1N/A**
1N/A** Returns:
1N/A** hash value.
1N/A*/
1N/A
1N/Astatic unsigned int
1N/Ahash_q(p, h)
1N/A char *p;
1N/A unsigned int h;
1N/A{
1N/A int c, d;
1N/A
1N/A while (*p != '\0')
1N/A {
1N/A d = *p++;
1N/A c = d;
1N/A c ^= c<<6;
1N/A h += (c<<11) ^ (c>>1);
1N/A h ^= (d<<14) + (d<<7) + (d<<4) + d;
1N/A }
1N/A return h;
1N/A}
1N/A
1N/A
1N/A#else /* SM_CONF_SHM */
1N/A# define FILE_SYS(i) FileSys[i]
1N/A#endif /* SM_CONF_SHM */
1N/A
1N/A/* access to the various components of file system data */
1N/A#define FILE_SYS_NAME(i) FSPath[i]
1N/A#define FILE_SYS_AVAIL(i) FILE_SYS(i).fs_avail
1N/A#define FILE_SYS_BLKSIZE(i) FILE_SYS(i).fs_blksize
1N/A#define FILE_SYS_DEV(i) FILE_SYS(i).fs_dev
1N/A
1N/A
1N/A/*
1N/A** Current qf file field assignments:
1N/A**
1N/A** A AUTH= parameter
1N/A** B body type
1N/A** C controlling user
1N/A** D data file name
1N/A** d data file directory name (added in 8.12)
1N/A** E error recipient
1N/A** F flag bits
1N/A** G free (was: queue delay algorithm if _FFR_QUEUEDELAY)
1N/A** H header
1N/A** I data file's inode number
1N/A** K time of last delivery attempt
1N/A** L Solaris Content-Length: header (obsolete)
1N/A** M message
1N/A** N number of delivery attempts
1N/A** P message priority
1N/A** q quarantine reason
1N/A** Q original recipient (ORCPT=)
1N/A** r final recipient (Final-Recipient: DSN field)
1N/A** R recipient
1N/A** S sender
1N/A** T init time
1N/A** V queue file version
1N/A** X free (was: character set if _FFR_SAVE_CHARSET)
1N/A** Y free (was: current delay if _FFR_QUEUEDELAY)
1N/A** Z original envelope id from ESMTP
1N/A** ! deliver by (added in 8.12)
1N/A** $ define macro
1N/A** . terminate file
1N/A*/
1N/A
1N/A/*
1N/A** QUEUEUP -- queue a message up for future transmission.
1N/A**
1N/A** Parameters:
1N/A** e -- the envelope to queue up.
1N/A** announce -- if true, tell when you are queueing up.
1N/A** msync -- if true, then fsync() if SuperSafe interactive mode.
1N/A**
1N/A** Returns:
1N/A** none.
1N/A**
1N/A** Side Effects:
1N/A** The current request is saved in a control file.
1N/A** The queue file is left locked.
1N/A*/
1N/A
1N/Avoid
1N/Aqueueup(e, announce, msync)
1N/A register ENVELOPE *e;
1N/A bool announce;
1N/A bool msync;
1N/A{
1N/A register SM_FILE_T *tfp;
1N/A register HDR *h;
1N/A register ADDRESS *q;
1N/A int tfd = -1;
1N/A int i;
1N/A bool newid;
1N/A register char *p;
1N/A MAILER nullmailer;
1N/A MCI mcibuf;
1N/A char qf[MAXPATHLEN];
1N/A char tf[MAXPATHLEN];
1N/A char df[MAXPATHLEN];
1N/A char buf[MAXLINE];
1N/A
1N/A /*
1N/A ** Create control file.
1N/A */
1N/A
1N/A#define OPEN_TF do \
1N/A { \
1N/A MODE_T oldumask = 0; \
1N/A \
1N/A if (bitset(S_IWGRP, QueueFileMode)) \
1N/A oldumask = umask(002); \
1N/A tfd = open(tf, TF_OPEN_FLAGS, QueueFileMode); \
1N/A if (bitset(S_IWGRP, QueueFileMode)) \
1N/A (void) umask(oldumask); \
1N/A } while (0)
1N/A
1N/A
1N/A newid = (e->e_id == NULL) || !bitset(EF_INQUEUE, e->e_flags);
1N/A (void) sm_strlcpy(tf, queuename(e, NEWQFL_LETTER), sizeof(tf));
1N/A tfp = e->e_lockfp;
1N/A if (tfp == NULL && newid)
1N/A {
1N/A /*
1N/A ** open qf file directly: this will give an error if the file
1N/A ** already exists and hence prevent problems if a queue-id
1N/A ** is reused (e.g., because the clock is set back).
1N/A */
1N/A
1N/A (void) sm_strlcpy(tf, queuename(e, ANYQFL_LETTER), sizeof(tf));
1N/A OPEN_TF;
1N/A if (tfd < 0 ||
1N/A#if !SM_OPEN_EXLOCK
1N/A !lockfile(tfd, tf, NULL, LOCK_EX|LOCK_NB) ||
1N/A#endif /* !SM_OPEN_EXLOCK */
1N/A (tfp = sm_io_open(SmFtStdiofd, SM_TIME_DEFAULT,
1N/A (void *) &tfd, SM_IO_WRONLY,
1N/A NULL)) == NULL)
1N/A {
1N/A int save_errno = errno;
1N/A
1N/A printopenfds(true);
1N/A errno = save_errno;
1N/A syserr("!queueup: cannot create queue file %s, euid=%d, fd=%d, fp=%p",
1N/A tf, (int) geteuid(), tfd, tfp);
1N/A /* NOTREACHED */
1N/A }
1N/A e->e_lockfp = tfp;
1N/A upd_qs(e, 1, 0, "queueup");
1N/A }
1N/A
1N/A /* if newid, write the queue file directly (instead of temp file) */
1N/A if (!newid)
1N/A {
1N/A /* get a locked tf file */
1N/A for (i = 0; i < 128; i++)
1N/A {
1N/A if (tfd < 0)
1N/A {
1N/A OPEN_TF;
1N/A if (tfd < 0)
1N/A {
1N/A if (errno != EEXIST)
1N/A break;
1N/A if (LogLevel > 0 && (i % 32) == 0)
1N/A sm_syslog(LOG_ALERT, e->e_id,
1N/A "queueup: cannot create %s, euid=%d: %s",
1N/A tf, (int) geteuid(),
1N/A sm_errstring(errno));
1N/A }
1N/A#if SM_OPEN_EXLOCK
1N/A else
1N/A break;
1N/A#endif /* SM_OPEN_EXLOCK */
1N/A }
1N/A if (tfd >= 0)
1N/A {
1N/A#if SM_OPEN_EXLOCK
1N/A /* file is locked by open() */
1N/A break;
1N/A#else /* SM_OPEN_EXLOCK */
1N/A if (lockfile(tfd, tf, NULL, LOCK_EX|LOCK_NB))
1N/A break;
1N/A else
1N/A#endif /* SM_OPEN_EXLOCK */
1N/A if (LogLevel > 0 && (i % 32) == 0)
1N/A sm_syslog(LOG_ALERT, e->e_id,
1N/A "queueup: cannot lock %s: %s",
1N/A tf, sm_errstring(errno));
1N/A if ((i % 32) == 31)
1N/A {
1N/A (void) close(tfd);
1N/A tfd = -1;
1N/A }
1N/A }
1N/A
1N/A if ((i % 32) == 31)
1N/A {
1N/A /* save the old temp file away */
1N/A (void) rename(tf, queuename(e, TEMPQF_LETTER));
1N/A }
1N/A else
1N/A (void) sleep(i % 32);
1N/A }
1N/A if (tfd < 0 || (tfp = sm_io_open(SmFtStdiofd, SM_TIME_DEFAULT,
1N/A (void *) &tfd, SM_IO_WRONLY_B,
1N/A NULL)) == NULL)
1N/A {
1N/A int save_errno = errno;
1N/A
1N/A printopenfds(true);
1N/A errno = save_errno;
1N/A syserr("!queueup: cannot create queue temp file %s, uid=%d",
1N/A tf, (int) geteuid());
1N/A }
1N/A }
1N/A
1N/A if (tTd(40, 1))
1N/A sm_dprintf("\n>>>>> queueing %s/%s%s >>>>>\n",
1N/A qid_printqueue(e->e_qgrp, e->e_qdir),
1N/A queuename(e, ANYQFL_LETTER),
1N/A newid ? " (new id)" : "");
1N/A if (tTd(40, 3))
1N/A {
1N/A sm_dprintf(" e_flags=");
1N/A printenvflags(e);
1N/A }
1N/A if (tTd(40, 32))
1N/A {
1N/A sm_dprintf(" sendq=");
1N/A printaddr(sm_debug_file(), e->e_sendqueue, true);
1N/A }
1N/A if (tTd(40, 9))
1N/A {
1N/A sm_dprintf(" tfp=");
1N/A dumpfd(sm_io_getinfo(tfp, SM_IO_WHAT_FD, NULL), true, false);
1N/A sm_dprintf(" lockfp=");
1N/A if (e->e_lockfp == NULL)
1N/A sm_dprintf("NULL\n");
1N/A else
1N/A dumpfd(sm_io_getinfo(e->e_lockfp, SM_IO_WHAT_FD, NULL),
1N/A true, false);
1N/A }
1N/A
1N/A /*
1N/A ** If there is no data file yet, create one.
1N/A */
1N/A
1N/A (void) sm_strlcpy(df, queuename(e, DATAFL_LETTER), sizeof(df));
1N/A if (bitset(EF_HAS_DF, e->e_flags))
1N/A {
1N/A if (e->e_dfp != NULL &&
1N/A SuperSafe != SAFE_REALLY &&
1N/A SuperSafe != SAFE_REALLY_POSTMILTER &&
1N/A sm_io_setinfo(e->e_dfp, SM_BF_COMMIT, NULL) < 0 &&
1N/A errno != EINVAL)
1N/A {
1N/A syserr("!queueup: cannot commit data file %s, uid=%d",
1N/A queuename(e, DATAFL_LETTER), (int) geteuid());
1N/A }
1N/A if (e->e_dfp != NULL &&
1N/A SuperSafe == SAFE_INTERACTIVE && msync)
1N/A {
1N/A if (tTd(40,32))
1N/A sm_syslog(LOG_INFO, e->e_id,
1N/A "queueup: fsync(e->e_dfp)");
1N/A
1N/A if (fsync(sm_io_getinfo(e->e_dfp, SM_IO_WHAT_FD,
1N/A NULL)) < 0)
1N/A {
1N/A if (newid)
1N/A syserr("!552 Error writing data file %s",
1N/A df);
1N/A else
1N/A syserr("!452 Error writing data file %s",
1N/A df);
1N/A }
1N/A }
1N/A }
1N/A else
1N/A {
1N/A int dfd;
1N/A MODE_T oldumask = 0;
1N/A register SM_FILE_T *dfp = NULL;
1N/A struct stat stbuf;
1N/A
1N/A if (e->e_dfp != NULL &&
1N/A sm_io_getinfo(e->e_dfp, SM_IO_WHAT_ISTYPE, BF_FILE_TYPE))
1N/A syserr("committing over bf file");
1N/A
1N/A if (bitset(S_IWGRP, QueueFileMode))
1N/A oldumask = umask(002);
1N/A dfd = open(df, O_WRONLY|O_CREAT|O_TRUNC|QF_O_EXTRA,
1N/A QueueFileMode);
1N/A if (bitset(S_IWGRP, QueueFileMode))
1N/A (void) umask(oldumask);
1N/A if (dfd < 0 || (dfp = sm_io_open(SmFtStdiofd, SM_TIME_DEFAULT,
1N/A (void *) &dfd, SM_IO_WRONLY_B,
1N/A NULL)) == NULL)
1N/A syserr("!queueup: cannot create data temp file %s, uid=%d",
1N/A df, (int) geteuid());
1N/A if (fstat(dfd, &stbuf) < 0)
1N/A e->e_dfino = -1;
1N/A else
1N/A {
1N/A e->e_dfdev = stbuf.st_dev;
1N/A e->e_dfino = ST_INODE(stbuf);
1N/A }
1N/A e->e_flags |= EF_HAS_DF;
1N/A memset(&mcibuf, '\0', sizeof(mcibuf));
1N/A mcibuf.mci_out = dfp;
1N/A mcibuf.mci_mailer = FileMailer;
1N/A (*e->e_putbody)(&mcibuf, e, NULL);
1N/A
1N/A if (SuperSafe == SAFE_REALLY ||
1N/A SuperSafe == SAFE_REALLY_POSTMILTER ||
1N/A (SuperSafe == SAFE_INTERACTIVE && msync))
1N/A {
1N/A if (tTd(40,32))
1N/A sm_syslog(LOG_INFO, e->e_id,
1N/A "queueup: fsync(dfp)");
1N/A
1N/A if (fsync(sm_io_getinfo(dfp, SM_IO_WHAT_FD, NULL)) < 0)
1N/A {
1N/A if (newid)
1N/A syserr("!552 Error writing data file %s",
1N/A df);
1N/A else
1N/A syserr("!452 Error writing data file %s",
1N/A df);
1N/A }
1N/A }
1N/A
1N/A if (sm_io_close(dfp, SM_TIME_DEFAULT) < 0)
1N/A syserr("!queueup: cannot save data temp file %s, uid=%d",
1N/A df, (int) geteuid());
1N/A e->e_putbody = putbody;
1N/A }
1N/A
1N/A /*
1N/A ** Output future work requests.
1N/A ** Priority and creation time should be first, since
1N/A ** they are required by gatherq.
1N/A */
1N/A
1N/A /* output queue version number (must be first!) */
1N/A (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "V%d\n", QF_VERSION);
1N/A
1N/A /* output creation time */
1N/A (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "T%ld\n", (long) e->e_ctime);
1N/A
1N/A /* output last delivery time */
1N/A (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "K%ld\n", (long) e->e_dtime);
1N/A
1N/A /* output number of delivery attempts */
1N/A (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "N%d\n", e->e_ntries);
1N/A
1N/A /* output message priority */
1N/A (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "P%ld\n", e->e_msgpriority);
1N/A
1N/A /*
1N/A ** If data file is in a different directory than the queue file,
1N/A ** output a "d" record naming the directory of the data file.
1N/A */
1N/A
1N/A if (e->e_dfqgrp != e->e_qgrp)
1N/A {
1N/A (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "d%s\n",
1N/A Queue[e->e_dfqgrp]->qg_qpaths[e->e_dfqdir].qp_name);
1N/A }
1N/A
1N/A /* output inode number of data file */
1N/A if (e->e_dfino != -1)
1N/A {
1N/A (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "I%ld/%ld/%llu\n",
1N/A (long) major(e->e_dfdev),
1N/A (long) minor(e->e_dfdev),
1N/A (ULONGLONG_T) e->e_dfino);
1N/A }
1N/A
1N/A /* output body type */
1N/A if (e->e_bodytype != NULL)
1N/A (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "B%s\n",
1N/A denlstring(e->e_bodytype, true, false));
1N/A
1N/A /* quarantine reason */
1N/A if (e->e_quarmsg != NULL)
1N/A (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "q%s\n",
1N/A denlstring(e->e_quarmsg, true, false));
1N/A
1N/A /* message from envelope, if it exists */
1N/A if (e->e_message != NULL)
1N/A (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "M%s\n",
1N/A denlstring(e->e_message, true, false));
1N/A
1N/A /* send various flag bits through */
1N/A p = buf;
1N/A if (bitset(EF_WARNING, e->e_flags))
1N/A *p++ = 'w';
1N/A if (bitset(EF_RESPONSE, e->e_flags))
1N/A *p++ = 'r';
1N/A if (bitset(EF_HAS8BIT, e->e_flags))
1N/A *p++ = '8';
1N/A if (bitset(EF_DELETE_BCC, e->e_flags))
1N/A *p++ = 'b';
1N/A if (bitset(EF_RET_PARAM, e->e_flags))
1N/A *p++ = 'd';
1N/A if (bitset(EF_NO_BODY_RETN, e->e_flags))
1N/A *p++ = 'n';
1N/A if (bitset(EF_SPLIT, e->e_flags))
1N/A *p++ = 's';
1N/A *p++ = '\0';
1N/A if (buf[0] != '\0')
1N/A (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "F%s\n", buf);
1N/A
1N/A /* save $={persistentMacros} macro values */
1N/A queueup_macros(macid("{persistentMacros}"), tfp, e);
1N/A
1N/A /* output name of sender */
1N/A if (bitnset(M_UDBENVELOPE, e->e_from.q_mailer->m_flags))
1N/A p = e->e_sender;
1N/A else
1N/A p = e->e_from.q_paddr;
1N/A (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "S%s\n",
1N/A denlstring(p, true, false));
1N/A
1N/A /* output ESMTP-supplied "original" information */
1N/A if (e->e_envid != NULL)
1N/A (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "Z%s\n",
1N/A denlstring(e->e_envid, true, false));
1N/A
1N/A /* output AUTH= parameter */
1N/A if (e->e_auth_param != NULL)
1N/A (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "A%s\n",
1N/A denlstring(e->e_auth_param, true, false));
1N/A if (e->e_dlvr_flag != 0)
1N/A (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "!%c %ld\n",
1N/A (char) e->e_dlvr_flag, e->e_deliver_by);
1N/A
1N/A /* output list of recipient addresses */
1N/A printctladdr(NULL, NULL);
1N/A for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1N/A {
1N/A if (!QS_IS_UNDELIVERED(q->q_state))
1N/A continue;
1N/A
1N/A /* message for this recipient, if it exists */
1N/A if (q->q_message != NULL)
1N/A (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "M%s\n",
1N/A denlstring(q->q_message, true,
1N/A false));
1N/A
1N/A printctladdr(q, tfp);
1N/A if (q->q_orcpt != NULL)
1N/A (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "Q%s\n",
1N/A denlstring(q->q_orcpt, true,
1N/A false));
1N/A if (q->q_finalrcpt != NULL)
1N/A (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "r%s\n",
1N/A denlstring(q->q_finalrcpt, true,
1N/A false));
1N/A (void) sm_io_putc(tfp, SM_TIME_DEFAULT, 'R');
1N/A if (bitset(QPRIMARY, q->q_flags))
1N/A (void) sm_io_putc(tfp, SM_TIME_DEFAULT, 'P');
1N/A if (bitset(QHASNOTIFY, q->q_flags))
1N/A (void) sm_io_putc(tfp, SM_TIME_DEFAULT, 'N');
1N/A if (bitset(QPINGONSUCCESS, q->q_flags))
1N/A (void) sm_io_putc(tfp, SM_TIME_DEFAULT, 'S');
1N/A if (bitset(QPINGONFAILURE, q->q_flags))
1N/A (void) sm_io_putc(tfp, SM_TIME_DEFAULT, 'F');
1N/A if (bitset(QPINGONDELAY, q->q_flags))
1N/A (void) sm_io_putc(tfp, SM_TIME_DEFAULT, 'D');
1N/A if (q->q_alias != NULL &&
1N/A bitset(QALIAS, q->q_alias->q_flags))
1N/A (void) sm_io_putc(tfp, SM_TIME_DEFAULT, 'A');
1N/A (void) sm_io_putc(tfp, SM_TIME_DEFAULT, ':');
1N/A (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "%s\n",
1N/A denlstring(q->q_paddr, true, false));
1N/A if (announce)
1N/A {
1N/A char *tag = "queued";
1N/A
1N/A if (e->e_quarmsg != NULL)
1N/A tag = "quarantined";
1N/A
1N/A e->e_to = q->q_paddr;
1N/A message(tag);
1N/A if (LogLevel > 8)
1N/A logdelivery(q->q_mailer, NULL, q->q_status,
1N/A tag, NULL, (time_t) 0, e);
1N/A e->e_to = NULL;
1N/A }
1N/A if (tTd(40, 1))
1N/A {
1N/A sm_dprintf("queueing ");
1N/A printaddr(sm_debug_file(), q, false);
1N/A }
1N/A }
1N/A
1N/A /*
1N/A ** Output headers for this message.
1N/A ** Expand macros completely here. Queue run will deal with
1N/A ** everything as absolute headers.
1N/A ** All headers that must be relative to the recipient
1N/A ** can be cracked later.
1N/A ** We set up a "null mailer" -- i.e., a mailer that will have
1N/A ** no effect on the addresses as they are output.
1N/A */
1N/A
1N/A memset((char *) &nullmailer, '\0', sizeof(nullmailer));
1N/A nullmailer.m_re_rwset = nullmailer.m_rh_rwset =
1N/A nullmailer.m_se_rwset = nullmailer.m_sh_rwset = -1;
1N/A nullmailer.m_eol = "\n";
1N/A memset(&mcibuf, '\0', sizeof(mcibuf));
1N/A mcibuf.mci_mailer = &nullmailer;
1N/A mcibuf.mci_out = tfp;
1N/A
1N/A macdefine(&e->e_macro, A_PERM, 'g', "\201f");
1N/A for (h = e->e_header; h != NULL; h = h->h_link)
1N/A {
1N/A if (h->h_value == NULL)
1N/A continue;
1N/A
1N/A /* don't output resent headers on non-resent messages */
1N/A if (bitset(H_RESENT, h->h_flags) &&
1N/A !bitset(EF_RESENT, e->e_flags))
1N/A continue;
1N/A
1N/A /* expand macros; if null, don't output header at all */
1N/A if (bitset(H_DEFAULT, h->h_flags))
1N/A {
1N/A (void) expand(h->h_value, buf, sizeof(buf), e);
1N/A if (buf[0] == '\0')
1N/A continue;
1N/A if (buf[0] == ' ' && buf[1] == '\0')
1N/A continue;
1N/A }
1N/A
1N/A /* output this header */
1N/A (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "H?");
1N/A
1N/A /* output conditional macro if present */
1N/A if (h->h_macro != '\0')
1N/A {
1N/A if (bitset(0200, h->h_macro))
1N/A (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT,
1N/A "${%s}",
1N/A macname(bitidx(h->h_macro)));
1N/A else
1N/A (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT,
1N/A "$%c", h->h_macro);
1N/A }
1N/A else if (!bitzerop(h->h_mflags) &&
1N/A bitset(H_CHECK|H_ACHECK, h->h_flags))
1N/A {
1N/A int j;
1N/A
1N/A /* if conditional, output the set of conditions */
1N/A for (j = '\0'; j <= '\177'; j++)
1N/A if (bitnset(j, h->h_mflags))
1N/A (void) sm_io_putc(tfp, SM_TIME_DEFAULT,
1N/A j);
1N/A }
1N/A (void) sm_io_putc(tfp, SM_TIME_DEFAULT, '?');
1N/A
1N/A /* output the header: expand macros, convert addresses */
1N/A if (bitset(H_DEFAULT, h->h_flags) &&
1N/A !bitset(H_BINDLATE, h->h_flags))
1N/A {
1N/A (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "%s:%s\n",
1N/A h->h_field,
1N/A denlstring(buf, false, true));
1N/A }
1N/A else if (bitset(H_FROM|H_RCPT, h->h_flags) &&
1N/A !bitset(H_BINDLATE, h->h_flags))
1N/A {
1N/A bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
1N/A SM_FILE_T *savetrace = TrafficLogFile;
1N/A
1N/A TrafficLogFile = NULL;
1N/A
1N/A if (bitset(H_FROM, h->h_flags))
1N/A oldstyle = false;
1N/A commaize(h, h->h_value, oldstyle, &mcibuf, e,
1N/A PXLF_HEADER);
1N/A
1N/A TrafficLogFile = savetrace;
1N/A }
1N/A else
1N/A {
1N/A (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "%s:%s\n",
1N/A h->h_field,
1N/A denlstring(h->h_value, false,
1N/A true));
1N/A }
1N/A }
1N/A
1N/A /*
1N/A ** Clean up.
1N/A **
1N/A ** Write a terminator record -- this is to prevent
1N/A ** scurrilous crackers from appending any data.
1N/A */
1N/A
1N/A (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, ".\n");
1N/A
1N/A if (sm_io_flush(tfp, SM_TIME_DEFAULT) != 0 ||
1N/A ((SuperSafe == SAFE_REALLY ||
1N/A SuperSafe == SAFE_REALLY_POSTMILTER ||
1N/A (SuperSafe == SAFE_INTERACTIVE && msync)) &&
1N/A fsync(sm_io_getinfo(tfp, SM_IO_WHAT_FD, NULL)) < 0) ||
1N/A sm_io_error(tfp))
1N/A {
1N/A if (newid)
1N/A syserr("!552 Error writing control file %s", tf);
1N/A else
1N/A syserr("!452 Error writing control file %s", tf);
1N/A }
1N/A
1N/A if (!newid)
1N/A {
1N/A char new = queue_letter(e, ANYQFL_LETTER);
1N/A
1N/A /* rename (locked) tf to be (locked) [qh]f */
1N/A (void) sm_strlcpy(qf, queuename(e, ANYQFL_LETTER),
1N/A sizeof(qf));
1N/A if (rename(tf, qf) < 0)
1N/A syserr("cannot rename(%s, %s), uid=%d",
1N/A tf, qf, (int) geteuid());
1N/A else
1N/A {
1N/A /*
1N/A ** Check if type has changed and only
1N/A ** remove the old item if the rename above
1N/A ** succeeded.
1N/A */
1N/A
1N/A if (e->e_qfletter != '\0' &&
1N/A e->e_qfletter != new)
1N/A {
1N/A if (tTd(40, 5))
1N/A {
1N/A sm_dprintf("type changed from %c to %c\n",
1N/A e->e_qfletter, new);
1N/A }
1N/A
1N/A if (unlink(queuename(e, e->e_qfletter)) < 0)
1N/A {
1N/A /* XXX: something more drastic? */
1N/A if (LogLevel > 0)
1N/A sm_syslog(LOG_ERR, e->e_id,
1N/A "queueup: unlink(%s) failed: %s",
1N/A queuename(e, e->e_qfletter),
1N/A sm_errstring(errno));
1N/A }
1N/A }
1N/A }
1N/A e->e_qfletter = new;
1N/A
1N/A /*
1N/A ** fsync() after renaming to make sure metadata is
1N/A ** written to disk on filesystems in which renames are
1N/A ** not guaranteed.
1N/A */
1N/A
1N/A if (SuperSafe != SAFE_NO)
1N/A {
1N/A /* for softupdates */
1N/A if (tfd >= 0 && fsync(tfd) < 0)
1N/A {
1N/A syserr("!queueup: cannot fsync queue temp file %s",
1N/A tf);
1N/A }
1N/A SYNC_DIR(qf, true);
1N/A }
1N/A
1N/A /* close and unlock old (locked) queue file */
1N/A if (e->e_lockfp != NULL)
1N/A (void) sm_io_close(e->e_lockfp, SM_TIME_DEFAULT);
1N/A e->e_lockfp = tfp;
1N/A
1N/A /* save log info */
1N/A if (LogLevel > 79)
1N/A sm_syslog(LOG_DEBUG, e->e_id, "queueup %s", qf);
1N/A }
1N/A else
1N/A {
1N/A /* save log info */
1N/A if (LogLevel > 79)
1N/A sm_syslog(LOG_DEBUG, e->e_id, "queueup %s", tf);
1N/A
1N/A e->e_qfletter = queue_letter(e, ANYQFL_LETTER);
1N/A }
1N/A
1N/A errno = 0;
1N/A e->e_flags |= EF_INQUEUE;
1N/A
1N/A if (tTd(40, 1))
1N/A sm_dprintf("<<<<< done queueing %s <<<<<\n\n", e->e_id);
1N/A return;
1N/A}
1N/A
1N/A/*
1N/A** PRINTCTLADDR -- print control address to file.
1N/A**
1N/A** Parameters:
1N/A** a -- address.
1N/A** tfp -- file pointer.
1N/A**
1N/A** Returns:
1N/A** none.
1N/A**
1N/A** Side Effects:
1N/A** The control address (if changed) is printed to the file.
1N/A** The last control address and uid are saved.
1N/A*/
1N/A
1N/Astatic void
1N/Aprintctladdr(a, tfp)
1N/A register ADDRESS *a;
1N/A SM_FILE_T *tfp;
1N/A{
1N/A char *user;
1N/A register ADDRESS *q;
1N/A uid_t uid;
1N/A gid_t gid;
1N/A static ADDRESS *lastctladdr = NULL;
1N/A static uid_t lastuid;
1N/A
1N/A /* initialization */
1N/A if (a == NULL || a->q_alias == NULL || tfp == NULL)
1N/A {
1N/A if (lastctladdr != NULL && tfp != NULL)
1N/A (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "C\n");
1N/A lastctladdr = NULL;
1N/A lastuid = 0;
1N/A return;
1N/A }
1N/A
1N/A /* find the active uid */
1N/A q = getctladdr(a);
1N/A if (q == NULL)
1N/A {
1N/A user = NULL;
1N/A uid = 0;
1N/A gid = 0;
1N/A }
1N/A else
1N/A {
1N/A user = q->q_ruser != NULL ? q->q_ruser : q->q_user;
1N/A uid = q->q_uid;
1N/A gid = q->q_gid;
1N/A }
1N/A a = a->q_alias;
1N/A
1N/A /* check to see if this is the same as last time */
1N/A if (lastctladdr != NULL && uid == lastuid &&
1N/A strcmp(lastctladdr->q_paddr, a->q_paddr) == 0)
1N/A return;
1N/A lastuid = uid;
1N/A lastctladdr = a;
1N/A
1N/A if (uid == 0 || user == NULL || user[0] == '\0')
1N/A (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "C");
1N/A else
1N/A (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "C%s:%ld:%ld",
1N/A denlstring(user, true, false), (long) uid,
1N/A (long) gid);
1N/A (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, ":%s\n",
1N/A denlstring(a->q_paddr, true, false));
1N/A}
1N/A
1N/A/*
1N/A** RUNNERS_SIGTERM -- propagate a SIGTERM to queue runner process
1N/A**
1N/A** This propagates the signal to the child processes that are queue
1N/A** runners. This is for a queue runner "cleanup". After all of the
1N/A** child queue runner processes are signaled (it should be SIGTERM
1N/A** being the sig) then the old signal handler (Oldsh) is called
1N/A** to handle any cleanup set for this process (provided it is not
1N/A** SIG_DFL or SIG_IGN). The signal may not be handled immediately
1N/A** if the BlockOldsh flag is set. If the current process doesn't
1N/A** have a parent then handle the signal immediately, regardless of
1N/A** BlockOldsh.
1N/A**
1N/A** Parameters:
1N/A** sig -- the signal number being sent
1N/A**
1N/A** Returns:
1N/A** none.
1N/A**
1N/A** Side Effects:
1N/A** Sets the NoMoreRunners boolean to true to stop more runners
1N/A** from being started in runqueue().
1N/A**
1N/A** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
1N/A** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
1N/A** DOING.
1N/A*/
1N/A
1N/Astatic bool volatile NoMoreRunners = false;
1N/Astatic sigfunc_t Oldsh_term = SIG_DFL;
1N/Astatic sigfunc_t Oldsh_hup = SIG_DFL;
1N/Astatic sigfunc_t volatile Oldsh = SIG_DFL;
1N/Astatic bool BlockOldsh = false;
1N/Astatic int volatile Oldsig = 0;
1N/Astatic SIGFUNC_DECL runners_sigterm __P((int));
1N/Astatic SIGFUNC_DECL runners_sighup __P((int));
1N/A
1N/Astatic SIGFUNC_DECL
1N/Arunners_sigterm(sig)
1N/A int sig;
1N/A{
1N/A int save_errno = errno;
1N/A
1N/A FIX_SYSV_SIGNAL(sig, runners_sigterm);
1N/A errno = save_errno;
1N/A CHECK_CRITICAL(sig);
1N/A NoMoreRunners = true;
1N/A Oldsh = Oldsh_term;
1N/A Oldsig = sig;
1N/A proc_list_signal(PROC_QUEUE, sig);
1N/A
1N/A if (!BlockOldsh || getppid() <= 1)
1N/A {
1N/A /* Check that a valid 'old signal handler' is callable */
1N/A if (Oldsh_term != SIG_DFL && Oldsh_term != SIG_IGN &&
1N/A Oldsh_term != runners_sigterm)
1N/A (*Oldsh_term)(sig);
1N/A }
1N/A errno = save_errno;
1N/A return SIGFUNC_RETURN;
1N/A}
1N/A/*
1N/A** RUNNERS_SIGHUP -- propagate a SIGHUP to queue runner process
1N/A**
1N/A** This propagates the signal to the child processes that are queue
1N/A** runners. This is for a queue runner "cleanup". After all of the
1N/A** child queue runner processes are signaled (it should be SIGHUP
1N/A** being the sig) then the old signal handler (Oldsh) is called to
1N/A** handle any cleanup set for this process (provided it is not SIG_DFL
1N/A** or SIG_IGN). The signal may not be handled immediately if the
1N/A** BlockOldsh flag is set. If the current process doesn't have
1N/A** a parent then handle the signal immediately, regardless of
1N/A** BlockOldsh.
1N/A**
1N/A** Parameters:
1N/A** sig -- the signal number being sent
1N/A**
1N/A** Returns:
1N/A** none.
1N/A**
1N/A** Side Effects:
1N/A** Sets the NoMoreRunners boolean to true to stop more runners
1N/A** from being started in runqueue().
1N/A**
1N/A** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
1N/A** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
1N/A** DOING.
1N/A*/
1N/A
1N/Astatic SIGFUNC_DECL
1N/Arunners_sighup(sig)
1N/A int sig;
1N/A{
1N/A int save_errno = errno;
1N/A
1N/A FIX_SYSV_SIGNAL(sig, runners_sighup);
1N/A errno = save_errno;
1N/A CHECK_CRITICAL(sig);
1N/A NoMoreRunners = true;
1N/A Oldsh = Oldsh_hup;
1N/A Oldsig = sig;
1N/A proc_list_signal(PROC_QUEUE, sig);
1N/A
1N/A if (!BlockOldsh || getppid() <= 1)
1N/A {
1N/A /* Check that a valid 'old signal handler' is callable */
1N/A if (Oldsh_hup != SIG_DFL && Oldsh_hup != SIG_IGN &&
1N/A Oldsh_hup != runners_sighup)
1N/A (*Oldsh_hup)(sig);
1N/A }
1N/A errno = save_errno;
1N/A return SIGFUNC_RETURN;
1N/A}
1N/A/*
1N/A** MARK_WORK_GROUP_RESTART -- mark a work group as needing a restart
1N/A**
1N/A** Sets a workgroup for restarting.
1N/A**
1N/A** Parameters:
1N/A** wgrp -- the work group id to restart.
1N/A** reason -- why (signal?), -1 to turn off restart
1N/A**
1N/A** Returns:
1N/A** none.
1N/A**
1N/A** Side effects:
1N/A** May set global RestartWorkGroup to true.
1N/A**
1N/A** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
1N/A** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
1N/A** DOING.
1N/A*/
1N/A
1N/Avoid
1N/Amark_work_group_restart(wgrp, reason)
1N/A int wgrp;
1N/A int reason;
1N/A{
1N/A if (wgrp < 0 || wgrp > NumWorkGroups)
1N/A return;
1N/A
1N/A WorkGrp[wgrp].wg_restart = reason;
1N/A if (reason >= 0)
1N/A RestartWorkGroup = true;
1N/A}
1N/A/*
1N/A** RESTART_MARKED_WORK_GROUPS -- restart work groups marked as needing restart
1N/A**
1N/A** Restart any workgroup marked as needing a restart provided more
1N/A** runners are allowed.
1N/A**
1N/A** Parameters:
1N/A** none.
1N/A**
1N/A** Returns:
1N/A** none.
1N/A**
1N/A** Side effects:
1N/A** Sets global RestartWorkGroup to false.
1N/A*/
1N/A
1N/Avoid
1N/Arestart_marked_work_groups()
1N/A{
1N/A int i;
1N/A int wasblocked;
1N/A
1N/A if (NoMoreRunners)
1N/A return;
1N/A
1N/A /* Block SIGCHLD so reapchild() doesn't mess with us */
1N/A wasblocked = sm_blocksignal(SIGCHLD);
1N/A
1N/A for (i = 0; i < NumWorkGroups; i++)
1N/A {
1N/A if (WorkGrp[i].wg_restart >= 0)
1N/A {
1N/A if (LogLevel > 8)
1N/A sm_syslog(LOG_ERR, NOQID,
1N/A "restart queue runner=%d due to signal 0x%x",
1N/A i, WorkGrp[i].wg_restart);
1N/A restart_work_group(i);
1N/A }
1N/A }
1N/A RestartWorkGroup = false;
1N/A
1N/A if (wasblocked == 0)
1N/A (void) sm_releasesignal(SIGCHLD);
1N/A}
1N/A/*
1N/A** RESTART_WORK_GROUP -- restart a specific work group
1N/A**
1N/A** Restart a specific workgroup provided more runners are allowed.
1N/A** If the requested work group has been restarted too many times log
1N/A** this and refuse to restart.
1N/A**
1N/A** Parameters:
1N/A** wgrp -- the work group id to restart
1N/A**
1N/A** Returns:
1N/A** none.
1N/A**
1N/A** Side Effects:
1N/A** starts another process doing the work of wgrp
1N/A*/
1N/A
1N/A#define MAX_PERSIST_RESTART 10 /* max allowed number of restarts */
1N/A
1N/Astatic void
1N/Arestart_work_group(wgrp)
1N/A int wgrp;
1N/A{
1N/A if (NoMoreRunners ||
1N/A wgrp < 0 || wgrp > NumWorkGroups)
1N/A return;
1N/A
1N/A WorkGrp[wgrp].wg_restart = -1;
1N/A if (WorkGrp[wgrp].wg_restartcnt < MAX_PERSIST_RESTART)
1N/A {
1N/A /* avoid overflow; increment here */
1N/A WorkGrp[wgrp].wg_restartcnt++;
1N/A (void) run_work_group(wgrp, RWG_FORK|RWG_PERSISTENT|RWG_RUNALL);
1N/A }
1N/A else
1N/A {
1N/A sm_syslog(LOG_ERR, NOQID,
1N/A "ERROR: persistent queue runner=%d restarted too many times, queue runner lost",
1N/A wgrp);
1N/A }
1N/A}
1N/A/*
1N/A** SCHEDULE_QUEUE_RUNS -- schedule the next queue run for a work group.
1N/A**
1N/A** Parameters:
1N/A** runall -- schedule even if individual bit is not set.
1N/A** wgrp -- the work group id to schedule.
1N/A** didit -- the queue run was performed for this work group.
1N/A**
1N/A** Returns:
1N/A** nothing
1N/A*/
1N/A
1N/A#define INCR_MOD(v, m) if (++v >= m) \
1N/A v = 0; \
1N/A else
1N/A
1N/Astatic void
1N/Aschedule_queue_runs(runall, wgrp, didit)
1N/A bool runall;
1N/A int wgrp;
1N/A bool didit;
1N/A{
1N/A int qgrp, cgrp, endgrp;
1N/A#if _FFR_QUEUE_SCHED_DBG
1N/A time_t lastsched;
1N/A bool sched;
1N/A#endif /* _FFR_QUEUE_SCHED_DBG */
1N/A time_t now;
1N/A time_t minqintvl;
1N/A
1N/A /*
1N/A ** This is a bit ugly since we have to duplicate the
1N/A ** code that "walks" through a work queue group.
1N/A */
1N/A
1N/A now = curtime();
1N/A minqintvl = 0;
1N/A cgrp = endgrp = WorkGrp[wgrp].wg_curqgrp;
1N/A do
1N/A {
1N/A time_t qintvl;
1N/A
1N/A#if _FFR_QUEUE_SCHED_DBG
1N/A lastsched = 0;
1N/A sched = false;
1N/A#endif /* _FFR_QUEUE_SCHED_DBG */
1N/A qgrp = WorkGrp[wgrp].wg_qgs[cgrp]->qg_index;
1N/A if (Queue[qgrp]->qg_queueintvl > 0)
1N/A qintvl = Queue[qgrp]->qg_queueintvl;
1N/A else if (QueueIntvl > 0)
1N/A qintvl = QueueIntvl;
1N/A else
1N/A qintvl = (time_t) 0;
1N/A#if _FFR_QUEUE_SCHED_DBG
1N/A lastsched = Queue[qgrp]->qg_nextrun;
1N/A#endif /* _FFR_QUEUE_SCHED_DBG */
1N/A if ((runall || Queue[qgrp]->qg_nextrun <= now) && qintvl > 0)
1N/A {
1N/A#if _FFR_QUEUE_SCHED_DBG
1N/A sched = true;
1N/A#endif /* _FFR_QUEUE_SCHED_DBG */
1N/A if (minqintvl == 0 || qintvl < minqintvl)
1N/A minqintvl = qintvl;
1N/A
1N/A /*
1N/A ** Only set a new time if a queue run was performed
1N/A ** for this queue group. If the queue was not run,
1N/A ** we could starve it by setting a new time on each
1N/A ** call.
1N/A */
1N/A
1N/A if (didit)
1N/A Queue[qgrp]->qg_nextrun += qintvl;
1N/A }
1N/A#if _FFR_QUEUE_SCHED_DBG
1N/A if (tTd(69, 10))
1N/A sm_syslog(LOG_INFO, NOQID,
1N/A "sqr: wgrp=%d, cgrp=%d, qgrp=%d, intvl=%ld, QI=%ld, runall=%d, lastrun=%ld, nextrun=%ld, sched=%d",
1N/A wgrp, cgrp, qgrp, Queue[qgrp]->qg_queueintvl,
1N/A QueueIntvl, runall, lastsched,
1N/A Queue[qgrp]->qg_nextrun, sched);
1N/A#endif /* _FFR_QUEUE_SCHED_DBG */
1N/A INCR_MOD(cgrp, WorkGrp[wgrp].wg_numqgrp);
1N/A } while (endgrp != cgrp);
1N/A if (minqintvl > 0)
1N/A (void) sm_setevent(minqintvl, runqueueevent, 0);
1N/A}
1N/A
1N/A#if _FFR_QUEUE_RUN_PARANOIA
1N/A/*
1N/A** CHECKQUEUERUNNER -- check whether a queue group hasn't been run.
1N/A**
1N/A** Use this if events may get lost and hence queue runners may not
1N/A** be started and mail will pile up in a queue.
1N/A**
1N/A** Parameters:
1N/A** none.
1N/A**
1N/A** Returns:
1N/A** true if a queue run is necessary.
1N/A**
1N/A** Side Effects:
1N/A** may schedule a queue run.
1N/A*/
1N/A
1N/Abool
1N/Acheckqueuerunner()
1N/A{
1N/A int qgrp;
1N/A time_t now, minqintvl;
1N/A
1N/A now = curtime();
1N/A minqintvl = 0;
1N/A for (qgrp = 0; qgrp < NumQueue && Queue[qgrp] != NULL; qgrp++)
1N/A {
1N/A time_t qintvl;
1N/A
1N/A if (Queue[qgrp]->qg_queueintvl > 0)
1N/A qintvl = Queue[qgrp]->qg_queueintvl;
1N/A else if (QueueIntvl > 0)
1N/A qintvl = QueueIntvl;
1N/A else
1N/A qintvl = (time_t) 0;
1N/A if (Queue[qgrp]->qg_nextrun <= now - qintvl)
1N/A {
1N/A if (minqintvl == 0 || qintvl < minqintvl)
1N/A minqintvl = qintvl;
1N/A if (LogLevel > 1)
1N/A sm_syslog(LOG_WARNING, NOQID,
1N/A "checkqueuerunner: queue %d should have been run at %s, queue interval %ld",
1N/A qgrp,
1N/A arpadate(ctime(&Queue[qgrp]->qg_nextrun)),
1N/A qintvl);
1N/A }
1N/A }
1N/A if (minqintvl > 0)
1N/A {
1N/A (void) sm_setevent(minqintvl, runqueueevent, 0);
1N/A return true;
1N/A }
1N/A return false;
1N/A}
1N/A#endif /* _FFR_QUEUE_RUN_PARANOIA */
1N/A
1N/A/*
1N/A** RUNQUEUE -- run the jobs in the queue.
1N/A**
1N/A** Gets the stuff out of the queue in some presumably logical
1N/A** order and processes them.
1N/A**
1N/A** Parameters:
1N/A** forkflag -- true if the queue scanning should be done in
1N/A** a child process. We double-fork so it is not our
1N/A** child and we don't have to clean up after it.
1N/A** false can be ignored if we have multiple queues.
1N/A** verbose -- if true, print out status information.
1N/A** persistent -- persistent queue runner?
1N/A** runall -- run all groups or only a subset (DoQueueRun)?
1N/A**
1N/A** Returns:
1N/A** true if the queue run successfully began.
1N/A**
1N/A** Side Effects:
1N/A** runs things in the mail queue using run_work_group().
1N/A** maybe schedules next queue run.
1N/A*/
1N/A
1N/Astatic ENVELOPE QueueEnvelope; /* the queue run envelope */
1N/Astatic time_t LastQueueTime = 0; /* last time a queue ID assigned */
1N/Astatic pid_t LastQueuePid = -1; /* last PID which had a queue ID */
1N/A
1N/A/* values for qp_supdirs */
1N/A#define QP_NOSUB 0x0000 /* No subdirectories */
1N/A#define QP_SUBDF 0x0001 /* "df" subdirectory */
1N/A#define QP_SUBQF 0x0002 /* "qf" subdirectory */
1N/A#define QP_SUBXF 0x0004 /* "xf" subdirectory */
1N/A
1N/Abool
1N/Arunqueue(forkflag, verbose, persistent, runall)
1N/A bool forkflag;
1N/A bool verbose;
1N/A bool persistent;
1N/A bool runall;
1N/A{
1N/A int i;
1N/A bool ret = true;
1N/A static int curnum = 0;
1N/A sigfunc_t cursh;
1N/A#if SM_HEAP_CHECK
1N/A SM_NONVOLATILE int oldgroup = 0;
1N/A
1N/A if (sm_debug_active(&DebugLeakQ, 1))
1N/A {
1N/A oldgroup = sm_heap_group();
1N/A sm_heap_newgroup();
1N/A sm_dprintf("runqueue() heap group #%d\n", sm_heap_group());
1N/A }
1N/A#endif /* SM_HEAP_CHECK */
1N/A
1N/A /* queue run has been started, don't do any more this time */
1N/A DoQueueRun = false;
1N/A
1N/A /* more than one queue or more than one directory per queue */
1N/A if (!forkflag && !verbose &&
1N/A (WorkGrp[0].wg_qgs[0]->qg_numqueues > 1 || NumWorkGroups > 1 ||
1N/A WorkGrp[0].wg_numqgrp > 1))
1N/A forkflag = true;
1N/A
1N/A /*
1N/A ** For controlling queue runners via signals sent to this process.
1N/A ** Oldsh* will get called too by runners_sig* (if it is not SIG_IGN
1N/A ** or SIG_DFL) to preserve cleanup behavior. Now that this process
1N/A ** will have children (and perhaps grandchildren) this handler will
1N/A ** be left in place. This is because this process, once it has
1N/A ** finished spinning off queue runners, may go back to doing something
1N/A ** else (like being a daemon). And we still want on a SIG{TERM,HUP} to
1N/A ** clean up the child queue runners. Only install 'runners_sig*' once
1N/A ** else we'll get stuck looping forever.
1N/A */
1N/A
1N/A cursh = sm_signal(SIGTERM, runners_sigterm);
1N/A if (cursh != runners_sigterm)
1N/A Oldsh_term = cursh;
1N/A cursh = sm_signal(SIGHUP, runners_sighup);
1N/A if (cursh != runners_sighup)
1N/A Oldsh_hup = cursh;
1N/A
1N/A for (i = 0; i < NumWorkGroups && !NoMoreRunners; i++)
1N/A {
1N/A int rwgflags = RWG_NONE;
1N/A
1N/A /*
1N/A ** If MaxQueueChildren active then test whether the start
1N/A ** of the next queue group's additional queue runners (maximum)
1N/A ** will result in MaxQueueChildren being exceeded.
1N/A **
1N/A ** Note: do not use continue; even though another workgroup
1N/A ** may have fewer queue runners, this would be "unfair",
1N/A ** i.e., this work group might "starve" then.
1N/A */
1N/A
1N/A#if _FFR_QUEUE_SCHED_DBG
1N/A if (tTd(69, 10))
1N/A sm_syslog(LOG_INFO, NOQID,
1N/A "rq: curnum=%d, MaxQueueChildren=%d, CurRunners=%d, WorkGrp[curnum].wg_maxact=%d",
1N/A curnum, MaxQueueChildren, CurRunners,
1N/A WorkGrp[curnum].wg_maxact);
1N/A#endif /* _FFR_QUEUE_SCHED_DBG */
1N/A if (MaxQueueChildren > 0 &&
1N/A CurRunners + WorkGrp[curnum].wg_maxact > MaxQueueChildren)
1N/A break;
1N/A
1N/A /*
1N/A ** Pick up where we left off (curnum), in case we
1N/A ** used up all the children last time without finishing.
1N/A ** This give a round-robin fairness to queue runs.
1N/A **
1N/A ** Increment CurRunners before calling run_work_group()
1N/A ** to avoid a "race condition" with proc_list_drop() which
1N/A ** decrements CurRunners if the queue runners terminate.
1N/A ** Notice: CurRunners is an upper limit, in some cases
1N/A ** (too few jobs in the queue) this value is larger than
1N/A ** the actual number of queue runners. The discrepancy can
1N/A ** increase if some queue runners "hang" for a long time.
1N/A */
1N/A
1N/A CurRunners += WorkGrp[curnum].wg_maxact;
1N/A if (forkflag)
1N/A rwgflags |= RWG_FORK;
1N/A if (verbose)
1N/A rwgflags |= RWG_VERBOSE;
1N/A if (persistent)
1N/A rwgflags |= RWG_PERSISTENT;
1N/A if (runall)
1N/A rwgflags |= RWG_RUNALL;
1N/A ret = run_work_group(curnum, rwgflags);
1N/A
1N/A /*
1N/A ** Failure means a message was printed for ETRN
1N/A ** and subsequent queues are likely to fail as well.
1N/A ** Decrement CurRunners in that case because
1N/A ** none have been started.
1N/A */
1N/A
1N/A if (!ret)
1N/A {
1N/A CurRunners -= WorkGrp[curnum].wg_maxact;
1N/A break;
1N/A }
1N/A
1N/A if (!persistent)
1N/A schedule_queue_runs(runall, curnum, true);
1N/A INCR_MOD(curnum, NumWorkGroups);
1N/A }
1N/A
1N/A /* schedule left over queue runs */
1N/A if (i < NumWorkGroups && !NoMoreRunners && !persistent)
1N/A {
1N/A int h;
1N/A
1N/A for (h = curnum; i < NumWorkGroups; i++)
1N/A {
1N/A schedule_queue_runs(runall, h, false);
1N/A INCR_MOD(h, NumWorkGroups);
1N/A }
1N/A }
1N/A
1N/A
1N/A#if SM_HEAP_CHECK
1N/A if (sm_debug_active(&DebugLeakQ, 1))
1N/A sm_heap_setgroup(oldgroup);
1N/A#endif /* SM_HEAP_CHECK */
1N/A return ret;
1N/A}
1N/A
1N/A#if _FFR_SKIP_DOMAINS
1N/A/*
1N/A** SKIP_DOMAINS -- Skip 'skip' number of domains in the WorkQ.
1N/A**
1N/A** Added by Stephen Frost <sfrost@snowman.net> to support
1N/A** having each runner process every N'th domain instead of
1N/A** every N'th message.
1N/A**
1N/A** Parameters:
1N/A** skip -- number of domains in WorkQ to skip.
1N/A**
1N/A** Returns:
1N/A** total number of messages skipped.
1N/A**
1N/A** Side Effects:
1N/A** may change WorkQ
1N/A*/
1N/A
1N/Astatic int
1N/Askip_domains(skip)
1N/A int skip;
1N/A{
1N/A int n, seqjump;
1N/A
1N/A for (n = 0, seqjump = 0; n < skip && WorkQ != NULL; seqjump++)
1N/A {
1N/A if (WorkQ->w_next != NULL)
1N/A {
1N/A if (WorkQ->w_host != NULL &&
1N/A WorkQ->w_next->w_host != NULL)
1N/A {
1N/A if (sm_strcasecmp(WorkQ->w_host,
1N/A WorkQ->w_next->w_host) != 0)
1N/A n++;
1N/A }
1N/A else
1N/A {
1N/A if ((WorkQ->w_host != NULL &&
1N/A WorkQ->w_next->w_host == NULL) ||
1N/A (WorkQ->w_host == NULL &&
1N/A WorkQ->w_next->w_host != NULL))
1N/A n++;
1N/A }
1N/A }
1N/A WorkQ = WorkQ->w_next;
1N/A }
1N/A return seqjump;
1N/A}
1N/A#endif /* _FFR_SKIP_DOMAINS */
1N/A
1N/A/*
1N/A** RUNNER_WORK -- have a queue runner do its work
1N/A**
1N/A** Have a queue runner do its work a list of entries.
1N/A** When work isn't directly being done then this process can take a signal
1N/A** and terminate immediately (in a clean fashion of course).
1N/A** When work is directly being done, it's not to be interrupted
1N/A** immediately: the work should be allowed to finish at a clean point
1N/A** before termination (in a clean fashion of course).
1N/A**
1N/A** Parameters:
1N/A** e -- envelope.
1N/A** sequenceno -- 'th process to run WorkQ.
1N/A** didfork -- did the calling process fork()?
1N/A** skip -- process only each skip'th item.
1N/A** njobs -- number of jobs in WorkQ.
1N/A**
1N/A** Returns:
1N/A** none.
1N/A**
1N/A** Side Effects:
1N/A** runs things in the mail queue.
1N/A*/
1N/A
1N/Astatic void
1N/Arunner_work(e, sequenceno, didfork, skip, njobs)
1N/A register ENVELOPE *e;
1N/A int sequenceno;
1N/A bool didfork;
1N/A int skip;
1N/A int njobs;
1N/A{
1N/A int n, seqjump;
1N/A WORK *w;
1N/A time_t now;
1N/A
1N/A SM_GET_LA(now);
1N/A
1N/A /*
1N/A ** Here we temporarily block the second calling of the handlers.
1N/A ** This allows us to handle the signal without terminating in the
1N/A ** middle of direct work. If a signal does come, the test for
1N/A ** NoMoreRunners will find it.
1N/A */
1N/A
1N/A BlockOldsh = true;
1N/A seqjump = skip;
1N/A
1N/A /* process them once at a time */
1N/A while (WorkQ != NULL)
1N/A {
1N/A#if SM_HEAP_CHECK
1N/A SM_NONVOLATILE int oldgroup = 0;
1N/A
1N/A if (sm_debug_active(&DebugLeakQ, 1))
1N/A {
1N/A oldgroup = sm_heap_group();
1N/A sm_heap_newgroup();
1N/A sm_dprintf("run_queue_group() heap group #%d\n",
1N/A sm_heap_group());
1N/A }
1N/A#endif /* SM_HEAP_CHECK */
1N/A
1N/A /* do no more work */
1N/A if (NoMoreRunners)
1N/A {
1N/A /* Check that a valid signal handler is callable */
1N/A if (Oldsh != SIG_DFL && Oldsh != SIG_IGN &&
1N/A Oldsh != runners_sighup &&
1N/A Oldsh != runners_sigterm)
1N/A (*Oldsh)(Oldsig);
1N/A break;
1N/A }
1N/A
1N/A w = WorkQ; /* assign current work item */
1N/A
1N/A /*
1N/A ** Set the head of the WorkQ to the next work item.
1N/A ** It is set 'skip' ahead (the number of parallel queue
1N/A ** runners working on WorkQ together) since each runner
1N/A ** works on every 'skip'th (N-th) item.
1N/A#if _FFR_SKIP_DOMAINS
1N/A ** In the case of the BYHOST Queue Sort Order, the 'item'
1N/A ** is a domain, so we work on every 'skip'th (N-th) domain.
1N/A#endif * _FFR_SKIP_DOMAINS *
1N/A */
1N/A
1N/A#if _FFR_SKIP_DOMAINS
1N/A if (QueueSortOrder == QSO_BYHOST)
1N/A {
1N/A seqjump = 1;
1N/A if (WorkQ->w_next != NULL)
1N/A {
1N/A if (WorkQ->w_host != NULL &&
1N/A WorkQ->w_next->w_host != NULL)
1N/A {
1N/A if (sm_strcasecmp(WorkQ->w_host,
1N/A WorkQ->w_next->w_host)
1N/A != 0)
1N/A seqjump = skip_domains(skip);
1N/A else
1N/A WorkQ = WorkQ->w_next;
1N/A }
1N/A else
1N/A {
1N/A if ((WorkQ->w_host != NULL &&
1N/A WorkQ->w_next->w_host == NULL) ||
1N/A (WorkQ->w_host == NULL &&
1N/A WorkQ->w_next->w_host != NULL))
1N/A seqjump = skip_domains(skip);
1N/A else
1N/A WorkQ = WorkQ->w_next;
1N/A }
1N/A }
1N/A else
1N/A WorkQ = WorkQ->w_next;
1N/A }
1N/A else
1N/A#endif /* _FFR_SKIP_DOMAINS */
1N/A {
1N/A for (n = 0; n < skip && WorkQ != NULL; n++)
1N/A WorkQ = WorkQ->w_next;
1N/A }
1N/A
1N/A e->e_to = NULL;
1N/A
1N/A /*
1N/A ** Ignore jobs that are too expensive for the moment.
1N/A **
1N/A ** Get new load average every GET_NEW_LA_TIME seconds.
1N/A */
1N/A
1N/A SM_GET_LA(now);
1N/A if (shouldqueue(WkRecipFact, Current_LA_time))
1N/A {
1N/A char *msg = "Aborting queue run: load average too high";
1N/A
1N/A if (Verbose)
1N/A message("%s", msg);
1N/A if (LogLevel > 8)
1N/A sm_syslog(LOG_INFO, NOQID, "runqueue: %s", msg);
1N/A break;
1N/A }
1N/A if (shouldqueue(w->w_pri, w->w_ctime))
1N/A {
1N/A if (Verbose)
1N/A message(EmptyString);
1N/A if (QueueSortOrder == QSO_BYPRIORITY)
1N/A {
1N/A if (Verbose)
1N/A message("Skipping %s/%s (sequence %d of %d) and flushing rest of queue",
1N/A qid_printqueue(w->w_qgrp,
1N/A w->w_qdir),
1N/A w->w_name + 2, sequenceno,
1N/A njobs);
1N/A if (LogLevel > 8)
1N/A sm_syslog(LOG_INFO, NOQID,
1N/A "runqueue: Flushing queue from %s/%s (pri %ld, LA %d, %d of %d)",
1N/A qid_printqueue(w->w_qgrp,
1N/A w->w_qdir),
1N/A w->w_name + 2, w->w_pri,
1N/A CurrentLA, sequenceno,
1N/A njobs);
1N/A break;
1N/A }
1N/A else if (Verbose)
1N/A message("Skipping %s/%s (sequence %d of %d)",
1N/A qid_printqueue(w->w_qgrp, w->w_qdir),
1N/A w->w_name + 2, sequenceno, njobs);
1N/A }
1N/A else
1N/A {
1N/A if (Verbose)
1N/A {
1N/A message(EmptyString);
1N/A message("Running %s/%s (sequence %d of %d)",
1N/A qid_printqueue(w->w_qgrp, w->w_qdir),
1N/A w->w_name + 2, sequenceno, njobs);
1N/A }
1N/A if (didfork && MaxQueueChildren > 0)
1N/A {
1N/A sm_blocksignal(SIGCHLD);
1N/A (void) sm_signal(SIGCHLD, reapchild);
1N/A }
1N/A if (tTd(63, 100))
1N/A sm_syslog(LOG_DEBUG, NOQID,
1N/A "runqueue %s dowork(%s)",
1N/A qid_printqueue(w->w_qgrp, w->w_qdir),
1N/A w->w_name + 2);
1N/A
1N/A (void) dowork(w->w_qgrp, w->w_qdir, w->w_name + 2,
1N/A ForkQueueRuns, false, e);
1N/A errno = 0;
1N/A }
1N/A sm_free(w->w_name); /* XXX */
1N/A if (w->w_host != NULL)
1N/A sm_free(w->w_host); /* XXX */
1N/A sm_free((char *) w); /* XXX */
1N/A sequenceno += seqjump; /* next sequence number */
1N/A#if SM_HEAP_CHECK
1N/A if (sm_debug_active(&DebugLeakQ, 1))
1N/A sm_heap_setgroup(oldgroup);
1N/A#endif /* SM_HEAP_CHECK */
1N/A }
1N/A
1N/A BlockOldsh = false;
1N/A
1N/A /* check the signals didn't happen during the revert */
1N/A if (NoMoreRunners)
1N/A {
1N/A /* Check that a valid signal handler is callable */
1N/A if (Oldsh != SIG_DFL && Oldsh != SIG_IGN &&
1N/A Oldsh != runners_sighup && Oldsh != runners_sigterm)
1N/A (*Oldsh)(Oldsig);
1N/A }
1N/A
1N/A Oldsh = SIG_DFL; /* after the NoMoreRunners check */
1N/A}
1N/A/*
1N/A** RUN_WORK_GROUP -- run the jobs in a queue group from a work group.
1N/A**
1N/A** Gets the stuff out of the queue in some presumably logical
1N/A** order and processes them.
1N/A**
1N/A** Parameters:
1N/A** wgrp -- work group to process.
1N/A** flags -- RWG_* flags
1N/A**
1N/A** Returns:
1N/A** true if the queue run successfully began.
1N/A**
1N/A** Side Effects:
1N/A** runs things in the mail queue.
1N/A*/
1N/A
1N/A/* Minimum sleep time for persistent queue runners */
1N/A#define MIN_SLEEP_TIME 5
1N/A
1N/Abool
1N/Arun_work_group(wgrp, flags)
1N/A int wgrp;
1N/A int flags;
1N/A{
1N/A register ENVELOPE *e;
1N/A int njobs, qdir;
1N/A int sequenceno = 1;
1N/A int qgrp, endgrp, h, i;
1N/A time_t now;
1N/A bool full, more;
1N/A SM_RPOOL_T *rpool;
1N/A extern ENVELOPE BlankEnvelope;
1N/A extern SIGFUNC_DECL reapchild __P((int));
1N/A
1N/A if (wgrp < 0)
1N/A return false;
1N/A
1N/A /*
1N/A ** If no work will ever be selected, don't even bother reading
1N/A ** the queue.
1N/A */
1N/A
1N/A SM_GET_LA(now);
1N/A
1N/A if (!bitset(RWG_PERSISTENT, flags) &&
1N/A shouldqueue(WkRecipFact, Current_LA_time))
1N/A {
1N/A char *msg = "Skipping queue run -- load average too high";
1N/A
1N/A if (bitset(RWG_VERBOSE, flags))
1N/A message("458 %s\n", msg);
1N/A if (LogLevel > 8)
1N/A sm_syslog(LOG_INFO, NOQID, "runqueue: %s", msg);
1N/A return false;
1N/A }
1N/A
1N/A /*
1N/A ** See if we already have too many children.
1N/A */
1N/A
1N/A if (bitset(RWG_FORK, flags) &&
1N/A WorkGrp[wgrp].wg_lowqintvl > 0 &&
1N/A !bitset(RWG_PERSISTENT, flags) &&
1N/A MaxChildren > 0 && CurChildren >= MaxChildren)
1N/A {
1N/A char *msg = "Skipping queue run -- too many children";
1N/A
1N/A if (bitset(RWG_VERBOSE, flags))
1N/A message("458 %s (%d)\n", msg, CurChildren);
1N/A if (LogLevel > 8)
1N/A sm_syslog(LOG_INFO, NOQID, "runqueue: %s (%d)",
1N/A msg, CurChildren);
1N/A return false;
1N/A }
1N/A
1N/A /*
1N/A ** See if we want to go off and do other useful work.
1N/A */
1N/A
1N/A if (bitset(RWG_FORK, flags))
1N/A {
1N/A pid_t pid;
1N/A
1N/A (void) sm_blocksignal(SIGCHLD);
1N/A (void) sm_signal(SIGCHLD, reapchild);
1N/A
1N/A pid = dofork();
1N/A if (pid == -1)
1N/A {
1N/A const char *msg = "Skipping queue run -- fork() failed";
1N/A const char *err = sm_errstring(errno);
1N/A
1N/A if (bitset(RWG_VERBOSE, flags))
1N/A message("458 %s: %s\n", msg, err);
1N/A if (LogLevel > 8)
1N/A sm_syslog(LOG_INFO, NOQID, "runqueue: %s: %s",
1N/A msg, err);
1N/A (void) sm_releasesignal(SIGCHLD);
1N/A return false;
1N/A }
1N/A if (pid != 0)
1N/A {
1N/A /* parent -- pick up intermediate zombie */
1N/A (void) sm_blocksignal(SIGALRM);
1N/A
1N/A /* wgrp only used when queue runners are persistent */
1N/A proc_list_add(pid, "Queue runner", PROC_QUEUE,
1N/A WorkGrp[wgrp].wg_maxact,
1N/A bitset(RWG_PERSISTENT, flags) ? wgrp : -1,
1N/A NULL);
1N/A (void) sm_releasesignal(SIGALRM);
1N/A (void) sm_releasesignal(SIGCHLD);
1N/A return true;
1N/A }
1N/A
1N/A /* child -- clean up signals */
1N/A
1N/A /* Reset global flags */
1N/A RestartRequest = NULL;
1N/A RestartWorkGroup = false;
1N/A ShutdownRequest = NULL;
1N/A PendingSignal = 0;
1N/A CurrentPid = getpid();
1N/A close_sendmail_pid();
1N/A
1N/A /*
1N/A ** Initialize exception stack and default exception
1N/A ** handler for child process.
1N/A */
1N/A
1N/A sm_exc_newthread(fatal_error);
1N/A clrcontrol();
1N/A proc_list_clear();
1N/A
1N/A /* Add parent process as first child item */
1N/A proc_list_add(CurrentPid, "Queue runner child process",
1N/A PROC_QUEUE_CHILD, 0, -1, NULL);
1N/A (void) sm_releasesignal(SIGCHLD);
1N/A (void) sm_signal(SIGCHLD, SIG_DFL);
1N/A (void) sm_signal(SIGHUP, SIG_DFL);
1N/A (void) sm_signal(SIGTERM, intsig);
1N/A }
1N/A
1N/A /*
1N/A ** Release any resources used by the daemon code.
1N/A */
1N/A
1N/A clrdaemon();
1N/A
1N/A /* force it to run expensive jobs */
1N/A NoConnect = false;
1N/A
1N/A /* drop privileges */
1N/A if (geteuid() == (uid_t) 0)
1N/A (void) drop_privileges(false);
1N/A
1N/A /*
1N/A ** Create ourselves an envelope
1N/A */
1N/A
1N/A CurEnv = &QueueEnvelope;
1N/A rpool = sm_rpool_new_x(NULL);
1N/A e = newenvelope(&QueueEnvelope, CurEnv, rpool);
1N/A e->e_flags = BlankEnvelope.e_flags;
1N/A e->e_parent = NULL;
1N/A
1N/A /* make sure we have disconnected from parent */
1N/A if (bitset(RWG_FORK, flags))
1N/A {
1N/A disconnect(1, e);
1N/A QuickAbort = false;
1N/A }
1N/A
1N/A /*
1N/A ** If we are running part of the queue, always ignore stored
1N/A ** host status.
1N/A */
1N/A
1N/A if (QueueLimitId != NULL || QueueLimitSender != NULL ||
1N/A QueueLimitQuarantine != NULL ||
1N/A QueueLimitRecipient != NULL)
1N/A {
1N/A IgnoreHostStatus = true;
1N/A MinQueueAge = 0;
1N/A }
1N/A
1N/A /*
1N/A ** Here is where we choose the queue group from the work group.
1N/A ** The caller of the "domorework" label must setup a new envelope.
1N/A */
1N/A
1N/A endgrp = WorkGrp[wgrp].wg_curqgrp; /* to not spin endlessly */
1N/A
1N/A domorework:
1N/A
1N/A /*
1N/A ** Run a queue group if:
1N/A ** RWG_RUNALL bit is set or the bit for this group is set.
1N/A */
1N/A
1N/A now = curtime();
1N/A for (;;)
1N/A {
1N/A /*
1N/A ** Find the next queue group within the work group that
1N/A ** has been marked as needing a run.
1N/A */
1N/A
1N/A qgrp = WorkGrp[wgrp].wg_qgs[WorkGrp[wgrp].wg_curqgrp]->qg_index;
1N/A WorkGrp[wgrp].wg_curqgrp++; /* advance */
1N/A WorkGrp[wgrp].wg_curqgrp %= WorkGrp[wgrp].wg_numqgrp; /* wrap */
1N/A if (bitset(RWG_RUNALL, flags) ||
1N/A (Queue[qgrp]->qg_nextrun <= now &&
1N/A Queue[qgrp]->qg_nextrun != (time_t) -1))
1N/A break;
1N/A if (endgrp == WorkGrp[wgrp].wg_curqgrp)
1N/A {
1N/A e->e_id = NULL;
1N/A if (bitset(RWG_FORK, flags))
1N/A finis(true, true, ExitStat);
1N/A return true; /* we're done */
1N/A }
1N/A }
1N/A
1N/A qdir = Queue[qgrp]->qg_curnum; /* round-robin init of queue position */
1N/A#if _FFR_QUEUE_SCHED_DBG
1N/A if (tTd(69, 12))
1N/A sm_syslog(LOG_INFO, NOQID,
1N/A "rwg: wgrp=%d, qgrp=%d, qdir=%d, name=%s, curqgrp=%d, numgrps=%d",
1N/A wgrp, qgrp, qdir, qid_printqueue(qgrp, qdir),
1N/A WorkGrp[wgrp].wg_curqgrp, WorkGrp[wgrp].wg_numqgrp);
1N/A#endif /* _FFR_QUEUE_SCHED_DBG */
1N/A
1N/A#if HASNICE
1N/A /* tweak niceness of queue runs */
1N/A if (Queue[qgrp]->qg_nice > 0)
1N/A (void) nice(Queue[qgrp]->qg_nice);
1N/A#endif /* HASNICE */
1N/A
1N/A /* XXX running queue group... */
1N/A sm_setproctitle(true, CurEnv, "running queue: %s",
1N/A qid_printqueue(qgrp, qdir));
1N/A
1N/A if (LogLevel > 69 || tTd(63, 99))
1N/A sm_syslog(LOG_DEBUG, NOQID,
1N/A "runqueue %s, pid=%d, forkflag=%d",
1N/A qid_printqueue(qgrp, qdir), (int) CurrentPid,
1N/A bitset(RWG_FORK, flags));
1N/A
1N/A /*
1N/A ** Start making passes through the queue.
1N/A ** First, read and sort the entire queue.
1N/A ** Then, process the work in that order.
1N/A ** But if you take too long, start over.
1N/A */
1N/A
1N/A for (i = 0; i < Queue[qgrp]->qg_numqueues; i++)
1N/A {
1N/A (void) gatherq(qgrp, qdir, false, &full, &more, &h);
1N/A#if SM_CONF_SHM
1N/A if (ShmId != SM_SHM_NO_ID)
1N/A QSHM_ENTRIES(Queue[qgrp]->qg_qpaths[qdir].qp_idx) = h;
1N/A#endif /* SM_CONF_SHM */
1N/A /* If there are no more items in this queue advance */
1N/A if (!more)
1N/A {
1N/A /* A round-robin advance */
1N/A qdir++;
1N/A qdir %= Queue[qgrp]->qg_numqueues;
1N/A }
1N/A
1N/A /* Has the WorkList reached the limit? */
1N/A if (full)
1N/A break; /* don't try to gather more */
1N/A }
1N/A
1N/A /* order the existing work requests */
1N/A njobs = sortq(Queue[qgrp]->qg_maxlist);
1N/A Queue[qgrp]->qg_curnum = qdir; /* update */
1N/A
1N/A
1N/A if (!Verbose && bitnset(QD_FORK, Queue[qgrp]->qg_flags))
1N/A {
1N/A int loop, maxrunners;
1N/A pid_t pid;
1N/A
1N/A /*
1N/A ** For this WorkQ we want to fork off N children (maxrunners)
1N/A ** at this point. Each child has a copy of WorkQ. Each child
1N/A ** will process every N-th item. The parent will wait for all
1N/A ** of the children to finish before moving on to the next
1N/A ** queue group within the work group. This saves us forking
1N/A ** a new runner-child for each work item.
1N/A ** It's valid for qg_maxqrun == 0 since this may be an
1N/A ** explicit "don't run this queue" setting.
1N/A */
1N/A
1N/A maxrunners = Queue[qgrp]->qg_maxqrun;
1N/A
1N/A /*
1N/A ** If no runners are configured for this group but
1N/A ** the queue is "forced" then lets use 1 runner.
1N/A */
1N/A
1N/A if (maxrunners == 0 && bitset(RWG_FORCE, flags))
1N/A maxrunners = 1;
1N/A
1N/A /* No need to have more runners then there are jobs */
1N/A if (maxrunners > njobs)
1N/A maxrunners = njobs;
1N/A for (loop = 0; loop < maxrunners; loop++)
1N/A {
1N/A /*
1N/A ** Since the delivery may happen in a child and the
1N/A ** parent does not wait, the parent may close the
1N/A ** maps thereby removing any shared memory used by
1N/A ** the map. Therefore, close the maps now so the
1N/A ** child will dynamically open them if necessary.
1N/A */
1N/A
1N/A closemaps(false);
1N/A
1N/A pid = fork();
1N/A if (pid < 0)
1N/A {
1N/A syserr("run_work_group: cannot fork");
1N/A return false;
1N/A }
1N/A else if (pid > 0)
1N/A {
1N/A /* parent -- clean out connection cache */
1N/A mci_flush(false, NULL);
1N/A#if _FFR_SKIP_DOMAINS
1N/A if (QueueSortOrder == QSO_BYHOST)
1N/A {
1N/A sequenceno += skip_domains(1);
1N/A }
1N/A else
1N/A#endif /* _FFR_SKIP_DOMAINS */
1N/A {
1N/A /* for the skip */
1N/A WorkQ = WorkQ->w_next;
1N/A sequenceno++;
1N/A }
1N/A proc_list_add(pid, "Queue child runner process",
1N/A PROC_QUEUE_CHILD, 0, -1, NULL);
1N/A
1N/A /* No additional work, no additional runners */
1N/A if (WorkQ == NULL)
1N/A break;
1N/A }
1N/A else
1N/A {
1N/A /* child -- Reset global flags */
1N/A RestartRequest = NULL;
1N/A RestartWorkGroup = false;
1N/A ShutdownRequest = NULL;
1N/A PendingSignal = 0;
1N/A CurrentPid = getpid();
1N/A close_sendmail_pid();
1N/A
1N/A /*
1N/A ** Initialize exception stack and default
1N/A ** exception handler for child process.
1N/A ** When fork()'d the child now has a private
1N/A ** copy of WorkQ at its current position.
1N/A */
1N/A
1N/A sm_exc_newthread(fatal_error);
1N/A
1N/A /*
1N/A ** SMTP processes (whether -bd or -bs) set
1N/A ** SIGCHLD to reapchild to collect
1N/A ** children status. However, at delivery
1N/A ** time, that status must be collected
1N/A ** by sm_wait() to be dealt with properly
1N/A ** (check success of delivery based
1N/A ** on status code, etc). Therefore, if we
1N/A ** are an SMTP process, reset SIGCHLD
1N/A ** back to the default so reapchild
1N/A ** doesn't collect status before
1N/A ** sm_wait().
1N/A */
1N/A
1N/A if (OpMode == MD_SMTP ||
1N/A OpMode == MD_DAEMON ||
1N/A MaxQueueChildren > 0)
1N/A {
1N/A proc_list_clear();
1N/A sm_releasesignal(SIGCHLD);
1N/A (void) sm_signal(SIGCHLD, SIG_DFL);
1N/A }
1N/A
1N/A /* child -- error messages to the transcript */
1N/A QuickAbort = OnlyOneError = false;
1N/A runner_work(e, sequenceno, true,
1N/A maxrunners, njobs);
1N/A
1N/A /* This child is done */
1N/A finis(true, true, ExitStat);
1N/A /* NOTREACHED */
1N/A }
1N/A }
1N/A
1N/A sm_releasesignal(SIGCHLD);
1N/A
1N/A /*
1N/A ** Wait until all of the runners have completed before
1N/A ** seeing if there is another queue group in the
1N/A ** work group to process.
1N/A ** XXX Future enhancement: don't wait() for all children
1N/A ** here, just go ahead and make sure that overall the number
1N/A ** of children is not exceeded.
1N/A */
1N/A
1N/A while (CurChildren > 0)
1N/A {
1N/A int status;
1N/A pid_t ret;
1N/A
1N/A while ((ret = sm_wait(&status)) <= 0)
1N/A continue;
1N/A proc_list_drop(ret, status, NULL);
1N/A }
1N/A }
1N/A else if (Queue[qgrp]->qg_maxqrun > 0 || bitset(RWG_FORCE, flags))
1N/A {
1N/A /*
1N/A ** When current process will not fork children to do the work,
1N/A ** it will do the work itself. The 'skip' will be 1 since
1N/A ** there are no child runners to divide the work across.
1N/A */
1N/A
1N/A runner_work(e, sequenceno, false, 1, njobs);
1N/A }
1N/A
1N/A /* free memory allocated by newenvelope() above */
1N/A sm_rpool_free(rpool);
1N/A QueueEnvelope.e_rpool = NULL;
1N/A
1N/A /* Are there still more queues in the work group to process? */
1N/A if (endgrp != WorkGrp[wgrp].wg_curqgrp)
1N/A {
1N/A rpool = sm_rpool_new_x(NULL);
1N/A e = newenvelope(&QueueEnvelope, CurEnv, rpool);
1N/A e->e_flags = BlankEnvelope.e_flags;
1N/A goto domorework;
1N/A }
1N/A
1N/A /* No more queues in work group to process. Now check persistent. */
1N/A if (bitset(RWG_PERSISTENT, flags))
1N/A {
1N/A sequenceno = 1;
1N/A sm_setproctitle(true, CurEnv, "running queue: %s",
1N/A qid_printqueue(qgrp, qdir));
1N/A
1N/A /*
1N/A ** close bogus maps, i.e., maps which caused a tempfail,
1N/A ** so we get fresh map connections on the next lookup.
1N/A ** closemaps() is also called when children are started.
1N/A */
1N/A
1N/A closemaps(true);
1N/A
1N/A /* Close any cached connections. */
1N/A mci_flush(true, NULL);
1N/A
1N/A /* Clean out expired related entries. */
1N/A rmexpstab();
1N/A
1N/A#if NAMED_BIND
1N/A /* Update MX records for FallbackMX. */
1N/A if (FallbackMX != NULL)
1N/A (void) getfallbackmxrr(FallbackMX);
1N/A#endif /* NAMED_BIND */
1N/A
1N/A#if USERDB
1N/A /* close UserDatabase */
1N/A _udbx_close();
1N/A#endif /* USERDB */
1N/A
1N/A#if SM_HEAP_CHECK
1N/A if (sm_debug_active(&SmHeapCheck, 2)
1N/A && access("memdump", F_OK) == 0
1N/A )
1N/A {
1N/A SM_FILE_T *out;
1N/A
1N/A remove("memdump");
1N/A out = sm_io_open(SmFtStdio, SM_TIME_DEFAULT,
1N/A "memdump.out", SM_IO_APPEND, NULL);
1N/A if (out != NULL)
1N/A {
1N/A (void) sm_io_fprintf(out, SM_TIME_DEFAULT, "----------------------\n");
1N/A sm_heap_report(out,
1N/A sm_debug_level(&SmHeapCheck) - 1);
1N/A (void) sm_io_close(out, SM_TIME_DEFAULT);
1N/A }
1N/A }
1N/A#endif /* SM_HEAP_CHECK */
1N/A
1N/A /* let me rest for a second to catch my breath */
1N/A if (njobs == 0 && WorkGrp[wgrp].wg_lowqintvl < MIN_SLEEP_TIME)
1N/A sleep(MIN_SLEEP_TIME);
1N/A else if (WorkGrp[wgrp].wg_lowqintvl <= 0)
1N/A sleep(QueueIntvl > 0 ? QueueIntvl : MIN_SLEEP_TIME);
1N/A else
1N/A sleep(WorkGrp[wgrp].wg_lowqintvl);
1N/A
1N/A /*
1N/A ** Get the LA outside the WorkQ loop if necessary.
1N/A ** In a persistent queue runner the code is repeated over
1N/A ** and over but gatherq() may ignore entries due to
1N/A ** shouldqueue() (do we really have to do this twice?).
1N/A ** Hence the queue runners would just idle around when once
1N/A ** CurrentLA caused all entries in a queue to be ignored.
1N/A */
1N/A
1N/A if (njobs == 0)
1N/A SM_GET_LA(now);
1N/A rpool = sm_rpool_new_x(NULL);
1N/A e = newenvelope(&QueueEnvelope, CurEnv, rpool);
1N/A e->e_flags = BlankEnvelope.e_flags;
1N/A goto domorework;
1N/A }
1N/A
1N/A /* exit without the usual cleanup */
1N/A e->e_id = NULL;
1N/A if (bitset(RWG_FORK, flags))
1N/A finis(true, true, ExitStat);
1N/A /* NOTREACHED */
1N/A return true;
1N/A}
1N/A
1N/A/*
1N/A** DOQUEUERUN -- do a queue run?
1N/A*/
1N/A
1N/Abool
1N/Adoqueuerun()
1N/A{
1N/A return DoQueueRun;
1N/A}
1N/A
1N/A/*
1N/A** RUNQUEUEEVENT -- Sets a flag to indicate that a queue run should be done.
1N/A**
1N/A** Parameters:
1N/A** none.
1N/A**
1N/A** Returns:
1N/A** none.
1N/A**
1N/A** Side Effects:
1N/A** The invocation of this function via an alarm may interrupt
1N/A** a set of actions. Thus errno may be set in that context.
1N/A** We need to restore errno at the end of this function to ensure
1N/A** that any work done here that sets errno doesn't return a
1N/A** misleading/false errno value. Errno may be EINTR upon entry to
1N/A** this function because of non-restartable/continuable system
1N/A** API was active. Iff this is true we will override errno as
1N/A** a timeout (as a more accurate error message).
1N/A**
1N/A** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
1N/A** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
1N/A** DOING.
1N/A*/
1N/A
1N/Avoid
1N/Arunqueueevent(ignore)
1N/A int ignore;
1N/A{
1N/A int save_errno = errno;
1N/A
1N/A /*
1N/A ** Set the general bit that we want a queue run,
1N/A ** tested in doqueuerun()
1N/A */
1N/A
1N/A DoQueueRun = true;
1N/A#if _FFR_QUEUE_SCHED_DBG
1N/A if (tTd(69, 10))
1N/A sm_syslog(LOG_INFO, NOQID, "rqe: done");
1N/A#endif /* _FFR_QUEUE_SCHED_DBG */
1N/A
1N/A errno = save_errno;
1N/A if (errno == EINTR)
1N/A errno = ETIMEDOUT;
1N/A}
1N/A/*
1N/A** GATHERQ -- gather messages from the message queue(s) the work queue.
1N/A**
1N/A** Parameters:
1N/A** qgrp -- the index of the queue group.
1N/A** qdir -- the index of the queue directory.
1N/A** doall -- if set, include everything in the queue (even
1N/A** the jobs that cannot be run because the load
1N/A** average is too high, or MaxQueueRun is reached).
1N/A** Otherwise, exclude those jobs.
1N/A** full -- (optional) to be set 'true' if WorkList is full
1N/A** more -- (optional) to be set 'true' if there are still more
1N/A** messages in this queue not added to WorkList
1N/A** pnentries -- (optional) total nuber of entries in queue
1N/A**
1N/A** Returns:
1N/A** The number of request in the queue (not necessarily
1N/A** the number of requests in WorkList however).
1N/A**
1N/A** Side Effects:
1N/A** prepares available work into WorkList
1N/A*/
1N/A
1N/A#define NEED_P 0001 /* 'P': priority */
1N/A#define NEED_T 0002 /* 'T': time */
1N/A#define NEED_R 0004 /* 'R': recipient */
1N/A#define NEED_S 0010 /* 'S': sender */
1N/A#define NEED_H 0020 /* host */
1N/A#define HAS_QUARANTINE 0040 /* has an unexpected 'q' line */
1N/A#define NEED_QUARANTINE 0100 /* 'q': reason */
1N/A
1N/Astatic WORK *WorkList = NULL; /* list of unsort work */
1N/Astatic int WorkListSize = 0; /* current max size of WorkList */
1N/Astatic int WorkListCount = 0; /* # of work items in WorkList */
1N/A
1N/Astatic int
1N/Agatherq(qgrp, qdir, doall, full, more, pnentries)
1N/A int qgrp;
1N/A int qdir;
1N/A bool doall;
1N/A bool *full;
1N/A bool *more;
1N/A int *pnentries;
1N/A{
1N/A register struct dirent *d;
1N/A register WORK *w;
1N/A register char *p;
1N/A DIR *f;
1N/A int i, num_ent, wn, nentries;
1N/A QUEUE_CHAR *check;
1N/A char qd[MAXPATHLEN];
1N/A char qf[MAXPATHLEN];
1N/A
1N/A wn = WorkListCount - 1;
1N/A num_ent = 0;
1N/A nentries = 0;
1N/A if (qdir == NOQDIR)
1N/A (void) sm_strlcpy(qd, ".", sizeof(qd));
1N/A else
1N/A (void) sm_strlcpyn(qd, sizeof(qd), 2,
1N/A Queue[qgrp]->qg_qpaths[qdir].qp_name,
1N/A (bitset(QP_SUBQF,
1N/A Queue[qgrp]->qg_qpaths[qdir].qp_subdirs)
1N/A ? "/qf" : ""));
1N/A
1N/A if (tTd(41, 1))
1N/A {
1N/A sm_dprintf("gatherq:\n");
1N/A
1N/A check = QueueLimitId;
1N/A while (check != NULL)
1N/A {
1N/A sm_dprintf("\tQueueLimitId = %s%s\n",
1N/A check->queue_negate ? "!" : "",
1N/A check->queue_match);
1N/A check = check->queue_next;
1N/A }
1N/A
1N/A check = QueueLimitSender;
1N/A while (check != NULL)
1N/A {
1N/A sm_dprintf("\tQueueLimitSender = %s%s\n",
1N/A check->queue_negate ? "!" : "",
1N/A check->queue_match);
1N/A check = check->queue_next;
1N/A }
1N/A
1N/A check = QueueLimitRecipient;
1N/A while (check != NULL)
1N/A {
1N/A sm_dprintf("\tQueueLimitRecipient = %s%s\n",
1N/A check->queue_negate ? "!" : "",
1N/A check->queue_match);
1N/A check = check->queue_next;
1N/A }
1N/A
1N/A if (QueueMode == QM_QUARANTINE)
1N/A {
1N/A check = QueueLimitQuarantine;
1N/A while (check != NULL)
1N/A {
1N/A sm_dprintf("\tQueueLimitQuarantine = %s%s\n",
1N/A check->queue_negate ? "!" : "",
1N/A check->queue_match);
1N/A check = check->queue_next;
1N/A }
1N/A }
1N/A }
1N/A
1N/A /* open the queue directory */
1N/A f = opendir(qd);
1N/A if (f == NULL)
1N/A {
1N/A syserr("gatherq: cannot open \"%s\"",
1N/A qid_printqueue(qgrp, qdir));
1N/A if (full != NULL)
1N/A *full = WorkListCount >= MaxQueueRun && MaxQueueRun > 0;
1N/A if (more != NULL)
1N/A *more = false;
1N/A return 0;
1N/A }
1N/A
1N/A /*
1N/A ** Read the work directory.
1N/A */
1N/A
1N/A while ((d = readdir(f)) != NULL)
1N/A {
1N/A SM_FILE_T *cf;
1N/A int qfver = 0;
1N/A char lbuf[MAXNAME + 1];
1N/A struct stat sbuf;
1N/A
1N/A if (tTd(41, 50))
1N/A sm_dprintf("gatherq: checking %s..", d->d_name);
1N/A
1N/A /* is this an interesting entry? */
1N/A if (!(((QueueMode == QM_NORMAL &&
1N/A d->d_name[0] == NORMQF_LETTER) ||
1N/A (QueueMode == QM_QUARANTINE &&
1N/A d->d_name[0] == QUARQF_LETTER) ||
1N/A (QueueMode == QM_LOST &&
1N/A d->d_name[0] == LOSEQF_LETTER)) &&
1N/A d->d_name[1] == 'f'))
1N/A {
1N/A if (tTd(41, 50))
1N/A sm_dprintf(" skipping\n");
1N/A continue;
1N/A }
1N/A if (tTd(41, 50))
1N/A sm_dprintf("\n");
1N/A
1N/A if (strlen(d->d_name) >= MAXQFNAME)
1N/A {
1N/A if (Verbose)
1N/A (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
1N/A "gatherq: %s too long, %d max characters\n",
1N/A d->d_name, MAXQFNAME);
1N/A if (LogLevel > 0)
1N/A sm_syslog(LOG_ALERT, NOQID,
1N/A "gatherq: %s too long, %d max characters",
1N/A d->d_name, MAXQFNAME);
1N/A continue;
1N/A }
1N/A
1N/A ++nentries;
1N/A check = QueueLimitId;
1N/A while (check != NULL)
1N/A {
1N/A if (strcontainedin(false, check->queue_match,
1N/A d->d_name) != check->queue_negate)
1N/A break;
1N/A else
1N/A check = check->queue_next;
1N/A }
1N/A if (QueueLimitId != NULL && check == NULL)
1N/A continue;
1N/A
1N/A /* grow work list if necessary */
1N/A if (++wn >= MaxQueueRun && MaxQueueRun > 0)
1N/A {
1N/A if (wn == MaxQueueRun && LogLevel > 0)
1N/A sm_syslog(LOG_WARNING, NOQID,
1N/A "WorkList for %s maxed out at %d",
1N/A qid_printqueue(qgrp, qdir),
1N/A MaxQueueRun);
1N/A if (doall)
1N/A continue; /* just count entries */
1N/A break;
1N/A }
1N/A if (wn >= WorkListSize)
1N/A {
1N/A grow_wlist(qgrp, qdir);
1N/A if (wn >= WorkListSize)
1N/A continue;
1N/A }
1N/A SM_ASSERT(wn >= 0);
1N/A w = &WorkList[wn];
1N/A
1N/A (void) sm_strlcpyn(qf, sizeof(qf), 3, qd, "/", d->d_name);
1N/A if (stat(qf, &sbuf) < 0)
1N/A {
1N/A if (errno != ENOENT)
1N/A sm_syslog(LOG_INFO, NOQID,
1N/A "gatherq: can't stat %s/%s",
1N/A qid_printqueue(qgrp, qdir),
1N/A d->d_name);
1N/A wn--;
1N/A continue;
1N/A }
1N/A if (!bitset(S_IFREG, sbuf.st_mode))
1N/A {
1N/A /* Yikes! Skip it or we will hang on open! */
1N/A if (!((d->d_name[0] == DATAFL_LETTER ||
1N/A d->d_name[0] == NORMQF_LETTER ||
1N/A d->d_name[0] == QUARQF_LETTER ||
1N/A d->d_name[0] == LOSEQF_LETTER ||
1N/A d->d_name[0] == XSCRPT_LETTER) &&
1N/A d->d_name[1] == 'f' && d->d_name[2] == '\0'))
1N/A syserr("gatherq: %s/%s is not a regular file",
1N/A qid_printqueue(qgrp, qdir), d->d_name);
1N/A wn--;
1N/A continue;
1N/A }
1N/A
1N/A /* avoid work if possible */
1N/A if ((QueueSortOrder == QSO_BYFILENAME ||
1N/A QueueSortOrder == QSO_BYMODTIME ||
1N/A QueueSortOrder == QSO_NONE ||
1N/A QueueSortOrder == QSO_RANDOM) &&
1N/A QueueLimitQuarantine == NULL &&
1N/A QueueLimitSender == NULL &&
1N/A QueueLimitRecipient == NULL)
1N/A {
1N/A w->w_qgrp = qgrp;
1N/A w->w_qdir = qdir;
1N/A w->w_name = newstr(d->d_name);
1N/A w->w_host = NULL;
1N/A w->w_lock = w->w_tooyoung = false;
1N/A w->w_pri = 0;
1N/A w->w_ctime = 0;
1N/A w->w_mtime = sbuf.st_mtime;
1N/A ++num_ent;
1N/A continue;
1N/A }
1N/A
1N/A /* open control file */
1N/A cf = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, qf, SM_IO_RDONLY_B,
1N/A NULL);
1N/A if (cf == NULL && OpMode != MD_PRINT)
1N/A {
1N/A /* this may be some random person sending hir msgs */
1N/A if (tTd(41, 2))
1N/A sm_dprintf("gatherq: cannot open %s: %s\n",
1N/A d->d_name, sm_errstring(errno));
1N/A errno = 0;
1N/A wn--;
1N/A continue;
1N/A }
1N/A w->w_qgrp = qgrp;
1N/A w->w_qdir = qdir;
1N/A w->w_name = newstr(d->d_name);
1N/A w->w_host = NULL;
1N/A if (cf != NULL)
1N/A {
1N/A w->w_lock = !lockfile(sm_io_getinfo(cf, SM_IO_WHAT_FD,
1N/A NULL),
1N/A w->w_name, NULL,
1N/A LOCK_SH|LOCK_NB);
1N/A }
1N/A w->w_tooyoung = false;
1N/A
1N/A /* make sure jobs in creation don't clog queue */
1N/A w->w_pri = 0x7fffffff;
1N/A w->w_ctime = 0;
1N/A w->w_mtime = sbuf.st_mtime;
1N/A
1N/A /* extract useful information */
1N/A i = NEED_P|NEED_T;
1N/A if (QueueSortOrder == QSO_BYHOST
1N/A#if _FFR_RHS
1N/A || QueueSortOrder == QSO_BYSHUFFLE
1N/A#endif /* _FFR_RHS */
1N/A )
1N/A {
1N/A /* need w_host set for host sort order */
1N/A i |= NEED_H;
1N/A }
1N/A if (QueueLimitSender != NULL)
1N/A i |= NEED_S;
1N/A if (QueueLimitRecipient != NULL)
1N/A i |= NEED_R;
1N/A if (QueueLimitQuarantine != NULL)
1N/A i |= NEED_QUARANTINE;
1N/A while (cf != NULL && i != 0 &&
1N/A sm_io_fgets(cf, SM_TIME_DEFAULT, lbuf,
1N/A sizeof(lbuf)) != NULL)
1N/A {
1N/A int c;
1N/A time_t age;
1N/A
1N/A p = strchr(lbuf, '\n');
1N/A if (p != NULL)
1N/A *p = '\0';
1N/A else
1N/A {
1N/A /* flush rest of overly long line */
1N/A while ((c = sm_io_getc(cf, SM_TIME_DEFAULT))
1N/A != SM_IO_EOF && c != '\n')
1N/A continue;
1N/A }
1N/A
1N/A switch (lbuf[0])
1N/A {
1N/A case 'V':
1N/A qfver = atoi(&lbuf[1]);
1N/A break;
1N/A
1N/A case 'P':
1N/A w->w_pri = atol(&lbuf[1]);
1N/A i &= ~NEED_P;
1N/A break;
1N/A
1N/A case 'T':
1N/A w->w_ctime = atol(&lbuf[1]);
1N/A i &= ~NEED_T;
1N/A break;
1N/A
1N/A case 'q':
1N/A if (QueueMode != QM_QUARANTINE &&
1N/A QueueMode != QM_LOST)
1N/A {
1N/A if (tTd(41, 49))
1N/A sm_dprintf("%s not marked as quarantined but has a 'q' line\n",
1N/A w->w_name);
1N/A i |= HAS_QUARANTINE;
1N/A }
1N/A else if (QueueMode == QM_QUARANTINE)
1N/A {
1N/A if (QueueLimitQuarantine == NULL)
1N/A {
1N/A i &= ~NEED_QUARANTINE;
1N/A break;
1N/A }
1N/A p = &lbuf[1];
1N/A check = QueueLimitQuarantine;
1N/A while (check != NULL)
1N/A {
1N/A if (strcontainedin(false,
1N/A check->queue_match,
1N/A p) !=
1N/A check->queue_negate)
1N/A break;
1N/A else
1N/A check = check->queue_next;
1N/A }
1N/A if (check != NULL)
1N/A i &= ~NEED_QUARANTINE;
1N/A }
1N/A break;
1N/A
1N/A case 'R':
1N/A if (w->w_host == NULL &&
1N/A (p = strrchr(&lbuf[1], '@')) != NULL)
1N/A {
1N/A#if _FFR_RHS
1N/A if (QueueSortOrder == QSO_BYSHUFFLE)
1N/A w->w_host = newstr(&p[1]);
1N/A else
1N/A#endif /* _FFR_RHS */
1N/A w->w_host = strrev(&p[1]);
1N/A makelower(w->w_host);
1N/A i &= ~NEED_H;
1N/A }
1N/A if (QueueLimitRecipient == NULL)
1N/A {
1N/A i &= ~NEED_R;
1N/A break;
1N/A }
1N/A if (qfver > 0)
1N/A {
1N/A p = strchr(&lbuf[1], ':');
1N/A if (p == NULL)
1N/A p = &lbuf[1];
1N/A else
1N/A ++p; /* skip over ':' */
1N/A }
1N/A else
1N/A p = &lbuf[1];
1N/A check = QueueLimitRecipient;
1N/A while (check != NULL)
1N/A {
1N/A if (strcontainedin(true,
1N/A check->queue_match,
1N/A p) !=
1N/A check->queue_negate)
1N/A break;
1N/A else
1N/A check = check->queue_next;
1N/A }
1N/A if (check != NULL)
1N/A i &= ~NEED_R;
1N/A break;
1N/A
1N/A case 'S':
1N/A check = QueueLimitSender;
1N/A while (check != NULL)
1N/A {
1N/A if (strcontainedin(true,
1N/A check->queue_match,
1N/A &lbuf[1]) !=
1N/A check->queue_negate)
1N/A break;
1N/A else
1N/A check = check->queue_next;
1N/A }
1N/A if (check != NULL)
1N/A i &= ~NEED_S;
1N/A break;
1N/A
1N/A case 'K':
1N/A#if _FFR_EXPDELAY
1N/A if (MaxQueueAge > 0)
1N/A {
1N/A time_t lasttry, delay;
1N/A
1N/A lasttry = (time_t) atol(&lbuf[1]);
1N/A delay = MIN(lasttry - w->w_ctime,
1N/A MaxQueueAge);
1N/A age = curtime() - lasttry;
1N/A if (age < delay)
1N/A w->w_tooyoung = true;
1N/A break;
1N/A }
1N/A#endif /* _FFR_EXPDELAY */
1N/A
1N/A age = curtime() - (time_t) atol(&lbuf[1]);
1N/A if (age >= 0 && MinQueueAge > 0 &&
1N/A age < MinQueueAge)
1N/A w->w_tooyoung = true;
1N/A break;
1N/A
1N/A case 'N':
1N/A if (atol(&lbuf[1]) == 0)
1N/A w->w_tooyoung = false;
1N/A break;
1N/A }
1N/A }
1N/A if (cf != NULL)
1N/A (void) sm_io_close(cf, SM_TIME_DEFAULT);
1N/A
1N/A if ((!doall && (shouldqueue(w->w_pri, w->w_ctime) ||
1N/A w->w_tooyoung)) ||
1N/A bitset(HAS_QUARANTINE, i) ||
1N/A bitset(NEED_QUARANTINE, i) ||
1N/A bitset(NEED_R|NEED_S, i))
1N/A {
1N/A /* don't even bother sorting this job in */
1N/A if (tTd(41, 49))
1N/A sm_dprintf("skipping %s (%x)\n", w->w_name, i);
1N/A sm_free(w->w_name); /* XXX */
1N/A if (w->w_host != NULL)
1N/A sm_free(w->w_host); /* XXX */
1N/A wn--;
1N/A }
1N/A else
1N/A ++num_ent;
1N/A }
1N/A (void) closedir(f);
1N/A wn++;
1N/A
1N/A i = wn - WorkListCount;
1N/A WorkListCount += SM_MIN(num_ent, WorkListSize);
1N/A
1N/A if (more != NULL)
1N/A *more = WorkListCount < wn;
1N/A
1N/A if (full != NULL)
1N/A *full = (wn >= MaxQueueRun && MaxQueueRun > 0) ||
1N/A (WorkList == NULL && wn > 0);
1N/A
1N/A if (pnentries != NULL)
1N/A *pnentries = nentries;
1N/A return i;
1N/A}
1N/A/*
1N/A** SORTQ -- sort the work list
1N/A**
1N/A** First the old WorkQ is cleared away. Then the WorkList is sorted
1N/A** for all items so that important (higher sorting value) items are not
1N/A** trunctated off. Then the most important items are moved from
1N/A** WorkList to WorkQ. The lower count of 'max' or MaxListCount items
1N/A** are moved.
1N/A**
1N/A** Parameters:
1N/A** max -- maximum number of items to be placed in WorkQ
1N/A**
1N/A** Returns:
1N/A** the number of items in WorkQ
1N/A**
1N/A** Side Effects:
1N/A** WorkQ gets released and filled with new work. WorkList
1N/A** gets released. Work items get sorted in order.
1N/A*/
1N/A
1N/Astatic int
1N/Asortq(max)
1N/A int max;
1N/A{
1N/A register int i; /* local counter */
1N/A register WORK *w; /* tmp item pointer */
1N/A int wc = WorkListCount; /* trim size for WorkQ */
1N/A
1N/A if (WorkQ != NULL)
1N/A {
1N/A WORK *nw;
1N/A
1N/A /* Clear out old WorkQ. */
1N/A for (w = WorkQ; w != NULL; w = nw)
1N/A {
1N/A nw = w->w_next;
1N/A sm_free(w->w_name); /* XXX */
1N/A if (w->w_host != NULL)
1N/A sm_free(w->w_host); /* XXX */
1N/A sm_free((char *) w); /* XXX */
1N/A }
1N/A WorkQ = NULL;
1N/A }
1N/A
1N/A if (WorkList == NULL || wc <= 0)
1N/A return 0;
1N/A
1N/A /*
1N/A ** The sort now takes place using all of the items in WorkList.
1N/A ** The list gets trimmed to the most important items after the sort.
1N/A ** If the trim were to happen before the sort then one or more
1N/A ** important items might get truncated off -- not what we want.
1N/A */
1N/A
1N/A if (QueueSortOrder == QSO_BYHOST)
1N/A {
1N/A /*
1N/A ** Sort the work directory for the first time,
1N/A ** based on host name, lock status, and priority.
1N/A */
1N/A
1N/A qsort((char *) WorkList, wc, sizeof(*WorkList), workcmpf1);
1N/A
1N/A /*
1N/A ** If one message to host is locked, "lock" all messages
1N/A ** to that host.
1N/A */
1N/A
1N/A i = 0;
1N/A while (i < wc)
1N/A {
1N/A if (!WorkList[i].w_lock)
1N/A {
1N/A i++;
1N/A continue;
1N/A }
1N/A w = &WorkList[i];
1N/A while (++i < wc)
1N/A {
1N/A if (WorkList[i].w_host == NULL &&
1N/A w->w_host == NULL)
1N/A WorkList[i].w_lock = true;
1N/A else if (WorkList[i].w_host != NULL &&
1N/A w->w_host != NULL &&
1N/A sm_strcasecmp(WorkList[i].w_host,
1N/A w->w_host) == 0)
1N/A WorkList[i].w_lock = true;
1N/A else
1N/A break;
1N/A }
1N/A }
1N/A
1N/A /*
1N/A ** Sort the work directory for the second time,
1N/A ** based on lock status, host name, and priority.
1N/A */
1N/A
1N/A qsort((char *) WorkList, wc, sizeof(*WorkList), workcmpf2);
1N/A }
1N/A else if (QueueSortOrder == QSO_BYTIME)
1N/A {
1N/A /*
1N/A ** Simple sort based on submission time only.
1N/A */
1N/A
1N/A qsort((char *) WorkList, wc, sizeof(*WorkList), workcmpf3);
1N/A }
1N/A else if (QueueSortOrder == QSO_BYFILENAME)
1N/A {
1N/A /*
1N/A ** Sort based on queue filename.
1N/A */
1N/A
1N/A qsort((char *) WorkList, wc, sizeof(*WorkList), workcmpf4);
1N/A }
1N/A else if (QueueSortOrder == QSO_RANDOM)
1N/A {
1N/A /*
1N/A ** Sort randomly. To avoid problems with an instable sort,
1N/A ** use a random index into the queue file name to start
1N/A ** comparison.
1N/A */
1N/A
1N/A randi = get_rand_mod(MAXQFNAME);
1N/A if (randi < 2)
1N/A randi = 3;
1N/A qsort((char *) WorkList, wc, sizeof(*WorkList), workcmpf5);
1N/A }
1N/A else if (QueueSortOrder == QSO_BYMODTIME)
1N/A {
1N/A /*
1N/A ** Simple sort based on modification time of queue file.
1N/A ** This puts the oldest items first.
1N/A */
1N/A
1N/A qsort((char *) WorkList, wc, sizeof(*WorkList), workcmpf6);
1N/A }
1N/A#if _FFR_RHS
1N/A else if (QueueSortOrder == QSO_BYSHUFFLE)
1N/A {
1N/A /*
1N/A ** Simple sort based on shuffled host name.
1N/A */
1N/A
1N/A init_shuffle_alphabet();
1N/A qsort((char *) WorkList, wc, sizeof(*WorkList), workcmpf7);
1N/A }
1N/A#endif /* _FFR_RHS */
1N/A else if (QueueSortOrder == QSO_BYPRIORITY)
1N/A {
1N/A /*
1N/A ** Simple sort based on queue priority only.
1N/A */
1N/A
1N/A qsort((char *) WorkList, wc, sizeof(*WorkList), workcmpf0);
1N/A }
1N/A /* else don't sort at all */
1N/A
1N/A /* Check if the per queue group item limit will be exceeded */
1N/A if (wc > max && max > 0)
1N/A wc = max;
1N/A
1N/A /*
1N/A ** Convert the work list into canonical form.
1N/A ** Should be turning it into a list of envelopes here perhaps.
1N/A ** Only take the most important items up to the per queue group
1N/A ** maximum.
1N/A */
1N/A
1N/A for (i = wc; --i >= 0; )
1N/A {
1N/A w = (WORK *) xalloc(sizeof(*w));
1N/A w->w_qgrp = WorkList[i].w_qgrp;
1N/A w->w_qdir = WorkList[i].w_qdir;
1N/A w->w_name = WorkList[i].w_name;
1N/A w->w_host = WorkList[i].w_host;
1N/A w->w_lock = WorkList[i].w_lock;
1N/A w->w_tooyoung = WorkList[i].w_tooyoung;
1N/A w->w_pri = WorkList[i].w_pri;
1N/A w->w_ctime = WorkList[i].w_ctime;
1N/A w->w_mtime = WorkList[i].w_mtime;
1N/A w->w_next = WorkQ;
1N/A WorkQ = w;
1N/A }
1N/A
1N/A /* free the rest of the list */
1N/A for (i = WorkListCount; --i >= wc; )
1N/A {
1N/A sm_free(WorkList[i].w_name);
1N/A if (WorkList[i].w_host != NULL)
1N/A sm_free(WorkList[i].w_host);
1N/A }
1N/A
1N/A if (WorkList != NULL)
1N/A sm_free(WorkList); /* XXX */
1N/A WorkList = NULL;
1N/A WorkListSize = 0;
1N/A WorkListCount = 0;
1N/A
1N/A if (tTd(40, 1))
1N/A {
1N/A for (w = WorkQ; w != NULL; w = w->w_next)
1N/A {
1N/A if (w->w_host != NULL)
1N/A sm_dprintf("%22s: pri=%ld %s\n",
1N/A w->w_name, w->w_pri, w->w_host);
1N/A else
1N/A sm_dprintf("%32s: pri=%ld\n",
1N/A w->w_name, w->w_pri);
1N/A }
1N/A }
1N/A
1N/A return wc; /* return number of WorkQ items */
1N/A}
1N/A/*
1N/A** GROW_WLIST -- make the work list larger
1N/A**
1N/A** Parameters:
1N/A** qgrp -- the index for the queue group.
1N/A** qdir -- the index for the queue directory.
1N/A**
1N/A** Returns:
1N/A** none.
1N/A**
1N/A** Side Effects:
1N/A** Adds another QUEUESEGSIZE entries to WorkList if possible.
1N/A** It can fail if there isn't enough memory, so WorkListSize
1N/A** should be checked again upon return.
1N/A*/
1N/A
1N/Astatic void
1N/Agrow_wlist(qgrp, qdir)
1N/A int qgrp;
1N/A int qdir;
1N/A{
1N/A if (tTd(41, 1))
1N/A sm_dprintf("grow_wlist: WorkListSize=%d\n", WorkListSize);
1N/A if (WorkList == NULL)
1N/A {
1N/A WorkList = (WORK *) xalloc((sizeof(*WorkList)) *
1N/A (QUEUESEGSIZE + 1));
1N/A WorkListSize = QUEUESEGSIZE;
1N/A }
1N/A else
1N/A {
1N/A int newsize = WorkListSize + QUEUESEGSIZE;
1N/A WORK *newlist = (WORK *) sm_realloc((char *) WorkList,
1N/A (unsigned) sizeof(WORK) * (newsize + 1));
1N/A
1N/A if (newlist != NULL)
1N/A {
1N/A WorkListSize = newsize;
1N/A WorkList = newlist;
1N/A if (LogLevel > 1)
1N/A {
1N/A sm_syslog(LOG_INFO, NOQID,
1N/A "grew WorkList for %s to %d",
1N/A qid_printqueue(qgrp, qdir),
1N/A WorkListSize);
1N/A }
1N/A }
1N/A else if (LogLevel > 0)
1N/A {
1N/A sm_syslog(LOG_ALERT, NOQID,
1N/A "FAILED to grow WorkList for %s to %d",
1N/A qid_printqueue(qgrp, qdir), newsize);
1N/A }
1N/A }
1N/A if (tTd(41, 1))
1N/A sm_dprintf("grow_wlist: WorkListSize now %d\n", WorkListSize);
1N/A}
1N/A/*
1N/A** WORKCMPF0 -- simple priority-only compare function.
1N/A**
1N/A** Parameters:
1N/A** a -- the first argument.
1N/A** b -- the second argument.
1N/A**
1N/A** Returns:
1N/A** -1 if a < b
1N/A** 0 if a == b
1N/A** +1 if a > b
1N/A**
1N/A*/
1N/A
1N/Astatic int
1N/Aworkcmpf0(a, b)
1N/A register WORK *a;
1N/A register WORK *b;
1N/A{
1N/A long pa = a->w_pri;
1N/A long pb = b->w_pri;
1N/A
1N/A if (pa == pb)
1N/A return 0;
1N/A else if (pa > pb)
1N/A return 1;
1N/A else
1N/A return -1;
1N/A}
1N/A/*
1N/A** WORKCMPF1 -- first compare function for ordering work based on host name.
1N/A**
1N/A** Sorts on host name, lock status, and priority in that order.
1N/A**
1N/A** Parameters:
1N/A** a -- the first argument.
1N/A** b -- the second argument.
1N/A**
1N/A** Returns:
1N/A** <0 if a < b
1N/A** 0 if a == b
1N/A** >0 if a > b
1N/A**
1N/A*/
1N/A
1N/Astatic int
1N/Aworkcmpf1(a, b)
1N/A register WORK *a;
1N/A register WORK *b;
1N/A{
1N/A int i;
1N/A
1N/A /* host name */
1N/A if (a->w_host != NULL && b->w_host == NULL)
1N/A return 1;
1N/A else if (a->w_host == NULL && b->w_host != NULL)
1N/A return -1;
1N/A if (a->w_host != NULL && b->w_host != NULL &&
1N/A (i = sm_strcasecmp(a->w_host, b->w_host)) != 0)
1N/A return i;
1N/A
1N/A /* lock status */
1N/A if (a->w_lock != b->w_lock)
1N/A return b->w_lock - a->w_lock;
1N/A
1N/A /* job priority */
1N/A return workcmpf0(a, b);
1N/A}
1N/A/*
1N/A** WORKCMPF2 -- second compare function for ordering work based on host name.
1N/A**
1N/A** Sorts on lock status, host name, and priority in that order.
1N/A**
1N/A** Parameters:
1N/A** a -- the first argument.
1N/A** b -- the second argument.
1N/A**
1N/A** Returns:
1N/A** <0 if a < b
1N/A** 0 if a == b
1N/A** >0 if a > b
1N/A**
1N/A*/
1N/A
1N/Astatic int
1N/Aworkcmpf2(a, b)
1N/A register WORK *a;
1N/A register WORK *b;
1N/A{
1N/A int i;
1N/A
1N/A /* lock status */
1N/A if (a->w_lock != b->w_lock)
1N/A return a->w_lock - b->w_lock;
1N/A
1N/A /* host name */
1N/A if (a->w_host != NULL && b->w_host == NULL)
1N/A return 1;
1N/A else if (a->w_host == NULL && b->w_host != NULL)
1N/A return -1;
1N/A if (a->w_host != NULL && b->w_host != NULL &&
1N/A (i = sm_strcasecmp(a->w_host, b->w_host)) != 0)
1N/A return i;
1N/A
1N/A /* job priority */
1N/A return workcmpf0(a, b);
1N/A}
1N/A/*
1N/A** WORKCMPF3 -- simple submission-time-only compare function.
1N/A**
1N/A** Parameters:
1N/A** a -- the first argument.
1N/A** b -- the second argument.
1N/A**
1N/A** Returns:
1N/A** -1 if a < b
1N/A** 0 if a == b
1N/A** +1 if a > b
1N/A**
1N/A*/
1N/A
1N/Astatic int
1N/Aworkcmpf3(a, b)
1N/A register WORK *a;
1N/A register WORK *b;
1N/A{
1N/A if (a->w_ctime > b->w_ctime)
1N/A return 1;
1N/A else if (a->w_ctime < b->w_ctime)
1N/A return -1;
1N/A else
1N/A return 0;
1N/A}
1N/A/*
1N/A** WORKCMPF4 -- compare based on file name
1N/A**
1N/A** Parameters:
1N/A** a -- the first argument.
1N/A** b -- the second argument.
1N/A**
1N/A** Returns:
1N/A** -1 if a < b
1N/A** 0 if a == b
1N/A** +1 if a > b
1N/A**
1N/A*/
1N/A
1N/Astatic int
1N/Aworkcmpf4(a, b)
1N/A register WORK *a;
1N/A register WORK *b;
1N/A{
1N/A return strcmp(a->w_name, b->w_name);
1N/A}
1N/A/*
1N/A** WORKCMPF5 -- compare based on assigned random number
1N/A**
1N/A** Parameters:
1N/A** a -- the first argument.
1N/A** b -- the second argument.
1N/A**
1N/A** Returns:
1N/A** randomly 1/-1
1N/A*/
1N/A
1N/A/* ARGSUSED0 */
1N/Astatic int
1N/Aworkcmpf5(a, b)
1N/A register WORK *a;
1N/A register WORK *b;
1N/A{
1N/A if (strlen(a->w_name) < randi || strlen(b->w_name) < randi)
1N/A return -1;
1N/A return a->w_name[randi] - b->w_name[randi];
1N/A}
1N/A/*
1N/A** WORKCMPF6 -- simple modification-time-only compare function.
1N/A**
1N/A** Parameters:
1N/A** a -- the first argument.
1N/A** b -- the second argument.
1N/A**
1N/A** Returns:
1N/A** -1 if a < b
1N/A** 0 if a == b
1N/A** +1 if a > b
1N/A**
1N/A*/
1N/A
1N/Astatic int
1N/Aworkcmpf6(a, b)
1N/A register WORK *a;
1N/A register WORK *b;
1N/A{
1N/A if (a->w_mtime > b->w_mtime)
1N/A return 1;
1N/A else if (a->w_mtime < b->w_mtime)
1N/A return -1;
1N/A else
1N/A return 0;
1N/A}
1N/A#if _FFR_RHS
1N/A/*
1N/A** WORKCMPF7 -- compare function for ordering work based on shuffled host name.
1N/A**
1N/A** Sorts on lock status, host name, and priority in that order.
1N/A**
1N/A** Parameters:
1N/A** a -- the first argument.
1N/A** b -- the second argument.
1N/A**
1N/A** Returns:
1N/A** <0 if a < b
1N/A** 0 if a == b
1N/A** >0 if a > b
1N/A**
1N/A*/
1N/A
1N/Astatic int
1N/Aworkcmpf7(a, b)
1N/A register WORK *a;
1N/A register WORK *b;
1N/A{
1N/A int i;
1N/A
1N/A /* lock status */
1N/A if (a->w_lock != b->w_lock)
1N/A return a->w_lock - b->w_lock;
1N/A
1N/A /* host name */
1N/A if (a->w_host != NULL && b->w_host == NULL)
1N/A return 1;
1N/A else if (a->w_host == NULL && b->w_host != NULL)
1N/A return -1;
1N/A if (a->w_host != NULL && b->w_host != NULL &&
1N/A (i = sm_strshufflecmp(a->w_host, b->w_host)) != 0)
1N/A return i;
1N/A
1N/A /* job priority */
1N/A return workcmpf0(a, b);
1N/A}
1N/A#endif /* _FFR_RHS */
1N/A/*
1N/A** STRREV -- reverse string
1N/A**
1N/A** Returns a pointer to a new string that is the reverse of
1N/A** the string pointed to by fwd. The space for the new
1N/A** string is obtained using xalloc().
1N/A**
1N/A** Parameters:
1N/A** fwd -- the string to reverse.
1N/A**
1N/A** Returns:
1N/A** the reversed string.
1N/A*/
1N/A
1N/Astatic char *
1N/Astrrev(fwd)
1N/A char *fwd;
1N/A{
1N/A char *rev = NULL;
1N/A int len, cnt;
1N/A
1N/A len = strlen(fwd);
1N/A rev = xalloc(len + 1);
1N/A for (cnt = 0; cnt < len; ++cnt)
1N/A rev[cnt] = fwd[len - cnt - 1];
1N/A rev[len] = '\0';
1N/A return rev;
1N/A}
1N/A
1N/A#if _FFR_RHS
1N/A
1N/A# define NASCII 128
1N/A# define NCHAR 256
1N/A
1N/Astatic unsigned char ShuffledAlphabet[NCHAR];
1N/A
1N/Avoid
1N/Ainit_shuffle_alphabet()
1N/A{
1N/A static bool init = false;
1N/A int i;
1N/A
1N/A if (init)
1N/A return;
1N/A
1N/A /* fill the ShuffledAlphabet */
1N/A for (i = 0; i < NASCII; i++)
1N/A ShuffledAlphabet[i] = i;
1N/A
1N/A /* mix it */
1N/A for (i = 1; i < NASCII; i++)
1N/A {
1N/A register int j = get_random() % NASCII;
1N/A register int tmp;
1N/A
1N/A tmp = ShuffledAlphabet[j];
1N/A ShuffledAlphabet[j] = ShuffledAlphabet[i];
1N/A ShuffledAlphabet[i] = tmp;
1N/A }
1N/A
1N/A /* make it case insensitive */
1N/A for (i = 'A'; i <= 'Z'; i++)
1N/A ShuffledAlphabet[i] = ShuffledAlphabet[i + 'a' - 'A'];
1N/A
1N/A /* fill the upper part */
1N/A for (i = 0; i < NASCII; i++)
1N/A ShuffledAlphabet[i + NASCII] = ShuffledAlphabet[i];
1N/A init = true;
1N/A}
1N/A
1N/Astatic int
1N/Asm_strshufflecmp(a, b)
1N/A char *a;
1N/A char *b;
1N/A{
1N/A const unsigned char *us1 = (const unsigned char *) a;
1N/A const unsigned char *us2 = (const unsigned char *) b;
1N/A
1N/A while (ShuffledAlphabet[*us1] == ShuffledAlphabet[*us2++])
1N/A {
1N/A if (*us1++ == '\0')
1N/A return 0;
1N/A }
1N/A return (ShuffledAlphabet[*us1] - ShuffledAlphabet[*--us2]);
1N/A}
1N/A#endif /* _FFR_RHS */
1N/A
1N/A/*
1N/A** DOWORK -- do a work request.
1N/A**
1N/A** Parameters:
1N/A** qgrp -- the index of the queue group for the job.
1N/A** qdir -- the index of the queue directory for the job.
1N/A** id -- the ID of the job to run.
1N/A** forkflag -- if set, run this in background.
1N/A** requeueflag -- if set, reinstantiate the queue quickly.
1N/A** This is used when expanding aliases in the queue.
1N/A** If forkflag is also set, it doesn't wait for the
1N/A** child.
1N/A** e - the envelope in which to run it.
1N/A**
1N/A** Returns:
1N/A** process id of process that is running the queue job.
1N/A**
1N/A** Side Effects:
1N/A** The work request is satisfied if possible.
1N/A*/
1N/A
1N/Apid_t
1N/Adowork(qgrp, qdir, id, forkflag, requeueflag, e)
1N/A int qgrp;
1N/A int qdir;
1N/A char *id;
1N/A bool forkflag;
1N/A bool requeueflag;
1N/A register ENVELOPE *e;
1N/A{
1N/A register pid_t pid;
1N/A SM_RPOOL_T *rpool;
1N/A
1N/A if (tTd(40, 1))
1N/A sm_dprintf("dowork(%s/%s)\n", qid_printqueue(qgrp, qdir), id);
1N/A
1N/A /*
1N/A ** Fork for work.
1N/A */
1N/A
1N/A if (forkflag)
1N/A {
1N/A /*
1N/A ** Since the delivery may happen in a child and the
1N/A ** parent does not wait, the parent may close the
1N/A ** maps thereby removing any shared memory used by
1N/A ** the map. Therefore, close the maps now so the
1N/A ** child will dynamically open them if necessary.
1N/A */
1N/A
1N/A closemaps(false);
1N/A
1N/A pid = fork();
1N/A if (pid < 0)
1N/A {
1N/A syserr("dowork: cannot fork");
1N/A return 0;
1N/A }
1N/A else if (pid > 0)
1N/A {
1N/A /* parent -- clean out connection cache */
1N/A mci_flush(false, NULL);
1N/A }
1N/A else
1N/A {
1N/A /*
1N/A ** Initialize exception stack and default exception
1N/A ** handler for child process.
1N/A */
1N/A
1N/A /* Reset global flags */
1N/A RestartRequest = NULL;
1N/A RestartWorkGroup = false;
1N/A ShutdownRequest = NULL;
1N/A PendingSignal = 0;
1N/A CurrentPid = getpid();
1N/A sm_exc_newthread(fatal_error);
1N/A
1N/A /*
1N/A ** See note above about SMTP processes and SIGCHLD.
1N/A */
1N/A
1N/A if (OpMode == MD_SMTP ||
1N/A OpMode == MD_DAEMON ||
1N/A MaxQueueChildren > 0)
1N/A {
1N/A proc_list_clear();
1N/A sm_releasesignal(SIGCHLD);
1N/A (void) sm_signal(SIGCHLD, SIG_DFL);
1N/A }
1N/A
1N/A /* child -- error messages to the transcript */
1N/A QuickAbort = OnlyOneError = false;
1N/A }
1N/A }
1N/A else
1N/A {
1N/A pid = 0;
1N/A }
1N/A
1N/A if (pid == 0)
1N/A {
1N/A /*
1N/A ** CHILD
1N/A ** Lock the control file to avoid duplicate deliveries.
1N/A ** Then run the file as though we had just read it.
1N/A ** We save an idea of the temporary name so we
1N/A ** can recover on interrupt.
1N/A */
1N/A
1N/A if (forkflag)
1N/A {
1N/A /* Reset global flags */
1N/A RestartRequest = NULL;
1N/A RestartWorkGroup = false;
1N/A ShutdownRequest = NULL;
1N/A PendingSignal = 0;
1N/A }
1N/A
1N/A /* set basic modes, etc. */
1N/A sm_clear_events();
1N/A clearstats();
1N/A rpool = sm_rpool_new_x(NULL);
1N/A clearenvelope(e, false, rpool);
1N/A e->e_flags |= EF_QUEUERUN|EF_GLOBALERRS;
1N/A set_delivery_mode(SM_DELIVER, e);
1N/A e->e_errormode = EM_MAIL;
1N/A e->e_id = id;
1N/A e->e_qgrp = qgrp;
1N/A e->e_qdir = qdir;
1N/A GrabTo = UseErrorsTo = false;
1N/A ExitStat = EX_OK;
1N/A if (forkflag)
1N/A {
1N/A disconnect(1, e);
1N/A set_op_mode(MD_QUEUERUN);
1N/A }
1N/A sm_setproctitle(true, e, "%s from queue", qid_printname(e));
1N/A if (LogLevel > 76)
1N/A sm_syslog(LOG_DEBUG, e->e_id, "dowork, pid=%d",
1N/A (int) CurrentPid);
1N/A
1N/A /* don't use the headers from sendmail.cf... */
1N/A e->e_header = NULL;
1N/A
1N/A /* read the queue control file -- return if locked */
1N/A if (!readqf(e, false))
1N/A {
1N/A if (tTd(40, 4) && e->e_id != NULL)
1N/A sm_dprintf("readqf(%s) failed\n",
1N/A qid_printname(e));
1N/A e->e_id = NULL;
1N/A if (forkflag)
1N/A finis(false, true, EX_OK);
1N/A else
1N/A {
1N/A /* adding this frees 8 bytes */
1N/A clearenvelope(e, false, rpool);
1N/A
1N/A /* adding this frees 12 bytes */
1N/A sm_rpool_free(rpool);
1N/A e->e_rpool = NULL;
1N/A return 0;
1N/A }
1N/A }
1N/A
1N/A e->e_flags |= EF_INQUEUE;
1N/A eatheader(e, requeueflag, true);
1N/A
1N/A if (requeueflag)
1N/A queueup(e, false, false);
1N/A
1N/A /* do the delivery */
1N/A sendall(e, SM_DELIVER);
1N/A
1N/A /* finish up and exit */
1N/A if (forkflag)
1N/A finis(true, true, ExitStat);
1N/A else
1N/A {
1N/A (void) dropenvelope(e, true, false);
1N/A sm_rpool_free(rpool);
1N/A e->e_rpool = NULL;
1N/A }
1N/A }
1N/A e->e_id = NULL;
1N/A return pid;
1N/A}
1N/A
1N/A/*
1N/A** DOWORKLIST -- process a list of envelopes as work requests
1N/A**
1N/A** Similar to dowork(), except that after forking, it processes an
1N/A** envelope and its siblings, treating each envelope as a work request.
1N/A**
1N/A** Parameters:
1N/A** el -- envelope to be processed including its siblings.
1N/A** forkflag -- if set, run this in background.
1N/A** requeueflag -- if set, reinstantiate the queue quickly.
1N/A** This is used when expanding aliases in the queue.
1N/A** If forkflag is also set, it doesn't wait for the
1N/A** child.
1N/A**
1N/A** Returns:
1N/A** process id of process that is running the queue job.
1N/A**
1N/A** Side Effects:
1N/A** The work request is satisfied if possible.
1N/A*/
1N/A
1N/Apid_t
1N/Adoworklist(el, forkflag, requeueflag)
1N/A ENVELOPE *el;
1N/A bool forkflag;
1N/A bool requeueflag;
1N/A{
1N/A register pid_t pid;
1N/A ENVELOPE *ei;
1N/A
1N/A if (tTd(40, 1))
1N/A sm_dprintf("doworklist()\n");
1N/A
1N/A /*
1N/A ** Fork for work.
1N/A */
1N/A
1N/A if (forkflag)
1N/A {
1N/A /*
1N/A ** Since the delivery may happen in a child and the
1N/A ** parent does not wait, the parent may close the
1N/A ** maps thereby removing any shared memory used by
1N/A ** the map. Therefore, close the maps now so the
1N/A ** child will dynamically open them if necessary.
1N/A */
1N/A
1N/A closemaps(false);
1N/A
1N/A pid = fork();
1N/A if (pid < 0)
1N/A {
1N/A syserr("doworklist: cannot fork");
1N/A return 0;
1N/A }
1N/A else if (pid > 0)
1N/A {
1N/A /* parent -- clean out connection cache */
1N/A mci_flush(false, NULL);
1N/A }
1N/A else
1N/A {
1N/A /*
1N/A ** Initialize exception stack and default exception
1N/A ** handler for child process.
1N/A */
1N/A
1N/A /* Reset global flags */
1N/A RestartRequest = NULL;
1N/A RestartWorkGroup = false;
1N/A ShutdownRequest = NULL;
1N/A PendingSignal = 0;
1N/A CurrentPid = getpid();
1N/A sm_exc_newthread(fatal_error);
1N/A
1N/A /*
1N/A ** See note above about SMTP processes and SIGCHLD.
1N/A */
1N/A
1N/A if (OpMode == MD_SMTP ||
1N/A OpMode == MD_DAEMON ||
1N/A MaxQueueChildren > 0)
1N/A {
1N/A proc_list_clear();
1N/A sm_releasesignal(SIGCHLD);
1N/A (void) sm_signal(SIGCHLD, SIG_DFL);
1N/A }
1N/A
1N/A /* child -- error messages to the transcript */
1N/A QuickAbort = OnlyOneError = false;
1N/A }
1N/A }
1N/A else
1N/A {
1N/A pid = 0;
1N/A }
1N/A
1N/A if (pid != 0)
1N/A return pid;
1N/A
1N/A /*
1N/A ** IN CHILD
1N/A ** Lock the control file to avoid duplicate deliveries.
1N/A ** Then run the file as though we had just read it.
1N/A ** We save an idea of the temporary name so we
1N/A ** can recover on interrupt.
1N/A */
1N/A
1N/A if (forkflag)
1N/A {
1N/A /* Reset global flags */
1N/A RestartRequest = NULL;
1N/A RestartWorkGroup = false;
1N/A ShutdownRequest = NULL;
1N/A PendingSignal = 0;
1N/A }
1N/A
1N/A /* set basic modes, etc. */
1N/A sm_clear_events();
1N/A clearstats();
1N/A GrabTo = UseErrorsTo = false;
1N/A ExitStat = EX_OK;
1N/A if (forkflag)
1N/A {
1N/A disconnect(1, el);
1N/A set_op_mode(MD_QUEUERUN);
1N/A }
1N/A if (LogLevel > 76)
1N/A sm_syslog(LOG_DEBUG, el->e_id, "doworklist, pid=%d",
1N/A (int) CurrentPid);
1N/A
1N/A for (ei = el; ei != NULL; ei = ei->e_sibling)
1N/A {
1N/A ENVELOPE e;
1N/A SM_RPOOL_T *rpool;
1N/A
1N/A if (WILL_BE_QUEUED(ei->e_sendmode))
1N/A continue;
1N/A else if (QueueMode != QM_QUARANTINE &&
1N/A ei->e_quarmsg != NULL)
1N/A continue;
1N/A
1N/A rpool = sm_rpool_new_x(NULL);
1N/A clearenvelope(&e, true, rpool);
1N/A e.e_flags |= EF_QUEUERUN|EF_GLOBALERRS;
1N/A set_delivery_mode(SM_DELIVER, &e);
1N/A e.e_errormode = EM_MAIL;
1N/A e.e_id = ei->e_id;
1N/A e.e_qgrp = ei->e_qgrp;
1N/A e.e_qdir = ei->e_qdir;
1N/A openxscript(&e);
1N/A sm_setproctitle(true, &e, "%s from queue", qid_printname(&e));
1N/A
1N/A /* don't use the headers from sendmail.cf... */
1N/A e.e_header = NULL;
1N/A CurEnv = &e;
1N/A
1N/A /* read the queue control file -- return if locked */
1N/A if (readqf(&e, false))
1N/A {
1N/A e.e_flags |= EF_INQUEUE;
1N/A eatheader(&e, requeueflag, true);
1N/A
1N/A if (requeueflag)
1N/A queueup(&e, false, false);
1N/A
1N/A /* do the delivery */
1N/A sendall(&e, SM_DELIVER);
1N/A (void) dropenvelope(&e, true, false);
1N/A }
1N/A else
1N/A {
1N/A if (tTd(40, 4) && e.e_id != NULL)
1N/A sm_dprintf("readqf(%s) failed\n",
1N/A qid_printname(&e));
1N/A }
1N/A sm_rpool_free(rpool);
1N/A ei->e_id = NULL;
1N/A }
1N/A
1N/A /* restore CurEnv */
1N/A CurEnv = el;
1N/A
1N/A /* finish up and exit */
1N/A if (forkflag)
1N/A finis(true, true, ExitStat);
1N/A return 0;
1N/A}
1N/A/*
1N/A** READQF -- read queue file and set up environment.
1N/A**
1N/A** Parameters:
1N/A** e -- the envelope of the job to run.
1N/A** openonly -- only open the qf (returned as e_lockfp)
1N/A**
1N/A** Returns:
1N/A** true if it successfully read the queue file.
1N/A** false otherwise.
1N/A**
1N/A** Side Effects:
1N/A** The queue file is returned locked.
1N/A*/
1N/A
1N/Astatic bool
1N/Areadqf(e, openonly)
1N/A register ENVELOPE *e;
1N/A bool openonly;
1N/A{
1N/A register SM_FILE_T *qfp;
1N/A ADDRESS *ctladdr;
1N/A struct stat st, stf;
1N/A char *bp;
1N/A int qfver = 0;
1N/A long hdrsize = 0;
1N/A register char *p;
1N/A char *frcpt = NULL;
1N/A char *orcpt = NULL;
1N/A bool nomore = false;
1N/A bool bogus = false;
1N/A MODE_T qsafe;
1N/A char *err;
1N/A char qf[MAXPATHLEN];
1N/A char buf[MAXLINE];
1N/A int bufsize;
1N/A
1N/A /*
1N/A ** Read and process the file.
1N/A */
1N/A
1N/A SM_REQUIRE(e != NULL);
1N/A bp = NULL;
1N/A (void) sm_strlcpy(qf, queuename(e, ANYQFL_LETTER), sizeof(qf));
1N/A qfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, qf, SM_IO_RDWR_B, NULL);
1N/A if (qfp == NULL)
1N/A {
1N/A int save_errno = errno;
1N/A
1N/A if (tTd(40, 8))
1N/A sm_dprintf("readqf(%s): sm_io_open failure (%s)\n",
1N/A qf, sm_errstring(errno));
1N/A errno = save_errno;
1N/A if (errno != ENOENT
1N/A )
1N/A syserr("readqf: no control file %s", qf);
1N/A RELEASE_QUEUE;
1N/A return false;
1N/A }
1N/A
1N/A if (!lockfile(sm_io_getinfo(qfp, SM_IO_WHAT_FD, NULL), qf, NULL,
1N/A LOCK_EX|LOCK_NB))
1N/A {
1N/A /* being processed by another queuer */
1N/A if (Verbose)
1N/A (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
1N/A "%s: locked\n", e->e_id);
1N/A if (tTd(40, 8))
1N/A sm_dprintf("%s: locked\n", e->e_id);
1N/A if (LogLevel > 19)
1N/A sm_syslog(LOG_DEBUG, e->e_id, "locked");
1N/A (void) sm_io_close(qfp, SM_TIME_DEFAULT);
1N/A RELEASE_QUEUE;
1N/A return false;
1N/A }
1N/A
1N/A RELEASE_QUEUE;
1N/A
1N/A /*
1N/A ** Prevent locking race condition.
1N/A **
1N/A ** Process A: readqf(): qfp = fopen(qffile)
1N/A ** Process B: queueup(): rename(tf, qf)
1N/A ** Process B: unlocks(tf)
1N/A ** Process A: lockfile(qf);
1N/A **
1N/A ** Process A (us) has the old qf file (before the rename deleted
1N/A ** the directory entry) and will be delivering based on old data.
1N/A ** This can lead to multiple deliveries of the same recipients.
1N/A **
1N/A ** Catch this by checking if the underlying qf file has changed
1N/A ** *after* acquiring our lock and if so, act as though the file
1N/A ** was still locked (i.e., just return like the lockfile() case
1N/A ** above.
1N/A */
1N/A
1N/A if (stat(qf, &stf) < 0 ||
1N/A fstat(sm_io_getinfo(qfp, SM_IO_WHAT_FD, NULL), &st) < 0)
1N/A {
1N/A /* must have been being processed by someone else */
1N/A if (tTd(40, 8))
1N/A sm_dprintf("readqf(%s): [f]stat failure (%s)\n",
1N/A qf, sm_errstring(errno));
1N/A (void) sm_io_close(qfp, SM_TIME_DEFAULT);
1N/A return false;
1N/A }
1N/A
1N/A if (st.st_nlink != stf.st_nlink ||
1N/A st.st_dev != stf.st_dev ||
1N/A ST_INODE(st) != ST_INODE(stf) ||
1N/A#if HAS_ST_GEN && 0 /* AFS returns garbage in st_gen */
1N/A st.st_gen != stf.st_gen ||
1N/A#endif /* HAS_ST_GEN && 0 */
1N/A st.st_uid != stf.st_uid ||
1N/A st.st_gid != stf.st_gid ||
1N/A st.st_size != stf.st_size)
1N/A {
1N/A /* changed after opened */
1N/A if (Verbose)
1N/A (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
1N/A "%s: changed\n", e->e_id);
1N/A if (tTd(40, 8))
1N/A sm_dprintf("%s: changed\n", e->e_id);
1N/A if (LogLevel > 19)
1N/A sm_syslog(LOG_DEBUG, e->e_id, "changed");
1N/A (void) sm_io_close(qfp, SM_TIME_DEFAULT);
1N/A return false;
1N/A }
1N/A
1N/A /*
1N/A ** Check the queue file for plausibility to avoid attacks.
1N/A */
1N/A
1N/A qsafe = S_IWOTH|S_IWGRP;
1N/A if (bitset(S_IWGRP, QueueFileMode))
1N/A qsafe &= ~S_IWGRP;
1N/A
1N/A bogus = st.st_uid != geteuid() &&
1N/A st.st_uid != TrustedUid &&
1N/A geteuid() != RealUid;
1N/A
1N/A /*
1N/A ** If this qf file results from a set-group-ID binary, then
1N/A ** we check whether the directory is group-writable,
1N/A ** the queue file mode contains the group-writable bit, and
1N/A ** the groups are the same.
1N/A ** Notice: this requires that the set-group-ID binary is used to
1N/A ** run the queue!
1N/A */
1N/A
1N/A if (bogus && st.st_gid == getegid() && UseMSP)
1N/A {
1N/A char delim;
1N/A struct stat dst;
1N/A
1N/A bp = SM_LAST_DIR_DELIM(qf);
1N/A if (bp == NULL)
1N/A delim = '\0';
1N/A else
1N/A {
1N/A delim = *bp;
1N/A *bp = '\0';
1N/A }
1N/A if (stat(delim == '\0' ? "." : qf, &dst) < 0)
1N/A syserr("readqf: cannot stat directory %s",
1N/A delim == '\0' ? "." : qf);
1N/A else
1N/A {
1N/A bogus = !(bitset(S_IWGRP, QueueFileMode) &&
1N/A bitset(S_IWGRP, dst.st_mode) &&
1N/A dst.st_gid == st.st_gid);
1N/A }
1N/A if (delim != '\0')
1N/A *bp = delim;
1N/A bp = NULL;
1N/A }
1N/A if (!bogus)
1N/A bogus = bitset(qsafe, st.st_mode);
1N/A if (bogus)
1N/A {
1N/A if (LogLevel > 0)
1N/A {
1N/A sm_syslog(LOG_ALERT, e->e_id,
1N/A "bogus queue file, uid=%d, gid=%d, mode=%o",
1N/A st.st_uid, st.st_gid, st.st_mode);
1N/A }
1N/A if (tTd(40, 8))
1N/A sm_dprintf("readqf(%s): bogus file\n", qf);
1N/A e->e_flags |= EF_INQUEUE;
1N/A if (!openonly)
1N/A loseqfile(e, "bogus file uid/gid in mqueue");
1N/A (void) sm_io_close(qfp, SM_TIME_DEFAULT);
1N/A return false;
1N/A }
1N/A
1N/A if (st.st_size == 0)
1N/A {
1N/A /* must be a bogus file -- if also old, just remove it */
1N/A if (!openonly && st.st_ctime + 10 * 60 < curtime())
1N/A {
1N/A (void) xunlink(queuename(e, DATAFL_LETTER));
1N/A (void) xunlink(queuename(e, ANYQFL_LETTER));
1N/A }
1N/A (void) sm_io_close(qfp, SM_TIME_DEFAULT);
1N/A return false;
1N/A }
1N/A
1N/A if (st.st_nlink == 0)
1N/A {
1N/A /*
1N/A ** Race condition -- we got a file just as it was being
1N/A ** unlinked. Just assume it is zero length.
1N/A */
1N/A
1N/A (void) sm_io_close(qfp, SM_TIME_DEFAULT);
1N/A return false;
1N/A }
1N/A
1N/A#if _FFR_TRUSTED_QF
1N/A /*
1N/A ** If we don't own the file mark it as unsafe.
1N/A ** However, allow TrustedUser to own it as well
1N/A ** in case TrustedUser manipulates the queue.
1N/A */
1N/A
1N/A if (st.st_uid != geteuid() && st.st_uid != TrustedUid)
1N/A e->e_flags |= EF_UNSAFE;
1N/A#else /* _FFR_TRUSTED_QF */
1N/A /* If we don't own the file mark it as unsafe */
1N/A if (st.st_uid != geteuid())
1N/A e->e_flags |= EF_UNSAFE;
1N/A#endif /* _FFR_TRUSTED_QF */
1N/A
1N/A /* good file -- save this lock */
1N/A e->e_lockfp = qfp;
1N/A
1N/A /* Just wanted the open file */
1N/A if (openonly)
1N/A return true;
1N/A
1N/A /* do basic system initialization */
1N/A initsys(e);
1N/A macdefine(&e->e_macro, A_PERM, 'i', e->e_id);
1N/A
1N/A LineNumber = 0;
1N/A e->e_flags |= EF_GLOBALERRS;
1N/A set_op_mode(MD_QUEUERUN);
1N/A ctladdr = NULL;
1N/A e->e_qfletter = queue_letter(e, ANYQFL_LETTER);
1N/A e->e_dfqgrp = e->e_qgrp;
1N/A e->e_dfqdir = e->e_qdir;
1N/A#if _FFR_QUEUE_MACRO
1N/A macdefine(&e->e_macro, A_TEMP, macid("{queue}"),
1N/A qid_printqueue(e->e_qgrp, e->e_qdir));
1N/A#endif /* _FFR_QUEUE_MACRO */
1N/A e->e_dfino = -1;
1N/A e->e_msgsize = -1;
1N/A while (bufsize = sizeof(buf),
1N/A (bp = fgetfolded(buf, &bufsize, qfp)) != NULL)
1N/A {
1N/A unsigned long qflags;
1N/A ADDRESS *q;
1N/A int r;
1N/A time_t now;
1N/A auto char *ep;
1N/A
1N/A if (tTd(40, 4))
1N/A sm_dprintf("+++++ %s\n", bp);
1N/A if (nomore)
1N/A {
1N/A /* hack attack */
1N/A hackattack:
1N/A syserr("SECURITY ALERT: extra or bogus data in queue file: %s",
1N/A bp);
1N/A err = "bogus queue line";
1N/A goto fail;
1N/A }
1N/A switch (bp[0])
1N/A {
1N/A case 'A': /* AUTH= parameter */
1N/A if (!xtextok(&bp[1]))
1N/A goto hackattack;
1N/A e->e_auth_param = sm_rpool_strdup_x(e->e_rpool, &bp[1]);
1N/A break;
1N/A
1N/A case 'B': /* body type */
1N/A r = check_bodytype(&bp[1]);
1N/A if (!BODYTYPE_VALID(r))
1N/A goto hackattack;
1N/A e->e_bodytype = sm_rpool_strdup_x(e->e_rpool, &bp[1]);
1N/A break;
1N/A
1N/A case 'C': /* specify controlling user */
1N/A ctladdr = setctluser(&bp[1], qfver, e);
1N/A break;
1N/A
1N/A case 'D': /* data file name */
1N/A /* obsolete -- ignore */
1N/A break;
1N/A
1N/A case 'd': /* data file directory name */
1N/A {
1N/A int qgrp, qdir;
1N/A
1N/A#if _FFR_MSP_PARANOIA
1N/A /* forbid queue groups in MSP? */
1N/A if (UseMSP)
1N/A goto hackattack;
1N/A#endif /* _FFR_MSP_PARANOIA */
1N/A for (qgrp = 0;
1N/A qgrp < NumQueue && Queue[qgrp] != NULL;
1N/A ++qgrp)
1N/A {
1N/A for (qdir = 0;
1N/A qdir < Queue[qgrp]->qg_numqueues;
1N/A ++qdir)
1N/A {
1N/A if (strcmp(&bp[1],
1N/A Queue[qgrp]->qg_qpaths[qdir].qp_name)
1N/A == 0)
1N/A {
1N/A e->e_dfqgrp = qgrp;
1N/A e->e_dfqdir = qdir;
1N/A goto done;
1N/A }
1N/A }
1N/A }
1N/A err = "bogus queue file directory";
1N/A goto fail;
1N/A done:
1N/A break;
1N/A }
1N/A
1N/A case 'E': /* specify error recipient */
1N/A /* no longer used */
1N/A break;
1N/A
1N/A case 'F': /* flag bits */
1N/A if (strncmp(bp, "From ", 5) == 0)
1N/A {
1N/A /* we are being spoofed! */
1N/A syserr("SECURITY ALERT: bogus qf line %s", bp);
1N/A err = "bogus queue line";
1N/A goto fail;
1N/A }
1N/A for (p = &bp[1]; *p != '\0'; p++)
1N/A {
1N/A switch (*p)
1N/A {
1N/A case '8': /* has 8 bit data */
1N/A e->e_flags |= EF_HAS8BIT;
1N/A break;
1N/A
1N/A case 'b': /* delete Bcc: header */
1N/A e->e_flags |= EF_DELETE_BCC;
1N/A break;
1N/A
1N/A case 'd': /* envelope has DSN RET= */
1N/A e->e_flags |= EF_RET_PARAM;
1N/A break;
1N/A
1N/A case 'n': /* don't return body */
1N/A e->e_flags |= EF_NO_BODY_RETN;
1N/A break;
1N/A
1N/A case 'r': /* response */
1N/A e->e_flags |= EF_RESPONSE;
1N/A break;
1N/A
1N/A case 's': /* split */
1N/A e->e_flags |= EF_SPLIT;
1N/A break;
1N/A
1N/A case 'w': /* warning sent */
1N/A e->e_flags |= EF_WARNING;
1N/A break;
1N/A }
1N/A }
1N/A break;
1N/A
1N/A case 'q': /* quarantine reason */
1N/A e->e_quarmsg = sm_rpool_strdup_x(e->e_rpool, &bp[1]);
1N/A macdefine(&e->e_macro, A_PERM,
1N/A macid("{quarantine}"), e->e_quarmsg);
1N/A break;
1N/A
1N/A case 'H': /* header */
1N/A
1N/A /*
1N/A ** count size before chompheader() destroys the line.
1N/A ** this isn't accurate due to macro expansion, but
1N/A ** better than before. "-3" to skip H?? at least.
1N/A */
1N/A
1N/A hdrsize += strlen(bp) - 3;
1N/A (void) chompheader(&bp[1], CHHDR_QUEUE, NULL, e);
1N/A break;
1N/A
1N/A case 'I': /* data file's inode number */
1N/A /* regenerated below */
1N/A break;
1N/A
1N/A case 'K': /* time of last delivery attempt */
1N/A e->e_dtime = atol(&buf[1]);
1N/A break;
1N/A
1N/A case 'L': /* Solaris Content-Length: */
1N/A case 'M': /* message */
1N/A /* ignore this; we want a new message next time */
1N/A break;
1N/A
1N/A case 'N': /* number of delivery attempts */
1N/A e->e_ntries = atoi(&buf[1]);
1N/A
1N/A /* if this has been tried recently, let it be */
1N/A now = curtime();
1N/A if (e->e_ntries > 0 && e->e_dtime <= now &&
1N/A now < e->e_dtime + MinQueueAge)
1N/A {
1N/A char *howlong;
1N/A
1N/A howlong = pintvl(now - e->e_dtime, true);
1N/A if (Verbose)
1N/A (void) sm_io_fprintf(smioout,
1N/A SM_TIME_DEFAULT,
1N/A "%s: too young (%s)\n",
1N/A e->e_id, howlong);
1N/A if (tTd(40, 8))
1N/A sm_dprintf("%s: too young (%s)\n",
1N/A e->e_id, howlong);
1N/A if (LogLevel > 19)
1N/A sm_syslog(LOG_DEBUG, e->e_id,
1N/A "too young (%s)",
1N/A howlong);
1N/A e->e_id = NULL;
1N/A unlockqueue(e);
1N/A if (bp != buf)
1N/A sm_free(bp);
1N/A return false;
1N/A }
1N/A macdefine(&e->e_macro, A_TEMP,
1N/A macid("{ntries}"), &buf[1]);
1N/A
1N/A#if NAMED_BIND
1N/A /* adjust BIND parameters immediately */
1N/A if (e->e_ntries == 0)
1N/A {
1N/A _res.retry = TimeOuts.res_retry[RES_TO_FIRST];
1N/A _res.retrans = TimeOuts.res_retrans[RES_TO_FIRST];
1N/A }
1N/A else
1N/A {
1N/A _res.retry = TimeOuts.res_retry[RES_TO_NORMAL];
1N/A _res.retrans = TimeOuts.res_retrans[RES_TO_NORMAL];
1N/A }
1N/A#endif /* NAMED_BIND */
1N/A break;
1N/A
1N/A case 'P': /* message priority */
1N/A e->e_msgpriority = atol(&bp[1]) + WkTimeFact;
1N/A break;
1N/A
1N/A case 'Q': /* original recipient */
1N/A orcpt = sm_rpool_strdup_x(e->e_rpool, &bp[1]);
1N/A break;
1N/A
1N/A case 'r': /* final recipient */
1N/A frcpt = sm_rpool_strdup_x(e->e_rpool, &bp[1]);
1N/A break;
1N/A
1N/A case 'R': /* specify recipient */
1N/A p = bp;
1N/A qflags = 0;
1N/A if (qfver >= 1)
1N/A {
1N/A /* get flag bits */
1N/A while (*++p != '\0' && *p != ':')
1N/A {
1N/A switch (*p)
1N/A {
1N/A case 'N':
1N/A qflags |= QHASNOTIFY;
1N/A break;
1N/A
1N/A case 'S':
1N/A qflags |= QPINGONSUCCESS;
1N/A break;
1N/A
1N/A case 'F':
1N/A qflags |= QPINGONFAILURE;
1N/A break;
1N/A
1N/A case 'D':
1N/A qflags |= QPINGONDELAY;
1N/A break;
1N/A
1N/A case 'P':
1N/A qflags |= QPRIMARY;
1N/A break;
1N/A
1N/A case 'A':
1N/A if (ctladdr != NULL)
1N/A ctladdr->q_flags |= QALIAS;
1N/A break;
1N/A
1N/A default: /* ignore or complain? */
1N/A break;
1N/A }
1N/A }
1N/A }
1N/A else
1N/A qflags |= QPRIMARY;
1N/A macdefine(&e->e_macro, A_PERM, macid("{addr_type}"),
1N/A "e r");
1N/A if (*p != '\0')
1N/A q = parseaddr(++p, NULLADDR, RF_COPYALL, '\0',
1N/A NULL, e, true);
1N/A else
1N/A q = NULL;
1N/A if (q != NULL)
1N/A {
1N/A /* make sure we keep the current qgrp */
1N/A if (ISVALIDQGRP(e->e_qgrp))
1N/A q->q_qgrp = e->e_qgrp;
1N/A q->q_alias = ctladdr;
1N/A if (qfver >= 1)
1N/A q->q_flags &= ~Q_PINGFLAGS;
1N/A q->q_flags |= qflags;
1N/A q->q_finalrcpt = frcpt;
1N/A q->q_orcpt = orcpt;
1N/A (void) recipient(q, &e->e_sendqueue, 0, e);
1N/A }
1N/A frcpt = NULL;
1N/A orcpt = NULL;
1N/A macdefine(&e->e_macro, A_PERM, macid("{addr_type}"),
1N/A NULL);
1N/A break;
1N/A
1N/A case 'S': /* sender */
1N/A setsender(sm_rpool_strdup_x(e->e_rpool, &bp[1]),
1N/A e, NULL, '\0', true);
1N/A break;
1N/A
1N/A case 'T': /* init time */
1N/A e->e_ctime = atol(&bp[1]);
1N/A break;
1N/A
1N/A case 'V': /* queue file version number */
1N/A qfver = atoi(&bp[1]);
1N/A if (qfver <= QF_VERSION)
1N/A break;
1N/A syserr("Version number in queue file (%d) greater than max (%d)",
1N/A qfver, QF_VERSION);
1N/A err = "unsupported queue file version";
1N/A goto fail;
1N/A /* NOTREACHED */
1N/A break;
1N/A
1N/A case 'Z': /* original envelope id from ESMTP */
1N/A e->e_envid = sm_rpool_strdup_x(e->e_rpool, &bp[1]);
1N/A macdefine(&e->e_macro, A_PERM,
1N/A macid("{dsn_envid}"), e->e_envid);
1N/A break;
1N/A
1N/A case '!': /* deliver by */
1N/A
1N/A /* format: flag (1 char) space long-integer */
1N/A e->e_dlvr_flag = buf[1];
1N/A e->e_deliver_by = strtol(&buf[3], NULL, 10);
1N/A
1N/A case '$': /* define macro */
1N/A {
1N/A char *p;
1N/A
1N/A /* XXX elimate p? */
1N/A r = macid_parse(&bp[1], &ep);
1N/A if (r == 0)
1N/A break;
1N/A p = sm_rpool_strdup_x(e->e_rpool, ep);
1N/A macdefine(&e->e_macro, A_PERM, r, p);
1N/A }
1N/A break;
1N/A
1N/A case '.': /* terminate file */
1N/A nomore = true;
1N/A break;
1N/A
1N/A#if _FFR_QUEUEDELAY
1N/A case 'G':
1N/A case 'Y':
1N/A
1N/A /*
1N/A ** Maintain backward compatibility for
1N/A ** users who defined _FFR_QUEUEDELAY in
1N/A ** previous releases. Remove this
1N/A ** code in 8.14 or 8.15.
1N/A */
1N/A
1N/A if (qfver == 5 || qfver == 7)
1N/A break;
1N/A
1N/A /* If not qfver 5 or 7, then 'G' or 'Y' is invalid */
1N/A /* FALLTHROUGH */
1N/A#endif /* _FFR_QUEUEDELAY */
1N/A
1N/A default:
1N/A syserr("readqf: %s: line %d: bad line \"%s\"",
1N/A qf, LineNumber, shortenstring(bp, MAXSHORTSTR));
1N/A err = "unrecognized line";
1N/A goto fail;
1N/A }
1N/A
1N/A if (bp != buf)
1N/A SM_FREE(bp);
1N/A }
1N/A
1N/A /*
1N/A ** If we haven't read any lines, this queue file is empty.
1N/A ** Arrange to remove it without referencing any null pointers.
1N/A */
1N/A
1N/A if (LineNumber == 0)
1N/A {
1N/A errno = 0;
1N/A e->e_flags |= EF_CLRQUEUE|EF_FATALERRS|EF_RESPONSE;
1N/A return true;
1N/A }
1N/A
1N/A /* Check to make sure we have a complete queue file read */
1N/A if (!nomore)
1N/A {
1N/A syserr("readqf: %s: incomplete queue file read", qf);
1N/A (void) sm_io_close(qfp, SM_TIME_DEFAULT);
1N/A return false;
1N/A }
1N/A
1N/A#if _FFR_QF_PARANOIA
1N/A /* Check to make sure key fields were read */
1N/A if (e->e_from.q_mailer == NULL)
1N/A {
1N/A syserr("readqf: %s: sender not specified in queue file", qf);
1N/A (void) sm_io_close(qfp, SM_TIME_DEFAULT);
1N/A return false;
1N/A }
1N/A /* other checks? */
1N/A#endif /* _FFR_QF_PARANOIA */
1N/A
1N/A /* possibly set ${dsn_ret} macro */
1N/A if (bitset(EF_RET_PARAM, e->e_flags))
1N/A {
1N/A if (bitset(EF_NO_BODY_RETN, e->e_flags))
1N/A macdefine(&e->e_macro, A_PERM,
1N/A macid("{dsn_ret}"), "hdrs");
1N/A else
1N/A macdefine(&e->e_macro, A_PERM,
1N/A macid("{dsn_ret}"), "full");
1N/A }
1N/A
1N/A /*
1N/A ** Arrange to read the data file.
1N/A */
1N/A
1N/A p = queuename(e, DATAFL_LETTER);
1N/A e->e_dfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, p, SM_IO_RDONLY_B,
1N/A NULL);
1N/A if (e->e_dfp == NULL)
1N/A {
1N/A syserr("readqf: cannot open %s", p);
1N/A }
1N/A else
1N/A {
1N/A e->e_flags |= EF_HAS_DF;
1N/A if (fstat(sm_io_getinfo(e->e_dfp, SM_IO_WHAT_FD, NULL), &st)
1N/A >= 0)
1N/A {
1N/A e->e_msgsize = st.st_size + hdrsize;
1N/A e->e_dfdev = st.st_dev;
1N/A e->e_dfino = ST_INODE(st);
1N/A (void) sm_snprintf(buf, sizeof(buf), "%ld",
1N/A e->e_msgsize);
1N/A macdefine(&e->e_macro, A_TEMP, macid("{msg_size}"),
1N/A buf);
1N/A }
1N/A }
1N/A
1N/A return true;
1N/A
1N/A fail:
1N/A /*
1N/A ** There was some error reading the qf file (reason is in err var.)
1N/A ** Cleanup:
1N/A ** close file; clear e_lockfp since it is the same as qfp,
1N/A ** hence it is invalid (as file) after qfp is closed;
1N/A ** the qf file is on disk, so set the flag to avoid calling
1N/A ** queueup() with bogus data.
1N/A */
1N/A
1N/A if (bp != buf)
1N/A SM_FREE(bp);
1N/A if (qfp != NULL)
1N/A (void) sm_io_close(qfp, SM_TIME_DEFAULT);
1N/A e->e_lockfp = NULL;
1N/A e->e_flags |= EF_INQUEUE;
1N/A loseqfile(e, err);
1N/A return false;
1N/A}
1N/A/*
1N/A** PRTSTR -- print a string, "unprintable" characters are shown as \oct
1N/A**
1N/A** Parameters:
1N/A** s -- string to print
1N/A** ml -- maximum length of output
1N/A**
1N/A** Returns:
1N/A** number of entries
1N/A**
1N/A** Side Effects:
1N/A** Prints a string on stdout.
1N/A*/
1N/A
1N/Astatic void prtstr __P((char *, int));
1N/A
1N/Astatic void
1N/Aprtstr(s, ml)
1N/A char *s;
1N/A int ml;
1N/A{
1N/A int c;
1N/A
1N/A if (s == NULL)
1N/A return;
1N/A while (ml-- > 0 && ((c = *s++) != '\0'))
1N/A {
1N/A if (c == '\\')
1N/A {
1N/A if (ml-- > 0)
1N/A {
1N/A (void) sm_io_putc(smioout, SM_TIME_DEFAULT, c);
1N/A (void) sm_io_putc(smioout, SM_TIME_DEFAULT, c);
1N/A }
1N/A }
1N/A else if (isascii(c) && isprint(c))
1N/A (void) sm_io_putc(smioout, SM_TIME_DEFAULT, c);
1N/A else
1N/A {
1N/A if ((ml -= 3) > 0)
1N/A (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
1N/A "\\%03o", c & 0xFF);
1N/A }
1N/A }
1N/A}
1N/A/*
1N/A** PRINTNQE -- print out number of entries in the mail queue
1N/A**
1N/A** Parameters:
1N/A** out -- output file pointer.
1N/A** prefix -- string to output in front of each line.
1N/A**
1N/A** Returns:
1N/A** none.
1N/A*/
1N/A
1N/Avoid
1N/Aprintnqe(out, prefix)
1N/A SM_FILE_T *out;
1N/A char *prefix;
1N/A{
1N/A#if SM_CONF_SHM
1N/A int i, k = 0, nrequests = 0;
1N/A bool unknown = false;
1N/A
1N/A if (ShmId == SM_SHM_NO_ID)
1N/A {
1N/A if (prefix == NULL)
1N/A (void) sm_io_fprintf(out, SM_TIME_DEFAULT,
1N/A "Data unavailable: shared memory not updated\n");
1N/A else
1N/A (void) sm_io_fprintf(out, SM_TIME_DEFAULT,
1N/A "%sNOTCONFIGURED:-1\r\n", prefix);
1N/A return;
1N/A }
1N/A for (i = 0; i < NumQueue && Queue[i] != NULL; i++)
1N/A {
1N/A int j;
1N/A
1N/A k++;
1N/A for (j = 0; j < Queue[i]->qg_numqueues; j++)
1N/A {
1N/A int n;
1N/A
1N/A if (StopRequest)
1N/A stop_sendmail();
1N/A
1N/A n = QSHM_ENTRIES(Queue[i]->qg_qpaths[j].qp_idx);
1N/A if (prefix != NULL)
1N/A (void) sm_io_fprintf(out, SM_TIME_DEFAULT,
1N/A "%s%s:%d\r\n",
1N/A prefix, qid_printqueue(i, j), n);
1N/A else if (n < 0)
1N/A {
1N/A (void) sm_io_fprintf(out, SM_TIME_DEFAULT,
1N/A "%s: unknown number of entries\n",
1N/A qid_printqueue(i, j));
1N/A unknown = true;
1N/A }
1N/A else if (n == 0)
1N/A {
1N/A (void) sm_io_fprintf(out, SM_TIME_DEFAULT,
1N/A "%s is empty\n",
1N/A qid_printqueue(i, j));
1N/A }
1N/A else if (n > 0)
1N/A {
1N/A (void) sm_io_fprintf(out, SM_TIME_DEFAULT,
1N/A "%s: entries=%d\n",
1N/A qid_printqueue(i, j), n);
1N/A nrequests += n;
1N/A k++;
1N/A }
1N/A }
1N/A }
1N/A if (prefix == NULL && k > 1)
1N/A (void) sm_io_fprintf(out, SM_TIME_DEFAULT,
1N/A "\t\tTotal requests: %d%s\n",
1N/A nrequests, unknown ? " (about)" : "");
1N/A#else /* SM_CONF_SHM */
1N/A if (prefix == NULL)
1N/A (void) sm_io_fprintf(out, SM_TIME_DEFAULT,
1N/A "Data unavailable without shared memory support\n");
1N/A else
1N/A (void) sm_io_fprintf(out, SM_TIME_DEFAULT,
1N/A "%sNOTAVAILABLE:-1\r\n", prefix);
1N/A#endif /* SM_CONF_SHM */
1N/A}
1N/A/*
1N/A** PRINTQUEUE -- print out a representation of the mail queue
1N/A**
1N/A** Parameters:
1N/A** none.
1N/A**
1N/A** Returns:
1N/A** none.
1N/A**
1N/A** Side Effects:
1N/A** Prints a listing of the mail queue on the standard output.
1N/A*/
1N/A
1N/Avoid
1N/Aprintqueue()
1N/A{
1N/A int i, k = 0, nrequests = 0;
1N/A
1N/A for (i = 0; i < NumQueue && Queue[i] != NULL; i++)
1N/A {
1N/A int j;
1N/A
1N/A k++;
1N/A for (j = 0; j < Queue[i]->qg_numqueues; j++)
1N/A {
1N/A if (StopRequest)
1N/A stop_sendmail();
1N/A nrequests += print_single_queue(i, j);
1N/A k++;
1N/A }
1N/A }
1N/A if (k > 1)
1N/A (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
1N/A "\t\tTotal requests: %d\n",
1N/A nrequests);
1N/A}
1N/A/*
1N/A** PRINT_SINGLE_QUEUE -- print out a representation of a single mail queue
1N/A**
1N/A** Parameters:
1N/A** qgrp -- the index of the queue group.
1N/A** qdir -- the queue directory.
1N/A**
1N/A** Returns:
1N/A** number of requests in mail queue.
1N/A**
1N/A** Side Effects:
1N/A** Prints a listing of the mail queue on the standard output.
1N/A*/
1N/A
1N/Aint
1N/Aprint_single_queue(qgrp, qdir)
1N/A int qgrp;
1N/A int qdir;
1N/A{
1N/A register WORK *w;
1N/A SM_FILE_T *f;
1N/A int nrequests;
1N/A char qd[MAXPATHLEN];
1N/A char qddf[MAXPATHLEN];
1N/A char buf[MAXLINE];
1N/A
1N/A if (qdir == NOQDIR)
1N/A {
1N/A (void) sm_strlcpy(qd, ".", sizeof(qd));
1N/A (void) sm_strlcpy(qddf, ".", sizeof(qddf));
1N/A }
1N/A else
1N/A {
1N/A (void) sm_strlcpyn(qd, sizeof(qd), 2,
1N/A Queue[qgrp]->qg_qpaths[qdir].qp_name,
1N/A (bitset(QP_SUBQF,
1N/A Queue[qgrp]->qg_qpaths[qdir].qp_subdirs)
1N/A ? "/qf" : ""));
1N/A (void) sm_strlcpyn(qddf, sizeof(qddf), 2,
1N/A Queue[qgrp]->qg_qpaths[qdir].qp_name,
1N/A (bitset(QP_SUBDF,
1N/A Queue[qgrp]->qg_qpaths[qdir].qp_subdirs)
1N/A ? "/df" : ""));
1N/A }
1N/A
1N/A /*
1N/A ** Check for permission to print the queue
1N/A */
1N/A
1N/A if (bitset(PRIV_RESTRICTMAILQ, PrivacyFlags) && RealUid != 0)
1N/A {
1N/A struct stat st;
1N/A#ifdef NGROUPS_MAX
1N/A int n;
1N/A extern GIDSET_T InitialGidSet[NGROUPS_MAX];
1N/A#endif /* NGROUPS_MAX */
1N/A
1N/A if (stat(qd, &st) < 0)
1N/A {
1N/A syserr("Cannot stat %s",
1N/A qid_printqueue(qgrp, qdir));
1N/A return 0;
1N/A }
1N/A#ifdef NGROUPS_MAX
1N/A n = NGROUPS_MAX;
1N/A while (--n >= 0)
1N/A {
1N/A if (InitialGidSet[n] == st.st_gid)
1N/A break;
1N/A }
1N/A if (n < 0 && RealGid != st.st_gid)
1N/A#else /* NGROUPS_MAX */
1N/A if (RealGid != st.st_gid)
1N/A#endif /* NGROUPS_MAX */
1N/A {
1N/A usrerr("510 You are not permitted to see the queue");
1N/A setstat(EX_NOPERM);
1N/A return 0;
1N/A }
1N/A }
1N/A
1N/A /*
1N/A ** Read and order the queue.
1N/A */
1N/A
1N/A nrequests = gatherq(qgrp, qdir, true, NULL, NULL, NULL);
1N/A (void) sortq(Queue[qgrp]->qg_maxlist);
1N/A
1N/A /*
1N/A ** Print the work list that we have read.
1N/A */
1N/A
1N/A /* first see if there is anything */
1N/A if (nrequests <= 0)
1N/A {
1N/A (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "%s is empty\n",
1N/A qid_printqueue(qgrp, qdir));
1N/A return 0;
1N/A }
1N/A
1N/A sm_getla(); /* get load average */
1N/A
1N/A (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "\t\t%s (%d request%s",
1N/A qid_printqueue(qgrp, qdir),
1N/A nrequests, nrequests == 1 ? "" : "s");
1N/A if (MaxQueueRun > 0 && nrequests > MaxQueueRun)
1N/A (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
1N/A ", only %d printed", MaxQueueRun);
1N/A if (Verbose)
1N/A (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
1N/A ")\n-----Q-ID----- --Size-- -Priority- ---Q-Time--- --------Sender/Recipient--------\n");
1N/A else
1N/A (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
1N/A ")\n-----Q-ID----- --Size-- -----Q-Time----- ------------Sender/Recipient-----------\n");
1N/A for (w = WorkQ; w != NULL; w = w->w_next)
1N/A {
1N/A struct stat st;
1N/A auto time_t submittime = 0;
1N/A long dfsize;
1N/A int flags = 0;
1N/A int qfver;
1N/A char quarmsg[MAXLINE];
1N/A char statmsg[MAXLINE];
1N/A char bodytype[MAXNAME + 1];
1N/A char qf[MAXPATHLEN];
1N/A
1N/A if (StopRequest)
1N/A stop_sendmail();
1N/A
1N/A (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "%13s",
1N/A w->w_name + 2);
1N/A (void) sm_strlcpyn(qf, sizeof(qf), 3, qd, "/", w->w_name);
1N/A f = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, qf, SM_IO_RDONLY_B,
1N/A NULL);
1N/A if (f == NULL)
1N/A {
1N/A if (errno == EPERM)
1N/A (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
1N/A " (permission denied)\n");
1N/A else if (errno == ENOENT)
1N/A (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
1N/A " (job completed)\n");
1N/A else
1N/A (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
1N/A " (%s)\n",
1N/A sm_errstring(errno));
1N/A errno = 0;
1N/A continue;
1N/A }
1N/A w->w_name[0] = DATAFL_LETTER;
1N/A (void) sm_strlcpyn(qf, sizeof(qf), 3, qddf, "/", w->w_name);
1N/A if (stat(qf, &st) >= 0)
1N/A dfsize = st.st_size;
1N/A else
1N/A {
1N/A ENVELOPE e;
1N/A
1N/A /*
1N/A ** Maybe the df file can't be statted because
1N/A ** it is in a different directory than the qf file.
1N/A ** In order to find out, we must read the qf file.
1N/A */
1N/A
1N/A newenvelope(&e, &BlankEnvelope, sm_rpool_new_x(NULL));
1N/A e.e_id = w->w_name + 2;
1N/A e.e_qgrp = qgrp;
1N/A e.e_qdir = qdir;
1N/A dfsize = -1;
1N/A if (readqf(&e, false))
1N/A {
1N/A char *df = queuename(&e, DATAFL_LETTER);
1N/A if (stat(df, &st) >= 0)
1N/A dfsize = st.st_size;
1N/A }
1N/A if (e.e_lockfp != NULL)
1N/A {
1N/A (void) sm_io_close(e.e_lockfp, SM_TIME_DEFAULT);
1N/A e.e_lockfp = NULL;
1N/A }
1N/A clearenvelope(&e, false, e.e_rpool);
1N/A sm_rpool_free(e.e_rpool);
1N/A }
1N/A if (w->w_lock)
1N/A (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "*");
1N/A else if (QueueMode == QM_LOST)
1N/A (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "?");
1N/A else if (w->w_tooyoung)
1N/A (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "-");
1N/A else if (shouldqueue(w->w_pri, w->w_ctime))
1N/A (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "X");
1N/A else
1N/A (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, " ");
1N/A
1N/A errno = 0;
1N/A
1N/A quarmsg[0] = '\0';
1N/A statmsg[0] = bodytype[0] = '\0';
1N/A qfver = 0;
1N/A while (sm_io_fgets(f, SM_TIME_DEFAULT, buf, sizeof(buf)) != NULL)
1N/A {
1N/A register int i;
1N/A register char *p;
1N/A
1N/A if (StopRequest)
1N/A stop_sendmail();
1N/A
1N/A fixcrlf(buf, true);
1N/A switch (buf[0])
1N/A {
1N/A case 'V': /* queue file version */
1N/A qfver = atoi(&buf[1]);
1N/A break;
1N/A
1N/A case 'M': /* error message */
1N/A if ((i = strlen(&buf[1])) >= sizeof(statmsg))
1N/A i = sizeof(statmsg) - 1;
1N/A memmove(statmsg, &buf[1], i);
1N/A statmsg[i] = '\0';
1N/A break;
1N/A
1N/A case 'q': /* quarantine reason */
1N/A if ((i = strlen(&buf[1])) >= sizeof(quarmsg))
1N/A i = sizeof(quarmsg) - 1;
1N/A memmove(quarmsg, &buf[1], i);
1N/A quarmsg[i] = '\0';
1N/A break;
1N/A
1N/A case 'B': /* body type */
1N/A if ((i = strlen(&buf[1])) >= sizeof(bodytype))
1N/A i = sizeof(bodytype) - 1;
1N/A memmove(bodytype, &buf[1], i);
1N/A bodytype[i] = '\0';
1N/A break;
1N/A
1N/A case 'S': /* sender name */
1N/A if (Verbose)
1N/A {
1N/A (void) sm_io_fprintf(smioout,
1N/A SM_TIME_DEFAULT,
1N/A "%8ld %10ld%c%.12s ",
1N/A dfsize,
1N/A w->w_pri,
1N/A bitset(EF_WARNING, flags)
1N/A ? '+' : ' ',
1N/A ctime(&submittime) + 4);
1N/A prtstr(&buf[1], 78);
1N/A }
1N/A else
1N/A {
1N/A (void) sm_io_fprintf(smioout,
1N/A SM_TIME_DEFAULT,
1N/A "%8ld %.16s ",
1N/A dfsize,
1N/A ctime(&submittime));
1N/A prtstr(&buf[1], 39);
1N/A }
1N/A
1N/A if (quarmsg[0] != '\0')
1N/A {
1N/A (void) sm_io_fprintf(smioout,
1N/A SM_TIME_DEFAULT,
1N/A "\n QUARANTINE: %.*s",
1N/A Verbose ? 100 : 60,
1N/A quarmsg);
1N/A quarmsg[0] = '\0';
1N/A }
1N/A
1N/A if (statmsg[0] != '\0' || bodytype[0] != '\0')
1N/A {
1N/A (void) sm_io_fprintf(smioout,
1N/A SM_TIME_DEFAULT,
1N/A "\n %10.10s",
1N/A bodytype);
1N/A if (statmsg[0] != '\0')
1N/A (void) sm_io_fprintf(smioout,
1N/A SM_TIME_DEFAULT,
1N/A " (%.*s)",
1N/A Verbose ? 100 : 60,
1N/A statmsg);
1N/A statmsg[0] = '\0';
1N/A }
1N/A break;
1N/A
1N/A case 'C': /* controlling user */
1N/A if (Verbose)
1N/A (void) sm_io_fprintf(smioout,
1N/A SM_TIME_DEFAULT,
1N/A "\n\t\t\t\t\t\t(---%.64s---)",
1N/A &buf[1]);
1N/A break;
1N/A
1N/A case 'R': /* recipient name */
1N/A p = &buf[1];
1N/A if (qfver >= 1)
1N/A {
1N/A p = strchr(p, ':');
1N/A if (p == NULL)
1N/A break;
1N/A p++;
1N/A }
1N/A if (Verbose)
1N/A {
1N/A (void) sm_io_fprintf(smioout,
1N/A SM_TIME_DEFAULT,
1N/A "\n\t\t\t\t\t\t");
1N/A prtstr(p, 71);
1N/A }
1N/A else
1N/A {
1N/A (void) sm_io_fprintf(smioout,
1N/A SM_TIME_DEFAULT,
1N/A "\n\t\t\t\t\t ");
1N/A prtstr(p, 38);
1N/A }
1N/A if (Verbose && statmsg[0] != '\0')
1N/A {
1N/A (void) sm_io_fprintf(smioout,
1N/A SM_TIME_DEFAULT,
1N/A "\n\t\t (%.100s)",
1N/A statmsg);
1N/A statmsg[0] = '\0';
1N/A }
1N/A break;
1N/A
1N/A case 'T': /* creation time */
1N/A submittime = atol(&buf[1]);
1N/A break;
1N/A
1N/A case 'F': /* flag bits */
1N/A for (p = &buf[1]; *p != '\0'; p++)
1N/A {
1N/A switch (*p)
1N/A {
1N/A case 'w':
1N/A flags |= EF_WARNING;
1N/A break;
1N/A }
1N/A }
1N/A }
1N/A }
1N/A if (submittime == (time_t) 0)
1N/A (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
1N/A " (no control file)");
1N/A (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "\n");
1N/A (void) sm_io_close(f, SM_TIME_DEFAULT);
1N/A }
1N/A return nrequests;
1N/A}
1N/A
1N/A/*
1N/A** QUEUE_LETTER -- get the proper queue letter for the current QueueMode.
1N/A**
1N/A** Parameters:
1N/A** e -- envelope to build it in/from.
1N/A** type -- the file type, used as the first character
1N/A** of the file name.
1N/A**
1N/A** Returns:
1N/A** the letter to use
1N/A*/
1N/A
1N/Astatic char
1N/Aqueue_letter(e, type)
1N/A ENVELOPE *e;
1N/A int type;
1N/A{
1N/A /* Change type according to QueueMode */
1N/A if (type == ANYQFL_LETTER)
1N/A {
1N/A if (e->e_quarmsg != NULL)
1N/A type = QUARQF_LETTER;
1N/A else
1N/A {
1N/A switch (QueueMode)
1N/A {
1N/A case QM_NORMAL:
1N/A type = NORMQF_LETTER;
1N/A break;
1N/A
1N/A case QM_QUARANTINE:
1N/A type = QUARQF_LETTER;
1N/A break;
1N/A
1N/A case QM_LOST:
1N/A type = LOSEQF_LETTER;
1N/A break;
1N/A
1N/A default:
1N/A /* should never happen */
1N/A abort();
1N/A /* NOTREACHED */
1N/A }
1N/A }
1N/A }
1N/A return type;
1N/A}
1N/A
1N/A/*
1N/A** QUEUENAME -- build a file name in the queue directory for this envelope.
1N/A**
1N/A** Parameters:
1N/A** e -- envelope to build it in/from.
1N/A** type -- the file type, used as the first character
1N/A** of the file name.
1N/A**
1N/A** Returns:
1N/A** a pointer to the queue name (in a static buffer).
1N/A**
1N/A** Side Effects:
1N/A** If no id code is already assigned, queuename() will
1N/A** assign an id code with assign_queueid(). If no queue
1N/A** directory is assigned, one will be set with setnewqueue().
1N/A*/
1N/A
1N/Achar *
1N/Aqueuename(e, type)
1N/A register ENVELOPE *e;
1N/A int type;
1N/A{
1N/A int qd, qg;
1N/A char *sub = "/";
1N/A char pref[3];
1N/A static char buf[MAXPATHLEN];
1N/A
1N/A /* Assign an ID if needed */
1N/A if (e->e_id == NULL)
1N/A {
1N/A if (IntSig)
1N/A return NULL;
1N/A assign_queueid(e);
1N/A }
1N/A type = queue_letter(e, type);
1N/A
1N/A /* begin of filename */
1N/A pref[0] = (char) type;
1N/A pref[1] = 'f';
1N/A pref[2] = '\0';
1N/A
1N/A /* Assign a queue group/directory if needed */
1N/A if (type == XSCRPT_LETTER)
1N/A {
1N/A /*
1N/A ** We don't want to call setnewqueue() if we are fetching
1N/A ** the pathname of the transcript file, because setnewqueue
1N/A ** chooses a queue, and sometimes we need to write to the
1N/A ** transcript file before we have gathered enough information
1N/A ** to choose a queue.
1N/A */
1N/A
1N/A if (e->e_xfqgrp == NOQGRP || e->e_xfqdir == NOQDIR)
1N/A {
1N/A if (e->e_qgrp != NOQGRP && e->e_qdir != NOQDIR)
1N/A {
1N/A e->e_xfqgrp = e->e_qgrp;
1N/A e->e_xfqdir = e->e_qdir;
1N/A }
1N/A else
1N/A {
1N/A e->e_xfqgrp = 0;
1N/A if (Queue[e->e_xfqgrp]->qg_numqueues <= 1)
1N/A e->e_xfqdir = 0;
1N/A else
1N/A {
1N/A e->e_xfqdir = get_rand_mod(
1N/A Queue[e->e_xfqgrp]->qg_numqueues);
1N/A }
1N/A }
1N/A }
1N/A qd = e->e_xfqdir;
1N/A qg = e->e_xfqgrp;
1N/A }
1N/A else
1N/A {
1N/A if (e->e_qgrp == NOQGRP || e->e_qdir == NOQDIR)
1N/A {
1N/A if (IntSig)
1N/A return NULL;
1N/A (void) setnewqueue(e);
1N/A }
1N/A if (type == DATAFL_LETTER)
1N/A {
1N/A qd = e->e_dfqdir;
1N/A qg = e->e_dfqgrp;
1N/A }
1N/A else
1N/A {
1N/A qd = e->e_qdir;
1N/A qg = e->e_qgrp;
1N/A }
1N/A }
1N/A
1N/A /* xf files always have a valid qd and qg picked above */
1N/A if ((qd == NOQDIR || qg == NOQGRP) && type != XSCRPT_LETTER)
1N/A (void) sm_strlcpyn(buf, sizeof(buf), 2, pref, e->e_id);
1N/A else
1N/A {
1N/A switch (type)
1N/A {
1N/A case DATAFL_LETTER:
1N/A if (bitset(QP_SUBDF, Queue[qg]->qg_qpaths[qd].qp_subdirs))
1N/A sub = "/df/";
1N/A break;
1N/A
1N/A case QUARQF_LETTER:
1N/A case TEMPQF_LETTER:
1N/A case NEWQFL_LETTER:
1N/A case LOSEQF_LETTER:
1N/A case NORMQF_LETTER:
1N/A if (bitset(QP_SUBQF, Queue[qg]->qg_qpaths[qd].qp_subdirs))
1N/A sub = "/qf/";
1N/A break;
1N/A
1N/A case XSCRPT_LETTER:
1N/A if (bitset(QP_SUBXF, Queue[qg]->qg_qpaths[qd].qp_subdirs))
1N/A sub = "/xf/";
1N/A break;
1N/A
1N/A default:
1N/A if (IntSig)
1N/A return NULL;
1N/A sm_abort("queuename: bad queue file type %d", type);
1N/A }
1N/A
1N/A (void) sm_strlcpyn(buf, sizeof(buf), 4,
1N/A Queue[qg]->qg_qpaths[qd].qp_name,
1N/A sub, pref, e->e_id);
1N/A }
1N/A
1N/A if (tTd(7, 2))
1N/A sm_dprintf("queuename: %s\n", buf);
1N/A return buf;
1N/A}
1N/A
1N/A/*
1N/A** INIT_QID_ALG -- Initialize the (static) parameters that are used to
1N/A** generate a queue ID.
1N/A**
1N/A** This function is called by the daemon to reset
1N/A** LastQueueTime and LastQueuePid which are used by assign_queueid().
1N/A** Otherwise the algorithm may cause problems because
1N/A** LastQueueTime and LastQueuePid are set indirectly by main()
1N/A** before the daemon process is started, hence LastQueuePid is not
1N/A** the pid of the daemon and therefore a child of the daemon can
1N/A** actually have the same pid as LastQueuePid which means the section
1N/A** in assign_queueid():
1N/A** * see if we need to get a new base time/pid *
1N/A** is NOT triggered which will cause the same queue id to be generated.
1N/A**
1N/A** Parameters:
1N/A** none
1N/A**
1N/A** Returns:
1N/A** none.
1N/A*/
1N/A
1N/Avoid
1N/Ainit_qid_alg()
1N/A{
1N/A LastQueueTime = 0;
1N/A LastQueuePid = -1;
1N/A}
1N/A
1N/A/*
1N/A** ASSIGN_QUEUEID -- assign a queue ID for this envelope.
1N/A**
1N/A** Assigns an id code if one does not already exist.
1N/A** This code assumes that nothing will remain in the queue for
1N/A** longer than 60 years. It is critical that files with the given
1N/A** name do not already exist in the queue.
1N/A** [No longer initializes e_qdir to NOQDIR.]
1N/A**
1N/A** Parameters:
1N/A** e -- envelope to set it in.
1N/A**
1N/A** Returns:
1N/A** none.
1N/A*/
1N/A
1N/Astatic const char QueueIdChars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
1N/A# define QIC_LEN 60
1N/A# define QIC_LEN_R 62
1N/A
1N/A/*
1N/A** Note: the length is "officially" 60 because minutes and seconds are
1N/A** usually only 0-59. However (Linux):
1N/A** tm_sec The number of seconds after the minute, normally in
1N/A** the range 0 to 59, but can be up to 61 to allow for
1N/A** leap seconds.
1N/A** Hence the real length of the string is 62 to take this into account.
1N/A** Alternatively % QIC_LEN can (should) be used for access everywhere.
1N/A*/
1N/A
1N/A# define queuenextid() CurrentPid
1N/A#define QIC_LEN_SQR (QIC_LEN * QIC_LEN)
1N/A
1N/Avoid
1N/Aassign_queueid(e)
1N/A register ENVELOPE *e;
1N/A{
1N/A pid_t pid = queuenextid();
1N/A static unsigned int cX = 0;
1N/A static unsigned int random_offset;
1N/A struct tm *tm;
1N/A char idbuf[MAXQFNAME - 2];
1N/A unsigned int seq;
1N/A
1N/A if (e->e_id != NULL)
1N/A return;
1N/A
1N/A /* see if we need to get a new base time/pid */
1N/A if (cX >= QIC_LEN_SQR || LastQueueTime == 0 || LastQueuePid != pid)
1N/A {
1N/A time_t then = LastQueueTime;
1N/A
1N/A /* if the first time through, pick a random offset */
1N/A if (LastQueueTime == 0)
1N/A random_offset = ((unsigned int)get_random())
1N/A % QIC_LEN_SQR;
1N/A
1N/A while ((LastQueueTime = curtime()) == then &&
1N/A LastQueuePid == pid)
1N/A {
1N/A (void) sleep(1);
1N/A }
1N/A LastQueuePid = queuenextid();
1N/A cX = 0;
1N/A }
1N/A
1N/A /*
1N/A ** Generate a new sequence number between 0 and QIC_LEN_SQR-1.
1N/A ** This lets us generate up to QIC_LEN_SQR unique queue ids
1N/A ** per second, per process. With envelope splitting,
1N/A ** a single message can consume many queue ids.
1N/A */
1N/A
1N/A seq = (cX + random_offset) % QIC_LEN_SQR;
1N/A ++cX;
1N/A if (tTd(7, 50))
1N/A sm_dprintf("assign_queueid: random_offset=%u (%u)\n",
1N/A random_offset, seq);
1N/A
1N/A tm = gmtime(&LastQueueTime);
1N/A idbuf[0] = QueueIdChars[tm->tm_year % QIC_LEN];
1N/A idbuf[1] = QueueIdChars[tm->tm_mon];
1N/A idbuf[2] = QueueIdChars[tm->tm_mday];
1N/A idbuf[3] = QueueIdChars[tm->tm_hour];
1N/A idbuf[4] = QueueIdChars[tm->tm_min % QIC_LEN_R];
1N/A idbuf[5] = QueueIdChars[tm->tm_sec % QIC_LEN_R];
1N/A idbuf[6] = QueueIdChars[seq / QIC_LEN];
1N/A idbuf[7] = QueueIdChars[seq % QIC_LEN];
1N/A (void) sm_snprintf(&idbuf[8], sizeof(idbuf) - 8, "%06d",
1N/A (int) LastQueuePid);
1N/A e->e_id = sm_rpool_strdup_x(e->e_rpool, idbuf);
1N/A macdefine(&e->e_macro, A_PERM, 'i', e->e_id);
1N/A#if 0
1N/A /* XXX: inherited from MainEnvelope */
1N/A e->e_qgrp = NOQGRP; /* too early to do anything else */
1N/A e->e_qdir = NOQDIR;
1N/A e->e_xfqgrp = NOQGRP;
1N/A#endif /* 0 */
1N/A
1N/A /* New ID means it's not on disk yet */
1N/A e->e_qfletter = '\0';
1N/A
1N/A if (tTd(7, 1))
1N/A sm_dprintf("assign_queueid: assigned id %s, e=%p\n",
1N/A e->e_id, e);
1N/A if (LogLevel > 93)
1N/A sm_syslog(LOG_DEBUG, e->e_id, "assigned id");
1N/A}
1N/A/*
1N/A** SYNC_QUEUE_TIME -- Assure exclusive PID in any given second
1N/A**
1N/A** Make sure one PID can't be used by two processes in any one second.
1N/A**
1N/A** If the system rotates PIDs fast enough, may get the
1N/A** same pid in the same second for two distinct processes.
1N/A** This will interfere with the queue file naming system.
1N/A**
1N/A** Parameters:
1N/A** none
1N/A**
1N/A** Returns:
1N/A** none
1N/A*/
1N/A
1N/Avoid
1N/Async_queue_time()
1N/A{
1N/A#if FAST_PID_RECYCLE
1N/A if (OpMode != MD_TEST &&
1N/A OpMode != MD_CHECKCONFIG &&
1N/A OpMode != MD_VERIFY &&
1N/A LastQueueTime > 0 &&
1N/A LastQueuePid == CurrentPid &&
1N/A curtime() == LastQueueTime)
1N/A (void) sleep(1);
1N/A#endif /* FAST_PID_RECYCLE */
1N/A}
1N/A/*
1N/A** UNLOCKQUEUE -- unlock the queue entry for a specified envelope
1N/A**
1N/A** Parameters:
1N/A** e -- the envelope to unlock.
1N/A**
1N/A** Returns:
1N/A** none
1N/A**
1N/A** Side Effects:
1N/A** unlocks the queue for `e'.
1N/A*/
1N/A
1N/Avoid
1N/Aunlockqueue(e)
1N/A ENVELOPE *e;
1N/A{
1N/A if (tTd(51, 4))
1N/A sm_dprintf("unlockqueue(%s)\n",
1N/A e->e_id == NULL ? "NOQUEUE" : e->e_id);
1N/A
1N/A
1N/A /* if there is a lock file in the envelope, close it */
1N/A if (e->e_lockfp != NULL)
1N/A (void) sm_io_close(e->e_lockfp, SM_TIME_DEFAULT);
1N/A e->e_lockfp = NULL;
1N/A
1N/A /* don't create a queue id if we don't already have one */
1N/A if (e->e_id == NULL)
1N/A return;
1N/A
1N/A /* remove the transcript */
1N/A if (LogLevel > 87)
1N/A sm_syslog(LOG_DEBUG, e->e_id, "unlock");
1N/A if (!tTd(51, 104))
1N/A (void) xunlink(queuename(e, XSCRPT_LETTER));
1N/A}
1N/A/*
1N/A** SETCTLUSER -- create a controlling address
1N/A**
1N/A** Create a fake "address" given only a local login name; this is
1N/A** used as a "controlling user" for future recipient addresses.
1N/A**
1N/A** Parameters:
1N/A** user -- the user name of the controlling user.
1N/A** qfver -- the version stamp of this queue file.
1N/A** e -- envelope
1N/A**
1N/A** Returns:
1N/A** An address descriptor for the controlling user,
1N/A** using storage allocated from e->e_rpool.
1N/A**
1N/A*/
1N/A
1N/Astatic ADDRESS *
1N/Asetctluser(user, qfver, e)
1N/A char *user;
1N/A int qfver;
1N/A ENVELOPE *e;
1N/A{
1N/A register ADDRESS *a;
1N/A struct passwd *pw;
1N/A char *p;
1N/A
1N/A /*
1N/A ** See if this clears our concept of controlling user.
1N/A */
1N/A
1N/A if (user == NULL || *user == '\0')
1N/A return NULL;
1N/A
1N/A /*
1N/A ** Set up addr fields for controlling user.
1N/A */
1N/A
1N/A a = (ADDRESS *) sm_rpool_malloc_x(e->e_rpool, sizeof(*a));
1N/A memset((char *) a, '\0', sizeof(*a));
1N/A
1N/A if (*user == ':')
1N/A {
1N/A p = &user[1];
1N/A a->q_user = sm_rpool_strdup_x(e->e_rpool, p);
1N/A }
1N/A else
1N/A {
1N/A p = strtok(user, ":");
1N/A a->q_user = sm_rpool_strdup_x(e->e_rpool, user);
1N/A if (qfver >= 2)
1N/A {
1N/A if ((p = strtok(NULL, ":")) != NULL)
1N/A a->q_uid = atoi(p);
1N/A if ((p = strtok(NULL, ":")) != NULL)
1N/A a->q_gid = atoi(p);
1N/A if ((p = strtok(NULL, ":")) != NULL)
1N/A {
1N/A char *o;
1N/A
1N/A a->q_flags |= QGOODUID;
1N/A
1N/A /* if there is another ':': restore it */
1N/A if ((o = strtok(NULL, ":")) != NULL && o > p)
1N/A o[-1] = ':';
1N/A }
1N/A }
1N/A else if ((pw = sm_getpwnam(user)) != NULL)
1N/A {
1N/A if (*pw->pw_dir == '\0')
1N/A a->q_home = NULL;
1N/A else if (strcmp(pw->pw_dir, "/") == 0)
1N/A a->q_home = "";
1N/A else
1N/A a->q_home = sm_rpool_strdup_x(e->e_rpool, pw->pw_dir);
1N/A a->q_uid = pw->pw_uid;
1N/A a->q_gid = pw->pw_gid;
1N/A a->q_flags |= QGOODUID;
1N/A }
1N/A }
1N/A
1N/A a->q_flags |= QPRIMARY; /* flag as a "ctladdr" */
1N/A a->q_mailer = LocalMailer;
1N/A if (p == NULL)
1N/A a->q_paddr = sm_rpool_strdup_x(e->e_rpool, a->q_user);
1N/A else
1N/A a->q_paddr = sm_rpool_strdup_x(e->e_rpool, p);
1N/A return a;
1N/A}
1N/A/*
1N/A** LOSEQFILE -- rename queue file with LOSEQF_LETTER & try to let someone know
1N/A**
1N/A** Parameters:
1N/A** e -- the envelope (e->e_id will be used).
1N/A** why -- reported to whomever can hear.
1N/A**
1N/A** Returns:
1N/A** none.
1N/A*/
1N/A
1N/Avoid
1N/Aloseqfile(e, why)
1N/A register ENVELOPE *e;
1N/A char *why;
1N/A{
1N/A bool loseit = true;
1N/A char *p;
1N/A char buf[MAXPATHLEN];
1N/A
1N/A if (e == NULL || e->e_id == NULL)
1N/A return;
1N/A p = queuename(e, ANYQFL_LETTER);
1N/A if (sm_strlcpy(buf, p, sizeof(buf)) >= sizeof(buf))
1N/A return;
1N/A if (!bitset(EF_INQUEUE, e->e_flags))
1N/A queueup(e, false, true);
1N/A else if (QueueMode == QM_LOST)
1N/A loseit = false;
1N/A
1N/A /* if already lost, no need to re-lose */
1N/A if (loseit)
1N/A {
1N/A p = queuename(e, LOSEQF_LETTER);
1N/A if (rename(buf, p) < 0)
1N/A syserr("cannot rename(%s, %s), uid=%d",
1N/A buf, p, (int) geteuid());
1N/A else if (LogLevel > 0)
1N/A sm_syslog(LOG_ALERT, e->e_id,
1N/A "Losing %s: %s", buf, why);
1N/A }
1N/A if (e->e_dfp != NULL)
1N/A {
1N/A (void) sm_io_close(e->e_dfp, SM_TIME_DEFAULT);
1N/A e->e_dfp = NULL;
1N/A }
1N/A e->e_flags &= ~EF_HAS_DF;
1N/A}
1N/A/*
1N/A** NAME2QID -- translate a queue group name to a queue group id
1N/A**
1N/A** Parameters:
1N/A** queuename -- name of queue group.
1N/A**
1N/A** Returns:
1N/A** queue group id if found.
1N/A** NOQGRP otherwise.
1N/A*/
1N/A
1N/Aint
1N/Aname2qid(queuename)
1N/A char *queuename;
1N/A{
1N/A register STAB *s;
1N/A
1N/A s = stab(queuename, ST_QUEUE, ST_FIND);
1N/A if (s == NULL)
1N/A return NOQGRP;
1N/A return s->s_quegrp->qg_index;
1N/A}
1N/A/*
1N/A** QID_PRINTNAME -- create externally printable version of queue id
1N/A**
1N/A** Parameters:
1N/A** e -- the envelope.
1N/A**
1N/A** Returns:
1N/A** a printable version
1N/A*/
1N/A
1N/Achar *
1N/Aqid_printname(e)
1N/A ENVELOPE *e;
1N/A{
1N/A char *id;
1N/A static char idbuf[MAXQFNAME + 34];
1N/A
1N/A if (e == NULL)
1N/A return "";
1N/A
1N/A if (e->e_id == NULL)
1N/A id = "";
1N/A else
1N/A id = e->e_id;
1N/A
1N/A if (e->e_qdir == NOQDIR)
1N/A return id;
1N/A
1N/A (void) sm_snprintf(idbuf, sizeof(idbuf), "%.32s/%s",
1N/A Queue[e->e_qgrp]->qg_qpaths[e->e_qdir].qp_name,
1N/A id);
1N/A return idbuf;
1N/A}
1N/A/*
1N/A** QID_PRINTQUEUE -- create full version of queue directory for data files
1N/A**
1N/A** Parameters:
1N/A** qgrp -- index in queue group.
1N/A** qdir -- the short version of the queue directory
1N/A**
1N/A** Returns:
1N/A** the full pathname to the queue (might point to a static var)
1N/A*/
1N/A
1N/Achar *
1N/Aqid_printqueue(qgrp, qdir)
1N/A int qgrp;
1N/A int qdir;
1N/A{
1N/A char *subdir;
1N/A static char dir[MAXPATHLEN];
1N/A
1N/A if (qdir == NOQDIR)
1N/A return Queue[qgrp]->qg_qdir;
1N/A
1N/A if (strcmp(Queue[qgrp]->qg_qpaths[qdir].qp_name, ".") == 0)
1N/A subdir = NULL;
1N/A else
1N/A subdir = Queue[qgrp]->qg_qpaths[qdir].qp_name;
1N/A
1N/A (void) sm_strlcpyn(dir, sizeof(dir), 4,
1N/A Queue[qgrp]->qg_qdir,
1N/A subdir == NULL ? "" : "/",
1N/A subdir == NULL ? "" : subdir,
1N/A (bitset(QP_SUBDF,
1N/A Queue[qgrp]->qg_qpaths[qdir].qp_subdirs)
1N/A ? "/df" : ""));
1N/A return dir;
1N/A}
1N/A
1N/A/*
1N/A** PICKQDIR -- Pick a queue directory from a queue group
1N/A**
1N/A** Parameters:
1N/A** qg -- queue group
1N/A** fsize -- file size in bytes
1N/A** e -- envelope, or NULL
1N/A**
1N/A** Result:
1N/A** NOQDIR if no queue directory in qg has enough free space to
1N/A** hold a file of size 'fsize', otherwise the index of
1N/A** a randomly selected queue directory which resides on a
1N/A** file system with enough disk space.
1N/A** XXX This could be extended to select a queuedir with
1N/A** a few (the fewest?) number of entries. That data
1N/A** is available if shared memory is used.
1N/A**
1N/A** Side Effects:
1N/A** If the request fails and e != NULL then sm_syslog is called.
1N/A*/
1N/A
1N/Aint
1N/Apickqdir(qg, fsize, e)
1N/A QUEUEGRP *qg;
1N/A long fsize;
1N/A ENVELOPE *e;
1N/A{
1N/A int qdir;
1N/A int i;
1N/A long avail = 0;
1N/A
1N/A /* Pick a random directory, as a starting point. */
1N/A if (qg->qg_numqueues <= 1)
1N/A qdir = 0;
1N/A else
1N/A qdir = get_rand_mod(qg->qg_numqueues);
1N/A
1N/A#if _FFR_TESTS
1N/A if (tTd(4, 101))
1N/A return NOQDIR;
1N/A#endif /* _FFR_TESTS */
1N/A if (MinBlocksFree <= 0 && fsize <= 0)
1N/A return qdir;
1N/A
1N/A /*
1N/A ** Now iterate over the queue directories,
1N/A ** looking for a directory with enough space for this message.
1N/A */
1N/A
1N/A i = qdir;
1N/A do
1N/A {
1N/A QPATHS *qp = &qg->qg_qpaths[i];
1N/A long needed = 0;
1N/A long fsavail = 0;
1N/A
1N/A if (fsize > 0)
1N/A needed += fsize / FILE_SYS_BLKSIZE(qp->qp_fsysidx)
1N/A + ((fsize % FILE_SYS_BLKSIZE(qp->qp_fsysidx)
1N/A > 0) ? 1 : 0);
1N/A if (MinBlocksFree > 0)
1N/A needed += MinBlocksFree;
1N/A fsavail = FILE_SYS_AVAIL(qp->qp_fsysidx);
1N/A#if SM_CONF_SHM
1N/A if (fsavail <= 0)
1N/A {
1N/A long blksize;
1N/A
1N/A /*
1N/A ** might be not correctly updated,
1N/A ** let's try to get the info directly.
1N/A */
1N/A
1N/A fsavail = freediskspace(FILE_SYS_NAME(qp->qp_fsysidx),
1N/A &blksize);
1N/A if (fsavail < 0)
1N/A fsavail = 0;
1N/A }
1N/A#endif /* SM_CONF_SHM */
1N/A if (needed <= fsavail)
1N/A return i;
1N/A if (avail < fsavail)
1N/A avail = fsavail;
1N/A
1N/A if (qg->qg_numqueues > 0)
1N/A i = (i + 1) % qg->qg_numqueues;
1N/A } while (i != qdir);
1N/A
1N/A if (e != NULL && LogLevel > 0)
1N/A sm_syslog(LOG_ALERT, e->e_id,
1N/A "low on space (%s needs %ld bytes + %ld blocks in %s), max avail: %ld",
1N/A CurHostName == NULL ? "SMTP-DAEMON" : CurHostName,
1N/A fsize, MinBlocksFree,
1N/A qg->qg_qdir, avail);
1N/A return NOQDIR;
1N/A}
1N/A/*
1N/A** SETNEWQUEUE -- Sets a new queue group and directory
1N/A**
1N/A** Assign a queue group and directory to an envelope and store the
1N/A** directory in e->e_qdir.
1N/A**
1N/A** Parameters:
1N/A** e -- envelope to assign a queue for.
1N/A**
1N/A** Returns:
1N/A** true if successful
1N/A** false otherwise
1N/A**
1N/A** Side Effects:
1N/A** On success, e->e_qgrp and e->e_qdir are non-negative.
1N/A** On failure (not enough disk space),
1N/A** e->qgrp = NOQGRP, e->e_qdir = NOQDIR
1N/A** and usrerr() is invoked (which could raise an exception).
1N/A*/
1N/A
1N/Abool
1N/Asetnewqueue(e)
1N/A ENVELOPE *e;
1N/A{
1N/A if (tTd(41, 20))
1N/A sm_dprintf("setnewqueue: called\n");
1N/A
1N/A /* not set somewhere else */
1N/A if (e->e_qgrp == NOQGRP)
1N/A {
1N/A ADDRESS *q;
1N/A
1N/A /*
1N/A ** Use the queue group of the "first" recipient, as set by
1N/A ** the "queuegroup" rule set. If that is not defined, then
1N/A ** use the queue group of the mailer of the first recipient.
1N/A ** If that is not defined either, then use the default
1N/A ** queue group.
1N/A ** Notice: "first" depends on the sorting of sendqueue
1N/A ** in recipient().
1N/A ** To avoid problems with "bad" recipients look
1N/A ** for a valid address first.
1N/A */
1N/A
1N/A q = e->e_sendqueue;
1N/A while (q != NULL &&
1N/A (QS_IS_BADADDR(q->q_state) || QS_IS_DEAD(q->q_state)))
1N/A {
1N/A q = q->q_next;
1N/A }
1N/A if (q == NULL)
1N/A e->e_qgrp = 0;
1N/A else if (q->q_qgrp >= 0)
1N/A e->e_qgrp = q->q_qgrp;
1N/A else if (q->q_mailer != NULL &&
1N/A ISVALIDQGRP(q->q_mailer->m_qgrp))
1N/A e->e_qgrp = q->q_mailer->m_qgrp;
1N/A else
1N/A e->e_qgrp = 0;
1N/A e->e_dfqgrp = e->e_qgrp;
1N/A }
1N/A
1N/A if (ISVALIDQDIR(e->e_qdir) && ISVALIDQDIR(e->e_dfqdir))
1N/A {
1N/A if (tTd(41, 20))
1N/A sm_dprintf("setnewqueue: e_qdir already assigned (%s)\n",
1N/A qid_printqueue(e->e_qgrp, e->e_qdir));
1N/A return true;
1N/A }
1N/A
1N/A filesys_update();
1N/A e->e_qdir = pickqdir(Queue[e->e_qgrp], e->e_msgsize, e);
1N/A if (e->e_qdir == NOQDIR)
1N/A {
1N/A e->e_qgrp = NOQGRP;
1N/A if (!bitset(EF_FATALERRS, e->e_flags))
1N/A usrerr("452 4.4.5 Insufficient disk space; try again later");
1N/A e->e_flags |= EF_FATALERRS;
1N/A return false;
1N/A }
1N/A
1N/A if (tTd(41, 3))
1N/A sm_dprintf("setnewqueue: Assigned queue directory %s\n",
1N/A qid_printqueue(e->e_qgrp, e->e_qdir));
1N/A
1N/A if (e->e_xfqgrp == NOQGRP || e->e_xfqdir == NOQDIR)
1N/A {
1N/A e->e_xfqgrp = e->e_qgrp;
1N/A e->e_xfqdir = e->e_qdir;
1N/A }
1N/A e->e_dfqdir = e->e_qdir;
1N/A return true;
1N/A}
1N/A/*
1N/A** CHKQDIR -- check a queue directory
1N/A**
1N/A** Parameters:
1N/A** name -- name of queue directory
1N/A** sff -- flags for safefile()
1N/A**
1N/A** Returns:
1N/A** is it a queue directory?
1N/A*/
1N/A
1N/Astatic bool chkqdir __P((char *, long));
1N/A
1N/Astatic bool
1N/Achkqdir(name, sff)
1N/A char *name;
1N/A long sff;
1N/A{
1N/A struct stat statb;
1N/A int i;
1N/A
1N/A /* skip over . and .. directories */
1N/A if (name[0] == '.' &&
1N/A (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')))
1N/A return false;
1N/A#if HASLSTAT
1N/A if (lstat(name, &statb) < 0)
1N/A#else /* HASLSTAT */
1N/A if (stat(name, &statb) < 0)
1N/A#endif /* HASLSTAT */
1N/A {
1N/A if (tTd(41, 2))
1N/A sm_dprintf("chkqdir: stat(\"%s\"): %s\n",
1N/A name, sm_errstring(errno));
1N/A return false;
1N/A }
1N/A#if HASLSTAT
1N/A if (S_ISLNK(statb.st_mode))
1N/A {
1N/A /*
1N/A ** For a symlink we need to make sure the
1N/A ** target is a directory
1N/A */
1N/A
1N/A if (stat(name, &statb) < 0)
1N/A {
1N/A if (tTd(41, 2))
1N/A sm_dprintf("chkqdir: stat(\"%s\"): %s\n",
1N/A name, sm_errstring(errno));
1N/A return false;
1N/A }
1N/A }
1N/A#endif /* HASLSTAT */
1N/A
1N/A if (!S_ISDIR(statb.st_mode))
1N/A {
1N/A if (tTd(41, 2))
1N/A sm_dprintf("chkqdir: \"%s\": Not a directory\n",
1N/A name);
1N/A return false;
1N/A }
1N/A
1N/A /* Print a warning if unsafe (but still use it) */
1N/A /* XXX do this only if we want the warning? */
1N/A i = safedirpath(name, RunAsUid, RunAsGid, NULL, sff, 0, 0);
1N/A if (i != 0)
1N/A {
1N/A if (tTd(41, 2))
1N/A sm_dprintf("chkqdir: \"%s\": Not safe: %s\n",
1N/A name, sm_errstring(i));
1N/A#if _FFR_CHK_QUEUE
1N/A if (LogLevel > 8)
1N/A sm_syslog(LOG_WARNING, NOQID,
1N/A "queue directory \"%s\": Not safe: %s",
1N/A name, sm_errstring(i));
1N/A#endif /* _FFR_CHK_QUEUE */
1N/A }
1N/A return true;
1N/A}
1N/A/*
1N/A** MULTIQUEUE_CACHE -- cache a list of paths to queues.
1N/A**
1N/A** Each potential queue is checked as the cache is built.
1N/A** Thereafter, each is blindly trusted.
1N/A** Note that we can be called again after a timeout to rebuild
1N/A** (although code for that is not ready yet).
1N/A**
1N/A** Parameters:
1N/A** basedir -- base of all queue directories.
1N/A** blen -- strlen(basedir).
1N/A** qg -- queue group.
1N/A** qn -- number of queue directories already cached.
1N/A** phash -- pointer to hash value over queue dirs.
1N/A#if SM_CONF_SHM
1N/A** only used if shared memory is active.
1N/A#endif * SM_CONF_SHM *
1N/A**
1N/A** Returns:
1N/A** new number of queue directories.
1N/A*/
1N/A
1N/A#define INITIAL_SLOTS 20
1N/A#define ADD_SLOTS 10
1N/A
1N/Astatic int
1N/Amultiqueue_cache(basedir, blen, qg, qn, phash)
1N/A char *basedir;
1N/A int blen;
1N/A QUEUEGRP *qg;
1N/A int qn;
1N/A unsigned int *phash;
1N/A{
1N/A char *cp;
1N/A int i, len;
1N/A int slotsleft = 0;
1N/A long sff = SFF_ANYFILE;
1N/A char qpath[MAXPATHLEN];
1N/A char subdir[MAXPATHLEN];
1N/A char prefix[MAXPATHLEN]; /* dir relative to basedir */
1N/A
1N/A if (tTd(41, 20))
1N/A sm_dprintf("multiqueue_cache: called\n");
1N/A
1N/A /* Initialize to current directory */
1N/A prefix[0] = '.';
1N/A prefix[1] = '\0';
1N/A if (qg->qg_numqueues != 0 && qg->qg_qpaths != NULL)
1N/A {
1N/A for (i = 0; i < qg->qg_numqueues; i++)
1N/A {
1N/A if (qg->qg_qpaths[i].qp_name != NULL)
1N/A (void) sm_free(qg->qg_qpaths[i].qp_name); /* XXX */
1N/A }
1N/A (void) sm_free((char *) qg->qg_qpaths); /* XXX */
1N/A qg->qg_qpaths = NULL;
1N/A qg->qg_numqueues = 0;
1N/A }
1N/A
1N/A /* If running as root, allow safedirpath() checks to use privs */
1N/A if (RunAsUid == 0)
1N/A sff |= SFF_ROOTOK;
1N/A#if _FFR_CHK_QUEUE
1N/A sff |= SFF_SAFEDIRPATH|SFF_NOWWFILES;
1N/A if (!UseMSP)
1N/A sff |= SFF_NOGWFILES;
1N/A#endif /* _FFR_CHK_QUEUE */
1N/A
1N/A if (!SM_IS_DIR_START(qg->qg_qdir))
1N/A {
1N/A /*
1N/A ** XXX we could add basedir, but then we have to realloc()
1N/A ** the string... Maybe another time.
1N/A */
1N/A
1N/A syserr("QueuePath %s not absolute", qg->qg_qdir);
1N/A ExitStat = EX_CONFIG;
1N/A return qn;
1N/A }
1N/A
1N/A /* qpath: directory of current workgroup */
1N/A len = sm_strlcpy(qpath, qg->qg_qdir, sizeof(qpath));
1N/A if (len >= sizeof(qpath))
1N/A {
1N/A syserr("QueuePath %.256s too long (%d max)",
1N/A qg->qg_qdir, (int) sizeof(qpath));
1N/A ExitStat = EX_CONFIG;
1N/A return qn;
1N/A }
1N/A
1N/A /* begin of qpath must be same as basedir */
1N/A if (strncmp(basedir, qpath, blen) != 0 &&
1N/A (strncmp(basedir, qpath, blen - 1) != 0 || len != blen - 1))
1N/A {
1N/A syserr("QueuePath %s not subpath of QueueDirectory %s",
1N/A qpath, basedir);
1N/A ExitStat = EX_CONFIG;
1N/A return qn;
1N/A }
1N/A
1N/A /* Do we have a nested subdirectory? */
1N/A if (blen < len && SM_FIRST_DIR_DELIM(qg->qg_qdir + blen) != NULL)
1N/A {
1N/A
1N/A /* Copy subdirectory into prefix for later use */
1N/A if (sm_strlcpy(prefix, qg->qg_qdir + blen, sizeof(prefix)) >=
1N/A sizeof(prefix))
1N/A {
1N/A syserr("QueuePath %.256s too long (%d max)",
1N/A qg->qg_qdir, (int) sizeof(qpath));
1N/A ExitStat = EX_CONFIG;
1N/A return qn;
1N/A }
1N/A cp = SM_LAST_DIR_DELIM(prefix);
1N/A SM_ASSERT(cp != NULL);
1N/A *cp = '\0'; /* cut off trailing / */
1N/A }
1N/A
1N/A /* This is guaranteed by the basedir check above */
1N/A SM_ASSERT(len >= blen - 1);
1N/A cp = &qpath[len - 1];
1N/A if (*cp == '*')
1N/A {
1N/A register DIR *dp;
1N/A register struct dirent *d;
1N/A int off;
1N/A char *delim;
1N/A char relpath[MAXPATHLEN];
1N/A
1N/A *cp = '\0'; /* Overwrite wildcard */
1N/A if ((cp = SM_LAST_DIR_DELIM(qpath)) == NULL)
1N/A {
1N/A syserr("QueueDirectory: can not wildcard relative path");
1N/A if (tTd(41, 2))
1N/A sm_dprintf("multiqueue_cache: \"%s*\": Can not wildcard relative path.\n",
1N/A qpath);
1N/A ExitStat = EX_CONFIG;
1N/A return qn;
1N/A }
1N/A if (cp == qpath)
1N/A {
1N/A /*
1N/A ** Special case of top level wildcard, like /foo*
1N/A ** Change to //foo*
1N/A */
1N/A
1N/A (void) sm_strlcpy(qpath + 1, qpath, sizeof(qpath) - 1);
1N/A ++cp;
1N/A }
1N/A delim = cp;
1N/A *(cp++) = '\0'; /* Replace / with \0 */
1N/A len = strlen(cp); /* Last component of queue directory */
1N/A
1N/A /*
1N/A ** Path relative to basedir, with trailing /
1N/A ** It will be modified below to specify the subdirectories
1N/A ** so they can be opened without chdir().
1N/A */
1N/A
1N/A off = sm_strlcpyn(relpath, sizeof(relpath), 2, prefix, "/");
1N/A SM_ASSERT(off < sizeof(relpath));
1N/A
1N/A if (tTd(41, 2))
1N/A sm_dprintf("multiqueue_cache: prefix=\"%s%s\"\n",
1N/A relpath, cp);
1N/A
1N/A /* It is always basedir: we don't need to store it per group */
1N/A /* XXX: optimize this! -> one more global? */
1N/A qg->qg_qdir = newstr(basedir);
1N/A qg->qg_qdir[blen - 1] = '\0'; /* cut off trailing / */
1N/A
1N/A /*
1N/A ** XXX Should probably wrap this whole loop in a timeout
1N/A ** in case some wag decides to NFS mount the queues.
1N/A */
1N/A
1N/A /* Test path to get warning messages. */
1N/A if (qn == 0)
1N/A {
1N/A /* XXX qg_runasuid and qg_runasgid for specials? */
1N/A i = safedirpath(basedir, RunAsUid, RunAsGid, NULL,
1N/A sff, 0, 0);
1N/A if (i != 0 && tTd(41, 2))
1N/A sm_dprintf("multiqueue_cache: \"%s\": Not safe: %s\n",
1N/A basedir, sm_errstring(i));
1N/A }
1N/A
1N/A if ((dp = opendir(prefix)) == NULL)
1N/A {
1N/A syserr("can not opendir(%s/%s)", qg->qg_qdir, prefix);
1N/A if (tTd(41, 2))
1N/A sm_dprintf("multiqueue_cache: opendir(\"%s/%s\"): %s\n",
1N/A qg->qg_qdir, prefix,
1N/A sm_errstring(errno));
1N/A ExitStat = EX_CONFIG;
1N/A return qn;
1N/A }
1N/A while ((d = readdir(dp)) != NULL)
1N/A {
1N/A /* Skip . and .. directories */
1N/A if (strcmp(d->d_name, ".") == 0 ||
1N/A strcmp(d->d_name, "..") == 0)
1N/A continue;
1N/A
1N/A i = strlen(d->d_name);
1N/A if (i < len || strncmp(d->d_name, cp, len) != 0)
1N/A {
1N/A if (tTd(41, 5))
1N/A sm_dprintf("multiqueue_cache: \"%s\", skipped\n",
1N/A d->d_name);
1N/A continue;
1N/A }
1N/A
1N/A /* Create relative pathname: prefix + local directory */
1N/A i = sizeof(relpath) - off;
1N/A if (sm_strlcpy(relpath + off, d->d_name, i) >= i)
1N/A continue; /* way too long */
1N/A
1N/A if (!chkqdir(relpath, sff))
1N/A continue;
1N/A
1N/A if (qg->qg_qpaths == NULL)
1N/A {
1N/A slotsleft = INITIAL_SLOTS;
1N/A qg->qg_qpaths = (QPATHS *)xalloc((sizeof(*qg->qg_qpaths)) *
1N/A slotsleft);
1N/A qg->qg_numqueues = 0;
1N/A }
1N/A else if (slotsleft < 1)
1N/A {
1N/A qg->qg_qpaths = (QPATHS *)sm_realloc((char *)qg->qg_qpaths,
1N/A (sizeof(*qg->qg_qpaths)) *
1N/A (qg->qg_numqueues +
1N/A ADD_SLOTS));
1N/A if (qg->qg_qpaths == NULL)
1N/A {
1N/A (void) closedir(dp);
1N/A return qn;
1N/A }
1N/A slotsleft += ADD_SLOTS;
1N/A }
1N/A
1N/A /* check subdirs */
1N/A qg->qg_qpaths[qg->qg_numqueues].qp_subdirs = QP_NOSUB;
1N/A
1N/A#define CHKRSUBDIR(name, flag) \
1N/A (void) sm_strlcpyn(subdir, sizeof(subdir), 3, relpath, "/", name); \
1N/A if (chkqdir(subdir, sff)) \
1N/A qg->qg_qpaths[qg->qg_numqueues].qp_subdirs |= flag; \
1N/A else
1N/A
1N/A
1N/A CHKRSUBDIR("qf", QP_SUBQF);
1N/A CHKRSUBDIR("df", QP_SUBDF);
1N/A CHKRSUBDIR("xf", QP_SUBXF);
1N/A
1N/A /* assert(strlen(d->d_name) < MAXPATHLEN - 14) */
1N/A /* maybe even - 17 (subdirs) */
1N/A
1N/A if (prefix[0] != '.')
1N/A qg->qg_qpaths[qg->qg_numqueues].qp_name =
1N/A newstr(relpath);
1N/A else
1N/A qg->qg_qpaths[qg->qg_numqueues].qp_name =
1N/A newstr(d->d_name);
1N/A
1N/A if (tTd(41, 2))
1N/A sm_dprintf("multiqueue_cache: %d: \"%s\" cached (%x).\n",
1N/A qg->qg_numqueues, relpath,
1N/A qg->qg_qpaths[qg->qg_numqueues].qp_subdirs);
1N/A#if SM_CONF_SHM
1N/A qg->qg_qpaths[qg->qg_numqueues].qp_idx = qn;
1N/A *phash = hash_q(relpath, *phash);
1N/A#endif /* SM_CONF_SHM */
1N/A qg->qg_numqueues++;
1N/A ++qn;
1N/A slotsleft--;
1N/A }
1N/A (void) closedir(dp);
1N/A
1N/A /* undo damage */
1N/A *delim = '/';
1N/A }
1N/A if (qg->qg_numqueues == 0)
1N/A {
1N/A qg->qg_qpaths = (QPATHS *) xalloc(sizeof(*qg->qg_qpaths));
1N/A
1N/A /* test path to get warning messages */
1N/A i = safedirpath(qpath, RunAsUid, RunAsGid, NULL, sff, 0, 0);
1N/A if (i == ENOENT)
1N/A {
1N/A syserr("can not opendir(%s)", qpath);
1N/A if (tTd(41, 2))
1N/A sm_dprintf("multiqueue_cache: opendir(\"%s\"): %s\n",
1N/A qpath, sm_errstring(i));
1N/A ExitStat = EX_CONFIG;
1N/A return qn;
1N/A }
1N/A
1N/A qg->qg_qpaths[0].qp_subdirs = QP_NOSUB;
1N/A qg->qg_numqueues = 1;
1N/A
1N/A /* check subdirs */
1N/A#define CHKSUBDIR(name, flag) \
1N/A (void) sm_strlcpyn(subdir, sizeof(subdir), 3, qg->qg_qdir, "/", name); \
1N/A if (chkqdir(subdir, sff)) \
1N/A qg->qg_qpaths[0].qp_subdirs |= flag; \
1N/A else
1N/A
1N/A CHKSUBDIR("qf", QP_SUBQF);
1N/A CHKSUBDIR("df", QP_SUBDF);
1N/A CHKSUBDIR("xf", QP_SUBXF);
1N/A
1N/A if (qg->qg_qdir[blen - 1] != '\0' &&
1N/A qg->qg_qdir[blen] != '\0')
1N/A {
1N/A /*
1N/A ** Copy the last component into qpaths and
1N/A ** cut off qdir
1N/A */
1N/A
1N/A qg->qg_qpaths[0].qp_name = newstr(qg->qg_qdir + blen);
1N/A qg->qg_qdir[blen - 1] = '\0';
1N/A }
1N/A else
1N/A qg->qg_qpaths[0].qp_name = newstr(".");
1N/A
1N/A#if SM_CONF_SHM
1N/A qg->qg_qpaths[0].qp_idx = qn;
1N/A *phash = hash_q(qg->qg_qpaths[0].qp_name, *phash);
1N/A#endif /* SM_CONF_SHM */
1N/A ++qn;
1N/A }
1N/A return qn;
1N/A}
1N/A
1N/A/*
1N/A** FILESYS_FIND -- find entry in FileSys table, or add new one
1N/A**
1N/A** Given the pathname of a directory, determine the file system
1N/A** in which that directory resides, and return a pointer to the
1N/A** entry in the FileSys table that describes the file system.
1N/A** A new entry is added if necessary (and requested).
1N/A** If the directory does not exist, -1 is returned.
1N/A**
1N/A** Parameters:
1N/A** name -- name of directory (must be persistent!)
1N/A** path -- pathname of directory (name plus maybe "/df")
1N/A** add -- add to structure if not found.
1N/A**
1N/A** Returns:
1N/A** >=0: found: index in file system table
1N/A** <0: some error, i.e.,
1N/A** FSF_TOO_MANY: too many filesystems (-> syserr())
1N/A** FSF_STAT_FAIL: can't stat() filesystem (-> syserr())
1N/A** FSF_NOT_FOUND: not in list
1N/A*/
1N/A
1N/Astatic short filesys_find __P((const char *, const char *, bool));
1N/A
1N/A#define FSF_NOT_FOUND (-1)
1N/A#define FSF_STAT_FAIL (-2)
1N/A#define FSF_TOO_MANY (-3)
1N/A
1N/Astatic short
1N/Afilesys_find(name, path, add)
1N/A const char *name;
1N/A const char *path;
1N/A bool add;
1N/A{
1N/A struct stat st;
1N/A short i;
1N/A
1N/A if (stat(path, &st) < 0)
1N/A {
1N/A syserr("cannot stat queue directory %s", path);
1N/A return FSF_STAT_FAIL;
1N/A }
1N/A for (i = 0; i < NumFileSys; ++i)
1N/A {
1N/A if (FILE_SYS_DEV(i) == st.st_dev)
1N/A {
1N/A /*
1N/A ** Make sure the file system (FS) name is set:
1N/A ** even though the source code indicates that
1N/A ** FILE_SYS_DEV() is only set below, it could be
1N/A ** set via shared memory, hence we need to perform
1N/A ** this check/assignment here.
1N/A */
1N/A
1N/A if (NULL == FILE_SYS_NAME(i))
1N/A FILE_SYS_NAME(i) = name;
1N/A return i;
1N/A }
1N/A }
1N/A if (i >= MAXFILESYS)
1N/A {
1N/A syserr("too many queue file systems (%d max)", MAXFILESYS);
1N/A return FSF_TOO_MANY;
1N/A }
1N/A if (!add)
1N/A return FSF_NOT_FOUND;
1N/A
1N/A ++NumFileSys;
1N/A FILE_SYS_NAME(i) = name;
1N/A FILE_SYS_DEV(i) = st.st_dev;
1N/A FILE_SYS_AVAIL(i) = 0;
1N/A FILE_SYS_BLKSIZE(i) = 1024; /* avoid divide by zero */
1N/A return i;
1N/A}
1N/A
1N/A/*
1N/A** FILESYS_SETUP -- set up mapping from queue directories to file systems
1N/A**
1N/A** This data structure is used to efficiently check the amount of
1N/A** free space available in a set of queue directories.
1N/A**
1N/A** Parameters:
1N/A** add -- initialize structure if necessary.
1N/A**
1N/A** Returns:
1N/A** 0: success
1N/A** <0: some error, i.e.,
1N/A** FSF_NOT_FOUND: not in list
1N/A** FSF_STAT_FAIL: can't stat() filesystem (-> syserr())
1N/A** FSF_TOO_MANY: too many filesystems (-> syserr())
1N/A*/
1N/A
1N/Astatic int filesys_setup __P((bool));
1N/A
1N/Astatic int
1N/Afilesys_setup(add)
1N/A bool add;
1N/A{
1N/A int i, j;
1N/A short fs;
1N/A int ret;
1N/A
1N/A ret = 0;
1N/A for (i = 0; i < NumQueue && Queue[i] != NULL; i++)
1N/A {
1N/A for (j = 0; j < Queue[i]->qg_numqueues; ++j)
1N/A {
1N/A QPATHS *qp = &Queue[i]->qg_qpaths[j];
1N/A char qddf[MAXPATHLEN];
1N/A
1N/A (void) sm_strlcpyn(qddf, sizeof(qddf), 2, qp->qp_name,
1N/A (bitset(QP_SUBDF, qp->qp_subdirs)
1N/A ? "/df" : ""));
1N/A fs = filesys_find(qp->qp_name, qddf, add);
1N/A if (fs >= 0)
1N/A qp->qp_fsysidx = fs;
1N/A else
1N/A qp->qp_fsysidx = 0;
1N/A if (fs < ret)
1N/A ret = fs;
1N/A }
1N/A }
1N/A return ret;
1N/A}
1N/A
1N/A/*
1N/A** FILESYS_UPDATE -- update amount of free space on all file systems
1N/A**
1N/A** The FileSys table is used to cache the amount of free space
1N/A** available on all queue directory file systems.
1N/A** This function updates the cached information if it has expired.
1N/A**
1N/A** Parameters:
1N/A** none.
1N/A**
1N/A** Returns:
1N/A** none.
1N/A**
1N/A** Side Effects:
1N/A** Updates FileSys table.
1N/A*/
1N/A
1N/Avoid
1N/Afilesys_update()
1N/A{
1N/A int i;
1N/A long avail, blksize;
1N/A time_t now;
1N/A static time_t nextupdate = 0;
1N/A
1N/A#if SM_CONF_SHM
1N/A /*
1N/A ** Only the daemon updates the shared memory, i.e.,
1N/A ** if shared memory is available but the pid is not the
1N/A ** one of the daemon, then don't do anything.
1N/A */
1N/A
1N/A if (ShmId != SM_SHM_NO_ID && DaemonPid != CurrentPid)
1N/A return;
1N/A#endif /* SM_CONF_SHM */
1N/A now = curtime();
1N/A if (now < nextupdate)
1N/A return;
1N/A nextupdate = now + FILESYS_UPDATE_INTERVAL;
1N/A for (i = 0; i < NumFileSys; ++i)
1N/A {
1N/A FILESYS *fs = &FILE_SYS(i);
1N/A
1N/A avail = freediskspace(FILE_SYS_NAME(i), &blksize);
1N/A if (avail < 0 || blksize <= 0)
1N/A {
1N/A if (LogLevel > 5)
1N/A sm_syslog(LOG_ERR, NOQID,
1N/A "filesys_update failed: %s, fs=%s, avail=%ld, blocksize=%ld",
1N/A sm_errstring(errno),
1N/A FILE_SYS_NAME(i), avail, blksize);
1N/A fs->fs_avail = 0;
1N/A fs->fs_blksize = 1024; /* avoid divide by zero */
1N/A nextupdate = now + 2; /* let's do this soon again */
1N/A }
1N/A else
1N/A {
1N/A fs->fs_avail = avail;
1N/A fs->fs_blksize = blksize;
1N/A }
1N/A }
1N/A}
1N/A
1N/A#if _FFR_ANY_FREE_FS
1N/A/*
1N/A** FILESYS_FREE -- check whether there is at least one fs with enough space.
1N/A**
1N/A** Parameters:
1N/A** fsize -- file size in bytes
1N/A**
1N/A** Returns:
1N/A** true iff there is one fs with more than fsize bytes free.
1N/A*/
1N/A
1N/Abool
1N/Afilesys_free(fsize)
1N/A long fsize;
1N/A{
1N/A int i;
1N/A
1N/A if (fsize <= 0)
1N/A return true;
1N/A for (i = 0; i < NumFileSys; ++i)
1N/A {
1N/A long needed = 0;
1N/A
1N/A if (FILE_SYS_AVAIL(i) < 0 || FILE_SYS_BLKSIZE(i) <= 0)
1N/A continue;
1N/A needed += fsize / FILE_SYS_BLKSIZE(i)
1N/A + ((fsize % FILE_SYS_BLKSIZE(i)
1N/A > 0) ? 1 : 0)
1N/A + MinBlocksFree;
1N/A if (needed <= FILE_SYS_AVAIL(i))
1N/A return true;
1N/A }
1N/A return false;
1N/A}
1N/A#endif /* _FFR_ANY_FREE_FS */
1N/A
1N/A/*
1N/A** DISK_STATUS -- show amount of free space in queue directories
1N/A**
1N/A** Parameters:
1N/A** out -- output file pointer.
1N/A** prefix -- string to output in front of each line.
1N/A**
1N/A** Returns:
1N/A** none.
1N/A*/
1N/A
1N/Avoid
1N/Adisk_status(out, prefix)
1N/A SM_FILE_T *out;
1N/A char *prefix;
1N/A{
1N/A int i;
1N/A long avail, blksize;
1N/A long free;
1N/A
1N/A for (i = 0; i < NumFileSys; ++i)
1N/A {
1N/A avail = freediskspace(FILE_SYS_NAME(i), &blksize);
1N/A if (avail >= 0 && blksize > 0)
1N/A {
1N/A free = (long)((double) avail *
1N/A ((double) blksize / 1024));
1N/A }
1N/A else
1N/A free = -1;
1N/A (void) sm_io_fprintf(out, SM_TIME_DEFAULT,
1N/A "%s%d/%s/%ld\r\n",
1N/A prefix, i,
1N/A FILE_SYS_NAME(i),
1N/A free);
1N/A }
1N/A}
1N/A
1N/A#if SM_CONF_SHM
1N/A
1N/A/*
1N/A** INIT_SEM -- initialize semaphore system
1N/A**
1N/A** Parameters:
1N/A** owner -- is this the owner of semaphores?
1N/A**
1N/A** Returns:
1N/A** none.
1N/A*/
1N/A
1N/A#if _FFR_USE_SEM_LOCKING
1N/A#if SM_CONF_SEM
1N/Astatic int SemId = -1; /* Semaphore Id */
1N/Aint SemKey = SM_SEM_KEY;
1N/A#endif /* SM_CONF_SEM */
1N/A#endif /* _FFR_USE_SEM_LOCKING */
1N/A
1N/Astatic void init_sem __P((bool));
1N/A
1N/Astatic void
1N/Ainit_sem(owner)
1N/A bool owner;
1N/A{
1N/A#if _FFR_USE_SEM_LOCKING
1N/A#if SM_CONF_SEM
1N/A SemId = sm_sem_start(SemKey, 1, 0, owner);
1N/A if (SemId < 0)
1N/A {
1N/A sm_syslog(LOG_ERR, NOQID,
1N/A "func=init_sem, sem_key=%ld, sm_sem_start=%d, error=%s",
1N/A (long) SemKey, SemId, sm_errstring(-SemId));
1N/A return;
1N/A }
1N/A if (owner && RunAsUid != 0)
1N/A {
1N/A int r;
1N/A
1N/A r = sm_semsetowner(SemId, RunAsUid, RunAsGid, 0660);
1N/A if (r != 0)
1N/A sm_syslog(LOG_ERR, NOQID,
1N/A "key=%ld, sm_semsetowner=%d, RunAsUid=%d, RunAsGid=%d",
1N/A (long) SemKey, r, RunAsUid, RunAsGid);
1N/A }
1N/A#endif /* SM_CONF_SEM */
1N/A#endif /* _FFR_USE_SEM_LOCKING */
1N/A return;
1N/A}
1N/A
1N/A/*
1N/A** STOP_SEM -- stop semaphore system
1N/A**
1N/A** Parameters:
1N/A** owner -- is this the owner of semaphores?
1N/A**
1N/A** Returns:
1N/A** none.
1N/A*/
1N/A
1N/Astatic void stop_sem __P((bool));
1N/A
1N/Astatic void
1N/Astop_sem(owner)
1N/A bool owner;
1N/A{
1N/A#if _FFR_USE_SEM_LOCKING
1N/A#if SM_CONF_SEM
1N/A if (owner && SemId >= 0)
1N/A sm_sem_stop(SemId);
1N/A#endif /* SM_CONF_SEM */
1N/A#endif /* _FFR_USE_SEM_LOCKING */
1N/A return;
1N/A}
1N/A
1N/A/*
1N/A** UPD_QS -- update information about queue when adding/deleting an entry
1N/A**
1N/A** Parameters:
1N/A** e -- envelope.
1N/A** count -- add/remove entry (+1/0/-1: add/no change/remove)
1N/A** space -- update the space available as well.
1N/A** (>0/0/<0: add/no change/remove)
1N/A** where -- caller (for logging)
1N/A**
1N/A** Returns:
1N/A** none.
1N/A**
1N/A** Side Effects:
1N/A** Modifies available space in filesystem.
1N/A** Changes number of entries in queue directory.
1N/A*/
1N/A
1N/Avoid
1N/Aupd_qs(e, count, space, where)
1N/A ENVELOPE *e;
1N/A int count;
1N/A int space;
1N/A char *where;
1N/A{
1N/A short fidx;
1N/A int idx;
1N/A# if _FFR_USE_SEM_LOCKING
1N/A int r;
1N/A# endif /* _FFR_USE_SEM_LOCKING */
1N/A long s;
1N/A
1N/A if (ShmId == SM_SHM_NO_ID || e == NULL)
1N/A return;
1N/A if (e->e_qgrp == NOQGRP || e->e_qdir == NOQDIR)
1N/A return;
1N/A idx = Queue[e->e_qgrp]->qg_qpaths[e->e_qdir].qp_idx;
1N/A if (tTd(73,2))
1N/A sm_dprintf("func=upd_qs, count=%d, space=%d, where=%s, idx=%d, entries=%d\n",
1N/A count, space, where, idx, QSHM_ENTRIES(idx));
1N/A
1N/A /* XXX in theory this needs to be protected with a mutex */
1N/A if (QSHM_ENTRIES(idx) >= 0 && count != 0)
1N/A {
1N/A# if _FFR_USE_SEM_LOCKING
1N/A r = sm_sem_acq(SemId, 0, 1);
1N/A# endif /* _FFR_USE_SEM_LOCKING */
1N/A QSHM_ENTRIES(idx) += count;
1N/A# if _FFR_USE_SEM_LOCKING
1N/A if (r >= 0)
1N/A r = sm_sem_rel(SemId, 0, 1);
1N/A# endif /* _FFR_USE_SEM_LOCKING */
1N/A }
1N/A
1N/A fidx = Queue[e->e_qgrp]->qg_qpaths[e->e_qdir].qp_fsysidx;
1N/A if (fidx < 0)
1N/A return;
1N/A
1N/A /* update available space also? (might be loseqfile) */
1N/A if (space == 0)
1N/A return;
1N/A
1N/A /* convert size to blocks; this causes rounding errors */
1N/A s = e->e_msgsize / FILE_SYS_BLKSIZE(fidx);
1N/A if (s == 0)
1N/A return;
1N/A
1N/A /* XXX in theory this needs to be protected with a mutex */
1N/A if (space > 0)
1N/A FILE_SYS_AVAIL(fidx) += s;
1N/A else
1N/A FILE_SYS_AVAIL(fidx) -= s;
1N/A
1N/A}
1N/A
1N/Astatic bool write_key_file __P((char *, long));
1N/Astatic long read_key_file __P((char *, long));
1N/A
1N/A/*
1N/A** WRITE_KEY_FILE -- record some key into a file.
1N/A**
1N/A** Parameters:
1N/A** keypath -- file name.
1N/A** key -- key to write.
1N/A**
1N/A** Returns:
1N/A** true iff file could be written.
1N/A**
1N/A** Side Effects:
1N/A** writes file.
1N/A*/
1N/A
1N/Astatic bool
1N/Awrite_key_file(keypath, key)
1N/A char *keypath;
1N/A long key;
1N/A{
1N/A bool ok;
1N/A long sff;
1N/A SM_FILE_T *keyf;
1N/A
1N/A ok = false;
1N/A if (keypath == NULL || *keypath == '\0')
1N/A return ok;
1N/A sff = SFF_NOLINK|SFF_ROOTOK|SFF_REGONLY|SFF_CREAT;
1N/A if (TrustedUid != 0 && RealUid == TrustedUid)
1N/A sff |= SFF_OPENASROOT;
1N/A keyf = safefopen(keypath, O_WRONLY|O_TRUNC, FileMode, sff);
1N/A if (keyf == NULL)
1N/A {
1N/A sm_syslog(LOG_ERR, NOQID, "unable to write %s: %s",
1N/A keypath, sm_errstring(errno));
1N/A }
1N/A else
1N/A {
1N/A if (geteuid() == 0 && RunAsUid != 0)
1N/A {
1N/A# if HASFCHOWN
1N/A int fd;
1N/A
1N/A fd = keyf->f_file;
1N/A if (fd >= 0 && fchown(fd, RunAsUid, -1) < 0)
1N/A {
1N/A int err = errno;
1N/A
1N/A sm_syslog(LOG_ALERT, NOQID,
1N/A "ownership change on %s to %d failed: %s",
1N/A keypath, RunAsUid, sm_errstring(err));
1N/A }
1N/A# endif /* HASFCHOWN */
1N/A }
1N/A ok = sm_io_fprintf(keyf, SM_TIME_DEFAULT, "%ld\n", key) !=
1N/A SM_IO_EOF;
1N/A ok = (sm_io_close(keyf, SM_TIME_DEFAULT) != SM_IO_EOF) && ok;
1N/A }
1N/A return ok;
1N/A}
1N/A
1N/A/*
1N/A** READ_KEY_FILE -- read a key from a file.
1N/A**
1N/A** Parameters:
1N/A** keypath -- file name.
1N/A** key -- default key.
1N/A**
1N/A** Returns:
1N/A** key.
1N/A*/
1N/A
1N/Astatic long
1N/Aread_key_file(keypath, key)
1N/A char *keypath;
1N/A long key;
1N/A{
1N/A int r;
1N/A long sff, n;
1N/A SM_FILE_T *keyf;
1N/A
1N/A if (keypath == NULL || *keypath == '\0')
1N/A return key;
1N/A sff = SFF_NOLINK|SFF_ROOTOK|SFF_REGONLY;
1N/A if (RealUid == 0 || (TrustedUid != 0 && RealUid == TrustedUid))
1N/A sff |= SFF_OPENASROOT;
1N/A keyf = safefopen(keypath, O_RDONLY, FileMode, sff);
1N/A if (keyf == NULL)
1N/A {
1N/A sm_syslog(LOG_ERR, NOQID, "unable to read %s: %s",
1N/A keypath, sm_errstring(errno));
1N/A }
1N/A else
1N/A {
1N/A r = sm_io_fscanf(keyf, SM_TIME_DEFAULT, "%ld", &n);
1N/A if (r == 1)
1N/A key = n;
1N/A (void) sm_io_close(keyf, SM_TIME_DEFAULT);
1N/A }
1N/A return key;
1N/A}
1N/A
1N/A/*
1N/A** INIT_SHM -- initialize shared memory structure
1N/A**
1N/A** Initialize or attach to shared memory segment.
1N/A** Currently it is not a fatal error if this doesn't work.
1N/A** However, it causes us to have a "fallback" storage location
1N/A** for everything that is supposed to be in the shared memory,
1N/A** which makes the code slightly ugly.
1N/A**
1N/A** Parameters:
1N/A** qn -- number of queue directories.
1N/A** owner -- owner of shared memory.
1N/A** hash -- identifies data that is stored in shared memory.
1N/A**
1N/A** Returns:
1N/A** none.
1N/A*/
1N/A
1N/Astatic void init_shm __P((int, bool, unsigned int));
1N/A
1N/Astatic void
1N/Ainit_shm(qn, owner, hash)
1N/A int qn;
1N/A bool owner;
1N/A unsigned int hash;
1N/A{
1N/A int i;
1N/A int count;
1N/A int save_errno;
1N/A bool keyselect;
1N/A
1N/A PtrFileSys = &FileSys[0];
1N/A PNumFileSys = &Numfilesys;
1N/A/* if this "key" is specified: select one yourself */
1N/A#define SEL_SHM_KEY ((key_t) -1)
1N/A#define FIRST_SHM_KEY 25
1N/A
1N/A /* This allows us to disable shared memory at runtime. */
1N/A if (ShmKey == 0)
1N/A return;
1N/A
1N/A count = 0;
1N/A shms = SM_T_SIZE + qn * sizeof(QUEUE_SHM_T);
1N/A keyselect = ShmKey == SEL_SHM_KEY;
1N/A if (keyselect)
1N/A {
1N/A if (owner)
1N/A ShmKey = FIRST_SHM_KEY;
1N/A else
1N/A {
1N/A errno = 0;
1N/A ShmKey = read_key_file(ShmKeyFile, ShmKey);
1N/A keyselect = false;
1N/A if (ShmKey == SEL_SHM_KEY)
1N/A {
1N/A save_errno = (errno != 0) ? errno : EINVAL;
1N/A goto error;
1N/A }
1N/A }
1N/A }
1N/A for (;;)
1N/A {
1N/A /* allow read/write access for group? */
1N/A Pshm = sm_shmstart(ShmKey, shms,
1N/A SHM_R|SHM_W|(SHM_R>>3)|(SHM_W>>3),
1N/A &ShmId, owner);
1N/A save_errno = errno;
1N/A if (Pshm != NULL || !sm_file_exists(save_errno))
1N/A break;
1N/A if (++count >= 3)
1N/A {
1N/A if (keyselect)
1N/A {
1N/A ++ShmKey;
1N/A
1N/A /* back where we started? */
1N/A if (ShmKey == SEL_SHM_KEY)
1N/A break;
1N/A continue;
1N/A }
1N/A break;
1N/A }
1N/A
1N/A /* only sleep if we are at the first key */
1N/A if (!keyselect || ShmKey == SEL_SHM_KEY)
1N/A sleep(count);
1N/A }
1N/A if (Pshm != NULL)
1N/A {
1N/A int *p;
1N/A
1N/A if (keyselect)
1N/A (void) write_key_file(ShmKeyFile, (long) ShmKey);
1N/A if (owner && RunAsUid != 0)
1N/A {
1N/A i = sm_shmsetowner(ShmId, RunAsUid, RunAsGid, 0660);
1N/A if (i != 0)
1N/A sm_syslog(LOG_ERR, NOQID,
1N/A "key=%ld, sm_shmsetowner=%d, RunAsUid=%d, RunAsGid=%d",
1N/A (long) ShmKey, i, RunAsUid, RunAsGid);
1N/A }
1N/A p = (int *) Pshm;
1N/A if (owner)
1N/A {
1N/A *p = (int) shms;
1N/A *((pid_t *) SHM_OFF_PID(Pshm)) = CurrentPid;
1N/A p = (int *) SHM_OFF_TAG(Pshm);
1N/A *p = hash;
1N/A }
1N/A else
1N/A {
1N/A if (*p != (int) shms)
1N/A {
1N/A save_errno = EINVAL;
1N/A cleanup_shm(false);
1N/A goto error;
1N/A }
1N/A p = (int *) SHM_OFF_TAG(Pshm);
1N/A if (*p != (int) hash)
1N/A {
1N/A save_errno = EINVAL;
1N/A cleanup_shm(false);
1N/A goto error;
1N/A }
1N/A
1N/A /*
1N/A ** XXX how to check the pid?
1N/A ** Read it from the pid-file? That does
1N/A ** not need to exist.
1N/A ** We could disable shm if we can't confirm
1N/A ** that it is the right one.
1N/A */
1N/A }
1N/A
1N/A PtrFileSys = (FILESYS *) OFF_FILE_SYS(Pshm);
1N/A PNumFileSys = (int *) OFF_NUM_FILE_SYS(Pshm);
1N/A QShm = (QUEUE_SHM_T *) OFF_QUEUE_SHM(Pshm);
1N/A PRSATmpCnt = (int *) OFF_RSA_TMP_CNT(Pshm);
1N/A *PRSATmpCnt = 0;
1N/A if (owner)
1N/A {
1N/A /* initialize values in shared memory */
1N/A NumFileSys = 0;
1N/A for (i = 0; i < qn; i++)
1N/A QShm[i].qs_entries = -1;
1N/A }
1N/A init_sem(owner);
1N/A return;
1N/A }
1N/A error:
1N/A if (LogLevel > (owner ? 8 : 11))
1N/A {
1N/A sm_syslog(owner ? LOG_ERR : LOG_NOTICE, NOQID,
1N/A "can't %s shared memory, key=%ld: %s",
1N/A owner ? "initialize" : "attach to",
1N/A (long) ShmKey, sm_errstring(save_errno));
1N/A }
1N/A}
1N/A#endif /* SM_CONF_SHM */
1N/A
1N/A
1N/A/*
1N/A** SETUP_QUEUES -- set up all queue groups
1N/A**
1N/A** Parameters:
1N/A** owner -- owner of shared memory?
1N/A**
1N/A** Returns:
1N/A** none.
1N/A**
1N/A#if SM_CONF_SHM
1N/A** Side Effects:
1N/A** attaches shared memory.
1N/A#endif * SM_CONF_SHM *
1N/A*/
1N/A
1N/Avoid
1N/Asetup_queues(owner)
1N/A bool owner;
1N/A{
1N/A int i, qn, len;
1N/A unsigned int hashval;
1N/A time_t now;
1N/A char basedir[MAXPATHLEN];
1N/A struct stat st;
1N/A
1N/A /*
1N/A ** Determine basedir for all queue directories.
1N/A ** All queue directories must be (first level) subdirectories
1N/A ** of the basedir. The basedir is the QueueDir
1N/A ** without wildcards, but with trailing /
1N/A */
1N/A
1N/A hashval = 0;
1N/A errno = 0;
1N/A len = sm_strlcpy(basedir, QueueDir, sizeof(basedir));
1N/A
1N/A /* Provide space for trailing '/' */
1N/A if (len >= sizeof(basedir) - 1)
1N/A {
1N/A syserr("QueueDirectory: path too long: %d, max %d",
1N/A len, (int) sizeof(basedir) - 1);
1N/A ExitStat = EX_CONFIG;
1N/A return;
1N/A }
1N/A SM_ASSERT(len > 0);
1N/A if (basedir[len - 1] == '*')
1N/A {
1N/A char *cp;
1N/A
1N/A cp = SM_LAST_DIR_DELIM(basedir);
1N/A if (cp == NULL)
1N/A {
1N/A syserr("QueueDirectory: can not wildcard relative path \"%s\"",
1N/A QueueDir);
1N/A if (tTd(41, 2))
1N/A sm_dprintf("setup_queues: \"%s\": Can not wildcard relative path.\n",
1N/A QueueDir);
1N/A ExitStat = EX_CONFIG;
1N/A return;
1N/A }
1N/A
1N/A /* cut off wildcard pattern */
1N/A *++cp = '\0';
1N/A len = cp - basedir;
1N/A }
1N/A else if (!SM_IS_DIR_DELIM(basedir[len - 1]))
1N/A {
1N/A /* append trailing slash since it is a directory */
1N/A basedir[len] = '/';
1N/A basedir[++len] = '\0';
1N/A }
1N/A
1N/A /* len counts up to the last directory delimiter */
1N/A SM_ASSERT(basedir[len - 1] == '/');
1N/A
1N/A if (chdir(basedir) < 0)
1N/A {
1N/A int save_errno = errno;
1N/A
1N/A syserr("can not chdir(%s)", basedir);
1N/A if (save_errno == EACCES)
1N/A (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
1N/A "Program mode requires special privileges, e.g., root or TrustedUser.\n");
1N/A if (tTd(41, 2))
1N/A sm_dprintf("setup_queues: \"%s\": %s\n",
1N/A basedir, sm_errstring(errno));
1N/A ExitStat = EX_CONFIG;
1N/A return;
1N/A }
1N/A#if SM_CONF_SHM
1N/A hashval = hash_q(basedir, hashval);
1N/A#endif /* SM_CONF_SHM */
1N/A
1N/A /* initialize for queue runs */
1N/A DoQueueRun = false;
1N/A now = curtime();
1N/A for (i = 0; i < NumQueue && Queue[i] != NULL; i++)
1N/A Queue[i]->qg_nextrun = now;
1N/A
1N/A
1N/A if (UseMSP && OpMode != MD_TEST)
1N/A {
1N/A long sff = SFF_CREAT;
1N/A
1N/A if (stat(".", &st) < 0)
1N/A {
1N/A syserr("can not stat(%s)", basedir);
1N/A if (tTd(41, 2))
1N/A sm_dprintf("setup_queues: \"%s\": %s\n",
1N/A basedir, sm_errstring(errno));
1N/A ExitStat = EX_CONFIG;
1N/A return;
1N/A }
1N/A if (RunAsUid == 0)
1N/A sff |= SFF_ROOTOK;
1N/A
1N/A /*
1N/A ** Check queue directory permissions.
1N/A ** Can we write to a group writable queue directory?
1N/A */
1N/A
1N/A if (bitset(S_IWGRP, QueueFileMode) &&
1N/A bitset(S_IWGRP, st.st_mode) &&
1N/A safefile(" ", RunAsUid, RunAsGid, RunAsUserName, sff,
1N/A QueueFileMode, NULL) != 0)
1N/A {
1N/A syserr("can not write to queue directory %s (RunAsGid=%d, required=%d)",
1N/A basedir, (int) RunAsGid, (int) st.st_gid);
1N/A }
1N/A if (bitset(S_IWOTH|S_IXOTH, st.st_mode))
1N/A {
1N/A#if _FFR_MSP_PARANOIA
1N/A syserr("dangerous permissions=%o on queue directory %s",
1N/A (int) st.st_mode, basedir);
1N/A#else /* _FFR_MSP_PARANOIA */
1N/A if (LogLevel > 0)
1N/A sm_syslog(LOG_ERR, NOQID,
1N/A "dangerous permissions=%o on queue directory %s",
1N/A (int) st.st_mode, basedir);
1N/A#endif /* _FFR_MSP_PARANOIA */
1N/A }
1N/A#if _FFR_MSP_PARANOIA
1N/A if (NumQueue > 1)
1N/A syserr("can not use multiple queues for MSP");
1N/A#endif /* _FFR_MSP_PARANOIA */
1N/A }
1N/A
1N/A /* initial number of queue directories */
1N/A qn = 0;
1N/A for (i = 0; i < NumQueue && Queue[i] != NULL; i++)
1N/A qn = multiqueue_cache(basedir, len, Queue[i], qn, &hashval);
1N/A
1N/A#if SM_CONF_SHM
1N/A init_shm(qn, owner, hashval);
1N/A i = filesys_setup(owner || ShmId == SM_SHM_NO_ID);
1N/A if (i == FSF_NOT_FOUND)
1N/A {
1N/A /*
1N/A ** We didn't get the right filesystem data
1N/A ** This may happen if we don't have the right shared memory.
1N/A ** So let's do this without shared memory.
1N/A */
1N/A
1N/A SM_ASSERT(!owner);
1N/A cleanup_shm(false); /* release shared memory */
1N/A i = filesys_setup(false);
1N/A if (i < 0)
1N/A syserr("filesys_setup failed twice, result=%d", i);
1N/A else if (LogLevel > 8)
1N/A sm_syslog(LOG_WARNING, NOQID,
1N/A "shared memory does not contain expected data, ignored");
1N/A }
1N/A#else /* SM_CONF_SHM */
1N/A i = filesys_setup(true);
1N/A#endif /* SM_CONF_SHM */
1N/A if (i < 0)
1N/A ExitStat = EX_CONFIG;
1N/A}
1N/A
1N/A#if SM_CONF_SHM
1N/A/*
1N/A** CLEANUP_SHM -- do some cleanup work for shared memory etc
1N/A**
1N/A** Parameters:
1N/A** owner -- owner of shared memory?
1N/A**
1N/A** Returns:
1N/A** none.
1N/A**
1N/A** Side Effects:
1N/A** detaches shared memory.
1N/A*/
1N/A
1N/Avoid
1N/Acleanup_shm(owner)
1N/A bool owner;
1N/A{
1N/A if (ShmId != SM_SHM_NO_ID)
1N/A {
1N/A if (sm_shmstop(Pshm, ShmId, owner) < 0 && LogLevel > 8)
1N/A sm_syslog(LOG_INFO, NOQID, "sm_shmstop failed=%s",
1N/A sm_errstring(errno));
1N/A Pshm = NULL;
1N/A ShmId = SM_SHM_NO_ID;
1N/A }
1N/A stop_sem(owner);
1N/A}
1N/A#endif /* SM_CONF_SHM */
1N/A
1N/A/*
1N/A** CLEANUP_QUEUES -- do some cleanup work for queues
1N/A**
1N/A** Parameters:
1N/A** none.
1N/A**
1N/A** Returns:
1N/A** none.
1N/A**
1N/A*/
1N/A
1N/Avoid
1N/Acleanup_queues()
1N/A{
1N/A sync_queue_time();
1N/A}
1N/A/*
1N/A** SET_DEF_QUEUEVAL -- set default values for a queue group.
1N/A**
1N/A** Parameters:
1N/A** qg -- queue group
1N/A** all -- set all values (true for default group)?
1N/A**
1N/A** Returns:
1N/A** none.
1N/A**
1N/A** Side Effects:
1N/A** sets default values for the queue group.
1N/A*/
1N/A
1N/Avoid
1N/Aset_def_queueval(qg, all)
1N/A QUEUEGRP *qg;
1N/A bool all;
1N/A{
1N/A if (bitnset(QD_DEFINED, qg->qg_flags))
1N/A return;
1N/A if (all)
1N/A qg->qg_qdir = QueueDir;
1N/A#if _FFR_QUEUE_GROUP_SORTORDER
1N/A qg->qg_sortorder = QueueSortOrder;
1N/A#endif /* _FFR_QUEUE_GROUP_SORTORDER */
1N/A qg->qg_maxqrun = all ? MaxRunnersPerQueue : -1;
1N/A qg->qg_nice = NiceQueueRun;
1N/A}
1N/A/*
1N/A** MAKEQUEUE -- define a new queue.
1N/A**
1N/A** Parameters:
1N/A** line -- description of queue. This is in labeled fields.
1N/A** The fields are:
1N/A** F -- the flags associated with the queue
1N/A** I -- the interval between running the queue
1N/A** J -- the maximum # of jobs in work list
1N/A** [M -- the maximum # of jobs in a queue run]
1N/A** N -- the niceness at which to run
1N/A** P -- the path to the queue
1N/A** S -- the queue sorting order
1N/A** R -- number of parallel queue runners
1N/A** r -- max recipients per envelope
1N/A** The first word is the canonical name of the queue.
1N/A** qdef -- this is a 'Q' definition from .cf
1N/A**
1N/A** Returns:
1N/A** none.
1N/A**
1N/A** Side Effects:
1N/A** enters the queue into the queue table.
1N/A*/
1N/A
1N/Avoid
1N/Amakequeue(line, qdef)
1N/A char *line;
1N/A bool qdef;
1N/A{
1N/A register char *p;
1N/A register QUEUEGRP *qg;
1N/A register STAB *s;
1N/A int i;
1N/A char fcode;
1N/A
1N/A /* allocate a queue and set up defaults */
1N/A qg = (QUEUEGRP *) xalloc(sizeof(*qg));
1N/A memset((char *) qg, '\0', sizeof(*qg));
1N/A
1N/A if (line[0] == '\0')
1N/A {
1N/A syserr("name required for queue");
1N/A return;
1N/A }
1N/A
1N/A /* collect the queue name */
1N/A for (p = line;
1N/A *p != '\0' && *p != ',' && !(isascii(*p) && isspace(*p));
1N/A p++)
1N/A continue;
1N/A if (*p != '\0')
1N/A *p++ = '\0';
1N/A qg->qg_name = newstr(line);
1N/A
1N/A /* set default values, can be overridden below */
1N/A set_def_queueval(qg, false);
1N/A
1N/A /* now scan through and assign info from the fields */
1N/A while (*p != '\0')
1N/A {
1N/A auto char *delimptr;
1N/A
1N/A while (*p != '\0' &&
1N/A (*p == ',' || (isascii(*p) && isspace(*p))))
1N/A p++;
1N/A
1N/A /* p now points to field code */
1N/A fcode = *p;
1N/A while (*p != '\0' && *p != '=' && *p != ',')
1N/A p++;
1N/A if (*p++ != '=')
1N/A {
1N/A syserr("queue %s: `=' expected", qg->qg_name);
1N/A return;
1N/A }
1N/A while (isascii(*p) && isspace(*p))
1N/A p++;
1N/A
1N/A /* p now points to the field body */
1N/A p = munchstring(p, &delimptr, ',');
1N/A
1N/A /* install the field into the queue struct */
1N/A switch (fcode)
1N/A {
1N/A case 'P': /* pathname */
1N/A if (*p == '\0')
1N/A syserr("queue %s: empty path name",
1N/A qg->qg_name);
1N/A else
1N/A qg->qg_qdir = newstr(p);
1N/A break;
1N/A
1N/A case 'F': /* flags */
1N/A for (; *p != '\0'; p++)
1N/A if (!(isascii(*p) && isspace(*p)))
1N/A setbitn(*p, qg->qg_flags);
1N/A break;
1N/A
1N/A /*
1N/A ** Do we need two intervals here:
1N/A ** One for persistent queue runners,
1N/A ** one for "normal" queue runs?
1N/A */
1N/A
1N/A case 'I': /* interval between running the queue */
1N/A qg->qg_queueintvl = convtime(p, 'm');
1N/A break;
1N/A
1N/A case 'N': /* run niceness */
1N/A qg->qg_nice = atoi(p);
1N/A break;
1N/A
1N/A case 'R': /* maximum # of runners for the group */
1N/A i = atoi(p);
1N/A
1N/A /* can't have more runners than allowed total */
1N/A if (MaxQueueChildren > 0 && i > MaxQueueChildren)
1N/A {
1N/A qg->qg_maxqrun = MaxQueueChildren;
1N/A (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
1N/A "Q=%s: R=%d exceeds MaxQueueChildren=%d, set to MaxQueueChildren\n",
1N/A qg->qg_name, i,
1N/A MaxQueueChildren);
1N/A }
1N/A else
1N/A qg->qg_maxqrun = i;
1N/A break;
1N/A
1N/A case 'J': /* maximum # of jobs in work list */
1N/A qg->qg_maxlist = atoi(p);
1N/A break;
1N/A
1N/A case 'r': /* max recipients per envelope */
1N/A qg->qg_maxrcpt = atoi(p);
1N/A break;
1N/A
1N/A#if _FFR_QUEUE_GROUP_SORTORDER
1N/A case 'S': /* queue sorting order */
1N/A switch (*p)
1N/A {
1N/A case 'h': /* Host first */
1N/A case 'H':
1N/A qg->qg_sortorder = QSO_BYHOST;
1N/A break;
1N/A
1N/A case 'p': /* Priority order */
1N/A case 'P':
1N/A qg->qg_sortorder = QSO_BYPRIORITY;
1N/A break;
1N/A
1N/A case 't': /* Submission time */
1N/A case 'T':
1N/A qg->qg_sortorder = QSO_BYTIME;
1N/A break;
1N/A
1N/A case 'f': /* File name */
1N/A case 'F':
1N/A qg->qg_sortorder = QSO_BYFILENAME;
1N/A break;
1N/A
1N/A case 'm': /* Modification time */
1N/A case 'M':
1N/A qg->qg_sortorder = QSO_BYMODTIME;
1N/A break;
1N/A
1N/A case 'r': /* Random */
1N/A case 'R':
1N/A qg->qg_sortorder = QSO_RANDOM;
1N/A break;
1N/A
1N/A# if _FFR_RHS
1N/A case 's': /* Shuffled host name */
1N/A case 'S':
1N/A qg->qg_sortorder = QSO_BYSHUFFLE;
1N/A break;
1N/A# endif /* _FFR_RHS */
1N/A
1N/A case 'n': /* none */
1N/A case 'N':
1N/A qg->qg_sortorder = QSO_NONE;
1N/A break;
1N/A
1N/A default:
1N/A syserr("Invalid queue sort order \"%s\"", p);
1N/A }
1N/A break;
1N/A#endif /* _FFR_QUEUE_GROUP_SORTORDER */
1N/A
1N/A default:
1N/A syserr("Q%s: unknown queue equate %c=",
1N/A qg->qg_name, fcode);
1N/A break;
1N/A }
1N/A
1N/A p = delimptr;
1N/A }
1N/A
1N/A#if !HASNICE
1N/A if (qg->qg_nice != NiceQueueRun)
1N/A {
1N/A (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
1N/A "Q%s: Warning: N= set on system that doesn't support nice()\n",
1N/A qg->qg_name);
1N/A }
1N/A#endif /* !HASNICE */
1N/A
1N/A /* do some rationality checking */
1N/A if (NumQueue >= MAXQUEUEGROUPS)
1N/A {
1N/A syserr("too many queue groups defined (%d max)",
1N/A MAXQUEUEGROUPS);
1N/A return;
1N/A }
1N/A
1N/A if (qg->qg_qdir == NULL)
1N/A {
1N/A if (QueueDir == NULL || *QueueDir == '\0')
1N/A {
1N/A syserr("QueueDir must be defined before queue groups");
1N/A return;
1N/A }
1N/A qg->qg_qdir = newstr(QueueDir);
1N/A }
1N/A
1N/A if (qg->qg_maxqrun > 1 && !bitnset(QD_FORK, qg->qg_flags))
1N/A {
1N/A (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
1N/A "Warning: Q=%s: R=%d: multiple queue runners specified\n\tbut flag '%c' is not set\n",
1N/A qg->qg_name, qg->qg_maxqrun, QD_FORK);
1N/A }
1N/A
1N/A /* enter the queue into the symbol table */
1N/A if (tTd(37, 8))
1N/A sm_syslog(LOG_INFO, NOQID,
1N/A "Adding %s to stab, path: %s", qg->qg_name,
1N/A qg->qg_qdir);
1N/A s = stab(qg->qg_name, ST_QUEUE, ST_ENTER);
1N/A if (s->s_quegrp != NULL)
1N/A {
1N/A i = s->s_quegrp->qg_index;
1N/A
1N/A /* XXX what about the pointers inside this struct? */
1N/A sm_free(s->s_quegrp); /* XXX */
1N/A }
1N/A else
1N/A i = NumQueue++;
1N/A Queue[i] = s->s_quegrp = qg;
1N/A qg->qg_index = i;
1N/A
1N/A /* set default value for max queue runners */
1N/A if (qg->qg_maxqrun < 0)
1N/A {
1N/A if (MaxRunnersPerQueue > 0)
1N/A qg->qg_maxqrun = MaxRunnersPerQueue;
1N/A else
1N/A qg->qg_maxqrun = 1;
1N/A }
1N/A if (qdef)
1N/A setbitn(QD_DEFINED, qg->qg_flags);
1N/A}
1N/A#if 0
1N/A/*
1N/A** HASHFQN -- calculate a hash value for a fully qualified host name
1N/A**
1N/A** Arguments:
1N/A** fqn -- an all lower-case host.domain string
1N/A** buckets -- the number of buckets (queue directories)
1N/A**
1N/A** Returns:
1N/A** a bucket number (signed integer)
1N/A** -1 on error
1N/A**
1N/A** Contributed by Exactis.com, Inc.
1N/A*/
1N/A
1N/Aint
1N/Ahashfqn(fqn, buckets)
1N/A register char *fqn;
1N/A int buckets;
1N/A{
1N/A register char *p;
1N/A register int h = 0, hash, cnt;
1N/A
1N/A if (fqn == NULL)
1N/A return -1;
1N/A
1N/A /*
1N/A ** A variation on the gdb hash
1N/A ** This is the best as of Feb 19, 1996 --bcx
1N/A */
1N/A
1N/A p = fqn;
1N/A h = 0x238F13AF * strlen(p);
1N/A for (cnt = 0; *p != 0; ++p, cnt++)
1N/A {
1N/A h = (h + (*p << (cnt * 5 % 24))) & 0x7FFFFFFF;
1N/A }
1N/A h = (1103515243 * h + 12345) & 0x7FFFFFFF;
1N/A if (buckets < 2)
1N/A hash = 0;
1N/A else
1N/A hash = (h % buckets);
1N/A
1N/A return hash;
1N/A}
1N/A#endif /* 0 */
1N/A
1N/A/*
1N/A** A structure for sorting Queue according to maxqrun without
1N/A** screwing up Queue itself.
1N/A*/
1N/A
1N/Astruct sortqgrp
1N/A{
1N/A int sg_idx; /* original index */
1N/A int sg_maxqrun; /* max queue runners */
1N/A};
1N/Atypedef struct sortqgrp SORTQGRP_T;
1N/Astatic int cmpidx __P((const void *, const void *));
1N/A
1N/Astatic int
1N/Acmpidx(a, b)
1N/A const void *a;
1N/A const void *b;
1N/A{
1N/A /* The sort is highest to lowest, so the comparison is reversed */
1N/A if (((SORTQGRP_T *)a)->sg_maxqrun < ((SORTQGRP_T *)b)->sg_maxqrun)
1N/A return 1;
1N/A else if (((SORTQGRP_T *)a)->sg_maxqrun > ((SORTQGRP_T *)b)->sg_maxqrun)
1N/A return -1;
1N/A else
1N/A return 0;
1N/A}
1N/A
1N/A/*
1N/A** MAKEWORKGROUP -- balance queue groups into work groups per MaxQueueChildren
1N/A**
1N/A** Take the now defined queue groups and assign them to work groups.
1N/A** This is done to balance out the number of concurrently active
1N/A** queue runners such that MaxQueueChildren is not exceeded. This may
1N/A** result in more than one queue group per work group. In such a case
1N/A** the number of running queue groups in that work group will have no
1N/A** more than the work group maximum number of runners (a "fair" portion
1N/A** of MaxQueueRunners). All queue groups within a work group will get a
1N/A** chance at running.
1N/A**
1N/A** Parameters:
1N/A** none.
1N/A**
1N/A** Returns:
1N/A** nothing.
1N/A**
1N/A** Side Effects:
1N/A** Sets up WorkGrp structure.
1N/A*/
1N/A
1N/Avoid
1N/Amakeworkgroups()
1N/A{
1N/A int i, j, total_runners, dir, h;
1N/A SORTQGRP_T si[MAXQUEUEGROUPS + 1];
1N/A
1N/A total_runners = 0;
1N/A if (NumQueue == 1 && strcmp(Queue[0]->qg_name, "mqueue") == 0)
1N/A {
1N/A /*
1N/A ** There is only the "mqueue" queue group (a default)
1N/A ** containing all of the queues. We want to provide to
1N/A ** this queue group the maximum allowable queue runners.
1N/A ** To match older behavior (8.10/8.11) we'll try for
1N/A ** 1 runner per queue capping it at MaxQueueChildren.
1N/A ** So if there are N queues, then there will be N runners
1N/A ** for the "mqueue" queue group (where N is kept less than
1N/A ** MaxQueueChildren).
1N/A */
1N/A
1N/A NumWorkGroups = 1;
1N/A WorkGrp[0].wg_numqgrp = 1;
1N/A WorkGrp[0].wg_qgs = (QUEUEGRP **) xalloc(sizeof(QUEUEGRP *));
1N/A WorkGrp[0].wg_qgs[0] = Queue[0];
1N/A if (MaxQueueChildren > 0 &&
1N/A Queue[0]->qg_numqueues > MaxQueueChildren)
1N/A WorkGrp[0].wg_runners = MaxQueueChildren;
1N/A else
1N/A WorkGrp[0].wg_runners = Queue[0]->qg_numqueues;
1N/A
1N/A Queue[0]->qg_wgrp = 0;
1N/A
1N/A /* can't have more runners than allowed total */
1N/A if (MaxQueueChildren > 0 &&
1N/A Queue[0]->qg_maxqrun > MaxQueueChildren)
1N/A Queue[0]->qg_maxqrun = MaxQueueChildren;
1N/A WorkGrp[0].wg_maxact = Queue[0]->qg_maxqrun;
1N/A WorkGrp[0].wg_lowqintvl = Queue[0]->qg_queueintvl;
1N/A return;
1N/A }
1N/A
1N/A for (i = 0; i < NumQueue; i++)
1N/A {
1N/A si[i].sg_maxqrun = Queue[i]->qg_maxqrun;
1N/A si[i].sg_idx = i;
1N/A }
1N/A qsort(si, NumQueue, sizeof(si[0]), cmpidx);
1N/A
1N/A NumWorkGroups = 0;
1N/A for (i = 0; i < NumQueue; i++)
1N/A {
1N/A total_runners += si[i].sg_maxqrun;
1N/A if (MaxQueueChildren <= 0 || total_runners <= MaxQueueChildren)
1N/A NumWorkGroups++;
1N/A else
1N/A break;
1N/A }
1N/A
1N/A if (NumWorkGroups < 1)
1N/A NumWorkGroups = 1; /* gotta have one at least */
1N/A else if (NumWorkGroups > MAXWORKGROUPS)
1N/A NumWorkGroups = MAXWORKGROUPS; /* the limit */
1N/A
1N/A /*
1N/A ** We now know the number of work groups to pack the queue groups
1N/A ** into. The queue groups in 'Queue' are sorted from highest
1N/A ** to lowest for the number of runners per queue group.
1N/A ** We put the queue groups with the largest number of runners
1N/A ** into work groups first. Then the smaller ones are fitted in
1N/A ** where it looks best.
1N/A */
1N/A
1N/A j = 0;
1N/A dir = 1;
1N/A for (i = 0; i < NumQueue; i++)
1N/A {
1N/A /* a to-and-fro packing scheme, continue from last position */
1N/A if (j >= NumWorkGroups)
1N/A {
1N/A dir = -1;
1N/A j = NumWorkGroups - 1;
1N/A }
1N/A else if (j < 0)
1N/A {
1N/A j = 0;
1N/A dir = 1;
1N/A }
1N/A
1N/A if (WorkGrp[j].wg_qgs == NULL)
1N/A WorkGrp[j].wg_qgs = (QUEUEGRP **)sm_malloc(sizeof(QUEUEGRP *) *
1N/A (WorkGrp[j].wg_numqgrp + 1));
1N/A else
1N/A WorkGrp[j].wg_qgs = (QUEUEGRP **)sm_realloc(WorkGrp[j].wg_qgs,
1N/A sizeof(QUEUEGRP *) *
1N/A (WorkGrp[j].wg_numqgrp + 1));
1N/A if (WorkGrp[j].wg_qgs == NULL)
1N/A {
1N/A syserr("!cannot allocate memory for work queues, need %d bytes",
1N/A (int) (sizeof(QUEUEGRP *) *
1N/A (WorkGrp[j].wg_numqgrp + 1)));
1N/A }
1N/A
1N/A h = si[i].sg_idx;
1N/A WorkGrp[j].wg_qgs[WorkGrp[j].wg_numqgrp] = Queue[h];
1N/A WorkGrp[j].wg_numqgrp++;
1N/A WorkGrp[j].wg_runners += Queue[h]->qg_maxqrun;
1N/A Queue[h]->qg_wgrp = j;
1N/A
1N/A if (WorkGrp[j].wg_maxact == 0)
1N/A {
1N/A /* can't have more runners than allowed total */
1N/A if (MaxQueueChildren > 0 &&
1N/A Queue[h]->qg_maxqrun > MaxQueueChildren)
1N/A Queue[h]->qg_maxqrun = MaxQueueChildren;
1N/A WorkGrp[j].wg_maxact = Queue[h]->qg_maxqrun;
1N/A }
1N/A
1N/A /*
1N/A ** XXX: must wg_lowqintvl be the GCD?
1N/A ** qg1: 2m, qg2: 3m, minimum: 2m, when do queue runs for
1N/A ** qg2 occur?
1N/A */
1N/A
1N/A /* keep track of the lowest interval for a persistent runner */
1N/A if (Queue[h]->qg_queueintvl > 0 &&
1N/A WorkGrp[j].wg_lowqintvl < Queue[h]->qg_queueintvl)
1N/A WorkGrp[j].wg_lowqintvl = Queue[h]->qg_queueintvl;
1N/A j += dir;
1N/A }
1N/A if (tTd(41, 9))
1N/A {
1N/A for (i = 0; i < NumWorkGroups; i++)
1N/A {
1N/A sm_dprintf("Workgroup[%d]=", i);
1N/A for (j = 0; j < WorkGrp[i].wg_numqgrp; j++)
1N/A {
1N/A sm_dprintf("%s, ",
1N/A WorkGrp[i].wg_qgs[j]->qg_name);
1N/A }
1N/A sm_dprintf("\n");
1N/A }
1N/A }
1N/A}
1N/A
1N/A/*
1N/A** DUP_DF -- duplicate envelope data file
1N/A**
1N/A** Copy the data file from the 'old' envelope to the 'new' envelope
1N/A** in the most efficient way possible.
1N/A**
1N/A** Create a hard link from the 'old' data file to the 'new' data file.
1N/A** If the old and new queue directories are on different file systems,
1N/A** then the new data file link is created in the old queue directory,
1N/A** and the new queue file will contain a 'd' record pointing to the
1N/A** directory containing the new data file.
1N/A**
1N/A** Parameters:
1N/A** old -- old envelope.
1N/A** new -- new envelope.
1N/A**
1N/A** Results:
1N/A** Returns true on success, false on failure.
1N/A**
1N/A** Side Effects:
1N/A** On success, the new data file is created.
1N/A** On fatal failure, EF_FATALERRS is set in old->e_flags.
1N/A*/
1N/A
1N/Astatic bool dup_df __P((ENVELOPE *, ENVELOPE *));
1N/A
1N/Astatic bool
1N/Adup_df(old, new)
1N/A ENVELOPE *old;
1N/A ENVELOPE *new;
1N/A{
1N/A int ofs, nfs, r;
1N/A char opath[MAXPATHLEN];
1N/A char npath[MAXPATHLEN];
1N/A
1N/A if (!bitset(EF_HAS_DF, old->e_flags))
1N/A {
1N/A /*
1N/A ** this can happen if: SuperSafe != True
1N/A ** and a bounce mail is sent that is split.
1N/A */
1N/A
1N/A queueup(old, false, true);
1N/A }
1N/A SM_REQUIRE(ISVALIDQGRP(old->e_qgrp) && ISVALIDQDIR(old->e_qdir));
1N/A SM_REQUIRE(ISVALIDQGRP(new->e_qgrp) && ISVALIDQDIR(new->e_qdir));
1N/A
1N/A (void) sm_strlcpy(opath, queuename(old, DATAFL_LETTER), sizeof(opath));
1N/A (void) sm_strlcpy(npath, queuename(new, DATAFL_LETTER), sizeof(npath));
1N/A
1N/A if (old->e_dfp != NULL)
1N/A {
1N/A r = sm_io_setinfo(old->e_dfp, SM_BF_COMMIT, NULL);
1N/A if (r < 0 && errno != EINVAL)
1N/A {
1N/A syserr("@can't commit %s", opath);
1N/A old->e_flags |= EF_FATALERRS;
1N/A return false;
1N/A }
1N/A }
1N/A
1N/A /*
1N/A ** Attempt to create a hard link, if we think both old and new
1N/A ** are on the same file system, otherwise copy the file.
1N/A **
1N/A ** Don't waste time attempting a hard link unless old and new
1N/A ** are on the same file system.
1N/A */
1N/A
1N/A SM_REQUIRE(ISVALIDQGRP(old->e_dfqgrp) && ISVALIDQDIR(old->e_dfqdir));
1N/A SM_REQUIRE(ISVALIDQGRP(new->e_dfqgrp) && ISVALIDQDIR(new->e_dfqdir));
1N/A
1N/A ofs = Queue[old->e_dfqgrp]->qg_qpaths[old->e_dfqdir].qp_fsysidx;
1N/A nfs = Queue[new->e_dfqgrp]->qg_qpaths[new->e_dfqdir].qp_fsysidx;
1N/A if (FILE_SYS_DEV(ofs) == FILE_SYS_DEV(nfs))
1N/A {
1N/A if (link(opath, npath) == 0)
1N/A {
1N/A new->e_flags |= EF_HAS_DF;
1N/A SYNC_DIR(npath, true);
1N/A return true;
1N/A }
1N/A goto error;
1N/A }
1N/A
1N/A /*
1N/A ** Can't link across queue directories, so try to create a hard
1N/A ** link in the same queue directory as the old df file.
1N/A ** The qf file will refer to the new df file using a 'd' record.
1N/A */
1N/A
1N/A new->e_dfqgrp = old->e_dfqgrp;
1N/A new->e_dfqdir = old->e_dfqdir;
1N/A (void) sm_strlcpy(npath, queuename(new, DATAFL_LETTER), sizeof(npath));
1N/A if (link(opath, npath) == 0)
1N/A {
1N/A new->e_flags |= EF_HAS_DF;
1N/A SYNC_DIR(npath, true);
1N/A return true;
1N/A }
1N/A
1N/A error:
1N/A if (LogLevel > 0)
1N/A sm_syslog(LOG_ERR, old->e_id,
1N/A "dup_df: can't link %s to %s, error=%s, envelope splitting failed",
1N/A opath, npath, sm_errstring(errno));
1N/A return false;
1N/A}
1N/A
1N/A/*
1N/A** SPLIT_ENV -- Allocate a new envelope based on a given envelope.
1N/A**
1N/A** Parameters:
1N/A** e -- envelope.
1N/A** sendqueue -- sendqueue for new envelope.
1N/A** qgrp -- index of queue group.
1N/A** qdir -- queue directory.
1N/A**
1N/A** Results:
1N/A** new envelope.
1N/A**
1N/A*/
1N/A
1N/Astatic ENVELOPE *split_env __P((ENVELOPE *, ADDRESS *, int, int));
1N/A
1N/Astatic ENVELOPE *
1N/Asplit_env(e, sendqueue, qgrp, qdir)
1N/A ENVELOPE *e;
1N/A ADDRESS *sendqueue;
1N/A int qgrp;
1N/A int qdir;
1N/A{
1N/A ENVELOPE *ee;
1N/A
1N/A ee = (ENVELOPE *) sm_rpool_malloc_x(e->e_rpool, sizeof(*ee));
1N/A STRUCTCOPY(*e, *ee);
1N/A ee->e_message = NULL; /* XXX use original message? */
1N/A ee->e_id = NULL;
1N/A assign_queueid(ee);
1N/A ee->e_sendqueue = sendqueue;
1N/A ee->e_flags &= ~(EF_INQUEUE|EF_CLRQUEUE|EF_FATALERRS
1N/A |EF_SENDRECEIPT|EF_RET_PARAM|EF_HAS_DF);
1N/A ee->e_flags |= EF_NORECEIPT; /* XXX really? */
1N/A ee->e_from.q_state = QS_SENDER;
1N/A ee->e_dfp = NULL;
1N/A ee->e_lockfp = NULL;
1N/A if (e->e_xfp != NULL)
1N/A ee->e_xfp = sm_io_dup(e->e_xfp);
1N/A
1N/A /* failed to dup e->e_xfp, start a new transcript */
1N/A if (ee->e_xfp == NULL)
1N/A openxscript(ee);
1N/A
1N/A ee->e_qgrp = ee->e_dfqgrp = qgrp;
1N/A ee->e_qdir = ee->e_dfqdir = qdir;
1N/A ee->e_errormode = EM_MAIL;
1N/A ee->e_statmsg = NULL;
1N/A if (e->e_quarmsg != NULL)
1N/A ee->e_quarmsg = sm_rpool_strdup_x(ee->e_rpool,
1N/A e->e_quarmsg);
1N/A
1N/A /*
1N/A ** XXX Not sure if this copying is necessary.
1N/A ** sendall() does this copying, but I (dm) don't know if that is
1N/A ** because of the storage management discipline we were using
1N/A ** before rpools were introduced, or if it is because these lists
1N/A ** can be modified later.
1N/A */
1N/A
1N/A ee->e_header = copyheader(e->e_header, ee->e_rpool);
1N/A ee->e_errorqueue = copyqueue(e->e_errorqueue, ee->e_rpool);
1N/A
1N/A return ee;
1N/A}
1N/A
1N/A/* return values from split functions, check also below! */
1N/A#define SM_SPLIT_FAIL (0)
1N/A#define SM_SPLIT_NONE (1)
1N/A#define SM_SPLIT_NEW(n) (1 + (n))
1N/A
1N/A/*
1N/A** SPLIT_ACROSS_QUEUE_GROUPS
1N/A**
1N/A** This function splits an envelope across multiple queue groups
1N/A** based on the queue group of each recipient.
1N/A**
1N/A** Parameters:
1N/A** e -- envelope.
1N/A**
1N/A** Results:
1N/A** SM_SPLIT_FAIL on failure
1N/A** SM_SPLIT_NONE if no splitting occurred,
1N/A** or 1 + the number of additional envelopes created.
1N/A**
1N/A** Side Effects:
1N/A** On success, e->e_sibling points to a list of zero or more
1N/A** additional envelopes, and the associated data files exist
1N/A** on disk. But the queue files are not created.
1N/A**
1N/A** On failure, e->e_sibling is not changed.
1N/A** The order of recipients in e->e_sendqueue is permuted.
1N/A** Abandoned data files for additional envelopes that failed
1N/A** to be created may exist on disk.
1N/A*/
1N/A
1N/Astatic int q_qgrp_compare __P((const void *, const void *));
1N/Astatic int e_filesys_compare __P((const void *, const void *));
1N/A
1N/Astatic int
1N/Aq_qgrp_compare(p1, p2)
1N/A const void *p1;
1N/A const void *p2;
1N/A{
1N/A ADDRESS **pq1 = (ADDRESS **) p1;
1N/A ADDRESS **pq2 = (ADDRESS **) p2;
1N/A
1N/A return (*pq1)->q_qgrp - (*pq2)->q_qgrp;
1N/A}
1N/A
1N/Astatic int
1N/Ae_filesys_compare(p1, p2)
1N/A const void *p1;
1N/A const void *p2;
1N/A{
1N/A ENVELOPE **pe1 = (ENVELOPE **) p1;
1N/A ENVELOPE **pe2 = (ENVELOPE **) p2;
1N/A int fs1, fs2;
1N/A
1N/A fs1 = Queue[(*pe1)->e_qgrp]->qg_qpaths[(*pe1)->e_qdir].qp_fsysidx;
1N/A fs2 = Queue[(*pe2)->e_qgrp]->qg_qpaths[(*pe2)->e_qdir].qp_fsysidx;
1N/A if (FILE_SYS_DEV(fs1) < FILE_SYS_DEV(fs2))
1N/A return -1;
1N/A if (FILE_SYS_DEV(fs1) > FILE_SYS_DEV(fs2))
1N/A return 1;
1N/A return 0;
1N/A}
1N/A
1N/Astatic int split_across_queue_groups __P((ENVELOPE *));
1N/Astatic int
1N/Asplit_across_queue_groups(e)
1N/A ENVELOPE *e;
1N/A{
1N/A int naddrs, nsplits, i;
1N/A bool changed;
1N/A char **pvp;
1N/A ADDRESS *q, **addrs;
1N/A ENVELOPE *ee, *es;
1N/A ENVELOPE *splits[MAXQUEUEGROUPS];
1N/A char pvpbuf[PSBUFSIZE];
1N/A
1N/A SM_REQUIRE(ISVALIDQGRP(e->e_qgrp));
1N/A
1N/A /* Count addresses and assign queue groups. */
1N/A naddrs = 0;
1N/A changed = false;
1N/A for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1N/A {
1N/A if (QS_IS_DEAD(q->q_state))
1N/A continue;
1N/A ++naddrs;
1N/A
1N/A /* bad addresses and those already sent stay put */
1N/A if (QS_IS_BADADDR(q->q_state) ||
1N/A QS_IS_SENT(q->q_state))
1N/A q->q_qgrp = e->e_qgrp;
1N/A else if (!ISVALIDQGRP(q->q_qgrp))
1N/A {
1N/A /* call ruleset which should return a queue group */
1N/A i = rscap(RS_QUEUEGROUP, q->q_user, NULL, e, &pvp,
1N/A pvpbuf, sizeof(pvpbuf));
1N/A if (i == EX_OK &&
1N/A pvp != NULL && pvp[0] != NULL &&
1N/A (pvp[0][0] & 0377) == CANONNET &&
1N/A pvp[1] != NULL && pvp[1][0] != '\0')
1N/A {
1N/A i = name2qid(pvp[1]);
1N/A if (ISVALIDQGRP(i))
1N/A {
1N/A q->q_qgrp = i;
1N/A changed = true;
1N/A if (tTd(20, 4))
1N/A sm_syslog(LOG_INFO, NOQID,
1N/A "queue group name %s -> %d",
1N/A pvp[1], i);
1N/A continue;
1N/A }
1N/A else if (LogLevel > 10)
1N/A sm_syslog(LOG_INFO, NOQID,
1N/A "can't find queue group name %s, selection ignored",
1N/A pvp[1]);
1N/A }
1N/A if (q->q_mailer != NULL &&
1N/A ISVALIDQGRP(q->q_mailer->m_qgrp))
1N/A {
1N/A changed = true;
1N/A q->q_qgrp = q->q_mailer->m_qgrp;
1N/A }
1N/A else if (ISVALIDQGRP(e->e_qgrp))
1N/A q->q_qgrp = e->e_qgrp;
1N/A else
1N/A q->q_qgrp = 0;
1N/A }
1N/A }
1N/A
1N/A /* only one address? nothing to split. */
1N/A if (naddrs <= 1 && !changed)
1N/A return SM_SPLIT_NONE;
1N/A
1N/A /* sort the addresses by queue group */
1N/A addrs = sm_rpool_malloc_x(e->e_rpool, naddrs * sizeof(ADDRESS *));
1N/A for (i = 0, q = e->e_sendqueue; q != NULL; q = q->q_next)
1N/A {
1N/A if (QS_IS_DEAD(q->q_state))
1N/A continue;
1N/A addrs[i++] = q;
1N/A }
1N/A qsort(addrs, naddrs, sizeof(ADDRESS *), q_qgrp_compare);
1N/A
1N/A /* split into multiple envelopes, by queue group */
1N/A nsplits = 0;
1N/A es = NULL;
1N/A e->e_sendqueue = NULL;
1N/A for (i = 0; i < naddrs; ++i)
1N/A {
1N/A if (i == naddrs - 1 || addrs[i]->q_qgrp != addrs[i + 1]->q_qgrp)
1N/A addrs[i]->q_next = NULL;
1N/A else
1N/A addrs[i]->q_next = addrs[i + 1];
1N/A
1N/A /* same queue group as original envelope? */
1N/A if (addrs[i]->q_qgrp == e->e_qgrp)
1N/A {
1N/A if (e->e_sendqueue == NULL)
1N/A e->e_sendqueue = addrs[i];
1N/A continue;
1N/A }
1N/A
1N/A /* different queue group than original envelope */
1N/A if (es == NULL || addrs[i]->q_qgrp != es->e_qgrp)
1N/A {
1N/A ee = split_env(e, addrs[i], addrs[i]->q_qgrp, NOQDIR);
1N/A es = ee;
1N/A splits[nsplits++] = ee;
1N/A }
1N/A }
1N/A
1N/A /* no splits? return right now. */
1N/A if (nsplits <= 0)
1N/A return SM_SPLIT_NONE;
1N/A
1N/A /* assign a queue directory to each additional envelope */
1N/A for (i = 0; i < nsplits; ++i)
1N/A {
1N/A es = splits[i];
1N/A#if 0
1N/A es->e_qdir = pickqdir(Queue[es->e_qgrp], es->e_msgsize, es);
1N/A#endif /* 0 */
1N/A if (!setnewqueue(es))
1N/A goto failure;
1N/A }
1N/A
1N/A /* sort the additional envelopes by queue file system */
1N/A qsort(splits, nsplits, sizeof(ENVELOPE *), e_filesys_compare);
1N/A
1N/A /* create data files for each additional envelope */
1N/A if (!dup_df(e, splits[0]))
1N/A {
1N/A i = 0;
1N/A goto failure;
1N/A }
1N/A for (i = 1; i < nsplits; ++i)
1N/A {
1N/A /* copy or link to the previous data file */
1N/A if (!dup_df(splits[i - 1], splits[i]))
1N/A goto failure;
1N/A }
1N/A
1N/A /* success: prepend the new envelopes to the e->e_sibling list */
1N/A for (i = 0; i < nsplits; ++i)
1N/A {
1N/A es = splits[i];
1N/A es->e_sibling = e->e_sibling;
1N/A e->e_sibling = es;
1N/A }
1N/A return SM_SPLIT_NEW(nsplits);
1N/A
1N/A /* failure: clean up */
1N/A failure:
1N/A if (i > 0)
1N/A {
1N/A int j;
1N/A
1N/A for (j = 0; j < i; j++)
1N/A (void) unlink(queuename(splits[j], DATAFL_LETTER));
1N/A }
1N/A e->e_sendqueue = addrs[0];
1N/A for (i = 0; i < naddrs - 1; ++i)
1N/A addrs[i]->q_next = addrs[i + 1];
1N/A addrs[naddrs - 1]->q_next = NULL;
1N/A return SM_SPLIT_FAIL;
1N/A}
1N/A
1N/A/*
1N/A** SPLIT_WITHIN_QUEUE
1N/A**
1N/A** Split an envelope with multiple recipients into several
1N/A** envelopes within the same queue directory, if the number of
1N/A** recipients exceeds the limit for the queue group.
1N/A**
1N/A** Parameters:
1N/A** e -- envelope.
1N/A**
1N/A** Results:
1N/A** SM_SPLIT_FAIL on failure
1N/A** SM_SPLIT_NONE if no splitting occurred,
1N/A** or 1 + the number of additional envelopes created.
1N/A*/
1N/A
1N/A#define SPLIT_LOG_LEVEL 8
1N/A
1N/Astatic int split_within_queue __P((ENVELOPE *));
1N/A
1N/Astatic int
1N/Asplit_within_queue(e)
1N/A ENVELOPE *e;
1N/A{
1N/A int maxrcpt, nrcpt, ndead, nsplit, i;
1N/A int j, l;
1N/A char *lsplits;
1N/A ADDRESS *q, **addrs;
1N/A ENVELOPE *ee, *firstsibling;
1N/A
1N/A if (!ISVALIDQGRP(e->e_qgrp) || bitset(EF_SPLIT, e->e_flags))
1N/A return SM_SPLIT_NONE;
1N/A
1N/A /* don't bother if there is no recipient limit */
1N/A maxrcpt = Queue[e->e_qgrp]->qg_maxrcpt;
1N/A if (maxrcpt <= 0)
1N/A return SM_SPLIT_NONE;
1N/A
1N/A /* count recipients */
1N/A nrcpt = 0;
1N/A for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1N/A {
1N/A if (QS_IS_DEAD(q->q_state))
1N/A continue;
1N/A ++nrcpt;
1N/A }
1N/A if (nrcpt <= maxrcpt)
1N/A return SM_SPLIT_NONE;
1N/A
1N/A /*
1N/A ** Preserve the recipient list
1N/A ** so that we can restore it in case of error.
1N/A ** (But we discard dead addresses.)
1N/A */
1N/A
1N/A addrs = sm_rpool_malloc_x(e->e_rpool, nrcpt * sizeof(ADDRESS *));
1N/A for (i = 0, q = e->e_sendqueue; q != NULL; q = q->q_next)
1N/A {
1N/A if (QS_IS_DEAD(q->q_state))
1N/A continue;
1N/A addrs[i++] = q;
1N/A }
1N/A
1N/A /*
1N/A ** Partition the recipient list so that bad and sent addresses
1N/A ** come first. These will go with the original envelope, and
1N/A ** do not count towards the maxrcpt limit.
1N/A ** addrs[] does not contain QS_IS_DEAD() addresses.
1N/A */
1N/A
1N/A ndead = 0;
1N/A for (i = 0; i < nrcpt; ++i)
1N/A {
1N/A if (QS_IS_BADADDR(addrs[i]->q_state) ||
1N/A QS_IS_SENT(addrs[i]->q_state) ||
1N/A QS_IS_DEAD(addrs[i]->q_state)) /* for paranoia's sake */
1N/A {
1N/A if (i > ndead)
1N/A {
1N/A ADDRESS *tmp = addrs[i];
1N/A
1N/A addrs[i] = addrs[ndead];
1N/A addrs[ndead] = tmp;
1N/A }
1N/A ++ndead;
1N/A }
1N/A }
1N/A
1N/A /* Check if no splitting required. */
1N/A if (nrcpt - ndead <= maxrcpt)
1N/A return SM_SPLIT_NONE;
1N/A
1N/A /* fix links */
1N/A for (i = 0; i < nrcpt - 1; ++i)
1N/A addrs[i]->q_next = addrs[i + 1];
1N/A addrs[nrcpt - 1]->q_next = NULL;
1N/A e->e_sendqueue = addrs[0];
1N/A
1N/A /* prepare buffer for logging */
1N/A if (LogLevel > SPLIT_LOG_LEVEL)
1N/A {
1N/A l = MAXLINE;
1N/A lsplits = sm_malloc(l);
1N/A if (lsplits != NULL)
1N/A *lsplits = '\0';
1N/A j = 0;
1N/A }
1N/A else
1N/A {
1N/A /* get rid of stupid compiler warnings */
1N/A lsplits = NULL;
1N/A j = l = 0;
1N/A }
1N/A
1N/A /* split the envelope */
1N/A firstsibling = e->e_sibling;
1N/A i = maxrcpt + ndead;
1N/A nsplit = 0;
1N/A for (;;)
1N/A {
1N/A addrs[i - 1]->q_next = NULL;
1N/A ee = split_env(e, addrs[i], e->e_qgrp, e->e_qdir);
1N/A if (!dup_df(e, ee))
1N/A {
1N/A
1N/A ee = firstsibling;
1N/A while (ee != NULL)
1N/A {
1N/A (void) unlink(queuename(ee, DATAFL_LETTER));
1N/A ee = ee->e_sibling;
1N/A }
1N/A
1N/A /* Error. Restore e's sibling & recipient lists. */
1N/A e->e_sibling = firstsibling;
1N/A for (i = 0; i < nrcpt - 1; ++i)
1N/A addrs[i]->q_next = addrs[i + 1];
1N/A if (lsplits != NULL)
1N/A sm_free(lsplits);
1N/A return SM_SPLIT_FAIL;
1N/A }
1N/A
1N/A /* prepend the new envelope to e->e_sibling */
1N/A ee->e_sibling = e->e_sibling;
1N/A e->e_sibling = ee;
1N/A ++nsplit;
1N/A if (LogLevel > SPLIT_LOG_LEVEL && lsplits != NULL)
1N/A {
1N/A if (j >= l - strlen(ee->e_id) - 3)
1N/A {
1N/A char *p;
1N/A
1N/A l += MAXLINE;
1N/A p = sm_realloc(lsplits, l);
1N/A if (p == NULL)
1N/A {
1N/A /* let's try to get this done */
1N/A sm_free(lsplits);
1N/A lsplits = NULL;
1N/A }
1N/A else
1N/A lsplits = p;
1N/A }
1N/A if (lsplits != NULL)
1N/A {
1N/A if (j == 0)
1N/A j += sm_strlcat(lsplits + j,
1N/A ee->e_id,
1N/A l - j);
1N/A else
1N/A j += sm_strlcat2(lsplits + j,
1N/A "; ",
1N/A ee->e_id,
1N/A l - j);
1N/A SM_ASSERT(j < l);
1N/A }
1N/A }
1N/A if (nrcpt - i <= maxrcpt)
1N/A break;
1N/A i += maxrcpt;
1N/A }
1N/A if (LogLevel > SPLIT_LOG_LEVEL && lsplits != NULL)
1N/A {
1N/A if (nsplit > 0)
1N/A {
1N/A sm_syslog(LOG_NOTICE, e->e_id,
1N/A "split: maxrcpts=%d, rcpts=%d, count=%d, id%s=%s",
1N/A maxrcpt, nrcpt - ndead, nsplit,
1N/A nsplit > 1 ? "s" : "", lsplits);
1N/A }
1N/A sm_free(lsplits);
1N/A }
1N/A return SM_SPLIT_NEW(nsplit);
1N/A}
1N/A/*
1N/A** SPLIT_BY_RECIPIENT
1N/A**
1N/A** Split an envelope with multiple recipients into multiple
1N/A** envelopes as required by the sendmail configuration.
1N/A**
1N/A** Parameters:
1N/A** e -- envelope.
1N/A**
1N/A** Results:
1N/A** Returns true on success, false on failure.
1N/A**
1N/A** Side Effects:
1N/A** see split_across_queue_groups(), split_within_queue(e)
1N/A*/
1N/A
1N/Abool
1N/Asplit_by_recipient(e)
1N/A ENVELOPE *e;
1N/A{
1N/A int split, n, i, j, l;
1N/A char *lsplits;
1N/A ENVELOPE *ee, *next, *firstsibling;
1N/A
1N/A if (OpMode == SM_VERIFY || !ISVALIDQGRP(e->e_qgrp) ||
1N/A bitset(EF_SPLIT, e->e_flags))
1N/A return true;
1N/A n = split_across_queue_groups(e);
1N/A if (n == SM_SPLIT_FAIL)
1N/A return false;
1N/A firstsibling = ee = e->e_sibling;
1N/A if (n > 1 && LogLevel > SPLIT_LOG_LEVEL)
1N/A {
1N/A l = MAXLINE;
1N/A lsplits = sm_malloc(l);
1N/A if (lsplits != NULL)
1N/A *lsplits = '\0';
1N/A j = 0;
1N/A }
1N/A else
1N/A {
1N/A /* get rid of stupid compiler warnings */
1N/A lsplits = NULL;
1N/A j = l = 0;
1N/A }
1N/A for (i = 1; i < n; ++i)
1N/A {
1N/A next = ee->e_sibling;
1N/A if (split_within_queue(ee) == SM_SPLIT_FAIL)
1N/A {
1N/A e->e_sibling = firstsibling;
1N/A return false;
1N/A }
1N/A ee->e_flags |= EF_SPLIT;
1N/A if (LogLevel > SPLIT_LOG_LEVEL && lsplits != NULL)
1N/A {
1N/A if (j >= l - strlen(ee->e_id) - 3)
1N/A {
1N/A char *p;
1N/A
1N/A l += MAXLINE;
1N/A p = sm_realloc(lsplits, l);
1N/A if (p == NULL)
1N/A {
1N/A /* let's try to get this done */
1N/A sm_free(lsplits);
1N/A lsplits = NULL;
1N/A }
1N/A else
1N/A lsplits = p;
1N/A }
1N/A if (lsplits != NULL)
1N/A {
1N/A if (j == 0)
1N/A j += sm_strlcat(lsplits + j,
1N/A ee->e_id, l - j);
1N/A else
1N/A j += sm_strlcat2(lsplits + j, "; ",
1N/A ee->e_id, l - j);
1N/A SM_ASSERT(j < l);
1N/A }
1N/A }
1N/A ee = next;
1N/A }
1N/A if (LogLevel > SPLIT_LOG_LEVEL && lsplits != NULL && n > 1)
1N/A {
1N/A sm_syslog(LOG_NOTICE, e->e_id, "split: count=%d, id%s=%s",
1N/A n - 1, n > 2 ? "s" : "", lsplits);
1N/A sm_free(lsplits);
1N/A }
1N/A split = split_within_queue(e) != SM_SPLIT_FAIL;
1N/A if (split)
1N/A e->e_flags |= EF_SPLIT;
1N/A return split;
1N/A}
1N/A
1N/A/*
1N/A** QUARANTINE_QUEUE_ITEM -- {un,}quarantine a single envelope
1N/A**
1N/A** Add/remove quarantine reason and requeue appropriately.
1N/A**
1N/A** Parameters:
1N/A** qgrp -- queue group for the item
1N/A** qdir -- queue directory in the given queue group
1N/A** e -- envelope information for the item
1N/A** reason -- quarantine reason, NULL means unquarantine.
1N/A**
1N/A** Results:
1N/A** true if item changed, false otherwise
1N/A**
1N/A** Side Effects:
1N/A** Changes quarantine tag in queue file and renames it.
1N/A*/
1N/A
1N/Astatic bool
1N/Aquarantine_queue_item(qgrp, qdir, e, reason)
1N/A int qgrp;
1N/A int qdir;
1N/A ENVELOPE *e;
1N/A char *reason;
1N/A{
1N/A bool dirty = false;
1N/A bool failing = false;
1N/A bool foundq = false;
1N/A bool finished = false;
1N/A int fd;
1N/A int flags;
1N/A int oldtype;
1N/A int newtype;
1N/A int save_errno;
1N/A MODE_T oldumask = 0;
1N/A SM_FILE_T *oldqfp, *tempqfp;
1N/A char *bp;
1N/A int bufsize;
1N/A char oldqf[MAXPATHLEN];
1N/A char tempqf[MAXPATHLEN];
1N/A char newqf[MAXPATHLEN];
1N/A char buf[MAXLINE];
1N/A
1N/A oldtype = queue_letter(e, ANYQFL_LETTER);
1N/A (void) sm_strlcpy(oldqf, queuename(e, ANYQFL_LETTER), sizeof(oldqf));
1N/A (void) sm_strlcpy(tempqf, queuename(e, NEWQFL_LETTER), sizeof(tempqf));
1N/A
1N/A /*
1N/A ** Instead of duplicating all the open
1N/A ** and lock code here, tell readqf() to
1N/A ** do that work and return the open
1N/A ** file pointer in e_lockfp. Note that
1N/A ** we must release the locks properly when
1N/A ** we are done.
1N/A */
1N/A
1N/A if (!readqf(e, true))
1N/A {
1N/A (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
1N/A "Skipping %s\n", qid_printname(e));
1N/A return false;
1N/A }
1N/A oldqfp = e->e_lockfp;
1N/A
1N/A /* open the new queue file */
1N/A flags = O_CREAT|O_WRONLY|O_EXCL;
1N/A if (bitset(S_IWGRP, QueueFileMode))
1N/A oldumask = umask(002);
1N/A fd = open(tempqf, flags, QueueFileMode);
1N/A if (bitset(S_IWGRP, QueueFileMode))
1N/A (void) umask(oldumask);
1N/A RELEASE_QUEUE;
1N/A
1N/A if (fd < 0)
1N/A {
1N/A save_errno = errno;
1N/A (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
1N/A "Skipping %s: Could not open %s: %s\n",
1N/A qid_printname(e), tempqf,
1N/A sm_errstring(save_errno));
1N/A (void) sm_io_close(oldqfp, SM_TIME_DEFAULT);
1N/A return false;
1N/A }
1N/A if (!lockfile(fd, tempqf, NULL, LOCK_EX|LOCK_NB))
1N/A {
1N/A (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
1N/A "Skipping %s: Could not lock %s\n",
1N/A qid_printname(e), tempqf);
1N/A (void) close(fd);
1N/A (void) sm_io_close(oldqfp, SM_TIME_DEFAULT);
1N/A return false;
1N/A }
1N/A
1N/A tempqfp = sm_io_open(SmFtStdiofd, SM_TIME_DEFAULT, (void *) &fd,
1N/A SM_IO_WRONLY_B, NULL);
1N/A if (tempqfp == NULL)
1N/A {
1N/A (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
1N/A "Skipping %s: Could not lock %s\n",
1N/A qid_printname(e), tempqf);
1N/A (void) close(fd);
1N/A (void) sm_io_close(oldqfp, SM_TIME_DEFAULT);
1N/A return false;
1N/A }
1N/A
1N/A /* Copy the data over, changing the quarantine reason */
1N/A while (bufsize = sizeof(buf),
1N/A (bp = fgetfolded(buf, &bufsize, oldqfp)) != NULL)
1N/A {
1N/A if (tTd(40, 4))
1N/A sm_dprintf("+++++ %s\n", bp);
1N/A switch (bp[0])
1N/A {
1N/A case 'q': /* quarantine reason */
1N/A foundq = true;
1N/A if (reason == NULL)
1N/A {
1N/A if (Verbose)
1N/A {
1N/A (void) sm_io_fprintf(smioout,
1N/A SM_TIME_DEFAULT,
1N/A "%s: Removed quarantine of \"%s\"\n",
1N/A e->e_id, &bp[1]);
1N/A }
1N/A sm_syslog(LOG_INFO, e->e_id, "unquarantine");
1N/A dirty = true;
1N/A }
1N/A else if (strcmp(reason, &bp[1]) == 0)
1N/A {
1N/A if (Verbose)
1N/A {
1N/A (void) sm_io_fprintf(smioout,
1N/A SM_TIME_DEFAULT,
1N/A "%s: Already quarantined with \"%s\"\n",
1N/A e->e_id, reason);
1N/A }
1N/A (void) sm_io_fprintf(tempqfp, SM_TIME_DEFAULT,
1N/A "q%s\n", reason);
1N/A }
1N/A else
1N/A {
1N/A if (Verbose)
1N/A {
1N/A (void) sm_io_fprintf(smioout,
1N/A SM_TIME_DEFAULT,
1N/A "%s: Quarantine changed from \"%s\" to \"%s\"\n",
1N/A e->e_id, &bp[1],
1N/A reason);
1N/A }
1N/A (void) sm_io_fprintf(tempqfp, SM_TIME_DEFAULT,
1N/A "q%s\n", reason);
1N/A sm_syslog(LOG_INFO, e->e_id, "quarantine=%s",
1N/A reason);
1N/A dirty = true;
1N/A }
1N/A break;
1N/A
1N/A case 'S':
1N/A /*
1N/A ** If we are quarantining an unquarantined item,
1N/A ** need to put in a new 'q' line before it's
1N/A ** too late.
1N/A */
1N/A
1N/A if (!foundq && reason != NULL)
1N/A {
1N/A if (Verbose)
1N/A {
1N/A (void) sm_io_fprintf(smioout,
1N/A SM_TIME_DEFAULT,
1N/A "%s: Quarantined with \"%s\"\n",
1N/A e->e_id, reason);
1N/A }
1N/A (void) sm_io_fprintf(tempqfp, SM_TIME_DEFAULT,
1N/A "q%s\n", reason);
1N/A sm_syslog(LOG_INFO, e->e_id, "quarantine=%s",
1N/A reason);
1N/A foundq = true;
1N/A dirty = true;
1N/A }
1N/A
1N/A /* Copy the line to the new file */
1N/A (void) sm_io_fprintf(tempqfp, SM_TIME_DEFAULT,
1N/A "%s\n", bp);
1N/A break;
1N/A
1N/A case '.':
1N/A finished = true;
1N/A /* FALLTHROUGH */
1N/A
1N/A default:
1N/A /* Copy the line to the new file */
1N/A (void) sm_io_fprintf(tempqfp, SM_TIME_DEFAULT,
1N/A "%s\n", bp);
1N/A break;
1N/A }
1N/A if (bp != buf)
1N/A sm_free(bp);
1N/A }
1N/A
1N/A /* Make sure we read the whole old file */
1N/A errno = sm_io_error(tempqfp);
1N/A if (errno != 0 && errno != SM_IO_EOF)
1N/A {
1N/A save_errno = errno;
1N/A (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
1N/A "Skipping %s: Error reading %s: %s\n",
1N/A qid_printname(e), oldqf,
1N/A sm_errstring(save_errno));
1N/A failing = true;
1N/A }
1N/A
1N/A if (!failing && !finished)
1N/A {
1N/A (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
1N/A "Skipping %s: Incomplete file: %s\n",
1N/A qid_printname(e), oldqf);
1N/A failing = true;
1N/A }
1N/A
1N/A /* Check if we actually changed anything or we can just bail now */
1N/A if (!dirty)
1N/A {
1N/A /* pretend we failed, even though we technically didn't */
1N/A failing = true;
1N/A }
1N/A
1N/A /* Make sure we wrote things out safely */
1N/A if (!failing &&
1N/A (sm_io_flush(tempqfp, SM_TIME_DEFAULT) != 0 ||
1N/A ((SuperSafe == SAFE_REALLY ||
1N/A SuperSafe == SAFE_REALLY_POSTMILTER ||
1N/A SuperSafe == SAFE_INTERACTIVE) &&
1N/A fsync(sm_io_getinfo(tempqfp, SM_IO_WHAT_FD, NULL)) < 0) ||
1N/A ((errno = sm_io_error(tempqfp)) != 0)))
1N/A {
1N/A save_errno = errno;
1N/A (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
1N/A "Skipping %s: Error writing %s: %s\n",
1N/A qid_printname(e), tempqf,
1N/A sm_errstring(save_errno));
1N/A failing = true;
1N/A }
1N/A
1N/A
1N/A /* Figure out the new filename */
1N/A newtype = (reason == NULL ? NORMQF_LETTER : QUARQF_LETTER);
1N/A if (oldtype == newtype)
1N/A {
1N/A /* going to rename tempqf to oldqf */
1N/A (void) sm_strlcpy(newqf, oldqf, sizeof(newqf));
1N/A }
1N/A else
1N/A {
1N/A /* going to rename tempqf to new name based on newtype */
1N/A (void) sm_strlcpy(newqf, queuename(e, newtype), sizeof(newqf));
1N/A }
1N/A
1N/A save_errno = 0;
1N/A
1N/A /* rename tempqf to newqf */
1N/A if (!failing &&
1N/A rename(tempqf, newqf) < 0)
1N/A save_errno = (errno == 0) ? EINVAL : errno;
1N/A
1N/A /* Check rename() success */
1N/A if (!failing && save_errno != 0)
1N/A {
1N/A sm_syslog(LOG_DEBUG, e->e_id,
1N/A "quarantine_queue_item: rename(%s, %s): %s",
1N/A tempqf, newqf, sm_errstring(save_errno));
1N/A
1N/A (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
1N/A "Error renaming %s to %s: %s\n",
1N/A tempqf, newqf,
1N/A sm_errstring(save_errno));
1N/A if (oldtype == newtype)
1N/A {
1N/A /*
1N/A ** Bail here since we don't know the state of
1N/A ** the filesystem and may need to keep tempqf
1N/A ** for the user to rescue us.
1N/A */
1N/A
1N/A RELEASE_QUEUE;
1N/A errno = save_errno;
1N/A syserr("!452 Error renaming control file %s", tempqf);
1N/A /* NOTREACHED */
1N/A }
1N/A else
1N/A {
1N/A /* remove new file (if rename() half completed) */
1N/A if (xunlink(newqf) < 0)
1N/A {
1N/A save_errno = errno;
1N/A (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
1N/A "Error removing %s: %s\n",
1N/A newqf,
1N/A sm_errstring(save_errno));
1N/A }
1N/A
1N/A /* tempqf removed below */
1N/A failing = true;
1N/A }
1N/A
1N/A }
1N/A
1N/A /* If changing file types, need to remove old type */
1N/A if (!failing && oldtype != newtype)
1N/A {
1N/A if (xunlink(oldqf) < 0)
1N/A {
1N/A save_errno = errno;
1N/A (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
1N/A "Error removing %s: %s\n",
1N/A oldqf, sm_errstring(save_errno));
1N/A }
1N/A }
1N/A
1N/A /* see if anything above failed */
1N/A if (failing)
1N/A {
1N/A /* Something failed: remove new file, old file still there */
1N/A (void) xunlink(tempqf);
1N/A }
1N/A
1N/A /*
1N/A ** fsync() after file operations to make sure metadata is
1N/A ** written to disk on filesystems in which renames are
1N/A ** not guaranteed. It's ok if they fail, mail won't be lost.
1N/A */
1N/A
1N/A if (SuperSafe != SAFE_NO)
1N/A {
1N/A /* for soft-updates */
1N/A (void) fsync(sm_io_getinfo(tempqfp,
1N/A SM_IO_WHAT_FD, NULL));
1N/A
1N/A if (!failing)
1N/A {
1N/A /* for soft-updates */
1N/A (void) fsync(sm_io_getinfo(oldqfp,
1N/A SM_IO_WHAT_FD, NULL));
1N/A }
1N/A
1N/A /* for other odd filesystems */
1N/A SYNC_DIR(tempqf, false);
1N/A }
1N/A
1N/A /* Close up shop */
1N/A RELEASE_QUEUE;
1N/A if (tempqfp != NULL)
1N/A (void) sm_io_close(tempqfp, SM_TIME_DEFAULT);
1N/A if (oldqfp != NULL)
1N/A (void) sm_io_close(oldqfp, SM_TIME_DEFAULT);
1N/A
1N/A /* All went well */
1N/A return !failing;
1N/A}
1N/A
1N/A/*
1N/A** QUARANTINE_QUEUE -- {un,}quarantine matching items in the queue
1N/A**
1N/A** Read all matching queue items, add/remove quarantine
1N/A** reason, and requeue appropriately.
1N/A**
1N/A** Parameters:
1N/A** reason -- quarantine reason, "." means unquarantine.
1N/A** qgrplimit -- limit to single queue group unless NOQGRP
1N/A**
1N/A** Results:
1N/A** none.
1N/A**
1N/A** Side Effects:
1N/A** Lots of changes to the queue.
1N/A*/
1N/A
1N/Avoid
1N/Aquarantine_queue(reason, qgrplimit)
1N/A char *reason;
1N/A int qgrplimit;
1N/A{
1N/A int changed = 0;
1N/A int qgrp;
1N/A
1N/A /* Convert internal representation of unquarantine */
1N/A if (reason != NULL && reason[0] == '.' && reason[1] == '\0')
1N/A reason = NULL;
1N/A
1N/A if (reason != NULL)
1N/A {
1N/A /* clean it */
1N/A reason = newstr(denlstring(reason, true, true));
1N/A }
1N/A
1N/A for (qgrp = 0; qgrp < NumQueue && Queue[qgrp] != NULL; qgrp++)
1N/A {
1N/A int qdir;
1N/A
1N/A if (qgrplimit != NOQGRP && qgrplimit != qgrp)
1N/A continue;
1N/A
1N/A for (qdir = 0; qdir < Queue[qgrp]->qg_numqueues; qdir++)
1N/A {
1N/A int i;
1N/A int nrequests;
1N/A
1N/A if (StopRequest)
1N/A stop_sendmail();
1N/A
1N/A nrequests = gatherq(qgrp, qdir, true, NULL, NULL, NULL);
1N/A
1N/A /* first see if there is anything */
1N/A if (nrequests <= 0)
1N/A {
1N/A if (Verbose)
1N/A {
1N/A (void) sm_io_fprintf(smioout,
1N/A SM_TIME_DEFAULT, "%s: no matches\n",
1N/A qid_printqueue(qgrp, qdir));
1N/A }
1N/A continue;
1N/A }
1N/A
1N/A if (Verbose)
1N/A {
1N/A (void) sm_io_fprintf(smioout,
1N/A SM_TIME_DEFAULT, "Processing %s:\n",
1N/A qid_printqueue(qgrp, qdir));
1N/A }
1N/A
1N/A for (i = 0; i < WorkListCount; i++)
1N/A {
1N/A ENVELOPE e;
1N/A
1N/A if (StopRequest)
1N/A stop_sendmail();
1N/A
1N/A /* setup envelope */
1N/A clearenvelope(&e, true, sm_rpool_new_x(NULL));
1N/A e.e_id = WorkList[i].w_name + 2;
1N/A e.e_qgrp = qgrp;
1N/A e.e_qdir = qdir;
1N/A
1N/A if (tTd(70, 101))
1N/A {
1N/A sm_io_fprintf(smioout, SM_TIME_DEFAULT,
1N/A "Would do %s\n", e.e_id);
1N/A changed++;
1N/A }
1N/A else if (quarantine_queue_item(qgrp, qdir,
1N/A &e, reason))
1N/A changed++;
1N/A
1N/A /* clean up */
1N/A sm_rpool_free(e.e_rpool);
1N/A e.e_rpool = NULL;
1N/A }
1N/A if (WorkList != NULL)
1N/A sm_free(WorkList); /* XXX */
1N/A WorkList = NULL;
1N/A WorkListSize = 0;
1N/A WorkListCount = 0;
1N/A }
1N/A }
1N/A if (Verbose)
1N/A {
1N/A if (changed == 0)
1N/A (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
1N/A "No changes\n");
1N/A else
1N/A (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
1N/A "%d change%s\n",
1N/A changed,
1N/A changed == 1 ? "" : "s");
1N/A }
1N/A}