1 /* 2 * Check decoding of clock_nanosleep and clock_gettime syscalls. 3 * 4 * Copyright (c) 2015-2016 Dmitry V. Levin <ldv (at) altlinux.org> 5 * Copyright (c) 2015-2017 The strace developers. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. The name of the author may not be used to endorse or promote products 17 * derived from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include "tests.h" 32 #include <assert.h> 33 #include <stdio.h> 34 #include <stdint.h> 35 #include <signal.h> 36 #include <time.h> 37 #include <unistd.h> 38 #include <sys/time.h> 39 #include <asm/unistd.h> 40 41 static void 42 handler(int signo) 43 { 44 } 45 46 int 47 main(void) 48 { 49 struct { 50 struct timespec ts; 51 uint32_t pad[2]; 52 } req = { 53 .ts.tv_nsec = 0xc0de1, 54 .pad = { 0xdeadbeef, 0xbadc0ded } 55 }, rem = { 56 .ts = { .tv_sec = 0xc0de2, .tv_nsec = 0xc0de3 }, 57 .pad = { 0xdeadbeef, 0xbadc0ded } 58 }; 59 const sigset_t set = {}; 60 const struct sigaction act = { .sa_handler = handler }; 61 const struct itimerval itv = { 62 .it_interval.tv_usec = 222222, 63 .it_value.tv_usec = 111111 64 }; 65 66 if (syscall(__NR_clock_nanosleep, CLOCK_REALTIME, 0, &req.ts, NULL)) 67 perror_msg_and_skip("clock_nanosleep CLOCK_REALTIME"); 68 printf("clock_nanosleep(CLOCK_REALTIME, 0" 69 ", {tv_sec=%lld, tv_nsec=%llu}, NULL) = 0\n", 70 (long long) req.ts.tv_sec, 71 zero_extend_signed_to_ull(req.ts.tv_nsec)); 72 73 assert(syscall(__NR_clock_nanosleep, CLOCK_REALTIME, 0, 74 NULL, &rem.ts) == -1); 75 printf("clock_nanosleep(CLOCK_REALTIME, 0, NULL, %p)" 76 " = -1 EFAULT (%m)\n", &rem.ts); 77 78 assert(syscall(__NR_clock_nanosleep, CLOCK_REALTIME, 0, 79 &req.ts, &rem.ts) == 0); 80 printf("clock_nanosleep(CLOCK_REALTIME, 0" 81 ", {tv_sec=%lld, tv_nsec=%llu}, %p) = 0\n", 82 (long long) req.ts.tv_sec, 83 zero_extend_signed_to_ull(req.ts.tv_nsec), &rem.ts); 84 85 req.ts.tv_nsec = 999999999 + 1; 86 assert(syscall(__NR_clock_nanosleep, CLOCK_MONOTONIC, 0, 87 &req.ts, &rem.ts) == -1); 88 printf("clock_nanosleep(CLOCK_MONOTONIC, 0" 89 ", {tv_sec=%lld, tv_nsec=%llu}, %p) = -1 EINVAL (%m)\n", 90 (long long) req.ts.tv_sec, 91 zero_extend_signed_to_ull(req.ts.tv_nsec), &rem.ts); 92 93 req.ts.tv_sec = 0xdeadbeefU; 94 req.ts.tv_nsec = 0xfacefeedU; 95 assert(syscall(__NR_clock_nanosleep, CLOCK_REALTIME, 0, 96 &req.ts, &rem.ts) == -1); 97 printf("clock_nanosleep(CLOCK_REALTIME, 0" 98 ", {tv_sec=%lld, tv_nsec=%llu}, %p) = -1 EINVAL (%m)\n", 99 (long long) req.ts.tv_sec, 100 zero_extend_signed_to_ull(req.ts.tv_nsec), &rem.ts); 101 102 req.ts.tv_sec = (time_t) 0xcafef00ddeadbeefLL; 103 req.ts.tv_nsec = (long) 0xbadc0dedfacefeedLL; 104 assert(syscall(__NR_clock_nanosleep, CLOCK_MONOTONIC, 0, 105 &req.ts, &rem.ts) == -1); 106 printf("clock_nanosleep(CLOCK_MONOTONIC, 0" 107 ", {tv_sec=%lld, tv_nsec=%llu}, %p) = -1 EINVAL (%m)\n", 108 (long long) req.ts.tv_sec, 109 zero_extend_signed_to_ull(req.ts.tv_nsec), &rem.ts); 110 111 assert(sigaction(SIGALRM, &act, NULL) == 0); 112 assert(sigprocmask(SIG_SETMASK, &set, NULL) == 0); 113 114 if (setitimer(ITIMER_REAL, &itv, NULL)) 115 perror_msg_and_skip("setitimer"); 116 117 req.ts.tv_sec = 0; 118 req.ts.tv_nsec = 999999999; 119 assert(syscall(__NR_clock_nanosleep, CLOCK_REALTIME, 0, 120 &req.ts, &rem.ts) == -1); 121 printf("clock_nanosleep(CLOCK_REALTIME, 0" 122 ", {tv_sec=%lld, tv_nsec=%llu}, {tv_sec=%lld, tv_nsec=%llu})" 123 " = ? ERESTART_RESTARTBLOCK (Interrupted by signal)\n", 124 (long long) req.ts.tv_sec, 125 zero_extend_signed_to_ull(req.ts.tv_nsec), 126 (long long) rem.ts.tv_sec, 127 zero_extend_signed_to_ull(rem.ts.tv_nsec)); 128 puts("--- SIGALRM {si_signo=SIGALRM, si_code=SI_KERNEL} ---"); 129 130 assert(syscall(__NR_clock_gettime, CLOCK_REALTIME, &req.ts) == 0); 131 printf("clock_gettime(CLOCK_REALTIME, {tv_sec=%lld, tv_nsec=%llu}) = 0\n", 132 (long long) req.ts.tv_sec, 133 zero_extend_signed_to_ull(req.ts.tv_nsec)); 134 135 ++req.ts.tv_sec; 136 rem.ts.tv_sec = 0xc0de4; 137 rem.ts.tv_nsec = 0xc0de5; 138 assert(syscall(__NR_clock_nanosleep, CLOCK_REALTIME, TIMER_ABSTIME, 139 &req.ts, &rem.ts) == -1); 140 printf("clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME" 141 ", {tv_sec=%lld, tv_nsec=%llu}, %p)" 142 " = ? ERESTARTNOHAND (To be restarted if no handler)\n", 143 (long long) req.ts.tv_sec, 144 zero_extend_signed_to_ull(req.ts.tv_nsec), &rem.ts); 145 puts("--- SIGALRM {si_signo=SIGALRM, si_code=SI_KERNEL} ---"); 146 147 puts("+++ exited with 0 +++"); 148 return 0; 149 } 150