Added support for setrlimit(2)

This commit is contained in:
Datong Sun
2014-07-14 01:49:24 -05:00
parent 0017b7c198
commit 1d8719f2e4
9 changed files with 87 additions and 2 deletions
+38
View File
@@ -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