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