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: | |
409 | if (name[0] == '.') { | |
410 | /* Skip hidden files */ | |
411 | BT_LOGV("Skipping hidden file: path=\"%s\"", file); | |
412 | goto end; | |
413 | } | |
414 | struct bt_plugin_set *plugins_from_file = | |
415 | bt_plugin_create_all_from_file(file); | |
416 | ||
417 | if (plugins_from_file) { | |
418 | size_t j; | |
419 | ||
420 | for (j = 0; j < plugins_from_file->plugins->len; j++) { | |
421 | struct bt_plugin *plugin = | |
422 | g_ptr_array_index(plugins_from_file->plugins, j); | |
423 | ||
424 | BT_LOGD("Adding plugin to plugin set: " | |
425 | "plugin-path=\"%s\", plugin-addr=%p, plugin-name=\"%s\"", | |
426 | file, plugin, bt_plugin_get_name(plugin)); | |
427 | bt_plugin_set_add_plugin(append_all_from_dir_info.plugin_set, plugin); | |
428 | } | |
33b34c43 | 429 | |
e1f4c4f7 MJ |
430 | bt_put(plugins_from_file); |
431 | } | |
432 | break; | |
433 | case FTW_DNR: | |
434 | /* Continue to next file / directory. */ | |
435 | BT_LOGW("Cannot enter directory: continuing: path=\"%s\"", file); | |
436 | break; | |
437 | case FTW_NS: | |
438 | /* Continue to next file / directory. */ | |
439 | BT_LOGD("Cannot get file information: continuing: path=\"%s\"", file); | |
440 | break; | |
33b34c43 | 441 | } |
e1f4c4f7 MJ |
442 | |
443 | end: | |
444 | return ret; | |
33b34c43 PP |
445 | } |
446 | ||
447 | static | |
448 | enum bt_plugin_status bt_plugin_create_append_all_from_dir( | |
a8ff38ef | 449 | struct bt_plugin_set *plugin_set, const char *path, |
c55a9f58 | 450 | bt_bool recurse) |
33b34c43 | 451 | { |
e1f4c4f7 | 452 | int nftw_flags = FTW_PHYS; |
6ba0b073 | 453 | size_t path_len; |
33b34c43 PP |
454 | enum bt_plugin_status ret = BT_PLUGIN_STATUS_OK; |
455 | ||
6ba0b073 | 456 | if (!path) { |
8c1a3187 | 457 | BT_LOGW_STR("Invalid parameter: path is NULL."); |
6ba0b073 PP |
458 | ret = BT_PLUGIN_STATUS_ERROR; |
459 | goto end; | |
460 | } | |
461 | ||
462 | path_len = strlen(path); | |
33b34c43 | 463 | if (path_len >= PATH_MAX) { |
8c1a3187 PP |
464 | BT_LOGW("Invalid parameter: path length is too large: " |
465 | "path-length=%zu", path_len); | |
33b34c43 PP |
466 | ret = BT_PLUGIN_STATUS_ERROR; |
467 | goto end; | |
468 | } | |
469 | ||
e1f4c4f7 | 470 | pthread_mutex_lock(&append_all_from_dir_info.lock); |
33b34c43 | 471 | |
e1f4c4f7 MJ |
472 | append_all_from_dir_info.plugin_set = plugin_set; |
473 | append_all_from_dir_info.recurse = recurse; | |
474 | ret = nftw(path, nftw_append_all_from_dir, | |
475 | APPEND_ALL_FROM_DIR_NFDOPEN_MAX, nftw_flags); | |
33b34c43 | 476 | |
e1f4c4f7 | 477 | pthread_mutex_unlock(&append_all_from_dir_info.lock); |
33b34c43 | 478 | |
e1f4c4f7 MJ |
479 | if (ret != 0) { |
480 | BT_LOGW("Cannot open directory: %s: path=\"%s\", errno=%d", | |
481 | strerror(errno), path, errno); | |
33b34c43 | 482 | ret = BT_PLUGIN_STATUS_ERROR; |
33b34c43 | 483 | } |
8c1a3187 | 484 | |
33b34c43 | 485 | end: |
33b34c43 PP |
486 | return ret; |
487 | } | |
488 | ||
a8ff38ef | 489 | struct bt_plugin_set *bt_plugin_create_all_from_dir(const char *path, |
c55a9f58 | 490 | bt_bool recurse) |
33b34c43 | 491 | { |
a8ff38ef | 492 | struct bt_plugin_set *plugin_set; |
33b34c43 PP |
493 | enum bt_plugin_status status; |
494 | ||
8c1a3187 PP |
495 | BT_LOGD("Creating all plugins in directory: path=\"%s\", recurse=%d", |
496 | path, recurse); | |
a8ff38ef PP |
497 | plugin_set = bt_plugin_set_create(); |
498 | if (!plugin_set) { | |
8c1a3187 | 499 | BT_LOGE_STR("Cannot create empty plugin set."); |
a8ff38ef PP |
500 | goto error; |
501 | } | |
502 | ||
33b34c43 | 503 | /* Append found plugins to array */ |
a8ff38ef | 504 | status = bt_plugin_create_append_all_from_dir(plugin_set, path, |
33b34c43 PP |
505 | recurse); |
506 | if (status < 0) { | |
8c1a3187 | 507 | BT_LOGW("Cannot append plugins found in directory: " |
dbd7f7e9 PP |
508 | "path=\"%s\", status=%s", |
509 | path, bt_plugin_status_string(status)); | |
33b34c43 PP |
510 | goto error; |
511 | } | |
512 | ||
8c1a3187 PP |
513 | BT_LOGD("Created %u plugins from directory: count=%u, path=\"%s\"", |
514 | plugin_set->plugins->len, plugin_set->plugins->len, path); | |
33b34c43 PP |
515 | goto end; |
516 | ||
517 | error: | |
a8ff38ef | 518 | BT_PUT(plugin_set); |
33b34c43 PP |
519 | |
520 | end: | |
a8ff38ef | 521 | return plugin_set; |
33b34c43 PP |
522 | } |
523 | ||
33b34c43 PP |
524 | const char *bt_plugin_get_name(struct bt_plugin *plugin) |
525 | { | |
8c1a3187 PP |
526 | const char *val = NULL; |
527 | ||
528 | if (!plugin) { | |
529 | BT_LOGW_STR("Invalid parameter: plugin is NULL."); | |
530 | goto end; | |
531 | } | |
532 | ||
533 | if (plugin->info.name_set) { | |
534 | val = plugin->info.name->str; | |
535 | } | |
536 | ||
537 | end: | |
538 | return val; | |
33b34c43 PP |
539 | } |
540 | ||
541 | const char *bt_plugin_get_author(struct bt_plugin *plugin) | |
542 | { | |
8c1a3187 PP |
543 | const char *val = NULL; |
544 | ||
545 | if (!plugin) { | |
546 | BT_LOGW_STR("Invalid parameter: plugin is NULL."); | |
547 | goto end; | |
548 | } | |
549 | ||
550 | if (plugin->info.author_set) { | |
551 | val = plugin->info.author->str; | |
552 | } | |
553 | ||
554 | end: | |
555 | return val; | |
33b34c43 PP |
556 | } |
557 | ||
558 | const char *bt_plugin_get_license(struct bt_plugin *plugin) | |
559 | { | |
8c1a3187 PP |
560 | const char *val = NULL; |
561 | ||
562 | if (!plugin) { | |
563 | BT_LOGW_STR("Invalid parameter: plugin is NULL."); | |
564 | goto end; | |
565 | } | |
566 | ||
567 | if (plugin->info.license_set) { | |
568 | val = plugin->info.license->str; | |
569 | } | |
570 | ||
571 | end: | |
572 | return val; | |
33b34c43 PP |
573 | } |
574 | ||
575 | const char *bt_plugin_get_path(struct bt_plugin *plugin) | |
576 | { | |
8c1a3187 PP |
577 | const char *val = NULL; |
578 | ||
579 | if (!plugin) { | |
580 | BT_LOGW_STR("Invalid parameter: plugin is NULL."); | |
581 | goto end; | |
582 | } | |
583 | ||
584 | if (plugin->info.path_set) { | |
585 | val = plugin->info.path->str; | |
586 | } | |
587 | ||
588 | end: | |
589 | return val; | |
33b34c43 PP |
590 | } |
591 | ||
592 | const char *bt_plugin_get_description(struct bt_plugin *plugin) | |
593 | { | |
8c1a3187 PP |
594 | const char *val = NULL; |
595 | ||
596 | if (!plugin) { | |
597 | BT_LOGW_STR("Invalid parameter: plugin is NULL."); | |
598 | goto end; | |
599 | } | |
600 | ||
601 | if (plugin->info.description_set) { | |
602 | val = plugin->info.description->str; | |
603 | } | |
604 | ||
605 | end: | |
606 | return val; | |
33b34c43 PP |
607 | } |
608 | ||
b6de043b PP |
609 | enum bt_plugin_status bt_plugin_get_version(struct bt_plugin *plugin, |
610 | unsigned int *major, unsigned int *minor, unsigned int *patch, | |
611 | const char **extra) | |
612 | { | |
613 | enum bt_plugin_status status = BT_PLUGIN_STATUS_OK; | |
614 | ||
8c1a3187 PP |
615 | if (!plugin) { |
616 | BT_LOGW_STR("Invalid parameter: plugin is NULL."); | |
617 | status = BT_PLUGIN_STATUS_ERROR; | |
618 | goto end; | |
619 | } | |
620 | ||
621 | if (!plugin->info.version_set) { | |
622 | BT_LOGV("Plugin's version is not set: addr=%p", plugin); | |
b6de043b PP |
623 | status = BT_PLUGIN_STATUS_ERROR; |
624 | goto end; | |
625 | } | |
626 | ||
627 | if (major) { | |
55bb57e0 | 628 | *major = plugin->info.version.major; |
b6de043b PP |
629 | } |
630 | ||
631 | if (minor) { | |
55bb57e0 | 632 | *minor = plugin->info.version.minor; |
b6de043b PP |
633 | } |
634 | ||
635 | if (patch) { | |
55bb57e0 | 636 | *patch = plugin->info.version.patch; |
b6de043b PP |
637 | } |
638 | ||
639 | if (extra) { | |
55bb57e0 | 640 | *extra = plugin->info.version.extra->str; |
b6de043b PP |
641 | } |
642 | ||
643 | end: | |
644 | return status; | |
645 | } | |
646 | ||
544d0515 | 647 | int64_t bt_plugin_get_component_class_count(struct bt_plugin *plugin) |
33b34c43 | 648 | { |
9ac68eb1 | 649 | return plugin ? plugin->comp_classes->len : (int64_t) -1; |
33b34c43 PP |
650 | } |
651 | ||
9ac68eb1 PP |
652 | struct bt_component_class *bt_plugin_get_component_class_by_index( |
653 | struct bt_plugin *plugin, uint64_t index) | |
33b34c43 PP |
654 | { |
655 | struct bt_component_class *comp_class = NULL; | |
656 | ||
8c1a3187 PP |
657 | if (!plugin) { |
658 | BT_LOGW_STR("Invalid parameter: plugin is NULL."); | |
659 | goto error; | |
660 | } | |
661 | ||
662 | if (index >= plugin->comp_classes->len) { | |
663 | BT_LOGW("Invalid parameter: index is out of bounds: " | |
664 | "addr=%p, index=%" PRIu64 ", count=%u", | |
665 | plugin, index, plugin->comp_classes->len); | |
33b34c43 PP |
666 | goto error; |
667 | } | |
668 | ||
669 | comp_class = g_ptr_array_index(plugin->comp_classes, index); | |
670 | bt_get(comp_class); | |
671 | goto end; | |
672 | ||
673 | error: | |
674 | BT_PUT(comp_class); | |
675 | ||
676 | end: | |
677 | return comp_class; | |
678 | } | |
679 | ||
680 | struct bt_component_class *bt_plugin_get_component_class_by_name_and_type( | |
681 | struct bt_plugin *plugin, const char *name, | |
d3e4dcd8 | 682 | enum bt_component_class_type type) |
33b34c43 PP |
683 | { |
684 | struct bt_component_class *comp_class = NULL; | |
685 | size_t i; | |
686 | ||
8c1a3187 PP |
687 | if (!plugin) { |
688 | BT_LOGW_STR("Invalid parameter: plugin is NULL."); | |
689 | goto error; | |
690 | } | |
691 | ||
692 | if (!name) { | |
693 | BT_LOGW_STR("Invalid parameter: name is NULL."); | |
33b34c43 PP |
694 | goto error; |
695 | } | |
696 | ||
697 | for (i = 0; i < plugin->comp_classes->len; i++) { | |
698 | struct bt_component_class *comp_class_candidate = | |
699 | g_ptr_array_index(plugin->comp_classes, i); | |
700 | const char *comp_class_cand_name = | |
701 | bt_component_class_get_name(comp_class_candidate); | |
d3e4dcd8 | 702 | enum bt_component_class_type comp_class_cand_type = |
33b34c43 PP |
703 | bt_component_class_get_type(comp_class_candidate); |
704 | ||
705 | assert(comp_class_cand_name); | |
706 | assert(comp_class_cand_type >= 0); | |
707 | ||
708 | if (strcmp(name, comp_class_cand_name) == 0 && | |
709 | comp_class_cand_type == type) { | |
710 | comp_class = bt_get(comp_class_candidate); | |
711 | break; | |
712 | } | |
713 | } | |
714 | ||
715 | goto end; | |
716 | ||
717 | error: | |
718 | BT_PUT(comp_class); | |
719 | ||
720 | end: | |
721 | return comp_class; | |
722 | } | |
723 | ||
33b34c43 PP |
724 | enum bt_plugin_status bt_plugin_add_component_class( |
725 | struct bt_plugin *plugin, struct bt_component_class *comp_class) | |
726 | { | |
727 | enum bt_plugin_status status = BT_PLUGIN_STATUS_OK; | |
728 | struct bt_component_class *comp_class_dup = NULL; | |
33b34c43 PP |
729 | int comp_class_index = -1; |
730 | ||
8c1a3187 PP |
731 | if (!plugin) { |
732 | BT_LOGW_STR("Invalid parameter: plugin is NULL."); | |
733 | goto error; | |
734 | } | |
735 | ||
736 | if (!comp_class) { | |
737 | BT_LOGW_STR("Invalid parameter: component class is NULL."); | |
738 | goto error; | |
739 | } | |
740 | ||
741 | if (plugin->frozen) { | |
742 | BT_LOGW("Invalid parameter: plugin is frozen: " | |
743 | "addr=%p, name=\"%s\"", plugin, | |
744 | bt_plugin_get_name(plugin)); | |
33b34c43 PP |
745 | goto error; |
746 | } | |
747 | ||
748 | /* Check for duplicate */ | |
749 | comp_class_dup = bt_plugin_get_component_class_by_name_and_type(plugin, | |
750 | bt_component_class_get_name(comp_class), | |
751 | bt_component_class_get_type(comp_class)); | |
752 | if (comp_class_dup) { | |
8c1a3187 PP |
753 | BT_LOGW("Invalid parameter: a component class with this name and type already exists in the plugin: " |
754 | "plugin-addr=%p, plugin-name=\"%s\", plugin-path=\"%s\", " | |
755 | "comp-class-name=\"%s\", comp-class-type=%s", | |
756 | plugin, bt_plugin_get_name(plugin), | |
757 | bt_plugin_get_path(plugin), | |
33b34c43 | 758 | bt_component_class_get_name(comp_class), |
8c1a3187 PP |
759 | bt_component_class_type_string( |
760 | bt_component_class_get_type(comp_class))); | |
33b34c43 PP |
761 | goto error; |
762 | } | |
763 | ||
764 | /* Add new component class */ | |
765 | comp_class_index = plugin->comp_classes->len; | |
766 | g_ptr_array_add(plugin->comp_classes, bt_get(comp_class)); | |
767 | ||
55bb57e0 PP |
768 | /* Special case for a shared object plugin */ |
769 | if (plugin->type == BT_PLUGIN_TYPE_SO) { | |
3230ee6b | 770 | bt_plugin_so_on_add_component_class(plugin, comp_class); |
33b34c43 PP |
771 | } |
772 | ||
8c1a3187 PP |
773 | BT_LOGD("Added component class to plugin: " |
774 | "plugin-addr=%p, plugin-name=\"%s\", plugin-path=\"%s\", " | |
775 | "comp-class-addr=%p, comp-class-name=\"%s\", comp-class-type=%s", | |
776 | plugin, bt_plugin_get_name(plugin), | |
777 | bt_plugin_get_path(plugin), | |
778 | comp_class, | |
779 | bt_component_class_get_name(comp_class), | |
780 | bt_component_class_type_string( | |
781 | bt_component_class_get_type(comp_class))); | |
33b34c43 PP |
782 | goto end; |
783 | ||
784 | error: | |
33b34c43 PP |
785 | /* Remove entry from plugin's component classes (if added) */ |
786 | if (comp_class_index >= 0) { | |
787 | g_ptr_array_remove_index(plugin->comp_classes, | |
788 | comp_class_index); | |
789 | } | |
790 | ||
791 | status = BT_PLUGIN_STATUS_ERROR; | |
792 | ||
793 | end: | |
794 | bt_put(comp_class_dup); | |
795 | return status; | |
796 | } |