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