head 1.2; access; symbols OPENPKG_2_STABLE_MP:1.2 OPENPKG_E1_MP_HEAD:1.2 OPENPKG_E1_MP:1.2 OPENPKG_E1_MP_2_STABLE:1.2 OPENPKG_E1_FP:1.2 OPENPKG_2_STABLE_20061018:1.2 OPENPKG_2_STABLE:1.2.0.12 OPENPKG_2_STABLE_BP:1.2 OPENPKG_2_5_SOLID:1.2.0.10 OPENPKG_2_5_SOLID_BP:1.2 OPENPKG_2_4_RELEASE:1.2 OPENPKG_2_4_SOLID:1.2.0.8 OPENPKG_2_4_SOLID_BP:1.2 OPENPKG_2_3_RELEASE:1.2 OPENPKG_2_3_SOLID:1.2.0.6 OPENPKG_2_3_SOLID_BP:1.2 OPENPKG_2_2_RELEASE:1.2 OPENPKG_2_2_SOLID:1.2.0.4 OPENPKG_2_2_SOLID_BP:1.2 OPENPKG_2_1_RELEASE:1.2 OPENPKG_2_1_SOLID:1.2.0.2 OPENPKG_2_1_SOLID_BP:1.2; locks; strict; comment @# @; 1.2 date 2004.06.21.09.03.48; author thl; state dead; branches; next 1.1; 1.1 date 2004.05.10.16.54.27; author rse; state Exp; branches; next ; desc @@ 1.2 log @upgrading package: sendmail 8.12.11 -> 8.13.0 @ text @--- sendmail/util.c.dist Fri May 7 14:47:17 2004 +++ sendmail/util.c Fri May 7 14:50:28 2004 @@@@ -540,49 +540,82 @@@@ ** ** Side Effects: ** writes pidfile, logs command line. +** keeps file open and locked to prevent overwrite of active file */ +static SM_FILE_T *Pidf = NULL; + void log_sendmail_pid(e) ENVELOPE *e; { long sff; - SM_FILE_T *pidf; char pidpath[MAXPATHLEN]; extern char *CommandLineArgs; /* write the pid to the log file for posterity */ - sff = SFF_NOLINK|SFF_ROOTOK|SFF_REGONLY|SFF_CREAT; + sff = SFF_NOLINK|SFF_ROOTOK|SFF_REGONLY|SFF_CREAT|SFF_NBLOCK; if (TrustedUid != 0 && RealUid == TrustedUid) sff |= SFF_OPENASROOT; expand(PidFile, pidpath, sizeof pidpath, e); - pidf = safefopen(pidpath, O_WRONLY|O_TRUNC, FileMode, sff); - if (pidf == NULL) + Pidf = safefopen(pidpath, O_WRONLY|O_TRUNC, FileMode, sff); + if (Pidf == NULL) { - sm_syslog(LOG_ERR, NOQID, "unable to write %s: %s", - pidpath, sm_errstring(errno)); + if (errno == EWOULDBLOCK) + sm_syslog(LOG_ERR, NOQID, + "unable to write pid to %s: file in use by another process", + pidpath); + else + sm_syslog(LOG_ERR, NOQID, + "unable to write pid to %s: %s", + pidpath, sm_errstring(errno)); } else { - pid_t pid; + PidFilePid = getpid(); - pid = getpid(); - /* write the process id on line 1 */ - (void) sm_io_fprintf(pidf, SM_TIME_DEFAULT, "%ld\n", - (long) pid); + (void) sm_io_fprintf(Pidf, SM_TIME_DEFAULT, "%ld\n", + (long) PidFilePid); /* line 2 contains all command line flags */ - (void) sm_io_fprintf(pidf, SM_TIME_DEFAULT, "%s\n", + (void) sm_io_fprintf(Pidf, SM_TIME_DEFAULT, "%s\n", CommandLineArgs); - /* flush and close */ - (void) sm_io_close(pidf, SM_TIME_DEFAULT); + /* flush */ + (void) sm_io_flush(Pidf, SM_TIME_DEFAULT); + + /* + ** Leave pid file open until process ends + ** so it's not overwritten by another + ** process. + */ } if (LogLevel > 9) sm_syslog(LOG_INFO, NOQID, "started as: %s", CommandLineArgs); } + /* +** CLOSE_SENDMAIL_PID -- close sendmail pid file +** +** Parameters: +** none. +** +** Returns: +** none. +*/ + +void +close_sendmail_pid() +{ + if (Pidf == NULL) + return; + + (void) sm_io_close(Pidf, SM_TIME_DEFAULT); + Pidf = NULL; +} + +/* ** SET_DELIVERY_MODE -- set and record the delivery mode ** ** Parameters: --- sendmail/main.c.dist Fri May 7 14:44:31 2004 +++ sendmail/main.c Fri May 7 14:47:08 2004 @@@@ -2385,6 +2385,14 @@@@ if (OpMode != MD_DAEMON && queuepersistent) { + /* + ** Write the pid to file + ** XXX Overwrites sendmail.pid + */ + + log_sendmail_pid(&MainEnvelope); + + /* set the title to make it easier to find */ sm_setproctitle(true, CurEnv, "Queue control"); (void) sm_signal(SIGCHLD, SIG_DFL); @@@@ -2881,6 +2889,7 @@@@ bool cleanup; volatile int exitstat; { + char pidpath[MAXPATHLEN]; /* Still want to process new timeouts added below */ sm_clear_events(); @@@@ -2953,6 +2962,25 @@@@ cleanup_shm(DaemonPid == getpid()); #endif /* SM_CONF_SHM */ + /* close locked pid file */ + close_sendmail_pid(); + + if (DaemonPid == getpid()) + { + /* blow away the pid file */ + expand(PidFile, pidpath, sizeof pidpath, CurEnv); + (void) unlink(pidpath); + } + + if (PidFilePid == getpid()) + { + /* blow away the pid file */ + expand(PidFile, pidpath, sizeof pidpath, CurEnv); + (void) unlink(pidpath); + } + + + /* reset uid for process accounting */ endpwent(); sm_mbdb_terminate(); --- sendmail/sendmail.h.dist Fri May 7 15:11:17 2004 +++ sendmail/sendmail.h Fri May 7 15:11:46 2004 @@@@ -2250,6 +2250,7 @@@@ #endif /* SM_CONF_SHM */ EXTERN pid_t CurrentPid; /* current process id */ EXTERN pid_t DaemonPid; /* process id of daemon */ +EXTERN pid_t PidFilePid; /* daemon/queue runner who wrote pid file */ EXTERN uid_t DefUid; /* default uid to run as */ EXTERN uid_t RealUid; /* real uid of caller */ EXTERN uid_t RunAsUid; /* UID to become for bulk of run */ --- libsmutil/safefile.c.dist Fri May 24 13:50:15 2002 +++ libsmutil/safefile.c Mon Jul 1 15:18:53 2002 @@@@ -15,7 +15,7 @@@@ #include #include -SM_RCSID("@@(#)$Id: sendmail.patch.pidfile,v 1.1 2004/05/10 16:54:27 openpkg-cvs Exp $") +SM_RCSID("@@(#)$Id: sendmail.patch.pidfile,v 1.1 2004/05/10 16:54:27 openpkg-cvs Exp $") /* @@@@ -684,6 +684,9 @@@@ int cmode; long sff; { +#if !NOFTRUNCATE + bool truncate; +#endif /* !NOFTRUNCATE */ int rval; int fd; int smode; @@@@ -735,6 +738,12 @@@@ return -1; } +#if !NOFTRUNCATE + truncate = bitset(O_TRUNC, omode); + if (truncate) + omode &= ~O_TRUNC; +#endif /* !NOFTRUNCATE */ + fd = dfopen(fn, omode, cmode, sff); if (fd < 0) return fd; @@@@ -745,6 +754,22 @@@@ errno = E_SM_FILECHANGE; return -1; } + +#if !NOFTRUNCATE + if (truncate && + ftruncate(fd, (off_t) 0) < 0) + { + int save_errno; + + save_errno = errno; + syserr("554 5.3.0 cannot open: file %s could not be truncated", + fn); + (void) close(fd); + errno = save_errno; + return -1; + } +#endif /* !NOFTRUNCATE */ + return fd; } /* @@@@ -940,6 +965,9 @@@@ locktype = LOCK_EX; else locktype = LOCK_SH; + if (bitset(SFF_NBLOCK, sff)) + locktype |= LOCK_NB; + if (!lockfile(fd, filename, NULL, locktype)) { int save_errno = errno; --- include/sendmail/sendmail.h.dist Fri May 7 16:04:06 2004 +++ include/sendmail/sendmail.h Fri May 7 16:03:36 2004 @@@@ -55,6 +55,7 @@@@ #define SFF_NOWRFILES 0x00010000L /* disallow o readable files */ #define SFF_NOTEXCL 0x00020000L /* creates don't need to be exclusive */ #define SFF_EXECOK 0x00040000L /* executable files are ok (E_SM_ISEXEC) */ +#define SFF_NBLOCK 0x00080000L /* use a non-blocking lock */ #define SFF_NORFILES (SFF_NOGRFILES|SFF_NOWRFILES) /* pseudo-flags */ @ 1.1 log @Include Sendmail PID file handling patch. Submitted by: Darrell Fuhriman @ text @d167 2 a168 2 -SM_RCSID("@@(#)$Id: safefile.c,v 8.124 2002/05/24 20:50:15 gshapiro Exp $") +SM_RCSID("@@(#)$Id: safefile.c,v 8.126 2002/07/01 22:18:53 gshapiro Exp $") @