2003-04-07 16:21:26 +00:00
|
|
|
/*
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/queue.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
|
2003-05-19 07:10:12 +00:00
|
|
|
#include "debug.h"
|
|
|
|
#include "rtld.h"
|
2003-06-18 05:31:08 +00:00
|
|
|
#include "libmap.h"
|
2003-05-19 07:10:12 +00:00
|
|
|
|
2003-04-07 16:21:26 +00:00
|
|
|
#ifndef _PATH_LIBMAP_CONF
|
|
|
|
#define _PATH_LIBMAP_CONF "/etc/libmap.conf"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
TAILQ_HEAD(lm_list, lm);
|
|
|
|
struct lm {
|
|
|
|
char *f;
|
|
|
|
char *t;
|
|
|
|
|
|
|
|
TAILQ_ENTRY(lm) lm_link;
|
|
|
|
};
|
|
|
|
|
|
|
|
TAILQ_HEAD(lmp_list, lmp) lmp_head = TAILQ_HEAD_INITIALIZER(lmp_head);
|
|
|
|
struct lmp {
|
|
|
|
char *p;
|
|
|
|
struct lm_list lml;
|
|
|
|
TAILQ_ENTRY(lmp) lmp_link;
|
|
|
|
};
|
|
|
|
|
2003-05-30 00:49:16 +00:00
|
|
|
static void lm_add (const char *, const char *, const char *);
|
2003-04-07 16:21:26 +00:00
|
|
|
static void lm_free (struct lm_list *);
|
|
|
|
static char * lml_find (struct lm_list *, const char *);
|
|
|
|
static struct lm_list * lmp_find (const char *);
|
|
|
|
static struct lm_list * lmp_init (char *);
|
|
|
|
|
2003-04-10 01:44:19 +00:00
|
|
|
#define iseol(c) (((c) == '#') || ((c) == '\0') || \
|
|
|
|
((c) == '\n') || ((c) == '\r'))
|
|
|
|
|
2003-09-13 21:43:08 +00:00
|
|
|
int
|
2003-04-07 16:21:26 +00:00
|
|
|
lm_init (void)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
char *cp;
|
2003-05-30 00:49:16 +00:00
|
|
|
char *f, *t, *p, *c;
|
2003-04-07 16:21:26 +00:00
|
|
|
char prog[MAXPATHLEN];
|
|
|
|
char line[MAXPATHLEN + 2];
|
|
|
|
|
2003-05-31 14:46:38 +00:00
|
|
|
dbg("%s()", __func__);
|
|
|
|
|
2003-04-07 16:21:26 +00:00
|
|
|
TAILQ_INIT(&lmp_head);
|
|
|
|
|
|
|
|
if ((fp = fopen(_PATH_LIBMAP_CONF, "r")) == NULL)
|
2003-09-13 21:43:08 +00:00
|
|
|
return (1);
|
2003-04-07 16:21:26 +00:00
|
|
|
|
|
|
|
p = NULL;
|
|
|
|
while ((cp = fgets(line, MAXPATHLEN + 1, fp)) != NULL) {
|
2003-05-30 00:49:16 +00:00
|
|
|
t = f = c = NULL;
|
2003-04-10 01:44:19 +00:00
|
|
|
|
2003-04-07 16:21:26 +00:00
|
|
|
/* Skip over leading space */
|
2003-04-10 01:44:19 +00:00
|
|
|
while (isspace(*cp)) cp++;
|
|
|
|
|
2003-04-07 16:21:26 +00:00
|
|
|
/* Found a comment or EOL */
|
2003-04-30 19:05:53 +00:00
|
|
|
if (iseol(*cp)) continue;
|
2003-04-10 01:44:19 +00:00
|
|
|
|
|
|
|
/* Found a constraint selector */
|
2003-04-07 16:21:26 +00:00
|
|
|
if (*cp == '[') {
|
|
|
|
cp++;
|
2003-04-10 01:44:19 +00:00
|
|
|
|
2003-04-07 16:21:26 +00:00
|
|
|
/* Skip leading space */
|
2003-04-10 01:44:19 +00:00
|
|
|
while (isspace(*cp)) cp++;
|
|
|
|
|
2003-04-07 16:21:26 +00:00
|
|
|
/* Found comment, EOL or end of selector */
|
2003-04-30 19:05:53 +00:00
|
|
|
if (iseol(*cp) || *cp == ']')
|
|
|
|
continue;
|
2003-04-10 01:44:19 +00:00
|
|
|
|
2003-05-30 00:49:16 +00:00
|
|
|
c = cp++;
|
2003-04-07 16:21:26 +00:00
|
|
|
/* Skip to end of word */
|
2003-04-30 19:05:53 +00:00
|
|
|
while (!isspace(*cp) && !iseol(*cp) && *cp != ']')
|
|
|
|
cp++;
|
2003-04-10 01:44:19 +00:00
|
|
|
|
|
|
|
/* Skip and zero out trailing space */
|
|
|
|
while (isspace(*cp)) *cp++ = '\0';
|
|
|
|
|
|
|
|
/* Check if there is a closing brace */
|
2003-04-30 19:05:53 +00:00
|
|
|
if (*cp != ']') continue;
|
2003-04-10 01:44:19 +00:00
|
|
|
|
|
|
|
/* Terminate string if there was no trailing space */
|
2003-04-07 16:21:26 +00:00
|
|
|
*cp++ = '\0';
|
2003-04-10 01:44:19 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* There should be nothing except whitespace or comment
|
2003-05-29 22:58:26 +00:00
|
|
|
from this point to the end of the line.
|
2003-04-10 01:44:19 +00:00
|
|
|
*/
|
2004-02-03 18:53:40 +00:00
|
|
|
while(isspace(*cp)) cp++;
|
2003-04-30 19:05:53 +00:00
|
|
|
if (!iseol(*cp)) continue;
|
|
|
|
|
2003-05-30 00:49:16 +00:00
|
|
|
strcpy(prog, c);
|
2003-04-30 19:05:53 +00:00
|
|
|
p = prog;
|
|
|
|
continue;
|
2003-04-07 16:21:26 +00:00
|
|
|
}
|
2003-04-10 01:44:19 +00:00
|
|
|
|
|
|
|
/* Parse the 'from' candidate. */
|
2003-04-30 19:05:53 +00:00
|
|
|
f = cp++;
|
2003-04-10 01:44:19 +00:00
|
|
|
while (!isspace(*cp) && !iseol(*cp)) cp++;
|
|
|
|
|
|
|
|
/* Skip and zero out the trailing whitespace */
|
|
|
|
while (isspace(*cp)) *cp++ = '\0';
|
|
|
|
|
|
|
|
/* Found a comment or EOL */
|
2003-04-30 19:05:53 +00:00
|
|
|
if (iseol(*cp)) continue;
|
2003-04-10 01:44:19 +00:00
|
|
|
|
|
|
|
/* Parse 'to' mapping */
|
2003-04-30 19:05:53 +00:00
|
|
|
t = cp++;
|
2003-04-10 01:44:19 +00:00
|
|
|
while (!isspace(*cp) && !iseol(*cp)) cp++;
|
2003-05-29 22:58:26 +00:00
|
|
|
|
2003-04-30 19:05:53 +00:00
|
|
|
/* Skip and zero out the trailing whitespace */
|
|
|
|
while (isspace(*cp)) *cp++ = '\0';
|
|
|
|
|
|
|
|
/* Should be no extra tokens at this point */
|
|
|
|
if (!iseol(*cp)) continue;
|
2003-04-07 16:21:26 +00:00
|
|
|
|
2003-04-30 19:05:53 +00:00
|
|
|
*cp = '\0';
|
2003-05-30 00:49:16 +00:00
|
|
|
lm_add(p, f, t);
|
2003-04-07 16:21:26 +00:00
|
|
|
}
|
2003-04-30 19:05:53 +00:00
|
|
|
fclose(fp);
|
2003-09-13 21:43:08 +00:00
|
|
|
return (0);
|
2003-04-07 16:21:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
lm_free (struct lm_list *lml)
|
|
|
|
{
|
|
|
|
struct lm *lm;
|
|
|
|
|
2003-05-31 14:46:38 +00:00
|
|
|
dbg("%s(%p)", __func__, lml);
|
|
|
|
|
2003-04-07 16:21:26 +00:00
|
|
|
while (!TAILQ_EMPTY(lml)) {
|
|
|
|
lm = TAILQ_FIRST(lml);
|
|
|
|
TAILQ_REMOVE(lml, lm, lm_link);
|
|
|
|
free(lm->f);
|
|
|
|
free(lm->t);
|
|
|
|
free(lm);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
lm_fini (void)
|
|
|
|
{
|
|
|
|
struct lmp *lmp;
|
|
|
|
|
2003-05-31 14:46:38 +00:00
|
|
|
dbg("%s()", __func__);
|
|
|
|
|
2003-04-07 16:21:26 +00:00
|
|
|
while (!TAILQ_EMPTY(&lmp_head)) {
|
|
|
|
lmp = TAILQ_FIRST(&lmp_head);
|
|
|
|
TAILQ_REMOVE(&lmp_head, lmp, lmp_link);
|
|
|
|
free(lmp->p);
|
|
|
|
lm_free(&lmp->lml);
|
|
|
|
free(lmp);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2003-05-30 00:49:16 +00:00
|
|
|
lm_add (const char *p, const char *f, const char *t)
|
2003-04-07 16:21:26 +00:00
|
|
|
{
|
|
|
|
struct lm_list *lml;
|
|
|
|
struct lm *lm;
|
|
|
|
|
|
|
|
if (p == NULL)
|
|
|
|
p = "$DEFAULT$";
|
|
|
|
|
2003-05-31 14:46:38 +00:00
|
|
|
dbg("%s(\"%s\", \"%s\", \"%s\")", __func__, p, f, t);
|
|
|
|
|
2003-04-07 16:21:26 +00:00
|
|
|
if ((lml = lmp_find(p)) == NULL)
|
2003-05-19 07:10:12 +00:00
|
|
|
lml = lmp_init(xstrdup(p));
|
2003-04-07 16:21:26 +00:00
|
|
|
|
2003-05-19 07:10:12 +00:00
|
|
|
lm = xmalloc(sizeof(struct lm));
|
2003-05-30 00:49:16 +00:00
|
|
|
lm->f = xstrdup(f);
|
|
|
|
lm->t = xstrdup(t);
|
2003-04-07 16:21:26 +00:00
|
|
|
TAILQ_INSERT_HEAD(lml, lm, lm_link);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
lm_find (const char *p, const char *f)
|
|
|
|
{
|
|
|
|
struct lm_list *lml;
|
|
|
|
char *t;
|
|
|
|
|
2003-05-31 14:46:38 +00:00
|
|
|
dbg("%s(\"%s\", \"%s\")", __func__, p, f);
|
|
|
|
|
2003-04-07 16:21:26 +00:00
|
|
|
if (p != NULL && (lml = lmp_find(p)) != NULL) {
|
|
|
|
t = lml_find(lml, f);
|
2003-05-19 07:10:12 +00:00
|
|
|
if (t != NULL) {
|
|
|
|
/*
|
|
|
|
* Add a global mapping if we have
|
|
|
|
* a successful constrained match.
|
|
|
|
*/
|
2003-05-30 00:49:16 +00:00
|
|
|
lm_add(NULL, f, t);
|
2003-04-07 16:21:26 +00:00
|
|
|
return (t);
|
2003-05-19 07:10:12 +00:00
|
|
|
}
|
2003-04-07 16:21:26 +00:00
|
|
|
}
|
|
|
|
lml = lmp_find("$DEFAULT$");
|
|
|
|
if (lml != NULL)
|
|
|
|
return (lml_find(lml, f));
|
|
|
|
else
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
lml_find (struct lm_list *lmh, const char *f)
|
|
|
|
{
|
|
|
|
struct lm *lm;
|
|
|
|
|
2003-05-31 14:46:38 +00:00
|
|
|
dbg("%s(%p, \"%s\")", __func__, lmh, f);
|
|
|
|
|
2003-04-07 16:21:26 +00:00
|
|
|
TAILQ_FOREACH(lm, lmh, lm_link)
|
|
|
|
if ((strncmp(f, lm->f, strlen(lm->f)) == 0) &&
|
|
|
|
(strlen(f) == strlen(lm->f)))
|
|
|
|
return (lm->t);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct lm_list *
|
|
|
|
lmp_find (const char *n)
|
|
|
|
{
|
|
|
|
struct lmp *lmp;
|
|
|
|
|
2003-05-31 14:46:38 +00:00
|
|
|
dbg("%s(\"%s\")", __func__, n);
|
|
|
|
|
2003-04-07 16:21:26 +00:00
|
|
|
TAILQ_FOREACH(lmp, &lmp_head, lmp_link)
|
|
|
|
if ((strncmp(n, lmp->p, strlen(lmp->p)) == 0) &&
|
|
|
|
(strlen(n) == strlen(lmp->p)))
|
|
|
|
return (&lmp->lml);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct lm_list *
|
|
|
|
lmp_init (char *n)
|
|
|
|
{
|
|
|
|
struct lmp *lmp;
|
|
|
|
|
2003-05-31 14:46:38 +00:00
|
|
|
dbg("%s(\"%s\")", __func__, n);
|
|
|
|
|
2003-05-19 07:10:12 +00:00
|
|
|
lmp = xmalloc(sizeof(struct lmp));
|
2003-04-07 16:21:26 +00:00
|
|
|
lmp->p = n;
|
|
|
|
TAILQ_INIT(&lmp->lml);
|
|
|
|
TAILQ_INSERT_HEAD(&lmp_head, lmp, lmp_link);
|
|
|
|
|
|
|
|
return (&lmp->lml);
|
|
|
|
}
|