[or-cvs] r23897: {arm} Applying patches from Fabian Keil and Hans Schnehl with fixe (in arm/trunk: . src/util)

Damian Johnson atagar1 at gmail.com
Mon Dec 6 18:16:30 UTC 2010


Author: atagar
Date: 2010-12-06 18:16:30 +0000 (Mon, 06 Dec 2010)
New Revision: 23897

Modified:
   arm/trunk/armrc.sample
   arm/trunk/src/util/connections.py
   arm/trunk/src/util/torTools.py
Log:
Applying patches from Fabian Keil and Hans Schnehl with fixes for BSD (most notably for getting the pid and connection resolution).



Modified: arm/trunk/armrc.sample
===================================================================
--- arm/trunk/armrc.sample	2010-12-05 19:23:15 UTC (rev 23896)
+++ arm/trunk/armrc.sample	2010-12-06 18:16:30 UTC (rev 23897)
@@ -218,6 +218,7 @@
 log.configDescriptions.persistance.loadFailed INFO
 log.configDescriptions.persistance.saveSuccess NOTICE
 log.configDescriptions.persistance.saveFailed NOTICE
+log.connResolverOptions INFO
 log.connLookupFailed INFO
 log.connLookupFailover NOTICE
 log.connLookupAbandon WARN

Modified: arm/trunk/src/util/connections.py
===================================================================
--- arm/trunk/src/util/connections.py	2010-12-05 19:23:15 UTC (rev 23896)
+++ arm/trunk/src/util/connections.py	2010-12-06 18:16:30 UTC (rev 23897)
@@ -12,8 +12,8 @@
 FreeBSD lacks support for the needed netstat flags and has a completely
 different program for 'ss'. However, there's a couple other options (thanks to
 Fabian Keil and Hans Schnehl):
-- sockstat    sockstat -4 | egrep '<process>\s*<pid>' | grep -v '*:*'
-- procstat    procstat -f 'pgrep <process>' | grep '<pid>' | grep -v '0.0.0.0:0'
+- sockstat    sockstat -4c | grep '<process> *<pid>'
+- procstat    procstat -f <pid> | grep TCP | grep -v 0.0.0.0:0
 """
 
 import os
@@ -62,8 +62,8 @@
 # *note: this isn't available by default under ubuntu
 RUN_SOCKSTAT = "sockstat | egrep \"%s\s*%s.*ESTABLISHED\""
 
-RUN_BSD_SOCKSTAT = "sockstat -4 | egrep '%s\s*%s' | grep -v '*:*'"
-RUN_BSD_PROCSTAT = "procstat -f 'pgrep %s' | grep '%s' | grep -v '0.0.0.0:0'"
+RUN_BSD_SOCKSTAT = "sockstat -4c | grep '%s *%s'"
+RUN_BSD_PROCSTAT = "procstat -f %s | grep TCP | grep -v 0.0.0.0:0"
 
 RESOLVERS = []                      # connection resolvers available via the singleton constructor
 RESOLVER_FAILURE_TOLERANCE = 3      # number of subsequent failures before moving on to another resolver
@@ -101,7 +101,7 @@
   elif resolutionCmd == CMD_LSOF: cmd = RUN_LSOF % (processName, processPid)
   elif resolutionCmd == CMD_SOCKSTAT: cmd = RUN_SOCKSTAT % (processName, processPid)
   elif resolutionCmd == CMD_BSD_SOCKSTAT: cmd = RUN_BSD_SOCKSTAT % (processName, processPid)
-  elif resolutionCmd == CMD_BSD_PROCSTAT: cmd = RUN_BSD_PROCSTAT % (processName, processPid)
+  elif resolutionCmd == CMD_BSD_PROCSTAT: cmd = RUN_BSD_PROCSTAT % processPid
   else: raise ValueError("Unrecognized resolution type: %s" % resolutionCmd)
   
   # raises an IOError if the command fails or isn't available
@@ -182,7 +182,7 @@
 
 def test():
   # quick method for testing connection resolution
-  userInput = raw_input("Enter query (<ss, netstat, lsof> PROCESS_NAME [PID]): ").split()
+  userInput = raw_input("Enter query (<ss, netstat, lsof, sockstat> PROCESS_NAME [PID]): ").split()
   
   # checks if there's enough arguments
   if len(userInput) == 0: sys.exit(0)
@@ -195,6 +195,7 @@
   if userInput[0] == "ss": userInput[0] = CMD_SS
   elif userInput[0] == "netstat": userInput[0] = CMD_NETSTAT
   elif userInput[0] == "lsof": userInput[0] = CMD_LSOF
+  elif userInput[0] == "sockstat": userInput[0] = CMD_SOCKSTAT
   else:
     print "unrecognized type of resolver: %s" % userInput[2]
     sys.exit(1)

Modified: arm/trunk/src/util/torTools.py
===================================================================
--- arm/trunk/src/util/torTools.py	2010-12-05 19:23:15 UTC (rev 23896)
+++ arm/trunk/src/util/torTools.py	2010-12-06 18:16:30 UTC (rev 23897)
@@ -93,9 +93,11 @@
   Attempts to determine the process id for a running tor process, using the
   following:
   1. GETCONF PidFile
-  2. "pidof tor"
-  3. "netstat -npl | grep 127.0.0.1:%s" % <tor control port>
-  4. "ps -o pid -C tor"
+  2. "pgrep -x tor"
+  3. "pidof tor"
+  4. "netstat -npl | grep 127.0.0.1:%s" % <tor control port>
+  5. "ps -o pid -C tor"
+  6. "sockstat -4l -P tcp -p %i | grep tor" % <tor control port>
   
   If pidof or ps provide multiple tor instances then their results are
   discarded (since only netstat can differentiate using the control port). This
@@ -119,6 +121,16 @@
       if pidEntry.isdigit(): return pidEntry
     except: pass
   
+  # attempts to resolve using pgrep, failing if:
+  # - tor is running under a different name
+  # - there are multiple instances of tor
+  try:
+    results = sysTools.call("pgrep -x 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 pidof, failing if:
   # - tor's running under a different name
   # - there's multiple instances of tor
@@ -150,6 +162,21 @@
       if pid.isdigit(): return pid
   except IOError: pass
   
+  # attempts to resolve using sockstat, failing if:
+  # - sockstat doesn't accept the -4 flag (BSD only)
+  # - tor is running under a different name
+  # - there are multiple instances of Tor, using the
+  #   same control port on different addresses.
+  # 
+  # TODO: the later two issues could be solved by filtering for the control
+  # port IP address instead of the process name.
+  try:
+    results = sysTools.call("sockstat -4l -P tcp -p %i | grep tor" % controlPort)
+    if len(results) == 1 and len(results[0].split()) == 7:
+      pid = results[0].split()[2]
+      if pid.isdigit(): return pid
+  except IOError: pass
+  
   return None
 
 def getConn():



More information about the tor-commits mailing list