add run wrapper, move t_printf and t_status into separate translation unit
[libc-test] / src / common / run.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include <errno.h>
4 #include <signal.h>
5 #include <time.h>
6 #include <sys/types.h>
7 #include <sys/wait.h>
8 #include <sys/time.h>
9 #include <sys/resource.h>
10 #include <unistd.h>
11 #include "test.h"
12
13 static void handler(int s)
14 {
15 }
16
17 static void setrl(int r, long lim) {
18         struct rlimit rl;
19
20         if (getrlimit(r, &rl))
21                 t_error("getrlimit %d: %s\n", r, strerror(errno));
22         rl.rlim_cur = lim;
23         if (lim < rl.rlim_max)
24                 rl.rlim_max = lim;
25         if (setrlimit(r, &rl))
26                 t_error("setrlimit %d: %s\n", r, strerror(errno));
27 }
28
29 static int start(char *argv[])
30 {
31         int pid;
32
33         pid = fork();
34         if (pid == 0) {
35                 setrl(RLIMIT_STACK, 100*1024);
36                 setrl(RLIMIT_CPU, 2);
37                 execv(argv[0], argv);
38                 t_error("%s exec failed: %s\n", argv[0], strerror(errno));
39                 exit(1);
40         }
41         if (pid == -1) {
42                 t_error("%s fork failed: %s\n", argv[0], strerror(errno));
43                 exit(-1);
44         }
45         return pid;
46 }
47
48 int main(int argc, char *argv[])
49 {
50         int status;
51         sigset_t set;
52         int timeout = 0;
53         int sig = 0;
54         int pid;
55
56         if (argc < 2) {
57                 t_error("usage: ./run cmd [args..]\n");
58                 return -1;
59         }
60         argv++;
61         sigemptyset(&set);
62         sigaddset(&set, SIGCHLD);
63         sigprocmask(SIG_BLOCK, &set, 0);
64         signal(SIGCHLD, handler);
65         pid = start(argv);
66         if (sigtimedwait(&set, 0, &(struct timespec){5,0}) == -1) {
67                 if (errno == EAGAIN)
68                         timeout = 1;
69                 if (kill(pid, SIGKILL) == -1)
70                         t_error("%s kill failed: %s\n", argv[0], strerror(errno));
71         }
72         if (waitpid(pid, &status, 0) != pid) {
73                 t_error("%s waitpid failed: %s\n", argv[0], strerror(errno));
74                 return -1;
75         }
76         if (WIFEXITED(status)) {
77                 if (WEXITSTATUS(status) == 0)
78                         return 0;
79                 t_printf("FAIL %s [status %d]\n", argv[0], WEXITSTATUS(status));
80         } else if (timeout) {
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)));
84         } else
85                 t_printf("FAIL %s [unknown]\n", argv[0]);
86         return 1;
87 }