Uses a common subprocess popen function

Fixes bug 1053381

Change-Id: I8a89ba8d4f03094fcc581981044582c95d1300bb
This commit is contained in:
Gary Kotton 2012-09-23 08:34:33 +00:00
parent 20aab79675
commit 9cb3652f97
4 changed files with 31 additions and 26 deletions

View File

@ -39,21 +39,16 @@
import ConfigParser import ConfigParser
import os import os
import signal import signal
import subprocess
import sys import sys
from quantum.common import utils
RC_UNAUTHORIZED = 99 RC_UNAUTHORIZED = 99
RC_NOCOMMAND = 98 RC_NOCOMMAND = 98
RC_BADCONFIG = 97 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__': if __name__ == '__main__':
# Split arguments, require at least a command # Split arguments, require at least a command
execname = sys.argv.pop(0) execname = sys.argv.pop(0)
@ -87,12 +82,11 @@ if __name__ == '__main__':
filters = wrapper.load_filters(filters_path) filters = wrapper.load_filters(filters_path)
filtermatch = wrapper.match_filter(filters, userargs) filtermatch = wrapper.match_filter(filters, userargs)
if filtermatch: if filtermatch:
obj = subprocess.Popen(filtermatch.get_command(userargs), obj = utils.subprocess_popen(filtermatch.get_command(userargs),
stdin=sys.stdin, stdin=sys.stdin,
stdout=sys.stdout, stdout=sys.stdout,
stderr=sys.stderr, stderr=sys.stderr,
preexec_fn=_subprocess_setup, env=filtermatch.get_environment(userargs))
env=filtermatch.get_environment(userargs))
obj.wait() obj.wait()
sys.exit(obj.returncode) sys.exit(obj.returncode)

View File

@ -27,16 +27,12 @@ import struct
from eventlet.green import subprocess from eventlet.green import subprocess
from quantum.common import utils
LOG = logging.getLogger(__name__) 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, def execute(cmd, root_helper=None, process_input=None, addl_env=None,
check_exit_code=True, return_stderr=False): check_exit_code=True, return_stderr=False):
if root_helper: if root_helper:
@ -47,10 +43,11 @@ def execute(cmd, root_helper=None, process_input=None, addl_env=None,
env = os.environ.copy() env = os.environ.copy()
if addl_env: if addl_env:
env.update(addl_env) env.update(addl_env)
obj = subprocess.Popen(cmd, shell=False, stdin=subprocess.PIPE, obj = utils.subprocess_popen(cmd, shell=False,
stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE,
preexec_fn=_subprocess_setup, stdout=subprocess.PIPE,
env=env) stderr=subprocess.PIPE,
env=env)
_stdout, _stderr = (process_input and _stdout, _stderr = (process_input and
obj.communicate(process_input) or obj.communicate(process_input) or

View File

@ -24,11 +24,11 @@
import logging import logging
import os import os
import signal import signal
import subprocess
import uuid import uuid
from eventlet.green import subprocess
from quantum.openstack.common import cfg from quantum.openstack.common import cfg
from quantum.openstack.common.exception import ProcessExecutionError
TIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ" TIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ"
@ -120,3 +120,16 @@ def find_config_file(options, config_file):
def str_uuid(): def str_uuid():
"""Return a uuid as a string""" """Return a uuid as a string"""
return str(uuid.uuid4()) 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)

View File

@ -19,6 +19,7 @@ import subprocess
import unittest2 as unittest import unittest2 as unittest
from quantum.common import utils
from quantum.rootwrap import filters from quantum.rootwrap import filters
from quantum.rootwrap import wrapper from quantum.rootwrap import wrapper
@ -65,7 +66,7 @@ class RootwrapTestCase(unittest.TestCase):
self.assertEqual(env.get('QUANTUM_NETWORK_ID'), 'foobar') self.assertEqual(env.get('QUANTUM_NETWORK_ID'), 'foobar')
def test_KillFilter(self): 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") f = filters.KillFilter("root", "/bin/sleep", "-9", "-HUP")
f2 = filters.KillFilter("root", "/usr/bin/sleep", "-9", "-HUP") f2 = filters.KillFilter("root", "/usr/bin/sleep", "-9", "-HUP")
usercmd = ['kill', '-ALRM', p.pid] usercmd = ['kill', '-ALRM', p.pid]