From 34a0a81ed23d9bccf9efbe92c58c85426bf41dc8 Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Tue, 8 Dec 2020 00:20:42 +0300 Subject: move test utilities to a separate directory --- Makefile | 6 ++-- test/testclient.c | 44 +++++++++++++++++++++++++ test/testserver.c | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ testclient.c | 44 ------------------------- testserver.c | 96 ------------------------------------------------------- 5 files changed, 143 insertions(+), 143 deletions(-) create mode 100644 test/testclient.c create mode 100644 test/testserver.c delete mode 100644 testclient.c delete mode 100644 testserver.c diff --git a/Makefile b/Makefile index 54945cb..479dbe7 100644 --- a/Makefile +++ b/Makefile @@ -16,10 +16,10 @@ voidnsrun: voidnsrun.o utils.o voidnsundo: voidnsundo.o utils.o $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) -testserver: testserver.o utils.o +testserver: test/testserver.o utils.o $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) -testclient: testclient.o utils.o +testclient: test/testclient.o utils.o $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) install: voidnsrun voidnsundo @@ -27,7 +27,7 @@ install: voidnsrun voidnsundo chmod u+s $(PREFIX)/bin/voidnsrun $(PREFIX)/bin/voidnsundo clean: - rm -f *.o voidnsrun voidnsundo testserver testclient + rm -f *.o test/*.o voidnsrun voidnsundo testserver testclient %.o: %.c $(CC) $(CFLAGS) -c $^ -I. -o $@ diff --git a/test/testclient.c b/test/testclient.c new file mode 100644 index 0000000..19db1c1 --- /dev/null +++ b/test/testclient.c @@ -0,0 +1,44 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include "utils.h" + +#define ERROR_EXIT(f_, ...) { \ + fprintf(stderr, (f_), ##__VA_ARGS__); \ + return 1; \ + } + +int main() +{ + struct sockaddr_un addr = {0}; + int sock; + + // Create and connect a unix domain socket + sock = socket(AF_UNIX, SOCK_STREAM, 0); + if (sock == -1) + ERROR_EXIT("socket: %s\n", strerror(errno)); + + addr.sun_family = AF_UNIX; + strcpy(&addr.sun_path[1], "/tmp/voidnsrun-test.sock"); + + if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) + ERROR_EXIT("connect: %s\n", strerror(errno)); + + int fd = recv_fd(sock); + close(sock); + + assert(fd != 0); + + struct stat st; + if (fstat(fd, &st) == -1) + ERROR_EXIT("stat: %s\n", strerror(errno)); + + printf("st_ino: %lu\n", st.st_ino); + + return 0; +} \ No newline at end of file diff --git a/test/testserver.c b/test/testserver.c new file mode 100644 index 0000000..091d580 --- /dev/null +++ b/test/testserver.c @@ -0,0 +1,96 @@ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "utils.h" + +#define ERROR(f_, ...) fprintf(stderr, (f_), ##__VA_ARGS__) +#define UNUSED(x) (void)(x) +#define ERROR_EXIT(f_, ...) { \ + fprintf(stderr, (f_), ##__VA_ARGS__); \ + return 1; \ + } + +volatile sig_atomic_t term_caught = 0; +void onterm(int sig) +{ + printf("sigterm caught\n"); + UNUSED(sig); + term_caught = 1; +} + +int main() +{ + int result; + int sock_fd, sock_conn; + int nsfd; + + /* Get current namespace's file descriptor. */ + nsfd = open("/proc/self/ns/mnt", O_RDONLY); + if (nsfd == -1) + ERROR_EXIT("error: failed to acquire mount namespace's fd.%s\n", + strerror(errno)); + + /* Fork. */ + pid_t ppid_before_fork = getpid(); + pid_t pid = fork(); + if (pid == -1) + ERROR_EXIT("fork: %s\n", strerror(errno)); + + if (pid == 0) { + /* Catch SIGTERM. */ + struct sigaction sa = {0}; + sa.sa_handler = onterm; + sigaction(SIGTERM, &sa, NULL); + + /* Ignore SIGINT. */ + signal(SIGINT, SIG_IGN); + + /* Set the child to die when parent thread dies. */ + int r = prctl(PR_SET_PDEATHSIG, SIGTERM); + if (r == -1) + ERROR_EXIT("prctl: %s\n", strerror(errno)); + if (getppid() != ppid_before_fork) + ERROR_EXIT("error: parent has died already.\n"); + + /* Create unix socket. */ + sock_fd = socket(AF_UNIX, SOCK_STREAM, 0); + if (sock_fd == -1) + ERROR_EXIT("socket: %s.\n", strerror(errno)); + + struct sockaddr_un sock_addr = {0}; + sock_addr.sun_family = AF_UNIX; + strcpy(&sock_addr.sun_path[1], "/tmp/voidnsrun-test.sock"); + + if (bind(sock_fd, (struct sockaddr *)&sock_addr, sizeof(sock_addr)) == -1) + ERROR_EXIT("bind: %s\n", strerror(errno)); + + listen(sock_fd, 1); + + while (!term_caught) { + sock_conn = accept(sock_fd, NULL, 0); + if (sock_conn == -1) { + ERROR("accept: %s\n", strerror(errno)); + continue; + } + printf("accepted\n"); + send_fd(sock_conn, nsfd); + } + printf("exiting\n"); + } else { + /* This is parent. Launch a program. */ + char *argv[2] = {"/bin/sh", NULL}; + + result = execvp(argv[0], (char *const *)argv); + if (result == -1) + ERROR_EXIT("execvp: %s\n", strerror(errno)); + } + + return 0; +} \ No newline at end of file diff --git a/testclient.c b/testclient.c deleted file mode 100644 index 19db1c1..0000000 --- a/testclient.c +++ /dev/null @@ -1,44 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include "utils.h" - -#define ERROR_EXIT(f_, ...) { \ - fprintf(stderr, (f_), ##__VA_ARGS__); \ - return 1; \ - } - -int main() -{ - struct sockaddr_un addr = {0}; - int sock; - - // Create and connect a unix domain socket - sock = socket(AF_UNIX, SOCK_STREAM, 0); - if (sock == -1) - ERROR_EXIT("socket: %s\n", strerror(errno)); - - addr.sun_family = AF_UNIX; - strcpy(&addr.sun_path[1], "/tmp/voidnsrun-test.sock"); - - if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) - ERROR_EXIT("connect: %s\n", strerror(errno)); - - int fd = recv_fd(sock); - close(sock); - - assert(fd != 0); - - struct stat st; - if (fstat(fd, &st) == -1) - ERROR_EXIT("stat: %s\n", strerror(errno)); - - printf("st_ino: %lu\n", st.st_ino); - - return 0; -} \ No newline at end of file diff --git a/testserver.c b/testserver.c deleted file mode 100644 index 091d580..0000000 --- a/testserver.c +++ /dev/null @@ -1,96 +0,0 @@ -#define _GNU_SOURCE -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "utils.h" - -#define ERROR(f_, ...) fprintf(stderr, (f_), ##__VA_ARGS__) -#define UNUSED(x) (void)(x) -#define ERROR_EXIT(f_, ...) { \ - fprintf(stderr, (f_), ##__VA_ARGS__); \ - return 1; \ - } - -volatile sig_atomic_t term_caught = 0; -void onterm(int sig) -{ - printf("sigterm caught\n"); - UNUSED(sig); - term_caught = 1; -} - -int main() -{ - int result; - int sock_fd, sock_conn; - int nsfd; - - /* Get current namespace's file descriptor. */ - nsfd = open("/proc/self/ns/mnt", O_RDONLY); - if (nsfd == -1) - ERROR_EXIT("error: failed to acquire mount namespace's fd.%s\n", - strerror(errno)); - - /* Fork. */ - pid_t ppid_before_fork = getpid(); - pid_t pid = fork(); - if (pid == -1) - ERROR_EXIT("fork: %s\n", strerror(errno)); - - if (pid == 0) { - /* Catch SIGTERM. */ - struct sigaction sa = {0}; - sa.sa_handler = onterm; - sigaction(SIGTERM, &sa, NULL); - - /* Ignore SIGINT. */ - signal(SIGINT, SIG_IGN); - - /* Set the child to die when parent thread dies. */ - int r = prctl(PR_SET_PDEATHSIG, SIGTERM); - if (r == -1) - ERROR_EXIT("prctl: %s\n", strerror(errno)); - if (getppid() != ppid_before_fork) - ERROR_EXIT("error: parent has died already.\n"); - - /* Create unix socket. */ - sock_fd = socket(AF_UNIX, SOCK_STREAM, 0); - if (sock_fd == -1) - ERROR_EXIT("socket: %s.\n", strerror(errno)); - - struct sockaddr_un sock_addr = {0}; - sock_addr.sun_family = AF_UNIX; - strcpy(&sock_addr.sun_path[1], "/tmp/voidnsrun-test.sock"); - - if (bind(sock_fd, (struct sockaddr *)&sock_addr, sizeof(sock_addr)) == -1) - ERROR_EXIT("bind: %s\n", strerror(errno)); - - listen(sock_fd, 1); - - while (!term_caught) { - sock_conn = accept(sock_fd, NULL, 0); - if (sock_conn == -1) { - ERROR("accept: %s\n", strerror(errno)); - continue; - } - printf("accepted\n"); - send_fd(sock_conn, nsfd); - } - printf("exiting\n"); - } else { - /* This is parent. Launch a program. */ - char *argv[2] = {"/bin/sh", NULL}; - - result = execvp(argv[0], (char *const *)argv); - if (result == -1) - ERROR_EXIT("execvp: %s\n", strerror(errno)); - } - - return 0; -} \ No newline at end of file -- cgit v1.2.3