2018-06-16 09:37:07 +08:00
|
|
|
/*
|
|
|
|
* libwebsockets - small server side websockets and web server implementation
|
|
|
|
*
|
2019-08-14 10:44:14 +01:00
|
|
|
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
|
2018-06-16 09:37:07 +08:00
|
|
|
*
|
2019-08-14 10:44:14 +01:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2018-06-16 09:37:07 +08:00
|
|
|
*
|
2019-08-14 10:44:14 +01:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2018-06-16 09:37:07 +08:00
|
|
|
*
|
2019-08-14 10:44:14 +01:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
2018-06-16 09:37:07 +08:00
|
|
|
*/
|
|
|
|
|
2019-12-22 03:38:05 +00:00
|
|
|
#if !defined(_GNU_SOURCE)
|
2018-06-16 09:37:07 +08:00
|
|
|
#define _GNU_SOURCE
|
2019-12-22 03:38:05 +00:00
|
|
|
#endif
|
2019-08-15 10:49:52 +01:00
|
|
|
#include "private-lib-core.h"
|
2018-06-16 09:37:07 +08:00
|
|
|
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <grp.h>
|
|
|
|
|
|
|
|
#ifdef LWS_WITH_PLUGINS
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#endif
|
|
|
|
#include <dirent.h>
|
|
|
|
|
2019-08-09 10:12:09 +01:00
|
|
|
#if defined(LWS_WITH_NETWORK)
|
|
|
|
static void
|
|
|
|
lws_sul_plat_unix(lws_sorted_usec_list_t *sul)
|
|
|
|
{
|
|
|
|
struct lws_context_per_thread *pt =
|
|
|
|
lws_container_of(sul, struct lws_context_per_thread, sul_plat);
|
|
|
|
struct lws_context *context = pt->context;
|
2020-11-16 19:32:58 +00:00
|
|
|
int n = 0, m = 0;
|
2019-08-09 10:12:09 +01:00
|
|
|
|
|
|
|
#if !defined(LWS_NO_DAEMONIZE)
|
|
|
|
/* if our parent went down, don't linger around */
|
|
|
|
if (pt->context->started_with_parent &&
|
|
|
|
kill(pt->context->started_with_parent, 0) < 0)
|
|
|
|
kill(getpid(), SIGTERM);
|
|
|
|
#endif
|
|
|
|
|
2020-11-16 19:32:58 +00:00
|
|
|
for (n = 0; n < context->count_threads; n++)
|
2021-01-19 14:19:12 +00:00
|
|
|
m = m | (int)pt->fds_count;
|
2020-11-16 19:32:58 +00:00
|
|
|
|
|
|
|
if (context->deprecated && !m) {
|
2019-08-09 10:12:09 +01:00
|
|
|
lwsl_notice("%s: ending deprecated context\n", __func__);
|
|
|
|
kill(getpid(), SIGINT);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-18 10:35:43 +01:00
|
|
|
#if defined(LWS_WITH_SERVER)
|
2019-08-09 10:12:09 +01:00
|
|
|
lws_context_lock(context, "periodic checks");
|
|
|
|
lws_start_foreach_llp(struct lws_vhost **, pv,
|
|
|
|
context->no_listener_vhost_list) {
|
|
|
|
struct lws_vhost *v = *pv;
|
|
|
|
lwsl_debug("deferred iface: checking if on vh %s\n", (*pv)->name);
|
|
|
|
if (_lws_vhost_init_server(NULL, *pv) == 0) {
|
|
|
|
/* became happy */
|
|
|
|
lwsl_notice("vh %s: became connected\n", v->name);
|
|
|
|
*pv = v->no_listener_vhost_list;
|
|
|
|
v->no_listener_vhost_list = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} lws_end_foreach_llp(pv, no_listener_vhost_list);
|
|
|
|
lws_context_unlock(context);
|
2019-08-18 10:35:43 +01:00
|
|
|
#endif
|
2019-08-09 10:12:09 +01:00
|
|
|
|
2020-05-28 12:48:17 +01:00
|
|
|
__lws_sul_insert_us(&pt->pt_sul_owner[LWSSULLI_MISS_IF_SUSPENDED],
|
|
|
|
&pt->sul_plat, 30 * LWS_US_PER_SEC);
|
2019-08-09 10:12:09 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-08-27 15:37:14 +01:00
|
|
|
#if defined(LWS_WITH_PLUGINS)
|
2020-08-27 09:34:35 +01:00
|
|
|
static int
|
|
|
|
protocol_plugin_cb(struct lws_plugin *pin, void *each_user)
|
|
|
|
{
|
|
|
|
struct lws_context *context = (struct lws_context *)each_user;
|
|
|
|
const lws_plugin_protocol_t *plpr =
|
|
|
|
(const lws_plugin_protocol_t *)pin->hdr;
|
|
|
|
|
2020-12-12 06:21:40 +00:00
|
|
|
context->plugin_protocol_count = (short)(context->plugin_protocol_count +
|
|
|
|
plpr->count_protocols);
|
|
|
|
context->plugin_extension_count = (short)(context->plugin_extension_count +
|
|
|
|
plpr->count_extensions);
|
2020-08-27 09:34:35 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2020-08-27 15:37:14 +01:00
|
|
|
#endif
|
2020-08-27 09:34:35 +01:00
|
|
|
|
2018-06-16 09:37:07 +08:00
|
|
|
int
|
|
|
|
lws_plat_init(struct lws_context *context,
|
|
|
|
const struct lws_context_creation_info *info)
|
|
|
|
{
|
|
|
|
int fd;
|
2019-01-13 06:58:21 +08:00
|
|
|
#if defined(LWS_WITH_NETWORK)
|
unix plat: add minimal wsi fd map option
An lws context usually contains a processwide fd -> wsi lookup table.
This allows any possible fd returned by a *nix type OS to be immediately
converted to a wsi just by indexing an array of struct lws * the size of
the highest possible fd, as found by ulimit -n or similar.
This works modestly for Linux type systems where the default ulimit -n for
a process is 1024, it means a 4KB or 8KB lookup table for 32-bit or
64-bit systems.
However in the case your lws usage is much simpler, like one outgoing
client connection and no serving, this represents increasing waste. It's
made much worse if the system has a much larger default ulimit -n, eg 1M,
the table is occupying 4MB or 8MB, of which you will only use one.
Even so, because lws can't be sure the OS won't return a socket fd at any
number up to (ulimit -n - 1), it has to allocate the whole lookup table
at the moment.
This patch looks to see if the context creation info is setting
info->fd_limit_per_thread... if it leaves it at the default 0, then
everything is as it was before this patch. However if finds that
(info->fd_limit_per_thread * actual_number_of_service_threads) where
the default number of service threads is 1, is less than the fd limit
set by ulimit -n, lws switches to a slower lookup table scheme, which
only allocates the requested number of slots. Lookups happen then by
iterating the table and comparing rather than indexing the array
directly, which is obviously somewhat of a performance hit.
However in the case where you know lws will only have a very few wsi
maximum, this method can very usefully trade off speed to be able to
avoid the allocation sized by ulimit -n.
minimal examples for client that can make use of this are also modified
by this patch to use the smaller context allocations.
2019-05-17 01:20:07 +01:00
|
|
|
/*
|
2020-10-11 07:43:46 +01:00
|
|
|
* context has the process-global fd lookup array. This can be
|
unix plat: add minimal wsi fd map option
An lws context usually contains a processwide fd -> wsi lookup table.
This allows any possible fd returned by a *nix type OS to be immediately
converted to a wsi just by indexing an array of struct lws * the size of
the highest possible fd, as found by ulimit -n or similar.
This works modestly for Linux type systems where the default ulimit -n for
a process is 1024, it means a 4KB or 8KB lookup table for 32-bit or
64-bit systems.
However in the case your lws usage is much simpler, like one outgoing
client connection and no serving, this represents increasing waste. It's
made much worse if the system has a much larger default ulimit -n, eg 1M,
the table is occupying 4MB or 8MB, of which you will only use one.
Even so, because lws can't be sure the OS won't return a socket fd at any
number up to (ulimit -n - 1), it has to allocate the whole lookup table
at the moment.
This patch looks to see if the context creation info is setting
info->fd_limit_per_thread... if it leaves it at the default 0, then
everything is as it was before this patch. However if finds that
(info->fd_limit_per_thread * actual_number_of_service_threads) where
the default number of service threads is 1, is less than the fd limit
set by ulimit -n, lws switches to a slower lookup table scheme, which
only allocates the requested number of slots. Lookups happen then by
iterating the table and comparing rather than indexing the array
directly, which is obviously somewhat of a performance hit.
However in the case where you know lws will only have a very few wsi
maximum, this method can very usefully trade off speed to be able to
avoid the allocation sized by ulimit -n.
minimal examples for client that can make use of this are also modified
by this patch to use the smaller context allocations.
2019-05-17 01:20:07 +01:00
|
|
|
* done two different ways now; one or the other is done depending on if
|
|
|
|
* info->fd_limit_per_thread was snonzero
|
|
|
|
*
|
|
|
|
* - default: allocate a worst-case lookup array sized for ulimit -n
|
|
|
|
* and use the fd directly as an index into it
|
|
|
|
*
|
|
|
|
* - slow: allocate context->max_fds entries only (which can be
|
|
|
|
* forced at context creation time to be
|
|
|
|
* info->fd_limit_per_thread * the number of threads)
|
|
|
|
* and search the array to lookup fds
|
|
|
|
*
|
|
|
|
* the default way is optimized for server, if you only use one or two
|
|
|
|
* client wsi the slow way may save a lot of memory.
|
|
|
|
*
|
|
|
|
* Both ways allocate an array of struct lws *... one allocates it for
|
|
|
|
* all possible fd indexes the process could produce and uses it as a
|
|
|
|
* map, the other allocates for an amount of wsi the lws context is
|
|
|
|
* expected to use and searches through it to manipulate it.
|
|
|
|
*/
|
|
|
|
|
2018-06-16 09:37:07 +08:00
|
|
|
context->lws_lookup = lws_zalloc(sizeof(struct lws *) *
|
|
|
|
context->max_fds, "lws_lookup");
|
unix plat: add minimal wsi fd map option
An lws context usually contains a processwide fd -> wsi lookup table.
This allows any possible fd returned by a *nix type OS to be immediately
converted to a wsi just by indexing an array of struct lws * the size of
the highest possible fd, as found by ulimit -n or similar.
This works modestly for Linux type systems where the default ulimit -n for
a process is 1024, it means a 4KB or 8KB lookup table for 32-bit or
64-bit systems.
However in the case your lws usage is much simpler, like one outgoing
client connection and no serving, this represents increasing waste. It's
made much worse if the system has a much larger default ulimit -n, eg 1M,
the table is occupying 4MB or 8MB, of which you will only use one.
Even so, because lws can't be sure the OS won't return a socket fd at any
number up to (ulimit -n - 1), it has to allocate the whole lookup table
at the moment.
This patch looks to see if the context creation info is setting
info->fd_limit_per_thread... if it leaves it at the default 0, then
everything is as it was before this patch. However if finds that
(info->fd_limit_per_thread * actual_number_of_service_threads) where
the default number of service threads is 1, is less than the fd limit
set by ulimit -n, lws switches to a slower lookup table scheme, which
only allocates the requested number of slots. Lookups happen then by
iterating the table and comparing rather than indexing the array
directly, which is obviously somewhat of a performance hit.
However in the case where you know lws will only have a very few wsi
maximum, this method can very usefully trade off speed to be able to
avoid the allocation sized by ulimit -n.
minimal examples for client that can make use of this are also modified
by this patch to use the smaller context allocations.
2019-05-17 01:20:07 +01:00
|
|
|
|
|
|
|
if (!context->lws_lookup) {
|
2021-06-18 07:28:23 +01:00
|
|
|
lwsl_cx_err(context, "OOM on alloc lws_lookup array for %d conn",
|
|
|
|
context->max_fds);
|
2018-06-16 09:37:07 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-07-09 18:04:08 +01:00
|
|
|
#if defined(LWS_WITH_MBEDTLS)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
|
|
|
/* initialize platform random through mbedtls */
|
|
|
|
mbedtls_entropy_init(&context->mec);
|
|
|
|
mbedtls_ctr_drbg_init(&context->mcdc);
|
|
|
|
|
|
|
|
n = mbedtls_ctr_drbg_seed(&context->mcdc, mbedtls_entropy_func,
|
|
|
|
&context->mec, NULL, 0);
|
|
|
|
if (n)
|
|
|
|
lwsl_err("%s: mbedtls_ctr_drbg_seed() returned 0x%x\n",
|
|
|
|
__func__, n);
|
|
|
|
#if 0
|
|
|
|
else {
|
|
|
|
uint8_t rtest[16];
|
|
|
|
lwsl_notice("%s: started drbg\n", __func__);
|
|
|
|
if (mbedtls_ctr_drbg_random(&context->mcdc, rtest,
|
|
|
|
sizeof(rtest)))
|
|
|
|
lwsl_err("%s: get random failed\n", __func__);
|
|
|
|
else
|
|
|
|
lwsl_hexdump_notice(rtest, sizeof(rtest));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-06-18 07:28:23 +01:00
|
|
|
lwsl_cx_info(context, " mem: platform fd map: %5lu B",
|
2018-06-16 09:37:07 +08:00
|
|
|
(unsigned long)(sizeof(struct lws *) * context->max_fds));
|
2019-01-13 06:58:21 +08:00
|
|
|
#endif
|
2019-08-18 10:35:43 +01:00
|
|
|
#if defined(LWS_WITH_FILE_OPS)
|
2018-06-23 12:56:21 +08:00
|
|
|
fd = lws_open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
|
2019-08-18 10:35:43 +01:00
|
|
|
#else
|
|
|
|
fd = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
|
|
|
|
#endif
|
2018-06-16 09:37:07 +08:00
|
|
|
context->fd_random = fd;
|
|
|
|
if (context->fd_random < 0) {
|
2020-05-06 07:41:16 +01:00
|
|
|
lwsl_err("Unable to open random device %s %d, errno %d\n",
|
|
|
|
SYSTEM_RANDOM_FILEPATH, context->fd_random, errno);
|
2018-06-16 09:37:07 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-03-16 16:42:36 +00:00
|
|
|
#if defined(LWS_HAVE_SSL_CTX_set_keylog_callback) && !defined(__COVERITY__) && \
|
2021-12-07 08:31:17 +00:00
|
|
|
defined(LWS_WITH_TLS) && defined(LWS_WITH_CLIENT)
|
|
|
|
{
|
|
|
|
char *klf_env = getenv("SSLKEYLOGFILE");
|
2022-03-16 16:42:36 +00:00
|
|
|
size_t n = 0;
|
2021-12-07 08:31:17 +00:00
|
|
|
|
2022-03-16 16:42:36 +00:00
|
|
|
/* ... coverity taint with lws_strncpy()... */
|
|
|
|
|
|
|
|
while (klf_env && klf_env[n] &&
|
|
|
|
n < sizeof(context->keylog_file) - 1) {
|
|
|
|
context->keylog_file[n] = klf_env[n];
|
|
|
|
n++;
|
2022-02-23 14:50:49 +00:00
|
|
|
}
|
2022-03-16 16:42:36 +00:00
|
|
|
context->keylog_file[n] = '\0';
|
2021-12-07 08:31:17 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-11-07 05:22:32 +00:00
|
|
|
#if defined(LWS_WITH_PLUGINS) && !defined(LWS_WITH_PLUGINS_BUILTIN)
|
2021-01-25 11:40:54 +00:00
|
|
|
{
|
|
|
|
char *ld_env = getenv("LD_LIBRARY_PATH");
|
|
|
|
|
|
|
|
if (ld_env) {
|
|
|
|
const char *pp[2] = { ld_env, NULL };
|
|
|
|
|
|
|
|
lws_plugins_init(&context->plugin_list, pp,
|
|
|
|
"lws_protocol_plugin", NULL,
|
|
|
|
protocol_plugin_cb, context);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info->plugin_dirs)
|
|
|
|
lws_plugins_init(&context->plugin_list,
|
|
|
|
info->plugin_dirs,
|
|
|
|
"lws_protocol_plugin", NULL,
|
|
|
|
protocol_plugin_cb, context);
|
|
|
|
}
|
2021-11-07 05:22:32 +00:00
|
|
|
|
|
|
|
#endif
|
2021-11-10 16:14:26 +00:00
|
|
|
#if defined(LWS_BUILTIN_PLUGIN_NAMES) && defined(LWS_WITH_PLUGINS)
|
2021-11-07 05:22:32 +00:00
|
|
|
lws_plugins_handle_builtin(&context->plugin_list,
|
|
|
|
protocol_plugin_cb, context);
|
2018-06-16 09:37:07 +08:00
|
|
|
#endif
|
|
|
|
|
2019-08-09 10:12:09 +01:00
|
|
|
|
|
|
|
#if defined(LWS_WITH_NETWORK)
|
|
|
|
/* we only need to do this on pt[0] */
|
|
|
|
|
|
|
|
context->pt[0].sul_plat.cb = lws_sul_plat_unix;
|
2020-05-28 12:48:17 +01:00
|
|
|
__lws_sul_insert_us(&context->pt[0].pt_sul_owner[LWSSULLI_MISS_IF_SUSPENDED],
|
|
|
|
&context->pt[0].sul_plat, 30 * LWS_US_PER_SEC);
|
2019-08-09 10:12:09 +01:00
|
|
|
#endif
|
|
|
|
|
2018-06-16 09:37:07 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
lws_plat_context_early_init(void)
|
|
|
|
{
|
|
|
|
#if !defined(LWS_AVOID_SIGPIPE_IGN)
|
|
|
|
signal(SIGPIPE, SIG_IGN);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
lws_plat_context_early_destroy(struct lws_context *context)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
lws_plat_context_late_destroy(struct lws_context *context)
|
|
|
|
{
|
2020-08-27 09:34:35 +01:00
|
|
|
#if defined(LWS_WITH_PLUGINS)
|
2018-06-16 09:37:07 +08:00
|
|
|
if (context->plugin_list)
|
2020-08-27 09:34:35 +01:00
|
|
|
lws_plugins_destroy(&context->plugin_list, NULL, NULL);
|
2018-06-16 09:37:07 +08:00
|
|
|
#endif
|
2019-01-13 06:58:21 +08:00
|
|
|
#if defined(LWS_WITH_NETWORK)
|
2018-06-16 09:37:07 +08:00
|
|
|
if (context->lws_lookup)
|
2019-03-21 09:27:45 +08:00
|
|
|
lws_free_set_NULL(context->lws_lookup);
|
2019-01-13 06:58:21 +08:00
|
|
|
#endif
|
2018-06-16 09:37:07 +08:00
|
|
|
if (!context->fd_random)
|
|
|
|
lwsl_err("ZERO RANDOM FD\n");
|
|
|
|
if (context->fd_random != LWS_INVALID_FILE)
|
|
|
|
close(context->fd_random);
|
|
|
|
}
|