commit b7d5a21ea2d583323262824b5e477e52a8179084 Author: Damian Johnson atagar@torproject.org Date: Sun Sep 8 22:37:14 2013 -0700
Revised form of getPathPrefix() called get_chroot()
More than just a rename, the old function was embarrasingly buggy. Revising to reflect PEP8 conventions in addition to fixing all the issues I could spot. Impressive how I screwed up the edge cases in such a tiny function... --- arm/graphing/bandwidthStats.py | 2 +- arm/logPanel.py | 2 +- arm/starter.py | 2 +- arm/util/torConfig.py | 2 +- arm/util/torTools.py | 51 +++++++++++++++++++++++----------------- armrc.sample | 3 ++- 6 files changed, 35 insertions(+), 27 deletions(-)
diff --git a/arm/graphing/bandwidthStats.py b/arm/graphing/bandwidthStats.py index b6f5d5a..9d05c72 100644 --- a/arm/graphing/bandwidthStats.py +++ b/arm/graphing/bandwidthStats.py @@ -151,7 +151,7 @@ class BandwidthStats(graphPanel.GraphStats): return False
# attempt to open the state file - try: stateFile = open("%s%s/state" % (torTools.getPathPrefix(), dataDir), "r") + try: stateFile = open("%s%s/state" % (torTools.get_chroot(), dataDir), "r") except IOError: msg = PREPOPULATE_FAILURE_MSG % "unable to read the state file" log.notice(msg) diff --git a/arm/logPanel.py b/arm/logPanel.py index a1b0b87..1f3067c 100644 --- a/arm/logPanel.py +++ b/arm/logPanel.py @@ -218,7 +218,7 @@ def getLogFileEntries(runlevels, readLimit = None, addLimit = None): if not loggingLocation: return []
# includes the prefix for tor paths - loggingLocation = torTools.getPathPrefix() + loggingLocation + loggingLocation = torTools.get_chroot() + loggingLocation
# if the runlevels argument is a superset of the log file then we can # limit the read contents to the addLimit diff --git a/arm/starter.py b/arm/starter.py index 5c51812..20a15c6 100644 --- a/arm/starter.py +++ b/arm/starter.py @@ -221,7 +221,7 @@ def _getController(controlAddr="127.0.0.1", controlPort=9051, passphrase=None, i Custom handler for establishing a stem connection (... needs an overhaul). """
- chroot = arm.util.torTools.getPathPrefix() + chroot = arm.util.torTools.get_chroot()
try: controller = Controller.from_port(controlAddr, controlPort) diff --git a/arm/util/torConfig.py b/arm/util/torConfig.py index beb88b1..955e1c5 100644 --- a/arm/util/torConfig.py +++ b/arm/util/torConfig.py @@ -343,7 +343,7 @@ def getConfigLocation():
conn = torTools.getConn() configLocation = conn.getInfo("config-file", None) - torPid, torPrefix = conn.controller.get_pid(None), torTools.getPathPrefix() + torPid, torPrefix = conn.controller.get_pid(None), torTools.get_chroot() if not configLocation: raise IOError("unable to query the torrc location")
try: diff --git a/arm/util/torTools.py b/arm/util/torTools.py index e7766e3..63dc257 100644 --- a/arm/util/torTools.py +++ b/arm/util/torTools.py @@ -3,10 +3,11 @@ Helper for working with an active tor process. This both provides a wrapper for accessing stem and notifications of state changes to subscribers. """
-import os -import time import math +import os +import platform import threading +import time
import stem import stem.control @@ -22,7 +23,7 @@ CONTROLLER = None # singleton Controller instance UNDEFINED = "<Undefined_ >"
CONFIG = conf.config_dict("arm", { - "features.pathPrefix": "", + "tor.chroot": "", })
# events used for controller functionality: @@ -35,7 +36,7 @@ REQ_EVENTS = {"NEWDESC": "information related to descriptors will grow stale", # only done once for the duration of the application to avoid pointless # messages.
-PATH_PREFIX_LOGGING = True +LOG_ABOUT_CHROOTS = True
def getConn(): """ @@ -47,35 +48,41 @@ def getConn(): if CONTROLLER == None: CONTROLLER = Controller() return CONTROLLER
-def getPathPrefix(): + +def get_chroot(): """ Provides the path prefix that should be used for fetching tor resources. - If undefined and Tor is inside a jail under FreeBsd then this provides the + If undefined and Tor is inside a jail under FreeBSD then this provides the jail's path. + + :returns: **str** with the path of the jail tor is running within, this is an + empty string if none can be determined """
- global PATH_PREFIX_LOGGING + global LOG_ABOUT_CHROOTS + + chroot = CONFIG["tor.chroot"].strip() + + if chroot and not os.path.exists(chroot): + if LOG_ABOUT_CHROOTS: + log.notice("The prefix path set in your config (%s) doesn't exist." % chroot) + + chroot = ''
- # make sure the path prefix is valid and exists (providing a notice if not) - prefixPath = CONFIG["features.pathPrefix"].strip() + if not chroot and platform.system() == "FreeBSD": + jail_chroot = system.get_bsd_jail_path(getConn().controller.get_pid(0))
- if not prefixPath and os.uname()[0] == "FreeBSD": - prefixPath = system.get_bsd_jail_path(getConn().controller.get_pid(0)) + if jail_chroot and os.path.exists(jail_chroot): + chroot = jail_chroot
- if prefixPath and PATH_PREFIX_LOGGING: - log.info("Adjusting paths to account for Tor running in a jail at: %s" % prefixPath) + if LOG_ABOUT_CHROOTS: + log.info("Adjusting paths to account for Tor running in a FreeBSD jail at: %s" % chroot)
- if prefixPath: - # strips off ending slash from the path - if prefixPath.endswith("/"): prefixPath = prefixPath[:-1] + chroot = chroot.rstrip(os.path.sep) # strip off trailing slashes + LOG_ABOUT_CHROOTS = False # don't log about further calls
- # avoid using paths that don't exist - if PATH_PREFIX_LOGGING and prefixPath and not os.path.exists(prefixPath): - log.notice("The prefix path set in your config (%s) doesn't exist." % prefixPath) - prefixPath = "" + return chroot
- PATH_PREFIX_LOGGING = False # prevents logging if fetched again - return prefixPath
class Controller: """ diff --git a/armrc.sample b/armrc.sample index 253da0d..89e3878 100644 --- a/armrc.sample +++ b/armrc.sample @@ -41,7 +41,8 @@ features.torrc.validate true
# Set this if you're running in a chroot jail or other environment where tor's # resources (log, state, etc) should have a prefix in their paths. -features.pathPrefix + +tor.chroot
# If set, arm appends any log messages it reports while running to the given # log file. This does not take filters into account or include prepopulated
tor-commits@lists.torproject.org