[Zope-Checkins] CVS: Zope/lib/python/zdaemon - Daemon.py:1.5

Jeremy Hylton jeremy@zope.com
Mon, 22 Apr 2002 18:09:27 -0400


Update of /cvs-repository/Zope/lib/python/zdaemon
In directory cvs.zope.org:/tmp/cvs-serv17942

Modified Files:
	Daemon.py 
Log Message:
Improve error reporting when child exits or is killed by a signal.

Report explicitly what happened and include either the exit status or
the signal number.

XXX Not sure what to do if the process is just stopped.  The previous
version of the code treated it as an error.  This version doesn't
change that but does log a message saying its stopped.

XXX Not sure how to report whether a core was created.  The old code
did this implicitly if the platform/C library supported it.



=== Zope/lib/python/zdaemon/Daemon.py 1.4 => 1.5 ===
                             continue
                     if s:
-                        pstamp(('Aiieee! %s exited with error code: %s' 
-                                % (p, s)), zLOG.ERROR)
+                        if os.WIFEXITED(s):
+                            es = os.WEXITSTATUS(s)
+                            msg = "terminated normally, exit status: %s" % es
+                        elif os.WIFSIGNALED(s):
+                            signum = os.WTERMSIG(s)
+                            signame = get_signal_name(signum)
+                            msg = "terminated by signal %s(%s)" % (signame,
+                                                                  signum)
+                        else:
+                            # XXX what should we do here?
+                            signum = os.WSTOPSIG(s)
+                            signame = get_signal_name(signum)
+                            msg = "stopped by signal %s(%s)" % (signame,
+                                                                signum)
+                        pstamp('Aiieee! Process %s %s' % (p, msg),
+                               zLOG.ERROR)
                     else:
                         pstamp(('The kid, %s, died on me.' % pid),
                                zLOG.WARNING)
@@ -112,3 +126,22 @@
             sys.exit()
         except KidDiedOnMeError:
             pass
+
+_signals = None
+
+def get_signal_name(n):
+    """Return the symbolic name for signal n.
+
+    Returns 'unknown' if there is no SIG name bound to n in the signal
+    module.
+    """
+    global _signals
+    if _signals is None:
+        _signals = {}
+        for k, v in signal.__dict__.items():
+            startswith = getattr(k, 'startswith', None)
+            if startswith is None:
+                continue
+            if startswith('SIG'):
+                _signals[v] = k
+    return _signals.get(n, 'unknown')