1 /*
2 * libwebsockets - small server side websockets and web server implementation
3 *
4 * Copyright (C) 2010 - 2020 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 #include <dlfcn.h>
33
34 const lws_plugin_header_t *
lws_plat_dlopen(struct lws_plugin ** pplugin,const char * libpath,const char * sofilename,const char * _class,each_plugin_cb_t each,void * each_user)35 lws_plat_dlopen(struct lws_plugin **pplugin, const char *libpath,
36 const char *sofilename, const char *_class,
37 each_plugin_cb_t each, void *each_user)
38 {
39 const lws_plugin_header_t *hdr;
40 struct lws_plugin *pin;
41 char sym[96];
42 void *l;
43 int m;
44
45 if (strlen(sofilename) < 6)
46 /* [lib]...[.so] */
47 return NULL;
48
49 lwsl_info(" trying %s\n", libpath);
50
51 l = dlopen(libpath, RTLD_NOW);
52 if (!l) {
53 lwsl_info("%s: Error loading DSO: %s\n", __func__, dlerror());
54
55 return NULL;
56 }
57
58 /* we could open it... can we get his export struct? */
59 m = lws_snprintf(sym, sizeof(sym) - 1, "%s", sofilename);
60 if (m < 4)
61 goto bail;
62 if (!strcmp(&sym[m - 3], ".so"))
63 sym[m - 3] = '\0'; /* snip the .so */
64
65 hdr = (const lws_plugin_header_t *)dlsym(l, sym);
66 if (!hdr) {
67 lwsl_info("%s: Failed to get export '%s' from %s: %s\n",
68 __func__, sym, libpath, dlerror());
69 goto bail;
70 }
71
72 if (hdr->api_magic != LWS_PLUGIN_API_MAGIC) {
73 lwsl_info("%s: plugin %s has outdated api %d (vs %d)\n",
74 __func__, libpath, hdr->api_magic,
75 LWS_PLUGIN_API_MAGIC);
76 goto bail;
77 }
78
79 if (strcmp(hdr->lws_build_hash, LWS_BUILD_HASH))
80 goto bail;
81
82 if (strcmp(hdr->_class, _class))
83 goto bail;
84
85 /*
86 * We don't already have one of these, right?
87 */
88
89 pin = *pplugin;
90 while (pin) {
91 if (!strcmp(pin->hdr->name, hdr->name))
92 goto bail;
93 pin = pin->list;
94 }
95
96 /*
97 * OK let's bring it in
98 */
99
100 pin = lws_malloc(sizeof(*pin), __func__);
101 if (!pin)
102 goto bail;
103
104 pin->list = *pplugin;
105 *pplugin = pin;
106
107 pin->u.l = l;
108 pin->hdr = hdr;
109
110 if (each)
111 each(pin, each_user);
112
113 lwsl_notice(" %s\n", libpath);
114
115 return hdr;
116
117 bail:
118 dlclose(l);
119
120 return NULL;
121 }
122
123 int
lws_plat_destroy_dl(struct lws_plugin * p)124 lws_plat_destroy_dl(struct lws_plugin *p)
125 {
126 return dlclose(p->u.l);
127 }
128