summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Thursfield <sam.thursfield@codethink.co.uk>2015-06-08 14:27:13 +0100
committerSam Thursfield <sam.thursfield@codethink.co.uk>2015-06-08 14:29:34 +0100
commitc6fe37a7d60e17d936d8a0f424190bb96fc882bc (patch)
tree6bb84cbb51e193858ae01b893846b1671fab7f8e
parent79504a2446c7617fb20b855117b5aa77e07603c8 (diff)
downloadsandboxlib-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.
-rw-r--r--sandboxlib/__init__.py33
-rw-r--r--sandboxlib/linux_user_chroot.py7
-rw-r--r--sandboxlib/utils.py45
3 files changed, 61 insertions, 24 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
diff --git a/sandboxlib/linux_user_chroot.py b/sandboxlib/linux_user_chroot.py
index a5f84e0..3397a1a 100644
--- a/sandboxlib/linux_user_chroot.py
+++ b/sandboxlib/linux_user_chroot.py
@@ -281,6 +281,11 @@ def create_mount_points_if_missing(filesystem_root, mount_info_list):
os.makedirs(path)
+def linux_user_chroot_program():
+ # Raises sandboxlib.ProgramNotFound if not found.
+ return sandboxlib.utils.find_program('linux-user-chroot')
+
+
def run_sandbox(command, cwd=None, env=None,
filesystem_root='/', filesystem_writable_paths='all',
mounts='undefined', extra_mounts=None,
@@ -289,7 +294,7 @@ def run_sandbox(command, cwd=None, env=None,
if type(command) == str:
command = [command]
- linux_user_chroot_command = ['linux-user-chroot']
+ linux_user_chroot_command = [linux_user_chroot_program()]
extra_mounts = sandboxlib.validate_extra_mounts(extra_mounts)
diff --git a/sandboxlib/utils.py b/sandboxlib/utils.py
new file mode 100644
index 0000000..af5fe3e
--- /dev/null
+++ b/sandboxlib/utils.py
@@ -0,0 +1,45 @@
+# Copyright (C) 2015 Codethink Limited
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program. If not, see <http://www.gnu.org/licenses/>.
+
+
+import logging
+import os
+import shutil
+import subprocess
+import sys
+
+import sandboxlib
+
+
+def find_program(program_name):
+ search_path = os.environ.get('PATH')
+
+ # 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, path=search_path)
+ 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
+
+ if program_path is None:
+ raise sandboxlib.ProgramNotFound(
+ "Did not find '%s' in PATH. Searched '%s'" % (
+ program_name, search_path))
+
+ return program_path