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 module_close(gpointer data
)
47 if (g_module_close((GModule
*) data
)) {
48 printf_error("Failed to close plugin");
53 void factory_destroy(gpointer data
)
55 bt_component_factory_destroy((struct bt_component_factory
*)data
);
58 /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
60 struct dirent
*alloc_dirent(const char *path
)
66 name_max
= pathconf(path
, _PC_NAME_MAX
);
70 len
= offsetof(struct dirent
, d_name
) + name_max
+ 1;
76 enum bt_component_factory_status
77 bt_component_factory_load_file(struct bt_component_factory
*factory
,
80 enum bt_component_factory_status ret
= BT_COMPONENT_FACTORY_STATUS_OK
;
84 if (!factory
|| !path
) {
85 ret
= BT_COMPONENT_FACTORY_STATUS_INVAL
;
89 path_len
= strlen(path
);
90 if (path_len
<= PLUGIN_SUFFIX_LEN
) {
96 * Check if the file ends with a known plugin file type suffix (i.e. .so
99 if (strncmp(NATIVE_PLUGIN_SUFFIX
,
100 path
+ path_len
- NATIVE_PLUGIN_SUFFIX_LEN
,
101 NATIVE_PLUGIN_SUFFIX_LEN
) &&
102 strncmp(LIBTOOL_PLUGIN_SUFFIX
,
103 path
+ path_len
- LIBTOOL_PLUGIN_SUFFIX_LEN
,
104 LIBTOOL_PLUGIN_SUFFIX_LEN
)) {
105 /* Not a plugin file. */
109 module
= g_module_open(path
, 0);
111 printf_error("Module open error: %s", g_module_error());
115 /* Check if the module defines the appropriate entry points */
121 enum bt_component_factory_status
122 bt_component_factory_load_dir_recursive(struct bt_component_factory
*factory
,
125 DIR *directory
= NULL
;
126 struct dirent
*entry
= NULL
, *result
= NULL
;
127 char *file_path
= NULL
;
128 size_t path_len
= strlen(path
);
129 enum bt_component_factory_status ret
= BT_COMPONENT_FACTORY_STATUS_OK
;
131 if (path_len
>= PATH_MAX
) {
132 ret
= BT_COMPONENT_FACTORY_STATUS_INVAL
;
136 entry
= alloc_dirent(path
);
138 ret
= BT_COMPONENT_FACTORY_STATUS_NOMEM
;
142 file_path
= zmalloc(PATH_MAX
);
144 ret
= BT_COMPONENT_FACTORY_STATUS_NOMEM
;
148 strncpy(file_path
, path
, path_len
);
149 /* Append a trailing '/' to the path */
150 if (file_path
[path_len
- 1] != '/') {
151 file_path
[path_len
++] = '/';
154 /* Recursively walk directory */
155 while (!readdir_r(directory
, entry
, &result
) && result
) {
158 size_t file_name_len
= strlen(result
->d_name
);
160 if (path_len
+ file_name_len
>= PATH_MAX
) {
164 strncpy(file_path
+ path_len
, result
->d_name
, file_name_len
);
165 file_path
[path_len
+ file_name_len
] = '\0';
167 stat_ret
= stat(file_path
, &st
);
169 /* Continue to next file / directory. */
170 printf_perror("Failed to stat() plugin file");
174 if (S_ISDIR(st
.st_mode
)) {
175 ret
= bt_component_factory_load_dir_recursive(factory
,
177 if (ret
!= BT_COMPONENT_FACTORY_STATUS_OK
) {
180 } else if (S_ISREG(st
.st_mode
)) {
181 ret
= bt_component_factory_load_file(factory
,
183 if (ret
!= BT_COMPONENT_FACTORY_STATUS_OK
) {
194 void bt_component_factory_destroy(struct bt_component_factory
*factory
)
203 struct bt_component_factory
*
204 bt_component_factory_create(void)
206 struct bt_component_factory
*factory
;
208 factory
= g_new0(struct bt_component_factory
, 1);
213 factory
->modules
= g_ptr_array_new_with_free_func(module_close
);
214 if (!factory
->modules
) {
218 factory
->components
= g_ptr_array_new_with_free_func(factory_destroy
);
219 if (factory
->components
) {
225 bt_component_factory_destroy(factory
);
229 enum bt_component_factory_status
bt_component_factory_load(
230 struct bt_component_factory
*factory
, const char *path
)
232 enum bt_component_factory_status ret
= BT_COMPONENT_FACTORY_STATUS_OK
;
233 DIR *directory
= NULL
;
235 if (!factory
|| !path
) {
236 ret
= BT_COMPONENT_FACTORY_STATUS_INVAL
;
240 directory
= opendir(path
) ;
244 /* Try loading as a file. */
247 ret
= BT_COMPONENT_FACTORY_STATUS_NOENT
;
250 ret
= BT_COMPONENT_FACTORY_STATUS_IO
;
256 ret
= bt_component_factory_load_dir_recursive(factory
, path
);
258 ret
= bt_component_factory_load_file(factory
, path
);