From 9cb3652f979a7d7a7614aa55286d0099834d604f Mon Sep 17 00:00:00 2001 From: Gary Kotton Date: Sun, 23 Sep 2012 08:34:33 +0000 Subject: [PATCH] Uses a common subprocess popen function Fixes bug 1053381 Change-Id: I8a89ba8d4f03094fcc581981044582c95d1300bb --- bin/quantum-rootwrap | 20 +++++++------------- quantum/agent/linux/utils.py | 17 +++++++---------- quantum/common/utils.py | 17 +++++++++++++++-- quantum/tests/unit/test_rootwrap.py | 3 ++- 4 files changed, 31 insertions(+), 26 deletions(-) diff --git a/bin/quantum-rootwrap b/bin/quantum-rootwrap index 96939ae2f..6b9973309 100755 --- a/bin/quantum-rootwrap +++ b/bin/quantum-rootwrap @@ -39,21 +39,16 @@ import ConfigParser import os import signal -import subprocess import sys +from quantum.common import utils + RC_UNAUTHORIZED = 99 RC_NOCOMMAND = 98 RC_BADCONFIG = 97 -def _subprocess_setup(): - # Python installs a SIGPIPE handler by default. This is usually not what - # non-Python subprocesses expect. - signal.signal(signal.SIGPIPE, signal.SIG_DFL) - - if __name__ == '__main__': # Split arguments, require at least a command execname = sys.argv.pop(0) @@ -87,12 +82,11 @@ if __name__ == '__main__': filters = wrapper.load_filters(filters_path) filtermatch = wrapper.match_filter(filters, userargs) if filtermatch: - obj = subprocess.Popen(filtermatch.get_command(userargs), - stdin=sys.stdin, - stdout=sys.stdout, - stderr=sys.stderr, - preexec_fn=_subprocess_setup, - env=filtermatch.get_environment(userargs)) + obj = utils.subprocess_popen(filtermatch.get_command(userargs), + stdin=sys.stdin, + stdout=sys.stdout, + stderr=sys.stderr, + env=filtermatch.get_environment(userargs)) obj.wait() sys.exit(obj.returncode) diff --git a/quantum/agent/linux/utils.py b/quantum/agent/linux/utils.py index e8f3b7167..0d11aa0c0 100644 --- a/quantum/agent/linux/utils.py +++ b/quantum/agent/linux/utils.py @@ -27,14 +27,10 @@ import struct from eventlet.green import subprocess - -LOG = logging.getLogger(__name__) +from quantum.common import utils -def _subprocess_setup(): - # Python installs a SIGPIPE handler by default. This is usually not what - # non-Python subprocesses expect. - signal.signal(signal.SIGPIPE, signal.SIG_DFL) +LOG = logging.getLogger(__name__) def execute(cmd, root_helper=None, process_input=None, addl_env=None, @@ -47,10 +43,11 @@ def execute(cmd, root_helper=None, process_input=None, addl_env=None, env = os.environ.copy() if addl_env: env.update(addl_env) - obj = subprocess.Popen(cmd, shell=False, stdin=subprocess.PIPE, - stdout=subprocess.PIPE, stderr=subprocess.PIPE, - preexec_fn=_subprocess_setup, - env=env) + obj = utils.subprocess_popen(cmd, shell=False, + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + env=env) _stdout, _stderr = (process_input and obj.communicate(process_input) or diff --git a/quantum/common/utils.py b/quantum/common/utils.py index 313ee2af1..f57fafca8 100644 --- a/quantum/common/utils.py +++ b/quantum/common/utils.py @@ -24,11 +24,11 @@ import logging import os import signal -import subprocess import uuid +from eventlet.green import subprocess + from quantum.openstack.common import cfg -from quantum.openstack.common.exception import ProcessExecutionError TIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ" @@ -120,3 +120,16 @@ def find_config_file(options, config_file): def str_uuid(): """Return a uuid as a string""" return str(uuid.uuid4()) + + +def _subprocess_setup(): + # Python installs a SIGPIPE handler by default. This is usually not what + # non-Python subprocesses expect. + signal.signal(signal.SIGPIPE, signal.SIG_DFL) + + +def subprocess_popen(args, stdin=None, stdout=None, stderr=None, shell=False, + env=None): + return subprocess.Popen(args, shell=shell, stdin=stdin, stdout=stdout, + stderr=stderr, preexec_fn=_subprocess_setup, + env=env) diff --git a/quantum/tests/unit/test_rootwrap.py b/quantum/tests/unit/test_rootwrap.py index 42ab473f7..979ff20ca 100644 --- a/quantum/tests/unit/test_rootwrap.py +++ b/quantum/tests/unit/test_rootwrap.py @@ -19,6 +19,7 @@ import subprocess import unittest2 as unittest +from quantum.common import utils from quantum.rootwrap import filters from quantum.rootwrap import wrapper @@ -65,7 +66,7 @@ class RootwrapTestCase(unittest.TestCase): self.assertEqual(env.get('QUANTUM_NETWORK_ID'), 'foobar') def test_KillFilter(self): - p = subprocess.Popen(["/bin/sleep", "5"]) + p = utils.subprocess_popen(["/bin/sleep", "5"]) f = filters.KillFilter("root", "/bin/sleep", "-9", "-HUP") f2 = filters.KillFilter("root", "/usr/bin/sleep", "-9", "-HUP") usercmd = ['kill', '-ALRM', p.pid] -- 2.45.2