2 * libwebsockets - small server side websockets and web server implementation
4 * Copyright (C) 2010-2018 Andy Green <andy@warmcat.com>
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.
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.
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,
23 #include "core/private.h"
28 #ifdef LWS_WITH_PLUGINS
33 static int filter(const struct dirent *ent)
35 if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
42 lws_plat_plugins_init(struct lws_context * context, const char * const *d)
44 struct lws_plugin_capability lcaps;
45 struct lws_plugin *plugin;
46 lws_plugin_init_func initfunc;
47 struct dirent **namelist;
52 #if defined(LWS_WITH_PLUGINS) && (UV_VERSION_MAJOR > 0)
53 if (lws_check_opt(context->options, LWS_SERVER_OPTION_LIBUV))
54 return lws_uv_plugins_init(context, d);
57 lwsl_notice(" Plugins:\n");
60 n = scandir(*d, &namelist, filter, alphasort);
62 lwsl_err("Scandir on %s failed\n", *d);
66 for (i = 0; i < n; i++) {
67 if (strlen(namelist[i]->d_name) < 7)
70 lwsl_notice(" %s\n", namelist[i]->d_name);
72 lws_snprintf(path, sizeof(path) - 1, "%s/%s", *d,
74 l = dlopen(path, RTLD_NOW);
76 lwsl_err("Error loading DSO: %s\n", dlerror());
81 /* we could open it, can we get his init function? */
82 m = lws_snprintf(path, sizeof(path) - 1, "init_%s",
83 namelist[i]->d_name + 3 /* snip lib... */);
84 path[m - 3] = '\0'; /* snip the .so */
85 initfunc = dlsym(l, path);
87 lwsl_err("Failed to get init on %s: %s",
88 namelist[i]->d_name, dlerror());
91 lcaps.api_magic = LWS_PLUGIN_API_MAGIC;
92 m = initfunc(context, &lcaps);
94 lwsl_err("Initializing %s failed %d\n",
95 namelist[i]->d_name, m);
100 plugin = lws_malloc(sizeof(*plugin), "plugin");
105 plugin->list = context->plugin_list;
106 context->plugin_list = plugin;
107 lws_strncpy(plugin->name, namelist[i]->d_name,
108 sizeof(plugin->name));
110 plugin->caps = lcaps;
111 context->plugin_protocol_count += lcaps.count_protocols;
112 context->plugin_extension_count += lcaps.count_extensions;
135 lws_plat_plugins_destroy(struct lws_context * context)
137 struct lws_plugin *plugin = context->plugin_list, *p;
138 lws_plugin_destroy_func func;
142 #if defined(LWS_WITH_PLUGINS) && (UV_VERSION_MAJOR > 0)
143 if (lws_check_opt(context->options, LWS_SERVER_OPTION_LIBUV))
144 return lws_uv_plugins_destroy(context);
150 lwsl_notice("%s\n", __func__);
154 m = lws_snprintf(path, sizeof(path) - 1, "destroy_%s",
157 func = dlsym(plugin->l, path);
159 lwsl_err("Failed to get destroy on %s: %s",
160 plugin->name, dlerror());
165 lwsl_err("Initializing %s failed %d\n",
174 context->plugin_list = NULL;