xref: /freebsd-14.2/libexec/rtld-elf/libmap.c (revision a8b31c14)
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;
102faf66437SBaptiste Daroussin 	struct stat st;
103*a8b31c14SEdward Tomasz Napierala 	ssize_t retval;
104faf66437SBaptiste Daroussin 	int fd;
105faf66437SBaptiste Daroussin 	char *lm_map;
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 
132*a8b31c14SEdward Tomasz Napierala 	lm_map = xmalloc(st.st_size);
133*a8b31c14SEdward Tomasz Napierala 	retval = read(fd, lm_map, st.st_size);
134*a8b31c14SEdward Tomasz Napierala 	if (retval != st.st_size) {
135faf66437SBaptiste Daroussin 		close(fd);
136*a8b31c14SEdward Tomasz Napierala 		dbg("lm_parse_file: read(\"%s\") failed, %s", path,
137faf66437SBaptiste Daroussin 		    rtld_strerror(errno));
138faf66437SBaptiste Daroussin 		return;
139faf66437SBaptiste Daroussin 	}
140faf66437SBaptiste Daroussin 	close(fd);
141faf66437SBaptiste Daroussin 	p = xmalloc(sizeof(struct lmc));
1426b61e3e4SEdward Tomasz Napierala 	p->path = xstrdup(path);
1436b61e3e4SEdward Tomasz Napierala 	p->dev = st.st_dev;
1446b61e3e4SEdward Tomasz Napierala 	p->ino = st.st_ino;
145faf66437SBaptiste Daroussin 	TAILQ_INSERT_HEAD(&lmc_head, p, next);
146faf66437SBaptiste Daroussin 	lmc_parse(lm_map, st.st_size);
147*a8b31c14SEdward Tomasz Napierala 	free(lm_map);
148faf66437SBaptiste Daroussin }
149faf66437SBaptiste Daroussin 
150faf66437SBaptiste Daroussin static void
151faf66437SBaptiste Daroussin lmc_parse_dir(char *idir)
152faf66437SBaptiste Daroussin {
153faf66437SBaptiste Daroussin 	DIR *d;
154faf66437SBaptiste Daroussin 	struct dirent *dp;
155faf66437SBaptiste Daroussin 	struct lmc *p;
156faf66437SBaptiste Daroussin 	char conffile[MAXPATHLEN];
157faf66437SBaptiste Daroussin 	char *ext;
158faf66437SBaptiste Daroussin 
159faf66437SBaptiste Daroussin 	TAILQ_FOREACH(p, &lmc_head, next) {
1606b61e3e4SEdward Tomasz Napierala 		if (strcmp(p->path, idir) == 0)
161faf66437SBaptiste Daroussin 			return;
162faf66437SBaptiste Daroussin 	}
163faf66437SBaptiste Daroussin 	d = opendir(idir);
1646b61e3e4SEdward Tomasz Napierala 	if (d == NULL)
165faf66437SBaptiste Daroussin 		return;
166faf66437SBaptiste Daroussin 
167faf66437SBaptiste Daroussin 	p = xmalloc(sizeof(struct lmc));
1686b61e3e4SEdward Tomasz Napierala 	p->path = xstrdup(idir);
1696b61e3e4SEdward Tomasz Napierala 	p->dev = NODEV;
1706b61e3e4SEdward Tomasz Napierala 	p->ino = 0;
171faf66437SBaptiste Daroussin 	TAILQ_INSERT_HEAD(&lmc_head, p, next);
172faf66437SBaptiste Daroussin 
173faf66437SBaptiste Daroussin 	while ((dp = readdir(d)) != NULL) {
174faf66437SBaptiste Daroussin 		if (dp->d_ino == 0)
175faf66437SBaptiste Daroussin 			continue;
176faf66437SBaptiste Daroussin 		if (dp->d_type != DT_REG)
177faf66437SBaptiste Daroussin 			continue;
178faf66437SBaptiste Daroussin 		ext = strrchr(dp->d_name, '.');
179faf66437SBaptiste Daroussin 		if (ext == NULL)
180faf66437SBaptiste Daroussin 			continue;
181faf66437SBaptiste Daroussin 		if (strcmp(ext, ".conf") != 0)
182faf66437SBaptiste Daroussin 			continue;
183faf66437SBaptiste Daroussin 		if (strlcpy(conffile, idir, MAXPATHLEN) >= MAXPATHLEN)
184faf66437SBaptiste Daroussin 			continue; /* too long */
185faf66437SBaptiste Daroussin 		if (strlcat(conffile, "/", MAXPATHLEN) >= MAXPATHLEN)
186faf66437SBaptiste Daroussin 			continue; /* too long */
187faf66437SBaptiste Daroussin 		if (strlcat(conffile, dp->d_name, MAXPATHLEN) >= MAXPATHLEN)
188faf66437SBaptiste Daroussin 			continue; /* too long */
189faf66437SBaptiste Daroussin 		lmc_parse_file(conffile);
190faf66437SBaptiste Daroussin 	}
191faf66437SBaptiste Daroussin 	closedir(d);
192faf66437SBaptiste Daroussin }
193faf66437SBaptiste Daroussin 
194faf66437SBaptiste Daroussin static void
19568f1db20SKonstantin Belousov lmc_parse(char *lm_p, size_t lm_len)
1965b08cb04SMatthew N. Dodd {
19768f1db20SKonstantin Belousov 	char *cp, *f, *t, *c, *p;
1985b08cb04SMatthew N. Dodd 	char prog[MAXPATHLEN];
199faf66437SBaptiste Daroussin 	/* allow includedir + full length path */
200faf66437SBaptiste Daroussin 	char line[MAXPATHLEN + 13];
20168f1db20SKonstantin Belousov 	size_t cnt;
20268f1db20SKonstantin Belousov 	int 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 */
22444976acaSSergey Kandaurov 		while (rtld_isspace(*cp)) cp++;
225623b6bd2SMatthew N. Dodd 
22629ade362SMatthew N. Dodd 		/* Found a comment or EOL */
227486089f0SAlexander Kabaev 		if (iseol(*cp)) continue;
228623b6bd2SMatthew N. Dodd 
229623b6bd2SMatthew N. Dodd 		/* Found a constraint selector */
23029ade362SMatthew N. Dodd 		if (*cp == '[') {
23129ade362SMatthew N. Dodd 			cp++;
232623b6bd2SMatthew N. Dodd 
23329ade362SMatthew N. Dodd 			/* Skip leading space */
23444976acaSSergey Kandaurov 			while (rtld_isspace(*cp)) cp++;
235623b6bd2SMatthew N. Dodd 
23629ade362SMatthew N. Dodd 			/* Found comment, EOL or end of selector */
237486089f0SAlexander Kabaev 			if  (iseol(*cp) || *cp == ']')
238486089f0SAlexander Kabaev 				continue;
239623b6bd2SMatthew N. Dodd 
2401340fc10SMatthew N. Dodd 			c = cp++;
24129ade362SMatthew N. Dodd 			/* Skip to end of word */
24244976acaSSergey Kandaurov 			while (!rtld_isspace(*cp) && !iseol(*cp) && *cp != ']')
243486089f0SAlexander Kabaev 				cp++;
244623b6bd2SMatthew N. Dodd 
245623b6bd2SMatthew N. Dodd 			/* Skip and zero out trailing space */
24644976acaSSergey Kandaurov 			while (rtld_isspace(*cp)) *cp++ = '\0';
247623b6bd2SMatthew N. Dodd 
248623b6bd2SMatthew N. Dodd 			/* Check if there is a closing brace */
249486089f0SAlexander Kabaev 			if (*cp != ']') continue;
250623b6bd2SMatthew N. Dodd 
251623b6bd2SMatthew N. Dodd 			/* Terminate string if there was no trailing space */
25229ade362SMatthew N. Dodd 			*cp++ = '\0';
253623b6bd2SMatthew N. Dodd 
254623b6bd2SMatthew N. Dodd 			/*
255623b6bd2SMatthew N. Dodd 			 * There should be nothing except whitespace or comment
2566d5d786fSAlexander Kabaev 			  from this point to the end of the line.
257623b6bd2SMatthew N. Dodd 			 */
25844976acaSSergey Kandaurov 			while(rtld_isspace(*cp)) cp++;
259486089f0SAlexander Kabaev 			if (!iseol(*cp)) continue;
260623b6bd2SMatthew N. Dodd 
261faf66437SBaptiste Daroussin 			if (strlcpy(prog, c, sizeof prog) >= sizeof prog)
262faf66437SBaptiste Daroussin 				continue;
26329ade362SMatthew N. Dodd 			p = prog;
264486089f0SAlexander Kabaev 			continue;
265623b6bd2SMatthew N. Dodd 		}
266623b6bd2SMatthew N. Dodd 
267623b6bd2SMatthew N. Dodd 		/* Parse the 'from' candidate. */
268486089f0SAlexander Kabaev 		f = cp++;
26944976acaSSergey Kandaurov 		while (!rtld_isspace(*cp) && !iseol(*cp)) cp++;
27029ade362SMatthew N. Dodd 
271623b6bd2SMatthew N. Dodd 		/* Skip and zero out the trailing whitespace */
27244976acaSSergey Kandaurov 		while (rtld_isspace(*cp)) *cp++ = '\0';
273623b6bd2SMatthew N. Dodd 
274623b6bd2SMatthew N. Dodd 		/* Found a comment or EOL */
275486089f0SAlexander Kabaev 		if (iseol(*cp)) continue;
276623b6bd2SMatthew N. Dodd 
277623b6bd2SMatthew N. Dodd 		/* Parse 'to' mapping */
278486089f0SAlexander Kabaev 		t = cp++;
27944976acaSSergey Kandaurov 		while (!rtld_isspace(*cp) && !iseol(*cp)) cp++;
280623b6bd2SMatthew N. Dodd 
281486089f0SAlexander Kabaev 		/* Skip and zero out the trailing whitespace */
28244976acaSSergey Kandaurov 		while (rtld_isspace(*cp)) *cp++ = '\0';
283486089f0SAlexander Kabaev 
284486089f0SAlexander Kabaev 		/* Should be no extra tokens at this point */
285486089f0SAlexander Kabaev 		if (!iseol(*cp)) continue;
286486089f0SAlexander Kabaev 
287486089f0SAlexander Kabaev 		*cp = '\0';
288faf66437SBaptiste Daroussin 		if (strcmp(f, "includedir") == 0)
289faf66437SBaptiste Daroussin 			lmc_parse_dir(t);
290faf66437SBaptiste Daroussin 		else if (strcmp(f, "include") == 0)
291faf66437SBaptiste Daroussin 			lmc_parse_file(t);
292faf66437SBaptiste Daroussin 		else
2931340fc10SMatthew N. Dodd 			lm_add(p, f, t);
29429ade362SMatthew N. Dodd 	}
29529ade362SMatthew N. Dodd }
29629ade362SMatthew N. Dodd 
29729ade362SMatthew N. Dodd static void
29829ade362SMatthew N. Dodd lm_free (struct lm_list *lml)
29929ade362SMatthew N. Dodd {
30029ade362SMatthew N. Dodd 	struct lm *lm;
30129ade362SMatthew N. Dodd 
3021aac1ed6SMatthew N. Dodd 	dbg("%s(%p)", __func__, lml);
3031aac1ed6SMatthew N. Dodd 
30429ade362SMatthew N. Dodd 	while (!TAILQ_EMPTY(lml)) {
30529ade362SMatthew N. Dodd 		lm = TAILQ_FIRST(lml);
30629ade362SMatthew N. Dodd 		TAILQ_REMOVE(lml, lm, lm_link);
30729ade362SMatthew N. Dodd 		free(lm->f);
30829ade362SMatthew N. Dodd 		free(lm->t);
30929ade362SMatthew N. Dodd 		free(lm);
31029ade362SMatthew N. Dodd 	}
31129ade362SMatthew N. Dodd 	return;
31229ade362SMatthew N. Dodd }
31329ade362SMatthew N. Dodd 
31429ade362SMatthew N. Dodd void
31529ade362SMatthew N. Dodd lm_fini (void)
31629ade362SMatthew N. Dodd {
31729ade362SMatthew N. Dodd 	struct lmp *lmp;
318faf66437SBaptiste Daroussin 	struct lmc *p;
31929ade362SMatthew N. Dodd 
3201aac1ed6SMatthew N. Dodd 	dbg("%s()", __func__);
3211aac1ed6SMatthew N. Dodd 
322faf66437SBaptiste Daroussin 	while (!TAILQ_EMPTY(&lmc_head)) {
323faf66437SBaptiste Daroussin 		p = TAILQ_FIRST(&lmc_head);
324faf66437SBaptiste Daroussin 		TAILQ_REMOVE(&lmc_head, p, next);
325faf66437SBaptiste Daroussin 		free(p->path);
326faf66437SBaptiste Daroussin 		free(p);
327faf66437SBaptiste Daroussin 	}
328faf66437SBaptiste Daroussin 
32929ade362SMatthew N. Dodd 	while (!TAILQ_EMPTY(&lmp_head)) {
33029ade362SMatthew N. Dodd 		lmp = TAILQ_FIRST(&lmp_head);
33129ade362SMatthew N. Dodd 		TAILQ_REMOVE(&lmp_head, lmp, lmp_link);
33229ade362SMatthew N. Dodd 		free(lmp->p);
33329ade362SMatthew N. Dodd 		lm_free(&lmp->lml);
33429ade362SMatthew N. Dodd 		free(lmp);
33529ade362SMatthew N. Dodd 	}
33629ade362SMatthew N. Dodd 	return;
33729ade362SMatthew N. Dodd }
33829ade362SMatthew N. Dodd 
33929ade362SMatthew N. Dodd static void
3401340fc10SMatthew N. Dodd lm_add (const char *p, const char *f, const char *t)
34129ade362SMatthew N. Dodd {
34229ade362SMatthew N. Dodd 	struct lm_list *lml;
34329ade362SMatthew N. Dodd 	struct lm *lm;
34429ade362SMatthew N. Dodd 
34529ade362SMatthew N. Dodd 	if (p == NULL)
34629ade362SMatthew N. Dodd 		p = "$DEFAULT$";
34729ade362SMatthew N. Dodd 
3481aac1ed6SMatthew N. Dodd 	dbg("%s(\"%s\", \"%s\", \"%s\")", __func__, p, f, t);
3491aac1ed6SMatthew N. Dodd 
35029ade362SMatthew N. Dodd 	if ((lml = lmp_find(p)) == NULL)
3513467e8b8SMatthew N. Dodd 		lml = lmp_init(xstrdup(p));
35229ade362SMatthew N. Dodd 
3533467e8b8SMatthew N. Dodd 	lm = xmalloc(sizeof(struct lm));
3541340fc10SMatthew N. Dodd 	lm->f = xstrdup(f);
3551340fc10SMatthew N. Dodd 	lm->t = xstrdup(t);
35629ade362SMatthew N. Dodd 	TAILQ_INSERT_HEAD(lml, lm, lm_link);
3575b08cb04SMatthew N. Dodd 	lm_count++;
35829ade362SMatthew N. Dodd }
35929ade362SMatthew N. Dodd 
36029ade362SMatthew N. Dodd char *
36129ade362SMatthew N. Dodd lm_find (const char *p, const char *f)
36229ade362SMatthew N. Dodd {
36329ade362SMatthew N. Dodd 	struct lm_list *lml;
36429ade362SMatthew N. Dodd 	char *t;
36529ade362SMatthew N. Dodd 
3661aac1ed6SMatthew N. Dodd 	dbg("%s(\"%s\", \"%s\")", __func__, p, f);
3671aac1ed6SMatthew N. Dodd 
36829ade362SMatthew N. Dodd 	if (p != NULL && (lml = lmp_find(p)) != NULL) {
36929ade362SMatthew N. Dodd 		t = lml_find(lml, f);
3703467e8b8SMatthew N. Dodd 		if (t != NULL) {
3713467e8b8SMatthew N. Dodd 			/*
3723467e8b8SMatthew N. Dodd 			 * Add a global mapping if we have
3733467e8b8SMatthew N. Dodd 			 * a successful constrained match.
3743467e8b8SMatthew N. Dodd 			 */
3751340fc10SMatthew N. Dodd 			lm_add(NULL, f, t);
37629ade362SMatthew N. Dodd 			return (t);
37729ade362SMatthew N. Dodd 		}
3783467e8b8SMatthew N. Dodd 	}
37929ade362SMatthew N. Dodd 	lml = lmp_find("$DEFAULT$");
38029ade362SMatthew N. Dodd 	if (lml != NULL)
38129ade362SMatthew N. Dodd 		return (lml_find(lml, f));
38229ade362SMatthew N. Dodd 	else
38329ade362SMatthew N. Dodd 		return (NULL);
38429ade362SMatthew N. Dodd }
38529ade362SMatthew N. Dodd 
386966efcc7SMatthew N. Dodd /* Given a libmap translation list and a library name, return the
387966efcc7SMatthew N. Dodd    replacement library, or NULL */
388c905e45dSPeter Wemm char *
389c905e45dSPeter Wemm lm_findn (const char *p, const char *f, const int n)
390c905e45dSPeter Wemm {
391c905e45dSPeter Wemm 	char pathbuf[64], *s, *t;
392c905e45dSPeter Wemm 
3938c6a035eSPeter Wemm 	if (n < sizeof(pathbuf) - 1)
394c905e45dSPeter Wemm 		s = pathbuf;
3958c6a035eSPeter Wemm 	else
396c905e45dSPeter Wemm 		s = xmalloc(n + 1);
3978c6a035eSPeter Wemm 	memcpy(s, f, n);
3988c6a035eSPeter Wemm 	s[n] = '\0';
399c905e45dSPeter Wemm 	t = lm_find(p, s);
400c905e45dSPeter Wemm 	if (s != pathbuf)
401c905e45dSPeter Wemm 		free(s);
402c905e45dSPeter Wemm 	return (t);
403c905e45dSPeter Wemm }
404c905e45dSPeter Wemm 
40529ade362SMatthew N. Dodd static char *
40629ade362SMatthew N. Dodd lml_find (struct lm_list *lmh, const char *f)
40729ade362SMatthew N. Dodd {
40829ade362SMatthew N. Dodd 	struct lm *lm;
40929ade362SMatthew N. Dodd 
4101aac1ed6SMatthew N. Dodd 	dbg("%s(%p, \"%s\")", __func__, lmh, f);
4111aac1ed6SMatthew N. Dodd 
41229ade362SMatthew N. Dodd 	TAILQ_FOREACH(lm, lmh, lm_link)
413c905e45dSPeter Wemm 		if (strcmp(f, lm->f) == 0)
41429ade362SMatthew N. Dodd 			return (lm->t);
415d33da23fSMatthew N. Dodd 	return (NULL);
41629ade362SMatthew N. Dodd }
41729ade362SMatthew N. Dodd 
418966efcc7SMatthew N. Dodd /* Given an executable name, return a pointer to the translation list or
419966efcc7SMatthew N. Dodd    NULL if no matches */
42029ade362SMatthew N. Dodd static struct lm_list *
42129ade362SMatthew N. Dodd lmp_find (const char *n)
42229ade362SMatthew N. Dodd {
42329ade362SMatthew N. Dodd 	struct lmp *lmp;
42429ade362SMatthew N. Dodd 
4251aac1ed6SMatthew N. Dodd 	dbg("%s(\"%s\")", __func__, n);
4261aac1ed6SMatthew N. Dodd 
42729ade362SMatthew N. Dodd 	TAILQ_FOREACH(lmp, &lmp_head, lmp_link)
428966efcc7SMatthew N. Dodd 		if ((lmp->type == T_EXACT && strcmp(n, lmp->p) == 0) ||
429966efcc7SMatthew N. Dodd 		    (lmp->type == T_DIRECTORY && strncmp(n, lmp->p, strlen(lmp->p)) == 0) ||
430966efcc7SMatthew N. Dodd 		    (lmp->type == T_BASENAME && strcmp(quickbasename(n), lmp->p) == 0))
43129ade362SMatthew N. Dodd 			return (&lmp->lml);
43229ade362SMatthew N. Dodd 	return (NULL);
43329ade362SMatthew N. Dodd }
43429ade362SMatthew N. Dodd 
43529ade362SMatthew N. Dodd static struct lm_list *
43629ade362SMatthew N. Dodd lmp_init (char *n)
43729ade362SMatthew N. Dodd {
43829ade362SMatthew N. Dodd 	struct lmp *lmp;
43929ade362SMatthew N. Dodd 
4401aac1ed6SMatthew N. Dodd 	dbg("%s(\"%s\")", __func__, n);
4411aac1ed6SMatthew N. Dodd 
4423467e8b8SMatthew N. Dodd 	lmp = xmalloc(sizeof(struct lmp));
44329ade362SMatthew N. Dodd 	lmp->p = n;
444966efcc7SMatthew N. Dodd 	if (n[strlen(n)-1] == '/')
445966efcc7SMatthew N. Dodd 		lmp->type = T_DIRECTORY;
446966efcc7SMatthew N. Dodd 	else if (strchr(n,'/') == NULL)
447966efcc7SMatthew N. Dodd 		lmp->type = T_BASENAME;
448966efcc7SMatthew N. Dodd 	else
449966efcc7SMatthew N. Dodd 		lmp->type = T_EXACT;
45029ade362SMatthew N. Dodd 	TAILQ_INIT(&lmp->lml);
45129ade362SMatthew N. Dodd 	TAILQ_INSERT_HEAD(&lmp_head, lmp, lmp_link);
45229ade362SMatthew N. Dodd 
45329ade362SMatthew N. Dodd 	return (&lmp->lml);
45429ade362SMatthew N. Dodd }
455966efcc7SMatthew N. Dodd 
456966efcc7SMatthew N. Dodd /* libc basename is overkill.  Return a pointer to the character after the
457966efcc7SMatthew N. Dodd    last /, or the original string if there are no slashes. */
458966efcc7SMatthew N. Dodd static const char *
459966efcc7SMatthew N. Dodd quickbasename (const char *path)
460966efcc7SMatthew N. Dodd {
461966efcc7SMatthew N. Dodd 	const char *p = path;
462d33da23fSMatthew N. Dodd 	for (; *path; path++) {
463966efcc7SMatthew N. Dodd 		if (*path == '/')
464966efcc7SMatthew N. Dodd 			p = path+1;
465966efcc7SMatthew N. Dodd 	}
466d33da23fSMatthew N. Dodd 	return (p);
467966efcc7SMatthew N. Dodd }
468