diff options
author | Sam Thursfield <sam.thursfield@codethink.co.uk> | 2015-06-08 14:27:13 +0100 |
---|---|---|
committer | Sam Thursfield <sam.thursfield@codethink.co.uk> | 2015-06-08 14:29:34 +0100 |
commit | c6fe37a7d60e17d936d8a0f424190bb96fc882bc (patch) | |
tree | 6bb84cbb51e193858ae01b893846b1671fab7f8e /sandboxlib/__init__.py | |
parent | 79504a2446c7617fb20b855117b5aa77e07603c8 (diff) | |
download | sandboxlib-c6fe37a7d60e17d936d8a0f424190bb96fc882bc.tar.gz |
Change how we search for 'linux-user-chroot'
First, the user should get better errors now if it is not found:
sandboxlib.ProgramNotFound: Did not find 'linux-user-chroot' in
PATH. Searched '/sbin:/bin:/usr/sbin:/usr/bin'
Second, we explicitly search for the program on each call to
run_sandbox() using the same search code used in
sandbox_module_for_platform(). This shouldn't change anything, but I
think it's better practice to search for the program ourselves than to
assume exec('linux-user-chroot') will do the right thing.
Diffstat (limited to 'sandboxlib/__init__.py')
-rw-r--r-- | sandboxlib/__init__.py | 33 |
1 files changed, 10 insertions, 23 deletions
diff --git a/sandboxlib/__init__.py b/sandboxlib/__init__.py index 946ba52..17b29da 100644 --- a/sandboxlib/__init__.py +++ b/sandboxlib/__init__.py @@ -26,9 +26,11 @@ import logging import os import platform import pipes -import shutil import subprocess -import sys + + +class ProgramNotFound(Exception): + pass def maximum_possible_isolation(): @@ -133,21 +135,6 @@ def run_sandbox_with_redirection(command, **sandbox_config): raise NotImplementedError() -def find_program(program_name): - # Python 3.3 and newer provide a 'find program in PATH' function. Otherwise - # we fall back to the `which` program. - if sys.version_info.major >= 3 and sys.version_info.minor >= 3: - program_path = shutil.which(program_name) - else: - try: - argv = ['which', program_name] - program_path = subprocess.check_output(argv).strip() - except subprocess.CalledProcessError as e: - logging.debug("Error searching for %s: %s", program_name, e) - program_path = None - return program_path - - def sandbox_module_for_platform(): '''Returns an execution module that will work on the current platform.''' @@ -157,13 +144,12 @@ def sandbox_module_for_platform(): if platform.uname() == 'Linux': log.info("Linux detected, looking for 'linux-user-chroot'.") - linux_user_chroot_program = find_program('linux-user-chroot') - if linux_user_chroot_program is not None: - log.info("Found %s, choosing 'linux_user_chroot' module.", - linux_user_chroot_program) + try: + program = sandboxlib.linux_user_chroot.linux_user_chroot_program() + log.info("Found %s, choosing 'linux_user_chroot' module.", program) backend = sandboxlib.linux_user_chroot - else: - log.debug("Did not find 'linux-user-chroot' program in PATH.") + except sandboxlib.utils.ProgramNotFound as e: + log.debug("Did not find 'linux-user-chroot': %s", e) if backend is None: log.info("Choosing 'chroot' sandbox module.") @@ -260,3 +246,4 @@ import sandboxlib.chroot import sandboxlib.linux_user_chroot import sandboxlib.load +import sandboxlib.utils |