8 #define TEST(c, ...) ( (c) || (t_error(#c " failed: " __VA_ARGS__),0) )
10 static int w(pid_t pid)
13 r = waitpid(pid, &s, 0);
15 t_error("waitpid failed: %s\n", strerror(errno));
17 t_error("child pid was %d, waitpid returned %d\n", pid, r);
23 static void test_exit(int code)
26 if((pid = vfork()) == 0) {
28 t_error("exit failed: %s\n", strerror(errno));
31 t_error("vfork failed: %s\n", strerror(errno));
35 TEST(WIFEXITED(r), "child terminated abnormally\n");
36 TEST(WEXITSTATUS(r) == code, "child exited with %d, expected %d\n", WEXITSTATUS(r), code);
39 static void test_kill(int sig)
42 if((pid = vfork()) == 0) {
44 t_error("raise failed: %s\n", strerror(errno));
47 t_error("vfork failed: %s\n", strerror(errno));
51 TEST(WIFSIGNALED(r), "child did not get killed\n");
52 TEST(WTERMSIG(r) == sig, "child is killed by %d, expected %d\n", WTERMSIG(r), sig);
55 static int sh(const char *cmd)
58 if((pid = vfork()) == 0) {
59 execl("/bin/sh", "/bin/sh", "-c", cmd, (char*)0);
60 t_error("execl failed: %s\n", strerror(errno));
64 t_error("vfork failed: %s\n", strerror(errno));
70 static void test_shell_exit(const char *cmd, int code)
73 TEST(WIFEXITED(r), "child terminated abnormally\n");
74 TEST(WEXITSTATUS(r) == code, "child exited with %d, expected %d\n", WEXITSTATUS(r), code);
77 static void test_shell_kill(const char *cmd, int sig)
80 TEST(WIFSIGNALED(r), "child did not get killed\n");
81 TEST(WTERMSIG(r) == sig, "child is killed by %d, expected %d\n", WTERMSIG(r), sig);
88 test_shell_exit("exit 0", 0);
89 test_shell_exit("exit 1", 1);
90 test_shell_kill("kill -s HUP $$", SIGHUP);