d85604afbd89a2e15ad15a5b402dbc19bcca72b0
[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 #include <babeltrace/babeltrace-internal.h>
31 #include <babeltrace/compiler-internal.h>
32 #include <babeltrace/ref.h>
33 #include <babeltrace/common-internal.h>
34 #include <babeltrace/plugin/plugin-internal.h>
35 #include <babeltrace/plugin/plugin-so-internal.h>
36 #include <glib.h>
37 #include <unistd.h>
38 #include <stdlib.h>
39 #include <sys/stat.h>
40 #include <dirent.h>
41
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)
45
46 #ifdef BT_BUILT_IN_PYTHON_PLUGIN_SUPPORT
47 #include <babeltrace/plugin/python-plugin-provider-internal.h>
48 static
49 struct 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 */
52 static GModule *python_plugin_provider_module;
53 static
54 struct bt_plugin **(*bt_plugin_python_create_all_from_file_sym)(const char *path);
55
56 __attribute__((constructor)) static
57 void 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;
65 }
66
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");
71 }
72 }
73
74 __attribute__((destructor)) static
75 void 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;
79 }
80 }
81 #endif
82
83 struct bt_plugin **bt_plugin_create_all_from_static(void)
84 {
85 return bt_plugin_so_create_all_from_static();
86 }
87
88 struct bt_plugin **bt_plugin_create_all_from_file(const char *path)
89 {
90 struct bt_plugin **plugins = NULL;
91
92 if (!path) {
93 goto end;
94 }
95
96 printf_verbose("Trying to load plugins from `%s`\n", path);
97
98 /* Try shared object plugins */
99 plugins = bt_plugin_so_create_all_from_file(path);
100 if (plugins) {
101 goto end;
102 }
103
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 }
110 }
111
112 end:
113 return plugins;
114 }
115
116 static void destroy_gstring(void *data)
117 {
118 g_string_free(data, TRUE);
119 }
120
121 void 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
134 struct bt_plugin *bt_plugin_find(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
238 end:
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
249 struct bt_component_class *bt_plugin_find_component_class(
250 const char *plugin_name, const char *comp_cls_name,
251 enum bt_component_class_type comp_cls_type)
252 {
253 struct bt_plugin *plugin = NULL;
254 struct bt_component_class *comp_cls = NULL;
255
256 if (!plugin_name || !comp_cls_name) {
257 goto end;
258 }
259
260 plugin = bt_plugin_find(plugin_name);
261 if (!plugin) {
262 goto end;
263 }
264
265 comp_cls = bt_plugin_get_component_class_by_name_and_type(
266 plugin, comp_cls_name, comp_cls_type);
267
268 end:
269 bt_put(plugin);
270 return comp_cls;
271 }
272
273 /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
274 static
275 struct dirent *alloc_dirent(const char *path)
276 {
277 size_t len;
278 long name_max;
279 struct dirent *entry;
280
281 name_max = pathconf(path, _PC_NAME_MAX);
282 if (name_max == -1) {
283 name_max = PATH_MAX;
284 }
285 len = offsetof(struct dirent, d_name) + name_max + 1;
286 entry = zmalloc(len);
287 return entry;
288 }
289
290 static
291 enum bt_plugin_status bt_plugin_create_append_all_from_dir(
292 GPtrArray *plugins, const char *path, bool recurse)
293 {
294 DIR *directory = NULL;
295 struct dirent *entry = NULL, *result = NULL;
296 char *file_path = NULL;
297 size_t path_len;
298 enum bt_plugin_status ret = BT_PLUGIN_STATUS_OK;
299
300 if (!path) {
301 ret = BT_PLUGIN_STATUS_ERROR;
302 goto end;
303 }
304
305 path_len = strlen(path);
306
307 if (path_len >= PATH_MAX) {
308 ret = BT_PLUGIN_STATUS_ERROR;
309 goto end;
310 }
311
312 entry = alloc_dirent(path);
313 if (!entry) {
314 ret = BT_PLUGIN_STATUS_ERROR;
315 goto end;
316 }
317
318 file_path = zmalloc(PATH_MAX);
319 if (!file_path) {
320 ret = BT_PLUGIN_STATUS_NOMEM;
321 goto end;
322 }
323
324 strncpy(file_path, path, path_len);
325 /* Append a trailing '/' to the path */
326 if (file_path[path_len - 1] != '/') {
327 file_path[path_len++] = '/';
328 }
329
330 directory = opendir(file_path);
331 if (!directory) {
332 printf_verbose("Failed to open plugin directory \"%s\"\n",
333 file_path);
334 ret = BT_PLUGIN_STATUS_ERROR;
335 goto end;
336 }
337
338 /* Recursively walk directory */
339 while (!readdir_r(directory, entry, &result) && result) {
340 struct stat st;
341 int stat_ret;
342 size_t file_name_len;
343
344 if (result->d_name[0] == '.') {
345 /* Skip hidden files, . and .. */
346 continue;
347 }
348
349 file_name_len = strlen(result->d_name);
350
351 if (path_len + file_name_len >= PATH_MAX) {
352 continue;
353 }
354
355 strncpy(file_path + path_len, result->d_name, file_name_len);
356 file_path[path_len + file_name_len] = '\0';
357 stat_ret = stat(file_path, &st);
358 if (stat_ret < 0) {
359 /* Continue to next file / directory. */
360 printf_perror("Failed to stat() plugin file\n");
361 continue;
362 }
363
364 if (S_ISDIR(st.st_mode) && recurse) {
365 ret = bt_plugin_create_append_all_from_dir(plugins,
366 file_path, true);
367 if (ret < 0) {
368 goto end;
369 }
370 } else if (S_ISREG(st.st_mode)) {
371 struct bt_plugin **plugins_from_file =
372 bt_plugin_create_all_from_file(file_path);
373
374 if (plugins_from_file) {
375 struct bt_plugin **plugin;
376
377 for (plugin = plugins_from_file; *plugin; plugin++) {
378 /* Transfer ownership to array */
379 g_ptr_array_add(plugins, *plugin);
380 }
381
382 free(plugins_from_file);
383 }
384 }
385 }
386 end:
387 if (directory) {
388 if (closedir(directory)) {
389 /*
390 * We don't want to override the error since there is
391 * nothing could do.
392 */
393 perror("Failed to close plug-in directory");
394 }
395 }
396 free(entry);
397 free(file_path);
398 return ret;
399 }
400
401 struct bt_plugin **bt_plugin_create_all_from_dir(const char *path,
402 bool recurse)
403 {
404 GPtrArray *plugins_array = g_ptr_array_new();
405 struct bt_plugin **plugins = NULL;
406 enum bt_plugin_status status;
407
408 /* Append found plugins to array */
409 status = bt_plugin_create_append_all_from_dir(plugins_array, path,
410 recurse);
411 if (status < 0) {
412 goto error;
413 }
414
415 /* Add sentinel to array */
416 g_ptr_array_add(plugins_array, NULL);
417 plugins = (struct bt_plugin **) plugins_array->pdata;
418 goto end;
419
420 error:
421 if (plugins_array) {
422 g_ptr_array_free(plugins_array, TRUE);
423 plugins_array = NULL;
424 }
425
426 end:
427 if (plugins_array) {
428 g_ptr_array_free(plugins_array, FALSE);
429 }
430
431 return plugins;
432 }
433
434 const char *bt_plugin_get_name(struct bt_plugin *plugin)
435 {
436 return plugin && plugin->info.name_set ? plugin->info.name->str : NULL;
437 }
438
439 const char *bt_plugin_get_author(struct bt_plugin *plugin)
440 {
441 return plugin && plugin->info.author_set ? plugin->info.author->str : NULL;
442 }
443
444 const char *bt_plugin_get_license(struct bt_plugin *plugin)
445 {
446 return plugin && plugin->info.license_set ? plugin->info.license->str : NULL;
447 }
448
449 const char *bt_plugin_get_path(struct bt_plugin *plugin)
450 {
451 return plugin && plugin->info.path_set ? plugin->info.path->str : NULL;
452 }
453
454 const char *bt_plugin_get_description(struct bt_plugin *plugin)
455 {
456 return plugin && plugin->info.description_set ? plugin->info.description->str : NULL;
457 }
458
459 enum bt_plugin_status bt_plugin_get_version(struct bt_plugin *plugin,
460 unsigned int *major, unsigned int *minor, unsigned int *patch,
461 const char **extra)
462 {
463 enum bt_plugin_status status = BT_PLUGIN_STATUS_OK;
464
465 if (!plugin || !plugin->info.version_set) {
466 status = BT_PLUGIN_STATUS_ERROR;
467 goto end;
468 }
469
470 if (major) {
471 *major = plugin->info.version.major;
472 }
473
474 if (minor) {
475 *minor = plugin->info.version.minor;
476 }
477
478 if (patch) {
479 *patch = plugin->info.version.patch;
480 }
481
482 if (extra) {
483 *extra = plugin->info.version.extra->str;
484 }
485
486 end:
487 return status;
488 }
489
490 int bt_plugin_get_component_class_count(struct bt_plugin *plugin)
491 {
492 return plugin ? plugin->comp_classes->len : -1;
493 }
494
495 struct bt_component_class *bt_plugin_get_component_class(
496 struct bt_plugin *plugin, size_t index)
497 {
498 struct bt_component_class *comp_class = NULL;
499
500 if (!plugin || index >= plugin->comp_classes->len) {
501 goto error;
502 }
503
504 comp_class = g_ptr_array_index(plugin->comp_classes, index);
505 bt_get(comp_class);
506 goto end;
507
508 error:
509 BT_PUT(comp_class);
510
511 end:
512 return comp_class;
513 }
514
515 struct bt_component_class *bt_plugin_get_component_class_by_name_and_type(
516 struct bt_plugin *plugin, const char *name,
517 enum bt_component_class_type type)
518 {
519 struct bt_component_class *comp_class = NULL;
520 size_t i;
521
522 if (!plugin || !name) {
523 goto error;
524 }
525
526 for (i = 0; i < plugin->comp_classes->len; i++) {
527 struct bt_component_class *comp_class_candidate =
528 g_ptr_array_index(plugin->comp_classes, i);
529 const char *comp_class_cand_name =
530 bt_component_class_get_name(comp_class_candidate);
531 enum bt_component_class_type comp_class_cand_type =
532 bt_component_class_get_type(comp_class_candidate);
533
534 assert(comp_class_cand_name);
535 assert(comp_class_cand_type >= 0);
536
537 if (strcmp(name, comp_class_cand_name) == 0 &&
538 comp_class_cand_type == type) {
539 comp_class = bt_get(comp_class_candidate);
540 break;
541 }
542 }
543
544 goto end;
545
546 error:
547 BT_PUT(comp_class);
548
549 end:
550 return comp_class;
551 }
552
553 enum bt_plugin_status bt_plugin_add_component_class(
554 struct bt_plugin *plugin, struct bt_component_class *comp_class)
555 {
556 enum bt_plugin_status status = BT_PLUGIN_STATUS_OK;
557 struct bt_component_class *comp_class_dup = NULL;
558 int comp_class_index = -1;
559
560 if (!plugin || !comp_class || plugin->frozen) {
561 goto error;
562 }
563
564 /* Check for duplicate */
565 comp_class_dup = bt_plugin_get_component_class_by_name_and_type(plugin,
566 bt_component_class_get_name(comp_class),
567 bt_component_class_get_type(comp_class));
568 if (comp_class_dup) {
569 printf_verbose("Plugin `%s`: adding component class with existing name `%s` and type %d\n",
570 bt_plugin_get_name(plugin),
571 bt_component_class_get_name(comp_class),
572 bt_component_class_get_type(comp_class));
573 goto error;
574 }
575
576 /* Add new component class */
577 comp_class_index = plugin->comp_classes->len;
578 g_ptr_array_add(plugin->comp_classes, bt_get(comp_class));
579
580 /* Special case for a shared object plugin */
581 if (plugin->type == BT_PLUGIN_TYPE_SO) {
582 bt_plugin_so_on_add_component_class(plugin, comp_class);
583 }
584
585 goto end;
586
587 error:
588 /* Remove entry from plugin's component classes (if added) */
589 if (comp_class_index >= 0) {
590 g_ptr_array_remove_index(plugin->comp_classes,
591 comp_class_index);
592 }
593
594 status = BT_PLUGIN_STATUS_ERROR;
595
596 end:
597 bt_put(comp_class_dup);
598 return status;
599 }
This page took 0.039643 seconds and 3 git commands to generate.