xref: /freebsd/libexec/rtld-elf/libmap.c (revision 42b388439bd3795e09258c57a74ce9eec3651c7b)
129ade362SMatthew N. Dodd /*
229ade362SMatthew N. Dodd  */
329ade362SMatthew N. Dodd 
4faf66437SBaptiste Daroussin #include <sys/types.h>
529ade362SMatthew N. Dodd #include <sys/param.h>
668f1db20SKonstantin Belousov #include <sys/fcntl.h>
768f1db20SKonstantin Belousov #include <sys/mman.h>
868f1db20SKonstantin Belousov #include <sys/queue.h>
968f1db20SKonstantin Belousov #include <sys/stat.h>
10faf66437SBaptiste Daroussin #include <dirent.h>
1168f1db20SKonstantin Belousov #include <errno.h>
1268f1db20SKonstantin Belousov #include <stdlib.h>
1368f1db20SKonstantin Belousov #include <string.h>
1429ade362SMatthew N. Dodd 
153467e8b8SMatthew N. Dodd #include "debug.h"
163467e8b8SMatthew N. Dodd #include "rtld.h"
177227105fSMatthew N. Dodd #include "libmap.h"
1833dba3bbSKonstantin Belousov #include "rtld_paths.h"
19b54a59f3SAlex Richardson #include "rtld_libc.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 
283ab5b6bdSAlex Richardson static 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);
47903e0ffdSAlex Richardson static void lmc_parse_file(const char *);
48903e0ffdSAlex Richardson static void lmc_parse_dir(const 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 /*
601251cf8aSKonstantin Belousov  * Do not use ctype.h macros, which rely on working TLS.  Rtld does
611251cf8aSKonstantin Belousov  * not support TLS for itself.
62b36070f5SKonstantin Belousov  */
6344976acaSSergey Kandaurov #define	rtld_isspace(c)	((c) == ' ' || (c) == '\t')
64b36070f5SKonstantin Belousov 
654402996dSMatthew N. Dodd int
lm_init(const char * libmap_override)66aa68b3bbSKonstantin Belousov lm_init(const char *libmap_override)
6729ade362SMatthew N. Dodd {
68aa68b3bbSKonstantin Belousov 	char *l, *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 
75aa68b3bbSKonstantin Belousov 	if (libmap_override != NULL) {
7668f1db20SKonstantin Belousov 		/*
77*ca2560bdSKonstantin Belousov 		 * Do some character replacement to make $LD_LIBMAP look
7868f1db20SKonstantin Belousov 		 * like a text file, then parse it.
7968f1db20SKonstantin Belousov 		 */
80aa68b3bbSKonstantin Belousov 		l = xstrdup(libmap_override);
81aa68b3bbSKonstantin Belousov 		for (p = l; *p != 0; 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 		}
91aa68b3bbSKonstantin Belousov 		lmc_parse(l, p - l);
92aa68b3bbSKonstantin Belousov 		free(l);
935b08cb04SMatthew N. Dodd 	}
945b08cb04SMatthew N. Dodd 
955b08cb04SMatthew N. Dodd 	return (lm_count == 0);
965b08cb04SMatthew N. Dodd }
975b08cb04SMatthew N. Dodd 
985b08cb04SMatthew N. Dodd static void
lmc_parse_file(const char * path)99903e0ffdSAlex Richardson lmc_parse_file(const 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;
105741d7812SKonstantin Belousov 	int fd, saved_errno;
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) {
1196b61e3e4SEdward Tomasz Napierala 		dbg("lm_parse_file: fstat(\"%s\") failed, %s", path,
120faf66437SBaptiste Daroussin 		    rtld_strerror(errno));
121741d7812SKonstantin Belousov 		close(fd);
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);
134741d7812SKonstantin Belousov 	saved_errno = errno;
135faf66437SBaptiste Daroussin 	close(fd);
136741d7812SKonstantin Belousov 	if (retval != st.st_size) {
137741d7812SKonstantin Belousov 		if (retval == -1) {
138a8b31c14SEdward Tomasz Napierala 			dbg("lm_parse_file: read(\"%s\") failed, %s", path,
139741d7812SKonstantin Belousov 			    rtld_strerror(saved_errno));
140741d7812SKonstantin Belousov 		} else {
141741d7812SKonstantin Belousov 			dbg("lm_parse_file: short read(\"%s\"), %zd vs %jd",
142741d7812SKonstantin Belousov 			    path, retval, (uintmax_t)st.st_size);
143741d7812SKonstantin Belousov 		}
144741d7812SKonstantin Belousov 		free(lm_map);
145faf66437SBaptiste Daroussin 		return;
146faf66437SBaptiste Daroussin 	}
147faf66437SBaptiste Daroussin 	p = xmalloc(sizeof(struct lmc));
1486b61e3e4SEdward Tomasz Napierala 	p->path = xstrdup(path);
1496b61e3e4SEdward Tomasz Napierala 	p->dev = st.st_dev;
1506b61e3e4SEdward Tomasz Napierala 	p->ino = st.st_ino;
151faf66437SBaptiste Daroussin 	TAILQ_INSERT_HEAD(&lmc_head, p, next);
152faf66437SBaptiste Daroussin 	lmc_parse(lm_map, st.st_size);
153a8b31c14SEdward Tomasz Napierala 	free(lm_map);
154faf66437SBaptiste Daroussin }
155faf66437SBaptiste Daroussin 
156faf66437SBaptiste Daroussin static void
lmc_parse_dir(const char * idir)157903e0ffdSAlex Richardson lmc_parse_dir(const char *idir)
158faf66437SBaptiste Daroussin {
159faf66437SBaptiste Daroussin 	DIR *d;
160faf66437SBaptiste Daroussin 	struct dirent *dp;
161faf66437SBaptiste Daroussin 	struct lmc *p;
162faf66437SBaptiste Daroussin 	char conffile[MAXPATHLEN];
163faf66437SBaptiste Daroussin 	char *ext;
164faf66437SBaptiste Daroussin 
165faf66437SBaptiste Daroussin 	TAILQ_FOREACH(p, &lmc_head, next) {
1666b61e3e4SEdward Tomasz Napierala 		if (strcmp(p->path, idir) == 0)
167faf66437SBaptiste Daroussin 			return;
168faf66437SBaptiste Daroussin 	}
169faf66437SBaptiste Daroussin 	d = opendir(idir);
1706b61e3e4SEdward Tomasz Napierala 	if (d == NULL)
171faf66437SBaptiste Daroussin 		return;
172faf66437SBaptiste Daroussin 
173faf66437SBaptiste Daroussin 	p = xmalloc(sizeof(struct lmc));
1746b61e3e4SEdward Tomasz Napierala 	p->path = xstrdup(idir);
1756b61e3e4SEdward Tomasz Napierala 	p->dev = NODEV;
1766b61e3e4SEdward Tomasz Napierala 	p->ino = 0;
177faf66437SBaptiste Daroussin 	TAILQ_INSERT_HEAD(&lmc_head, p, next);
178faf66437SBaptiste Daroussin 
179faf66437SBaptiste Daroussin 	while ((dp = readdir(d)) != NULL) {
180faf66437SBaptiste Daroussin 		if (dp->d_ino == 0)
181faf66437SBaptiste Daroussin 			continue;
182faf66437SBaptiste Daroussin 		if (dp->d_type != DT_REG)
183faf66437SBaptiste Daroussin 			continue;
184faf66437SBaptiste Daroussin 		ext = strrchr(dp->d_name, '.');
185faf66437SBaptiste Daroussin 		if (ext == NULL)
186faf66437SBaptiste Daroussin 			continue;
187faf66437SBaptiste Daroussin 		if (strcmp(ext, ".conf") != 0)
188faf66437SBaptiste Daroussin 			continue;
189faf66437SBaptiste Daroussin 		if (strlcpy(conffile, idir, MAXPATHLEN) >= MAXPATHLEN)
190faf66437SBaptiste Daroussin 			continue; /* too long */
191faf66437SBaptiste Daroussin 		if (strlcat(conffile, "/", MAXPATHLEN) >= MAXPATHLEN)
192faf66437SBaptiste Daroussin 			continue; /* too long */
193faf66437SBaptiste Daroussin 		if (strlcat(conffile, dp->d_name, MAXPATHLEN) >= MAXPATHLEN)
194faf66437SBaptiste Daroussin 			continue; /* too long */
195faf66437SBaptiste Daroussin 		lmc_parse_file(conffile);
196faf66437SBaptiste Daroussin 	}
197faf66437SBaptiste Daroussin 	closedir(d);
198faf66437SBaptiste Daroussin }
199faf66437SBaptiste Daroussin 
200faf66437SBaptiste Daroussin static void
lmc_parse(char * lm_p,size_t lm_len)20168f1db20SKonstantin Belousov lmc_parse(char *lm_p, size_t lm_len)
2025b08cb04SMatthew N. Dodd {
20368f1db20SKonstantin Belousov 	char *cp, *f, *t, *c, *p;
2045b08cb04SMatthew N. Dodd 	char prog[MAXPATHLEN];
205faf66437SBaptiste Daroussin 	/* allow includedir + full length path */
206faf66437SBaptiste Daroussin 	char line[MAXPATHLEN + 13];
20778b64846SAlex Richardson 	size_t cnt, i;
2085b08cb04SMatthew N. Dodd 
20968f1db20SKonstantin Belousov 	cnt = 0;
21029ade362SMatthew N. Dodd 	p = NULL;
21168f1db20SKonstantin Belousov 	while (cnt < lm_len) {
21268f1db20SKonstantin Belousov 		i = 0;
2130fa46a42SPedro F. Giffuni 		while (cnt < lm_len && lm_p[cnt] != '\n' &&
21468f1db20SKonstantin Belousov 		    i < sizeof(line) - 1) {
21568f1db20SKonstantin Belousov 			line[i] = lm_p[cnt];
21668f1db20SKonstantin Belousov 			cnt++;
21768f1db20SKonstantin Belousov 			i++;
21868f1db20SKonstantin Belousov 		}
21968f1db20SKonstantin Belousov 		line[i] = '\0';
2200fa46a42SPedro F. Giffuni 		while (cnt < lm_len && lm_p[cnt] != '\n')
22168f1db20SKonstantin Belousov 			cnt++;
22268f1db20SKonstantin Belousov 		/* skip over nl */
22368f1db20SKonstantin Belousov 		cnt++;
22468f1db20SKonstantin Belousov 
22568f1db20SKonstantin Belousov 		cp = &line[0];
2261340fc10SMatthew N. Dodd 		t = f = c = NULL;
227623b6bd2SMatthew N. Dodd 
22829ade362SMatthew N. Dodd 		/* Skip over leading space */
229c1a0a86eSKonstantin Belousov 		while (rtld_isspace(*cp))
230c1a0a86eSKonstantin Belousov 			cp++;
231623b6bd2SMatthew N. Dodd 
23229ade362SMatthew N. Dodd 		/* Found a comment or EOL */
233c1a0a86eSKonstantin Belousov 		if (iseol(*cp))
234c1a0a86eSKonstantin Belousov 			continue;
235623b6bd2SMatthew N. Dodd 
236623b6bd2SMatthew N. Dodd 		/* Found a constraint selector */
23729ade362SMatthew N. Dodd 		if (*cp == '[') {
23829ade362SMatthew N. Dodd 			cp++;
239623b6bd2SMatthew N. Dodd 
24029ade362SMatthew N. Dodd 			/* Skip leading space */
241c1a0a86eSKonstantin Belousov 			while (rtld_isspace(*cp))
242c1a0a86eSKonstantin Belousov 				cp++;
243623b6bd2SMatthew N. Dodd 
24429ade362SMatthew N. Dodd 			/* Found comment, EOL or end of selector */
245486089f0SAlexander Kabaev 			if  (iseol(*cp) || *cp == ']')
246486089f0SAlexander Kabaev 				continue;
247623b6bd2SMatthew N. Dodd 
2481340fc10SMatthew N. Dodd 			c = cp++;
24929ade362SMatthew N. Dodd 			/* Skip to end of word */
25044976acaSSergey Kandaurov 			while (!rtld_isspace(*cp) && !iseol(*cp) && *cp != ']')
251486089f0SAlexander Kabaev 				cp++;
252623b6bd2SMatthew N. Dodd 
253623b6bd2SMatthew N. Dodd 			/* Skip and zero out trailing space */
254c1a0a86eSKonstantin Belousov 			while (rtld_isspace(*cp))
255c1a0a86eSKonstantin Belousov 				*cp++ = '\0';
256623b6bd2SMatthew N. Dodd 
257623b6bd2SMatthew N. Dodd 			/* Check if there is a closing brace */
258c1a0a86eSKonstantin Belousov 			if (*cp != ']')
259c1a0a86eSKonstantin Belousov 				continue;
260623b6bd2SMatthew N. Dodd 
261623b6bd2SMatthew N. Dodd 			/* Terminate string if there was no trailing space */
26229ade362SMatthew N. Dodd 			*cp++ = '\0';
263623b6bd2SMatthew N. Dodd 
264623b6bd2SMatthew N. Dodd 			/*
265623b6bd2SMatthew N. Dodd 			 * There should be nothing except whitespace or comment
2666d5d786fSAlexander Kabaev 			  from this point to the end of the line.
267623b6bd2SMatthew N. Dodd 			 */
268c1a0a86eSKonstantin Belousov 			while (rtld_isspace(*cp))
269c1a0a86eSKonstantin Belousov 				cp++;
270c1a0a86eSKonstantin Belousov 			if (!iseol(*cp))
271c1a0a86eSKonstantin Belousov 				continue;
272623b6bd2SMatthew N. Dodd 
273faf66437SBaptiste Daroussin 			if (strlcpy(prog, c, sizeof prog) >= sizeof prog)
274faf66437SBaptiste Daroussin 				continue;
27529ade362SMatthew N. Dodd 			p = prog;
276486089f0SAlexander Kabaev 			continue;
277623b6bd2SMatthew N. Dodd 		}
278623b6bd2SMatthew N. Dodd 
279623b6bd2SMatthew N. Dodd 		/* Parse the 'from' candidate. */
280486089f0SAlexander Kabaev 		f = cp++;
281c1a0a86eSKonstantin Belousov 		while (!rtld_isspace(*cp) && !iseol(*cp))
282c1a0a86eSKonstantin Belousov 			cp++;
28329ade362SMatthew N. Dodd 
284623b6bd2SMatthew N. Dodd 		/* Skip and zero out the trailing whitespace */
285c1a0a86eSKonstantin Belousov 		while (rtld_isspace(*cp))
286c1a0a86eSKonstantin Belousov 			*cp++ = '\0';
287623b6bd2SMatthew N. Dodd 
288623b6bd2SMatthew N. Dodd 		/* Found a comment or EOL */
289c1a0a86eSKonstantin Belousov 		if (iseol(*cp))
290c1a0a86eSKonstantin Belousov 			continue;
291623b6bd2SMatthew N. Dodd 
292623b6bd2SMatthew N. Dodd 		/* Parse 'to' mapping */
293486089f0SAlexander Kabaev 		t = cp++;
294c1a0a86eSKonstantin Belousov 		while (!rtld_isspace(*cp) && !iseol(*cp))
295c1a0a86eSKonstantin Belousov 			cp++;
296623b6bd2SMatthew N. Dodd 
297486089f0SAlexander Kabaev 		/* Skip and zero out the trailing whitespace */
298c1a0a86eSKonstantin Belousov 		while (rtld_isspace(*cp))
299c1a0a86eSKonstantin Belousov 			*cp++ = '\0';
300486089f0SAlexander Kabaev 
301486089f0SAlexander Kabaev 		/* Should be no extra tokens at this point */
302c1a0a86eSKonstantin Belousov 		if (!iseol(*cp))
303c1a0a86eSKonstantin Belousov 			continue;
304486089f0SAlexander Kabaev 
305486089f0SAlexander Kabaev 		*cp = '\0';
306faf66437SBaptiste Daroussin 		if (strcmp(f, "includedir") == 0)
307faf66437SBaptiste Daroussin 			lmc_parse_dir(t);
308faf66437SBaptiste Daroussin 		else if (strcmp(f, "include") == 0)
309faf66437SBaptiste Daroussin 			lmc_parse_file(t);
310faf66437SBaptiste Daroussin 		else
3111340fc10SMatthew N. Dodd 			lm_add(p, f, t);
31229ade362SMatthew N. Dodd 	}
31329ade362SMatthew N. Dodd }
31429ade362SMatthew N. Dodd 
31529ade362SMatthew N. Dodd static void
lm_free(struct lm_list * lml)31629ade362SMatthew N. Dodd lm_free(struct lm_list *lml)
31729ade362SMatthew N. Dodd {
31829ade362SMatthew N. Dodd 	struct lm *lm;
31929ade362SMatthew N. Dodd 
3201aac1ed6SMatthew N. Dodd 	dbg("%s(%p)", __func__, lml);
3211aac1ed6SMatthew N. Dodd 
32229ade362SMatthew N. Dodd 	while (!TAILQ_EMPTY(lml)) {
32329ade362SMatthew N. Dodd 		lm = TAILQ_FIRST(lml);
32429ade362SMatthew N. Dodd 		TAILQ_REMOVE(lml, lm, lm_link);
32529ade362SMatthew N. Dodd 		free(lm->f);
32629ade362SMatthew N. Dodd 		free(lm->t);
32729ade362SMatthew N. Dodd 		free(lm);
32829ade362SMatthew N. Dodd 	}
32929ade362SMatthew N. Dodd }
33029ade362SMatthew N. Dodd 
33129ade362SMatthew N. Dodd void
lm_fini(void)33229ade362SMatthew N. Dodd lm_fini(void)
33329ade362SMatthew N. Dodd {
33429ade362SMatthew N. Dodd 	struct lmp *lmp;
335faf66437SBaptiste Daroussin 	struct lmc *p;
33629ade362SMatthew N. Dodd 
3371aac1ed6SMatthew N. Dodd 	dbg("%s()", __func__);
3381aac1ed6SMatthew N. Dodd 
339faf66437SBaptiste Daroussin 	while (!TAILQ_EMPTY(&lmc_head)) {
340faf66437SBaptiste Daroussin 		p = TAILQ_FIRST(&lmc_head);
341faf66437SBaptiste Daroussin 		TAILQ_REMOVE(&lmc_head, p, next);
342faf66437SBaptiste Daroussin 		free(p->path);
343faf66437SBaptiste Daroussin 		free(p);
344faf66437SBaptiste Daroussin 	}
345faf66437SBaptiste Daroussin 
34629ade362SMatthew N. Dodd 	while (!TAILQ_EMPTY(&lmp_head)) {
34729ade362SMatthew N. Dodd 		lmp = TAILQ_FIRST(&lmp_head);
34829ade362SMatthew N. Dodd 		TAILQ_REMOVE(&lmp_head, lmp, lmp_link);
34929ade362SMatthew N. Dodd 		free(lmp->p);
35029ade362SMatthew N. Dodd 		lm_free(&lmp->lml);
35129ade362SMatthew N. Dodd 		free(lmp);
35229ade362SMatthew N. Dodd 	}
35329ade362SMatthew N. Dodd }
35429ade362SMatthew N. Dodd 
35529ade362SMatthew N. Dodd static void
lm_add(const char * p,const char * f,const char * t)3561340fc10SMatthew N. Dodd lm_add(const char *p, const char *f, const char *t)
35729ade362SMatthew N. Dodd {
35829ade362SMatthew N. Dodd 	struct lm_list *lml;
35929ade362SMatthew N. Dodd 	struct lm *lm;
3606c306765SKonstantin Belousov 	const char *t1;
36129ade362SMatthew N. Dodd 
36229ade362SMatthew N. Dodd 	if (p == NULL)
36329ade362SMatthew N. Dodd 		p = "$DEFAULT$";
36429ade362SMatthew N. Dodd 
3651aac1ed6SMatthew N. Dodd 	dbg("%s(\"%s\", \"%s\", \"%s\")", __func__, p, f, t);
3661aac1ed6SMatthew N. Dodd 
36729ade362SMatthew N. Dodd 	if ((lml = lmp_find(p)) == NULL)
3683467e8b8SMatthew N. Dodd 		lml = lmp_init(xstrdup(p));
36929ade362SMatthew N. Dodd 
3706c306765SKonstantin Belousov 	t1 = lml_find(lml, f);
3716c306765SKonstantin Belousov 	if (t1 == NULL || strcmp(t1, t) != 0) {
3723467e8b8SMatthew N. Dodd 		lm = xmalloc(sizeof(struct lm));
3731340fc10SMatthew N. Dodd 		lm->f = xstrdup(f);
3741340fc10SMatthew N. Dodd 		lm->t = xstrdup(t);
37529ade362SMatthew N. Dodd 		TAILQ_INSERT_HEAD(lml, lm, lm_link);
3765b08cb04SMatthew N. Dodd 		lm_count++;
37729ade362SMatthew N. Dodd 	}
3786c306765SKonstantin Belousov }
37929ade362SMatthew N. Dodd 
38029ade362SMatthew N. Dodd char *
lm_find(const char * p,const char * f)38129ade362SMatthew N. Dodd lm_find(const char *p, const char *f)
38229ade362SMatthew N. Dodd {
38329ade362SMatthew N. Dodd 	struct lm_list *lml;
38429ade362SMatthew N. Dodd 	char *t;
38529ade362SMatthew N. Dodd 
3861aac1ed6SMatthew N. Dodd 	dbg("%s(\"%s\", \"%s\")", __func__, p, f);
3871aac1ed6SMatthew N. Dodd 
38829ade362SMatthew N. Dodd 	if (p != NULL && (lml = lmp_find(p)) != NULL) {
38929ade362SMatthew N. Dodd 		t = lml_find(lml, f);
3903467e8b8SMatthew N. Dodd 		if (t != NULL) {
3913467e8b8SMatthew N. Dodd 			/*
3923467e8b8SMatthew N. Dodd 			 * Add a global mapping if we have
3933467e8b8SMatthew N. Dodd 			 * a successful constrained match.
3943467e8b8SMatthew N. Dodd 			 */
3951340fc10SMatthew N. Dodd 			lm_add(NULL, f, t);
39629ade362SMatthew N. Dodd 			return (t);
39729ade362SMatthew N. Dodd 		}
3983467e8b8SMatthew N. Dodd 	}
39929ade362SMatthew N. Dodd 	lml = lmp_find("$DEFAULT$");
40029ade362SMatthew N. Dodd 	if (lml != NULL)
40129ade362SMatthew N. Dodd 		return (lml_find(lml, f));
40229ade362SMatthew N. Dodd 	return (NULL);
40329ade362SMatthew N. Dodd }
40429ade362SMatthew N. Dodd 
405c1a0a86eSKonstantin Belousov /*
406c1a0a86eSKonstantin Belousov  * Given a libmap translation list and a library name, return the
407c1a0a86eSKonstantin Belousov  * replacement library, or NULL.
408c1a0a86eSKonstantin Belousov  */
409c905e45dSPeter Wemm char *
lm_findn(const char * p,const char * f,const size_t n)41078b64846SAlex Richardson lm_findn(const char *p, const char *f, const size_t n)
411c905e45dSPeter Wemm {
412c905e45dSPeter Wemm 	char pathbuf[64], *s, *t;
413c905e45dSPeter Wemm 
4148c6a035eSPeter Wemm 	if (n < sizeof(pathbuf) - 1)
415c905e45dSPeter Wemm 		s = pathbuf;
4168c6a035eSPeter Wemm 	else
417c905e45dSPeter Wemm 		s = xmalloc(n + 1);
4188c6a035eSPeter Wemm 	memcpy(s, f, n);
4198c6a035eSPeter Wemm 	s[n] = '\0';
420c905e45dSPeter Wemm 	t = lm_find(p, s);
421c905e45dSPeter Wemm 	if (s != pathbuf)
422c905e45dSPeter Wemm 		free(s);
423c905e45dSPeter Wemm 	return (t);
424c905e45dSPeter Wemm }
425c905e45dSPeter Wemm 
42629ade362SMatthew N. Dodd static char *
lml_find(struct lm_list * lmh,const char * f)42729ade362SMatthew N. Dodd lml_find(struct lm_list *lmh, const char *f)
42829ade362SMatthew N. Dodd {
42929ade362SMatthew N. Dodd 	struct lm *lm;
43029ade362SMatthew N. Dodd 
4311aac1ed6SMatthew N. Dodd 	dbg("%s(%p, \"%s\")", __func__, lmh, f);
4321aac1ed6SMatthew N. Dodd 
433c1a0a86eSKonstantin Belousov 	TAILQ_FOREACH(lm, lmh, lm_link) {
434c905e45dSPeter Wemm 		if (strcmp(f, lm->f) == 0)
43529ade362SMatthew N. Dodd 			return (lm->t);
436c1a0a86eSKonstantin Belousov 	}
437d33da23fSMatthew N. Dodd 	return (NULL);
43829ade362SMatthew N. Dodd }
43929ade362SMatthew N. Dodd 
440c1a0a86eSKonstantin Belousov /*
441c1a0a86eSKonstantin Belousov  * Given an executable name, return a pointer to the translation list or
442c1a0a86eSKonstantin Belousov  * NULL if no matches.
443c1a0a86eSKonstantin Belousov  */
44429ade362SMatthew N. Dodd static struct lm_list *
lmp_find(const char * n)44529ade362SMatthew N. Dodd lmp_find(const char *n)
44629ade362SMatthew N. Dodd {
44729ade362SMatthew N. Dodd 	struct lmp *lmp;
44829ade362SMatthew N. Dodd 
4491aac1ed6SMatthew N. Dodd 	dbg("%s(\"%s\")", __func__, n);
4501aac1ed6SMatthew N. Dodd 
451c1a0a86eSKonstantin Belousov 	TAILQ_FOREACH(lmp, &lmp_head, lmp_link) {
452966efcc7SMatthew N. Dodd 		if ((lmp->type == T_EXACT && strcmp(n, lmp->p) == 0) ||
453c1a0a86eSKonstantin Belousov 		    (lmp->type == T_DIRECTORY && strncmp(n, lmp->p,
454c1a0a86eSKonstantin Belousov 		    strlen(lmp->p)) == 0) ||
455c1a0a86eSKonstantin Belousov 		    (lmp->type == T_BASENAME && strcmp(quickbasename(n),
456c1a0a86eSKonstantin Belousov 		    lmp->p) == 0))
45729ade362SMatthew N. Dodd 			return (&lmp->lml);
458c1a0a86eSKonstantin Belousov 	}
45929ade362SMatthew N. Dodd 	return (NULL);
46029ade362SMatthew N. Dodd }
46129ade362SMatthew N. Dodd 
46229ade362SMatthew N. Dodd static struct lm_list *
lmp_init(char * n)46329ade362SMatthew N. Dodd lmp_init(char *n)
46429ade362SMatthew N. Dodd {
46529ade362SMatthew N. Dodd 	struct lmp *lmp;
46629ade362SMatthew N. Dodd 
4671aac1ed6SMatthew N. Dodd 	dbg("%s(\"%s\")", __func__, n);
4681aac1ed6SMatthew N. Dodd 
4693467e8b8SMatthew N. Dodd 	lmp = xmalloc(sizeof(struct lmp));
47029ade362SMatthew N. Dodd 	lmp->p = n;
471966efcc7SMatthew N. Dodd 	if (n[strlen(n) - 1] == '/')
472966efcc7SMatthew N. Dodd 		lmp->type = T_DIRECTORY;
473966efcc7SMatthew N. Dodd 	else if (strchr(n,'/') == NULL)
474966efcc7SMatthew N. Dodd 		lmp->type = T_BASENAME;
475966efcc7SMatthew N. Dodd 	else
476966efcc7SMatthew N. Dodd 		lmp->type = T_EXACT;
47729ade362SMatthew N. Dodd 	TAILQ_INIT(&lmp->lml);
47829ade362SMatthew N. Dodd 	TAILQ_INSERT_HEAD(&lmp_head, lmp, lmp_link);
47929ade362SMatthew N. Dodd 
48029ade362SMatthew N. Dodd 	return (&lmp->lml);
48129ade362SMatthew N. Dodd }
482966efcc7SMatthew N. Dodd 
483c1a0a86eSKonstantin Belousov /*
484c1a0a86eSKonstantin Belousov  * libc basename is overkill.  Return a pointer to the character after
485c1a0a86eSKonstantin Belousov  * the last /, or the original string if there are no slashes.
486c1a0a86eSKonstantin Belousov  */
487966efcc7SMatthew N. Dodd static const char *
quickbasename(const char * path)488966efcc7SMatthew N. Dodd quickbasename(const char *path)
489966efcc7SMatthew N. Dodd {
490c1a0a86eSKonstantin Belousov 	const char *p;
491c1a0a86eSKonstantin Belousov 
492c1a0a86eSKonstantin Belousov 	for (p = path; *path != '\0'; path++) {
493966efcc7SMatthew N. Dodd 		if (*path == '/')
494966efcc7SMatthew N. Dodd 			p = path + 1;
495966efcc7SMatthew N. Dodd 	}
496d33da23fSMatthew N. Dodd 	return (p);
497966efcc7SMatthew N. Dodd }
498