shlib.c revision 1.5
1/*	$OpenBSD: shlib.c,v 1.5 2002/05/24 03:44:37 deraadt Exp $	*/
2/*	$NetBSD: shlib.c,v 1.13 1998/04/04 01:00:29 fvdl Exp $	*/
3
4/*
5 * Copyright (c) 1993 Paul Kranenburg
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 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *      This product includes software developed by Paul Kranenburg.
19 * 4. The name of the author may not be used to endorse or promote products
20 *    derived from this software without specific prior written permission
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 */
34
35#ifdef sun
36char	*strsep();
37int	isdigit();
38#endif
39
40#include <sys/param.h>
41#include <sys/types.h>
42#include <sys/stat.h>
43#include <sys/file.h>
44#include <sys/time.h>
45#include <ranlib.h>
46#include <a.out.h>
47#include <ctype.h>
48#include <dirent.h>
49#include <err.h>
50#include <fcntl.h>
51#include <stdio.h>
52#include <stdlib.h>
53#include <string.h>
54
55#include "ld.h"
56
57/*
58 * Standard directories to search for files specified by -l.
59 */
60#ifndef STANDARD_SEARCH_DIRS
61#define	STANDARD_SEARCH_DIRS	"/usr/lib"
62#endif
63
64/*
65 * Actual vector of library search directories,
66 * including `-L'ed and LD_LIBRARY_PATH spec'd ones.
67 */
68char	 **search_dirs;
69int	n_search_dirs;
70
71char	*standard_search_dirs[] = {
72	STANDARD_SEARCH_DIRS
73};
74
75
76void
77add_search_dir(name)
78	char	*name;
79{
80	int i, len;
81
82	len = strlen(name);
83
84	while (len > 1 && name[len - 1] == '/')
85		--len;
86
87	for (i = 0; i < n_search_dirs; i++)
88		if (strlen(search_dirs[i]) == len &&
89			!strncmp(search_dirs[i], name, len))
90				return;
91	n_search_dirs++;
92	search_dirs = (char **)
93		xrealloc(search_dirs, n_search_dirs * sizeof search_dirs[0]);
94	search_dirs[n_search_dirs - 1] = xmalloc(++len);
95	(void)strlcpy(search_dirs[n_search_dirs - 1], name, len);
96}
97
98void
99remove_search_dir(name)
100	char	*name;
101{
102	int	i, len;
103
104	len = strlen(name);
105
106	while (len > 1 && name[len - 1] == '/')
107		--len;
108
109	for (i = 0; i < n_search_dirs; i++) {
110		if (strlen(search_dirs[i]) != len ||
111		    strncmp(search_dirs[i], name, len))
112			continue;
113		free(search_dirs[i]);
114		if (i < (n_search_dirs - 1))
115			bcopy(&search_dirs[i+1], &search_dirs[i],
116			      (n_search_dirs - i - 1) * sizeof search_dirs[0]);
117		n_search_dirs--;
118		search_dirs = (char **)xrealloc(search_dirs,
119			n_search_dirs * sizeof search_dirs[0]);
120		break;
121	}
122}
123
124void
125add_search_path(path)
126char	*path;
127{
128	char	*cp, *dup;
129
130	if (path == NULL)
131		return;
132
133	/* Add search directories from `path' */
134	path = dup = strdup(path);
135	while ((cp = strsep(&path, ":")) != NULL)
136		add_search_dir(cp);
137	free(dup);
138}
139
140void
141remove_search_path(path)
142char	*path;
143{
144	char	*cp, *dup;
145
146	if (path == NULL)
147		return;
148
149	/* Remove search directories from `path' */
150	path = dup = strdup(path);
151	while ((cp = strsep(&path, ":")) != NULL)
152		remove_search_dir(cp);
153	free(dup);
154}
155
156void
157std_search_path(void)
158{
159	int	i, n;
160
161	/* Append standard search directories */
162	n = sizeof standard_search_dirs / sizeof standard_search_dirs[0];
163	for (i = 0; i < n; i++)
164		add_search_dir(standard_search_dirs[i]);
165}
166
167/*
168 * Return true if CP points to a valid dewey number.
169 * Decode and leave the result in the array DEWEY.
170 * Return the number of decoded entries in DEWEY.
171 */
172
173int
174getdewey(dewey, cp)
175	int	dewey[];
176	char	*cp;
177{
178	int	i, n;
179
180	for (n = 0, i = 0; i < MAXDEWEY; i++) {
181		if (*cp == '\0')
182			break;
183
184		if (*cp == '.') cp++;
185#ifdef SUNOS_LIB_COMPAT
186		if (!(isdigit)(*cp))
187#else
188		if (!isdigit(*cp))
189#endif
190			return 0;
191
192		dewey[n++] = strtol(cp, &cp, 10);
193	}
194
195	return n;
196}
197
198/*
199 * Compare two dewey arrays.
200 * Return -1 if `d1' represents a smaller value than `d2'.
201 * Return  1 if `d1' represents a greater value than `d2'.
202 * Return  0 if equal.
203 */
204int
205cmpndewey(d1, n1, d2, n2)
206int	d1[], d2[];
207int	n1, n2;
208{
209	int	i;
210
211	for (i = 0; i < n1 && i < n2; i++) {
212		if (d1[i] < d2[i])
213			return -1;
214		if (d1[i] > d2[i])
215			return 1;
216	}
217
218	if (n1 == n2)
219		return 0;
220
221	if (i == n1)
222		return -1;
223
224	if (i == n2)
225		return 1;
226
227	errx(1, "cmpndewey: cant happen");
228	return 0;
229}
230
231/*
232 * Search directories for a shared library matching the given
233 * major and minor version numbers.
234 *
235 * MAJOR == -1 && MINOR == -1	--> find highest version
236 * MAJOR != -1 && MINOR == -1   --> find highest minor version
237 * MAJOR == -1 && MINOR != -1   --> invalid
238 * MAJOR != -1 && MINOR != -1   --> find highest micro version
239 */
240
241/* Not interested in devices right now... */
242#undef major
243#undef minor
244
245char *
246findshlib(name, majorp, minorp, do_dot_a)
247char	*name;
248int	*majorp, *minorp;
249int	do_dot_a;
250{
251	int		dewey[MAXDEWEY];
252	int		ndewey;
253	int		tmp[MAXDEWEY];
254	int		i;
255	int		len;
256	char		*lname;
257	int		major = *majorp, minor = *minorp;
258
259	len = strlen(name);
260	lname = (char *)alloca(len + sizeof("lib"));
261	sprintf(lname, "lib%s", name);
262	len += 3;
263
264	ndewey = 0;
265
266	for (i = 0; i < n_search_dirs; i++) {
267		DIR		*dd = opendir(search_dirs[i]);
268		struct dirent	*dp;
269		char		*path = NULL;
270
271		if (dd == NULL)
272			continue;
273
274		while ((dp = readdir(dd)) != NULL) {
275			int	n;
276
277			if (do_dot_a && path == NULL &&
278			    dp->d_namlen == len + 2 &&
279			    strncmp(dp->d_name, lname, len) == 0 &&
280			    (dp->d_name+len)[0] == '.' &&
281			    (dp->d_name+len)[1] == 'a')
282				path = concat(search_dirs[i], "/", dp->d_name);
283
284			if (dp->d_namlen < len + 4)
285				continue;
286			if (strncmp(dp->d_name, lname, len) != 0)
287				continue;
288			if (strncmp(dp->d_name+len, ".so.", 4) != 0)
289				continue;
290
291			if ((n = getdewey(tmp, dp->d_name+len+4)) == 0)
292				continue;
293
294			/* skip inappropriate versions. */
295			if (major != -1) {
296				if (tmp[0] != major)
297					continue;
298				if (n != 1 && minor != -1 && tmp[1] < minor)
299					continue;
300			}
301
302			if (cmpndewey(tmp, n, dewey, ndewey) <= 0)
303				continue;
304
305			/* We have a better version */
306			if (path)
307				free(path);
308			path = concat(search_dirs[i], "/", dp->d_name);
309			bcopy(tmp, dewey, sizeof(dewey));
310			ndewey = n;
311			*majorp = dewey[0];
312			*minorp = dewey[1];
313		}
314		closedir(dd);
315
316		if (path != NULL)
317			/*
318			 * There's a lib in this dir; take it.
319			 */
320			return path;
321	}
322
323	return NULL;
324}
325