afb-api-so: take care of SIGSEGV in dlopen
[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 #include "sig-monitor.h"
32
33 struct safe_dlopen
34 {
35         const char *path;
36         void *handle;
37         int flags;
38 };
39
40 static void safe_dlopen_cb(int sig, void *closure)
41 {
42         struct safe_dlopen *sd = closure;
43         if (!sig)
44                 sd->handle = dlopen(sd->path, sd->flags);
45         else {
46                 ERROR("dlopen of %s raised signal %s", sd->path, strsignal(sig));
47                 sd->handle = NULL;
48         }
49 }
50
51 static void *safe_dlopen(const char *filename, int flags)
52 {
53         struct safe_dlopen sd;
54         sd.path = filename;
55         sd.flags = flags;
56         sd.handle = NULL;
57         sig_monitor(0, safe_dlopen_cb, &sd);
58         return sd.handle;
59 }
60
61 static int load_binding(const char *path, int force, struct afb_apiset *apiset)
62 {
63         int rc;
64         void *handle;
65
66         // This is a loadable library let's check if it's a binding
67         rc = -!!force;
68         handle = safe_dlopen(path, RTLD_NOW | RTLD_LOCAL);
69         if (handle == NULL) {
70                 if (force)
71                         ERROR("binding [%s] not loadable: %s", path, dlerror());
72                 else
73                         INFO("binding [%s] not loadable: %s", path, dlerror());
74                 goto error;
75         }
76
77         /* try the version 2 */
78         rc = afb_api_so_v2_add(path, handle, apiset);
79         if (rc < 0) {
80                 /* error when loading a valid v2 binding */
81                 goto error2;
82         }
83         if (rc)
84                 return 0; /* yes version 2 */
85
86         /* try the version 1 */
87         rc = afb_api_so_v1_add(path, handle, apiset);
88         if (rc < 0) {
89                 /* error when loading a valid v1 binding */
90                 goto error2;
91         }
92         if (rc)
93                 return 0; /* yes version 1 */
94
95         /* not a valid binding */
96         if (force)
97                 ERROR("binding [%s] is not an AFB binding", path);
98         else
99                 INFO("binding [%s] is not an AFB binding", path);
100
101 error2:
102         dlclose(handle);
103 error:
104         return rc;
105 }
106
107
108 int afb_api_so_add_binding(const char *path, struct afb_apiset *apiset)
109 {
110         return load_binding(path, 1, apiset);
111 }
112
113 static int adddirs(char path[PATH_MAX], size_t end, struct afb_apiset *apiset)
114 {
115         DIR *dir;
116         struct dirent *dent;
117         size_t len;
118
119         /* open the DIR now */
120         dir = opendir(path);
121         if (dir == NULL) {
122                 ERROR("can't scan binding directory %s, %m", path);
123                 return -1;
124         }
125         INFO("Scanning dir=[%s] for bindings", path);
126
127         /* scan each entry */
128         if (end)
129                 path[end++] = '/';
130         for (;;) {
131                 errno = 0;
132                 dent = readdir(dir);
133                 if (dent == NULL) {
134                         if (errno != 0)
135                                 ERROR("read error while scanning directory %.*s: %m", (int)(end - 1), path);
136                         break;
137                 }
138
139                 len = strlen(dent->d_name);
140                 if (len + end >= PATH_MAX) {
141                         ERROR("path too long while scanning bindings for %s", dent->d_name);
142                         continue;
143                 }
144                 if (dent->d_type == DT_DIR) {
145                         /* case of directories */
146                         if (dent->d_name[0] == '.') {
147                                 if (len == 1)
148                                         continue; /* . */
149                                 if (dent->d_name[1] == '.' && len == 2)
150                                         continue; /* .. */
151                         }
152                         memcpy(&path[end], dent->d_name, len+1);
153                         adddirs(path, end+len, apiset);
154                 } else if (dent->d_type == DT_REG) {
155                         /* case of files */
156                         if (memcmp(&dent->d_name[len - 3], ".so", 4))
157                                 continue;
158                         memcpy(&path[end], dent->d_name, len+1);
159                         if (load_binding(path, 0, apiset) < 0)
160                                 return -1;
161                 }
162         }
163         closedir(dir);
164         return 0;
165 }
166
167 int afb_api_so_add_directory(const char *path, struct afb_apiset *apiset)
168 {
169         size_t length;
170         char buffer[PATH_MAX];
171
172         length = strlen(path);
173         if (length >= sizeof(buffer)) {
174                 ERROR("path too long %lu [%.99s...]", (unsigned long)length, path);
175                 return -1;
176         }
177
178         memcpy(buffer, path, length + 1);
179         return adddirs(buffer, length, apiset);
180 }
181
182 int afb_api_so_add_path(const char *path, struct afb_apiset *apiset)
183 {
184         struct stat st;
185         int rc;
186
187         rc = stat(path, &st);
188         if (rc < 0)
189                 ERROR("Invalid binding path [%s]: %m", path);
190         else if (S_ISDIR(st.st_mode))
191                 rc = afb_api_so_add_directory(path, apiset);
192         else if (strstr(path, ".so"))
193                 rc = load_binding(path, 0, apiset);
194         else
195                 INFO("not a binding [%s], skipped", path);
196         return rc;
197 }
198
199 int afb_api_so_add_pathset(const char *pathset, struct afb_apiset *apiset)
200 {
201         static char sep[] = ":";
202         char *ps, *p;
203
204         ps = strdupa(pathset);
205         for (;;) {
206                 p = strsep(&ps, sep);
207                 if (!p)
208                         return 0;
209                 if (afb_api_so_add_path(p, apiset) < 0)
210                         return -1;
211         }
212 }
213