rename run to runtest so src/common/run make target does not collide
[libc-test] / src / common / runtest.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 int start(char *argv[])
18 {
19         int pid;
20
21         pid = fork();
22         if (pid == 0) {
23                 t_setrlim(RLIMIT_STACK, 100*1024);
24                 t_setrlim(RLIMIT_CPU, 2);
25                 execv(argv[0], argv);
26                 t_error("%s exec failed: %s\n", argv[0], strerror(errno));
27                 exit(1);
28         }
29         if (pid == -1) {
30                 t_error("%s fork failed: %s\n", argv[0], strerror(errno));
31                 exit(-1);
32         }
33         return pid;
34 }
35
36 int main(int argc, char *argv[])
37 {
38         int status;
39         sigset_t set;
40         int timeout = 0;
41         int sig = 0;
42         int pid;
43
44         if (argc < 2) {
45                 t_error("usage: ./runtest cmd [args..]\n");
46                 return -1;
47         }
48         argv++;
49         sigemptyset(&set);
50         sigaddset(&set, SIGCHLD);
51         sigprocmask(SIG_BLOCK, &set, 0);
52         signal(SIGCHLD, handler);
53         pid = start(argv);
54         if (sigtimedwait(&set, 0, &(struct timespec){5,0}) == -1) {
55                 if (errno == EAGAIN)
56                         timeout = 1;
57                 if (kill(pid, SIGKILL) == -1)
58                         t_error("%s kill failed: %s\n", argv[0], strerror(errno));
59         }
60         if (waitpid(pid, &status, 0) != pid) {
61                 t_error("%s waitpid failed: %s\n", argv[0], strerror(errno));
62                 return -1;
63         }
64         if (WIFEXITED(status)) {
65                 if (WEXITSTATUS(status) == 0)
66                         return t_status;
67                 t_printf("FAIL %s [status %d]\n", argv[0], WEXITSTATUS(status));
68         } else if (timeout) {
69                 t_printf("FAIL %s [timed out]\n", argv[0]);
70         } else if (WIFSIGNALED(status)) {
71                 t_printf("FAIL %s [signal %s]\n", argv[0], strsignal(WTERMSIG(status)));
72         } else
73                 t_printf("FAIL %s [unknown]\n", argv[0]);
74         return 1;
75 }