2 /* $NetBSD: citrus_module.c,v 1.9 2009/01/11 02:46:24 christos Exp $ */
5 * SPDX-License-Identifier: BSD-2-Clause
7 * Copyright (c)1999, 2000, 2001, 2002 Citrus Project,
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Copyright (c) 1998 The NetBSD Foundation, Inc.
34 * All rights reserved.
36 * This code is derived from software contributed to The NetBSD Foundation
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
48 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
49 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
50 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
51 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
52 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
53 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
54 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
55 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
56 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
57 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
58 * POSSIBILITY OF SUCH DAMAGE.
63 * The Regents of the University of California. All rights reserved.
65 * This code is derived from software contributed to Berkeley by
66 * Paul Borman at Krystal Technologies.
68 * Redistribution and use in source and binary forms, with or without
69 * modification, are permitted provided that the following conditions
71 * 1. Redistributions of source code must retain the above copyright
72 * notice, this list of conditions and the following disclaimer.
73 * 2. Redistributions in binary form must reproduce the above copyright
74 * notice, this list of conditions and the following disclaimer in the
75 * documentation and/or other materials provided with the distribution.
76 * 3. Neither the name of the University nor the names of its contributors
77 * may be used to endorse or promote products derived from this software
78 * without specific prior written permission.
80 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
81 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
82 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
83 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
84 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
85 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
86 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
87 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
88 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
89 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
93 #include <sys/cdefs.h>
94 #include <sys/types.h>
109 #define I18NMODULE_MAJOR 4
111 #include "citrus_namespace.h"
112 #include "citrus_bcs.h"
113 #include "citrus_module.h"
114 #include "libc_private.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 for (n = 0, i = 0; i < MAXDEWEY; i++) {
136 if (*cp == '.') cp++;
137 if (*cp < '0' || '9' < *cp)
140 dewey[n++] = (int)_bcs_strtol(cp, &cp, 10);
147 * Compare two dewey arrays.
148 * Return -1 if `d1' represents a smaller value than `d2'.
149 * Return 1 if `d1' represents a greater value than `d2'.
153 _cmpndewey(int d1[], int n1, int d2[], int n2)
157 for (i = 0; i < n1 && i < n2; i++) {
178 _findshlib(char *name, int *majorp, int *minorp)
181 const char *search_dirs[1];
182 static char path[PATH_MAX];
183 int dewey[MAXDEWEY], tmp[MAXDEWEY];
184 int i, len, major, minor, ndewey, n_search_dirs;
190 search_dirs[0] = _pathI18nModule;
196 for (i = 0; i < n_search_dirs; i++) {
198 DIR *dd = opendir(search_dirs[i]);
199 int found_dot_a = 0, found_dot_so = 0;
204 while ((dp = readdir(dd)) != NULL) {
207 if (dp->d_namlen < len + 4)
209 if (strncmp(dp->d_name, lname, (size_t)len) != 0)
211 if (strncmp(dp->d_name+len, ".so.", 4) != 0)
214 if ((n = _getdewey(tmp, dp->d_name+len+4)) == 0)
217 if (major != -1 && found_dot_a)
220 /* XXX should verify the library is a.out/ELF? */
222 if (major == -1 && minor == -1)
223 goto compare_version;
224 else if (major != -1 && minor == -1) {
226 goto compare_version;
227 } else if (major != -1 && minor != -1) {
228 if (tmp[0] == major) {
229 if (n == 1 || tmp[1] >= minor)
230 goto compare_version;
234 /* else, this file does not qualify */
238 if (_cmpndewey(tmp, n, dewey, ndewey) <= 0)
241 /* We have a better version */
243 snprintf(path, sizeof(path), "%s/%s", search_dirs[i],
246 bcopy(tmp, dewey, sizeof(dewey));
253 if (found_dot_a || found_dot_so)
255 * There's a lib in this dir; take it.
257 return (path[0] ? path : NULL);
260 return (path[0] ? path : NULL);
264 _citrus_find_getops(_citrus_module_t handle, const char *modname,
270 snprintf(name, sizeof(name), "_citrus_%s_%s_getops",
272 p = dlsym((void *)handle, name);
277 _citrus_load_module(_citrus_module_t *rhandle, const char *encname)
284 if (_pathI18nModule == NULL) {
285 p = getenv("PATH_I18NMODULE");
286 if (p != NULL && !issetugid()) {
287 _pathI18nModule = strdup(p);
288 if (_pathI18nModule == NULL)
291 _pathI18nModule = _PATH_I18NMODULE;
294 (void)snprintf(path, sizeof(path), "lib%s", encname);
295 maj = I18NMODULE_MAJOR;
297 p = _findshlib(path, &maj, &min);
300 handle = libc_dlopen(p, RTLD_LAZY);
302 printf("%s", dlerror());
306 *rhandle = (_citrus_module_t)handle;
312 _citrus_unload_module(_citrus_module_t handle)
316 dlclose((void *)handle);