diff options
author | Roger Dingledine <arma@torproject.org> | 2005-02-22 23:52:44 +0000 |
---|---|---|
committer | Roger Dingledine <arma@torproject.org> | 2005-02-22 23:52:44 +0000 |
commit | 55c3f15ed1334fc4f63e04c4edc4bb2340623fac (patch) | |
tree | 73f97c3898cbcb049bb50d895c4260925ae22b64 /src/common/compat.c | |
parent | 6e6d95b3db091d4ae107c8d4f03354ccadd20757 (diff) | |
download | tor-55c3f15ed1334fc4f63e04c4edc4bb2340623fac.tar.gz tor-55c3f15ed1334fc4f63e04c4edc4bb2340623fac.zip |
Forward-port the trick to bump up ConnLimit as far as it can
go, but not past MAXCONNECTIONS. Also throw away the FAKE_POLL
caveats since 0.1.0 doesn't need it any more.
svn:r3657
Diffstat (limited to 'src/common/compat.c')
-rw-r--r-- | src/common/compat.c | 34 |
1 files changed, 24 insertions, 10 deletions
diff --git a/src/common/compat.c b/src/common/compat.c index 095f1b2f71..7667dd3541 100644 --- a/src/common/compat.c +++ b/src/common/compat.c @@ -357,37 +357,51 @@ tor_socketpair(int family, int type, int protocol, int fd[2]) #endif } +#define ULIMIT_BUFFER 32 /* keep 32 extra fd's beyond _ConnLimit */ + /** Get the maximum allowed number of file descriptors. (Some systems * have a low soft limit.) Make sure we set it to at least - * <b>required_min</b>. Return 0 if we can, or -1 if we fail. */ -int set_max_file_descriptors(unsigned int required_min) { + * <b>*limit</b>. Return a new limit if we can, or -1 if we fail. */ +int set_max_file_descriptors(int limit, int cap) { #ifndef HAVE_GETRLIMIT log_fn(LOG_INFO,"This platform is missing getrlimit(). Proceeding."); - return 0; /* hope we'll be ok */ + if (limit > cap) { + log(LOG_INFO, "ConnLimit must be at most %d. Capping it.", cap); + limit = cap; + } #else struct rlimit rlim; + int most; if (getrlimit(RLIMIT_NOFILE, &rlim) != 0) { log_fn(LOG_WARN, "Could not get maximum number of file descriptors: %s", strerror(errno)); return -1; } - if (required_min > rlim.rlim_max) { - log_fn(LOG_WARN,"We need %u file descriptors available, and we're limited to %lu. Please change your ulimit.", required_min, (unsigned long int)rlim.rlim_max); + if (rlim.rlim_max < limit) { + log_fn(LOG_WARN,"We need %d file descriptors available, and we're limited to %lu. Please change your ulimit -n.", limit, (unsigned long int)rlim.rlim_max); return -1; } - if (required_min > rlim.rlim_cur) { - log_fn(LOG_INFO,"Raising max file descriptors from %lu to %lu.", - (unsigned long int)rlim.rlim_cur, (unsigned long int)rlim.rlim_max); + most = ((rlim.rlim_max > cap) ? cap : rlim.rlim_max); + if (most > rlim.rlim_cur) { + log_fn(LOG_INFO,"Raising max file descriptors from %lu to %d.", + (unsigned long int)rlim.rlim_cur, most); } - rlim.rlim_cur = rlim.rlim_max; + rlim.rlim_cur = most; if (setrlimit(RLIMIT_NOFILE, &rlim) != 0) { log_fn(LOG_WARN, "Could not set maximum number of file descriptors: %s", strerror(errno)); return -1; } - return 0; + /* leave some overhead for logs, etc, */ + limit = most; #endif + + if (limit < ULIMIT_BUFFER) { + log_fn(LOG_WARN,"ConnLimit must be at least %d. Failing.", ULIMIT_BUFFER); + return -1; + } + return limit - ULIMIT_BUFFER; } /** Call setuid and setgid to run as <b>user</b>:<b>group</b>. Return 0 on |