]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Add get_max_sockets() and remove dead code
authorDavid Goulet <dgoulet@ev0ke.net>
Thu, 30 Jul 2015 18:43:15 +0000 (14:43 -0400)
committerDavid Goulet <dgoulet@ev0ke.net>
Thu, 30 Jul 2015 19:21:12 +0000 (15:21 -0400)
The control port was using set_max_file_descriptors() with a limit set to 0
to query the number of maximum socket Tor can use. With the recent changes
to that function, a check was introduced to make sure a user can not set a
value below the amount we reserved for non socket.

This commit adds get_max_sockets() that returns the value of max_sockets so
we can stop using that "setter" function to get the current value.

Finally, the dead code is removed that is the code that checked for limit
equal to 0. From now on, set_max_file_descriptors() should never be used
with a limit set to 0 for a valid use case.

Fixes #16697

Signed-off-by: David Goulet <dgoulet@ev0ke.net>
changes/bug16697 [new file with mode: 0644]
src/common/compat.c
src/common/compat.h
src/or/control.c

diff --git a/changes/bug16697 b/changes/bug16697
new file mode 100644 (file)
index 0000000..ae6cf2b
--- /dev/null
@@ -0,0 +1,10 @@
+  o Minor bugfixes
+    - Control port was using set_max_file_descriptors() with a limit set to
+      0 to get the max value. A recent fix made this use case return an
+      error and introduced dead code in that function. This triggered a
+      warning that our limit (ConnLimit) was invalid but in reality it was
+      not.
+      
+      Now, to the control port uses a specific getter function to query the
+      value and set_max_file_descriptors() should never be used again for
+      that purpose. Fixes #16697; bugfix on 0.2.7.2-alpha.
index 4930c05c2ac4bcebbb25afeb0df01c53553f7c1b..306081754ef60a137d5b0b366b1f0b56041b56e4 100644 (file)
@@ -1589,6 +1589,13 @@ tor_ersatz_socketpair(int family, int type, int protocol, tor_socket_t fd[2])
 }
 #endif
 
+/* Return the maximum number of allowed sockets. */
+int
+get_max_sockets(void)
+{
+  return max_sockets;
+}
+
 /** Number of extra file descriptors to keep in reserve beyond those that we
  * tell Tor it's allowed to use. */
 #define ULIMIT_BUFFER 32 /* keep 32 extra fd's beyond ConnLimit_ */
@@ -1601,8 +1608,6 @@ tor_ersatz_socketpair(int family, int type, int protocol, tor_socket_t fd[2])
  * If we can't find a number greater than or equal to <b>limit</b>,
  * then we fail: return -1.
  *
- * If <b>limit</b> is 0, then do not adjust the current maximum.
- *
  * Otherwise, return 0 and store the maximum we found inside <b>max_out</b>.*/
 int
 set_max_file_descriptors(rlim_t limit, int *max_out)
@@ -1647,17 +1652,6 @@ set_max_file_descriptors(rlim_t limit, int *max_out)
              strerror(errno));
     return -1;
   }
-  if (limit == 0) {
-    /* XXXX DEAD CODE We can't reach this point, since the first "if" in this
-     * function increases limit if it started out less than ULIMIT_BUFFER */
-
-    /* If limit == 0, return the maximum value without setting it. */
-    limit = rlim.rlim_max;
-    if (limit > INT_MAX)
-      limit = INT_MAX;
-    *max_out = max_sockets = (int)limit - ULIMIT_BUFFER;
-    return 0;
-  }
   if (rlim.rlim_max < limit) {
     log_warn(LD_CONFIG,"We need %lu file descriptors available, and we're "
              "limited to %lu. Please change your ulimit -n.",
index 68985d8554962f9710b47a8147301fc8ffabe316..3247a598786ae9f4b4a9d5db63f6f02124117988 100644 (file)
@@ -617,6 +617,7 @@ set_uint8(void *cp, uint8_t v)
 #if !defined(HAVE_RLIM_T)
 typedef unsigned long rlim_t;
 #endif
+int get_max_sockets(void);
 int set_max_file_descriptors(rlim_t limit, int *max);
 int tor_disable_debugger_attach(void);
 int switch_id(const char *user);
index 7a113f2c1cb92dd8e2e43a1b7f108a222fa8ee59..56f179c394a5013f4537de736771f81d9eedd414 100644 (file)
@@ -1541,8 +1541,7 @@ getinfo_helper_misc(control_connection_t *conn, const char *question,
       }
     #endif
   } else if (!strcmp(question, "process/descriptor-limit")) {
-    int max_fds=-1;
-    set_max_file_descriptors(0, &max_fds);
+    int max_fds = get_max_sockets();
     tor_asprintf(answer, "%d", max_fds);
   } else if (!strcmp(question, "limits/max-mem-in-queues")) {
     tor_asprintf(answer, U64_FORMAT,