Lines 21-26
portage.proxy.lazyimport.lazyimport(globals(),
Link Here
|
21 |
|
21 |
|
22 |
from portage.const import BASH_BINARY, SANDBOX_BINARY, FAKEROOT_BINARY |
22 |
from portage.const import BASH_BINARY, SANDBOX_BINARY, FAKEROOT_BINARY |
23 |
from portage.exception import CommandNotFound |
23 |
from portage.exception import CommandNotFound |
|
|
24 |
from portage.util._ctypes import find_library, LoadLibrary, ctypes |
24 |
|
25 |
|
25 |
try: |
26 |
try: |
26 |
import resource |
27 |
import resource |
Lines 180-186
def cleanup():
Link Here
|
180 |
|
181 |
|
181 |
def spawn(mycommand, env={}, opt_name=None, fd_pipes=None, returnpid=False, |
182 |
def spawn(mycommand, env={}, opt_name=None, fd_pipes=None, returnpid=False, |
182 |
uid=None, gid=None, groups=None, umask=None, logfile=None, |
183 |
uid=None, gid=None, groups=None, umask=None, logfile=None, |
183 |
path_lookup=True, pre_exec=None, close_fds=True): |
184 |
path_lookup=True, pre_exec=None, close_fds=True, unshare_net=False): |
184 |
""" |
185 |
""" |
185 |
Spawns a given command. |
186 |
Spawns a given command. |
186 |
|
187 |
|
Lines 213-219
def spawn(mycommand, env={}, opt_name=None, fd_pipes=None, returnpid=False,
Link Here
|
213 |
@param close_fds: If True, then close all file descriptors except those |
214 |
@param close_fds: If True, then close all file descriptors except those |
214 |
referenced by fd_pipes (default is True). |
215 |
referenced by fd_pipes (default is True). |
215 |
@type close_fds: Boolean |
216 |
@type close_fds: Boolean |
216 |
|
217 |
@param unshare_net: If True, networking will be unshared from the spawned process |
|
|
218 |
@type unshare_net: Boolean |
219 |
|
217 |
logfile requires stdout and stderr to be assigned to this process (ie not pointed |
220 |
logfile requires stdout and stderr to be assigned to this process (ie not pointed |
218 |
somewhere else.) |
221 |
somewhere else.) |
219 |
|
222 |
|
Lines 284-290
def spawn(mycommand, env={}, opt_name=None, fd_pipes=None, returnpid=False,
Link Here
|
284 |
if pid == 0: |
287 |
if pid == 0: |
285 |
try: |
288 |
try: |
286 |
_exec(binary, mycommand, opt_name, fd_pipes, |
289 |
_exec(binary, mycommand, opt_name, fd_pipes, |
287 |
env, gid, groups, uid, umask, pre_exec, close_fds) |
290 |
env, gid, groups, uid, umask, pre_exec, close_fds, |
|
|
291 |
unshare_net) |
288 |
except SystemExit: |
292 |
except SystemExit: |
289 |
raise |
293 |
raise |
290 |
except Exception as e: |
294 |
except Exception as e: |
Lines 354-360
def spawn(mycommand, env={}, opt_name=None, fd_pipes=None, returnpid=False,
Link Here
|
354 |
return 0 |
358 |
return 0 |
355 |
|
359 |
|
356 |
def _exec(binary, mycommand, opt_name, fd_pipes, env, gid, groups, uid, umask, |
360 |
def _exec(binary, mycommand, opt_name, fd_pipes, env, gid, groups, uid, umask, |
357 |
pre_exec, close_fds): |
361 |
pre_exec, close_fds, unshare_net): |
358 |
|
362 |
|
359 |
""" |
363 |
""" |
360 |
Execute a given binary with options |
364 |
Execute a given binary with options |
Lines 379-388
def _exec(binary, mycommand, opt_name, fd_pipes, env, gid, groups, uid, umask,
Link Here
|
379 |
@type umask: Integer |
383 |
@type umask: Integer |
380 |
@param pre_exec: A function to be called with no arguments just prior to the exec call. |
384 |
@param pre_exec: A function to be called with no arguments just prior to the exec call. |
381 |
@type pre_exec: callable |
385 |
@type pre_exec: callable |
|
|
386 |
@param unshare_net: If True, networking will be unshared from the spawned process |
387 |
@type unshare_net: Boolean |
382 |
@rtype: None |
388 |
@rtype: None |
383 |
@return: Never returns (calls os.execve) |
389 |
@return: Never returns (calls os.execve) |
384 |
""" |
390 |
""" |
385 |
|
391 |
|
386 |
# If the process we're creating hasn't been given a name |
392 |
# If the process we're creating hasn't been given a name |
387 |
# assign it the name of the executable. |
393 |
# assign it the name of the executable. |
388 |
if not opt_name: |
394 |
if not opt_name: |
Lines 415-420
def _exec(binary, mycommand, opt_name, fd_pipes, env, gid, groups, uid, umask,
Link Here
|
415 |
|
421 |
|
416 |
_setup_pipes(fd_pipes, close_fds=close_fds) |
422 |
_setup_pipes(fd_pipes, close_fds=close_fds) |
417 |
|
423 |
|
|
|
424 |
# Unshare network (while still uid==0) |
425 |
if unshare_net: |
426 |
filename = find_library("c") |
427 |
if filename is not None: |
428 |
libc = LoadLibrary(filename) |
429 |
if libc is not None: |
430 |
CLONE_NEWNET = 0x40000000 |
431 |
try: |
432 |
if libc.unshare(CLONE_NEWNET) != 0: |
433 |
writemsg("Unable to unshare network: %s\n" % ( |
434 |
errno.errorcode.get(ctypes.get_errno(), '?')), |
435 |
noiselevel=-1) |
436 |
except AttributeError: |
437 |
# unshare() not supported by libc |
438 |
pass |
439 |
|
418 |
# Set requested process permissions. |
440 |
# Set requested process permissions. |
419 |
if gid: |
441 |
if gid: |
420 |
# Cast proxies to int, in case it matters. |
442 |
# Cast proxies to int, in case it matters. |
421 |
- |
|
|