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