| 1 |
""" |
| 2 |
Error pipe and serialization code comes from Python 2.5 subprocess module. |
| 3 |
""" |
| 4 |
from os import ( |
| 5 |
fork, execvp, execvpe, waitpid, |
| 6 |
close, dup2, pipe, |
| 7 |
read, write, devnull, sysconf, set_inheritable) |
| 8 |
from sys import exc_info |
| 9 |
from traceback import format_exception |
| 10 |
from ptrace.binding import ptrace_traceme |
| 11 |
from ptrace import PtraceError |
| 12 |
from sys import exit |
| 13 |
from errno import EINTR |
| 14 |
import fcntl |
| 15 |
import pickle |
| 16 |
|
| 17 |
try: |
| 18 |
MAXFD = sysconf("SC_OPEN_MAX") |
| 19 |
except Exception: |
| 20 |
MAXFD = 256 |
| 21 |
|
| 22 |
|
| 23 |
class ChildError(RuntimeError): |
| 24 |
pass |
| 25 |
|
| 26 |
|
| 27 |
class ChildPtraceError(ChildError): |
| 28 |
pass |
| 29 |
|
| 30 |
|
| 31 |
def _set_cloexec_flag(fd): |
| 32 |
try: |
| 33 |
cloexec_flag = fcntl.FD_CLOEXEC |
| 34 |
except AttributeError: |
| 35 |
cloexec_flag = 1 |
| 36 |
|
| 37 |
old = fcntl.fcntl(fd, fcntl.F_GETFD) |
| 38 |
fcntl.fcntl(fd, fcntl.F_SETFD, old | cloexec_flag) |
| 39 |
|
| 40 |
|
| 41 |
def _waitpid_no_intr(pid, options): |
| 42 |
"""Like os.waitpid, but retries on EINTR""" |
| 43 |
while True: |
| 44 |
try: |
| 45 |
return waitpid(pid, options) |
| 46 |
except OSError as e: |
| 47 |
if e.errno == EINTR: |
| 48 |
continue |
| 49 |
else: |
| 50 |
raise |
| 51 |
|
| 52 |
|
| 53 |
def _read_no_intr(fd, buffersize): |
| 54 |
"""Like os.read, but retries on EINTR""" |
| 55 |
while True: |
| 56 |
try: |
| 57 |
return read(fd, buffersize) |
| 58 |
except OSError as e: |
| 59 |
if e.errno == EINTR: |
| 60 |
continue |
| 61 |
else: |
| 62 |
raise |
| 63 |
|
| 64 |
|
| 65 |
def _write_no_intr(fd, s): |
| 66 |
"""Like os.write, but retries on EINTR""" |
| 67 |
while True: |
| 68 |
try: |
| 69 |
return write(fd, s) |
| 70 |
except OSError as e: |
| 71 |
if e.errno == EINTR: |
| 72 |
continue |
| 73 |
else: |
| 74 |
raise |
| 75 |
|
| 76 |
|
| 77 |
def _createParent(pid, errpipe_read): |
| 78 |
# Wait for exec to fail or succeed; possibly raising exception |
| 79 |
data = _read_no_intr(errpipe_read, 1048576) # Exceptions limited to 1 MB |
| 80 |
close(errpipe_read) |
| 81 |
if data: |
| 82 |
_waitpid_no_intr(pid, 0) |
| 83 |
child_exception = pickle.loads(data) |
| 84 |
raise child_exception |
| 85 |
|
| 86 |
|
| 87 |
def _createChild(arguments, |
| 88 |
no_stdout, |
| 89 |
env, |
| 90 |
errpipe_write, |
| 91 |
close_fds=True, |
| 92 |
pass_fds=()): |
| 93 |
# Child code |
| 94 |
try: |
| 95 |
ptrace_traceme() |
| 96 |
except PtraceError as err: |
| 97 |
raise ChildError(str(err)) |
| 98 |
|
| 99 |
for fd in pass_fds: |
| 100 |
set_inheritable(fd, True) |
| 101 |
|
| 102 |
if close_fds: |
| 103 |
# Close all files except 0, 1, 2 and errpipe_write |
| 104 |
for fd in range(3, MAXFD): |
| 105 |
if fd == errpipe_write or (fd in pass_fds): |
| 106 |
continue |
| 107 |
try: |
| 108 |
close(fd) |
| 109 |
except OSError: |
| 110 |
pass |
| 111 |
|
| 112 |
try: |
| 113 |
_execChild(arguments, no_stdout, env) |
| 114 |
except: # noqa: E722 |
| 115 |
exc_type, exc_value, tb = exc_info() |
| 116 |
# Save the traceback and attach it to the exception object |
| 117 |
exc_lines = format_exception(exc_type, exc_value, tb) |
| 118 |
exc_value.child_traceback = ''.join(exc_lines) |
| 119 |
_write_no_intr(errpipe_write, pickle.dumps(exc_value)) |
| 120 |
exit(255) |
| 121 |
|
| 122 |
|
| 123 |
def _execChild(arguments, no_stdout, env): |
| 124 |
if no_stdout: |
| 125 |
try: |
| 126 |
null = open(devnull, 'wb') |
| 127 |
dup2(null.fileno(), 1) |
| 128 |
dup2(1, 2) |
| 129 |
null.close() |
| 130 |
except IOError: |
| 131 |
close(2) |
| 132 |
close(1) |
| 133 |
try: |
| 134 |
if env is not None: |
| 135 |
execvpe(arguments[0], arguments, env) |
| 136 |
else: |
| 137 |
execvp(arguments[0], arguments) |
| 138 |
except Exception as err: |
| 139 |
raise ChildError(str(err)) |
| 140 |
|
| 141 |
|
| 142 |
def createChild(arguments, no_stdout, env=None, close_fds=True, pass_fds=()): |
| 143 |
""" |
| 144 |
Create a child process: |
| 145 |
- arguments: list of string where (e.g. ['ls', '-la']) |
| 146 |
- no_stdout: if True, use null device for stdout/stderr |
| 147 |
- env: environment variables dictionary |
| 148 |
|
| 149 |
Use: |
| 150 |
- env={} to start with an empty environment |
| 151 |
- env=None (default) to copy the environment |
| 152 |
""" |
| 153 |
|
| 154 |
if pass_fds and not close_fds: |
| 155 |
close_fds = True |
| 156 |
|
| 157 |
errpipe_read, errpipe_write = pipe() |
| 158 |
_set_cloexec_flag(errpipe_write) |
| 159 |
|
| 160 |
# Fork process |
| 161 |
pid = fork() |
| 162 |
if pid: |
| 163 |
close(errpipe_write) |
| 164 |
_createParent(pid, errpipe_read) |
| 165 |
return pid |
| 166 |
else: |
| 167 |
close(errpipe_read) |
| 168 |
_createChild(arguments, |
| 169 |
no_stdout, |
| 170 |
env, |
| 171 |
errpipe_write, |
| 172 |
close_fds=close_fds, |
| 173 |
pass_fds=pass_fds) |