From 59d52c5499b0c1ba861b8222daf245b1b47ba3ee Mon Sep 17 00:00:00 2001 From: Fabian Keil Date: Wed, 8 Dec 2010 23:51:12 +0100 Subject: [PATCH 1/2] When monitoring a Tor process running in a FreeBSD jail, automatically prepend the jailpath to the configLocation. The jailpath magic can be overwritten using features.pathPrefix. --- src/util/torTools.py | 21 +++++++++++++++++++++ 1 files changed, 21 insertions(+), 0 deletions(-) diff --git a/src/util/torTools.py b/src/util/torTools.py index 03bdfdb..154aca0 100644 --- a/src/util/torTools.py +++ b/src/util/torTools.py @@ -86,6 +86,27 @@ def getPathPrefix(): Provides the path prefix that should be used for fetching tor resources. """ + osType = os.uname()[0] + if osType == "FreeBSD" and not CONFIG["features.pathPrefix"]: + torPid = getConn().getMyPid() + psOutput = sysTools.call("ps -p %s -o jid" % torPid) + # Output when called from a FreeBSD jail or when Tor isn't jailed: + # JID + # 0 + # otherwise something like: + # JID + # 1 + if len(psOutput) == 2 and len(psOutput[1].split()) == 1: + jid = psOutput[1].strip() + if jid.isdigit() and int(jid) != 0: + jlsOutput = sysTools.call("jls -j %s" % jid) + # Output should be something like: + # JID IP Address Hostname Path + # 1 10.0.0.2 tor-jail /usr/jails/tor-jail + if len(jlsOutput) == 2 and len(jlsOutput[1].split()) == 4: + jailPath = jlsOutput[1].split()[3] + return jailPath + return CONFIG["features.pathPrefix"] def getPid(controlPort=9051, pidFilePath=None): -- 1.7.3.3 From dc803beac0a5eb9b14b3b3c48f8355e5e7bce247 Mon Sep 17 00:00:00 2001 From: Fabian Keil Date: Fri, 10 Dec 2010 00:04:41 +0100 Subject: [PATCH 2/2] Factor getJid() out of getPathPrefix() --- src/util/torTools.py | 48 ++++++++++++++++++++++++++++++------------------ 1 files changed, 30 insertions(+), 18 deletions(-) diff --git a/src/util/torTools.py b/src/util/torTools.py index 154aca0..2a22a17 100644 --- a/src/util/torTools.py +++ b/src/util/torTools.py @@ -81,6 +81,27 @@ def loadConfig(config): CONFIG["features.pathPrefix"] = prefixPath +def getJid(): + """ + Get the FreeBSD jail id for the monitored tor process. + """ + + torPid = getConn().getMyPid() + psOutput = sysTools.call("ps -p %s -o jid" % torPid) + # Output when called from a FreeBSD jail or when Tor isn't jailed: + # JID + # 0 + # otherwise something like: + # JID + # 1 + if len(psOutput) == 2 and len(psOutput[1].split()) == 1: + jid = psOutput[1].strip() + if jid.isdigit(): + return int(jid) + + log.log(log.WARN, "Failed to figure out the FreeBSD jail id. Assuming 0.") + return 0 + def getPathPrefix(): """ Provides the path prefix that should be used for fetching tor resources. @@ -88,24 +109,15 @@ def getPathPrefix(): osType = os.uname()[0] if osType == "FreeBSD" and not CONFIG["features.pathPrefix"]: - torPid = getConn().getMyPid() - psOutput = sysTools.call("ps -p %s -o jid" % torPid) - # Output when called from a FreeBSD jail or when Tor isn't jailed: - # JID - # 0 - # otherwise something like: - # JID - # 1 - if len(psOutput) == 2 and len(psOutput[1].split()) == 1: - jid = psOutput[1].strip() - if jid.isdigit() and int(jid) != 0: - jlsOutput = sysTools.call("jls -j %s" % jid) - # Output should be something like: - # JID IP Address Hostname Path - # 1 10.0.0.2 tor-jail /usr/jails/tor-jail - if len(jlsOutput) == 2 and len(jlsOutput[1].split()) == 4: - jailPath = jlsOutput[1].split()[3] - return jailPath + jid = getJid() + if jid != 0: + jlsOutput = sysTools.call("jls -j %s" % jid) + # Output should be something like: + # JID IP Address Hostname Path + # 1 10.0.0.2 tor-jail /usr/jails/tor-jail + if len(jlsOutput) == 2 and len(jlsOutput[1].split()) == 4: + jailPath = jlsOutput[1].split()[3] + return jailPath return CONFIG["features.pathPrefix"] -- 1.7.3.3