hammer2 - Fix snapshots and multi-label mounts
[dragonfly.git] / libexec / rtld-elf / libmap.c
blob80d9a911d5a9fdcee8d0780abd5eb48efca7e5a1
1 /*
2 * $FreeBSD$
3 */
5 #include <sys/types.h>
6 #include <sys/param.h>
7 #include <sys/fcntl.h>
8 #include <sys/mman.h>
9 #include <sys/queue.h>
10 #include <sys/stat.h>
11 #include <dirent.h>
12 #include <errno.h>
13 #include <stdlib.h>
14 #include <string.h>
16 #include "debug.h"
17 #include "rtld.h"
18 #include "libmap.h"
20 #ifndef _PATH_LIBMAP_CONF
21 #define _PATH_LIBMAP_CONF "/etc/libmap.conf"
22 #endif
24 TAILQ_HEAD(lm_list, lm);
25 struct lm {
26 char *f;
27 char *t;
28 TAILQ_ENTRY(lm) lm_link;
31 TAILQ_HEAD(lmp_list, lmp) lmp_head = TAILQ_HEAD_INITIALIZER(lmp_head);
32 struct lmp {
33 char *p;
34 enum { T_EXACT=0, T_BASENAME, T_DIRECTORY } type;
35 struct lm_list lml;
36 TAILQ_ENTRY(lmp) lmp_link;
39 static TAILQ_HEAD(lmc_list, lmc) lmc_head = TAILQ_HEAD_INITIALIZER(lmc_head);
40 struct lmc {
41 char *path;
42 TAILQ_ENTRY(lmc) next;
45 static int lm_count;
47 static void lmc_parse(char *, size_t);
48 static void lmc_parse_file(char *);
49 static void lmc_parse_dir(char *);
50 static void lm_add(const char *, const char *, const char *);
51 static void lm_free(struct lm_list *);
52 static char *lml_find(struct lm_list *, const char *);
53 static struct lm_list *lmp_find(const char *);
54 static struct lm_list *lmp_init(char *);
55 static const char *quickbasename(const char *);
57 #define iseol(c) (((c) == '#') || ((c) == '\0') || \
58 ((c) == '\n') || ((c) == '\r'))
61 * Do not use ctype.h macros, which rely on working TLS. It is
62 * too early to have thread-local variables functional.
64 #define rtld_isspace(c) ((c) == ' ' || (c) == '\t')
66 int
67 lm_init(char *libmap_override)
69 char *p;
71 dbg("lm_init(\"%s\")", libmap_override);
72 TAILQ_INIT(&lmp_head);
74 lmc_parse_file(_PATH_LIBMAP_CONF);
76 if (libmap_override) {
78 * Do some character replacement to make $LDLIBMAP look
79 * like a text file, then parse it.
81 libmap_override = xstrdup(libmap_override);
82 for (p = libmap_override; *p; p++) {
83 switch (*p) {
84 case '=':
85 *p = ' ';
86 break;
87 case ',':
88 *p = '\n';
89 break;
92 lmc_parse(libmap_override, p - libmap_override);
93 free(libmap_override);
96 return (lm_count == 0);
99 static void
100 lmc_parse_file(char *path)
102 struct lmc *p;
103 struct stat st;
104 int fd;
105 char *rpath;
106 char *lm_map;
108 rpath = realpath(path, NULL);
109 if (rpath == NULL)
110 return;
112 TAILQ_FOREACH(p, &lmc_head, next) {
113 if (strcmp(p->path, rpath) == 0) {
114 free(rpath);
115 return;
119 fd = open(rpath, O_RDONLY | O_CLOEXEC);
120 if (fd == -1) {
121 dbg("lm_parse_file: open(\"%s\") failed, %s", rpath,
122 rtld_strerror(errno));
123 free(rpath);
124 return;
126 if (fstat(fd, &st) == -1) {
127 close(fd);
128 dbg("lm_parse_file: fstat(\"%s\") failed, %s", rpath,
129 rtld_strerror(errno));
130 free(rpath);
131 return;
133 lm_map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
134 if (lm_map == (const char *)MAP_FAILED) {
135 close(fd);
136 dbg("lm_parse_file: mmap(\"%s\") failed, %s", rpath,
137 rtld_strerror(errno));
138 free(rpath);
139 return;
141 close(fd);
142 p = xmalloc(sizeof(struct lmc));
143 p->path = rpath;
144 TAILQ_INSERT_HEAD(&lmc_head, p, next);
145 lmc_parse(lm_map, st.st_size);
146 munmap(lm_map, st.st_size);
149 static void
150 lmc_parse_dir(char *idir)
152 DIR *d;
153 struct dirent *dp;
154 struct lmc *p;
155 char conffile[MAXPATHLEN];
156 char *ext;
157 char *rpath;
159 rpath = realpath(idir, NULL);
160 if (rpath == NULL)
161 return;
163 TAILQ_FOREACH(p, &lmc_head, next) {
164 if (strcmp(p->path, rpath) == 0) {
165 free(rpath);
166 return;
169 d = opendir(idir);
170 if (d == NULL) {
171 free(rpath);
172 return;
175 p = xmalloc(sizeof(struct lmc));
176 p->path = rpath;
177 TAILQ_INSERT_HEAD(&lmc_head, p, next);
179 while ((dp = readdir(d)) != NULL) {
180 if (dp->d_ino == 0)
181 continue;
182 if (dp->d_type != DT_REG)
183 continue;
184 ext = strrchr(dp->d_name, '.');
185 if (ext == NULL)
186 continue;
187 if (strcmp(ext, ".conf") != 0)
188 continue;
189 if (strlcpy(conffile, idir, MAXPATHLEN) >= MAXPATHLEN)
190 continue; /* too long */
191 if (strlcat(conffile, "/", MAXPATHLEN) >= MAXPATHLEN)
192 continue; /* too long */
193 if (strlcat(conffile, dp->d_name, MAXPATHLEN) >= MAXPATHLEN)
194 continue; /* too long */
195 lmc_parse_file(conffile);
197 closedir(d);
200 static void
201 lmc_parse(char *lm_p, size_t lm_len)
203 char *cp, *f, *t, *c, *p;
204 char prog[MAXPATHLEN];
205 /* allow includedir + full length path */
206 char line[MAXPATHLEN + 13];
207 size_t cnt;
208 int i;
210 cnt = 0;
211 p = NULL;
212 while (cnt < lm_len) {
213 i = 0;
214 while (cnt < lm_len && lm_p[cnt] != '\n' &&
215 i < sizeof(line) - 1) {
216 line[i] = lm_p[cnt];
217 cnt++;
218 i++;
220 line[i] = '\0';
221 while (cnt < lm_len && lm_p[cnt] != '\n')
222 cnt++;
223 /* skip over nl */
224 cnt++;
226 cp = &line[0];
227 t = f = c = NULL;
229 /* Skip over leading space */
230 while (rtld_isspace(*cp)) cp++;
232 /* Found a comment or EOL */
233 if (iseol(*cp)) continue;
235 /* Found a constraint selector */
236 if (*cp == '[') {
237 cp++;
239 /* Skip leading space */
240 while (rtld_isspace(*cp)) cp++;
242 /* Found comment, EOL or end of selector */
243 if (iseol(*cp) || *cp == ']')
244 continue;
246 c = cp++;
247 /* Skip to end of word */
248 while (!rtld_isspace(*cp) && !iseol(*cp) && *cp != ']')
249 cp++;
251 /* Skip and zero out trailing space */
252 while (rtld_isspace(*cp)) *cp++ = '\0';
254 /* Check if there is a closing brace */
255 if (*cp != ']') continue;
257 /* Terminate string if there was no trailing space */
258 *cp++ = '\0';
261 * There should be nothing except whitespace or comment
262 from this point to the end of the line.
264 while(rtld_isspace(*cp)) cp++;
265 if (!iseol(*cp)) continue;
267 if (strlcpy(prog, c, sizeof prog) >= sizeof prog)
268 continue;
269 p = prog;
270 continue;
273 /* Parse the 'from' candidate. */
274 f = cp++;
275 while (!rtld_isspace(*cp) && !iseol(*cp)) cp++;
277 /* Skip and zero out the trailing whitespace */
278 while (rtld_isspace(*cp)) *cp++ = '\0';
280 /* Found a comment or EOL */
281 if (iseol(*cp)) continue;
283 /* Parse 'to' mapping */
284 t = cp++;
285 while (!rtld_isspace(*cp) && !iseol(*cp)) cp++;
287 /* Skip and zero out the trailing whitespace */
288 while (rtld_isspace(*cp)) *cp++ = '\0';
290 /* Should be no extra tokens at this point */
291 if (!iseol(*cp)) continue;
293 *cp = '\0';
294 if (strcmp(f, "includedir") == 0)
295 lmc_parse_dir(t);
296 else if (strcmp(f, "include") == 0)
297 lmc_parse_file(t);
298 else
299 lm_add(p, f, t);
303 static void
304 lm_free (struct lm_list *lml)
306 struct lm *lm;
308 dbg("%s(%p)", __func__, lml);
310 while (!TAILQ_EMPTY(lml)) {
311 lm = TAILQ_FIRST(lml);
312 TAILQ_REMOVE(lml, lm, lm_link);
313 free(lm->f);
314 free(lm->t);
315 free(lm);
317 return;
320 void
321 lm_fini (void)
323 struct lmp *lmp;
324 struct lmc *p;
326 dbg("%s()", __func__);
328 while (!TAILQ_EMPTY(&lmc_head)) {
329 p = TAILQ_FIRST(&lmc_head);
330 TAILQ_REMOVE(&lmc_head, p, next);
331 free(p->path);
332 free(p);
335 while (!TAILQ_EMPTY(&lmp_head)) {
336 lmp = TAILQ_FIRST(&lmp_head);
337 TAILQ_REMOVE(&lmp_head, lmp, lmp_link);
338 free(lmp->p);
339 lm_free(&lmp->lml);
340 free(lmp);
342 return;
345 static void
346 lm_add (const char *p, const char *f, const char *t)
348 struct lm_list *lml;
349 struct lm *lm;
351 if (p == NULL)
352 p = "$DEFAULT$";
354 dbg("%s(\"%s\", \"%s\", \"%s\")", __func__, p, f, t);
356 if ((lml = lmp_find(p)) == NULL)
357 lml = lmp_init(xstrdup(p));
359 lm = xmalloc(sizeof(struct lm));
360 lm->f = xstrdup(f);
361 lm->t = xstrdup(t);
362 TAILQ_INSERT_HEAD(lml, lm, lm_link);
363 lm_count++;
366 char *
367 lm_find (const char *p, const char *f)
369 struct lm_list *lml;
370 char *t;
372 dbg("%s(\"%s\", \"%s\")", __func__, p, f);
374 if (p != NULL && (lml = lmp_find(p)) != NULL) {
375 t = lml_find(lml, f);
376 if (t != NULL) {
378 * Add a global mapping if we have
379 * a successful constrained match.
381 lm_add(NULL, f, t);
382 return (t);
385 lml = lmp_find("$DEFAULT$");
386 if (lml != NULL)
387 return (lml_find(lml, f));
388 else
389 return (NULL);
392 /* Given a libmap translation list and a library name, return the
393 replacement library, or NULL */
394 char *
395 lm_findn (const char *p, const char *f, const int n)
397 char pathbuf[64], *s, *t;
399 if (n < sizeof(pathbuf) - 1)
400 s = pathbuf;
401 else
402 s = xmalloc(n + 1);
403 memcpy(s, f, n);
404 s[n] = '\0';
405 t = lm_find(p, s);
406 if (s != pathbuf)
407 free(s);
408 return (t);
411 static char *
412 lml_find (struct lm_list *lmh, const char *f)
414 struct lm *lm;
416 dbg("%s(%p, \"%s\")", __func__, lmh, f);
418 TAILQ_FOREACH(lm, lmh, lm_link)
419 if (strcmp(f, lm->f) == 0)
420 return (lm->t);
421 return (NULL);
424 /* Given an executable name, return a pointer to the translation list or
425 NULL if no matches */
426 static struct lm_list *
427 lmp_find (const char *n)
429 struct lmp *lmp;
431 dbg("%s(\"%s\")", __func__, n);
433 TAILQ_FOREACH(lmp, &lmp_head, lmp_link)
434 if ((lmp->type == T_EXACT && strcmp(n, lmp->p) == 0) ||
435 (lmp->type == T_DIRECTORY && strncmp(n, lmp->p, strlen(lmp->p)) == 0) ||
436 (lmp->type == T_BASENAME && strcmp(quickbasename(n), lmp->p) == 0))
437 return (&lmp->lml);
438 return (NULL);
441 static struct lm_list *
442 lmp_init (char *n)
444 struct lmp *lmp;
446 dbg("%s(\"%s\")", __func__, n);
448 lmp = xmalloc(sizeof(struct lmp));
449 lmp->p = n;
450 if (n[strlen(n)-1] == '/')
451 lmp->type = T_DIRECTORY;
452 else if (strchr(n,'/') == NULL)
453 lmp->type = T_BASENAME;
454 else
455 lmp->type = T_EXACT;
456 TAILQ_INIT(&lmp->lml);
457 TAILQ_INSERT_HEAD(&lmp_head, lmp, lmp_link);
459 return (&lmp->lml);
462 /* libc basename is overkill. Return a pointer to the character after the
463 last /, or the original string if there are no slashes. */
464 static const char *
465 quickbasename (const char *path)
467 const char *p = path;
468 for (; *path; path++) {
469 if (*path == '/')
470 p = path+1;
472 return (p);