4 * Babeltrace Plugin (generic)
6 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
9 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
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:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
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
30 #include <babeltrace/compiler.h>
31 #include <babeltrace/ref.h>
32 #include <babeltrace/plugin/plugin-internal.h>
33 #include <babeltrace/plugin/plugin-so-internal.h>
40 #ifdef WITH_PYTHON_PLUGINS
41 # include <babeltrace/plugin/plugin-python-enabled-internal.h>
43 # include <babeltrace/plugin/plugin-python-disabled-internal.h>
47 void bt_plugin_destroy(struct bt_object
*obj
)
49 struct bt_plugin
*plugin
;
52 plugin
= container_of(obj
, struct bt_plugin
, base
);
54 if (plugin
->type
== BT_PLUGIN_TYPE_SO
) {
55 bt_plugin_so_destroy_spec_data(plugin
);
56 } else if (plugin
->type
== BT_PLUGIN_TYPE_PYTHON
) {
57 bt_plugin_python_destroy_spec_data(plugin
);
62 if (plugin
->comp_classes
) {
63 g_ptr_array_free(plugin
->comp_classes
, TRUE
);
66 if (plugin
->info
.name
) {
67 g_string_free(plugin
->info
.name
, TRUE
);
70 if (plugin
->info
.path
) {
71 g_string_free(plugin
->info
.path
, TRUE
);
74 if (plugin
->info
.description
) {
75 g_string_free(plugin
->info
.description
, TRUE
);
78 if (plugin
->info
.author
) {
79 g_string_free(plugin
->info
.author
, TRUE
);
82 if (plugin
->info
.license
) {
83 g_string_free(plugin
->info
.license
, TRUE
);
86 if (plugin
->info
.version
.extra
) {
87 g_string_free(plugin
->info
.version
.extra
, TRUE
);
94 struct bt_plugin
*bt_plugin_create_empty(enum bt_plugin_type type
)
96 struct bt_plugin
*plugin
= NULL
;
98 plugin
= g_new0(struct bt_plugin
, 1);
103 bt_object_init(plugin
, bt_plugin_destroy
);
106 /* Create empty array of component classes */
107 plugin
->comp_classes
=
108 g_ptr_array_new_with_free_func((GDestroyNotify
) bt_put
);
109 if (!plugin
->comp_classes
) {
113 /* Create empty info */
114 plugin
->info
.name
= g_string_new(NULL
);
115 if (!plugin
->info
.name
) {
119 plugin
->info
.path
= g_string_new(NULL
);
120 if (!plugin
->info
.path
) {
124 plugin
->info
.description
= g_string_new(NULL
);
125 if (!plugin
->info
.description
) {
129 plugin
->info
.author
= g_string_new(NULL
);
130 if (!plugin
->info
.author
) {
134 plugin
->info
.license
= g_string_new(NULL
);
135 if (!plugin
->info
.license
) {
139 plugin
->info
.version
.extra
= g_string_new(NULL
);
140 if (!plugin
->info
.version
.extra
) {
153 struct bt_plugin
**bt_plugin_create_all_from_static(void)
155 return bt_plugin_so_create_all_from_static();
158 struct bt_plugin
**bt_plugin_create_all_from_file(const char *path
)
160 struct bt_plugin
**plugins
= NULL
;
166 printf_verbose("Trying to load plugins from `%s`\n", path
);
168 /* Try shared object plugins */
169 plugins
= bt_plugin_so_create_all_from_file(path
);
174 plugins
= bt_plugin_python_create_all_from_file(path
);
183 /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
185 struct dirent
*alloc_dirent(const char *path
)
189 struct dirent
*entry
;
191 name_max
= pathconf(path
, _PC_NAME_MAX
);
192 if (name_max
== -1) {
195 len
= offsetof(struct dirent
, d_name
) + name_max
+ 1;
196 entry
= zmalloc(len
);
201 enum bt_plugin_status
bt_plugin_create_append_all_from_dir(
202 GPtrArray
*plugins
, const char *path
, bool recurse
)
204 DIR *directory
= NULL
;
205 struct dirent
*entry
= NULL
, *result
= NULL
;
206 char *file_path
= NULL
;
208 enum bt_plugin_status ret
= BT_PLUGIN_STATUS_OK
;
211 ret
= BT_PLUGIN_STATUS_ERROR
;
215 path_len
= strlen(path
);
217 if (path_len
>= PATH_MAX
) {
218 ret
= BT_PLUGIN_STATUS_ERROR
;
222 entry
= alloc_dirent(path
);
224 ret
= BT_PLUGIN_STATUS_ERROR
;
228 file_path
= zmalloc(PATH_MAX
);
230 ret
= BT_PLUGIN_STATUS_NOMEM
;
234 strncpy(file_path
, path
, path_len
);
235 /* Append a trailing '/' to the path */
236 if (file_path
[path_len
- 1] != '/') {
237 file_path
[path_len
++] = '/';
240 directory
= opendir(file_path
);
242 perror("Failed to open plug-in directory");
243 ret
= BT_PLUGIN_STATUS_ERROR
;
247 /* Recursively walk directory */
248 while (!readdir_r(directory
, entry
, &result
) && result
) {
251 size_t file_name_len
;
253 if (result
->d_name
[0] == '.') {
254 /* Skip hidden files, . and .. */
258 file_name_len
= strlen(result
->d_name
);
260 if (path_len
+ file_name_len
>= PATH_MAX
) {
264 strncpy(file_path
+ path_len
, result
->d_name
, file_name_len
);
265 file_path
[path_len
+ file_name_len
] = '\0';
266 stat_ret
= stat(file_path
, &st
);
268 /* Continue to next file / directory. */
269 printf_perror("Failed to stat() plugin file\n");
273 if (S_ISDIR(st
.st_mode
) && recurse
) {
274 ret
= bt_plugin_create_append_all_from_dir(plugins
,
279 } else if (S_ISREG(st
.st_mode
)) {
280 struct bt_plugin
**plugins_from_file
=
281 bt_plugin_create_all_from_file(file_path
);
283 if (plugins_from_file
) {
284 struct bt_plugin
**plugin
;
286 for (plugin
= plugins_from_file
; *plugin
; plugin
++) {
287 /* Transfer ownership to array */
288 g_ptr_array_add(plugins
, *plugin
);
291 free(plugins_from_file
);
297 if (closedir(directory
)) {
299 * We don't want to override the error since there is
302 perror("Failed to close plug-in directory");
310 struct bt_plugin
**bt_plugin_create_all_from_dir(const char *path
,
313 GPtrArray
*plugins_array
= g_ptr_array_new();
314 struct bt_plugin
**plugins
= NULL
;
315 enum bt_plugin_status status
;
317 /* Append found plugins to array */
318 status
= bt_plugin_create_append_all_from_dir(plugins_array
, path
,
324 /* Add sentinel to array */
325 g_ptr_array_add(plugins_array
, NULL
);
326 plugins
= (struct bt_plugin
**) plugins_array
->pdata
;
331 g_ptr_array_free(plugins_array
, TRUE
);
332 plugins_array
= NULL
;
337 g_ptr_array_free(plugins_array
, FALSE
);
343 const char *bt_plugin_get_name(struct bt_plugin
*plugin
)
345 return plugin
&& plugin
->info
.name_set
? plugin
->info
.name
->str
: NULL
;
348 const char *bt_plugin_get_author(struct bt_plugin
*plugin
)
350 return plugin
&& plugin
->info
.author_set
? plugin
->info
.author
->str
: NULL
;
353 const char *bt_plugin_get_license(struct bt_plugin
*plugin
)
355 return plugin
&& plugin
->info
.license_set
? plugin
->info
.license
->str
: NULL
;
358 const char *bt_plugin_get_path(struct bt_plugin
*plugin
)
360 return plugin
&& plugin
->info
.path_set
? plugin
->info
.path
->str
: NULL
;
363 const char *bt_plugin_get_description(struct bt_plugin
*plugin
)
365 return plugin
&& plugin
->info
.description_set
? plugin
->info
.description
->str
: NULL
;
368 enum bt_plugin_status
bt_plugin_get_version(struct bt_plugin
*plugin
,
369 unsigned int *major
, unsigned int *minor
, unsigned int *patch
,
372 enum bt_plugin_status status
= BT_PLUGIN_STATUS_OK
;
374 if (!plugin
|| !plugin
->info
.version_set
) {
375 status
= BT_PLUGIN_STATUS_ERROR
;
380 *major
= plugin
->info
.version
.major
;
384 *minor
= plugin
->info
.version
.minor
;
388 *patch
= plugin
->info
.version
.patch
;
392 *extra
= plugin
->info
.version
.extra
->str
;
399 int bt_plugin_get_component_class_count(struct bt_plugin
*plugin
)
401 return plugin
? plugin
->comp_classes
->len
: -1;
404 struct bt_component_class
*bt_plugin_get_component_class(
405 struct bt_plugin
*plugin
, size_t index
)
407 struct bt_component_class
*comp_class
= NULL
;
409 if (!plugin
|| index
>= plugin
->comp_classes
->len
) {
413 comp_class
= g_ptr_array_index(plugin
->comp_classes
, index
);
424 struct bt_component_class
*bt_plugin_get_component_class_by_name_and_type(
425 struct bt_plugin
*plugin
, const char *name
,
426 enum bt_component_class_type type
)
428 struct bt_component_class
*comp_class
= NULL
;
431 if (!plugin
|| !name
) {
435 for (i
= 0; i
< plugin
->comp_classes
->len
; i
++) {
436 struct bt_component_class
*comp_class_candidate
=
437 g_ptr_array_index(plugin
->comp_classes
, i
);
438 const char *comp_class_cand_name
=
439 bt_component_class_get_name(comp_class_candidate
);
440 enum bt_component_class_type comp_class_cand_type
=
441 bt_component_class_get_type(comp_class_candidate
);
443 assert(comp_class_cand_name
);
444 assert(comp_class_cand_type
>= 0);
446 if (strcmp(name
, comp_class_cand_name
) == 0 &&
447 comp_class_cand_type
== type
) {
448 comp_class
= bt_get(comp_class_candidate
);
462 enum bt_plugin_status
bt_plugin_add_component_class(
463 struct bt_plugin
*plugin
, struct bt_component_class
*comp_class
)
465 enum bt_plugin_status status
= BT_PLUGIN_STATUS_OK
;
466 struct bt_component_class
*comp_class_dup
= NULL
;
468 int comp_class_index
= -1;
470 if (!plugin
|| !comp_class
|| plugin
->frozen
) {
474 /* Check for duplicate */
475 comp_class_dup
= bt_plugin_get_component_class_by_name_and_type(plugin
,
476 bt_component_class_get_name(comp_class
),
477 bt_component_class_get_type(comp_class
));
478 if (comp_class_dup
) {
479 printf_verbose("Plugin `%s`: adding component class with existing name `%s` and type %d\n",
480 bt_plugin_get_name(plugin
),
481 bt_component_class_get_name(comp_class
),
482 bt_component_class_get_type(comp_class
));
486 /* Add new component class */
487 comp_class_index
= plugin
->comp_classes
->len
;
488 g_ptr_array_add(plugin
->comp_classes
, bt_get(comp_class
));
490 /* Special case for a shared object plugin */
491 if (plugin
->type
== BT_PLUGIN_TYPE_SO
) {
492 ret
= bt_plugin_so_on_add_component_class(plugin
, comp_class
);
501 /* Remove entry from plugin's component classes (if added) */
502 if (comp_class_index
>= 0) {
503 g_ptr_array_remove_index(plugin
->comp_classes
,
507 status
= BT_PLUGIN_STATUS_ERROR
;
510 bt_put(comp_class_dup
);