diff --git a/qse/cmd/http/httpd.c b/qse/cmd/http/httpd.c index 37e43984..7b356dcd 100644 --- a/qse/cmd/http/httpd.c +++ b/qse/cmd/http/httpd.c @@ -43,6 +43,10 @@ # include #endif +#if defined(HAVE_SYS_RESOURCE_H) +# include +#endif + /* --------------------------------------------------------------------- */ static qse_httpd_t* g_httpd = QSE_NULL; @@ -1294,6 +1298,39 @@ static int close_config_file (qse_httpd_t* httpd) return 0; } +static void set_limit (qse_httpd_t* httpd, const qse_char_t* name, int what) +{ + qse_xli_pair_t* pair; + httpd_xtn_t* httpd_xtn; + + httpd_xtn = (httpd_xtn_t*)qse_httpd_getxtnstd (httpd); + + pair = qse_xli_findpairbyname (httpd_xtn->xli, QSE_NULL, name); + if (pair && pair->val->type == QSE_XLI_STR) + { +#if defined(HAVE_GETRLIMIT) && defined(HAVE_SETRLIMIT) + struct rlimit lim; + + if (getrlimit (what, &lim) == 0) + { + const qse_char_t* str; + + str = ((qse_xli_str_t*)pair->val)->ptr; + if (qse_strcasecmp (str, QSE_T("none")) != 0) + { + if (qse_strcasecmp (str, QSE_T("unlimited")) == 0) + lim.rlim_cur = RLIM_INFINITY; + else + lim.rlim_cur = qse_strtoui (((qse_xli_str_t*)pair->val)->ptr); + if (setrlimit (what, &lim) <= -1) + { + /* TODO: warning */ + } + } + } +#endif + } +} static int load_config (qse_httpd_t* httpd) { @@ -1314,6 +1351,13 @@ static int load_config (qse_httpd_t* httpd) qse_httpd_freemem (httpd, tmp); } +#if defined(RLIMIT_NOFILE) + set_limit (httpd, QSE_T("max-nofile"), RLIMIT_NOFILE); +#endif +#if defined(RLIMIT_NPROC) + set_limit (httpd, QSE_T("max-nproc"), RLIMIT_NPROC); +#endif + for (i = 0; ; i++) { qse_char_t buf[32]; diff --git a/qse/cmd/http/httpd.conf b/qse/cmd/http/httpd.conf index 8921ce4b..57a38c86 100644 --- a/qse/cmd/http/httpd.conf +++ b/qse/cmd/http/httpd.conf @@ -5,6 +5,10 @@ # string to be used as a httpd name name = "QSEHTTPD v1"; +# none, unlimited, or positive number +max-nofile = none; +max-nproc = none; + ################################################ # default server configuration ################################################ diff --git a/qse/configure b/qse/configure index 54a41247..fff06df5 100755 --- a/qse/configure +++ b/qse/configure @@ -17560,7 +17560,7 @@ _ACEOF fi done -for ac_func in sysconf prctl fdopendir +for ac_func in sysconf prctl fdopendir setrlimit getrlimit 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" diff --git a/qse/configure.ac b/qse/configure.ac index 069e48d4..d9fb9185 100644 --- a/qse/configure.ac +++ b/qse/configure.ac @@ -139,7 +139,7 @@ AC_CHECK_FUNCS([isblank iswblank]) AC_CHECK_FUNCS([lseek64 stat64 fstat64 lstat64 ftruncate64 readdir64]) AC_CHECK_FUNCS([timegm timelocal]) AC_CHECK_FUNCS([utime utimes]) -AC_CHECK_FUNCS([sysconf prctl fdopendir]) +AC_CHECK_FUNCS([sysconf prctl fdopendir setrlimit getrlimit]) AC_CHECK_FUNCS([backtrace backtrace_symbols]) AC_CHECK_FUNCS([fork vfork posix_spawn gettid nanosleep select]) AC_CHECK_FUNCS([makecontext swapcontext getcontext setcontext]) diff --git a/qse/include/qse/config.h.in b/qse/include/qse/config.h.in index 11ea266d..2afdfbdb 100644 --- a/qse/include/qse/config.h.in +++ b/qse/include/qse/config.h.in @@ -173,6 +173,9 @@ /* Define to 1 if you have the `getifaddrs' function. */ #undef HAVE_GETIFADDRS +/* Define to 1 if you have the `getrlimit' function. */ +#undef HAVE_GETRLIMIT + /* Define to 1 if you have the `gettid' function. */ #undef HAVE_GETTID @@ -344,6 +347,9 @@ /* Define to 1 if you have the `setcontext' function. */ #undef HAVE_SETCONTEXT +/* Define to 1 if you have the `setrlimit' function. */ +#undef HAVE_SETRLIMIT + /* Define if you have the shl_load function. */ #undef HAVE_SHL_LOAD