kernel: Add some missing headers to the Makefiles of cryptodev and tun.
[dragonfly.git] / libexec / rtld-elf / libmap.c
blobedc759ea835c1609b08dc35f7e056272253a9ff3
1 /*
2 * $FreeBSD: src/libexec/rtld-elf/libmap.c,v 1.15 2006/01/31 06:08:28 peter Exp $
3 */
5 #include <stdio.h>
6 #include <ctype.h>
7 #include <string.h>
8 #include <stdlib.h>
9 #include <sys/queue.h>
10 #include <sys/param.h>
12 #include "debug.h"
13 #include "rtld.h"
14 #include "libmap.h"
16 #ifndef _PATH_LIBMAP_CONF
17 #define _PATH_LIBMAP_CONF "/etc/libmap.conf"
18 #endif
20 TAILQ_HEAD(lm_list, lm);
21 struct lm {
22 char *f;
23 char *t;
25 TAILQ_ENTRY(lm) lm_link;
28 TAILQ_HEAD(lmp_list, lmp) lmp_head = TAILQ_HEAD_INITIALIZER(lmp_head);
29 struct lmp {
30 char *p;
31 enum { T_EXACT=0, T_BASENAME, T_DIRECTORY } type;
32 struct lm_list lml;
33 TAILQ_ENTRY(lmp) lmp_link;
36 static int lm_count;
38 static void lmc_parse (FILE *);
39 static void lm_add (const char *, const char *, const char *);
40 static void lm_free (struct lm_list *);
41 static char * lml_find (struct lm_list *, const char *);
42 static struct lm_list * lmp_find (const char *);
43 static struct lm_list * lmp_init (char *);
44 static const char * quickbasename (const char *);
45 static int readstrfn (void * cookie, char *buf, int len);
46 static int closestrfn (void * cookie);
48 #define iseol(c) (((c) == '#') || ((c) == '\0') || \
49 ((c) == '\n') || ((c) == '\r'))
51 int
52 lm_init (char *libmap_override)
54 FILE *fp;
56 dbg("%s(\"%s\")", __func__, libmap_override);
58 TAILQ_INIT(&lmp_head);
60 fp = fopen(_PATH_LIBMAP_CONF, "r");
61 if (fp) {
62 lmc_parse(fp);
63 fclose(fp);
66 if (libmap_override) {
67 char *p;
68 /* do some character replacement to make $LIBMAP look like a
69 text file, then "open" it with funopen */
70 libmap_override = xstrdup(libmap_override);
72 for (p = libmap_override; *p; p++) {
73 switch (*p) {
74 case '=':
75 *p = ' '; break;
76 case ',':
77 *p = '\n'; break;
80 fp = funopen(libmap_override, readstrfn, NULL, NULL, closestrfn);
81 if (fp) {
82 lmc_parse(fp);
83 fclose(fp);
87 return (lm_count == 0);
90 static void
91 lmc_parse (FILE *fp)
93 char *cp;
94 char *f, *t, *c, *p;
95 char prog[MAXPATHLEN];
96 char line[MAXPATHLEN + 2];
98 dbg("%s(%p)", __func__, fp);
100 p = NULL;
101 while ((cp = fgets(line, MAXPATHLEN + 1, fp)) != NULL) {
102 t = f = c = NULL;
104 /* Skip over leading space */
105 while (isspace(*cp)) cp++;
107 /* Found a comment or EOL */
108 if (iseol(*cp)) continue;
110 /* Found a constraint selector */
111 if (*cp == '[') {
112 cp++;
114 /* Skip leading space */
115 while (isspace(*cp)) cp++;
117 /* Found comment, EOL or end of selector */
118 if (iseol(*cp) || *cp == ']')
119 continue;
121 c = cp++;
122 /* Skip to end of word */
123 while (!isspace(*cp) && !iseol(*cp) && *cp != ']')
124 cp++;
126 /* Skip and zero out trailing space */
127 while (isspace(*cp)) *cp++ = '\0';
129 /* Check if there is a closing brace */
130 if (*cp != ']') continue;
132 /* Terminate string if there was no trailing space */
133 *cp++ = '\0';
136 * There should be nothing except whitespace or comment
137 from this point to the end of the line.
139 while(isspace(*cp)) cp++;
140 if (!iseol(*cp)) continue;
142 strcpy(prog, c);
143 p = prog;
144 continue;
147 /* Parse the 'from' candidate. */
148 f = cp++;
149 while (!isspace(*cp) && !iseol(*cp)) cp++;
151 /* Skip and zero out the trailing whitespace */
152 while (isspace(*cp)) *cp++ = '\0';
154 /* Found a comment or EOL */
155 if (iseol(*cp)) continue;
157 /* Parse 'to' mapping */
158 t = cp++;
159 while (!isspace(*cp) && !iseol(*cp)) cp++;
161 /* Skip and zero out the trailing whitespace */
162 while (isspace(*cp)) *cp++ = '\0';
164 /* Should be no extra tokens at this point */
165 if (!iseol(*cp)) continue;
167 *cp = '\0';
168 lm_add(p, f, t);
172 static void
173 lm_free (struct lm_list *lml)
175 struct lm *lm;
177 dbg("%s(%p)", __func__, lml);
179 while (!TAILQ_EMPTY(lml)) {
180 lm = TAILQ_FIRST(lml);
181 TAILQ_REMOVE(lml, lm, lm_link);
182 free(lm->f);
183 free(lm->t);
184 free(lm);
186 return;
189 void
190 lm_fini (void)
192 struct lmp *lmp;
194 dbg("%s()", __func__);
196 while (!TAILQ_EMPTY(&lmp_head)) {
197 lmp = TAILQ_FIRST(&lmp_head);
198 TAILQ_REMOVE(&lmp_head, lmp, lmp_link);
199 free(lmp->p);
200 lm_free(&lmp->lml);
201 free(lmp);
203 return;
206 static void
207 lm_add (const char *p, const char *f, const char *t)
209 struct lm_list *lml;
210 struct lm *lm;
212 if (p == NULL)
213 p = "$DEFAULT$";
215 dbg("%s(\"%s\", \"%s\", \"%s\")", __func__, p, f, t);
217 if ((lml = lmp_find(p)) == NULL)
218 lml = lmp_init(xstrdup(p));
220 lm = xmalloc(sizeof(struct lm));
221 lm->f = xstrdup(f);
222 lm->t = xstrdup(t);
223 TAILQ_INSERT_HEAD(lml, lm, lm_link);
224 lm_count++;
227 char *
228 lm_find (const char *p, const char *f)
230 struct lm_list *lml;
231 char *t;
233 dbg("%s(\"%s\", \"%s\")", __func__, p, f);
235 if (p != NULL && (lml = lmp_find(p)) != NULL) {
236 t = lml_find(lml, f);
237 if (t != NULL) {
239 * Add a global mapping if we have
240 * a successful constrained match.
242 lm_add(NULL, f, t);
243 return (t);
246 lml = lmp_find("$DEFAULT$");
247 if (lml != NULL)
248 return (lml_find(lml, f));
249 else
250 return (NULL);
253 static char *
254 lml_find (struct lm_list *lmh, const char *f)
256 struct lm *lm;
258 dbg("%s(%p, \"%s\")", __func__, lmh, f);
260 TAILQ_FOREACH(lm, lmh, lm_link)
261 if (strcmp(f, lm->f) == 0)
262 return (lm->t);
263 return (NULL);
266 /* Given an executable name, return a pointer to the translation list or
267 NULL if no matches */
268 static struct lm_list *
269 lmp_find (const char *n)
271 struct lmp *lmp;
273 dbg("%s(\"%s\")", __func__, n);
275 TAILQ_FOREACH(lmp, &lmp_head, lmp_link)
276 if ((lmp->type == T_EXACT && strcmp(n, lmp->p) == 0) ||
277 (lmp->type == T_DIRECTORY && strncmp(n, lmp->p, strlen(lmp->p)) == 0) ||
278 (lmp->type == T_BASENAME && strcmp(quickbasename(n), lmp->p) == 0))
279 return (&lmp->lml);
280 return (NULL);
283 static struct lm_list *
284 lmp_init (char *n)
286 struct lmp *lmp;
288 dbg("%s(\"%s\")", __func__, n);
290 lmp = xmalloc(sizeof(struct lmp));
291 lmp->p = n;
292 if (n[strlen(n)-1] == '/')
293 lmp->type = T_DIRECTORY;
294 else if (strchr(n,'/') == NULL)
295 lmp->type = T_BASENAME;
296 else
297 lmp->type = T_EXACT;
298 TAILQ_INIT(&lmp->lml);
299 TAILQ_INSERT_HEAD(&lmp_head, lmp, lmp_link);
301 return (&lmp->lml);
304 /* libc basename is overkill. Return a pointer to the character after the
305 last /, or the original string if there are no slashes. */
306 static const char *
307 quickbasename (const char *path)
309 const char *p = path;
310 for (; *path; path++) {
311 if (*path == '/')
312 p = path+1;
314 return (p);
317 static int
318 readstrfn(void * cookie, char *buf, int len)
320 static char *current;
321 static int left;
322 int copied;
324 copied = 0;
325 if (!current) {
326 current = cookie;
327 left = strlen(cookie);
329 while (*current && left && len) {
330 *buf++ = *current++;
331 left--;
332 len--;
333 copied++;
335 return copied;
338 static int
339 closestrfn(void * cookie)
341 free(cookie);
342 return 0;