Plugin symbol resolving fix
[babeltrace.git] / plugins / component-factory.c
CommitLineData
f3e4505b
JG
1/*
2 * component-factory.c
3 *
4 * Babeltrace Plugin Component Factory
5 *
6 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 *
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 *
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:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
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
26 * SOFTWARE.
27 */
28
29#include <babeltrace/plugin/component-factory.h>
30#include <babeltrace/plugin/component-factory-internal.h>
92893b7b
JG
31#include <babeltrace/plugin/source-internal.h>
32#include <babeltrace/plugin/sink-internal.h>
f3e4505b
JG
33#include <babeltrace/babeltrace-internal.h>
34#include <babeltrace/compiler.h>
b8a06801 35#include <babeltrace/ref.h>
f3e4505b
JG
36#include <unistd.h>
37#include <stdlib.h>
38#include <sys/stat.h>
fb2dcc52 39#include <gmodule.h>
39cfa40f 40#include <stdbool.h>
f3e4505b
JG
41
42#define NATIVE_PLUGIN_SUFFIX ".so"
43#define NATIVE_PLUGIN_SUFFIX_LEN sizeof(NATIVE_PLUGIN_SUFFIX)
44#define LIBTOOL_PLUGIN_SUFFIX ".la"
45#define LIBTOOL_PLUGIN_SUFFIX_LEN sizeof(LIBTOOL_PLUGIN_SUFFIX)
73299554
JG
46#define PLUGIN_SUFFIX_LEN max_t(size_t, sizeof(NATIVE_PLUGIN_SUFFIX), \
47 sizeof(LIBTOOL_PLUGIN_SUFFIX))
f3e4505b 48
f3e4505b
JG
49/* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
50static
51struct dirent *alloc_dirent(const char *path)
52{
53 size_t len;
54 long name_max;
55 struct dirent *entry;
56
57 name_max = pathconf(path, _PC_NAME_MAX);
58 if (name_max == -1) {
59 name_max = PATH_MAX;
60 }
61 len = offsetof(struct dirent, d_name) + name_max + 1;
62 entry = zmalloc(len);
63 return entry;
64}
65
66static
67enum bt_component_factory_status
68bt_component_factory_load_file(struct bt_component_factory *factory,
69 const char *path)
70{
71 enum bt_component_factory_status ret = BT_COMPONENT_FACTORY_STATUS_OK;
fb2dcc52 72 enum bt_component_status component_status;
f3e4505b
JG
73 size_t path_len;
74 GModule *module;
fb2dcc52 75 struct bt_plugin *plugin;
92893b7b 76 bool is_libtool_wrapper = false, is_shared_object = false;
f3e4505b
JG
77
78 if (!factory || !path) {
79 ret = BT_COMPONENT_FACTORY_STATUS_INVAL;
80 goto end;
81 }
82
83 path_len = strlen(path);
84 if (path_len <= PLUGIN_SUFFIX_LEN) {
73299554 85 ret = BT_COMPONENT_FACTORY_STATUS_INVAL;
f3e4505b
JG
86 goto end;
87 }
88
89 path_len++;
90 /*
91 * Check if the file ends with a known plugin file type suffix (i.e. .so
92 * or .la on Linux).
93 */
92893b7b 94 is_libtool_wrapper = !strncmp(LIBTOOL_PLUGIN_SUFFIX,
b8a06801 95 path + path_len - LIBTOOL_PLUGIN_SUFFIX_LEN,
92893b7b
JG
96 LIBTOOL_PLUGIN_SUFFIX_LEN);
97 is_shared_object = !strncmp(NATIVE_PLUGIN_SUFFIX,
98 path + path_len - NATIVE_PLUGIN_SUFFIX_LEN,
99 NATIVE_PLUGIN_SUFFIX_LEN);
100 if (!is_shared_object && !is_libtool_wrapper) {
73299554
JG
101 /* Name indicates that this is not a plugin file. */
102 ret = BT_COMPONENT_FACTORY_STATUS_INVAL;
f3e4505b
JG
103 goto end;
104 }
105
106 module = g_module_open(path, 0);
107 if (!module) {
108 printf_error("Module open error: %s", g_module_error());
73299554 109 ret = BT_COMPONENT_FACTORY_STATUS_ERROR;
f3e4505b
JG
110 goto end;
111 }
112
fb2dcc52
JG
113 /* Load plugin and make sure it defines the required entry points */
114 plugin = bt_plugin_create(module);
73299554 115 if (!plugin) {
fb2dcc52 116 ret = BT_COMPONENT_FACTORY_STATUS_INVAL_PLUGIN;
73299554
JG
117 if (!g_module_close(module)) {
118 printf_error("Module close error: %s",
fb2dcc52
JG
119 g_module_error());
120 }
121 goto end;
122 }
123
124 component_status = bt_plugin_register_component_classes(plugin,
b8a06801 125 factory);
fb2dcc52
JG
126 if (component_status != BT_COMPONENT_STATUS_OK) {
127 switch (component_status) {
128 case BT_COMPONENT_STATUS_NOMEM:
129 ret = BT_COMPONENT_FACTORY_STATUS_NOMEM;
130 break;
131 default:
132 ret = BT_COMPONENT_FACTORY_STATUS_ERROR;
133 break;
73299554 134 }
b8a06801
JG
135
136 BT_PUT(plugin);
73299554
JG
137 goto end;
138 }
fb2dcc52 139 g_ptr_array_add(factory->plugins, plugin);
f3e4505b
JG
140end:
141 return ret;
142}
143
144static
145enum bt_component_factory_status
146bt_component_factory_load_dir_recursive(struct bt_component_factory *factory,
147 const char *path)
148{
149 DIR *directory = NULL;
150 struct dirent *entry = NULL, *result = NULL;
151 char *file_path = NULL;
152 size_t path_len = strlen(path);
153 enum bt_component_factory_status ret = BT_COMPONENT_FACTORY_STATUS_OK;
154
155 if (path_len >= PATH_MAX) {
156 ret = BT_COMPONENT_FACTORY_STATUS_INVAL;
157 goto end;
158 }
159
160 entry = alloc_dirent(path);
161 if (!entry) {
162 ret = BT_COMPONENT_FACTORY_STATUS_NOMEM;
163 goto end;
164 }
165
166 file_path = zmalloc(PATH_MAX);
167 if (!file_path) {
168 ret = BT_COMPONENT_FACTORY_STATUS_NOMEM;
169 goto end;
170 }
171
172 strncpy(file_path, path, path_len);
173 /* Append a trailing '/' to the path */
174 if (file_path[path_len - 1] != '/') {
175 file_path[path_len++] = '/';
176 }
177
33bceaf8
JG
178 directory = opendir(file_path);
179 if (!directory) {
180 perror("Failed to open plug-in directory");
181 ret = BT_COMPONENT_FACTORY_STATUS_ERROR;
182 goto end;
183 }
184
f3e4505b
JG
185 /* Recursively walk directory */
186 while (!readdir_r(directory, entry, &result) && result) {
187 struct stat st;
188 int stat_ret;
33bceaf8
JG
189 size_t file_name_len;
190
191 if (result->d_name[0] == '.') {
192 /* Skip hidden files, . and .. */
193 continue;
194 }
195
196 file_name_len = strlen(result->d_name);
f3e4505b
JG
197
198 if (path_len + file_name_len >= PATH_MAX) {
199 continue;
200 }
201
202 strncpy(file_path + path_len, result->d_name, file_name_len);
203 file_path[path_len + file_name_len] = '\0';
204
205 stat_ret = stat(file_path, &st);
206 if (stat_ret < 0) {
207 /* Continue to next file / directory. */
208 printf_perror("Failed to stat() plugin file");
209 continue;
210 }
211
212 if (S_ISDIR(st.st_mode)) {
213 ret = bt_component_factory_load_dir_recursive(factory,
b8a06801 214 file_path);
f3e4505b
JG
215 if (ret != BT_COMPONENT_FACTORY_STATUS_OK) {
216 goto end;
217 }
218 } else if (S_ISREG(st.st_mode)) {
73299554 219 bt_component_factory_load_file(factory, file_path);
f3e4505b
JG
220 }
221 }
222end:
33bceaf8
JG
223 if (directory) {
224 if (closedir(directory)) {
225 /*
226 * We don't want to override the error since there is
227 * nothing could do.
228 */
229 perror("Failed to close plug-in directory");
230 }
231 }
f3e4505b
JG
232 free(entry);
233 free(file_path);
234 return ret;
235}
236
b8a06801
JG
237static
238void bt_component_factory_destroy(struct bt_object *obj)
f3e4505b 239{
b8a06801
JG
240 struct bt_component_factory *factory = NULL;
241
242 assert(obj);
243 factory = container_of(obj, struct bt_component_factory, base);
f3e4505b 244
fb2dcc52
JG
245 if (factory->plugins) {
246 g_ptr_array_free(factory->plugins, TRUE);
247 }
92893b7b
JG
248 if (factory->component_classes) {
249 g_ptr_array_free(factory->component_classes, TRUE);
fb2dcc52 250 }
f3e4505b
JG
251 g_free(factory);
252}
253
b8a06801 254struct bt_component_factory *bt_component_factory_create(void)
f3e4505b
JG
255{
256 struct bt_component_factory *factory;
257
258 factory = g_new0(struct bt_component_factory, 1);
259 if (!factory) {
260 goto end;
261 }
262
b8a06801 263 bt_object_init(factory, bt_component_factory_destroy);
fb2dcc52 264 factory->plugins = g_ptr_array_new_with_free_func(
b8a06801 265 (GDestroyNotify) bt_put);
73299554 266 if (!factory->plugins) {
f3e4505b
JG
267 goto error;
268 }
92893b7b 269 factory->component_classes = g_ptr_array_new_with_free_func(
b8a06801 270 (GDestroyNotify) bt_put);
92893b7b 271 if (!factory->component_classes) {
fb2dcc52
JG
272 goto error;
273 }
f3e4505b
JG
274end:
275 return factory;
276error:
b8a06801
JG
277 BT_PUT(factory);
278 return factory;
f3e4505b
JG
279}
280
2e339de1 281struct bt_value *bt_component_factory_get_components(
92893b7b
JG
282 struct bt_component_factory *factory)
283{
284 assert(0);
285 return NULL;
286}
287
f3e4505b
JG
288enum bt_component_factory_status bt_component_factory_load(
289 struct bt_component_factory *factory, const char *path)
290{
291 enum bt_component_factory_status ret = BT_COMPONENT_FACTORY_STATUS_OK;
f3e4505b
JG
292
293 if (!factory || !path) {
294 ret = BT_COMPONENT_FACTORY_STATUS_INVAL;
295 goto end;
296 }
297
b8a06801
JG
298 if (!g_file_test(path, G_FILE_TEST_EXISTS)) {
299 ret = BT_COMPONENT_FACTORY_STATUS_NOENT;
300 goto end;
f3e4505b
JG
301 }
302
b8a06801 303 if (g_file_test(path, G_FILE_TEST_IS_DIR)) {
f3e4505b 304 ret = bt_component_factory_load_dir_recursive(factory, path);
b8a06801
JG
305 } else if (g_file_test(path, G_FILE_TEST_IS_REGULAR) ||
306 g_file_test(path, G_FILE_TEST_IS_SYMLINK)) {
f3e4505b 307 ret = bt_component_factory_load_file(factory, path);
b8a06801
JG
308 } else {
309 ret = BT_COMPONENT_FACTORY_STATUS_INVAL;
310 goto end;
f3e4505b
JG
311 }
312end:
f3e4505b
JG
313 return ret;
314}
92893b7b
JG
315
316enum bt_component_factory_status
317bt_component_factory_register_source_component_class(
318 struct bt_component_factory *factory, const char *name,
319 bt_component_source_init_cb init)
320{
321 assert(0);
322 return BT_COMPONENT_FACTORY_STATUS_ERROR;
323}
324
325enum bt_component_factory_status
326bt_component_factory_register_sink_component_class(
327 struct bt_component_factory *factory, const char *name,
328 bt_component_sink_init_cb init)
329{
330 struct bt_component_class *class;
331 enum bt_component_factory_status ret = BT_COMPONENT_FACTORY_STATUS_OK;
332
333 if (!factory || !name || !init) {
334 ret = BT_COMPONENT_FACTORY_STATUS_INVAL;
335 goto end;
336 }
337
92893b7b
JG
338end:
339 return ret;
340}
This page took 0.038044 seconds and 4 git commands to generate.