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"
32 static int load_binding(const char *path, int force, struct afb_apiset *apiset)
37 // This is a loadable library let's check if it's a binding
39 handle = dlopen(path, RTLD_NOW | RTLD_LOCAL);
42 ERROR("binding [%s] not loadable: %s", path, dlerror());
44 INFO("binding [%s] not loadable: %s", path, dlerror());
48 /* try the version 2 */
49 rc = afb_api_so_v2_add(path, handle, apiset);
51 /* error when loading a valid v2 binding */
55 return 0; /* yes version 2 */
57 /* try the version 1 */
58 rc = afb_api_so_v1_add(path, handle, apiset);
60 /* error when loading a valid v1 binding */
64 return 0; /* yes version 1 */
66 /* not a valid binding */
68 ERROR("binding [%s] is not an AFB binding", path);
70 INFO("binding [%s] is not an AFB binding", path);
79 int afb_api_so_add_binding(const char *path, struct afb_apiset *apiset)
81 return load_binding(path, 1, apiset);
84 static int adddirs(char path[PATH_MAX], size_t end, struct afb_apiset *apiset)
90 /* open the DIR now */
93 ERROR("can't scan binding directory %s, %m", path);
96 INFO("Scanning dir=[%s] for bindings", path);
106 ERROR("read error while scanning directory %.*s: %m", (int)(end - 1), path);
110 len = strlen(dent->d_name);
111 if (len + end >= PATH_MAX) {
112 ERROR("path too long while scanning bindings for %s", dent->d_name);
115 if (dent->d_type == DT_DIR) {
116 /* case of directories */
117 if (dent->d_name[0] == '.') {
120 if (dent->d_name[1] == '.' && len == 2)
123 memcpy(&path[end], dent->d_name, len+1);
124 adddirs(path, end+len, apiset);
125 } else if (dent->d_type == DT_REG) {
127 if (memcmp(&dent->d_name[len - 3], ".so", 4))
129 memcpy(&path[end], dent->d_name, len+1);
130 if (load_binding(path, 0, apiset) < 0)
138 int afb_api_so_add_directory(const char *path, struct afb_apiset *apiset)
141 char buffer[PATH_MAX];
143 length = strlen(path);
144 if (length >= sizeof(buffer)) {
145 ERROR("path too long %lu [%.99s...]", (unsigned long)length, path);
149 memcpy(buffer, path, length + 1);
150 return adddirs(buffer, length, apiset);
153 int afb_api_so_add_path(const char *path, struct afb_apiset *apiset)
158 rc = stat(path, &st);
160 ERROR("Invalid binding path [%s]: %m", path);
161 else if (S_ISDIR(st.st_mode))
162 rc = afb_api_so_add_directory(path, apiset);
163 else if (strstr(path, ".so"))
164 rc = load_binding(path, 0, apiset);
166 INFO("not a binding [%s], skipped", path);
170 int afb_api_so_add_pathset(const char *pathset, struct afb_apiset *apiset)
172 static char sep[] = ":";
175 ps = strdupa(pathset);
177 p = strsep(&ps, sep);
180 if (afb_api_so_add_path(p, apiset) < 0)