vh doubly linked list for wsi on same protocol
[platform/upstream/libwebsockets.git] / lib / libuv.c
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010-2016 Andy Green <andy@warmcat.com>
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Lesser General Public
8  *  License as published by the Free Software Foundation:
9  *  version 2.1 of the License.
10  *
11  *  This library is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *  Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public
17  *  License along with this library; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  *  MA  02110-1301  USA
20  */
21
22 #include "private-libwebsockets.h"
23
24 void
25 lws_feature_status_libuv(struct lws_context_creation_info *info)
26 {
27         if (lws_check_opt(info->options, LWS_SERVER_OPTION_LIBUV))
28                 lwsl_notice("libuv support compiled in and enabled\n");
29         else
30                 lwsl_notice("libuv support compiled in but disabled\n");
31 }
32
33 static void
34 lws_uv_idle(uv_idle_t *handle
35 #if UV_VERSION_MAJOR == 0
36                 , int status
37 #endif
38 )
39 {
40         struct lws_context_per_thread *pt = lws_container_of(handle,
41                                         struct lws_context_per_thread, uv_idle);
42
43         lwsl_debug("%s\n", __func__);
44
45         /*
46          * is there anybody with pending stuff that needs service forcing?
47          */
48         if (!lws_service_adjust_timeout(pt->context, 1, pt->tid)) {
49                 /* -1 timeout means just do forced service */
50                 lws_plat_service_tsi(pt->context, -1, pt->tid);
51                 /* still somebody left who wants forced service? */
52                 if (!lws_service_adjust_timeout(pt->context, 1, pt->tid))
53                         /* yes... come back again later */
54                         lwsl_debug("%s: done again\n", __func__);
55                         return;
56         }
57
58         /* there is nobody who needs service forcing, shut down idle */
59         uv_idle_stop(handle);
60
61         lwsl_debug("%s: done stop\n", __func__);
62 }
63
64 static void
65 lws_io_cb(uv_poll_t *watcher, int status, int revents)
66 {
67         struct lws_io_watcher *lws_io = lws_container_of(watcher,
68                                         struct lws_io_watcher, uv_watcher);
69         struct lws *wsi = lws_container_of(lws_io, struct lws, w_read);
70         struct lws_context *context = lws_io->context;
71         struct lws_pollfd eventfd;
72
73 #if defined(WIN32) || defined(_WIN32)
74         eventfd.fd = watcher->socket;
75 #else
76         eventfd.fd = watcher->io_watcher.fd;
77 #endif
78         eventfd.events = 0;
79         eventfd.revents = 0;
80
81         if (status < 0) {
82                 /* at this point status will be an UV error, like UV_EBADF,
83                 we treat all errors as LWS_POLLHUP */
84
85                 /* you might want to return; instead of servicing the fd in some cases */
86                 if (status == UV_EAGAIN)
87                         return;
88
89                 eventfd.events |= LWS_POLLHUP;
90                 eventfd.revents |= LWS_POLLHUP;
91         } else {
92                 if (revents & UV_READABLE) {
93                         eventfd.events |= LWS_POLLIN;
94                         eventfd.revents |= LWS_POLLIN;
95                 }
96                 if (revents & UV_WRITABLE) {
97                         eventfd.events |= LWS_POLLOUT;
98                         eventfd.revents |= LWS_POLLOUT;
99                 }
100         }
101         lws_service_fd(context, &eventfd);
102
103         uv_idle_start(&context->pt[(int)wsi->tsi].uv_idle, lws_uv_idle);
104 }
105
106 LWS_VISIBLE void
107 lws_uv_sigint_cb(uv_signal_t *watcher, int signum)
108 {
109         lwsl_info("internal signal handler caught signal %d\n", signum);
110         lws_libuv_stop(watcher->data);
111 }
112
113 LWS_VISIBLE int
114 lws_uv_sigint_cfg(struct lws_context *context, int use_uv_sigint,
115                   uv_signal_cb cb)
116 {
117         context->use_ev_sigint = use_uv_sigint;
118         if (cb)
119                 context->lws_uv_sigint_cb = cb;
120         else
121                 context->lws_uv_sigint_cb = &lws_uv_sigint_cb;
122
123         return 0;
124 }
125
126 static void
127 lws_uv_timeout_cb(uv_timer_t *timer
128 #if UV_VERSION_MAJOR == 0
129                 , int status
130 #endif
131 )
132 {
133         struct lws_context_per_thread *pt = lws_container_of(timer,
134                         struct lws_context_per_thread, uv_timeout_watcher);
135
136         lwsl_debug("%s\n", __func__);
137
138         lws_service_fd_tsi(pt->context, NULL, pt->tid);
139 }
140
141 static const int sigs[] = { SIGINT, SIGTERM, SIGSEGV, SIGFPE };
142
143 LWS_VISIBLE int
144 lws_uv_initloop(struct lws_context *context, uv_loop_t *loop, int tsi)
145 {
146         struct lws_context_per_thread *pt = &context->pt[tsi];
147         struct lws_vhost *vh = context->vhost_list;
148         int status = 0, n;
149
150         if (!loop) {
151                 loop = lws_malloc(sizeof(*loop));
152 #if UV_VERSION_MAJOR > 0
153                 uv_loop_init(loop);
154 #else
155                 lwsl_err("This libuv is too old to work...\n");
156                 return 1;
157 #endif
158                 pt->ev_loop_foreign = 0;
159         } else
160                 pt->ev_loop_foreign = 1;
161
162         pt->io_loop_uv = loop;
163         uv_idle_init(loop, &pt->uv_idle);
164
165         if (pt->context->use_ev_sigint) {
166                 assert(ARRAY_SIZE(sigs) <= ARRAY_SIZE(pt->signals));
167                 for (n = 0; n < ARRAY_SIZE(sigs); n++) {
168                         uv_signal_init(loop, &pt->signals[n]);
169                         pt->signals[n].data = pt->context;
170                         uv_signal_start(&pt->signals[n],
171                                         context->lws_uv_sigint_cb, sigs[n]);
172                 }
173         }
174
175         /*
176          * Initialize the accept wsi read watcher with all the listening sockets
177          * and register a callback for read operations
178          *
179          * We have to do it here because the uv loop(s) are not
180          * initialized until after context creation.
181          */
182         while (vh) {
183                 if (vh->lserv_wsi) {
184                         vh->lserv_wsi->w_read.context = context;
185                         n = uv_poll_init_socket(pt->io_loop_uv,
186                                      &vh->lserv_wsi->w_read.uv_watcher,
187                                      vh->lserv_wsi->sock);
188                         if (n) {
189                                 lwsl_err("uv_poll_init failed %d, sockfd=%p\n",
190                                         n, (void *)(long)vh->lserv_wsi->sock);
191
192                                 return -1;
193                         }
194                         uv_poll_start(&vh->lserv_wsi->w_read.uv_watcher,
195                                       UV_READABLE, lws_io_cb);
196                 }
197                 vh = vh->vhost_next;
198         }
199
200         uv_timer_init(pt->io_loop_uv, &pt->uv_timeout_watcher);
201         uv_timer_start(&pt->uv_timeout_watcher, lws_uv_timeout_cb, 1000, 1000);
202
203         return status;
204 }
205
206 void lws_uv_close_cb(uv_handle_t *handle)
207 {
208
209 }
210
211 void lws_uv_walk_cb(uv_handle_t *handle, void *arg)
212 {
213         uv_close(handle, lws_uv_close_cb);
214 }
215
216 void
217 lws_libuv_destroyloop(struct lws_context *context, int tsi)
218 {
219         struct lws_context_per_thread *pt = &context->pt[tsi];
220         int m;
221
222         if (!lws_check_opt(context->options, LWS_SERVER_OPTION_LIBUV))
223                 return;
224
225         if (!pt->io_loop_uv)
226                 return;
227
228         if (context->use_ev_sigint)
229                 uv_signal_stop(&pt->w_sigint.uv_watcher);
230         for (m = 0; m < ARRAY_SIZE(sigs); m++)
231                 uv_signal_stop(&pt->signals[m]);
232         if (!pt->ev_loop_foreign) {
233                 uv_stop(pt->io_loop_uv);
234                 uv_walk(pt->io_loop_uv, lws_uv_walk_cb, NULL);
235                 while (uv_run(pt->io_loop_uv, UV_RUN_NOWAIT));
236 #if UV_VERSION_MAJOR > 0
237                 m = uv_loop_close(pt->io_loop_uv);
238                 if (m == UV_EBUSY)
239                         lwsl_debug("%s: uv_loop_close: UV_EBUSY\n", __func__);
240 #endif
241                 lws_free(pt->io_loop_uv);
242         }
243 }
244
245 void
246 lws_libuv_accept(struct lws *wsi, int accept_fd)
247 {
248         struct lws_context *context = lws_get_context(wsi);
249         struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
250
251         if (!LWS_LIBUV_ENABLED(context))
252                 return;
253
254         lwsl_debug("%s: new wsi %p\n", __func__, wsi);
255
256         wsi->w_read.context = context;
257
258         uv_poll_init(pt->io_loop_uv, &wsi->w_read.uv_watcher, accept_fd);
259 }
260
261 void
262 lws_libuv_io(struct lws *wsi, int flags)
263 {
264         struct lws_context *context = lws_get_context(wsi);
265         struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
266 #if defined(WIN32) || defined(_WIN32)
267         int current_events = wsi->w_read.uv_watcher.events &
268                              (UV_READABLE | UV_WRITABLE);
269 #else
270         int current_events = wsi->w_read.uv_watcher.io_watcher.pevents &
271                              (UV_READABLE | UV_WRITABLE);
272 #endif
273         struct lws_io_watcher *w = &wsi->w_read;
274
275         if (!LWS_LIBUV_ENABLED(context))
276                 return;
277
278         lwsl_debug("%s: wsi: %p, flags:0x%x\n", __func__, wsi, flags);
279
280         if (!pt->io_loop_uv) {
281                 lwsl_info("%s: no io loop yet\n", __func__);
282                 return;
283         }
284
285         assert((flags & (LWS_EV_START | LWS_EV_STOP)) &&
286                (flags & (LWS_EV_READ | LWS_EV_WRITE)));
287
288         if (flags & LWS_EV_START) {
289                 if (flags & LWS_EV_WRITE)
290                         current_events |= UV_WRITABLE;
291
292                 if (flags & LWS_EV_READ)
293                         current_events |= UV_READABLE;
294
295                 uv_poll_start(&w->uv_watcher, current_events, lws_io_cb);
296         } else {
297                 if (flags & LWS_EV_WRITE)
298                         current_events &= ~UV_WRITABLE;
299
300                 if (flags & LWS_EV_READ)
301                         current_events &= ~UV_READABLE;
302
303                 if (!(current_events & (UV_READABLE | UV_WRITABLE)))
304                         uv_poll_stop(&w->uv_watcher);
305                 else
306                         uv_poll_start(&w->uv_watcher, current_events,
307                                       lws_io_cb);
308         }
309 }
310
311 int
312 lws_libuv_init_fd_table(struct lws_context *context)
313 {
314         int n;
315
316         if (!LWS_LIBUV_ENABLED(context))
317                 return 0;
318
319         for (n = 0; n < context->count_threads; n++)
320                 context->pt[n].w_sigint.context = context;
321
322         return 1;
323 }
324
325 LWS_VISIBLE void
326 lws_libuv_run(const struct lws_context *context, int tsi)
327 {
328         if (context->pt[tsi].io_loop_uv && LWS_LIBUV_ENABLED(context))
329                 uv_run(context->pt[tsi].io_loop_uv, 0);
330 }
331
332 static void
333 lws_libuv_kill(const struct lws_context *context)
334 {
335         int n;
336
337         for (n = 0; n < context->count_threads; n++)
338                 if (context->pt[n].io_loop_uv && LWS_LIBUV_ENABLED(context))
339                         uv_stop(context->pt[n].io_loop_uv);
340         // TODO uv_stop check foreign loop? or not?
341 }
342
343 /*
344  * This does not actually stop the event loop.  The reason is we have to pass
345  * libuv handle closures through its event loop.  So this tries to close all
346  * wsi, and set a flag; when all the wsi closures are finalized then we
347  * actually stop the libuv event loops.
348  */
349
350 LWS_VISIBLE void
351 lws_libuv_stop(struct lws_context *context)
352 {
353         struct lws_context_per_thread *pt;
354         int n, m;
355
356         context->requested_kill = 1;
357
358         m = context->count_threads;
359         context->being_destroyed = 1;
360
361         while (m--) {
362                 pt = &context->pt[m];
363
364                 for (n = 0; (unsigned int)n < context->pt[m].fds_count; n++) {
365                         struct lws *wsi = wsi_from_fd(context, pt->fds[n].fd);
366
367                         if (!wsi)
368                                 continue;
369                         lws_close_free_wsi(wsi,
370                                 LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY
371                                 /* no protocol close */);
372                         n--;
373                 }
374         }
375
376         lwsl_info("%s: feels everything closed\n", __func__);
377         if (context->count_wsi_allocated == 0)
378                 lws_libuv_kill(context);
379 }
380
381 LWS_VISIBLE uv_loop_t *
382 lws_uv_getloop(struct lws_context *context, int tsi)
383 {
384         if (context->pt[tsi].io_loop_uv && LWS_LIBUV_ENABLED(context))
385                 return context->pt[tsi].io_loop_uv;
386
387         return NULL;
388 }
389
390 static void
391 lws_libuv_closewsi(uv_handle_t* handle)
392 {
393         struct lws *n = NULL, *wsi = (struct lws *)(((char *)handle) -
394                           (char *)(&n->w_read.uv_watcher));
395         struct lws_context *context = lws_get_context(wsi);
396
397         lws_close_free_wsi_final(wsi);
398
399         if (context->requested_kill && context->count_wsi_allocated == 0)
400                 lws_libuv_kill(context);
401 }
402
403 void
404 lws_libuv_closehandle(struct lws *wsi)
405 {
406         struct lws_context *context = lws_get_context(wsi);
407
408         /* required to defer actual deletion until libuv has processed it */
409
410         uv_close((uv_handle_t*)&wsi->w_read.uv_watcher, lws_libuv_closewsi);
411
412         if (context->requested_kill && context->count_wsi_allocated == 0)
413                 lws_libuv_kill(context);
414 }
415
416 #if defined(LWS_WITH_PLUGINS) && (UV_VERSION_MAJOR > 0)
417
418 LWS_VISIBLE int
419 lws_plat_plugins_init(struct lws_context * context, const char *d)
420 {
421         struct lws_plugin_capability lcaps;
422         struct lws_plugin *plugin;
423         lws_plugin_init_func initfunc;
424         int m, ret = 0;
425         void *v;
426         uv_dirent_t dent;
427         uv_fs_t req;
428         char path[256];
429         uv_loop_t loop;
430         uv_lib_t lib;
431
432         lib.errmsg = NULL;
433         lib.handle = NULL;
434
435         uv_loop_init(&loop);
436
437         if (!uv_fs_scandir(&loop, &req, d, 0, NULL)) {
438                 lwsl_err("Scandir on %s failed\n", d);
439                 return 1;
440         }
441
442         lwsl_notice("  Plugins:\n");
443
444         while (uv_fs_scandir_next(&req, &dent) != UV_EOF) {
445                 if (strlen(dent.name) < 7)
446                         continue;
447
448                 lwsl_notice("   %s\n", dent.name);
449
450                 snprintf(path, sizeof(path) - 1, "%s/%s", d, dent.name);
451                 if (uv_dlopen(path, &lib)) {
452                         uv_dlerror(&lib);
453                         lwsl_err("Error loading DSO: %s\n", lib.errmsg);
454                         goto bail;
455                 }
456                 /* we could open it, can we get his init function? */
457                 m = snprintf(path, sizeof(path) - 1, "init_%s",
458                              dent.name + 3 /* snip lib... */);
459                 path[m - 3] = '\0'; /* snip the .so */
460                 if (uv_dlsym(&lib, path, &v)) {
461                         uv_dlerror(&lib);
462                         lwsl_err("Failed to get init on %s: %s",
463                                         dent.name, lib.errmsg);
464                         goto bail;
465                 }
466                 initfunc = (lws_plugin_init_func)v;
467                 lcaps.api_magic = LWS_PLUGIN_API_MAGIC;
468                 m = initfunc(context, &lcaps);
469                 if (m) {
470                         lwsl_err("Initializing %s failed %d\n", dent.name, m);
471                         goto skip;
472                 }
473
474                 plugin = lws_malloc(sizeof(*plugin));
475                 if (!plugin) {
476                         lwsl_err("OOM\n");
477                         goto bail;
478                 }
479                 plugin->list = context->plugin_list;
480                 context->plugin_list = plugin;
481                 strncpy(plugin->name, dent.name, sizeof(plugin->name) - 1);
482                 plugin->name[sizeof(plugin->name) - 1] = '\0';
483                 plugin->lib = lib;
484                 plugin->caps = lcaps;
485                 context->plugin_protocol_count += lcaps.count_protocols;
486                 context->plugin_extension_count += lcaps.count_extensions;
487
488                 continue;
489
490 skip:
491                 uv_dlclose(&lib);
492         }
493
494 bail:
495         uv_fs_req_cleanup(&req);
496         uv_loop_close(&loop);
497
498         return ret;
499
500 }
501
502 LWS_VISIBLE int
503 lws_plat_plugins_destroy(struct lws_context * context)
504 {
505         struct lws_plugin *plugin = context->plugin_list, *p;
506         lws_plugin_destroy_func func;
507         char path[256];
508         void *v;
509         int m;
510
511         if (!plugin)
512                 return 0;
513
514         lwsl_notice("%s\n", __func__);
515
516         while (plugin) {
517                 p = plugin;
518                 m = snprintf(path, sizeof(path) - 1, "destroy_%s", plugin->name + 3);
519                 path[m - 3] = '\0';
520
521                 if (uv_dlsym(&plugin->lib, path, &v)) {
522                         uv_dlerror(&plugin->lib);
523                         lwsl_err("Failed to get init on %s: %s",
524                                         plugin->name, plugin->lib.errmsg);
525                 } else {
526                         func = (lws_plugin_destroy_func)v;
527                         m = func(context);
528                         if (m)
529                                 lwsl_err("Destroying %s failed %d\n",
530                                                 plugin->name, m);
531                 }
532
533                 uv_dlclose(&p->lib);
534                 plugin = p->list;
535                 p->list = NULL;
536                 free(p);
537         }
538
539         context->plugin_list = NULL;
540
541         return 0;
542 }
543
544 #endif
545