2 * Copyright (C) 2016, 2017 "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-v1.h"
29 #include "afb-api-so-v2.h"
31 #include "sig-monitor.h"
40 static void safe_dlopen_cb(int sig, void *closure)
42 struct safe_dlopen *sd = closure;
44 sd->handle = dlopen(sd->path, sd->flags);
46 ERROR("dlopen of %s raised signal %s", sd->path, strsignal(sig));
51 static void *safe_dlopen(const char *filename, int flags)
53 struct safe_dlopen sd;
57 sig_monitor(0, safe_dlopen_cb, &sd);
61 static int load_binding(const char *path, int force, struct afb_apiset *apiset)
66 // This is a loadable library let's check if it's a binding
68 handle = safe_dlopen(path, RTLD_NOW | RTLD_LOCAL);
71 ERROR("binding [%s] not loadable: %s", path, dlerror());
73 INFO("binding [%s] not loadable: %s", path, dlerror());
77 /* try the version 2 */
78 rc = afb_api_so_v2_add(path, handle, apiset);
80 /* error when loading a valid v2 binding */
84 return 0; /* yes version 2 */
86 /* try the version 1 */
87 rc = afb_api_so_v1_add(path, handle, apiset);
89 /* error when loading a valid v1 binding */
93 return 0; /* yes version 1 */
95 /* not a valid binding */
97 ERROR("binding [%s] is not an AFB binding", path);
99 INFO("binding [%s] is not an AFB binding", path);
108 int afb_api_so_add_binding(const char *path, struct afb_apiset *apiset)
110 return load_binding(path, 1, apiset);
113 static int adddirs(char path[PATH_MAX], size_t end, struct afb_apiset *apiset, int failstops)
120 /* open the DIR now */
123 ERROR("can't scan binding directory %s, %m", path);
126 INFO("Scanning dir=[%s] for bindings", path);
128 /* scan each entry */
136 ERROR("read error while scanning directory %.*s: %m", (int)(end - 1), path);
140 len = strlen(dent->d_name);
141 if (len + end >= PATH_MAX) {
142 ERROR("path too long while scanning bindings for %s", dent->d_name);
145 if (dent->d_type == DT_DIR) {
146 /* case of directories */
147 if (dent->d_name[0] == '.') {
150 if (dent->d_name[1] == '.' && len == 2)
153 memcpy(&path[end], dent->d_name, len+1);
154 rc = adddirs(path, end+len, apiset, failstops);
155 } else if (dent->d_type == DT_REG) {
157 if (memcmp(&dent->d_name[len - 3], ".so", 4))
159 memcpy(&path[end], dent->d_name, len+1);
160 rc = load_binding(path, 0, apiset);
162 if (rc < 0 && failstops) {
171 int afb_api_so_add_directory(const char *path, struct afb_apiset *apiset, int failstops)
174 char buffer[PATH_MAX];
176 length = strlen(path);
177 if (length >= sizeof(buffer)) {
178 ERROR("path too long %lu [%.99s...]", (unsigned long)length, path);
182 memcpy(buffer, path, length + 1);
183 return adddirs(buffer, length, apiset, failstops);
186 int afb_api_so_add_path(const char *path, struct afb_apiset *apiset, int failstops)
191 rc = stat(path, &st);
193 ERROR("Invalid binding path [%s]: %m", path);
194 else if (S_ISDIR(st.st_mode))
195 rc = afb_api_so_add_directory(path, apiset, failstops);
196 else if (strstr(path, ".so"))
197 rc = load_binding(path, 0, apiset);
199 INFO("not a binding [%s], skipped", path);
203 int afb_api_so_add_pathset(const char *pathset, struct afb_apiset *apiset, int failstops)
205 static char sep[] = ":";
209 ps = strdupa(pathset);
211 p = strsep(&ps, sep);
214 rc = afb_api_so_add_path(p, apiset, failstops);
220 int afb_api_so_add_pathset_fails(const char *pathset, struct afb_apiset *apiset)
222 return afb_api_so_add_pathset(pathset, apiset, 1);
225 int afb_api_so_add_pathset_nofails(const char *pathset, struct afb_apiset *apiset)
227 return afb_api_so_add_pathset(pathset, apiset, 0);