4 * Babeltrace Plugin Component Factory
6 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #include <babeltrace/plugin/component-factory.h>
30 #include <babeltrace/plugin/component-factory-internal.h>
31 #include <babeltrace/babeltrace-internal.h>
32 #include <babeltrace/compiler.h>
38 #define NATIVE_PLUGIN_SUFFIX ".so"
39 #define NATIVE_PLUGIN_SUFFIX_LEN sizeof(NATIVE_PLUGIN_SUFFIX)
40 #define LIBTOOL_PLUGIN_SUFFIX ".la"
41 #define LIBTOOL_PLUGIN_SUFFIX_LEN sizeof(LIBTOOL_PLUGIN_SUFFIX)
42 #define PLUGIN_SUFFIX_LEN max_t(size_t, sizeof(NATIVE_PLUGIN_SUFFIX), \
43 sizeof(LIBTOOL_PLUGIN_SUFFIX))
45 /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
47 struct dirent
*alloc_dirent(const char *path
)
53 name_max
= pathconf(path
, _PC_NAME_MAX
);
57 len
= offsetof(struct dirent
, d_name
) + name_max
+ 1;
63 enum bt_component_factory_status
64 bt_component_factory_load_file(struct bt_component_factory
*factory
,
67 enum bt_component_factory_status ret
= BT_COMPONENT_FACTORY_STATUS_OK
;
68 enum bt_component_status component_status
;
71 struct bt_plugin
*plugin
;
73 if (!factory
|| !path
) {
74 ret
= BT_COMPONENT_FACTORY_STATUS_INVAL
;
78 path_len
= strlen(path
);
79 if (path_len
<= PLUGIN_SUFFIX_LEN
) {
80 ret
= BT_COMPONENT_FACTORY_STATUS_INVAL
;
86 * Check if the file ends with a known plugin file type suffix (i.e. .so
89 if (strncmp(NATIVE_PLUGIN_SUFFIX
,
90 path
+ path_len
- NATIVE_PLUGIN_SUFFIX_LEN
,
91 NATIVE_PLUGIN_SUFFIX_LEN
) &&
92 strncmp(LIBTOOL_PLUGIN_SUFFIX
,
93 path
+ path_len
- LIBTOOL_PLUGIN_SUFFIX_LEN
,
94 LIBTOOL_PLUGIN_SUFFIX_LEN
)) {
95 /* Name indicates that this is not a plugin file. */
96 ret
= BT_COMPONENT_FACTORY_STATUS_INVAL
;
100 module
= g_module_open(path
, 0);
102 printf_error("Module open error: %s", g_module_error());
103 ret
= BT_COMPONENT_FACTORY_STATUS_ERROR
;
107 /* Load plugin and make sure it defines the required entry points */
108 plugin
= bt_plugin_create(module
);
110 ret
= BT_COMPONENT_FACTORY_STATUS_INVAL_PLUGIN
;
111 if (!g_module_close(module
)) {
112 printf_error("Module close error: %s",
118 component_status
= bt_plugin_register_component_classes(plugin
,
120 if (component_status
!= BT_COMPONENT_STATUS_OK
) {
121 switch (component_status
) {
122 case BT_COMPONENT_STATUS_NOMEM
:
123 ret
= BT_COMPONENT_FACTORY_STATUS_NOMEM
;
126 ret
= BT_COMPONENT_FACTORY_STATUS_ERROR
;
129 bt_plugin_put(plugin
);
133 g_ptr_array_add(factory
->plugins
, plugin
);
139 enum bt_component_factory_status
140 bt_component_factory_load_dir_recursive(struct bt_component_factory
*factory
,
143 DIR *directory
= NULL
;
144 struct dirent
*entry
= NULL
, *result
= NULL
;
145 char *file_path
= NULL
;
146 size_t path_len
= strlen(path
);
147 enum bt_component_factory_status ret
= BT_COMPONENT_FACTORY_STATUS_OK
;
149 if (path_len
>= PATH_MAX
) {
150 ret
= BT_COMPONENT_FACTORY_STATUS_INVAL
;
154 entry
= alloc_dirent(path
);
156 ret
= BT_COMPONENT_FACTORY_STATUS_NOMEM
;
160 file_path
= zmalloc(PATH_MAX
);
162 ret
= BT_COMPONENT_FACTORY_STATUS_NOMEM
;
166 strncpy(file_path
, path
, path_len
);
167 /* Append a trailing '/' to the path */
168 if (file_path
[path_len
- 1] != '/') {
169 file_path
[path_len
++] = '/';
172 /* Recursively walk directory */
173 while (!readdir_r(directory
, entry
, &result
) && result
) {
176 size_t file_name_len
= strlen(result
->d_name
);
178 if (path_len
+ file_name_len
>= PATH_MAX
) {
182 strncpy(file_path
+ path_len
, result
->d_name
, file_name_len
);
183 file_path
[path_len
+ file_name_len
] = '\0';
185 stat_ret
= stat(file_path
, &st
);
187 /* Continue to next file / directory. */
188 printf_perror("Failed to stat() plugin file");
192 if (S_ISDIR(st
.st_mode
)) {
193 ret
= bt_component_factory_load_dir_recursive(factory
,
195 if (ret
!= BT_COMPONENT_FACTORY_STATUS_OK
) {
198 } else if (S_ISREG(st
.st_mode
)) {
199 bt_component_factory_load_file(factory
, file_path
);
208 void bt_component_factory_destroy(struct bt_component_factory
*factory
)
214 if (factory
->plugins
) {
215 g_ptr_array_free(factory
->plugins
, TRUE
);
217 if (factory
->components
) {
218 g_ptr_array_free(factory
->components
, TRUE
);
223 struct bt_component_factory
*
224 bt_component_factory_create(void)
226 struct bt_component_factory
*factory
;
228 factory
= g_new0(struct bt_component_factory
, 1);
233 factory
->plugins
= g_ptr_array_new_with_free_func(
234 (GDestroyNotify
) bt_plugin_put
);
235 if (!factory
->plugins
) {
238 factory
->components
= g_ptr_array_new_with_free_func(
239 (GDestroyNotify
) bt_component_put
);
240 if (!factory
->components
) {
246 bt_component_factory_destroy(factory
);
250 enum bt_component_factory_status
bt_component_factory_load(
251 struct bt_component_factory
*factory
, const char *path
)
253 enum bt_component_factory_status ret
= BT_COMPONENT_FACTORY_STATUS_OK
;
254 DIR *directory
= NULL
;
256 if (!factory
|| !path
) {
257 ret
= BT_COMPONENT_FACTORY_STATUS_INVAL
;
261 directory
= opendir(path
) ;
265 /* Try loading as a file. */
268 ret
= BT_COMPONENT_FACTORY_STATUS_NOENT
;
271 ret
= BT_COMPONENT_FACTORY_STATUS_IO
;
277 ret
= bt_component_factory_load_dir_recursive(factory
, path
);
279 ret
= bt_component_factory_load_file(factory
, path
);