forked from Azure/azure-linux-extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subprocessfactory.py
51 lines (41 loc) · 1.78 KB
/
subprocessfactory.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python2
#
# Copyright (C) Microsoft Corporation, All rights reserved.
"""Process factory which returns a process enforcing the preexec_fn."""
try:
import ctypes
# See : http://man7.org/linux/man-pages/man2/prctl.2.html
# See : http://lxr.free-electrons.com/source/include/uapi/linux/prctl.h
libc = ctypes.CDLL("libc.so.6")
PR_SET_PDEATHSIG = 1
def set_process_death_signal(death_signal):
libc.prctl(PR_SET_PDEATHSIG, death_signal)
except ImportError:
# TODO(dalbe): Trace
pass
except:
# For test to run on windows
# TODO(dalbe): Trace
pass
import os
import signal
import subprocess
import sys
CTYPES_MODULE_NAME = "ctypes"
def create_subprocess(cmd, env=None, stdout=None, stderr=None, cwd=None):
"""Creates a process forcing and sets the SIGTERM signal handler using Ctypes (when available). Else creates a
process based on the pipe_output argument.
Ctypes are only available in 2.5+ so processes create in python 2.4 won't die if their parent process dies.
Args:
cmd : string , the cmd to execute.
env : dictonary(string) , the process level environment variable.
stdout : boolean , sets the stdout to subprocess.PIPE when True, else stdout is left untouched.
stderr : boolean , sets the stderr to subprocess.PIPE when True, else stdout is left untouched.
Returns:
The process object.
"""
if CTYPES_MODULE_NAME not in sys.modules or os.name.lower() == "nt":
return subprocess.Popen(cmd, env=env, stdout=stdout, stderr=stderr, cwd=cwd)
else:
return subprocess.Popen(cmd, env=env, stdout=stdout, stderr=stderr, cwd=cwd,
preexec_fn=set_process_death_signal(signal.SIGTERM))