2018-06-16 09:37:07 +08:00
|
|
|
/*
|
|
|
|
* libwebsockets - small server side websockets and web server implementation
|
|
|
|
*
|
2020-08-27 15:37:14 +01:00
|
|
|
* Copyright (C) 2010 - 2020 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_plat_pipe_create(struct lws *wsi)
|
|
|
|
{
|
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_context_per_thread *pt = &wsi->a.context->pt[(int)wsi->tsi];
|
2021-07-11 10:53:16 +01:00
|
|
|
int n;
|
2020-08-27 15:37:14 +01:00
|
|
|
|
2020-02-09 05:13:18 +00:00
|
|
|
#if defined(LWS_HAVE_EVENTFD)
|
2020-08-27 15:37:14 +01:00
|
|
|
pt->dummy_pipe_fds[0] = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
|
2020-02-09 05:13:18 +00:00
|
|
|
pt->dummy_pipe_fds[1] = -1;
|
2020-08-27 15:37:14 +01:00
|
|
|
|
2021-07-11 10:53:16 +01:00
|
|
|
n = pt->dummy_pipe_fds[0] < 0 ? -1 : 0;
|
|
|
|
goto set;
|
2020-08-27 15:37:14 +01:00
|
|
|
|
2020-02-09 05:13:18 +00:00
|
|
|
#elif defined(LWS_HAVE_PIPE2)
|
2021-07-11 10:53:16 +01:00
|
|
|
n = pipe2(pt->dummy_pipe_fds, O_NONBLOCK);
|
2018-06-16 09:37:07 +08:00
|
|
|
#else
|
2021-07-11 10:53:16 +01:00
|
|
|
n = pipe(pt->dummy_pipe_fds);
|
2018-06-16 09:37:07 +08:00
|
|
|
#endif
|
2021-07-11 10:53:16 +01:00
|
|
|
|
|
|
|
#if defined(LWS_HAVE_EVENTFD)
|
|
|
|
set:
|
|
|
|
#endif
|
|
|
|
if (n >= 0) {
|
|
|
|
if (fcntl(pt->dummy_pipe_fds[0], F_SETFL, O_NONBLOCK) < 0)
|
|
|
|
n = -1;
|
|
|
|
else if (pt->dummy_pipe_fds[1] >= 0) {
|
|
|
|
if (fcntl(pt->dummy_pipe_fds[1], F_SETFL, O_NONBLOCK) < 0)
|
|
|
|
n = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
2018-06-16 09:37:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2020-11-16 19:32:58 +00:00
|
|
|
lws_plat_pipe_signal(struct lws_context *ctx, int tsi)
|
2018-06-16 09:37:07 +08:00
|
|
|
{
|
2020-11-16 19:32:58 +00:00
|
|
|
struct lws_context_per_thread *pt = &ctx->pt[tsi];
|
2020-02-09 05:13:18 +00:00
|
|
|
#if defined(LWS_HAVE_EVENTFD)
|
|
|
|
eventfd_t value = 1;
|
2020-08-27 15:37:14 +01:00
|
|
|
|
2020-02-09 05:13:18 +00:00
|
|
|
return eventfd_write(pt->dummy_pipe_fds[0], value);
|
|
|
|
#else
|
2018-06-16 09:37:07 +08:00
|
|
|
char buf = 0;
|
|
|
|
int n;
|
|
|
|
|
2020-12-12 06:21:40 +00:00
|
|
|
n = (int)write(pt->dummy_pipe_fds[1], &buf, 1);
|
2018-06-16 09:37:07 +08:00
|
|
|
|
|
|
|
return n != 1;
|
2020-02-09 05:13:18 +00:00
|
|
|
#endif
|
2018-06-16 09:37:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
lws_plat_pipe_close(struct lws *wsi)
|
|
|
|
{
|
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_context_per_thread *pt = &wsi->a.context->pt[(int)wsi->tsi];
|
2018-06-16 09:37:07 +08:00
|
|
|
|
|
|
|
if (pt->dummy_pipe_fds[0] && pt->dummy_pipe_fds[0] != -1)
|
|
|
|
close(pt->dummy_pipe_fds[0]);
|
|
|
|
if (pt->dummy_pipe_fds[1] && pt->dummy_pipe_fds[1] != -1)
|
|
|
|
close(pt->dummy_pipe_fds[1]);
|
|
|
|
|
|
|
|
pt->dummy_pipe_fds[0] = pt->dummy_pipe_fds[1] = -1;
|
|
|
|
}
|