Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
U
userhosts
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
userhosts
Commits
6e230f01
Commit
6e230f01
authored
Aug 28, 2014
by
Julien Muchembled
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make userhosts an executable library which can be used as a wrapper
parent
74b2ee92
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
54 additions
and
9 deletions
+54
-9
.gitignore
.gitignore
+1
-1
Makefile
Makefile
+14
-7
README
README
+5
-1
userhosts.c
userhosts.c
+34
-0
No files found.
.gitignore
View file @
6e230f01
/
libuserhosts.so
/
userhosts
Makefile
View file @
6e230f01
CFLAGS
?=
-O2
-s
CFLAGS
?=
-O2
-s
CFLAGS
+=
-Wall
-fPI
C
CFLAGS
+=
-Wall
-fPI
E
-pie
LDLIBS
=
-ldl
LDLIBS
=
-ldl
PREFIX
=
/usr/local
PREFIX
=
/usr/local
.PHONY
:
all install uninstall clean
.PHONY
:
all install uninstall clean
all
:
userhosts
.so
all
:
userhosts
userhosts
.so
:
userhosts.c
userhosts
:
userhosts.c
$(CC)
$(CPPFLAGS)
$(CFLAGS)
$(LDFLAGS)
-shared
-Wl
,-soname,
$@
$(LDLIBS)
-o
$@
$<
$(CC)
$(CPPFLAGS)
$(CFLAGS)
$(LDFLAGS)
$(LDLIBS)
-o
$@
$<
install
:
all
install
:
all
install
-Dp
m
0644 userhosts.so
$(DESTDIR)$(PREFIX)
/lib/userhosts/userhosts.so
install
-Dp
userhosts
$(DESTDIR)$(PREFIX)
/bin/userhosts
uninstall
:
uninstall
:
-
cd
$(DESTDIR)$(PREFIX)
&&
rm
-f
lib/userhosts/userhosts.so
-
cd
$(DESTDIR)$(PREFIX)
&&
rm
-f
bin/userhosts
clean
:
clean
:
-
rm
-f
libuserhosts.so
-
rm
-f
userhosts
check
:
userhosts
@
export
HOSTS
=
`
mktemp
`
&&
trap
"rm
$$
HOSTS"
0
&&
\
echo
2001:db8::1
$<
.example.com
>
$$
HOSTS
&&
\
set
getent hosts
$<
.example.com
&&
\
a
=
`
./
$<
$$
@
`
&&
b
=
`
LD_PRELOAD
=
./
$<
$$
@
`
&&
[
"
$$
a"
=
"
$$
b"
]
&&
\
echo
"
$$
a"
|grep
-q
^2001:db8::1
&&
!
$$
@
README
View file @
6e230f01
...
@@ -31,4 +31,8 @@ Just run "make".
...
@@ -31,4 +31,8 @@ Just run "make".
Usage
Usage
-----
-----
$ env HOSTS=/path/to/custom/hosts LD_PRELOAD=/path/to/libuserhosts.so <command>
$ HOSTS=/path/to/custom/hosts LD_PRELOAD=/path/to/userhosts <command>
`userhosts` is also an executable wrapper that appends itself to LD_PRELOAD:
$ HOSTS=/path/to/custom/hosts userhosts <command>
userhosts.c
View file @
6e230f01
...
@@ -26,6 +26,7 @@
...
@@ -26,6 +26,7 @@
#include <stdlib.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <dlfcn.h>
#include <fcntl.h>
#include <fcntl.h>
#include <unistd.h>
#define ORIGINAL_HOSTS_PATH "/etc/hosts"
#define ORIGINAL_HOSTS_PATH "/etc/hosts"
...
@@ -85,3 +86,36 @@ FILE *fopen(const char *path, const char *mode) {
...
@@ -85,3 +86,36 @@ FILE *fopen(const char *path, const char *mode) {
path
=
replacement_hosts
;
path
=
replacement_hosts
;
return
(
*
original_fopen
)(
path
,
mode
);
return
(
*
original_fopen
)(
path
,
mode
);
}
}
int
main
(
int
argc
,
char
*
argv
[])
{
char
*
ld_preload
,
*
p
;
int
ret
=
-
1
;
if
(
argc
<=
1
)
fprintf
(
stderr
,
"usage: userhosts <command> [<args>]
\n
"
);
else
{
/* In order not to depend on /proc, getauxval(AT_EXECFN) from <sys/auxv.h>
* should be used. However, this requires glibc >= 2.16. */
ld_preload
=
realpath
(
"/proc/self/exe"
,
NULL
);
if
(
!
ld_preload
)
p
=
"realpath"
;
else
{
if
((
p
=
getenv
(
"LD_PRELOAD"
)))
{
size_t
n
=
strlen
(
ld_preload
);
ld_preload
=
realloc
(
ld_preload
,
n
+
strlen
(
p
)
+
2
);
ld_preload
[
n
++
]
=
':'
;
strcpy
(
ld_preload
+
n
,
p
);
}
ret
=
setenv
(
"LD_PRELOAD"
,
ld_preload
,
1
);
free
(
ld_preload
);
if
(
ret
)
p
=
"setenv"
;
else
{
ret
=
execvp
(
argv
[
1
],
&
argv
[
1
]);
p
=
"execvp"
;
}
}
perror
(
p
);
}
return
ret
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment