18 #ifndef _PATH_LIBMAP_CONF
19 #define _PATH_LIBMAP_CONF "/etc/libmap.conf"
22 TAILQ_HEAD(lm_list, lm);
26 TAILQ_ENTRY(lm) lm_link;
29 TAILQ_HEAD(lmp_list, lmp) lmp_head = TAILQ_HEAD_INITIALIZER(lmp_head);
32 enum { T_EXACT=0, T_BASENAME, T_DIRECTORY } type;
34 TAILQ_ENTRY(lmp) lmp_link;
39 static void lmc_parse(char *, size_t);
40 static void lm_add(const char *, const char *, const char *);
41 static void lm_free(struct lm_list *);
42 static char *lml_find(struct lm_list *, const char *);
43 static struct lm_list *lmp_find(const char *);
44 static struct lm_list *lmp_init(char *);
45 static const char *quickbasename(const char *);
47 #define iseol(c) (((c) == '#') || ((c) == '\0') || \
48 ((c) == '\n') || ((c) == '\r'))
51 * Do not use ctype.h macros, which rely on working TLS. It is
52 * too early to have thread-local variables functional.
54 #define rtld_isspace(c) ((c) == ' ' || (c) == '\t')
57 lm_init(char *libmap_override)
63 dbg("lm_init(\"%s\")", libmap_override);
64 TAILQ_INIT(&lmp_head);
66 fd = open(_PATH_LIBMAP_CONF, O_RDONLY);
68 dbg("lm_init: open(\"%s\") failed, %s", _PATH_LIBMAP_CONF,
69 rtld_strerror(errno));
72 if (fstat(fd, &st) == -1) {
74 dbg("lm_init: fstat(\"%s\") failed, %s", _PATH_LIBMAP_CONF,
75 rtld_strerror(errno));
78 lm_map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
79 if (lm_map == (const char *)MAP_FAILED) {
81 dbg("lm_init: mmap(\"%s\") failed, %s", _PATH_LIBMAP_CONF,
82 rtld_strerror(errno));
86 lmc_parse(lm_map, st.st_size);
87 munmap(lm_map, st.st_size);
90 if (libmap_override) {
92 * Do some character replacement to make $LIBMAP look
93 * like a text file, then parse it.
95 libmap_override = xstrdup(libmap_override);
96 for (p = libmap_override; *p; p++) {
106 lmc_parse(p, strlen(p));
110 return (lm_count == 0);
114 lmc_parse(char *lm_p, size_t lm_len)
116 char *cp, *f, *t, *c, *p;
117 char prog[MAXPATHLEN];
118 char line[MAXPATHLEN + 2];
124 while (cnt < lm_len) {
126 while (lm_p[cnt] != '\n' && cnt < lm_len &&
127 i < sizeof(line) - 1) {
133 while (lm_p[cnt] != '\n' && cnt < lm_len)
141 /* Skip over leading space */
142 while (rtld_isspace(*cp)) cp++;
144 /* Found a comment or EOL */
145 if (iseol(*cp)) continue;
147 /* Found a constraint selector */
151 /* Skip leading space */
152 while (rtld_isspace(*cp)) cp++;
154 /* Found comment, EOL or end of selector */
155 if (iseol(*cp) || *cp == ']')
159 /* Skip to end of word */
160 while (!rtld_isspace(*cp) && !iseol(*cp) && *cp != ']')
163 /* Skip and zero out trailing space */
164 while (rtld_isspace(*cp)) *cp++ = '\0';
166 /* Check if there is a closing brace */
167 if (*cp != ']') continue;
169 /* Terminate string if there was no trailing space */
173 * There should be nothing except whitespace or comment
174 from this point to the end of the line.
176 while(rtld_isspace(*cp)) cp++;
177 if (!iseol(*cp)) continue;
184 /* Parse the 'from' candidate. */
186 while (!rtld_isspace(*cp) && !iseol(*cp)) cp++;
188 /* Skip and zero out the trailing whitespace */
189 while (rtld_isspace(*cp)) *cp++ = '\0';
191 /* Found a comment or EOL */
192 if (iseol(*cp)) continue;
194 /* Parse 'to' mapping */
196 while (!rtld_isspace(*cp) && !iseol(*cp)) cp++;
198 /* Skip and zero out the trailing whitespace */
199 while (rtld_isspace(*cp)) *cp++ = '\0';
201 /* Should be no extra tokens at this point */
202 if (!iseol(*cp)) continue;
210 lm_free (struct lm_list *lml)
214 dbg("%s(%p)", __func__, lml);
216 while (!TAILQ_EMPTY(lml)) {
217 lm = TAILQ_FIRST(lml);
218 TAILQ_REMOVE(lml, lm, lm_link);
231 dbg("%s()", __func__);
233 while (!TAILQ_EMPTY(&lmp_head)) {
234 lmp = TAILQ_FIRST(&lmp_head);
235 TAILQ_REMOVE(&lmp_head, lmp, lmp_link);
244 lm_add (const char *p, const char *f, const char *t)
252 dbg("%s(\"%s\", \"%s\", \"%s\")", __func__, p, f, t);
254 if ((lml = lmp_find(p)) == NULL)
255 lml = lmp_init(xstrdup(p));
257 lm = xmalloc(sizeof(struct lm));
260 TAILQ_INSERT_HEAD(lml, lm, lm_link);
265 lm_find (const char *p, const char *f)
270 dbg("%s(\"%s\", \"%s\")", __func__, p, f);
272 if (p != NULL && (lml = lmp_find(p)) != NULL) {
273 t = lml_find(lml, f);
276 * Add a global mapping if we have
277 * a successful constrained match.
283 lml = lmp_find("$DEFAULT$");
285 return (lml_find(lml, f));
291 lml_find (struct lm_list *lmh, const char *f)
295 dbg("%s(%p, \"%s\")", __func__, lmh, f);
297 TAILQ_FOREACH(lm, lmh, lm_link)
298 if (strcmp(f, lm->f) == 0)
303 /* Given an executable name, return a pointer to the translation list or
304 NULL if no matches */
305 static struct lm_list *
306 lmp_find (const char *n)
310 dbg("%s(\"%s\")", __func__, n);
312 TAILQ_FOREACH(lmp, &lmp_head, lmp_link)
313 if ((lmp->type == T_EXACT && strcmp(n, lmp->p) == 0) ||
314 (lmp->type == T_DIRECTORY && strncmp(n, lmp->p, strlen(lmp->p)) == 0) ||
315 (lmp->type == T_BASENAME && strcmp(quickbasename(n), lmp->p) == 0))
320 static struct lm_list *
325 dbg("%s(\"%s\")", __func__, n);
327 lmp = xmalloc(sizeof(struct lmp));
329 if (n[strlen(n)-1] == '/')
330 lmp->type = T_DIRECTORY;
331 else if (strchr(n,'/') == NULL)
332 lmp->type = T_BASENAME;
335 TAILQ_INIT(&lmp->lml);
336 TAILQ_INSERT_HEAD(&lmp_head, lmp, lmp_link);
341 /* libc basename is overkill. Return a pointer to the character after the
342 last /, or the original string if there are no slashes. */
344 quickbasename (const char *path)
346 const char *p = path;
347 for (; *path; path++) {