win fix warnings from appveyor
[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_err("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         if (pt->context->requested_kill)
137                 return;
138
139         lwsl_debug("%s\n", __func__);
140
141         lws_service_fd_tsi(pt->context, NULL, pt->tid);
142 }
143
144 static const int sigs[] = { SIGINT, SIGTERM, SIGSEGV, SIGFPE };
145
146 LWS_VISIBLE int
147 lws_uv_initloop(struct lws_context *context, uv_loop_t *loop, int tsi)
148 {
149         struct lws_context_per_thread *pt = &context->pt[tsi];
150         struct lws_vhost *vh = context->vhost_list;
151         int status = 0, n;
152
153         if (!loop) {
154                 loop = lws_malloc(sizeof(*loop));
155                 if (!loop) {
156                         lwsl_err("OOM\n");
157                         return -1;
158                 }
159 #if UV_VERSION_MAJOR > 0
160                 uv_loop_init(loop);
161 #else
162                 lwsl_err("This libuv is too old to work...\n");
163                 return 1;
164 #endif
165                 pt->ev_loop_foreign = 0;
166         } else {
167                 lwsl_notice(" Using foreign event loop...\n");
168                 pt->ev_loop_foreign = 1;
169         }
170
171         pt->io_loop_uv = loop;
172         uv_idle_init(loop, &pt->uv_idle);
173
174         if (pt->context->use_ev_sigint) {
175                 assert(ARRAY_SIZE(sigs) <= ARRAY_SIZE(pt->signals));
176                 for (n = 0; n < ARRAY_SIZE(sigs); n++) {
177                         uv_signal_init(loop, &pt->signals[n]);
178                         pt->signals[n].data = pt->context;
179                         uv_signal_start(&pt->signals[n],
180                                         context->lws_uv_sigint_cb, sigs[n]);
181                 }
182         }
183
184         /*
185          * Initialize the accept wsi read watcher with all the listening sockets
186          * and register a callback for read operations
187          *
188          * We have to do it here because the uv loop(s) are not
189          * initialized until after context creation.
190          */
191         while (vh) {
192                 if (vh->lserv_wsi) {
193                         vh->lserv_wsi->w_read.context = context;
194                         n = uv_poll_init_socket(pt->io_loop_uv,
195                                                 &vh->lserv_wsi->w_read.uv_watcher,
196                                                 vh->lserv_wsi->sock);
197                         if (n) {
198                                 lwsl_err("uv_poll_init failed %d, sockfd=%p\n",
199                                         n, (void *)(long)vh->lserv_wsi->sock);
200
201                                 return -1;
202                         }
203                         lws_libuv_io(vh->lserv_wsi, LWS_EV_START | LWS_EV_READ);
204                 }
205                 vh = vh->vhost_next;
206         }
207
208         uv_timer_init(pt->io_loop_uv, &pt->uv_timeout_watcher);
209         uv_timer_start(&pt->uv_timeout_watcher, lws_uv_timeout_cb, 10, 1000);
210
211         return status;
212 }
213
214 static void lws_uv_close_cb(uv_handle_t *handle)
215 {
216         //lwsl_err("%s: handle %p\n", __func__, handle);
217 }
218
219 static void lws_uv_walk_cb(uv_handle_t *handle, void *arg)
220 {
221         uv_close(handle, lws_uv_close_cb);
222 }
223
224 void
225 lws_libuv_destroyloop(struct lws_context *context, int tsi)
226 {
227         struct lws_context_per_thread *pt = &context->pt[tsi];
228         int m, budget = 100;
229
230         if (!lws_check_opt(context->options, LWS_SERVER_OPTION_LIBUV))
231                 return;
232
233         if (!pt->io_loop_uv)
234                 return;
235
236         if (context->use_ev_sigint) {
237                 uv_signal_stop(&pt->w_sigint.uv_watcher);
238
239                 for (m = 0; m < ARRAY_SIZE(sigs); m++) {
240                         uv_signal_stop(&pt->signals[m]);
241                         uv_close((uv_handle_t *)&pt->signals[m], lws_uv_close_cb);
242                 }
243         }
244
245         uv_timer_stop(&pt->uv_timeout_watcher);
246         uv_close((uv_handle_t *)&pt->uv_timeout_watcher, lws_uv_close_cb);
247
248         uv_idle_stop(&pt->uv_idle);
249         uv_close((uv_handle_t *)&pt->uv_idle, lws_uv_close_cb);
250
251         while (budget-- && uv_run(pt->io_loop_uv, UV_RUN_NOWAIT))
252                 ;
253
254         if (pt->ev_loop_foreign)
255                 return;
256
257         uv_stop(pt->io_loop_uv);
258
259         uv_walk(pt->io_loop_uv, lws_uv_walk_cb, NULL);
260
261         while (uv_run(pt->io_loop_uv, UV_RUN_NOWAIT))
262                 ;
263 #if UV_VERSION_MAJOR > 0
264         m = uv_loop_close(pt->io_loop_uv);
265         if (m == UV_EBUSY)
266                 lwsl_err("%s: uv_loop_close: UV_EBUSY\n", __func__);
267 #endif
268         lws_free(pt->io_loop_uv);
269 }
270
271 void
272 lws_libuv_accept(struct lws *wsi, lws_sockfd_type accept_fd)
273 {
274         struct lws_context *context = lws_get_context(wsi);
275         struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
276
277         if (!LWS_LIBUV_ENABLED(context))
278                 return;
279
280         lwsl_debug("%s: new wsi %p\n", __func__, wsi);
281
282         wsi->w_read.context = context;
283
284         uv_poll_init_socket(pt->io_loop_uv, &wsi->w_read.uv_watcher, accept_fd);
285 }
286
287 void
288 lws_libuv_io(struct lws *wsi, int flags)
289 {
290         struct lws_context *context = lws_get_context(wsi);
291         struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
292 #if defined(WIN32) || defined(_WIN32)
293         int current_events = wsi->w_read.uv_watcher.events &
294                              (UV_READABLE | UV_WRITABLE);
295 #else
296         int current_events = wsi->w_read.uv_watcher.io_watcher.pevents &
297                              (UV_READABLE | UV_WRITABLE);
298 #endif
299         struct lws_io_watcher *w = &wsi->w_read;
300
301         if (!LWS_LIBUV_ENABLED(context))
302                 return;
303
304         // lwsl_notice("%s: wsi: %p, flags:0x%x\n", __func__, wsi, flags);
305
306         if (!pt->io_loop_uv) {
307                 lwsl_info("%s: no io loop yet\n", __func__);
308                 return;
309         }
310
311         if (!((flags & (LWS_EV_START | LWS_EV_STOP)) &&
312               (flags & (LWS_EV_READ | LWS_EV_WRITE)))) {
313                 lwsl_err("%s: assert: flags %d", __func__, flags);
314                 assert(0);
315         }
316
317         if (flags & LWS_EV_START) {
318                 if (flags & LWS_EV_WRITE)
319                         current_events |= UV_WRITABLE;
320
321                 if (flags & LWS_EV_READ)
322                         current_events |= UV_READABLE;
323
324                 uv_poll_start(&w->uv_watcher, current_events, lws_io_cb);
325         } else {
326                 if (flags & LWS_EV_WRITE)
327                         current_events &= ~UV_WRITABLE;
328
329                 if (flags & LWS_EV_READ)
330                         current_events &= ~UV_READABLE;
331
332                 if (!(current_events & (UV_READABLE | UV_WRITABLE)))
333                         uv_poll_stop(&w->uv_watcher);
334                 else
335                         uv_poll_start(&w->uv_watcher, current_events,
336                                       lws_io_cb);
337         }
338 }
339
340 int
341 lws_libuv_init_fd_table(struct lws_context *context)
342 {
343         int n;
344
345         if (!LWS_LIBUV_ENABLED(context))
346                 return 0;
347
348         for (n = 0; n < context->count_threads; n++)
349                 context->pt[n].w_sigint.context = context;
350
351         return 1;
352 }
353
354 LWS_VISIBLE void
355 lws_libuv_run(const struct lws_context *context, int tsi)
356 {
357         if (context->pt[tsi].io_loop_uv && LWS_LIBUV_ENABLED(context))
358                 uv_run(context->pt[tsi].io_loop_uv, 0);
359 }
360
361 static void
362 lws_libuv_kill(const struct lws_context *context)
363 {
364         int n;
365
366         for (n = 0; n < context->count_threads; n++)
367                 if (context->pt[n].io_loop_uv &&
368                     LWS_LIBUV_ENABLED(context) &&
369                     !context->pt[n].ev_loop_foreign)
370                         uv_stop(context->pt[n].io_loop_uv);
371 }
372
373 /*
374  * This does not actually stop the event loop.  The reason is we have to pass
375  * libuv handle closures through its event loop.  So this tries to close all
376  * wsi, and set a flag; when all the wsi closures are finalized then we
377  * actually stop the libuv event loops.
378  */
379
380 LWS_VISIBLE void
381 lws_libuv_stop(struct lws_context *context)
382 {
383         struct lws_context_per_thread *pt;
384         int n, m;
385
386         if (context->requested_kill)
387                 return;
388
389         context->requested_kill = 1;
390
391         m = context->count_threads;
392         context->being_destroyed = 1;
393
394         while (m--) {
395                 pt = &context->pt[m];
396
397                 for (n = 0; (unsigned int)n < context->pt[m].fds_count; n++) {
398                         struct lws *wsi = wsi_from_fd(context, pt->fds[n].fd);
399
400                         if (!wsi)
401                                 continue;
402                         lws_close_free_wsi(wsi,
403                                 LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY
404                                 /* no protocol close */);
405                         n--;
406                 }
407         }
408
409         lwsl_info("%s: feels everything closed\n", __func__);
410         if (context->count_wsi_allocated == 0)
411                 lws_libuv_kill(context);
412 }
413
414 LWS_VISIBLE uv_loop_t *
415 lws_uv_getloop(struct lws_context *context, int tsi)
416 {
417         if (context->pt[tsi].io_loop_uv && LWS_LIBUV_ENABLED(context))
418                 return context->pt[tsi].io_loop_uv;
419
420         return NULL;
421 }
422
423 static void
424 lws_libuv_closewsi(uv_handle_t* handle)
425 {
426         struct lws *n = NULL, *wsi = (struct lws *)(((char *)handle) -
427                           (char *)(&n->w_read.uv_watcher));
428         struct lws_context *context = lws_get_context(wsi);
429
430         lws_close_free_wsi_final(wsi);
431
432         if (context->requested_kill && context->count_wsi_allocated == 0)
433                 lws_libuv_kill(context);
434 }
435
436 void
437 lws_libuv_closehandle(struct lws *wsi)
438 {
439         struct lws_context *context = lws_get_context(wsi);
440
441         /* required to defer actual deletion until libuv has processed it */
442
443         uv_close((uv_handle_t*)&wsi->w_read.uv_watcher, lws_libuv_closewsi);
444
445         if (context->requested_kill && context->count_wsi_allocated == 0)
446                 lws_libuv_kill(context);
447 }
448
449 #if defined(LWS_WITH_PLUGINS) && (UV_VERSION_MAJOR > 0)
450
451 LWS_VISIBLE int
452 lws_plat_plugins_init(struct lws_context * context, const char * const *d)
453 {
454         struct lws_plugin_capability lcaps;
455         struct lws_plugin *plugin;
456         lws_plugin_init_func initfunc;
457         int m, ret = 0;
458         void *v;
459         uv_dirent_t dent;
460         uv_fs_t req;
461         char path[256];
462         uv_loop_t loop;
463         uv_lib_t lib;
464
465         lib.errmsg = NULL;
466         lib.handle = NULL;
467
468         uv_loop_init(&loop);
469
470         lwsl_notice("  Plugins:\n");
471
472         while (d && *d) {
473
474                 lwsl_notice("  Scanning %s\n", *d);
475                 m =uv_fs_scandir(&loop, &req, *d, 0, NULL);
476                 if (m < 1) {
477                         lwsl_err("Scandir on %s failed\n", *d);
478                         return 1;
479                 }
480
481                 while (uv_fs_scandir_next(&req, &dent) != UV_EOF) {
482                         if (strlen(dent.name) < 7)
483                                 continue;
484
485                         lwsl_notice("   %s\n", dent.name);
486
487                         snprintf(path, sizeof(path) - 1, "%s/%s", *d, dent.name);
488                         if (uv_dlopen(path, &lib)) {
489                                 uv_dlerror(&lib);
490                                 lwsl_err("Error loading DSO: %s\n", lib.errmsg);
491                                 goto bail;
492                         }
493                         /* we could open it, can we get his init function? */
494                         m = snprintf(path, sizeof(path) - 1, "init_%s",
495                                      dent.name + 3 /* snip lib... */);
496                         path[m - 3] = '\0'; /* snip the .so */
497                         if (uv_dlsym(&lib, path, &v)) {
498                                 uv_dlerror(&lib);
499                                 lwsl_err("Failed to get init on %s: %s",
500                                                 dent.name, lib.errmsg);
501                                 goto bail;
502                         }
503                         initfunc = (lws_plugin_init_func)v;
504                         lcaps.api_magic = LWS_PLUGIN_API_MAGIC;
505                         m = initfunc(context, &lcaps);
506                         if (m) {
507                                 lwsl_err("Initializing %s failed %d\n", dent.name, m);
508                                 goto skip;
509                         }
510
511                         plugin = lws_malloc(sizeof(*plugin));
512                         if (!plugin) {
513                                 lwsl_err("OOM\n");
514                                 goto bail;
515                         }
516                         plugin->list = context->plugin_list;
517                         context->plugin_list = plugin;
518                         strncpy(plugin->name, dent.name, sizeof(plugin->name) - 1);
519                         plugin->name[sizeof(plugin->name) - 1] = '\0';
520                         plugin->lib = lib;
521                         plugin->caps = lcaps;
522                         context->plugin_protocol_count += lcaps.count_protocols;
523                         context->plugin_extension_count += lcaps.count_extensions;
524
525                         continue;
526
527 skip:
528                         uv_dlclose(&lib);
529                 }
530 bail:
531                 uv_fs_req_cleanup(&req);
532                 d++;
533         }
534
535         uv_loop_close(&loop);
536
537         return ret;
538
539 }
540
541 LWS_VISIBLE int
542 lws_plat_plugins_destroy(struct lws_context * context)
543 {
544         struct lws_plugin *plugin = context->plugin_list, *p;
545         lws_plugin_destroy_func func;
546         char path[256];
547         void *v;
548         int m;
549
550         if (!plugin)
551                 return 0;
552
553         // lwsl_notice("%s\n", __func__);
554
555         while (plugin) {
556                 p = plugin;
557                 m = snprintf(path, sizeof(path) - 1, "destroy_%s", plugin->name + 3);
558                 path[m - 3] = '\0';
559
560                 if (uv_dlsym(&plugin->lib, path, &v)) {
561                         uv_dlerror(&plugin->lib);
562                         lwsl_err("Failed to get init on %s: %s",
563                                         plugin->name, plugin->lib.errmsg);
564                 } else {
565                         func = (lws_plugin_destroy_func)v;
566                         m = func(context);
567                         if (m)
568                                 lwsl_err("Destroying %s failed %d\n",
569                                                 plugin->name, m);
570                 }
571
572                 uv_dlclose(&p->lib);
573                 plugin = p->list;
574                 p->list = NULL;
575                 free(p);
576         }
577
578         context->plugin_list = NULL;
579
580         return 0;
581 }
582
583 #endif
584