[tor-commits] [stem/master] Supporting get_pid() remotely via GETINFO

atagar at torproject.org atagar at torproject.org
Wed May 29 16:09:43 UTC 2013


commit 341091492edf7c060609b73b8619b18eda322ebd
Author: Damian Johnson <atagar at torproject.org>
Date:   Wed May 29 08:44:44 2013 -0700

    Supporting get_pid() remotely via GETINFO
    
    One of get_pid()'s resolution methods (GETINFO process/pid) works remotely, so
    attempting it before checking that tor is running locally.
---
 docs/change_log.rst |    2 +-
 stem/control.py     |   37 +++++++++++++++++++++----------------
 stem/util/system.py |    1 +
 3 files changed, 23 insertions(+), 17 deletions(-)

diff --git a/docs/change_log.rst b/docs/change_log.rst
index 9d49537..6e969fa 100644
--- a/docs/change_log.rst
+++ b/docs/change_log.rst
@@ -40,11 +40,11 @@ The following are only available within stem's `git repository
 
  * **Controller**
 
-  * Added a :class:`~stem.control.Controller` method for querying tor's pid (:func:`~stem.control.Controller.get_pid`)
   * :class:`~stem.response.events.AddrMapEvent` support for the new CACHED argument (:trac:`8596`, :spec:`25b0d43`)
   * :func:`~stem.control.Controller.attach_stream` could encounter an undocumented 555 response (:trac:`8701`, :spec:`7286576`)
   * :class:`~stem.descriptor.server_descriptor.RelayDescriptor` digest validation was broken when dealing with non-unicode content with python 3 (:trac:`8755`)
   * The :class:`~stem.control.Controller` use of cached content wasn't thread safe (:trac:`8607`)
+  * Added :func:`~stem.control.Controller.get_pid` method to the:class:`~stem.control.Controller`
 
  * **Descriptors**
 
diff --git a/stem/control.py b/stem/control.py
index 4d416e8..0062007 100644
--- a/stem/control.py
+++ b/stem/control.py
@@ -996,9 +996,9 @@ class Controller(BaseController):
 
   def get_pid(self, default = UNDEFINED):
     """
-    Provides the process id of tor. This only works if tor is running locally.
-    Also, most of its checks are platform dependent, and hence are not entirely
-    reliable.
+    Provides the process id of tor. This often only works if tor is running
+    locally. Also, most of its checks are platform dependent, and hence are not
+    entirely reliable.
 
     :param object default: response if the query fails
 
@@ -1008,29 +1008,34 @@ class Controller(BaseController):
       provided
     """
 
-    if not self.get_socket().is_localhost():
-      if default == UNDEFINED:
-        raise ValueError("Tor isn't running locally")
-      else:
-        return default
-
     pid = self._get_cache("pid")
 
     if not pid:
+      # this is the only pid resolution method that works remotely
+
       getinfo_pid = self.get_info("process/pid", None)
 
       if getinfo_pid and getinfo_pid.isdigit():
         pid = int(getinfo_pid)
 
-      if not pid:
-        pid_file_path = self.get_conf("PidFile", None)
+        if self.is_caching_enabled():
+          self._set_cache({"pid": pid})
+
+    if not pid:
+      if not self.get_socket().is_localhost():
+        if default == UNDEFINED:
+          raise ValueError("Tor isn't running locally")
+        else:
+          return default
+
+      pid_file_path = self.get_conf("PidFile", None)
 
-        if pid_file_path is not None:
-          with open(pid_file_path) as pid_file:
-            pid_file_contents = pid_file.read().strip()
+      if pid_file_path is not None:
+        with open(pid_file_path) as pid_file:
+          pid_file_contents = pid_file.read().strip()
 
-            if pid_file_contents.isdigit():
-              pid = int(pid_file_contents)
+          if pid_file_contents.isdigit():
+            pid = int(pid_file_contents)
 
       if not pid:
         pid = stem.util.system.get_pid_by_name('tor')
diff --git a/stem/util/system.py b/stem/util/system.py
index ea8513f..3e72d83 100644
--- a/stem/util/system.py
+++ b/stem/util/system.py
@@ -22,6 +22,7 @@ best-effort, providing **None** if the lookup fails.
   get_cwd - provides the current working directory for a given process
   get_start_time - provides the unix timestamp when the process started
   get_bsd_jail_id - provides the BSD jail id a given process is running within
+  get_bsd_jail_path - provides the path of the given BSD jail
   expand_path - expands relative paths and ~ entries
   call - runs the given system command and provides back the results
 





More information about the tor-commits mailing list