xref: /minix/lib/libc/citrus/citrus_module.c (revision 0a6a1f1d)
1 /*	$NetBSD: citrus_module.c,v 1.12 2015/08/28 11:45:02 joerg 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.12 2015/08/28 11:45:02 joerg 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 <stddef.h>
103 #include <paths.h>
104 #include "citrus_namespace.h"
105 #include "citrus_bcs.h"
106 #include "citrus_module.h"
107 
108 #include <sys/types.h>
109 #include <dirent.h>
110 #include <dlfcn.h>
111 
112 #ifdef _I18N_DYNAMIC
113 
114 static unsigned int _getdewey(int [], char *);
115 static int _cmpndewey(int [], unsigned int, int [], unsigned int);
116 static const char *_findshlib(char *, int *, int *);
117 
118 static const char *_pathI18nModule = NULL;
119 
120 /* from libexec/ld.aout_so/shlib.c */
121 #undef major
122 #undef minor
123 #define MAXDEWEY	3	/*ELF*/
124 
125 static unsigned int
_getdewey(int dewey[],char * cp)126 _getdewey(int dewey[], char *cp)
127 {
128 	unsigned int	i, n;
129 
130 	_DIAGASSERT(dewey != NULL);
131 	_DIAGASSERT(cp != NULL);
132 
133 	for (n = 0, i = 0; i < MAXDEWEY; i++) {
134 		if (*cp == '\0')
135 			break;
136 
137 		if (*cp == '.') cp++;
138 		if (*cp < '0' || '9' < *cp)
139 			return 0;
140 
141 		dewey[n++] = (int)_bcs_strtol(cp, &cp, 10);
142 	}
143 
144 	return n;
145 }
146 
147 /*
148  * Compare two dewey arrays.
149  * Return -1 if `d1' represents a smaller value than `d2'.
150  * Return  1 if `d1' represents a greater value than `d2'.
151  * Return  0 if equal.
152  */
153 static int
_cmpndewey(int d1[],unsigned int n1,int d2[],unsigned int n2)154 _cmpndewey(int d1[], unsigned int n1, int d2[], unsigned int n2)
155 {
156 	register unsigned int	i;
157 
158 	_DIAGASSERT(d1 != NULL);
159 	_DIAGASSERT(d2 != NULL);
160 
161 	for (i = 0; i < n1 && i < n2; i++) {
162 		if (d1[i] < d2[i])
163 			return -1;
164 		if (d1[i] > d2[i])
165 			return 1;
166 	}
167 
168 	if (n1 == n2)
169 		return 0;
170 
171 	if (i == n1)
172 		return -1;
173 
174 	if (i == n2)
175 		return 1;
176 
177 	/* XXX cannot happen */
178 	return 0;
179 }
180 
181 static const char *
_findshlib(char * name,int * majorp,int * minorp)182 _findshlib(char *name, int *majorp, int *minorp)
183 {
184 	int		dewey[MAXDEWEY];
185 	unsigned int	ndewey;
186 	int		tmp[MAXDEWEY];
187 	int		i;
188 	int		len;
189 	char		*lname;
190 	static char	path[PATH_MAX];
191 	int		major, minor;
192 	const char	*search_dirs[1];
193 	const int	n_search_dirs = 1;
194 
195 	_DIAGASSERT(name != NULL);
196 	_DIAGASSERT(majorp != NULL);
197 	_DIAGASSERT(minorp != NULL);
198 
199 	major = *majorp;
200 	minor = *minorp;
201 	path[0] = '\0';
202 	search_dirs[0] = _pathI18nModule;
203 	len = strlen(name);
204 	lname = name;
205 
206 	ndewey = 0;
207 
208 	for (i = 0; i < n_search_dirs; i++) {
209 		DIR		*dd = opendir(search_dirs[i]);
210 		struct dirent	*dp;
211 		int		found_dot_a = 0;
212 		int		found_dot_so = 0;
213 
214 		if (dd == NULL)
215 			continue;
216 
217 		while ((dp = readdir(dd)) != NULL) {
218 			unsigned int	n;
219 
220 			if (dp->d_namlen < len + 4)
221 				continue;
222 			if (strncmp(dp->d_name, lname, (size_t)len) != 0)
223 				continue;
224 			if (strncmp(dp->d_name+len, ".so.", 4) != 0)
225 				continue;
226 
227 			if ((n = _getdewey(tmp, dp->d_name+len+4)) == 0)
228 				continue;
229 
230 			if (major != -1 && found_dot_a)
231 				found_dot_a = 0;
232 
233 			/* XXX should verify the library is a.out/ELF? */
234 
235 			if (major == -1 && minor == -1) {
236 				goto compare_version;
237 			} else if (major != -1 && minor == -1) {
238 				if (tmp[0] == major)
239 					goto compare_version;
240 			} else if (major != -1 && minor != -1) {
241 				if (tmp[0] == major) {
242 					if (n == 1 || tmp[1] >= minor)
243 						goto compare_version;
244 				}
245 			}
246 
247 			/* else, this file does not qualify */
248 			continue;
249 
250 		compare_version:
251 			if (_cmpndewey(tmp, n, dewey, ndewey) <= 0)
252 				continue;
253 
254 			/* We have a better version */
255 			found_dot_so = 1;
256 			snprintf(path, sizeof(path), "%s/%s", search_dirs[i],
257 			    dp->d_name);
258 			found_dot_a = 0;
259 			bcopy(tmp, dewey, sizeof(dewey));
260 			ndewey = n;
261 			*majorp = dewey[0];
262 			*minorp = dewey[1];
263 		}
264 		closedir(dd);
265 
266 		if (found_dot_a || found_dot_so)
267 			/*
268 			 * There's a lib in this dir; take it.
269 			 */
270 			return path[0] ? path : NULL;
271 	}
272 
273 	return path[0] ? path : NULL;
274 }
275 
276 void *
_citrus_find_getops(_citrus_module_t handle,const char * modname,const char * ifname)277 _citrus_find_getops(_citrus_module_t handle, const char *modname,
278 		    const char *ifname)
279 {
280 	char name[PATH_MAX];
281 	void *p;
282 
283 	_DIAGASSERT(handle != NULL);
284 	_DIAGASSERT(modname != NULL);
285 	_DIAGASSERT(ifname != NULL);
286 
287 	snprintf(name, sizeof(name), _C_LABEL_STRING("_citrus_%s_%s_getops"),
288 	    modname, ifname);
289 	p = dlsym((void *)handle, name);
290 	return p;
291 }
292 
293 int
_citrus_load_module(_citrus_module_t * rhandle,const char * encname)294 _citrus_load_module(_citrus_module_t *rhandle, const char *encname)
295 {
296 	const char *p;
297 	char path[PATH_MAX];
298 	int maj, min;
299 	void *handle;
300 
301 	_DIAGASSERT(rhandle != NULL);
302 
303 	if (_pathI18nModule == NULL) {
304 		p = getenv("PATH_I18NMODULE");
305 		if (p == NULL || issetugid()) {
306 			_pathI18nModule = _PATH_I18NMODULE;
307 #ifdef MLIBDIR
308 			p = strrchr(_pathI18nModule, '/');
309 			if (p != NULL) {
310 				snprintf(path, sizeof(path), "%.*s/%s/%s",
311 				    (int)(p - _pathI18nModule),
312 				    _pathI18nModule, MLIBDIR, p + 1);
313 				p = path;
314 			} else
315 				p = NULL;
316 #endif
317 		}
318 		if (p != NULL) {
319 			_pathI18nModule = strdup(p);
320 			if (_pathI18nModule == NULL)
321 				return ENOMEM;
322 		}
323 	}
324 
325 	(void)snprintf(path, sizeof(path), "lib%s", encname);
326 	maj = I18NMODULE_MAJOR;
327 	min = -1;
328 	p = _findshlib(path, &maj, &min);
329 	if (!p)
330 		return (EINVAL);
331 	handle = dlopen(p, RTLD_LAZY);
332 	if (!handle)
333 		return (EINVAL);
334 
335 	*rhandle = (_citrus_module_t)handle;
336 
337 	return (0);
338 }
339 
340 void
_citrus_unload_module(_citrus_module_t handle)341 _citrus_unload_module(_citrus_module_t handle)
342 {
343 	if (handle)
344 		dlclose((void *)handle);
345 }
346 #else
347 /* !_I18N_DYNAMIC */
348 
349 void *
350 /*ARGSUSED*/
_citrus_find_getops(_citrus_module_t handle,const char * modname,const char * ifname)351 _citrus_find_getops(_citrus_module_t handle, const char *modname,
352 		    const char *ifname)
353 {
354 	return (NULL);
355 }
356 
357 int
358 /*ARGSUSED*/
_citrus_load_module(_citrus_module_t * rhandle,char const * modname)359 _citrus_load_module(_citrus_module_t *rhandle, char const *modname)
360 {
361 	return (EINVAL);
362 }
363 
364 void
365 /*ARGSUSED*/
_citrus_unload_module(_citrus_module_t handle)366 _citrus_unload_module(_citrus_module_t handle)
367 {
368 }
369 #endif
370