2 * Copyright (C) 2016, 2017, 2018 "IoT.bzh"
3 * Author José Bollo <jose.bollo@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
27 #include "afb-api-so.h"
28 #include "afb-api-so-v2.h"
29 #include "afb-api-so-v3.h"
31 #include "sig-monitor.h"
33 #if defined(WITH_LEGACY_BINDING_V1)
34 # include "afb-api-so-v1.h"
36 #if defined(WITH_LEGACY_BINDING_VDYN)
37 # include "afb-api-so-vdyn.h"
47 static void safe_dlopen_cb(int sig, void *closure)
49 struct safe_dlopen *sd = closure;
51 sd->handle = dlopen(sd->path, sd->flags);
53 ERROR("dlopen of %s raised signal %s", sd->path, strsignal(sig));
58 static void *safe_dlopen(const char *filename, int flags)
60 struct safe_dlopen sd;
64 sig_monitor(0, safe_dlopen_cb, &sd);
68 static int load_binding(const char *path, int force, struct afb_apiset *declare_set, struct afb_apiset * call_set)
74 // This is a loadable library let's check if it's a binding
76 handle = safe_dlopen(path, RTLD_NOW | RTLD_LOCAL | RTLD_DEEPBIND);
79 ERROR("binding [%s] not loadable: %s", path, dlerror());
81 WARNING("binding [%s] not loadable: %s", path, dlerror());
85 /* try the version 3 */
86 rc = afb_api_so_v3_add(path, handle, declare_set, call_set);
88 /* error when loading a valid v3 binding */
92 return 0; /* yes version 3 */
94 /* try the version 2 */
95 rc = afb_api_so_v2_add(path, handle, declare_set, call_set);
97 /* error when loading a valid v2 binding */
101 return 0; /* yes version 2 */
103 #if defined(WITH_LEGACY_BINDING_VDYN)
104 /* try the version dyn */
105 rc = afb_api_so_vdyn_add(path, handle, declare_set, call_set);
107 /* error when loading a valid dyn binding */
111 return 0; /* yes version dyn */
113 if (dlsym(handle, "afbBindingVdyn")) {
114 WARNING("binding [%s]: version DYN not supported", path);
119 #if defined(WITH_LEGACY_BINDING_V1)
120 /* try the version 1 */
121 rc = afb_api_so_v1_add(path, handle, declare_set, call_set);
123 /* error when loading a valid v1 binding */
127 return 0; /* yes version 1 */
129 if (dlsym(handle, "afbBindingV1Register")) {
130 WARNING("binding [%s]: version 1 not supported", path);
135 /* not a valid binding */
136 _VERBOSE_(force ? Log_Level_Error : Log_Level_Info, "binding [%s] %s",
137 path, obsolete ? "is obsolete" : "isn't an AFB binding");
146 int afb_api_so_add_binding(const char *path, struct afb_apiset *declare_set, struct afb_apiset * call_set)
148 return load_binding(path, 1, declare_set, call_set);
151 static int adddirs(char path[PATH_MAX], size_t end, struct afb_apiset *declare_set, struct afb_apiset * call_set, int failstops)
158 /* open the DIR now */
161 ERROR("can't scan binding directory %s, %m", path);
164 INFO("Scanning dir=[%s] for bindings", path);
166 /* scan each entry */
174 ERROR("read error while scanning directory %.*s: %m", (int)(end - 1), path);
178 len = strlen(dent->d_name);
179 if (len + end >= PATH_MAX) {
180 ERROR("path too long while scanning bindings for %s", dent->d_name);
183 if (dent->d_type == DT_DIR) {
184 /* case of directories */
185 if (dent->d_name[0] == '.') {
187 Exclude from the search of bindings any
188 directory starting with a dot (.) by default.
190 It is possible to reactivate the prvious behaviour
191 by defining the following preprocessor variables
193 - AFB_API_SO_ACCEPT_DOT_PREFIXED_DIRS
195 When this variable is defined, the directories
196 starting with a dot are searched except
197 if their name is "." or ".." or ".debug"
199 - AFB_API_SO_ACCEPT_DOT_DEBUG_DIRS
201 When this variable is defined and the variable
202 AFB_API_SO_ACCEPT_DOT_PREFIXED_DIRS is also defined
203 scans any directory not being "." or "..".
205 The previous behaviour was like difining the 2 variables,
206 meaning that only . and .. were excluded from the search.
208 This change is intended to definitely solve the issue
209 SPEC-662. Yocto installed the debugging symbols in the
210 subdirectory .debug. For example the binding.so also
211 had a .debug/binding.so file attached. Opening that
212 debug file made dlopen crashing.
213 See https://sourceware.org/bugzilla/show_bug.cgi?id=22101
215 #if !defined(AFB_API_SO_ACCEPT_DOT_PREFIXED_DIRS) /* not defined by default */
216 continue; /* ignore any directory beginnign with a dot */
220 if (dent->d_name[1] == '.' && len == 2)
222 #if !defined(AFB_API_SO_ACCEPT_DOT_DEBUG_DIRS) /* not defined by default */
224 && dent->d_name[1] == 'd'
225 && dent->d_name[2] == 'e'
226 && dent->d_name[3] == 'b'
227 && dent->d_name[4] == 'u'
228 && dent->d_name[5] == 'g')
229 continue; /* .debug */
233 memcpy(&path[end], dent->d_name, len+1);
234 rc = adddirs(path, end+len, declare_set, call_set, failstops);
235 } else if (dent->d_type == DT_REG || dent->d_type == DT_LNK) {
237 if (memcmp(&dent->d_name[len - 3], ".so", 4))
239 memcpy(&path[end], dent->d_name, len+1);
240 rc = load_binding(path, 0, declare_set, call_set);
242 if (rc < 0 && failstops) {
251 int afb_api_so_add_directory(const char *path, struct afb_apiset *declare_set, struct afb_apiset * call_set, int failstops)
254 char buffer[PATH_MAX];
256 length = strlen(path);
257 if (length >= sizeof(buffer)) {
258 ERROR("path too long %lu [%.99s...]", (unsigned long)length, path);
262 memcpy(buffer, path, length + 1);
263 return adddirs(buffer, length, declare_set, call_set, failstops);
266 int afb_api_so_add_path(const char *path, struct afb_apiset *declare_set, struct afb_apiset * call_set, int failstops)
271 rc = stat(path, &st);
273 ERROR("Invalid binding path [%s]: %m", path);
274 else if (S_ISDIR(st.st_mode))
275 rc = afb_api_so_add_directory(path, declare_set, call_set, failstops);
276 else if (strstr(path, ".so"))
277 rc = load_binding(path, 0, declare_set, call_set);
279 INFO("not a binding [%s], skipped", path);
283 int afb_api_so_add_pathset(const char *pathset, struct afb_apiset *declare_set, struct afb_apiset * call_set, int failstops)
285 static char sep[] = ":";
289 ps = strdupa(pathset);
291 p = strsep(&ps, sep);
294 rc = afb_api_so_add_path(p, declare_set, call_set, failstops);
300 int afb_api_so_add_pathset_fails(const char *pathset, struct afb_apiset *declare_set, struct afb_apiset * call_set)
302 return afb_api_so_add_pathset(pathset, declare_set, call_set, 1);
305 int afb_api_so_add_pathset_nofails(const char *pathset, struct afb_apiset *declare_set, struct afb_apiset * call_set)
307 return afb_api_so_add_pathset(pathset, declare_set, call_set, 0);