2003-04-07 16:21:26 +00:00
|
|
|
/*
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
|
2012-04-30 21:26:35 +00:00
|
|
|
#include <sys/types.h>
|
2003-04-07 16:21:26 +00:00
|
|
|
#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>
|
2012-04-30 21:26:35 +00:00
|
|
|
#include <dirent.h>
|
2012-03-12 12:16:08 +00:00
|
|
|
#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"
|
2015-10-31 04:39:28 +00:00
|
|
|
#include "paths.h"
|
2004-03-21 01:21:26 +00:00
|
|
|
|
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-04-30 21:26:35 +00:00
|
|
|
static TAILQ_HEAD(lmc_list, lmc) lmc_head = TAILQ_HEAD_INITIALIZER(lmc_head);
|
|
|
|
struct lmc {
|
|
|
|
char *path;
|
Don't call realpath(3) from libmap rtld code. This gets rid of a few calls
to fstatat(2) at binary startup; the difference looks like this:
--- przed 2017-10-14 13:55:49.983528000 +0100
+++ po 2017-10-14 14:10:39.134343000 +0100
@@ -1,15 +1,10 @@
mmap(0x0,32768,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANON,-1,0x0) = 34366173184 (0x800623000)
issetugid() = 0 (0x0)
-fstatat(AT_FDCWD,"/etc",{ mode=drwxr-xr-x ,inode=1364352,size=2560,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
-fstatat(AT_FDCWD,"/etc/libmap.conf",{ mode=-rw-r--r-- ,inode=1373288,size=102,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
openat(AT_FDCWD,"/etc/libmap.conf",O_RDONLY|O_CLOEXEC,00) = 3 (0x3)
fstat(3,{ mode=-rw-r--r-- ,inode=1373288,size=102,blksize=32768 }) = 0 (0x0)
mmap(0x0,102,PROT_READ,MAP_PRIVATE,3,0x0) = 34366205952 (0x80062b000)
close(3) = 0 (0x0)
-fstatat(AT_FDCWD,"/usr",{ mode=drwxr-xr-x ,inode=561792,size=512,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
-fstatat(AT_FDCWD,"/usr/local",{ mode=drwxr-xr-x ,inode=561800,size=512,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
-fstatat(AT_FDCWD,"/usr/local/etc",{ mode=drwxr-xr-x ,inode=653279,size=1536,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
-fstatat(AT_FDCWD,"/usr/local/etc/libmap.d",0x7fffffffcf50,AT_SYMLINK_NOFOLLOW) ERR#2 'No such file or directory'
+open("/usr/local/etc/libmap.d",O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC,0165) ERR#2 'No such file or directory'
munmap(0x80062b000,102) = 0 (0x0)
openat(AT_FDCWD,"/var/run/ld-elf.so.hints",O_RDONLY|O_CLOEXEC,00) = 3 (0x3)
read(3,"Ehnt\^A\0\0\0\M^@\0\0\0\M-2\0\0"...,128) = 128 (0x80)
Reviewed by: kib
MFC after: 2 weeks
Sponsored by: DARPA, AFRL
Differential Revision: https://reviews.freebsd.org/D12741
2017-10-22 10:32:40 +00:00
|
|
|
dev_t dev;
|
|
|
|
ino_t ino;
|
2012-04-30 21:26:35 +00:00
|
|
|
TAILQ_ENTRY(lmc) next;
|
|
|
|
};
|
|
|
|
|
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);
|
2012-04-30 21:26:35 +00:00
|
|
|
static void lmc_parse_file(char *);
|
|
|
|
static void lmc_parse_dir(char *);
|
2012-03-12 12:16:08 +00:00
|
|
|
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-04-30 21:26:35 +00:00
|
|
|
char *p;
|
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);
|
|
|
|
|
2015-10-31 04:39:55 +00:00
|
|
|
lmc_parse_file(ld_path_libmap_conf);
|
2005-02-04 02:46:41 +00:00
|
|
|
|
|
|
|
if (libmap_override) {
|
2012-03-12 12:16:08 +00:00
|
|
|
/*
|
2014-05-18 17:14:08 +00:00
|
|
|
* Do some character replacement to make $LDLIBMAP look
|
2012-03-12 12:16:08 +00:00
|
|
|
* 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
|
|
|
}
|
|
|
|
}
|
2014-05-18 17:14:08 +00:00
|
|
|
lmc_parse(libmap_override, p - libmap_override);
|
|
|
|
free(libmap_override);
|
2005-02-04 02:46:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (lm_count == 0);
|
|
|
|
}
|
|
|
|
|
2012-04-30 21:26:35 +00:00
|
|
|
static void
|
|
|
|
lmc_parse_file(char *path)
|
|
|
|
{
|
|
|
|
struct lmc *p;
|
|
|
|
struct stat st;
|
2017-10-24 10:48:26 +00:00
|
|
|
ssize_t retval;
|
2012-04-30 21:26:35 +00:00
|
|
|
int fd;
|
|
|
|
char *lm_map;
|
|
|
|
|
|
|
|
TAILQ_FOREACH(p, &lmc_head, next) {
|
Don't call realpath(3) from libmap rtld code. This gets rid of a few calls
to fstatat(2) at binary startup; the difference looks like this:
--- przed 2017-10-14 13:55:49.983528000 +0100
+++ po 2017-10-14 14:10:39.134343000 +0100
@@ -1,15 +1,10 @@
mmap(0x0,32768,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANON,-1,0x0) = 34366173184 (0x800623000)
issetugid() = 0 (0x0)
-fstatat(AT_FDCWD,"/etc",{ mode=drwxr-xr-x ,inode=1364352,size=2560,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
-fstatat(AT_FDCWD,"/etc/libmap.conf",{ mode=-rw-r--r-- ,inode=1373288,size=102,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
openat(AT_FDCWD,"/etc/libmap.conf",O_RDONLY|O_CLOEXEC,00) = 3 (0x3)
fstat(3,{ mode=-rw-r--r-- ,inode=1373288,size=102,blksize=32768 }) = 0 (0x0)
mmap(0x0,102,PROT_READ,MAP_PRIVATE,3,0x0) = 34366205952 (0x80062b000)
close(3) = 0 (0x0)
-fstatat(AT_FDCWD,"/usr",{ mode=drwxr-xr-x ,inode=561792,size=512,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
-fstatat(AT_FDCWD,"/usr/local",{ mode=drwxr-xr-x ,inode=561800,size=512,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
-fstatat(AT_FDCWD,"/usr/local/etc",{ mode=drwxr-xr-x ,inode=653279,size=1536,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
-fstatat(AT_FDCWD,"/usr/local/etc/libmap.d",0x7fffffffcf50,AT_SYMLINK_NOFOLLOW) ERR#2 'No such file or directory'
+open("/usr/local/etc/libmap.d",O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC,0165) ERR#2 'No such file or directory'
munmap(0x80062b000,102) = 0 (0x0)
openat(AT_FDCWD,"/var/run/ld-elf.so.hints",O_RDONLY|O_CLOEXEC,00) = 3 (0x3)
read(3,"Ehnt\^A\0\0\0\M^@\0\0\0\M-2\0\0"...,128) = 128 (0x80)
Reviewed by: kib
MFC after: 2 weeks
Sponsored by: DARPA, AFRL
Differential Revision: https://reviews.freebsd.org/D12741
2017-10-22 10:32:40 +00:00
|
|
|
if (strcmp(p->path, path) == 0)
|
2012-04-30 21:26:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
Don't call realpath(3) from libmap rtld code. This gets rid of a few calls
to fstatat(2) at binary startup; the difference looks like this:
--- przed 2017-10-14 13:55:49.983528000 +0100
+++ po 2017-10-14 14:10:39.134343000 +0100
@@ -1,15 +1,10 @@
mmap(0x0,32768,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANON,-1,0x0) = 34366173184 (0x800623000)
issetugid() = 0 (0x0)
-fstatat(AT_FDCWD,"/etc",{ mode=drwxr-xr-x ,inode=1364352,size=2560,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
-fstatat(AT_FDCWD,"/etc/libmap.conf",{ mode=-rw-r--r-- ,inode=1373288,size=102,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
openat(AT_FDCWD,"/etc/libmap.conf",O_RDONLY|O_CLOEXEC,00) = 3 (0x3)
fstat(3,{ mode=-rw-r--r-- ,inode=1373288,size=102,blksize=32768 }) = 0 (0x0)
mmap(0x0,102,PROT_READ,MAP_PRIVATE,3,0x0) = 34366205952 (0x80062b000)
close(3) = 0 (0x0)
-fstatat(AT_FDCWD,"/usr",{ mode=drwxr-xr-x ,inode=561792,size=512,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
-fstatat(AT_FDCWD,"/usr/local",{ mode=drwxr-xr-x ,inode=561800,size=512,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
-fstatat(AT_FDCWD,"/usr/local/etc",{ mode=drwxr-xr-x ,inode=653279,size=1536,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
-fstatat(AT_FDCWD,"/usr/local/etc/libmap.d",0x7fffffffcf50,AT_SYMLINK_NOFOLLOW) ERR#2 'No such file or directory'
+open("/usr/local/etc/libmap.d",O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC,0165) ERR#2 'No such file or directory'
munmap(0x80062b000,102) = 0 (0x0)
openat(AT_FDCWD,"/var/run/ld-elf.so.hints",O_RDONLY|O_CLOEXEC,00) = 3 (0x3)
read(3,"Ehnt\^A\0\0\0\M^@\0\0\0\M-2\0\0"...,128) = 128 (0x80)
Reviewed by: kib
MFC after: 2 weeks
Sponsored by: DARPA, AFRL
Differential Revision: https://reviews.freebsd.org/D12741
2017-10-22 10:32:40 +00:00
|
|
|
fd = open(path, O_RDONLY | O_CLOEXEC);
|
2012-04-30 21:26:35 +00:00
|
|
|
if (fd == -1) {
|
Don't call realpath(3) from libmap rtld code. This gets rid of a few calls
to fstatat(2) at binary startup; the difference looks like this:
--- przed 2017-10-14 13:55:49.983528000 +0100
+++ po 2017-10-14 14:10:39.134343000 +0100
@@ -1,15 +1,10 @@
mmap(0x0,32768,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANON,-1,0x0) = 34366173184 (0x800623000)
issetugid() = 0 (0x0)
-fstatat(AT_FDCWD,"/etc",{ mode=drwxr-xr-x ,inode=1364352,size=2560,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
-fstatat(AT_FDCWD,"/etc/libmap.conf",{ mode=-rw-r--r-- ,inode=1373288,size=102,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
openat(AT_FDCWD,"/etc/libmap.conf",O_RDONLY|O_CLOEXEC,00) = 3 (0x3)
fstat(3,{ mode=-rw-r--r-- ,inode=1373288,size=102,blksize=32768 }) = 0 (0x0)
mmap(0x0,102,PROT_READ,MAP_PRIVATE,3,0x0) = 34366205952 (0x80062b000)
close(3) = 0 (0x0)
-fstatat(AT_FDCWD,"/usr",{ mode=drwxr-xr-x ,inode=561792,size=512,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
-fstatat(AT_FDCWD,"/usr/local",{ mode=drwxr-xr-x ,inode=561800,size=512,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
-fstatat(AT_FDCWD,"/usr/local/etc",{ mode=drwxr-xr-x ,inode=653279,size=1536,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
-fstatat(AT_FDCWD,"/usr/local/etc/libmap.d",0x7fffffffcf50,AT_SYMLINK_NOFOLLOW) ERR#2 'No such file or directory'
+open("/usr/local/etc/libmap.d",O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC,0165) ERR#2 'No such file or directory'
munmap(0x80062b000,102) = 0 (0x0)
openat(AT_FDCWD,"/var/run/ld-elf.so.hints",O_RDONLY|O_CLOEXEC,00) = 3 (0x3)
read(3,"Ehnt\^A\0\0\0\M^@\0\0\0\M-2\0\0"...,128) = 128 (0x80)
Reviewed by: kib
MFC after: 2 weeks
Sponsored by: DARPA, AFRL
Differential Revision: https://reviews.freebsd.org/D12741
2017-10-22 10:32:40 +00:00
|
|
|
dbg("lm_parse_file: open(\"%s\") failed, %s", path,
|
2012-04-30 21:26:35 +00:00
|
|
|
rtld_strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (fstat(fd, &st) == -1) {
|
|
|
|
close(fd);
|
Don't call realpath(3) from libmap rtld code. This gets rid of a few calls
to fstatat(2) at binary startup; the difference looks like this:
--- przed 2017-10-14 13:55:49.983528000 +0100
+++ po 2017-10-14 14:10:39.134343000 +0100
@@ -1,15 +1,10 @@
mmap(0x0,32768,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANON,-1,0x0) = 34366173184 (0x800623000)
issetugid() = 0 (0x0)
-fstatat(AT_FDCWD,"/etc",{ mode=drwxr-xr-x ,inode=1364352,size=2560,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
-fstatat(AT_FDCWD,"/etc/libmap.conf",{ mode=-rw-r--r-- ,inode=1373288,size=102,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
openat(AT_FDCWD,"/etc/libmap.conf",O_RDONLY|O_CLOEXEC,00) = 3 (0x3)
fstat(3,{ mode=-rw-r--r-- ,inode=1373288,size=102,blksize=32768 }) = 0 (0x0)
mmap(0x0,102,PROT_READ,MAP_PRIVATE,3,0x0) = 34366205952 (0x80062b000)
close(3) = 0 (0x0)
-fstatat(AT_FDCWD,"/usr",{ mode=drwxr-xr-x ,inode=561792,size=512,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
-fstatat(AT_FDCWD,"/usr/local",{ mode=drwxr-xr-x ,inode=561800,size=512,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
-fstatat(AT_FDCWD,"/usr/local/etc",{ mode=drwxr-xr-x ,inode=653279,size=1536,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
-fstatat(AT_FDCWD,"/usr/local/etc/libmap.d",0x7fffffffcf50,AT_SYMLINK_NOFOLLOW) ERR#2 'No such file or directory'
+open("/usr/local/etc/libmap.d",O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC,0165) ERR#2 'No such file or directory'
munmap(0x80062b000,102) = 0 (0x0)
openat(AT_FDCWD,"/var/run/ld-elf.so.hints",O_RDONLY|O_CLOEXEC,00) = 3 (0x3)
read(3,"Ehnt\^A\0\0\0\M^@\0\0\0\M-2\0\0"...,128) = 128 (0x80)
Reviewed by: kib
MFC after: 2 weeks
Sponsored by: DARPA, AFRL
Differential Revision: https://reviews.freebsd.org/D12741
2017-10-22 10:32:40 +00:00
|
|
|
dbg("lm_parse_file: fstat(\"%s\") failed, %s", path,
|
2012-04-30 21:26:35 +00:00
|
|
|
rtld_strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
Don't call realpath(3) from libmap rtld code. This gets rid of a few calls
to fstatat(2) at binary startup; the difference looks like this:
--- przed 2017-10-14 13:55:49.983528000 +0100
+++ po 2017-10-14 14:10:39.134343000 +0100
@@ -1,15 +1,10 @@
mmap(0x0,32768,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANON,-1,0x0) = 34366173184 (0x800623000)
issetugid() = 0 (0x0)
-fstatat(AT_FDCWD,"/etc",{ mode=drwxr-xr-x ,inode=1364352,size=2560,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
-fstatat(AT_FDCWD,"/etc/libmap.conf",{ mode=-rw-r--r-- ,inode=1373288,size=102,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
openat(AT_FDCWD,"/etc/libmap.conf",O_RDONLY|O_CLOEXEC,00) = 3 (0x3)
fstat(3,{ mode=-rw-r--r-- ,inode=1373288,size=102,blksize=32768 }) = 0 (0x0)
mmap(0x0,102,PROT_READ,MAP_PRIVATE,3,0x0) = 34366205952 (0x80062b000)
close(3) = 0 (0x0)
-fstatat(AT_FDCWD,"/usr",{ mode=drwxr-xr-x ,inode=561792,size=512,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
-fstatat(AT_FDCWD,"/usr/local",{ mode=drwxr-xr-x ,inode=561800,size=512,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
-fstatat(AT_FDCWD,"/usr/local/etc",{ mode=drwxr-xr-x ,inode=653279,size=1536,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
-fstatat(AT_FDCWD,"/usr/local/etc/libmap.d",0x7fffffffcf50,AT_SYMLINK_NOFOLLOW) ERR#2 'No such file or directory'
+open("/usr/local/etc/libmap.d",O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC,0165) ERR#2 'No such file or directory'
munmap(0x80062b000,102) = 0 (0x0)
openat(AT_FDCWD,"/var/run/ld-elf.so.hints",O_RDONLY|O_CLOEXEC,00) = 3 (0x3)
read(3,"Ehnt\^A\0\0\0\M^@\0\0\0\M-2\0\0"...,128) = 128 (0x80)
Reviewed by: kib
MFC after: 2 weeks
Sponsored by: DARPA, AFRL
Differential Revision: https://reviews.freebsd.org/D12741
2017-10-22 10:32:40 +00:00
|
|
|
|
|
|
|
TAILQ_FOREACH(p, &lmc_head, next) {
|
|
|
|
if (p->dev == st.st_dev && p->ino == st.st_ino) {
|
|
|
|
close(fd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-24 10:48:26 +00:00
|
|
|
lm_map = xmalloc(st.st_size);
|
|
|
|
retval = read(fd, lm_map, st.st_size);
|
|
|
|
if (retval != st.st_size) {
|
2012-04-30 21:26:35 +00:00
|
|
|
close(fd);
|
2017-10-29 12:07:27 +00:00
|
|
|
free(lm_map);
|
2017-10-24 10:48:26 +00:00
|
|
|
dbg("lm_parse_file: read(\"%s\") failed, %s", path,
|
2012-04-30 21:26:35 +00:00
|
|
|
rtld_strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
p = xmalloc(sizeof(struct lmc));
|
Don't call realpath(3) from libmap rtld code. This gets rid of a few calls
to fstatat(2) at binary startup; the difference looks like this:
--- przed 2017-10-14 13:55:49.983528000 +0100
+++ po 2017-10-14 14:10:39.134343000 +0100
@@ -1,15 +1,10 @@
mmap(0x0,32768,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANON,-1,0x0) = 34366173184 (0x800623000)
issetugid() = 0 (0x0)
-fstatat(AT_FDCWD,"/etc",{ mode=drwxr-xr-x ,inode=1364352,size=2560,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
-fstatat(AT_FDCWD,"/etc/libmap.conf",{ mode=-rw-r--r-- ,inode=1373288,size=102,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
openat(AT_FDCWD,"/etc/libmap.conf",O_RDONLY|O_CLOEXEC,00) = 3 (0x3)
fstat(3,{ mode=-rw-r--r-- ,inode=1373288,size=102,blksize=32768 }) = 0 (0x0)
mmap(0x0,102,PROT_READ,MAP_PRIVATE,3,0x0) = 34366205952 (0x80062b000)
close(3) = 0 (0x0)
-fstatat(AT_FDCWD,"/usr",{ mode=drwxr-xr-x ,inode=561792,size=512,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
-fstatat(AT_FDCWD,"/usr/local",{ mode=drwxr-xr-x ,inode=561800,size=512,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
-fstatat(AT_FDCWD,"/usr/local/etc",{ mode=drwxr-xr-x ,inode=653279,size=1536,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
-fstatat(AT_FDCWD,"/usr/local/etc/libmap.d",0x7fffffffcf50,AT_SYMLINK_NOFOLLOW) ERR#2 'No such file or directory'
+open("/usr/local/etc/libmap.d",O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC,0165) ERR#2 'No such file or directory'
munmap(0x80062b000,102) = 0 (0x0)
openat(AT_FDCWD,"/var/run/ld-elf.so.hints",O_RDONLY|O_CLOEXEC,00) = 3 (0x3)
read(3,"Ehnt\^A\0\0\0\M^@\0\0\0\M-2\0\0"...,128) = 128 (0x80)
Reviewed by: kib
MFC after: 2 weeks
Sponsored by: DARPA, AFRL
Differential Revision: https://reviews.freebsd.org/D12741
2017-10-22 10:32:40 +00:00
|
|
|
p->path = xstrdup(path);
|
|
|
|
p->dev = st.st_dev;
|
|
|
|
p->ino = st.st_ino;
|
2012-04-30 21:26:35 +00:00
|
|
|
TAILQ_INSERT_HEAD(&lmc_head, p, next);
|
|
|
|
lmc_parse(lm_map, st.st_size);
|
2017-10-24 10:48:26 +00:00
|
|
|
free(lm_map);
|
2012-04-30 21:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
lmc_parse_dir(char *idir)
|
|
|
|
{
|
|
|
|
DIR *d;
|
|
|
|
struct dirent *dp;
|
|
|
|
struct lmc *p;
|
|
|
|
char conffile[MAXPATHLEN];
|
|
|
|
char *ext;
|
|
|
|
|
|
|
|
TAILQ_FOREACH(p, &lmc_head, next) {
|
Don't call realpath(3) from libmap rtld code. This gets rid of a few calls
to fstatat(2) at binary startup; the difference looks like this:
--- przed 2017-10-14 13:55:49.983528000 +0100
+++ po 2017-10-14 14:10:39.134343000 +0100
@@ -1,15 +1,10 @@
mmap(0x0,32768,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANON,-1,0x0) = 34366173184 (0x800623000)
issetugid() = 0 (0x0)
-fstatat(AT_FDCWD,"/etc",{ mode=drwxr-xr-x ,inode=1364352,size=2560,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
-fstatat(AT_FDCWD,"/etc/libmap.conf",{ mode=-rw-r--r-- ,inode=1373288,size=102,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
openat(AT_FDCWD,"/etc/libmap.conf",O_RDONLY|O_CLOEXEC,00) = 3 (0x3)
fstat(3,{ mode=-rw-r--r-- ,inode=1373288,size=102,blksize=32768 }) = 0 (0x0)
mmap(0x0,102,PROT_READ,MAP_PRIVATE,3,0x0) = 34366205952 (0x80062b000)
close(3) = 0 (0x0)
-fstatat(AT_FDCWD,"/usr",{ mode=drwxr-xr-x ,inode=561792,size=512,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
-fstatat(AT_FDCWD,"/usr/local",{ mode=drwxr-xr-x ,inode=561800,size=512,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
-fstatat(AT_FDCWD,"/usr/local/etc",{ mode=drwxr-xr-x ,inode=653279,size=1536,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
-fstatat(AT_FDCWD,"/usr/local/etc/libmap.d",0x7fffffffcf50,AT_SYMLINK_NOFOLLOW) ERR#2 'No such file or directory'
+open("/usr/local/etc/libmap.d",O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC,0165) ERR#2 'No such file or directory'
munmap(0x80062b000,102) = 0 (0x0)
openat(AT_FDCWD,"/var/run/ld-elf.so.hints",O_RDONLY|O_CLOEXEC,00) = 3 (0x3)
read(3,"Ehnt\^A\0\0\0\M^@\0\0\0\M-2\0\0"...,128) = 128 (0x80)
Reviewed by: kib
MFC after: 2 weeks
Sponsored by: DARPA, AFRL
Differential Revision: https://reviews.freebsd.org/D12741
2017-10-22 10:32:40 +00:00
|
|
|
if (strcmp(p->path, idir) == 0)
|
2012-04-30 21:26:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
d = opendir(idir);
|
Don't call realpath(3) from libmap rtld code. This gets rid of a few calls
to fstatat(2) at binary startup; the difference looks like this:
--- przed 2017-10-14 13:55:49.983528000 +0100
+++ po 2017-10-14 14:10:39.134343000 +0100
@@ -1,15 +1,10 @@
mmap(0x0,32768,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANON,-1,0x0) = 34366173184 (0x800623000)
issetugid() = 0 (0x0)
-fstatat(AT_FDCWD,"/etc",{ mode=drwxr-xr-x ,inode=1364352,size=2560,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
-fstatat(AT_FDCWD,"/etc/libmap.conf",{ mode=-rw-r--r-- ,inode=1373288,size=102,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
openat(AT_FDCWD,"/etc/libmap.conf",O_RDONLY|O_CLOEXEC,00) = 3 (0x3)
fstat(3,{ mode=-rw-r--r-- ,inode=1373288,size=102,blksize=32768 }) = 0 (0x0)
mmap(0x0,102,PROT_READ,MAP_PRIVATE,3,0x0) = 34366205952 (0x80062b000)
close(3) = 0 (0x0)
-fstatat(AT_FDCWD,"/usr",{ mode=drwxr-xr-x ,inode=561792,size=512,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
-fstatat(AT_FDCWD,"/usr/local",{ mode=drwxr-xr-x ,inode=561800,size=512,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
-fstatat(AT_FDCWD,"/usr/local/etc",{ mode=drwxr-xr-x ,inode=653279,size=1536,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
-fstatat(AT_FDCWD,"/usr/local/etc/libmap.d",0x7fffffffcf50,AT_SYMLINK_NOFOLLOW) ERR#2 'No such file or directory'
+open("/usr/local/etc/libmap.d",O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC,0165) ERR#2 'No such file or directory'
munmap(0x80062b000,102) = 0 (0x0)
openat(AT_FDCWD,"/var/run/ld-elf.so.hints",O_RDONLY|O_CLOEXEC,00) = 3 (0x3)
read(3,"Ehnt\^A\0\0\0\M^@\0\0\0\M-2\0\0"...,128) = 128 (0x80)
Reviewed by: kib
MFC after: 2 weeks
Sponsored by: DARPA, AFRL
Differential Revision: https://reviews.freebsd.org/D12741
2017-10-22 10:32:40 +00:00
|
|
|
if (d == NULL)
|
2012-04-30 21:26:35 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
p = xmalloc(sizeof(struct lmc));
|
Don't call realpath(3) from libmap rtld code. This gets rid of a few calls
to fstatat(2) at binary startup; the difference looks like this:
--- przed 2017-10-14 13:55:49.983528000 +0100
+++ po 2017-10-14 14:10:39.134343000 +0100
@@ -1,15 +1,10 @@
mmap(0x0,32768,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANON,-1,0x0) = 34366173184 (0x800623000)
issetugid() = 0 (0x0)
-fstatat(AT_FDCWD,"/etc",{ mode=drwxr-xr-x ,inode=1364352,size=2560,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
-fstatat(AT_FDCWD,"/etc/libmap.conf",{ mode=-rw-r--r-- ,inode=1373288,size=102,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
openat(AT_FDCWD,"/etc/libmap.conf",O_RDONLY|O_CLOEXEC,00) = 3 (0x3)
fstat(3,{ mode=-rw-r--r-- ,inode=1373288,size=102,blksize=32768 }) = 0 (0x0)
mmap(0x0,102,PROT_READ,MAP_PRIVATE,3,0x0) = 34366205952 (0x80062b000)
close(3) = 0 (0x0)
-fstatat(AT_FDCWD,"/usr",{ mode=drwxr-xr-x ,inode=561792,size=512,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
-fstatat(AT_FDCWD,"/usr/local",{ mode=drwxr-xr-x ,inode=561800,size=512,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
-fstatat(AT_FDCWD,"/usr/local/etc",{ mode=drwxr-xr-x ,inode=653279,size=1536,blksize=32768 },AT_SYMLINK_NOFOLLOW) = 0 (0x0)
-fstatat(AT_FDCWD,"/usr/local/etc/libmap.d",0x7fffffffcf50,AT_SYMLINK_NOFOLLOW) ERR#2 'No such file or directory'
+open("/usr/local/etc/libmap.d",O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC,0165) ERR#2 'No such file or directory'
munmap(0x80062b000,102) = 0 (0x0)
openat(AT_FDCWD,"/var/run/ld-elf.so.hints",O_RDONLY|O_CLOEXEC,00) = 3 (0x3)
read(3,"Ehnt\^A\0\0\0\M^@\0\0\0\M-2\0\0"...,128) = 128 (0x80)
Reviewed by: kib
MFC after: 2 weeks
Sponsored by: DARPA, AFRL
Differential Revision: https://reviews.freebsd.org/D12741
2017-10-22 10:32:40 +00:00
|
|
|
p->path = xstrdup(idir);
|
|
|
|
p->dev = NODEV;
|
|
|
|
p->ino = 0;
|
2012-04-30 21:26:35 +00:00
|
|
|
TAILQ_INSERT_HEAD(&lmc_head, p, next);
|
|
|
|
|
|
|
|
while ((dp = readdir(d)) != NULL) {
|
|
|
|
if (dp->d_ino == 0)
|
|
|
|
continue;
|
|
|
|
if (dp->d_type != DT_REG)
|
|
|
|
continue;
|
|
|
|
ext = strrchr(dp->d_name, '.');
|
|
|
|
if (ext == NULL)
|
|
|
|
continue;
|
|
|
|
if (strcmp(ext, ".conf") != 0)
|
|
|
|
continue;
|
|
|
|
if (strlcpy(conffile, idir, MAXPATHLEN) >= MAXPATHLEN)
|
|
|
|
continue; /* too long */
|
|
|
|
if (strlcat(conffile, "/", MAXPATHLEN) >= MAXPATHLEN)
|
|
|
|
continue; /* too long */
|
|
|
|
if (strlcat(conffile, dp->d_name, MAXPATHLEN) >= MAXPATHLEN)
|
|
|
|
continue; /* too long */
|
|
|
|
lmc_parse_file(conffile);
|
|
|
|
}
|
|
|
|
closedir(d);
|
|
|
|
}
|
|
|
|
|
2005-02-04 02:46:41 +00:00
|
|
|
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];
|
2012-04-30 21:26:35 +00:00
|
|
|
/* allow includedir + full length path */
|
|
|
|
char line[MAXPATHLEN + 13];
|
2012-03-12 12:16:08 +00:00
|
|
|
size_t cnt;
|
|
|
|
int i;
|
2012-04-30 21:26:35 +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;
|
2014-08-21 02:40:33 +00:00
|
|
|
while (cnt < lm_len && lm_p[cnt] != '\n' &&
|
2012-03-12 12:16:08 +00:00
|
|
|
i < sizeof(line) - 1) {
|
|
|
|
line[i] = lm_p[cnt];
|
|
|
|
cnt++;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
line[i] = '\0';
|
2014-08-21 02:40:33 +00:00
|
|
|
while (cnt < lm_len && lm_p[cnt] != '\n')
|
2012-03-12 12:16:08 +00:00
|
|
|
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;
|
|
|
|
|
2012-04-30 21:26:35 +00:00
|
|
|
if (strlcpy(prog, c, sizeof prog) >= sizeof prog)
|
|
|
|
continue;
|
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';
|
2012-04-30 21:26:35 +00:00
|
|
|
if (strcmp(f, "includedir") == 0)
|
|
|
|
lmc_parse_dir(t);
|
|
|
|
else if (strcmp(f, "include") == 0)
|
|
|
|
lmc_parse_file(t);
|
|
|
|
else
|
|
|
|
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;
|
2012-04-30 21:26:35 +00:00
|
|
|
struct lmc *p;
|
2003-04-07 16:21:26 +00:00
|
|
|
|
2003-05-31 14:46:38 +00:00
|
|
|
dbg("%s()", __func__);
|
|
|
|
|
2012-04-30 21:26:35 +00:00
|
|
|
while (!TAILQ_EMPTY(&lmc_head)) {
|
|
|
|
p = TAILQ_FIRST(&lmc_head);
|
|
|
|
TAILQ_REMOVE(&lmc_head, p, next);
|
|
|
|
free(p->path);
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|