xref: /freebsd-14.2/libexec/rtld-elf/libmap.c (revision 6b61e3e4)
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;
39*6b61e3e4SEdward Tomasz Napierala 	dev_t dev;
40*6b61e3e4SEdward 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;
103faf66437SBaptiste Daroussin 	int fd;
104faf66437SBaptiste Daroussin 	char *lm_map;
105faf66437SBaptiste Daroussin 
106faf66437SBaptiste Daroussin 	TAILQ_FOREACH(p, &lmc_head, next) {
107*6b61e3e4SEdward Tomasz Napierala 		if (strcmp(p->path, path) == 0)
108faf66437SBaptiste Daroussin 			return;
109faf66437SBaptiste Daroussin 	}
110faf66437SBaptiste Daroussin 
111*6b61e3e4SEdward Tomasz Napierala 	fd = open(path, O_RDONLY | O_CLOEXEC);
112faf66437SBaptiste Daroussin 	if (fd == -1) {
113*6b61e3e4SEdward Tomasz Napierala 		dbg("lm_parse_file: open(\"%s\") failed, %s", path,
114faf66437SBaptiste Daroussin 		    rtld_strerror(errno));
115faf66437SBaptiste Daroussin 		return;
116faf66437SBaptiste Daroussin 	}
117faf66437SBaptiste Daroussin 	if (fstat(fd, &st) == -1) {
118faf66437SBaptiste Daroussin 		close(fd);
119*6b61e3e4SEdward Tomasz Napierala 		dbg("lm_parse_file: fstat(\"%s\") failed, %s", path,
120faf66437SBaptiste Daroussin 		    rtld_strerror(errno));
121faf66437SBaptiste Daroussin 		return;
122faf66437SBaptiste Daroussin 	}
123*6b61e3e4SEdward Tomasz Napierala 
124*6b61e3e4SEdward Tomasz Napierala 	TAILQ_FOREACH(p, &lmc_head, next) {
125*6b61e3e4SEdward Tomasz Napierala 		if (p->dev == st.st_dev && p->ino == st.st_ino) {
126*6b61e3e4SEdward Tomasz Napierala 			close(fd);
127*6b61e3e4SEdward Tomasz Napierala 			return;
128*6b61e3e4SEdward Tomasz Napierala 		}
129*6b61e3e4SEdward Tomasz Napierala 	}
130*6b61e3e4SEdward Tomasz Napierala 
131faf66437SBaptiste Daroussin 	lm_map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
132faf66437SBaptiste Daroussin 	if (lm_map == (const char *)MAP_FAILED) {
133faf66437SBaptiste Daroussin 		close(fd);
134*6b61e3e4SEdward Tomasz Napierala 		dbg("lm_parse_file: mmap(\"%s\") failed, %s", path,
135faf66437SBaptiste Daroussin 		    rtld_strerror(errno));
136faf66437SBaptiste Daroussin 		return;
137faf66437SBaptiste Daroussin 	}
138faf66437SBaptiste Daroussin 	close(fd);
139faf66437SBaptiste Daroussin 	p = xmalloc(sizeof(struct lmc));
140*6b61e3e4SEdward Tomasz Napierala 	p->path = xstrdup(path);
141*6b61e3e4SEdward Tomasz Napierala 	p->dev = st.st_dev;
142*6b61e3e4SEdward Tomasz Napierala 	p->ino = st.st_ino;
143faf66437SBaptiste Daroussin 	TAILQ_INSERT_HEAD(&lmc_head, p, next);
144faf66437SBaptiste Daroussin 	lmc_parse(lm_map, st.st_size);
145faf66437SBaptiste Daroussin 	munmap(lm_map, st.st_size);
146faf66437SBaptiste Daroussin }
147faf66437SBaptiste Daroussin 
148faf66437SBaptiste Daroussin static void
149faf66437SBaptiste Daroussin lmc_parse_dir(char *idir)
150faf66437SBaptiste Daroussin {
151faf66437SBaptiste Daroussin 	DIR *d;
152faf66437SBaptiste Daroussin 	struct dirent *dp;
153faf66437SBaptiste Daroussin 	struct lmc *p;
154faf66437SBaptiste Daroussin 	char conffile[MAXPATHLEN];
155faf66437SBaptiste Daroussin 	char *ext;
156faf66437SBaptiste Daroussin 
157faf66437SBaptiste Daroussin 	TAILQ_FOREACH(p, &lmc_head, next) {
158*6b61e3e4SEdward Tomasz Napierala 		if (strcmp(p->path, idir) == 0)
159faf66437SBaptiste Daroussin 			return;
160faf66437SBaptiste Daroussin 	}
161faf66437SBaptiste Daroussin 	d = opendir(idir);
162*6b61e3e4SEdward Tomasz Napierala 	if (d == NULL)
163faf66437SBaptiste Daroussin 		return;
164faf66437SBaptiste Daroussin 
165faf66437SBaptiste Daroussin 	p = xmalloc(sizeof(struct lmc));
166*6b61e3e4SEdward Tomasz Napierala 	p->path = xstrdup(idir);
167*6b61e3e4SEdward Tomasz Napierala 	p->dev = NODEV;
168*6b61e3e4SEdward Tomasz Napierala 	p->ino = 0;
169faf66437SBaptiste Daroussin 	TAILQ_INSERT_HEAD(&lmc_head, p, next);
170faf66437SBaptiste Daroussin 
171faf66437SBaptiste Daroussin 	while ((dp = readdir(d)) != NULL) {
172faf66437SBaptiste Daroussin 		if (dp->d_ino == 0)
173faf66437SBaptiste Daroussin 			continue;
174faf66437SBaptiste Daroussin 		if (dp->d_type != DT_REG)
175faf66437SBaptiste Daroussin 			continue;
176faf66437SBaptiste Daroussin 		ext = strrchr(dp->d_name, '.');
177faf66437SBaptiste Daroussin 		if (ext == NULL)
178faf66437SBaptiste Daroussin 			continue;
179faf66437SBaptiste Daroussin 		if (strcmp(ext, ".conf") != 0)
180faf66437SBaptiste Daroussin 			continue;
181faf66437SBaptiste Daroussin 		if (strlcpy(conffile, idir, MAXPATHLEN) >= MAXPATHLEN)
182faf66437SBaptiste Daroussin 			continue; /* too long */
183faf66437SBaptiste Daroussin 		if (strlcat(conffile, "/", MAXPATHLEN) >= MAXPATHLEN)
184faf66437SBaptiste Daroussin 			continue; /* too long */
185faf66437SBaptiste Daroussin 		if (strlcat(conffile, dp->d_name, MAXPATHLEN) >= MAXPATHLEN)
186faf66437SBaptiste Daroussin 			continue; /* too long */
187faf66437SBaptiste Daroussin 		lmc_parse_file(conffile);
188faf66437SBaptiste Daroussin 	}
189faf66437SBaptiste Daroussin 	closedir(d);
190faf66437SBaptiste Daroussin }
191faf66437SBaptiste Daroussin 
192faf66437SBaptiste Daroussin static void
19368f1db20SKonstantin Belousov lmc_parse(char *lm_p, size_t lm_len)
1945b08cb04SMatthew N. Dodd {
19568f1db20SKonstantin Belousov 	char *cp, *f, *t, *c, *p;
1965b08cb04SMatthew N. Dodd 	char prog[MAXPATHLEN];
197faf66437SBaptiste Daroussin 	/* allow includedir + full length path */
198faf66437SBaptiste Daroussin 	char line[MAXPATHLEN + 13];
19968f1db20SKonstantin Belousov 	size_t cnt;
20068f1db20SKonstantin Belousov 	int i;
2015b08cb04SMatthew N. Dodd 
20268f1db20SKonstantin Belousov 	cnt = 0;
20329ade362SMatthew N. Dodd 	p = NULL;
20468f1db20SKonstantin Belousov 	while (cnt < lm_len) {
20568f1db20SKonstantin Belousov 		i = 0;
2060fa46a42SPedro F. Giffuni 		while (cnt < lm_len && lm_p[cnt] != '\n' &&
20768f1db20SKonstantin Belousov 		    i < sizeof(line) - 1) {
20868f1db20SKonstantin Belousov 			line[i] = lm_p[cnt];
20968f1db20SKonstantin Belousov 			cnt++;
21068f1db20SKonstantin Belousov 			i++;
21168f1db20SKonstantin Belousov 		}
21268f1db20SKonstantin Belousov 		line[i] = '\0';
2130fa46a42SPedro F. Giffuni 		while (cnt < lm_len && lm_p[cnt] != '\n')
21468f1db20SKonstantin Belousov 			cnt++;
21568f1db20SKonstantin Belousov 		/* skip over nl */
21668f1db20SKonstantin Belousov 		cnt++;
21768f1db20SKonstantin Belousov 
21868f1db20SKonstantin Belousov 		cp = &line[0];
2191340fc10SMatthew N. Dodd 		t = f = c = NULL;
220623b6bd2SMatthew N. Dodd 
22129ade362SMatthew N. Dodd 		/* Skip over leading space */
22244976acaSSergey Kandaurov 		while (rtld_isspace(*cp)) cp++;
223623b6bd2SMatthew N. Dodd 
22429ade362SMatthew N. Dodd 		/* Found a comment or EOL */
225486089f0SAlexander Kabaev 		if (iseol(*cp)) continue;
226623b6bd2SMatthew N. Dodd 
227623b6bd2SMatthew N. Dodd 		/* Found a constraint selector */
22829ade362SMatthew N. Dodd 		if (*cp == '[') {
22929ade362SMatthew N. Dodd 			cp++;
230623b6bd2SMatthew N. Dodd 
23129ade362SMatthew N. Dodd 			/* Skip leading space */
23244976acaSSergey Kandaurov 			while (rtld_isspace(*cp)) cp++;
233623b6bd2SMatthew N. Dodd 
23429ade362SMatthew N. Dodd 			/* Found comment, EOL or end of selector */
235486089f0SAlexander Kabaev 			if  (iseol(*cp) || *cp == ']')
236486089f0SAlexander Kabaev 				continue;
237623b6bd2SMatthew N. Dodd 
2381340fc10SMatthew N. Dodd 			c = cp++;
23929ade362SMatthew N. Dodd 			/* Skip to end of word */
24044976acaSSergey Kandaurov 			while (!rtld_isspace(*cp) && !iseol(*cp) && *cp != ']')
241486089f0SAlexander Kabaev 				cp++;
242623b6bd2SMatthew N. Dodd 
243623b6bd2SMatthew N. Dodd 			/* Skip and zero out trailing space */
24444976acaSSergey Kandaurov 			while (rtld_isspace(*cp)) *cp++ = '\0';
245623b6bd2SMatthew N. Dodd 
246623b6bd2SMatthew N. Dodd 			/* Check if there is a closing brace */
247486089f0SAlexander Kabaev 			if (*cp != ']') continue;
248623b6bd2SMatthew N. Dodd 
249623b6bd2SMatthew N. Dodd 			/* Terminate string if there was no trailing space */
25029ade362SMatthew N. Dodd 			*cp++ = '\0';
251623b6bd2SMatthew N. Dodd 
252623b6bd2SMatthew N. Dodd 			/*
253623b6bd2SMatthew N. Dodd 			 * There should be nothing except whitespace or comment
2546d5d786fSAlexander Kabaev 			  from this point to the end of the line.
255623b6bd2SMatthew N. Dodd 			 */
25644976acaSSergey Kandaurov 			while(rtld_isspace(*cp)) cp++;
257486089f0SAlexander Kabaev 			if (!iseol(*cp)) continue;
258623b6bd2SMatthew N. Dodd 
259faf66437SBaptiste Daroussin 			if (strlcpy(prog, c, sizeof prog) >= sizeof prog)
260faf66437SBaptiste Daroussin 				continue;
26129ade362SMatthew N. Dodd 			p = prog;
262486089f0SAlexander Kabaev 			continue;
263623b6bd2SMatthew N. Dodd 		}
264623b6bd2SMatthew N. Dodd 
265623b6bd2SMatthew N. Dodd 		/* Parse the 'from' candidate. */
266486089f0SAlexander Kabaev 		f = cp++;
26744976acaSSergey Kandaurov 		while (!rtld_isspace(*cp) && !iseol(*cp)) cp++;
26829ade362SMatthew N. Dodd 
269623b6bd2SMatthew N. Dodd 		/* Skip and zero out the trailing whitespace */
27044976acaSSergey Kandaurov 		while (rtld_isspace(*cp)) *cp++ = '\0';
271623b6bd2SMatthew N. Dodd 
272623b6bd2SMatthew N. Dodd 		/* Found a comment or EOL */
273486089f0SAlexander Kabaev 		if (iseol(*cp)) continue;
274623b6bd2SMatthew N. Dodd 
275623b6bd2SMatthew N. Dodd 		/* Parse 'to' mapping */
276486089f0SAlexander Kabaev 		t = cp++;
27744976acaSSergey Kandaurov 		while (!rtld_isspace(*cp) && !iseol(*cp)) cp++;
278623b6bd2SMatthew N. Dodd 
279486089f0SAlexander Kabaev 		/* Skip and zero out the trailing whitespace */
28044976acaSSergey Kandaurov 		while (rtld_isspace(*cp)) *cp++ = '\0';
281486089f0SAlexander Kabaev 
282486089f0SAlexander Kabaev 		/* Should be no extra tokens at this point */
283486089f0SAlexander Kabaev 		if (!iseol(*cp)) continue;
284486089f0SAlexander Kabaev 
285486089f0SAlexander Kabaev 		*cp = '\0';
286faf66437SBaptiste Daroussin 		if (strcmp(f, "includedir") == 0)
287faf66437SBaptiste Daroussin 			lmc_parse_dir(t);
288faf66437SBaptiste Daroussin 		else if (strcmp(f, "include") == 0)
289faf66437SBaptiste Daroussin 			lmc_parse_file(t);
290faf66437SBaptiste Daroussin 		else
2911340fc10SMatthew N. Dodd 			lm_add(p, f, t);
29229ade362SMatthew N. Dodd 	}
29329ade362SMatthew N. Dodd }
29429ade362SMatthew N. Dodd 
29529ade362SMatthew N. Dodd static void
29629ade362SMatthew N. Dodd lm_free (struct lm_list *lml)
29729ade362SMatthew N. Dodd {
29829ade362SMatthew N. Dodd 	struct lm *lm;
29929ade362SMatthew N. Dodd 
3001aac1ed6SMatthew N. Dodd 	dbg("%s(%p)", __func__, lml);
3011aac1ed6SMatthew N. Dodd 
30229ade362SMatthew N. Dodd 	while (!TAILQ_EMPTY(lml)) {
30329ade362SMatthew N. Dodd 		lm = TAILQ_FIRST(lml);
30429ade362SMatthew N. Dodd 		TAILQ_REMOVE(lml, lm, lm_link);
30529ade362SMatthew N. Dodd 		free(lm->f);
30629ade362SMatthew N. Dodd 		free(lm->t);
30729ade362SMatthew N. Dodd 		free(lm);
30829ade362SMatthew N. Dodd 	}
30929ade362SMatthew N. Dodd 	return;
31029ade362SMatthew N. Dodd }
31129ade362SMatthew N. Dodd 
31229ade362SMatthew N. Dodd void
31329ade362SMatthew N. Dodd lm_fini (void)
31429ade362SMatthew N. Dodd {
31529ade362SMatthew N. Dodd 	struct lmp *lmp;
316faf66437SBaptiste Daroussin 	struct lmc *p;
31729ade362SMatthew N. Dodd 
3181aac1ed6SMatthew N. Dodd 	dbg("%s()", __func__);
3191aac1ed6SMatthew N. Dodd 
320faf66437SBaptiste Daroussin 	while (!TAILQ_EMPTY(&lmc_head)) {
321faf66437SBaptiste Daroussin 		p = TAILQ_FIRST(&lmc_head);
322faf66437SBaptiste Daroussin 		TAILQ_REMOVE(&lmc_head, p, next);
323faf66437SBaptiste Daroussin 		free(p->path);
324faf66437SBaptiste Daroussin 		free(p);
325faf66437SBaptiste Daroussin 	}
326faf66437SBaptiste Daroussin 
32729ade362SMatthew N. Dodd 	while (!TAILQ_EMPTY(&lmp_head)) {
32829ade362SMatthew N. Dodd 		lmp = TAILQ_FIRST(&lmp_head);
32929ade362SMatthew N. Dodd 		TAILQ_REMOVE(&lmp_head, lmp, lmp_link);
33029ade362SMatthew N. Dodd 		free(lmp->p);
33129ade362SMatthew N. Dodd 		lm_free(&lmp->lml);
33229ade362SMatthew N. Dodd 		free(lmp);
33329ade362SMatthew N. Dodd 	}
33429ade362SMatthew N. Dodd 	return;
33529ade362SMatthew N. Dodd }
33629ade362SMatthew N. Dodd 
33729ade362SMatthew N. Dodd static void
3381340fc10SMatthew N. Dodd lm_add (const char *p, const char *f, const char *t)
33929ade362SMatthew N. Dodd {
34029ade362SMatthew N. Dodd 	struct lm_list *lml;
34129ade362SMatthew N. Dodd 	struct lm *lm;
34229ade362SMatthew N. Dodd 
34329ade362SMatthew N. Dodd 	if (p == NULL)
34429ade362SMatthew N. Dodd 		p = "$DEFAULT$";
34529ade362SMatthew N. Dodd 
3461aac1ed6SMatthew N. Dodd 	dbg("%s(\"%s\", \"%s\", \"%s\")", __func__, p, f, t);
3471aac1ed6SMatthew N. Dodd 
34829ade362SMatthew N. Dodd 	if ((lml = lmp_find(p)) == NULL)
3493467e8b8SMatthew N. Dodd 		lml = lmp_init(xstrdup(p));
35029ade362SMatthew N. Dodd 
3513467e8b8SMatthew N. Dodd 	lm = xmalloc(sizeof(struct lm));
3521340fc10SMatthew N. Dodd 	lm->f = xstrdup(f);
3531340fc10SMatthew N. Dodd 	lm->t = xstrdup(t);
35429ade362SMatthew N. Dodd 	TAILQ_INSERT_HEAD(lml, lm, lm_link);
3555b08cb04SMatthew N. Dodd 	lm_count++;
35629ade362SMatthew N. Dodd }
35729ade362SMatthew N. Dodd 
35829ade362SMatthew N. Dodd char *
35929ade362SMatthew N. Dodd lm_find (const char *p, const char *f)
36029ade362SMatthew N. Dodd {
36129ade362SMatthew N. Dodd 	struct lm_list *lml;
36229ade362SMatthew N. Dodd 	char *t;
36329ade362SMatthew N. Dodd 
3641aac1ed6SMatthew N. Dodd 	dbg("%s(\"%s\", \"%s\")", __func__, p, f);
3651aac1ed6SMatthew N. Dodd 
36629ade362SMatthew N. Dodd 	if (p != NULL && (lml = lmp_find(p)) != NULL) {
36729ade362SMatthew N. Dodd 		t = lml_find(lml, f);
3683467e8b8SMatthew N. Dodd 		if (t != NULL) {
3693467e8b8SMatthew N. Dodd 			/*
3703467e8b8SMatthew N. Dodd 			 * Add a global mapping if we have
3713467e8b8SMatthew N. Dodd 			 * a successful constrained match.
3723467e8b8SMatthew N. Dodd 			 */
3731340fc10SMatthew N. Dodd 			lm_add(NULL, f, t);
37429ade362SMatthew N. Dodd 			return (t);
37529ade362SMatthew N. Dodd 		}
3763467e8b8SMatthew N. Dodd 	}
37729ade362SMatthew N. Dodd 	lml = lmp_find("$DEFAULT$");
37829ade362SMatthew N. Dodd 	if (lml != NULL)
37929ade362SMatthew N. Dodd 		return (lml_find(lml, f));
38029ade362SMatthew N. Dodd 	else
38129ade362SMatthew N. Dodd 		return (NULL);
38229ade362SMatthew N. Dodd }
38329ade362SMatthew N. Dodd 
384966efcc7SMatthew N. Dodd /* Given a libmap translation list and a library name, return the
385966efcc7SMatthew N. Dodd    replacement library, or NULL */
386c905e45dSPeter Wemm char *
387c905e45dSPeter Wemm lm_findn (const char *p, const char *f, const int n)
388c905e45dSPeter Wemm {
389c905e45dSPeter Wemm 	char pathbuf[64], *s, *t;
390c905e45dSPeter Wemm 
3918c6a035eSPeter Wemm 	if (n < sizeof(pathbuf) - 1)
392c905e45dSPeter Wemm 		s = pathbuf;
3938c6a035eSPeter Wemm 	else
394c905e45dSPeter Wemm 		s = xmalloc(n + 1);
3958c6a035eSPeter Wemm 	memcpy(s, f, n);
3968c6a035eSPeter Wemm 	s[n] = '\0';
397c905e45dSPeter Wemm 	t = lm_find(p, s);
398c905e45dSPeter Wemm 	if (s != pathbuf)
399c905e45dSPeter Wemm 		free(s);
400c905e45dSPeter Wemm 	return (t);
401c905e45dSPeter Wemm }
402c905e45dSPeter Wemm 
40329ade362SMatthew N. Dodd static char *
40429ade362SMatthew N. Dodd lml_find (struct lm_list *lmh, const char *f)
40529ade362SMatthew N. Dodd {
40629ade362SMatthew N. Dodd 	struct lm *lm;
40729ade362SMatthew N. Dodd 
4081aac1ed6SMatthew N. Dodd 	dbg("%s(%p, \"%s\")", __func__, lmh, f);
4091aac1ed6SMatthew N. Dodd 
41029ade362SMatthew N. Dodd 	TAILQ_FOREACH(lm, lmh, lm_link)
411c905e45dSPeter Wemm 		if (strcmp(f, lm->f) == 0)
41229ade362SMatthew N. Dodd 			return (lm->t);
413d33da23fSMatthew N. Dodd 	return (NULL);
41429ade362SMatthew N. Dodd }
41529ade362SMatthew N. Dodd 
416966efcc7SMatthew N. Dodd /* Given an executable name, return a pointer to the translation list or
417966efcc7SMatthew N. Dodd    NULL if no matches */
41829ade362SMatthew N. Dodd static struct lm_list *
41929ade362SMatthew N. Dodd lmp_find (const char *n)
42029ade362SMatthew N. Dodd {
42129ade362SMatthew N. Dodd 	struct lmp *lmp;
42229ade362SMatthew N. Dodd 
4231aac1ed6SMatthew N. Dodd 	dbg("%s(\"%s\")", __func__, n);
4241aac1ed6SMatthew N. Dodd 
42529ade362SMatthew N. Dodd 	TAILQ_FOREACH(lmp, &lmp_head, lmp_link)
426966efcc7SMatthew N. Dodd 		if ((lmp->type == T_EXACT && strcmp(n, lmp->p) == 0) ||
427966efcc7SMatthew N. Dodd 		    (lmp->type == T_DIRECTORY && strncmp(n, lmp->p, strlen(lmp->p)) == 0) ||
428966efcc7SMatthew N. Dodd 		    (lmp->type == T_BASENAME && strcmp(quickbasename(n), lmp->p) == 0))
42929ade362SMatthew N. Dodd 			return (&lmp->lml);
43029ade362SMatthew N. Dodd 	return (NULL);
43129ade362SMatthew N. Dodd }
43229ade362SMatthew N. Dodd 
43329ade362SMatthew N. Dodd static struct lm_list *
43429ade362SMatthew N. Dodd lmp_init (char *n)
43529ade362SMatthew N. Dodd {
43629ade362SMatthew N. Dodd 	struct lmp *lmp;
43729ade362SMatthew N. Dodd 
4381aac1ed6SMatthew N. Dodd 	dbg("%s(\"%s\")", __func__, n);
4391aac1ed6SMatthew N. Dodd 
4403467e8b8SMatthew N. Dodd 	lmp = xmalloc(sizeof(struct lmp));
44129ade362SMatthew N. Dodd 	lmp->p = n;
442966efcc7SMatthew N. Dodd 	if (n[strlen(n)-1] == '/')
443966efcc7SMatthew N. Dodd 		lmp->type = T_DIRECTORY;
444966efcc7SMatthew N. Dodd 	else if (strchr(n,'/') == NULL)
445966efcc7SMatthew N. Dodd 		lmp->type = T_BASENAME;
446966efcc7SMatthew N. Dodd 	else
447966efcc7SMatthew N. Dodd 		lmp->type = T_EXACT;
44829ade362SMatthew N. Dodd 	TAILQ_INIT(&lmp->lml);
44929ade362SMatthew N. Dodd 	TAILQ_INSERT_HEAD(&lmp_head, lmp, lmp_link);
45029ade362SMatthew N. Dodd 
45129ade362SMatthew N. Dodd 	return (&lmp->lml);
45229ade362SMatthew N. Dodd }
453966efcc7SMatthew N. Dodd 
454966efcc7SMatthew N. Dodd /* libc basename is overkill.  Return a pointer to the character after the
455966efcc7SMatthew N. Dodd    last /, or the original string if there are no slashes. */
456966efcc7SMatthew N. Dodd static const char *
457966efcc7SMatthew N. Dodd quickbasename (const char *path)
458966efcc7SMatthew N. Dodd {
459966efcc7SMatthew N. Dodd 	const char *p = path;
460d33da23fSMatthew N. Dodd 	for (; *path; path++) {
461966efcc7SMatthew N. Dodd 		if (*path == '/')
462966efcc7SMatthew N. Dodd 			p = path+1;
463966efcc7SMatthew N. Dodd 	}
464d33da23fSMatthew N. Dodd 	return (p);
465966efcc7SMatthew N. Dodd }
466