lws-vhost-destroy
[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 = wsi->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, SIGHUP };
145
146 int
147 lws_uv_initvhost(struct lws_vhost* vh, struct lws* wsi)
148 {
149         struct lws_context_per_thread *pt;
150         int n;
151
152         if (!LWS_LIBUV_ENABLED(vh->context))
153                 return 0;
154         if (!wsi)
155                 wsi = vh->lserv_wsi;
156         if (!wsi)
157                 return 0;
158         if (wsi->w_read.context)
159                 return 0;
160
161         pt = &vh->context->pt[(int)wsi->tsi];
162         if (!pt->io_loop_uv)
163                 return 0;
164
165         wsi->w_read.context = vh->context;
166         n = uv_poll_init_socket(pt->io_loop_uv,
167                                 &wsi->w_read.uv_watcher, wsi->desc.sockfd);
168         if (n) {
169                 lwsl_err("uv_poll_init failed %d, sockfd=%p\n",
170                                  n, (void *)(lws_intptr_t)wsi->desc.sockfd);
171
172                 return -1;
173         }
174         lws_libuv_io(wsi, LWS_EV_START | LWS_EV_READ);
175
176         return 0;
177 }
178
179 /*
180  * This needs to be called after vhosts have been defined.
181  *
182  * If later, after server start, another vhost is added, this must be
183  * called again to bind the vhost
184  */
185
186 LWS_VISIBLE int
187 lws_uv_initloop(struct lws_context *context, uv_loop_t *loop, int tsi)
188 {
189         struct lws_context_per_thread *pt = &context->pt[tsi];
190         struct lws_vhost *vh = context->vhost_list;
191         int status = 0, n, ns, first = 1;
192
193         if (!pt->io_loop_uv) {
194                 if (!loop) {
195                         loop = lws_malloc(sizeof(*loop));
196                         if (!loop) {
197                                 lwsl_err("OOM\n");
198                                 return -1;
199                         }
200         #if UV_VERSION_MAJOR > 0
201                         uv_loop_init(loop);
202         #else
203                         lwsl_err("This libuv is too old to work...\n");
204                         return 1;
205         #endif
206                         pt->ev_loop_foreign = 0;
207                 } else {
208                         lwsl_notice(" Using foreign event loop...\n");
209                         pt->ev_loop_foreign = 1;
210                 }
211
212                 pt->io_loop_uv = loop;
213                 uv_idle_init(loop, &pt->uv_idle);
214
215                 ns = ARRAY_SIZE(sigs);
216                 if (lws_check_opt(context->options,
217                                   LWS_SERVER_OPTION_UV_NO_SIGSEGV_SIGFPE_SPIN))
218                         ns = 2;
219
220                 if (pt->context->use_ev_sigint) {
221                         assert(ns <= ARRAY_SIZE(pt->signals));
222                         for (n = 0; n < ns; n++) {
223                                 uv_signal_init(loop, &pt->signals[n]);
224                                 pt->signals[n].data = pt->context;
225                                 uv_signal_start(&pt->signals[n],
226                                                 context->lws_uv_sigint_cb, sigs[n]);
227                         }
228                 }
229         } else
230                 first = 0;
231
232         /*
233          * Initialize the accept wsi read watcher with all the listening sockets
234          * and register a callback for read operations
235          *
236          * We have to do it here because the uv loop(s) are not
237          * initialized until after context creation.
238          */
239         while (vh) {
240                 if (lws_uv_initvhost(vh, vh->lserv_wsi) == -1)
241                         return -1;
242                 vh = vh->vhost_next;
243         }
244
245         if (first) {
246                 uv_timer_init(pt->io_loop_uv, &pt->uv_timeout_watcher);
247                 uv_timer_start(&pt->uv_timeout_watcher, lws_uv_timeout_cb,
248                                10, 1000);
249         }
250
251         return status;
252 }
253
254 static void lws_uv_close_cb(uv_handle_t *handle)
255 {
256         //lwsl_err("%s: handle %p\n", __func__, handle);
257 }
258
259 static void lws_uv_walk_cb(uv_handle_t *handle, void *arg)
260 {
261         if (!uv_is_closing(handle))
262                 uv_close(handle, lws_uv_close_cb);
263 }
264
265 LWS_VISIBLE void
266 lws_close_all_handles_in_loop(uv_loop_t *loop)
267 {
268         uv_walk(loop, lws_uv_walk_cb, NULL);
269 }
270
271 void
272 lws_libuv_destroyloop(struct lws_context *context, int tsi)
273 {
274         struct lws_context_per_thread *pt = &context->pt[tsi];
275 //      struct lws_context *ctx;
276         int m, budget = 100, ns;
277
278         if (!lws_check_opt(context->options, LWS_SERVER_OPTION_LIBUV))
279                 return;
280
281         if (!pt->io_loop_uv)
282                 return;
283
284         lwsl_notice("%s: closing signals + timers context %p\n", __func__, context);
285
286         if (context->use_ev_sigint) {
287                 uv_signal_stop(&pt->w_sigint.uv_watcher);
288
289                 ns = ARRAY_SIZE(sigs);
290                 if (lws_check_opt(context->options, LWS_SERVER_OPTION_UV_NO_SIGSEGV_SIGFPE_SPIN))
291                         ns = 2;
292
293                 for (m = 0; m < ns; m++) {
294                         uv_signal_stop(&pt->signals[m]);
295                         uv_close((uv_handle_t *)&pt->signals[m], lws_uv_close_cb);
296                 }
297         }
298
299         uv_timer_stop(&pt->uv_timeout_watcher);
300         uv_close((uv_handle_t *)&pt->uv_timeout_watcher, lws_uv_close_cb);
301
302         uv_idle_stop(&pt->uv_idle);
303         uv_close((uv_handle_t *)&pt->uv_idle, lws_uv_close_cb);
304
305         if (pt->ev_loop_foreign)
306                 return;
307
308         while (budget-- && uv_run(pt->io_loop_uv, UV_RUN_NOWAIT))
309                 ;
310
311         lwsl_notice("%s: closing all loop handles context %p\n", __func__, context);
312
313         uv_stop(pt->io_loop_uv);
314
315         uv_walk(pt->io_loop_uv, lws_uv_walk_cb, NULL);
316
317         while (uv_run(pt->io_loop_uv, UV_RUN_NOWAIT))
318                 ;
319 #if UV_VERSION_MAJOR > 0
320         m = uv_loop_close(pt->io_loop_uv);
321         if (m == UV_EBUSY)
322                 lwsl_err("%s: uv_loop_close: UV_EBUSY\n", __func__);
323 #endif
324         lws_free(pt->io_loop_uv);
325 }
326
327 void
328 lws_libuv_accept(struct lws *wsi, lws_sock_file_fd_type desc)
329 {
330         struct lws_context *context = lws_get_context(wsi);
331         struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
332
333         if (!LWS_LIBUV_ENABLED(context))
334                 return;
335
336         lwsl_debug("%s: new wsi %p\n", __func__, wsi);
337
338         wsi->w_read.context = context;
339         if (wsi->mode == LWSCM_RAW_FILEDESC)
340                 uv_poll_init(pt->io_loop_uv, &wsi->w_read.uv_watcher,
341                              (int)desc.filefd);
342         else
343                 uv_poll_init_socket(pt->io_loop_uv, &wsi->w_read.uv_watcher,
344                                     desc.sockfd);
345 }
346
347 void
348 lws_libuv_io(struct lws *wsi, int flags)
349 {
350         struct lws_context *context = lws_get_context(wsi);
351         struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
352 #if defined(WIN32) || defined(_WIN32)
353         int current_events = wsi->w_read.uv_watcher.events &
354                              (UV_READABLE | UV_WRITABLE);
355 #else
356         int current_events = wsi->w_read.uv_watcher.io_watcher.pevents &
357                              (UV_READABLE | UV_WRITABLE);
358 #endif
359         struct lws_io_watcher *w = &wsi->w_read;
360
361         if (!LWS_LIBUV_ENABLED(context))
362                 return;
363
364         // lwsl_notice("%s: wsi: %p, flags:0x%x\n", __func__, wsi, flags);
365
366         // w->context is set after the loop is initialized
367
368         if (!pt->io_loop_uv || !w->context) {
369                 lwsl_info("%s: no io loop yet\n", __func__);
370                 return;
371         }
372
373         if (!((flags & (LWS_EV_START | LWS_EV_STOP)) &&
374               (flags & (LWS_EV_READ | LWS_EV_WRITE)))) {
375                 lwsl_err("%s: assert: flags %d", __func__, flags);
376                 assert(0);
377         }
378
379         if (flags & LWS_EV_START) {
380                 if (flags & LWS_EV_WRITE)
381                         current_events |= UV_WRITABLE;
382
383                 if (flags & LWS_EV_READ)
384                         current_events |= UV_READABLE;
385
386                 uv_poll_start(&w->uv_watcher, current_events, lws_io_cb);
387         } else {
388                 if (flags & LWS_EV_WRITE)
389                         current_events &= ~UV_WRITABLE;
390
391                 if (flags & LWS_EV_READ)
392                         current_events &= ~UV_READABLE;
393
394                 if (!(current_events & (UV_READABLE | UV_WRITABLE)))
395                         uv_poll_stop(&w->uv_watcher);
396                 else
397                         uv_poll_start(&w->uv_watcher, current_events,
398                                       lws_io_cb);
399         }
400 }
401
402 int
403 lws_libuv_init_fd_table(struct lws_context *context)
404 {
405         int n;
406
407         if (!LWS_LIBUV_ENABLED(context))
408                 return 0;
409
410         for (n = 0; n < context->count_threads; n++)
411                 context->pt[n].w_sigint.context = context;
412
413         return 1;
414 }
415
416 LWS_VISIBLE void
417 lws_libuv_run(const struct lws_context *context, int tsi)
418 {
419         if (context->pt[tsi].io_loop_uv && LWS_LIBUV_ENABLED(context))
420                 uv_run(context->pt[tsi].io_loop_uv, 0);
421 }
422
423 LWS_VISIBLE void
424 lws_libuv_stop_without_kill(const struct lws_context *context, int tsi)
425 {
426         if (context->pt[tsi].io_loop_uv && LWS_LIBUV_ENABLED(context))
427                 uv_stop(context->pt[tsi].io_loop_uv);
428 }
429
430 static void
431 lws_libuv_kill(const struct lws_context *context)
432 {
433         int n;
434
435         lwsl_notice("%s\n", __func__);
436
437         for (n = 0; n < context->count_threads; n++)
438                 if (context->pt[n].io_loop_uv &&
439                     LWS_LIBUV_ENABLED(context) )//&&
440                     //!context->pt[n].ev_loop_foreign)
441                         uv_stop(context->pt[n].io_loop_uv);
442 }
443
444 /*
445  * This does not actually stop the event loop.  The reason is we have to pass
446  * libuv handle closures through its event loop.  So this tries to close all
447  * wsi, and set a flag; when all the wsi closures are finalized then we
448  * actually stop the libuv event loops.
449  */
450
451 LWS_VISIBLE void
452 lws_libuv_stop(struct lws_context *context)
453 {
454         struct lws_context_per_thread *pt;
455         int n, m;
456
457         if (context->requested_kill)
458                 return;
459
460         context->requested_kill = 1;
461
462         m = context->count_threads;
463         context->being_destroyed = 1;
464
465         while (m--) {
466                 pt = &context->pt[m];
467
468                 for (n = 0; (unsigned int)n < context->pt[m].fds_count; n++) {
469                         struct lws *wsi = wsi_from_fd(context, pt->fds[n].fd);
470
471                         if (!wsi)
472                                 continue;
473                         lws_close_free_wsi(wsi,
474                                 LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY
475                                 /* no protocol close */);
476                         n--;
477                 }
478         }
479
480         lwsl_info("%s: feels everything closed\n", __func__);
481         if (context->count_wsi_allocated == 0)
482                 lws_libuv_kill(context);
483 }
484
485 LWS_VISIBLE uv_loop_t *
486 lws_uv_getloop(struct lws_context *context, int tsi)
487 {
488         if (context->pt[tsi].io_loop_uv && LWS_LIBUV_ENABLED(context))
489                 return context->pt[tsi].io_loop_uv;
490
491         return NULL;
492 }
493
494 static void
495 lws_libuv_closewsi(uv_handle_t* handle)
496 {
497         struct lws *n = NULL, *wsi = (struct lws *)(((char *)handle) -
498                           (char *)(&n->w_read.uv_watcher));
499         struct lws_context *context = lws_get_context(wsi);
500         int lspd = 0;
501
502         if (wsi->mode == LWSCM_SERVER_LISTENER &&
503             wsi->context->deprecated) {
504                 lspd = 1;
505                 context->deprecation_pending_listen_close_count--;
506                 if (!context->deprecation_pending_listen_close_count)
507                         lspd = 2;
508         }
509
510         lws_close_free_wsi_final(wsi);
511
512         if (lspd == 2 && context->deprecation_cb) {
513                 lwsl_notice("calling deprecation callback\n");
514                 context->deprecation_cb();
515         }
516
517         //lwsl_notice("%s: ctx %p: wsi left %d\n", __func__, context, context->count_wsi_allocated);
518
519         if (context->requested_kill && context->count_wsi_allocated == 0)
520                 lws_libuv_kill(context);
521 }
522
523 void
524 lws_libuv_closehandle(struct lws *wsi)
525 {
526         struct lws_context *context = lws_get_context(wsi);
527
528         /* required to defer actual deletion until libuv has processed it */
529         uv_close((uv_handle_t*)&wsi->w_read.uv_watcher, lws_libuv_closewsi);
530
531         if (context->requested_kill && context->count_wsi_allocated == 0)
532                 lws_libuv_kill(context);
533 }
534
535 static void
536 lws_libuv_closewsi_m(uv_handle_t* handle)
537 {
538         lws_sockfd_type sockfd = (lws_sockfd_type)(lws_intptr_t)handle->data;
539
540         compatible_close(sockfd);
541 }
542
543 void
544 lws_libuv_closehandle_manually(struct lws *wsi)
545 {
546         uv_handle_t *h = (void *)&wsi->w_read.uv_watcher;
547
548         h->data = (void *)(lws_intptr_t)wsi->desc.sockfd;
549         /* required to defer actual deletion until libuv has processed it */
550         uv_close((uv_handle_t*)&wsi->w_read.uv_watcher, lws_libuv_closewsi_m);
551 }
552
553 int
554 lws_libuv_check_watcher_active(struct lws *wsi)
555 {
556         uv_handle_t *h = (void *)&wsi->w_read.uv_watcher;
557
558         return uv_is_active(h);
559 }
560
561
562 #if defined(LWS_WITH_PLUGINS) && (UV_VERSION_MAJOR > 0)
563
564 LWS_VISIBLE int
565 lws_plat_plugins_init(struct lws_context *context, const char * const *d)
566 {
567         struct lws_plugin_capability lcaps;
568         struct lws_plugin *plugin;
569         lws_plugin_init_func initfunc;
570         int m, ret = 0;
571         void *v;
572         uv_dirent_t dent;
573         uv_fs_t req;
574         char path[256];
575         uv_lib_t lib;
576         int pofs = 0;
577
578 #if  defined(__MINGW32__) || !defined(WIN32)
579         pofs = 3;
580 #endif
581
582         lib.errmsg = NULL;
583         lib.handle = NULL;
584
585         uv_loop_init(&context->pu_loop);
586
587         lwsl_notice("  Plugins:\n");
588
589         while (d && *d) {
590
591                 lwsl_notice("  Scanning %s\n", *d);
592                 m =uv_fs_scandir(&context->pu_loop, &req, *d, 0, NULL);
593                 if (m < 1) {
594                         lwsl_err("Scandir on %s failed\n", *d);
595                         return 1;
596                 }
597
598                 while (uv_fs_scandir_next(&req, &dent) != UV_EOF) {
599                         if (strlen(dent.name) < 7)
600                                 continue;
601
602                         lwsl_notice("   %s\n", dent.name);
603
604                         lws_snprintf(path, sizeof(path) - 1, "%s/%s", *d, dent.name);
605                         if (uv_dlopen(path, &lib)) {
606                                 uv_dlerror(&lib);
607                                 lwsl_err("Error loading DSO: %s\n", lib.errmsg);
608                                 uv_dlclose(&lib);
609                                 goto bail;
610                         }
611
612                         /* we could open it, can we get his init function? */
613
614 #if !defined(WIN32) && !defined(__MINGW32__)
615                         m = lws_snprintf(path, sizeof(path) - 1, "init_%s",
616                                      dent.name + pofs /* snip lib... */);
617                         path[m - 3] = '\0'; /* snip the .so */
618 #else
619                         m = lws_snprintf(path, sizeof(path) - 1, "init_%s",
620                                      dent.name + pofs);
621                         path[m - 4] = '\0'; /* snip the .dll */
622 #endif
623                         if (uv_dlsym(&lib, path, &v)) {
624                                 uv_dlerror(&lib);
625                                 lwsl_err("Failed to get %s on %s: %s", path,
626                                                 dent.name, lib.errmsg);
627                                 uv_dlclose(&lib);
628                                 goto bail;
629                         }
630                         initfunc = (lws_plugin_init_func)v;
631                         lcaps.api_magic = LWS_PLUGIN_API_MAGIC;
632                         m = initfunc(context, &lcaps);
633                         if (m) {
634                                 lwsl_err("Initializing %s failed %d\n", dent.name, m);
635                                 goto skip;
636                         }
637
638                         plugin = lws_malloc(sizeof(*plugin));
639                         if (!plugin) {
640                                 uv_dlclose(&lib);
641                                 lwsl_err("OOM\n");
642                                 goto bail;
643                         }
644                         plugin->list = context->plugin_list;
645                         context->plugin_list = plugin;
646                         strncpy(plugin->name, dent.name, sizeof(plugin->name) - 1);
647                         plugin->name[sizeof(plugin->name) - 1] = '\0';
648                         plugin->lib = lib;
649                         plugin->caps = lcaps;
650                         context->plugin_protocol_count += lcaps.count_protocols;
651                         context->plugin_extension_count += lcaps.count_extensions;
652
653                         continue;
654
655 skip:
656                         uv_dlclose(&lib);
657                 }
658 bail:
659                 uv_fs_req_cleanup(&req);
660                 d++;
661         }
662
663         return ret;
664 }
665
666 LWS_VISIBLE int
667 lws_plat_plugins_destroy(struct lws_context *context)
668 {
669         struct lws_plugin *plugin = context->plugin_list, *p;
670         lws_plugin_destroy_func func;
671         char path[256];
672         void *v;
673         int m;
674         int pofs = 0;
675
676 #if  defined(__MINGW32__) || !defined(WIN32)
677         pofs = 3;
678 #endif
679
680         if (!plugin)
681                 return 0;
682
683         // lwsl_notice("%s\n", __func__);
684
685         while (plugin) {
686                 p = plugin;
687
688 #if !defined(WIN32) && !defined(__MINGW32__)
689                 m = lws_snprintf(path, sizeof(path) - 1, "destroy_%s", plugin->name + pofs);
690                 path[m - 3] = '\0';
691 #else
692                 m = lws_snprintf(path, sizeof(path) - 1, "destroy_%s", plugin->name + pofs);
693                 path[m - 4] = '\0';
694 #endif
695
696                 if (uv_dlsym(&plugin->lib, path, &v)) {
697                         uv_dlerror(&plugin->lib);
698                         lwsl_err("Failed to get %s on %s: %s", path,
699                                         plugin->name, plugin->lib.errmsg);
700                 } else {
701                         func = (lws_plugin_destroy_func)v;
702                         m = func(context);
703                         if (m)
704                                 lwsl_err("Destroying %s failed %d\n",
705                                                 plugin->name, m);
706                 }
707
708                 uv_dlclose(&p->lib);
709                 plugin = p->list;
710                 p->list = NULL;
711                 free(p);
712         }
713
714         context->plugin_list = NULL;
715
716         while (uv_loop_close(&context->pu_loop))
717                 ;
718
719         return 0;
720 }
721
722 #endif
723