1
0
Fork 0
mirror of https://github.com/warmcat/libwebsockets.git synced 2025-03-09 00:00:04 +01:00

context: if linux and fd_limit_per_thread set try to configure by setrlimit

This commit is contained in:
Andy Green 2020-05-06 07:41:16 +01:00
parent f1f27562a0
commit 641831b3c5
4 changed files with 32 additions and 2 deletions

View file

@ -737,6 +737,11 @@ struct lws_context_creation_info {
* ss_proxy_bind and the given port */
#endif
int rlimit_nofile;
/**< 0 = inherit the initial ulimit for files / sockets from the startup
* environment. Nonzero = try to set the limit for this process.
*/
/* Add new things just above here ---^
* This is part of the ABI, don't needlessly break compatibility
*

View file

@ -30,6 +30,11 @@
static const char *library_version = LWS_LIBRARY_VERSION " " LWS_BUILD_HASH;
#if defined(__linux__)
/* for setrlimit */
#include <sys/resource.h>
#endif
#if defined(LWS_WITH_NETWORK)
/* in ms */
static uint32_t default_backoff_table[] = { 1000, 3000, 9000, 17000 };
@ -411,6 +416,20 @@ lws_create_context(const struct lws_context_creation_info *info)
context->options = info->options;
#if !defined(LWS_PLAT_FREERTOS) && !defined(LWS_PLAT_OPTEE) && !defined(WIN32)
/*
* If asked, try to set the rlimit / ulimit for process sockets / files.
* We read the effective limit in a moment, so we will find out the
* real limit according to system constraints then.
*/
if (info->rlimit_nofile) {
struct rlimit rl;
rl.rlim_cur = rl.rlim_max = info->rlimit_nofile;
setrlimit(RLIMIT_NOFILE, &rl);
}
#endif
#ifndef LWS_NO_DAEMONIZE
if (pid_daemon) {
context->started_with_parent = pid_daemon;

View file

@ -126,8 +126,8 @@ lws_plat_init(struct lws_context *context,
#endif
context->fd_random = fd;
if (context->fd_random < 0) {
lwsl_err("Unable to open random device %s %d\n",
SYSTEM_RANDOM_FILEPATH, context->fd_random);
lwsl_err("Unable to open random device %s %d, errno %d\n",
SYSTEM_RANDOM_FILEPATH, context->fd_random, errno);
return 1;
}

View file

@ -47,6 +47,7 @@ static const char * const paths_global[] = {
"global.default-alpn",
"global.ip-limit-ah",
"global.ip-limit-wsi",
"global.rlimit-nofile",
};
enum lejp_global_paths {
@ -65,6 +66,7 @@ enum lejp_global_paths {
LWJPGP_DEFAULT_ALPN,
LWJPGP_IP_LIMIT_AH,
LWJPGP_IP_LIMIT_WSI,
LWJPGP_FD_LIMIT_PT,
};
static const char * const paths_vhosts[] = {
@ -346,6 +348,10 @@ lejp_globals_cb(struct lejp_ctx *ctx, char reason)
a->info->ip_limit_wsi = atoi(ctx->buf);
return 0;
case LWJPGP_FD_LIMIT_PT:
a->info->rlimit_nofile = atoi(ctx->buf);
return 0;
default:
return 0;
}