Commit a172c8a1 authored by Vincent Pelletier's avatar Vincent Pelletier

Stop pretending to be an executable.

Fixes support on glibc >=2.30 , which otherwise emits a message like:
ERROR: ld.so: object '.../parts/userhosts/userhosts' from LD_PRELOAD cannot be preloaded (cannot dynamically load position-independent executable): ignored.
parent 1d3b463e
CFLAGS ?= -O2 -s CFLAGS ?= -O2 -s
CFLAGS += -Wall -fPIC -pie CFLAGS += -Wall -fPIC -shared
LDLIBS = -ldl LDLIBS = -ldl
PREFIX = /usr/local PREFIX = /usr/local
.PHONY: all install uninstall clean .PHONY: all install uninstall clean
all: userhosts all: userhosts.so
userhosts: userhosts.c userhosts.so: userhosts.c
$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $< $(LDLIBS) $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $< $(LDLIBS)
install: all install: all
install -Dp userhosts $(DESTDIR)$(PREFIX)/bin/userhosts install -Dp userhosts $(DESTDIR)$(PREFIX)/lib/userhosts.so
uninstall: uninstall:
-cd $(DESTDIR)$(PREFIX) && rm -f bin/userhosts -cd $(DESTDIR)$(PREFIX) && rm -f lib/userhosts.so
clean: clean:
-rm -f userhosts -rm -f userhosts.so
check: userhosts check: userhosts.so
@export HOSTS=`mktemp` && trap "rm $$HOSTS" 0 && \ @export HOSTS=`mktemp` && trap "rm $$HOSTS" 0 && \
echo 2001:db8::1 $<.example.com >$$HOSTS && \ echo 2001:db8::1 $<.example.com >$$HOSTS && \
set getent hosts $<.example.com && \ set getent hosts $<.example.com && \
a=`./$< $$@` && b=`LD_PRELOAD=./$< $$@` && [ "$$a" = "$$b" ] && \ a=`LD_PRELOAD=./$< $$@` && \
echo "$$a" |grep -q ^2001:db8::1 && ! $$@ echo "$$a" |grep -q ^2001:db8::1 && ! $$@
...@@ -90,36 +90,3 @@ FILE *fopen(const char *path, const char *mode) { ...@@ -90,36 +90,3 @@ 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)
dprintf(STDERR_FILENO, "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;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment