From 53c97eaef9d6f6cbc975134cccaaf383d289f6a0 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Wed, 14 Feb 2007 12:16:34 +0000 Subject: [PATCH] close spurious fds in child after forking; debug on entry to cleanup --- virt-subproc/VirtSubproc.py | 12 ++++++++---- virt-subproc/adt-virt-chroot | 3 +++ virt-subproc/adt-virt-xenlvm | 5 +++++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/virt-subproc/VirtSubproc.py b/virt-subproc/VirtSubproc.py index bc9af88..192e729 100644 --- a/virt-subproc/VirtSubproc.py +++ b/virt-subproc/VirtSubproc.py @@ -64,9 +64,12 @@ def cmd_close(c, ce): if not downtmp: bomb("`close' when not open") cleanup() +def preexecfn(): + caller.hook_forked_inchild() + def execute_raw(what, instr, *popenargs, **popenargsk): debug(" ++ %s" % string.join(popenargs[0])) - sp = subprocess.Popen(*popenargs, **popenargsk) + sp = subprocess.Popen(preexec_fn=preexecfn, *popenargs, **popenargsk) if instr is None: popenargsk['stdin'] = devnull_read (out, err) = sp.communicate(instr) if err: bomb("%s unexpectedly produced stderr output `%s'" % @@ -186,7 +189,6 @@ def copyupdown(c, ce, upp): localfd = None deststdout = devnull_read srcstdin = devnull_read - preexecfns = [None, None] if not dirsp: modestr = '' if upp: @@ -231,11 +233,12 @@ def copyupdown(c, ce, upp): subprocs = [None,None] debug(" +< %s" % string.join(cmdls[0])) subprocs[0] = subprocess.Popen(cmdls[0], stdin=srcstdin, - stdout=subprocess.PIPE, preexec_fn=preexecfns[0]) + stdout=subprocess.PIPE, preexec_fn=preexecfn) debug(" +> %s" % string.join(cmdls[1])) subprocs[1] = subprocess.Popen(cmdls[1], stdin=subprocs[0].stdout, - stdout=deststdout, preexec_fn=preexecfns[1]) + stdout=deststdout, preexec_fn=preexecfn) for sdn in [1,0]: + debug(" +"+"<>"[sdn]+"?"); status = subprocs[sdn].wait() if status: bomb("%s %s failed, status %d" % (wh, ['source','destination'][sdn], status)) @@ -260,6 +263,7 @@ def command(): def cleanup(): global downtmp, cleaning + debug("cleanup..."); cleaning = True if downtmp: caller.hook_cleanup() cleaning = False diff --git a/virt-subproc/adt-virt-chroot b/virt-subproc/adt-virt-chroot index 4ec079c..5fdfe26 100755 --- a/virt-subproc/adt-virt-chroot +++ b/virt-subproc/adt-virt-chroot @@ -75,6 +75,9 @@ def hook_open(): def hook_cleanup(): vsp.execute('rm -rf --', [downtmp], downp=True) +def hook_forked_inchild(): + pass + def hook_capabilities(): return capabilities diff --git a/virt-subproc/adt-virt-xenlvm b/virt-subproc/adt-virt-xenlvm index dedf7fe..a4a1ec5 100755 --- a/virt-subproc/adt-virt-xenlvm +++ b/virt-subproc/adt-virt-xenlvm @@ -105,6 +105,11 @@ def do_close(): withholder = None if rc: bomb("with-testbed failed when closing/resetting, code %d" % rc) +def hook_forked_inchild(): + if withholder is not None: + withholder.stdin.close() + withholder.stdout.close() + def hook_open(): hook_cleanup() do_open() -- 2.30.2