lws-vhost-destroy
[platform/upstream/libwebsockets.git] / lib / libuv.c
index 4e80936..7d305d7 100644 (file)
@@ -40,7 +40,7 @@ lws_uv_idle(uv_idle_t *handle
        struct lws_context_per_thread *pt = lws_container_of(handle,
                                        struct lws_context_per_thread, uv_idle);
 
-       lwsl_debug("%s\n", __func__);
+//     lwsl_debug("%s\n", __func__);
 
        /*
         * is there anybody with pending stuff that needs service forcing?
@@ -51,14 +51,14 @@ lws_uv_idle(uv_idle_t *handle
                /* still somebody left who wants forced service? */
                if (!lws_service_adjust_timeout(pt->context, 1, pt->tid))
                        /* yes... come back again later */
-                       lwsl_debug("%s: done again\n", __func__);
+//                     lwsl_debug("%s: done again\n", __func__);
                return;
        }
 
        /* there is nobody who needs service forcing, shut down idle */
        uv_idle_stop(handle);
 
-       lwsl_debug("%s: done stop\n", __func__);
+       //lwsl_debug("%s: done stop\n", __func__);
 }
 
 static void
@@ -67,7 +67,7 @@ lws_io_cb(uv_poll_t *watcher, int status, int revents)
        struct lws_io_watcher *lws_io = lws_container_of(watcher,
                                        struct lws_io_watcher, uv_watcher);
        struct lws *wsi = lws_container_of(lws_io, struct lws, w_read);
-       struct lws_context *context = lws_io->context;
+       struct lws_context *context = wsi->context;
        struct lws_pollfd eventfd;
 
 #if defined(WIN32) || defined(_WIN32)
@@ -141,7 +141,7 @@ lws_uv_timeout_cb(uv_timer_t *timer
        lws_service_fd_tsi(pt->context, NULL, pt->tid);
 }
 
-static const int sigs[] = { SIGINT, SIGTERM, SIGSEGV, SIGFPE };
+static const int sigs[] = { SIGINT, SIGTERM, SIGSEGV, SIGFPE, SIGHUP };
 
 int
 lws_uv_initvhost(struct lws_vhost* vh, struct lws* wsi)
@@ -164,10 +164,10 @@ lws_uv_initvhost(struct lws_vhost* vh, struct lws* wsi)
 
        wsi->w_read.context = vh->context;
        n = uv_poll_init_socket(pt->io_loop_uv,
-                               &wsi->w_read.uv_watcher, wsi->sock);
+                               &wsi->w_read.uv_watcher, wsi->desc.sockfd);
        if (n) {
                lwsl_err("uv_poll_init failed %d, sockfd=%p\n",
-                                n, (void *)(long)wsi->sock);
+                                n, (void *)(lws_intptr_t)wsi->desc.sockfd);
 
                return -1;
        }
