[tor-commits] [arm/master] Utilizing stem's pid resolution

atagar at torproject.org atagar at torproject.org
Tue May 28 04:49:19 UTC 2013


commit 9ea729942a7046ea72ccfbddb011c0342eaf3560
Author: Damian Johnson <atagar at torproject.org>
Date:   Mon May 27 21:40:55 2013 -0700

    Utilizing stem's pid resolution
    
    Dropping our getPid() helper function in favor of stem's new Controller method
    for doing this.
---
 src/cli/controller.py              |   10 +++----
 src/cli/graphing/bandwidthStats.py |    2 +-
 src/cli/graphing/resourceStats.py  |    2 +-
 src/cli/headerPanel.py             |    3 +-
 src/util/torConfig.py              |    2 +-
 src/util/torTools.py               |   55 ++++--------------------------------
 6 files changed, 15 insertions(+), 59 deletions(-)

diff --git a/src/cli/controller.py b/src/cli/controller.py
index 03b05e2..0e45ca9 100644
--- a/src/cli/controller.py
+++ b/src/cli/controller.py
@@ -499,10 +499,10 @@ def connResetListener(controller, eventType, _):
       if getController().getPanel("torrc") == None:
         torConfig.getTorrc().load(True)
       
-      torPid = controller.get_info("process/pid", None)
-      
-      if torPid and torPid != resolver.getPid():
-        resolver.setPid(torPid)
+      try:
+        resolver.setPid(controller.get_pid())
+      except ValueError:
+        pass
 
 def startTorMonitor(startTime):
   """
@@ -515,7 +515,7 @@ def startTorMonitor(startTime):
   # attempts to fetch the tor pid, warning if unsuccessful (this is needed for
   # checking its resource usage, among other things)
   conn = torTools.getConn()
-  torPid = conn.getMyPid()
+  torPid = conn.controller.get_pid(None)
   
   if not torPid and conn.isAlive():
     log.warn("Unable to determine Tor's pid. Some information, like its resource usage will be unavailable.")
diff --git a/src/cli/graphing/bandwidthStats.py b/src/cli/graphing/bandwidthStats.py
index 698a2b1..935e23d 100644
--- a/src/cli/graphing/bandwidthStats.py
+++ b/src/cli/graphing/bandwidthStats.py
@@ -125,7 +125,7 @@ class BandwidthStats(graphPanel.GraphStats):
     # TODO: stem dropped system caching support so we'll need to think of
     # something else
     uptime = None
-    queryPid = conn.getMyPid()
+    queryPid = conn.controller.get_pid(None)
     if queryPid:
       queryParam = ["%cpu", "rss", "%mem", "etime"]
       queryCmd = "ps -p %s -o %s" % (queryPid, ",".join(queryParam))
diff --git a/src/cli/graphing/resourceStats.py b/src/cli/graphing/resourceStats.py
index d9c15b5..c0f18c9 100644
--- a/src/cli/graphing/resourceStats.py
+++ b/src/cli/graphing/resourceStats.py
@@ -14,7 +14,7 @@ class ResourceStats(graphPanel.GraphStats):
   
   def __init__(self):
     graphPanel.GraphStats.__init__(self)
-    self.queryPid = torTools.getConn().getMyPid()
+    self.queryPid = torTools.getConn().controller.get_pid(None)
   
   def clone(self, newCopy=None):
     if not newCopy: newCopy = ResourceStats()
diff --git a/src/cli/headerPanel.py b/src/cli/headerPanel.py
index b1bd65e..895db01 100644
--- a/src/cli/headerPanel.py
+++ b/src/cli/headerPanel.py
@@ -516,8 +516,7 @@ class HeaderPanel(panel.Panel, threading.Thread):
       self.vals["sys/os"] = unameVals[0]
       self.vals["sys/version"] = unameVals[2]
       
-      pid = conn.getMyPid()
-      self.vals["tor/pid"] = pid if pid else ""
+      self.vals["tor/pid"] = conn.controller.get_pid("")
       
       startTime = conn.getStartTime()
       self.vals["tor/startTime"] = startTime if startTime else ""
diff --git a/src/util/torConfig.py b/src/util/torConfig.py
index 52caae6..5f4e525 100644
--- a/src/util/torConfig.py
+++ b/src/util/torConfig.py
@@ -343,7 +343,7 @@ def getConfigLocation():
   
   conn = torTools.getConn()
   configLocation = conn.getInfo("config-file", None)
-  torPid, torPrefix = conn.getMyPid(), conn.getPathPrefix()
+  torPid, torPrefix = conn.controller.get_pid(None), conn.getPathPrefix()
   if not configLocation: raise IOError("unable to query the torrc location")
   
   try:
diff --git a/src/util/torTools.py b/src/util/torTools.py
index 397a32a..62898d5 100644
--- a/src/util/torTools.py
+++ b/src/util/torTools.py
@@ -51,37 +51,6 @@ REQ_EVENTS = {"NEWDESC": "information related to descriptors will grow stale",
               "NS": "information related to the consensus will grow stale",
               "NEWCONSENSUS": "information related to the consensus will grow stale"}
 
-def getPid(controlPort=9051, pidFilePath=None):
-  """
-  Attempts to determine the process id for a running tor process. This uses
-  the PidFile if it's available, otherwise trying to figure it out from our
-  process name and control port.
-  
-  Arguments:
-    controlPort - control port of the tor process if multiple exist
-    pidFilePath - path to the pid file generated by tor
-  """
-  
-  # attempts to fetch via the PidFile, failing if:
-  # - the option is unset
-  # - unable to read the file (such as insufficient permissions)
-  
-  if pidFilePath:
-    try:
-      pidFile = open(pidFilePath, "r")
-      pidEntry = pidFile.readline().strip()
-      pidFile.close()
-      
-      if pidEntry.isdigit(): return pidEntry
-    except: pass
-  
-  pid = stem.util.system.get_pid_by_name('tor')
-
-  if pid is None:
-    pid = stem.util.system.get_pid_by_port(controlPort)
-
-  return pid
-
 def isTorRunning():
   """
   Simple check for if a tor process is running. If this can't be determined
