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

ccdd61
diff --git a/bin/mailmanctl b/bin/mailmanctl
ccdd61
index fa14a2c..e291df1 100644
ccdd61
--- a/bin/mailmanctl
ccdd61
+++ b/bin/mailmanctl
ccdd61
@@ -36,7 +36,7 @@ in the file data/master-qrunner.pid but you normally don't need to use this
ccdd61
 pid directly.  The `start', `stop', `restart', and `reopen' commands handle
ccdd61
 everything for you.
ccdd61
 
ccdd61
-Usage: %(PROGRAM)s [options] [ start | stop | restart | reopen ]
ccdd61
+Usage: %(PROGRAM)s [options] [ start | stop | restart | reopen | status ]
ccdd61
 
ccdd61
 Options:
ccdd61
 
ccdd61
@@ -90,6 +90,9 @@ Commands:
ccdd61
 
ccdd61
     reopen  - This will close all log files, causing them to be re-opened the
ccdd61
               next time a message is written to them
ccdd61
+
ccdd61
+    status  - This returns a string indicating the status of the master
ccdd61
+              qrunner
ccdd61
 """
ccdd61
 
ccdd61
 import sys
ccdd61
@@ -190,6 +193,52 @@ def qrunner_state():
ccdd61
         return 0
ccdd61
     return 1
ccdd61
 
ccdd61
+def mailman_status():
ccdd61
+    # return status, pid
ccdd61
+    #
ccdd61
+    # These status values match the /etc/init.d status values
ccdd61
+    # (at least on Red Hat), try to return equivalent status if possible
ccdd61
+    # status is 0 if running,
ccdd61
+    # status is 1 if dead but pid file exists
ccdd61
+    # status is 2 if dead but subsys locked
ccdd61
+    # status is 3 if stopped (pid returned will be 0)
ccdd61
+    #
ccdd61
+    #
ccdd61
+    # We want any user to be able to query the status and this presents
ccdd61
+    # few interesting permission problems and is why we don't use
ccdd61
+    # qrunner_state(). The pidfile is only readable by the mailman owner
ccdd61
+    # and group, however the lockfile is world readable. So we will
ccdd61
+    # get the master pid from the lockfile. We try to determine if the
ccdd61
+    # master process exists by sending it a signal. If we don't have
ccdd61
+    # permission to signal the process, but the process exists we'll
ccdd61
+    # get a EPERM error, if the process does not exist then we'll get
ccdd61
+    # a ESRCH error.
ccdd61
+
ccdd61
+    try:
ccdd61
+        hostname, pid, tempfile = get_lock_data()
ccdd61
+    except IOError, e:
ccdd61
+        if e.errno == errno.ENOENT:
ccdd61
+            # Lock file didn't exist, can't be running
ccdd61
+            return 3, 0
ccdd61
+        else:
ccdd61
+            raise
ccdd61
+    if hostname <> socket.gethostname():
ccdd61
+        # not running on this host
ccdd61
+        return 3, 0
ccdd61
+    # Find out if the process exists by calling kill with a signal 0.
ccdd61
+    try:
ccdd61
+        os.kill(pid, 0)
ccdd61
+    except OSError, e:
ccdd61
+        if e.errno == errno.ESRCH:
ccdd61
+            # process does not exist
ccdd61
+            return 1, pid
ccdd61
+        elif e.errno == errno.EPERM:
ccdd61
+            # we don't have permission signal the process but it exists
ccdd61
+            return 0, pid
ccdd61
+        else:
ccdd61
+            raise
ccdd61
+    return 0, pid
ccdd61
+
ccdd61
 
ccdd61
 def acquire_lock_1(force):
ccdd61
     # Be sure we can acquire the master qrunner lock.  If not, it means some
ccdd61
@@ -338,13 +387,15 @@ def main():
ccdd61
         command = COMMASPACE.join(args)
ccdd61
         usage(1, C_('Bad command: %(command)s'))
ccdd61
 
ccdd61
+    command = args[0].lower()
ccdd61
+
ccdd61
     if checkprivs:
ccdd61
         check_privs()
ccdd61
     else:
ccdd61
-        print C_('Warning!  You may encounter permission problems.')
ccdd61
+        if command != 'status':
ccdd61
+	    print C_('Warning!  You may encounter permission problems.')
ccdd61
 
ccdd61
     # Handle the commands
ccdd61
-    command = args[0].lower()
ccdd61
     if command == 'stop':
ccdd61
         # Sent the master qrunner process a SIGINT, which is equivalent to
ccdd61
         # giving cron/qrunner a ctrl-c or KeyboardInterrupt.  This will
ccdd61
@@ -363,6 +414,14 @@ def main():
ccdd61
         if not quiet:
ccdd61
             print C_('Re-opening all log files')
ccdd61
         kill_watcher(signal.SIGHUP)
ccdd61
+    elif command == 'status':
ccdd61
+        status, pid = mailman_status()
ccdd61
+        if not quiet:
ccdd61
+            if status == 0:
ccdd61
+                print C_("mailman (pid %(pid)d) is running...")
ccdd61
+            else:
ccdd61
+                print C_("mailman is stopped")
ccdd61
+        sys.exit(status)
ccdd61
     elif command == 'start':
ccdd61
         # First, complain loudly if there's no site list.
ccdd61
         check_for_site_list()