citrus_module.c revision 219019
1/* $FreeBSD: head/lib/libc/iconv/citrus_module.c 219019 2011-02-25 00:04:39Z gabor $ */
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
113static int		 _getdewey(int[], char *);
114static int		 _cmpndewey(int[], int, int[], int);
115static const char	*_findshlib(char *, int *, int *);
116
117static 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
124static int
125_getdewey(int dewey[], char *cp)
126{
127	int i, n;
128
129	for (n = 0, i = 0; i < MAXDEWEY; i++) {
130		if (*cp == '\0')
131			break;
132
133		if (*cp == '.') cp++;
134		if (*cp < '0' || '9' < *cp)
135			return (0);
136
137		dewey[n++] = (int)_bcs_strtol(cp, &cp, 10);
138	}
139
140	return (n);
141}
142
143/*
144 * Compare two dewey arrays.
145 * Return -1 if `d1' represents a smaller value than `d2'.
146 * Return  1 if `d1' represents a greater value than `d2'.
147 * Return  0 if equal.
148 */
149static int
150_cmpndewey(int d1[], int n1, int d2[], int n2)
151{
152	int i;
153
154	for (i = 0; i < n1 && i < n2; i++) {
155		if (d1[i] < d2[i])
156			return (-1);
157		if (d1[i] > d2[i])
158			return (1);
159	}
160
161	if (n1 == n2)
162		return (0);
163
164	if (i == n1)
165		return (-1);
166
167	if (i == n2)
168		return (1);
169
170	/* cannot happen */
171	return (0);
172}
173
174static const char *
175_findshlib(char *name, int *majorp, int *minorp)
176{
177	char *lname;
178	const char *search_dirs[1];
179	static char path[PATH_MAX];
180	int dewey[MAXDEWEY], tmp[MAXDEWEY];
181	int i, len, major, minor, ndewey, n_search_dirs;
182
183	n_search_dirs = 1;
184	major = *majorp;
185	minor = *minorp;
186	path[0] = '\0';
187	search_dirs[0] = _pathI18nModule;
188	len = strlen(name);
189	lname = name;
190
191	ndewey = 0;
192
193	for (i = 0; i < n_search_dirs; i++) {
194		struct dirent *dp;
195		DIR *dd = opendir(search_dirs[i]);
196		int found_dot_a = 0, found_dot_so = 0;
197
198		if (dd == NULL)
199			break;
200
201		while ((dp = readdir(dd)) != NULL) {
202			int n;
203
204			if (dp->d_namlen < len + 4)
205				continue;
206			if (strncmp(dp->d_name, lname, (size_t)len) != 0)
207				continue;
208			if (strncmp(dp->d_name+len, ".so.", 4) != 0)
209				continue;
210
211			if ((n = _getdewey(tmp, dp->d_name+len+4)) == 0)
212				continue;
213
214			if (major != -1 && found_dot_a)
215				found_dot_a = 0;
216
217			/* XXX should verify the library is a.out/ELF? */
218
219			if (major == -1 && minor == -1)
220				goto compare_version;
221			else if (major != -1 && minor == -1) {
222				if (tmp[0] == major)
223					goto compare_version;
224			} else if (major != -1 && minor != -1) {
225				if (tmp[0] == major) {
226					if (n == 1 || tmp[1] >= minor)
227						goto compare_version;
228				}
229			}
230
231			/* else, this file does not qualify */
232			continue;
233
234		compare_version:
235			if (_cmpndewey(tmp, n, dewey, ndewey) <= 0)
236				continue;
237
238			/* We have a better version */
239			found_dot_so = 1;
240			snprintf(path, sizeof(path), "%s/%s", search_dirs[i],
241			    dp->d_name);
242			found_dot_a = 0;
243			bcopy(tmp, dewey, sizeof(dewey));
244			ndewey = n;
245			*majorp = dewey[0];
246			*minorp = dewey[1];
247		}
248		closedir(dd);
249
250		if (found_dot_a || found_dot_so)
251			/*
252			 * There's a lib in this dir; take it.
253			 */
254			return (path[0] ? path : NULL);
255	}
256
257	return (path[0] ? path : NULL);
258}
259
260void *
261_citrus_find_getops(_citrus_module_t handle, const char *modname,
262    const char *ifname)
263{
264	char name[PATH_MAX];
265	void *p;
266
267	snprintf(name, sizeof(name), "_citrus_%s_%s_getops",
268	    modname, ifname);
269	p = dlsym((void *)handle, name);
270	return (p);
271}
272
273int
274_citrus_load_module(_citrus_module_t *rhandle, const char *encname)
275{
276	const char *p;
277	char path[PATH_MAX];
278	void *handle;
279	int maj, min;
280
281	if (_pathI18nModule == NULL) {
282		p = getenv("PATH_I18NMODULE");
283		if (p != NULL && !issetugid()) {
284			_pathI18nModule = strdup(p);
285			if (_pathI18nModule == NULL)
286				return (ENOMEM);
287		} else
288			_pathI18nModule = _PATH_I18NMODULE;
289	}
290
291	(void)snprintf(path, sizeof(path), "lib%s", encname);
292	maj = I18NMODULE_MAJOR;
293	min = -1;
294	p = _findshlib(path, &maj, &min);
295	if (!p)
296		return (EINVAL);
297	handle = dlopen(p, RTLD_LAZY);
298	if (!handle) {
299		printf("%s", dlerror());
300		return (EINVAL);
301	}
302
303	*rhandle = (_citrus_module_t)handle;
304
305	return (0);
306}
307
308void
309_citrus_unload_module(_citrus_module_t handle)
310{
311
312	if (handle)
313		dlclose((void *)handle);
314}
315