@@ -258,13 +258,21 @@ static void lws_uv_close_cb(uv_handle_t *handle)
 
 static void lws_uv_walk_cb(uv_handle_t *handle, void *arg)
 {
-       uv_close(handle, lws_uv_close_cb);
+       if (!uv_is_closing(handle))
+               uv_close(handle, lws_uv_close_cb);
+}
+
+LWS_VISIBLE void
+lws_close_all_handles_in_loop(uv_loop_t *loop)
+{
+       uv_walk(loop, lws_uv_walk_cb, NULL);
 }
 
 void
 lws_libuv_destroyloop(struct lws_context *context, int tsi)
 {
        struct lws_context_per_thread *pt = &context->pt[tsi];
+//     struct lws_context *ctx;
        int m, budget = 100, ns;
 
        if (!lws_check_opt(context->options, LWS_SERVER_OPTION_LIBUV))
@@ -273,6 +281,8 @@ lws_libuv_destroyloop(struct lws_context *context, int tsi)
        if (!pt->io_loop_uv)
                return;
 
+       lwsl_notice("%s: closing signals + timers context %p\n", __func__, context);
+
        if (context->use_ev_sigint) {
                uv_signal_stop(&pt->w_sigint.uv_watcher);
 
@@ -292,11 +302,13 @@ lws_libuv_destroyloop(struct lws_context *context, int tsi)
        uv_idle_stop(&pt->uv_idle);
        uv_close((uv_handle_t *)&pt->uv_idle, lws_uv_close_cb);
 
+       if (pt->ev_loop_foreign)
+               return;
+
        while (budget-- && uv_run(pt->io_loop_uv, UV_RUN_NOWAIT))
                ;
 
-       if (pt->ev_loop_foreign)
-               return;
+       lwsl_notice("%s: closing all loop handles context %p\n", __func__, context);
 
        uv_stop(pt->io_loop_uv);
 
@@ -313,7 +325,7 @@ lws_libuv_destroyloop(struct lws_context *context, int tsi)
 }
 
 void
-lws_libuv_accept(struct lws *wsi, lws_sockfd_type accept_fd)
+lws_libuv_accept(struct lws *wsi, lws_sock_file_fd_type desc)
 {
        struct lws_context *context = lws_get_context(wsi);
        struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
@@ -324,8 +336,12 @@ lws_libuv_accept(struct lws *wsi, lws_sockfd_type accept_fd)
        lwsl_debug("%s: new wsi %p\n", __func__, wsi);
 
        wsi->w_read.context = context;
-
-       uv_poll_init_socket(pt->io_loop_uv, &wsi->w_read.uv_watcher, accept_fd);
+       if (wsi->mode == LWSCM_RAW_FILEDESC)
+               uv_poll_init(pt->io_loop_uv, &wsi->w_read.uv_watcher,
+                            (int)desc.filefd);
+       else
+               uv_poll_init_socket(pt->io_loop_uv, &wsi->w_read.uv_watcher,
+                                   desc.sockfd);
 }
 
 void
@@ -347,7 +363,9 @@ lws_libuv_io(struct lws *wsi, int flags)
 
        // lwsl_notice("%s: wsi: %p, flags:0x%x\n", __func__, wsi, flags);
 
-       if (!pt->io_loop_uv) {
+       // w->context is set after the loop is initialized
+
+       if (!pt->io_loop_uv || !w->context) {
                lwsl_info("%s: no io loop yet\n", __func__);
                return;
        }
@@ -402,15 +420,24 @@ lws_libuv_run(const struct lws_context *context, int tsi)
                uv_run(context->pt[tsi].io_loop_uv, 0);
 }
 
+LWS_VISIBLE void
+lws_libuv_stop_without_kill(const struct lws_context *context, int tsi)
+{
+       if (context->pt[tsi].io_loop_uv && LWS_LIBUV_ENABLED(context))
+               uv_stop(context->pt[tsi].io_loop_uv);
+}
+
 static void
 lws_libuv_kill(const struct lws_context *context)
 {
        int n;
 
+       lwsl_notice("%s\n", __func__);
+
        for (n = 0; n < context->count_threads; n++)
                if (context->pt[n].io_loop_uv &&
-                   LWS_LIBUV_ENABLED(context) &&
-                   !context->pt[n].ev_loop_foreign)
+                   LWS_LIBUV_ENABLED(context) )//&&
+                   //!context->pt[n].ev_loop_foreign)
                        uv_stop(context->pt[n].io_loop_uv);
 }
 
@@ -470,9 +497,25 @@ lws_libuv_closewsi(uv_handle_t* handle)
        struct lws *n = NULL, *wsi = (struct lws *)(((char *)handle) -
                          (char *)(&n->w_read.uv_watcher));
        struct lws_context *context = lws_get_context(wsi);
+       int lspd = 0;
+
+       if (wsi->mode == LWSCM_SERVER_LISTENER &&
+           wsi->context->deprecated) {
+               lspd = 1;
+               context->deprecation_pending_listen_close_count--;
+               if (!context->deprecation_pending_listen_close_count)
+                       lspd = 2;
+       }
 
        lws_close_free_wsi_final(wsi);
 
+       if (lspd == 2 && context->deprecation_cb) {
+               lwsl_notice("calling deprecation callback\n");
+               context->deprecation_cb();
+       }
+
+       //lwsl_notice("%s: ctx %p: wsi left %d\n", __func__, context, context->count_wsi_allocated);
+
        if (context->requested_kill && context->count_wsi_allocated == 0)
                lws_libuv_kill(context);
 }
@@ -483,17 +526,43 @@ lws_libuv_closehandle(struct lws *wsi)
        struct lws_context *context = lws_get_context(wsi);
 
        /* required to defer actual deletion until libuv has processed it */
-
        uv_close((uv_handle_t*)&wsi->w_read.uv_watcher, lws_libuv_closewsi);
 
        if (context->requested_kill && context->count_wsi_allocated == 0)
                lws_libuv_kill(context);
 }
 
+static void
+lws_libuv_closewsi_m(uv_handle_t* handle)
+{
+       lws_sockfd_type sockfd = (lws_sockfd_type)(lws_intptr_t)handle->data;
+
+       compatible_close(sockfd);
+}
+
+void
+lws_libuv_closehandle_manually(struct lws *wsi)
+{
+       uv_handle_t *h = (void *)&wsi->w_read.uv_watcher;
+
+       h->data = (void *)(lws_intptr_t)wsi->desc.sockfd;
+       /* required to defer actual deletion until libuv has processed it */
+       uv_close((uv_handle_t*)&wsi->w_read.uv_watcher, lws_libuv_closewsi_m);
+}
+
+int
+lws_libuv_check_watcher_active(struct lws *wsi)
+{
+       uv_handle_t *h = (void *)&wsi->w_read.uv_watcher;
+
+       return uv_is_active(h);
+}
+
+
 #if defined(LWS_WITH_PLUGINS) && (UV_VERSION_MAJOR > 0)
 
 LWS_VISIBLE int
