2016-02-14 09:27:41 +08:00
|
|
|
/*
|
|
|
|
* libwebsockets - small server side websockets and web server implementation
|
|
|
|
*
|
2018-05-02 18:35:58 +08:00
|
|
|
* Copyright (C) 2010-2018 Andy Green <andy@warmcat.com>
|
2016-02-14 09:27:41 +08:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation:
|
|
|
|
* version 2.1 of the License.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
* MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2018-05-03 10:49:36 +08:00
|
|
|
#include "core/private.h"
|
2016-02-14 09:27:41 +08:00
|
|
|
|
2018-03-19 16:37:37 +08:00
|
|
|
static void
|
|
|
|
lws_uv_hrtimer_cb(uv_timer_t *timer
|
|
|
|
#if UV_VERSION_MAJOR == 0
|
|
|
|
, int status
|
|
|
|
#endif
|
|
|
|
)
|
|
|
|
{
|
|
|
|
struct lws_context_per_thread *pt = lws_container_of(timer,
|
2018-04-29 08:34:19 +08:00
|
|
|
struct lws_context_per_thread, uv.hrtimer);
|
2018-03-19 16:37:37 +08:00
|
|
|
lws_usec_t us;
|
|
|
|
|
|
|
|
lws_pt_lock(pt, __func__);
|
2019-08-08 06:30:14 +01:00
|
|
|
us = __lws_event_service_get_earliest_wake(pt, lws_now_usecs());
|
|
|
|
if (us)
|
|
|
|
uv_timer_start(&pt->uv.hrtimer, lws_uv_hrtimer_cb,
|
|
|
|
LWS_US_TO_MS(us), 0);
|
2018-03-19 16:37:37 +08:00
|
|
|
lws_pt_unlock(pt);
|
|
|
|
}
|
|
|
|
|
2016-04-06 09:23:16 +08:00
|
|
|
static void
|
2016-04-06 16:15:40 +08:00
|
|
|
lws_uv_idle(uv_idle_t *handle
|
|
|
|
#if UV_VERSION_MAJOR == 0
|
|
|
|
, int status
|
|
|
|
#endif
|
|
|
|
)
|
2016-04-06 09:23:16 +08:00
|
|
|
{
|
2016-04-06 16:15:40 +08:00
|
|
|
struct lws_context_per_thread *pt = lws_container_of(handle,
|
2018-04-29 08:34:19 +08:00
|
|
|
struct lws_context_per_thread, uv.idle);
|
2018-03-19 16:37:37 +08:00
|
|
|
lws_usec_t us;
|
2016-04-06 09:23:16 +08:00
|
|
|
|
2018-04-17 11:43:20 +08:00
|
|
|
lws_service_do_ripe_rxflow(pt);
|
|
|
|
|
2016-04-06 09:23:16 +08:00
|
|
|
/*
|
|
|
|
* is there anybody with pending stuff that needs service forcing?
|
|
|
|
*/
|
|
|
|
if (!lws_service_adjust_timeout(pt->context, 1, pt->tid)) {
|
|
|
|
/* -1 timeout means just do forced service */
|
2016-10-20 09:09:56 +08:00
|
|
|
_lws_plat_service_tsi(pt->context, -1, pt->tid);
|
2016-04-06 09:23:16 +08:00
|
|
|
/* still somebody left who wants forced service? */
|
|
|
|
if (!lws_service_adjust_timeout(pt->context, 1, pt->tid))
|
|
|
|
/* yes... come back again later */
|
2016-10-04 08:39:14 +08:00
|
|
|
return;
|
2016-04-06 09:23:16 +08:00
|
|
|
}
|
|
|
|
|
2018-03-19 16:37:37 +08:00
|
|
|
/* account for hrtimer */
|
|
|
|
|
|
|
|
lws_pt_lock(pt, __func__);
|
2019-08-08 06:30:14 +01:00
|
|
|
us = __lws_event_service_get_earliest_wake(pt, lws_now_usecs());
|
|
|
|
if (us)
|
|
|
|
uv_timer_start(&pt->uv.hrtimer, lws_uv_hrtimer_cb,
|
|
|
|
LWS_US_TO_MS(us), 0);
|
2018-03-19 16:37:37 +08:00
|
|
|
lws_pt_unlock(pt);
|
|
|
|
|
2016-04-06 09:23:16 +08:00
|
|
|
/* there is nobody who needs service forcing, shut down idle */
|
|
|
|
uv_idle_stop(handle);
|
|
|
|
}
|
|
|
|
|
2016-02-14 09:27:41 +08:00
|
|
|
static void
|
2016-03-02 11:39:37 +01:00
|
|
|
lws_io_cb(uv_poll_t *watcher, int status, int revents)
|
2016-02-14 09:27:41 +08:00
|
|
|
{
|
2018-10-13 11:59:04 +08:00
|
|
|
struct lws *wsi = (struct lws *)((uv_handle_t *)watcher)->data;
|
context deprecation
1) This makes lwsws run a parent process with the original permissions.
But this process is only able to respond to SIGHUP, it doesn't do anything
else.
2) You can send this parent process a SIGHUP now to cause it to
- close listening sockets in existing lwsws processes
- mark those processes as to exit when the number of active connections
on the falls to zero
- spawn a fresh child process from scratch, using latest configuration
file content, latest plugins, etc. It can now reopen listening sockets
if it chooses to, or open different listen ports or whatever.
Notes:
1) lws_context_destroy() has been split into two pieces... the reason for
the split is the first part closes the per-vhost protocols, but since
they may have created libuv objects in the per-vhost protocol storage,
these cannot be freed until after the loop has been run.
That's the purpose of the second part of the context destruction,
lws_context_destroy2().
For compatibility, if you are not using libuv, the first part calls the
second part. However if you are using libuv, you must now call the
second part from your own main.c after the first part.
2016-12-16 07:37:43 +08:00
|
|
|
struct lws_context *context = wsi->context;
|
2018-03-19 16:37:37 +08:00
|
|
|
struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
|
2016-02-14 09:27:41 +08:00
|
|
|
struct lws_pollfd eventfd;
|
|
|
|
|
2016-03-31 20:11:53 +08:00
|
|
|
#if defined(WIN32) || defined(_WIN32)
|
2016-04-01 15:56:48 +08:00
|
|
|
eventfd.fd = watcher->socket;
|
2016-03-31 20:11:53 +08:00
|
|
|
#else
|
2016-02-14 09:27:41 +08:00
|
|
|
eventfd.fd = watcher->io_watcher.fd;
|
2016-03-31 20:11:53 +08:00
|
|
|
#endif
|
2016-02-14 09:27:41 +08:00
|
|
|
eventfd.events = 0;
|
2016-02-20 16:01:12 +01:00
|
|
|
eventfd.revents = 0;
|
2016-03-02 11:39:37 +01:00
|
|
|
|
|
|
|
if (status < 0) {
|
2017-09-23 12:55:21 +08:00
|
|
|
/*
|
|
|
|
* At this point status will be an UV error, like UV_EBADF,
|
|
|
|
* we treat all errors as LWS_POLLHUP
|
|
|
|
*
|
|
|
|
* You might want to return; instead of servicing the fd in
|
|
|
|
* some cases */
|
2016-03-02 11:39:37 +01:00
|
|
|
if (status == UV_EAGAIN)
|
|
|
|
return;
|
|
|
|
|
|
|
|
eventfd.events |= LWS_POLLHUP;
|
|
|
|
eventfd.revents |= LWS_POLLHUP;
|
|
|
|
} else {
|
|
|
|
if (revents & UV_READABLE) {
|
|
|
|
eventfd.events |= LWS_POLLIN;
|
|
|
|
eventfd.revents |= LWS_POLLIN;
|
|
|
|
}
|
|
|
|
if (revents & UV_WRITABLE) {
|
|
|
|
eventfd.events |= LWS_POLLOUT;
|
|
|
|
eventfd.revents |= LWS_POLLOUT;
|
|
|
|
}
|
2016-02-14 09:27:41 +08:00
|
|
|
}
|
2018-05-02 18:35:58 +08:00
|
|
|
lws_service_fd_tsi(context, &eventfd, wsi->tsi);
|
2018-03-19 16:37:37 +08:00
|
|
|
|
2018-04-29 08:34:19 +08:00
|
|
|
uv_idle_start(&pt->uv.idle, lws_uv_idle);
|
2016-02-14 09:27:41 +08:00
|
|
|
}
|
|
|
|
|
2018-04-29 10:44:36 +08:00
|
|
|
/*
|
|
|
|
* This does not actually stop the event loop. The reason is we have to pass
|
|
|
|
* libuv handle closures through its event loop. So this tries to close all
|
|
|
|
* wsi, and set a flag; when all the wsi closures are finalized then we
|
|
|
|
* actually stop the libuv event loops.
|
|
|
|
*/
|
2018-04-30 09:16:04 +08:00
|
|
|
static void
|
2018-04-29 10:44:36 +08:00
|
|
|
lws_libuv_stop(struct lws_context *context)
|
2016-02-14 09:27:41 +08:00
|
|
|
{
|
2018-04-29 10:44:36 +08:00
|
|
|
struct lws_context_per_thread *pt;
|
|
|
|
int n, m;
|
|
|
|
|
|
|
|
lwsl_err("%s\n", __func__);
|
|
|
|
|
|
|
|
if (context->requested_kill) {
|
|
|
|
lwsl_err("%s: ignoring\n", __func__);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
context->requested_kill = 1;
|
|
|
|
|
|
|
|
m = context->count_threads;
|
|
|
|
context->being_destroyed = 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Phase 1: start the close of every dynamic uv handle
|
|
|
|
*/
|
|
|
|
|
|
|
|
while (m--) {
|
|
|
|
pt = &context->pt[m];
|
|
|
|
|
|
|
|
if (pt->pipe_wsi) {
|
2018-10-13 11:59:04 +08:00
|
|
|
uv_poll_stop(pt->pipe_wsi->w_read.uv.pwatcher);
|
2018-04-29 10:44:36 +08:00
|
|
|
lws_destroy_event_pipe(pt->pipe_wsi);
|
|
|
|
pt->pipe_wsi = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (n = 0; (unsigned int)n < context->pt[m].fds_count; n++) {
|
|
|
|
struct lws *wsi = wsi_from_fd(context, pt->fds[n].fd);
|
|
|
|
|
|
|
|
if (!wsi)
|
|
|
|
continue;
|
|
|
|
lws_close_free_wsi(wsi,
|
2018-11-23 08:47:56 +08:00
|
|
|
LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY,
|
|
|
|
__func__ /* no protocol close */);
|
2018-04-29 10:44:36 +08:00
|
|
|
n--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lwsl_info("%s: started closing all wsi\n", __func__);
|
|
|
|
|
|
|
|
/* we cannot have completed... there are at least the cancel pipes */
|
2016-02-14 09:27:41 +08:00
|
|
|
}
|
|
|
|
|
2018-04-29 10:44:36 +08:00
|
|
|
static void
|
|
|
|
lws_uv_signal_handler(uv_signal_t *watcher, int signum)
|
2016-02-14 09:27:41 +08:00
|
|
|
{
|
2018-04-29 10:44:36 +08:00
|
|
|
struct lws_context *context = watcher->data;
|
2016-02-14 09:27:41 +08:00
|
|
|
|
2018-04-29 10:44:36 +08:00
|
|
|
if (context->eventlib_signal_cb) {
|
|
|
|
context->eventlib_signal_cb((void *)watcher, signum);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
lwsl_err("internal signal handler caught signal %d\n", signum);
|
|
|
|
lws_libuv_stop(watcher->data);
|
2016-02-14 09:27:41 +08:00
|
|
|
}
|
|
|
|
|
2016-02-27 11:03:27 +08:00
|
|
|
static void
|
2016-04-06 16:15:40 +08:00
|
|
|
lws_uv_timeout_cb(uv_timer_t *timer
|
|
|
|
#if UV_VERSION_MAJOR == 0
|
|
|
|
, int status
|
|
|
|
#endif
|
|
|
|
)
|
2016-02-27 11:03:27 +08:00
|
|
|
{
|
2016-04-06 16:15:40 +08:00
|
|
|
struct lws_context_per_thread *pt = lws_container_of(timer,
|
2018-04-29 08:34:19 +08:00
|
|
|
struct lws_context_per_thread, uv.timeout_watcher);
|
2016-02-27 11:03:27 +08:00
|
|
|
|
2016-05-08 17:07:46 +08:00
|
|
|
if (pt->context->requested_kill)
|
|
|
|
return;
|
|
|
|
|
2016-02-27 11:42:22 +08:00
|
|
|
lwsl_debug("%s\n", __func__);
|
2016-04-06 09:23:16 +08:00
|
|
|
|
2016-02-27 11:03:27 +08:00
|
|
|
lws_service_fd_tsi(pt->context, NULL, pt->tid);
|
|
|
|
}
|
|
|
|
|
context deprecation
1) This makes lwsws run a parent process with the original permissions.
But this process is only able to respond to SIGHUP, it doesn't do anything
else.
2) You can send this parent process a SIGHUP now to cause it to
- close listening sockets in existing lwsws processes
- mark those processes as to exit when the number of active connections
on the falls to zero
- spawn a fresh child process from scratch, using latest configuration
file content, latest plugins, etc. It can now reopen listening sockets
if it chooses to, or open different listen ports or whatever.
Notes:
1) lws_context_destroy() has been split into two pieces... the reason for
the split is the first part closes the per-vhost protocols, but since
they may have created libuv objects in the per-vhost protocol storage,
these cannot be freed until after the loop has been run.
That's the purpose of the second part of the context destruction,
lws_context_destroy2().
For compatibility, if you are not using libuv, the first part calls the
second part. However if you are using libuv, you must now call the
second part from your own main.c after the first part.
2016-12-16 07:37:43 +08:00
|
|
|
static const int sigs[] = { SIGINT, SIGTERM, SIGSEGV, SIGFPE, SIGHUP };
|
2016-02-14 09:27:41 +08:00
|
|
|
|
2018-03-30 20:22:46 +08:00
|
|
|
/*
|
|
|
|
* Closing Phase 2: Close callback for a static UV asset
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
lws_uv_close_cb_sa(uv_handle_t *handle)
|
|
|
|
{
|
|
|
|
struct lws_context *context =
|
|
|
|
LWS_UV_REFCOUNT_STATIC_HANDLE_TO_CONTEXT(handle);
|
|
|
|
int n;
|
|
|
|
|
|
|
|
lwsl_info("%s: sa left %d: dyn left: %d\n", __func__,
|
2018-04-29 08:34:19 +08:00
|
|
|
context->count_event_loop_static_asset_handles,
|
2018-03-30 20:22:46 +08:00
|
|
|
context->count_wsi_allocated);
|
|
|
|
|
|
|
|
/* any static assets left? */
|
|
|
|
|
|
|
|
if (LWS_UV_REFCOUNT_STATIC_HANDLE_DESTROYED(handle) ||
|
|
|
|
context->count_wsi_allocated)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* That's it... all wsi were down, and now every
|
|
|
|
* static asset lws had a UV handle for is down.
|
|
|
|
*
|
|
|
|
* Stop the loop so we can get out of here.
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (n = 0; n < context->count_threads; n++) {
|
|
|
|
struct lws_context_per_thread *pt = &context->pt[n];
|
|
|
|
|
2018-04-29 10:44:36 +08:00
|
|
|
if (pt->uv.io_loop && !pt->event_loop_foreign)
|
|
|
|
uv_stop(pt->uv.io_loop);
|
|
|
|
}
|
2018-03-30 20:22:46 +08:00
|
|
|
|
2018-04-29 10:44:36 +08:00
|
|
|
if (!context->pt[0].event_loop_foreign) {
|
|
|
|
lwsl_info("%s: calling lws_context_destroy2\n", __func__);
|
|
|
|
lws_context_destroy2(context);
|
2018-03-30 20:22:46 +08:00
|
|
|
}
|
2018-04-29 10:44:36 +08:00
|
|
|
|
|
|
|
lwsl_info("%s: all done\n", __func__);
|
2018-03-30 20:22:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* These must be called by protocols that want to use libuv objects directly...
|
|
|
|
*
|
|
|
|
* .... when the libuv object is created...
|
|
|
|
*/
|
|
|
|
|
|
|
|
LWS_VISIBLE void
|
|
|
|
lws_libuv_static_refcount_add(uv_handle_t *h, struct lws_context *context)
|
|
|
|
{
|
|
|
|
LWS_UV_REFCOUNT_STATIC_HANDLE_NEW(h, context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ... and in the close callback when the object is closed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
LWS_VISIBLE void
|
|
|
|
lws_libuv_static_refcount_del(uv_handle_t *h)
|
|
|
|
{
|
2018-04-16 19:52:28 +08:00
|
|
|
lws_uv_close_cb_sa(h);
|
2018-03-30 20:22:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-04 08:27:56 +08:00
|
|
|
static void lws_uv_close_cb(uv_handle_t *handle)
|
2016-02-20 15:32:57 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-05-04 08:27:56 +08:00
|
|
|
static void lws_uv_walk_cb(uv_handle_t *handle, void *arg)
|
2016-02-20 15:32:57 +01:00
|
|
|
{
|
2017-05-19 14:41:03 +02:00
|
|
|
if (!uv_is_closing(handle))
|
|
|
|
uv_close(handle, lws_uv_close_cb);
|
2016-02-20 15:32:57 +01:00
|
|
|
}
|
|
|
|
|
2017-07-15 17:48:37 +08:00
|
|
|
LWS_VISIBLE void
|
|
|
|
lws_close_all_handles_in_loop(uv_loop_t *loop)
|
|
|
|
{
|
|
|
|
uv_walk(loop, lws_uv_walk_cb, NULL);
|
|
|
|
}
|
|
|
|
|
2018-03-30 20:22:46 +08:00
|
|
|
|
2018-04-29 10:44:36 +08:00
|
|
|
LWS_VISIBLE void
|
|
|
|
lws_libuv_stop_without_kill(const struct lws_context *context, int tsi)
|
|
|
|
{
|
2018-04-30 09:16:04 +08:00
|
|
|
if (context->pt[tsi].uv.io_loop)
|
2018-04-29 10:44:36 +08:00
|
|
|
uv_stop(context->pt[tsi].uv.io_loop);
|
|
|
|
}
|
2016-05-04 08:27:56 +08:00
|
|
|
|
2016-08-20 05:47:29 +08:00
|
|
|
|
2016-05-04 08:27:56 +08:00
|
|
|
|
2018-04-29 10:44:36 +08:00
|
|
|
LWS_VISIBLE uv_loop_t *
|
|
|
|
lws_uv_getloop(struct lws_context *context, int tsi)
|
|
|
|
{
|
2018-04-30 09:16:04 +08:00
|
|
|
if (context->pt[tsi].uv.io_loop)
|
2018-04-29 10:44:36 +08:00
|
|
|
return context->pt[tsi].uv.io_loop;
|
2016-05-04 08:27:56 +08:00
|
|
|
|
2018-04-29 10:44:36 +08:00
|
|
|
return NULL;
|
2016-02-14 09:27:41 +08:00
|
|
|
}
|
|
|
|
|
2018-04-29 10:44:36 +08:00
|
|
|
int
|
|
|
|
lws_libuv_check_watcher_active(struct lws *wsi)
|
2016-02-14 09:27:41 +08:00
|
|
|
{
|
2018-10-13 11:59:04 +08:00
|
|
|
uv_handle_t *h = (uv_handle_t *)wsi->w_read.uv.pwatcher;
|
|
|
|
|
|
|
|
if (!h)
|
|
|
|
return 0;
|
2016-02-14 09:27:41 +08:00
|
|
|
|
2018-04-29 10:44:36 +08:00
|
|
|
return uv_is_active(h);
|
|
|
|
}
|
2016-12-16 08:41:16 +08:00
|
|
|
|
2016-02-14 09:27:41 +08:00
|
|
|
|
2018-04-29 10:44:36 +08:00
|
|
|
#if defined(LWS_WITH_PLUGINS) && (UV_VERSION_MAJOR > 0)
|
2016-04-09 07:22:40 +08:00
|
|
|
|
2019-04-21 19:46:41 +01:00
|
|
|
int
|
|
|
|
lws_uv_plugins_init(struct lws_context *context, const char * const *d)
|
2016-04-09 07:22:40 +08:00
|
|
|
{
|
|
|
|
struct lws_plugin_capability lcaps;
|
|
|
|
struct lws_plugin *plugin;
|
|
|
|
lws_plugin_init_func initfunc;
|
|
|
|
int m, ret = 0;
|
|
|
|
void *v;
|
|
|
|
uv_dirent_t dent;
|
|
|
|
uv_fs_t req;
|
|
|
|
char path[256];
|
|
|
|
uv_lib_t lib;
|
2017-04-28 11:54:27 +08:00
|
|
|
int pofs = 0;
|
|
|
|
|
|
|
|
#if defined(__MINGW32__) || !defined(WIN32)
|
|
|
|
pofs = 3;
|
|
|
|
#endif
|
2016-04-09 07:22:40 +08:00
|
|
|
|
|
|
|
lib.errmsg = NULL;
|
|
|
|
lib.handle = NULL;
|
|
|
|
|
2018-04-30 09:16:04 +08:00
|
|
|
uv_loop_init(&context->uv.loop);
|
2016-04-09 07:22:40 +08:00
|
|
|
|
|
|
|
lwsl_notice(" Plugins:\n");
|
|
|
|
|
2016-05-02 10:03:25 +08:00
|
|
|
while (d && *d) {
|
2016-04-09 07:22:40 +08:00
|
|
|
|
2016-05-02 10:03:25 +08:00
|
|
|
lwsl_notice(" Scanning %s\n", *d);
|
2018-04-30 09:16:04 +08:00
|
|
|
m =uv_fs_scandir(&context->uv.loop, &req, *d, 0, NULL);
|
2016-05-02 10:03:25 +08:00
|
|
|
if (m < 1) {
|
|
|
|
lwsl_err("Scandir on %s failed\n", *d);
|
|
|
|
return 1;
|
2016-04-09 07:22:40 +08:00
|
|
|
}
|
|
|
|
|
2016-05-02 10:03:25 +08:00
|
|
|
while (uv_fs_scandir_next(&req, &dent) != UV_EOF) {
|
|
|
|
if (strlen(dent.name) < 7)
|
|
|
|
continue;
|
2016-04-09 07:22:40 +08:00
|
|
|
|
2016-05-02 10:03:25 +08:00
|
|
|
lwsl_notice(" %s\n", dent.name);
|
|
|
|
|
2017-09-23 12:55:21 +08:00
|
|
|
lws_snprintf(path, sizeof(path) - 1, "%s/%s", *d,
|
|
|
|
dent.name);
|
2016-05-02 10:03:25 +08:00
|
|
|
if (uv_dlopen(path, &lib)) {
|
|
|
|
uv_dlerror(&lib);
|
|
|
|
lwsl_err("Error loading DSO: %s\n", lib.errmsg);
|
2017-07-15 14:37:04 +08:00
|
|
|
uv_dlclose(&lib);
|
2016-05-02 10:03:25 +08:00
|
|
|
goto bail;
|
|
|
|
}
|
2017-04-28 11:54:27 +08:00
|
|
|
|
2016-05-02 10:03:25 +08:00
|
|
|
/* we could open it, can we get his init function? */
|
2017-04-28 11:54:27 +08:00
|
|
|
|
|
|
|
#if !defined(WIN32) && !defined(__MINGW32__)
|
2016-09-15 02:22:57 +08:00
|
|
|
m = lws_snprintf(path, sizeof(path) - 1, "init_%s",
|
2017-04-28 11:54:27 +08:00
|
|
|
dent.name + pofs /* snip lib... */);
|
2016-05-02 10:03:25 +08:00
|
|
|
path[m - 3] = '\0'; /* snip the .so */
|
2016-05-25 21:43:58 +08:00
|
|
|
#else
|
2016-09-15 02:22:57 +08:00
|
|
|
m = lws_snprintf(path, sizeof(path) - 1, "init_%s",
|
2017-04-28 11:54:27 +08:00
|
|
|
dent.name + pofs);
|
2016-05-25 21:43:58 +08:00
|
|
|
path[m - 4] = '\0'; /* snip the .dll */
|
|
|
|
#endif
|
2016-05-02 10:03:25 +08:00
|
|
|
if (uv_dlsym(&lib, path, &v)) {
|
|
|
|
uv_dlerror(&lib);
|
2017-04-28 11:54:27 +08:00
|
|
|
lwsl_err("Failed to get %s on %s: %s", path,
|
2016-05-02 10:03:25 +08:00
|
|
|
dent.name, lib.errmsg);
|
2017-07-15 17:57:14 +08:00
|
|
|
uv_dlclose(&lib);
|
2016-05-02 10:03:25 +08:00
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
initfunc = (lws_plugin_init_func)v;
|
|
|
|
lcaps.api_magic = LWS_PLUGIN_API_MAGIC;
|
|
|
|
m = initfunc(context, &lcaps);
|
|
|
|
if (m) {
|
2017-09-23 12:55:21 +08:00
|
|
|
lwsl_err("Init %s failed %d\n", dent.name, m);
|
2016-05-02 10:03:25 +08:00
|
|
|
goto skip;
|
|
|
|
}
|
|
|
|
|
2017-10-04 07:10:39 +08:00
|
|
|
plugin = lws_malloc(sizeof(*plugin), "plugin");
|
2016-05-02 10:03:25 +08:00
|
|
|
if (!plugin) {
|
2017-07-15 17:57:14 +08:00
|
|
|
uv_dlclose(&lib);
|
2016-05-02 10:03:25 +08:00
|
|
|
lwsl_err("OOM\n");
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
plugin->list = context->plugin_list;
|
|
|
|
context->plugin_list = plugin;
|
2018-03-12 09:28:26 +08:00
|
|
|
lws_strncpy(plugin->name, dent.name, sizeof(plugin->name));
|
2016-05-02 10:03:25 +08:00
|
|
|
plugin->lib = lib;
|
|
|
|
plugin->caps = lcaps;
|
|
|
|
context->plugin_protocol_count += lcaps.count_protocols;
|
|
|
|
context->plugin_extension_count += lcaps.count_extensions;
|
|
|
|
|
|
|
|
continue;
|
2016-04-09 07:22:40 +08:00
|
|
|
|
|
|
|
skip:
|
2016-05-02 10:03:25 +08:00
|
|
|
uv_dlclose(&lib);
|
|
|
|
}
|
|
|
|
bail:
|
|
|
|
uv_fs_req_cleanup(&req);
|
|
|
|
d++;
|
2016-04-09 07:22:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-04-21 19:46:41 +01:00
|
|
|
int
|
|
|
|
lws_uv_plugins_destroy(struct lws_context *context)
|
2016-04-09 07:22:40 +08:00
|
|
|
{
|
|
|
|
struct lws_plugin *plugin = context->plugin_list, *p;
|
|
|
|
lws_plugin_destroy_func func;
|
|
|
|
char path[256];
|
2017-09-23 12:55:21 +08:00
|
|
|
int pofs = 0;
|
2016-04-09 07:22:40 +08:00
|
|
|
void *v;
|
|
|
|
int m;
|
2017-04-28 11:54:27 +08:00
|
|
|
|
|
|
|
#if defined(__MINGW32__) || !defined(WIN32)
|
|
|
|
pofs = 3;
|
|
|
|
#endif
|
2016-04-09 07:22:40 +08:00
|
|
|
|
|
|
|
if (!plugin)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
while (plugin) {
|
|
|
|
p = plugin;
|
2017-04-28 11:54:27 +08:00
|
|
|
|
|
|
|
#if !defined(WIN32) && !defined(__MINGW32__)
|
2017-09-23 12:55:21 +08:00
|
|
|
m = lws_snprintf(path, sizeof(path) - 1, "destroy_%s",
|
|
|
|
plugin->name + pofs);
|
2016-04-09 07:22:40 +08:00
|
|
|
path[m - 3] = '\0';
|
2016-05-25 21:43:58 +08:00
|
|
|
#else
|
2017-09-23 12:55:21 +08:00
|
|
|
m = lws_snprintf(path, sizeof(path) - 1, "destroy_%s",
|
|
|
|
plugin->name + pofs);
|
2016-05-25 21:43:58 +08:00
|
|
|
path[m - 4] = '\0';
|
|
|
|
#endif
|
2016-04-09 07:22:40 +08:00
|
|
|
|
|
|
|
if (uv_dlsym(&plugin->lib, path, &v)) {
|
|
|
|
uv_dlerror(&plugin->lib);
|
2017-04-28 11:54:27 +08:00
|
|
|
lwsl_err("Failed to get %s on %s: %s", path,
|
2016-04-09 07:22:40 +08:00
|
|
|
plugin->name, plugin->lib.errmsg);
|
|
|
|
} else {
|
|
|
|
func = (lws_plugin_destroy_func)v;
|
|
|
|
m = func(context);
|
|
|
|
if (m)
|
|
|
|
lwsl_err("Destroying %s failed %d\n",
|
|
|
|
plugin->name, m);
|
|
|
|
}
|
|
|
|
|
|
|
|
uv_dlclose(&p->lib);
|
|
|
|
plugin = p->list;
|
|
|
|
p->list = NULL;
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
context->plugin_list = NULL;
|
|
|
|
|
2018-04-30 09:16:04 +08:00
|
|
|
while (uv_loop_close(&context->uv.loop))
|
2017-07-15 17:57:14 +08:00
|
|
|
;
|
|
|
|
|
2016-04-09 07:22:40 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2018-04-29 10:44:36 +08:00
|
|
|
static int
|
|
|
|
elops_init_context_uv(struct lws_context *context,
|
|
|
|
const struct lws_context_creation_info *info)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
|
|
|
context->eventlib_signal_cb = info->signal_cb;
|
|
|
|
|
|
|
|
for (n = 0; n < context->count_threads; n++)
|
|
|
|
context->pt[n].w_sigint.context = context;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
elops_destroy_context1_uv(struct lws_context *context)
|
|
|
|
{
|
|
|
|
struct lws_context_per_thread *pt;
|
2019-02-22 15:47:41 -05:00
|
|
|
int n, m = 0;
|
2018-04-29 10:44:36 +08:00
|
|
|
|
|
|
|
for (n = 0; n < context->count_threads; n++) {
|
|
|
|
int budget = 10000;
|
|
|
|
pt = &context->pt[n];
|
|
|
|
|
|
|
|
/* only for internal loops... */
|
|
|
|
|
|
|
|
if (!pt->event_loop_foreign) {
|
|
|
|
|
|
|
|
while (budget-- && (m = uv_run(pt->uv.io_loop,
|
|
|
|
UV_RUN_NOWAIT)))
|
|
|
|
;
|
|
|
|
if (m)
|
2018-05-02 18:35:58 +08:00
|
|
|
lwsl_err("%s: tsi %d: not all closed\n",
|
|
|
|
__func__, n);
|
2018-04-29 10:44:36 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* call destroy2 if internal loop */
|
|
|
|
return !context->pt[0].event_loop_foreign;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
elops_destroy_context2_uv(struct lws_context *context)
|
|
|
|
{
|
|
|
|
struct lws_context_per_thread *pt;
|
|
|
|
int n, internal = 0;
|
|
|
|
|
|
|
|
for (n = 0; n < context->count_threads; n++) {
|
|
|
|
pt = &context->pt[n];
|
|
|
|
|
|
|
|
/* only for internal loops... */
|
|
|
|
|
|
|
|
if (!pt->event_loop_foreign && pt->uv.io_loop) {
|
|
|
|
internal = 1;
|
|
|
|
if (!context->finalize_destroy_after_internal_loops_stopped)
|
|
|
|
uv_stop(pt->uv.io_loop);
|
|
|
|
else {
|
|
|
|
#if UV_VERSION_MAJOR > 0
|
|
|
|
uv_loop_close(pt->uv.io_loop);
|
|
|
|
#endif
|
|
|
|
lws_free_set_NULL(pt->uv.io_loop);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return internal;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
elops_wsi_logical_close_uv(struct lws *wsi)
|
|
|
|
{
|
2018-05-10 16:13:26 +08:00
|
|
|
if (!lws_socket_is_valid(wsi->desc.sockfd))
|
2018-04-29 10:44:36 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (wsi->listener || wsi->event_pipe) {
|
|
|
|
lwsl_debug("%s: %p: %d %d stop listener / pipe poll\n",
|
|
|
|
__func__, wsi, wsi->listener, wsi->event_pipe);
|
2019-03-21 09:27:45 +08:00
|
|
|
if (wsi->w_read.uv.pwatcher)
|
|
|
|
uv_poll_stop(wsi->w_read.uv.pwatcher);
|
2018-04-29 10:44:36 +08:00
|
|
|
}
|
|
|
|
lwsl_debug("%s: lws_libuv_closehandle: wsi %p\n", __func__, wsi);
|
|
|
|
/*
|
|
|
|
* libuv has to do his own close handle processing asynchronously
|
|
|
|
*/
|
|
|
|
lws_libuv_closehandle(wsi);
|
|
|
|
|
|
|
|
return 1; /* do not complete the wsi close, uv close cb will do it */
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
elops_check_client_connect_ok_uv(struct lws *wsi)
|
|
|
|
{
|
|
|
|
if (lws_libuv_check_watcher_active(wsi)) {
|
|
|
|
lwsl_warn("Waiting for libuv watcher to close\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2018-10-13 11:59:04 +08:00
|
|
|
lws_libuv_closewsi_m(uv_handle_t* handle)
|
2018-04-29 10:44:36 +08:00
|
|
|
{
|
2018-10-13 11:59:04 +08:00
|
|
|
lws_sockfd_type sockfd = (lws_sockfd_type)(lws_intptr_t)handle->data;
|
|
|
|
lwsl_debug("%s: sockfd %d\n", __func__, sockfd);
|
|
|
|
compatible_close(sockfd);
|
|
|
|
lws_free(handle);
|
|
|
|
}
|
2018-09-02 19:23:40 +08:00
|
|
|
|
2018-10-13 11:59:04 +08:00
|
|
|
static void
|
|
|
|
elops_close_handle_manually_uv(struct lws *wsi)
|
|
|
|
{
|
|
|
|
uv_handle_t *h = (uv_handle_t *)wsi->w_read.uv.pwatcher;
|
2018-04-29 10:44:36 +08:00
|
|
|
|
|
|
|
lwsl_debug("%s: lws_libuv_closehandle: wsi %p\n", __func__, wsi);
|
2018-10-13 11:59:04 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* the "manual" variant only closes the handle itself and the
|
|
|
|
* related fd. handle->data is the fd.
|
|
|
|
*/
|
|
|
|
h->data = (void *)(lws_intptr_t)wsi->desc.sockfd;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We take responsibility to close / destroy these now.
|
|
|
|
* Remove any trace from the wsi.
|
|
|
|
*/
|
|
|
|
|
|
|
|
wsi->desc.sockfd = LWS_SOCK_INVALID;
|
|
|
|
wsi->w_read.uv.pwatcher = NULL;
|
2019-02-24 06:16:57 +08:00
|
|
|
wsi->told_event_loop_closed = 1;
|
2018-10-13 11:59:04 +08:00
|
|
|
|
|
|
|
uv_close(h, lws_libuv_closewsi_m);
|
2018-04-29 10:44:36 +08:00
|
|
|
}
|
|
|
|
|
2018-10-13 11:59:04 +08:00
|
|
|
static int
|
2018-04-29 10:44:36 +08:00
|
|
|
elops_accept_uv(struct lws *wsi)
|
|
|
|
{
|
|
|
|
struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
|
|
|
|
|
|
|
|
wsi->w_read.context = wsi->context;
|
2018-10-13 11:59:04 +08:00
|
|
|
|
|
|
|
wsi->w_read.uv.pwatcher =
|
|
|
|
lws_malloc(sizeof(*wsi->w_read.uv.pwatcher), "uvh");
|
|
|
|
if (!wsi->w_read.uv.pwatcher)
|
|
|
|
return -1;
|
|
|
|
|
2018-04-29 10:44:36 +08:00
|
|
|
if (wsi->role_ops->file_handle)
|
2018-10-13 11:59:04 +08:00
|
|
|
uv_poll_init(pt->uv.io_loop, wsi->w_read.uv.pwatcher,
|
2018-04-29 10:44:36 +08:00
|
|
|
(int)(long long)wsi->desc.filefd);
|
|
|
|
else
|
2018-10-13 11:59:04 +08:00
|
|
|
uv_poll_init_socket(pt->uv.io_loop,
|
2018-11-23 08:47:56 +08:00
|
|
|
wsi->w_read.uv.pwatcher,
|
|
|
|
wsi->desc.sockfd);
|
2018-10-13 11:59:04 +08:00
|
|
|
|
|
|
|
((uv_handle_t *)wsi->w_read.uv.pwatcher)->data = (void *)wsi;
|
|
|
|
|
|
|
|
return 0;
|
2018-04-29 10:44:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
elops_io_uv(struct lws *wsi, int flags)
|
|
|
|
{
|
|
|
|
struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
|
|
|
|
struct lws_io_watcher *w = &wsi->w_read;
|
|
|
|
int current_events = w->actual_events & (UV_READABLE | UV_WRITABLE);
|
|
|
|
|
|
|
|
lwsl_debug("%s: %p: %d\n", __func__, wsi, flags);
|
|
|
|
|
|
|
|
/* w->context is set after the loop is initialized */
|
|
|
|
|
|
|
|
if (!pt->uv.io_loop || !w->context) {
|
|
|
|
lwsl_info("%s: no io loop yet\n", __func__);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!((flags & (LWS_EV_START | LWS_EV_STOP)) &&
|
|
|
|
(flags & (LWS_EV_READ | LWS_EV_WRITE)))) {
|
|
|
|
lwsl_err("%s: assert: flags %d", __func__, flags);
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
|
2019-02-24 06:16:57 +08:00
|
|
|
if (!w->uv.pwatcher || wsi->told_event_loop_closed) {
|
|
|
|
lwsl_err("%s: no watcher\n", __func__);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-29 10:44:36 +08:00
|
|
|
if (flags & LWS_EV_START) {
|
|
|
|
if (flags & LWS_EV_WRITE)
|
|
|
|
current_events |= UV_WRITABLE;
|
|
|
|
|
|
|
|
if (flags & LWS_EV_READ)
|
|
|
|
current_events |= UV_READABLE;
|
|
|
|
|
2018-10-13 11:59:04 +08:00
|
|
|
uv_poll_start(w->uv.pwatcher, current_events, lws_io_cb);
|
2018-04-29 10:44:36 +08:00
|
|
|
} else {
|
|
|
|
if (flags & LWS_EV_WRITE)
|
|
|
|
current_events &= ~UV_WRITABLE;
|
|
|
|
|
|
|
|
if (flags & LWS_EV_READ)
|
|
|
|
current_events &= ~UV_READABLE;
|
|
|
|
|
|
|
|
if (!(current_events & (UV_READABLE | UV_WRITABLE)))
|
2018-10-13 11:59:04 +08:00
|
|
|
uv_poll_stop(w->uv.pwatcher);
|
2018-04-29 10:44:36 +08:00
|
|
|
else
|
2018-10-13 11:59:04 +08:00
|
|
|
uv_poll_start(w->uv.pwatcher, current_events,
|
2018-04-29 10:44:36 +08:00
|
|
|
lws_io_cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
w->actual_events = current_events;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
elops_init_vhost_listen_wsi_uv(struct lws *wsi)
|
|
|
|
{
|
|
|
|
struct lws_context_per_thread *pt;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
if (!wsi)
|
|
|
|
return 0;
|
|
|
|
if (wsi->w_read.context)
|
|
|
|
return 0;
|
|
|
|
|
2018-05-03 10:49:36 +08:00
|
|
|
pt = &wsi->context->pt[(int)wsi->tsi];
|
2018-04-29 10:44:36 +08:00
|
|
|
if (!pt->uv.io_loop)
|
|
|
|
return 0;
|
|
|
|
|
2018-05-03 10:49:36 +08:00
|
|
|
wsi->w_read.context = wsi->context;
|
2018-10-13 11:59:04 +08:00
|
|
|
|
|
|
|
wsi->w_read.uv.pwatcher =
|
|
|
|
lws_malloc(sizeof(*wsi->w_read.uv.pwatcher), "uvh");
|
|
|
|
if (!wsi->w_read.uv.pwatcher)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
n = uv_poll_init_socket(pt->uv.io_loop, wsi->w_read.uv.pwatcher,
|
|
|
|
wsi->desc.sockfd);
|
2018-04-29 10:44:36 +08:00
|
|
|
if (n) {
|
2018-10-13 11:59:04 +08:00
|
|
|
lwsl_err("uv_poll_init failed %d, sockfd=%p\n", n,
|
|
|
|
(void *)(lws_intptr_t)wsi->desc.sockfd);
|
2018-04-29 10:44:36 +08:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2018-10-13 11:59:04 +08:00
|
|
|
|
|
|
|
((uv_handle_t *)wsi->w_read.uv.pwatcher)->data = (void *)wsi;
|
|
|
|
|
2018-04-29 10:44:36 +08:00
|
|
|
elops_io_uv(wsi, LWS_EV_START | LWS_EV_READ);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
elops_run_pt_uv(struct lws_context *context, int tsi)
|
|
|
|
{
|
2018-04-30 09:16:04 +08:00
|
|
|
if (context->pt[tsi].uv.io_loop)
|
2018-04-29 10:44:36 +08:00
|
|
|
uv_run(context->pt[tsi].uv.io_loop, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
elops_destroy_pt_uv(struct lws_context *context, int tsi)
|
|
|
|
{
|
|
|
|
struct lws_context_per_thread *pt = &context->pt[tsi];
|
|
|
|
int m, ns;
|
|
|
|
|
|
|
|
lwsl_info("%s: %d\n", __func__, tsi);
|
|
|
|
|
|
|
|
if (!lws_check_opt(context->options, LWS_SERVER_OPTION_LIBUV))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!pt->uv.io_loop)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (pt->event_loop_destroy_processing_done)
|
|
|
|
return;
|
|
|
|
|
|
|
|
pt->event_loop_destroy_processing_done = 1;
|
|
|
|
|
|
|
|
if (!pt->event_loop_foreign) {
|
|
|
|
uv_signal_stop(&pt->w_sigint.uv.watcher);
|
|
|
|
|
2018-08-16 19:10:32 +08:00
|
|
|
ns = LWS_ARRAY_SIZE(sigs);
|
2018-04-29 10:44:36 +08:00
|
|
|
if (lws_check_opt(context->options,
|
|
|
|
LWS_SERVER_OPTION_UV_NO_SIGSEGV_SIGFPE_SPIN))
|
|
|
|
ns = 2;
|
|
|
|
|
|
|
|
for (m = 0; m < ns; m++) {
|
|
|
|
uv_signal_stop(&pt->uv.signals[m]);
|
|
|
|
uv_close((uv_handle_t *)&pt->uv.signals[m],
|
|
|
|
lws_uv_close_cb_sa);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
lwsl_debug("%s: not closing pt signals\n", __func__);
|
|
|
|
|
|
|
|
uv_timer_stop(&pt->uv.timeout_watcher);
|
|
|
|
uv_close((uv_handle_t *)&pt->uv.timeout_watcher, lws_uv_close_cb_sa);
|
|
|
|
uv_timer_stop(&pt->uv.hrtimer);
|
|
|
|
uv_close((uv_handle_t *)&pt->uv.hrtimer, lws_uv_close_cb_sa);
|
|
|
|
|
|
|
|
uv_idle_stop(&pt->uv.idle);
|
|
|
|
uv_close((uv_handle_t *)&pt->uv.idle, lws_uv_close_cb_sa);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This needs to be called after vhosts have been defined.
|
|
|
|
*
|
|
|
|
* If later, after server start, another vhost is added, this must be
|
|
|
|
* called again to bind the vhost
|
|
|
|
*/
|
|
|
|
|
|
|
|
LWS_VISIBLE int
|
|
|
|
elops_init_pt_uv(struct lws_context *context, void *_loop, int tsi)
|
|
|
|
{
|
|
|
|
struct lws_context_per_thread *pt = &context->pt[tsi];
|
|
|
|
struct lws_vhost *vh = context->vhost_list;
|
|
|
|
int status = 0, n, ns, first = 1;
|
|
|
|
uv_loop_t *loop = (uv_loop_t *)_loop;
|
|
|
|
|
|
|
|
if (!pt->uv.io_loop) {
|
|
|
|
if (!loop) {
|
|
|
|
loop = lws_malloc(sizeof(*loop), "libuv loop");
|
|
|
|
if (!loop) {
|
|
|
|
lwsl_err("OOM\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#if UV_VERSION_MAJOR > 0
|
|
|
|
uv_loop_init(loop);
|
|
|
|
#else
|
|
|
|
lwsl_err("This libuv is too old to work...\n");
|
|
|
|
return 1;
|
|
|
|
#endif
|
|
|
|
pt->event_loop_foreign = 0;
|
|
|
|
} else {
|
|
|
|
lwsl_notice(" Using foreign event loop...\n");
|
|
|
|
pt->event_loop_foreign = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
pt->uv.io_loop = loop;
|
|
|
|
uv_idle_init(loop, &pt->uv.idle);
|
|
|
|
LWS_UV_REFCOUNT_STATIC_HANDLE_NEW(&pt->uv.idle, context);
|
|
|
|
|
|
|
|
|
2018-08-16 19:10:32 +08:00
|
|
|
ns = LWS_ARRAY_SIZE(sigs);
|
2018-04-29 10:44:36 +08:00
|
|
|
if (lws_check_opt(context->options,
|
|
|
|
LWS_SERVER_OPTION_UV_NO_SIGSEGV_SIGFPE_SPIN))
|
|
|
|
ns = 2;
|
|
|
|
|
|
|
|
if (!pt->event_loop_foreign) {
|
2018-08-16 19:10:32 +08:00
|
|
|
assert(ns <= (int)LWS_ARRAY_SIZE(pt->uv.signals));
|
2018-04-29 10:44:36 +08:00
|
|
|
for (n = 0; n < ns; n++) {
|
|
|
|
uv_signal_init(loop, &pt->uv.signals[n]);
|
|
|
|
LWS_UV_REFCOUNT_STATIC_HANDLE_NEW(&pt->uv.signals[n],
|
|
|
|
context);
|
|
|
|
pt->uv.signals[n].data = pt->context;
|
|
|
|
uv_signal_start(&pt->uv.signals[n],
|
|
|
|
lws_uv_signal_handler, sigs[n]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
first = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize the accept wsi read watcher with all the listening sockets
|
|
|
|
* and register a callback for read operations
|
|
|
|
*
|
|
|
|
* We have to do it here because the uv loop(s) are not
|
|
|
|
* initialized until after context creation.
|
|
|
|
*/
|
|
|
|
while (vh) {
|
|
|
|
if (elops_init_vhost_listen_wsi_uv(vh->lserv_wsi) == -1)
|
|
|
|
return -1;
|
|
|
|
vh = vh->vhost_next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!first)
|
|
|
|
return status;
|
|
|
|
|
|
|
|
uv_timer_init(pt->uv.io_loop, &pt->uv.timeout_watcher);
|
|
|
|
LWS_UV_REFCOUNT_STATIC_HANDLE_NEW(&pt->uv.timeout_watcher, context);
|
|
|
|
uv_timer_start(&pt->uv.timeout_watcher, lws_uv_timeout_cb, 10, 1000);
|
|
|
|
|
|
|
|
uv_timer_init(pt->uv.io_loop, &pt->uv.hrtimer);
|
|
|
|
LWS_UV_REFCOUNT_STATIC_HANDLE_NEW(&pt->uv.hrtimer, context);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
lws_libuv_closewsi(uv_handle_t* handle)
|
|
|
|
{
|
2018-10-13 11:59:04 +08:00
|
|
|
struct lws *wsi = (struct lws *)handle->data;
|
2018-04-29 10:44:36 +08:00
|
|
|
struct lws_context *context = lws_get_context(wsi);
|
|
|
|
struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
|
2019-05-09 07:28:36 +01:00
|
|
|
#if !defined(LWS_WITHOUT_SERVER)
|
|
|
|
int lspd = 0;
|
|
|
|
#endif
|
2018-04-29 10:44:36 +08:00
|
|
|
|
|
|
|
lwsl_info("%s: %p\n", __func__, wsi);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We get called back here for every wsi that closes
|
|
|
|
*/
|
|
|
|
|
2019-05-09 07:28:36 +01:00
|
|
|
#if !defined(LWS_WITHOUT_SERVER)
|
2018-04-29 10:44:36 +08:00
|
|
|
if (wsi->role_ops == &role_ops_listen && wsi->context->deprecated) {
|
|
|
|
lspd = 1;
|
|
|
|
context->deprecation_pending_listen_close_count--;
|
|
|
|
if (!context->deprecation_pending_listen_close_count)
|
|
|
|
lspd = 2;
|
|
|
|
}
|
2019-05-09 07:28:36 +01:00
|
|
|
#endif
|
2018-04-29 10:44:36 +08:00
|
|
|
|
|
|
|
lws_pt_lock(pt, __func__);
|
|
|
|
__lws_close_free_wsi_final(wsi);
|
|
|
|
lws_pt_unlock(pt);
|
|
|
|
|
2018-10-13 11:59:04 +08:00
|
|
|
/* it's our job to close the handle finally */
|
|
|
|
lws_free(handle);
|
|
|
|
|
2019-05-09 07:28:36 +01:00
|
|
|
#if !defined(LWS_WITHOUT_SERVER)
|
2018-04-29 10:44:36 +08:00
|
|
|
if (lspd == 2 && context->deprecation_cb) {
|
|
|
|
lwsl_notice("calling deprecation callback\n");
|
|
|
|
context->deprecation_cb();
|
|
|
|
}
|
2019-05-09 07:28:36 +01:00
|
|
|
#endif
|
2018-04-29 10:44:36 +08:00
|
|
|
|
|
|
|
lwsl_info("%s: sa left %d: dyn left: %d (rk %d)\n", __func__,
|
|
|
|
context->count_event_loop_static_asset_handles,
|
|
|
|
context->count_wsi_allocated, context->requested_kill);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* eventually, we closed all the wsi...
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (context->requested_kill && !context->count_wsi_allocated) {
|
|
|
|
struct lws_vhost *vh = context->vhost_list;
|
2019-05-09 07:28:36 +01:00
|
|
|
int m;
|
2018-04-29 10:44:36 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Start Closing Phase 2: close of static handles
|
|
|
|
*/
|
|
|
|
|
|
|
|
lwsl_info("%s: all lws dynamic handles down, closing static\n",
|
|
|
|
__func__);
|
|
|
|
|
|
|
|
for (m = 0; m < context->count_threads; m++)
|
|
|
|
elops_destroy_pt_uv(context, m);
|
|
|
|
|
|
|
|
/* protocols may have initialized libuv objects */
|
|
|
|
|
|
|
|
while (vh) {
|
|
|
|
lws_vhost_destroy1(vh);
|
|
|
|
vh = vh->vhost_next;
|
|
|
|
}
|
|
|
|
|
2018-05-02 18:35:58 +08:00
|
|
|
if (!context->count_event_loop_static_asset_handles &&
|
|
|
|
context->pt[0].event_loop_foreign) {
|
|
|
|
lwsl_info("%s: call lws_context_destroy2\n", __func__);
|
2018-04-29 10:44:36 +08:00
|
|
|
lws_context_destroy2(context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
lws_libuv_closehandle(struct lws *wsi)
|
|
|
|
{
|
2018-10-13 11:59:04 +08:00
|
|
|
uv_handle_t* handle;
|
|
|
|
|
|
|
|
if (!wsi->w_read.uv.pwatcher)
|
|
|
|
return;
|
|
|
|
|
2018-04-29 10:44:36 +08:00
|
|
|
if (wsi->told_event_loop_closed) {
|
2019-04-05 21:13:59 +08:00
|
|
|
// assert(0);
|
2018-04-29 10:44:36 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
lwsl_debug("%s: %p\n", __func__, wsi);
|
|
|
|
|
|
|
|
wsi->told_event_loop_closed = 1;
|
|
|
|
|
2018-10-13 11:59:04 +08:00
|
|
|
/*
|
|
|
|
* The normal close path attaches the related wsi as the
|
|
|
|
* handle->data.
|
|
|
|
*/
|
|
|
|
|
|
|
|
handle = (uv_handle_t *)wsi->w_read.uv.pwatcher;
|
|
|
|
|
|
|
|
/* ensure we can only do this once */
|
|
|
|
|
|
|
|
wsi->w_read.uv.pwatcher = NULL;
|
|
|
|
|
|
|
|
uv_close(handle, lws_libuv_closewsi);
|
2018-04-29 10:44:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct lws_event_loop_ops event_loop_ops_uv = {
|
|
|
|
/* name */ "libuv",
|
|
|
|
/* init_context */ elops_init_context_uv,
|
|
|
|
/* destroy_context1 */ elops_destroy_context1_uv,
|
|
|
|
/* destroy_context2 */ elops_destroy_context2_uv,
|
|
|
|
/* init_vhost_listen_wsi */ elops_init_vhost_listen_wsi_uv,
|
|
|
|
/* init_pt */ elops_init_pt_uv,
|
|
|
|
/* wsi_logical_close */ elops_wsi_logical_close_uv,
|
|
|
|
/* check_client_connect_ok */ elops_check_client_connect_ok_uv,
|
|
|
|
/* close_handle_manually */ elops_close_handle_manually_uv,
|
|
|
|
/* accept */ elops_accept_uv,
|
|
|
|
/* io */ elops_io_uv,
|
|
|
|
/* run_pt */ elops_run_pt_uv,
|
|
|
|
/* destroy_pt */ elops_destroy_pt_uv,
|
|
|
|
/* destroy wsi */ NULL,
|
|
|
|
|
|
|
|
/* periodic_events_available */ 0,
|
|
|
|
};
|