dbus.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010 - 2019 Andy Green <[email protected]>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to
  8. * deal in the Software without restriction, including without limitation the
  9. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. * sell copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. * IN THE SOFTWARE.
  23. *
  24. * This role for wrapping dbus fds in a wsi + role is unusual in that the
  25. * wsi it creates and binds to the role do not have control over the related fd
  26. * lifecycle. In fact dbus doesn't inform us directly about the lifecycle of
  27. * the fds it wants to be managed by the lws event loop.
  28. *
  29. * What it does tell us is when it wants to wait on POLLOUT and / or POLLIN,
  30. * and since it should stop any watchers before close, we take the approach to
  31. * create a lightweight "shadow" wsi for any fd from dbus that has a POLLIN or
  32. * POLLOUT wait active. When the dbus fd asks to have no wait active, we
  33. * destroy the wsi, since this is indistinguishable from dbus close path
  34. * behaviour. If it actually stays alive and later asks to wait again, well no
  35. * worries we create a new shadow wsi until it looks like it is closing again.
  36. */
  37. #include <private-lib-core.h>
  38. #include <libwebsockets/lws-dbus.h>
  39. /*
  40. * retreives existing or creates new shadow wsi for fd owned by dbus stuff.
  41. *
  42. * Requires vhost lock
  43. */
  44. static struct lws *
  45. __lws_shadow_wsi(struct lws_dbus_ctx *ctx, DBusWatch *w, int fd, int create_ok)
  46. {
  47. size_t s = sizeof(struct lws);
  48. struct lws *wsi;
  49. if (fd < 0 || fd >= (int)ctx->vh->context->fd_limit_per_thread) {
  50. lwsl_err("%s: fd %d vs fds_count %d\n", __func__, fd,
  51. (int)ctx->vh->context->fd_limit_per_thread);
  52. assert(0);
  53. return NULL;
  54. }
  55. wsi = wsi_from_fd(ctx->vh->context, fd);
  56. if (wsi) {
  57. assert(wsi->opaque_parent_data == ctx);
  58. return wsi;
  59. }
  60. if (!create_ok)
  61. return NULL;
  62. #if defined(LWS_WITH_EVENT_LIBS)
  63. s += ctx->vh->context->event_loop_ops->evlib_size_wsi;
  64. #endif
  65. wsi = lws_zalloc(s, "shadow wsi");
  66. if (wsi == NULL) {
  67. lwsl_err("Out of mem\n");
  68. return NULL;
  69. }
  70. #if defined(LWS_WITH_EVENT_LIBS)
  71. wsi->evlib_wsi = (uint8_t *)wsi + sizeof(*wsi);
  72. #endif
  73. lwsl_info("%s: creating shadow wsi\n", __func__);
  74. wsi->a.context = ctx->vh->context;
  75. wsi->desc.sockfd = fd;
  76. lws_role_transition(wsi, 0, LRS_ESTABLISHED, &role_ops_dbus);
  77. wsi->a.protocol = ctx->vh->protocols;
  78. wsi->tsi = ctx->tsi;
  79. wsi->shadow = 1;
  80. wsi->opaque_parent_data = ctx;
  81. ctx->w[0] = w;
  82. lws_vhost_bind_wsi(ctx->vh, wsi);
  83. if (__insert_wsi_socket_into_fds(ctx->vh->context, wsi)) {
  84. lwsl_err("inserting wsi socket into fds failed\n");
  85. lws_vhost_unbind_wsi(wsi);
  86. lws_free(wsi);
  87. return NULL;
  88. }
  89. ctx->vh->context->count_wsi_allocated++;
  90. return wsi;
  91. }
  92. /*
  93. * Requires vhost lock
  94. */
  95. static int
  96. __lws_shadow_wsi_destroy(struct lws_dbus_ctx *ctx, struct lws *wsi)
  97. {
  98. lwsl_info("%s: destroying shadow wsi\n", __func__);
  99. if (__remove_wsi_socket_from_fds(wsi)) {
  100. lwsl_err("%s: unable to remove %d from fds\n", __func__,
  101. wsi->desc.sockfd);
  102. return 1;
  103. }
  104. ctx->vh->context->count_wsi_allocated--;
  105. lws_vhost_unbind_wsi(wsi);
  106. lws_free(wsi);
  107. return 0;
  108. }
  109. static void
  110. handle_dispatch_status(DBusConnection *c, DBusDispatchStatus s, void *data)
  111. {
  112. lwsl_info("%s: new dbus dispatch status: %d\n", __func__, s);
  113. }
  114. /*
  115. * These are complicated by the fact libdbus can have two separate DBusWatch
  116. * objects for the same fd, to control watching POLLIN and POLLOUT individually.
  117. *
  118. * However we will actually watch using poll(), where the unit is the fd, and
  119. * it has a unified events field with just POLLIN / POLLOUT flags.
  120. *
  121. * So we have to be prepared for one or two watchers coming in any order.
  122. */
  123. static dbus_bool_t
  124. lws_dbus_add_watch(DBusWatch *w, void *data)
  125. {
  126. struct lws_dbus_ctx *ctx = (struct lws_dbus_ctx *)data;
  127. struct lws_context_per_thread *pt = &ctx->vh->context->pt[ctx->tsi];
  128. unsigned int flags = 0, lws_flags = 0;
  129. struct lws *wsi;
  130. int n;
  131. lws_pt_lock(pt, __func__);
  132. wsi = __lws_shadow_wsi(ctx, w, dbus_watch_get_unix_fd(w), 1);
  133. if (!wsi) {
  134. lws_pt_unlock(pt);
  135. lwsl_err("%s: unable to get wsi\n", __func__);
  136. return FALSE;
  137. }
  138. for (n = 0; n < (int)LWS_ARRAY_SIZE(ctx->w); n++)
  139. if (w == ctx->w[n])
  140. break;
  141. if (n == (int)LWS_ARRAY_SIZE(ctx->w))
  142. for (n = 0; n < (int)LWS_ARRAY_SIZE(ctx->w); n++)
  143. if (!ctx->w[n]) {
  144. ctx->w[n] = w;
  145. break;
  146. }
  147. for (n = 0; n < (int)LWS_ARRAY_SIZE(ctx->w); n++)
  148. if (ctx->w[n])
  149. flags |= dbus_watch_get_flags(ctx->w[n]);
  150. if (flags & DBUS_WATCH_READABLE)
  151. lws_flags |= LWS_POLLIN;
  152. if (flags & DBUS_WATCH_WRITABLE)
  153. lws_flags |= LWS_POLLOUT;
  154. lwsl_info("%s: w %p, fd %d, data %p, flags %d\n", __func__, w,
  155. dbus_watch_get_unix_fd(w), data, lws_flags);
  156. __lws_change_pollfd(wsi, 0, lws_flags);
  157. lws_pt_unlock(pt);
  158. return TRUE;
  159. }
  160. static int
  161. check_destroy_shadow_wsi(struct lws_dbus_ctx *ctx, struct lws *wsi)
  162. {
  163. int n;
  164. if (!wsi)
  165. return 0;
  166. for (n = 0; n < (int)LWS_ARRAY_SIZE(ctx->w); n++)
  167. if (ctx->w[n])
  168. return 0;
  169. __lws_shadow_wsi_destroy(ctx, wsi);
  170. if (!ctx->conn || !ctx->hup || ctx->timeouts)
  171. return 0;
  172. if (dbus_connection_get_dispatch_status(ctx->conn) ==
  173. DBUS_DISPATCH_DATA_REMAINS)
  174. return 0;
  175. if (ctx->cb_closing)
  176. ctx->cb_closing(ctx);
  177. return 1;
  178. }
  179. static void
  180. lws_dbus_remove_watch(DBusWatch *w, void *data)
  181. {
  182. struct lws_dbus_ctx *ctx = (struct lws_dbus_ctx *)data;
  183. struct lws_context_per_thread *pt = &ctx->vh->context->pt[ctx->tsi];
  184. unsigned int flags = 0, lws_flags = 0;
  185. struct lws *wsi;
  186. int n;
  187. lws_pt_lock(pt, __func__);
  188. wsi = __lws_shadow_wsi(ctx, w, dbus_watch_get_unix_fd(w), 0);
  189. if (!wsi)
  190. goto bail;
  191. for (n = 0; n < (int)LWS_ARRAY_SIZE(ctx->w); n++)
  192. if (w == ctx->w[n]) {
  193. ctx->w[n] = NULL;
  194. break;
  195. }
  196. for (n = 0; n < (int)LWS_ARRAY_SIZE(ctx->w); n++)
  197. if (ctx->w[n])
  198. flags |= dbus_watch_get_flags(ctx->w[n]);
  199. if ((~flags) & DBUS_WATCH_READABLE)
  200. lws_flags |= LWS_POLLIN;
  201. if ((~flags) & DBUS_WATCH_WRITABLE)
  202. lws_flags |= LWS_POLLOUT;
  203. lwsl_info("%s: w %p, fd %d, data %p, clearing lws flags %d\n",
  204. __func__, w, dbus_watch_get_unix_fd(w), data, lws_flags);
  205. __lws_change_pollfd(wsi, lws_flags, 0);
  206. bail:
  207. lws_pt_unlock(pt);
  208. }
  209. static void
  210. lws_dbus_toggle_watch(DBusWatch *w, void *data)
  211. {
  212. if (dbus_watch_get_enabled(w))
  213. lws_dbus_add_watch(w, data);
  214. else
  215. lws_dbus_remove_watch(w, data);
  216. }
  217. static dbus_bool_t
  218. lws_dbus_add_timeout(DBusTimeout *t, void *data)
  219. {
  220. struct lws_dbus_ctx *ctx = (struct lws_dbus_ctx *)data;
  221. struct lws_context_per_thread *pt = &ctx->vh->context->pt[ctx->tsi];
  222. int ms = dbus_timeout_get_interval(t);
  223. struct lws_role_dbus_timer *dbt;
  224. time_t ti = time(NULL);
  225. if (!dbus_timeout_get_enabled(t))
  226. return TRUE;
  227. if (ms < 1000)
  228. ms = 1000;
  229. dbt = lws_malloc(sizeof(*dbt), "dbus timer");
  230. if (!dbt)
  231. return FALSE;
  232. lwsl_info("%s: adding timeout %dms\n", __func__,
  233. dbus_timeout_get_interval(t));
  234. dbt->data = t;
  235. dbt->fire = ti + (ms < 1000);
  236. dbt->timer_list.prev = NULL;
  237. dbt->timer_list.next = NULL;
  238. dbt->timer_list.owner = NULL;
  239. lws_dll2_add_head(&dbt->timer_list, &pt->dbus.timer_list_owner);
  240. ctx->timeouts++;
  241. return TRUE;
  242. }
  243. static void
  244. lws_dbus_remove_timeout(DBusTimeout *t, void *data)
  245. {
  246. struct lws_dbus_ctx *ctx = (struct lws_dbus_ctx *)data;
  247. struct lws_context_per_thread *pt = &ctx->vh->context->pt[ctx->tsi];
  248. lwsl_info("%s: t %p, data %p\n", __func__, t, data);
  249. lws_start_foreach_dll_safe(struct lws_dll2 *, rdt, nx,
  250. lws_dll2_get_head(&pt->dbus.timer_list_owner)) {
  251. struct lws_role_dbus_timer *r = lws_container_of(rdt,
  252. struct lws_role_dbus_timer, timer_list);
  253. if (t == r->data) {
  254. lws_dll2_remove(rdt);
  255. lws_free(rdt);
  256. ctx->timeouts--;
  257. break;
  258. }
  259. } lws_end_foreach_dll_safe(rdt, nx);
  260. }
  261. static void
  262. lws_dbus_toggle_timeout(DBusTimeout *t, void *data)
  263. {
  264. if (dbus_timeout_get_enabled(t))
  265. lws_dbus_add_timeout(t, data);
  266. else
  267. lws_dbus_remove_timeout(t, data);
  268. }
  269. /*
  270. * This sets up a connection along the same lines as
  271. * dbus_connection_setup_with_g_main(), but for using the lws event loop.
  272. */
  273. int
  274. lws_dbus_connection_setup(struct lws_dbus_ctx *ctx, DBusConnection *conn,
  275. lws_dbus_closing_t cb_closing)
  276. {
  277. int n;
  278. ctx->conn = conn;
  279. ctx->cb_closing = cb_closing;
  280. ctx->hup = 0;
  281. ctx->timeouts = 0;
  282. for (n = 0; n < (int)LWS_ARRAY_SIZE(ctx->w); n++)
  283. ctx->w[n] = NULL;
  284. if (!dbus_connection_set_watch_functions(conn, lws_dbus_add_watch,
  285. lws_dbus_remove_watch,
  286. lws_dbus_toggle_watch,
  287. ctx, NULL)) {
  288. lwsl_err("%s: dbus_connection_set_watch_functions fail\n",
  289. __func__);
  290. return 1;
  291. }
  292. if (!dbus_connection_set_timeout_functions(conn,
  293. lws_dbus_add_timeout,
  294. lws_dbus_remove_timeout,
  295. lws_dbus_toggle_timeout,
  296. ctx, NULL)) {
  297. lwsl_err("%s: dbus_connection_set_timeout_functions fail\n",
  298. __func__);
  299. return 1;
  300. }
  301. dbus_connection_set_dispatch_status_function(conn,
  302. handle_dispatch_status,
  303. ctx, NULL);
  304. return 0;
  305. }
  306. /*
  307. * This wraps dbus_server_listen(), additionally taking care of the event loop
  308. * -related setups.
  309. */
  310. DBusServer *
  311. lws_dbus_server_listen(struct lws_dbus_ctx *ctx, const char *ads, DBusError *e,
  312. DBusNewConnectionFunction new_conn)
  313. {
  314. ctx->cb_closing = NULL;
  315. ctx->hup = 0;
  316. ctx->timeouts = 0;
  317. ctx->dbs = dbus_server_listen(ads, e);
  318. if (!ctx->dbs)
  319. return NULL;
  320. dbus_server_set_new_connection_function(ctx->dbs, new_conn, ctx, NULL);
  321. if (!dbus_server_set_watch_functions(ctx->dbs, lws_dbus_add_watch,
  322. lws_dbus_remove_watch,
  323. lws_dbus_toggle_watch,
  324. ctx, NULL)) {
  325. lwsl_err("%s: dbus_connection_set_watch_functions fail\n",
  326. __func__);
  327. goto bail;
  328. }
  329. if (!dbus_server_set_timeout_functions(ctx->dbs, lws_dbus_add_timeout,
  330. lws_dbus_remove_timeout,
  331. lws_dbus_toggle_timeout,
  332. ctx, NULL)) {
  333. lwsl_err("%s: dbus_connection_set_timeout_functions fail\n",
  334. __func__);
  335. goto bail;
  336. }
  337. return ctx->dbs;
  338. bail:
  339. dbus_server_disconnect(ctx->dbs);
  340. dbus_server_unref(ctx->dbs);
  341. return NULL;
  342. }
  343. /*
  344. * There shouldn't be a race here with watcher removal and poll wait, because
  345. * everything including the dbus activity is serialized in one event loop.
  346. *
  347. * If it removes the watcher and we remove the wsi and fd entry before this,
  348. * actually we can no longer map the fd to this invalidated wsi pointer to call
  349. * this.
  350. */
  351. static int
  352. rops_handle_POLLIN_dbus(struct lws_context_per_thread *pt, struct lws *wsi,
  353. struct lws_pollfd *pollfd)
  354. {
  355. struct lws_dbus_ctx *ctx =
  356. (struct lws_dbus_ctx *)wsi->opaque_parent_data;
  357. unsigned int flags = 0;
  358. int n;
  359. if (pollfd->revents & LWS_POLLIN)
  360. flags |= DBUS_WATCH_READABLE;
  361. if (pollfd->revents & LWS_POLLOUT)
  362. flags |= DBUS_WATCH_WRITABLE;
  363. if (pollfd->revents & (LWS_POLLHUP))
  364. ctx->hup = 1;
  365. /*
  366. * POLLIN + POLLOUT gets us called here on the corresponding shadow
  367. * wsi. wsi->opaque_parent_data is the watcher handle bound to the wsi
  368. */
  369. for (n = 0; n < (int)LWS_ARRAY_SIZE(ctx->w); n++)
  370. if (ctx->w[n] && !dbus_watch_handle(ctx->w[n], flags))
  371. lwsl_err("%s: dbus_watch_handle failed\n", __func__);
  372. if (ctx->conn) {
  373. lwsl_info("%s: conn: flags %d\n", __func__, flags);
  374. while (dbus_connection_get_dispatch_status(ctx->conn) ==
  375. DBUS_DISPATCH_DATA_REMAINS)
  376. dbus_connection_dispatch(ctx->conn);
  377. handle_dispatch_status(NULL, DBUS_DISPATCH_DATA_REMAINS, NULL);
  378. check_destroy_shadow_wsi(ctx, wsi);
  379. } else
  380. if (ctx->dbs)
  381. /* ??? */
  382. lwsl_debug("%s: dbs: %d\n", __func__, flags);
  383. return LWS_HPI_RET_HANDLED;
  384. }
  385. static void
  386. lws_dbus_sul_cb(lws_sorted_usec_list_t *sul)
  387. {
  388. struct lws_context_per_thread *pt = lws_container_of(sul,
  389. struct lws_context_per_thread, dbus.sul);
  390. lws_start_foreach_dll_safe(struct lws_dll2 *, rdt, nx,
  391. lws_dll2_get_head(&pt->dbus.timer_list_owner)) {
  392. struct lws_role_dbus_timer *r = lws_container_of(rdt,
  393. struct lws_role_dbus_timer, timer_list);
  394. if (time(NULL) > r->fire) {
  395. lwsl_notice("%s: firing timer\n", __func__);
  396. dbus_timeout_handle(r->data);
  397. lws_dll2_remove(rdt);
  398. lws_free(rdt);
  399. }
  400. } lws_end_foreach_dll_safe(rdt, nx);
  401. lws_sul_schedule(pt->context, pt->tid, &pt->dbus.sul, lws_dbus_sul_cb,
  402. 3 * LWS_US_PER_SEC);
  403. }
  404. static int
  405. rops_pt_init_destroy_dbus(struct lws_context *context,
  406. const struct lws_context_creation_info *info,
  407. struct lws_context_per_thread *pt, int destroy)
  408. {
  409. if (!destroy) {
  410. lws_sul_schedule(context, pt->tid, &pt->dbus.sul, lws_dbus_sul_cb,
  411. 3 * LWS_US_PER_SEC);
  412. } else
  413. lws_sul_cancel(&pt->dbus.sul);
  414. return 0;
  415. }
  416. const struct lws_role_ops role_ops_dbus = {
  417. /* role name */ "dbus",
  418. /* alpn id */ NULL,
  419. /* check_upgrades */ NULL,
  420. /* pt_init_destroy */ rops_pt_init_destroy_dbus,
  421. /* init_vhost */ NULL,
  422. /* destroy_vhost */ NULL,
  423. /* service_flag_pending */ NULL,
  424. /* handle_POLLIN */ rops_handle_POLLIN_dbus,
  425. /* handle_POLLOUT */ NULL,
  426. /* perform_user_POLLOUT */ NULL,
  427. /* callback_on_writable */ NULL,
  428. /* tx_credit */ NULL,
  429. /* write_role_protocol */ NULL,
  430. /* encapsulation_parent */ NULL,
  431. /* alpn_negotiated */ NULL,
  432. /* close_via_role_protocol */ NULL,
  433. /* close_role */ NULL,
  434. /* close_kill_connection */ NULL,
  435. /* destroy_role */ NULL,
  436. /* adoption_bind */ NULL,
  437. /* client_bind */ NULL,
  438. /* issue_keepalive */ NULL,
  439. /* adoption_cb clnt, srv */ { 0, 0 },
  440. /* rx_cb clnt, srv */ { 0, 0 },
  441. /* writeable cb clnt, srv */ { 0, 0 },
  442. /* close cb clnt, srv */ { 0, 0 },
  443. /* protocol_bind_cb c,s */ { 0, 0 },
  444. /* protocol_unbind_cb c,s */ { 0, 0 },
  445. /* file_handle */ 0,
  446. };