diff --git a/src/pikiwidb.cc b/src/pikiwidb.cc index 8a17d4b76..5108e8d9d 100644 --- a/src/pikiwidb.cc +++ b/src/pikiwidb.cc @@ -11,6 +11,7 @@ #include "pikiwidb.h" #include +#include #include #include #include @@ -216,6 +217,29 @@ static void InitLogs() { #endif } +static int InitLimit() { + rlimit limit; + rlim_t maxfiles = g_config.max_clients; + if (getrlimit(RLIMIT_NOFILE, &limit) == -1) { + WARN("getrlimit error: {}", strerror(errno)); + } else if (limit.rlim_cur < maxfiles) { + rlim_t old_limit = limit.rlim_cur; + limit.rlim_cur = maxfiles; + limit.rlim_max = maxfiles; + if (setrlimit(RLIMIT_NOFILE, &limit) != -1) { + WARN("your 'limit -n ' of {} is not enough for PikiwiDB to start. PikiwiDB have successfully reconfig it to ", + old_limit, limit.rlim_cur); + } else { + ERROR( + "your 'limit -n ' of {} is not enough for PikiwiDB to start." + " PikiwiDB can not reconfig it({}), do it by yourself", + old_limit, strerror(errno)); + return -1; + } + } + return 0; +} + static void daemonize() { if (fork()) { exit(0); /* parent exits */ @@ -258,6 +282,7 @@ int main(int ac, char* av[]) { daemonize(); } + InitLimit(); pstd::InitRandom(); SignalSetup(); InitLogs();