]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - libexec/rtld-elf/libmap.c
bhnd(9): Fix a few mandoc related issues
[FreeBSD/FreeBSD.git] / libexec / rtld-elf / libmap.c
1 /*
2  * $FreeBSD$
3  */
4
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>
15
16 #include "debug.h"
17 #include "rtld.h"
18 #include "libmap.h"
19 #include "paths.h"
20 #include "rtld_libc.h"
21
22 TAILQ_HEAD(lm_list, lm);
23 struct lm {
24         char *f;
25         char *t;
26         TAILQ_ENTRY(lm) lm_link;
27 };
28
29 static TAILQ_HEAD(lmp_list, lmp) lmp_head = TAILQ_HEAD_INITIALIZER(lmp_head);
30 struct lmp {
31         char *p;
32         enum { T_EXACT=0, T_BASENAME, T_DIRECTORY } type;
33         struct lm_list lml;
34         TAILQ_ENTRY(lmp) lmp_link;
35 };
36
37 static TAILQ_HEAD(lmc_list, lmc) lmc_head = TAILQ_HEAD_INITIALIZER(lmc_head);
38 struct lmc {
39         char *path;
40         dev_t dev;
41         ino_t ino;
42         TAILQ_ENTRY(lmc) next;
43 };
44
45 static int lm_count;
46
47 static void lmc_parse(char *, size_t);
48 static void lmc_parse_file(const char *);
49 static void lmc_parse_dir(const 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 *);
56
57 #define iseol(c)        (((c) == '#') || ((c) == '\0') || \
58                          ((c) == '\n') || ((c) == '\r'))
59
60 /*
61  * Do not use ctype.h macros, which rely on working TLS.  It is
62  * too early to have thread-local variables functional.
63  */
64 #define rtld_isspace(c) ((c) == ' ' || (c) == '\t')
65
66 int
67 lm_init(char *libmap_override)
68 {
69         char *p;
70
71         dbg("lm_init(\"%s\")", libmap_override);
72         TAILQ_INIT(&lmp_head);
73
74         lmc_parse_file(ld_path_libmap_conf);
75
76         if (libmap_override) {
77                 /*
78                  * Do some character replacement to make $LDLIBMAP look
79                  * like a text file, then parse it.
80                  */
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;
90                         }
91                 }
92                 lmc_parse(libmap_override, p - libmap_override);
93                 free(libmap_override);
94         }
95
96         return (lm_count == 0);
97 }
98
99 static void
100 lmc_parse_file(const char *path)
101 {
102         struct lmc *p;
103         char *lm_map;
104         struct stat st;
105         ssize_t retval;
106         int fd;
107
108         TAILQ_FOREACH(p, &lmc_head, next) {
109                 if (strcmp(p->path, path) == 0)
110                         return;
111         }
112
113         fd = open(path, O_RDONLY | O_CLOEXEC);
114         if (fd == -1) {
115                 dbg("lm_parse_file: open(\"%s\") failed, %s", path,
116                     rtld_strerror(errno));
117                 return;
118         }
119         if (fstat(fd, &st) == -1) {
120                 close(fd);
121                 dbg("lm_parse_file: fstat(\"%s\") failed, %s", path,
122                     rtld_strerror(errno));
123                 return;
124         }
125
126         TAILQ_FOREACH(p, &lmc_head, next) {
127                 if (p->dev == st.st_dev && p->ino == st.st_ino) {
128                         close(fd);
129                         return;
130                 }
131         }
132
133         lm_map = xmalloc(st.st_size);
134         retval = read(fd, lm_map, st.st_size);
135         if (retval != st.st_size) {
136                 close(fd);
137                 free(lm_map);
138                 dbg("lm_parse_file: read(\"%s\") failed, %s", path,
139                     rtld_strerror(errno));
140                 return;
141         }
142         close(fd);
143         p = xmalloc(sizeof(struct lmc));
144         p->path = xstrdup(path);
145         p->dev = st.st_dev;
146         p->ino = st.st_ino;
147         TAILQ_INSERT_HEAD(&lmc_head, p, next);
148         lmc_parse(lm_map, st.st_size);
149         free(lm_map);
150 }
151
152 static void
153 lmc_parse_dir(const char *idir)
154 {
155         DIR *d;
156         struct dirent *dp;
157         struct lmc *p;
158         char conffile[MAXPATHLEN];
159         char *ext;
160
161         TAILQ_FOREACH(p, &lmc_head, next) {
162                 if (strcmp(p->path, idir) == 0)
163                         return;
164         }
165         d = opendir(idir);
166         if (d == NULL)
167                 return;
168
169         p = xmalloc(sizeof(struct lmc));
170         p->path = xstrdup(idir);
171         p->dev = NODEV;
172         p->ino = 0;
173         TAILQ_INSERT_HEAD(&lmc_head, p, next);
174
175         while ((dp = readdir(d)) != NULL) {
176                 if (dp->d_ino == 0)
177                         continue;
178                 if (dp->d_type != DT_REG)
179                         continue;
180                 ext = strrchr(dp->d_name, '.');
181                 if (ext == NULL)
182                         continue;
183                 if (strcmp(ext, ".conf") != 0)
184                         continue;
185                 if (strlcpy(conffile, idir, MAXPATHLEN) >= MAXPATHLEN)
186                         continue; /* too long */
187                 if (strlcat(conffile, "/", MAXPATHLEN) >= MAXPATHLEN)
188                         continue; /* too long */
189                 if (strlcat(conffile, dp->d_name, MAXPATHLEN) >= MAXPATHLEN)
190                         continue; /* too long */
191                 lmc_parse_file(conffile);
192         }
193         closedir(d);
194 }
195
196 static void
197 lmc_parse(char *lm_p, size_t lm_len)
198 {
199         char *cp, *f, *t, *c, *p;
200         char prog[MAXPATHLEN];
201         /* allow includedir + full length path */
202         char line[MAXPATHLEN + 13];
203         size_t cnt, i;
204
205         cnt = 0;
206         p = NULL;
207         while (cnt < lm_len) {
208                 i = 0;
209                 while (cnt < lm_len && lm_p[cnt] != '\n' &&
210                     i < sizeof(line) - 1) {
211                         line[i] = lm_p[cnt];
212                         cnt++;
213                         i++;
214                 }
215                 line[i] = '\0';
216                 while (cnt < lm_len && lm_p[cnt] != '\n')
217                         cnt++;
218                 /* skip over nl */
219                 cnt++;
220
221                 cp = &line[0];
222                 t = f = c = NULL;
223
224                 /* Skip over leading space */
225                 while (rtld_isspace(*cp))
226                         cp++;
227
228                 /* Found a comment or EOL */
229                 if (iseol(*cp))
230                         continue;
231
232                 /* Found a constraint selector */
233                 if (*cp == '[') {
234                         cp++;
235
236                         /* Skip leading space */
237                         while (rtld_isspace(*cp))
238                                 cp++;
239
240                         /* Found comment, EOL or end of selector */
241                         if  (iseol(*cp) || *cp == ']')
242                                 continue;
243
244                         c = cp++;
245                         /* Skip to end of word */
246                         while (!rtld_isspace(*cp) && !iseol(*cp) && *cp != ']')
247                                 cp++;
248
249                         /* Skip and zero out trailing space */
250                         while (rtld_isspace(*cp))
251                                 *cp++ = '\0';
252
253                         /* Check if there is a closing brace */
254                         if (*cp != ']')
255                                 continue;
256
257                         /* Terminate string if there was no trailing space */
258                         *cp++ = '\0';
259
260                         /*
261                          * There should be nothing except whitespace or comment
262                           from this point to the end of the line.
263                          */
264                         while (rtld_isspace(*cp))
265                                 cp++;
266                         if (!iseol(*cp))
267                                 continue;
268
269                         if (strlcpy(prog, c, sizeof prog) >= sizeof prog)
270                                 continue;
271                         p = prog;
272                         continue;
273                 }
274
275                 /* Parse the 'from' candidate. */
276                 f = cp++;
277                 while (!rtld_isspace(*cp) && !iseol(*cp))
278                         cp++;
279
280                 /* Skip and zero out the trailing whitespace */
281                 while (rtld_isspace(*cp))
282                         *cp++ = '\0';
283
284                 /* Found a comment or EOL */
285                 if (iseol(*cp))
286                         continue;
287
288                 /* Parse 'to' mapping */
289                 t = cp++;
290                 while (!rtld_isspace(*cp) && !iseol(*cp))
291                         cp++;
292
293                 /* Skip and zero out the trailing whitespace */
294                 while (rtld_isspace(*cp))
295                         *cp++ = '\0';
296
297                 /* Should be no extra tokens at this point */
298                 if (!iseol(*cp))
299                         continue;
300
301                 *cp = '\0';
302                 if (strcmp(f, "includedir") == 0)
303                         lmc_parse_dir(t);
304                 else if (strcmp(f, "include") == 0)
305                         lmc_parse_file(t);
306                 else
307                         lm_add(p, f, t);
308         }
309 }
310
311 static void
312 lm_free(struct lm_list *lml)
313 {
314         struct lm *lm;
315
316         dbg("%s(%p)", __func__, lml);
317
318         while (!TAILQ_EMPTY(lml)) {
319                 lm = TAILQ_FIRST(lml);
320                 TAILQ_REMOVE(lml, lm, lm_link);
321                 free(lm->f);
322                 free(lm->t);
323                 free(lm);
324         }
325 }
326
327 void
328 lm_fini(void)
329 {
330         struct lmp *lmp;
331         struct lmc *p;
332
333         dbg("%s()", __func__);
334
335         while (!TAILQ_EMPTY(&lmc_head)) {
336                 p = TAILQ_FIRST(&lmc_head);
337                 TAILQ_REMOVE(&lmc_head, p, next);
338                 free(p->path);
339                 free(p);
340         }
341
342         while (!TAILQ_EMPTY(&lmp_head)) {
343                 lmp = TAILQ_FIRST(&lmp_head);
344                 TAILQ_REMOVE(&lmp_head, lmp, lmp_link);
345                 free(lmp->p);
346                 lm_free(&lmp->lml);
347                 free(lmp);
348         }
349 }
350
351 static void
352 lm_add(const char *p, const char *f, const char *t)
353 {
354         struct lm_list *lml;
355         struct lm *lm;
356         const char *t1;
357
358         if (p == NULL)
359                 p = "$DEFAULT$";
360
361         dbg("%s(\"%s\", \"%s\", \"%s\")", __func__, p, f, t);
362
363         if ((lml = lmp_find(p)) == NULL)
364                 lml = lmp_init(xstrdup(p));
365
366         t1 = lml_find(lml, f);
367         if (t1 == NULL || strcmp(t1, t) != 0) {
368                 lm = xmalloc(sizeof(struct lm));
369                 lm->f = xstrdup(f);
370                 lm->t = xstrdup(t);
371                 TAILQ_INSERT_HEAD(lml, lm, lm_link);
372                 lm_count++;
373         }
374 }
375
376 char *
377 lm_find(const char *p, const char *f)
378 {
379         struct lm_list *lml;
380         char *t;
381
382         dbg("%s(\"%s\", \"%s\")", __func__, p, f);
383
384         if (p != NULL && (lml = lmp_find(p)) != NULL) {
385                 t = lml_find(lml, f);
386                 if (t != NULL) {
387                         /*
388                          * Add a global mapping if we have
389                          * a successful constrained match.
390                          */
391                         lm_add(NULL, f, t);
392                         return (t);
393                 }
394         }
395         lml = lmp_find("$DEFAULT$");
396         if (lml != NULL)
397                 return (lml_find(lml, f));
398         return (NULL);
399 }
400
401 /*
402  * Given a libmap translation list and a library name, return the
403  * replacement library, or NULL.
404  */
405 char *
406 lm_findn(const char *p, const char *f, const size_t n)
407 {
408         char pathbuf[64], *s, *t;
409
410         if (n < sizeof(pathbuf) - 1)
411                 s = pathbuf;
412         else
413                 s = xmalloc(n + 1);
414         memcpy(s, f, n);
415         s[n] = '\0';
416         t = lm_find(p, s);
417         if (s != pathbuf)
418                 free(s);
419         return (t);
420 }
421
422 static char *
423 lml_find(struct lm_list *lmh, const char *f)
424 {
425         struct lm *lm;
426
427         dbg("%s(%p, \"%s\")", __func__, lmh, f);
428
429         TAILQ_FOREACH(lm, lmh, lm_link) {
430                 if (strcmp(f, lm->f) == 0)
431                         return (lm->t);
432         }
433         return (NULL);
434 }
435
436 /*
437  * Given an executable name, return a pointer to the translation list or
438  * NULL if no matches.
439  */
440 static struct lm_list *
441 lmp_find(const char *n)
442 {
443         struct lmp *lmp;
444
445         dbg("%s(\"%s\")", __func__, n);
446
447         TAILQ_FOREACH(lmp, &lmp_head, lmp_link) {
448                 if ((lmp->type == T_EXACT && strcmp(n, lmp->p) == 0) ||
449                     (lmp->type == T_DIRECTORY && strncmp(n, lmp->p,
450                     strlen(lmp->p)) == 0) ||
451                     (lmp->type == T_BASENAME && strcmp(quickbasename(n),
452                     lmp->p) == 0))
453                         return (&lmp->lml);
454         }
455         return (NULL);
456 }
457
458 static struct lm_list *
459 lmp_init(char *n)
460 {
461         struct lmp *lmp;
462
463         dbg("%s(\"%s\")", __func__, n);
464
465         lmp = xmalloc(sizeof(struct lmp));
466         lmp->p = n;
467         if (n[strlen(n) - 1] == '/')
468                 lmp->type = T_DIRECTORY;
469         else if (strchr(n,'/') == NULL)
470                 lmp->type = T_BASENAME;
471         else
472                 lmp->type = T_EXACT;
473         TAILQ_INIT(&lmp->lml);
474         TAILQ_INSERT_HEAD(&lmp_head, lmp, lmp_link);
475
476         return (&lmp->lml);
477 }
478
479 /*
480  * libc basename is overkill.  Return a pointer to the character after
481  * the last /, or the original string if there are no slashes.
482  */
483 static const char *
484 quickbasename(const char *path)
485 {
486         const char *p;
487
488         for (p = path; *path != '\0'; path++) {
489                 if (*path == '/')
490                         p = path + 1;
491         }
492         return (p);
493 }