1 /*
2 * libwebsockets - small server side websockets and web server implementation
3 *
4 * Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 */
24
25 #if !defined(_GNU_SOURCE)
26 #define _GNU_SOURCE
27 #endif
28 #include "private-lib-core.h"
29
30 #include <pwd.h>
31 #include <grp.h>
32
33 #ifdef LWS_WITH_PLUGINS
34 #include <dlfcn.h>
35 #endif
36 #include <dirent.h>
37
filter(const struct dirent * ent)38 static int filter(const struct dirent *ent)
39 {
40 if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
41 return 0;
42
43 return 1;
44 }
45
46 int
lws_plat_plugins_init(struct lws_context * context,const char * const * d)47 lws_plat_plugins_init(struct lws_context * context, const char * const *d)
48 {
49 struct lws_plugin_capability lcaps;
50 struct lws_plugin *plugin;
51 lws_plugin_init_func initfunc;
52 struct dirent **namelist;
53 int n, i, m, ret = 0;
54 char path[256];
55 void *l;
56
57 #if defined(LWS_WITH_PLUGINS) && (UV_VERSION_MAJOR > 0)
58 if (lws_check_opt(context->options, LWS_SERVER_OPTION_LIBUV))
59 return lws_uv_plugins_init(context, d);
60 #endif
61
62 lwsl_notice(" Plugins:\n");
63
64 while (d && *d) {
65 n = scandir(*d, &namelist, filter, alphasort);
66 if (n < 0) {
67 lwsl_err("Scandir on %s failed\n", *d);
68 return 1;
69 }
70
71 for (i = 0; i < n; i++) {
72 if (strlen(namelist[i]->d_name) < 7)
73 goto inval;
74
75 lwsl_notice(" %s\n", namelist[i]->d_name);
76
77 lws_snprintf(path, sizeof(path) - 1, "%s/%s", *d,
78 namelist[i]->d_name);
79 l = dlopen(path, RTLD_NOW);
80 if (!l) {
81 lwsl_err("Error loading DSO: %s\n", dlerror());
82 while (i++ < n)
83 free(namelist[i]);
84 goto bail;
85 }
86 /* we could open it, can we get his init function? */
87 m = lws_snprintf(path, sizeof(path) - 1, "init_%s",
88 namelist[i]->d_name + 3 /* snip lib... */);
89 path[m - 3] = '\0'; /* snip the .so */
90 initfunc = dlsym(l, path);
91 if (!initfunc) {
92 lwsl_err("%s: Failed to get init '%s' on %s: %s\n",
93 __func__, path, namelist[i]->d_name, dlerror());
94 goto skip;
95 }
96 lcaps.api_magic = LWS_PLUGIN_API_MAGIC;
97 m = initfunc(context, &lcaps);
98 if (m) {
99 lwsl_err("Initializing %s failed %d\n",
100 namelist[i]->d_name, m);
101 goto skip;
102 }
103
104 plugin = lws_malloc(sizeof(*plugin), "plugin");
105 if (!plugin) {
106 dlclose(l);
107 lwsl_err("OOM\n");
108 goto bail;
109 }
110 plugin->list = context->plugin_list;
111 context->plugin_list = plugin;
112 lws_strncpy(plugin->name, namelist[i]->d_name,
113 sizeof(plugin->name));
114 plugin->l = l;
115 plugin->caps = lcaps;
116 context->plugin_protocol_count += lcaps.count_protocols;
117 context->plugin_extension_count += lcaps.count_extensions;
118
119 free(namelist[i]);
120 continue;
121
122 skip:
123 dlclose(l);
124 inval:
125 free(namelist[i]);
126 }
127 free(namelist);
128 d++;
129 }
130
131 return 0;
132
133 bail:
134 free(namelist);
135
136 return ret;
137 }
138
139 int
lws_plat_plugins_destroy(struct lws_context * context)140 lws_plat_plugins_destroy(struct lws_context * context)
141 {
142 struct lws_plugin *plugin = context->plugin_list, *p;
143 lws_plugin_destroy_func func;
144 char path[256];
145 int m;
146
147 #if defined(LWS_WITH_PLUGINS) && (UV_VERSION_MAJOR > 0)
148 if (lws_check_opt(context->options, LWS_SERVER_OPTION_LIBUV))
149 return lws_uv_plugins_destroy(context);
150 #endif
151
152 if (!plugin)
153 return 0;
154
155 lwsl_notice("%s\n", __func__);
156
157 while (plugin) {
158 p = plugin;
159 m = lws_snprintf(path, sizeof(path) - 1, "destroy_%s",
160 plugin->name + 3);
161 path[m - 3] = '\0';
162 func = dlsym(plugin->l, path);
163 if (!func) {
164 lwsl_err("Failed to get destroy on %s: %s",
165 plugin->name, dlerror());
166 goto next;
167 }
168 m = func(context);
169 if (m)
170 lwsl_err("Initializing %s failed %d\n",
171 plugin->name, m);
172 next:
173 dlclose(p->l);
174 plugin = p->list;
175 p->list = NULL;
176 free(p);
177 }
178
179 context->plugin_list = NULL;
180
181 return 0;
182 }
183