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