areguera / rpms / mailman

Forked from rpms/mailman 5 years ago
Clone

Blame SOURCES/mailman-2.1-mailmanctl-status.patch

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