2019-08-14 10:44:14 +01:00
|
|
|
/*
|
|
|
|
* libwebsockets - small server side websockets and web server implementation
|
2018-10-02 10:50:24 +08:00
|
|
|
*
|
2019-08-14 10:44:14 +01:00
|
|
|
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
|
2018-10-02 10:50:24 +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-10-02 10:50:24 +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-10-02 10:50:24 +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-10-02 10:50:24 +08:00
|
|
|
*
|
|
|
|
* This role for wrapping dbus fds in a wsi + role is unusual in that the
|
|
|
|
* wsi it creates and binds to the role do not have control over the related fd
|
|
|
|
* lifecycle. In fact dbus doesn't inform us directly about the lifecycle of
|
|
|
|
* the fds it wants to be managed by the lws event loop.
|
|
|
|
*
|
|
|
|
* What it does tell us is when it wants to wait on POLLOUT and / or POLLIN,
|
|
|
|
* and since it should stop any watchers before close, we take the approach to
|
|
|
|
* create a lightweight "shadow" wsi for any fd from dbus that has a POLLIN or
|
|
|
|
* POLLOUT wait active. When the dbus fd asks to have no wait active, we
|
|
|
|
* destroy the wsi, since this is indistinguishable from dbus close path
|
|
|
|
* behaviour. If it actually stays alive and later asks to wait again, well no
|
|
|
|
* worries we create a new shadow wsi until it looks like it is closing again.
|
|
|
|
*/
|
|
|
|
|
2019-08-15 10:49:52 +01:00
|
|
|
#include <private-lib-core.h>
|
2018-10-02 10:50:24 +08:00
|
|
|
|
|
|
|
#include <libwebsockets/lws-dbus.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* retreives existing or creates new shadow wsi for fd owned by dbus stuff.
|
|
|
|
*
|
2020-12-24 16:06:50 +00:00
|
|
|
* Requires context + vhost lock
|
2018-10-02 10:50:24 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
static struct lws *
|
|
|
|
__lws_shadow_wsi(struct lws_dbus_ctx *ctx, DBusWatch *w, int fd, int create_ok)
|
|
|
|
{
|
|
|
|
struct lws *wsi;
|
|
|
|
|
|
|
|
if (fd < 0 || fd >= (int)ctx->vh->context->fd_limit_per_thread) {
|
|
|
|
lwsl_err("%s: fd %d vs fds_count %d\n", __func__, fd,
|
|
|
|
(int)ctx->vh->context->fd_limit_per_thread);
|
|
|
|
assert(0);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
wsi = wsi_from_fd(ctx->vh->context, fd);
|
|
|
|
if (wsi) {
|
|
|
|
assert(wsi->opaque_parent_data == ctx);
|
|
|
|
|
|
|
|
return wsi;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!create_ok)
|
|
|
|
return NULL;
|
|
|
|
|
2021-04-04 04:06:24 +01:00
|
|
|
lws_context_assert_lock_held(wsi->a.context);
|
|
|
|
lws_vhost_assert_lock_held(wsi->a.vhost);
|
|
|
|
|
2020-12-24 16:06:50 +00:00
|
|
|
/* requires context lock */
|
|
|
|
wsi = __lws_wsi_create_with_role(ctx->vh->context, ctx->tsi, NULL);
|
2018-10-02 10:50:24 +08:00
|
|
|
if (wsi == NULL) {
|
|
|
|
lwsl_err("Out of mem\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
lwsl_info("%s: creating shadow wsi\n", __func__);
|
|
|
|
|
|
|
|
wsi->desc.sockfd = fd;
|
|
|
|
lws_role_transition(wsi, 0, LRS_ESTABLISHED, &role_ops_dbus);
|
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
|
|
|
wsi->a.protocol = ctx->vh->protocols;
|
2018-10-02 10:50:24 +08:00
|
|
|
wsi->shadow = 1;
|
|
|
|
wsi->opaque_parent_data = ctx;
|
|
|
|
ctx->w[0] = w;
|
|
|
|
|
2021-01-24 12:05:45 +00:00
|
|
|
__lws_lc_tag(&ctx->vh->context->lcg[LWSLCG_WSI], &wsi->lc, "dbus|%s", ctx->vh->name);
|
|
|
|
|
2018-10-02 10:50:24 +08:00
|
|
|
lws_vhost_bind_wsi(ctx->vh, wsi);
|
|
|
|
if (__insert_wsi_socket_into_fds(ctx->vh->context, wsi)) {
|
|
|
|
lwsl_err("inserting wsi socket into fds failed\n");
|
2021-04-04 04:06:24 +01:00
|
|
|
__lws_vhost_unbind_wsi(wsi); /* cx + vh lock */
|
2018-10-02 10:50:24 +08:00
|
|
|
lws_free(wsi);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return wsi;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2021-04-04 04:06:24 +01:00
|
|
|
* Requires cx + vhost lock
|
2018-10-02 10:50:24 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
|
|
|
__lws_shadow_wsi_destroy(struct lws_dbus_ctx *ctx, struct lws *wsi)
|
|
|
|
{
|
|
|
|
lwsl_info("%s: destroying shadow wsi\n", __func__);
|
|
|
|
|
2021-04-04 04:06:24 +01:00
|
|
|
lws_context_assert_lock_held(wsi->a.context);
|
|
|
|
lws_vhost_assert_lock_held(wsi->a.vhost);
|
|
|
|
|
2018-10-02 10:50:24 +08:00
|
|
|
if (__remove_wsi_socket_from_fds(wsi)) {
|
|
|
|
lwsl_err("%s: unable to remove %d from fds\n", __func__,
|
|
|
|
wsi->desc.sockfd);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-04-04 04:06:24 +01:00
|
|
|
__lws_vhost_unbind_wsi(wsi);
|
2018-10-02 10:50:24 +08:00
|
|
|
|
|
|
|
lws_free(wsi);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
handle_dispatch_status(DBusConnection *c, DBusDispatchStatus s, void *data)
|
|
|
|
{
|
|
|
|
lwsl_info("%s: new dbus dispatch status: %d\n", __func__, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* These are complicated by the fact libdbus can have two separate DBusWatch
|
|
|
|
* objects for the same fd, to control watching POLLIN and POLLOUT individually.
|
|
|
|
*
|
|
|
|
* However we will actually watch using poll(), where the unit is the fd, and
|
|
|
|
* it has a unified events field with just POLLIN / POLLOUT flags.
|
|
|
|
*
|
|
|
|
* So we have to be prepared for one or two watchers coming in any order.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static dbus_bool_t
|
|
|
|
lws_dbus_add_watch(DBusWatch *w, void *data)
|
|
|
|
{
|
|
|
|
struct lws_dbus_ctx *ctx = (struct lws_dbus_ctx *)data;
|
|
|
|
struct lws_context_per_thread *pt = &ctx->vh->context->pt[ctx->tsi];
|
|
|
|
unsigned int flags = 0, lws_flags = 0;
|
|
|
|
struct lws *wsi;
|
|
|
|
int n;
|
|
|
|
|
2020-12-24 16:06:50 +00:00
|
|
|
lws_context_lock(pt->context, __func__);
|
2018-10-02 10:50:24 +08:00
|
|
|
lws_pt_lock(pt, __func__);
|
|
|
|
|
|
|
|
wsi = __lws_shadow_wsi(ctx, w, dbus_watch_get_unix_fd(w), 1);
|
|
|
|
if (!wsi) {
|
|
|
|
lws_pt_unlock(pt);
|
2020-12-24 16:06:50 +00:00
|
|
|
lws_context_unlock(pt->context);
|
2018-10-02 10:50:24 +08:00
|
|
|
lwsl_err("%s: unable to get wsi\n", __func__);
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (n = 0; n < (int)LWS_ARRAY_SIZE(ctx->w); n++)
|
|
|
|
if (w == ctx->w[n])
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (n == (int)LWS_ARRAY_SIZE(ctx->w))
|
|
|
|
for (n = 0; n < (int)LWS_ARRAY_SIZE(ctx->w); n++)
|
|
|
|
if (!ctx->w[n]) {
|
|
|
|
ctx->w[n] = w;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (n = 0; n < (int)LWS_ARRAY_SIZE(ctx->w); n++)
|
2021-01-24 12:05:45 +00:00
|
|
|
if (ctx->w[n] && dbus_watch_get_enabled(ctx->w[n]))
|
2018-10-02 10:50:24 +08:00
|
|
|
flags |= dbus_watch_get_flags(ctx->w[n]);
|
|
|
|
|
|
|
|
if (flags & DBUS_WATCH_READABLE)
|
|
|
|
lws_flags |= LWS_POLLIN;
|
|
|
|
if (flags & DBUS_WATCH_WRITABLE)
|
|
|
|
lws_flags |= LWS_POLLOUT;
|
|
|
|
|
2021-01-24 12:05:45 +00:00
|
|
|
lwsl_info("%s: %s: %p, fd %d, data %p, fl %d\n", __func__,
|
|
|
|
lws_wsi_tag(wsi), w, dbus_watch_get_unix_fd(w),
|
|
|
|
data, lws_flags);
|
2018-10-02 10:50:24 +08:00
|
|
|
|
2021-01-24 12:05:45 +00:00
|
|
|
if (lws_flags)
|
|
|
|
__lws_change_pollfd(wsi, 0, (int)lws_flags);
|
2018-10-02 10:50:24 +08:00
|
|
|
|
|
|
|
lws_pt_unlock(pt);
|
2020-12-24 16:06:50 +00:00
|
|
|
lws_context_unlock(pt->context);
|
2018-10-02 10:50:24 +08:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2021-04-04 04:06:24 +01:00
|
|
|
/* cx + vh lock */
|
2018-10-02 10:50:24 +08:00
|
|
|
static int
|
2021-04-04 04:06:24 +01:00
|
|
|
__check_destroy_shadow_wsi(struct lws_dbus_ctx *ctx, struct lws *wsi)
|
2018-10-02 10:50:24 +08:00
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
|
|
|
if (!wsi)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (n = 0; n < (int)LWS_ARRAY_SIZE(ctx->w); n++)
|
|
|
|
if (ctx->w[n])
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
__lws_shadow_wsi_destroy(ctx, wsi);
|
|
|
|
|
|
|
|
if (!ctx->conn || !ctx->hup || ctx->timeouts)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (dbus_connection_get_dispatch_status(ctx->conn) ==
|
|
|
|
DBUS_DISPATCH_DATA_REMAINS)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (ctx->cb_closing)
|
|
|
|
ctx->cb_closing(ctx);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
lws_dbus_remove_watch(DBusWatch *w, void *data)
|
|
|
|
{
|
|
|
|
struct lws_dbus_ctx *ctx = (struct lws_dbus_ctx *)data;
|
|
|
|
struct lws_context_per_thread *pt = &ctx->vh->context->pt[ctx->tsi];
|
|
|
|
unsigned int flags = 0, lws_flags = 0;
|
|
|
|
struct lws *wsi;
|
|
|
|
int n;
|
|
|
|
|
2020-12-24 16:06:50 +00:00
|
|
|
lws_context_lock(pt->context, __func__);
|
2018-10-02 10:50:24 +08:00
|
|
|
lws_pt_lock(pt, __func__);
|
|
|
|
|
|
|
|
wsi = __lws_shadow_wsi(ctx, w, dbus_watch_get_unix_fd(w), 0);
|
|
|
|
if (!wsi)
|
|
|
|
goto bail;
|
|
|
|
|
|
|
|
for (n = 0; n < (int)LWS_ARRAY_SIZE(ctx->w); n++)
|
|
|
|
if (w == ctx->w[n]) {
|
|
|
|
ctx->w[n] = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (n = 0; n < (int)LWS_ARRAY_SIZE(ctx->w); n++)
|
|
|
|
if (ctx->w[n])
|
|
|
|
flags |= dbus_watch_get_flags(ctx->w[n]);
|
|
|
|
|
|
|
|
if ((~flags) & DBUS_WATCH_READABLE)
|
|
|
|
lws_flags |= LWS_POLLIN;
|
|
|
|
if ((~flags) & DBUS_WATCH_WRITABLE)
|
|
|
|
lws_flags |= LWS_POLLOUT;
|
|
|
|
|
2020-12-25 05:54:19 +00:00
|
|
|
lwsl_info("%s: %p, fd %d, data %p, clearing lws flags %d\n",
|
|
|
|
__func__, w, dbus_watch_get_unix_fd(w),
|
|
|
|
data, lws_flags);
|
2018-10-02 10:50:24 +08:00
|
|
|
|
2020-12-12 06:21:40 +00:00
|
|
|
__lws_change_pollfd(wsi, (int)lws_flags, 0);
|
2018-10-02 10:50:24 +08:00
|
|
|
|
|
|
|
bail:
|
|
|
|
lws_pt_unlock(pt);
|
2020-12-24 16:06:50 +00:00
|
|
|
lws_context_unlock(pt->context);
|
2018-10-02 10:50:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
lws_dbus_toggle_watch(DBusWatch *w, void *data)
|
|
|
|
{
|
|
|
|
if (dbus_watch_get_enabled(w))
|
|
|
|
lws_dbus_add_watch(w, data);
|
|
|
|
else
|
|
|
|
lws_dbus_remove_watch(w, data);
|
|
|
|
}
|
|
|
|
|
2021-01-17 15:02:57 +00:00
|
|
|
static void
|
|
|
|
lws_dbus_sul_cb(lws_sorted_usec_list_t *sul)
|
|
|
|
{
|
|
|
|
struct lws_context_per_thread *pt = lws_container_of(sul,
|
|
|
|
struct lws_context_per_thread, dbus.sul);
|
|
|
|
|
|
|
|
lws_start_foreach_dll_safe(struct lws_dll2 *, rdt, nx,
|
|
|
|
lws_dll2_get_head(&pt->dbus.timer_list_owner)) {
|
|
|
|
struct lws_role_dbus_timer *r = lws_container_of(rdt,
|
|
|
|
struct lws_role_dbus_timer, timer_list);
|
|
|
|
|
|
|
|
if (time(NULL) > r->fire) {
|
|
|
|
lwsl_notice("%s: firing timer\n", __func__);
|
|
|
|
dbus_timeout_handle(r->data);
|
|
|
|
lws_dll2_remove(rdt);
|
|
|
|
lws_free(rdt);
|
|
|
|
}
|
|
|
|
} lws_end_foreach_dll_safe(rdt, nx);
|
|
|
|
|
|
|
|
if (pt->dbus.timer_list_owner.count)
|
|
|
|
lws_sul_schedule(pt->context, pt->tid, &pt->dbus.sul,
|
|
|
|
lws_dbus_sul_cb, 3 * LWS_US_PER_SEC);
|
|
|
|
}
|
2018-10-02 10:50:24 +08:00
|
|
|
|
|
|
|
static dbus_bool_t
|
|
|
|
lws_dbus_add_timeout(DBusTimeout *t, void *data)
|
|
|
|
{
|
|
|
|
struct lws_dbus_ctx *ctx = (struct lws_dbus_ctx *)data;
|
|
|
|
struct lws_context_per_thread *pt = &ctx->vh->context->pt[ctx->tsi];
|
|
|
|
int ms = dbus_timeout_get_interval(t);
|
|
|
|
struct lws_role_dbus_timer *dbt;
|
|
|
|
time_t ti = time(NULL);
|
|
|
|
|
|
|
|
if (!dbus_timeout_get_enabled(t))
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
if (ms < 1000)
|
|
|
|
ms = 1000;
|
|
|
|
|
|
|
|
dbt = lws_malloc(sizeof(*dbt), "dbus timer");
|
|
|
|
if (!dbt)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
lwsl_info("%s: adding timeout %dms\n", __func__,
|
|
|
|
dbus_timeout_get_interval(t));
|
|
|
|
|
|
|
|
dbt->data = t;
|
|
|
|
dbt->fire = ti + (ms < 1000);
|
|
|
|
dbt->timer_list.prev = NULL;
|
|
|
|
dbt->timer_list.next = NULL;
|
2019-08-08 20:05:03 +01:00
|
|
|
dbt->timer_list.owner = NULL;
|
2019-08-08 16:58:55 +01:00
|
|
|
lws_dll2_add_head(&dbt->timer_list, &pt->dbus.timer_list_owner);
|
2018-10-02 10:50:24 +08:00
|
|
|
|
2021-01-17 15:02:57 +00:00
|
|
|
if (!pt->dbus.sul.list.owner)
|
|
|
|
lws_sul_schedule(pt->context, pt->tid, &pt->dbus.sul,
|
|
|
|
lws_dbus_sul_cb, 3 * LWS_US_PER_SEC);
|
|
|
|
|
2018-10-02 10:50:24 +08:00
|
|
|
ctx->timeouts++;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
lws_dbus_remove_timeout(DBusTimeout *t, void *data)
|
|
|
|
{
|
|
|
|
struct lws_dbus_ctx *ctx = (struct lws_dbus_ctx *)data;
|
|
|
|
struct lws_context_per_thread *pt = &ctx->vh->context->pt[ctx->tsi];
|
|
|
|
|
|
|
|
lwsl_info("%s: t %p, data %p\n", __func__, t, data);
|
|
|
|
|
2019-08-08 16:58:55 +01:00
|
|
|
lws_start_foreach_dll_safe(struct lws_dll2 *, rdt, nx,
|
|
|
|
lws_dll2_get_head(&pt->dbus.timer_list_owner)) {
|
2018-10-02 10:50:24 +08:00
|
|
|
struct lws_role_dbus_timer *r = lws_container_of(rdt,
|
|
|
|
struct lws_role_dbus_timer, timer_list);
|
|
|
|
if (t == r->data) {
|
2019-08-08 16:58:55 +01:00
|
|
|
lws_dll2_remove(rdt);
|
2018-10-02 10:50:24 +08:00
|
|
|
lws_free(rdt);
|
|
|
|
ctx->timeouts--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} lws_end_foreach_dll_safe(rdt, nx);
|
2021-01-17 15:02:57 +00:00
|
|
|
|
|
|
|
if (!pt->dbus.timer_list_owner.count)
|
|
|
|
lws_sul_cancel(&pt->dbus.sul);
|
2018-10-02 10:50:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
lws_dbus_toggle_timeout(DBusTimeout *t, void *data)
|
|
|
|
{
|
|
|
|
if (dbus_timeout_get_enabled(t))
|
|
|
|
lws_dbus_add_timeout(t, data);
|
|
|
|
else
|
|
|
|
lws_dbus_remove_timeout(t, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This sets up a connection along the same lines as
|
|
|
|
* dbus_connection_setup_with_g_main(), but for using the lws event loop.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
lws_dbus_connection_setup(struct lws_dbus_ctx *ctx, DBusConnection *conn,
|
|
|
|
lws_dbus_closing_t cb_closing)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
|
|
|
ctx->conn = conn;
|
|
|
|
ctx->cb_closing = cb_closing;
|
|
|
|
ctx->hup = 0;
|
|
|
|
ctx->timeouts = 0;
|
|
|
|
for (n = 0; n < (int)LWS_ARRAY_SIZE(ctx->w); n++)
|
|
|
|
ctx->w[n] = NULL;
|
|
|
|
|
|
|
|
if (!dbus_connection_set_watch_functions(conn, lws_dbus_add_watch,
|
|
|
|
lws_dbus_remove_watch,
|
|
|
|
lws_dbus_toggle_watch,
|
|
|
|
ctx, NULL)) {
|
|
|
|
lwsl_err("%s: dbus_connection_set_watch_functions fail\n",
|
|
|
|
__func__);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dbus_connection_set_timeout_functions(conn,
|
|
|
|
lws_dbus_add_timeout,
|
|
|
|
lws_dbus_remove_timeout,
|
|
|
|
lws_dbus_toggle_timeout,
|
|
|
|
ctx, NULL)) {
|
|
|
|
lwsl_err("%s: dbus_connection_set_timeout_functions fail\n",
|
|
|
|
__func__);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
dbus_connection_set_dispatch_status_function(conn,
|
|
|
|
handle_dispatch_status,
|
|
|
|
ctx, NULL);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This wraps dbus_server_listen(), additionally taking care of the event loop
|
|
|
|
* -related setups.
|
|
|
|
*/
|
|
|
|
|
|
|
|
DBusServer *
|
|
|
|
lws_dbus_server_listen(struct lws_dbus_ctx *ctx, const char *ads, DBusError *e,
|
|
|
|
DBusNewConnectionFunction new_conn)
|
|
|
|
{
|
|
|
|
ctx->cb_closing = NULL;
|
|
|
|
ctx->hup = 0;
|
|
|
|
ctx->timeouts = 0;
|
|
|
|
|
|
|
|
ctx->dbs = dbus_server_listen(ads, e);
|
|
|
|
if (!ctx->dbs)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
dbus_server_set_new_connection_function(ctx->dbs, new_conn, ctx, NULL);
|
|
|
|
|
|
|
|
if (!dbus_server_set_watch_functions(ctx->dbs, lws_dbus_add_watch,
|
|
|
|
lws_dbus_remove_watch,
|
|
|
|
lws_dbus_toggle_watch,
|
|
|
|
ctx, NULL)) {
|
|
|
|
lwsl_err("%s: dbus_connection_set_watch_functions fail\n",
|
|
|
|
__func__);
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dbus_server_set_timeout_functions(ctx->dbs, lws_dbus_add_timeout,
|
|
|
|
lws_dbus_remove_timeout,
|
|
|
|
lws_dbus_toggle_timeout,
|
|
|
|
ctx, NULL)) {
|
|
|
|
lwsl_err("%s: dbus_connection_set_timeout_functions fail\n",
|
|
|
|
__func__);
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx->dbs;
|
|
|
|
|
|
|
|
bail:
|
|
|
|
dbus_server_disconnect(ctx->dbs);
|
|
|
|
dbus_server_unref(ctx->dbs);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* There shouldn't be a race here with watcher removal and poll wait, because
|
|
|
|
* everything including the dbus activity is serialized in one event loop.
|
|
|
|
*
|
|
|
|
* If it removes the watcher and we remove the wsi and fd entry before this,
|
|
|
|
* actually we can no longer map the fd to this invalidated wsi pointer to call
|
|
|
|
* this.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
|
|
|
rops_handle_POLLIN_dbus(struct lws_context_per_thread *pt, struct lws *wsi,
|
|
|
|
struct lws_pollfd *pollfd)
|
|
|
|
{
|
|
|
|
struct lws_dbus_ctx *ctx =
|
|
|
|
(struct lws_dbus_ctx *)wsi->opaque_parent_data;
|
|
|
|
unsigned int flags = 0;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
if (pollfd->revents & LWS_POLLIN)
|
|
|
|
flags |= DBUS_WATCH_READABLE;
|
|
|
|
if (pollfd->revents & LWS_POLLOUT)
|
|
|
|
flags |= DBUS_WATCH_WRITABLE;
|
|
|
|
|
|
|
|
if (pollfd->revents & (LWS_POLLHUP))
|
|
|
|
ctx->hup = 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* POLLIN + POLLOUT gets us called here on the corresponding shadow
|
|
|
|
* wsi. wsi->opaque_parent_data is the watcher handle bound to the wsi
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (n = 0; n < (int)LWS_ARRAY_SIZE(ctx->w); n++)
|
|
|
|
if (ctx->w[n] && !dbus_watch_handle(ctx->w[n], flags))
|
|
|
|
lwsl_err("%s: dbus_watch_handle failed\n", __func__);
|
|
|
|
|
|
|
|
if (ctx->conn) {
|
|
|
|
lwsl_info("%s: conn: flags %d\n", __func__, flags);
|
|
|
|
|
|
|
|
while (dbus_connection_get_dispatch_status(ctx->conn) ==
|
|
|
|
DBUS_DISPATCH_DATA_REMAINS)
|
|
|
|
dbus_connection_dispatch(ctx->conn);
|
|
|
|
|
|
|
|
handle_dispatch_status(NULL, DBUS_DISPATCH_DATA_REMAINS, NULL);
|
|
|
|
|
2021-04-04 04:06:24 +01:00
|
|
|
__check_destroy_shadow_wsi(ctx, wsi);
|
2018-10-02 10:50:24 +08:00
|
|
|
} else
|
|
|
|
if (ctx->dbs)
|
|
|
|
/* ??? */
|
|
|
|
lwsl_debug("%s: dbs: %d\n", __func__, flags);
|
|
|
|
|
|
|
|
return LWS_HPI_RET_HANDLED;
|
|
|
|
}
|
|
|
|
|
2019-09-18 13:09:32 +01:00
|
|
|
static int
|
|
|
|
rops_pt_init_destroy_dbus(struct lws_context *context,
|
|
|
|
const struct lws_context_creation_info *info,
|
|
|
|
struct lws_context_per_thread *pt, int destroy)
|
|
|
|
{
|
2021-01-17 15:02:57 +00:00
|
|
|
if (destroy)
|
2020-05-28 12:48:17 +01:00
|
|
|
lws_sul_cancel(&pt->dbus.sul);
|
2019-09-18 13:09:32 +01:00
|
|
|
|
2018-10-02 10:50:24 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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_dbus[] = {
|
|
|
|
/* 1 */ { .pt_init_destroy = rops_pt_init_destroy_dbus },
|
|
|
|
/* 2 */ { .handle_POLLIN = rops_handle_POLLIN_dbus },
|
|
|
|
};
|
|
|
|
|
2020-01-15 06:31:19 +00:00
|
|
|
const struct lws_role_ops role_ops_dbus = {
|
2018-10-02 10:50:24 +08:00
|
|
|
/* role name */ "dbus",
|
|
|
|
/* 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_dbus,
|
|
|
|
/* rops_idx */ {
|
|
|
|
/* LWS_ROPS_check_upgrades */
|
|
|
|
/* LWS_ROPS_pt_init_destroy */ 0x01,
|
|
|
|
/* LWS_ROPS_init_vhost */
|
|
|
|
/* LWS_ROPS_destroy_vhost */ 0x00,
|
|
|
|
/* LWS_ROPS_service_flag_pending */
|
|
|
|
/* LWS_ROPS_handle_POLLIN */ 0x02,
|
|
|
|
/* 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-10-02 10:50:24 +08:00
|
|
|
/* writeable cb clnt, srv */ { 0, 0 },
|
|
|
|
/* close cb clnt, srv */ { 0, 0 },
|
|
|
|
/* protocol_bind_cb c,s */ { 0, 0 },
|
|
|
|
/* protocol_unbind_cb c,s */ { 0, 0 },
|
|
|
|
/* file_handle */ 0,
|
|
|
|
};
|