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
|
|
|
|
|
|
|
int
|
|
|
|
lws_poll_listen_fd(struct lws_pollfd *fd)
|
|
|
|
{
|
|
|
|
return poll(fd, 1, 0);
|
|
|
|
}
|
|
|
|
|
2019-09-16 15:36:12 +01:00
|
|
|
int
|
|
|
|
_lws_plat_service_forced_tsi(struct lws_context *context, int tsi)
|
|
|
|
{
|
|
|
|
struct lws_context_per_thread *pt = &context->pt[tsi];
|
2020-01-22 19:15:04 +00:00
|
|
|
int m, n, r;
|
2019-09-16 15:36:12 +01:00
|
|
|
|
2020-01-22 19:15:04 +00:00
|
|
|
r = lws_service_flag_pending(context, tsi);
|
2019-09-16 15:36:12 +01:00
|
|
|
|
|
|
|
/* any socket with events to service? */
|
|
|
|
for (n = 0; n < (int)pt->fds_count; n++) {
|
2021-03-14 13:53:48 +00:00
|
|
|
lws_sockfd_type fd = pt->fds[n].fd;
|
|
|
|
|
2019-09-16 15:36:12 +01:00
|
|
|
if (!pt->fds[n].revents)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
m = lws_service_fd_tsi(context, &pt->fds[n], tsi);
|
|
|
|
if (m < 0) {
|
|
|
|
lwsl_err("%s: lws_service_fd_tsi returned %d\n",
|
|
|
|
__func__, m);
|
|
|
|
return -1;
|
|
|
|
}
|
2021-03-16 13:32:05 +00:00
|
|
|
|
2021-03-14 13:53:48 +00:00
|
|
|
/* if something closed, retry this slot since may have been
|
|
|
|
* swapped with end fd */
|
|
|
|
if (m && pt->fds[n].fd != fd)
|
2019-09-16 15:36:12 +01:00
|
|
|
n--;
|
|
|
|
}
|
|
|
|
|
|
|
|
lws_service_do_ripe_rxflow(pt);
|
|
|
|
|
2020-01-22 19:15:04 +00:00
|
|
|
return r;
|
2019-09-16 15:36:12 +01:00
|
|
|
}
|
|
|
|
|
2019-10-27 06:17:35 +00:00
|
|
|
#define LWS_POLL_WAIT_LIMIT 2000000000
|
|
|
|
|
2019-09-16 15:36:12 +01:00
|
|
|
int
|
2018-06-16 09:37:07 +08:00
|
|
|
_lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi)
|
|
|
|
{
|
|
|
|
volatile struct lws_foreign_thread_pollfd *ftp, *next;
|
|
|
|
volatile struct lws_context_per_thread *vpt;
|
|
|
|
struct lws_context_per_thread *pt;
|
2019-11-01 07:07:33 +00:00
|
|
|
lws_usec_t timeout_us, us;
|
2021-01-06 15:08:22 +00:00
|
|
|
#if defined(LWS_WITH_SYS_METRICS)
|
|
|
|
lws_usec_t a, b;
|
|
|
|
#endif
|
2020-08-18 13:25:47 +01:00
|
|
|
int n;
|
2019-09-16 15:36:12 +01:00
|
|
|
#if (defined(LWS_ROLE_WS) && !defined(LWS_WITHOUT_EXTENSIONS)) || defined(LWS_WITH_TLS)
|
|
|
|
int m;
|
|
|
|
#endif
|
2018-06-16 09:37:07 +08:00
|
|
|
|
|
|
|
/* stay dead once we are dead */
|
|
|
|
|
2020-06-24 20:15:46 +01:00
|
|
|
if (!context)
|
2018-06-16 09:37:07 +08:00
|
|
|
return 1;
|
|
|
|
|
2021-01-06 15:08:22 +00:00
|
|
|
#if defined(LWS_WITH_SYS_METRICS)
|
|
|
|
b =
|
|
|
|
#endif
|
|
|
|
us = lws_now_usecs();
|
|
|
|
|
2018-06-16 09:37:07 +08:00
|
|
|
pt = &context->pt[tsi];
|
|
|
|
vpt = (volatile struct lws_context_per_thread *)pt;
|
|
|
|
|
|
|
|
if (timeout_ms < 0)
|
2019-09-16 15:36:12 +01:00
|
|
|
timeout_ms = 0;
|
|
|
|
else
|
|
|
|
/* force a default timeout of 23 days */
|
2019-10-27 06:17:35 +00:00
|
|
|
timeout_ms = LWS_POLL_WAIT_LIMIT;
|
2019-08-09 10:12:09 +01:00
|
|
|
timeout_us = ((lws_usec_t)timeout_ms) * LWS_US_PER_MS;
|
|
|
|
|
2018-06-16 09:37:07 +08:00
|
|
|
if (context->event_loop_ops->run_pt)
|
|
|
|
context->event_loop_ops->run_pt(context, tsi);
|
|
|
|
|
2020-06-24 20:15:46 +01:00
|
|
|
if (!pt->service_tid_detected && context->vhost_list) {
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
lws_fakewsi_def_plwsa(pt);
|
2018-06-16 09:37:07 +08:00
|
|
|
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
lws_fakewsi_prep_plwsa_ctx(context);
|
2018-06-16 09:37:07 +08:00
|
|
|
|
2019-08-08 06:30:14 +01:00
|
|
|
pt->service_tid = context->vhost_list->protocols[0].callback(
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
(struct lws *)plwsa,
|
|
|
|
LWS_CALLBACK_GET_THREAD_ID,
|
2019-08-08 06:30:14 +01:00
|
|
|
NULL, NULL, 0);
|
2018-07-25 16:26:41 +08:00
|
|
|
pt->service_tid_detected = 1;
|
2018-06-16 09:37:07 +08:00
|
|
|
}
|
|
|
|
|
2019-11-01 07:07:33 +00:00
|
|
|
lws_pt_lock(pt, __func__);
|
|
|
|
/*
|
|
|
|
* service ripe scheduled events, and limit wait to next expected one
|
|
|
|
*/
|
2020-05-28 12:48:17 +01:00
|
|
|
us = __lws_sul_service_ripe(pt->pt_sul_owner, LWS_COUNT_PT_SUL_OWNERS, us);
|
2019-11-01 07:07:33 +00:00
|
|
|
if (us && us < timeout_us)
|
2021-01-17 11:27:59 +00:00
|
|
|
/*
|
|
|
|
* If something wants zero wait, that's OK, but if the next sul
|
|
|
|
* coming ripe is an interval less than our wait resolution,
|
|
|
|
* bump it to be the wait resolution.
|
|
|
|
*/
|
|
|
|
timeout_us = us < context->us_wait_resolution ?
|
|
|
|
context->us_wait_resolution : us;
|
2019-11-01 07:07:33 +00:00
|
|
|
|
|
|
|
lws_pt_unlock(pt);
|
|
|
|
|
2018-06-16 09:37:07 +08:00
|
|
|
/*
|
|
|
|
* is there anybody with pending stuff that needs service forcing?
|
|
|
|
*/
|
2019-11-01 07:07:33 +00:00
|
|
|
if (!lws_service_adjust_timeout(context, 1, tsi))
|
|
|
|
timeout_us = 0;
|
2018-06-16 09:37:07 +08:00
|
|
|
|
2019-11-01 07:07:33 +00:00
|
|
|
/* ensure we don't wrap at 2^31 with poll()'s signed int ms */
|
2019-08-08 06:30:14 +01:00
|
|
|
|
2019-11-01 07:07:33 +00:00
|
|
|
timeout_us /= LWS_US_PER_MS; /* ms now */
|
2018-06-16 09:37:07 +08:00
|
|
|
|
2021-01-25 11:40:54 +00:00
|
|
|
#if defined(LWS_WITH_SYS_METRICS)
|
|
|
|
a = lws_now_usecs() - b;
|
|
|
|
#endif
|
2019-11-01 07:07:33 +00:00
|
|
|
vpt->inside_poll = 1;
|
|
|
|
lws_memory_barrier();
|
2020-12-12 06:21:40 +00:00
|
|
|
n = poll(pt->fds, pt->fds_count, (int)timeout_us /* ms now */ );
|
2019-11-01 07:07:33 +00:00
|
|
|
vpt->inside_poll = 0;
|
|
|
|
lws_memory_barrier();
|
2018-06-16 09:37:07 +08:00
|
|
|
|
2021-01-06 15:08:22 +00:00
|
|
|
#if defined(LWS_WITH_SYS_METRICS)
|
|
|
|
b = lws_now_usecs();
|
2019-11-01 07:07:33 +00:00
|
|
|
#endif
|
|
|
|
/* Collision will be rare and brief. Spin until it completes */
|
|
|
|
while (vpt->foreign_spinlock)
|
|
|
|
;
|
2019-10-27 06:17:35 +00:00
|
|
|
|
2019-11-01 07:07:33 +00:00
|
|
|
/*
|
|
|
|
* At this point we are not inside a foreign thread pollfd
|
|
|
|
* change, and we have marked ourselves as outside the poll()
|
|
|
|
* wait. So we are the only guys that can modify the
|
|
|
|
* lws_foreign_thread_pollfd list on the pt. Drain the list
|
|
|
|
* and apply the changes to the affected pollfds in the correct
|
|
|
|
* order.
|
|
|
|
*/
|
2019-09-16 15:36:12 +01:00
|
|
|
|
2019-11-01 07:07:33 +00:00
|
|
|
lws_pt_lock(pt, __func__);
|
|
|
|
|
|
|
|
ftp = vpt->foreign_pfd_list;
|
|
|
|
//lwsl_notice("cleared list %p\n", ftp);
|
|
|
|
while (ftp) {
|
|
|
|
struct lws *wsi;
|
|
|
|
struct lws_pollfd *pfd;
|
|
|
|
|
|
|
|
next = ftp->next;
|
|
|
|
pfd = &vpt->fds[ftp->fd_index];
|
|
|
|
if (lws_socket_is_valid(pfd->fd)) {
|
|
|
|
wsi = wsi_from_fd(context, pfd->fd);
|
|
|
|
if (wsi)
|
|
|
|
__lws_change_pollfd(wsi, ftp->_and,
|
|
|
|
ftp->_or);
|
2018-06-16 09:37:07 +08:00
|
|
|
}
|
2019-11-01 07:07:33 +00:00
|
|
|
lws_free((void *)ftp);
|
|
|
|
ftp = next;
|
|
|
|
}
|
|
|
|
vpt->foreign_pfd_list = NULL;
|
|
|
|
lws_memory_barrier();
|
2018-06-16 09:37:07 +08:00
|
|
|
|
2019-11-01 07:07:33 +00:00
|
|
|
lws_pt_unlock(pt);
|
2018-06-16 09:37:07 +08:00
|
|
|
|
2019-09-16 15:36:12 +01:00
|
|
|
#if (defined(LWS_ROLE_WS) && !defined(LWS_WITHOUT_EXTENSIONS)) || defined(LWS_WITH_TLS)
|
2019-11-01 07:07:33 +00:00
|
|
|
m = 0;
|
2019-09-16 15:36:12 +01:00
|
|
|
#endif
|
2018-06-16 09:37:07 +08:00
|
|
|
#if defined(LWS_ROLE_WS) && !defined(LWS_WITHOUT_EXTENSIONS)
|
2019-11-01 07:07:33 +00:00
|
|
|
m |= !!pt->ws.rx_draining_ext_list;
|
2018-06-16 09:37:07 +08:00
|
|
|
#endif
|
|
|
|
|
2019-01-13 06:58:21 +08:00
|
|
|
#if defined(LWS_WITH_TLS)
|
2019-11-01 07:07:33 +00:00
|
|
|
if (pt->context->tls_ops &&
|
|
|
|
pt->context->tls_ops->fake_POLLIN_for_buffered)
|
|
|
|
m |= pt->context->tls_ops->fake_POLLIN_for_buffered(pt);
|
2019-01-13 06:58:21 +08:00
|
|
|
#endif
|
2018-06-16 09:37:07 +08:00
|
|
|
|
2019-11-01 07:07:33 +00:00
|
|
|
if (
|
2019-02-15 08:46:30 +08:00
|
|
|
#if (defined(LWS_ROLE_WS) && !defined(LWS_WITHOUT_EXTENSIONS)) || defined(LWS_WITH_TLS)
|
2019-11-01 07:07:33 +00:00
|
|
|
!m &&
|
2019-02-15 08:46:30 +08:00
|
|
|
#endif
|
2021-01-06 15:08:22 +00:00
|
|
|
!n) /* nothing to do */
|
2019-11-01 07:07:33 +00:00
|
|
|
lws_service_do_ripe_rxflow(pt);
|
2021-01-06 15:08:22 +00:00
|
|
|
else
|
|
|
|
if (_lws_plat_service_forced_tsi(context, tsi) < 0)
|
|
|
|
return -1;
|
2018-06-16 09:37:07 +08:00
|
|
|
|
2021-01-06 15:08:22 +00:00
|
|
|
#if defined(LWS_WITH_SYS_METRICS)
|
|
|
|
lws_metric_event(context->mt_service, METRES_GO,
|
|
|
|
(u_mt_t) (a + (lws_now_usecs() - b)));
|
|
|
|
#endif
|
2018-06-16 09:37:07 +08:00
|
|
|
|
2020-01-23 11:12:28 +00:00
|
|
|
if (pt->destroy_self) {
|
|
|
|
lws_context_destroy(pt->context);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-06-16 09:37:07 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
lws_plat_service(struct lws_context *context, int timeout_ms)
|
|
|
|
{
|
|
|
|
return _lws_plat_service_tsi(context, timeout_ms, 0);
|
|
|
|
}
|