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