include: reduce default stack size
[minix.git] / libexec / ld.elf_so / paths.c
blob70865da2ec230b9d424e28f17e36d5ad6c65cd50
1 /* $NetBSD: paths.c,v 1.40 2009/05/19 20:44:52 christos 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.40 2009/05/19 20:44:52 christos 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 #ifndef __minix
53 #include <sys/mbuf.h>
54 #endif
55 #include <sys/resource.h>
56 #include <machine/cpu.h>
58 #include "debug.h"
59 #include "rtld.h"
61 static Search_Path *_rtld_find_path(Search_Path *, const char *, size_t);
62 static Search_Path **_rtld_append_path(Search_Path **, Search_Path **,
63 const char *, const char *, const char *);
64 static void _rtld_process_mapping(Library_Xform **, const char *,
65 const char *);
66 static char *exstrdup(const char *, const char *);
67 static const char *getstr(const char **, const char *, const char *);
68 static const char *getcstr(const char **, const char *, const char *);
69 static const char *getword(const char **, const char *, const char *);
70 static int matchstr(const char *, const char *, const char *);
72 static const char WS[] = " \t\n";
75 * Like xstrdup(), but takes end of string as a argument.
77 static char *
78 exstrdup(const char *bp, const char *ep)
80 char *cp;
81 size_t len = ep - bp;
83 cp = xmalloc(len + 1);
84 memcpy(cp, bp, len);
85 cp[len] = '\0';
86 return (cp);
90 * Like strsep(), but takes end of string and doesn't put any NUL. To
91 * detect empty string, compare `*p' and return value.
93 static const char *
94 getstr(const char **p, const char *ep, const char *delim)
96 const char *cp = *p, *q, *r;
98 if (ep < cp)
99 /* End of string */
100 return (NULL);
102 for (q = cp; q < ep; q++)
103 for (r = delim; *r != 0; r++)
104 if (*r == *q)
105 goto done;
107 done:
108 *p = q;
109 return (cp);
113 * Like getstr() above, but delim[] is complemented.
115 static const char *
116 getcstr(const char **p, const char *ep, const char *delim)
118 const char *cp = *p, *q, *r;
120 if (ep < cp)
121 /* End of string */
122 return (NULL);
124 for (q = cp; q < ep; q++)
125 for (r = delim; *r != *q; r++)
126 if (*r == 0)
127 goto done;
129 done:
130 *p = q;
131 return (cp);
134 static const char *
135 getword(const char **p, const char *ep, const char *delim)
138 (void)getcstr(p, ep, delim);
141 * Now, we're looking non-delim, or end of string.
144 return (getstr(p, ep, delim));
148 * Match `bp' against NUL terminated string pointed by `p'.
150 static int
151 matchstr(const char *p, const char *bp, const char *ep)
153 int c;
155 while (bp < ep)
156 if ((c = *p++) == 0 || c != *bp++)
157 return (0);
159 return (*p == 0);
162 static Search_Path *
163 _rtld_find_path(Search_Path *path, const char *pathstr, size_t pathlen)
166 for (; path != NULL; path = path->sp_next) {
167 if (pathlen == path->sp_pathlen &&
168 memcmp(path->sp_path, pathstr, pathlen) == 0)
169 return path;
171 return NULL;
174 static Search_Path **
175 _rtld_append_path(Search_Path **head_p, Search_Path **path_p,
176 const char *execname, const char *bp, const char *ep)
178 Search_Path *path;
179 char epath[MAXPATHLEN];
180 size_t len;
182 len = _rtld_expand_path(epath, sizeof(epath), execname, bp, ep);
183 if (len == 0)
184 return path_p;
186 if (_rtld_find_path(*head_p, bp, ep - bp) != NULL)
187 return path_p;
189 path = NEW(Search_Path);
190 path->sp_pathlen = len;
191 path->sp_path = exstrdup(epath, epath + len);
192 path->sp_next = (*path_p);
193 (*path_p) = path;
194 path_p = &path->sp_next;
196 dbg((" added path \"%s\"", path->sp_path));
197 return path_p;
200 void
201 _rtld_add_paths(const char *execname, Search_Path **path_p, const char *pathstr)
203 Search_Path **head_p = path_p;
205 if (pathstr == NULL)
206 return;
208 if (pathstr[0] == ':') {
210 * Leading colon means append to current path
212 while ((*path_p) != NULL)
213 path_p = &(*path_p)->sp_next;
214 pathstr++;
217 for (;;) {
218 const char *bp = pathstr;
219 const char *ep = strchr(bp, ':');
220 if (ep == NULL)
221 ep = &pathstr[strlen(pathstr)];
223 path_p = _rtld_append_path(head_p, path_p, execname, bp, ep);
225 if (ep[0] == '\0')
226 break;
227 pathstr = ep + 1;
232 * Process library mappings of the form:
233 * <library_name> <machdep_variable> <value,...:library_name,...> ...
235 static void
236 _rtld_process_mapping(Library_Xform **lib_p, const char *bp, const char *ep)
238 Library_Xform *hwptr = NULL;
239 const char *ptr, *key, *ekey, *lib, *elib, *l;
240 int i, j;
242 dbg((" processing mapping \"%.*s\"", (int)(ep - bp), bp));
244 if ((ptr = getword(&bp, ep, WS)) == NULL || ptr == bp)
245 return;
247 dbg((" library \"%.*s\"", (int)(bp - ptr), ptr));
249 hwptr = xmalloc(sizeof(*hwptr));
250 memset(hwptr, 0, sizeof(*hwptr));
251 hwptr->name = exstrdup(ptr, bp);
253 bp++;
255 if ((ptr = getword(&bp, ep, WS)) == NULL || ptr == bp) {
256 xwarnx("missing sysctl variable name");
257 goto cleanup;
260 dbg((" sysctl \"%.*s\"", (int)(bp - ptr), ptr));
262 hwptr->ctlname = exstrdup(ptr, bp);
264 for (i = 0; bp++, (ptr = getword(&bp, ep, WS)) != NULL;) {
265 dbg((" ptr = %.*s", (int)(bp - ptr), ptr));
266 if (ptr == bp)
267 continue;
269 if (i == RTLD_MAX_ENTRY) {
270 no_more:
271 xwarnx("maximum library entries exceeded `%s'",
272 hwptr->name);
273 goto cleanup;
275 if ((key = getstr(&ptr, bp, ":")) == NULL) {
276 xwarnx("missing sysctl variable value for `%s'",
277 hwptr->name);
278 goto cleanup;
280 ekey = ptr++;
281 if ((lib = getstr(&ptr, bp, ":")) == NULL) {
282 xwarnx("missing sysctl library list for `%s'",
283 hwptr->name);
284 goto cleanup;
286 elib = ptr; /* No need to advance */
287 for (j = 0; (l = getstr(&lib, elib, ",")) != NULL;
288 j++, lib++) {
289 if (j == RTLD_MAX_LIBRARY) {
290 xwarnx("maximum library entries exceeded `%s'",
291 hwptr->name);
292 goto cleanup;
294 dbg((" library \"%.*s\"", (int)(lib - l), l));
295 hwptr->entry[i].library[j] = exstrdup(l, lib);
297 if (j == 0) {
298 xwarnx("No library map entries for `%s/%.*s'",
299 hwptr->name, (int)(bp - ptr), ptr);
300 goto cleanup;
302 j = i;
303 for (; (l = getstr(&key, ekey, ",")) != NULL; i++, key++) {
305 * Allow empty key (it is valid as string
306 * value). Thus, we loop at least once and
307 * `i' is incremented.
310 dbg((" key \"%.*s\"", (int)(key - l), l));
311 if (i == RTLD_MAX_ENTRY)
312 goto no_more;
313 if (i != j)
314 (void)memcpy(hwptr->entry[i].library,
315 hwptr->entry[j].library,
316 sizeof(hwptr->entry[j].library));
317 hwptr->entry[i].value = exstrdup(l, key);
321 if (i == 0) {
322 xwarnx("No library entries for `%s'", hwptr->name);
323 goto cleanup;
326 hwptr->next = *lib_p;
327 *lib_p = hwptr;
329 return;
331 cleanup:
332 if (hwptr->name)
333 xfree(hwptr->name);
334 xfree(hwptr);
337 void
338 _rtld_process_hints(const char *execname, Search_Path **path_p,
339 Library_Xform **lib_p, const char *fname)
342 #ifdef __minix
343 /* Minix doesn't support MAP_SHARED. */
344 return;
345 #else
346 int fd;
347 char *buf, small[128];
348 const char *b, *ep, *ptr;
349 struct stat st;
350 ssize_t sz;
351 Search_Path **head_p = path_p;
352 if ((fd = open(fname, O_RDONLY)) == -1) {
353 /* Don't complain */
354 return;
357 /* Try to avoid mmap/stat on the file. */
358 buf = small;
359 buf[0] = '\0';
360 sz = read(fd, buf, sizeof(small));
361 if (sz == -1) {
362 xwarn("read: %s", fname);
363 (void)close(fd);
364 return;
366 if (sz >= (ssize_t)sizeof(small)) {
367 if (fstat(fd, &st) == -1) {
368 /* Complain */
369 xwarn("fstat: %s", fname);
370 (void)close(fd);
371 return;
374 sz = (ssize_t) st.st_size;
376 buf = mmap(0, sz, PROT_READ, MAP_SHARED|MAP_FILE, fd, 0);
377 if (buf == MAP_FAILED) {
378 xwarn("mmap: %s", fname);
379 (void)close(fd);
380 return;
383 (void)close(fd);
385 while ((*path_p) != NULL)
386 path_p = &(*path_p)->sp_next;
388 for (b = buf, ep = buf + sz; b < ep; b++) {
389 (void)getcstr(&b, ep, WS);
390 if (b == ep)
391 break;
393 ptr = getstr(&b, ep, "\n#");
394 if (*ptr == '/') {
396 * Since '/' != '\n' and != '#', we know ptr <
397 * b. And we will stop when b[-1] == '/'.
399 while (b[-1] == ' ' || b[-1] == '\t')
400 b--;
401 path_p = _rtld_append_path(head_p, path_p, execname,
402 ptr, b);
403 } else
404 _rtld_process_mapping(lib_p, ptr, b);
407 * b points one of ' ', \t, \n, # or equal to ep. So,
408 * make sure we are at newline or end of string.
410 (void)getstr(&b, ep, "\n");
413 if (buf != small)
414 (void)munmap(buf, sz);
415 #endif
418 #ifndef __minix
419 /* Basic name -> sysctl MIB translation */
421 _rtld_sysctl(const char *name, void *oldp, size_t *oldlen)
423 const char *node, *ep;
424 struct sysctlnode query, *result, *newresult;
425 int mib[CTL_MAXNAME], r;
426 size_t res_size, n, i;
427 u_int miblen = 0;
429 /* Start with 16 entries, will grow it up as needed. */
430 res_size = 16 * sizeof(struct sysctlnode);
431 result = xmalloc(res_size);
432 if (result == NULL)
433 return (-1);
435 ep = name + strlen(name);
436 do {
437 i = ~0ul;
438 while (*name == '/' || *name == '.')
439 name++;
440 if (name >= ep)
441 break;
443 mib[miblen] = CTL_QUERY;
444 memset(&query, 0, sizeof(query));
445 query.sysctl_flags = SYSCTL_VERSION;
447 n = res_size;
448 if (sysctl(mib, miblen + 1, result, &n, &query,
449 sizeof(query)) == -1) {
450 if (errno != ENOMEM)
451 goto bad;
452 /* Grow up result */
453 res_size = n;
454 newresult = xrealloc(result, res_size);
455 if (newresult == NULL)
456 goto bad;
457 result = newresult;
458 if (sysctl(mib, miblen + 1, result, &n, &query,
459 sizeof(query)) == -1)
460 goto bad;
462 n /= sizeof(struct sysctlnode);
464 node = getstr(&name, ep, "./");
466 for (i = 0; i < n; i++)
467 if (matchstr(result[i].sysctl_name, node, name)) {
468 mib[miblen] = result[i].sysctl_num;
469 miblen++;
470 break;
472 } while (name < ep && miblen <= CTL_MAXNAME);
474 if (name < ep || i == ~0ul)
475 goto bad;
476 r = SYSCTL_TYPE(result[i].sysctl_flags);
478 xfree(result);
479 if (sysctl(mib, miblen, oldp, oldlen, NULL, 0) == -1)
480 return (-1);
481 return r;
483 bad:
484 xfree(result);
485 return (-1);
487 #endif