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> | |
38b48196 | 31 | #include <babeltrace/plugin/component-class-internal.h> |
92893b7b JG |
32 | #include <babeltrace/plugin/source-internal.h> |
33 | #include <babeltrace/plugin/sink-internal.h> | |
f3e4505b JG |
34 | #include <babeltrace/babeltrace-internal.h> |
35 | #include <babeltrace/compiler.h> | |
b8a06801 | 36 | #include <babeltrace/ref.h> |
f3e4505b JG |
37 | #include <unistd.h> |
38 | #include <stdlib.h> | |
39 | #include <sys/stat.h> | |
fb2dcc52 | 40 | #include <gmodule.h> |
39cfa40f | 41 | #include <stdbool.h> |
eef2121c | 42 | #include <dirent.h> |
f3e4505b JG |
43 | |
44 | #define NATIVE_PLUGIN_SUFFIX ".so" | |
45 | #define NATIVE_PLUGIN_SUFFIX_LEN sizeof(NATIVE_PLUGIN_SUFFIX) | |
46 | #define LIBTOOL_PLUGIN_SUFFIX ".la" | |
47 | #define LIBTOOL_PLUGIN_SUFFIX_LEN sizeof(LIBTOOL_PLUGIN_SUFFIX) | |
73299554 JG |
48 | #define PLUGIN_SUFFIX_LEN max_t(size_t, sizeof(NATIVE_PLUGIN_SUFFIX), \ |
49 | sizeof(LIBTOOL_PLUGIN_SUFFIX)) | |
f3e4505b | 50 | |
f3e4505b JG |
51 | /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */ |
52 | static | |
53 | struct dirent *alloc_dirent(const char *path) | |
54 | { | |
55 | size_t len; | |
56 | long name_max; | |
57 | struct dirent *entry; | |
58 | ||
59 | name_max = pathconf(path, _PC_NAME_MAX); | |
60 | if (name_max == -1) { | |
61 | name_max = PATH_MAX; | |
62 | } | |
63 | len = offsetof(struct dirent, d_name) + name_max + 1; | |
64 | entry = zmalloc(len); | |
65 | return entry; | |
66 | } | |
67 | ||
68 | static | |
69 | enum bt_component_factory_status | |
70 | bt_component_factory_load_file(struct bt_component_factory *factory, | |
71 | const char *path) | |
72 | { | |
73 | enum bt_component_factory_status ret = BT_COMPONENT_FACTORY_STATUS_OK; | |
fb2dcc52 | 74 | enum bt_component_status component_status; |
f3e4505b JG |
75 | size_t path_len; |
76 | GModule *module; | |
fb2dcc52 | 77 | struct bt_plugin *plugin; |
92893b7b | 78 | bool is_libtool_wrapper = false, is_shared_object = false; |
f3e4505b JG |
79 | |
80 | if (!factory || !path) { | |
81 | ret = BT_COMPONENT_FACTORY_STATUS_INVAL; | |
82 | goto end; | |
83 | } | |
84 | ||
85 | path_len = strlen(path); | |
86 | if (path_len <= PLUGIN_SUFFIX_LEN) { | |
73299554 | 87 | ret = BT_COMPONENT_FACTORY_STATUS_INVAL; |
f3e4505b JG |
88 | goto end; |
89 | } | |
90 | ||
91 | path_len++; | |
92 | /* | |
93 | * Check if the file ends with a known plugin file type suffix (i.e. .so | |
94 | * or .la on Linux). | |
95 | */ | |
92893b7b | 96 | is_libtool_wrapper = !strncmp(LIBTOOL_PLUGIN_SUFFIX, |
b8a06801 | 97 | path + path_len - LIBTOOL_PLUGIN_SUFFIX_LEN, |
92893b7b JG |
98 | LIBTOOL_PLUGIN_SUFFIX_LEN); |
99 | is_shared_object = !strncmp(NATIVE_PLUGIN_SUFFIX, | |
100 | path + path_len - NATIVE_PLUGIN_SUFFIX_LEN, | |
101 | NATIVE_PLUGIN_SUFFIX_LEN); | |
102 | if (!is_shared_object && !is_libtool_wrapper) { | |
73299554 JG |
103 | /* Name indicates that this is not a plugin file. */ |
104 | ret = BT_COMPONENT_FACTORY_STATUS_INVAL; | |
f3e4505b JG |
105 | goto end; |
106 | } | |
107 | ||
108 | module = g_module_open(path, 0); | |
109 | if (!module) { | |
8bbddeca | 110 | printf_verbose("Module open error: %s\n", g_module_error()); |
73299554 | 111 | ret = BT_COMPONENT_FACTORY_STATUS_ERROR; |
f3e4505b JG |
112 | goto end; |
113 | } | |
114 | ||
fb2dcc52 | 115 | /* Load plugin and make sure it defines the required entry points */ |
7c7c0433 | 116 | plugin = bt_plugin_create(module, path); |
73299554 | 117 | if (!plugin) { |
fb2dcc52 | 118 | ret = BT_COMPONENT_FACTORY_STATUS_INVAL_PLUGIN; |
73299554 JG |
119 | if (!g_module_close(module)) { |
120 | printf_error("Module close error: %s", | |
fb2dcc52 JG |
121 | g_module_error()); |
122 | } | |
123 | goto end; | |
124 | } | |
125 | ||
0cc9e945 JG |
126 | BT_MOVE(factory->current_plugin, plugin); |
127 | component_status = bt_plugin_register_component_classes( | |
128 | factory->current_plugin, factory); | |
129 | BT_PUT(factory->current_plugin); | |
fb2dcc52 JG |
130 | if (component_status != BT_COMPONENT_STATUS_OK) { |
131 | switch (component_status) { | |
132 | case BT_COMPONENT_STATUS_NOMEM: | |
133 | ret = BT_COMPONENT_FACTORY_STATUS_NOMEM; | |
134 | break; | |
135 | default: | |
136 | ret = BT_COMPONENT_FACTORY_STATUS_ERROR; | |
137 | break; | |
73299554 | 138 | } |
b8a06801 | 139 | |
73299554 JG |
140 | goto end; |
141 | } | |
f3e4505b JG |
142 | end: |
143 | return ret; | |
144 | } | |
145 | ||
146 | static | |
147 | enum bt_component_factory_status | |
5d4cba3d JG |
148 | bt_component_factory_load_dir(struct bt_component_factory *factory, |
149 | const char *path, bool recurse) | |
f3e4505b JG |
150 | { |
151 | DIR *directory = NULL; | |
152 | struct dirent *entry = NULL, *result = NULL; | |
153 | char *file_path = NULL; | |
154 | size_t path_len = strlen(path); | |
155 | enum bt_component_factory_status ret = BT_COMPONENT_FACTORY_STATUS_OK; | |
156 | ||
157 | if (path_len >= PATH_MAX) { | |
158 | ret = BT_COMPONENT_FACTORY_STATUS_INVAL; | |
159 | goto end; | |
160 | } | |
161 | ||
162 | entry = alloc_dirent(path); | |
163 | if (!entry) { | |
164 | ret = BT_COMPONENT_FACTORY_STATUS_NOMEM; | |
165 | goto end; | |
166 | } | |
167 | ||
168 | file_path = zmalloc(PATH_MAX); | |
169 | if (!file_path) { | |
170 | ret = BT_COMPONENT_FACTORY_STATUS_NOMEM; | |
171 | goto end; | |
172 | } | |
173 | ||
174 | strncpy(file_path, path, path_len); | |
175 | /* Append a trailing '/' to the path */ | |
176 | if (file_path[path_len - 1] != '/') { | |
177 | file_path[path_len++] = '/'; | |
178 | } | |
179 | ||
33bceaf8 JG |
180 | directory = opendir(file_path); |
181 | if (!directory) { | |
182 | perror("Failed to open plug-in directory"); | |
183 | ret = BT_COMPONENT_FACTORY_STATUS_ERROR; | |
184 | goto end; | |
185 | } | |
186 | ||
f3e4505b JG |
187 | /* Recursively walk directory */ |
188 | while (!readdir_r(directory, entry, &result) && result) { | |
189 | struct stat st; | |
190 | int stat_ret; | |
33bceaf8 JG |
191 | size_t file_name_len; |
192 | ||
193 | if (result->d_name[0] == '.') { | |
194 | /* Skip hidden files, . and .. */ | |
195 | continue; | |
196 | } | |
197 | ||
198 | file_name_len = strlen(result->d_name); | |
f3e4505b JG |
199 | |
200 | if (path_len + file_name_len >= PATH_MAX) { | |
201 | continue; | |
202 | } | |
203 | ||
204 | strncpy(file_path + path_len, result->d_name, file_name_len); | |
205 | file_path[path_len + file_name_len] = '\0'; | |
206 | ||
207 | stat_ret = stat(file_path, &st); | |
208 | if (stat_ret < 0) { | |
209 | /* Continue to next file / directory. */ | |
210 | printf_perror("Failed to stat() plugin file"); | |
211 | continue; | |
212 | } | |
213 | ||
5d4cba3d JG |
214 | if (S_ISDIR(st.st_mode) && recurse) { |
215 | ret = bt_component_factory_load_dir(factory, | |
216 | file_path, true); | |
f3e4505b JG |
217 | if (ret != BT_COMPONENT_FACTORY_STATUS_OK) { |
218 | goto end; | |
219 | } | |
220 | } else if (S_ISREG(st.st_mode)) { | |
73299554 | 221 | bt_component_factory_load_file(factory, file_path); |
f3e4505b JG |
222 | } |
223 | } | |
224 | end: | |
33bceaf8 JG |
225 | if (directory) { |
226 | if (closedir(directory)) { | |
227 | /* | |
228 | * We don't want to override the error since there is | |
229 | * nothing could do. | |
230 | */ | |
231 | perror("Failed to close plug-in directory"); | |
232 | } | |
233 | } | |
f3e4505b JG |
234 | free(entry); |
235 | free(file_path); | |
236 | return ret; | |
237 | } | |
238 | ||
b8a06801 JG |
239 | static |
240 | void bt_component_factory_destroy(struct bt_object *obj) | |
f3e4505b | 241 | { |
b8a06801 JG |
242 | struct bt_component_factory *factory = NULL; |
243 | ||
244 | assert(obj); | |
245 | factory = container_of(obj, struct bt_component_factory, base); | |
f3e4505b | 246 | |
92893b7b JG |
247 | if (factory->component_classes) { |
248 | g_ptr_array_free(factory->component_classes, TRUE); | |
fb2dcc52 | 249 | } |
f3e4505b JG |
250 | g_free(factory); |
251 | } | |
252 | ||
b8a06801 | 253 | struct bt_component_factory *bt_component_factory_create(void) |
f3e4505b JG |
254 | { |
255 | struct bt_component_factory *factory; | |
256 | ||
257 | factory = g_new0(struct bt_component_factory, 1); | |
258 | if (!factory) { | |
259 | goto end; | |
260 | } | |
261 | ||
b8a06801 | 262 | bt_object_init(factory, bt_component_factory_destroy); |
92893b7b | 263 | factory->component_classes = g_ptr_array_new_with_free_func( |
b8a06801 | 264 | (GDestroyNotify) bt_put); |
92893b7b | 265 | if (!factory->component_classes) { |
fb2dcc52 JG |
266 | goto error; |
267 | } | |
f3e4505b JG |
268 | end: |
269 | return factory; | |
270 | error: | |
b8a06801 JG |
271 | BT_PUT(factory); |
272 | return factory; | |
f3e4505b JG |
273 | } |
274 | ||
38b48196 | 275 | int bt_component_factory_get_component_class_count( |
92893b7b JG |
276 | struct bt_component_factory *factory) |
277 | { | |
38b48196 JG |
278 | return factory ? factory->component_classes->len : -1; |
279 | } | |
280 | ||
281 | struct bt_component_class *bt_component_factory_get_component_class_index( | |
282 | struct bt_component_factory *factory, int index) | |
283 | { | |
284 | struct bt_component_class *component_class = NULL; | |
285 | ||
286 | if (!factory || index < 0 || index >= factory->component_classes->len) { | |
287 | goto end; | |
288 | } | |
289 | ||
290 | component_class = bt_get(g_ptr_array_index( | |
291 | factory->component_classes, index)); | |
292 | end: | |
293 | return component_class; | |
294 | } | |
295 | ||
296 | struct bt_component_class *bt_component_factory_get_component_class( | |
297 | struct bt_component_factory *factory, | |
298 | const char *plugin_name, enum bt_component_type type, | |
299 | const char *component_name) | |
300 | { | |
301 | size_t i; | |
302 | struct bt_component_class *component_class = NULL; | |
303 | ||
304 | if (!factory || (!plugin_name && !component_name && | |
305 | type == BT_COMPONENT_TYPE_UNKNOWN)) { | |
306 | /* At least one criterion must be provided. */ | |
307 | goto no_match; | |
308 | } | |
309 | ||
310 | for (i = 0; i < factory->component_classes->len; i++) { | |
311 | struct bt_plugin *plugin = NULL; | |
312 | ||
313 | component_class = g_ptr_array_index(factory->component_classes, | |
314 | i); | |
315 | plugin = bt_component_class_get_plugin(component_class); | |
316 | assert(plugin); | |
317 | ||
318 | if (type != BT_COMPONENT_TYPE_UNKNOWN) { | |
319 | if (type != bt_component_class_get_type( | |
320 | component_class)) { | |
0cc9e945 | 321 | bt_put(plugin); |
38b48196 JG |
322 | continue; |
323 | } | |
324 | } | |
325 | ||
326 | if (plugin_name) { | |
327 | const char *cur_plugin_name = bt_plugin_get_name( | |
328 | plugin); | |
329 | ||
330 | assert(cur_plugin_name); | |
331 | if (strcmp(plugin_name, cur_plugin_name)) { | |
0cc9e945 | 332 | bt_put(plugin); |
38b48196 JG |
333 | continue; |
334 | } | |
335 | } | |
336 | ||
337 | if (component_name) { | |
338 | const char *cur_cc_name = bt_component_class_get_name( | |
339 | component_class); | |
340 | ||
341 | assert(cur_cc_name); | |
342 | if (strcmp(component_name, cur_cc_name)) { | |
0cc9e945 | 343 | bt_put(plugin); |
38b48196 JG |
344 | continue; |
345 | } | |
346 | } | |
347 | ||
0cc9e945 | 348 | bt_put(plugin); |
38b48196 JG |
349 | /* All criteria met. */ |
350 | goto match; | |
351 | } | |
352 | ||
353 | no_match: | |
92893b7b | 354 | return NULL; |
38b48196 JG |
355 | match: |
356 | return bt_get(component_class); | |
92893b7b JG |
357 | } |
358 | ||
5d4cba3d JG |
359 | static |
360 | enum bt_component_factory_status _bt_component_factory_load( | |
361 | struct bt_component_factory *factory, const char *path, | |
362 | bool recursive) | |
f3e4505b JG |
363 | { |
364 | enum bt_component_factory_status ret = BT_COMPONENT_FACTORY_STATUS_OK; | |
f3e4505b JG |
365 | |
366 | if (!factory || !path) { | |
367 | ret = BT_COMPONENT_FACTORY_STATUS_INVAL; | |
368 | goto end; | |
369 | } | |
370 | ||
b8a06801 JG |
371 | if (!g_file_test(path, G_FILE_TEST_EXISTS)) { |
372 | ret = BT_COMPONENT_FACTORY_STATUS_NOENT; | |
373 | goto end; | |
f3e4505b JG |
374 | } |
375 | ||
b8a06801 | 376 | if (g_file_test(path, G_FILE_TEST_IS_DIR)) { |
5d4cba3d | 377 | ret = bt_component_factory_load_dir(factory, path, recursive); |
b8a06801 JG |
378 | } else if (g_file_test(path, G_FILE_TEST_IS_REGULAR) || |
379 | g_file_test(path, G_FILE_TEST_IS_SYMLINK)) { | |
f3e4505b | 380 | ret = bt_component_factory_load_file(factory, path); |
b8a06801 JG |
381 | } else { |
382 | ret = BT_COMPONENT_FACTORY_STATUS_INVAL; | |
383 | goto end; | |
f3e4505b JG |
384 | } |
385 | end: | |
f3e4505b JG |
386 | return ret; |
387 | } | |
92893b7b | 388 | |
5d4cba3d JG |
389 | enum bt_component_factory_status bt_component_factory_load_recursive( |
390 | struct bt_component_factory *factory, const char *path) | |
391 | { | |
392 | return _bt_component_factory_load(factory, path, true); | |
393 | } | |
394 | ||
395 | enum bt_component_factory_status bt_component_factory_load( | |
396 | struct bt_component_factory *factory, const char *path) | |
397 | { | |
398 | return _bt_component_factory_load(factory, path, false); | |
399 | } | |
400 | ||
38b48196 | 401 | static |
92893b7b | 402 | enum bt_component_factory_status |
38b48196 | 403 | add_component_class(struct bt_component_factory *factory, const char *name, |
7c7c0433 JG |
404 | const char *description, bt_component_init_cb init, |
405 | enum bt_component_type type) | |
92893b7b | 406 | { |
0859fe1f | 407 | struct bt_component_class *component_class; |
92893b7b JG |
408 | enum bt_component_factory_status ret = BT_COMPONENT_FACTORY_STATUS_OK; |
409 | ||
410 | if (!factory || !name || !init) { | |
411 | ret = BT_COMPONENT_FACTORY_STATUS_INVAL; | |
412 | goto end; | |
413 | } | |
0859fe1f | 414 | assert(factory->current_plugin); |
92893b7b | 415 | |
0859fe1f JG |
416 | /* |
417 | * Ensure this component class does not clash with a currently | |
418 | * registered class. | |
419 | */ | |
420 | component_class = bt_component_factory_get_component_class(factory, | |
421 | bt_plugin_get_name(factory->current_plugin), type, name); | |
422 | if (component_class) { | |
423 | struct bt_plugin *plugin = bt_component_class_get_plugin( | |
424 | component_class); | |
425 | ||
426 | printf_warning("Duplicate component class registration attempted. Component class %s being registered by plugin %s (path: %s) conflicts with one already registered by plugin %s (path: %s)", | |
427 | name, bt_plugin_get_name(factory->current_plugin), | |
428 | bt_plugin_get_path(factory->current_plugin), | |
429 | bt_plugin_get_name(plugin), | |
430 | bt_plugin_get_path(plugin)); | |
431 | ret = BT_COMPONENT_FACTORY_STATUS_DUPLICATE; | |
432 | BT_PUT(component_class); | |
433 | bt_put(plugin); | |
434 | goto end; | |
435 | } | |
436 | ||
437 | component_class = bt_component_class_create(type, name, description, | |
4b70dd83 | 438 | init, factory->current_plugin); |
0859fe1f | 439 | g_ptr_array_add(factory->component_classes, component_class); |
92893b7b JG |
440 | end: |
441 | return ret; | |
442 | } | |
38b48196 JG |
443 | |
444 | enum bt_component_factory_status | |
445 | bt_component_factory_register_source_component_class( | |
446 | struct bt_component_factory *factory, const char *name, | |
7c7c0433 | 447 | const char *description, bt_component_init_cb init) |
38b48196 | 448 | { |
7c7c0433 | 449 | return add_component_class(factory, name, description, init, |
38b48196 JG |
450 | BT_COMPONENT_TYPE_SOURCE); |
451 | } | |
452 | ||
453 | enum bt_component_factory_status | |
454 | bt_component_factory_register_sink_component_class( | |
455 | struct bt_component_factory *factory, const char *name, | |
7c7c0433 | 456 | const char *description, bt_component_init_cb init) |
38b48196 | 457 | { |
7c7c0433 | 458 | return add_component_class(factory, name, description, init, |
38b48196 JG |
459 | BT_COMPONENT_TYPE_SINK); |
460 | } |