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" |
578e048b | 27 | #include "lib/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 |
a8ff38ef | 60 | struct bt_plugin_set *(*bt_plugin_python_create_all_from_file_sym)(const char *path) = |
6fbd4105 | 61 | bt_plugin_python_create_all_from_file; |
95ef44ce MJ |
62 | |
63 | static | |
64 | void init_python_plugin_provider(void) {} | |
6fbd4105 PP |
65 | #else /* BT_BUILT_IN_PYTHON_PLUGIN_SUPPORT */ |
66 | static GModule *python_plugin_provider_module; | |
67 | static | |
a8ff38ef | 68 | struct bt_plugin_set *(*bt_plugin_python_create_all_from_file_sym)(const char *path); |
33b34c43 | 69 | |
95ef44ce | 70 | static |
6fbd4105 | 71 | void init_python_plugin_provider(void) { |
95ef44ce MJ |
72 | if (bt_plugin_python_create_all_from_file_sym != NULL) { |
73 | return; | |
74 | } | |
75 | ||
3f7d4d90 | 76 | BT_LOGI_STR("Loading Python plugin provider module."); |
6fbd4105 | 77 | python_plugin_provider_module = |
c92fb666 | 78 | g_module_open(PYTHON_PLUGIN_PROVIDER_FILENAME, 0); |
6fbd4105 | 79 | if (!python_plugin_provider_module) { |
a77aed14 PP |
80 | BT_LOGI("Cannot open `%s`: %s: continuing without Python plugin support.", |
81 | PYTHON_PLUGIN_PROVIDER_FILENAME, g_module_error()); | |
6fbd4105 | 82 | return; |
33b34c43 PP |
83 | } |
84 | ||
6fbd4105 PP |
85 | if (!g_module_symbol(python_plugin_provider_module, |
86 | PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR, | |
87 | (gpointer) &bt_plugin_python_create_all_from_file_sym)) { | |
8c1a3187 PP |
88 | BT_LOGI("Cannot find the Python plugin provider loading symbol: continuing without Python plugin support: " |
89 | "file=\"%s\", symbol=\"%s\"", | |
90 | PYTHON_PLUGIN_PROVIDER_FILENAME, | |
91 | PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR); | |
92 | return; | |
33b34c43 | 93 | } |
8c1a3187 | 94 | |
9e0bf9b0 | 95 | BT_LOGI("Loaded Python plugin provider module: addr=%p", |
8c1a3187 | 96 | python_plugin_provider_module); |
33b34c43 PP |
97 | } |
98 | ||
6fbd4105 PP |
99 | __attribute__((destructor)) static |
100 | void fini_python_plugin_provider(void) { | |
101 | if (python_plugin_provider_module) { | |
3f7d4d90 | 102 | BT_LOGI("Unloading Python plugin provider module."); |
8c1a3187 PP |
103 | |
104 | if (!g_module_close(python_plugin_provider_module)) { | |
105 | BT_LOGE("Failed to close the Python plugin provider module: %s.", | |
106 | g_module_error()); | |
107 | } | |
108 | ||
6fbd4105 | 109 | python_plugin_provider_module = NULL; |
6ba0b073 | 110 | } |
6ba0b073 | 111 | } |
6fbd4105 | 112 | #endif |
6ba0b073 | 113 | |
d94d92ac | 114 | uint64_t bt_plugin_set_get_plugin_count(struct bt_plugin_set *plugin_set) |
a8ff38ef | 115 | { |
d94d92ac PP |
116 | BT_ASSERT_PRE_NON_NULL(plugin_set, "Plugin set"); |
117 | return (uint64_t) plugin_set->plugins->len; | |
a8ff38ef PP |
118 | } |
119 | ||
92fed4e1 PP |
120 | const struct bt_plugin *bt_plugin_set_borrow_plugin_by_index_const( |
121 | const struct bt_plugin_set *plugin_set, uint64_t index) | |
a8ff38ef | 122 | { |
d94d92ac PP |
123 | BT_ASSERT_PRE_NON_NULL(plugin_set, "Plugin set"); |
124 | BT_ASSERT_PRE_VALID_INDEX(index, plugin_set->plugins->len); | |
125 | return g_ptr_array_index(plugin_set->plugins, index); | |
a8ff38ef PP |
126 | } |
127 | ||
c8db3219 | 128 | const struct bt_plugin_set *bt_plugin_find_all_from_static(void) |
6ba0b073 | 129 | { |
8c1a3187 | 130 | /* bt_plugin_so_create_all_from_static() logs errors */ |
55bb57e0 | 131 | return bt_plugin_so_create_all_from_static(); |
6ba0b073 PP |
132 | } |
133 | ||
c8db3219 | 134 | const struct bt_plugin_set *bt_plugin_find_all_from_file(const char *path) |
33b34c43 | 135 | { |
a8ff38ef | 136 | struct bt_plugin_set *plugin_set = NULL; |
33b34c43 | 137 | |
d94d92ac | 138 | BT_ASSERT_PRE_NON_NULL(path, "Path"); |
3f7d4d90 | 139 | BT_LOGI("Creating plugins from file: path=\"%s\"", path); |
6ba0b073 | 140 | |
55bb57e0 | 141 | /* Try shared object plugins */ |
a8ff38ef PP |
142 | plugin_set = bt_plugin_so_create_all_from_file(path); |
143 | if (plugin_set) { | |
6ba0b073 PP |
144 | goto end; |
145 | } | |
146 | ||
6fbd4105 | 147 | /* Try Python plugins if support is available */ |
95ef44ce | 148 | init_python_plugin_provider(); |
6fbd4105 | 149 | if (bt_plugin_python_create_all_from_file_sym) { |
a8ff38ef PP |
150 | plugin_set = bt_plugin_python_create_all_from_file_sym(path); |
151 | if (plugin_set) { | |
6fbd4105 PP |
152 | goto end; |
153 | } | |
6ba0b073 PP |
154 | } |
155 | ||
33b34c43 | 156 | end: |
8c1a3187 | 157 | if (plugin_set) { |
3f7d4d90 | 158 | BT_LOGI("Created %u plugins from file: " |
8c1a3187 PP |
159 | "path=\"%s\", count=%u, plugin-set-addr=%p", |
160 | plugin_set->plugins->len, path, | |
161 | plugin_set->plugins->len, plugin_set); | |
162 | } else { | |
3f7d4d90 | 163 | BT_LOGI("Found no plugins in file: path=\"%s\"", path); |
8c1a3187 PP |
164 | } |
165 | ||
a8ff38ef | 166 | return plugin_set; |
33b34c43 PP |
167 | } |
168 | ||
1670bffd PP |
169 | static void destroy_gstring(void *data) |
170 | { | |
171 | g_string_free(data, TRUE); | |
172 | } | |
173 | ||
92fed4e1 | 174 | const struct bt_plugin *bt_plugin_find(const char *plugin_name) |
1670bffd PP |
175 | { |
176 | const char *system_plugin_dir; | |
177 | char *home_plugin_dir = NULL; | |
178 | const char *envvar; | |
92fed4e1 PP |
179 | const struct bt_plugin *plugin = NULL; |
180 | const struct bt_plugin_set *plugin_set = NULL; | |
1670bffd PP |
181 | GPtrArray *dirs = NULL; |
182 | int ret; | |
a8ff38ef | 183 | size_t i, j; |
1670bffd | 184 | |
d94d92ac | 185 | BT_ASSERT_PRE_NON_NULL(plugin_name, "Name"); |
3f7d4d90 | 186 | BT_LOGI("Finding named plugin in standard directories and built-in plugins: " |
8c1a3187 | 187 | "name=\"%s\"", plugin_name); |
1670bffd PP |
188 | dirs = g_ptr_array_new_with_free_func((GDestroyNotify) destroy_gstring); |
189 | if (!dirs) { | |
8c1a3187 | 190 | BT_LOGE_STR("Failed to allocate a GPtrArray."); |
1670bffd PP |
191 | goto end; |
192 | } | |
193 | ||
194 | /* | |
195 | * Search order is: | |
196 | * | |
197 | * 1. BABELTRACE_PLUGIN_PATH environment variable | |
198 | * (colon-separated list of directories) | |
d9676d8c | 199 | * 2. ~/.local/lib/babeltrace2/plugins |
1670bffd | 200 | * 3. Default system directory for Babeltrace plugins, usually |
d9676d8c PP |
201 | * /usr/lib/babeltrace2/plugins or |
202 | * /usr/local/lib/babeltrace2/plugins if installed | |
1670bffd PP |
203 | * locally |
204 | * 4. Built-in plugins (static) | |
205 | * | |
206 | * Directories are searched non-recursively. | |
207 | */ | |
208 | envvar = getenv("BABELTRACE_PLUGIN_PATH"); | |
209 | if (envvar) { | |
210 | ret = bt_common_append_plugin_path_dirs(envvar, dirs); | |
211 | if (ret) { | |
8c1a3187 | 212 | BT_LOGE_STR("Failed to append plugin path to array of directories."); |
1670bffd PP |
213 | goto end; |
214 | } | |
215 | } | |
216 | ||
217 | home_plugin_dir = bt_common_get_home_plugin_path(); | |
218 | if (home_plugin_dir) { | |
219 | GString *home_plugin_dir_str = | |
220 | g_string_new(home_plugin_dir); | |
221 | ||
222 | if (!home_plugin_dir_str) { | |
8c1a3187 | 223 | BT_LOGE_STR("Failed to allocate a GString."); |
1670bffd PP |
224 | goto end; |
225 | } | |
226 | ||
227 | g_ptr_array_add(dirs, home_plugin_dir_str); | |
228 | } | |
229 | ||
230 | system_plugin_dir = bt_common_get_system_plugin_path(); | |
231 | if (system_plugin_dir) { | |
232 | GString *system_plugin_dir_str = | |
233 | g_string_new(system_plugin_dir); | |
234 | ||
235 | if (!system_plugin_dir_str) { | |
8c1a3187 | 236 | BT_LOGE_STR("Failed to allocate a GString."); |
1670bffd PP |
237 | goto end; |
238 | } | |
239 | ||
240 | g_ptr_array_add(dirs, system_plugin_dir_str); | |
241 | } | |
242 | ||
243 | for (i = 0; i < dirs->len; i++) { | |
244 | GString *dir = g_ptr_array_index(dirs, i); | |
245 | ||
65300d60 | 246 | BT_OBJECT_PUT_REF_AND_RESET(plugin_set); |
8c1a3187 | 247 | |
50ad9320 PP |
248 | /* |
249 | * Skip this if the directory does not exist because | |
c8db3219 | 250 | * bt_plugin_find_all_from_dir() would log a warning. |
50ad9320 PP |
251 | */ |
252 | if (!g_file_test(dir->str, G_FILE_TEST_IS_DIR)) { | |
3f7d4d90 | 253 | BT_LOGI("Skipping nonexistent directory path: " |
50ad9320 PP |
254 | "path=\"%s\"", dir->str); |
255 | continue; | |
256 | } | |
257 | ||
c8db3219 PP |
258 | /* bt_plugin_find_all_from_dir() logs details/errors */ |
259 | plugin_set = bt_plugin_find_all_from_dir(dir->str, BT_FALSE); | |
a8ff38ef | 260 | if (!plugin_set) { |
3f7d4d90 | 261 | BT_LOGI("No plugins found in directory: path=\"%s\"", |
8c1a3187 | 262 | dir->str); |
1670bffd PP |
263 | continue; |
264 | } | |
265 | ||
a8ff38ef | 266 | for (j = 0; j < plugin_set->plugins->len; j++) { |
92fed4e1 | 267 | const struct bt_plugin *candidate_plugin = |
a8ff38ef | 268 | g_ptr_array_index(plugin_set->plugins, j); |
1670bffd | 269 | |
a8ff38ef PP |
270 | if (strcmp(bt_plugin_get_name(candidate_plugin), |
271 | plugin_name) == 0) { | |
3f7d4d90 | 272 | BT_LOGI("Plugin found in directory: name=\"%s\", path=\"%s\"", |
8c1a3187 | 273 | plugin_name, dir->str); |
398454ed PP |
274 | plugin = candidate_plugin; |
275 | bt_object_get_no_null_check(plugin); | |
1670bffd PP |
276 | goto end; |
277 | } | |
1670bffd | 278 | } |
8c1a3187 | 279 | |
3f7d4d90 | 280 | BT_LOGI("Plugin not found in directory: name=\"%s\", path=\"%s\"", |
8c1a3187 | 281 | plugin_name, dir->str); |
1670bffd PP |
282 | } |
283 | ||
65300d60 | 284 | bt_object_put_ref(plugin_set); |
c8db3219 | 285 | plugin_set = bt_plugin_find_all_from_static(); |
8c1a3187 PP |
286 | if (plugin_set) { |
287 | for (j = 0; j < plugin_set->plugins->len; j++) { | |
92fed4e1 | 288 | const struct bt_plugin *candidate_plugin = |
8c1a3187 | 289 | g_ptr_array_index(plugin_set->plugins, j); |
a8ff38ef | 290 | |
8c1a3187 PP |
291 | if (strcmp(bt_plugin_get_name(candidate_plugin), |
292 | plugin_name) == 0) { | |
3f7d4d90 | 293 | BT_LOGI("Plugin found in built-in plugins: " |
8c1a3187 | 294 | "name=\"%s\"", plugin_name); |
398454ed PP |
295 | plugin = candidate_plugin; |
296 | bt_object_get_no_null_check(plugin); | |
8c1a3187 PP |
297 | goto end; |
298 | } | |
1670bffd | 299 | } |
1670bffd PP |
300 | } |
301 | ||
302 | end: | |
303 | free(home_plugin_dir); | |
65300d60 | 304 | bt_object_put_ref(plugin_set); |
1670bffd PP |
305 | |
306 | if (dirs) { | |
307 | g_ptr_array_free(dirs, TRUE); | |
308 | } | |
309 | ||
8c1a3187 | 310 | if (plugin) { |
3f7d4d90 | 311 | BT_LIB_LOGI("Found plugin in standard directories and built-in plugins: " |
d94d92ac | 312 | "%!+l", plugin); |
8c1a3187 | 313 | } else { |
3f7d4d90 | 314 | BT_LOGI("No plugin found in standard directories and built-in plugins: " |
8c1a3187 PP |
315 | "name=\"%s\"", plugin_name); |
316 | } | |
317 | ||
1670bffd PP |
318 | return plugin; |
319 | } | |
320 | ||
e1f4c4f7 MJ |
321 | static struct { |
322 | pthread_mutex_t lock; | |
323 | struct bt_plugin_set *plugin_set; | |
d94d92ac | 324 | bool recurse; |
e1f4c4f7 MJ |
325 | } append_all_from_dir_info = { |
326 | .lock = PTHREAD_MUTEX_INITIALIZER | |
327 | }; | |
328 | ||
33b34c43 | 329 | static |
e1f4c4f7 MJ |
330 | int nftw_append_all_from_dir(const char *file, const struct stat *sb, int flag, |
331 | struct FTW *s) | |
33b34c43 | 332 | { |
e1f4c4f7 MJ |
333 | int ret = 0; |
334 | const char *name = file + s->base; | |
335 | ||
336 | /* Check for recursion */ | |
337 | if (!append_all_from_dir_info.recurse && s->level > 1) { | |
338 | goto end; | |
339 | } | |
340 | ||
341 | switch (flag) { | |
342 | case FTW_F: | |
52238017 | 343 | { |
92fed4e1 | 344 | const struct bt_plugin_set *plugins_from_file; |
52238017 | 345 | |
e1f4c4f7 MJ |
346 | if (name[0] == '.') { |
347 | /* Skip hidden files */ | |
3f7d4d90 | 348 | BT_LOGI("Skipping hidden file: path=\"%s\"", file); |
e1f4c4f7 MJ |
349 | goto end; |
350 | } | |
52238017 | 351 | |
c8db3219 | 352 | plugins_from_file = bt_plugin_find_all_from_file(file); |
e1f4c4f7 MJ |
353 | |
354 | if (plugins_from_file) { | |
355 | size_t j; | |
356 | ||
357 | for (j = 0; j < plugins_from_file->plugins->len; j++) { | |
358 | struct bt_plugin *plugin = | |
359 | g_ptr_array_index(plugins_from_file->plugins, j); | |
360 | ||
3f7d4d90 | 361 | BT_LIB_LOGI("Adding plugin to plugin set: " |
d94d92ac PP |
362 | "plugin-path=\"%s\", %![plugin-]+l", |
363 | file, plugin); | |
92fed4e1 PP |
364 | bt_plugin_set_add_plugin( |
365 | append_all_from_dir_info.plugin_set, | |
366 | plugin); | |
e1f4c4f7 | 367 | } |
33b34c43 | 368 | |
65300d60 | 369 | bt_object_put_ref(plugins_from_file); |
e1f4c4f7 MJ |
370 | } |
371 | break; | |
52238017 | 372 | } |
e1f4c4f7 MJ |
373 | case FTW_DNR: |
374 | /* Continue to next file / directory. */ | |
375 | BT_LOGW("Cannot enter directory: continuing: path=\"%s\"", file); | |
376 | break; | |
377 | case FTW_NS: | |
378 | /* Continue to next file / directory. */ | |
3f7d4d90 | 379 | BT_LOGI("Cannot get file information: continuing: path=\"%s\"", file); |
e1f4c4f7 | 380 | break; |
33b34c43 | 381 | } |
e1f4c4f7 MJ |
382 | |
383 | end: | |
384 | return ret; | |
33b34c43 PP |
385 | } |
386 | ||
387 | static | |
388 | enum bt_plugin_status bt_plugin_create_append_all_from_dir( | |
a8ff38ef | 389 | struct bt_plugin_set *plugin_set, const char *path, |
c55a9f58 | 390 | bt_bool recurse) |
33b34c43 | 391 | { |
e1f4c4f7 | 392 | int nftw_flags = FTW_PHYS; |
33b34c43 PP |
393 | enum bt_plugin_status ret = BT_PLUGIN_STATUS_OK; |
394 | ||
d94d92ac PP |
395 | BT_ASSERT(plugin_set); |
396 | BT_ASSERT(path); | |
397 | BT_ASSERT(strlen(path) < PATH_MAX); | |
e1f4c4f7 | 398 | pthread_mutex_lock(&append_all_from_dir_info.lock); |
e1f4c4f7 MJ |
399 | append_all_from_dir_info.plugin_set = plugin_set; |
400 | append_all_from_dir_info.recurse = recurse; | |
401 | ret = nftw(path, nftw_append_all_from_dir, | |
402 | APPEND_ALL_FROM_DIR_NFDOPEN_MAX, nftw_flags); | |
e1f4c4f7 | 403 | pthread_mutex_unlock(&append_all_from_dir_info.lock); |
e1f4c4f7 | 404 | if (ret != 0) { |
d94d92ac | 405 | BT_LOGW_ERRNO("Cannot open directory", ": path=\"%s\"", path); |
33b34c43 | 406 | ret = BT_PLUGIN_STATUS_ERROR; |
33b34c43 | 407 | } |
8c1a3187 | 408 | |
33b34c43 PP |
409 | return ret; |
410 | } | |
411 | ||
c8db3219 | 412 | const struct bt_plugin_set *bt_plugin_find_all_from_dir(const char *path, |
c55a9f58 | 413 | bt_bool recurse) |
33b34c43 | 414 | { |
a8ff38ef | 415 | struct bt_plugin_set *plugin_set; |
33b34c43 PP |
416 | enum bt_plugin_status status; |
417 | ||
3f7d4d90 | 418 | BT_LOGI("Creating all plugins in directory: path=\"%s\", recurse=%d", |
8c1a3187 | 419 | path, recurse); |
a8ff38ef PP |
420 | plugin_set = bt_plugin_set_create(); |
421 | if (!plugin_set) { | |
8c1a3187 | 422 | BT_LOGE_STR("Cannot create empty plugin set."); |
a8ff38ef PP |
423 | goto error; |
424 | } | |
425 | ||
33b34c43 | 426 | /* Append found plugins to array */ |
a8ff38ef | 427 | status = bt_plugin_create_append_all_from_dir(plugin_set, path, |
33b34c43 PP |
428 | recurse); |
429 | if (status < 0) { | |
8c1a3187 | 430 | BT_LOGW("Cannot append plugins found in directory: " |
dbd7f7e9 PP |
431 | "path=\"%s\", status=%s", |
432 | path, bt_plugin_status_string(status)); | |
33b34c43 PP |
433 | goto error; |
434 | } | |
435 | ||
3f7d4d90 | 436 | BT_LOGI("Created %u plugins from directory: count=%u, path=\"%s\"", |
8c1a3187 | 437 | plugin_set->plugins->len, plugin_set->plugins->len, path); |
33b34c43 PP |
438 | goto end; |
439 | ||
440 | error: | |
65300d60 | 441 | BT_OBJECT_PUT_REF_AND_RESET(plugin_set); |
33b34c43 PP |
442 | |
443 | end: | |
a8ff38ef | 444 | return plugin_set; |
33b34c43 PP |
445 | } |
446 | ||
92fed4e1 | 447 | const char *bt_plugin_get_name(const struct bt_plugin *plugin) |
33b34c43 | 448 | { |
d94d92ac PP |
449 | BT_ASSERT_PRE_NON_NULL(plugin, "Plugin"); |
450 | return plugin->info.name_set ? plugin->info.name->str : NULL; | |
33b34c43 PP |
451 | } |
452 | ||
92fed4e1 | 453 | const char *bt_plugin_get_author(const struct bt_plugin *plugin) |
33b34c43 | 454 | { |
d94d92ac PP |
455 | BT_ASSERT_PRE_NON_NULL(plugin, "Plugin"); |
456 | return plugin->info.author_set ? plugin->info.author->str : NULL; | |
33b34c43 PP |
457 | } |
458 | ||
92fed4e1 | 459 | const char *bt_plugin_get_license(const struct bt_plugin *plugin) |
33b34c43 | 460 | { |
d94d92ac PP |
461 | BT_ASSERT_PRE_NON_NULL(plugin, "Plugin"); |
462 | return plugin->info.license_set ? plugin->info.license->str : NULL; | |
33b34c43 PP |
463 | } |
464 | ||
92fed4e1 | 465 | const char *bt_plugin_get_path(const struct bt_plugin *plugin) |
33b34c43 | 466 | { |
d94d92ac PP |
467 | BT_ASSERT_PRE_NON_NULL(plugin, "Plugin"); |
468 | return plugin->info.path_set ? plugin->info.path->str : NULL; | |
33b34c43 PP |
469 | } |
470 | ||
92fed4e1 | 471 | const char *bt_plugin_get_description(const struct bt_plugin *plugin) |
33b34c43 | 472 | { |
d94d92ac PP |
473 | BT_ASSERT_PRE_NON_NULL(plugin, "Plugin"); |
474 | return plugin->info.description_set ? | |
475 | plugin->info.description->str : NULL; | |
33b34c43 PP |
476 | } |
477 | ||
92fed4e1 | 478 | enum bt_property_availability bt_plugin_get_version(const struct bt_plugin *plugin, |
b6de043b PP |
479 | unsigned int *major, unsigned int *minor, unsigned int *patch, |
480 | const char **extra) | |
481 | { | |
d94d92ac PP |
482 | enum bt_property_availability avail = |
483 | BT_PROPERTY_AVAILABILITY_AVAILABLE; | |
b6de043b | 484 | |
d94d92ac | 485 | BT_ASSERT_PRE_NON_NULL(plugin, "Plugin"); |
8c1a3187 PP |
486 | |
487 | if (!plugin->info.version_set) { | |
3f7d4d90 | 488 | BT_LIB_LOGD("Plugin's version is not set: %!+l", plugin); |
d94d92ac | 489 | avail = BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE; |
b6de043b PP |
490 | goto end; |
491 | } | |
492 | ||
493 | if (major) { | |
55bb57e0 | 494 | *major = plugin->info.version.major; |
b6de043b PP |
495 | } |
496 | ||
497 | if (minor) { | |
55bb57e0 | 498 | *minor = plugin->info.version.minor; |
b6de043b PP |
499 | } |
500 | ||
501 | if (patch) { | |
55bb57e0 | 502 | *patch = plugin->info.version.patch; |
b6de043b PP |
503 | } |
504 | ||
505 | if (extra) { | |
55bb57e0 | 506 | *extra = plugin->info.version.extra->str; |
b6de043b PP |
507 | } |
508 | ||
509 | end: | |
d94d92ac | 510 | return avail; |
b6de043b PP |
511 | } |
512 | ||
92fed4e1 | 513 | uint64_t bt_plugin_get_source_component_class_count(const struct bt_plugin *plugin) |
33b34c43 | 514 | { |
d94d92ac PP |
515 | BT_ASSERT_PRE_NON_NULL(plugin, "Plugin"); |
516 | return (uint64_t) plugin->src_comp_classes->len; | |
33b34c43 PP |
517 | } |
518 | ||
92fed4e1 | 519 | uint64_t bt_plugin_get_filter_component_class_count(const struct bt_plugin *plugin) |
33b34c43 | 520 | { |
d94d92ac PP |
521 | BT_ASSERT_PRE_NON_NULL(plugin, "Plugin"); |
522 | return (uint64_t) plugin->flt_comp_classes->len; | |
523 | } | |
33b34c43 | 524 | |
92fed4e1 | 525 | uint64_t bt_plugin_get_sink_component_class_count(const struct bt_plugin *plugin) |
d94d92ac PP |
526 | { |
527 | BT_ASSERT_PRE_NON_NULL(plugin, "Plugin"); | |
528 | return (uint64_t) plugin->sink_comp_classes->len; | |
529 | } | |
8c1a3187 | 530 | |
d94d92ac PP |
531 | static inline |
532 | struct bt_component_class *borrow_component_class_by_index( | |
92fed4e1 | 533 | const struct bt_plugin *plugin, GPtrArray *comp_classes, |
d94d92ac PP |
534 | uint64_t index) |
535 | { | |
536 | BT_ASSERT_PRE_NON_NULL(plugin, "Plugin"); | |
537 | BT_ASSERT_PRE_VALID_INDEX(index, comp_classes->len); | |
538 | return g_ptr_array_index(comp_classes, index); | |
539 | } | |
33b34c43 | 540 | |
0d72b8c3 | 541 | const struct bt_component_class_source * |
3f79b5cf | 542 | bt_plugin_borrow_source_component_class_by_index_const( |
92fed4e1 | 543 | const struct bt_plugin *plugin, uint64_t index) |
d94d92ac | 544 | { |
0d72b8c3 | 545 | return (const void *) borrow_component_class_by_index(plugin, |
d94d92ac PP |
546 | plugin->src_comp_classes, index); |
547 | } | |
33b34c43 | 548 | |
0d72b8c3 | 549 | const struct bt_component_class_filter * |
92fed4e1 PP |
550 | bt_plugin_borrow_filter_component_class_by_index_const( |
551 | const struct bt_plugin *plugin, uint64_t index) | |
d94d92ac | 552 | { |
0d72b8c3 | 553 | return (const void *) borrow_component_class_by_index(plugin, |
d94d92ac PP |
554 | plugin->flt_comp_classes, index); |
555 | } | |
556 | ||
0d72b8c3 | 557 | const struct bt_component_class_sink * |
92fed4e1 PP |
558 | bt_plugin_borrow_sink_component_class_by_index_const( |
559 | const struct bt_plugin *plugin, uint64_t index) | |
d94d92ac | 560 | { |
0d72b8c3 | 561 | return (const void *) borrow_component_class_by_index(plugin, |
d94d92ac | 562 | plugin->sink_comp_classes, index); |
33b34c43 PP |
563 | } |
564 | ||
d94d92ac PP |
565 | static inline |
566 | struct bt_component_class *borrow_component_class_by_name( | |
92fed4e1 | 567 | const struct bt_plugin *plugin, GPtrArray *comp_classes, |
d94d92ac | 568 | const char *name) |
33b34c43 PP |
569 | { |
570 | struct bt_component_class *comp_class = NULL; | |
571 | size_t i; | |
572 | ||
d94d92ac PP |
573 | BT_ASSERT_PRE_NON_NULL(plugin, "Plugin"); |
574 | BT_ASSERT_PRE_NON_NULL(name, "Name"); | |
33b34c43 | 575 | |
d94d92ac | 576 | for (i = 0; i < comp_classes->len; i++) { |
33b34c43 | 577 | struct bt_component_class *comp_class_candidate = |
d94d92ac | 578 | g_ptr_array_index(comp_classes, i); |
33b34c43 PP |
579 | const char *comp_class_cand_name = |
580 | bt_component_class_get_name(comp_class_candidate); | |
33b34c43 | 581 | |
f6ccaed9 | 582 | BT_ASSERT(comp_class_cand_name); |
33b34c43 | 583 | |
d94d92ac PP |
584 | if (strcmp(name, comp_class_cand_name) == 0) { |
585 | comp_class = comp_class_candidate; | |
33b34c43 PP |
586 | break; |
587 | } | |
588 | } | |
589 | ||
33b34c43 PP |
590 | return comp_class; |
591 | } | |
592 | ||
0d72b8c3 | 593 | const struct bt_component_class_source * |
92fed4e1 PP |
594 | bt_plugin_borrow_source_component_class_by_name_const( |
595 | const struct bt_plugin *plugin, const char *name) | |
33b34c43 | 596 | { |
0d72b8c3 | 597 | return (const void *) borrow_component_class_by_name(plugin, |
d94d92ac PP |
598 | plugin->src_comp_classes, name); |
599 | } | |
33b34c43 | 600 | |
0d72b8c3 | 601 | const struct bt_component_class_filter * |
92fed4e1 PP |
602 | bt_plugin_borrow_filter_component_class_by_name_const( |
603 | const struct bt_plugin *plugin, const char *name) | |
d94d92ac | 604 | { |
0d72b8c3 | 605 | return (const void *) borrow_component_class_by_name(plugin, |
d94d92ac PP |
606 | plugin->flt_comp_classes, name); |
607 | } | |
33b34c43 | 608 | |
0d72b8c3 | 609 | const struct bt_component_class_sink * |
92fed4e1 PP |
610 | bt_plugin_borrow_sink_component_class_by_name_const( |
611 | const struct bt_plugin *plugin, const char *name) | |
d94d92ac | 612 | { |
0d72b8c3 | 613 | return (const void *) borrow_component_class_by_name(plugin, |
d94d92ac | 614 | plugin->sink_comp_classes, name); |
33b34c43 | 615 | } |
c5b9b441 PP |
616 | |
617 | void bt_plugin_get_ref(const struct bt_plugin *plugin) | |
618 | { | |
619 | bt_object_get_ref(plugin); | |
620 | } | |
621 | ||
622 | void bt_plugin_put_ref(const struct bt_plugin *plugin) | |
623 | { | |
624 | bt_object_put_ref(plugin); | |
625 | } | |
626 | ||
627 | void bt_plugin_set_get_ref(const struct bt_plugin_set *plugin_set) | |
628 | { | |
629 | bt_object_get_ref(plugin_set); | |
630 | } | |
631 | ||
632 | void bt_plugin_set_put_ref(const struct bt_plugin_set *plugin_set) | |
633 | { | |
634 | bt_object_put_ref(plugin_set); | |
635 | } |