Commit | Line | Data |
---|---|---|
f3e4505b JG |
1 | /* |
2 | * component-factory.c | |
3 | * | |
4 | * Babeltrace Plugin Component Factory | |
5 | * | |
6 | * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
7 | * | |
8 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
9 | * | |
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
11 | * of this software and associated documentation files (the "Software"), to deal | |
12 | * in the Software without restriction, including without limitation the rights | |
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
14 | * copies of the Software, and to permit persons to whom the Software is | |
15 | * furnished to do so, subject to the following conditions: | |
16 | * | |
17 | * The above copyright notice and this permission notice shall be included in | |
18 | * all copies or substantial portions of the Software. | |
19 | * | |
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
26 | * SOFTWARE. | |
27 | */ | |
28 | ||
29 | #include <babeltrace/plugin/component-factory.h> | |
30 | #include <babeltrace/plugin/component-factory-internal.h> | |
31 | #include <babeltrace/babeltrace-internal.h> | |
32 | #include <babeltrace/compiler.h> | |
b8a06801 | 33 | #include <babeltrace/ref.h> |
f3e4505b JG |
34 | #include <unistd.h> |
35 | #include <stdlib.h> | |
36 | #include <sys/stat.h> | |
fb2dcc52 | 37 | #include <gmodule.h> |
f3e4505b JG |
38 | |
39 | #define NATIVE_PLUGIN_SUFFIX ".so" | |
40 | #define NATIVE_PLUGIN_SUFFIX_LEN sizeof(NATIVE_PLUGIN_SUFFIX) | |
41 | #define LIBTOOL_PLUGIN_SUFFIX ".la" | |
42 | #define LIBTOOL_PLUGIN_SUFFIX_LEN sizeof(LIBTOOL_PLUGIN_SUFFIX) | |
73299554 JG |
43 | #define PLUGIN_SUFFIX_LEN max_t(size_t, sizeof(NATIVE_PLUGIN_SUFFIX), \ |
44 | sizeof(LIBTOOL_PLUGIN_SUFFIX)) | |
f3e4505b | 45 | |
f3e4505b JG |
46 | /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */ |
47 | static | |
48 | struct dirent *alloc_dirent(const char *path) | |
49 | { | |
50 | size_t len; | |
51 | long name_max; | |
52 | struct dirent *entry; | |
53 | ||
54 | name_max = pathconf(path, _PC_NAME_MAX); | |
55 | if (name_max == -1) { | |
56 | name_max = PATH_MAX; | |
57 | } | |
58 | len = offsetof(struct dirent, d_name) + name_max + 1; | |
59 | entry = zmalloc(len); | |
60 | return entry; | |
61 | } | |
62 | ||
63 | static | |
64 | enum bt_component_factory_status | |
65 | bt_component_factory_load_file(struct bt_component_factory *factory, | |
66 | const char *path) | |
67 | { | |
68 | enum bt_component_factory_status ret = BT_COMPONENT_FACTORY_STATUS_OK; | |
fb2dcc52 | 69 | enum bt_component_status component_status; |
f3e4505b JG |
70 | size_t path_len; |
71 | GModule *module; | |
fb2dcc52 | 72 | struct bt_plugin *plugin; |
f3e4505b JG |
73 | |
74 | if (!factory || !path) { | |
75 | ret = BT_COMPONENT_FACTORY_STATUS_INVAL; | |
76 | goto end; | |
77 | } | |
78 | ||
79 | path_len = strlen(path); | |
80 | if (path_len <= PLUGIN_SUFFIX_LEN) { | |
73299554 | 81 | ret = BT_COMPONENT_FACTORY_STATUS_INVAL; |
f3e4505b JG |
82 | goto end; |
83 | } | |
84 | ||
85 | path_len++; | |
86 | /* | |
87 | * Check if the file ends with a known plugin file type suffix (i.e. .so | |
88 | * or .la on Linux). | |
89 | */ | |
90 | if (strncmp(NATIVE_PLUGIN_SUFFIX, | |
b8a06801 JG |
91 | path + path_len - NATIVE_PLUGIN_SUFFIX_LEN, |
92 | NATIVE_PLUGIN_SUFFIX_LEN) && | |
93 | strncmp(LIBTOOL_PLUGIN_SUFFIX, | |
94 | path + path_len - LIBTOOL_PLUGIN_SUFFIX_LEN, | |
95 | LIBTOOL_PLUGIN_SUFFIX_LEN)) { | |
73299554 JG |
96 | /* Name indicates that this is not a plugin file. */ |
97 | ret = BT_COMPONENT_FACTORY_STATUS_INVAL; | |
f3e4505b JG |
98 | goto end; |
99 | } | |
100 | ||
101 | module = g_module_open(path, 0); | |
102 | if (!module) { | |
103 | printf_error("Module open error: %s", g_module_error()); | |
73299554 | 104 | ret = BT_COMPONENT_FACTORY_STATUS_ERROR; |
f3e4505b JG |
105 | goto end; |
106 | } | |
107 | ||
fb2dcc52 JG |
108 | /* Load plugin and make sure it defines the required entry points */ |
109 | plugin = bt_plugin_create(module); | |
73299554 | 110 | if (!plugin) { |
fb2dcc52 | 111 | ret = BT_COMPONENT_FACTORY_STATUS_INVAL_PLUGIN; |
73299554 JG |
112 | if (!g_module_close(module)) { |
113 | printf_error("Module close error: %s", | |
fb2dcc52 JG |
114 | g_module_error()); |
115 | } | |
116 | goto end; | |
117 | } | |
118 | ||
119 | component_status = bt_plugin_register_component_classes(plugin, | |
b8a06801 | 120 | factory); |
fb2dcc52 JG |
121 | if (component_status != BT_COMPONENT_STATUS_OK) { |
122 | switch (component_status) { | |
123 | case BT_COMPONENT_STATUS_NOMEM: | |
124 | ret = BT_COMPONENT_FACTORY_STATUS_NOMEM; | |
125 | break; | |
126 | default: | |
127 | ret = BT_COMPONENT_FACTORY_STATUS_ERROR; | |
128 | break; | |
73299554 | 129 | } |
b8a06801 JG |
130 | |
131 | BT_PUT(plugin); | |
73299554 JG |
132 | goto end; |
133 | } | |
fb2dcc52 | 134 | g_ptr_array_add(factory->plugins, plugin); |
f3e4505b JG |
135 | end: |
136 | return ret; | |
137 | } | |
138 | ||
139 | static | |
140 | enum bt_component_factory_status | |
141 | bt_component_factory_load_dir_recursive(struct bt_component_factory *factory, | |
142 | const char *path) | |
143 | { | |
144 | DIR *directory = NULL; | |
145 | struct dirent *entry = NULL, *result = NULL; | |
146 | char *file_path = NULL; | |
147 | size_t path_len = strlen(path); | |
148 | enum bt_component_factory_status ret = BT_COMPONENT_FACTORY_STATUS_OK; | |
149 | ||
150 | if (path_len >= PATH_MAX) { | |
151 | ret = BT_COMPONENT_FACTORY_STATUS_INVAL; | |
152 | goto end; | |
153 | } | |
154 | ||
155 | entry = alloc_dirent(path); | |
156 | if (!entry) { | |
157 | ret = BT_COMPONENT_FACTORY_STATUS_NOMEM; | |
158 | goto end; | |
159 | } | |
160 | ||
161 | file_path = zmalloc(PATH_MAX); | |
162 | if (!file_path) { | |
163 | ret = BT_COMPONENT_FACTORY_STATUS_NOMEM; | |
164 | goto end; | |
165 | } | |
166 | ||
167 | strncpy(file_path, path, path_len); | |
168 | /* Append a trailing '/' to the path */ | |
169 | if (file_path[path_len - 1] != '/') { | |
170 | file_path[path_len++] = '/'; | |
171 | } | |
172 | ||
33bceaf8 JG |
173 | directory = opendir(file_path); |
174 | if (!directory) { | |
175 | perror("Failed to open plug-in directory"); | |
176 | ret = BT_COMPONENT_FACTORY_STATUS_ERROR; | |
177 | goto end; | |
178 | } | |
179 | ||
f3e4505b JG |
180 | /* Recursively walk directory */ |
181 | while (!readdir_r(directory, entry, &result) && result) { | |
182 | struct stat st; | |
183 | int stat_ret; | |
33bceaf8 JG |
184 | size_t file_name_len; |
185 | ||
186 | if (result->d_name[0] == '.') { | |
187 | /* Skip hidden files, . and .. */ | |
188 | continue; | |
189 | } | |
190 | ||
191 | file_name_len = strlen(result->d_name); | |
f3e4505b JG |
192 | |
193 | if (path_len + file_name_len >= PATH_MAX) { | |
194 | continue; | |
195 | } | |
196 | ||
197 | strncpy(file_path + path_len, result->d_name, file_name_len); | |
198 | file_path[path_len + file_name_len] = '\0'; | |
199 | ||
200 | stat_ret = stat(file_path, &st); | |
201 | if (stat_ret < 0) { | |
202 | /* Continue to next file / directory. */ | |
203 | printf_perror("Failed to stat() plugin file"); | |
204 | continue; | |
205 | } | |
206 | ||
207 | if (S_ISDIR(st.st_mode)) { | |
208 | ret = bt_component_factory_load_dir_recursive(factory, | |
b8a06801 | 209 | file_path); |
f3e4505b JG |
210 | if (ret != BT_COMPONENT_FACTORY_STATUS_OK) { |
211 | goto end; | |
212 | } | |
213 | } else if (S_ISREG(st.st_mode)) { | |
73299554 | 214 | bt_component_factory_load_file(factory, file_path); |
f3e4505b JG |
215 | } |
216 | } | |
217 | end: | |
33bceaf8 JG |
218 | if (directory) { |
219 | if (closedir(directory)) { | |
220 | /* | |
221 | * We don't want to override the error since there is | |
222 | * nothing could do. | |
223 | */ | |
224 | perror("Failed to close plug-in directory"); | |
225 | } | |
226 | } | |
f3e4505b JG |
227 | free(entry); |
228 | free(file_path); | |
229 | return ret; | |
230 | } | |
231 | ||
b8a06801 JG |
232 | static |
233 | void bt_component_factory_destroy(struct bt_object *obj) | |
f3e4505b | 234 | { |
b8a06801 JG |
235 | struct bt_component_factory *factory = NULL; |
236 | ||
237 | assert(obj); | |
238 | factory = container_of(obj, struct bt_component_factory, base); | |
f3e4505b | 239 | |
fb2dcc52 JG |
240 | if (factory->plugins) { |
241 | g_ptr_array_free(factory->plugins, TRUE); | |
242 | } | |
243 | if (factory->components) { | |
244 | g_ptr_array_free(factory->components, TRUE); | |
245 | } | |
f3e4505b JG |
246 | g_free(factory); |
247 | } | |
248 | ||
b8a06801 | 249 | struct bt_component_factory *bt_component_factory_create(void) |
f3e4505b JG |
250 | { |
251 | struct bt_component_factory *factory; | |
252 | ||
253 | factory = g_new0(struct bt_component_factory, 1); | |
254 | if (!factory) { | |
255 | goto end; | |
256 | } | |
257 | ||
b8a06801 | 258 | bt_object_init(factory, bt_component_factory_destroy); |
fb2dcc52 | 259 | factory->plugins = g_ptr_array_new_with_free_func( |
b8a06801 | 260 | (GDestroyNotify) bt_put); |
73299554 | 261 | if (!factory->plugins) { |
f3e4505b JG |
262 | goto error; |
263 | } | |
fb2dcc52 | 264 | factory->components = g_ptr_array_new_with_free_func( |
b8a06801 | 265 | (GDestroyNotify) bt_put); |
fb2dcc52 JG |
266 | if (!factory->components) { |
267 | goto error; | |
268 | } | |
f3e4505b JG |
269 | end: |
270 | return factory; | |
271 | error: | |
b8a06801 JG |
272 | BT_PUT(factory); |
273 | return factory; | |
f3e4505b JG |
274 | } |
275 | ||
276 | enum bt_component_factory_status bt_component_factory_load( | |
277 | struct bt_component_factory *factory, const char *path) | |
278 | { | |
279 | enum bt_component_factory_status ret = BT_COMPONENT_FACTORY_STATUS_OK; | |
f3e4505b JG |
280 | |
281 | if (!factory || !path) { | |
282 | ret = BT_COMPONENT_FACTORY_STATUS_INVAL; | |
283 | goto end; | |
284 | } | |
285 | ||
b8a06801 JG |
286 | if (!g_file_test(path, G_FILE_TEST_EXISTS)) { |
287 | ret = BT_COMPONENT_FACTORY_STATUS_NOENT; | |
288 | goto end; | |
f3e4505b JG |
289 | } |
290 | ||
b8a06801 | 291 | if (g_file_test(path, G_FILE_TEST_IS_DIR)) { |
f3e4505b | 292 | ret = bt_component_factory_load_dir_recursive(factory, path); |
b8a06801 JG |
293 | } else if (g_file_test(path, G_FILE_TEST_IS_REGULAR) || |
294 | g_file_test(path, G_FILE_TEST_IS_SYMLINK)) { | |
f3e4505b | 295 | ret = bt_component_factory_load_file(factory, path); |
b8a06801 JG |
296 | } else { |
297 | ret = BT_COMPONENT_FACTORY_STATUS_INVAL; | |
298 | goto end; | |
f3e4505b JG |
299 | } |
300 | end: | |
f3e4505b JG |
301 | return ret; |
302 | } |