[or-cvs] [tor/master 1/2] Implementing getinfo options for the pid, uid, user, and descriptor limit as per proposal 173.

nickm at torproject.org nickm at torproject.org
Mon Jan 3 17:09:19 UTC 2011


Author: Damian Johnson <atagar at torproject.org>
Date: Wed, 22 Dec 2010 09:15:24 -0800
Subject: Implementing getinfo options for the pid, uid, user, and descriptor limit as per proposal 173.
Commit: 8708ffa655820df9db587228a6a60a9d1ccbf039

---
 doc/spec/control-spec.txt |    9 +++++++
 src/or/control.c          |   58 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 67 insertions(+), 0 deletions(-)

diff --git a/doc/spec/control-spec.txt b/doc/spec/control-spec.txt
index e8a314c..45fa3e7 100644
--- a/doc/spec/control-spec.txt
+++ b/doc/spec/control-spec.txt
@@ -566,6 +566,15 @@
     "next-circuit/IP:port"
       XXX todo.
 
+    "process/pid" -- Process id belonging to the main tor process.
+    "process/uid" -- User id running the tor process, -1 if unknown (this is
+     unimplemented on Windows, returning -1).
+    "process/user" -- Username under which the tor process is running,
+     providing an empty string if none exists (this is unimplemented on
+     Windows, returning an empty string).
+    "process/descriptor-limit" -- Upper bound on the file descriptor limit, -1
+     if unknown.
+
     "dir/status-vote/current/consensus" [added in Tor 0.2.1.6-alpha]
     "dir/status/authority"
     "dir/status/fp/<F>"
diff --git a/src/or/control.c b/src/or/control.c
index 22f4263..e4ab387 100644
--- a/src/or/control.c
+++ b/src/or/control.c
@@ -33,6 +33,11 @@
 #include "routerlist.h"
 #include "routerparse.h"
 
+#ifndef MS_WINDOWS
+#include <pwd.h>
+#include <sys/resource.h>
+#endif
+
 /** Yield true iff <b>s</b> is the state of a control_connection_t that has
  * finished authentication and is accepting commands. */
 #define STATE_IS_OPEN(s) ((s) == CONTROL_CONN_STATE_OPEN)
@@ -1345,6 +1350,55 @@ getinfo_helper_misc(control_connection_t *conn, const char *question,
       return -1;
     }
     *answer = tor_dup_ip(addr);
+  } else if (!strcmp(question, "process/pid")) {
+    int myPid = -1;
+
+    #ifdef MS_WINDOWS
+      myPid = _getpid();
+    #else
+      myPid = getpid();
+    #endif
+
+    tor_asprintf(answer, U64_FORMAT, U64_PRINTF_ARG(myPid));
+  } else if (!strcmp(question, "process/uid")) {
+    #ifdef MS_WINDOWS
+      *answer = tor_strdup("-1");
+    #else
+      int myUid = geteuid();
+      tor_asprintf(answer, U64_FORMAT, U64_PRINTF_ARG(myUid));
+    #endif
+  } else if (!strcmp(question, "process/user")) {
+    #ifdef MS_WINDOWS
+      *answer = tor_strdup("");
+    #else
+      int myUid = geteuid();
+      struct passwd *myPwEntry = getpwuid(myUid);
+
+      if (myPwEntry) {
+        *answer = tor_strdup(myPwEntry->pw_name);
+      } else {
+        *answer = tor_strdup("");
+      }
+    #endif
+  } else if (!strcmp(question, "process/descriptor-limit")) {
+    /** platform specifc limits are from the set_max_file_descriptors function
+      * of src/common/compat.c */
+    #ifdef HAVE_GETRLIMIT
+      struct rlimit descriptorLimit;
+
+      if (getrlimit(RLIMIT_NOFILE, &descriptorLimit) == 0) {
+        tor_asprintf(answer, U64_FORMAT,
+        U64_PRINTF_ARG(descriptorLimit.rlim_max));
+      } else {
+        *answer = tor_strdup("-1");
+      }
+    #elif defined(CYGWIN) || defined(__CYGWIN__)
+      *answer = tor_strdup("3200");
+    #elif defined(MS_WINDOWS)
+      *answer = tor_strdup("15000");
+    #else
+      *answer = tor_strdup("15000");
+    #endif
   } else if (!strcmp(question, "dir-usage")) {
     *answer = directory_dump_request_log();
   } else if (!strcmp(question, "fingerprint")) {
@@ -1902,6 +1956,10 @@ static const getinfo_item_t getinfo_items[] = {
       "Number of versioning authorities agreeing on the status of the "
       "current version"),
   ITEM("address", misc, "IP address of this Tor host, if we can guess it."),
+  ITEM("process/pid", misc, "Process id belonging to the main tor process."),
+  ITEM("process/uid", misc, "User id running the tor process."),
+  ITEM("process/user", misc,"Username under which the tor process is running."),
+  ITEM("process/descriptor-limit", misc, "File descriptor limit."),
   ITEM("dir-usage", misc, "Breakdown of bytes transferred over DirPort."),
   PREFIX("desc-annotations/id/", dir, "Router annotations by hexdigest."),
   PREFIX("dir/server/", dir,"Router descriptors as retrieved from a DirPort."),
-- 
1.7.1




More information about the tor-commits mailing list