xref: /netbsd-src/lib/libc/citrus/citrus_module.c (revision cd22f25e6f6d1cc1f197fe8c5468a80f51d1c4e1)
1 /*	$NetBSD: citrus_module.c,v 1.6 2008/04/28 20:22:58 martin Exp $	*/
2 
3 /*-
4  * Copyright (c)1999, 2000, 2001, 2002 Citrus Project,
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 /*-
30  * Copyright (c) 1998 The NetBSD Foundation, Inc.
31  * All rights reserved.
32  *
33  * This code is derived from software contributed to The NetBSD Foundation
34  * by Paul Kranenburg.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  *
45  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
46  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
47  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
48  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
49  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
50  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
51  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
52  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
53  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
55  * POSSIBILITY OF SUCH DAMAGE.
56  */
57 
58 /*-
59  * Copyright (c) 1993
60  *	The Regents of the University of California.  All rights reserved.
61  *
62  * This code is derived from software contributed to Berkeley by
63  * Paul Borman at Krystal Technologies.
64  *
65  * Redistribution and use in source and binary forms, with or without
66  * modification, are permitted provided that the following conditions
67  * are met:
68  * 1. Redistributions of source code must retain the above copyright
69  *    notice, this list of conditions and the following disclaimer.
70  * 2. Redistributions in binary form must reproduce the above copyright
71  *    notice, this list of conditions and the following disclaimer in the
72  *    documentation and/or other materials provided with the distribution.
73  * 3. Neither the name of the University nor the names of its contributors
74  *    may be used to endorse or promote products derived from this software
75  *    without specific prior written permission.
76  *
77  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
78  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
79  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
80  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
81  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
82  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
83  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
84  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
85  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
86  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
87  * SUCH DAMAGE.
88  */
89 
90 #include <sys/cdefs.h>
91 #if defined(LIBC_SCCS) && !defined(lint)
92 __RCSID("$NetBSD: citrus_module.c,v 1.6 2008/04/28 20:22:58 martin Exp $");
93 #endif /* LIBC_SCCS and not lint */
94 
95 #include <assert.h>
96 #include <errno.h>
97 #include <limits.h>
98 #include <string.h>
99 #include <stdio.h>
100 #include <stdlib.h>
101 #include <unistd.h>
102 #include <locale.h>
103 #include <stddef.h>
104 #include <paths.h>
105 #include "citrus_module.h"
106 
107 #include <sys/types.h>
108 #include <dirent.h>
109 #include <dlfcn.h>
110 
111 #ifdef _I18N_DYNAMIC
112 
113 static int _getdewey(int [], char *);
114 static int _cmpndewey(int [], int, int [], int);
115 static const char *_findshlib(char *, int *, int *);
116 
117 static const char *_pathI18nModule = NULL;
118 
119 /* from libexec/ld.aout_so/shlib.c */
120 #undef major
121 #undef minor
122 #define MAXDEWEY	3	/*ELF*/
123 
124 static int
125 _getdewey(int dewey[], char *cp)
126 {
127 	int	i, n;
128 
129 	_DIAGASSERT(dewey != NULL);
130 	_DIAGASSERT(cp != NULL);
131 
132 	for (n = 0, i = 0; i < MAXDEWEY; i++) {
133 		if (*cp == '\0')
134 			break;
135 
136 		if (*cp == '.') cp++;
137 		if (*cp < '0' || '9' < *cp)
138 			return 0;
139 
140 		dewey[n++] = (int)strtol(cp, &cp, 10);
141 	}
142 
143 	return n;
144 }
145 
146 /*
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'.
150  * Return  0 if equal.
151  */
152 static int
153 _cmpndewey(int d1[], int n1, int d2[], int n2)
154 {
155 	register int	i;
156 
157 	_DIAGASSERT(d1 != NULL);
158 	_DIAGASSERT(d2 != NULL);
159 
160 	for (i = 0; i < n1 && i < n2; i++) {
161 		if (d1[i] < d2[i])
162 			return -1;
163 		if (d1[i] > d2[i])
164 			return 1;
165 	}
166 
167 	if (n1 == n2)
168 		return 0;
169 
170 	if (i == n1)
171 		return -1;
172 
173 	if (i == n2)
174 		return 1;
175 
176 	/* XXX cannot happen */
177 	return 0;
178 }
179 
180 static const char *
181 _findshlib(char *name, int *majorp, int *minorp)
182 {
183 	int		dewey[MAXDEWEY];
184 	int		ndewey;
185 	int		tmp[MAXDEWEY];
186 	int		i;
187 	int		len;
188 	char		*lname;
189 	static char	path[PATH_MAX];
190 	int		major, minor;
191 	const char	*search_dirs[1];
192 	const int	n_search_dirs = 1;
193 
194 	_DIAGASSERT(name != NULL);
195 	_DIAGASSERT(majorp != NULL);
196 	_DIAGASSERT(minorp != NULL);
197 
198 	major = *majorp;
199 	minor = *minorp;
200 	path[0] = '\0';
201 	search_dirs[0] = _pathI18nModule;
202 	len = strlen(name);
203 	lname = name;
204 
205 	ndewey = 0;
206 
207 	for (i = 0; i < n_search_dirs; i++) {
208 		DIR		*dd = opendir(search_dirs[i]);
209 		struct dirent	*dp;
210 		int		found_dot_a = 0;
211 		int		found_dot_so = 0;
212 
213 		if (dd == NULL)
214 			continue;
215 
216 		while ((dp = readdir(dd)) != NULL) {
217 			int	n;
218 
219 			if (dp->d_namlen < len + 4)
220 				continue;
221 			if (strncmp(dp->d_name, lname, (size_t)len) != 0)
222 				continue;
223 			if (strncmp(dp->d_name+len, ".so.", 4) != 0)
224 				continue;
225 
226 			if ((n = _getdewey(tmp, dp->d_name+len+4)) == 0)
227 				continue;
228 
229 			if (major != -1 && found_dot_a)
230 				found_dot_a = 0;
231 
232 			/* XXX should verify the library is a.out/ELF? */
233 
234 			if (major == -1 && minor == -1) {
235 				goto compare_version;
236 			} else if (major != -1 && minor == -1) {
237 				if (tmp[0] == major)
238 					goto compare_version;
239 			} else if (major != -1 && minor != -1) {
240 				if (tmp[0] == major) {
241 					if (n == 1 || tmp[1] >= minor)
242 						goto compare_version;
243 				}
244 			}
245 
246 			/* else, this file does not qualify */
247 			continue;
248 
249 		compare_version:
250 			if (_cmpndewey(tmp, n, dewey, ndewey) <= 0)
251 				continue;
252 
253 			/* We have a better version */
254 			found_dot_so = 1;
255 			snprintf(path, sizeof(path), "%s/%s", search_dirs[i],
256 			    dp->d_name);
257 			found_dot_a = 0;
258 			bcopy(tmp, dewey, sizeof(dewey));
259 			ndewey = n;
260 			*majorp = dewey[0];
261 			*minorp = dewey[1];
262 		}
263 		closedir(dd);
264 
265 		if (found_dot_a || found_dot_so)
266 			/*
267 			 * There's a lib in this dir; take it.
268 			 */
269 			return path[0] ? path : NULL;
270 	}
271 
272 	return path[0] ? path : NULL;
273 }
274 
275 void *
276 _citrus_find_getops(_citrus_module_t handle, const char *modname,
277 		    const char *ifname)
278 {
279 	char name[PATH_MAX];
280 	void *p;
281 
282 	_DIAGASSERT(handle != NULL);
283 	_DIAGASSERT(modname != NULL);
284 	_DIAGASSERT(ifname != NULL);
285 
286 	snprintf(name, sizeof(name), _C_LABEL_STRING("_citrus_%s_%s_getops"),
287 	    modname, ifname);
288 	p = dlsym((void *)handle, name);
289 	return p;
290 }
291 
292 int
293 _citrus_load_module(_citrus_module_t *rhandle, const char *encname)
294 {
295 	const char *p;
296 	char path[PATH_MAX];
297 	int maj, min;
298 	void *handle;
299 
300 	_DIAGASSERT(rhandle != NULL);
301 
302 	if (_pathI18nModule == NULL) {
303 		p = getenv("PATH_I18NMODULE");
304 		if (p != NULL && !issetugid()) {
305 			_pathI18nModule = strdup(p);
306 			if (_pathI18nModule == NULL)
307 				return ENOMEM;
308 		} else
309 			_pathI18nModule = _PATH_I18NMODULE;
310 	}
311 
312 	(void)snprintf(path, sizeof(path), "lib%s", encname);
313 	maj = I18NMODULE_MAJOR;
314 	min = -1;
315 	p = _findshlib(path, &maj, &min);
316 	if (!p)
317 		return (EINVAL);
318 	handle = dlopen(p, RTLD_LAZY);
319 	if (!handle)
320 		return (EINVAL);
321 
322 	*rhandle = (_citrus_module_t)handle;
323 
324 	return (0);
325 }
326 
327 void
328 _citrus_unload_module(_citrus_module_t handle)
329 {
330 	if (handle)
331 		dlclose((void *)handle);
332 }
333 #else
334 /* !_I18N_DYNAMIC */
335 
336 void *
337 /*ARGSUSED*/
338 _citrus_find_getops(_citrus_module_t handle, const char *modname,
339 		    const char *ifname)
340 {
341 	return (NULL);
342 }
343 
344 int
345 /*ARGSUSED*/
346 _citrus_load_module(_citrus_module_t *rhandle, char const *modname)
347 {
348 	return (EINVAL);
349 }
350 
351 void
352 /*ARGSUSED*/
353 _citrus_unload_module(_citrus_module_t handle)
354 {
355 }
356 #endif
357