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 int sh(const char *cmd
)
42 if((pid
= vfork()) == 0) {
43 execl("/bin/sh", "/bin/sh", "-c", cmd
, (char*)0);
44 t_error("execl failed: %s\n", strerror(errno
));
48 t_error("vfork failed: %s\n", strerror(errno
));
54 static void test_shell_exit(const char *cmd
, int code
)
57 TEST(WIFEXITED(r
), "child terminated abnormally\n");
58 TEST(WEXITSTATUS(r
) == code
, "child exited with %d, expected %d\n", WEXITSTATUS(r
), code
);
61 static void test_shell_kill(const char *cmd
, int sig
)
64 TEST(WIFSIGNALED(r
), "child did not get killed\n");
65 TEST(WTERMSIG(r
) == sig
, "child is killed by %d, expected %d\n", WTERMSIG(r
), sig
);
71 test_shell_exit("exit 0", 0);
72 test_shell_exit("exit 1", 1);
73 test_shell_kill("kill -9 $$", 9);