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" 20*b54a59f3SAlex Richardson #include "rtld_libc.h" 21c905e45dSPeter Wemm 2229ade362SMatthew N. Dodd TAILQ_HEAD(lm_list, lm); 2329ade362SMatthew N. Dodd struct lm { 2429ade362SMatthew N. Dodd char *f; 2529ade362SMatthew N. Dodd char *t; 2629ade362SMatthew N. Dodd TAILQ_ENTRY(lm) lm_link; 2729ade362SMatthew N. Dodd }; 2829ade362SMatthew N. Dodd 293ab5b6bdSAlex Richardson static TAILQ_HEAD(lmp_list, lmp) lmp_head = TAILQ_HEAD_INITIALIZER(lmp_head); 3029ade362SMatthew N. Dodd struct lmp { 3129ade362SMatthew N. Dodd char *p; 32966efcc7SMatthew N. Dodd enum { T_EXACT=0, T_BASENAME, T_DIRECTORY } type; 3329ade362SMatthew N. Dodd struct lm_list lml; 3429ade362SMatthew N. Dodd TAILQ_ENTRY(lmp) lmp_link; 3529ade362SMatthew N. Dodd }; 3629ade362SMatthew N. Dodd 37faf66437SBaptiste Daroussin static TAILQ_HEAD(lmc_list, lmc) lmc_head = TAILQ_HEAD_INITIALIZER(lmc_head); 38faf66437SBaptiste Daroussin struct lmc { 39faf66437SBaptiste Daroussin char *path; 406b61e3e4SEdward Tomasz Napierala dev_t dev; 416b61e3e4SEdward Tomasz Napierala ino_t ino; 42faf66437SBaptiste Daroussin TAILQ_ENTRY(lmc) next; 43faf66437SBaptiste Daroussin }; 44faf66437SBaptiste Daroussin 455b08cb04SMatthew N. Dodd static int lm_count; 465b08cb04SMatthew N. Dodd 4768f1db20SKonstantin Belousov static void lmc_parse(char *, size_t); 48903e0ffdSAlex Richardson static void lmc_parse_file(const char *); 49903e0ffdSAlex Richardson static void lmc_parse_dir(const char *); 501340fc10SMatthew N. Dodd static void lm_add(const char *, const char *, const char *); 5129ade362SMatthew N. Dodd static void lm_free(struct lm_list *); 5229ade362SMatthew N. Dodd static char *lml_find(struct lm_list *, const char *); 5329ade362SMatthew N. Dodd static struct lm_list *lmp_find(const char *); 5429ade362SMatthew N. Dodd static struct lm_list *lmp_init(char *); 55966efcc7SMatthew N. Dodd static const char *quickbasename(const char *); 5629ade362SMatthew N. Dodd 57623b6bd2SMatthew N. Dodd #define iseol(c) (((c) == '#') || ((c) == '\0') || \ 58623b6bd2SMatthew N. Dodd ((c) == '\n') || ((c) == '\r')) 59623b6bd2SMatthew N. Dodd 60b36070f5SKonstantin Belousov /* 61b36070f5SKonstantin Belousov * Do not use ctype.h macros, which rely on working TLS. It is 62b36070f5SKonstantin Belousov * too early to have thread-local variables functional. 63b36070f5SKonstantin Belousov */ 6444976acaSSergey Kandaurov #define rtld_isspace(c) ((c) == ' ' || (c) == '\t') 65b36070f5SKonstantin Belousov 664402996dSMatthew N. Dodd int 675b08cb04SMatthew N. Dodd lm_init(char *libmap_override) 6829ade362SMatthew N. Dodd { 69faf66437SBaptiste Daroussin char *p; 7029ade362SMatthew N. Dodd 7168f1db20SKonstantin Belousov dbg("lm_init(\"%s\")", libmap_override); 7229ade362SMatthew N. Dodd TAILQ_INIT(&lmp_head); 7329ade362SMatthew N. Dodd 743deca56fSWarner Losh lmc_parse_file(ld_path_libmap_conf); 755b08cb04SMatthew N. Dodd 765b08cb04SMatthew N. Dodd if (libmap_override) { 7768f1db20SKonstantin Belousov /* 78490c68deSKonstantin Belousov * Do some character replacement to make $LDLIBMAP look 7968f1db20SKonstantin Belousov * like a text file, then parse it. 8068f1db20SKonstantin Belousov */ 815b08cb04SMatthew N. Dodd libmap_override = xstrdup(libmap_override); 825b08cb04SMatthew N. Dodd for (p = libmap_override; *p; p++) { 835b08cb04SMatthew N. Dodd switch (*p) { 845b08cb04SMatthew N. Dodd case '=': 8568f1db20SKonstantin Belousov *p = ' '; 8668f1db20SKonstantin Belousov break; 875b08cb04SMatthew N. Dodd case ',': 8868f1db20SKonstantin Belousov *p = '\n'; 8968f1db20SKonstantin Belousov break; 905b08cb04SMatthew N. Dodd } 915b08cb04SMatthew N. Dodd } 92490c68deSKonstantin Belousov lmc_parse(libmap_override, p - libmap_override); 93490c68deSKonstantin Belousov free(libmap_override); 945b08cb04SMatthew N. Dodd } 955b08cb04SMatthew N. Dodd 965b08cb04SMatthew N. Dodd return (lm_count == 0); 975b08cb04SMatthew N. Dodd } 985b08cb04SMatthew N. Dodd 995b08cb04SMatthew N. Dodd static void 100903e0ffdSAlex Richardson lmc_parse_file(const char *path) 101faf66437SBaptiste Daroussin { 102faf66437SBaptiste Daroussin struct lmc *p; 103c1a0a86eSKonstantin Belousov char *lm_map; 104faf66437SBaptiste Daroussin struct stat st; 105a8b31c14SEdward Tomasz Napierala ssize_t retval; 106faf66437SBaptiste Daroussin int fd; 107faf66437SBaptiste Daroussin 108faf66437SBaptiste Daroussin TAILQ_FOREACH(p, &lmc_head, next) { 1096b61e3e4SEdward Tomasz Napierala if (strcmp(p->path, path) == 0) 110faf66437SBaptiste Daroussin return; 111faf66437SBaptiste Daroussin } 112faf66437SBaptiste Daroussin 1136b61e3e4SEdward Tomasz Napierala fd = open(path, O_RDONLY | O_CLOEXEC); 114faf66437SBaptiste Daroussin if (fd == -1) { 1156b61e3e4SEdward Tomasz Napierala dbg("lm_parse_file: open(\"%s\") failed, %s", path, 116faf66437SBaptiste Daroussin rtld_strerror(errno)); 117faf66437SBaptiste Daroussin return; 118faf66437SBaptiste Daroussin } 119faf66437SBaptiste Daroussin if (fstat(fd, &st) == -1) { 120faf66437SBaptiste Daroussin close(fd); 1216b61e3e4SEdward Tomasz Napierala dbg("lm_parse_file: fstat(\"%s\") failed, %s", path, 122faf66437SBaptiste Daroussin rtld_strerror(errno)); 123faf66437SBaptiste Daroussin return; 124faf66437SBaptiste Daroussin } 1256b61e3e4SEdward Tomasz Napierala 1266b61e3e4SEdward Tomasz Napierala TAILQ_FOREACH(p, &lmc_head, next) { 1276b61e3e4SEdward Tomasz Napierala if (p->dev == st.st_dev && p->ino == st.st_ino) { 1286b61e3e4SEdward Tomasz Napierala close(fd); 1296b61e3e4SEdward Tomasz Napierala return; 1306b61e3e4SEdward Tomasz Napierala } 1316b61e3e4SEdward Tomasz Napierala } 1326b61e3e4SEdward Tomasz Napierala 133a8b31c14SEdward Tomasz Napierala lm_map = xmalloc(st.st_size); 134a8b31c14SEdward Tomasz Napierala retval = read(fd, lm_map, st.st_size); 135a8b31c14SEdward Tomasz Napierala if (retval != st.st_size) { 136faf66437SBaptiste Daroussin close(fd); 137152036a0SEdward Tomasz Napierala free(lm_map); 138a8b31c14SEdward Tomasz Napierala dbg("lm_parse_file: read(\"%s\") failed, %s", path, 139faf66437SBaptiste Daroussin rtld_strerror(errno)); 140faf66437SBaptiste Daroussin return; 141faf66437SBaptiste Daroussin } 142faf66437SBaptiste Daroussin close(fd); 143faf66437SBaptiste Daroussin p = xmalloc(sizeof(struct lmc)); 1446b61e3e4SEdward Tomasz Napierala p->path = xstrdup(path); 1456b61e3e4SEdward Tomasz Napierala p->dev = st.st_dev; 1466b61e3e4SEdward Tomasz Napierala p->ino = st.st_ino; 147faf66437SBaptiste Daroussin TAILQ_INSERT_HEAD(&lmc_head, p, next); 148faf66437SBaptiste Daroussin lmc_parse(lm_map, st.st_size); 149a8b31c14SEdward Tomasz Napierala free(lm_map); 150faf66437SBaptiste Daroussin } 151faf66437SBaptiste Daroussin 152faf66437SBaptiste Daroussin static void 153903e0ffdSAlex Richardson lmc_parse_dir(const char *idir) 154faf66437SBaptiste Daroussin { 155faf66437SBaptiste Daroussin DIR *d; 156faf66437SBaptiste Daroussin struct dirent *dp; 157faf66437SBaptiste Daroussin struct lmc *p; 158faf66437SBaptiste Daroussin char conffile[MAXPATHLEN]; 159faf66437SBaptiste Daroussin char *ext; 160faf66437SBaptiste Daroussin 161faf66437SBaptiste Daroussin TAILQ_FOREACH(p, &lmc_head, next) { 1626b61e3e4SEdward Tomasz Napierala if (strcmp(p->path, idir) == 0) 163faf66437SBaptiste Daroussin return; 164faf66437SBaptiste Daroussin } 165faf66437SBaptiste Daroussin d = opendir(idir); 1666b61e3e4SEdward Tomasz Napierala if (d == NULL) 167faf66437SBaptiste Daroussin return; 168faf66437SBaptiste Daroussin 169faf66437SBaptiste Daroussin p = xmalloc(sizeof(struct lmc)); 1706b61e3e4SEdward Tomasz Napierala p->path = xstrdup(idir); 1716b61e3e4SEdward Tomasz Napierala p->dev = NODEV; 1726b61e3e4SEdward Tomasz Napierala p->ino = 0; 173faf66437SBaptiste Daroussin TAILQ_INSERT_HEAD(&lmc_head, p, next); 174faf66437SBaptiste Daroussin 175faf66437SBaptiste Daroussin while ((dp = readdir(d)) != NULL) { 176faf66437SBaptiste Daroussin if (dp->d_ino == 0) 177faf66437SBaptiste Daroussin continue; 178faf66437SBaptiste Daroussin if (dp->d_type != DT_REG) 179faf66437SBaptiste Daroussin continue; 180faf66437SBaptiste Daroussin ext = strrchr(dp->d_name, '.'); 181faf66437SBaptiste Daroussin if (ext == NULL) 182faf66437SBaptiste Daroussin continue; 183faf66437SBaptiste Daroussin if (strcmp(ext, ".conf") != 0) 184faf66437SBaptiste Daroussin continue; 185faf66437SBaptiste Daroussin if (strlcpy(conffile, idir, MAXPATHLEN) >= MAXPATHLEN) 186faf66437SBaptiste Daroussin continue; /* too long */ 187faf66437SBaptiste Daroussin if (strlcat(conffile, "/", MAXPATHLEN) >= MAXPATHLEN) 188faf66437SBaptiste Daroussin continue; /* too long */ 189faf66437SBaptiste Daroussin if (strlcat(conffile, dp->d_name, MAXPATHLEN) >= MAXPATHLEN) 190faf66437SBaptiste Daroussin continue; /* too long */ 191faf66437SBaptiste Daroussin lmc_parse_file(conffile); 192faf66437SBaptiste Daroussin } 193faf66437SBaptiste Daroussin closedir(d); 194faf66437SBaptiste Daroussin } 195faf66437SBaptiste Daroussin 196faf66437SBaptiste Daroussin static void 19768f1db20SKonstantin Belousov lmc_parse(char *lm_p, size_t lm_len) 1985b08cb04SMatthew N. Dodd { 19968f1db20SKonstantin Belousov char *cp, *f, *t, *c, *p; 2005b08cb04SMatthew N. Dodd char prog[MAXPATHLEN]; 201faf66437SBaptiste Daroussin /* allow includedir + full length path */ 202faf66437SBaptiste Daroussin char line[MAXPATHLEN + 13]; 20378b64846SAlex Richardson size_t cnt, i; 2045b08cb04SMatthew N. Dodd 20568f1db20SKonstantin Belousov cnt = 0; 20629ade362SMatthew N. Dodd p = NULL; 20768f1db20SKonstantin Belousov while (cnt < lm_len) { 20868f1db20SKonstantin Belousov i = 0; 2090fa46a42SPedro F. Giffuni while (cnt < lm_len && lm_p[cnt] != '\n' && 21068f1db20SKonstantin Belousov i < sizeof(line) - 1) { 21168f1db20SKonstantin Belousov line[i] = lm_p[cnt]; 21268f1db20SKonstantin Belousov cnt++; 21368f1db20SKonstantin Belousov i++; 21468f1db20SKonstantin Belousov } 21568f1db20SKonstantin Belousov line[i] = '\0'; 2160fa46a42SPedro F. Giffuni while (cnt < lm_len && lm_p[cnt] != '\n') 21768f1db20SKonstantin Belousov cnt++; 21868f1db20SKonstantin Belousov /* skip over nl */ 21968f1db20SKonstantin Belousov cnt++; 22068f1db20SKonstantin Belousov 22168f1db20SKonstantin Belousov cp = &line[0]; 2221340fc10SMatthew N. Dodd t = f = c = NULL; 223623b6bd2SMatthew N. Dodd 22429ade362SMatthew N. Dodd /* Skip over leading space */ 225c1a0a86eSKonstantin Belousov while (rtld_isspace(*cp)) 226c1a0a86eSKonstantin Belousov cp++; 227623b6bd2SMatthew N. Dodd 22829ade362SMatthew N. Dodd /* Found a comment or EOL */ 229c1a0a86eSKonstantin Belousov if (iseol(*cp)) 230c1a0a86eSKonstantin Belousov 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 */ 237c1a0a86eSKonstantin Belousov while (rtld_isspace(*cp)) 238c1a0a86eSKonstantin Belousov cp++; 239623b6bd2SMatthew N. Dodd 24029ade362SMatthew N. Dodd /* Found comment, EOL or end of selector */ 241486089f0SAlexander Kabaev if (iseol(*cp) || *cp == ']') 242486089f0SAlexander Kabaev continue; 243623b6bd2SMatthew N. Dodd 2441340fc10SMatthew N. Dodd c = cp++; 24529ade362SMatthew N. Dodd /* Skip to end of word */ 24644976acaSSergey Kandaurov while (!rtld_isspace(*cp) && !iseol(*cp) && *cp != ']') 247486089f0SAlexander Kabaev cp++; 248623b6bd2SMatthew N. Dodd 249623b6bd2SMatthew N. Dodd /* Skip and zero out trailing space */ 250c1a0a86eSKonstantin Belousov while (rtld_isspace(*cp)) 251c1a0a86eSKonstantin Belousov *cp++ = '\0'; 252623b6bd2SMatthew N. Dodd 253623b6bd2SMatthew N. Dodd /* Check if there is a closing brace */ 254c1a0a86eSKonstantin Belousov if (*cp != ']') 255c1a0a86eSKonstantin Belousov continue; 256623b6bd2SMatthew N. Dodd 257623b6bd2SMatthew N. Dodd /* Terminate string if there was no trailing space */ 25829ade362SMatthew N. Dodd *cp++ = '\0'; 259623b6bd2SMatthew N. Dodd 260623b6bd2SMatthew N. Dodd /* 261623b6bd2SMatthew N. Dodd * There should be nothing except whitespace or comment 2626d5d786fSAlexander Kabaev from this point to the end of the line. 263623b6bd2SMatthew N. Dodd */ 264c1a0a86eSKonstantin Belousov while (rtld_isspace(*cp)) 265c1a0a86eSKonstantin Belousov cp++; 266c1a0a86eSKonstantin Belousov if (!iseol(*cp)) 267c1a0a86eSKonstantin Belousov continue; 268623b6bd2SMatthew N. Dodd 269faf66437SBaptiste Daroussin if (strlcpy(prog, c, sizeof prog) >= sizeof prog) 270faf66437SBaptiste Daroussin continue; 27129ade362SMatthew N. Dodd p = prog; 272486089f0SAlexander Kabaev continue; 273623b6bd2SMatthew N. Dodd } 274623b6bd2SMatthew N. Dodd 275623b6bd2SMatthew N. Dodd /* Parse the 'from' candidate. */ 276486089f0SAlexander Kabaev f = cp++; 277c1a0a86eSKonstantin Belousov while (!rtld_isspace(*cp) && !iseol(*cp)) 278c1a0a86eSKonstantin Belousov cp++; 27929ade362SMatthew N. Dodd 280623b6bd2SMatthew N. Dodd /* Skip and zero out the trailing whitespace */ 281c1a0a86eSKonstantin Belousov while (rtld_isspace(*cp)) 282c1a0a86eSKonstantin Belousov *cp++ = '\0'; 283623b6bd2SMatthew N. Dodd 284623b6bd2SMatthew N. Dodd /* Found a comment or EOL */ 285c1a0a86eSKonstantin Belousov if (iseol(*cp)) 286c1a0a86eSKonstantin Belousov continue; 287623b6bd2SMatthew N. Dodd 288623b6bd2SMatthew N. Dodd /* Parse 'to' mapping */ 289486089f0SAlexander Kabaev t = cp++; 290c1a0a86eSKonstantin Belousov while (!rtld_isspace(*cp) && !iseol(*cp)) 291c1a0a86eSKonstantin Belousov cp++; 292623b6bd2SMatthew N. Dodd 293486089f0SAlexander Kabaev /* Skip and zero out the trailing whitespace */ 294c1a0a86eSKonstantin Belousov while (rtld_isspace(*cp)) 295c1a0a86eSKonstantin Belousov *cp++ = '\0'; 296486089f0SAlexander Kabaev 297486089f0SAlexander Kabaev /* Should be no extra tokens at this point */ 298c1a0a86eSKonstantin Belousov if (!iseol(*cp)) 299c1a0a86eSKonstantin Belousov continue; 300486089f0SAlexander Kabaev 301486089f0SAlexander Kabaev *cp = '\0'; 302faf66437SBaptiste Daroussin if (strcmp(f, "includedir") == 0) 303faf66437SBaptiste Daroussin lmc_parse_dir(t); 304faf66437SBaptiste Daroussin else if (strcmp(f, "include") == 0) 305faf66437SBaptiste Daroussin lmc_parse_file(t); 306faf66437SBaptiste Daroussin else 3071340fc10SMatthew N. Dodd lm_add(p, f, t); 30829ade362SMatthew N. Dodd } 30929ade362SMatthew N. Dodd } 31029ade362SMatthew N. Dodd 31129ade362SMatthew N. Dodd static void 31229ade362SMatthew N. Dodd lm_free(struct lm_list *lml) 31329ade362SMatthew N. Dodd { 31429ade362SMatthew N. Dodd struct lm *lm; 31529ade362SMatthew N. Dodd 3161aac1ed6SMatthew N. Dodd dbg("%s(%p)", __func__, lml); 3171aac1ed6SMatthew N. Dodd 31829ade362SMatthew N. Dodd while (!TAILQ_EMPTY(lml)) { 31929ade362SMatthew N. Dodd lm = TAILQ_FIRST(lml); 32029ade362SMatthew N. Dodd TAILQ_REMOVE(lml, lm, lm_link); 32129ade362SMatthew N. Dodd free(lm->f); 32229ade362SMatthew N. Dodd free(lm->t); 32329ade362SMatthew N. Dodd free(lm); 32429ade362SMatthew N. Dodd } 32529ade362SMatthew N. Dodd } 32629ade362SMatthew N. Dodd 32729ade362SMatthew N. Dodd void 32829ade362SMatthew N. Dodd lm_fini(void) 32929ade362SMatthew N. Dodd { 33029ade362SMatthew N. Dodd struct lmp *lmp; 331faf66437SBaptiste Daroussin struct lmc *p; 33229ade362SMatthew N. Dodd 3331aac1ed6SMatthew N. Dodd dbg("%s()", __func__); 3341aac1ed6SMatthew N. Dodd 335faf66437SBaptiste Daroussin while (!TAILQ_EMPTY(&lmc_head)) { 336faf66437SBaptiste Daroussin p = TAILQ_FIRST(&lmc_head); 337faf66437SBaptiste Daroussin TAILQ_REMOVE(&lmc_head, p, next); 338faf66437SBaptiste Daroussin free(p->path); 339faf66437SBaptiste Daroussin free(p); 340faf66437SBaptiste Daroussin } 341faf66437SBaptiste Daroussin 34229ade362SMatthew N. Dodd while (!TAILQ_EMPTY(&lmp_head)) { 34329ade362SMatthew N. Dodd lmp = TAILQ_FIRST(&lmp_head); 34429ade362SMatthew N. Dodd TAILQ_REMOVE(&lmp_head, lmp, lmp_link); 34529ade362SMatthew N. Dodd free(lmp->p); 34629ade362SMatthew N. Dodd lm_free(&lmp->lml); 34729ade362SMatthew N. Dodd free(lmp); 34829ade362SMatthew N. Dodd } 34929ade362SMatthew N. Dodd } 35029ade362SMatthew N. Dodd 35129ade362SMatthew N. Dodd static void 3521340fc10SMatthew N. Dodd lm_add(const char *p, const char *f, const char *t) 35329ade362SMatthew N. Dodd { 35429ade362SMatthew N. Dodd struct lm_list *lml; 35529ade362SMatthew N. Dodd struct lm *lm; 3566c306765SKonstantin Belousov const char *t1; 35729ade362SMatthew N. Dodd 35829ade362SMatthew N. Dodd if (p == NULL) 35929ade362SMatthew N. Dodd p = "$DEFAULT$"; 36029ade362SMatthew N. Dodd 3611aac1ed6SMatthew N. Dodd dbg("%s(\"%s\", \"%s\", \"%s\")", __func__, p, f, t); 3621aac1ed6SMatthew N. Dodd 36329ade362SMatthew N. Dodd if ((lml = lmp_find(p)) == NULL) 3643467e8b8SMatthew N. Dodd lml = lmp_init(xstrdup(p)); 36529ade362SMatthew N. Dodd 3666c306765SKonstantin Belousov t1 = lml_find(lml, f); 3676c306765SKonstantin Belousov if (t1 == NULL || strcmp(t1, t) != 0) { 3683467e8b8SMatthew N. Dodd lm = xmalloc(sizeof(struct lm)); 3691340fc10SMatthew N. Dodd lm->f = xstrdup(f); 3701340fc10SMatthew N. Dodd lm->t = xstrdup(t); 37129ade362SMatthew N. Dodd TAILQ_INSERT_HEAD(lml, lm, lm_link); 3725b08cb04SMatthew N. Dodd lm_count++; 37329ade362SMatthew N. Dodd } 3746c306765SKonstantin Belousov } 37529ade362SMatthew N. Dodd 37629ade362SMatthew N. Dodd char * 37729ade362SMatthew N. Dodd lm_find(const char *p, const char *f) 37829ade362SMatthew N. Dodd { 37929ade362SMatthew N. Dodd struct lm_list *lml; 38029ade362SMatthew N. Dodd char *t; 38129ade362SMatthew N. Dodd 3821aac1ed6SMatthew N. Dodd dbg("%s(\"%s\", \"%s\")", __func__, p, f); 3831aac1ed6SMatthew N. Dodd 38429ade362SMatthew N. Dodd if (p != NULL && (lml = lmp_find(p)) != NULL) { 38529ade362SMatthew N. Dodd t = lml_find(lml, f); 3863467e8b8SMatthew N. Dodd if (t != NULL) { 3873467e8b8SMatthew N. Dodd /* 3883467e8b8SMatthew N. Dodd * Add a global mapping if we have 3893467e8b8SMatthew N. Dodd * a successful constrained match. 3903467e8b8SMatthew N. Dodd */ 3911340fc10SMatthew N. Dodd lm_add(NULL, f, t); 39229ade362SMatthew N. Dodd return (t); 39329ade362SMatthew N. Dodd } 3943467e8b8SMatthew N. Dodd } 39529ade362SMatthew N. Dodd lml = lmp_find("$DEFAULT$"); 39629ade362SMatthew N. Dodd if (lml != NULL) 39729ade362SMatthew N. Dodd return (lml_find(lml, f)); 39829ade362SMatthew N. Dodd return (NULL); 39929ade362SMatthew N. Dodd } 40029ade362SMatthew N. Dodd 401c1a0a86eSKonstantin Belousov /* 402c1a0a86eSKonstantin Belousov * Given a libmap translation list and a library name, return the 403c1a0a86eSKonstantin Belousov * replacement library, or NULL. 404c1a0a86eSKonstantin Belousov */ 405c905e45dSPeter Wemm char * 40678b64846SAlex Richardson lm_findn(const char *p, const char *f, const size_t n) 407c905e45dSPeter Wemm { 408c905e45dSPeter Wemm char pathbuf[64], *s, *t; 409c905e45dSPeter Wemm 4108c6a035eSPeter Wemm if (n < sizeof(pathbuf) - 1) 411c905e45dSPeter Wemm s = pathbuf; 4128c6a035eSPeter Wemm else 413c905e45dSPeter Wemm s = xmalloc(n + 1); 4148c6a035eSPeter Wemm memcpy(s, f, n); 4158c6a035eSPeter Wemm s[n] = '\0'; 416c905e45dSPeter Wemm t = lm_find(p, s); 417c905e45dSPeter Wemm if (s != pathbuf) 418c905e45dSPeter Wemm free(s); 419c905e45dSPeter Wemm return (t); 420c905e45dSPeter Wemm } 421c905e45dSPeter Wemm 42229ade362SMatthew N. Dodd static char * 42329ade362SMatthew N. Dodd lml_find(struct lm_list *lmh, const char *f) 42429ade362SMatthew N. Dodd { 42529ade362SMatthew N. Dodd struct lm *lm; 42629ade362SMatthew N. Dodd 4271aac1ed6SMatthew N. Dodd dbg("%s(%p, \"%s\")", __func__, lmh, f); 4281aac1ed6SMatthew N. Dodd 429c1a0a86eSKonstantin Belousov TAILQ_FOREACH(lm, lmh, lm_link) { 430c905e45dSPeter Wemm if (strcmp(f, lm->f) == 0) 43129ade362SMatthew N. Dodd return (lm->t); 432c1a0a86eSKonstantin Belousov } 433d33da23fSMatthew N. Dodd return (NULL); 43429ade362SMatthew N. Dodd } 43529ade362SMatthew N. Dodd 436c1a0a86eSKonstantin Belousov /* 437c1a0a86eSKonstantin Belousov * Given an executable name, return a pointer to the translation list or 438c1a0a86eSKonstantin Belousov * NULL if no matches. 439c1a0a86eSKonstantin Belousov */ 44029ade362SMatthew N. Dodd static struct lm_list * 44129ade362SMatthew N. Dodd lmp_find(const char *n) 44229ade362SMatthew N. Dodd { 44329ade362SMatthew N. Dodd struct lmp *lmp; 44429ade362SMatthew N. Dodd 4451aac1ed6SMatthew N. Dodd dbg("%s(\"%s\")", __func__, n); 4461aac1ed6SMatthew N. Dodd 447c1a0a86eSKonstantin Belousov TAILQ_FOREACH(lmp, &lmp_head, lmp_link) { 448966efcc7SMatthew N. Dodd if ((lmp->type == T_EXACT && strcmp(n, lmp->p) == 0) || 449c1a0a86eSKonstantin Belousov (lmp->type == T_DIRECTORY && strncmp(n, lmp->p, 450c1a0a86eSKonstantin Belousov strlen(lmp->p)) == 0) || 451c1a0a86eSKonstantin Belousov (lmp->type == T_BASENAME && strcmp(quickbasename(n), 452c1a0a86eSKonstantin Belousov lmp->p) == 0)) 45329ade362SMatthew N. Dodd return (&lmp->lml); 454c1a0a86eSKonstantin Belousov } 45529ade362SMatthew N. Dodd return (NULL); 45629ade362SMatthew N. Dodd } 45729ade362SMatthew N. Dodd 45829ade362SMatthew N. Dodd static struct lm_list * 45929ade362SMatthew N. Dodd lmp_init(char *n) 46029ade362SMatthew N. Dodd { 46129ade362SMatthew N. Dodd struct lmp *lmp; 46229ade362SMatthew N. Dodd 4631aac1ed6SMatthew N. Dodd dbg("%s(\"%s\")", __func__, n); 4641aac1ed6SMatthew N. Dodd 4653467e8b8SMatthew N. Dodd lmp = xmalloc(sizeof(struct lmp)); 46629ade362SMatthew N. Dodd lmp->p = n; 467966efcc7SMatthew N. Dodd if (n[strlen(n) - 1] == '/') 468966efcc7SMatthew N. Dodd lmp->type = T_DIRECTORY; 469966efcc7SMatthew N. Dodd else if (strchr(n,'/') == NULL) 470966efcc7SMatthew N. Dodd lmp->type = T_BASENAME; 471966efcc7SMatthew N. Dodd else 472966efcc7SMatthew N. Dodd lmp->type = T_EXACT; 47329ade362SMatthew N. Dodd TAILQ_INIT(&lmp->lml); 47429ade362SMatthew N. Dodd TAILQ_INSERT_HEAD(&lmp_head, lmp, lmp_link); 47529ade362SMatthew N. Dodd 47629ade362SMatthew N. Dodd return (&lmp->lml); 47729ade362SMatthew N. Dodd } 478966efcc7SMatthew N. Dodd 479c1a0a86eSKonstantin Belousov /* 480c1a0a86eSKonstantin Belousov * libc basename is overkill. Return a pointer to the character after 481c1a0a86eSKonstantin Belousov * the last /, or the original string if there are no slashes. 482c1a0a86eSKonstantin Belousov */ 483966efcc7SMatthew N. Dodd static const char * 484966efcc7SMatthew N. Dodd quickbasename(const char *path) 485966efcc7SMatthew N. Dodd { 486c1a0a86eSKonstantin Belousov const char *p; 487c1a0a86eSKonstantin Belousov 488c1a0a86eSKonstantin Belousov for (p = path; *path != '\0'; path++) { 489966efcc7SMatthew N. Dodd if (*path == '/') 490966efcc7SMatthew N. Dodd p = path + 1; 491966efcc7SMatthew N. Dodd } 492d33da23fSMatthew N. Dodd return (p); 493966efcc7SMatthew N. Dodd } 494