11541Srgrimes/* $FreeBSD: releng/10.3/lib/libc/iconv/citrus_module.c 228843 2011-12-23 15:00:37Z cperciva $ */
21541Srgrimes/* $NetBSD: citrus_module.c,v 1.9 2009/01/11 02:46:24 christos Exp $ */
31541Srgrimes
41541Srgrimes/*-
51541Srgrimes * Copyright (c)1999, 2000, 2001, 2002 Citrus Project,
61541Srgrimes * All rights reserved.
71541Srgrimes *
81541Srgrimes * Redistribution and use in source and binary forms, with or without
91541Srgrimes * modification, are permitted provided that the following conditions
101541Srgrimes * are met:
111541Srgrimes * 1. Redistributions of source code must retain the above copyright
121541Srgrimes *    notice, this list of conditions and the following disclaimer.
131541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141541Srgrimes *    notice, this list of conditions and the following disclaimer in the
151541Srgrimes *    documentation and/or other materials provided with the distribution.
161541Srgrimes *
171541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
181541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
211541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271541Srgrimes * SUCH DAMAGE.
281541Srgrimes */
291541Srgrimes
301541Srgrimes/*-
311541Srgrimes * Copyright (c) 1998 The NetBSD Foundation, Inc.
321541Srgrimes * All rights reserved.
3314482Shsu *
3424896Sbde * This code is derived from software contributed to The NetBSD Foundation
351541Srgrimes * by Paul Kranenburg.
361541Srgrimes *
372165Spaul * Redistribution and use in source and binary forms, with or without
382165Spaul * modification, are permitted provided that the following conditions
392165Spaul * are met:
401541Srgrimes * 1. Redistributions of source code must retain the above copyright
411541Srgrimes *    notice, this list of conditions and the following disclaimer.
421541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
4314482Shsu *    notice, this list of conditions and the following disclaimer in the
441541Srgrimes *    documentation and/or other materials provided with the distribution.
451541Srgrimes *
461541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
471541Srgrimes * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
481541Srgrimes * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
491541Srgrimes * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
501541Srgrimes * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
511541Srgrimes * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
529507Sdg * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
539507Sdg * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
549507Sdg * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
551541Srgrimes * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
561541Srgrimes * POSSIBILITY OF SUCH DAMAGE.
571541Srgrimes */
581541Srgrimes
591541Srgrimes/*-
601541Srgrimes * Copyright (c) 1993
611541Srgrimes *	The Regents of the University of California.  All rights reserved.
621541Srgrimes *
631541Srgrimes * This code is derived from software contributed to Berkeley by
641541Srgrimes * Paul Borman at Krystal Technologies.
651541Srgrimes *
661541Srgrimes * Redistribution and use in source and binary forms, with or without
6720346Salex * modification, are permitted provided that the following conditions
6820346Salex * are met:
6920346Salex * 1. Redistributions of source code must retain the above copyright
7020346Salex *    notice, this list of conditions and the following disclaimer.
7120346Salex * 2. Redistributions in binary form must reproduce the above copyright
727358Sdg *    notice, this list of conditions and the following disclaimer in the
737358Sdg *    documentation and/or other materials provided with the distribution.
747358Sdg * 3. Neither the name of the University nor the names of its contributors
757358Sdg *    may be used to endorse or promote products derived from this software
767358Sdg *    without specific prior written permission.
777358Sdg *
7814482Shsu * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
791541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
8014482Shsu * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
811541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
821541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
831541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
841541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
851541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
861541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
871541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
881541Srgrimes * SUCH DAMAGE.
891541Srgrimes */
901541Srgrimes
9115873Sdyson#include <sys/cdefs.h>
921541Srgrimes#include <sys/types.h>
9315819Sdyson
9415819Sdyson#include <assert.h>
9515819Sdyson#include <dirent.h>
9615819Sdyson#include <dlfcn.h>
9715819Sdyson#include <errno.h>
9815819Sdyson#include <limits.h>
9915819Sdyson#include <paths.h>
10015819Sdyson#include <stdbool.h>
10115819Sdyson#include <stddef.h>
1021541Srgrimes#include <stdio.h>
1031541Srgrimes#include <stdlib.h>
1041541Srgrimes#include <string.h>
1051541Srgrimes#include <unistd.h>
1061541Srgrimes
10724896Sbde#define	I18NMODULE_MAJOR	4
10824896Sbde
1091541Srgrimes#include "citrus_namespace.h"
11024896Sbde#include "citrus_bcs.h"
1111541Srgrimes#include "citrus_module.h"
1121541Srgrimes#include "libc_private.h"
1137363Sdg
1141541Srgrimesstatic int		 _getdewey(int[], char *);
1151541Srgrimesstatic int		 _cmpndewey(int[], int, int[], int);
11614482Shsustatic const char	*_findshlib(char *, int *, int *);
11714323Speter
11814323Speterstatic const char *_pathI18nModule = NULL;
1191541Srgrimes
1201541Srgrimes/* from libexec/ld.aout_so/shlib.c */
1211541Srgrimes#undef major
1222165Spaul#undef minor
1232165Spaul#define MAXDEWEY	3	/*ELF*/
124
125static 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 */
150static 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
175static 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
261void *
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
274int
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
309void
310_citrus_unload_module(_citrus_module_t handle)
311{
312
313	if (handle)
314		dlclose((void *)handle);
315}
316