]> granicus.if.org Git - strace/blob - tests/clock_nanosleep.c
tests: check decoding of invalid tv_sec and tv_nsec values
[strace] / tests / clock_nanosleep.c
1 /*
2  * Check decoding of clock_nanosleep and clock_gettime syscalls.
3  *
4  * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
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 <assert.h>
32 #include <stdio.h>
33 #include <stdint.h>
34 #include <signal.h>
35 #include <time.h>
36 #include <unistd.h>
37 #include <sys/time.h>
38 #include <asm/unistd.h>
39
40 static void
41 handler(int signo)
42 {
43 }
44
45 int
46 main(void)
47 {
48         struct {
49                 struct timespec ts;
50                 uint32_t pad[2];
51         } req = {
52                 .ts.tv_nsec = 0xc0de1,
53                 .pad = { 0xdeadbeef, 0xbadc0ded }
54         }, rem = {
55                 .ts = { .tv_sec = 0xc0de2, .tv_nsec = 0xc0de3 },
56                 .pad = { 0xdeadbeef, 0xbadc0ded }
57         };
58         const sigset_t set = {};
59         const struct sigaction act = { .sa_handler = handler };
60         const struct itimerval itv = {
61                 .it_interval.tv_usec = 222222,
62                 .it_value.tv_usec = 111111
63         };
64
65         if (syscall(__NR_clock_nanosleep, CLOCK_REALTIME, 0, &req.ts, NULL))
66                 perror_msg_and_skip("clock_nanosleep CLOCK_REALTIME");
67         printf("clock_nanosleep(CLOCK_REALTIME, 0"
68                ", {tv_sec=%lld, tv_nsec=%llu}, NULL) = 0\n",
69                (long long) req.ts.tv_sec,
70                zero_extend_signed_to_ull(req.ts.tv_nsec));
71
72         assert(syscall(__NR_clock_nanosleep, CLOCK_REALTIME, 0,
73                        NULL, &rem.ts) == -1);
74         printf("clock_nanosleep(CLOCK_REALTIME, 0, NULL, %p)"
75                " = -1 EFAULT (%m)\n", &rem.ts);
76
77         assert(syscall(__NR_clock_nanosleep, CLOCK_REALTIME, 0,
78                        &req.ts, &rem.ts) == 0);
79         printf("clock_nanosleep(CLOCK_REALTIME, 0"
80                ", {tv_sec=%lld, tv_nsec=%llu}, %p) = 0\n",
81                (long long) req.ts.tv_sec,
82                zero_extend_signed_to_ull(req.ts.tv_nsec), &rem.ts);
83
84         req.ts.tv_nsec = 999999999 + 1;
85         assert(syscall(__NR_clock_nanosleep, CLOCK_MONOTONIC, 0,
86                        &req.ts, &rem.ts) == -1);
87         printf("clock_nanosleep(CLOCK_MONOTONIC, 0"
88                ", {tv_sec=%lld, tv_nsec=%llu}, %p) = -1 EINVAL (%m)\n",
89                (long long) req.ts.tv_sec,
90                zero_extend_signed_to_ull(req.ts.tv_nsec), &rem.ts);
91
92         req.ts.tv_sec = 0xdeadbeefU;
93         req.ts.tv_nsec = 0xfacefeedU;
94         assert(syscall(__NR_clock_nanosleep, CLOCK_REALTIME, 0,
95                        &req.ts, &rem.ts) == -1);
96         printf("clock_nanosleep(CLOCK_REALTIME, 0"
97                ", {tv_sec=%lld, tv_nsec=%llu}, %p) = -1 EINVAL (%m)\n",
98                (long long) req.ts.tv_sec,
99                zero_extend_signed_to_ull(req.ts.tv_nsec), &rem.ts);
100
101         req.ts.tv_sec = (time_t) 0xcafef00ddeadbeefLL;
102         req.ts.tv_nsec = (long) 0xbadc0dedfacefeedLL;
103         assert(syscall(__NR_clock_nanosleep, CLOCK_MONOTONIC, 0,
104                        &req.ts, &rem.ts) == -1);
105         printf("clock_nanosleep(CLOCK_MONOTONIC, 0"
106                ", {tv_sec=%lld, tv_nsec=%llu}, %p) = -1 EINVAL (%m)\n",
107                (long long) req.ts.tv_sec,
108                zero_extend_signed_to_ull(req.ts.tv_nsec), &rem.ts);
109
110         assert(sigaction(SIGALRM, &act, NULL) == 0);
111         assert(sigprocmask(SIG_SETMASK, &set, NULL) == 0);
112
113         if (setitimer(ITIMER_REAL, &itv, NULL))
114                 perror_msg_and_skip("setitimer");
115
116         req.ts.tv_sec = 0;
117         req.ts.tv_nsec = 999999999;
118         assert(syscall(__NR_clock_nanosleep, CLOCK_REALTIME, 0,
119                        &req.ts, &rem.ts) == -1);
120         printf("clock_nanosleep(CLOCK_REALTIME, 0"
121                ", {tv_sec=%lld, tv_nsec=%llu}, {tv_sec=%lld, tv_nsec=%llu})"
122                " = ? ERESTART_RESTARTBLOCK (Interrupted by signal)\n",
123                (long long) req.ts.tv_sec,
124                zero_extend_signed_to_ull(req.ts.tv_nsec),
125                (long long) rem.ts.tv_sec,
126                zero_extend_signed_to_ull(rem.ts.tv_nsec));
127         puts("--- SIGALRM {si_signo=SIGALRM, si_code=SI_KERNEL} ---");
128
129         assert(syscall(__NR_clock_gettime, CLOCK_REALTIME, &req.ts) == 0);
130         printf("clock_gettime(CLOCK_REALTIME, {tv_sec=%lld, tv_nsec=%llu}) = 0\n",
131                (long long) req.ts.tv_sec,
132                zero_extend_signed_to_ull(req.ts.tv_nsec));
133
134         ++req.ts.tv_sec;
135         rem.ts.tv_sec = 0xc0de4;
136         rem.ts.tv_nsec = 0xc0de5;
137         assert(syscall(__NR_clock_nanosleep, CLOCK_REALTIME, TIMER_ABSTIME,
138                        &req.ts, &rem.ts) == -1);
139         printf("clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME"
140                ", {tv_sec=%lld, tv_nsec=%llu}, %p)"
141                " = ? ERESTARTNOHAND (To be restarted if no handler)\n",
142                (long long) req.ts.tv_sec,
143                zero_extend_signed_to_ull(req.ts.tv_nsec), &rem.ts);
144         puts("--- SIGALRM {si_signo=SIGALRM, si_code=SI_KERNEL} ---");
145
146         puts("+++ exited with 0 +++");
147         return 0;
148 }