[or-cvs] r22320: {arm} change: moving tor pid resolution to its own util function (in arm/trunk: init interface util)

Damian Johnson atagar1 at gmail.com
Wed May 12 15:54:45 UTC 2010


Author: atagar
Date: 2010-05-12 15:54:45 +0000 (Wed, 12 May 2010)
New Revision: 22320

Modified:
   arm/trunk/init/starter.py
   arm/trunk/interface/controller.py
   arm/trunk/util/torTools.py
Log:
change: moving tor pid resolution to its own util function



Modified: arm/trunk/init/starter.py
===================================================================
--- arm/trunk/init/starter.py	2010-05-12 14:58:18 UTC (rev 22319)
+++ arm/trunk/init/starter.py	2010-05-12 15:54:45 UTC (rev 22320)
@@ -136,7 +136,7 @@
   TorCtl.TorUtil.loglevel = "NONE"
   
   # sets up TorCtl connection, prompting for the passphrase if necessary and
-  # printing a notice if they arise
+  # sending problems to stdout if they arise
   util.torTools.INCORRECT_PASSWORD_MSG = "Controller password found in '%s' was incorrect" % configPath
   authPassword = config.get(AUTH_CFG, None)
   conn = util.torTools.getConn(controlAddr, controlPort, authPassword)

Modified: arm/trunk/interface/controller.py
===================================================================
--- arm/trunk/interface/controller.py	2010-05-12 14:58:18 UTC (rev 22319)
+++ arm/trunk/interface/controller.py	2010-05-12 15:54:45 UTC (rev 22320)
@@ -22,7 +22,7 @@
 import descriptorPopup
 import fileDescriptorPopup
 
-from util import log, connections, hostnames, panel, sysTools, uiTools
+from util import log, connections, hostnames, panel, sysTools, torTools, uiTools
 import bandwidthMonitor
 import cpuMemMonitor
 import connCountMonitor
@@ -320,34 +320,13 @@
   try: curses.curs_set(0)
   except curses.error: pass
   
-  # gets pid of tor instance with control port open
-  torPid = None       # None if couldn't be resolved (provides error later)
+  # attempts to determine tor's current pid (left as None if unresolveable, logging an error later)
+  controlPort = None
+  try: controlPort = int(conn.get_option("ControlPort")[0][1])
+  except (socket.error, TorCtl.ErrorReply, TorCtl.TorCtlClosed): pass
+  torPid = torTools.getPid(controlPort)
   
   try:
-    # gets pid if there's only one possability
-    results = sysTools.call("pidof tor")
-    if len(results) == 1 and len(results[0].split()) == 1: torPid = results[0].strip()
-  except IOError: pass # pid call failed
-  
-  if not torPid:
-    try:
-      # uses netstat to identify process with open control port (might not
-      # work if tor's being run as a different user due to permissions)
-      results = sysTools.call("netstat -npl | grep 127.0.0.1:%s" % conn.get_option("ControlPort")[0][1])
-      
-      if len(results) == 1:
-        results = results[0].split()[6] # process field (ex. "7184/tor")
-        torPid = results[:results.find("/")]
-    except (IOError, socket.error, TorCtl.ErrorReply, TorCtl.TorCtlClosed): pass # netstat or control port calls failed
-  
-  if not torPid:
-    try:
-      # third try, use ps if there's only one possability
-      results = sysTools.call("ps -o pid -C tor")
-      if len(results) == 2 and len(results[0].split()) == 1: torPid = results[1].strip()
-    except IOError: pass # ps call failed
-  
-  try:
     confLocation = conn.get_info("config-file")["config-file"]
     if confLocation[0] != "/":
       # relative path - attempt to add process pwd

Modified: arm/trunk/util/torTools.py
===================================================================
--- arm/trunk/util/torTools.py	2010-05-12 14:58:18 UTC (rev 22319)
+++ arm/trunk/util/torTools.py	2010-05-12 15:54:45 UTC (rev 22320)
@@ -8,6 +8,8 @@
 
 from TorCtl import TorCtl
 
+import sysTools
+
 INCORRECT_PASSWORD_MSG = "Provided passphrase was incorrect"
 
 def makeCtlConn(controlAddr="127.0.0.1", controlPort=9051):
@@ -150,3 +152,53 @@
       print exc
       return None
 
+def getPid(controlPort=9051):
+  """
+  Attempts to determine the process id for a running tor process, using the
+  following:
+  1. "pidof tor"
+  2. "netstat -npl | grep 127.0.0.1:%s" % <tor control port>
+  3. "ps -o pid -C tor"
+  
+  If pidof or ps promide multiple tor instances then their results are discared
+  (since only netstat differentiates using the control port). This provdes None
+  if either no running process exists or it can't be determined.
+  
+  Arguments:
+    controlPort - control port of the tor process if multiple exist
+  """
+  
+  # attempts to resolve using pidof, failing if:
+  # - tor's running under a different name
+  # - there's multiple instances of tor
+  try:
+    results = sysTools.call("pidof tor")
+    if len(results) == 1 and len(results[0].split()) == 1:
+      pid = results[0].strip()
+      if pid.isdigit(): return pid
+  except IOError: pass
+  
+  # attempts to resolve using netstat (identifying process via the open control
+  # port), failing if:
+  # - tor's being run as a different user due to permissions
+  try:
+    results = sysTools.call("netstat -npl | grep 127.0.0.1:%i" % controlPort)
+    
+    if len(results) == 1:
+      results = results[0].split()[6] # process field (ex. "7184/tor")
+      pid = results[:results.find("/")]
+      if pid.isdigit(): return pid
+  except IOError: pass
+  
+  # attempts to resolve using ps, failing if:
+  # - tor's running under a different name
+  # - there's multiple instances of tor
+  try:
+    results = sysTools.call("ps -o pid -C tor")
+    if len(results) == 2:
+      pid = results[1].strip()
+      if pid.isdigit(): return pid
+  except IOError: pass
+  
+  return None
+



More information about the tor-commits mailing list