]> granicus.if.org Git - strace/blob - tests/delay.c
strace: terminate itself if interrupted by a signal
[strace] / tests / delay.c
1 /*
2  * Check delay injection.
3  *
4  * Copyright (c) 2018 The strace developers.
5  * All rights reserved.
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  */
9
10 #include "tests.h"
11 #include <limits.h>
12 #include <stdio.h>
13 #include <stdint.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <time.h>
17 #include <sys/time.h>
18 #include <sys/wait.h>
19 #include <asm/unistd.h>
20
21 static int64_t
22 usecs_from_tv(const struct timeval *const tv)
23 {
24     return (int64_t) tv->tv_sec * 1000000 + tv->tv_usec;
25 }
26
27 static int64_t
28 usecs_from_ts(const struct timespec *const ts)
29 {
30     return (int64_t) ts->tv_sec * 1000000 + ts->tv_nsec / 1000;
31 }
32
33 static void
34 check_delay(const struct timeval *const tv0,
35             const struct timespec *const ts,
36             const struct timeval *const tv1,
37             const int nproc,
38             const int64_t delay_enter,
39             const int64_t delay_exit)
40 {
41         const int64_t us0 = usecs_from_tv(tv0);
42         const int64_t us  = usecs_from_ts(ts);
43         const int64_t us1 = usecs_from_tv(tv1);
44
45         if (us - us0 < delay_exit * (nproc - 1) / nproc)
46                 _exit(1);
47
48         if (us - us0 > delay_exit * (nproc + 1) / nproc)
49                 _exit(2);
50
51         if (us1 - us < delay_enter * (nproc - 1) / nproc)
52                 _exit(3);
53
54         if (us1 - us > delay_enter * (nproc + 1) / nproc)
55                 _exit(4);
56 }
57
58 static void
59 run(const int nproc, const int delay_enter, const int delay_exit)
60 {
61         struct timeval prev = { 0, 0 }, now;
62
63         for (int i = 0; i < nproc; ++i, prev = now) {
64                 struct timespec ts;
65
66                 if (i && clock_gettime(CLOCK_REALTIME, &ts))
67                         perror_msg_and_fail("clock_gettime");
68
69                 if (syscall(__NR_gettimeofday, &now, NULL))
70                         perror_msg_and_fail("gettimeofday");
71
72                 if (!i)
73                         continue;
74
75                 check_delay(&prev, &ts, &now, nproc, delay_enter, delay_exit);
76         }
77
78         _exit(0);
79 }
80
81 int
82 main(int ac, char *av[])
83 {
84         if (ac != 4)
85                 error_msg_and_fail("usage: delay <nproc> <delay_enter> <delay_exit>");
86
87         const int nproc = atoi(av[1]);
88         if (nproc <= 1)
89                 perror_msg_and_fail("invalid nproc: %s", av[1]);
90
91         const int delay_enter = atoi(av[2]);
92         if (delay_enter <= 0)
93                 perror_msg_and_fail("invalid delay_enter: %s", av[2]);
94
95         const int delay_exit = atoi(av[3]);
96         if (delay_exit <= 0)
97                 perror_msg_and_fail("invalid delay_exit: %s", av[3]);
98
99         for (int i = 0; i < nproc; ++i) {
100                 pid_t pid = fork();
101
102                 if (pid)
103                         usleep(MAX(delay_enter, delay_exit) / nproc);
104                 else
105                         run(nproc, delay_enter, delay_exit);
106         }
107
108         int status;
109         while (wait(&status) > 0) {
110                 if (status)
111                         perror_msg_and_fail("wait status %d", status);
112         }
113
114         return 0;
115 }