Commit | Line | Data |
---|---|---|
33b34c43 PP |
1 | /* |
2 | * plugin.c | |
3 | * | |
55bb57e0 | 4 | * Babeltrace Plugin (generic) |
33b34c43 PP |
5 | * |
6 | * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
7 | * Copyright 2017 Philippe Proulx <pproulx@efficios.com> | |
8 | * | |
9 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
10 | * | |
11 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
12 | * of this software and associated documentation files (the "Software"), to deal | |
13 | * in the Software without restriction, including without limitation the rights | |
14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
15 | * copies of the Software, and to permit persons to whom the Software is | |
16 | * furnished to do so, subject to the following conditions: | |
17 | * | |
18 | * The above copyright notice and this permission notice shall be included in | |
19 | * all copies or substantial portions of the Software. | |
20 | * | |
21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
27 | * SOFTWARE. | |
28 | */ | |
29 | ||
8c1a3187 PP |
30 | #define BT_LOG_TAG "PLUGIN" |
31 | #include <babeltrace/lib-logging-internal.h> | |
32 | ||
6fbd4105 | 33 | #include <babeltrace/babeltrace-internal.h> |
3d9990ac | 34 | #include <babeltrace/compiler-internal.h> |
33b34c43 | 35 | #include <babeltrace/ref.h> |
1670bffd | 36 | #include <babeltrace/common-internal.h> |
33b34c43 | 37 | #include <babeltrace/plugin/plugin-internal.h> |
55bb57e0 | 38 | #include <babeltrace/plugin/plugin-so-internal.h> |
8c1a3187 PP |
39 | #include <babeltrace/graph/component-class.h> |
40 | #include <babeltrace/graph/component-class-internal.h> | |
c55a9f58 | 41 | #include <babeltrace/types.h> |
33b34c43 | 42 | #include <glib.h> |
33b34c43 PP |
43 | #include <unistd.h> |
44 | #include <stdlib.h> | |
9ac68eb1 | 45 | #include <stdint.h> |
8c1a3187 | 46 | #include <inttypes.h> |
33b34c43 | 47 | #include <sys/stat.h> |
e1f4c4f7 MJ |
48 | #include <ftw.h> |
49 | #include <pthread.h> | |
33b34c43 | 50 | |
6fbd4105 PP |
51 | #define PYTHON_PLUGIN_PROVIDER_FILENAME "libbabeltrace-python-plugin-provider." G_MODULE_SUFFIX |
52 | #define PYTHON_PLUGIN_PROVIDER_SYM_NAME bt_plugin_python_create_all_from_file | |
53 | #define PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR TOSTRING(PYTHON_PLUGIN_PROVIDER_SYM_NAME) | |
33b34c43 | 54 | |
e1f4c4f7 MJ |
55 | #define APPEND_ALL_FROM_DIR_NFDOPEN_MAX 8 |
56 | ||
6fbd4105 PP |
57 | #ifdef BT_BUILT_IN_PYTHON_PLUGIN_SUPPORT |
58 | #include <babeltrace/plugin/python-plugin-provider-internal.h> | |
e1f4c4f7 | 59 | |
33b34c43 | 60 | static |
a8ff38ef | 61 | struct bt_plugin_set *(*bt_plugin_python_create_all_from_file_sym)(const char *path) = |
6fbd4105 PP |
62 | bt_plugin_python_create_all_from_file; |
63 | #else /* BT_BUILT_IN_PYTHON_PLUGIN_SUPPORT */ | |
64 | static GModule *python_plugin_provider_module; | |
65 | static | |
a8ff38ef | 66 | struct bt_plugin_set *(*bt_plugin_python_create_all_from_file_sym)(const char *path); |
33b34c43 | 67 | |
6fbd4105 PP |
68 | __attribute__((constructor)) static |
69 | void init_python_plugin_provider(void) { | |
8c1a3187 | 70 | BT_LOGD_STR("Loading Python plugin provider module."); |
6fbd4105 PP |
71 | python_plugin_provider_module = |
72 | g_module_open(PYTHON_PLUGIN_PROVIDER_FILENAME, | |
73 | G_MODULE_BIND_LOCAL); | |
74 | if (!python_plugin_provider_module) { | |
8c1a3187 | 75 | BT_LOGI("Cannot find `%s`: continuing without Python plugin support.", |
6fbd4105 PP |
76 | PYTHON_PLUGIN_PROVIDER_FILENAME); |
77 | return; | |
33b34c43 PP |
78 | } |
79 | ||
6fbd4105 PP |
80 | if (!g_module_symbol(python_plugin_provider_module, |
81 | PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR, | |
82 | (gpointer) &bt_plugin_python_create_all_from_file_sym)) { | |
8c1a3187 PP |
83 | BT_LOGI("Cannot find the Python plugin provider loading symbol: continuing without Python plugin support: " |
84 | "file=\"%s\", symbol=\"%s\"", | |
85 | PYTHON_PLUGIN_PROVIDER_FILENAME, | |
86 | PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR); | |
87 | return; | |
33b34c43 | 88 | } |
8c1a3187 | 89 | |
9e0bf9b0 | 90 | BT_LOGI("Loaded Python plugin provider module: addr=%p", |
8c1a3187 | 91 | python_plugin_provider_module); |
33b34c43 PP |
92 | } |
93 | ||
6fbd4105 PP |
94 | __attribute__((destructor)) static |
95 | void fini_python_plugin_provider(void) { | |
96 | if (python_plugin_provider_module) { | |
8c1a3187 PP |
97 | BT_LOGD("Unloading Python plugin provider module."); |
98 | ||
99 | if (!g_module_close(python_plugin_provider_module)) { | |
100 | BT_LOGE("Failed to close the Python plugin provider module: %s.", | |
101 | g_module_error()); | |
102 | } | |
103 | ||
6fbd4105 | 104 | python_plugin_provider_module = NULL; |
6ba0b073 | 105 | } |
6ba0b073 | 106 | } |
6fbd4105 | 107 | #endif |
6ba0b073 | 108 | |
a8ff38ef | 109 | extern |
544d0515 | 110 | int64_t bt_plugin_set_get_plugin_count(struct bt_plugin_set *plugin_set) |
a8ff38ef | 111 | { |
544d0515 | 112 | int64_t count = -1; |
a8ff38ef PP |
113 | |
114 | if (!plugin_set) { | |
8c1a3187 | 115 | BT_LOGW_STR("Invalid parameter: plugin set is NULL."); |
a8ff38ef PP |
116 | goto end; |
117 | } | |
118 | ||
9ac68eb1 | 119 | count = (int64_t) plugin_set->plugins->len; |
a8ff38ef PP |
120 | |
121 | end: | |
122 | return count; | |
123 | } | |
124 | ||
125 | extern | |
126 | struct bt_plugin *bt_plugin_set_get_plugin(struct bt_plugin_set *plugin_set, | |
9ac68eb1 | 127 | uint64_t index) |
a8ff38ef PP |
128 | { |
129 | struct bt_plugin *plugin = NULL; | |
130 | ||
8c1a3187 PP |
131 | if (!plugin_set) { |
132 | BT_LOGW_STR("Invalid parameter: plugin set is NULL."); | |
133 | goto end; | |
134 | } | |
135 | ||
136 | if (index >= plugin_set->plugins->len) { | |
137 | BT_LOGW("Invalid parameter: index is out of bounds: " | |
138 | "addr=%p, index=%" PRIu64 ", count=%u", | |
139 | plugin_set, index, plugin_set->plugins->len); | |
a8ff38ef PP |
140 | goto end; |
141 | } | |
142 | ||
143 | plugin = bt_get(g_ptr_array_index(plugin_set->plugins, index)); | |
144 | ||
145 | end: | |
146 | return plugin; | |
147 | } | |
148 | ||
149 | struct bt_plugin_set *bt_plugin_create_all_from_static(void) | |
6ba0b073 | 150 | { |
8c1a3187 | 151 | /* bt_plugin_so_create_all_from_static() logs errors */ |
55bb57e0 | 152 | return bt_plugin_so_create_all_from_static(); |
6ba0b073 PP |
153 | } |
154 | ||
a8ff38ef | 155 | struct bt_plugin_set *bt_plugin_create_all_from_file(const char *path) |
33b34c43 | 156 | { |
a8ff38ef | 157 | struct bt_plugin_set *plugin_set = NULL; |
33b34c43 PP |
158 | |
159 | if (!path) { | |
8c1a3187 | 160 | BT_LOGW_STR("Invalid parameter: path is NULL."); |
6ba0b073 | 161 | goto end; |
33b34c43 PP |
162 | } |
163 | ||
8c1a3187 | 164 | BT_LOGD("Creating plugins from file: path=\"%s\"", path); |
6ba0b073 | 165 | |
55bb57e0 | 166 | /* Try shared object plugins */ |
a8ff38ef PP |
167 | plugin_set = bt_plugin_so_create_all_from_file(path); |
168 | if (plugin_set) { | |
6ba0b073 PP |
169 | goto end; |
170 | } | |
171 | ||
6fbd4105 PP |
172 | /* Try Python plugins if support is available */ |
173 | if (bt_plugin_python_create_all_from_file_sym) { | |
a8ff38ef PP |
174 | plugin_set = bt_plugin_python_create_all_from_file_sym(path); |
175 | if (plugin_set) { | |
6fbd4105 PP |
176 | goto end; |
177 | } | |
6ba0b073 PP |
178 | } |
179 | ||
33b34c43 | 180 | end: |
8c1a3187 PP |
181 | if (plugin_set) { |
182 | BT_LOGD("Created %u plugins from file: " | |
183 | "path=\"%s\", count=%u, plugin-set-addr=%p", | |
184 | plugin_set->plugins->len, path, | |
185 | plugin_set->plugins->len, plugin_set); | |
186 | } else { | |
187 | BT_LOGD("Found no plugins in file: path=\"%s\"", path); | |
188 | } | |
189 | ||
a8ff38ef | 190 | return plugin_set; |
33b34c43 PP |
191 | } |
192 | ||
1670bffd PP |
193 | static void destroy_gstring(void *data) |
194 | { | |
195 | g_string_free(data, TRUE); | |
196 | } | |
197 | ||
2b43acf9 | 198 | struct bt_plugin *bt_plugin_find(const char *plugin_name) |
1670bffd PP |
199 | { |
200 | const char *system_plugin_dir; | |
201 | char *home_plugin_dir = NULL; | |
202 | const char *envvar; | |
203 | struct bt_plugin *plugin = NULL; | |
a8ff38ef | 204 | struct bt_plugin_set *plugin_set = NULL; |
1670bffd PP |
205 | GPtrArray *dirs = NULL; |
206 | int ret; | |
a8ff38ef | 207 | size_t i, j; |
1670bffd PP |
208 | |
209 | if (!plugin_name) { | |
8c1a3187 | 210 | BT_LOGW_STR("Invalid parameter: plugin name is NULL."); |
1670bffd PP |
211 | goto end; |
212 | } | |
213 | ||
8c1a3187 PP |
214 | BT_LOGD("Finding named plugin in standard directories and built-in plugins: " |
215 | "name=\"%s\"", plugin_name); | |
1670bffd PP |
216 | dirs = g_ptr_array_new_with_free_func((GDestroyNotify) destroy_gstring); |
217 | if (!dirs) { | |
8c1a3187 | 218 | BT_LOGE_STR("Failed to allocate a GPtrArray."); |
1670bffd PP |
219 | goto end; |
220 | } | |
221 | ||
222 | /* | |
223 | * Search order is: | |
224 | * | |
225 | * 1. BABELTRACE_PLUGIN_PATH environment variable | |
226 | * (colon-separated list of directories) | |
227 | * 2. ~/.local/lib/babeltrace/plugins | |
228 | * 3. Default system directory for Babeltrace plugins, usually | |
229 | * /usr/lib/babeltrace/plugins or | |
230 | * /usr/local/lib/babeltrace/plugins if installed | |
231 | * locally | |
232 | * 4. Built-in plugins (static) | |
233 | * | |
234 | * Directories are searched non-recursively. | |
235 | */ | |
236 | envvar = getenv("BABELTRACE_PLUGIN_PATH"); | |
237 | if (envvar) { | |
238 | ret = bt_common_append_plugin_path_dirs(envvar, dirs); | |
239 | if (ret) { | |
8c1a3187 | 240 | BT_LOGE_STR("Failed to append plugin path to array of directories."); |
1670bffd PP |
241 | goto end; |
242 | } | |
243 | } | |
244 | ||
245 | home_plugin_dir = bt_common_get_home_plugin_path(); | |
246 | if (home_plugin_dir) { | |
247 | GString *home_plugin_dir_str = | |
248 | g_string_new(home_plugin_dir); | |
249 | ||
250 | if (!home_plugin_dir_str) { | |
8c1a3187 | 251 | BT_LOGE_STR("Failed to allocate a GString."); |
1670bffd PP |
252 | goto end; |
253 | } | |
254 | ||
255 | g_ptr_array_add(dirs, home_plugin_dir_str); | |
256 | } | |
257 | ||
258 | system_plugin_dir = bt_common_get_system_plugin_path(); | |
259 | if (system_plugin_dir) { | |
260 | GString *system_plugin_dir_str = | |
261 | g_string_new(system_plugin_dir); | |
262 | ||
263 | if (!system_plugin_dir_str) { | |
8c1a3187 | 264 | BT_LOGE_STR("Failed to allocate a GString."); |
1670bffd PP |
265 | goto end; |
266 | } | |
267 | ||
268 | g_ptr_array_add(dirs, system_plugin_dir_str); | |
269 | } | |
270 | ||
271 | for (i = 0; i < dirs->len; i++) { | |
272 | GString *dir = g_ptr_array_index(dirs, i); | |
273 | ||
a8ff38ef | 274 | BT_PUT(plugin_set); |
8c1a3187 | 275 | |
50ad9320 PP |
276 | /* |
277 | * Skip this if the directory does not exist because | |
278 | * bt_plugin_create_all_from_dir() would log a warning. | |
279 | */ | |
280 | if (!g_file_test(dir->str, G_FILE_TEST_IS_DIR)) { | |
281 | BT_LOGV("Skipping nonexistent directory path: " | |
282 | "path=\"%s\"", dir->str); | |
283 | continue; | |
284 | } | |
285 | ||
8c1a3187 | 286 | /* bt_plugin_create_all_from_dir() logs details/errors */ |
c55a9f58 | 287 | plugin_set = bt_plugin_create_all_from_dir(dir->str, BT_FALSE); |
a8ff38ef | 288 | if (!plugin_set) { |
8c1a3187 PP |
289 | BT_LOGD("No plugins found in directory: path=\"%s\"", |
290 | dir->str); | |
1670bffd PP |
291 | continue; |
292 | } | |
293 | ||
a8ff38ef PP |
294 | for (j = 0; j < plugin_set->plugins->len; j++) { |
295 | struct bt_plugin *candidate_plugin = | |
296 | g_ptr_array_index(plugin_set->plugins, j); | |
1670bffd | 297 | |
a8ff38ef PP |
298 | if (strcmp(bt_plugin_get_name(candidate_plugin), |
299 | plugin_name) == 0) { | |
8c1a3187 PP |
300 | BT_LOGD("Plugin found in directory: name=\"%s\", path=\"%s\"", |
301 | plugin_name, dir->str); | |
a8ff38ef | 302 | plugin = bt_get(candidate_plugin); |
1670bffd PP |
303 | goto end; |
304 | } | |
1670bffd | 305 | } |
8c1a3187 PP |
306 | |
307 | BT_LOGD("Plugin not found in directory: name=\"%s\", path=\"%s\"", | |
308 | plugin_name, dir->str); | |
1670bffd PP |
309 | } |
310 | ||
a8ff38ef PP |
311 | bt_put(plugin_set); |
312 | plugin_set = bt_plugin_create_all_from_static(); | |
8c1a3187 PP |
313 | if (plugin_set) { |
314 | for (j = 0; j < plugin_set->plugins->len; j++) { | |
315 | struct bt_plugin *candidate_plugin = | |
316 | g_ptr_array_index(plugin_set->plugins, j); | |
a8ff38ef | 317 | |
8c1a3187 PP |
318 | if (strcmp(bt_plugin_get_name(candidate_plugin), |
319 | plugin_name) == 0) { | |
320 | BT_LOGD("Plugin found in built-in plugins: " | |
321 | "name=\"%s\"", plugin_name); | |
322 | plugin = bt_get(candidate_plugin); | |
323 | goto end; | |
324 | } | |
1670bffd | 325 | } |
1670bffd PP |
326 | } |
327 | ||
328 | end: | |
329 | free(home_plugin_dir); | |
a8ff38ef | 330 | bt_put(plugin_set); |
1670bffd PP |
331 | |
332 | if (dirs) { | |
333 | g_ptr_array_free(dirs, TRUE); | |
334 | } | |
335 | ||
8c1a3187 PP |
336 | if (plugin) { |
337 | BT_LOGD("Found plugin in standard directories and built-in plugins: " | |
338 | "addr=%p, name=\"%s\", path=\"%s\"", | |
339 | plugin, plugin_name, bt_plugin_get_path(plugin)); | |
340 | } else { | |
341 | BT_LOGD("No plugin found in standard directories and built-in plugins: " | |
342 | "name=\"%s\"", plugin_name); | |
343 | } | |
344 | ||
1670bffd PP |
345 | return plugin; |
346 | } | |
347 | ||
2b43acf9 PP |
348 | struct bt_component_class *bt_plugin_find_component_class( |
349 | const char *plugin_name, const char *comp_cls_name, | |
350 | enum bt_component_class_type comp_cls_type) | |
351 | { | |
352 | struct bt_plugin *plugin = NULL; | |
353 | struct bt_component_class *comp_cls = NULL; | |
354 | ||
8c1a3187 PP |
355 | if (!plugin_name) { |
356 | BT_LOGW_STR("Invalid parameter: plugin name is NULL."); | |
2b43acf9 PP |
357 | goto end; |
358 | } | |
359 | ||
8c1a3187 PP |
360 | if (!comp_cls_name) { |
361 | BT_LOGW_STR("Invalid parameter: component class name is NULL."); | |
362 | goto end; | |
363 | } | |
364 | ||
365 | BT_LOGD("Finding named plugin and component class in standard directories and built-in plugins: " | |
366 | "plugin-name=\"%s\", comp-class-name=\"%s\"", | |
367 | plugin_name, comp_cls_name); | |
2b43acf9 PP |
368 | plugin = bt_plugin_find(plugin_name); |
369 | if (!plugin) { | |
8c1a3187 | 370 | BT_LOGD_STR("Plugin not found."); |
2b43acf9 PP |
371 | goto end; |
372 | } | |
373 | ||
374 | comp_cls = bt_plugin_get_component_class_by_name_and_type( | |
375 | plugin, comp_cls_name, comp_cls_type); | |
8c1a3187 PP |
376 | if (!comp_cls) { |
377 | BT_LOGD("Component class not found in plugin: " | |
378 | "plugin-addr=%p, plugin-path=\"%s\"", | |
379 | plugin, bt_plugin_get_path(plugin)); | |
380 | } | |
2b43acf9 PP |
381 | |
382 | end: | |
383 | bt_put(plugin); | |
384 | return comp_cls; | |
385 | } | |
386 | ||
e1f4c4f7 MJ |
387 | static struct { |
388 | pthread_mutex_t lock; | |
389 | struct bt_plugin_set *plugin_set; | |
390 | bt_bool recurse; | |
391 | } append_all_from_dir_info = { | |
392 | .lock = PTHREAD_MUTEX_INITIALIZER | |
393 | }; | |
394 | ||
33b34c43 | 395 | static |
e1f4c4f7 MJ |
396 | int nftw_append_all_from_dir(const char *file, const struct stat *sb, int flag, |
397 | struct FTW *s) | |
33b34c43 | 398 | { |
e1f4c4f7 MJ |
399 | int ret = 0; |
400 | const char *name = file + s->base; | |
401 | ||
402 | /* Check for recursion */ | |
403 | if (!append_all_from_dir_info.recurse && s->level > 1) { | |
404 | goto end; | |
405 | } | |
406 | ||
407 | switch (flag) { | |
408 | case FTW_F: | |
52238017 MJ |
409 | { |
410 | struct bt_plugin_set *plugins_from_file; | |
411 | ||
e1f4c4f7 MJ |
412 | if (name[0] == '.') { |
413 | /* Skip hidden files */ | |
414 | BT_LOGV("Skipping hidden file: path=\"%s\"", file); | |
415 | goto end; | |
416 | } | |
52238017 MJ |
417 | |
418 | plugins_from_file = bt_plugin_create_all_from_file(file); | |
e1f4c4f7 MJ |
419 | |
420 | if (plugins_from_file) { | |
421 | size_t j; | |
422 | ||
423 | for (j = 0; j < plugins_from_file->plugins->len; j++) { | |
424 | struct bt_plugin *plugin = | |
425 | g_ptr_array_index(plugins_from_file->plugins, j); | |
426 | ||
427 | BT_LOGD("Adding plugin to plugin set: " | |
428 | "plugin-path=\"%s\", plugin-addr=%p, plugin-name=\"%s\"", | |
429 | file, plugin, bt_plugin_get_name(plugin)); | |
430 | bt_plugin_set_add_plugin(append_all_from_dir_info.plugin_set, plugin); | |
431 | } | |
33b34c43 | 432 | |
e1f4c4f7 MJ |
433 | bt_put(plugins_from_file); |
434 | } | |
435 | break; | |
52238017 | 436 | } |
e1f4c4f7 MJ |
437 | case FTW_DNR: |
438 | /* Continue to next file / directory. */ | |
439 | BT_LOGW("Cannot enter directory: continuing: path=\"%s\"", file); | |
440 | break; | |
441 | case FTW_NS: | |
442 | /* Continue to next file / directory. */ | |
443 | BT_LOGD("Cannot get file information: continuing: path=\"%s\"", file); | |
444 | break; | |
33b34c43 | 445 | } |
e1f4c4f7 MJ |
446 | |
447 | end: | |
448 | return ret; | |
33b34c43 PP |
449 | } |
450 | ||
451 | static | |
452 | enum bt_plugin_status bt_plugin_create_append_all_from_dir( | |
a8ff38ef | 453 | struct bt_plugin_set *plugin_set, const char *path, |
c55a9f58 | 454 | bt_bool recurse) |
33b34c43 | 455 | { |
e1f4c4f7 | 456 | int nftw_flags = FTW_PHYS; |
6ba0b073 | 457 | size_t path_len; |
33b34c43 PP |
458 | enum bt_plugin_status ret = BT_PLUGIN_STATUS_OK; |
459 | ||
6ba0b073 | 460 | if (!path) { |
8c1a3187 | 461 | BT_LOGW_STR("Invalid parameter: path is NULL."); |
6ba0b073 PP |
462 | ret = BT_PLUGIN_STATUS_ERROR; |
463 | goto end; | |
464 | } | |
465 | ||
466 | path_len = strlen(path); | |
33b34c43 | 467 | if (path_len >= PATH_MAX) { |
8c1a3187 PP |
468 | BT_LOGW("Invalid parameter: path length is too large: " |
469 | "path-length=%zu", path_len); | |
33b34c43 PP |
470 | ret = BT_PLUGIN_STATUS_ERROR; |
471 | goto end; | |
472 | } | |
473 | ||
e1f4c4f7 | 474 | pthread_mutex_lock(&append_all_from_dir_info.lock); |
33b34c43 | 475 | |
e1f4c4f7 MJ |
476 | append_all_from_dir_info.plugin_set = plugin_set; |
477 | append_all_from_dir_info.recurse = recurse; | |
478 | ret = nftw(path, nftw_append_all_from_dir, | |
479 | APPEND_ALL_FROM_DIR_NFDOPEN_MAX, nftw_flags); | |
33b34c43 | 480 | |
e1f4c4f7 | 481 | pthread_mutex_unlock(&append_all_from_dir_info.lock); |
33b34c43 | 482 | |
e1f4c4f7 MJ |
483 | if (ret != 0) { |
484 | BT_LOGW("Cannot open directory: %s: path=\"%s\", errno=%d", | |
485 | strerror(errno), path, errno); | |
33b34c43 | 486 | ret = BT_PLUGIN_STATUS_ERROR; |
33b34c43 | 487 | } |
8c1a3187 | 488 | |
33b34c43 | 489 | end: |
33b34c43 PP |
490 | return ret; |
491 | } | |
492 | ||
a8ff38ef | 493 | struct bt_plugin_set *bt_plugin_create_all_from_dir(const char *path, |
c55a9f58 | 494 | bt_bool recurse) |
33b34c43 | 495 | { |
a8ff38ef | 496 | struct bt_plugin_set *plugin_set; |
33b34c43 PP |
497 | enum bt_plugin_status status; |
498 | ||
8c1a3187 PP |
499 | BT_LOGD("Creating all plugins in directory: path=\"%s\", recurse=%d", |
500 | path, recurse); | |
a8ff38ef PP |
501 | plugin_set = bt_plugin_set_create(); |
502 | if (!plugin_set) { | |
8c1a3187 | 503 | BT_LOGE_STR("Cannot create empty plugin set."); |
a8ff38ef PP |
504 | goto error; |
505 | } | |
506 | ||
33b34c43 | 507 | /* Append found plugins to array */ |
a8ff38ef | 508 | status = bt_plugin_create_append_all_from_dir(plugin_set, path, |
33b34c43 PP |
509 | recurse); |
510 | if (status < 0) { | |
8c1a3187 | 511 | BT_LOGW("Cannot append plugins found in directory: " |
dbd7f7e9 PP |
512 | "path=\"%s\", status=%s", |
513 | path, bt_plugin_status_string(status)); | |
33b34c43 PP |
514 | goto error; |
515 | } | |
516 | ||
8c1a3187 PP |
517 | BT_LOGD("Created %u plugins from directory: count=%u, path=\"%s\"", |
518 | plugin_set->plugins->len, plugin_set->plugins->len, path); | |
33b34c43 PP |
519 | goto end; |
520 | ||
521 | error: | |
a8ff38ef | 522 | BT_PUT(plugin_set); |
33b34c43 PP |
523 | |
524 | end: | |
a8ff38ef | 525 | return plugin_set; |
33b34c43 PP |
526 | } |
527 | ||
33b34c43 PP |
528 | const char *bt_plugin_get_name(struct bt_plugin *plugin) |
529 | { | |
8c1a3187 PP |
530 | const char *val = NULL; |
531 | ||
532 | if (!plugin) { | |
533 | BT_LOGW_STR("Invalid parameter: plugin is NULL."); | |
534 | goto end; | |
535 | } | |
536 | ||
537 | if (plugin->info.name_set) { | |
538 | val = plugin->info.name->str; | |
539 | } | |
540 | ||
541 | end: | |
542 | return val; | |
33b34c43 PP |
543 | } |
544 | ||
545 | const char *bt_plugin_get_author(struct bt_plugin *plugin) | |
546 | { | |
8c1a3187 PP |
547 | const char *val = NULL; |
548 | ||
549 | if (!plugin) { | |
550 | BT_LOGW_STR("Invalid parameter: plugin is NULL."); | |
551 | goto end; | |
552 | } | |
553 | ||
554 | if (plugin->info.author_set) { | |
555 | val = plugin->info.author->str; | |
556 | } | |
557 | ||
558 | end: | |
559 | return val; | |
33b34c43 PP |
560 | } |
561 | ||
562 | const char *bt_plugin_get_license(struct bt_plugin *plugin) | |
563 | { | |
8c1a3187 PP |
564 | const char *val = NULL; |
565 | ||
566 | if (!plugin) { | |
567 | BT_LOGW_STR("Invalid parameter: plugin is NULL."); | |
568 | goto end; | |
569 | } | |
570 | ||
571 | if (plugin->info.license_set) { | |
572 | val = plugin->info.license->str; | |
573 | } | |
574 | ||
575 | end: | |
576 | return val; | |
33b34c43 PP |
577 | } |
578 | ||
579 | const char *bt_plugin_get_path(struct bt_plugin *plugin) | |
580 | { | |
8c1a3187 PP |
581 | const char *val = NULL; |
582 | ||
583 | if (!plugin) { | |
584 | BT_LOGW_STR("Invalid parameter: plugin is NULL."); | |
585 | goto end; | |
586 | } | |
587 | ||
588 | if (plugin->info.path_set) { | |
589 | val = plugin->info.path->str; | |
590 | } | |
591 | ||
592 | end: | |
593 | return val; | |
33b34c43 PP |
594 | } |
595 | ||
596 | const char *bt_plugin_get_description(struct bt_plugin *plugin) | |
597 | { | |
8c1a3187 PP |
598 | const char *val = NULL; |
599 | ||
600 | if (!plugin) { | |
601 | BT_LOGW_STR("Invalid parameter: plugin is NULL."); | |
602 | goto end; | |
603 | } | |
604 | ||
605 | if (plugin->info.description_set) { | |
606 | val = plugin->info.description->str; | |
607 | } | |
608 | ||
609 | end: | |
610 | return val; | |
33b34c43 PP |
611 | } |
612 | ||
b6de043b PP |
613 | enum bt_plugin_status bt_plugin_get_version(struct bt_plugin *plugin, |
614 | unsigned int *major, unsigned int *minor, unsigned int *patch, | |
615 | const char **extra) | |
616 | { | |
617 | enum bt_plugin_status status = BT_PLUGIN_STATUS_OK; | |
618 | ||
8c1a3187 PP |
619 | if (!plugin) { |
620 | BT_LOGW_STR("Invalid parameter: plugin is NULL."); | |
621 | status = BT_PLUGIN_STATUS_ERROR; | |
622 | goto end; | |
623 | } | |
624 | ||
625 | if (!plugin->info.version_set) { | |
626 | BT_LOGV("Plugin's version is not set: addr=%p", plugin); | |
b6de043b PP |
627 | status = BT_PLUGIN_STATUS_ERROR; |
628 | goto end; | |
629 | } | |
630 | ||
631 | if (major) { | |
55bb57e0 | 632 | *major = plugin->info.version.major; |
b6de043b PP |
633 | } |
634 | ||
635 | if (minor) { | |
55bb57e0 | 636 | *minor = plugin->info.version.minor; |
b6de043b PP |
637 | } |
638 | ||
639 | if (patch) { | |
55bb57e0 | 640 | *patch = plugin->info.version.patch; |
b6de043b PP |
641 | } |
642 | ||
643 | if (extra) { | |
55bb57e0 | 644 | *extra = plugin->info.version.extra->str; |
b6de043b PP |
645 | } |
646 | ||
647 | end: | |
648 | return status; | |
649 | } | |
650 | ||
544d0515 | 651 | int64_t bt_plugin_get_component_class_count(struct bt_plugin *plugin) |
33b34c43 | 652 | { |
9ac68eb1 | 653 | return plugin ? plugin->comp_classes->len : (int64_t) -1; |
33b34c43 PP |
654 | } |
655 | ||
9ac68eb1 PP |
656 | struct bt_component_class *bt_plugin_get_component_class_by_index( |
657 | struct bt_plugin *plugin, uint64_t index) | |
33b34c43 PP |
658 | { |
659 | struct bt_component_class *comp_class = NULL; | |
660 | ||
8c1a3187 PP |
661 | if (!plugin) { |
662 | BT_LOGW_STR("Invalid parameter: plugin is NULL."); | |
663 | goto error; | |
664 | } | |
665 | ||
666 | if (index >= plugin->comp_classes->len) { | |
667 | BT_LOGW("Invalid parameter: index is out of bounds: " | |
668 | "addr=%p, index=%" PRIu64 ", count=%u", | |
669 | plugin, index, plugin->comp_classes->len); | |
33b34c43 PP |
670 | goto error; |
671 | } | |
672 | ||
673 | comp_class = g_ptr_array_index(plugin->comp_classes, index); | |
674 | bt_get(comp_class); | |
675 | goto end; | |
676 | ||
677 | error: | |
678 | BT_PUT(comp_class); | |
679 | ||
680 | end: | |
681 | return comp_class; | |
682 | } | |
683 | ||
684 | struct bt_component_class *bt_plugin_get_component_class_by_name_and_type( | |
685 | struct bt_plugin *plugin, const char *name, | |
d3e4dcd8 | 686 | enum bt_component_class_type type) |
33b34c43 PP |
687 | { |
688 | struct bt_component_class *comp_class = NULL; | |
689 | size_t i; | |
690 | ||
8c1a3187 PP |
691 | if (!plugin) { |
692 | BT_LOGW_STR("Invalid parameter: plugin is NULL."); | |
693 | goto error; | |
694 | } | |
695 | ||
696 | if (!name) { | |
697 | BT_LOGW_STR("Invalid parameter: name is NULL."); | |
33b34c43 PP |
698 | goto error; |
699 | } | |
700 | ||
701 | for (i = 0; i < plugin->comp_classes->len; i++) { | |
702 | struct bt_component_class *comp_class_candidate = | |
703 | g_ptr_array_index(plugin->comp_classes, i); | |
704 | const char *comp_class_cand_name = | |
705 | bt_component_class_get_name(comp_class_candidate); | |
d3e4dcd8 | 706 | enum bt_component_class_type comp_class_cand_type = |
33b34c43 PP |
707 | bt_component_class_get_type(comp_class_candidate); |
708 | ||
709 | assert(comp_class_cand_name); | |
710 | assert(comp_class_cand_type >= 0); | |
711 | ||
712 | if (strcmp(name, comp_class_cand_name) == 0 && | |
713 | comp_class_cand_type == type) { | |
714 | comp_class = bt_get(comp_class_candidate); | |
715 | break; | |
716 | } | |
717 | } | |
718 | ||
719 | goto end; | |
720 | ||
721 | error: | |
722 | BT_PUT(comp_class); | |
723 | ||
724 | end: | |
725 | return comp_class; | |
726 | } | |
727 | ||
33b34c43 PP |
728 | enum bt_plugin_status bt_plugin_add_component_class( |
729 | struct bt_plugin *plugin, struct bt_component_class *comp_class) | |
730 | { | |
731 | enum bt_plugin_status status = BT_PLUGIN_STATUS_OK; | |
732 | struct bt_component_class *comp_class_dup = NULL; | |
33b34c43 PP |
733 | int comp_class_index = -1; |
734 | ||
8c1a3187 PP |
735 | if (!plugin) { |
736 | BT_LOGW_STR("Invalid parameter: plugin is NULL."); | |
737 | goto error; | |
738 | } | |
739 | ||
740 | if (!comp_class) { | |
741 | BT_LOGW_STR("Invalid parameter: component class is NULL."); | |
742 | goto error; | |
743 | } | |
744 | ||
745 | if (plugin->frozen) { | |
746 | BT_LOGW("Invalid parameter: plugin is frozen: " | |
747 | "addr=%p, name=\"%s\"", plugin, | |
748 | bt_plugin_get_name(plugin)); | |
33b34c43 PP |
749 | goto error; |
750 | } | |
751 | ||
752 | /* Check for duplicate */ | |
753 | comp_class_dup = bt_plugin_get_component_class_by_name_and_type(plugin, | |
754 | bt_component_class_get_name(comp_class), | |
755 | bt_component_class_get_type(comp_class)); | |
756 | if (comp_class_dup) { | |
8c1a3187 PP |
757 | BT_LOGW("Invalid parameter: a component class with this name and type already exists in the plugin: " |
758 | "plugin-addr=%p, plugin-name=\"%s\", plugin-path=\"%s\", " | |
759 | "comp-class-name=\"%s\", comp-class-type=%s", | |
760 | plugin, bt_plugin_get_name(plugin), | |
761 | bt_plugin_get_path(plugin), | |
33b34c43 | 762 | bt_component_class_get_name(comp_class), |
8c1a3187 PP |
763 | bt_component_class_type_string( |
764 | bt_component_class_get_type(comp_class))); | |
33b34c43 PP |
765 | goto error; |
766 | } | |
767 | ||
768 | /* Add new component class */ | |
769 | comp_class_index = plugin->comp_classes->len; | |
770 | g_ptr_array_add(plugin->comp_classes, bt_get(comp_class)); | |
771 | ||
55bb57e0 PP |
772 | /* Special case for a shared object plugin */ |
773 | if (plugin->type == BT_PLUGIN_TYPE_SO) { | |
3230ee6b | 774 | bt_plugin_so_on_add_component_class(plugin, comp_class); |
33b34c43 PP |
775 | } |
776 | ||
8c1a3187 PP |
777 | BT_LOGD("Added component class to plugin: " |
778 | "plugin-addr=%p, plugin-name=\"%s\", plugin-path=\"%s\", " | |
779 | "comp-class-addr=%p, comp-class-name=\"%s\", comp-class-type=%s", | |
780 | plugin, bt_plugin_get_name(plugin), | |
781 | bt_plugin_get_path(plugin), | |
782 | comp_class, | |
783 | bt_component_class_get_name(comp_class), | |
784 | bt_component_class_type_string( | |
785 | bt_component_class_get_type(comp_class))); | |
33b34c43 PP |
786 | goto end; |
787 | ||
788 | error: | |
33b34c43 PP |
789 | /* Remove entry from plugin's component classes (if added) */ |
790 | if (comp_class_index >= 0) { | |
791 | g_ptr_array_remove_index(plugin->comp_classes, | |
792 | comp_class_index); | |
793 | } | |
794 | ||
795 | status = BT_PLUGIN_STATUS_ERROR; | |
796 | ||
797 | end: | |
798 | bt_put(comp_class_dup); | |
799 | return status; | |
800 | } |