xref: /freebsd/lib/libc/iconv/citrus_module.c (revision 193d9e768ba63fcfb187cfd17f461f7d41345048)
1 /* $FreeBSD$ */
2 /* $NetBSD: citrus_module.c,v 1.9 2009/01/11 02:46:24 christos Exp $ */
3 
4 /*-
5  * Copyright (c)1999, 2000, 2001, 2002 Citrus Project,
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  */
29 
30 /*-
31  * Copyright (c) 1998 The NetBSD Foundation, Inc.
32  * All rights reserved.
33  *
34  * This code is derived from software contributed to The NetBSD Foundation
35  * by Paul Kranenburg.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
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  *
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.
57  */
58 
59 /*-
60  * Copyright (c) 1993
61  *	The Regents of the University of California.  All rights reserved.
62  *
63  * This code is derived from software contributed to Berkeley by
64  * Paul Borman at Krystal Technologies.
65  *
66  * Redistribution and use in source and binary forms, with or without
67  * modification, are permitted provided that the following conditions
68  * are met:
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.
77  *
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
88  * SUCH DAMAGE.
89  */
90 
91 #include <sys/cdefs.h>
92 #include <sys/types.h>
93 
94 #include <assert.h>
95 #include <dirent.h>
96 #include <dlfcn.h>
97 #include <errno.h>
98 #include <limits.h>
99 #include <paths.h>
100 #include <stdbool.h>
101 #include <stddef.h>
102 #include <stdio.h>
103 #include <stdlib.h>
104 #include <string.h>
105 #include <unistd.h>
106 
107 #define	I18NMODULE_MAJOR	4
108 
109 #include "citrus_namespace.h"
110 #include "citrus_bcs.h"
111 #include "citrus_module.h"
112 #include "libc_private.h"
113 
114 static int		 _getdewey(int[], char *);
115 static int		 _cmpndewey(int[], int, int[], 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 int
126 _getdewey(int dewey[], char *cp)
127 {
128 	int i, n;
129 
130 	for (n = 0, i = 0; i < MAXDEWEY; i++) {
131 		if (*cp == '\0')
132 			break;
133 
134 		if (*cp == '.') cp++;
135 		if (*cp < '0' || '9' < *cp)
136 			return (0);
137 
138 		dewey[n++] = (int)_bcs_strtol(cp, &cp, 10);
139 	}
140 
141 	return (n);
142 }
143 
144 /*
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'.
148  * Return  0 if equal.
149  */
150 static int
151 _cmpndewey(int d1[], int n1, int d2[], int n2)
152 {
153 	int i;
154 
155 	for (i = 0; i < n1 && i < n2; i++) {
156 		if (d1[i] < d2[i])
157 			return (-1);
158 		if (d1[i] > d2[i])
159 			return (1);
160 	}
161 
162 	if (n1 == n2)
163 		return (0);
164 
165 	if (i == n1)
166 		return (-1);
167 
168 	if (i == n2)
169 		return (1);
170 
171 	/* cannot happen */
172 	return (0);
173 }
174 
175 static const char *
176 _findshlib(char *name, int *majorp, int *minorp)
177 {
178 	char *lname;
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;
183 
184 	n_search_dirs = 1;
185 	major = *majorp;
186 	minor = *minorp;
187 	path[0] = '\0';
188 	search_dirs[0] = _pathI18nModule;
189 	len = strlen(name);
190 	lname = name;
191 
192 	ndewey = 0;
193 
194 	for (i = 0; i < n_search_dirs; i++) {
195 		struct dirent *dp;
196 		DIR *dd = opendir(search_dirs[i]);
197 		int found_dot_a = 0, found_dot_so = 0;
198 
199 		if (dd == NULL)
200 			break;
201 
202 		while ((dp = readdir(dd)) != NULL) {
203 			int n;
204 
205 			if (dp->d_namlen < len + 4)
206 				continue;
207 			if (strncmp(dp->d_name, lname, (size_t)len) != 0)
208 				continue;
209 			if (strncmp(dp->d_name+len, ".so.", 4) != 0)
210 				continue;
211 
212 			if ((n = _getdewey(tmp, dp->d_name+len+4)) == 0)
213 				continue;
214 
215 			if (major != -1 && found_dot_a)
216 				found_dot_a = 0;
217 
218 			/* XXX should verify the library is a.out/ELF? */
219 
220 			if (major == -1 && minor == -1)
221 				goto compare_version;
222 			else if (major != -1 && minor == -1) {
223 				if (tmp[0] == major)
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;
229 				}
230 			}
231 
232 			/* else, this file does not qualify */
233 			continue;
234 
235 		compare_version:
236 			if (_cmpndewey(tmp, n, dewey, ndewey) <= 0)
237 				continue;
238 
239 			/* We have a better version */
240 			found_dot_so = 1;
241 			snprintf(path, sizeof(path), "%s/%s", search_dirs[i],
242 			    dp->d_name);
243 			found_dot_a = 0;
244 			bcopy(tmp, dewey, sizeof(dewey));
245 			ndewey = n;
246 			*majorp = dewey[0];
247 			*minorp = dewey[1];
248 		}
249 		closedir(dd);
250 
251 		if (found_dot_a || found_dot_so)
252 			/*
253 			 * There's a lib in this dir; take it.
254 			 */
255 			return (path[0] ? path : NULL);
256 	}
257 
258 	return (path[0] ? path : NULL);
259 }
260 
261 void *
262 _citrus_find_getops(_citrus_module_t handle, const char *modname,
263     const char *ifname)
264 {
265 	char name[PATH_MAX];
266 	void *p;
267 
268 	snprintf(name, sizeof(name), "_citrus_%s_%s_getops",
269 	    modname, ifname);
270 	p = dlsym((void *)handle, name);
271 	return (p);
272 }
273 
274 int
275 _citrus_load_module(_citrus_module_t *rhandle, const char *encname)
276 {
277 	const char *p;
278 	char path[PATH_MAX];
279 	void *handle;
280 	int maj, min;
281 
282 	if (_pathI18nModule == NULL) {
283 		p = getenv("PATH_I18NMODULE");
284 		if (p != NULL && !issetugid()) {
285 			_pathI18nModule = strdup(p);
286 			if (_pathI18nModule == NULL)
287 				return (ENOMEM);
288 		} else
289 			_pathI18nModule = _PATH_I18NMODULE;
290 	}
291 
292 	(void)snprintf(path, sizeof(path), "lib%s", encname);
293 	maj = I18NMODULE_MAJOR;
294 	min = -1;
295 	p = _findshlib(path, &maj, &min);
296 	if (!p)
297 		return (EINVAL);
298 	handle = libc_dlopen(p, RTLD_LAZY);
299 	if (!handle) {
300 		printf("%s", dlerror());
301 		return (EINVAL);
302 	}
303 
304 	*rhandle = (_citrus_module_t)handle;
305 
306 	return (0);
307 }
308 
309 void
310 _citrus_unload_module(_citrus_module_t handle)
311 {
312 
313 	if (handle)
314 		dlclose((void *)handle);
315 }
316