1 /* $NetBSD: paths.c,v 1.37 2007/10/05 22:21:07 ad Exp $ */ 2 3 /* 4 * Copyright 1996 Matt Thomas <matt@3am-software.com> 5 * Copyright 2002 Charles M. Hannum <root@ihack.net> 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. The name of the author may not be used to endorse or promote products 17 * derived from this software without specific prior written permission. 18 * 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. 29 */ 30 31 #include <sys/cdefs.h> 32 #ifndef lint 33 __RCSID("$NetBSD: paths.c,v 1.37 2007/10/05 22:21:07 ad Exp $"); 34 #endif /* not lint */ 35 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> 55 56 #include "debug.h" 57 #include "rtld.h" 58 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 *); 69 70 static const char WS[] = " \t\n"; 71 72 /* 73 * Like xstrdup(), but takes end of string as a argument. 74 */ 75 static char * 76 exstrdup(const char *bp, const char *ep) 77 { 78 char *cp; 79 size_t len = ep - bp; 80 81 cp = xmalloc(len + 1); 82 memcpy(cp, bp, len); 83 cp[len] = '\0'; 84 return (cp); 85 } 86 87 /* 88 * Like strsep(), but takes end of string and doesn't put any NUL. To 89 * detect empty string, compare `*p' and return value. 90 */ 91 static const char * 92 getstr(const char **p, const char *ep, const char *delim) 93 { 94 const char *cp = *p, *q, *r; 95 96 if (ep < cp) 97 /* End of string */ 98 return (NULL); 99 100 for (q = cp; q < ep; q++) 101 for (r = delim; *r != 0; r++) 102 if (*r == *q) 103 goto done; 104 105 done: 106 *p = q; 107 return (cp); 108 } 109 110 /* 111 * Like getstr() above, but delim[] is complemented. 112 */ 113 static const char * 114 getcstr(const char **p, const char *ep, const char *delim) 115 { 116 const char *cp = *p, *q, *r; 117 118 if (ep < cp) 119 /* End of string */ 120 return (NULL); 121 122 for (q = cp; q < ep; q++) 123 for (r = delim; *r != *q; r++) 124 if (*r == 0) 125 goto done; 126 127 done: 128 *p = q; 129 return (cp); 130 } 131 132 static const char * 133 getword(const char **p, const char *ep, const char *delim) 134 { 135 136 (void)getcstr(p, ep, delim); 137 138 /* 139 * Now, we're looking non-delim, or end of string. 140 */ 141 142 return (getstr(p, ep, delim)); 143 } 144 145 /* 146 * Match `bp' against NUL terminated string pointed by `p'. 147 */ 148 static int 149 matchstr(const char *p, const char *bp, const char *ep) 150 { 151 int c; 152 153 while (bp < ep) 154 if ((c = *p++) == 0 || c != *bp++) 155 return (0); 156 157 return (*p == 0); 158 } 159 160 static Search_Path * 161 _rtld_find_path(Search_Path *path, const char *pathstr, size_t pathlen) 162 { 163 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; 168 } 169 return NULL; 170 } 171 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) 175 { 176 Search_Path *path; 177 char epath[MAXPATHLEN]; 178 size_t len; 179 180 len = _rtld_expand_path(epath, sizeof(epath), execname, bp, ep); 181 if (len == 0) 182 return path_p; 183 184 if (_rtld_find_path(*head_p, bp, ep - bp) != NULL) 185 return path_p; 186 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; 193 194 dbg((" added path \"%s\"", path->sp_path)); 195 return path_p; 196 } 197 198 void 199 _rtld_add_paths(const char *execname, Search_Path **path_p, const char *pathstr) 200 { 201 Search_Path **head_p = path_p; 202 203 if (pathstr == NULL) 204 return; 205 206 if (pathstr[0] == ':') { 207 /* 208 * Leading colon means append to current path 209 */ 210 while ((*path_p) != NULL) 211 path_p = &(*path_p)->sp_next; 212 pathstr++; 213 } 214 215 for (;;) { 216 const char *bp = pathstr; 217 const char *ep = strchr(bp, ':'); 218 if (ep == NULL) 219 ep = &pathstr[strlen(pathstr)]; 220 221 path_p = _rtld_append_path(head_p, path_p, execname, bp, ep); 222 223 if (ep[0] == '\0') 224 break; 225 pathstr = ep + 1; 226 } 227 } 228 229 /* 230 * Process library mappings of the form: 231 * <library_name> <machdep_variable> <value,...:library_name,...> ... 232 */ 233 static void 234 _rtld_process_mapping(Library_Xform **lib_p, const char *bp, const char *ep) 235 { 236 Library_Xform *hwptr = NULL; 237 const char *ptr, *key, *ekey, *lib, *elib, *l; 238 int i, j; 239 240 dbg((" processing mapping \"%.*s\"", (int)(ep - bp), bp)); 241 242 if ((ptr = getword(&bp, ep, WS)) == NULL || ptr == bp) 243 return; 244 245 dbg((" library \"%.*s\"", (int)(bp - ptr), ptr)); 246 247 hwptr = xmalloc(sizeof(*hwptr)); 248 memset(hwptr, 0, sizeof(*hwptr)); 249 hwptr->name = exstrdup(ptr, bp); 250 251 bp++; 252 253 if ((ptr = getword(&bp, ep, WS)) == NULL || ptr == bp) { 254 xwarnx("missing sysctl variable name"); 255 goto cleanup; 256 } 257 258 dbg((" sysctl \"%.*s\"", (int)(bp - ptr), ptr)); 259 260 hwptr->ctlname = exstrdup(ptr, bp); 261 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; 266 267 if (i == RTLD_MAX_ENTRY) { 268 no_more: 269 xwarnx("maximum library entries exceeded `%s'", 270 hwptr->name); 271 goto cleanup; 272 } 273 if ((key = getstr(&ptr, bp, ":")) == NULL) { 274 xwarnx("missing sysctl variable value for `%s'", 275 hwptr->name); 276 goto cleanup; 277 } 278 ekey = ptr++; 279 if ((lib = getstr(&ptr, bp, ":")) == NULL) { 280 xwarnx("missing sysctl library list for `%s'", 281 hwptr->name); 282 goto cleanup; 283 } 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; 291 } 292 dbg((" library \"%.*s\"", (int)(lib - l), l)); 293 hwptr->entry[i].library[j] = exstrdup(l, lib); 294 } 295 if (j == 0) { 296 xwarnx("No library map entries for `%s/%.*s'", 297 hwptr->name, (int)(bp - ptr), ptr); 298 goto cleanup; 299 } 300 j = i; 301 for (; (l = getstr(&key, ekey, ",")) != NULL; i++, key++) { 302 /* 303 * Allow empty key (it is valid as string 304 * value). Thus, we loop at least once and 305 * `i' is incremented. 306 */ 307 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); 316 } 317 } 318 319 if (i == 0) { 320 xwarnx("No library entries for `%s'", hwptr->name); 321 goto cleanup; 322 } 323 324 hwptr->next = *lib_p; 325 *lib_p = hwptr; 326 327 return; 328 329 cleanup: 330 if (hwptr->name) 331 xfree(hwptr->name); 332 xfree(hwptr); 333 } 334 335 void 336 _rtld_process_hints(const char *execname, Search_Path **path_p, 337 Library_Xform **lib_p, const char *fname) 338 { 339 int fd; 340 char *buf; 341 const char *b, *ep, *ptr; 342 struct stat st; 343 size_t sz; 344 Search_Path **head_p = path_p; 345 346 if ((fd = open(fname, O_RDONLY)) == -1) { 347 /* Don't complain */ 348 return; 349 } 350 351 if (fstat(fd, &st) == -1) { 352 /* Complain */ 353 xwarn("fstat: %s", fname); 354 return; 355 } 356 357 sz = (size_t) st.st_size; 358 359 buf = mmap(0, sz, PROT_READ, MAP_SHARED|MAP_FILE, fd, 0); 360 if (buf == MAP_FAILED) { 361 xwarn("mmap: %s", fname); 362 (void)close(fd); 363 return; 364 } 365 (void)close(fd); 366 367 while ((*path_p) != NULL) 368 path_p = &(*path_p)->sp_next; 369 370 for (b = buf, ep = buf + sz; b < ep; b++) { 371 (void)getcstr(&b, ep, WS); 372 if (b == ep) 373 break; 374 375 ptr = getstr(&b, ep, "\n#"); 376 if (*ptr == '/') { 377 /* 378 * Since '/' != '\n' and != '#', we know ptr < 379 * b. And we will stop when b[-1] == '/'. 380 */ 381 while (b[-1] == ' ' || b[-1] == '\t') 382 b--; 383 path_p = _rtld_append_path(head_p, path_p, execname, 384 ptr, b); 385 } else 386 _rtld_process_mapping(lib_p, ptr, b); 387 388 /* 389 * b points one of ' ', \t, \n, # or equal to ep. So, 390 * make sure we are at newline or end of string. 391 */ 392 (void)getstr(&b, ep, "\n"); 393 } 394 395 (void)munmap(buf, sz); 396 } 397 398 /* Basic name -> sysctl MIB translation */ 399 int 400 _rtld_sysctl(const char *name, void *oldp, size_t *oldlen) 401 { 402 const char *node, *ep; 403 struct sysctlnode query, *result, *newresult; 404 int mib[CTL_MAXNAME], i, r; 405 size_t res_size, n; 406 u_int miblen = 0; 407 408 /* Start with 16 entries, will grow it up as needed. */ 409 res_size = 16 * sizeof(struct sysctlnode); 410 result = xmalloc(res_size); 411 if (result == NULL) 412 return (-1); 413 414 ep = name + strlen(name); 415 do { 416 i = -1; 417 while (*name == '/' || *name == '.') 418 name++; 419 if (name >= ep) 420 break; 421 422 mib[miblen] = CTL_QUERY; 423 memset(&query, 0, sizeof(query)); 424 query.sysctl_flags = SYSCTL_VERSION; 425 426 n = res_size; 427 if (sysctl(mib, miblen + 1, result, &n, &query, 428 sizeof(query)) == -1) { 429 if (errno != ENOMEM) 430 goto bad; 431 /* Grow up result */ 432 res_size = n; 433 newresult = xrealloc(result, res_size); 434 if (newresult == NULL) 435 goto bad; 436 result = newresult; 437 if (sysctl(mib, miblen + 1, result, &n, &query, 438 sizeof(query)) == -1) 439 goto bad; 440 } 441 n /= sizeof(struct sysctlnode); 442 443 node = getstr(&name, ep, "./"); 444 445 for (i = 0; i < n; i++) 446 if (matchstr(result[i].sysctl_name, node, name)) { 447 mib[miblen] = result[i].sysctl_num; 448 miblen++; 449 break; 450 } 451 } while (name < ep && miblen <= CTL_MAXNAME); 452 453 if (name < ep || i == -1) 454 goto bad; 455 r = SYSCTL_TYPE(result[i].sysctl_flags); 456 457 xfree(result); 458 if (sysctl(mib, miblen, oldp, oldlen, NULL, 0) == -1) 459 return (-1); 460 return r; 461 462 bad: 463 xfree(result); 464 return (-1); 465 } 466