[tor-commits] [stem/master] System utility for getting FreeBSD jail paths

atagar at torproject.org atagar at torproject.org
Wed May 29 15:22:57 UTC 2013


commit 18aa77f4d6074e54db84f93e630aaacd5e3fbcb3
Author: Damian Johnson <atagar at torproject.org>
Date:   Tue May 28 17:32:11 2013 -0700

    System utility for getting FreeBSD jail paths
    
    Adding a util to better support FreeBSD jails (a rather common way to run tor
    on that platform).
---
 stem/util/system.py      |   23 +++++++++++++++++++++++
 test/unit/util/system.py |   20 +++++++++++++++++++-
 2 files changed, 42 insertions(+), 1 deletion(-)

diff --git a/stem/util/system.py b/stem/util/system.py
index e760ae5..2f3cbd2 100644
--- a/stem/util/system.py
+++ b/stem/util/system.py
@@ -69,6 +69,7 @@ GET_PID_BY_FILE_LSOF = "lsof -tw %s"
 GET_CWD_PWDX = "pwdx %s"
 GET_CWD_LSOF = "lsof -a -p %s -d cwd -Fn"
 GET_BSD_JAIL_ID_PS = "ps -p %s -o jid"
+GET_BSD_JAIL_PATH = "jls -j %s"
 
 # flag for setting the process name, found in '/usr/include/linux/prctl.h'
 
@@ -657,6 +658,28 @@ def get_bsd_jail_id(pid):
   return 0
 
 
+def get_bsd_jail_path(jid):
+  """
+  Provides the path of the given FreeBSD jail.
+
+  :param int jid: jail id to be queried
+
+  :returns: **str** of the path prefix, **None** if this can't be determined
+  """
+
+  if jid != 0:
+    # Output should be something like:
+    #    JID  IP Address      Hostname      Path
+    #      1  10.0.0.2        tor-jail      /usr/jails/tor-jail
+  
+    jls_output = call(GET_BSD_JAIL_PATH % jid, [])
+
+    if len(jls_output) == 2 and len(jls_output[1].split()) == 4:
+      return jls_output[1].split()[3]
+
+  return None
+
+
 def expand_path(path, cwd = None):
   """
   Provides an absolute path, expanding tildes with the user's home and
diff --git a/test/unit/util/system.py b/test/unit/util/system.py
index fc961cd..c59d2ee 100644
--- a/test/unit/util/system.py
+++ b/test/unit/util/system.py
@@ -62,6 +62,11 @@ GET_PID_BY_PORT_LSOF_RESULTS = [
   "tor     1745 atagar    6u  IPv4  14229      0t0  TCP 127.0.0.1:9051 (LISTEN)",
   "apache   329 atagar    6u  IPv4  14229      0t0  TCP 127.0.0.1:80 (LISTEN)"]
 
+GET_BSD_JAIL_PATH_RESULTS = [
+  "   JID  IP Address      Hostname      Path",
+  "     1  10.0.0.2        tor-jail      /usr/jails/tor-jail",
+]
+
 
 def mock_call(base_cmd, responses):
   """
@@ -87,7 +92,7 @@ def mock_call(base_cmd, responses):
     functor to override stem.util.system.call with
   """
 
-  def _mock_call(base_cmd, responses, command):
+  def _mock_call(base_cmd, responses, command, default = None):
     if isinstance(responses, list):
       if base_cmd == command:
         return responses
@@ -310,6 +315,19 @@ class TestSystem(unittest.TestCase):
       expected_response = 1 if test_input == "1111" else 0
       self.assertEquals(expected_response, system.get_bsd_jail_id(test_input))
 
+  def test_get_bsd_jail_path(self):
+    """
+    Tests the get_bsd_jail_path function.
+    """
+
+    # check when we don't have a jail
+
+    mocking.mock(system.call, mocking.return_value([]))
+    self.assertEquals(None, system.get_bsd_jail_path(1))
+
+    mocking.mock(system.call, mock_call(system.GET_BSD_JAIL_PATH % '1', GET_BSD_JAIL_PATH_RESULTS))
+    self.assertEquals("/usr/jails/tor-jail", system.get_bsd_jail_path(1))
+
   def test_expand_path_unix(self):
     """
     Tests the expand_path function. This does not exercise home directory





More information about the tor-commits mailing list