| 1 |
|
| 2 |
import pytest |
| 3 |
|
| 4 |
_posixsubprocess = pytest.importorskip('_posixsubprocess') |
| 5 |
|
| 6 |
from os.path import dirname |
| 7 |
import traceback |
| 8 |
import subprocess |
| 9 |
import os |
| 10 |
import posix |
| 11 |
import errno |
| 12 |
try: |
| 13 |
import pwd |
| 14 |
import grp |
| 15 |
except ImportError: |
| 16 |
pwd = grp = None |
| 17 |
|
| 18 |
directory = dirname(__file__) |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
def test_close_fds_true(): |
| 23 |
fds = os.pipe() |
| 24 |
|
| 25 |
open_fds = set(fds) |
| 26 |
|
| 27 |
for _ in range(9): |
| 28 |
fd = os.open("/dev/null", os.O_RDONLY) |
| 29 |
open_fds.add(fd) |
| 30 |
|
| 31 |
p = subprocess.Popen(['/usr/bin/env', 'python', os.path.join(directory, 'fd_status.py')], stdout=subprocess.PIPE, close_fds=True) |
| 32 |
output, ignored = p.communicate() |
| 33 |
remaining_fds = set(map(int, output.split(b','))) |
| 34 |
|
| 35 |
assert not (remaining_fds & open_fds), "Some fds were left open" |
| 36 |
assert 1 in remaining_fds, "Subprocess failed" |
| 37 |
|
| 38 |
def test_start_new_session(): |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
try: |
| 43 |
output = subprocess.check_output( |
| 44 |
['/usr/bin/env', 'python', "-c", |
| 45 |
"import os; print(os.getpgid(os.getpid()))"], |
| 46 |
start_new_session=True) |
| 47 |
except OSError as e: |
| 48 |
if e.errno != errno.EPERM: |
| 49 |
raise |
| 50 |
else: |
| 51 |
parent_pgid = os.getpgid(os.getpid()) |
| 52 |
child_pgid = int(output) |
| 53 |
assert parent_pgid != child_pgid |
| 54 |
|
| 55 |
def test_seq_bytes_to_charp_array(): |
| 56 |
n = 0 |
| 57 |
class Z(object): |
| 58 |
def __len__(self): |
| 59 |
import sys |
| 60 |
return sys.maxsize + n |
| 61 |
def __getitem__(self, i): |
| 62 |
return b'x' |
| 63 |
pytest.raises(MemoryError, _posixsubprocess.fork_exec, |
| 64 |
1,Z(),3,(1, 2),5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23) |
| 65 |
n = 1 |
| 66 |
pytest.raises(OverflowError, _posixsubprocess.fork_exec, |
| 67 |
1,Z(),3,(1, 2),5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23) |
| 68 |
|
| 69 |
def test_pass_fds_make_inheritable(): |
| 70 |
fd1, fd2 = posix.pipe() |
| 71 |
assert posix.get_inheritable(fd1) is False |
| 72 |
assert posix.get_inheritable(fd2) is False |
| 73 |
|
| 74 |
subprocess.check_call(['/usr/bin/env', 'python', '-c', |
| 75 |
'import os;os.write(%d,b"K")' % fd2], |
| 76 |
close_fds=True, pass_fds=[fd2]) |
| 77 |
res = posix.read(fd1, 1) |
| 78 |
assert res == b"K" |
| 79 |
posix.close(fd1) |
| 80 |
posix.close(fd2) |
| 81 |
|
| 82 |
def test_user(): |
| 83 |
uid = os.geteuid() |
| 84 |
test_users = [65534 if uid != 65534 else 65533, uid] |
| 85 |
|
| 86 |
for user in test_users: |
| 87 |
try: |
| 88 |
output = subprocess.check_output( |
| 89 |
["python", "-c", |
| 90 |
"import os; print(os.getuid())"], |
| 91 |
user=user) |
| 92 |
except OSError as e: |
| 93 |
if e.errno != errno.EPERM: |
| 94 |
raise |
| 95 |
else: |
| 96 |
if isinstance(user, str): |
| 97 |
user_uid = pwd.getpwnam(user).pw_uid |
| 98 |
else: |
| 99 |
user_uid = user |
| 100 |
child_user = int(output) |
| 101 |
assert child_user == user_uid |
| 102 |
|
| 103 |
with pytest.raises(ValueError): |
| 104 |
subprocess.check_call(["python", "-c", "pass"], user=-1) |
| 105 |
|
| 106 |
def test_extra_groups(): |
| 107 |
gid = os.getegid() |
| 108 |
group_list = [65534 if gid != 65534 else 65533] |
| 109 |
perm_error = False |
| 110 |
|
| 111 |
try: |
| 112 |
output = subprocess.check_output( |
| 113 |
["python", "-c", |
| 114 |
"import os, sys, json; json.dump(os.getgroups(), sys.stdout)"], |
| 115 |
extra_groups=group_list) |
| 116 |
except OSError as ex: |
| 117 |
if ex.errno != errno.EPERM: |
| 118 |
raise |
| 119 |
perm_error = True |
| 120 |
|
| 121 |
else: |
| 122 |
parent_groups = os.getgroups() |
| 123 |
child_groups = json.loads(output) |
| 124 |
|
| 125 |
if grp is not None: |
| 126 |
desired_gids = [grp.getgrnam(g).gr_gid if isinstance(g, str) else g |
| 127 |
for g in group_list] |
| 128 |
else: |
| 129 |
desired_gids = group_list |
| 130 |
|
| 131 |
if perm_error: |
| 132 |
assert set(child_groups) == set(parent_groups) |
| 133 |
else: |
| 134 |
assert set(desired_gids) == set(parent_groups) |
| 135 |
|
| 136 |
def test_umask(): |
| 137 |
import tempfile, shutil |
| 138 |
tmpdir = None |
| 139 |
try: |
| 140 |
tmpdir = tempfile.mkdtemp() |
| 141 |
name = os.path.join(tmpdir, "beans") |
| 142 |
|
| 143 |
|
| 144 |
subprocess.check_call( |
| 145 |
["python", "-c", f"open({name!r}, 'w').close()"], |
| 146 |
umask=0o053) |
| 147 |
|
| 148 |
|
| 149 |
st_mode = os.stat(name).st_mode & 0o666 |
| 150 |
expected_mode = 0o624 |
| 151 |
assert expected_mode == st_mode |
| 152 |
finally: |
| 153 |
if tmpdir is not None: |
| 154 |
shutil.rmtree(tmpdir) |
| 155 |
|
| 156 |
def test_issue_3630(): |
| 157 |
import time |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
tmpfile = 'fork_exec.txt' |
| 162 |
with open(tmpfile, 'w'): |
| 163 |
pass |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
def spawnv_passfds(path, args, passfds, preexec_fn=None): |
| 168 |
passfds = tuple(sorted(map(int, fds_to_pass))) |
| 169 |
errpipe_read, errpipe_write = os.pipe() |
| 170 |
try: |
| 171 |
return _posixsubprocess.fork_exec( |
| 172 |
args, [os.fsencode(path)], True, passfds, None, None, |
| 173 |
-1, -1, -1, -1, -1, -1, errpipe_read, errpipe_write, |
| 174 |
False, False, |
| 175 |
None, None, None, None, -1, |
| 176 |
preexec_fn, subprocess._USE_VFORK) |
| 177 |
finally: |
| 178 |
os.close(errpipe_read) |
| 179 |
os.close(errpipe_write) |
| 180 |
|
| 181 |
def preexec_fn(): |
| 182 |
pass |
| 183 |
|
| 184 |
def before(): |
| 185 |
with open(tmpfile, mode='a') as fid: |
| 186 |
fid.write('before\n') |
| 187 |
print('before hook done') |
| 188 |
|
| 189 |
def parent(): |
| 190 |
print('parent hook') |
| 191 |
for i in range(10): |
| 192 |
time.sleep(0.1) |
| 193 |
with open(tmpfile, mode='r') as fid: |
| 194 |
if 'child' in fid.read(): |
| 195 |
break |
| 196 |
else: |
| 197 |
print('tmpfile not updated') |
| 198 |
with open(tmpfile, mode='a') as fid: |
| 199 |
fid.write('parent\n') |
| 200 |
print('parent hook done') |
| 201 |
|
| 202 |
def child(): |
| 203 |
with open(tmpfile, mode='a') as fid: |
| 204 |
fid.write('child\n') |
| 205 |
print('child hook done') |
| 206 |
|
| 207 |
os.register_at_fork(before=before, after_in_parent=parent, after_in_child=child) |
| 208 |
fds_to_pass = [] |
| 209 |
try: |
| 210 |
fds_to_pass.append(sys.stderr.fileno()) |
| 211 |
except Exception: |
| 212 |
pass |
| 213 |
|
| 214 |
r, w = os.pipe() |
| 215 |
fds_to_pass.append(r) |
| 216 |
args = ['ls', '.'] |
| 217 |
|
| 218 |
try: |
| 219 |
spawnv_passfds('ls', args, fds_to_pass) |
| 220 |
with open(tmpfile, mode='r') as fid: |
| 221 |
contents = fid.read() |
| 222 |
assert len(contents) == 0 |
| 223 |
|
| 224 |
spawnv_passfds('ls', args, fds_to_pass, preexec_fn=preexec_fn) |
| 225 |
with open(tmpfile, mode='r') as fid: |
| 226 |
contents = fid.read() |
| 227 |
assert 'child' in contents |
| 228 |
assert 'parent' in contents |
| 229 |
assert 'before' in contents |
| 230 |
finally: |
| 231 |
os.remove(tmpfile) |
| 232 |
|
| 233 |
def test_restore_signals(): |
| 234 |
import posix as os |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
def check_output(restore_signals): |
| 239 |
c2pread, c2pwrite = os.pipe() |
| 240 |
errpipe_read, errpipe_write = os.pipe() |
| 241 |
try: |
| 242 |
low_fds_to_close = [] |
| 243 |
while errpipe_write < 3: |
| 244 |
low_fds_to_close.append(errpipe_write) |
| 245 |
errpipe_write = os.dup(errpipe_write) |
| 246 |
for low_fd in low_fds_to_close: |
| 247 |
os.close(low_fd) |
| 248 |
args = [b'cat', b'/proc/self/status'] |
| 249 |
executable_list=[b'/usr/bin/cat'] |
| 250 |
close_fds = True |
| 251 |
fds_to_keep = (errpipe_write,) |
| 252 |
cwd = None |
| 253 |
env_list = None |
| 254 |
p2cread = p2cwrite = -1 |
| 255 |
errread = errwrite = umask = -1 |
| 256 |
call_setsid = False |
| 257 |
pgid_to_set = -1 |
| 258 |
preexec_fn = None |
| 259 |
gid = gids = uid = None |
| 260 |
pid = _posixsubprocess.fork_exec(args, executable_list, close_fds, |
| 261 |
fds_to_keep, cwd, env_list, p2cread, p2cwrite, c2pread, |
| 262 |
c2pwrite, errread, errwrite, errpipe_read, |
| 263 |
errpipe_write, restore_signals, call_setsid, pgid_to_set, |
| 264 |
gid, gids, uid, umask, |
| 265 |
preexec_fn, subprocess._USE_VFORK) |
| 266 |
os.close(errpipe_write) |
| 267 |
|
| 268 |
|
| 269 |
errpipe_data = bytearray() |
| 270 |
while True: |
| 271 |
part = os.read(errpipe_read, 50000) |
| 272 |
errpipe_data += part |
| 273 |
if not part or len(errpipe_data) > 50000: |
| 274 |
break |
| 275 |
if errpipe_data: |
| 276 |
newpid, sts = os.waitpid(pid, 0) |
| 277 |
raise RuntimeError('running commande returned %s' % sts) |
| 278 |
out = os.read(c2pread, 50000) |
| 279 |
finally: |
| 280 |
os.close(c2pwrite) |
| 281 |
os.close(c2pread) |
| 282 |
os.close(errpipe_read) |
| 283 |
return out |
| 284 |
|
| 285 |
sig_ign_mask1 = '' |
| 286 |
sig_ign_mask2 = '' |
| 287 |
for line in check_output(True).splitlines(): |
| 288 |
if line.startswith(b'SigIgn'): |
| 289 |
sig_ign_mask1 = line |
| 290 |
for line in check_output(False).splitlines(): |
| 291 |
if line.startswith(b'SigIgn'): |
| 292 |
sig_ign_mask2 = line |
| 293 |
assert b'1' not in sig_ign_mask1 |
| 294 |
assert sig_ign_mask2 |
| 295 |
assert sig_ign_mask1 != sig_ign_mask2 |