diff options
-rw-r--r-- | changes/descriptor_limit | 2 | ||||
-rw-r--r-- | src/common/compat.c | 17 | ||||
-rw-r--r-- | src/or/control.c | 23 |
3 files changed, 18 insertions, 24 deletions
diff --git a/changes/descriptor_limit b/changes/descriptor_limit new file mode 100644 index 000000000..29be3d981 --- /dev/null +++ b/changes/descriptor_limit @@ -0,0 +1,2 @@ + o Code simplification and refactoring: + - Remove duplicate code for invoking getrlimit() from control.c. diff --git a/src/common/compat.c b/src/common/compat.c index 7dfc6885e..06eb5839f 100644 --- a/src/common/compat.c +++ b/src/common/compat.c @@ -1258,13 +1258,16 @@ tor_socketpair(int family, int type, int protocol, tor_socket_t fd[2]) * tell Tor it's allowed to use. */ #define ULIMIT_BUFFER 32 /* keep 32 extra fd's beyond _ConnLimit */ -/** Learn the maximum allowed number of file descriptors. (Some systems - * have a low soft limit. +/** Learn the maximum allowed number of file descriptors, and tell the system + * we want to use up to that number. (Some systems have a low soft limit, and + * let us set it higher.) * * We compute this by finding the largest number that we can use. * 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) @@ -1297,14 +1300,20 @@ set_max_file_descriptors(rlim_t limit, int *max_out) limit = MAX_CONNECTIONS; #else /* HAVE_GETRLIMIT */ struct rlimit rlim; - tor_assert(limit > 0); if (getrlimit(RLIMIT_NOFILE, &rlim) != 0) { log_warn(LD_NET, "Could not get maximum number of file descriptors: %s", strerror(errno)); return -1; } - + if (limit == 0) { + /* If limit == 0, return the maximum value without setting it. */ + limit = rlim.rlim_max; + if (limit > INT_MAX) + limit = INT_MAX; + *max_out = 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.", diff --git a/src/or/control.c b/src/or/control.c index 9465f895a..9fc28bb5c 100644 --- a/src/or/control.c +++ b/src/or/control.c @@ -1466,26 +1466,9 @@ getinfo_helper_misc(control_connection_t *conn, const char *question, } #endif } else if (!strcmp(question, "process/descriptor-limit")) { - /** platform specifc limits are from the set_max_file_descriptors function - * of src/common/compat.c */ - /* XXXX023 This is duplicated code from compat.c; it should turn into a - * function. */ - #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(_WIN32) - *answer = tor_strdup("15000"); - #else - *answer = tor_strdup("15000"); - #endif + int max_fds=-1; + set_max_file_descriptors(0, &max_fds); + tor_asprintf(answer, "%d", max_fds); } else if (!strcmp(question, "dir-usage")) { *answer = directory_dump_request_log(); } else if (!strcmp(question, "fingerprint")) { |