xref: /freebsd/libexec/rtld-elf/libmap.c (revision 78b648465d34c1d96cd60b65b64a824393a19f91)
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"
19b2a4014cSWarner Losh #include "paths.h"
20c905e45dSPeter Wemm 
2129ade362SMatthew N. Dodd TAILQ_HEAD(lm_list, lm);
2229ade362SMatthew N. Dodd struct lm {
2329ade362SMatthew N. Dodd 	char *f;
2429ade362SMatthew N. Dodd 	char *t;
2529ade362SMatthew N. Dodd 	TAILQ_ENTRY(lm)	lm_link;
2629ade362SMatthew N. Dodd };
2729ade362SMatthew N. Dodd 
2829ade362SMatthew N. Dodd TAILQ_HEAD(lmp_list, lmp) lmp_head = TAILQ_HEAD_INITIALIZER(lmp_head);
2929ade362SMatthew N. Dodd struct lmp {
3029ade362SMatthew N. Dodd 	char *p;
31966efcc7SMatthew N. Dodd 	enum { T_EXACT=0, T_BASENAME, T_DIRECTORY } type;
3229ade362SMatthew N. Dodd 	struct lm_list lml;
3329ade362SMatthew N. Dodd 	TAILQ_ENTRY(lmp) lmp_link;
3429ade362SMatthew N. Dodd };
3529ade362SMatthew N. Dodd 
36faf66437SBaptiste Daroussin static TAILQ_HEAD(lmc_list, lmc) lmc_head = TAILQ_HEAD_INITIALIZER(lmc_head);
37faf66437SBaptiste Daroussin struct lmc {
38faf66437SBaptiste Daroussin 	char *path;
396b61e3e4SEdward Tomasz Napierala 	dev_t dev;
406b61e3e4SEdward Tomasz Napierala 	ino_t ino;
41faf66437SBaptiste Daroussin 	TAILQ_ENTRY(lmc) next;
42faf66437SBaptiste Daroussin };
43faf66437SBaptiste Daroussin 
445b08cb04SMatthew N. Dodd static int lm_count;
455b08cb04SMatthew N. Dodd 
4668f1db20SKonstantin Belousov static void lmc_parse(char *, size_t);
47faf66437SBaptiste Daroussin static void lmc_parse_file(char *);
48faf66437SBaptiste Daroussin static void lmc_parse_dir(char *);
491340fc10SMatthew N. Dodd static void lm_add(const char *, const char *, const char *);
5029ade362SMatthew N. Dodd static void lm_free(struct lm_list *);
5129ade362SMatthew N. Dodd static char *lml_find(struct lm_list *, const char *);
5229ade362SMatthew N. Dodd static struct lm_list *lmp_find(const char *);
5329ade362SMatthew N. Dodd static struct lm_list *lmp_init(char *);
54966efcc7SMatthew N. Dodd static const char *quickbasename(const char *);
5529ade362SMatthew N. Dodd 
56623b6bd2SMatthew N. Dodd #define	iseol(c)	(((c) == '#') || ((c) == '\0') || \
57623b6bd2SMatthew N. Dodd 			 ((c) == '\n') || ((c) == '\r'))
58623b6bd2SMatthew N. Dodd 
59b36070f5SKonstantin Belousov /*
60b36070f5SKonstantin Belousov  * Do not use ctype.h macros, which rely on working TLS.  It is
61b36070f5SKonstantin Belousov  * too early to have thread-local variables functional.
62b36070f5SKonstantin Belousov  */
6344976acaSSergey Kandaurov #define	rtld_isspace(c)	((c) == ' ' || (c) == '\t')
64b36070f5SKonstantin Belousov 
654402996dSMatthew N. Dodd int
665b08cb04SMatthew N. Dodd lm_init(char *libmap_override)
6729ade362SMatthew N. Dodd {
68faf66437SBaptiste Daroussin 	char *p;
6929ade362SMatthew N. Dodd 
7068f1db20SKonstantin Belousov 	dbg("lm_init(\"%s\")", libmap_override);
7129ade362SMatthew N. Dodd 	TAILQ_INIT(&lmp_head);
7229ade362SMatthew N. Dodd 
733deca56fSWarner Losh 	lmc_parse_file(ld_path_libmap_conf);
745b08cb04SMatthew N. Dodd 
755b08cb04SMatthew N. Dodd 	if (libmap_override) {
7668f1db20SKonstantin Belousov 		/*
77490c68deSKonstantin Belousov 		 * Do some character replacement to make $LDLIBMAP look
7868f1db20SKonstantin Belousov 		 * like a text file, then parse it.
7968f1db20SKonstantin Belousov 		 */
805b08cb04SMatthew N. Dodd 		libmap_override = xstrdup(libmap_override);
815b08cb04SMatthew N. Dodd 		for (p = libmap_override; *p; p++) {
825b08cb04SMatthew N. Dodd 			switch (*p) {
835b08cb04SMatthew N. Dodd 			case '=':
8468f1db20SKonstantin Belousov 				*p = ' ';
8568f1db20SKonstantin Belousov 				break;
865b08cb04SMatthew N. Dodd 			case ',':
8768f1db20SKonstantin Belousov 				*p = '\n';
8868f1db20SKonstantin Belousov 				break;
895b08cb04SMatthew N. Dodd 			}
905b08cb04SMatthew N. Dodd 		}
91490c68deSKonstantin Belousov 		lmc_parse(libmap_override, p - libmap_override);
92490c68deSKonstantin Belousov 		free(libmap_override);
935b08cb04SMatthew N. Dodd 	}
945b08cb04SMatthew N. Dodd 
955b08cb04SMatthew N. Dodd 	return (lm_count == 0);
965b08cb04SMatthew N. Dodd }
975b08cb04SMatthew N. Dodd 
985b08cb04SMatthew N. Dodd static void
99faf66437SBaptiste Daroussin lmc_parse_file(char *path)
100faf66437SBaptiste Daroussin {
101faf66437SBaptiste Daroussin 	struct lmc *p;
102c1a0a86eSKonstantin Belousov 	char *lm_map;
103faf66437SBaptiste Daroussin 	struct stat st;
104a8b31c14SEdward Tomasz Napierala 	ssize_t retval;
105faf66437SBaptiste Daroussin 	int fd;
106faf66437SBaptiste Daroussin 
107faf66437SBaptiste Daroussin 	TAILQ_FOREACH(p, &lmc_head, next) {
1086b61e3e4SEdward Tomasz Napierala 		if (strcmp(p->path, path) == 0)
109faf66437SBaptiste Daroussin 			return;
110faf66437SBaptiste Daroussin 	}
111faf66437SBaptiste Daroussin 
1126b61e3e4SEdward Tomasz Napierala 	fd = open(path, O_RDONLY | O_CLOEXEC);
113faf66437SBaptiste Daroussin 	if (fd == -1) {
1146b61e3e4SEdward Tomasz Napierala 		dbg("lm_parse_file: open(\"%s\") failed, %s", path,
115faf66437SBaptiste Daroussin 		    rtld_strerror(errno));
116faf66437SBaptiste Daroussin 		return;
117faf66437SBaptiste Daroussin 	}
118faf66437SBaptiste Daroussin 	if (fstat(fd, &st) == -1) {
119faf66437SBaptiste Daroussin 		close(fd);
1206b61e3e4SEdward Tomasz Napierala 		dbg("lm_parse_file: fstat(\"%s\") failed, %s", path,
121faf66437SBaptiste Daroussin 		    rtld_strerror(errno));
122faf66437SBaptiste Daroussin 		return;
123faf66437SBaptiste Daroussin 	}
1246b61e3e4SEdward Tomasz Napierala 
1256b61e3e4SEdward Tomasz Napierala 	TAILQ_FOREACH(p, &lmc_head, next) {
1266b61e3e4SEdward Tomasz Napierala 		if (p->dev == st.st_dev && p->ino == st.st_ino) {
1276b61e3e4SEdward Tomasz Napierala 			close(fd);
1286b61e3e4SEdward Tomasz Napierala 			return;
1296b61e3e4SEdward Tomasz Napierala 		}
1306b61e3e4SEdward Tomasz Napierala 	}
1316b61e3e4SEdward Tomasz Napierala 
132a8b31c14SEdward Tomasz Napierala 	lm_map = xmalloc(st.st_size);
133a8b31c14SEdward Tomasz Napierala 	retval = read(fd, lm_map, st.st_size);
134a8b31c14SEdward Tomasz Napierala 	if (retval != st.st_size) {
135faf66437SBaptiste Daroussin 		close(fd);
136152036a0SEdward Tomasz Napierala 		free(lm_map);
137a8b31c14SEdward Tomasz Napierala 		dbg("lm_parse_file: read(\"%s\") failed, %s", path,
138faf66437SBaptiste Daroussin 		    rtld_strerror(errno));
139faf66437SBaptiste Daroussin 		return;
140faf66437SBaptiste Daroussin 	}
141faf66437SBaptiste Daroussin 	close(fd);
142faf66437SBaptiste Daroussin 	p = xmalloc(sizeof(struct lmc));
1436b61e3e4SEdward Tomasz Napierala 	p->path = xstrdup(path);
1446b61e3e4SEdward Tomasz Napierala 	p->dev = st.st_dev;
1456b61e3e4SEdward Tomasz Napierala 	p->ino = st.st_ino;
146faf66437SBaptiste Daroussin 	TAILQ_INSERT_HEAD(&lmc_head, p, next);
147faf66437SBaptiste Daroussin 	lmc_parse(lm_map, st.st_size);
148a8b31c14SEdward Tomasz Napierala 	free(lm_map);
149faf66437SBaptiste Daroussin }
150faf66437SBaptiste Daroussin 
151faf66437SBaptiste Daroussin static void
152faf66437SBaptiste Daroussin lmc_parse_dir(char *idir)
153faf66437SBaptiste Daroussin {
154faf66437SBaptiste Daroussin 	DIR *d;
155faf66437SBaptiste Daroussin 	struct dirent *dp;
156faf66437SBaptiste Daroussin 	struct lmc *p;
157faf66437SBaptiste Daroussin 	char conffile[MAXPATHLEN];
158faf66437SBaptiste Daroussin 	char *ext;
159faf66437SBaptiste Daroussin 
160faf66437SBaptiste Daroussin 	TAILQ_FOREACH(p, &lmc_head, next) {
1616b61e3e4SEdward Tomasz Napierala 		if (strcmp(p->path, idir) == 0)
162faf66437SBaptiste Daroussin 			return;
163faf66437SBaptiste Daroussin 	}
164faf66437SBaptiste Daroussin 	d = opendir(idir);
1656b61e3e4SEdward Tomasz Napierala 	if (d == NULL)
166faf66437SBaptiste Daroussin 		return;
167faf66437SBaptiste Daroussin 
168faf66437SBaptiste Daroussin 	p = xmalloc(sizeof(struct lmc));
1696b61e3e4SEdward Tomasz Napierala 	p->path = xstrdup(idir);
1706b61e3e4SEdward Tomasz Napierala 	p->dev = NODEV;
1716b61e3e4SEdward Tomasz Napierala 	p->ino = 0;
172faf66437SBaptiste Daroussin 	TAILQ_INSERT_HEAD(&lmc_head, p, next);
173faf66437SBaptiste Daroussin 
174faf66437SBaptiste Daroussin 	while ((dp = readdir(d)) != NULL) {
175faf66437SBaptiste Daroussin 		if (dp->d_ino == 0)
176faf66437SBaptiste Daroussin 			continue;
177faf66437SBaptiste Daroussin 		if (dp->d_type != DT_REG)
178faf66437SBaptiste Daroussin 			continue;
179faf66437SBaptiste Daroussin 		ext = strrchr(dp->d_name, '.');
180faf66437SBaptiste Daroussin 		if (ext == NULL)
181faf66437SBaptiste Daroussin 			continue;
182faf66437SBaptiste Daroussin 		if (strcmp(ext, ".conf") != 0)
183faf66437SBaptiste Daroussin 			continue;
184faf66437SBaptiste Daroussin 		if (strlcpy(conffile, idir, MAXPATHLEN) >= MAXPATHLEN)
185faf66437SBaptiste Daroussin 			continue; /* too long */
186faf66437SBaptiste Daroussin 		if (strlcat(conffile, "/", MAXPATHLEN) >= MAXPATHLEN)
187faf66437SBaptiste Daroussin 			continue; /* too long */
188faf66437SBaptiste Daroussin 		if (strlcat(conffile, dp->d_name, MAXPATHLEN) >= MAXPATHLEN)
189faf66437SBaptiste Daroussin 			continue; /* too long */
190faf66437SBaptiste Daroussin 		lmc_parse_file(conffile);
191faf66437SBaptiste Daroussin 	}
192faf66437SBaptiste Daroussin 	closedir(d);
193faf66437SBaptiste Daroussin }
194faf66437SBaptiste Daroussin 
195faf66437SBaptiste Daroussin static void
19668f1db20SKonstantin Belousov lmc_parse(char *lm_p, size_t lm_len)
1975b08cb04SMatthew N. Dodd {
19868f1db20SKonstantin Belousov 	char *cp, *f, *t, *c, *p;
1995b08cb04SMatthew N. Dodd 	char prog[MAXPATHLEN];
200faf66437SBaptiste Daroussin 	/* allow includedir + full length path */
201faf66437SBaptiste Daroussin 	char line[MAXPATHLEN + 13];
202*78b64846SAlex Richardson 	size_t cnt, i;
2035b08cb04SMatthew N. Dodd 
20468f1db20SKonstantin Belousov 	cnt = 0;
20529ade362SMatthew N. Dodd 	p = NULL;
20668f1db20SKonstantin Belousov 	while (cnt < lm_len) {
20768f1db20SKonstantin Belousov 		i = 0;
2080fa46a42SPedro F. Giffuni 		while (cnt < lm_len && lm_p[cnt] != '\n' &&
20968f1db20SKonstantin Belousov 		    i < sizeof(line) - 1) {
21068f1db20SKonstantin Belousov 			line[i] = lm_p[cnt];
21168f1db20SKonstantin Belousov 			cnt++;
21268f1db20SKonstantin Belousov 			i++;
21368f1db20SKonstantin Belousov 		}
21468f1db20SKonstantin Belousov 		line[i] = '\0';
2150fa46a42SPedro F. Giffuni 		while (cnt < lm_len && lm_p[cnt] != '\n')
21668f1db20SKonstantin Belousov 			cnt++;
21768f1db20SKonstantin Belousov 		/* skip over nl */
21868f1db20SKonstantin Belousov 		cnt++;
21968f1db20SKonstantin Belousov 
22068f1db20SKonstantin Belousov 		cp = &line[0];
2211340fc10SMatthew N. Dodd 		t = f = c = NULL;
222623b6bd2SMatthew N. Dodd 
22329ade362SMatthew N. Dodd 		/* Skip over leading space */
224c1a0a86eSKonstantin Belousov 		while (rtld_isspace(*cp))
225c1a0a86eSKonstantin Belousov 			cp++;
226623b6bd2SMatthew N. Dodd 
22729ade362SMatthew N. Dodd 		/* Found a comment or EOL */
228c1a0a86eSKonstantin Belousov 		if (iseol(*cp))
229c1a0a86eSKonstantin Belousov 			continue;
230623b6bd2SMatthew N. Dodd 
231623b6bd2SMatthew N. Dodd 		/* Found a constraint selector */
23229ade362SMatthew N. Dodd 		if (*cp == '[') {
23329ade362SMatthew N. Dodd 			cp++;
234623b6bd2SMatthew N. Dodd 
23529ade362SMatthew N. Dodd 			/* Skip leading space */
236c1a0a86eSKonstantin Belousov 			while (rtld_isspace(*cp))
237c1a0a86eSKonstantin Belousov 				cp++;
238623b6bd2SMatthew N. Dodd 
23929ade362SMatthew N. Dodd 			/* Found comment, EOL or end of selector */
240486089f0SAlexander Kabaev 			if  (iseol(*cp) || *cp == ']')
241486089f0SAlexander Kabaev 				continue;
242623b6bd2SMatthew N. Dodd 
2431340fc10SMatthew N. Dodd 			c = cp++;
24429ade362SMatthew N. Dodd 			/* Skip to end of word */
24544976acaSSergey Kandaurov 			while (!rtld_isspace(*cp) && !iseol(*cp) && *cp != ']')
246486089f0SAlexander Kabaev 				cp++;
247623b6bd2SMatthew N. Dodd 
248623b6bd2SMatthew N. Dodd 			/* Skip and zero out trailing space */
249c1a0a86eSKonstantin Belousov 			while (rtld_isspace(*cp))
250c1a0a86eSKonstantin Belousov 				*cp++ = '\0';
251623b6bd2SMatthew N. Dodd 
252623b6bd2SMatthew N. Dodd 			/* Check if there is a closing brace */
253c1a0a86eSKonstantin Belousov 			if (*cp != ']')
254c1a0a86eSKonstantin Belousov 				continue;
255623b6bd2SMatthew N. Dodd 
256623b6bd2SMatthew N. Dodd 			/* Terminate string if there was no trailing space */
25729ade362SMatthew N. Dodd 			*cp++ = '\0';
258623b6bd2SMatthew N. Dodd 
259623b6bd2SMatthew N. Dodd 			/*
260623b6bd2SMatthew N. Dodd 			 * There should be nothing except whitespace or comment
2616d5d786fSAlexander Kabaev 			  from this point to the end of the line.
262623b6bd2SMatthew N. Dodd 			 */
263c1a0a86eSKonstantin Belousov 			while (rtld_isspace(*cp))
264c1a0a86eSKonstantin Belousov 				cp++;
265c1a0a86eSKonstantin Belousov 			if (!iseol(*cp))
266c1a0a86eSKonstantin Belousov 				continue;
267623b6bd2SMatthew N. Dodd 
268faf66437SBaptiste Daroussin 			if (strlcpy(prog, c, sizeof prog) >= sizeof prog)
269faf66437SBaptiste Daroussin 				continue;
27029ade362SMatthew N. Dodd 			p = prog;
271486089f0SAlexander Kabaev 			continue;
272623b6bd2SMatthew N. Dodd 		}
273623b6bd2SMatthew N. Dodd 
274623b6bd2SMatthew N. Dodd 		/* Parse the 'from' candidate. */
275486089f0SAlexander Kabaev 		f = cp++;
276c1a0a86eSKonstantin Belousov 		while (!rtld_isspace(*cp) && !iseol(*cp))
277c1a0a86eSKonstantin Belousov 			cp++;
27829ade362SMatthew N. Dodd 
279623b6bd2SMatthew N. Dodd 		/* Skip and zero out the trailing whitespace */
280c1a0a86eSKonstantin Belousov 		while (rtld_isspace(*cp))
281c1a0a86eSKonstantin Belousov 			*cp++ = '\0';
282623b6bd2SMatthew N. Dodd 
283623b6bd2SMatthew N. Dodd 		/* Found a comment or EOL */
284c1a0a86eSKonstantin Belousov 		if (iseol(*cp))
285c1a0a86eSKonstantin Belousov 			continue;
286623b6bd2SMatthew N. Dodd 
287623b6bd2SMatthew N. Dodd 		/* Parse 'to' mapping */
288486089f0SAlexander Kabaev 		t = cp++;
289c1a0a86eSKonstantin Belousov 		while (!rtld_isspace(*cp) && !iseol(*cp))
290c1a0a86eSKonstantin Belousov 			cp++;
291623b6bd2SMatthew N. Dodd 
292486089f0SAlexander Kabaev 		/* Skip and zero out the trailing whitespace */
293c1a0a86eSKonstantin Belousov 		while (rtld_isspace(*cp))
294c1a0a86eSKonstantin Belousov 			*cp++ = '\0';
295486089f0SAlexander Kabaev 
296486089f0SAlexander Kabaev 		/* Should be no extra tokens at this point */
297c1a0a86eSKonstantin Belousov 		if (!iseol(*cp))
298c1a0a86eSKonstantin Belousov 			continue;
299486089f0SAlexander Kabaev 
300486089f0SAlexander Kabaev 		*cp = '\0';
301faf66437SBaptiste Daroussin 		if (strcmp(f, "includedir") == 0)
302faf66437SBaptiste Daroussin 			lmc_parse_dir(t);
303faf66437SBaptiste Daroussin 		else if (strcmp(f, "include") == 0)
304faf66437SBaptiste Daroussin 			lmc_parse_file(t);
305faf66437SBaptiste Daroussin 		else
3061340fc10SMatthew N. Dodd 			lm_add(p, f, t);
30729ade362SMatthew N. Dodd 	}
30829ade362SMatthew N. Dodd }
30929ade362SMatthew N. Dodd 
31029ade362SMatthew N. Dodd static void
31129ade362SMatthew N. Dodd lm_free(struct lm_list *lml)
31229ade362SMatthew N. Dodd {
31329ade362SMatthew N. Dodd 	struct lm *lm;
31429ade362SMatthew N. Dodd 
3151aac1ed6SMatthew N. Dodd 	dbg("%s(%p)", __func__, lml);
3161aac1ed6SMatthew N. Dodd 
31729ade362SMatthew N. Dodd 	while (!TAILQ_EMPTY(lml)) {
31829ade362SMatthew N. Dodd 		lm = TAILQ_FIRST(lml);
31929ade362SMatthew N. Dodd 		TAILQ_REMOVE(lml, lm, lm_link);
32029ade362SMatthew N. Dodd 		free(lm->f);
32129ade362SMatthew N. Dodd 		free(lm->t);
32229ade362SMatthew N. Dodd 		free(lm);
32329ade362SMatthew N. Dodd 	}
32429ade362SMatthew N. Dodd }
32529ade362SMatthew N. Dodd 
32629ade362SMatthew N. Dodd void
32729ade362SMatthew N. Dodd lm_fini(void)
32829ade362SMatthew N. Dodd {
32929ade362SMatthew N. Dodd 	struct lmp *lmp;
330faf66437SBaptiste Daroussin 	struct lmc *p;
33129ade362SMatthew N. Dodd 
3321aac1ed6SMatthew N. Dodd 	dbg("%s()", __func__);
3331aac1ed6SMatthew N. Dodd 
334faf66437SBaptiste Daroussin 	while (!TAILQ_EMPTY(&lmc_head)) {
335faf66437SBaptiste Daroussin 		p = TAILQ_FIRST(&lmc_head);
336faf66437SBaptiste Daroussin 		TAILQ_REMOVE(&lmc_head, p, next);
337faf66437SBaptiste Daroussin 		free(p->path);
338faf66437SBaptiste Daroussin 		free(p);
339faf66437SBaptiste Daroussin 	}
340faf66437SBaptiste Daroussin 
34129ade362SMatthew N. Dodd 	while (!TAILQ_EMPTY(&lmp_head)) {
34229ade362SMatthew N. Dodd 		lmp = TAILQ_FIRST(&lmp_head);
34329ade362SMatthew N. Dodd 		TAILQ_REMOVE(&lmp_head, lmp, lmp_link);
34429ade362SMatthew N. Dodd 		free(lmp->p);
34529ade362SMatthew N. Dodd 		lm_free(&lmp->lml);
34629ade362SMatthew N. Dodd 		free(lmp);
34729ade362SMatthew N. Dodd 	}
34829ade362SMatthew N. Dodd }
34929ade362SMatthew N. Dodd 
35029ade362SMatthew N. Dodd static void
3511340fc10SMatthew N. Dodd lm_add(const char *p, const char *f, const char *t)
35229ade362SMatthew N. Dodd {
35329ade362SMatthew N. Dodd 	struct lm_list *lml;
35429ade362SMatthew N. Dodd 	struct lm *lm;
3556c306765SKonstantin Belousov 	const char *t1;
35629ade362SMatthew N. Dodd 
35729ade362SMatthew N. Dodd 	if (p == NULL)
35829ade362SMatthew N. Dodd 		p = "$DEFAULT$";
35929ade362SMatthew N. Dodd 
3601aac1ed6SMatthew N. Dodd 	dbg("%s(\"%s\", \"%s\", \"%s\")", __func__, p, f, t);
3611aac1ed6SMatthew N. Dodd 
36229ade362SMatthew N. Dodd 	if ((lml = lmp_find(p)) == NULL)
3633467e8b8SMatthew N. Dodd 		lml = lmp_init(xstrdup(p));
36429ade362SMatthew N. Dodd 
3656c306765SKonstantin Belousov 	t1 = lml_find(lml, f);
3666c306765SKonstantin Belousov 	if (t1 == NULL || strcmp(t1, t) != 0) {
3673467e8b8SMatthew N. Dodd 		lm = xmalloc(sizeof(struct lm));
3681340fc10SMatthew N. Dodd 		lm->f = xstrdup(f);
3691340fc10SMatthew N. Dodd 		lm->t = xstrdup(t);
37029ade362SMatthew N. Dodd 		TAILQ_INSERT_HEAD(lml, lm, lm_link);
3715b08cb04SMatthew N. Dodd 		lm_count++;
37229ade362SMatthew N. Dodd 	}
3736c306765SKonstantin Belousov }
37429ade362SMatthew N. Dodd 
37529ade362SMatthew N. Dodd char *
37629ade362SMatthew N. Dodd lm_find(const char *p, const char *f)
37729ade362SMatthew N. Dodd {
37829ade362SMatthew N. Dodd 	struct lm_list *lml;
37929ade362SMatthew N. Dodd 	char *t;
38029ade362SMatthew N. Dodd 
3811aac1ed6SMatthew N. Dodd 	dbg("%s(\"%s\", \"%s\")", __func__, p, f);
3821aac1ed6SMatthew N. Dodd 
38329ade362SMatthew N. Dodd 	if (p != NULL && (lml = lmp_find(p)) != NULL) {
38429ade362SMatthew N. Dodd 		t = lml_find(lml, f);
3853467e8b8SMatthew N. Dodd 		if (t != NULL) {
3863467e8b8SMatthew N. Dodd 			/*
3873467e8b8SMatthew N. Dodd 			 * Add a global mapping if we have
3883467e8b8SMatthew N. Dodd 			 * a successful constrained match.
3893467e8b8SMatthew N. Dodd 			 */
3901340fc10SMatthew N. Dodd 			lm_add(NULL, f, t);
39129ade362SMatthew N. Dodd 			return (t);
39229ade362SMatthew N. Dodd 		}
3933467e8b8SMatthew N. Dodd 	}
39429ade362SMatthew N. Dodd 	lml = lmp_find("$DEFAULT$");
39529ade362SMatthew N. Dodd 	if (lml != NULL)
39629ade362SMatthew N. Dodd 		return (lml_find(lml, f));
39729ade362SMatthew N. Dodd 	return (NULL);
39829ade362SMatthew N. Dodd }
39929ade362SMatthew N. Dodd 
400c1a0a86eSKonstantin Belousov /*
401c1a0a86eSKonstantin Belousov  * Given a libmap translation list and a library name, return the
402c1a0a86eSKonstantin Belousov  * replacement library, or NULL.
403c1a0a86eSKonstantin Belousov  */
404c905e45dSPeter Wemm char *
405*78b64846SAlex Richardson lm_findn(const char *p, const char *f, const size_t n)
406c905e45dSPeter Wemm {
407c905e45dSPeter Wemm 	char pathbuf[64], *s, *t;
408c905e45dSPeter Wemm 
4098c6a035eSPeter Wemm 	if (n < sizeof(pathbuf) - 1)
410c905e45dSPeter Wemm 		s = pathbuf;
4118c6a035eSPeter Wemm 	else
412c905e45dSPeter Wemm 		s = xmalloc(n + 1);
4138c6a035eSPeter Wemm 	memcpy(s, f, n);
4148c6a035eSPeter Wemm 	s[n] = '\0';
415c905e45dSPeter Wemm 	t = lm_find(p, s);
416c905e45dSPeter Wemm 	if (s != pathbuf)
417c905e45dSPeter Wemm 		free(s);
418c905e45dSPeter Wemm 	return (t);
419c905e45dSPeter Wemm }
420c905e45dSPeter Wemm 
42129ade362SMatthew N. Dodd static char *
42229ade362SMatthew N. Dodd lml_find(struct lm_list *lmh, const char *f)
42329ade362SMatthew N. Dodd {
42429ade362SMatthew N. Dodd 	struct lm *lm;
42529ade362SMatthew N. Dodd 
4261aac1ed6SMatthew N. Dodd 	dbg("%s(%p, \"%s\")", __func__, lmh, f);
4271aac1ed6SMatthew N. Dodd 
428c1a0a86eSKonstantin Belousov 	TAILQ_FOREACH(lm, lmh, lm_link) {
429c905e45dSPeter Wemm 		if (strcmp(f, lm->f) == 0)
43029ade362SMatthew N. Dodd 			return (lm->t);
431c1a0a86eSKonstantin Belousov 	}
432d33da23fSMatthew N. Dodd 	return (NULL);
43329ade362SMatthew N. Dodd }
43429ade362SMatthew N. Dodd 
435c1a0a86eSKonstantin Belousov /*
436c1a0a86eSKonstantin Belousov  * Given an executable name, return a pointer to the translation list or
437c1a0a86eSKonstantin Belousov  * NULL if no matches.
438c1a0a86eSKonstantin Belousov  */
43929ade362SMatthew N. Dodd static struct lm_list *
44029ade362SMatthew N. Dodd lmp_find(const char *n)
44129ade362SMatthew N. Dodd {
44229ade362SMatthew N. Dodd 	struct lmp *lmp;
44329ade362SMatthew N. Dodd 
4441aac1ed6SMatthew N. Dodd 	dbg("%s(\"%s\")", __func__, n);
4451aac1ed6SMatthew N. Dodd 
446c1a0a86eSKonstantin Belousov 	TAILQ_FOREACH(lmp, &lmp_head, lmp_link) {
447966efcc7SMatthew N. Dodd 		if ((lmp->type == T_EXACT && strcmp(n, lmp->p) == 0) ||
448c1a0a86eSKonstantin Belousov 		    (lmp->type == T_DIRECTORY && strncmp(n, lmp->p,
449c1a0a86eSKonstantin Belousov 		    strlen(lmp->p)) == 0) ||
450c1a0a86eSKonstantin Belousov 		    (lmp->type == T_BASENAME && strcmp(quickbasename(n),
451c1a0a86eSKonstantin Belousov 		    lmp->p) == 0))
45229ade362SMatthew N. Dodd 			return (&lmp->lml);
453c1a0a86eSKonstantin Belousov 	}
45429ade362SMatthew N. Dodd 	return (NULL);
45529ade362SMatthew N. Dodd }
45629ade362SMatthew N. Dodd 
45729ade362SMatthew N. Dodd static struct lm_list *
45829ade362SMatthew N. Dodd lmp_init(char *n)
45929ade362SMatthew N. Dodd {
46029ade362SMatthew N. Dodd 	struct lmp *lmp;
46129ade362SMatthew N. Dodd 
4621aac1ed6SMatthew N. Dodd 	dbg("%s(\"%s\")", __func__, n);
4631aac1ed6SMatthew N. Dodd 
4643467e8b8SMatthew N. Dodd 	lmp = xmalloc(sizeof(struct lmp));
46529ade362SMatthew N. Dodd 	lmp->p = n;
466966efcc7SMatthew N. Dodd 	if (n[strlen(n) - 1] == '/')
467966efcc7SMatthew N. Dodd 		lmp->type = T_DIRECTORY;
468966efcc7SMatthew N. Dodd 	else if (strchr(n,'/') == NULL)
469966efcc7SMatthew N. Dodd 		lmp->type = T_BASENAME;
470966efcc7SMatthew N. Dodd 	else
471966efcc7SMatthew N. Dodd 		lmp->type = T_EXACT;
47229ade362SMatthew N. Dodd 	TAILQ_INIT(&lmp->lml);
47329ade362SMatthew N. Dodd 	TAILQ_INSERT_HEAD(&lmp_head, lmp, lmp_link);
47429ade362SMatthew N. Dodd 
47529ade362SMatthew N. Dodd 	return (&lmp->lml);
47629ade362SMatthew N. Dodd }
477966efcc7SMatthew N. Dodd 
478c1a0a86eSKonstantin Belousov /*
479c1a0a86eSKonstantin Belousov  * libc basename is overkill.  Return a pointer to the character after
480c1a0a86eSKonstantin Belousov  * the last /, or the original string if there are no slashes.
481c1a0a86eSKonstantin Belousov  */
482966efcc7SMatthew N. Dodd static const char *
483966efcc7SMatthew N. Dodd quickbasename(const char *path)
484966efcc7SMatthew N. Dodd {
485c1a0a86eSKonstantin Belousov 	const char *p;
486c1a0a86eSKonstantin Belousov 
487c1a0a86eSKonstantin Belousov 	for (p = path; *path != '\0'; path++) {
488966efcc7SMatthew N. Dodd 		if (*path == '/')
489966efcc7SMatthew N. Dodd 			p = path + 1;
490966efcc7SMatthew N. Dodd 	}
491d33da23fSMatthew N. Dodd 	return (p);
492966efcc7SMatthew N. Dodd }
493