49da8b
diff --git a/bin/mailmanctl b/bin/mailmanctl
49da8b
index 613f909..3d59d57 100644
49da8b
--- a/bin/mailmanctl
49da8b
+++ b/bin/mailmanctl
49da8b
@@ -36,7 +36,7 @@ in the file data/master-qrunner.pid but you normally don't need to use this
49da8b
 pid directly.  The `start', `stop', `restart', and `reopen' commands handle
49da8b
 everything for you.
49da8b
 
49da8b
-Usage: %(PROGRAM)s [options] [ start | stop | restart | reopen ]
49da8b
+Usage: %(PROGRAM)s [options] [ start | stop | restart | reopen | status ]
49da8b
 
49da8b
 Options:
49da8b
 
49da8b
@@ -90,6 +90,9 @@ Commands:
49da8b
 
49da8b
     reopen  - This will close all log files, causing them to be re-opened the
49da8b
               next time a message is written to them
49da8b
+
49da8b
+    status  - This returns a string indicating the status of the master
49da8b
+              qrunner
49da8b
 """
49da8b
 
49da8b
 import sys
49da8b
@@ -190,6 +193,52 @@ def qrunner_state():
49da8b
         return 0
49da8b
     return 1
49da8b
 
49da8b
+def mailman_status():
49da8b
+    # return status, pid
49da8b
+    #
49da8b
+    # These status values match the /etc/init.d status values
49da8b
+    # (at least on Red Hat), try to return equivalent status if possible
49da8b
+    # status is 0 if running,
49da8b
+    # status is 1 if dead but pid file exists
49da8b
+    # status is 2 if dead but subsys locked
49da8b
+    # status is 3 if stopped (pid returned will be 0)
49da8b
+    #
49da8b
+    #
49da8b
+    # We want any user to be able to query the status and this presents
49da8b
+    # few interesting permission problems and is why we don't use
49da8b
+    # qrunner_state(). The pidfile is only readable by the mailman owner
49da8b
+    # and group, however the lockfile is world readable. So we will
49da8b
+    # get the master pid from the lockfile. We try to determine if the
49da8b
+    # master process exists by sending it a signal. If we don't have
49da8b
+    # permission to signal the process, but the process exists we'll
49da8b
+    # get a EPERM error, if the process does not exist then we'll get
49da8b
+    # a ESRCH error.
49da8b
+
49da8b
+    try:
49da8b
+        hostname, pid, tempfile = get_lock_data()
49da8b
+    except IOError, e:
49da8b
+        if e.errno == errno.ENOENT:
49da8b
+            # Lock file didn't exist, can't be running
49da8b
+            return 3, 0
49da8b
+        else:
49da8b
+            raise
49da8b
+    if hostname <> socket.gethostname():
49da8b
+        # not running on this host
49da8b
+        return 3, 0
49da8b
+    # Find out if the process exists by calling kill with a signal 0.
49da8b
+    try:
49da8b
+        os.kill(pid, 0)
49da8b
+    except OSError, e:
49da8b
+        if e.errno == errno.ESRCH:
49da8b
+            # process does not exist
49da8b
+            return 1, pid
49da8b
+        elif e.errno == errno.EPERM:
49da8b
+            # we don't have permission signal the process but it exists
49da8b
+            return 0, pid
49da8b
+        else:
49da8b
+            raise
49da8b
+    return 0, pid
49da8b
+
49da8b
 
49da8b
 def acquire_lock_1(force):
49da8b
     # Be sure we can acquire the master qrunner lock.  If not, it means some
49da8b
@@ -337,13 +386,15 @@ def main():
49da8b
         command = COMMASPACE.join(args)
49da8b
         usage(1, _('Bad command: %(command)s'))
49da8b
 
49da8b
+    command = args[0].lower()
49da8b
+
49da8b
     if checkprivs:
49da8b
         check_privs()
49da8b
     else:
49da8b
-        print _('Warning!  You may encounter permission problems.')
49da8b
+        if command != 'status':
49da8b
+	    print _('Warning!  You may encounter permission problems.')
49da8b
 
49da8b
     # Handle the commands
49da8b
-    command = args[0].lower()
49da8b
     if command == 'stop':
49da8b
         # Sent the master qrunner process a SIGINT, which is equivalent to
49da8b
         # giving cron/qrunner a ctrl-c or KeyboardInterrupt.  This will
49da8b
@@ -362,6 +413,14 @@ def main():
49da8b
         if not quiet:
49da8b
             print _('Re-opening all log files')
49da8b
         kill_watcher(signal.SIGHUP)
49da8b
+    elif command == 'status':
49da8b
+        status, pid = mailman_status()
49da8b
+        if not quiet:
49da8b
+            if status == 0:
49da8b
+                print _("mailman (pid %(pid)d) is running...")
49da8b
+            else:
49da8b
+                print _("mailman is stopped")
49da8b
+        sys.exit(status)
49da8b
     elif command == 'start':
49da8b
         # First, complain loudly if there's no site list.
49da8b
         check_for_site_list()
49da8b
diff --git a/misc/mailman.in b/misc/mailman.in
49da8b
index 2f5fbc7..bce344c 100644
49da8b
--- a/misc/mailman.in
49da8b
+++ b/misc/mailman.in
49da8b
@@ -36,19 +36,70 @@ PYTHON=@PYTHON@
49da8b
 MAILMANHOME=@prefix@
49da8b
 MAILMANCTL=$MAILMANHOME/bin/mailmanctl
49da8b
 
49da8b
+# Source function library.
49da8b
+. /etc/rc.d/init.d/functions
49da8b
+
49da8b
+RETVAL=0
49da8b
+prog="mailman"
49da8b
+
49da8b
+function start()
49da8b
+{
49da8b
+    echo -n $"Starting $prog: "
49da8b
+    daemon $PYTHON $MAILMANCTL -s -q start
49da8b
+    RETVAL=$?
49da8b
+    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
49da8b
+    echo
49da8b
+    return $RETVAL
49da8b
+}
49da8b
+
49da8b
+function stop()
49da8b
+{
49da8b
+    echo -n $"Shutting down $prog: "
49da8b
+    daemon $PYTHON $MAILMANCTL -q stop
49da8b
+    RETVAL=$?
49da8b
+    [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog
49da8b
+    echo
49da8b
+    return $RETVAL
49da8b
+}
49da8b
+
49da8b
+function restart()
49da8b
+{
49da8b
+    stop
49da8b
+    start
49da8b
+    RETVAL=$?
49da8b
+    return $RETVAL
49da8b
+}
49da8b
+
49da8b
 case "$1" in
49da8b
 'start')
49da8b
-    #rm -f $MAILMANHOME/locks/*
49da8b
-    $PYTHON $MAILMANCTL -s -q start
49da8b
+    start
49da8b
+    RETVAL=$?
49da8b
     ;;
49da8b
 
49da8b
 'stop')
49da8b
-    $PYTHON $MAILMANCTL -q stop
49da8b
+    stop
49da8b
+    RETVAL=$?
49da8b
     ;;
49da8b
 
49da8b
 'restart')
49da8b
-    $PYTHON $MAILMANCTL -q restart
49da8b
+    restart
49da8b
+    RETVAL=$?
49da8b
+    ;;
49da8b
+
49da8b
+'condrestart')
49da8b
+    $PYTHON $MAILMANCTL -q -u status
49da8b
+    retval=$?
49da8b
+    if [ $retval -eq 0 ]
49da8b
+    then
49da8b
+	restart
49da8b
+	RETVAL=$?
49da8b
+    fi
49da8b
+    ;;
49da8b
+
49da8b
+'status')
49da8b
+    $PYTHON $MAILMANCTL -u status
49da8b
+    RETVAL=$?
49da8b
     ;;
49da8b
 
49da8b
 esac
49da8b
-exit 0
49da8b
+exit $RETVAL