2018-04-11 13:39:42 +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-04-11 13:39:42 +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-04-11 13:39:42 +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-04-11 13:39:42 +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-04-11 13:39:42 +08:00
|
|
|
*/
|
|
|
|
|
2019-08-15 10:49:52 +01:00
|
|
|
#include <private-lib-core.h>
|
2018-04-11 13:39:42 +08:00
|
|
|
|
|
|
|
static int
|
|
|
|
rops_handle_POLLIN_pipe(struct lws_context_per_thread *pt, struct lws *wsi,
|
|
|
|
struct lws_pollfd *pollfd)
|
|
|
|
{
|
2020-02-09 05:13:18 +00:00
|
|
|
#if defined(LWS_HAVE_EVENTFD)
|
|
|
|
eventfd_t value;
|
2020-08-27 15:37:14 +01:00
|
|
|
int n;
|
|
|
|
|
|
|
|
n = eventfd_read(wsi->desc.sockfd, &value);
|
|
|
|
if (n < 0) {
|
|
|
|
lwsl_notice("%s: eventfd read %d bailed errno %d\n", __func__,
|
|
|
|
wsi->desc.sockfd, LWS_ERRNO);
|
2020-02-09 05:13:18 +00:00
|
|
|
return LWS_HPI_RET_PLEASE_CLOSE_ME;
|
2020-08-27 15:37:14 +01:00
|
|
|
}
|
2020-02-09 05:13:18 +00:00
|
|
|
#elif !defined(WIN32) && !defined(_WIN32)
|
2018-04-12 21:37:59 +08:00
|
|
|
char s[100];
|
2018-04-11 13:39:42 +08:00
|
|
|
int n;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* discard the byte(s) that signaled us
|
|
|
|
* We really don't care about the number of bytes, but coverity
|
|
|
|
* thinks we should.
|
|
|
|
*/
|
2020-12-12 06:21:40 +00:00
|
|
|
n = (int)read(wsi->desc.sockfd, s, sizeof(s));
|
2018-04-11 13:39:42 +08:00
|
|
|
(void)n;
|
|
|
|
if (n < 0)
|
2018-04-17 15:35:15 +08:00
|
|
|
return LWS_HPI_RET_PLEASE_CLOSE_ME;
|
2021-01-27 15:56:23 +08:00
|
|
|
#elif defined(WIN32)
|
|
|
|
char s[100];
|
|
|
|
int n;
|
|
|
|
|
|
|
|
n = recv(wsi->desc.sockfd, s, sizeof(s), 0);
|
|
|
|
if (n == SOCKET_ERROR)
|
|
|
|
return LWS_HPI_RET_PLEASE_CLOSE_ME;
|
2018-04-11 13:39:42 +08:00
|
|
|
#endif
|
2018-09-02 14:35:37 +08:00
|
|
|
|
2022-04-11 06:42:53 +01:00
|
|
|
#if defined(LWS_WITH_THREADPOOL) && defined(LWS_HAVE_PTHREAD_H)
|
2018-09-02 14:35:37 +08:00
|
|
|
/*
|
|
|
|
* threadpools that need to call for on_writable callbacks do it by
|
|
|
|
* marking the task as needing one for its wsi, then cancelling service.
|
|
|
|
*
|
|
|
|
* Each tsi will call this to perform the actual callback_on_writable
|
|
|
|
* from the correct service thread context
|
|
|
|
*/
|
|
|
|
lws_threadpool_tsi_context(pt->context, pt->tid);
|
|
|
|
#endif
|
|
|
|
|
2021-01-22 09:11:22 +00:00
|
|
|
#if LWS_MAX_SMP > 1
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Other pts need to take care of their own wsi bound to a vhost that
|
|
|
|
* is going down
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (pt->context->owner_vh_being_destroyed.head) {
|
|
|
|
|
|
|
|
lws_start_foreach_dll_safe(struct lws_dll2 *, d, d1,
|
|
|
|
pt->context->owner_vh_being_destroyed.head) {
|
|
|
|
struct lws_vhost *v =
|
|
|
|
lws_container_of(d, struct lws_vhost,
|
|
|
|
vh_being_destroyed_list);
|
|
|
|
|
|
|
|
lws_vhost_lock(v); /* -------------- vh { */
|
|
|
|
__lws_vhost_destroy_pt_wsi_dieback_start(v);
|
|
|
|
lws_vhost_unlock(v); /* } vh -------------- */
|
|
|
|
|
|
|
|
} lws_end_foreach_dll_safe(d, d1);
|
|
|
|
}
|
|
|
|
|
2021-04-13 08:51:27 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(LWS_WITH_SECURE_STREAMS)
|
|
|
|
lws_dll2_foreach_safe(&pt->ss_owner, NULL, lws_ss_cancel_notify_dll);
|
|
|
|
#if defined(LWS_WITH_SECURE_STREAMS_PROXY_API) && defined(LWS_WITH_CLIENT)
|
|
|
|
lws_dll2_foreach_safe(&pt->ss_client_owner, NULL, lws_sspc_cancel_notify_dll);
|
|
|
|
#endif
|
2021-01-22 09:11:22 +00:00
|
|
|
#endif
|
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
/*
|
|
|
|
* the poll() wait, or the event loop for libuv etc is a
|
|
|
|
* process-wide resource that we interrupted. So let every
|
|
|
|
* protocol that may be interested in the pipe event know that
|
|
|
|
* it happened.
|
|
|
|
*/
|
2019-08-09 10:12:09 +01:00
|
|
|
if (lws_broadcast(pt, LWS_CALLBACK_EVENT_WAIT_CANCELLED, NULL, 0)) {
|
2018-04-11 13:39:42 +08:00
|
|
|
lwsl_info("closed in event cancel\n");
|
2018-04-17 15:35:15 +08:00
|
|
|
return LWS_HPI_RET_PLEASE_CLOSE_ME;
|
2018-04-11 13:39:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return LWS_HPI_RET_HANDLED;
|
|
|
|
}
|
|
|
|
|
roles: compress role ops structs
role ops are usually only sparsely filled, there are currently 20
function pointers but several roles only fill in two. No single
role has more than 14 of the ops. On a 32/64 bit build this part
of the ops struct takes a fixed 80 / 160 bytes then.
First reduce the type of the callback reason part from uint16_t to
uint8_t, this saves 12 bytes unconditionally.
Change to a separate function pointer array with a nybble index
array, it costs 10 bytes for the index and a pointer to the
separate array, for 32-bit the cost is
2 + (4 x ops_used)
and for 64-bit
6 + (8 x ops_used)
for 2 x ops_used it means 32-bit: 10 vs 80 / 64-bit: 22 vs 160
For a typical system with h1 (9), h2 (14), listen (2), netlink (2),
pipe (1), raw_skt (3), ws (12), == 43 ops_used out of 140, it means
the .rodata for this reduced from 32-bit: 560 -> 174 (386 byte
saving) and 64-bit: 1120 -> 350 (770 byte saving)
This doesn't account for the changed function ops calling code, two
ways were tried, a preprocessor macro and explicit functions
For an x86_64 gcc 10 build with most options, release mode,
.text + .rodata
before patch: 553282
accessor macro: 552714 (568 byte saving)
accessor functions: 553674 (392 bytes worse than without patch)
therefore we went with the macros
2020-10-19 13:55:21 +01:00
|
|
|
static const lws_rops_t rops_table_pipe[] = {
|
|
|
|
/* 1 */ { .handle_POLLIN = rops_handle_POLLIN_pipe },
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2020-01-15 06:31:19 +00:00
|
|
|
const struct lws_role_ops role_ops_pipe = {
|
2018-04-12 15:56:38 +08:00
|
|
|
/* role name */ "pipe",
|
|
|
|
/* alpn id */ NULL,
|
roles: compress role ops structs
role ops are usually only sparsely filled, there are currently 20
function pointers but several roles only fill in two. No single
role has more than 14 of the ops. On a 32/64 bit build this part
of the ops struct takes a fixed 80 / 160 bytes then.
First reduce the type of the callback reason part from uint16_t to
uint8_t, this saves 12 bytes unconditionally.
Change to a separate function pointer array with a nybble index
array, it costs 10 bytes for the index and a pointer to the
separate array, for 32-bit the cost is
2 + (4 x ops_used)
and for 64-bit
6 + (8 x ops_used)
for 2 x ops_used it means 32-bit: 10 vs 80 / 64-bit: 22 vs 160
For a typical system with h1 (9), h2 (14), listen (2), netlink (2),
pipe (1), raw_skt (3), ws (12), == 43 ops_used out of 140, it means
the .rodata for this reduced from 32-bit: 560 -> 174 (386 byte
saving) and 64-bit: 1120 -> 350 (770 byte saving)
This doesn't account for the changed function ops calling code, two
ways were tried, a preprocessor macro and explicit functions
For an x86_64 gcc 10 build with most options, release mode,
.text + .rodata
before patch: 553282
accessor macro: 552714 (568 byte saving)
accessor functions: 553674 (392 bytes worse than without patch)
therefore we went with the macros
2020-10-19 13:55:21 +01:00
|
|
|
|
|
|
|
/* rops_table */ rops_table_pipe,
|
|
|
|
/* rops_idx */ {
|
|
|
|
/* LWS_ROPS_check_upgrades */
|
|
|
|
/* LWS_ROPS_pt_init_destroy */ 0x00,
|
|
|
|
/* LWS_ROPS_init_vhost */
|
|
|
|
/* LWS_ROPS_destroy_vhost */ 0x00,
|
|
|
|
/* LWS_ROPS_service_flag_pending */
|
|
|
|
/* LWS_ROPS_handle_POLLIN */ 0x01,
|
|
|
|
/* LWS_ROPS_handle_POLLOUT */
|
|
|
|
/* LWS_ROPS_perform_user_POLLOUT */ 0x00,
|
|
|
|
/* LWS_ROPS_callback_on_writable */
|
|
|
|
/* LWS_ROPS_tx_credit */ 0x00,
|
|
|
|
/* LWS_ROPS_write_role_protocol */
|
|
|
|
/* LWS_ROPS_encapsulation_parent */ 0x00,
|
|
|
|
/* LWS_ROPS_alpn_negotiated */
|
|
|
|
/* LWS_ROPS_close_via_role_protocol */ 0x00,
|
|
|
|
/* LWS_ROPS_close_role */
|
|
|
|
/* LWS_ROPS_close_kill_connection */ 0x00,
|
|
|
|
/* LWS_ROPS_destroy_role */
|
|
|
|
/* LWS_ROPS_adoption_bind */ 0x00,
|
|
|
|
/* LWS_ROPS_client_bind */
|
|
|
|
/* LWS_ROPS_issue_keepalive */ 0x00,
|
|
|
|
},
|
|
|
|
|
2018-11-29 08:29:48 +08:00
|
|
|
/* adoption_cb clnt, srv */ { 0, 0 },
|
|
|
|
/* rx_cb clnt, srv */ { 0, 0 },
|
2018-04-11 13:39:42 +08:00
|
|
|
/* writeable cb clnt, srv */ { 0, 0 },
|
|
|
|
/* close cb clnt, srv */ { 0, 0 },
|
2018-08-18 14:11:29 +08:00
|
|
|
/* protocol_bind_cb c,s */ { 0, 0 },
|
|
|
|
/* protocol_unbind_cb c,s */ { 0, 0 },
|
2021-04-04 17:01:32 +01:00
|
|
|
#if defined(WIN32)
|
|
|
|
/* file_handle (no, UDP) */ 0,
|
|
|
|
#else
|
2018-04-29 10:44:36 +08:00
|
|
|
/* file_handle */ 1,
|
2021-04-04 17:01:32 +01:00
|
|
|
#endif
|
2018-04-11 13:39:42 +08:00
|
|
|
};
|