lib: graph: add "self" and some "private" APIs
[babeltrace.git] / lib / plugin / plugin.c
1 /*
2 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
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
26 #define BT_LOG_TAG "PLUGIN"
27 #include <babeltrace/lib-logging-internal.h>
28
29 #include <babeltrace/babeltrace-internal.h>
30 #include <babeltrace/compiler-internal.h>
31 #include <babeltrace/object.h>
32 #include <babeltrace/common-internal.h>
33 #include <babeltrace/plugin/plugin-internal.h>
34 #include <babeltrace/plugin/plugin-so-internal.h>
35 #include <babeltrace/graph/component-class.h>
36 #include <babeltrace/graph/component-class-internal.h>
37 #include <babeltrace/types.h>
38 #include <babeltrace/assert-internal.h>
39 #include <babeltrace/assert-pre-internal.h>
40 #include <glib.h>
41 #include <unistd.h>
42 #include <stdlib.h>
43 #include <stdint.h>
44 #include <inttypes.h>
45 #include <sys/stat.h>
46 #include <ftw.h>
47 #include <pthread.h>
48
49 #define PYTHON_PLUGIN_PROVIDER_FILENAME "libbabeltrace-python-plugin-provider." G_MODULE_SUFFIX
50 #define PYTHON_PLUGIN_PROVIDER_SYM_NAME bt_plugin_python_create_all_from_file
51 #define PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR TOSTRING(PYTHON_PLUGIN_PROVIDER_SYM_NAME)
52
53 #define APPEND_ALL_FROM_DIR_NFDOPEN_MAX 8
54
55 #ifdef BT_BUILT_IN_PYTHON_PLUGIN_SUPPORT
56 #include <babeltrace/plugin/python-plugin-provider-internal.h>
57
58 static
59 struct bt_plugin_set *(*bt_plugin_python_create_all_from_file_sym)(const char *path) =
60 bt_plugin_python_create_all_from_file;
61
62 static
63 void init_python_plugin_provider(void) {}
64 #else /* BT_BUILT_IN_PYTHON_PLUGIN_SUPPORT */
65 static GModule *python_plugin_provider_module;
66 static
67 struct bt_plugin_set *(*bt_plugin_python_create_all_from_file_sym)(const char *path);
68
69 static
70 void init_python_plugin_provider(void) {
71 if (bt_plugin_python_create_all_from_file_sym != NULL) {
72 return;
73 }
74
75 BT_LOGD_STR("Loading Python plugin provider module.");
76 python_plugin_provider_module =
77 g_module_open(PYTHON_PLUGIN_PROVIDER_FILENAME, 0);
78 if (!python_plugin_provider_module) {
79 BT_LOGI("Cannot open `%s`: %s: continuing without Python plugin support.",
80 PYTHON_PLUGIN_PROVIDER_FILENAME, g_module_error());
81 return;
82 }
83
84 if (!g_module_symbol(python_plugin_provider_module,
85 PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR,
86 (gpointer) &bt_plugin_python_create_all_from_file_sym)) {
87 BT_LOGI("Cannot find the Python plugin provider loading symbol: continuing without Python plugin support: "
88 "file=\"%s\", symbol=\"%s\"",
89 PYTHON_PLUGIN_PROVIDER_FILENAME,
90 PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR);
91 return;
92 }
93
94 BT_LOGI("Loaded Python plugin provider module: addr=%p",
95 python_plugin_provider_module);
96 }
97
98 __attribute__((destructor)) static
99 void fini_python_plugin_provider(void) {
100 if (python_plugin_provider_module) {
101 BT_LOGD("Unloading Python plugin provider module.");
102
103 if (!g_module_close(python_plugin_provider_module)) {
104 BT_LOGE("Failed to close the Python plugin provider module: %s.",
105 g_module_error());
106 }
107
108 python_plugin_provider_module = NULL;
109 }
110 }
111 #endif
112
113 uint64_t bt_plugin_set_get_plugin_count(struct bt_plugin_set *plugin_set)
114 {
115 BT_ASSERT_PRE_NON_NULL(plugin_set, "Plugin set");
116 return (uint64_t) plugin_set->plugins->len;
117 }
118
119 struct bt_plugin *bt_plugin_set_borrow_plugin_by_index(
120 struct bt_plugin_set *plugin_set,
121 uint64_t index)
122 {
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);
126 }
127
128 struct bt_plugin_set *bt_plugin_create_all_from_static(void)
129 {
130 /* bt_plugin_so_create_all_from_static() logs errors */
131 return bt_plugin_so_create_all_from_static();
132 }
133
134 struct bt_plugin_set *bt_plugin_create_all_from_file(const char *path)
135 {
136 struct bt_plugin_set *plugin_set = NULL;
137
138 BT_ASSERT_PRE_NON_NULL(path, "Path");
139 BT_LOGD("Creating plugins from file: path=\"%s\"", path);
140
141 /* Try shared object plugins */
142 plugin_set = bt_plugin_so_create_all_from_file(path);
143 if (plugin_set) {
144 goto end;
145 }
146
147 /* Try Python plugins if support is available */
148 init_python_plugin_provider();
149 if (bt_plugin_python_create_all_from_file_sym) {
150 plugin_set = bt_plugin_python_create_all_from_file_sym(path);
151 if (plugin_set) {
152 goto end;
153 }
154 }
155
156 end:
157 if (plugin_set) {
158 BT_LOGD("Created %u plugins from file: "
159 "path=\"%s\", count=%u, plugin-set-addr=%p",
160 plugin_set->plugins->len, path,
161 plugin_set->plugins->len, plugin_set);
162 } else {
163 BT_LOGD("Found no plugins in file: path=\"%s\"", path);
164 }
165
166 return plugin_set;
167 }
168
169 static void destroy_gstring(void *data)
170 {
171 g_string_free(data, TRUE);
172 }
173
174 struct bt_plugin *bt_plugin_find(const char *plugin_name)
175 {
176 const char *system_plugin_dir;
177 char *home_plugin_dir = NULL;
178 const char *envvar;
179 struct bt_plugin *plugin = NULL;
180 struct bt_plugin_set *plugin_set = NULL;
181 GPtrArray *dirs = NULL;
182 int ret;
183 size_t i, j;
184
185 BT_ASSERT_PRE_NON_NULL(plugin_name, "Name");
186 BT_LOGD("Finding named plugin in standard directories and built-in plugins: "
187 "name=\"%s\"", plugin_name);
188 dirs = g_ptr_array_new_with_free_func((GDestroyNotify) destroy_gstring);
189 if (!dirs) {
190 BT_LOGE_STR("Failed to allocate a GPtrArray.");
191 goto end;
192 }
193
194 /*
195 * Search order is:
196 *
197 * 1. BABELTRACE_PLUGIN_PATH environment variable
198 * (colon-separated list of directories)
199 * 2. ~/.local/lib/babeltrace/plugins
200 * 3. Default system directory for Babeltrace plugins, usually
201 * /usr/lib/babeltrace/plugins or
202 * /usr/local/lib/babeltrace/plugins if installed
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) {
212 BT_LOGE_STR("Failed to append plugin path to array of directories.");
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) {
223 BT_LOGE_STR("Failed to allocate a GString.");
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) {
236 BT_LOGE_STR("Failed to allocate a GString.");
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
246 BT_OBJECT_PUT_REF_AND_RESET(plugin_set);
247
248 /*
249 * Skip this if the directory does not exist because
250 * bt_plugin_create_all_from_dir() would log a warning.
251 */
252 if (!g_file_test(dir->str, G_FILE_TEST_IS_DIR)) {
253 BT_LOGV("Skipping nonexistent directory path: "
254 "path=\"%s\"", dir->str);
255 continue;
256 }
257
258 /* bt_plugin_create_all_from_dir() logs details/errors */
259 plugin_set = bt_plugin_create_all_from_dir(dir->str, BT_FALSE);
260 if (!plugin_set) {
261 BT_LOGD("No plugins found in directory: path=\"%s\"",
262 dir->str);
263 continue;
264 }
265
266 for (j = 0; j < plugin_set->plugins->len; j++) {
267 struct bt_plugin *candidate_plugin =
268 g_ptr_array_index(plugin_set->plugins, j);
269
270 if (strcmp(bt_plugin_get_name(candidate_plugin),
271 plugin_name) == 0) {
272 BT_LOGD("Plugin found in directory: name=\"%s\", path=\"%s\"",
273 plugin_name, dir->str);
274 plugin = bt_object_get_ref(candidate_plugin);
275 goto end;
276 }
277 }
278
279 BT_LOGD("Plugin not found in directory: name=\"%s\", path=\"%s\"",
280 plugin_name, dir->str);
281 }
282
283 bt_object_put_ref(plugin_set);
284 plugin_set = bt_plugin_create_all_from_static();
285 if (plugin_set) {
286 for (j = 0; j < plugin_set->plugins->len; j++) {
287 struct bt_plugin *candidate_plugin =
288 g_ptr_array_index(plugin_set->plugins, j);
289
290 if (strcmp(bt_plugin_get_name(candidate_plugin),
291 plugin_name) == 0) {
292 BT_LOGD("Plugin found in built-in plugins: "
293 "name=\"%s\"", plugin_name);
294 plugin = bt_object_get_ref(candidate_plugin);
295 goto end;
296 }
297 }
298 }
299
300 end:
301 free(home_plugin_dir);
302 bt_object_put_ref(plugin_set);
303
304 if (dirs) {
305 g_ptr_array_free(dirs, TRUE);
306 }
307
308 if (plugin) {
309 BT_LIB_LOGD("Found plugin in standard directories and built-in plugins: "
310 "%!+l", plugin);
311 } else {
312 BT_LOGD("No plugin found in standard directories and built-in plugins: "
313 "name=\"%s\"", plugin_name);
314 }
315
316 return plugin;
317 }
318
319 static struct {
320 pthread_mutex_t lock;
321 struct bt_plugin_set *plugin_set;
322 bool recurse;
323 } append_all_from_dir_info = {
324 .lock = PTHREAD_MUTEX_INITIALIZER
325 };
326
327 static
328 int nftw_append_all_from_dir(const char *file, const struct stat *sb, int flag,
329 struct FTW *s)
330 {
331 int ret = 0;
332 const char *name = file + s->base;
333
334 /* Check for recursion */
335 if (!append_all_from_dir_info.recurse && s->level > 1) {
336 goto end;
337 }
338
339 switch (flag) {
340 case FTW_F:
341 {
342 struct bt_plugin_set *plugins_from_file;
343
344 if (name[0] == '.') {
345 /* Skip hidden files */
346 BT_LOGV("Skipping hidden file: path=\"%s\"", file);
347 goto end;
348 }
349
350 plugins_from_file = bt_plugin_create_all_from_file(file);
351
352 if (plugins_from_file) {
353 size_t j;
354
355 for (j = 0; j < plugins_from_file->plugins->len; j++) {
356 struct bt_plugin *plugin =
357 g_ptr_array_index(plugins_from_file->plugins, j);
358
359 BT_LIB_LOGD("Adding plugin to plugin set: "
360 "plugin-path=\"%s\", %![plugin-]+l",
361 file, plugin);
362 bt_plugin_set_add_plugin(append_all_from_dir_info.plugin_set, plugin);
363 }
364
365 bt_object_put_ref(plugins_from_file);
366 }
367 break;
368 }
369 case FTW_DNR:
370 /* Continue to next file / directory. */
371 BT_LOGW("Cannot enter directory: continuing: path=\"%s\"", file);
372 break;
373 case FTW_NS:
374 /* Continue to next file / directory. */
375 BT_LOGD("Cannot get file information: continuing: path=\"%s\"", file);
376 break;
377 }
378
379 end:
380 return ret;
381 }
382
383 static
384 enum bt_plugin_status bt_plugin_create_append_all_from_dir(
385 struct bt_plugin_set *plugin_set, const char *path,
386 bt_bool recurse)
387 {
388 int nftw_flags = FTW_PHYS;
389 enum bt_plugin_status ret = BT_PLUGIN_STATUS_OK;
390
391 BT_ASSERT(plugin_set);
392 BT_ASSERT(path);
393 BT_ASSERT(strlen(path) < PATH_MAX);
394 pthread_mutex_lock(&append_all_from_dir_info.lock);
395 append_all_from_dir_info.plugin_set = plugin_set;
396 append_all_from_dir_info.recurse = recurse;
397 ret = nftw(path, nftw_append_all_from_dir,
398 APPEND_ALL_FROM_DIR_NFDOPEN_MAX, nftw_flags);
399 pthread_mutex_unlock(&append_all_from_dir_info.lock);
400 if (ret != 0) {
401 BT_LOGW_ERRNO("Cannot open directory", ": path=\"%s\"", path);
402 ret = BT_PLUGIN_STATUS_ERROR;
403 }
404
405 return ret;
406 }
407
408 struct bt_plugin_set *bt_plugin_create_all_from_dir(const char *path,
409 bt_bool recurse)
410 {
411 struct bt_plugin_set *plugin_set;
412 enum bt_plugin_status status;
413
414 BT_LOGD("Creating all plugins in directory: path=\"%s\", recurse=%d",
415 path, recurse);
416 plugin_set = bt_plugin_set_create();
417 if (!plugin_set) {
418 BT_LOGE_STR("Cannot create empty plugin set.");
419 goto error;
420 }
421
422 /* Append found plugins to array */
423 status = bt_plugin_create_append_all_from_dir(plugin_set, path,
424 recurse);
425 if (status < 0) {
426 BT_LOGW("Cannot append plugins found in directory: "
427 "path=\"%s\", status=%s",
428 path, bt_plugin_status_string(status));
429 goto error;
430 }
431
432 BT_LOGD("Created %u plugins from directory: count=%u, path=\"%s\"",
433 plugin_set->plugins->len, plugin_set->plugins->len, path);
434 goto end;
435
436 error:
437 BT_OBJECT_PUT_REF_AND_RESET(plugin_set);
438
439 end:
440 return plugin_set;
441 }
442
443 const char *bt_plugin_get_name(struct bt_plugin *plugin)
444 {
445 BT_ASSERT_PRE_NON_NULL(plugin, "Plugin");
446 return plugin->info.name_set ? plugin->info.name->str : NULL;
447 }
448
449 const char *bt_plugin_get_author(struct bt_plugin *plugin)
450 {
451 BT_ASSERT_PRE_NON_NULL(plugin, "Plugin");
452 return plugin->info.author_set ? plugin->info.author->str : NULL;
453 }
454
455 const char *bt_plugin_get_license(struct bt_plugin *plugin)
456 {
457 BT_ASSERT_PRE_NON_NULL(plugin, "Plugin");
458 return plugin->info.license_set ? plugin->info.license->str : NULL;
459 }
460
461 const char *bt_plugin_get_path(struct bt_plugin *plugin)
462 {
463 BT_ASSERT_PRE_NON_NULL(plugin, "Plugin");
464 return plugin->info.path_set ? plugin->info.path->str : NULL;
465 }
466
467 const char *bt_plugin_get_description(struct bt_plugin *plugin)
468 {
469 BT_ASSERT_PRE_NON_NULL(plugin, "Plugin");
470 return plugin->info.description_set ?
471 plugin->info.description->str : NULL;
472 }
473
474 enum bt_property_availability bt_plugin_get_version(struct bt_plugin *plugin,
475 unsigned int *major, unsigned int *minor, unsigned int *patch,
476 const char **extra)
477 {
478 enum bt_property_availability avail =
479 BT_PROPERTY_AVAILABILITY_AVAILABLE;
480
481 BT_ASSERT_PRE_NON_NULL(plugin, "Plugin");
482
483 if (!plugin->info.version_set) {
484 BT_LIB_LOGV("Plugin's version is not set: %!+l", plugin);
485 avail = BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE;
486 goto end;
487 }
488
489 if (major) {
490 *major = plugin->info.version.major;
491 }
492
493 if (minor) {
494 *minor = plugin->info.version.minor;
495 }
496
497 if (patch) {
498 *patch = plugin->info.version.patch;
499 }
500
501 if (extra) {
502 *extra = plugin->info.version.extra->str;
503 }
504
505 end:
506 return avail;
507 }
508
509 uint64_t bt_plugin_get_source_component_class_count(struct bt_plugin *plugin)
510 {
511 BT_ASSERT_PRE_NON_NULL(plugin, "Plugin");
512 return (uint64_t) plugin->src_comp_classes->len;
513 }
514
515 uint64_t bt_plugin_get_filter_component_class_count(struct bt_plugin *plugin)
516 {
517 BT_ASSERT_PRE_NON_NULL(plugin, "Plugin");
518 return (uint64_t) plugin->flt_comp_classes->len;
519 }
520
521 uint64_t bt_plugin_get_sink_component_class_count(struct bt_plugin *plugin)
522 {
523 BT_ASSERT_PRE_NON_NULL(plugin, "Plugin");
524 return (uint64_t) plugin->sink_comp_classes->len;
525 }
526
527 static inline
528 struct bt_component_class *borrow_component_class_by_index(
529 struct bt_plugin *plugin, GPtrArray *comp_classes,
530 uint64_t index)
531 {
532 BT_ASSERT_PRE_NON_NULL(plugin, "Plugin");
533 BT_ASSERT_PRE_VALID_INDEX(index, comp_classes->len);
534 return g_ptr_array_index(comp_classes, index);
535 }
536
537
538 struct bt_component_class_source *
539 bt_plugin_borrow_source_component_class_by_index(
540 struct bt_plugin *plugin, uint64_t index)
541 {
542 return (void *) borrow_component_class_by_index(plugin,
543 plugin->src_comp_classes, index);
544 }
545
546 struct bt_component_class_filter *
547 bt_plugin_borrow_filter_component_class_by_index(
548 struct bt_plugin *plugin, uint64_t index)
549 {
550 return (void *) borrow_component_class_by_index(plugin,
551 plugin->flt_comp_classes, index);
552 }
553
554 struct bt_component_class_sink *
555 bt_plugin_borrow_sink_component_class_by_index(
556 struct bt_plugin *plugin, uint64_t index)
557 {
558 return (void *) borrow_component_class_by_index(plugin,
559 plugin->sink_comp_classes, index);
560 }
561
562 static inline
563 struct bt_component_class *borrow_component_class_by_name(
564 struct bt_plugin *plugin, GPtrArray *comp_classes,
565 const char *name)
566 {
567 struct bt_component_class *comp_class = NULL;
568 size_t i;
569
570 BT_ASSERT_PRE_NON_NULL(plugin, "Plugin");
571 BT_ASSERT_PRE_NON_NULL(name, "Name");
572
573 for (i = 0; i < comp_classes->len; i++) {
574 struct bt_component_class *comp_class_candidate =
575 g_ptr_array_index(comp_classes, i);
576 const char *comp_class_cand_name =
577 bt_component_class_get_name(comp_class_candidate);
578
579 BT_ASSERT(comp_class_cand_name);
580
581 if (strcmp(name, comp_class_cand_name) == 0) {
582 comp_class = comp_class_candidate;
583 break;
584 }
585 }
586
587 return comp_class;
588 }
589
590 struct bt_component_class_source *
591 bt_plugin_borrow_source_component_class_by_name(struct bt_plugin *plugin,
592 const char *name)
593 {
594 return (void *) borrow_component_class_by_name(plugin,
595 plugin->src_comp_classes, name);
596 }
597
598 struct bt_component_class_filter *
599 bt_plugin_borrow_filter_component_class_by_name(struct bt_plugin *plugin,
600 const char *name)
601 {
602 return (void *) borrow_component_class_by_name(plugin,
603 plugin->flt_comp_classes, name);
604 }
605
606 struct bt_component_class_sink *
607 bt_plugin_borrow_sink_component_class_by_name(struct bt_plugin *plugin,
608 const char *name)
609 {
610 return (void *) borrow_component_class_by_name(plugin,
611 plugin->sink_comp_classes, name);
612 }
This page took 0.043766 seconds and 4 git commands to generate.