Prepare bindings version 2
[src/app-framework-binder.git] / src / afb-api-so.c
1 /*
2  * Copyright (C) 2016, 2017 "IoT.bzh"
3  * Author José Bollo <jose.bollo@iot.bzh>
4  *
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
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #define _GNU_SOURCE
19
20 #include <stdio.h>
21 #include <dlfcn.h>
22 #include <string.h>
23 #include <dirent.h>
24 #include <errno.h>
25 #include <sys/stat.h>
26
27 #include "afb-api-so.h"
28 #include "afb-api-so-v1.h"
29 #include "afb-api-so-v2.h"
30 #include "verbose.h"
31
32 static int load_binding(const char *path, int force, struct afb_apiset *apiset)
33 {
34         int rc;
35         void *handle;
36
37         // This is a loadable library let's check if it's a binding
38         rc = -!!force;
39         handle = dlopen(path, RTLD_NOW | RTLD_LOCAL);
40         if (handle == NULL) {
41                 if (force)
42                         ERROR("binding [%s] not loadable: %s", path, dlerror());
43                 else
44                         INFO("binding [%s] not loadable: %s", path, dlerror());
45                 goto error;
46         }
47
48         /* retrieves the register function */
49         rc = afb_api_so_v2_add(path, handle, apiset);
50         if (rc < 0) {
51                 /* error when loading a valid v2 binding */
52                 goto error2;
53         }
54         rc = afb_api_so_v1_add(path, handle, apiset);
55         if (rc < 0) {
56                 /* error when loading a valid v1 binding */
57                 goto error2;
58         }
59         if (rc == 0) {
60                 /* not a v1 binding */
61                 if (force)
62                         ERROR("binding [%s] is not an AFB binding", path);
63                 else
64                         INFO("binding [%s] is not an AFB binding", path);
65                 goto error2;
66         }
67         return 0;
68
69 error2:
70         dlclose(handle);
71 error:
72         return rc;
73 }
74
75
76 int afb_api_so_add_binding(const char *path, struct afb_apiset *apiset)
77 {
78         return load_binding(path, 1, apiset);
79 }
80
81 static int adddirs(char path[PATH_MAX], size_t end, struct afb_apiset *apiset)
82 {
83         DIR *dir;
84         struct dirent *dent;
85         size_t len;
86
87         /* open the DIR now */
88         dir = opendir(path);
89         if (dir == NULL) {
90                 ERROR("can't scan binding directory %s, %m", path);
91                 return -1;
92         }
93         INFO("Scanning dir=[%s] for bindings", path);
94
95         /* scan each entry */
96         if (end)
97                 path[end++] = '/';
98         for (;;) {
99                 errno = 0;
100                 dent = readdir(dir);
101                 if (dent == NULL) {
102                         if (errno != 0)
103                                 ERROR("read error while scanning directory %.*s: %m", (int)(end - 1), path);
104                         break;
105                 }
106
107                 len = strlen(dent->d_name);
108                 if (len + end >= PATH_MAX) {
109                         ERROR("path too long while scanning bindings for %s", dent->d_name);
110                         continue;
111                 }
112                 if (dent->d_type == DT_DIR) {
113                         /* case of directories */
114                         if (dent->d_name[0] == '.') {
115                                 if (len == 1)
116                                         continue;
117                                 if (dent->d_name[1] == '.' && len == 2)
118                                         continue;
119                         }
120                         memcpy(&path[end], dent->d_name, len+1);
121                         adddirs(path, end+len, apiset);
122                 } else if (dent->d_type == DT_REG) {
123                         /* case of files */
124                         if (memcmp(&dent->d_name[len - 3], ".so", 4))
125                                 continue;
126                         memcpy(&path[end], dent->d_name, len+1);
127                         if (load_binding(path, 0, apiset) < 0)
128                                 return -1;
129                 }
130         }
131         closedir(dir);
132         return 0;
133 }
134
135 int afb_api_so_add_directory(const char *path, struct afb_apiset *apiset)
136 {
137         size_t length;
138         char buffer[PATH_MAX];
139
140         length = strlen(path);
141         if (length >= sizeof(buffer)) {
142                 ERROR("path too long %lu [%.99s...]", (unsigned long)length, path);
143                 return -1;
144         }
145
146         memcpy(buffer, path, length + 1);
147         return adddirs(buffer, length, apiset);
148 }
149
150 int afb_api_so_add_path(const char *path, struct afb_apiset *apiset)
151 {
152         struct stat st;
153         int rc;
154
155         rc = stat(path, &st);
156         if (rc < 0)
157                 ERROR("Invalid binding path [%s]: %m", path);
158         else if (S_ISDIR(st.st_mode))
159                 rc = afb_api_so_add_directory(path, apiset);
160         else if (strstr(path, ".so"))
161                 rc = load_binding(path, 0, apiset);
162         else
163                 INFO("not a binding [%s], skipped", path);
164         return rc;
165 }
166
167 int afb_api_so_add_pathset(const char *pathset, struct afb_apiset *apiset)
168 {
169         static char sep[] = ":";
170         char *ps, *p;
171
172         ps = strdupa(pathset);
173         for (;;) {
174                 p = strsep(&ps, sep);
175                 if (!p)
176                         return 0;
177                 if (afb_api_so_add_path(p, apiset) < 0)
178                         return -1;
179         }
180 }
181