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" 19*33dba3bbSKonstantin Belousov #include "rtld_paths.h" 20b54a59f3SAlex 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 67aa68b3bbSKonstantin Belousov lm_init(const char *libmap_override) 6829ade362SMatthew N. Dodd { 69aa68b3bbSKonstantin Belousov char *l, *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 76aa68b3bbSKonstantin Belousov if (libmap_override != NULL) { 7768f1db20SKonstantin Belousov /* 78490c68deSKonstantin Belousov * Do some character replacement to make $LDLIBMAP look 7968f1db20SKonstantin Belousov * like a text file, then parse it. 8068f1db20SKonstantin Belousov */ 81aa68b3bbSKonstantin Belousov l = xstrdup(libmap_override); 82aa68b3bbSKonstantin Belousov for (p = l; *p != 0; 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 } 92aa68b3bbSKonstantin Belousov lmc_parse(l, p - l); 93aa68b3bbSKonstantin Belousov free(l); 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; 106741d7812SKonstantin Belousov int fd, saved_errno; 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) { 1206b61e3e4SEdward Tomasz Napierala dbg("lm_parse_file: fstat(\"%s\") failed, %s", path, 121faf66437SBaptiste Daroussin rtld_strerror(errno)); 122741d7812SKonstantin Belousov close(fd); 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); 135741d7812SKonstantin Belousov saved_errno = errno; 136faf66437SBaptiste Daroussin close(fd); 137741d7812SKonstantin Belousov if (retval != st.st_size) { 138741d7812SKonstantin Belousov if (retval == -1) { 139a8b31c14SEdward Tomasz Napierala dbg("lm_parse_file: read(\"%s\") failed, %s", path, 140741d7812SKonstantin Belousov rtld_strerror(saved_errno)); 141741d7812SKonstantin Belousov } else { 142741d7812SKonstantin Belousov dbg("lm_parse_file: short read(\"%s\"), %zd vs %jd", 143741d7812SKonstantin Belousov path, retval, (uintmax_t)st.st_size); 144741d7812SKonstantin Belousov } 145741d7812SKonstantin Belousov free(lm_map); 146faf66437SBaptiste Daroussin return; 147faf66437SBaptiste Daroussin } 148faf66437SBaptiste Daroussin p = xmalloc(sizeof(struct lmc)); 1496b61e3e4SEdward Tomasz Napierala p->path = xstrdup(path); 1506b61e3e4SEdward Tomasz Napierala p->dev = st.st_dev; 1516b61e3e4SEdward Tomasz Napierala p->ino = st.st_ino; 152faf66437SBaptiste Daroussin TAILQ_INSERT_HEAD(&lmc_head, p, next); 153faf66437SBaptiste Daroussin lmc_parse(lm_map, st.st_size); 154a8b31c14SEdward Tomasz Napierala free(lm_map); 155faf66437SBaptiste Daroussin } 156faf66437SBaptiste Daroussin 157faf66437SBaptiste Daroussin static void 158903e0ffdSAlex Richardson lmc_parse_dir(const char *idir) 159faf66437SBaptiste Daroussin { 160faf66437SBaptiste Daroussin DIR *d; 161faf66437SBaptiste Daroussin struct dirent *dp; 162faf66437SBaptiste Daroussin struct lmc *p; 163faf66437SBaptiste Daroussin char conffile[MAXPATHLEN]; 164faf66437SBaptiste Daroussin char *ext; 165faf66437SBaptiste Daroussin 166faf66437SBaptiste Daroussin TAILQ_FOREACH(p, &lmc_head, next) { 1676b61e3e4SEdward Tomasz Napierala if (strcmp(p->path, idir) == 0) 168faf66437SBaptiste Daroussin return; 169faf66437SBaptiste Daroussin } 170faf66437SBaptiste Daroussin d = opendir(idir); 1716b61e3e4SEdward Tomasz Napierala if (d == NULL) 172faf66437SBaptiste Daroussin return; 173faf66437SBaptiste Daroussin 174faf66437SBaptiste Daroussin p = xmalloc(sizeof(struct lmc)); 1756b61e3e4SEdward Tomasz Napierala p->path = xstrdup(idir); 1766b61e3e4SEdward Tomasz Napierala p->dev = NODEV; 1776b61e3e4SEdward Tomasz Napierala p->ino = 0; 178faf66437SBaptiste Daroussin TAILQ_INSERT_HEAD(&lmc_head, p, next); 179faf66437SBaptiste Daroussin 180faf66437SBaptiste Daroussin while ((dp = readdir(d)) != NULL) { 181faf66437SBaptiste Daroussin if (dp->d_ino == 0) 182faf66437SBaptiste Daroussin continue; 183faf66437SBaptiste Daroussin if (dp->d_type != DT_REG) 184faf66437SBaptiste Daroussin continue; 185faf66437SBaptiste Daroussin ext = strrchr(dp->d_name, '.'); 186faf66437SBaptiste Daroussin if (ext == NULL) 187faf66437SBaptiste Daroussin continue; 188faf66437SBaptiste Daroussin if (strcmp(ext, ".conf") != 0) 189faf66437SBaptiste Daroussin continue; 190faf66437SBaptiste Daroussin if (strlcpy(conffile, idir, MAXPATHLEN) >= MAXPATHLEN) 191faf66437SBaptiste Daroussin continue; /* too long */ 192faf66437SBaptiste Daroussin if (strlcat(conffile, "/", MAXPATHLEN) >= MAXPATHLEN) 193faf66437SBaptiste Daroussin continue; /* too long */ 194faf66437SBaptiste Daroussin if (strlcat(conffile, dp->d_name, MAXPATHLEN) >= MAXPATHLEN) 195faf66437SBaptiste Daroussin continue; /* too long */ 196faf66437SBaptiste Daroussin lmc_parse_file(conffile); 197faf66437SBaptiste Daroussin } 198faf66437SBaptiste Daroussin closedir(d); 199faf66437SBaptiste Daroussin } 200faf66437SBaptiste Daroussin 201faf66437SBaptiste Daroussin static void 20268f1db20SKonstantin Belousov lmc_parse(char *lm_p, size_t lm_len) 2035b08cb04SMatthew N. Dodd { 20468f1db20SKonstantin Belousov char *cp, *f, *t, *c, *p; 2055b08cb04SMatthew N. Dodd char prog[MAXPATHLEN]; 206faf66437SBaptiste Daroussin /* allow includedir + full length path */ 207faf66437SBaptiste Daroussin char line[MAXPATHLEN + 13]; 20878b64846SAlex Richardson size_t cnt, i; 2095b08cb04SMatthew N. Dodd 21068f1db20SKonstantin Belousov cnt = 0; 21129ade362SMatthew N. Dodd p = NULL; 21268f1db20SKonstantin Belousov while (cnt < lm_len) { 21368f1db20SKonstantin Belousov i = 0; 2140fa46a42SPedro F. Giffuni while (cnt < lm_len && lm_p[cnt] != '\n' && 21568f1db20SKonstantin Belousov i < sizeof(line) - 1) { 21668f1db20SKonstantin Belousov line[i] = lm_p[cnt]; 21768f1db20SKonstantin Belousov cnt++; 21868f1db20SKonstantin Belousov i++; 21968f1db20SKonstantin Belousov } 22068f1db20SKonstantin Belousov line[i] = '\0'; 2210fa46a42SPedro F. Giffuni while (cnt < lm_len && lm_p[cnt] != '\n') 22268f1db20SKonstantin Belousov cnt++; 22368f1db20SKonstantin Belousov /* skip over nl */ 22468f1db20SKonstantin Belousov cnt++; 22568f1db20SKonstantin Belousov 22668f1db20SKonstantin Belousov cp = &line[0]; 2271340fc10SMatthew N. Dodd t = f = c = NULL; 228623b6bd2SMatthew N. Dodd 22929ade362SMatthew N. Dodd /* Skip over leading space */ 230c1a0a86eSKonstantin Belousov while (rtld_isspace(*cp)) 231c1a0a86eSKonstantin Belousov cp++; 232623b6bd2SMatthew N. Dodd 23329ade362SMatthew N. Dodd /* Found a comment or EOL */ 234c1a0a86eSKonstantin Belousov if (iseol(*cp)) 235c1a0a86eSKonstantin Belousov continue; 236623b6bd2SMatthew N. Dodd 237623b6bd2SMatthew N. Dodd /* Found a constraint selector */ 23829ade362SMatthew N. Dodd if (*cp == '[') { 23929ade362SMatthew N. Dodd cp++; 240623b6bd2SMatthew N. Dodd 24129ade362SMatthew N. Dodd /* Skip leading space */ 242c1a0a86eSKonstantin Belousov while (rtld_isspace(*cp)) 243c1a0a86eSKonstantin Belousov cp++; 244623b6bd2SMatthew N. Dodd 24529ade362SMatthew N. Dodd /* Found comment, EOL or end of selector */ 246486089f0SAlexander Kabaev if (iseol(*cp) || *cp == ']') 247486089f0SAlexander Kabaev continue; 248623b6bd2SMatthew N. Dodd 2491340fc10SMatthew N. Dodd c = cp++; 25029ade362SMatthew N. Dodd /* Skip to end of word */ 25144976acaSSergey Kandaurov while (!rtld_isspace(*cp) && !iseol(*cp) && *cp != ']') 252486089f0SAlexander Kabaev cp++; 253623b6bd2SMatthew N. Dodd 254623b6bd2SMatthew N. Dodd /* Skip and zero out trailing space */ 255c1a0a86eSKonstantin Belousov while (rtld_isspace(*cp)) 256c1a0a86eSKonstantin Belousov *cp++ = '\0'; 257623b6bd2SMatthew N. Dodd 258623b6bd2SMatthew N. Dodd /* Check if there is a closing brace */ 259c1a0a86eSKonstantin Belousov if (*cp != ']') 260c1a0a86eSKonstantin Belousov continue; 261623b6bd2SMatthew N. Dodd 262623b6bd2SMatthew N. Dodd /* Terminate string if there was no trailing space */ 26329ade362SMatthew N. Dodd *cp++ = '\0'; 264623b6bd2SMatthew N. Dodd 265623b6bd2SMatthew N. Dodd /* 266623b6bd2SMatthew N. Dodd * There should be nothing except whitespace or comment 2676d5d786fSAlexander Kabaev from this point to the end of the line. 268623b6bd2SMatthew N. Dodd */ 269c1a0a86eSKonstantin Belousov while (rtld_isspace(*cp)) 270c1a0a86eSKonstantin Belousov cp++; 271c1a0a86eSKonstantin Belousov if (!iseol(*cp)) 272c1a0a86eSKonstantin Belousov continue; 273623b6bd2SMatthew N. Dodd 274faf66437SBaptiste Daroussin if (strlcpy(prog, c, sizeof prog) >= sizeof prog) 275faf66437SBaptiste Daroussin continue; 27629ade362SMatthew N. Dodd p = prog; 277486089f0SAlexander Kabaev continue; 278623b6bd2SMatthew N. Dodd } 279623b6bd2SMatthew N. Dodd 280623b6bd2SMatthew N. Dodd /* Parse the 'from' candidate. */ 281486089f0SAlexander Kabaev f = cp++; 282c1a0a86eSKonstantin Belousov while (!rtld_isspace(*cp) && !iseol(*cp)) 283c1a0a86eSKonstantin Belousov cp++; 28429ade362SMatthew N. Dodd 285623b6bd2SMatthew N. Dodd /* Skip and zero out the trailing whitespace */ 286c1a0a86eSKonstantin Belousov while (rtld_isspace(*cp)) 287c1a0a86eSKonstantin Belousov *cp++ = '\0'; 288623b6bd2SMatthew N. Dodd 289623b6bd2SMatthew N. Dodd /* Found a comment or EOL */ 290c1a0a86eSKonstantin Belousov if (iseol(*cp)) 291c1a0a86eSKonstantin Belousov continue; 292623b6bd2SMatthew N. Dodd 293623b6bd2SMatthew N. Dodd /* Parse 'to' mapping */ 294486089f0SAlexander Kabaev t = cp++; 295c1a0a86eSKonstantin Belousov while (!rtld_isspace(*cp) && !iseol(*cp)) 296c1a0a86eSKonstantin Belousov cp++; 297623b6bd2SMatthew N. Dodd 298486089f0SAlexander Kabaev /* Skip and zero out the trailing whitespace */ 299c1a0a86eSKonstantin Belousov while (rtld_isspace(*cp)) 300c1a0a86eSKonstantin Belousov *cp++ = '\0'; 301486089f0SAlexander Kabaev 302486089f0SAlexander Kabaev /* Should be no extra tokens at this point */ 303c1a0a86eSKonstantin Belousov if (!iseol(*cp)) 304c1a0a86eSKonstantin Belousov continue; 305486089f0SAlexander Kabaev 306486089f0SAlexander Kabaev *cp = '\0'; 307faf66437SBaptiste Daroussin if (strcmp(f, "includedir") == 0) 308faf66437SBaptiste Daroussin lmc_parse_dir(t); 309faf66437SBaptiste Daroussin else if (strcmp(f, "include") == 0) 310faf66437SBaptiste Daroussin lmc_parse_file(t); 311faf66437SBaptiste Daroussin else 3121340fc10SMatthew N. Dodd lm_add(p, f, t); 31329ade362SMatthew N. Dodd } 31429ade362SMatthew N. Dodd } 31529ade362SMatthew N. Dodd 31629ade362SMatthew N. Dodd static void 31729ade362SMatthew N. Dodd lm_free(struct lm_list *lml) 31829ade362SMatthew N. Dodd { 31929ade362SMatthew N. Dodd struct lm *lm; 32029ade362SMatthew N. Dodd 3211aac1ed6SMatthew N. Dodd dbg("%s(%p)", __func__, lml); 3221aac1ed6SMatthew N. Dodd 32329ade362SMatthew N. Dodd while (!TAILQ_EMPTY(lml)) { 32429ade362SMatthew N. Dodd lm = TAILQ_FIRST(lml); 32529ade362SMatthew N. Dodd TAILQ_REMOVE(lml, lm, lm_link); 32629ade362SMatthew N. Dodd free(lm->f); 32729ade362SMatthew N. Dodd free(lm->t); 32829ade362SMatthew N. Dodd free(lm); 32929ade362SMatthew N. Dodd } 33029ade362SMatthew N. Dodd } 33129ade362SMatthew N. Dodd 33229ade362SMatthew N. Dodd void 33329ade362SMatthew N. Dodd lm_fini(void) 33429ade362SMatthew N. Dodd { 33529ade362SMatthew N. Dodd struct lmp *lmp; 336faf66437SBaptiste Daroussin struct lmc *p; 33729ade362SMatthew N. Dodd 3381aac1ed6SMatthew N. Dodd dbg("%s()", __func__); 3391aac1ed6SMatthew N. Dodd 340faf66437SBaptiste Daroussin while (!TAILQ_EMPTY(&lmc_head)) { 341faf66437SBaptiste Daroussin p = TAILQ_FIRST(&lmc_head); 342faf66437SBaptiste Daroussin TAILQ_REMOVE(&lmc_head, p, next); 343faf66437SBaptiste Daroussin free(p->path); 344faf66437SBaptiste Daroussin free(p); 345faf66437SBaptiste Daroussin } 346faf66437SBaptiste Daroussin 34729ade362SMatthew N. Dodd while (!TAILQ_EMPTY(&lmp_head)) { 34829ade362SMatthew N. Dodd lmp = TAILQ_FIRST(&lmp_head); 34929ade362SMatthew N. Dodd TAILQ_REMOVE(&lmp_head, lmp, lmp_link); 35029ade362SMatthew N. Dodd free(lmp->p); 35129ade362SMatthew N. Dodd lm_free(&lmp->lml); 35229ade362SMatthew N. Dodd free(lmp); 35329ade362SMatthew N. Dodd } 35429ade362SMatthew N. Dodd } 35529ade362SMatthew N. Dodd 35629ade362SMatthew N. Dodd static void 3571340fc10SMatthew N. Dodd lm_add(const char *p, const char *f, const char *t) 35829ade362SMatthew N. Dodd { 35929ade362SMatthew N. Dodd struct lm_list *lml; 36029ade362SMatthew N. Dodd struct lm *lm; 3616c306765SKonstantin Belousov const char *t1; 36229ade362SMatthew N. Dodd 36329ade362SMatthew N. Dodd if (p == NULL) 36429ade362SMatthew N. Dodd p = "$DEFAULT$"; 36529ade362SMatthew N. Dodd 3661aac1ed6SMatthew N. Dodd dbg("%s(\"%s\", \"%s\", \"%s\")", __func__, p, f, t); 3671aac1ed6SMatthew N. Dodd 36829ade362SMatthew N. Dodd if ((lml = lmp_find(p)) == NULL) 3693467e8b8SMatthew N. Dodd lml = lmp_init(xstrdup(p)); 37029ade362SMatthew N. Dodd 3716c306765SKonstantin Belousov t1 = lml_find(lml, f); 3726c306765SKonstantin Belousov if (t1 == NULL || strcmp(t1, t) != 0) { 3733467e8b8SMatthew N. Dodd lm = xmalloc(sizeof(struct lm)); 3741340fc10SMatthew N. Dodd lm->f = xstrdup(f); 3751340fc10SMatthew N. Dodd lm->t = xstrdup(t); 37629ade362SMatthew N. Dodd TAILQ_INSERT_HEAD(lml, lm, lm_link); 3775b08cb04SMatthew N. Dodd lm_count++; 37829ade362SMatthew N. Dodd } 3796c306765SKonstantin Belousov } 38029ade362SMatthew N. Dodd 38129ade362SMatthew N. Dodd char * 38229ade362SMatthew N. Dodd lm_find(const char *p, const char *f) 38329ade362SMatthew N. Dodd { 38429ade362SMatthew N. Dodd struct lm_list *lml; 38529ade362SMatthew N. Dodd char *t; 38629ade362SMatthew N. Dodd 3871aac1ed6SMatthew N. Dodd dbg("%s(\"%s\", \"%s\")", __func__, p, f); 3881aac1ed6SMatthew N. Dodd 38929ade362SMatthew N. Dodd if (p != NULL && (lml = lmp_find(p)) != NULL) { 39029ade362SMatthew N. Dodd t = lml_find(lml, f); 3913467e8b8SMatthew N. Dodd if (t != NULL) { 3923467e8b8SMatthew N. Dodd /* 3933467e8b8SMatthew N. Dodd * Add a global mapping if we have 3943467e8b8SMatthew N. Dodd * a successful constrained match. 3953467e8b8SMatthew N. Dodd */ 3961340fc10SMatthew N. Dodd lm_add(NULL, f, t); 39729ade362SMatthew N. Dodd return (t); 39829ade362SMatthew N. Dodd } 3993467e8b8SMatthew N. Dodd } 40029ade362SMatthew N. Dodd lml = lmp_find("$DEFAULT$"); 40129ade362SMatthew N. Dodd if (lml != NULL) 40229ade362SMatthew N. Dodd return (lml_find(lml, f)); 40329ade362SMatthew N. Dodd return (NULL); 40429ade362SMatthew N. Dodd } 40529ade362SMatthew N. Dodd 406c1a0a86eSKonstantin Belousov /* 407c1a0a86eSKonstantin Belousov * Given a libmap translation list and a library name, return the 408c1a0a86eSKonstantin Belousov * replacement library, or NULL. 409c1a0a86eSKonstantin Belousov */ 410c905e45dSPeter Wemm char * 41178b64846SAlex Richardson lm_findn(const char *p, const char *f, const size_t n) 412c905e45dSPeter Wemm { 413c905e45dSPeter Wemm char pathbuf[64], *s, *t; 414c905e45dSPeter Wemm 4158c6a035eSPeter Wemm if (n < sizeof(pathbuf) - 1) 416c905e45dSPeter Wemm s = pathbuf; 4178c6a035eSPeter Wemm else 418c905e45dSPeter Wemm s = xmalloc(n + 1); 4198c6a035eSPeter Wemm memcpy(s, f, n); 4208c6a035eSPeter Wemm s[n] = '\0'; 421c905e45dSPeter Wemm t = lm_find(p, s); 422c905e45dSPeter Wemm if (s != pathbuf) 423c905e45dSPeter Wemm free(s); 424c905e45dSPeter Wemm return (t); 425c905e45dSPeter Wemm } 426c905e45dSPeter Wemm 42729ade362SMatthew N. Dodd static char * 42829ade362SMatthew N. Dodd lml_find(struct lm_list *lmh, const char *f) 42929ade362SMatthew N. Dodd { 43029ade362SMatthew N. Dodd struct lm *lm; 43129ade362SMatthew N. Dodd 4321aac1ed6SMatthew N. Dodd dbg("%s(%p, \"%s\")", __func__, lmh, f); 4331aac1ed6SMatthew N. Dodd 434c1a0a86eSKonstantin Belousov TAILQ_FOREACH(lm, lmh, lm_link) { 435c905e45dSPeter Wemm if (strcmp(f, lm->f) == 0) 43629ade362SMatthew N. Dodd return (lm->t); 437c1a0a86eSKonstantin Belousov } 438d33da23fSMatthew N. Dodd return (NULL); 43929ade362SMatthew N. Dodd } 44029ade362SMatthew N. Dodd 441c1a0a86eSKonstantin Belousov /* 442c1a0a86eSKonstantin Belousov * Given an executable name, return a pointer to the translation list or 443c1a0a86eSKonstantin Belousov * NULL if no matches. 444c1a0a86eSKonstantin Belousov */ 44529ade362SMatthew N. Dodd static struct lm_list * 44629ade362SMatthew N. Dodd lmp_find(const char *n) 44729ade362SMatthew N. Dodd { 44829ade362SMatthew N. Dodd struct lmp *lmp; 44929ade362SMatthew N. Dodd 4501aac1ed6SMatthew N. Dodd dbg("%s(\"%s\")", __func__, n); 4511aac1ed6SMatthew N. Dodd 452c1a0a86eSKonstantin Belousov TAILQ_FOREACH(lmp, &lmp_head, lmp_link) { 453966efcc7SMatthew N. Dodd if ((lmp->type == T_EXACT && strcmp(n, lmp->p) == 0) || 454c1a0a86eSKonstantin Belousov (lmp->type == T_DIRECTORY && strncmp(n, lmp->p, 455c1a0a86eSKonstantin Belousov strlen(lmp->p)) == 0) || 456c1a0a86eSKonstantin Belousov (lmp->type == T_BASENAME && strcmp(quickbasename(n), 457c1a0a86eSKonstantin Belousov lmp->p) == 0)) 45829ade362SMatthew N. Dodd return (&lmp->lml); 459c1a0a86eSKonstantin Belousov } 46029ade362SMatthew N. Dodd return (NULL); 46129ade362SMatthew N. Dodd } 46229ade362SMatthew N. Dodd 46329ade362SMatthew N. Dodd static struct lm_list * 46429ade362SMatthew N. Dodd lmp_init(char *n) 46529ade362SMatthew N. Dodd { 46629ade362SMatthew N. Dodd struct lmp *lmp; 46729ade362SMatthew N. Dodd 4681aac1ed6SMatthew N. Dodd dbg("%s(\"%s\")", __func__, n); 4691aac1ed6SMatthew N. Dodd 4703467e8b8SMatthew N. Dodd lmp = xmalloc(sizeof(struct lmp)); 47129ade362SMatthew N. Dodd lmp->p = n; 472966efcc7SMatthew N. Dodd if (n[strlen(n) - 1] == '/') 473966efcc7SMatthew N. Dodd lmp->type = T_DIRECTORY; 474966efcc7SMatthew N. Dodd else if (strchr(n,'/') == NULL) 475966efcc7SMatthew N. Dodd lmp->type = T_BASENAME; 476966efcc7SMatthew N. Dodd else 477966efcc7SMatthew N. Dodd lmp->type = T_EXACT; 47829ade362SMatthew N. Dodd TAILQ_INIT(&lmp->lml); 47929ade362SMatthew N. Dodd TAILQ_INSERT_HEAD(&lmp_head, lmp, lmp_link); 48029ade362SMatthew N. Dodd 48129ade362SMatthew N. Dodd return (&lmp->lml); 48229ade362SMatthew N. Dodd } 483966efcc7SMatthew N. Dodd 484c1a0a86eSKonstantin Belousov /* 485c1a0a86eSKonstantin Belousov * libc basename is overkill. Return a pointer to the character after 486c1a0a86eSKonstantin Belousov * the last /, or the original string if there are no slashes. 487c1a0a86eSKonstantin Belousov */ 488966efcc7SMatthew N. Dodd static const char * 489966efcc7SMatthew N. Dodd quickbasename(const char *path) 490966efcc7SMatthew N. Dodd { 491c1a0a86eSKonstantin Belousov const char *p; 492c1a0a86eSKonstantin Belousov 493c1a0a86eSKonstantin Belousov for (p = path; *path != '\0'; path++) { 494966efcc7SMatthew N. Dodd if (*path == '/') 495966efcc7SMatthew N. Dodd p = path + 1; 496966efcc7SMatthew N. Dodd } 497d33da23fSMatthew N. Dodd return (p); 498966efcc7SMatthew N. Dodd } 499