xref: /freebsd/libexec/rtld-elf/libmap.c (revision 29ade362259ab5b1bcb692c1f5c74fb839b4e91c)
129ade362SMatthew N. Dodd /*
229ade362SMatthew N. Dodd  * $FreeBSD$
329ade362SMatthew N. Dodd  */
429ade362SMatthew N. Dodd 
529ade362SMatthew N. Dodd #include <stdio.h>
629ade362SMatthew N. Dodd #include <ctype.h>
729ade362SMatthew N. Dodd #include <string.h>
829ade362SMatthew N. Dodd #include <stdlib.h>
929ade362SMatthew N. Dodd #include <sys/queue.h>
1029ade362SMatthew N. Dodd #include <sys/param.h>
1129ade362SMatthew N. Dodd 
1229ade362SMatthew N. Dodd #ifndef _PATH_LIBMAP_CONF
1329ade362SMatthew N. Dodd #define	_PATH_LIBMAP_CONF	"/etc/libmap.conf"
1429ade362SMatthew N. Dodd #endif
1529ade362SMatthew N. Dodd 
1629ade362SMatthew N. Dodd TAILQ_HEAD(lm_list, lm);
1729ade362SMatthew N. Dodd struct lm {
1829ade362SMatthew N. Dodd 	char *f;
1929ade362SMatthew N. Dodd 	char *t;
2029ade362SMatthew N. Dodd 
2129ade362SMatthew N. Dodd 	TAILQ_ENTRY(lm)	lm_link;
2229ade362SMatthew N. Dodd };
2329ade362SMatthew N. Dodd 
2429ade362SMatthew N. Dodd TAILQ_HEAD(lmp_list, lmp) lmp_head = TAILQ_HEAD_INITIALIZER(lmp_head);
2529ade362SMatthew N. Dodd struct lmp {
2629ade362SMatthew N. Dodd 	char *p;
2729ade362SMatthew N. Dodd 	struct lm_list lml;
2829ade362SMatthew N. Dodd 	TAILQ_ENTRY(lmp) lmp_link;
2929ade362SMatthew N. Dodd };
3029ade362SMatthew N. Dodd 
3129ade362SMatthew N. Dodd static void		lm_add		(char *, char *, char *);
3229ade362SMatthew N. Dodd static void		lm_free		(struct lm_list *);
3329ade362SMatthew N. Dodd static char *		lml_find	(struct lm_list *, const char *);
3429ade362SMatthew N. Dodd static struct lm_list *	lmp_find	(const char *);
3529ade362SMatthew N. Dodd static struct lm_list *	lmp_init	(char *);
3629ade362SMatthew N. Dodd 
3729ade362SMatthew N. Dodd void
3829ade362SMatthew N. Dodd lm_init (void)
3929ade362SMatthew N. Dodd {
4029ade362SMatthew N. Dodd 	FILE	*fp;
4129ade362SMatthew N. Dodd 	char	*cp;
4229ade362SMatthew N. Dodd 	char	*f, *t, *p;
4329ade362SMatthew N. Dodd 	char	prog[MAXPATHLEN];
4429ade362SMatthew N. Dodd 	char	line[MAXPATHLEN + 2];
4529ade362SMatthew N. Dodd 
4629ade362SMatthew N. Dodd 	TAILQ_INIT(&lmp_head);
4729ade362SMatthew N. Dodd 
4829ade362SMatthew N. Dodd 	if ((fp = fopen(_PATH_LIBMAP_CONF, "r")) == NULL)
4929ade362SMatthew N. Dodd 		return;
5029ade362SMatthew N. Dodd 
5129ade362SMatthew N. Dodd 	p = NULL;
5229ade362SMatthew N. Dodd 	while ((cp = fgets(line, MAXPATHLEN + 1, fp)) != NULL) {
5329ade362SMatthew N. Dodd 		t = f = NULL;
5429ade362SMatthew N. Dodd 		/* Skip over leading space */
5529ade362SMatthew N. Dodd 		while (!isalpha(*cp) &&
5629ade362SMatthew N. Dodd 		       *cp != '#' && *cp != '\0' && *cp != '[') cp++;
5729ade362SMatthew N. Dodd 		/* Found a comment or EOL */
5829ade362SMatthew N. Dodd 		if (*cp == '#' || *cp == '\0')
5929ade362SMatthew N. Dodd 			continue;
6029ade362SMatthew N. Dodd 		/* Found a costraint selector */
6129ade362SMatthew N. Dodd 		if (*cp == '[') {
6229ade362SMatthew N. Dodd 			cp++;
6329ade362SMatthew N. Dodd 			/* Skip leading space */
6429ade362SMatthew N. Dodd 			while (isspace(*cp) &&
6529ade362SMatthew N. Dodd 			       *cp != '#' && *cp != '\0' && *cp != ']') cp++;
6629ade362SMatthew N. Dodd 			/* Found comment, EOL or end of selector */
6729ade362SMatthew N. Dodd 			if  (*cp == '#' || *cp == '\0' || *cp == ']')
6829ade362SMatthew N. Dodd 				continue;
6929ade362SMatthew N. Dodd 			p = cp;
7029ade362SMatthew N. Dodd 			/* Skip to end of word */
7129ade362SMatthew N. Dodd 			while (!isspace(*cp) &&
7229ade362SMatthew N. Dodd 			       *cp != '#' && *cp != '\0' && *cp != ']') cp++;
7329ade362SMatthew N. Dodd 			*cp++ = '\0';
7429ade362SMatthew N. Dodd 			bzero(prog, MAXPATHLEN);
7529ade362SMatthew N. Dodd 			strncpy(prog, p, strlen(p));
7629ade362SMatthew N. Dodd 			p = prog;
7729ade362SMatthew N. Dodd 			continue;
7829ade362SMatthew N. Dodd 		}
7929ade362SMatthew N. Dodd 		f = cp;
8029ade362SMatthew N. Dodd 		while (!isspace(*cp) && *cp != '#' && *cp != '\0') cp++;
8129ade362SMatthew N. Dodd 		*cp++ = '\0';
8229ade362SMatthew N. Dodd 		while (isspace(*cp) && *cp != '#' && *cp != '\0') cp++;
8329ade362SMatthew N. Dodd 		t = cp;
8429ade362SMatthew N. Dodd 		while (!isspace(*cp) && *cp != '#' && *cp != '\0') cp++;
8529ade362SMatthew N. Dodd 		*cp++ = '\0';
8629ade362SMatthew N. Dodd 
8729ade362SMatthew N. Dodd 		lm_add(p, strdup(f), strdup(t));
8829ade362SMatthew N. Dodd 		bzero(line, sizeof(line));
8929ade362SMatthew N. Dodd 	}
9029ade362SMatthew N. Dodd 	(void)fclose(fp);
9129ade362SMatthew N. Dodd 	return;
9229ade362SMatthew N. Dodd }
9329ade362SMatthew N. Dodd 
9429ade362SMatthew N. Dodd static void
9529ade362SMatthew N. Dodd lm_free (struct lm_list *lml)
9629ade362SMatthew N. Dodd {
9729ade362SMatthew N. Dodd 	struct lm *lm;
9829ade362SMatthew N. Dodd 
9929ade362SMatthew N. Dodd 	while (!TAILQ_EMPTY(lml)) {
10029ade362SMatthew N. Dodd 		lm = TAILQ_FIRST(lml);
10129ade362SMatthew N. Dodd 		TAILQ_REMOVE(lml, lm, lm_link);
10229ade362SMatthew N. Dodd 		free(lm->f);
10329ade362SMatthew N. Dodd 		free(lm->t);
10429ade362SMatthew N. Dodd 		free(lm);
10529ade362SMatthew N. Dodd 	}
10629ade362SMatthew N. Dodd 	return;
10729ade362SMatthew N. Dodd }
10829ade362SMatthew N. Dodd 
10929ade362SMatthew N. Dodd void
11029ade362SMatthew N. Dodd lm_fini (void)
11129ade362SMatthew N. Dodd {
11229ade362SMatthew N. Dodd 	struct lmp *lmp;
11329ade362SMatthew N. Dodd 
11429ade362SMatthew N. Dodd 	while (!TAILQ_EMPTY(&lmp_head)) {
11529ade362SMatthew N. Dodd 		lmp = TAILQ_FIRST(&lmp_head);
11629ade362SMatthew N. Dodd 		TAILQ_REMOVE(&lmp_head, lmp, lmp_link);
11729ade362SMatthew N. Dodd 		free(lmp->p);
11829ade362SMatthew N. Dodd 		lm_free(&lmp->lml);
11929ade362SMatthew N. Dodd 		free(lmp);
12029ade362SMatthew N. Dodd 	}
12129ade362SMatthew N. Dodd 	return;
12229ade362SMatthew N. Dodd }
12329ade362SMatthew N. Dodd 
12429ade362SMatthew N. Dodd static void
12529ade362SMatthew N. Dodd lm_add (char *p, char *f, char *t)
12629ade362SMatthew N. Dodd {
12729ade362SMatthew N. Dodd 	struct lm_list *lml;
12829ade362SMatthew N. Dodd 	struct lm *lm;
12929ade362SMatthew N. Dodd 
13029ade362SMatthew N. Dodd 	if (p == NULL)
13129ade362SMatthew N. Dodd 		p = "$DEFAULT$";
13229ade362SMatthew N. Dodd 
13329ade362SMatthew N. Dodd #if 0
13429ade362SMatthew N. Dodd 	printf("%s(\"%s\", \"%s\", \"%s\")\n", __func__, p, f, t);
13529ade362SMatthew N. Dodd #endif
13629ade362SMatthew N. Dodd 
13729ade362SMatthew N. Dodd 	if ((lml = lmp_find(p)) == NULL)
13829ade362SMatthew N. Dodd 		lml = lmp_init(strdup(p));
13929ade362SMatthew N. Dodd 
14029ade362SMatthew N. Dodd 	lm = malloc(sizeof(struct lm));
14129ade362SMatthew N. Dodd 	lm->f = f;
14229ade362SMatthew N. Dodd 	lm->t = t;
14329ade362SMatthew N. Dodd 	TAILQ_INSERT_HEAD(lml, lm, lm_link);
14429ade362SMatthew N. Dodd }
14529ade362SMatthew N. Dodd 
14629ade362SMatthew N. Dodd char *
14729ade362SMatthew N. Dodd lm_find (const char *p, const char *f)
14829ade362SMatthew N. Dodd {
14929ade362SMatthew N. Dodd 	struct lm_list *lml;
15029ade362SMatthew N. Dodd 	char *t;
15129ade362SMatthew N. Dodd 
15229ade362SMatthew N. Dodd 	if (p != NULL && (lml = lmp_find(p)) != NULL) {
15329ade362SMatthew N. Dodd 		t = lml_find(lml, f);
15429ade362SMatthew N. Dodd 		if (t != NULL)
15529ade362SMatthew N. Dodd 			return (t);
15629ade362SMatthew N. Dodd 	}
15729ade362SMatthew N. Dodd 	lml = lmp_find("$DEFAULT$");
15829ade362SMatthew N. Dodd 	if (lml != NULL)
15929ade362SMatthew N. Dodd 		return (lml_find(lml, f));
16029ade362SMatthew N. Dodd 	else
16129ade362SMatthew N. Dodd 		return (NULL);
16229ade362SMatthew N. Dodd }
16329ade362SMatthew N. Dodd 
16429ade362SMatthew N. Dodd static char *
16529ade362SMatthew N. Dodd lml_find (struct lm_list *lmh, const char *f)
16629ade362SMatthew N. Dodd {
16729ade362SMatthew N. Dodd 	struct lm *lm;
16829ade362SMatthew N. Dodd 
16929ade362SMatthew N. Dodd 	TAILQ_FOREACH(lm, lmh, lm_link)
17029ade362SMatthew N. Dodd 		if ((strncmp(f, lm->f, strlen(lm->f)) == 0) &&
17129ade362SMatthew N. Dodd 		    (strlen(f) == strlen(lm->f)))
17229ade362SMatthew N. Dodd 			return (lm->t);
17329ade362SMatthew N. Dodd 	return NULL;
17429ade362SMatthew N. Dodd }
17529ade362SMatthew N. Dodd 
17629ade362SMatthew N. Dodd static struct lm_list *
17729ade362SMatthew N. Dodd lmp_find (const char *n)
17829ade362SMatthew N. Dodd {
17929ade362SMatthew N. Dodd 	struct lmp *lmp;
18029ade362SMatthew N. Dodd 
18129ade362SMatthew N. Dodd 	TAILQ_FOREACH(lmp, &lmp_head, lmp_link)
18229ade362SMatthew N. Dodd 		if ((strncmp(n, lmp->p, strlen(lmp->p)) == 0) &&
18329ade362SMatthew N. Dodd 		    (strlen(n) == strlen(lmp->p)))
18429ade362SMatthew N. Dodd 			return (&lmp->lml);
18529ade362SMatthew N. Dodd 	return (NULL);
18629ade362SMatthew N. Dodd }
18729ade362SMatthew N. Dodd 
18829ade362SMatthew N. Dodd static struct lm_list *
18929ade362SMatthew N. Dodd lmp_init (char *n)
19029ade362SMatthew N. Dodd {
19129ade362SMatthew N. Dodd 	struct lmp *lmp;
19229ade362SMatthew N. Dodd 
19329ade362SMatthew N. Dodd 	lmp = malloc(sizeof(struct lmp));
19429ade362SMatthew N. Dodd 	lmp->p = n;
19529ade362SMatthew N. Dodd 	TAILQ_INIT(&lmp->lml);
19629ade362SMatthew N. Dodd 	TAILQ_INSERT_HEAD(&lmp_head, lmp, lmp_link);
19729ade362SMatthew N. Dodd 
19829ade362SMatthew N. Dodd 	return (&lmp->lml);
19929ade362SMatthew N. Dodd }
200