b4ebe3f2ef9f0ac57abae257bdfac92a9f30c63a
[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/babeltrace-internal.h>
32 #include <babeltrace/compiler.h>
33 #include <unistd.h>
34 #include <stdlib.h>
35 #include <sys/stat.h>
36
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))
43
44 static
45 void module_close(gpointer data)
46 {
47 if (g_module_close((GModule *) data)) {
48 printf_error("Failed to close plugin");
49 }
50 }
51
52 static
53 void factory_destroy(gpointer data)
54 {
55 bt_component_factory_destroy((struct bt_component_factory *)data);
56 }
57
58 /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
59 static
60 struct dirent *alloc_dirent(const char *path)
61 {
62 size_t len;
63 long name_max;
64 struct dirent *entry;
65
66 name_max = pathconf(path, _PC_NAME_MAX);
67 if (name_max == -1) {
68 name_max = PATH_MAX;
69 }
70 len = offsetof(struct dirent, d_name) + name_max + 1;
71 entry = zmalloc(len);
72 return entry;
73 }
74
75 static
76 enum bt_component_factory_status
77 bt_component_factory_load_file(struct bt_component_factory *factory,
78 const char *path)
79 {
80 enum bt_component_factory_status ret = BT_COMPONENT_FACTORY_STATUS_OK;
81 size_t path_len;
82 GModule *module;
83
84 if (!factory || !path) {
85 ret = BT_COMPONENT_FACTORY_STATUS_INVAL;
86 goto end;
87 }
88
89 path_len = strlen(path);
90 if (path_len <= PLUGIN_SUFFIX_LEN) {
91 goto end;
92 }
93
94 path_len++;
95 /*
96 * Check if the file ends with a known plugin file type suffix (i.e. .so
97 * or .la on Linux).
98 */
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. */
106 goto end;
107 }
108
109 module = g_module_open(path, 0);
110 if (!module) {
111 printf_error("Module open error: %s", g_module_error());
112 goto end;
113 }
114
115 /* Check if the module defines the appropriate entry points */
116 end:
117 return ret;
118 }
119
120 static
121 enum bt_component_factory_status
122 bt_component_factory_load_dir_recursive(struct bt_component_factory *factory,
123 const char *path)
124 {
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;
130
131 if (path_len >= PATH_MAX) {
132 ret = BT_COMPONENT_FACTORY_STATUS_INVAL;
133 goto end;
134 }
135
136 entry = alloc_dirent(path);
137 if (!entry) {
138 ret = BT_COMPONENT_FACTORY_STATUS_NOMEM;
139 goto end;
140 }
141
142 file_path = zmalloc(PATH_MAX);
143 if (!file_path) {
144 ret = BT_COMPONENT_FACTORY_STATUS_NOMEM;
145 goto end;
146 }
147
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++] = '/';
152 }
153
154 /* Recursively walk directory */
155 while (!readdir_r(directory, entry, &result) && result) {
156 struct stat st;
157 int stat_ret;
158 size_t file_name_len = strlen(result->d_name);
159
160 if (path_len + file_name_len >= PATH_MAX) {
161 continue;
162 }
163
164 strncpy(file_path + path_len, result->d_name, file_name_len);
165 file_path[path_len + file_name_len] = '\0';
166
167 stat_ret = stat(file_path, &st);
168 if (stat_ret < 0) {
169 /* Continue to next file / directory. */
170 printf_perror("Failed to stat() plugin file");
171 continue;
172 }
173
174 if (S_ISDIR(st.st_mode)) {
175 ret = bt_component_factory_load_dir_recursive(factory,
176 file_path);
177 if (ret != BT_COMPONENT_FACTORY_STATUS_OK) {
178 goto end;
179 }
180 } else if (S_ISREG(st.st_mode)) {
181 ret = bt_component_factory_load_file(factory,
182 file_path);
183 if (ret != BT_COMPONENT_FACTORY_STATUS_OK) {
184 goto end;
185 }
186 }
187 }
188 end:
189 free(entry);
190 free(file_path);
191 return ret;
192 }
193
194 void bt_component_factory_destroy(struct bt_component_factory *factory)
195 {
196 if (!factory) {
197 return;
198 }
199
200 g_free(factory);
201 }
202
203 struct bt_component_factory *
204 bt_component_factory_create(void)
205 {
206 struct bt_component_factory *factory;
207
208 factory = g_new0(struct bt_component_factory, 1);
209 if (!factory) {
210 goto end;
211 }
212
213 factory->modules = g_ptr_array_new_with_free_func(module_close);
214 if (!factory->modules) {
215 goto error;
216 }
217
218 factory->components = g_ptr_array_new_with_free_func(factory_destroy);
219 if (factory->components) {
220 goto error;
221 }
222 end:
223 return factory;
224 error:
225 bt_component_factory_destroy(factory);
226 return NULL;
227 }
228
229 enum bt_component_factory_status bt_component_factory_load(
230 struct bt_component_factory *factory, const char *path)
231 {
232 enum bt_component_factory_status ret = BT_COMPONENT_FACTORY_STATUS_OK;
233 DIR *directory = NULL;
234
235 if (!factory || !path) {
236 ret = BT_COMPONENT_FACTORY_STATUS_INVAL;
237 goto end;
238 }
239
240 directory = opendir(path) ;
241 if (!directory) {
242 switch (errno) {
243 case ENOTDIR:
244 /* Try loading as a file. */
245 break;
246 case ENOENT:
247 ret = BT_COMPONENT_FACTORY_STATUS_NOENT;
248 goto end;
249 default:
250 ret = BT_COMPONENT_FACTORY_STATUS_IO;
251 goto end;
252 }
253 }
254
255 if (directory) {
256 ret = bt_component_factory_load_dir_recursive(factory, path);
257 } else {
258 ret = bt_component_factory_load_file(factory, path);
259 }
260 end:
261 if (directory) {
262 closedir(directory);
263 }
264 return ret;
265 }
This page took 0.033096 seconds and 3 git commands to generate.