1 /* $NetBSD: src/lib/libc/citrus/citrus_module.c,v 1.4 2004/12/21 09:00:01 yamt Exp $ */
2 /* $DragonFly: src/lib/libc/citrus/citrus_module.c,v 1.4 2005/11/12 22:21:13 swildner Exp $ */
5 * Copyright (c)1999, 2000, 2001, 2002 Citrus Project,
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Copyright (c) 1998 The NetBSD Foundation, Inc.
32 * All rights reserved.
34 * This code is derived from software contributed to The NetBSD Foundation
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the NetBSD
48 * Foundation, Inc. and its contributors.
49 * 4. Neither the name of The NetBSD Foundation nor the names of its
50 * contributors may be used to endorse or promote products derived
51 * from this software without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
54 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
55 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
56 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
57 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
58 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
59 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
60 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
61 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
62 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
63 * POSSIBILITY OF SUCH DAMAGE.
68 * The Regents of the University of California. All rights reserved.
70 * This code is derived from software contributed to Berkeley by
71 * Paul Borman at Krystal Technologies.
73 * Redistribution and use in source and binary forms, with or without
74 * modification, are permitted provided that the following conditions
76 * 1. Redistributions of source code must retain the above copyright
77 * notice, this list of conditions and the following disclaimer.
78 * 2. Redistributions in binary form must reproduce the above copyright
79 * notice, this list of conditions and the following disclaimer in the
80 * documentation and/or other materials provided with the distribution.
81 * 3. Neither the name of the University nor the names of its contributors
82 * may be used to endorse or promote products derived from this software
83 * without specific prior written permission.
85 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
86 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
87 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
88 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
89 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
90 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
91 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
92 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
93 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
94 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
108 #include "citrus_module.h"
110 #include <sys/types.h>
116 static int _getdewey(int [], char *);
117 static int _cmpndewey(int [], int, int [], int);
118 static const char *_findshlib(char *, int *, int *);
120 static const char *_pathI18nModule = NULL;
122 /* from libexec/ld.aout_so/shlib.c */
125 #define MAXDEWEY 3 /*ELF*/
128 _getdewey(int dewey[], char *cp)
132 _DIAGASSERT(dewey != NULL);
133 _DIAGASSERT(cp != NULL);
135 for (n = 0, i = 0; i < MAXDEWEY; i++) {
139 if (*cp == '.') cp++;
140 if (*cp < '0' || '9' < *cp)
143 dewey[n++] = (int)strtol(cp, &cp, 10);
150 * Compare two dewey arrays.
151 * Return -1 if `d1' represents a smaller value than `d2'.
152 * Return 1 if `d1' represents a greater value than `d2'.
156 _cmpndewey(int d1[], int n1, int d2[], int n2)
160 _DIAGASSERT(d1 != NULL);
161 _DIAGASSERT(d2 != NULL);
163 for (i = 0; i < n1 && i < n2; i++) {
179 /* XXX cannot happen */
184 _findshlib(char *name, int *majorp, int *minorp)
192 static char path[PATH_MAX];
194 const char *search_dirs[1];
195 const int n_search_dirs = 1;
197 _DIAGASSERT(name != NULL);
198 _DIAGASSERT(majorp != NULL);
199 _DIAGASSERT(minorp != NULL);
204 search_dirs[0] = _pathI18nModule;
210 for (i = 0; i < n_search_dirs; i++) {
211 DIR *dd = opendir(search_dirs[i]);
214 int found_dot_so = 0;
219 while ((dp = readdir(dd)) != NULL) {
222 if (dp->d_namlen < len + 4)
224 if (strncmp(dp->d_name, lname, (size_t)len) != 0)
226 if (strncmp(dp->d_name+len, ".so.", 4) != 0)
229 if ((n = _getdewey(tmp, dp->d_name+len+4)) == 0)
232 if (major != -1 && found_dot_a)
235 /* XXX should verify the library is a.out/ELF? */
237 if (major == -1 && minor == -1) {
238 goto compare_version;
239 } else if (major != -1 && minor == -1) {
241 goto compare_version;
242 } else if (major != -1 && minor != -1) {
243 if (tmp[0] == major) {
244 if (n == 1 || tmp[1] >= minor)
245 goto compare_version;
249 /* else, this file does not qualify */
253 if (_cmpndewey(tmp, n, dewey, ndewey) <= 0)
256 /* We have a better version */
258 snprintf(path, sizeof(path), "%s/%s", search_dirs[i],
261 bcopy(tmp, dewey, sizeof(dewey));
268 if (found_dot_a || found_dot_so)
270 * There's a lib in this dir; take it.
272 return path[0] ? path : NULL;
275 return path[0] ? path : NULL;
279 _citrus_find_getops(_citrus_module_t handle, const char *modname,
285 _DIAGASSERT(handle != NULL);
286 _DIAGASSERT(modname != NULL);
287 _DIAGASSERT(ifname != NULL);
289 snprintf(name, sizeof(name), "_citrus_%s_%s_getops", modname, ifname);
290 p = dlsym((void *)handle, name);
295 _citrus_load_module(_citrus_module_t *rhandle, const char *encname)
302 _DIAGASSERT(rhandle != NULL);
304 if (_pathI18nModule == NULL) {
305 p = getenv("PATH_I18NMODULE");
306 if (p != NULL && !issetugid()) {
307 _pathI18nModule = strdup(p);
308 if (_pathI18nModule == NULL)
311 _pathI18nModule = _PATH_I18NMODULE;
314 snprintf(path, sizeof(path), "lib%s", encname);
315 maj = I18NMODULE_MAJOR;
317 p = _findshlib(path, &maj, &min);
320 handle = dlopen(p, RTLD_LAZY);
324 *rhandle = (_citrus_module_t)handle;
330 _citrus_unload_module(_citrus_module_t handle)
333 dlclose((void *)handle);
338 SET_DECLARE(citrus_set, struct citrus_metadata);
340 struct citrus_metadata empty = {
344 DATA_SET(citrus_set, empty);
346 #define MAGIC_HANDLE (void *)(0xC178C178)
350 _citrus_find_getops(_citrus_module_t handle __unused, const char *modname,
353 struct citrus_metadata **mdp, *mod;
355 _DIAGASSERT(handle == MAGIC_HANDLE);
357 SET_FOREACH(mdp, citrus_set) {
359 if (mod == NULL || mod->module_name == NULL || mod->interface_name == NULL)
361 if (strcmp(mod->module_name, modname) != 0)
363 if (strcmp(mod->interface_name, ifname) != 0)
365 return(mod->module_ops);
372 _citrus_load_module(_citrus_module_t *rhandle, char const *modname)
374 struct citrus_metadata **mdp, *mod;
376 SET_FOREACH(mdp, citrus_set) {
378 if (mod == NULL || mod->module_name == NULL)
380 if (strcmp(mod->module_name, modname) != 0)
382 *rhandle = MAGIC_HANDLE;
390 _citrus_unload_module(_citrus_module_t handle __unused)