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>
37 #define NATIVE_PLUGIN_SUFFIX ".so"
38 #define NATIVE_PLUGIN_SUFFIX_LEN sizeof(NATIVE_PLUGIN_SUFFIX)
39 #define LIBTOOL_PLUGIN_SUFFIX ".la"
40 #define LIBTOOL_PLUGIN_SUFFIX_LEN sizeof(LIBTOOL_PLUGIN_SUFFIX)
41 #define PLUGIN_SUFFIX_LEN max_t(size_t, sizeof(NATIVE_PLUGIN_SUFFIX), \
42 sizeof(LIBTOOL_PLUGIN_SUFFIX))
45 void component_destroy(gpointer data
)
51 void plugin_destroy(gpointer data
)
53 struct plugin
*plugin
= data
;
55 if (plugin
->module
&& g_module_close(plugin
->module
)) {
56 printf_error("Failed to close plugin");
59 if (plugin
->components
) {
60 g_ptr_array_free(plugin
->components
, TRUE
);
67 struct plugin
*plugin_create(void)
69 struct plugin
*plugin
= g_new0(struct plugin
, 1);
75 plugin
->components
= g_ptr_array_new_with_free_func(component_destroy
);
76 if (!plugin
->components
) {
85 /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
87 struct dirent
*alloc_dirent(const char *path
)
93 name_max
= pathconf(path
, _PC_NAME_MAX
);
97 len
= offsetof(struct dirent
, d_name
) + name_max
+ 1;
103 struct plugin
*load_plugin(GModule
*module
)
109 enum bt_component_factory_status
110 bt_component_factory_load_file(struct bt_component_factory
*factory
,
113 enum bt_component_factory_status ret
= BT_COMPONENT_FACTORY_STATUS_OK
;
116 struct plugin
*plugin
;
118 if (!factory
|| !path
) {
119 ret
= BT_COMPONENT_FACTORY_STATUS_INVAL
;
123 path_len
= strlen(path
);
124 if (path_len
<= PLUGIN_SUFFIX_LEN
) {
125 ret
= BT_COMPONENT_FACTORY_STATUS_INVAL
;
131 * Check if the file ends with a known plugin file type suffix (i.e. .so
134 if (strncmp(NATIVE_PLUGIN_SUFFIX
,
135 path
+ path_len
- NATIVE_PLUGIN_SUFFIX_LEN
,
136 NATIVE_PLUGIN_SUFFIX_LEN
) &&
137 strncmp(LIBTOOL_PLUGIN_SUFFIX
,
138 path
+ path_len
- LIBTOOL_PLUGIN_SUFFIX_LEN
,
139 LIBTOOL_PLUGIN_SUFFIX_LEN
)) {
140 /* Name indicates that this is not a plugin file. */
141 ret
= BT_COMPONENT_FACTORY_STATUS_INVAL
;
145 module
= g_module_open(path
, 0);
147 printf_error("Module open error: %s", g_module_error());
148 ret
= BT_COMPONENT_FACTORY_STATUS_ERROR
;
152 /* Check if the module defines the appropriate entry points */
153 plugin
= load_plugin(module
);
155 /* Not a Babeltrace plugin */
156 ret
= BT_COMPONENT_FACTORY_STATUS_INVAL
;
157 if (!g_module_close(module
)) {
158 printf_error("Module close error: %s",
168 enum bt_component_factory_status
169 bt_component_factory_load_dir_recursive(struct bt_component_factory
*factory
,
172 DIR *directory
= NULL
;
173 struct dirent
*entry
= NULL
, *result
= NULL
;
174 char *file_path
= NULL
;
175 size_t path_len
= strlen(path
);
176 enum bt_component_factory_status ret
= BT_COMPONENT_FACTORY_STATUS_OK
;
178 if (path_len
>= PATH_MAX
) {
179 ret
= BT_COMPONENT_FACTORY_STATUS_INVAL
;
183 entry
= alloc_dirent(path
);
185 ret
= BT_COMPONENT_FACTORY_STATUS_NOMEM
;
189 file_path
= zmalloc(PATH_MAX
);
191 ret
= BT_COMPONENT_FACTORY_STATUS_NOMEM
;
195 strncpy(file_path
, path
, path_len
);
196 /* Append a trailing '/' to the path */
197 if (file_path
[path_len
- 1] != '/') {
198 file_path
[path_len
++] = '/';
201 /* Recursively walk directory */
202 while (!readdir_r(directory
, entry
, &result
) && result
) {
205 size_t file_name_len
= strlen(result
->d_name
);
207 if (path_len
+ file_name_len
>= PATH_MAX
) {
211 strncpy(file_path
+ path_len
, result
->d_name
, file_name_len
);
212 file_path
[path_len
+ file_name_len
] = '\0';
214 stat_ret
= stat(file_path
, &st
);
216 /* Continue to next file / directory. */
217 printf_perror("Failed to stat() plugin file");
221 if (S_ISDIR(st
.st_mode
)) {
222 ret
= bt_component_factory_load_dir_recursive(factory
,
224 if (ret
!= BT_COMPONENT_FACTORY_STATUS_OK
) {
227 } else if (S_ISREG(st
.st_mode
)) {
228 bt_component_factory_load_file(factory
, file_path
);
237 void bt_component_factory_destroy(struct bt_component_factory
*factory
)
246 struct bt_component_factory
*
247 bt_component_factory_create(void)
249 struct bt_component_factory
*factory
;
251 factory
= g_new0(struct bt_component_factory
, 1);
256 factory
->plugins
= g_ptr_array_new_with_free_func(plugin_destroy
);
257 if (!factory
->plugins
) {
263 bt_component_factory_destroy(factory
);
267 enum bt_component_factory_status
bt_component_factory_load(
268 struct bt_component_factory
*factory
, const char *path
)
270 enum bt_component_factory_status ret
= BT_COMPONENT_FACTORY_STATUS_OK
;
271 DIR *directory
= NULL
;
273 if (!factory
|| !path
) {
274 ret
= BT_COMPONENT_FACTORY_STATUS_INVAL
;
278 directory
= opendir(path
) ;
282 /* Try loading as a file. */
285 ret
= BT_COMPONENT_FACTORY_STATUS_NOENT
;
288 ret
= BT_COMPONENT_FACTORY_STATUS_IO
;
294 ret
= bt_component_factory_load_dir_recursive(factory
, path
);
296 ret
= bt_component_factory_load_file(factory
, path
);