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/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 5
108
109 #include "citrus_namespace.h"
110 #include "citrus_bcs.h"
111 #include "citrus_module.h"
112 #include "libc_private.h"
113
114 static int _getdewey(int[], char *);
115 static int _cmpndewey(int[], int, int[], int);
116 static const char *_findshlib(char *, int *, int *);
117
118 static const char *_pathI18nModule = NULL;
119
120 /* from libexec/ld.aout_so/shlib.c */
121 #undef major
122 #undef minor
123 #define MAXDEWEY 3 /*ELF*/
124
125 static int
_getdewey(int dewey[],char * cp)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 */
150 static int
_cmpndewey(int d1[],int n1,int d2[],int n2)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
175 static const char *
_findshlib(char * name,int * majorp,int * minorp)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
261 void *
_citrus_find_getops(_citrus_module_t handle,const char * modname,const char * ifname)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
274 int
_citrus_load_module(_citrus_module_t * rhandle,const char * encname)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 = secure_getenv("PATH_I18NMODULE");
284 if (p != NULL) {
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
309 void
_citrus_unload_module(_citrus_module_t handle)310 _citrus_unload_module(_citrus_module_t handle)
311 {
312
313 if (handle)
314 dlclose((void *)handle);
315 }
316