1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2021 Cyril Hrubis <chrubis@suse.cz>
4 */
5
6 #include <stdio.h>
7 #include <signal.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <errno.h>
11 #include <string.h>
12
print_help(const char * name)13 static void print_help(const char *name)
14 {
15 fprintf(stderr, "usage: %s timeout pid\n", name);
16 }
17
18 #define print_msg(fmt, ...) fprintf(stderr, fmt "\n", ##__VA_ARGS__)
19
main(int argc,char * argv[])20 int main(int argc, char *argv[])
21 {
22 int timeout, pid, ret, i;
23
24 if (argc != 3) {
25 print_help(argv[0]);
26 return 1;
27 }
28
29 timeout = atoi(argv[1]);
30 pid = atoi(argv[2]);
31
32 if (timeout < 0) {
33 fprintf(stderr, "Invalid timeout '%s'\n", argv[1]);
34 print_help(argv[0]);
35 return 1;
36 }
37
38 if (pid <= 1) {
39 fprintf(stderr, "Invalid pid '%s'\n", argv[2]);
40 print_help(argv[0]);
41 return 1;
42 }
43
44 ret = setpgid(0, 0);
45 if (ret)
46 print_msg("setpgid() failed: %s", strerror(errno));
47
48 if (timeout)
49 sleep(timeout);
50
51 print_msg("Test timed out, sending SIGTERM!");
52 print_msg("If you are running on slow machine, try exporting LTP_TIMEOUT_MUL > 1");
53
54 ret = kill(-pid, SIGTERM);
55 if (ret) {
56 print_msg("kill(%i) failed: %s", -pid, strerror(errno));
57 return 1;
58 }
59
60 usleep(100000);
61
62 i = 10;
63
64 while (!kill(-pid, 0) && i-- > 0) {
65 print_msg("Test is still running... %i", i + 1);
66 sleep(1);
67 }
68
69 if (!kill(-pid, 0)) {
70 print_msg("Test is still running, sending SIGKILL");
71 ret = kill(-pid, SIGKILL);
72 if (ret) {
73 print_msg("kill(%i) failed: %s", -pid, strerror(errno));
74 return 1;
75 }
76 }
77
78 return 0;
79 }
80