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