xref: /freebsd/libexec/rtld-elf/libmap.c (revision faf66437f825629ed30ab5b93edeab52df135ef7)
129ade362SMatthew N. Dodd /*
229ade362SMatthew N. Dodd  * $FreeBSD$
329ade362SMatthew N. Dodd  */
429ade362SMatthew N. Dodd 
5*faf66437SBaptiste 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>
11*faf66437SBaptiste 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"
193467e8b8SMatthew N. Dodd 
2029ade362SMatthew N. Dodd #ifndef _PATH_LIBMAP_CONF
2129ade362SMatthew N. Dodd #define	_PATH_LIBMAP_CONF	"/etc/libmap.conf"
2229ade362SMatthew N. Dodd #endif
2329ade362SMatthew N. Dodd 
24c905e45dSPeter Wemm #ifdef COMPAT_32BIT
25c905e45dSPeter Wemm #undef _PATH_LIBMAP_CONF
26c905e45dSPeter Wemm #define	_PATH_LIBMAP_CONF	"/etc/libmap32.conf"
27c905e45dSPeter Wemm #endif
28c905e45dSPeter Wemm 
2929ade362SMatthew N. Dodd TAILQ_HEAD(lm_list, lm);
3029ade362SMatthew N. Dodd struct lm {
3129ade362SMatthew N. Dodd 	char *f;
3229ade362SMatthew N. Dodd 	char *t;
3329ade362SMatthew N. Dodd 	TAILQ_ENTRY(lm)	lm_link;
3429ade362SMatthew N. Dodd };
3529ade362SMatthew N. Dodd 
3629ade362SMatthew N. Dodd TAILQ_HEAD(lmp_list, lmp) lmp_head = TAILQ_HEAD_INITIALIZER(lmp_head);
3729ade362SMatthew N. Dodd struct lmp {
3829ade362SMatthew N. Dodd 	char *p;
39966efcc7SMatthew N. Dodd 	enum { T_EXACT=0, T_BASENAME, T_DIRECTORY } type;
4029ade362SMatthew N. Dodd 	struct lm_list lml;
4129ade362SMatthew N. Dodd 	TAILQ_ENTRY(lmp) lmp_link;
4229ade362SMatthew N. Dodd };
4329ade362SMatthew N. Dodd 
44*faf66437SBaptiste Daroussin static TAILQ_HEAD(lmc_list, lmc) lmc_head = TAILQ_HEAD_INITIALIZER(lmc_head);
45*faf66437SBaptiste Daroussin struct lmc {
46*faf66437SBaptiste Daroussin 	char *path;
47*faf66437SBaptiste Daroussin 	TAILQ_ENTRY(lmc) next;
48*faf66437SBaptiste Daroussin };
49*faf66437SBaptiste Daroussin 
505b08cb04SMatthew N. Dodd static int lm_count;
515b08cb04SMatthew N. Dodd 
5268f1db20SKonstantin Belousov static void lmc_parse(char *, size_t);
53*faf66437SBaptiste Daroussin static void lmc_parse_file(char *);
54*faf66437SBaptiste Daroussin static void lmc_parse_dir(char *);
551340fc10SMatthew N. Dodd static void lm_add(const char *, const char *, const char *);
5629ade362SMatthew N. Dodd static void lm_free(struct lm_list *);
5729ade362SMatthew N. Dodd static char *lml_find(struct lm_list *, const char *);
5829ade362SMatthew N. Dodd static struct lm_list *lmp_find(const char *);
5929ade362SMatthew N. Dodd static struct lm_list *lmp_init(char *);
60966efcc7SMatthew N. Dodd static const char *quickbasename(const char *);
6129ade362SMatthew N. Dodd 
62623b6bd2SMatthew N. Dodd #define	iseol(c)	(((c) == '#') || ((c) == '\0') || \
63623b6bd2SMatthew N. Dodd 			 ((c) == '\n') || ((c) == '\r'))
64623b6bd2SMatthew N. Dodd 
65b36070f5SKonstantin Belousov /*
66b36070f5SKonstantin Belousov  * Do not use ctype.h macros, which rely on working TLS.  It is
67b36070f5SKonstantin Belousov  * too early to have thread-local variables functional.
68b36070f5SKonstantin Belousov  */
6944976acaSSergey Kandaurov #define	rtld_isspace(c)	((c) == ' ' || (c) == '\t')
70b36070f5SKonstantin Belousov 
714402996dSMatthew N. Dodd int
725b08cb04SMatthew N. Dodd lm_init(char *libmap_override)
7329ade362SMatthew N. Dodd {
74*faf66437SBaptiste Daroussin 	char *p;
7529ade362SMatthew N. Dodd 
7668f1db20SKonstantin Belousov 	dbg("lm_init(\"%s\")", libmap_override);
7729ade362SMatthew N. Dodd 	TAILQ_INIT(&lmp_head);
7829ade362SMatthew N. Dodd 
79*faf66437SBaptiste Daroussin 	lmc_parse_file(_PATH_LIBMAP_CONF);
805b08cb04SMatthew N. Dodd 
815b08cb04SMatthew N. Dodd 	if (libmap_override) {
8268f1db20SKonstantin Belousov 		/*
8368f1db20SKonstantin Belousov 		 * Do some character replacement to make $LIBMAP look
8468f1db20SKonstantin Belousov 		 * like a text file, then parse it.
8568f1db20SKonstantin Belousov 		 */
865b08cb04SMatthew N. Dodd 		libmap_override = xstrdup(libmap_override);
875b08cb04SMatthew N. Dodd 		for (p = libmap_override; *p; p++) {
885b08cb04SMatthew N. Dodd 			switch (*p) {
895b08cb04SMatthew N. Dodd 			case '=':
9068f1db20SKonstantin Belousov 				*p = ' ';
9168f1db20SKonstantin Belousov 				break;
925b08cb04SMatthew N. Dodd 			case ',':
9368f1db20SKonstantin Belousov 				*p = '\n';
9468f1db20SKonstantin Belousov 				break;
955b08cb04SMatthew N. Dodd 			}
965b08cb04SMatthew N. Dodd 		}
9768f1db20SKonstantin Belousov 		lmc_parse(p, strlen(p));
9868f1db20SKonstantin Belousov 		free(p);
995b08cb04SMatthew N. Dodd 	}
1005b08cb04SMatthew N. Dodd 
1015b08cb04SMatthew N. Dodd 	return (lm_count == 0);
1025b08cb04SMatthew N. Dodd }
1035b08cb04SMatthew N. Dodd 
1045b08cb04SMatthew N. Dodd static void
105*faf66437SBaptiste Daroussin lmc_parse_file(char *path)
106*faf66437SBaptiste Daroussin {
107*faf66437SBaptiste Daroussin 	struct lmc *p;
108*faf66437SBaptiste Daroussin 	struct stat st;
109*faf66437SBaptiste Daroussin 	int fd;
110*faf66437SBaptiste Daroussin 	char *rpath;
111*faf66437SBaptiste Daroussin 	char *lm_map;
112*faf66437SBaptiste Daroussin 
113*faf66437SBaptiste Daroussin 	rpath = realpath(path, NULL);
114*faf66437SBaptiste Daroussin 	if (rpath == NULL)
115*faf66437SBaptiste Daroussin 		return;
116*faf66437SBaptiste Daroussin 
117*faf66437SBaptiste Daroussin 	TAILQ_FOREACH(p, &lmc_head, next) {
118*faf66437SBaptiste Daroussin 		if (strcmp(p->path, rpath) == 0) {
119*faf66437SBaptiste Daroussin 			free(rpath);
120*faf66437SBaptiste Daroussin 			return;
121*faf66437SBaptiste Daroussin 		}
122*faf66437SBaptiste Daroussin 	}
123*faf66437SBaptiste Daroussin 
124*faf66437SBaptiste Daroussin 	fd = open(path, O_RDONLY);
125*faf66437SBaptiste Daroussin 	if (fd == -1) {
126*faf66437SBaptiste Daroussin 		dbg("lm_init: open(\"%s\") failed, %s", path,
127*faf66437SBaptiste Daroussin 		    rtld_strerror(errno));
128*faf66437SBaptiste Daroussin 		free(rpath);
129*faf66437SBaptiste Daroussin 		return;
130*faf66437SBaptiste Daroussin 	}
131*faf66437SBaptiste Daroussin 	if (fstat(fd, &st) == -1) {
132*faf66437SBaptiste Daroussin 		close(fd);
133*faf66437SBaptiste Daroussin 		dbg("lm_init: fstat(\"%s\") failed, %s", path,
134*faf66437SBaptiste Daroussin 		    rtld_strerror(errno));
135*faf66437SBaptiste Daroussin 		free(rpath);
136*faf66437SBaptiste Daroussin 		return;
137*faf66437SBaptiste Daroussin 	}
138*faf66437SBaptiste Daroussin 	lm_map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
139*faf66437SBaptiste Daroussin 	if (lm_map == (const char *)MAP_FAILED) {
140*faf66437SBaptiste Daroussin 		close(fd);
141*faf66437SBaptiste Daroussin 		dbg("lm_init: mmap(\"%s\") failed, %s", path,
142*faf66437SBaptiste Daroussin 		    rtld_strerror(errno));
143*faf66437SBaptiste Daroussin 		free(rpath);
144*faf66437SBaptiste Daroussin 		return;
145*faf66437SBaptiste Daroussin 	}
146*faf66437SBaptiste Daroussin 	close(fd);
147*faf66437SBaptiste Daroussin 	p = xmalloc(sizeof(struct lmc));
148*faf66437SBaptiste Daroussin 	p->path = rpath;
149*faf66437SBaptiste Daroussin 	TAILQ_INSERT_HEAD(&lmc_head, p, next);
150*faf66437SBaptiste Daroussin 	lmc_parse(lm_map, st.st_size);
151*faf66437SBaptiste Daroussin 	munmap(lm_map, st.st_size);
152*faf66437SBaptiste Daroussin }
153*faf66437SBaptiste Daroussin 
154*faf66437SBaptiste Daroussin static void
155*faf66437SBaptiste Daroussin lmc_parse_dir(char *idir)
156*faf66437SBaptiste Daroussin {
157*faf66437SBaptiste Daroussin 	DIR *d;
158*faf66437SBaptiste Daroussin 	struct dirent *dp;
159*faf66437SBaptiste Daroussin 	struct lmc *p;
160*faf66437SBaptiste Daroussin 	char conffile[MAXPATHLEN];
161*faf66437SBaptiste Daroussin 	char *ext;
162*faf66437SBaptiste Daroussin 	char *rpath;
163*faf66437SBaptiste Daroussin 
164*faf66437SBaptiste Daroussin 	rpath = realpath(idir, NULL);
165*faf66437SBaptiste Daroussin 	if (rpath == NULL)
166*faf66437SBaptiste Daroussin 		return;
167*faf66437SBaptiste Daroussin 
168*faf66437SBaptiste Daroussin 	TAILQ_FOREACH(p, &lmc_head, next) {
169*faf66437SBaptiste Daroussin 		if (strcmp(p->path, rpath) == 0) {
170*faf66437SBaptiste Daroussin 			free(rpath);
171*faf66437SBaptiste Daroussin 			return;
172*faf66437SBaptiste Daroussin 		}
173*faf66437SBaptiste Daroussin 	}
174*faf66437SBaptiste Daroussin 	d = opendir(idir);
175*faf66437SBaptiste Daroussin 	if (d == NULL) {
176*faf66437SBaptiste Daroussin 		free(rpath);
177*faf66437SBaptiste Daroussin 		return;
178*faf66437SBaptiste Daroussin 	}
179*faf66437SBaptiste Daroussin 
180*faf66437SBaptiste Daroussin 	p = xmalloc(sizeof(struct lmc));
181*faf66437SBaptiste Daroussin 	p->path = rpath;
182*faf66437SBaptiste Daroussin 	TAILQ_INSERT_HEAD(&lmc_head, p, next);
183*faf66437SBaptiste Daroussin 
184*faf66437SBaptiste Daroussin 	while ((dp = readdir(d)) != NULL) {
185*faf66437SBaptiste Daroussin 		if (dp->d_ino == 0)
186*faf66437SBaptiste Daroussin 			continue;
187*faf66437SBaptiste Daroussin 		if (dp->d_type != DT_REG)
188*faf66437SBaptiste Daroussin 			continue;
189*faf66437SBaptiste Daroussin 		ext = strrchr(dp->d_name, '.');
190*faf66437SBaptiste Daroussin 		if (ext == NULL)
191*faf66437SBaptiste Daroussin 			continue;
192*faf66437SBaptiste Daroussin 		if (strcmp(ext, ".conf") != 0)
193*faf66437SBaptiste Daroussin 			continue;
194*faf66437SBaptiste Daroussin 		if (strlcpy(conffile, idir, MAXPATHLEN) >= MAXPATHLEN)
195*faf66437SBaptiste Daroussin 			continue; /* too long */
196*faf66437SBaptiste Daroussin 		if (strlcat(conffile, "/", MAXPATHLEN) >= MAXPATHLEN)
197*faf66437SBaptiste Daroussin 			continue; /* too long */
198*faf66437SBaptiste Daroussin 		if (strlcat(conffile, dp->d_name, MAXPATHLEN) >= MAXPATHLEN)
199*faf66437SBaptiste Daroussin 			continue; /* too long */
200*faf66437SBaptiste Daroussin 		lmc_parse_file(conffile);
201*faf66437SBaptiste Daroussin 	}
202*faf66437SBaptiste Daroussin 	closedir(d);
203*faf66437SBaptiste Daroussin }
204*faf66437SBaptiste Daroussin 
205*faf66437SBaptiste Daroussin static void
20668f1db20SKonstantin Belousov lmc_parse(char *lm_p, size_t lm_len)
2075b08cb04SMatthew N. Dodd {
20868f1db20SKonstantin Belousov 	char *cp, *f, *t, *c, *p;
2095b08cb04SMatthew N. Dodd 	char prog[MAXPATHLEN];
210*faf66437SBaptiste Daroussin 	/* allow includedir + full length path */
211*faf66437SBaptiste Daroussin 	char line[MAXPATHLEN + 13];
21268f1db20SKonstantin Belousov 	size_t cnt;
21368f1db20SKonstantin Belousov 	int i;
2145b08cb04SMatthew N. Dodd 
21568f1db20SKonstantin Belousov 	cnt = 0;
21629ade362SMatthew N. Dodd 	p = NULL;
21768f1db20SKonstantin Belousov 	while (cnt < lm_len) {
21868f1db20SKonstantin Belousov 		i = 0;
21968f1db20SKonstantin Belousov 		while (lm_p[cnt] != '\n' && cnt < lm_len &&
22068f1db20SKonstantin Belousov 		    i < sizeof(line) - 1) {
22168f1db20SKonstantin Belousov 			line[i] = lm_p[cnt];
22268f1db20SKonstantin Belousov 			cnt++;
22368f1db20SKonstantin Belousov 			i++;
22468f1db20SKonstantin Belousov 		}
22568f1db20SKonstantin Belousov 		line[i] = '\0';
22668f1db20SKonstantin Belousov 		while (lm_p[cnt] != '\n' && cnt < lm_len)
22768f1db20SKonstantin Belousov 			cnt++;
22868f1db20SKonstantin Belousov 		/* skip over nl */
22968f1db20SKonstantin Belousov 		cnt++;
23068f1db20SKonstantin Belousov 
23168f1db20SKonstantin Belousov 		cp = &line[0];
2321340fc10SMatthew N. Dodd 		t = f = c = NULL;
233623b6bd2SMatthew N. Dodd 
23429ade362SMatthew N. Dodd 		/* Skip over leading space */
23544976acaSSergey Kandaurov 		while (rtld_isspace(*cp)) cp++;
236623b6bd2SMatthew N. Dodd 
23729ade362SMatthew N. Dodd 		/* Found a comment or EOL */
238486089f0SAlexander Kabaev 		if (iseol(*cp)) continue;
239623b6bd2SMatthew N. Dodd 
240623b6bd2SMatthew N. Dodd 		/* Found a constraint selector */
24129ade362SMatthew N. Dodd 		if (*cp == '[') {
24229ade362SMatthew N. Dodd 			cp++;
243623b6bd2SMatthew N. Dodd 
24429ade362SMatthew N. Dodd 			/* Skip leading space */
24544976acaSSergey Kandaurov 			while (rtld_isspace(*cp)) cp++;
246623b6bd2SMatthew N. Dodd 
24729ade362SMatthew N. Dodd 			/* Found comment, EOL or end of selector */
248486089f0SAlexander Kabaev 			if  (iseol(*cp) || *cp == ']')
249486089f0SAlexander Kabaev 				continue;
250623b6bd2SMatthew N. Dodd 
2511340fc10SMatthew N. Dodd 			c = cp++;
25229ade362SMatthew N. Dodd 			/* Skip to end of word */
25344976acaSSergey Kandaurov 			while (!rtld_isspace(*cp) && !iseol(*cp) && *cp != ']')
254486089f0SAlexander Kabaev 				cp++;
255623b6bd2SMatthew N. Dodd 
256623b6bd2SMatthew N. Dodd 			/* Skip and zero out trailing space */
25744976acaSSergey Kandaurov 			while (rtld_isspace(*cp)) *cp++ = '\0';
258623b6bd2SMatthew N. Dodd 
259623b6bd2SMatthew N. Dodd 			/* Check if there is a closing brace */
260486089f0SAlexander Kabaev 			if (*cp != ']') continue;
261623b6bd2SMatthew N. Dodd 
262623b6bd2SMatthew N. Dodd 			/* Terminate string if there was no trailing space */
26329ade362SMatthew N. Dodd 			*cp++ = '\0';
264623b6bd2SMatthew N. Dodd 
265623b6bd2SMatthew N. Dodd 			/*
266623b6bd2SMatthew N. Dodd 			 * There should be nothing except whitespace or comment
2676d5d786fSAlexander Kabaev 			  from this point to the end of the line.
268623b6bd2SMatthew N. Dodd 			 */
26944976acaSSergey Kandaurov 			while(rtld_isspace(*cp)) cp++;
270486089f0SAlexander Kabaev 			if (!iseol(*cp)) continue;
271623b6bd2SMatthew N. Dodd 
272*faf66437SBaptiste Daroussin 			if (strlcpy(prog, c, sizeof prog) >= sizeof prog)
273*faf66437SBaptiste Daroussin 				continue;
27429ade362SMatthew N. Dodd 			p = prog;
275486089f0SAlexander Kabaev 			continue;
276623b6bd2SMatthew N. Dodd 		}
277623b6bd2SMatthew N. Dodd 
278623b6bd2SMatthew N. Dodd 		/* Parse the 'from' candidate. */
279486089f0SAlexander Kabaev 		f = cp++;
28044976acaSSergey Kandaurov 		while (!rtld_isspace(*cp) && !iseol(*cp)) cp++;
28129ade362SMatthew N. Dodd 
282623b6bd2SMatthew N. Dodd 		/* Skip and zero out the trailing whitespace */
28344976acaSSergey Kandaurov 		while (rtld_isspace(*cp)) *cp++ = '\0';
284623b6bd2SMatthew N. Dodd 
285623b6bd2SMatthew N. Dodd 		/* Found a comment or EOL */
286486089f0SAlexander Kabaev 		if (iseol(*cp)) continue;
287623b6bd2SMatthew N. Dodd 
288623b6bd2SMatthew N. Dodd 		/* Parse 'to' mapping */
289486089f0SAlexander Kabaev 		t = cp++;
29044976acaSSergey Kandaurov 		while (!rtld_isspace(*cp) && !iseol(*cp)) cp++;
291623b6bd2SMatthew N. Dodd 
292486089f0SAlexander Kabaev 		/* Skip and zero out the trailing whitespace */
29344976acaSSergey Kandaurov 		while (rtld_isspace(*cp)) *cp++ = '\0';
294486089f0SAlexander Kabaev 
295486089f0SAlexander Kabaev 		/* Should be no extra tokens at this point */
296486089f0SAlexander Kabaev 		if (!iseol(*cp)) continue;
297486089f0SAlexander Kabaev 
298486089f0SAlexander Kabaev 		*cp = '\0';
299*faf66437SBaptiste Daroussin 		if (strcmp(f, "includedir") == 0)
300*faf66437SBaptiste Daroussin 			lmc_parse_dir(t);
301*faf66437SBaptiste Daroussin 		else if (strcmp(f, "include") == 0)
302*faf66437SBaptiste Daroussin 			lmc_parse_file(t);
303*faf66437SBaptiste Daroussin 		else
3041340fc10SMatthew N. Dodd 			lm_add(p, f, t);
30529ade362SMatthew N. Dodd 	}
30629ade362SMatthew N. Dodd }
30729ade362SMatthew N. Dodd 
30829ade362SMatthew N. Dodd static void
30929ade362SMatthew N. Dodd lm_free (struct lm_list *lml)
31029ade362SMatthew N. Dodd {
31129ade362SMatthew N. Dodd 	struct lm *lm;
31229ade362SMatthew N. Dodd 
3131aac1ed6SMatthew N. Dodd 	dbg("%s(%p)", __func__, lml);
3141aac1ed6SMatthew N. Dodd 
31529ade362SMatthew N. Dodd 	while (!TAILQ_EMPTY(lml)) {
31629ade362SMatthew N. Dodd 		lm = TAILQ_FIRST(lml);
31729ade362SMatthew N. Dodd 		TAILQ_REMOVE(lml, lm, lm_link);
31829ade362SMatthew N. Dodd 		free(lm->f);
31929ade362SMatthew N. Dodd 		free(lm->t);
32029ade362SMatthew N. Dodd 		free(lm);
32129ade362SMatthew N. Dodd 	}
32229ade362SMatthew N. Dodd 	return;
32329ade362SMatthew N. Dodd }
32429ade362SMatthew N. Dodd 
32529ade362SMatthew N. Dodd void
32629ade362SMatthew N. Dodd lm_fini (void)
32729ade362SMatthew N. Dodd {
32829ade362SMatthew N. Dodd 	struct lmp *lmp;
329*faf66437SBaptiste Daroussin 	struct lmc *p;
33029ade362SMatthew N. Dodd 
3311aac1ed6SMatthew N. Dodd 	dbg("%s()", __func__);
3321aac1ed6SMatthew N. Dodd 
333*faf66437SBaptiste Daroussin 	while (!TAILQ_EMPTY(&lmc_head)) {
334*faf66437SBaptiste Daroussin 		p = TAILQ_FIRST(&lmc_head);
335*faf66437SBaptiste Daroussin 		TAILQ_REMOVE(&lmc_head, p, next);
336*faf66437SBaptiste Daroussin 		free(p->path);
337*faf66437SBaptiste Daroussin 		free(p);
338*faf66437SBaptiste Daroussin 	}
339*faf66437SBaptiste Daroussin 
34029ade362SMatthew N. Dodd 	while (!TAILQ_EMPTY(&lmp_head)) {
34129ade362SMatthew N. Dodd 		lmp = TAILQ_FIRST(&lmp_head);
34229ade362SMatthew N. Dodd 		TAILQ_REMOVE(&lmp_head, lmp, lmp_link);
34329ade362SMatthew N. Dodd 		free(lmp->p);
34429ade362SMatthew N. Dodd 		lm_free(&lmp->lml);
34529ade362SMatthew N. Dodd 		free(lmp);
34629ade362SMatthew N. Dodd 	}
34729ade362SMatthew N. Dodd 	return;
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;
35529ade362SMatthew N. Dodd 
35629ade362SMatthew N. Dodd 	if (p == NULL)
35729ade362SMatthew N. Dodd 		p = "$DEFAULT$";
35829ade362SMatthew N. Dodd 
3591aac1ed6SMatthew N. Dodd 	dbg("%s(\"%s\", \"%s\", \"%s\")", __func__, p, f, t);
3601aac1ed6SMatthew N. Dodd 
36129ade362SMatthew N. Dodd 	if ((lml = lmp_find(p)) == NULL)
3623467e8b8SMatthew N. Dodd 		lml = lmp_init(xstrdup(p));
36329ade362SMatthew N. Dodd 
3643467e8b8SMatthew N. Dodd 	lm = xmalloc(sizeof(struct lm));
3651340fc10SMatthew N. Dodd 	lm->f = xstrdup(f);
3661340fc10SMatthew N. Dodd 	lm->t = xstrdup(t);
36729ade362SMatthew N. Dodd 	TAILQ_INSERT_HEAD(lml, lm, lm_link);
3685b08cb04SMatthew N. Dodd 	lm_count++;
36929ade362SMatthew N. Dodd }
37029ade362SMatthew N. Dodd 
37129ade362SMatthew N. Dodd char *
37229ade362SMatthew N. Dodd lm_find (const char *p, const char *f)
37329ade362SMatthew N. Dodd {
37429ade362SMatthew N. Dodd 	struct lm_list *lml;
37529ade362SMatthew N. Dodd 	char *t;
37629ade362SMatthew N. Dodd 
3771aac1ed6SMatthew N. Dodd 	dbg("%s(\"%s\", \"%s\")", __func__, p, f);
3781aac1ed6SMatthew N. Dodd 
37929ade362SMatthew N. Dodd 	if (p != NULL && (lml = lmp_find(p)) != NULL) {
38029ade362SMatthew N. Dodd 		t = lml_find(lml, f);
3813467e8b8SMatthew N. Dodd 		if (t != NULL) {
3823467e8b8SMatthew N. Dodd 			/*
3833467e8b8SMatthew N. Dodd 			 * Add a global mapping if we have
3843467e8b8SMatthew N. Dodd 			 * a successful constrained match.
3853467e8b8SMatthew N. Dodd 			 */
3861340fc10SMatthew N. Dodd 			lm_add(NULL, f, t);
38729ade362SMatthew N. Dodd 			return (t);
38829ade362SMatthew N. Dodd 		}
3893467e8b8SMatthew N. Dodd 	}
39029ade362SMatthew N. Dodd 	lml = lmp_find("$DEFAULT$");
39129ade362SMatthew N. Dodd 	if (lml != NULL)
39229ade362SMatthew N. Dodd 		return (lml_find(lml, f));
39329ade362SMatthew N. Dodd 	else
39429ade362SMatthew N. Dodd 		return (NULL);
39529ade362SMatthew N. Dodd }
39629ade362SMatthew N. Dodd 
397966efcc7SMatthew N. Dodd /* Given a libmap translation list and a library name, return the
398966efcc7SMatthew N. Dodd    replacement library, or NULL */
399c905e45dSPeter Wemm #ifdef COMPAT_32BIT
400c905e45dSPeter Wemm char *
401c905e45dSPeter Wemm lm_findn (const char *p, const char *f, const int n)
402c905e45dSPeter Wemm {
403c905e45dSPeter Wemm 	char pathbuf[64], *s, *t;
404c905e45dSPeter Wemm 
4058c6a035eSPeter Wemm 	if (n < sizeof(pathbuf) - 1)
406c905e45dSPeter Wemm 		s = pathbuf;
4078c6a035eSPeter Wemm 	else
408c905e45dSPeter Wemm 		s = xmalloc(n + 1);
4098c6a035eSPeter Wemm 	memcpy(s, f, n);
4108c6a035eSPeter Wemm 	s[n] = '\0';
411c905e45dSPeter Wemm 	t = lm_find(p, s);
412c905e45dSPeter Wemm 	if (s != pathbuf)
413c905e45dSPeter Wemm 		free(s);
414c905e45dSPeter Wemm 	return (t);
415c905e45dSPeter Wemm }
416c905e45dSPeter Wemm #endif
417c905e45dSPeter Wemm 
41829ade362SMatthew N. Dodd static char *
41929ade362SMatthew N. Dodd lml_find (struct lm_list *lmh, const char *f)
42029ade362SMatthew N. Dodd {
42129ade362SMatthew N. Dodd 	struct lm *lm;
42229ade362SMatthew N. Dodd 
4231aac1ed6SMatthew N. Dodd 	dbg("%s(%p, \"%s\")", __func__, lmh, f);
4241aac1ed6SMatthew N. Dodd 
42529ade362SMatthew N. Dodd 	TAILQ_FOREACH(lm, lmh, lm_link)
426c905e45dSPeter Wemm 		if (strcmp(f, lm->f) == 0)
42729ade362SMatthew N. Dodd 			return (lm->t);
428d33da23fSMatthew N. Dodd 	return (NULL);
42929ade362SMatthew N. Dodd }
43029ade362SMatthew N. Dodd 
431966efcc7SMatthew N. Dodd /* Given an executable name, return a pointer to the translation list or
432966efcc7SMatthew N. Dodd    NULL if no matches */
43329ade362SMatthew N. Dodd static struct lm_list *
43429ade362SMatthew N. Dodd lmp_find (const 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 
44029ade362SMatthew N. Dodd 	TAILQ_FOREACH(lmp, &lmp_head, lmp_link)
441966efcc7SMatthew N. Dodd 		if ((lmp->type == T_EXACT && strcmp(n, lmp->p) == 0) ||
442966efcc7SMatthew N. Dodd 		    (lmp->type == T_DIRECTORY && strncmp(n, lmp->p, strlen(lmp->p)) == 0) ||
443966efcc7SMatthew N. Dodd 		    (lmp->type == T_BASENAME && strcmp(quickbasename(n), lmp->p) == 0))
44429ade362SMatthew N. Dodd 			return (&lmp->lml);
44529ade362SMatthew N. Dodd 	return (NULL);
44629ade362SMatthew N. Dodd }
44729ade362SMatthew N. Dodd 
44829ade362SMatthew N. Dodd static struct lm_list *
44929ade362SMatthew N. Dodd lmp_init (char *n)
45029ade362SMatthew N. Dodd {
45129ade362SMatthew N. Dodd 	struct lmp *lmp;
45229ade362SMatthew N. Dodd 
4531aac1ed6SMatthew N. Dodd 	dbg("%s(\"%s\")", __func__, n);
4541aac1ed6SMatthew N. Dodd 
4553467e8b8SMatthew N. Dodd 	lmp = xmalloc(sizeof(struct lmp));
45629ade362SMatthew N. Dodd 	lmp->p = n;
457966efcc7SMatthew N. Dodd 	if (n[strlen(n)-1] == '/')
458966efcc7SMatthew N. Dodd 		lmp->type = T_DIRECTORY;
459966efcc7SMatthew N. Dodd 	else if (strchr(n,'/') == NULL)
460966efcc7SMatthew N. Dodd 		lmp->type = T_BASENAME;
461966efcc7SMatthew N. Dodd 	else
462966efcc7SMatthew N. Dodd 		lmp->type = T_EXACT;
46329ade362SMatthew N. Dodd 	TAILQ_INIT(&lmp->lml);
46429ade362SMatthew N. Dodd 	TAILQ_INSERT_HEAD(&lmp_head, lmp, lmp_link);
46529ade362SMatthew N. Dodd 
46629ade362SMatthew N. Dodd 	return (&lmp->lml);
46729ade362SMatthew N. Dodd }
468966efcc7SMatthew N. Dodd 
469966efcc7SMatthew N. Dodd /* libc basename is overkill.  Return a pointer to the character after the
470966efcc7SMatthew N. Dodd    last /, or the original string if there are no slashes. */
471966efcc7SMatthew N. Dodd static const char *
472966efcc7SMatthew N. Dodd quickbasename (const char *path)
473966efcc7SMatthew N. Dodd {
474966efcc7SMatthew N. Dodd 	const char *p = path;
475d33da23fSMatthew N. Dodd 	for (; *path; path++) {
476966efcc7SMatthew N. Dodd 		if (*path == '/')
477966efcc7SMatthew N. Dodd 			p = path+1;
478966efcc7SMatthew N. Dodd 	}
479d33da23fSMatthew N. Dodd 	return (p);
480966efcc7SMatthew N. Dodd }
481