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,16 +27,12 @@ import struct from eventlet.green import subprocess +from quantum.common import utils + LOG = logging.getLogger(__name__) -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 execute(cmd, root_helper=None, process_input=None, addl_env=None, check_exit_code=True, return_stderr=False): if root_helper: @@ -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]