2 * Check decoding of clock_nanosleep and clock_gettime syscalls.
4 * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
38 #include <asm/unistd.h>
52 .ts.tv_nsec = 0xc0de1,
53 .pad = { 0xdeadbeef, 0xbadc0ded }
55 .ts = { .tv_sec = 0xc0de2, .tv_nsec = 0xc0de3 },
56 .pad = { 0xdeadbeef, 0xbadc0ded }
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
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));
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);
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);
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);
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);
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);
110 assert(sigaction(SIGALRM, &act, NULL) == 0);
111 assert(sigprocmask(SIG_SETMASK, &set, NULL) == 0);
113 if (setitimer(ITIMER_REAL, &itv, NULL))
114 perror_msg_and_skip("setitimer");
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} ---");
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));
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} ---");
146 puts("+++ exited with 0 +++");