1 /*
2 * Check delay injection.
3 *
4 * Copyright (c) 2018 The strace developers.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include "tests.h"
31 #include <limits.h>
32 #include <stdio.h>
33 #include <stdint.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <time.h>
37 #include <sys/time.h>
38 #include <sys/wait.h>
39 #include <asm/unistd.h>
40
41 static int64_t
usecs_from_tv(const struct timeval * const tv)42 usecs_from_tv(const struct timeval *const tv)
43 {
44 return (int64_t) tv->tv_sec * 1000000 + tv->tv_usec;
45 }
46
47 static int64_t
usecs_from_ts(const struct timespec * const ts)48 usecs_from_ts(const struct timespec *const ts)
49 {
50 return (int64_t) ts->tv_sec * 1000000 + ts->tv_nsec / 1000;
51 }
52
53 static void
check_delay(const struct timeval * const tv0,const struct timespec * const ts,const struct timeval * const tv1,const int nproc,const int64_t delay_enter,const int64_t delay_exit)54 check_delay(const struct timeval *const tv0,
55 const struct timespec *const ts,
56 const struct timeval *const tv1,
57 const int nproc,
58 const int64_t delay_enter,
59 const int64_t delay_exit)
60 {
61 const int64_t us0 = usecs_from_tv(tv0);
62 const int64_t us = usecs_from_ts(ts);
63 const int64_t us1 = usecs_from_tv(tv1);
64
65 if (us - us0 < delay_exit * (nproc - 1) / nproc)
66 _exit(1);
67
68 if (us - us0 > delay_exit * (nproc + 1) / nproc)
69 _exit(2);
70
71 if (us1 - us < delay_enter * (nproc - 1) / nproc)
72 _exit(3);
73
74 if (us1 - us > delay_enter * (nproc + 1) / nproc)
75 _exit(4);
76 }
77
78 static void
run(const int nproc,const int delay_enter,const int delay_exit)79 run(const int nproc, const int delay_enter, const int delay_exit)
80 {
81 struct timeval prev = { 0, 0 }, now;
82
83 for (int i = 0; i < nproc; ++i, prev = now) {
84 struct timespec ts;
85
86 if (i && clock_gettime(CLOCK_REALTIME, &ts))
87 perror_msg_and_fail("clock_gettime");
88
89 if (syscall(__NR_gettimeofday, &now, NULL))
90 perror_msg_and_fail("gettimeofday");
91
92 if (!i)
93 continue;
94
95 check_delay(&prev, &ts, &now, nproc, delay_enter, delay_exit);
96 }
97
98 _exit(0);
99 }
100
101 int
main(int ac,char * av[])102 main(int ac, char *av[])
103 {
104 if (ac != 4)
105 error_msg_and_fail("usage: delay <nproc> <delay_enter> <delay_exit>");
106
107 const int nproc = atoi(av[1]);
108 if (nproc <= 1)
109 perror_msg_and_fail("invalid nproc: %s", av[1]);
110
111 const int delay_enter = atoi(av[2]);
112 if (delay_enter <= 0)
113 perror_msg_and_fail("invalid delay_enter: %s", av[2]);
114
115 const int delay_exit = atoi(av[3]);
116 if (delay_exit <= 0)
117 perror_msg_and_fail("invalid delay_exit: %s", av[3]);
118
119 for (int i = 0; i < nproc; ++i) {
120 pid_t pid = fork();
121
122 if (pid)
123 usleep(MAX(delay_enter, delay_exit) / nproc);
124 else
125 run(nproc, delay_enter, delay_exit);
126 }
127
128 int status;
129 while (wait(&status) > 0) {
130 if (status)
131 perror_msg_and_fail("wait status %d", status);
132 }
133
134 return 0;
135 }
136