List detected component classes
[babeltrace.git] / plugins / component-factory.c
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>
31 #include <babeltrace/plugin/component-class-internal.h>
32 #include <babeltrace/plugin/source-internal.h>
33 #include <babeltrace/plugin/sink-internal.h>
34 #include <babeltrace/babeltrace-internal.h>
35 #include <babeltrace/compiler.h>
36 #include <babeltrace/ref.h>
37 #include <unistd.h>
38 #include <stdlib.h>
39 #include <sys/stat.h>
40 #include <gmodule.h>
41 #include <stdbool.h>
42
43 #define NATIVE_PLUGIN_SUFFIX ".so"
44 #define NATIVE_PLUGIN_SUFFIX_LEN sizeof(NATIVE_PLUGIN_SUFFIX)
45 #define LIBTOOL_PLUGIN_SUFFIX ".la"
46 #define LIBTOOL_PLUGIN_SUFFIX_LEN sizeof(LIBTOOL_PLUGIN_SUFFIX)
47 #define PLUGIN_SUFFIX_LEN max_t(size_t, sizeof(NATIVE_PLUGIN_SUFFIX), \
48 sizeof(LIBTOOL_PLUGIN_SUFFIX))
49
50 /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
51 static
52 struct dirent *alloc_dirent(const char *path)
53 {
54 size_t len;
55 long name_max;
56 struct dirent *entry;
57
58 name_max = pathconf(path, _PC_NAME_MAX);
59 if (name_max == -1) {
60 name_max = PATH_MAX;
61 }
62 len = offsetof(struct dirent, d_name) + name_max + 1;
63 entry = zmalloc(len);
64 return entry;
65 }
66
67 static
68 enum bt_component_factory_status
69 bt_component_factory_load_file(struct bt_component_factory *factory,
70 const char *path)
71 {
72 enum bt_component_factory_status ret = BT_COMPONENT_FACTORY_STATUS_OK;
73 enum bt_component_status component_status;
74 size_t path_len;
75 GModule *module;
76 struct bt_plugin *plugin;
77 bool is_libtool_wrapper = false, is_shared_object = false;
78
79 if (!factory || !path) {
80 ret = BT_COMPONENT_FACTORY_STATUS_INVAL;
81 goto end;
82 }
83
84 path_len = strlen(path);
85 if (path_len <= PLUGIN_SUFFIX_LEN) {
86 ret = BT_COMPONENT_FACTORY_STATUS_INVAL;
87 goto end;
88 }
89
90 path_len++;
91 /*
92 * Check if the file ends with a known plugin file type suffix (i.e. .so
93 * or .la on Linux).
94 */
95 is_libtool_wrapper = !strncmp(LIBTOOL_PLUGIN_SUFFIX,
96 path + path_len - LIBTOOL_PLUGIN_SUFFIX_LEN,
97 LIBTOOL_PLUGIN_SUFFIX_LEN);
98 is_shared_object = !strncmp(NATIVE_PLUGIN_SUFFIX,
99 path + path_len - NATIVE_PLUGIN_SUFFIX_LEN,
100 NATIVE_PLUGIN_SUFFIX_LEN);
101 if (!is_shared_object && !is_libtool_wrapper) {
102 /* Name indicates that this is not a plugin file. */
103 ret = BT_COMPONENT_FACTORY_STATUS_INVAL;
104 goto end;
105 }
106
107 module = g_module_open(path, 0);
108 if (!module) {
109 printf_error("Module open error: %s", g_module_error());
110 ret = BT_COMPONENT_FACTORY_STATUS_ERROR;
111 goto end;
112 }
113
114 /* Load plugin and make sure it defines the required entry points */
115 plugin = bt_plugin_create(module, path);
116 if (!plugin) {
117 ret = BT_COMPONENT_FACTORY_STATUS_INVAL_PLUGIN;
118 if (!g_module_close(module)) {
119 printf_error("Module close error: %s",
120 g_module_error());
121 }
122 goto end;
123 }
124
125 factory->current_plugin = plugin;
126 component_status = bt_plugin_register_component_classes(plugin,
127 factory);
128 factory->current_plugin = NULL;
129 if (component_status != BT_COMPONENT_STATUS_OK) {
130 switch (component_status) {
131 case BT_COMPONENT_STATUS_NOMEM:
132 ret = BT_COMPONENT_FACTORY_STATUS_NOMEM;
133 break;
134 default:
135 ret = BT_COMPONENT_FACTORY_STATUS_ERROR;
136 break;
137 }
138
139 BT_PUT(plugin);
140 goto end;
141 }
142 end:
143 return ret;
144 }
145
146 static
147 enum bt_component_factory_status
148 bt_component_factory_load_dir_recursive(struct bt_component_factory *factory,
149 const char *path)
150 {
151 DIR *directory = NULL;
152 struct dirent *entry = NULL, *result = NULL;
153 char *file_path = NULL;
154 size_t path_len = strlen(path);
155 enum bt_component_factory_status ret = BT_COMPONENT_FACTORY_STATUS_OK;
156
157 if (path_len >= PATH_MAX) {
158 ret = BT_COMPONENT_FACTORY_STATUS_INVAL;
159 goto end;
160 }
161
162 entry = alloc_dirent(path);
163 if (!entry) {
164 ret = BT_COMPONENT_FACTORY_STATUS_NOMEM;
165 goto end;
166 }
167
168 file_path = zmalloc(PATH_MAX);
169 if (!file_path) {
170 ret = BT_COMPONENT_FACTORY_STATUS_NOMEM;
171 goto end;
172 }
173
174 strncpy(file_path, path, path_len);
175 /* Append a trailing '/' to the path */
176 if (file_path[path_len - 1] != '/') {
177 file_path[path_len++] = '/';
178 }
179
180 directory = opendir(file_path);
181 if (!directory) {
182 perror("Failed to open plug-in directory");
183 ret = BT_COMPONENT_FACTORY_STATUS_ERROR;
184 goto end;
185 }
186
187 /* Recursively walk directory */
188 while (!readdir_r(directory, entry, &result) && result) {
189 struct stat st;
190 int stat_ret;
191 size_t file_name_len;
192
193 if (result->d_name[0] == '.') {
194 /* Skip hidden files, . and .. */
195 continue;
196 }
197
198 file_name_len = strlen(result->d_name);
199
200 if (path_len + file_name_len >= PATH_MAX) {
201 continue;
202 }
203
204 strncpy(file_path + path_len, result->d_name, file_name_len);
205 file_path[path_len + file_name_len] = '\0';
206
207 stat_ret = stat(file_path, &st);
208 if (stat_ret < 0) {
209 /* Continue to next file / directory. */
210 printf_perror("Failed to stat() plugin file");
211 continue;
212 }
213
214 if (S_ISDIR(st.st_mode)) {
215 ret = bt_component_factory_load_dir_recursive(factory,
216 file_path);
217 if (ret != BT_COMPONENT_FACTORY_STATUS_OK) {
218 goto end;
219 }
220 } else if (S_ISREG(st.st_mode)) {
221 bt_component_factory_load_file(factory, file_path);
222 }
223 }
224 end:
225 if (directory) {
226 if (closedir(directory)) {
227 /*
228 * We don't want to override the error since there is
229 * nothing could do.
230 */
231 perror("Failed to close plug-in directory");
232 }
233 }
234 free(entry);
235 free(file_path);
236 return ret;
237 }
238
239 static
240 void bt_component_factory_destroy(struct bt_object *obj)
241 {
242 struct bt_component_factory *factory = NULL;
243
244 assert(obj);
245 factory = container_of(obj, struct bt_component_factory, base);
246
247 if (factory->component_classes) {
248 g_ptr_array_free(factory->component_classes, TRUE);
249 }
250 g_free(factory);
251 }
252
253 struct bt_component_factory *bt_component_factory_create(void)
254 {
255 struct bt_component_factory *factory;
256
257 factory = g_new0(struct bt_component_factory, 1);
258 if (!factory) {
259 goto end;
260 }
261
262 bt_object_init(factory, bt_component_factory_destroy);
263 factory->component_classes = g_ptr_array_new_with_free_func(
264 (GDestroyNotify) bt_put);
265 if (!factory->component_classes) {
266 goto error;
267 }
268 end:
269 return factory;
270 error:
271 BT_PUT(factory);
272 return factory;
273 }
274
275 int bt_component_factory_get_component_class_count(
276 struct bt_component_factory *factory)
277 {
278 return factory ? factory->component_classes->len : -1;
279 }
280
281 struct bt_component_class *bt_component_factory_get_component_class_index(
282 struct bt_component_factory *factory, int index)
283 {
284 struct bt_component_class *component_class = NULL;
285
286 if (!factory || index < 0 || index >= factory->component_classes->len) {
287 goto end;
288 }
289
290 component_class = bt_get(g_ptr_array_index(
291 factory->component_classes, index));
292 end:
293 return component_class;
294 }
295
296 struct bt_component_class *bt_component_factory_get_component_class(
297 struct bt_component_factory *factory,
298 const char *plugin_name, enum bt_component_type type,
299 const char *component_name)
300 {
301 size_t i;
302 struct bt_component_class *component_class = NULL;
303
304 if (!factory || (!plugin_name && !component_name &&
305 type == BT_COMPONENT_TYPE_UNKNOWN)) {
306 /* At least one criterion must be provided. */
307 goto no_match;
308 }
309
310 for (i = 0; i < factory->component_classes->len; i++) {
311 struct bt_plugin *plugin = NULL;
312
313 component_class = g_ptr_array_index(factory->component_classes,
314 i);
315 plugin = bt_component_class_get_plugin(component_class);
316 assert(plugin);
317
318 if (type != BT_COMPONENT_TYPE_UNKNOWN) {
319 if (type != bt_component_class_get_type(
320 component_class)) {
321 continue;
322 }
323 }
324
325 if (plugin_name) {
326 const char *cur_plugin_name = bt_plugin_get_name(
327 plugin);
328
329 assert(cur_plugin_name);
330 if (strcmp(plugin_name, cur_plugin_name)) {
331 continue;
332 }
333 }
334
335 if (component_name) {
336 const char *cur_cc_name = bt_component_class_get_name(
337 component_class);
338
339 assert(cur_cc_name);
340 if (strcmp(component_name, cur_cc_name)) {
341 continue;
342 }
343 }
344
345 /* All criteria met. */
346 goto match;
347 }
348
349 no_match:
350 return NULL;
351 match:
352 return bt_get(component_class);
353 }
354
355 enum bt_component_factory_status bt_component_factory_load(
356 struct bt_component_factory *factory, const char *path)
357 {
358 enum bt_component_factory_status ret = BT_COMPONENT_FACTORY_STATUS_OK;
359
360 if (!factory || !path) {
361 ret = BT_COMPONENT_FACTORY_STATUS_INVAL;
362 goto end;
363 }
364
365 if (!g_file_test(path, G_FILE_TEST_EXISTS)) {
366 ret = BT_COMPONENT_FACTORY_STATUS_NOENT;
367 goto end;
368 }
369
370 if (g_file_test(path, G_FILE_TEST_IS_DIR)) {
371 ret = bt_component_factory_load_dir_recursive(factory, path);
372 } else if (g_file_test(path, G_FILE_TEST_IS_REGULAR) ||
373 g_file_test(path, G_FILE_TEST_IS_SYMLINK)) {
374 ret = bt_component_factory_load_file(factory, path);
375 } else {
376 ret = BT_COMPONENT_FACTORY_STATUS_INVAL;
377 goto end;
378 }
379 end:
380 return ret;
381 }
382
383 static
384 enum bt_component_factory_status
385 add_component_class(struct bt_component_factory *factory, const char *name,
386 const char *description, bt_component_init_cb init,
387 enum bt_component_type type)
388 {
389 struct bt_component_class *class;
390 enum bt_component_factory_status ret = BT_COMPONENT_FACTORY_STATUS_OK;
391
392 if (!factory || !name || !init) {
393 ret = BT_COMPONENT_FACTORY_STATUS_INVAL;
394 goto end;
395 }
396
397 class = bt_component_class_create(type, name, description,
398 factory->current_plugin);
399 g_ptr_array_add(factory->component_classes, class);
400 end:
401 return ret;
402 }
403
404 enum bt_component_factory_status
405 bt_component_factory_register_source_component_class(
406 struct bt_component_factory *factory, const char *name,
407 const char *description, bt_component_init_cb init)
408 {
409 return add_component_class(factory, name, description, init,
410 BT_COMPONENT_TYPE_SOURCE);
411 }
412
413 enum bt_component_factory_status
414 bt_component_factory_register_sink_component_class(
415 struct bt_component_factory *factory, const char *name,
416 const char *description, bt_component_init_cb init)
417 {
418 return add_component_class(factory, name, description, init,
419 BT_COMPONENT_TYPE_SINK);
420 }
This page took 0.038574 seconds and 4 git commands to generate.