Commit | Line | Data |
---|---|---|
33b34c43 PP |
1 | /* |
2 | * plugin.c | |
3 | * | |
55bb57e0 | 4 | * Babeltrace Plugin (generic) |
33b34c43 PP |
5 | * |
6 | * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
7 | * Copyright 2017 Philippe Proulx <pproulx@efficios.com> | |
8 | * | |
9 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
10 | * | |
11 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
12 | * of this software and associated documentation files (the "Software"), to deal | |
13 | * in the Software without restriction, including without limitation the rights | |
14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
15 | * copies of the Software, and to permit persons to whom the Software is | |
16 | * furnished to do so, subject to the following conditions: | |
17 | * | |
18 | * The above copyright notice and this permission notice shall be included in | |
19 | * all copies or substantial portions of the Software. | |
20 | * | |
21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
27 | * SOFTWARE. | |
28 | */ | |
29 | ||
6fbd4105 | 30 | #include <babeltrace/babeltrace-internal.h> |
3d9990ac | 31 | #include <babeltrace/compiler-internal.h> |
33b34c43 | 32 | #include <babeltrace/ref.h> |
1670bffd | 33 | #include <babeltrace/common-internal.h> |
33b34c43 | 34 | #include <babeltrace/plugin/plugin-internal.h> |
55bb57e0 | 35 | #include <babeltrace/plugin/plugin-so-internal.h> |
33b34c43 | 36 | #include <glib.h> |
33b34c43 PP |
37 | #include <unistd.h> |
38 | #include <stdlib.h> | |
9ac68eb1 | 39 | #include <stdint.h> |
33b34c43 PP |
40 | #include <sys/stat.h> |
41 | #include <dirent.h> | |
42 | ||
6fbd4105 PP |
43 | #define PYTHON_PLUGIN_PROVIDER_FILENAME "libbabeltrace-python-plugin-provider." G_MODULE_SUFFIX |
44 | #define PYTHON_PLUGIN_PROVIDER_SYM_NAME bt_plugin_python_create_all_from_file | |
45 | #define PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR TOSTRING(PYTHON_PLUGIN_PROVIDER_SYM_NAME) | |
33b34c43 | 46 | |
6fbd4105 PP |
47 | #ifdef BT_BUILT_IN_PYTHON_PLUGIN_SUPPORT |
48 | #include <babeltrace/plugin/python-plugin-provider-internal.h> | |
33b34c43 | 49 | static |
a8ff38ef | 50 | struct bt_plugin_set *(*bt_plugin_python_create_all_from_file_sym)(const char *path) = |
6fbd4105 PP |
51 | bt_plugin_python_create_all_from_file; |
52 | #else /* BT_BUILT_IN_PYTHON_PLUGIN_SUPPORT */ | |
53 | static GModule *python_plugin_provider_module; | |
54 | static | |
a8ff38ef | 55 | struct bt_plugin_set *(*bt_plugin_python_create_all_from_file_sym)(const char *path); |
33b34c43 | 56 | |
6fbd4105 PP |
57 | __attribute__((constructor)) static |
58 | void init_python_plugin_provider(void) { | |
59 | python_plugin_provider_module = | |
60 | g_module_open(PYTHON_PLUGIN_PROVIDER_FILENAME, | |
61 | G_MODULE_BIND_LOCAL); | |
62 | if (!python_plugin_provider_module) { | |
63 | printf_warning("Cannot find `%s`: Python plugin support is disabled\n", | |
64 | PYTHON_PLUGIN_PROVIDER_FILENAME); | |
65 | return; | |
33b34c43 PP |
66 | } |
67 | ||
6fbd4105 PP |
68 | if (!g_module_symbol(python_plugin_provider_module, |
69 | PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR, | |
70 | (gpointer) &bt_plugin_python_create_all_from_file_sym)) { | |
71 | printf_warning("Cannot find the Python plugin provider loading symbole: Python plugin support is disabled\n"); | |
33b34c43 | 72 | } |
33b34c43 PP |
73 | } |
74 | ||
6fbd4105 PP |
75 | __attribute__((destructor)) static |
76 | void fini_python_plugin_provider(void) { | |
77 | if (python_plugin_provider_module) { | |
78 | (void) g_module_close(python_plugin_provider_module); | |
79 | python_plugin_provider_module = NULL; | |
6ba0b073 | 80 | } |
6ba0b073 | 81 | } |
6fbd4105 | 82 | #endif |
6ba0b073 | 83 | |
a8ff38ef | 84 | extern |
544d0515 | 85 | int64_t bt_plugin_set_get_plugin_count(struct bt_plugin_set *plugin_set) |
a8ff38ef | 86 | { |
544d0515 | 87 | int64_t count = -1; |
a8ff38ef PP |
88 | |
89 | if (!plugin_set) { | |
90 | goto end; | |
91 | } | |
92 | ||
9ac68eb1 | 93 | count = (int64_t) plugin_set->plugins->len; |
a8ff38ef PP |
94 | |
95 | end: | |
96 | return count; | |
97 | } | |
98 | ||
99 | extern | |
100 | struct bt_plugin *bt_plugin_set_get_plugin(struct bt_plugin_set *plugin_set, | |
9ac68eb1 | 101 | uint64_t index) |
a8ff38ef PP |
102 | { |
103 | struct bt_plugin *plugin = NULL; | |
104 | ||
105 | if (!plugin_set || index >= plugin_set->plugins->len) { | |
106 | goto end; | |
107 | } | |
108 | ||
109 | plugin = bt_get(g_ptr_array_index(plugin_set->plugins, index)); | |
110 | ||
111 | end: | |
112 | return plugin; | |
113 | } | |
114 | ||
115 | struct bt_plugin_set *bt_plugin_create_all_from_static(void) | |
6ba0b073 | 116 | { |
55bb57e0 | 117 | return bt_plugin_so_create_all_from_static(); |
6ba0b073 PP |
118 | } |
119 | ||
a8ff38ef | 120 | struct bt_plugin_set *bt_plugin_create_all_from_file(const char *path) |
33b34c43 | 121 | { |
a8ff38ef | 122 | struct bt_plugin_set *plugin_set = NULL; |
33b34c43 PP |
123 | |
124 | if (!path) { | |
6ba0b073 | 125 | goto end; |
33b34c43 PP |
126 | } |
127 | ||
55bb57e0 | 128 | printf_verbose("Trying to load plugins from `%s`\n", path); |
6ba0b073 | 129 | |
55bb57e0 | 130 | /* Try shared object plugins */ |
a8ff38ef PP |
131 | plugin_set = bt_plugin_so_create_all_from_file(path); |
132 | if (plugin_set) { | |
6ba0b073 PP |
133 | goto end; |
134 | } | |
135 | ||
6fbd4105 PP |
136 | /* Try Python plugins if support is available */ |
137 | if (bt_plugin_python_create_all_from_file_sym) { | |
a8ff38ef PP |
138 | plugin_set = bt_plugin_python_create_all_from_file_sym(path); |
139 | if (plugin_set) { | |
6fbd4105 PP |
140 | goto end; |
141 | } | |
6ba0b073 PP |
142 | } |
143 | ||
33b34c43 | 144 | end: |
a8ff38ef | 145 | return plugin_set; |
33b34c43 PP |
146 | } |
147 | ||
1670bffd PP |
148 | static void destroy_gstring(void *data) |
149 | { | |
150 | g_string_free(data, TRUE); | |
151 | } | |
152 | ||
2b43acf9 | 153 | struct bt_plugin *bt_plugin_find(const char *plugin_name) |
1670bffd PP |
154 | { |
155 | const char *system_plugin_dir; | |
156 | char *home_plugin_dir = NULL; | |
157 | const char *envvar; | |
158 | struct bt_plugin *plugin = NULL; | |
a8ff38ef | 159 | struct bt_plugin_set *plugin_set = NULL; |
1670bffd PP |
160 | GPtrArray *dirs = NULL; |
161 | int ret; | |
a8ff38ef | 162 | size_t i, j; |
1670bffd PP |
163 | |
164 | if (!plugin_name) { | |
165 | goto end; | |
166 | } | |
167 | ||
168 | dirs = g_ptr_array_new_with_free_func((GDestroyNotify) destroy_gstring); | |
169 | if (!dirs) { | |
170 | goto end; | |
171 | } | |
172 | ||
173 | /* | |
174 | * Search order is: | |
175 | * | |
176 | * 1. BABELTRACE_PLUGIN_PATH environment variable | |
177 | * (colon-separated list of directories) | |
178 | * 2. ~/.local/lib/babeltrace/plugins | |
179 | * 3. Default system directory for Babeltrace plugins, usually | |
180 | * /usr/lib/babeltrace/plugins or | |
181 | * /usr/local/lib/babeltrace/plugins if installed | |
182 | * locally | |
183 | * 4. Built-in plugins (static) | |
184 | * | |
185 | * Directories are searched non-recursively. | |
186 | */ | |
187 | envvar = getenv("BABELTRACE_PLUGIN_PATH"); | |
188 | if (envvar) { | |
189 | ret = bt_common_append_plugin_path_dirs(envvar, dirs); | |
190 | if (ret) { | |
191 | goto end; | |
192 | } | |
193 | } | |
194 | ||
195 | home_plugin_dir = bt_common_get_home_plugin_path(); | |
196 | if (home_plugin_dir) { | |
197 | GString *home_plugin_dir_str = | |
198 | g_string_new(home_plugin_dir); | |
199 | ||
200 | if (!home_plugin_dir_str) { | |
201 | goto end; | |
202 | } | |
203 | ||
204 | g_ptr_array_add(dirs, home_plugin_dir_str); | |
205 | } | |
206 | ||
207 | system_plugin_dir = bt_common_get_system_plugin_path(); | |
208 | if (system_plugin_dir) { | |
209 | GString *system_plugin_dir_str = | |
210 | g_string_new(system_plugin_dir); | |
211 | ||
212 | if (!system_plugin_dir_str) { | |
213 | goto end; | |
214 | } | |
215 | ||
216 | g_ptr_array_add(dirs, system_plugin_dir_str); | |
217 | } | |
218 | ||
219 | for (i = 0; i < dirs->len; i++) { | |
220 | GString *dir = g_ptr_array_index(dirs, i); | |
221 | ||
222 | printf_verbose("Trying to load plugins from directory `%s`\n", | |
223 | dir->str); | |
a8ff38ef PP |
224 | BT_PUT(plugin_set); |
225 | plugin_set = bt_plugin_create_all_from_dir(dir->str, false); | |
226 | if (!plugin_set) { | |
1670bffd PP |
227 | continue; |
228 | } | |
229 | ||
a8ff38ef PP |
230 | for (j = 0; j < plugin_set->plugins->len; j++) { |
231 | struct bt_plugin *candidate_plugin = | |
232 | g_ptr_array_index(plugin_set->plugins, j); | |
1670bffd | 233 | |
a8ff38ef PP |
234 | if (strcmp(bt_plugin_get_name(candidate_plugin), |
235 | plugin_name) == 0) { | |
236 | plugin = bt_get(candidate_plugin); | |
1670bffd PP |
237 | goto end; |
238 | } | |
1670bffd PP |
239 | } |
240 | } | |
241 | ||
a8ff38ef PP |
242 | bt_put(plugin_set); |
243 | plugin_set = bt_plugin_create_all_from_static(); | |
244 | ||
245 | for (j = 0; j < plugin_set->plugins->len; j++) { | |
246 | struct bt_plugin *candidate_plugin = | |
247 | g_ptr_array_index(plugin_set->plugins, j); | |
1670bffd | 248 | |
a8ff38ef PP |
249 | if (strcmp(bt_plugin_get_name(candidate_plugin), |
250 | plugin_name) == 0) { | |
251 | plugin = bt_get(candidate_plugin); | |
1670bffd PP |
252 | goto end; |
253 | } | |
1670bffd PP |
254 | } |
255 | ||
256 | end: | |
257 | free(home_plugin_dir); | |
a8ff38ef | 258 | bt_put(plugin_set); |
1670bffd PP |
259 | |
260 | if (dirs) { | |
261 | g_ptr_array_free(dirs, TRUE); | |
262 | } | |
263 | ||
264 | return plugin; | |
265 | } | |
266 | ||
2b43acf9 PP |
267 | struct bt_component_class *bt_plugin_find_component_class( |
268 | const char *plugin_name, const char *comp_cls_name, | |
269 | enum bt_component_class_type comp_cls_type) | |
270 | { | |
271 | struct bt_plugin *plugin = NULL; | |
272 | struct bt_component_class *comp_cls = NULL; | |
273 | ||
274 | if (!plugin_name || !comp_cls_name) { | |
275 | goto end; | |
276 | } | |
277 | ||
278 | plugin = bt_plugin_find(plugin_name); | |
279 | if (!plugin) { | |
280 | goto end; | |
281 | } | |
282 | ||
283 | comp_cls = bt_plugin_get_component_class_by_name_and_type( | |
284 | plugin, comp_cls_name, comp_cls_type); | |
285 | ||
286 | end: | |
287 | bt_put(plugin); | |
288 | return comp_cls; | |
289 | } | |
290 | ||
33b34c43 PP |
291 | /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */ |
292 | static | |
293 | struct dirent *alloc_dirent(const char *path) | |
294 | { | |
295 | size_t len; | |
296 | long name_max; | |
297 | struct dirent *entry; | |
298 | ||
299 | name_max = pathconf(path, _PC_NAME_MAX); | |
300 | if (name_max == -1) { | |
301 | name_max = PATH_MAX; | |
302 | } | |
303 | len = offsetof(struct dirent, d_name) + name_max + 1; | |
304 | entry = zmalloc(len); | |
305 | return entry; | |
306 | } | |
307 | ||
308 | static | |
309 | enum bt_plugin_status bt_plugin_create_append_all_from_dir( | |
a8ff38ef PP |
310 | struct bt_plugin_set *plugin_set, const char *path, |
311 | bool recurse) | |
33b34c43 PP |
312 | { |
313 | DIR *directory = NULL; | |
314 | struct dirent *entry = NULL, *result = NULL; | |
315 | char *file_path = NULL; | |
6ba0b073 | 316 | size_t path_len; |
33b34c43 PP |
317 | enum bt_plugin_status ret = BT_PLUGIN_STATUS_OK; |
318 | ||
6ba0b073 PP |
319 | if (!path) { |
320 | ret = BT_PLUGIN_STATUS_ERROR; | |
321 | goto end; | |
322 | } | |
323 | ||
324 | path_len = strlen(path); | |
325 | ||
33b34c43 PP |
326 | if (path_len >= PATH_MAX) { |
327 | ret = BT_PLUGIN_STATUS_ERROR; | |
328 | goto end; | |
329 | } | |
330 | ||
331 | entry = alloc_dirent(path); | |
332 | if (!entry) { | |
333 | ret = BT_PLUGIN_STATUS_ERROR; | |
334 | goto end; | |
335 | } | |
336 | ||
337 | file_path = zmalloc(PATH_MAX); | |
338 | if (!file_path) { | |
339 | ret = BT_PLUGIN_STATUS_NOMEM; | |
340 | goto end; | |
341 | } | |
342 | ||
343 | strncpy(file_path, path, path_len); | |
344 | /* Append a trailing '/' to the path */ | |
345 | if (file_path[path_len - 1] != '/') { | |
346 | file_path[path_len++] = '/'; | |
347 | } | |
348 | ||
349 | directory = opendir(file_path); | |
350 | if (!directory) { | |
47e56665 PP |
351 | printf_verbose("Failed to open plugin directory \"%s\"\n", |
352 | file_path); | |
33b34c43 PP |
353 | ret = BT_PLUGIN_STATUS_ERROR; |
354 | goto end; | |
355 | } | |
356 | ||
357 | /* Recursively walk directory */ | |
358 | while (!readdir_r(directory, entry, &result) && result) { | |
359 | struct stat st; | |
360 | int stat_ret; | |
361 | size_t file_name_len; | |
362 | ||
363 | if (result->d_name[0] == '.') { | |
364 | /* Skip hidden files, . and .. */ | |
365 | continue; | |
366 | } | |
367 | ||
368 | file_name_len = strlen(result->d_name); | |
369 | ||
370 | if (path_len + file_name_len >= PATH_MAX) { | |
371 | continue; | |
372 | } | |
373 | ||
374 | strncpy(file_path + path_len, result->d_name, file_name_len); | |
375 | file_path[path_len + file_name_len] = '\0'; | |
33b34c43 PP |
376 | stat_ret = stat(file_path, &st); |
377 | if (stat_ret < 0) { | |
378 | /* Continue to next file / directory. */ | |
379 | printf_perror("Failed to stat() plugin file\n"); | |
380 | continue; | |
381 | } | |
382 | ||
383 | if (S_ISDIR(st.st_mode) && recurse) { | |
a8ff38ef | 384 | ret = bt_plugin_create_append_all_from_dir(plugin_set, |
33b34c43 PP |
385 | file_path, true); |
386 | if (ret < 0) { | |
387 | goto end; | |
388 | } | |
389 | } else if (S_ISREG(st.st_mode)) { | |
a8ff38ef | 390 | struct bt_plugin_set *plugins_from_file = |
6ba0b073 | 391 | bt_plugin_create_all_from_file(file_path); |
33b34c43 | 392 | |
6ba0b073 | 393 | if (plugins_from_file) { |
a8ff38ef PP |
394 | size_t j; |
395 | ||
396 | for (j = 0; j < plugins_from_file->plugins->len; j++) { | |
397 | struct bt_plugin *plugin = | |
398 | g_ptr_array_index(plugins_from_file->plugins, j); | |
6ba0b073 | 399 | |
a8ff38ef PP |
400 | bt_plugin_set_add_plugin(plugin_set, |
401 | plugin); | |
6ba0b073 PP |
402 | } |
403 | ||
a8ff38ef | 404 | bt_put(plugins_from_file); |
33b34c43 PP |
405 | } |
406 | } | |
407 | } | |
408 | end: | |
409 | if (directory) { | |
410 | if (closedir(directory)) { | |
411 | /* | |
412 | * We don't want to override the error since there is | |
413 | * nothing could do. | |
414 | */ | |
415 | perror("Failed to close plug-in directory"); | |
416 | } | |
417 | } | |
418 | free(entry); | |
419 | free(file_path); | |
420 | return ret; | |
421 | } | |
422 | ||
a8ff38ef | 423 | struct bt_plugin_set *bt_plugin_create_all_from_dir(const char *path, |
33b34c43 PP |
424 | bool recurse) |
425 | { | |
a8ff38ef | 426 | struct bt_plugin_set *plugin_set; |
33b34c43 PP |
427 | enum bt_plugin_status status; |
428 | ||
a8ff38ef PP |
429 | plugin_set = bt_plugin_set_create(); |
430 | if (!plugin_set) { | |
431 | goto error; | |
432 | } | |
433 | ||
33b34c43 | 434 | /* Append found plugins to array */ |
a8ff38ef | 435 | status = bt_plugin_create_append_all_from_dir(plugin_set, path, |
33b34c43 PP |
436 | recurse); |
437 | if (status < 0) { | |
438 | goto error; | |
439 | } | |
440 | ||
33b34c43 PP |
441 | goto end; |
442 | ||
443 | error: | |
a8ff38ef | 444 | BT_PUT(plugin_set); |
33b34c43 PP |
445 | |
446 | end: | |
a8ff38ef | 447 | return plugin_set; |
33b34c43 PP |
448 | } |
449 | ||
33b34c43 PP |
450 | const char *bt_plugin_get_name(struct bt_plugin *plugin) |
451 | { | |
55bb57e0 | 452 | return plugin && plugin->info.name_set ? plugin->info.name->str : NULL; |
33b34c43 PP |
453 | } |
454 | ||
455 | const char *bt_plugin_get_author(struct bt_plugin *plugin) | |
456 | { | |
55bb57e0 | 457 | return plugin && plugin->info.author_set ? plugin->info.author->str : NULL; |
33b34c43 PP |
458 | } |
459 | ||
460 | const char *bt_plugin_get_license(struct bt_plugin *plugin) | |
461 | { | |
55bb57e0 | 462 | return plugin && plugin->info.license_set ? plugin->info.license->str : NULL; |
33b34c43 PP |
463 | } |
464 | ||
465 | const char *bt_plugin_get_path(struct bt_plugin *plugin) | |
466 | { | |
55bb57e0 | 467 | return plugin && plugin->info.path_set ? plugin->info.path->str : NULL; |
33b34c43 PP |
468 | } |
469 | ||
470 | const char *bt_plugin_get_description(struct bt_plugin *plugin) | |
471 | { | |
55bb57e0 | 472 | return plugin && plugin->info.description_set ? plugin->info.description->str : NULL; |
33b34c43 PP |
473 | } |
474 | ||
b6de043b PP |
475 | enum bt_plugin_status bt_plugin_get_version(struct bt_plugin *plugin, |
476 | unsigned int *major, unsigned int *minor, unsigned int *patch, | |
477 | const char **extra) | |
478 | { | |
479 | enum bt_plugin_status status = BT_PLUGIN_STATUS_OK; | |
480 | ||
55bb57e0 | 481 | if (!plugin || !plugin->info.version_set) { |
b6de043b PP |
482 | status = BT_PLUGIN_STATUS_ERROR; |
483 | goto end; | |
484 | } | |
485 | ||
486 | if (major) { | |
55bb57e0 | 487 | *major = plugin->info.version.major; |
b6de043b PP |
488 | } |
489 | ||
490 | if (minor) { | |
55bb57e0 | 491 | *minor = plugin->info.version.minor; |
b6de043b PP |
492 | } |
493 | ||
494 | if (patch) { | |
55bb57e0 | 495 | *patch = plugin->info.version.patch; |
b6de043b PP |
496 | } |
497 | ||
498 | if (extra) { | |
55bb57e0 | 499 | *extra = plugin->info.version.extra->str; |
b6de043b PP |
500 | } |
501 | ||
502 | end: | |
503 | return status; | |
504 | } | |
505 | ||
544d0515 | 506 | int64_t bt_plugin_get_component_class_count(struct bt_plugin *plugin) |
33b34c43 | 507 | { |
9ac68eb1 | 508 | return plugin ? plugin->comp_classes->len : (int64_t) -1; |
33b34c43 PP |
509 | } |
510 | ||
9ac68eb1 PP |
511 | struct bt_component_class *bt_plugin_get_component_class_by_index( |
512 | struct bt_plugin *plugin, uint64_t index) | |
33b34c43 PP |
513 | { |
514 | struct bt_component_class *comp_class = NULL; | |
515 | ||
516 | if (!plugin || index >= plugin->comp_classes->len) { | |
517 | goto error; | |
518 | } | |
519 | ||
520 | comp_class = g_ptr_array_index(plugin->comp_classes, index); | |
521 | bt_get(comp_class); | |
522 | goto end; | |
523 | ||
524 | error: | |
525 | BT_PUT(comp_class); | |
526 | ||
527 | end: | |
528 | return comp_class; | |
529 | } | |
530 | ||
531 | struct bt_component_class *bt_plugin_get_component_class_by_name_and_type( | |
532 | struct bt_plugin *plugin, const char *name, | |
d3e4dcd8 | 533 | enum bt_component_class_type type) |
33b34c43 PP |
534 | { |
535 | struct bt_component_class *comp_class = NULL; | |
536 | size_t i; | |
537 | ||
538 | if (!plugin || !name) { | |
539 | goto error; | |
540 | } | |
541 | ||
542 | for (i = 0; i < plugin->comp_classes->len; i++) { | |
543 | struct bt_component_class *comp_class_candidate = | |
544 | g_ptr_array_index(plugin->comp_classes, i); | |
545 | const char *comp_class_cand_name = | |
546 | bt_component_class_get_name(comp_class_candidate); | |
d3e4dcd8 | 547 | enum bt_component_class_type comp_class_cand_type = |
33b34c43 PP |
548 | bt_component_class_get_type(comp_class_candidate); |
549 | ||
550 | assert(comp_class_cand_name); | |
551 | assert(comp_class_cand_type >= 0); | |
552 | ||
553 | if (strcmp(name, comp_class_cand_name) == 0 && | |
554 | comp_class_cand_type == type) { | |
555 | comp_class = bt_get(comp_class_candidate); | |
556 | break; | |
557 | } | |
558 | } | |
559 | ||
560 | goto end; | |
561 | ||
562 | error: | |
563 | BT_PUT(comp_class); | |
564 | ||
565 | end: | |
566 | return comp_class; | |
567 | } | |
568 | ||
33b34c43 PP |
569 | enum bt_plugin_status bt_plugin_add_component_class( |
570 | struct bt_plugin *plugin, struct bt_component_class *comp_class) | |
571 | { | |
572 | enum bt_plugin_status status = BT_PLUGIN_STATUS_OK; | |
573 | struct bt_component_class *comp_class_dup = NULL; | |
33b34c43 PP |
574 | int comp_class_index = -1; |
575 | ||
576 | if (!plugin || !comp_class || plugin->frozen) { | |
577 | goto error; | |
578 | } | |
579 | ||
580 | /* Check for duplicate */ | |
581 | comp_class_dup = bt_plugin_get_component_class_by_name_and_type(plugin, | |
582 | bt_component_class_get_name(comp_class), | |
583 | bt_component_class_get_type(comp_class)); | |
584 | if (comp_class_dup) { | |
585 | printf_verbose("Plugin `%s`: adding component class with existing name `%s` and type %d\n", | |
55bb57e0 | 586 | bt_plugin_get_name(plugin), |
33b34c43 PP |
587 | bt_component_class_get_name(comp_class), |
588 | bt_component_class_get_type(comp_class)); | |
589 | goto error; | |
590 | } | |
591 | ||
592 | /* Add new component class */ | |
593 | comp_class_index = plugin->comp_classes->len; | |
594 | g_ptr_array_add(plugin->comp_classes, bt_get(comp_class)); | |
595 | ||
55bb57e0 PP |
596 | /* Special case for a shared object plugin */ |
597 | if (plugin->type == BT_PLUGIN_TYPE_SO) { | |
3230ee6b | 598 | bt_plugin_so_on_add_component_class(plugin, comp_class); |
33b34c43 PP |
599 | } |
600 | ||
601 | goto end; | |
602 | ||
603 | error: | |
33b34c43 PP |
604 | /* Remove entry from plugin's component classes (if added) */ |
605 | if (comp_class_index >= 0) { | |
606 | g_ptr_array_remove_index(plugin->comp_classes, | |
607 | comp_class_index); | |
608 | } | |
609 | ||
610 | status = BT_PLUGIN_STATUS_ERROR; | |
611 | ||
612 | end: | |
613 | bt_put(comp_class_dup); | |
614 | return status; | |
615 | } |