mirror of
https://github.com/shadowsocks/shadowsocks-libev.git
synced 2026-10-23 16:13:46 +00:00
Added support for setrlimit(2)
This commit is contained in:
@@ -151,6 +151,9 @@
|
||||
/* Define to 1 if you have the `setreuid' function. */
|
||||
#undef HAVE_SETREUID
|
||||
|
||||
/* Define to 1 if you have the `setrlimit' function. */
|
||||
#undef HAVE_SETRLIMIT
|
||||
|
||||
/* Define to 1 if you have the `signalfd' function. */
|
||||
#undef HAVE_SIGNALFD
|
||||
|
||||
|
||||
@@ -14272,7 +14272,7 @@ cat >>confdefs.h <<_ACEOF
|
||||
_ACEOF
|
||||
|
||||
|
||||
for ac_func in memset select setresuid setreuid strerror getpwnam_r
|
||||
for ac_func in memset select setresuid setreuid strerror getpwnam_r setrlimit
|
||||
do :
|
||||
as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
|
||||
ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
|
||||
|
||||
+1
-1
@@ -153,7 +153,7 @@ dnl Checks for library functions.
|
||||
AC_FUNC_FORK
|
||||
AC_FUNC_SELECT_ARGTYPES
|
||||
AC_TYPE_SIGNAL
|
||||
AC_CHECK_FUNCS([memset select setresuid setreuid strerror getpwnam_r])
|
||||
AC_CHECK_FUNCS([memset select setresuid setreuid strerror getpwnam_r setrlimit])
|
||||
|
||||
dnl Check for select() into ws2_32 for Msys/Mingw
|
||||
if test "$ac_cv_func_select" != "yes"; then
|
||||
|
||||
@@ -183,6 +183,10 @@ jconf_t *read_jconf(const char* file)
|
||||
{
|
||||
conf.fast_open = value->u.boolean;
|
||||
}
|
||||
else if (strcmp(name, "nofile") == 0)
|
||||
{
|
||||
conf.nofile = value->u.integer;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
|
||||
@@ -44,6 +44,7 @@ typedef struct
|
||||
char *method;
|
||||
char *timeout;
|
||||
int fast_open;
|
||||
int nofile;
|
||||
} jconf_t;
|
||||
|
||||
jconf_t *read_jconf(const char* file);
|
||||
|
||||
+18
@@ -74,6 +74,9 @@ int acl = 0;
|
||||
int verbose = 0;
|
||||
int udprelay = 0;
|
||||
static int fast_open = 0;
|
||||
#ifdef HAVE_SETRLIMIT
|
||||
static int nofile = 0;
|
||||
#endif
|
||||
|
||||
#ifndef __MINGW32__
|
||||
static int setnonblocking(int fd)
|
||||
@@ -1025,6 +1028,21 @@ int main (int argc, char **argv)
|
||||
if (method == NULL) method = conf->method;
|
||||
if (timeout == NULL) timeout = conf->timeout;
|
||||
if (fast_open == 0) fast_open = conf->fast_open;
|
||||
#ifdef HAVE_SETRLIMIT
|
||||
if (nofile == 0) nofile = conf->nofile;
|
||||
/*
|
||||
* no need to check the return value here since we will show
|
||||
* the user an error message if setrlimit(2) fails
|
||||
*/
|
||||
if (nofile)
|
||||
{
|
||||
if (verbose)
|
||||
{
|
||||
LOGD("setting NOFILE to %d", nofile);
|
||||
}
|
||||
set_nofile(nofile);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
if (remote_num == 0 || remote_port == NULL ||
|
||||
|
||||
@@ -67,6 +67,9 @@ int udprelay = 0;
|
||||
#ifdef TCP_FASTOPEN
|
||||
static int fast_open = 0;
|
||||
#endif
|
||||
#ifdef HAVE_SETRLIMIT
|
||||
static int nofile = 0;
|
||||
#endif
|
||||
static int remote_conn = 0;
|
||||
static int server_conn = 0;
|
||||
|
||||
@@ -1048,6 +1051,21 @@ int main (int argc, char **argv)
|
||||
if (timeout == NULL) timeout = conf->timeout;
|
||||
#ifdef TCP_FASTOPEN
|
||||
if (fast_open == 0) fast_open = conf->fast_open;
|
||||
#endif
|
||||
#ifdef HAVE_SETRLIMIT
|
||||
if (nofile == 0) nofile = conf->nofile;
|
||||
/*
|
||||
* no need to check the return value here since we will show
|
||||
* the user an error message if setrlimit(2) fails
|
||||
*/
|
||||
if (nofile)
|
||||
{
|
||||
if (verbose)
|
||||
{
|
||||
LOGD("setting NOFILE to %d", nofile);
|
||||
}
|
||||
set_nofile(nofile);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
+38
@@ -37,6 +37,11 @@
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_SETRLIMIT
|
||||
#include <sys/time.h>
|
||||
#include <sys/resource.h>
|
||||
#endif
|
||||
|
||||
#define INT_DIGITS 19 /* enough for 64 bit integer */
|
||||
|
||||
#ifdef HAS_SYSLOG
|
||||
@@ -275,3 +280,36 @@ void daemonize(const char* path)
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef HAVE_SETRLIMIT
|
||||
int set_nofile(int nofile)
|
||||
{
|
||||
struct rlimit limit = {nofile, nofile}; /* set both soft and hard limit */
|
||||
|
||||
if (nofile <= 0)
|
||||
{
|
||||
FATAL("nofile must be greater than 0\n");
|
||||
}
|
||||
|
||||
if (setrlimit(RLIMIT_NOFILE, &limit) < 0)
|
||||
{
|
||||
if (errno == EPERM)
|
||||
{
|
||||
LOGE("insufficient permission to change NOFILE, not starting as root?");
|
||||
return -1;
|
||||
}
|
||||
else if (errno == EINVAL)
|
||||
{
|
||||
LOGE("invalid fileno, decrease nofile and try again");
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
LOGE("setrlimit failed: %s", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
@@ -124,5 +124,8 @@ void FATAL(const char *msg);
|
||||
void usage(void);
|
||||
void daemonize(const char* path);
|
||||
char *ss_strndup(const char *s, size_t n);
|
||||
#ifdef HAVE_SETRLIMIT
|
||||
int set_nofile(int nofile);
|
||||
#endif
|
||||
|
||||
#endif // _UTILS_H
|
||||
|
||||
Reference in New Issue
Block a user