Add bt_version_get_*() functions to dynamically get the lib's version
[babeltrace.git] / lib / plugin / plugin.c
CommitLineData
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>
33b34c43
PP
31#include <babeltrace/compiler.h>
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>
39#include <sys/stat.h>
40#include <dirent.h>
41
6fbd4105
PP
42#define PYTHON_PLUGIN_PROVIDER_FILENAME "libbabeltrace-python-plugin-provider." G_MODULE_SUFFIX
43#define PYTHON_PLUGIN_PROVIDER_SYM_NAME bt_plugin_python_create_all_from_file
44#define PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR TOSTRING(PYTHON_PLUGIN_PROVIDER_SYM_NAME)
33b34c43 45
6fbd4105
PP
46#ifdef BT_BUILT_IN_PYTHON_PLUGIN_SUPPORT
47#include <babeltrace/plugin/python-plugin-provider-internal.h>
33b34c43 48static
6fbd4105
PP
49struct bt_plugin **(*bt_plugin_python_create_all_from_file_sym)(const char *path) =
50 bt_plugin_python_create_all_from_file;
51#else /* BT_BUILT_IN_PYTHON_PLUGIN_SUPPORT */
52static GModule *python_plugin_provider_module;
53static
54struct bt_plugin **(*bt_plugin_python_create_all_from_file_sym)(const char *path);
33b34c43 55
6fbd4105
PP
56__attribute__((constructor)) static
57void init_python_plugin_provider(void) {
58 python_plugin_provider_module =
59 g_module_open(PYTHON_PLUGIN_PROVIDER_FILENAME,
60 G_MODULE_BIND_LOCAL);
61 if (!python_plugin_provider_module) {
62 printf_warning("Cannot find `%s`: Python plugin support is disabled\n",
63 PYTHON_PLUGIN_PROVIDER_FILENAME);
64 return;
33b34c43
PP
65 }
66
6fbd4105
PP
67 if (!g_module_symbol(python_plugin_provider_module,
68 PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR,
69 (gpointer) &bt_plugin_python_create_all_from_file_sym)) {
70 printf_warning("Cannot find the Python plugin provider loading symbole: Python plugin support is disabled\n");
33b34c43 71 }
33b34c43
PP
72}
73
6fbd4105
PP
74__attribute__((destructor)) static
75void fini_python_plugin_provider(void) {
76 if (python_plugin_provider_module) {
77 (void) g_module_close(python_plugin_provider_module);
78 python_plugin_provider_module = NULL;
6ba0b073 79 }
6ba0b073 80}
6fbd4105 81#endif
6ba0b073
PP
82
83struct bt_plugin **bt_plugin_create_all_from_static(void)
84{
55bb57e0 85 return bt_plugin_so_create_all_from_static();
6ba0b073
PP
86}
87
88struct bt_plugin **bt_plugin_create_all_from_file(const char *path)
33b34c43 89{
6ba0b073 90 struct bt_plugin **plugins = NULL;
33b34c43
PP
91
92 if (!path) {
6ba0b073 93 goto end;
33b34c43
PP
94 }
95
55bb57e0 96 printf_verbose("Trying to load plugins from `%s`\n", path);
6ba0b073 97
55bb57e0
PP
98 /* Try shared object plugins */
99 plugins = bt_plugin_so_create_all_from_file(path);
100 if (plugins) {
6ba0b073
PP
101 goto end;
102 }
103
6fbd4105
PP
104 /* Try Python plugins if support is available */
105 if (bt_plugin_python_create_all_from_file_sym) {
106 plugins = bt_plugin_python_create_all_from_file_sym(path);
107 if (plugins) {
108 goto end;
109 }
6ba0b073
PP
110 }
111
33b34c43 112end:
6ba0b073 113 return plugins;
33b34c43
PP
114}
115
1670bffd
PP
116static void destroy_gstring(void *data)
117{
118 g_string_free(data, TRUE);
119}
120
121void free_plugins(struct bt_plugin **plugins) {
122 if (plugins) {
123 struct bt_plugin **cur_plugin = plugins;
124
125 while (*cur_plugin) {
126 bt_put(*cur_plugin);
127 cur_plugin++;
128 }
129
130 free(plugins);
131 }
132}
133
134struct bt_plugin *bt_plugin_create_from_name(const char *plugin_name)
135{
136 const char *system_plugin_dir;
137 char *home_plugin_dir = NULL;
138 const char *envvar;
139 struct bt_plugin *plugin = NULL;
140 struct bt_plugin **plugins = NULL;
141 struct bt_plugin **cur_plugin;
142 GPtrArray *dirs = NULL;
143 int ret;
144 size_t i;
145
146 if (!plugin_name) {
147 goto end;
148 }
149
150 dirs = g_ptr_array_new_with_free_func((GDestroyNotify) destroy_gstring);
151 if (!dirs) {
152 goto end;
153 }
154
155 /*
156 * Search order is:
157 *
158 * 1. BABELTRACE_PLUGIN_PATH environment variable
159 * (colon-separated list of directories)
160 * 2. ~/.local/lib/babeltrace/plugins
161 * 3. Default system directory for Babeltrace plugins, usually
162 * /usr/lib/babeltrace/plugins or
163 * /usr/local/lib/babeltrace/plugins if installed
164 * locally
165 * 4. Built-in plugins (static)
166 *
167 * Directories are searched non-recursively.
168 */
169 envvar = getenv("BABELTRACE_PLUGIN_PATH");
170 if (envvar) {
171 ret = bt_common_append_plugin_path_dirs(envvar, dirs);
172 if (ret) {
173 goto end;
174 }
175 }
176
177 home_plugin_dir = bt_common_get_home_plugin_path();
178 if (home_plugin_dir) {
179 GString *home_plugin_dir_str =
180 g_string_new(home_plugin_dir);
181
182 if (!home_plugin_dir_str) {
183 goto end;
184 }
185
186 g_ptr_array_add(dirs, home_plugin_dir_str);
187 }
188
189 system_plugin_dir = bt_common_get_system_plugin_path();
190 if (system_plugin_dir) {
191 GString *system_plugin_dir_str =
192 g_string_new(system_plugin_dir);
193
194 if (!system_plugin_dir_str) {
195 goto end;
196 }
197
198 g_ptr_array_add(dirs, system_plugin_dir_str);
199 }
200
201 for (i = 0; i < dirs->len; i++) {
202 GString *dir = g_ptr_array_index(dirs, i);
203
204 printf_verbose("Trying to load plugins from directory `%s`\n",
205 dir->str);
206 free_plugins(plugins);
207 plugins = bt_plugin_create_all_from_dir(dir->str, false);
208 if (!plugins) {
209 continue;
210 }
211
212 cur_plugin = plugins;
213
214 while (*cur_plugin) {
215 if (strcmp(bt_plugin_get_name(*cur_plugin), plugin_name)
216 == 0) {
217 plugin = bt_get(*cur_plugin);
218 goto end;
219 }
220
221 cur_plugin++;
222 }
223 }
224
225 free_plugins(plugins);
226 plugins = bt_plugin_create_all_from_static();
227 cur_plugin = plugins;
228
229 while (*cur_plugin) {
230 if (strcmp(bt_plugin_get_name(*cur_plugin), plugin_name) == 0) {
231 plugin = bt_get(*cur_plugin);
232 goto end;
233 }
234
235 cur_plugin++;
236 }
237
238end:
239 free(home_plugin_dir);
240 free_plugins(plugins);
241
242 if (dirs) {
243 g_ptr_array_free(dirs, TRUE);
244 }
245
246 return plugin;
247}
248
33b34c43
PP
249/* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
250static
251struct dirent *alloc_dirent(const char *path)
252{
253 size_t len;
254 long name_max;
255 struct dirent *entry;
256
257 name_max = pathconf(path, _PC_NAME_MAX);
258 if (name_max == -1) {
259 name_max = PATH_MAX;
260 }
261 len = offsetof(struct dirent, d_name) + name_max + 1;
262 entry = zmalloc(len);
263 return entry;
264}
265
266static
267enum bt_plugin_status bt_plugin_create_append_all_from_dir(
268 GPtrArray *plugins, const char *path, bool recurse)
269{
270 DIR *directory = NULL;
271 struct dirent *entry = NULL, *result = NULL;
272 char *file_path = NULL;
6ba0b073 273 size_t path_len;
33b34c43
PP
274 enum bt_plugin_status ret = BT_PLUGIN_STATUS_OK;
275
6ba0b073
PP
276 if (!path) {
277 ret = BT_PLUGIN_STATUS_ERROR;
278 goto end;
279 }
280
281 path_len = strlen(path);
282
33b34c43
PP
283 if (path_len >= PATH_MAX) {
284 ret = BT_PLUGIN_STATUS_ERROR;
285 goto end;
286 }
287
288 entry = alloc_dirent(path);
289 if (!entry) {
290 ret = BT_PLUGIN_STATUS_ERROR;
291 goto end;
292 }
293
294 file_path = zmalloc(PATH_MAX);
295 if (!file_path) {
296 ret = BT_PLUGIN_STATUS_NOMEM;
297 goto end;
298 }
299
300 strncpy(file_path, path, path_len);
301 /* Append a trailing '/' to the path */
302 if (file_path[path_len - 1] != '/') {
303 file_path[path_len++] = '/';
304 }
305
306 directory = opendir(file_path);
307 if (!directory) {
308 perror("Failed to open plug-in directory");
309 ret = BT_PLUGIN_STATUS_ERROR;
310 goto end;
311 }
312
313 /* Recursively walk directory */
314 while (!readdir_r(directory, entry, &result) && result) {
315 struct stat st;
316 int stat_ret;
317 size_t file_name_len;
318
319 if (result->d_name[0] == '.') {
320 /* Skip hidden files, . and .. */
321 continue;
322 }
323
324 file_name_len = strlen(result->d_name);
325
326 if (path_len + file_name_len >= PATH_MAX) {
327 continue;
328 }
329
330 strncpy(file_path + path_len, result->d_name, file_name_len);
331 file_path[path_len + file_name_len] = '\0';
33b34c43
PP
332 stat_ret = stat(file_path, &st);
333 if (stat_ret < 0) {
334 /* Continue to next file / directory. */
335 printf_perror("Failed to stat() plugin file\n");
336 continue;
337 }
338
339 if (S_ISDIR(st.st_mode) && recurse) {
340 ret = bt_plugin_create_append_all_from_dir(plugins,
341 file_path, true);
342 if (ret < 0) {
343 goto end;
344 }
345 } else if (S_ISREG(st.st_mode)) {
6ba0b073
PP
346 struct bt_plugin **plugins_from_file =
347 bt_plugin_create_all_from_file(file_path);
33b34c43 348
6ba0b073
PP
349 if (plugins_from_file) {
350 struct bt_plugin **plugin;
351
352 for (plugin = plugins_from_file; *plugin; plugin++) {
353 /* Transfer ownership to array */
354 g_ptr_array_add(plugins, *plugin);
355 }
356
357 free(plugins_from_file);
33b34c43
PP
358 }
359 }
360 }
361end:
362 if (directory) {
363 if (closedir(directory)) {
364 /*
365 * We don't want to override the error since there is
366 * nothing could do.
367 */
368 perror("Failed to close plug-in directory");
369 }
370 }
371 free(entry);
372 free(file_path);
373 return ret;
374}
375
376struct bt_plugin **bt_plugin_create_all_from_dir(const char *path,
377 bool recurse)
378{
6ba0b073 379 GPtrArray *plugins_array = g_ptr_array_new();
33b34c43
PP
380 struct bt_plugin **plugins = NULL;
381 enum bt_plugin_status status;
382
33b34c43
PP
383 /* Append found plugins to array */
384 status = bt_plugin_create_append_all_from_dir(plugins_array, path,
385 recurse);
386 if (status < 0) {
387 goto error;
388 }
389
390 /* Add sentinel to array */
391 g_ptr_array_add(plugins_array, NULL);
392 plugins = (struct bt_plugin **) plugins_array->pdata;
393 goto end;
394
395error:
396 if (plugins_array) {
397 g_ptr_array_free(plugins_array, TRUE);
398 plugins_array = NULL;
399 }
400
401end:
402 if (plugins_array) {
403 g_ptr_array_free(plugins_array, FALSE);
404 }
405
406 return plugins;
407}
408
33b34c43
PP
409const char *bt_plugin_get_name(struct bt_plugin *plugin)
410{
55bb57e0 411 return plugin && plugin->info.name_set ? plugin->info.name->str : NULL;
33b34c43
PP
412}
413
414const char *bt_plugin_get_author(struct bt_plugin *plugin)
415{
55bb57e0 416 return plugin && plugin->info.author_set ? plugin->info.author->str : NULL;
33b34c43
PP
417}
418
419const char *bt_plugin_get_license(struct bt_plugin *plugin)
420{
55bb57e0 421 return plugin && plugin->info.license_set ? plugin->info.license->str : NULL;
33b34c43
PP
422}
423
424const char *bt_plugin_get_path(struct bt_plugin *plugin)
425{
55bb57e0 426 return plugin && plugin->info.path_set ? plugin->info.path->str : NULL;
33b34c43
PP
427}
428
429const char *bt_plugin_get_description(struct bt_plugin *plugin)
430{
55bb57e0 431 return plugin && plugin->info.description_set ? plugin->info.description->str : NULL;
33b34c43
PP
432}
433
b6de043b
PP
434enum bt_plugin_status bt_plugin_get_version(struct bt_plugin *plugin,
435 unsigned int *major, unsigned int *minor, unsigned int *patch,
436 const char **extra)
437{
438 enum bt_plugin_status status = BT_PLUGIN_STATUS_OK;
439
55bb57e0 440 if (!plugin || !plugin->info.version_set) {
b6de043b
PP
441 status = BT_PLUGIN_STATUS_ERROR;
442 goto end;
443 }
444
445 if (major) {
55bb57e0 446 *major = plugin->info.version.major;
b6de043b
PP
447 }
448
449 if (minor) {
55bb57e0 450 *minor = plugin->info.version.minor;
b6de043b
PP
451 }
452
453 if (patch) {
55bb57e0 454 *patch = plugin->info.version.patch;
b6de043b
PP
455 }
456
457 if (extra) {
55bb57e0 458 *extra = plugin->info.version.extra->str;
b6de043b
PP
459 }
460
461end:
462 return status;
463}
464
33b34c43
PP
465int bt_plugin_get_component_class_count(struct bt_plugin *plugin)
466{
467 return plugin ? plugin->comp_classes->len : -1;
468}
469
470struct bt_component_class *bt_plugin_get_component_class(
471 struct bt_plugin *plugin, size_t index)
472{
473 struct bt_component_class *comp_class = NULL;
474
475 if (!plugin || index >= plugin->comp_classes->len) {
476 goto error;
477 }
478
479 comp_class = g_ptr_array_index(plugin->comp_classes, index);
480 bt_get(comp_class);
481 goto end;
482
483error:
484 BT_PUT(comp_class);
485
486end:
487 return comp_class;
488}
489
490struct bt_component_class *bt_plugin_get_component_class_by_name_and_type(
491 struct bt_plugin *plugin, const char *name,
d3e4dcd8 492 enum bt_component_class_type type)
33b34c43
PP
493{
494 struct bt_component_class *comp_class = NULL;
495 size_t i;
496
497 if (!plugin || !name) {
498 goto error;
499 }
500
501 for (i = 0; i < plugin->comp_classes->len; i++) {
502 struct bt_component_class *comp_class_candidate =
503 g_ptr_array_index(plugin->comp_classes, i);
504 const char *comp_class_cand_name =
505 bt_component_class_get_name(comp_class_candidate);
d3e4dcd8 506 enum bt_component_class_type comp_class_cand_type =
33b34c43
PP
507 bt_component_class_get_type(comp_class_candidate);
508
509 assert(comp_class_cand_name);
510 assert(comp_class_cand_type >= 0);
511
512 if (strcmp(name, comp_class_cand_name) == 0 &&
513 comp_class_cand_type == type) {
514 comp_class = bt_get(comp_class_candidate);
515 break;
516 }
517 }
518
519 goto end;
520
521error:
522 BT_PUT(comp_class);
523
524end:
525 return comp_class;
526}
527
33b34c43
PP
528enum bt_plugin_status bt_plugin_add_component_class(
529 struct bt_plugin *plugin, struct bt_component_class *comp_class)
530{
531 enum bt_plugin_status status = BT_PLUGIN_STATUS_OK;
532 struct bt_component_class *comp_class_dup = NULL;
533 int ret;
534 int comp_class_index = -1;
535
536 if (!plugin || !comp_class || plugin->frozen) {
537 goto error;
538 }
539
540 /* Check for duplicate */
541 comp_class_dup = bt_plugin_get_component_class_by_name_and_type(plugin,
542 bt_component_class_get_name(comp_class),
543 bt_component_class_get_type(comp_class));
544 if (comp_class_dup) {
545 printf_verbose("Plugin `%s`: adding component class with existing name `%s` and type %d\n",
55bb57e0 546 bt_plugin_get_name(plugin),
33b34c43
PP
547 bt_component_class_get_name(comp_class),
548 bt_component_class_get_type(comp_class));
549 goto error;
550 }
551
552 /* Add new component class */
553 comp_class_index = plugin->comp_classes->len;
554 g_ptr_array_add(plugin->comp_classes, bt_get(comp_class));
555
55bb57e0
PP
556 /* Special case for a shared object plugin */
557 if (plugin->type == BT_PLUGIN_TYPE_SO) {
558 ret = bt_plugin_so_on_add_component_class(plugin, comp_class);
559 if (ret) {
560 goto error;
561 }
33b34c43
PP
562 }
563
564 goto end;
565
566error:
33b34c43
PP
567 /* Remove entry from plugin's component classes (if added) */
568 if (comp_class_index >= 0) {
569 g_ptr_array_remove_index(plugin->comp_classes,
570 comp_class_index);
571 }
572
573 status = BT_PLUGIN_STATUS_ERROR;
574
575end:
576 bt_put(comp_class_dup);
577 return status;
578}
This page took 0.04746 seconds and 4 git commands to generate.