2 /* $NetBSD: citrus_module.c,v 1.9 2009/01/11 02:46:24 christos 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.
46 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
47 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
48 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
49 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
50 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
51 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
52 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
53 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
54 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
55 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
56 * POSSIBILITY OF SUCH DAMAGE.
61 * The Regents of the University of California. All rights reserved.
63 * This code is derived from software contributed to Berkeley by
64 * Paul Borman at Krystal Technologies.
66 * Redistribution and use in source and binary forms, with or without
67 * modification, are permitted provided that the following conditions
69 * 1. Redistributions of source code must retain the above copyright
70 * notice, this list of conditions and the following disclaimer.
71 * 2. Redistributions in binary form must reproduce the above copyright
72 * notice, this list of conditions and the following disclaimer in the
73 * documentation and/or other materials provided with the distribution.
74 * 3. Neither the name of the University nor the names of its contributors
75 * may be used to endorse or promote products derived from this software
76 * without specific prior written permission.
78 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
79 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
80 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
81 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
82 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
83 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
84 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
85 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
86 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
87 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
91 #include <sys/cdefs.h>
92 #include <sys/types.h>
107 #define I18NMODULE_MAJOR 4
109 #include "citrus_namespace.h"
110 #include "citrus_bcs.h"
111 #include "citrus_module.h"
112 #include "libc_private.h"
114 static int _getdewey(int[], char *);
115 static int _cmpndewey(int[], int, int[], int);
116 static const char *_findshlib(char *, int *, int *);
118 static const char *_pathI18nModule = NULL;
120 /* from libexec/ld.aout_so/shlib.c */
123 #define MAXDEWEY 3 /*ELF*/
126 _getdewey(int dewey[], char *cp)
130 for (n = 0, i = 0; i < MAXDEWEY; i++) {
134 if (*cp == '.') cp++;
135 if (*cp < '0' || '9' < *cp)
138 dewey[n++] = (int)_bcs_strtol(cp, &cp, 10);
145 * Compare two dewey arrays.
146 * Return -1 if `d1' represents a smaller value than `d2'.
147 * Return 1 if `d1' represents a greater value than `d2'.
151 _cmpndewey(int d1[], int n1, int d2[], int n2)
155 for (i = 0; i < n1 && i < n2; i++) {
176 _findshlib(char *name, int *majorp, int *minorp)
179 const char *search_dirs[1];
180 static char path[PATH_MAX];
181 int dewey[MAXDEWEY], tmp[MAXDEWEY];
182 int i, len, major, minor, ndewey, n_search_dirs;
188 search_dirs[0] = _pathI18nModule;
194 for (i = 0; i < n_search_dirs; i++) {
196 DIR *dd = opendir(search_dirs[i]);
197 int found_dot_a = 0, found_dot_so = 0;
202 while ((dp = readdir(dd)) != NULL) {
205 if (dp->d_namlen < len + 4)
207 if (strncmp(dp->d_name, lname, (size_t)len) != 0)
209 if (strncmp(dp->d_name+len, ".so.", 4) != 0)
212 if ((n = _getdewey(tmp, dp->d_name+len+4)) == 0)
215 if (major != -1 && found_dot_a)
218 /* XXX should verify the library is a.out/ELF? */
220 if (major == -1 && minor == -1)
221 goto compare_version;
222 else if (major != -1 && minor == -1) {
224 goto compare_version;
225 } else if (major != -1 && minor != -1) {
226 if (tmp[0] == major) {
227 if (n == 1 || tmp[1] >= minor)
228 goto compare_version;
232 /* else, this file does not qualify */
236 if (_cmpndewey(tmp, n, dewey, ndewey) <= 0)
239 /* We have a better version */
241 snprintf(path, sizeof(path), "%s/%s", search_dirs[i],
244 bcopy(tmp, dewey, sizeof(dewey));
251 if (found_dot_a || found_dot_so)
253 * There's a lib in this dir; take it.
255 return (path[0] ? path : NULL);
258 return (path[0] ? path : NULL);
262 _citrus_find_getops(_citrus_module_t handle, const char *modname,
268 snprintf(name, sizeof(name), "_citrus_%s_%s_getops",
270 p = dlsym((void *)handle, name);
275 _citrus_load_module(_citrus_module_t *rhandle, const char *encname)
282 if (_pathI18nModule == NULL) {
283 p = getenv("PATH_I18NMODULE");
284 if (p != NULL && !issetugid()) {
285 _pathI18nModule = strdup(p);
286 if (_pathI18nModule == NULL)
289 _pathI18nModule = _PATH_I18NMODULE;
292 (void)snprintf(path, sizeof(path), "lib%s", encname);
293 maj = I18NMODULE_MAJOR;
295 p = _findshlib(path, &maj, &min);
298 handle = libc_dlopen(p, RTLD_LAZY);
300 printf("%s", dlerror());
304 *rhandle = (_citrus_module_t)handle;
310 _citrus_unload_module(_citrus_module_t handle)
314 dlclose((void *)handle);