selftest.py: Add cleanup_pid.
authorJelmer Vernooij <jelmer@samba.org>
Fri, 23 Mar 2012 23:40:49 +0000 (00:40 +0100)
committerJelmer Vernooij <jelmer@samba.org>
Sat, 24 Mar 2012 03:52:59 +0000 (04:52 +0100)
Autobuild-User: Jelmer Vernooij <jelmer@samba.org>
Autobuild-Date: Sat Mar 24 04:52:59 CET 2012 on sn-devel-104

selftest/target/samba.py
selftest/tests/test_samba.py

index 3d63fe58db6b02ac4dc264a778ae495e14b8cb49..1ea156c0031859b9debb9bcd89963d7b0fe35ecf 100644 (file)
@@ -5,9 +5,7 @@
 
 import os
 import sys
-import warnings
 
-from selftest.target import Target
 
 def bindir_path(binary_mapping, bindir, path):
     """Find the executable to use.
@@ -94,3 +92,30 @@ def write_krb5_conf(f, realm, dnsname, domain, kdc_ipv4, tlsdir=None,
        pkinit_anchors = FILE:%(tlsdir)s/ca.pem
 
     """ % {"tlsdir": tlsdir})
+
+
+def cleanup_child(pid, name, outf=None):
+    """Cleanup a child process.
+
+    :param pid: Parent pid process to be passed to waitpid()
+    :param name: Name to use when referring to process
+    :param outf: File-like object to write to (defaults to stderr)
+    :return: Child pid
+    """
+    if outf is None:
+        outf = sys.stderr
+    (childpid, status) = os.waitpid(pid, os.WNOHANG)
+    if childpid == 0:
+        pass
+    elif childpid < 0:
+        outf.write("%s child process %d isn't here any more.\n" % (name, pid))
+        return childpid
+    elif status & 127:
+        if status & 128:
+            core_status = 'with'
+        else:
+            core_status = 'without'
+        outf.write("%s child process %d, died with signal %d, %s coredump.\n" % (name, childpid, (status & 127), core_status))
+    else:
+        outf.write("%s child process %d exited with value %d.\n" % (name, childpid, status >> 8))
+    return childpid
index 6fe1efefaff4acd1e6c61a3ea6dc79f301c2a4ec..f06d846debf6f9b30120bd9375bcefb24cebbdd6 100644 (file)
 
 """Tests for selftest.target.samba."""
 
+import os
+import sys
+
 from cStringIO import StringIO
 
 from selftest.tests import TestCase
 
 from selftest.target.samba import (
     bindir_path,
+    cleanup_child,
     mk_realms_stanza,
     write_krb5_conf,
     )