129ade362SMatthew N. Dodd /* 229ade362SMatthew N. Dodd * $FreeBSD$ 329ade362SMatthew N. Dodd */ 429ade362SMatthew N. Dodd 5faf66437SBaptiste Daroussin #include <sys/types.h> 629ade362SMatthew N. Dodd #include <sys/param.h> 768f1db20SKonstantin Belousov #include <sys/fcntl.h> 868f1db20SKonstantin Belousov #include <sys/mman.h> 968f1db20SKonstantin Belousov #include <sys/queue.h> 1068f1db20SKonstantin Belousov #include <sys/stat.h> 11faf66437SBaptiste Daroussin #include <dirent.h> 1268f1db20SKonstantin Belousov #include <errno.h> 1368f1db20SKonstantin Belousov #include <stdlib.h> 1468f1db20SKonstantin Belousov #include <string.h> 1529ade362SMatthew N. Dodd 163467e8b8SMatthew N. Dodd #include "debug.h" 173467e8b8SMatthew N. Dodd #include "rtld.h" 187227105fSMatthew N. Dodd #include "libmap.h" 19b2a4014cSWarner Losh #include "paths.h" 20c905e45dSPeter Wemm 2129ade362SMatthew N. Dodd TAILQ_HEAD(lm_list, lm); 2229ade362SMatthew N. Dodd struct lm { 2329ade362SMatthew N. Dodd char *f; 2429ade362SMatthew N. Dodd char *t; 2529ade362SMatthew N. Dodd TAILQ_ENTRY(lm) lm_link; 2629ade362SMatthew N. Dodd }; 2729ade362SMatthew N. Dodd 2829ade362SMatthew N. Dodd TAILQ_HEAD(lmp_list, lmp) lmp_head = TAILQ_HEAD_INITIALIZER(lmp_head); 2929ade362SMatthew N. Dodd struct lmp { 3029ade362SMatthew N. Dodd char *p; 31966efcc7SMatthew N. Dodd enum { T_EXACT=0, T_BASENAME, T_DIRECTORY } type; 3229ade362SMatthew N. Dodd struct lm_list lml; 3329ade362SMatthew N. Dodd TAILQ_ENTRY(lmp) lmp_link; 3429ade362SMatthew N. Dodd }; 3529ade362SMatthew N. Dodd 36faf66437SBaptiste Daroussin static TAILQ_HEAD(lmc_list, lmc) lmc_head = TAILQ_HEAD_INITIALIZER(lmc_head); 37faf66437SBaptiste Daroussin struct lmc { 38faf66437SBaptiste Daroussin char *path; 39faf66437SBaptiste Daroussin TAILQ_ENTRY(lmc) next; 40faf66437SBaptiste Daroussin }; 41faf66437SBaptiste Daroussin 425b08cb04SMatthew N. Dodd static int lm_count; 435b08cb04SMatthew N. Dodd 4468f1db20SKonstantin Belousov static void lmc_parse(char *, size_t); 45faf66437SBaptiste Daroussin static void lmc_parse_file(char *); 46faf66437SBaptiste Daroussin static void lmc_parse_dir(char *); 471340fc10SMatthew N. Dodd static void lm_add(const char *, const char *, const char *); 4829ade362SMatthew N. Dodd static void lm_free(struct lm_list *); 4929ade362SMatthew N. Dodd static char *lml_find(struct lm_list *, const char *); 5029ade362SMatthew N. Dodd static struct lm_list *lmp_find(const char *); 5129ade362SMatthew N. Dodd static struct lm_list *lmp_init(char *); 52966efcc7SMatthew N. Dodd static const char *quickbasename(const char *); 5329ade362SMatthew N. Dodd 54623b6bd2SMatthew N. Dodd #define iseol(c) (((c) == '#') || ((c) == '\0') || \ 55623b6bd2SMatthew N. Dodd ((c) == '\n') || ((c) == '\r')) 56623b6bd2SMatthew N. Dodd 57b36070f5SKonstantin Belousov /* 58b36070f5SKonstantin Belousov * Do not use ctype.h macros, which rely on working TLS. It is 59b36070f5SKonstantin Belousov * too early to have thread-local variables functional. 60b36070f5SKonstantin Belousov */ 6144976acaSSergey Kandaurov #define rtld_isspace(c) ((c) == ' ' || (c) == '\t') 62b36070f5SKonstantin Belousov 634402996dSMatthew N. Dodd int 645b08cb04SMatthew N. Dodd lm_init(char *libmap_override) 6529ade362SMatthew N. Dodd { 66faf66437SBaptiste Daroussin char *p; 6729ade362SMatthew N. Dodd 6868f1db20SKonstantin Belousov dbg("lm_init(\"%s\")", libmap_override); 6929ade362SMatthew N. Dodd TAILQ_INIT(&lmp_head); 7029ade362SMatthew N. Dodd 71*3deca56fSWarner Losh lmc_parse_file(ld_path_libmap_conf); 725b08cb04SMatthew N. Dodd 735b08cb04SMatthew N. Dodd if (libmap_override) { 7468f1db20SKonstantin Belousov /* 75490c68deSKonstantin Belousov * Do some character replacement to make $LDLIBMAP look 7668f1db20SKonstantin Belousov * like a text file, then parse it. 7768f1db20SKonstantin Belousov */ 785b08cb04SMatthew N. Dodd libmap_override = xstrdup(libmap_override); 795b08cb04SMatthew N. Dodd for (p = libmap_override; *p; p++) { 805b08cb04SMatthew N. Dodd switch (*p) { 815b08cb04SMatthew N. Dodd case '=': 8268f1db20SKonstantin Belousov *p = ' '; 8368f1db20SKonstantin Belousov break; 845b08cb04SMatthew N. Dodd case ',': 8568f1db20SKonstantin Belousov *p = '\n'; 8668f1db20SKonstantin Belousov break; 875b08cb04SMatthew N. Dodd } 885b08cb04SMatthew N. Dodd } 89490c68deSKonstantin Belousov lmc_parse(libmap_override, p - libmap_override); 90490c68deSKonstantin Belousov free(libmap_override); 915b08cb04SMatthew N. Dodd } 925b08cb04SMatthew N. Dodd 935b08cb04SMatthew N. Dodd return (lm_count == 0); 945b08cb04SMatthew N. Dodd } 955b08cb04SMatthew N. Dodd 965b08cb04SMatthew N. Dodd static void 97faf66437SBaptiste Daroussin lmc_parse_file(char *path) 98faf66437SBaptiste Daroussin { 99faf66437SBaptiste Daroussin struct lmc *p; 100faf66437SBaptiste Daroussin struct stat st; 101faf66437SBaptiste Daroussin int fd; 102faf66437SBaptiste Daroussin char *rpath; 103faf66437SBaptiste Daroussin char *lm_map; 104faf66437SBaptiste Daroussin 105faf66437SBaptiste Daroussin rpath = realpath(path, NULL); 106faf66437SBaptiste Daroussin if (rpath == NULL) 107faf66437SBaptiste Daroussin return; 108faf66437SBaptiste Daroussin 109faf66437SBaptiste Daroussin TAILQ_FOREACH(p, &lmc_head, next) { 110faf66437SBaptiste Daroussin if (strcmp(p->path, rpath) == 0) { 111faf66437SBaptiste Daroussin free(rpath); 112faf66437SBaptiste Daroussin return; 113faf66437SBaptiste Daroussin } 114faf66437SBaptiste Daroussin } 115faf66437SBaptiste Daroussin 116e1942829SJilles Tjoelker fd = open(rpath, O_RDONLY | O_CLOEXEC); 117faf66437SBaptiste Daroussin if (fd == -1) { 1185c98f307SBaptiste Daroussin dbg("lm_parse_file: open(\"%s\") failed, %s", rpath, 119faf66437SBaptiste Daroussin rtld_strerror(errno)); 120faf66437SBaptiste Daroussin free(rpath); 121faf66437SBaptiste Daroussin return; 122faf66437SBaptiste Daroussin } 123faf66437SBaptiste Daroussin if (fstat(fd, &st) == -1) { 124faf66437SBaptiste Daroussin close(fd); 1255c98f307SBaptiste Daroussin dbg("lm_parse_file: fstat(\"%s\") failed, %s", rpath, 126faf66437SBaptiste Daroussin rtld_strerror(errno)); 127faf66437SBaptiste Daroussin free(rpath); 128faf66437SBaptiste Daroussin return; 129faf66437SBaptiste Daroussin } 130faf66437SBaptiste Daroussin lm_map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); 131faf66437SBaptiste Daroussin if (lm_map == (const char *)MAP_FAILED) { 132faf66437SBaptiste Daroussin close(fd); 1335c98f307SBaptiste Daroussin dbg("lm_parse_file: mmap(\"%s\") failed, %s", rpath, 134faf66437SBaptiste Daroussin rtld_strerror(errno)); 135faf66437SBaptiste Daroussin free(rpath); 136faf66437SBaptiste Daroussin return; 137faf66437SBaptiste Daroussin } 138faf66437SBaptiste Daroussin close(fd); 139faf66437SBaptiste Daroussin p = xmalloc(sizeof(struct lmc)); 140faf66437SBaptiste Daroussin p->path = rpath; 141faf66437SBaptiste Daroussin TAILQ_INSERT_HEAD(&lmc_head, p, next); 142faf66437SBaptiste Daroussin lmc_parse(lm_map, st.st_size); 143faf66437SBaptiste Daroussin munmap(lm_map, st.st_size); 144faf66437SBaptiste Daroussin } 145faf66437SBaptiste Daroussin 146faf66437SBaptiste Daroussin static void 147faf66437SBaptiste Daroussin lmc_parse_dir(char *idir) 148faf66437SBaptiste Daroussin { 149faf66437SBaptiste Daroussin DIR *d; 150faf66437SBaptiste Daroussin struct dirent *dp; 151faf66437SBaptiste Daroussin struct lmc *p; 152faf66437SBaptiste Daroussin char conffile[MAXPATHLEN]; 153faf66437SBaptiste Daroussin char *ext; 154faf66437SBaptiste Daroussin char *rpath; 155faf66437SBaptiste Daroussin 156faf66437SBaptiste Daroussin rpath = realpath(idir, NULL); 157faf66437SBaptiste Daroussin if (rpath == NULL) 158faf66437SBaptiste Daroussin return; 159faf66437SBaptiste Daroussin 160faf66437SBaptiste Daroussin TAILQ_FOREACH(p, &lmc_head, next) { 161faf66437SBaptiste Daroussin if (strcmp(p->path, rpath) == 0) { 162faf66437SBaptiste Daroussin free(rpath); 163faf66437SBaptiste Daroussin return; 164faf66437SBaptiste Daroussin } 165faf66437SBaptiste Daroussin } 166faf66437SBaptiste Daroussin d = opendir(idir); 167faf66437SBaptiste Daroussin if (d == NULL) { 168faf66437SBaptiste Daroussin free(rpath); 169faf66437SBaptiste Daroussin return; 170faf66437SBaptiste Daroussin } 171faf66437SBaptiste Daroussin 172faf66437SBaptiste Daroussin p = xmalloc(sizeof(struct lmc)); 173faf66437SBaptiste Daroussin p->path = rpath; 174faf66437SBaptiste Daroussin TAILQ_INSERT_HEAD(&lmc_head, p, next); 175faf66437SBaptiste Daroussin 176faf66437SBaptiste Daroussin while ((dp = readdir(d)) != NULL) { 177faf66437SBaptiste Daroussin if (dp->d_ino == 0) 178faf66437SBaptiste Daroussin continue; 179faf66437SBaptiste Daroussin if (dp->d_type != DT_REG) 180faf66437SBaptiste Daroussin continue; 181faf66437SBaptiste Daroussin ext = strrchr(dp->d_name, '.'); 182faf66437SBaptiste Daroussin if (ext == NULL) 183faf66437SBaptiste Daroussin continue; 184faf66437SBaptiste Daroussin if (strcmp(ext, ".conf") != 0) 185faf66437SBaptiste Daroussin continue; 186faf66437SBaptiste Daroussin if (strlcpy(conffile, idir, MAXPATHLEN) >= MAXPATHLEN) 187faf66437SBaptiste Daroussin continue; /* too long */ 188faf66437SBaptiste Daroussin if (strlcat(conffile, "/", MAXPATHLEN) >= MAXPATHLEN) 189faf66437SBaptiste Daroussin continue; /* too long */ 190faf66437SBaptiste Daroussin if (strlcat(conffile, dp->d_name, MAXPATHLEN) >= MAXPATHLEN) 191faf66437SBaptiste Daroussin continue; /* too long */ 192faf66437SBaptiste Daroussin lmc_parse_file(conffile); 193faf66437SBaptiste Daroussin } 194faf66437SBaptiste Daroussin closedir(d); 195faf66437SBaptiste Daroussin } 196faf66437SBaptiste Daroussin 197faf66437SBaptiste Daroussin static void 19868f1db20SKonstantin Belousov lmc_parse(char *lm_p, size_t lm_len) 1995b08cb04SMatthew N. Dodd { 20068f1db20SKonstantin Belousov char *cp, *f, *t, *c, *p; 2015b08cb04SMatthew N. Dodd char prog[MAXPATHLEN]; 202faf66437SBaptiste Daroussin /* allow includedir + full length path */ 203faf66437SBaptiste Daroussin char line[MAXPATHLEN + 13]; 20468f1db20SKonstantin Belousov size_t cnt; 20568f1db20SKonstantin Belousov int i; 2065b08cb04SMatthew N. Dodd 20768f1db20SKonstantin Belousov cnt = 0; 20829ade362SMatthew N. Dodd p = NULL; 20968f1db20SKonstantin Belousov while (cnt < lm_len) { 21068f1db20SKonstantin Belousov i = 0; 2110fa46a42SPedro F. Giffuni while (cnt < lm_len && lm_p[cnt] != '\n' && 21268f1db20SKonstantin Belousov i < sizeof(line) - 1) { 21368f1db20SKonstantin Belousov line[i] = lm_p[cnt]; 21468f1db20SKonstantin Belousov cnt++; 21568f1db20SKonstantin Belousov i++; 21668f1db20SKonstantin Belousov } 21768f1db20SKonstantin Belousov line[i] = '\0'; 2180fa46a42SPedro F. Giffuni while (cnt < lm_len && lm_p[cnt] != '\n') 21968f1db20SKonstantin Belousov cnt++; 22068f1db20SKonstantin Belousov /* skip over nl */ 22168f1db20SKonstantin Belousov cnt++; 22268f1db20SKonstantin Belousov 22368f1db20SKonstantin Belousov cp = &line[0]; 2241340fc10SMatthew N. Dodd t = f = c = NULL; 225623b6bd2SMatthew N. Dodd 22629ade362SMatthew N. Dodd /* Skip over leading space */ 22744976acaSSergey Kandaurov while (rtld_isspace(*cp)) cp++; 228623b6bd2SMatthew N. Dodd 22929ade362SMatthew N. Dodd /* Found a comment or EOL */ 230486089f0SAlexander Kabaev if (iseol(*cp)) continue; 231623b6bd2SMatthew N. Dodd 232623b6bd2SMatthew N. Dodd /* Found a constraint selector */ 23329ade362SMatthew N. Dodd if (*cp == '[') { 23429ade362SMatthew N. Dodd cp++; 235623b6bd2SMatthew N. Dodd 23629ade362SMatthew N. Dodd /* Skip leading space */ 23744976acaSSergey Kandaurov while (rtld_isspace(*cp)) cp++; 238623b6bd2SMatthew N. Dodd 23929ade362SMatthew N. Dodd /* Found comment, EOL or end of selector */ 240486089f0SAlexander Kabaev if (iseol(*cp) || *cp == ']') 241486089f0SAlexander Kabaev continue; 242623b6bd2SMatthew N. Dodd 2431340fc10SMatthew N. Dodd c = cp++; 24429ade362SMatthew N. Dodd /* Skip to end of word */ 24544976acaSSergey Kandaurov while (!rtld_isspace(*cp) && !iseol(*cp) && *cp != ']') 246486089f0SAlexander Kabaev cp++; 247623b6bd2SMatthew N. Dodd 248623b6bd2SMatthew N. Dodd /* Skip and zero out trailing space */ 24944976acaSSergey Kandaurov while (rtld_isspace(*cp)) *cp++ = '\0'; 250623b6bd2SMatthew N. Dodd 251623b6bd2SMatthew N. Dodd /* Check if there is a closing brace */ 252486089f0SAlexander Kabaev if (*cp != ']') continue; 253623b6bd2SMatthew N. Dodd 254623b6bd2SMatthew N. Dodd /* Terminate string if there was no trailing space */ 25529ade362SMatthew N. Dodd *cp++ = '\0'; 256623b6bd2SMatthew N. Dodd 257623b6bd2SMatthew N. Dodd /* 258623b6bd2SMatthew N. Dodd * There should be nothing except whitespace or comment 2596d5d786fSAlexander Kabaev from this point to the end of the line. 260623b6bd2SMatthew N. Dodd */ 26144976acaSSergey Kandaurov while(rtld_isspace(*cp)) cp++; 262486089f0SAlexander Kabaev if (!iseol(*cp)) continue; 263623b6bd2SMatthew N. Dodd 264faf66437SBaptiste Daroussin if (strlcpy(prog, c, sizeof prog) >= sizeof prog) 265faf66437SBaptiste Daroussin continue; 26629ade362SMatthew N. Dodd p = prog; 267486089f0SAlexander Kabaev continue; 268623b6bd2SMatthew N. Dodd } 269623b6bd2SMatthew N. Dodd 270623b6bd2SMatthew N. Dodd /* Parse the 'from' candidate. */ 271486089f0SAlexander Kabaev f = cp++; 27244976acaSSergey Kandaurov while (!rtld_isspace(*cp) && !iseol(*cp)) cp++; 27329ade362SMatthew N. Dodd 274623b6bd2SMatthew N. Dodd /* Skip and zero out the trailing whitespace */ 27544976acaSSergey Kandaurov while (rtld_isspace(*cp)) *cp++ = '\0'; 276623b6bd2SMatthew N. Dodd 277623b6bd2SMatthew N. Dodd /* Found a comment or EOL */ 278486089f0SAlexander Kabaev if (iseol(*cp)) continue; 279623b6bd2SMatthew N. Dodd 280623b6bd2SMatthew N. Dodd /* Parse 'to' mapping */ 281486089f0SAlexander Kabaev t = cp++; 28244976acaSSergey Kandaurov while (!rtld_isspace(*cp) && !iseol(*cp)) cp++; 283623b6bd2SMatthew N. Dodd 284486089f0SAlexander Kabaev /* Skip and zero out the trailing whitespace */ 28544976acaSSergey Kandaurov while (rtld_isspace(*cp)) *cp++ = '\0'; 286486089f0SAlexander Kabaev 287486089f0SAlexander Kabaev /* Should be no extra tokens at this point */ 288486089f0SAlexander Kabaev if (!iseol(*cp)) continue; 289486089f0SAlexander Kabaev 290486089f0SAlexander Kabaev *cp = '\0'; 291faf66437SBaptiste Daroussin if (strcmp(f, "includedir") == 0) 292faf66437SBaptiste Daroussin lmc_parse_dir(t); 293faf66437SBaptiste Daroussin else if (strcmp(f, "include") == 0) 294faf66437SBaptiste Daroussin lmc_parse_file(t); 295faf66437SBaptiste Daroussin else 2961340fc10SMatthew N. Dodd lm_add(p, f, t); 29729ade362SMatthew N. Dodd } 29829ade362SMatthew N. Dodd } 29929ade362SMatthew N. Dodd 30029ade362SMatthew N. Dodd static void 30129ade362SMatthew N. Dodd lm_free (struct lm_list *lml) 30229ade362SMatthew N. Dodd { 30329ade362SMatthew N. Dodd struct lm *lm; 30429ade362SMatthew N. Dodd 3051aac1ed6SMatthew N. Dodd dbg("%s(%p)", __func__, lml); 3061aac1ed6SMatthew N. Dodd 30729ade362SMatthew N. Dodd while (!TAILQ_EMPTY(lml)) { 30829ade362SMatthew N. Dodd lm = TAILQ_FIRST(lml); 30929ade362SMatthew N. Dodd TAILQ_REMOVE(lml, lm, lm_link); 31029ade362SMatthew N. Dodd free(lm->f); 31129ade362SMatthew N. Dodd free(lm->t); 31229ade362SMatthew N. Dodd free(lm); 31329ade362SMatthew N. Dodd } 31429ade362SMatthew N. Dodd return; 31529ade362SMatthew N. Dodd } 31629ade362SMatthew N. Dodd 31729ade362SMatthew N. Dodd void 31829ade362SMatthew N. Dodd lm_fini (void) 31929ade362SMatthew N. Dodd { 32029ade362SMatthew N. Dodd struct lmp *lmp; 321faf66437SBaptiste Daroussin struct lmc *p; 32229ade362SMatthew N. Dodd 3231aac1ed6SMatthew N. Dodd dbg("%s()", __func__); 3241aac1ed6SMatthew N. Dodd 325faf66437SBaptiste Daroussin while (!TAILQ_EMPTY(&lmc_head)) { 326faf66437SBaptiste Daroussin p = TAILQ_FIRST(&lmc_head); 327faf66437SBaptiste Daroussin TAILQ_REMOVE(&lmc_head, p, next); 328faf66437SBaptiste Daroussin free(p->path); 329faf66437SBaptiste Daroussin free(p); 330faf66437SBaptiste Daroussin } 331faf66437SBaptiste Daroussin 33229ade362SMatthew N. Dodd while (!TAILQ_EMPTY(&lmp_head)) { 33329ade362SMatthew N. Dodd lmp = TAILQ_FIRST(&lmp_head); 33429ade362SMatthew N. Dodd TAILQ_REMOVE(&lmp_head, lmp, lmp_link); 33529ade362SMatthew N. Dodd free(lmp->p); 33629ade362SMatthew N. Dodd lm_free(&lmp->lml); 33729ade362SMatthew N. Dodd free(lmp); 33829ade362SMatthew N. Dodd } 33929ade362SMatthew N. Dodd return; 34029ade362SMatthew N. Dodd } 34129ade362SMatthew N. Dodd 34229ade362SMatthew N. Dodd static void 3431340fc10SMatthew N. Dodd lm_add (const char *p, const char *f, const char *t) 34429ade362SMatthew N. Dodd { 34529ade362SMatthew N. Dodd struct lm_list *lml; 34629ade362SMatthew N. Dodd struct lm *lm; 34729ade362SMatthew N. Dodd 34829ade362SMatthew N. Dodd if (p == NULL) 34929ade362SMatthew N. Dodd p = "$DEFAULT$"; 35029ade362SMatthew N. Dodd 3511aac1ed6SMatthew N. Dodd dbg("%s(\"%s\", \"%s\", \"%s\")", __func__, p, f, t); 3521aac1ed6SMatthew N. Dodd 35329ade362SMatthew N. Dodd if ((lml = lmp_find(p)) == NULL) 3543467e8b8SMatthew N. Dodd lml = lmp_init(xstrdup(p)); 35529ade362SMatthew N. Dodd 3563467e8b8SMatthew N. Dodd lm = xmalloc(sizeof(struct lm)); 3571340fc10SMatthew N. Dodd lm->f = xstrdup(f); 3581340fc10SMatthew N. Dodd lm->t = xstrdup(t); 35929ade362SMatthew N. Dodd TAILQ_INSERT_HEAD(lml, lm, lm_link); 3605b08cb04SMatthew N. Dodd lm_count++; 36129ade362SMatthew N. Dodd } 36229ade362SMatthew N. Dodd 36329ade362SMatthew N. Dodd char * 36429ade362SMatthew N. Dodd lm_find (const char *p, const char *f) 36529ade362SMatthew N. Dodd { 36629ade362SMatthew N. Dodd struct lm_list *lml; 36729ade362SMatthew N. Dodd char *t; 36829ade362SMatthew N. Dodd 3691aac1ed6SMatthew N. Dodd dbg("%s(\"%s\", \"%s\")", __func__, p, f); 3701aac1ed6SMatthew N. Dodd 37129ade362SMatthew N. Dodd if (p != NULL && (lml = lmp_find(p)) != NULL) { 37229ade362SMatthew N. Dodd t = lml_find(lml, f); 3733467e8b8SMatthew N. Dodd if (t != NULL) { 3743467e8b8SMatthew N. Dodd /* 3753467e8b8SMatthew N. Dodd * Add a global mapping if we have 3763467e8b8SMatthew N. Dodd * a successful constrained match. 3773467e8b8SMatthew N. Dodd */ 3781340fc10SMatthew N. Dodd lm_add(NULL, f, t); 37929ade362SMatthew N. Dodd return (t); 38029ade362SMatthew N. Dodd } 3813467e8b8SMatthew N. Dodd } 38229ade362SMatthew N. Dodd lml = lmp_find("$DEFAULT$"); 38329ade362SMatthew N. Dodd if (lml != NULL) 38429ade362SMatthew N. Dodd return (lml_find(lml, f)); 38529ade362SMatthew N. Dodd else 38629ade362SMatthew N. Dodd return (NULL); 38729ade362SMatthew N. Dodd } 38829ade362SMatthew N. Dodd 389966efcc7SMatthew N. Dodd /* Given a libmap translation list and a library name, return the 390966efcc7SMatthew N. Dodd replacement library, or NULL */ 391c905e45dSPeter Wemm char * 392c905e45dSPeter Wemm lm_findn (const char *p, const char *f, const int n) 393c905e45dSPeter Wemm { 394c905e45dSPeter Wemm char pathbuf[64], *s, *t; 395c905e45dSPeter Wemm 3968c6a035eSPeter Wemm if (n < sizeof(pathbuf) - 1) 397c905e45dSPeter Wemm s = pathbuf; 3988c6a035eSPeter Wemm else 399c905e45dSPeter Wemm s = xmalloc(n + 1); 4008c6a035eSPeter Wemm memcpy(s, f, n); 4018c6a035eSPeter Wemm s[n] = '\0'; 402c905e45dSPeter Wemm t = lm_find(p, s); 403c905e45dSPeter Wemm if (s != pathbuf) 404c905e45dSPeter Wemm free(s); 405c905e45dSPeter Wemm return (t); 406c905e45dSPeter Wemm } 407c905e45dSPeter Wemm 40829ade362SMatthew N. Dodd static char * 40929ade362SMatthew N. Dodd lml_find (struct lm_list *lmh, const char *f) 41029ade362SMatthew N. Dodd { 41129ade362SMatthew N. Dodd struct lm *lm; 41229ade362SMatthew N. Dodd 4131aac1ed6SMatthew N. Dodd dbg("%s(%p, \"%s\")", __func__, lmh, f); 4141aac1ed6SMatthew N. Dodd 41529ade362SMatthew N. Dodd TAILQ_FOREACH(lm, lmh, lm_link) 416c905e45dSPeter Wemm if (strcmp(f, lm->f) == 0) 41729ade362SMatthew N. Dodd return (lm->t); 418d33da23fSMatthew N. Dodd return (NULL); 41929ade362SMatthew N. Dodd } 42029ade362SMatthew N. Dodd 421966efcc7SMatthew N. Dodd /* Given an executable name, return a pointer to the translation list or 422966efcc7SMatthew N. Dodd NULL if no matches */ 42329ade362SMatthew N. Dodd static struct lm_list * 42429ade362SMatthew N. Dodd lmp_find (const char *n) 42529ade362SMatthew N. Dodd { 42629ade362SMatthew N. Dodd struct lmp *lmp; 42729ade362SMatthew N. Dodd 4281aac1ed6SMatthew N. Dodd dbg("%s(\"%s\")", __func__, n); 4291aac1ed6SMatthew N. Dodd 43029ade362SMatthew N. Dodd TAILQ_FOREACH(lmp, &lmp_head, lmp_link) 431966efcc7SMatthew N. Dodd if ((lmp->type == T_EXACT && strcmp(n, lmp->p) == 0) || 432966efcc7SMatthew N. Dodd (lmp->type == T_DIRECTORY && strncmp(n, lmp->p, strlen(lmp->p)) == 0) || 433966efcc7SMatthew N. Dodd (lmp->type == T_BASENAME && strcmp(quickbasename(n), lmp->p) == 0)) 43429ade362SMatthew N. Dodd return (&lmp->lml); 43529ade362SMatthew N. Dodd return (NULL); 43629ade362SMatthew N. Dodd } 43729ade362SMatthew N. Dodd 43829ade362SMatthew N. Dodd static struct lm_list * 43929ade362SMatthew N. Dodd lmp_init (char *n) 44029ade362SMatthew N. Dodd { 44129ade362SMatthew N. Dodd struct lmp *lmp; 44229ade362SMatthew N. Dodd 4431aac1ed6SMatthew N. Dodd dbg("%s(\"%s\")", __func__, n); 4441aac1ed6SMatthew N. Dodd 4453467e8b8SMatthew N. Dodd lmp = xmalloc(sizeof(struct lmp)); 44629ade362SMatthew N. Dodd lmp->p = n; 447966efcc7SMatthew N. Dodd if (n[strlen(n)-1] == '/') 448966efcc7SMatthew N. Dodd lmp->type = T_DIRECTORY; 449966efcc7SMatthew N. Dodd else if (strchr(n,'/') == NULL) 450966efcc7SMatthew N. Dodd lmp->type = T_BASENAME; 451966efcc7SMatthew N. Dodd else 452966efcc7SMatthew N. Dodd lmp->type = T_EXACT; 45329ade362SMatthew N. Dodd TAILQ_INIT(&lmp->lml); 45429ade362SMatthew N. Dodd TAILQ_INSERT_HEAD(&lmp_head, lmp, lmp_link); 45529ade362SMatthew N. Dodd 45629ade362SMatthew N. Dodd return (&lmp->lml); 45729ade362SMatthew N. Dodd } 458966efcc7SMatthew N. Dodd 459966efcc7SMatthew N. Dodd /* libc basename is overkill. Return a pointer to the character after the 460966efcc7SMatthew N. Dodd last /, or the original string if there are no slashes. */ 461966efcc7SMatthew N. Dodd static const char * 462966efcc7SMatthew N. Dodd quickbasename (const char *path) 463966efcc7SMatthew N. Dodd { 464966efcc7SMatthew N. Dodd const char *p = path; 465d33da23fSMatthew N. Dodd for (; *path; path++) { 466966efcc7SMatthew N. Dodd if (*path == '/') 467966efcc7SMatthew N. Dodd p = path+1; 468966efcc7SMatthew N. Dodd } 469d33da23fSMatthew N. Dodd return (p); 470966efcc7SMatthew N. Dodd } 471