9 #include <sys/resource.h>
13 static void handler(int s)
17 static void setrl(int r, long lim) {
20 if (getrlimit(r, &rl))
21 t_error("getrlimit %d: %s\n", r, strerror(errno));
23 if (lim < rl.rlim_max)
25 if (setrlimit(r, &rl))
26 t_error("setrlimit %d: %s\n", r, strerror(errno));
29 static int start(char *argv[])
35 setrl(RLIMIT_STACK, 100*1024);
38 t_error("%s exec failed: %s\n", argv[0], strerror(errno));
42 t_error("%s fork failed: %s\n", argv[0], strerror(errno));
48 int main(int argc, char *argv[])
57 t_error("usage: ./run cmd [args..]\n");
62 sigaddset(&set, SIGCHLD);
63 sigprocmask(SIG_BLOCK, &set, 0);
64 signal(SIGCHLD, handler);
66 if (sigtimedwait(&set, 0, &(struct timespec){5,0}) == -1) {
69 if (kill(pid, SIGKILL) == -1)
70 t_error("%s kill failed: %s\n", argv[0], strerror(errno));
72 if (waitpid(pid, &status, 0) != pid) {
73 t_error("%s waitpid failed: %s\n", argv[0], strerror(errno));
76 if (WIFEXITED(status)) {
77 if (WEXITSTATUS(status) == 0)
79 t_printf("FAIL %s [status %d]\n", argv[0], WEXITSTATUS(status));
81 t_printf("FAIL %s [timed out]\n", argv[0]);
82 } else if (WIFSIGNALED(status)) {
83 t_printf("FAIL %s [signal %s]\n", argv[0], strsignal(WTERMSIG(status)));
85 t_printf("FAIL %s [unknown]\n", argv[0]);