1 /* Run a testsuite script with a timeout. */
5 #define DEFAULT_TIMEOUT_SECS (5*60)
6 #define TIMEOUT_ENV "TESTRUN_TIMEOUT"
8 int main(int argc
, char *argv
[])
12 int status
, timeout_secs
, slept
= 0;
15 fprintf(stderr
, "Usage: testrun [SHELL_OPTIONS] TESTSUITE_SCRIPT [ARGS]\n");
19 if ((timeout_env
= getenv(TIMEOUT_ENV
)) != NULL
)
20 timeout_secs
= atoi(timeout_env
);
22 timeout_secs
= DEFAULT_TIMEOUT_SECS
;
24 if ((pid
= fork()) < 0) {
25 fprintf(stderr
, "TESTRUN ERROR: fork failed: %s\n", strerror(errno
));
31 execvp(argv
[0], argv
);
32 fprintf(stderr
, "TESTRUN ERROR: failed to exec %s: %s\n", argv
[0], strerror(errno
));
37 int ret
= waitpid(pid
, &status
, WNOHANG
);
43 fprintf(stderr
, "TESTRUN ERROR: waitpid failed: %s\n", strerror(errno
));
46 if (slept
++ > timeout_secs
) {
47 fprintf(stderr
, "TESTRUN TIMEOUT: test took over %d seconds.\n", timeout_secs
);
48 if (kill(pid
, SIGTERM
) < 0)
49 fprintf(stderr
, "TESTRUN ERROR: failed to kill pid %ld: %s\n", (long)pid
, strerror(errno
));
51 fprintf(stderr
, "TESTRUN INFO: killed pid %ld\n", (long)pid
);
57 if (!WIFEXITED(status
))
60 return WEXITSTATUS(status
);