@@ -523,14 +492,6 @@ class Controller:
     else:
       return False
   
-  def getMyPid(self):
-    """
-    Provides the pid of the attached tor process (None if no controller exists
-    or this can't be determined).
-    """
-    
-    return self._getRelayAttr("pid", None)
-  
   def getMyUser(self):
     """
     Provides the user this process is running under. If unavailable this
@@ -553,7 +514,7 @@ class Controller:
     
     result = None
     if self.isAlive() and proc.is_available():
-      myPid = self.getMyPid()
+      myPid = self.controller.get_pid(None)
       
       if myPid:
         try: result = len(os.listdir("/proc/%s/fd" % myPid))
@@ -996,7 +957,8 @@ class Controller:
             if self._isReset: break
           
           if not self._isReset:
-            errorLine, torPid = "", self.getMyPid()
+            errorLine, torPid = "", self.controller.get_pid(None)
+
             if torPid:
               for line in pkillOutput:
                 if line.startswith("pkill: %s - " % torPid):
@@ -1320,11 +1282,6 @@ class Controller:
           if line.startswith("s "):
             result = line[2:].split()
             break
-      elif key == "pid":
-        result = self.getInfo("process/pid", None)
-        
-        if not result:
-          result = getPid(int(self.getOption("ControlPort", 9051)), self.getOption("PidFile", None))
       elif key == "user":
         # provides the empty string if the query fails
         queriedUser = self.getInfo("process/user", None)
@@ -1332,7 +1289,7 @@ class Controller:
         if queriedUser != None and queriedUser != "":
           result = queriedUser
         else:
-          myPid = self.getMyPid()
+          myPid = self.controller.get_pid(None)
           
           if myPid:
             # if proc contents are available then fetch the pid from there and
@@ -1378,7 +1335,7 @@ class Controller:
         # adjusts the prefix path to account for jails under FreeBSD (many
         # thanks to Fabian Keil!)
         if not prefixPath and os.uname()[0] == "FreeBSD":
-          torPid = getConn().getMyPid()
+          torPid = getConn().controller.get_pid(None)
           jid = system.get_bsd_jail_id(torPid)
           if jid != 0:
             # Output should be something like:
@@ -1404,7 +1361,7 @@ class Controller:
         self._pathPrefixLogging = False # prevents logging if fetched again
         result = prefixPath
       elif key == "startTime":
-        myPid = self.getMyPid()
+        myPid = self.controller.get_pid(None)
         
         if myPid:
           if proc.is_available():





More information about the tor-commits mailing list