20 #ifndef _PATH_LIBMAP_CONF
21 #define _PATH_LIBMAP_CONF "/etc/libmap.conf"
25 #undef _PATH_LIBMAP_CONF
26 #define _PATH_LIBMAP_CONF "/etc/libmap32.conf"
29 TAILQ_HEAD(lm_list, lm);
33 TAILQ_ENTRY(lm) lm_link;
36 TAILQ_HEAD(lmp_list, lmp) lmp_head = TAILQ_HEAD_INITIALIZER(lmp_head);
39 enum { T_EXACT=0, T_BASENAME, T_DIRECTORY } type;
41 TAILQ_ENTRY(lmp) lmp_link;
44 static TAILQ_HEAD(lmc_list, lmc) lmc_head = TAILQ_HEAD_INITIALIZER(lmc_head);
47 TAILQ_ENTRY(lmc) next;
52 static void lmc_parse(char *, size_t);
53 static void lmc_parse_file(char *);
54 static void lmc_parse_dir(char *);
55 static void lm_add(const char *, const char *, const char *);
56 static void lm_free(struct lm_list *);
57 static char *lml_find(struct lm_list *, const char *);
58 static struct lm_list *lmp_find(const char *);
59 static struct lm_list *lmp_init(char *);
60 static const char *quickbasename(const char *);
62 #define iseol(c) (((c) == '#') || ((c) == '\0') || \
63 ((c) == '\n') || ((c) == '\r'))
66 * Do not use ctype.h macros, which rely on working TLS. It is
67 * too early to have thread-local variables functional.
69 #define rtld_isspace(c) ((c) == ' ' || (c) == '\t')
72 lm_init(char *libmap_override)
76 dbg("lm_init(\"%s\")", libmap_override);
77 TAILQ_INIT(&lmp_head);
79 lmc_parse_file(_PATH_LIBMAP_CONF);
81 if (libmap_override) {
83 * Do some character replacement to make $LDLIBMAP look
84 * like a text file, then parse it.
86 libmap_override = xstrdup(libmap_override);
87 for (p = libmap_override; *p; p++) {
97 lmc_parse(libmap_override, p - libmap_override);
98 free(libmap_override);
101 return (lm_count == 0);
105 lmc_parse_file(char *path)
113 rpath = realpath(path, NULL);
117 TAILQ_FOREACH(p, &lmc_head, next) {
118 if (strcmp(p->path, rpath) == 0) {
124 fd = open(rpath, O_RDONLY | O_CLOEXEC);
126 dbg("lm_parse_file: open(\"%s\") failed, %s", rpath,
127 rtld_strerror(errno));
131 if (fstat(fd, &st) == -1) {
133 dbg("lm_parse_file: fstat(\"%s\") failed, %s", rpath,
134 rtld_strerror(errno));
138 lm_map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
139 if (lm_map == (const char *)MAP_FAILED) {
141 dbg("lm_parse_file: mmap(\"%s\") failed, %s", rpath,
142 rtld_strerror(errno));
147 p = xmalloc(sizeof(struct lmc));
149 TAILQ_INSERT_HEAD(&lmc_head, p, next);
150 lmc_parse(lm_map, st.st_size);
151 munmap(lm_map, st.st_size);
155 lmc_parse_dir(char *idir)
160 char conffile[MAXPATHLEN];
164 rpath = realpath(idir, NULL);
168 TAILQ_FOREACH(p, &lmc_head, next) {
169 if (strcmp(p->path, rpath) == 0) {
180 p = xmalloc(sizeof(struct lmc));
182 TAILQ_INSERT_HEAD(&lmc_head, p, next);
184 while ((dp = readdir(d)) != NULL) {
187 if (dp->d_type != DT_REG)
189 ext = strrchr(dp->d_name, '.');
192 if (strcmp(ext, ".conf") != 0)
194 if (strlcpy(conffile, idir, MAXPATHLEN) >= MAXPATHLEN)
195 continue; /* too long */
196 if (strlcat(conffile, "/", MAXPATHLEN) >= MAXPATHLEN)
197 continue; /* too long */
198 if (strlcat(conffile, dp->d_name, MAXPATHLEN) >= MAXPATHLEN)
199 continue; /* too long */
200 lmc_parse_file(conffile);
206 lmc_parse(char *lm_p, size_t lm_len)
208 char *cp, *f, *t, *c, *p;
209 char prog[MAXPATHLEN];
210 /* allow includedir + full length path */
211 char line[MAXPATHLEN + 13];
217 while (cnt < lm_len) {
219 while (lm_p[cnt] != '\n' && cnt < lm_len &&
220 i < sizeof(line) - 1) {
226 while (lm_p[cnt] != '\n' && cnt < lm_len)
234 /* Skip over leading space */
235 while (rtld_isspace(*cp)) cp++;
237 /* Found a comment or EOL */
238 if (iseol(*cp)) continue;
240 /* Found a constraint selector */
244 /* Skip leading space */
245 while (rtld_isspace(*cp)) cp++;
247 /* Found comment, EOL or end of selector */
248 if (iseol(*cp) || *cp == ']')
252 /* Skip to end of word */
253 while (!rtld_isspace(*cp) && !iseol(*cp) && *cp != ']')
256 /* Skip and zero out trailing space */
257 while (rtld_isspace(*cp)) *cp++ = '\0';
259 /* Check if there is a closing brace */
260 if (*cp != ']') continue;
262 /* Terminate string if there was no trailing space */
266 * There should be nothing except whitespace or comment
267 from this point to the end of the line.
269 while(rtld_isspace(*cp)) cp++;
270 if (!iseol(*cp)) continue;
272 if (strlcpy(prog, c, sizeof prog) >= sizeof prog)
278 /* Parse the 'from' candidate. */
280 while (!rtld_isspace(*cp) && !iseol(*cp)) cp++;
282 /* Skip and zero out the trailing whitespace */
283 while (rtld_isspace(*cp)) *cp++ = '\0';
285 /* Found a comment or EOL */
286 if (iseol(*cp)) continue;
288 /* Parse 'to' mapping */
290 while (!rtld_isspace(*cp) && !iseol(*cp)) cp++;
292 /* Skip and zero out the trailing whitespace */
293 while (rtld_isspace(*cp)) *cp++ = '\0';
295 /* Should be no extra tokens at this point */
296 if (!iseol(*cp)) continue;
299 if (strcmp(f, "includedir") == 0)
301 else if (strcmp(f, "include") == 0)
309 lm_free (struct lm_list *lml)
313 dbg("%s(%p)", __func__, lml);
315 while (!TAILQ_EMPTY(lml)) {
316 lm = TAILQ_FIRST(lml);
317 TAILQ_REMOVE(lml, lm, lm_link);
331 dbg("%s()", __func__);
333 while (!TAILQ_EMPTY(&lmc_head)) {
334 p = TAILQ_FIRST(&lmc_head);
335 TAILQ_REMOVE(&lmc_head, p, next);
340 while (!TAILQ_EMPTY(&lmp_head)) {
341 lmp = TAILQ_FIRST(&lmp_head);
342 TAILQ_REMOVE(&lmp_head, lmp, lmp_link);
351 lm_add (const char *p, const char *f, const char *t)
359 dbg("%s(\"%s\", \"%s\", \"%s\")", __func__, p, f, t);
361 if ((lml = lmp_find(p)) == NULL)
362 lml = lmp_init(xstrdup(p));
364 lm = xmalloc(sizeof(struct lm));
367 TAILQ_INSERT_HEAD(lml, lm, lm_link);
372 lm_find (const char *p, const char *f)
377 dbg("%s(\"%s\", \"%s\")", __func__, p, f);
379 if (p != NULL && (lml = lmp_find(p)) != NULL) {
380 t = lml_find(lml, f);
383 * Add a global mapping if we have
384 * a successful constrained match.
390 lml = lmp_find("$DEFAULT$");
392 return (lml_find(lml, f));
397 /* Given a libmap translation list and a library name, return the
398 replacement library, or NULL */
400 lm_findn (const char *p, const char *f, const int n)
402 char pathbuf[64], *s, *t;
404 if (n < sizeof(pathbuf) - 1)
417 lml_find (struct lm_list *lmh, const char *f)
421 dbg("%s(%p, \"%s\")", __func__, lmh, f);
423 TAILQ_FOREACH(lm, lmh, lm_link)
424 if (strcmp(f, lm->f) == 0)
429 /* Given an executable name, return a pointer to the translation list or
430 NULL if no matches */
431 static struct lm_list *
432 lmp_find (const char *n)
436 dbg("%s(\"%s\")", __func__, n);
438 TAILQ_FOREACH(lmp, &lmp_head, lmp_link)
439 if ((lmp->type == T_EXACT && strcmp(n, lmp->p) == 0) ||
440 (lmp->type == T_DIRECTORY && strncmp(n, lmp->p, strlen(lmp->p)) == 0) ||
441 (lmp->type == T_BASENAME && strcmp(quickbasename(n), lmp->p) == 0))
446 static struct lm_list *
451 dbg("%s(\"%s\")", __func__, n);
453 lmp = xmalloc(sizeof(struct lmp));
455 if (n[strlen(n)-1] == '/')
456 lmp->type = T_DIRECTORY;
457 else if (strchr(n,'/') == NULL)
458 lmp->type = T_BASENAME;
461 TAILQ_INIT(&lmp->lml);
462 TAILQ_INSERT_HEAD(&lmp_head, lmp, lmp_link);
467 /* libc basename is overkill. Return a pointer to the character after the
468 last /, or the original string if there are no slashes. */
470 quickbasename (const char *path)
472 const char *p = path;
473 for (; *path; path++) {