Expand PMF_FN_* macros.
[netbsd-mini2440.git] / libexec / ld.elf_so / paths.c
blob728c46746b602a6b9b01f26a4972361955bd3214
1 /* $NetBSD: paths.c,v 1.39 2008/06/05 00:03:20 ad Exp $ */
3 /*
4 * Copyright 1996 Matt Thomas <matt@3am-software.com>
5 * Copyright 2002 Charles M. Hannum <root@ihack.net>
6 * All rights reserved.
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. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 #ifndef lint
33 __RCSID("$NetBSD: paths.c,v 1.39 2008/06/05 00:03:20 ad Exp $");
34 #endif /* not lint */
36 #include <err.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <stdarg.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44 #include <sys/types.h>
45 #include <sys/param.h>
46 #include <sys/sysctl.h>
47 #include <sys/mman.h>
48 #include <sys/stat.h>
49 #include <sys/gmon.h>
50 #include <sys/socket.h>
51 #include <sys/mount.h>
52 #include <sys/mbuf.h>
53 #include <sys/resource.h>
54 #include <machine/cpu.h>
56 #include "debug.h"
57 #include "rtld.h"
59 static Search_Path *_rtld_find_path(Search_Path *, const char *, size_t);
60 static Search_Path **_rtld_append_path(Search_Path **, Search_Path **,
61 const char *, const char *, const char *);
62 static void _rtld_process_mapping(Library_Xform **, const char *,
63 const char *);
64 static char *exstrdup(const char *, const char *);
65 static const char *getstr(const char **, const char *, const char *);
66 static const char *getcstr(const char **, const char *, const char *);
67 static const char *getword(const char **, const char *, const char *);
68 static int matchstr(const char *, const char *, const char *);
70 static const char WS[] = " \t\n";
73 * Like xstrdup(), but takes end of string as a argument.
75 static char *
76 exstrdup(const char *bp, const char *ep)
78 char *cp;
79 size_t len = ep - bp;
81 cp = xmalloc(len + 1);
82 memcpy(cp, bp, len);
83 cp[len] = '\0';
84 return (cp);
88 * Like strsep(), but takes end of string and doesn't put any NUL. To
89 * detect empty string, compare `*p' and return value.
91 static const char *
92 getstr(const char **p, const char *ep, const char *delim)
94 const char *cp = *p, *q, *r;
96 if (ep < cp)
97 /* End of string */
98 return (NULL);
100 for (q = cp; q < ep; q++)
101 for (r = delim; *r != 0; r++)
102 if (*r == *q)
103 goto done;
105 done:
106 *p = q;
107 return (cp);
111 * Like getstr() above, but delim[] is complemented.
113 static const char *
114 getcstr(const char **p, const char *ep, const char *delim)
116 const char *cp = *p, *q, *r;
118 if (ep < cp)
119 /* End of string */
120 return (NULL);
122 for (q = cp; q < ep; q++)
123 for (r = delim; *r != *q; r++)
124 if (*r == 0)
125 goto done;
127 done:
128 *p = q;
129 return (cp);
132 static const char *
133 getword(const char **p, const char *ep, const char *delim)
136 (void)getcstr(p, ep, delim);
139 * Now, we're looking non-delim, or end of string.
142 return (getstr(p, ep, delim));
146 * Match `bp' against NUL terminated string pointed by `p'.
148 static int
149 matchstr(const char *p, const char *bp, const char *ep)
151 int c;
153 while (bp < ep)
154 if ((c = *p++) == 0 || c != *bp++)
155 return (0);
157 return (*p == 0);
160 static Search_Path *
161 _rtld_find_path(Search_Path *path, const char *pathstr, size_t pathlen)
164 for (; path != NULL; path = path->sp_next) {
165 if (pathlen == path->sp_pathlen &&
166 memcmp(path->sp_path, pathstr, pathlen) == 0)
167 return path;
169 return NULL;
172 static Search_Path **
173 _rtld_append_path(Search_Path **head_p, Search_Path **path_p,
174 const char *execname, const char *bp, const char *ep)
176 Search_Path *path;
177 char epath[MAXPATHLEN];
178 size_t len;
180 len = _rtld_expand_path(epath, sizeof(epath), execname, bp, ep);
181 if (len == 0)
182 return path_p;
184 if (_rtld_find_path(*head_p, bp, ep - bp) != NULL)
185 return path_p;
187 path = NEW(Search_Path);
188 path->sp_pathlen = len;
189 path->sp_path = exstrdup(epath, epath + len);
190 path->sp_next = (*path_p);
191 (*path_p) = path;
192 path_p = &path->sp_next;
194 dbg((" added path \"%s\"", path->sp_path));
195 return path_p;
198 void
199 _rtld_add_paths(const char *execname, Search_Path **path_p, const char *pathstr)
201 Search_Path **head_p = path_p;
203 if (pathstr == NULL)
204 return;
206 if (pathstr[0] == ':') {
208 * Leading colon means append to current path
210 while ((*path_p) != NULL)
211 path_p = &(*path_p)->sp_next;
212 pathstr++;
215 for (;;) {
216 const char *bp = pathstr;
217 const char *ep = strchr(bp, ':');
218 if (ep == NULL)
219 ep = &pathstr[strlen(pathstr)];
221 path_p = _rtld_append_path(head_p, path_p, execname, bp, ep);
223 if (ep[0] == '\0')
224 break;
225 pathstr = ep + 1;
230 * Process library mappings of the form:
231 * <library_name> <machdep_variable> <value,...:library_name,...> ...
233 static void
234 _rtld_process_mapping(Library_Xform **lib_p, const char *bp, const char *ep)
236 Library_Xform *hwptr = NULL;
237 const char *ptr, *key, *ekey, *lib, *elib, *l;
238 int i, j;
240 dbg((" processing mapping \"%.*s\"", (int)(ep - bp), bp));
242 if ((ptr = getword(&bp, ep, WS)) == NULL || ptr == bp)
243 return;
245 dbg((" library \"%.*s\"", (int)(bp - ptr), ptr));
247 hwptr = xmalloc(sizeof(*hwptr));
248 memset(hwptr, 0, sizeof(*hwptr));
249 hwptr->name = exstrdup(ptr, bp);
251 bp++;
253 if ((ptr = getword(&bp, ep, WS)) == NULL || ptr == bp) {
254 xwarnx("missing sysctl variable name");
255 goto cleanup;
258 dbg((" sysctl \"%.*s\"", (int)(bp - ptr), ptr));
260 hwptr->ctlname = exstrdup(ptr, bp);
262 for (i = 0; bp++, (ptr = getword(&bp, ep, WS)) != NULL;) {
263 dbg((" ptr = %.*s", (int)(bp - ptr), ptr));
264 if (ptr == bp)
265 continue;
267 if (i == RTLD_MAX_ENTRY) {
268 no_more:
269 xwarnx("maximum library entries exceeded `%s'",
270 hwptr->name);
271 goto cleanup;
273 if ((key = getstr(&ptr, bp, ":")) == NULL) {
274 xwarnx("missing sysctl variable value for `%s'",
275 hwptr->name);
276 goto cleanup;
278 ekey = ptr++;
279 if ((lib = getstr(&ptr, bp, ":")) == NULL) {
280 xwarnx("missing sysctl library list for `%s'",
281 hwptr->name);
282 goto cleanup;
284 elib = ptr; /* No need to advance */
285 for (j = 0; (l = getstr(&lib, elib, ",")) != NULL;
286 j++, lib++) {
287 if (j == RTLD_MAX_LIBRARY) {
288 xwarnx("maximum library entries exceeded `%s'",
289 hwptr->name);
290 goto cleanup;
292 dbg((" library \"%.*s\"", (int)(lib - l), l));
293 hwptr->entry[i].library[j] = exstrdup(l, lib);
295 if (j == 0) {
296 xwarnx("No library map entries for `%s/%.*s'",
297 hwptr->name, (int)(bp - ptr), ptr);
298 goto cleanup;
300 j = i;
301 for (; (l = getstr(&key, ekey, ",")) != NULL; i++, key++) {
303 * Allow empty key (it is valid as string
304 * value). Thus, we loop at least once and
305 * `i' is incremented.
308 dbg((" key \"%.*s\"", (int)(key - l), l));
309 if (i == RTLD_MAX_ENTRY)
310 goto no_more;
311 if (i != j)
312 (void)memcpy(hwptr->entry[i].library,
313 hwptr->entry[j].library,
314 sizeof(hwptr->entry[j].library));
315 hwptr->entry[i].value = exstrdup(l, key);
319 if (i == 0) {
320 xwarnx("No library entries for `%s'", hwptr->name);
321 goto cleanup;
324 hwptr->next = *lib_p;
325 *lib_p = hwptr;
327 return;
329 cleanup:
330 if (hwptr->name)
331 xfree(hwptr->name);
332 xfree(hwptr);
335 void
336 _rtld_process_hints(const char *execname, Search_Path **path_p,
337 Library_Xform **lib_p, const char *fname)
339 int fd;
340 char *buf, small[128];
341 const char *b, *ep, *ptr;
342 struct stat st;
343 ssize_t sz;
344 Search_Path **head_p = path_p;
346 if ((fd = open(fname, O_RDONLY)) == -1) {
347 /* Don't complain */
348 return;
351 /* Try to avoid mmap/stat on the file. */
352 buf = small;
353 buf[0] = '\0';
354 sz = read(fd, buf, sizeof(small));
355 if (sz == -1) {
356 xwarn("read: %s", fname);
357 (void)close(fd);
358 return;
360 if (sz >= (ssize_t)sizeof(small)) {
361 if (fstat(fd, &st) == -1) {
362 /* Complain */
363 xwarn("fstat: %s", fname);
364 (void)close(fd);
365 return;
368 sz = (ssize_t) st.st_size;
370 buf = mmap(0, sz, PROT_READ, MAP_SHARED|MAP_FILE, fd, 0);
371 if (buf == MAP_FAILED) {
372 xwarn("mmap: %s", fname);
373 (void)close(fd);
374 return;
377 (void)close(fd);
379 while ((*path_p) != NULL)
380 path_p = &(*path_p)->sp_next;
382 for (b = buf, ep = buf + sz; b < ep; b++) {
383 (void)getcstr(&b, ep, WS);
384 if (b == ep)
385 break;
387 ptr = getstr(&b, ep, "\n#");
388 if (*ptr == '/') {
390 * Since '/' != '\n' and != '#', we know ptr <
391 * b. And we will stop when b[-1] == '/'.
393 while (b[-1] == ' ' || b[-1] == '\t')
394 b--;
395 path_p = _rtld_append_path(head_p, path_p, execname,
396 ptr, b);
397 } else
398 _rtld_process_mapping(lib_p, ptr, b);
401 * b points one of ' ', \t, \n, # or equal to ep. So,
402 * make sure we are at newline or end of string.
404 (void)getstr(&b, ep, "\n");
407 if (buf != small)
408 (void)munmap(buf, sz);
411 /* Basic name -> sysctl MIB translation */
413 _rtld_sysctl(const char *name, void *oldp, size_t *oldlen)
415 const char *node, *ep;
416 struct sysctlnode query, *result, *newresult;
417 int mib[CTL_MAXNAME], r;
418 size_t res_size, n, i;
419 u_int miblen = 0;
421 /* Start with 16 entries, will grow it up as needed. */
422 res_size = 16 * sizeof(struct sysctlnode);
423 result = xmalloc(res_size);
424 if (result == NULL)
425 return (-1);
427 ep = name + strlen(name);
428 do {
429 i = ~0ul;
430 while (*name == '/' || *name == '.')
431 name++;
432 if (name >= ep)
433 break;
435 mib[miblen] = CTL_QUERY;
436 memset(&query, 0, sizeof(query));
437 query.sysctl_flags = SYSCTL_VERSION;
439 n = res_size;
440 if (sysctl(mib, miblen + 1, result, &n, &query,
441 sizeof(query)) == -1) {
442 if (errno != ENOMEM)
443 goto bad;
444 /* Grow up result */
445 res_size = n;
446 newresult = xrealloc(result, res_size);
447 if (newresult == NULL)
448 goto bad;
449 result = newresult;
450 if (sysctl(mib, miblen + 1, result, &n, &query,
451 sizeof(query)) == -1)
452 goto bad;
454 n /= sizeof(struct sysctlnode);
456 node = getstr(&name, ep, "./");
458 for (i = 0; i < n; i++)
459 if (matchstr(result[i].sysctl_name, node, name)) {
460 mib[miblen] = result[i].sysctl_num;
461 miblen++;
462 break;
464 } while (name < ep && miblen <= CTL_MAXNAME);
466 if (name < ep || i == ~0ul)
467 goto bad;
468 r = SYSCTL_TYPE(result[i].sysctl_flags);
470 xfree(result);
471 if (sysctl(mib, miblen, oldp, oldlen, NULL, 0) == -1)
472 return (-1);
473 return r;
475 bad:
476 xfree(result);
477 return (-1);