2003-04-07 16:21:26 +00:00
|
|
|
/*
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
2012-03-12 12:16:08 +00:00
|
|
|
#include <sys/fcntl.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/queue.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2003-04-07 16:21:26 +00:00
|
|
|
|
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
|
|
|
|
|
2004-03-21 01:21:26 +00:00
|
|
|
#ifdef COMPAT_32BIT
|
|
|
|
#undef _PATH_LIBMAP_CONF
|
|
|
|
#define _PATH_LIBMAP_CONF "/etc/libmap32.conf"
|
|
|
|
#endif
|
|
|
|
|
2003-04-07 16:21:26 +00:00
|
|
|
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;
|
2004-05-24 01:24:13 +00:00
|
|
|
enum { T_EXACT=0, T_BASENAME, T_DIRECTORY } type;
|
2003-04-07 16:21:26 +00:00
|
|
|
struct lm_list lml;
|
|
|
|
TAILQ_ENTRY(lmp) lmp_link;
|
|
|
|
};
|
|
|
|
|
2012-03-12 12:16:08 +00:00
|
|
|
static int lm_count;
|
2005-02-04 02:46:41 +00:00
|
|
|
|
2012-03-12 12:16:08 +00:00
|
|
|
static void lmc_parse(char *, size_t);
|
|
|
|
static void lm_add(const char *, const char *, const char *);
|
|
|
|
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 *);
|
|
|
|
static const char *quickbasename(const char *);
|
2003-04-07 16:21:26 +00:00
|
|
|
|
2003-04-10 01:44:19 +00:00
|
|
|
#define iseol(c) (((c) == '#') || ((c) == '\0') || \
|
|
|
|
((c) == '\n') || ((c) == '\r'))
|
|
|
|
|
2012-03-05 20:43:06 +00:00
|
|
|
/*
|
|
|
|
* Do not use ctype.h macros, which rely on working TLS. It is
|
|
|
|
* too early to have thread-local variables functional.
|
|
|
|
*/
|
2012-03-06 09:34:30 +00:00
|
|
|
#define rtld_isspace(c) ((c) == ' ' || (c) == '\t')
|
2012-03-05 20:43:06 +00:00
|
|
|
|
2003-09-13 21:43:08 +00:00
|
|
|
int
|
2012-03-12 12:16:08 +00:00
|
|
|
lm_init(char *libmap_override)
|
2003-04-07 16:21:26 +00:00
|
|
|
{
|
2012-03-12 12:16:08 +00:00
|
|
|
struct stat st;
|
|
|
|
char *lm_map, *p;
|
|
|
|
int fd;
|
2003-05-31 14:46:38 +00:00
|
|
|
|
2012-03-12 12:16:08 +00:00
|
|
|
dbg("lm_init(\"%s\")", libmap_override);
|
2003-04-07 16:21:26 +00:00
|
|
|
TAILQ_INIT(&lmp_head);
|
|
|
|
|
2012-03-12 12:16:08 +00:00
|
|
|
fd = open(_PATH_LIBMAP_CONF, O_RDONLY);
|
|
|
|
if (fd == -1) {
|
|
|
|
dbg("lm_init: open(\"%s\") failed, %s", _PATH_LIBMAP_CONF,
|
2012-03-14 15:39:59 +00:00
|
|
|
rtld_strerror(errno));
|
2012-03-12 12:16:08 +00:00
|
|
|
goto override;
|
|
|
|
}
|
|
|
|
if (fstat(fd, &st) == -1) {
|
|
|
|
close(fd);
|
|
|
|
dbg("lm_init: fstat(\"%s\") failed, %s", _PATH_LIBMAP_CONF,
|
2012-03-14 15:39:59 +00:00
|
|
|
rtld_strerror(errno));
|
2012-03-12 12:16:08 +00:00
|
|
|
goto override;
|
2005-02-04 02:46:41 +00:00
|
|
|
}
|
2012-03-12 12:16:08 +00:00
|
|
|
lm_map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
|
|
|
|
if (lm_map == (const char *)MAP_FAILED) {
|
|
|
|
close(fd);
|
|
|
|
dbg("lm_init: mmap(\"%s\") failed, %s", _PATH_LIBMAP_CONF,
|
2012-03-14 15:39:59 +00:00
|
|
|
rtld_strerror(errno));
|
2012-03-12 12:16:08 +00:00
|
|
|
goto override;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
lmc_parse(lm_map, st.st_size);
|
|
|
|
munmap(lm_map, st.st_size);
|
2005-02-04 02:46:41 +00:00
|
|
|
|
2012-03-12 12:16:08 +00:00
|
|
|
override:
|
2005-02-04 02:46:41 +00:00
|
|
|
if (libmap_override) {
|
2012-03-12 12:16:08 +00:00
|
|
|
/*
|
|
|
|
* Do some character replacement to make $LIBMAP look
|
|
|
|
* like a text file, then parse it.
|
|
|
|
*/
|
2005-02-04 02:46:41 +00:00
|
|
|
libmap_override = xstrdup(libmap_override);
|
|
|
|
for (p = libmap_override; *p; p++) {
|
|
|
|
switch (*p) {
|
2012-03-12 12:16:08 +00:00
|
|
|
case '=':
|
|
|
|
*p = ' ';
|
|
|
|
break;
|
|
|
|
case ',':
|
|
|
|
*p = '\n';
|
|
|
|
break;
|
2005-02-04 02:46:41 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-12 12:16:08 +00:00
|
|
|
lmc_parse(p, strlen(p));
|
|
|
|
free(p);
|
2005-02-04 02:46:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (lm_count == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-03-12 12:16:08 +00:00
|
|
|
lmc_parse(char *lm_p, size_t lm_len)
|
2005-02-04 02:46:41 +00:00
|
|
|
{
|
2012-03-12 12:16:08 +00:00
|
|
|
char *cp, *f, *t, *c, *p;
|
|
|
|
char prog[MAXPATHLEN];
|
|
|
|
char line[MAXPATHLEN + 2];
|
|
|
|
size_t cnt;
|
|
|
|
int i;
|
2005-02-04 02:46:41 +00:00
|
|
|
|
2012-03-12 12:16:08 +00:00
|
|
|
cnt = 0;
|
2003-04-07 16:21:26 +00:00
|
|
|
p = NULL;
|
2012-03-12 12:16:08 +00:00
|
|
|
while (cnt < lm_len) {
|
|
|
|
i = 0;
|
|
|
|
while (lm_p[cnt] != '\n' && cnt < lm_len &&
|
|
|
|
i < sizeof(line) - 1) {
|
|
|
|
line[i] = lm_p[cnt];
|
|
|
|
cnt++;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
line[i] = '\0';
|
|
|
|
while (lm_p[cnt] != '\n' && cnt < lm_len)
|
|
|
|
cnt++;
|
|
|
|
/* skip over nl */
|
|
|
|
cnt++;
|
|
|
|
|
|
|
|
cp = &line[0];
|
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 */
|
2012-03-06 09:34:30 +00:00
|
|
|
while (rtld_isspace(*cp)) cp++;
|
2003-04-10 01:44:19 +00:00
|
|
|
|
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 */
|
2012-03-06 09:34:30 +00:00
|
|
|
while (rtld_isspace(*cp)) cp++;
|
2003-04-10 01:44:19 +00:00
|
|
|
|
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 */
|
2012-03-06 09:34:30 +00:00
|
|
|
while (!rtld_isspace(*cp) && !iseol(*cp) && *cp != ']')
|
2003-04-30 19:05:53 +00:00
|
|
|
cp++;
|
2003-04-10 01:44:19 +00:00
|
|
|
|
|
|
|
/* Skip and zero out trailing space */
|
2012-03-06 09:34:30 +00:00
|
|
|
while (rtld_isspace(*cp)) *cp++ = '\0';
|
2003-04-10 01:44:19 +00:00
|
|
|
|
|
|
|
/* 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
|
|
|
*/
|
2012-03-06 09:34:30 +00:00
|
|
|
while(rtld_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++;
|
2012-03-06 09:34:30 +00:00
|
|
|
while (!rtld_isspace(*cp) && !iseol(*cp)) cp++;
|
2003-04-10 01:44:19 +00:00
|
|
|
|
|
|
|
/* Skip and zero out the trailing whitespace */
|
2012-03-06 09:34:30 +00:00
|
|
|
while (rtld_isspace(*cp)) *cp++ = '\0';
|
2003-04-10 01:44:19 +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
|
|
|
|
|
|
|
/* Parse 'to' mapping */
|
2003-04-30 19:05:53 +00:00
|
|
|
t = cp++;
|
2012-03-06 09:34:30 +00:00
|
|
|
while (!rtld_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 */
|
2012-03-06 09:34:30 +00:00
|
|
|
while (rtld_isspace(*cp)) *cp++ = '\0';
|
2003-04-30 19:05:53 +00:00
|
|
|
|
|
|
|
/* 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2005-02-04 02:46:41 +00:00
|
|
|
lm_count++;
|
2003-04-07 16:21:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2004-05-24 01:24:13 +00:00
|
|
|
/* Given a libmap translation list and a library name, return the
|
|
|
|
replacement library, or NULL */
|
2004-03-21 01:21:26 +00:00
|
|
|
#ifdef COMPAT_32BIT
|
|
|
|
char *
|
|
|
|
lm_findn (const char *p, const char *f, const int n)
|
|
|
|
{
|
|
|
|
char pathbuf[64], *s, *t;
|
|
|
|
|
2006-01-31 06:08:28 +00:00
|
|
|
if (n < sizeof(pathbuf) - 1)
|
2004-03-21 01:21:26 +00:00
|
|
|
s = pathbuf;
|
2006-01-31 06:08:28 +00:00
|
|
|
else
|
2004-03-21 01:21:26 +00:00
|
|
|
s = xmalloc(n + 1);
|
2006-01-31 06:08:28 +00:00
|
|
|
memcpy(s, f, n);
|
|
|
|
s[n] = '\0';
|
2004-03-21 01:21:26 +00:00
|
|
|
t = lm_find(p, s);
|
|
|
|
if (s != pathbuf)
|
|
|
|
free(s);
|
|
|
|
return (t);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-04-07 16:21:26 +00:00
|
|
|
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)
|
2004-03-21 01:21:26 +00:00
|
|
|
if (strcmp(f, lm->f) == 0)
|
2003-04-07 16:21:26 +00:00
|
|
|
return (lm->t);
|
2005-02-04 02:13:37 +00:00
|
|
|
return (NULL);
|
2003-04-07 16:21:26 +00:00
|
|
|
}
|
|
|
|
|
2004-05-24 01:24:13 +00:00
|
|
|
/* Given an executable name, return a pointer to the translation list or
|
|
|
|
NULL if no matches */
|
2003-04-07 16:21:26 +00:00
|
|
|
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)
|
2004-05-24 01:24:13 +00:00
|
|
|
if ((lmp->type == T_EXACT && strcmp(n, lmp->p) == 0) ||
|
|
|
|
(lmp->type == T_DIRECTORY && strncmp(n, lmp->p, strlen(lmp->p)) == 0) ||
|
|
|
|
(lmp->type == T_BASENAME && strcmp(quickbasename(n), lmp->p) == 0))
|
2003-04-07 16:21:26 +00:00
|
|
|
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;
|
2004-05-24 01:24:13 +00:00
|
|
|
if (n[strlen(n)-1] == '/')
|
|
|
|
lmp->type = T_DIRECTORY;
|
|
|
|
else if (strchr(n,'/') == NULL)
|
|
|
|
lmp->type = T_BASENAME;
|
|
|
|
else
|
|
|
|
lmp->type = T_EXACT;
|
2003-04-07 16:21:26 +00:00
|
|
|
TAILQ_INIT(&lmp->lml);
|
|
|
|
TAILQ_INSERT_HEAD(&lmp_head, lmp, lmp_link);
|
|
|
|
|
|
|
|
return (&lmp->lml);
|
|
|
|
}
|
2004-05-24 01:24:13 +00:00
|
|
|
|
|
|
|
/* libc basename is overkill. Return a pointer to the character after the
|
|
|
|
last /, or the original string if there are no slashes. */
|
|
|
|
static const char *
|
|
|
|
quickbasename (const char *path)
|
|
|
|
{
|
|
|
|
const char *p = path;
|
2005-02-04 02:13:37 +00:00
|
|
|
for (; *path; path++) {
|
2004-05-24 01:24:13 +00:00
|
|
|
if (*path == '/')
|
|
|
|
p = path+1;
|
|
|
|
}
|
2005-02-04 02:13:37 +00:00
|
|
|
return (p);
|
2004-05-24 01:24:13 +00:00
|
|
|
}
|