remove core limit setting
[libc-test] / common / t.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <stdarg.h>
4 #include <string.h>
5 #include <errno.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 #define B(f)
14 #define T(f) void f();
15 #include "tests.h"
16 #undef T
17
18 static int failed;
19 static const char *name;
20
21 static int slow;
22 static int verbose;
23 static int count;
24 static int nfailed;
25
26 static void errtimer() { error("use *_timer in benchmarks only\n"); }
27 void start_timer() { errtimer(); }
28 void stop_timer() { errtimer(); }
29 void reset_timer() { errtimer(); }
30
31 void error__(const char *n, int l, const char *s, ...) {
32         va_list ap;
33
34         if (failed == 0 && nfailed == 0)
35                 dprintf(1, "FAIL\n");
36         failed = 1;
37         dprintf(1, " ERROR %s %s:%d: ", name, n, l);
38         va_start(ap, s);
39         vdprintf(1, s, ap);
40         va_end(ap);
41 }
42
43 static void setrl(int r, long lim) {
44         struct rlimit rl;
45
46         if (getrlimit(r, &rl))
47                 error("getrlimit %d: %s\n", r, strerror(errno));
48         rl.rlim_cur = lim;
49         if (setrlimit(r, &rl))
50                 error("setrlimit %d: %s\n", r, strerror(errno));
51 }
52
53 static void run(const char *n, void (*f)()) {
54         pid_t pid;
55         int s;
56
57         count++;
58         failed = 0;
59         name = n;
60         if (verbose)
61                 dprintf(1, "running %s:\n", name);
62
63         pid = fork();
64         if (pid == 0) {
65                 /* run test in a child process */
66                 setrl(RLIMIT_STACK, 1<<16);
67                 setrl(RLIMIT_CPU, 2);
68                 f();
69                 exit(failed);
70         }
71
72         if (pid == -1)
73                 error("fork failed: %s\n", strerror(errno));
74         else {
75                 if (waitpid(pid, &s, 0) == -1)
76                         error("waitpid failed: %s\n", strerror(errno));
77                 else if (!WIFEXITED(s))
78                         error("abnormal exit: %s\n", WIFSIGNALED(s) ? strsignal(WTERMSIG(s)) : "(unknown)");
79                 else
80                         failed = !!WEXITSTATUS(s);
81         }
82
83         if (failed) {
84                 nfailed++;
85                 dprintf(1, "FAILED %s\n", name);
86         } else if (verbose)
87                 dprintf(1, "PASSED %s\n", name);
88 }
89
90 static int summary() {
91         if (nfailed)
92                 dprintf(1, "FAIL (%d out of %d tests)\n", nfailed, count);
93         else
94                 dprintf(1, "ok (%d tests)\n", count);
95         return !!nfailed;
96 }
97
98 static void usage() {
99         dprintf(1, "usage: ./t [-vs]\n");
100         exit(1);
101 }
102
103 int main(int argc, char *argv[]) {
104         int c;
105
106         while((c = getopt(argc, argv, "vs")) != -1)
107                 switch(c) {
108                 case 'v':
109                         verbose = 1;
110                         break;
111                 case 's':
112                         slow = 1; /* TODO */
113                         break;
114                 default:
115                         usage();
116                 }
117         if (optind != argc)
118                 usage();
119
120 #define T(t) run(#t, t);
121 #include "tests.h"
122         return summary();
123 }