-lws_plat_plugins_init(struct lws_context * context, const char * const *d)
+lws_plat_plugins_init(struct lws_context *context, const char * const *d)
 {
        struct lws_plugin_capability lcaps;
        struct lws_plugin *plugin;
@@ -503,20 +572,24 @@ lws_plat_plugins_init(struct lws_context * context, const char * const *d)
        uv_dirent_t dent;
        uv_fs_t req;
        char path[256];
-       uv_loop_t loop;
        uv_lib_t lib;
+       int pofs = 0;
+
+#if  defined(__MINGW32__) || !defined(WIN32)
+       pofs = 3;
+#endif
 
        lib.errmsg = NULL;
        lib.handle = NULL;
 
-       uv_loop_init(&loop);
+       uv_loop_init(&context->pu_loop);
 
        lwsl_notice("  Plugins:\n");
 
        while (d && *d) {
 
                lwsl_notice("  Scanning %s\n", *d);
-               m =uv_fs_scandir(&loop, &req, *d, 0, NULL);
+               m =uv_fs_scandir(&context->pu_loop, &req, *d, 0, NULL);
                if (m < 1) {
                        lwsl_err("Scandir on %s failed\n", *d);
                        return 1;
@@ -532,22 +605,26 @@ lws_plat_plugins_init(struct lws_context * context, const char * const *d)
                        if (uv_dlopen(path, &lib)) {
                                uv_dlerror(&lib);
                                lwsl_err("Error loading DSO: %s\n", lib.errmsg);
+                               uv_dlclose(&lib);
                                goto bail;
                        }
+
                        /* we could open it, can we get his init function? */
-#if !defined(WIN32)
+
+#if !defined(WIN32) && !defined(__MINGW32__)
                        m = lws_snprintf(path, sizeof(path) - 1, "init_%s",
-                                    dent.name + 3 /* snip lib... */);
+                                    dent.name + pofs /* snip lib... */);
                        path[m - 3] = '\0'; /* snip the .so */
 #else
                        m = lws_snprintf(path, sizeof(path) - 1, "init_%s",
-                                    dent.name);
+                                    dent.name + pofs);
                        path[m - 4] = '\0'; /* snip the .dll */
 #endif
                        if (uv_dlsym(&lib, path, &v)) {
                                uv_dlerror(&lib);
-                               lwsl_err("Failed to get init on %s: %s",
+                               lwsl_err("Failed to get %s on %s: %s", path,
                                                dent.name, lib.errmsg);
+                               uv_dlclose(&lib);
                                goto bail;
                        }
                        initfunc = (lws_plugin_init_func)v;
@@ -560,6 +637,7 @@ lws_plat_plugins_init(struct lws_context * context, const char * const *d)
 
                        plugin = lws_malloc(sizeof(*plugin));
                        if (!plugin) {
+                               uv_dlclose(&lib);
                                lwsl_err("OOM\n");
                                goto bail;
                        }
@@ -582,20 +660,22 @@ bail:
                d++;
        }
 
-       uv_loop_close(&loop);
-
        return ret;
-
 }
 
 LWS_VISIBLE int
-lws_plat_plugins_destroy(struct lws_context * context)
+lws_plat_plugins_destroy(struct lws_context *context)
 {
        struct lws_plugin *plugin = context->plugin_list, *p;
        lws_plugin_destroy_func func;
        char path[256];
        void *v;
        int m;
+       int pofs = 0;
+
+#if  defined(__MINGW32__) || !defined(WIN32)
+       pofs = 3;
+#endif
 
        if (!plugin)
                return 0;
@@ -604,17 +684,18 @@ lws_plat_plugins_destroy(struct lws_context * context)
 
        while (plugin) {
                p = plugin;
-#if !defined(WIN32)
-               m = lws_snprintf(path, sizeof(path) - 1, "destroy_%s", plugin->name + 3);
+
+#if !defined(WIN32) && !defined(__MINGW32__)
+               m = lws_snprintf(path, sizeof(path) - 1, "destroy_%s", plugin->name + pofs);
                path[m - 3] = '\0';
 #else
-               m = lws_snprintf(path, sizeof(path) - 1, "destroy_%s", plugin->name);
+               m = lws_snprintf(path, sizeof(path) - 1, "destroy_%s", plugin->name + pofs);
                path[m - 4] = '\0';
 #endif
 
                if (uv_dlsym(&plugin->lib, path, &v)) {
                        uv_dlerror(&plugin->lib);
-                       lwsl_err("Failed to get init on %s: %s",
+                       lwsl_err("Failed to get %s on %s: %s", path,
                                        plugin->name, plugin->lib.errmsg);
                } else {
                        func = (lws_plugin_destroy_func)v;
@@ -632,6 +713,9 @@ lws_plat_plugins_destroy(struct lws_context * context)
 
        context->plugin_list = NULL;
 
+       while (uv_loop_close(&context->pu_loop))
+               ;
+
        return 0;
 }