diff --git a/Makefile b/Makefile index 5c14638..d95d3f0 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,9 @@ all: build build: $(MAKE) -C src +check: + $(MAKE) -C src check + syntaxcheck: shellcheck shellcheck: diff --git a/src/Makefile b/src/Makefile index 6878977..d103a5d 100644 --- a/src/Makefile +++ b/src/Makefile @@ -4,12 +4,18 @@ CPPFLAGS ?= CPPFLAGS += -DPACKAGE_VERSION="\"$(PACKAGE_VERSION)\"" CFLAGS ?= -ggdb -O2 -Wall -Wextra -Wno-unused-parameter -CFLAGS += -fPIC -ldl +CFLAGS += -fPIC -ldl -fvisibility=hidden PLUGIN = fake-uname.so $(PLUGIN): fake-uname.c $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ -shared $< +UNAME_CHECK = LD_PRELOAD="$(CURDIR)/$(PLUGIN)" uname + +check: $(PLUGIN) + test "0.0.0" = "$(shell $(UNAME_CHECK) -r)" + test "1.2.3" = "$(shell UTS_RELEASE="1.2.3" $(UNAME_CHECK) -r)" + clean: $(RM) *.o *.so diff --git a/src/fake-uname.c b/src/fake-uname.c index cad9850..d71a4f4 100644 --- a/src/fake-uname.c +++ b/src/fake-uname.c @@ -17,28 +17,45 @@ #define RTLD_NEXT ((void *) -1l) #endif -typedef int (*uname_t) (struct utsname * buf); +#define SYMBOL_EXPORT __attribute__((visibility("default"))) + +typedef int uname_func(struct utsname *buf); static void *get_libc_func(const char *funcname) { void *func; char *error; + /* Clear any previous errors. */ + dlerror(); func = dlsym(RTLD_NEXT, funcname); - if ((error = dlerror()) != NULL) { - fprintf(stderr, "Can't locate libc function '%s' error: %s", funcname, error); + error = dlerror(); + if (error != NULL) { + fprintf(stderr, "Cannot locate libc function '%s' error: %s", + funcname, error); _exit(EXIT_FAILURE); } return func; } -int uname(struct utsname *buf) +int SYMBOL_EXPORT uname(struct utsname *buf) { + static uname_func *real_uname; + const char *release; int ret; - char *env = NULL; - uname_t real_uname = (uname_t) get_libc_func("uname"); - ret = real_uname((struct utsname *) buf); - strncpy(buf->release, ((env = getenv("UTS_RELEASE")) == NULL) ? UTS_RELEASE : env, 65); + if (real_uname == NULL) + real_uname = (uname_func *)get_libc_func("uname"); + + ret = real_uname(buf); + if (ret < 0) + return ret; + + release = getenv("UTS_RELEASE"); + if (release == NULL) + release = UTS_RELEASE; + strncpy(buf->release, release, sizeof(buf->release) - 1); + buf->release[sizeof(buf->release) - 1] = '\0'; + return ret; } diff --git a/t/testrunner b/t/testrunner index 981e1c7..7ca9c66 100755 --- a/t/testrunner +++ b/t/testrunner @@ -17,5 +17,6 @@ else fi make +make check make syntaxcheck make script_version