Debian Codegraph @

src/main/module.c

raw symbols SCIP
1 /*
2 *
3 * Gutenprint module loader - load modules with libltdl/libdl.
4 *
5 * Copyright 2002 Roger Leigh ([email protected])
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24 #include <gutenprint/gutenprint.h>
25 #include "gutenprint-internal.h"
26 #include <gutenprint/gutenprint-intl-internal.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <libgen.h>
31 #include <errno.h>
32 #include <unistd.h>
33 #include <sys/param.h>
34
35 typedef struct stpi_internal_module_class
36 {
37 stp_module_class_t class;
38 const char *description;
39 } stpi_internal_module_class_t;
40
41
42 static void module_list_freefunc(void *item);
43 static int stp_module_register(stp_module_t *module);
44 #if defined(MODULE) && defined(USE_DLOPEN)
45 static void *stp_dlsym(void *handle, const char *symbol, const char *modulename);
46 #endif
47
48 #ifdef __GNUC__
49 #define UNUSED __attribute__ ((unused))
50 #else
51 #define UNUSED
52 #endif
53
54 static const stpi_internal_module_class_t UNUSED module_classes[] =
55 {
56 {STP_MODULE_CLASS_MISC, N_("Miscellaneous (unclassified)")},
57 {STP_MODULE_CLASS_FAMILY, N_("Family driver")},
58 {STP_MODULE_CLASS_COLOR, N_("Color conversion module")},
59 {STP_MODULE_CLASS_DITHER, N_("Dither algorithm")},
60 {STP_MODULE_CLASS_INVALID, NULL} /* Must be last */
61 };
62
63 #if !defined(MODULE)
64 extern stp_module_t print_canon_LTX_stp_module_data;
65 extern stp_module_t print_escp2_LTX_stp_module_data;
66 extern stp_module_t print_lexmark_LTX_stp_module_data;
67 extern stp_module_t print_pcl_LTX_stp_module_data;
68 extern stp_module_t print_dpl_LTX_stp_module_data;
69 extern stp_module_t print_ps_LTX_stp_module_data;
70 extern stp_module_t print_dyesub_LTX_stp_module_data;
71 extern stp_module_t print_raw_LTX_stp_module_data;
72 extern stp_module_t color_traditional_LTX_stp_module_data;
73
74 /*
75 * A list of modules, for use when the modules are linked statically.
76 */
77 static stp_module_t *static_modules[] =
78 {
79 &print_ps_LTX_stp_module_data,
80 &print_canon_LTX_stp_module_data,
81 &print_escp2_LTX_stp_module_data,
82 &print_pcl_LTX_stp_module_data,
83 &print_dpl_LTX_stp_module_data,
84 &print_lexmark_LTX_stp_module_data,
85 &print_dyesub_LTX_stp_module_data,
86 &print_raw_LTX_stp_module_data,
87 &color_traditional_LTX_stp_module_data,
88 NULL
89 };
90 #endif
91
92 static stp_list_t *module_list = NULL;
93
94
95 /*
96 * Callback for removing a module from stp_module_list.
97 */
98 static void
99 module_list_freefunc(void *item /* module to remove */)
100 {
101 stp_module_t *module = (stp_module_t *) item;
102 if (module && module->fini) /* Call the module exit function */
103 module->fini();
104 #if defined(MODULE)
105 if (module && module->handle)
106 DLCLOSE(module->handle); /* Close the module if it's not static */
107 #endif
108 }
109
110
111 /*
112 * Load all available modules. Return nonzero on failure.
113 */
114 int stp_module_load(void)
115 {
116 /* initialise libltdl */
117 #ifdef USE_LTDL
118 static int ltdl_is_initialised = 0; /* Is libltdl initialised? */
119 #endif
120 static int module_list_is_initialised = 0; /* Is the module list initialised? */
121 #if defined(MODULE)
122 stp_list_t *dir_list; /* List of directories to scan */
123 stp_list_t *file_list; /* List of modules to open */
124 stp_list_item_t *file; /* Pointer to current module */
125 #endif
126
127 #ifdef USE_LTDL
128 if (!ltdl_is_initialised)
129 {
130 if (lt_dlinit())
131 {
132 stp_erprintf("Error initialising libltdl: %s\n", DLERROR());
133 return 1;
134 }
135 ltdl_is_initialised = 1;
136 }
137 /* set default search paths */
138 lt_dladdsearchdir(PKGMODULEDIR);
139 #endif
140
141 /* initialise module_list */
142 if (!module_list_is_initialised)
143 {
144 if (!(module_list = stp_list_create()))
145 return 1;
146 stp_list_set_freefunc(module_list, module_list_freefunc);
147 module_list_is_initialised = 1;
148 }
149
150 /* search for available modules */
151 #if defined(MODULE)
152 if (!(dir_list = stp_list_create()))
153 return 1;
154 stp_list_set_freefunc(dir_list, stp_list_node_free_data);
155 if (getenv("STP_MODULE_PATH"))
156 {
157 stp_path_split(dir_list, getenv("STP_MODULE_PATH"));
158 }
159 else
160 {
161 #ifdef USE_LTDL
162 stp_path_split(dir_list, getenv("LTDL_LIBRARY_PATH"));
163 stp_path_split(dir_list, lt_dlgetsearchpath());
164 #else
165 stp_path_split(dir_list, PKGMODULEDIR);
166 #endif
167 }
168 #ifdef USE_LTDL
169 file_list = stp_path_search(dir_list, ".la");
170 #else
171 file_list = stp_path_search(dir_list, ".so");
172 #endif
173 stp_list_destroy(dir_list);
174
175 /* load modules */
176 file = stp_list_get_start(file_list);
177 while (file)
178 {
179 stp_module_open((const char *) stp_list_item_get_data(file));
180 file = stp_list_item_next(file);
181 }
182
183 stp_list_destroy(file_list);
184 #else /* use a static module list */
185 {
186 int i=0;
187 while (static_modules[i])
188 {
189 stp_module_register(static_modules[i]);
190 i++;
191 }
192 }
193 #endif
194 return 0;
195 }
196
197
198 /*
199 * Unload all modules and clean up.
200 */
201 int
202 stp_module_exit(void)
203 {
204 /* destroy the module list (modules unloaded by callback) */
205 if (module_list)
206 stp_list_destroy(module_list);
207 /* shut down libltdl (forces close of any unclosed modules) */
208 #ifdef USE_LTDL
209 return lt_dlexit();
210 #else
211 return 0;
212 #endif
213 }
214
215
216 /*
217 * Find all modules in a given class.
218 */
219 stp_list_t *
220 stp_module_get_class(stp_module_class_t class /* Module class */)
221 {
222 stp_list_t *list; /* List to return */
223 stp_list_item_t *ln; /* Module to check*/
224
225 list = stp_list_create(); /* No freefunc, so it can be destroyed
226 without unloading any modules! */
227 if (!list)
228 return NULL;
229
230 ln = stp_list_get_start(module_list);
231 while (ln)
232 {
233 /* Add modules of the same class to our list */
234 if (((stp_module_t *) stp_list_item_get_data(ln))->class == class)
235 stp_list_item_create(list, NULL, stp_list_item_get_data(ln));
236 ln = stp_list_item_next(ln);
237 }
238 return list;
239 }
240
241
242 /*
243 * Open a module.
244 */
245 int
246 stp_module_open(const char *modulename /* Module filename */)
247 {
248 #if defined(MODULE)
249 #ifdef USE_LTDL
250 lt_dlhandle module; /* Handle for module */
251 #else
252 void *module; /* Handle for module */
253 #endif
254 stp_module_version_t *version; /* Module version */
255 stp_module_t *data; /* Module data */
256 stp_list_item_t *reg_module; /* Pointer to module list nodes */
257 int error = 0; /* Error status */
258
259 stp_deprintf(STP_DBG_MODULE, "stp-module: open: %s\n", modulename);
260 while(1)
261 {
262 module = DLOPEN(modulename);
263 if (!module)
264 break;
265
266 /* check version is valid */
267 version = (stp_module_version_t *) DLSYM(module, "stp_module_version");
268 if (!version)
269 break;
270 if (version->major != 1 && version->minor < 0)
271 break;
272
273 data = (void *) DLSYM(module, "stp_module_data");
274 if (!data)
275 break;
276 data->handle = module; /* store module handle */
277
278 /* check same module isn't already loaded */
279 reg_module = stp_list_get_start(module_list);
280 while (reg_module)
281 {
282 if (!strcmp(data->name, ((stp_module_t *)
283 stp_list_item_get_data(reg_module))->name) &&
284 data->class == ((stp_module_t *)
285 stp_list_item_get_data(reg_module))->class)
286 {
287 stp_deprintf(STP_DBG_MODULE,
288 "stp-module: reject duplicate: %s\n",
289 data->name);
290 error = 1;
291 break;
292 }
293 reg_module = stp_list_item_next(reg_module);
294 }
295 if (error)
296 break;
297
298 /* Register the module */
299 if (stp_module_register(data))
300 break;
301
302 return 0;
303 }
304
305 if (module)
306 DLCLOSE(module);
307 #else
308 (void)modulename;
309 #endif
310 return 1;
311 }
312
313
314 /*
315 * Register a loaded module.
316 */
317 static int stp_module_register(stp_module_t *module /* Module to register */)
318 {
319 /* Add to the module list */
320 if (stp_list_item_create(module_list, NULL, module))
321 return 1;
322
323 if (module->class == STP_MODULE_CLASS_FAMILY)
324 {
325 char buf[MAXPATHLEN+1];
326 (void) snprintf(buf, MAXPATHLEN, "printers/%s.xml", module->name);
327 stp_deprintf(STP_DBG_MODULE, "stp-module: attempting to load: %s\n", buf);
328 stp_xml_parse_file_named(buf);
329 }
330 stp_deprintf(STP_DBG_MODULE, "stp-module: register: %s\n", module->name);
331 return 0;
332 }
333
334
335 /*
336 * Initialise all loaded modules
337 */
338 int stp_module_init(void)
339 {
340 stp_list_item_t *module_item; /* Module list pointer */
341 stp_module_t *module; /* Module to initialise */
342
343 module_item = stp_list_get_start(module_list);
344 while (module_item)
345 {
346 module = (stp_module_t *) stp_list_item_get_data(module_item);
347 if (module)
348 {
349 stp_deprintf(STP_DBG_MODULE, "stp-module-init: %s\n", module->name);
350 /* Initialise module */
351 if (module->init && module->init())
352 {
353 stp_deprintf(STP_DBG_MODULE,
354 "stp-module-init: %s: Module init failed\n",
355 module->name);
356 }
357 }
358 module_item = stp_list_item_next(module_item);
359 }
360 stpi_find_duplicate_printers();
361 return 0;
362 }
363
364
365
366 /*
367 * Close a module.
368 */
369 int
370 stp_module_close(stp_list_item_t *module /* Module to close */)
371 {
372 return stp_list_item_destroy(module_list, module);
373 }
374
375
376 /*
377 * If using dlopen, add modulename_LTX_ to symbol name
378 */
379 #if defined(MODULE) && defined(USE_DLOPEN)
380 static void *stp_dlsym(void *handle, /* Module */
381 const char *symbol, /* Symbol name */
382 const char *modulename) /* Module name */
383 {
384 int len; /* Length of string */
385 static char *full_symbol = NULL; /* Symbol to return */
386 char *module; /* Real module name */
387 char *tmp = stp_strdup(modulename); /* Temporary string */
388
389 module = basename(tmp);
390
391 if (full_symbol)
392 {
393 stp_free (full_symbol);
394 full_symbol = NULL;
395 }
396
397 /* "_LTX_" + '\0' - ".so" */
398 len = strlen(symbol) + strlen(module) + 3;
399 full_symbol = (char *) stp_malloc(sizeof(char) * len);
400
401 len = 0;
402 strncpy (full_symbol, module, strlen(module) - 3);
403 len = strlen(module) - 3;
404 strcpy (full_symbol+len, "_LTX_");
405 len += 5;
406 strcpy (full_symbol+len, symbol);
407 len += strlen(symbol);
408 full_symbol[len] = '\0';
409
410 #if defined(__OpenBSD__)
411 /* OpenBSD needs a prepended underscore to match symbols */
412 {
413 char *prefix_symbol = stp_malloc(sizeof(char) * (strlen(full_symbol) + 2));
414 prefix_symbol[0] = '_';
415 strcpy(prefix_symbol+1, full_symbol);
416 stp_free(full_symbol);
417 full_symbol = prefix_symbol;
418 }
419 #endif
420
421 /* Change any hyphens to underscores */
422 for (len = 0; full_symbol[len] != '\0'; len++)
423 if (full_symbol[len] == '-')
424 full_symbol[len] = '_';
425
426 stp_deprintf(STP_DBG_MODULE, "SYMBOL: %s\n", full_symbol);
427
428 stp_free(tmp);
429 return dlsym(handle, full_symbol);
430 }
431 #endif