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