Browse Source

Merge pull request #89 from dndx/master

Added ability to set open file descriptor limit at run-time
pull/100/head
Max Lv 10 years ago
parent
commit
dc9f18a2d8
9 changed files with 87 additions and 2 deletions
  1. 3
      config.h.in
  2. 2
      configure
  3. 2
      configure.ac
  4. 4
      src/jconf.c
  5. 1
      src/jconf.h
  6. 18
      src/local.c
  7. 18
      src/server.c
  8. 38
      src/utils.c
  9. 3
      src/utils.h

3
config.h.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

2
configure

@ -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"

2
configure.ac

@ -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

4
src/jconf.c

@ -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

1
src/jconf.h

@ -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
src/local.c

@ -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 ||

18
src/server.c

@ -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
src/utils.c

@ -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 nofile, decrease nofile and try again");
return -1;
}
else
{
LOGE("setrlimit failed: %s", strerror(errno));
return -1;
}
}
return 0;
}
#endif

3
src/utils.h

@ -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
Loading…
Cancel
Save