From 125fb7fbdb6c71e5d60ddd0ff71e0b8800c5cfe0 Mon Sep 17 00:00:00 2001 From: Petr Rockai Date: Mon, 12 Jan 2009 18:45:44 +0000 Subject: [PATCH] A C implementation of "not" that handles fatal signals rather more intelligently than the shell implementation. C code by Jaroslav Stava. I have done a rudimentary review and checked that tests still pass. --- test/Makefile.in | 7 +++++-- test/not.c | 36 ++++++++++++++++++++++++++++++++++++ test/test-utils.sh | 10 ---------- 3 files changed, 41 insertions(+), 12 deletions(-) create mode 100644 test/not.c diff --git a/test/Makefile.in b/test/Makefile.in index bafe0df2e..e21f9f6bd 100644 --- a/test/Makefile.in +++ b/test/Makefile.in @@ -25,9 +25,12 @@ abs_builddir = @abs_builddir@ abs_top_builddir = @abs_top_builddir@ abs_top_srcdir = @abs_top_srcdir@ -all: init.sh +all: bin/not init.sh sh harness.sh +bin/not: .bin-dir-stamp + $(CC) -o bin/not not.c + init.sh: Makefile.in .bin-dir-stamp rm -f $@-t $@ echo 'top_srcdir=$(top_srcdir)' >> $@-t @@ -49,7 +52,7 @@ T = $(wildcard t-*.sh) Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ -$(T): init.sh +$(T): bin/not init.sh sh harness.sh $@ .bin-dir-stamp: lvm-wrapper diff --git a/test/not.c b/test/not.c new file mode 100644 index 000000000..4885944bf --- /dev/null +++ b/test/not.c @@ -0,0 +1,36 @@ +#include +#include +#include +#include +#include + +int main(int args, char **argv) { + pid_t pid; + int status; + int FAILURE = 6; + + if (args < 2) { + fprintf(stderr, "Need args\n"); + return FAILURE; + } + + pid = fork(); + if (pid == -1) { + fprintf(stderr, "Could not fork\n"); + return FAILURE; + } else if (pid == 0) { /* child */ + execvp(argv[1], &argv[1]); + /* should not be accessible */ + return FAILURE; + } else { /* parent */ + waitpid(pid, &status, 0); + if (!WIFEXITED(status)) { + /* did not exit correctly */ + return FAILURE; + } + /* return the opposite */ + return !WEXITSTATUS(status); + } + /* not accessible */ + return FAILURE; +} diff --git a/test/test-utils.sh b/test/test-utils.sh index bcbb21461..a00b64e31 100644 --- a/test/test-utils.sh +++ b/test/test-utils.sh @@ -17,16 +17,6 @@ aux() { #"$@" } -not () { - "$@" && exit 1 || { - err="$?" - if test "$err" = 129; then - echo "fatal error $err" - exit 1 - fi - } -} - STACKTRACE() { trap - ERR; i=0; -- 2.43.5