This patch should be applied on top of the previous one in this thread (in
message <20011128123038.K13192@khazad-dum>).

It adds configure support for --enable-pidfile (which enables pidfile and
daemon mode). The full filename (with path info) for the pidfile can be sent
as the optional argument for --enable-pidfile.

The default is the old behaviour (no pidfile). The default pidfile is
/var/run/cyrus-master.pid (unlikely to cause colisions).

Please consider adding this patch to Cyrus IMAP 2.1.0.

-- 
  "One disk to rule them all, One disk to find them. One disk to bring
  them all and in the darkness grind them. In the Land of Redmond
  where the shadows lie." -- The Silicon Valley Tarot
  Henrique Holschuh
Index: debian/cyrus21-imapd/acconfig.h
diff -u debian/cyrus21-imapd/acconfig.h:1.2 debian/cyrus21-imapd/acconfig.h:1.3
--- debian/cyrus21-imapd/acconfig.h:1.2 Fri Nov 30 22:57:11 2001
+++ debian/cyrus21-imapd/acconfig.h     Mon Dec  3 12:31:06 2001
@@ -120,6 +120,12 @@
 /* do we have rlim_t? */
 #undef HAVE_RLIM_T
 
+/* do we want master to run in daemon mode? */
+#undef ENABLE_DAEMON_MODE
+
+/* path to pid lockfile for master */
+#undef MASTER_PIDFILE
+
 @BOTTOM@
 
 #ifndef __GNUC__
Index: debian/cyrus21-imapd/configure.in
diff -u debian/cyrus21-imapd/configure.in:1.3 debian/cyrus21-imapd/configure.in:1.4
--- debian/cyrus21-imapd/configure.in:1.3       Fri Nov 30 22:57:11 2001
+++ debian/cyrus21-imapd/configure.in   Mon Dec  3 12:31:06 2001
@@ -597,6 +597,21 @@
 AC_SUBST(NOTIFY_ZEPHYR)
 
 dnl
+dnl Enable/disable daemon support, set pidfile location
+dnl
+AC_ARG_ENABLE(pidfile,[  --enable-pidfile[=PATH]     Enable daemon mode, with pidfile 
+set to PATH (/var/run/cyrus-master.pid)],
+       [MASTERPIDFILE="$enableval"],
+       [MASTERPIDFILE="/var/run/cyrus-master.pid"])
+if test "yes" = "$MASTERPIDFILE" ; then
+       MASTERPIDFILE="/var/run/cyrus-master.pid"
+fi
+if test "$MASTERPIDFILE" != "no" ; then
+       AC_DEFINE(ENABLE_DAEMON_MODE)
+       MASTERPIDFILE="\"$MASTERPIDFILE\""
+       AC_DEFINE_UNQUOTED(MASTER_PIDFILE, $MASTERPIDFILE)
+fi
+
+dnl
 dnl Select a method for IMAP IDLE
 dnl
 AC_ARG_WITH(idle,[  --with-idle=METHOD      use METHOD for IMAP IDLE
Index: debian/cyrus21-imapd/master/master.c
diff -u debian/cyrus21-imapd/master/master.c:1.4 
debian/cyrus21-imapd/master/master.c:1.5
--- debian/cyrus21-imapd/master/master.c:1.4    Wed Nov 28 12:55:53 2001
+++ debian/cyrus21-imapd/master/master.c        Mon Dec  3 12:31:06 2001
@@ -96,7 +96,6 @@
 #include "lock.h"
 
 #define SERVICE_PATH (CYRUS_PATH "/bin")
-#define PIDFILE "/var/run/cyrmaster.pid"
 
 enum {
     become_cyrus_early = 1,
@@ -187,6 +186,7 @@
     return result;
 }
 
+#ifdef ENABLE_DAEMON_MODE
 void acquire_daemon_lock(int closeflag)
 /* Copyright 1988,1990,1993,1994 by Paul Vixie
  * All rights reserved
@@ -222,10 +222,10 @@
        if (!fp) {
                int     fd;
 
-               if ((-1 == (fd = open(PIDFILE, O_RDWR|O_CREAT, 0644)))
+               if ((-1 == (fd = open(MASTER_PIDFILE, O_RDWR|O_CREAT, 0644)))
                    || (NULL == (fp = fdopen(fd, "r+")))
                    ) {
-                       fatal("couldn't open or create pidfile " PIDFILE ": %m",1);
+                       fatal("couldn't open or create pidfile " MASTER_PIDFILE ": 
+%m",1);
                }
 
                if (lock_nonblocking(fd)) {
@@ -248,6 +248,7 @@
         * keep it open and locked, but we don't need the handles elsewhere.
         */
 }
+#endif /* ENABLE_DAEMON_MODE */
 
 void get_prog(char *path, char *const *cmd)
 {
@@ -495,7 +496,9 @@
        break;
        
     case 0:
+#ifdef ENABLE_DAEMON_MODE
        acquire_daemon_lock(1);
+#endif
     
        if (become_cyrus() != 0) {
            syslog(LOG_ERR, "can't change to the cyrus user");
@@ -544,7 +547,9 @@
 
     case 0:
        /* child */
-       acquire_daemon_lock(1);
+#ifdef ENABLE_DAEMON_MODE
+       acquire_daemon_lock(1);
+#endif
        if (become_cyrus() != 0) {
            syslog(LOG_ERR, "can't change to the cyrus user");
            exit(1);
@@ -655,7 +660,9 @@
            break;
 
        case 0:
+#ifdef ENABLE_DAEMON_MODE
            acquire_daemon_lock(1);
+#endif
            if (become_cyrus() != 0) {
                syslog(LOG_ERR, "can't change to the cyrus user");
                exit(1);
@@ -1149,18 +1156,32 @@
 
     limit_fds(RLIM_INFINITY);
 
+#ifdef ENABLE_DAEMON_MODE
     /* lock pidfile (and create it while we're root) */
     acquire_daemon_lock(0);
+#endif
 
     masterconf_init("master");
 
-#ifdef HAVE_UNISTD_H
+#ifdef ENABLE_DAEMON_MODE
     /* go daemon, if not in debug mode */
     if (close_std) {
+#ifdef HAVE_UNISTD_H
         if (daemon(0, 0)) fatal("could not enter daemon mode: %m", 2);
+#else
+       switch (fork()) {
+               case -1:
+                       fatal("could not fork to background: %m", 2);
+               case 0: /* child */
+                       break;
+               default: /* parent */
+                       exit(0);
+       }
+       setsid();
+#endif /* HAVE_UNISTD_H */
         acquire_daemon_lock(0);
     }
-#endif /* HAVE_UNISTD_H */
+#endif
 
     syslog(LOG_NOTICE, "process started");
 

Reply via email to