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