[or-cvs] r24022: {arm} fix: Connection resolution wasn't finding results if tor was (in arm/trunk/src: interface util)

Damian Johnson atagar1 at gmail.com
Sun Jan 2 21:48:04 UTC 2011


Author: atagar
Date: 2011-01-02 21:48:04 +0000 (Sun, 02 Jan 2011)
New Revision: 24022

Modified:
   arm/trunk/src/interface/controller.py
   arm/trunk/src/util/connections.py
   arm/trunk/src/util/sysTools.py
Log:
fix: Connection resolution wasn't finding results if tor was running under a different name.



Modified: arm/trunk/src/interface/controller.py
===================================================================
--- arm/trunk/src/interface/controller.py	2011-01-02 16:44:07 UTC (rev 24021)
+++ arm/trunk/src/interface/controller.py	2011-01-02 21:48:04 UTC (rev 24022)
@@ -530,7 +530,8 @@
   if not isBlindMode:
     if torPid:
       # use the tor pid to help narrow connection results
-      resolver = connections.getResolver("tor", torPid)
+      torCmdName = sysTools.getProcessName(torPid, "tor")
+      resolver = connections.getResolver(torCmdName, torPid, "tor")
     else:
       resolver = connections.getResolver("tor")
   

Modified: arm/trunk/src/util/connections.py
===================================================================
--- arm/trunk/src/util/connections.py	2011-01-02 16:44:07 UTC (rev 24021)
+++ arm/trunk/src/util/connections.py	2011-01-02 21:48:04 UTC (rev 24022)
@@ -199,7 +199,7 @@
   
   return False
 
-def getResolver(processName, processPid = ""):
+def getResolver(processName, processPid = "", alias=None):
   """
   Singleton constructor for resolver instances. If a resolver already exists
   for the process then it's returned. Otherwise one is created and started.
@@ -208,18 +208,20 @@
     processName - name of the process being resolved
     processPid  - pid of the process being resolved, if undefined this matches
                   against any resolver with the process name
+    alias       - alternative handle under which the resolver can be requested
   """
   
   # check if one's already been created
+  requestHandle = alias if alias else processName
   haltedIndex = -1 # old instance of this resolver with the _halt flag set
   for i in range(len(RESOLVERS)):
     resolver = RESOLVERS[i]
-    if resolver.processName == processName and (not processPid or resolver.processPid == processPid):
+    if resolver.handle == requestHandle and (not processPid or resolver.processPid == processPid):
       if resolver._halt and RECREATE_HALTED_RESOLVERS: haltedIndex = i
       else: return resolver
   
   # make a new resolver
-  r = ConnectionResolver(processName, processPid)
+  r = ConnectionResolver(processName, processPid, handle = requestHandle)
   r.start()
   
   # overwrites halted instance of this resolver if it exists, otherwise append
@@ -291,7 +293,7 @@
     * read-only
   """
   
-  def __init__(self, processName, processPid = "", resolveRate = None):
+  def __init__(self, processName, processPid = "", resolveRate = None, handle = None):
     """
     Initializes a new resolver daemon. When no longer needed it's suggested
     that this is stopped.
@@ -301,6 +303,8 @@
       processPid  - pid of the process being resolved
       resolveRate - time between resolving connections (in seconds, None if
                     chosen dynamically)
+      handle      - name used to query this resolver, this is the processName
+                    if undefined
     """
     
     threading.Thread.__init__(self)
@@ -309,6 +313,7 @@
     self.processName = processName
     self.processPid = processPid
     self.resolveRate = resolveRate
+    self.handle = handle if handle else processName
     self.defaultRate = CONFIG["queries.connections.minRate"]
     self.lastLookup = -1
     self.overwriteResolver = None

Modified: arm/trunk/src/util/sysTools.py
===================================================================
--- arm/trunk/src/util/sysTools.py	2011-01-02 16:44:07 UTC (rev 24021)
+++ arm/trunk/src/util/sysTools.py	2011-01-02 21:48:04 UTC (rev 24022)
@@ -122,12 +122,26 @@
     return PROCESS_NAME_CACHE[pid]
   
   processName, raisedExc = "", None
+  
+  # fetch it from proc contents if available
   if procTools.isProcAvailable():
     try:
       processName = procTools.getStats(pid, procTools.STAT_COMMAND)[0]
     except IOError, exc:
       raisedExc = exc
   
+  # fall back to querying via ps
+  if not processName:
+    # the ps call formats results as:
+    # COMMAND
+    # tor
+    psCall = call("ps -p %s -o command" % pid)
+    
+    if psCall and len(psCall) >= 2 and not " " in psCall[1]:
+      processName, raisedExc = psCall[1].strip(), None
+    else:
+      raisedExc = ValueError("Unexpected output from ps: %s" % psCall)
+  
   if raisedExc:
     if default == None: raise raisedExc
     else:



More information about the tor-commits mailing list