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