Dmitry V. Levin | 49faae9 | 2016-02-19 03:27:09 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of rt_sigtimedwait strace test. |
| 3 | * |
| 4 | * Copyright (c) 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 <sys/syscall.h> |
| 32 | |
| 33 | #ifdef __NR_rt_sigtimedwait |
| 34 | |
| 35 | # include <assert.h> |
| 36 | # include <errno.h> |
| 37 | # include <signal.h> |
| 38 | # include <stdio.h> |
| 39 | # include <stdint.h> |
| 40 | # include <string.h> |
| 41 | # include <unistd.h> |
| 42 | |
| 43 | static long |
| 44 | k_sigtimedwait(const sigset_t *const set, siginfo_t *const info, |
| 45 | const struct timespec *const timeout, const unsigned long size) |
| 46 | { |
| 47 | return syscall(__NR_rt_sigtimedwait, set, info, timeout, size); |
| 48 | } |
| 49 | |
| 50 | static void |
| 51 | iterate(const char *const text, const void *set, |
| 52 | const struct timespec *const timeout, unsigned int size) |
| 53 | { |
| 54 | for (;;) { |
| 55 | assert(k_sigtimedwait(set, NULL, timeout, size) == -1); |
| 56 | if (EINTR == errno) { |
| 57 | tprintf("rt_sigtimedwait(%s, NULL, {%jd, %jd}, %u)" |
| 58 | " = -1 EAGAIN (%m)\n", text, |
| 59 | (intmax_t) timeout->tv_sec, |
| 60 | (intmax_t) timeout->tv_nsec, |
| 61 | size); |
| 62 | } else { |
| 63 | if (size < sizeof(long)) |
| 64 | tprintf("rt_sigtimedwait(%p, NULL, {%jd, %jd}" |
| 65 | ", %u) = -1 EINVAL (%m)\n", |
| 66 | set, (intmax_t) timeout->tv_sec, |
| 67 | (intmax_t) timeout->tv_nsec, size); |
| 68 | else |
| 69 | tprintf("rt_sigtimedwait(%s, NULL, {%jd, %jd}" |
| 70 | ", %u) = -1 EINVAL (%m)\n", |
| 71 | text, (intmax_t) timeout->tv_sec, |
| 72 | (intmax_t) timeout->tv_nsec, size); |
| 73 | } |
| 74 | if (!size) |
| 75 | break; |
| 76 | size >>= 1; |
| 77 | set += size; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | int |
| 82 | main(void) |
| 83 | { |
| 84 | tprintf("%s", ""); |
| 85 | |
| 86 | siginfo_t *const info = tail_alloc(sizeof(*info)); |
| 87 | struct timespec *const timeout = tail_alloc(sizeof(*timeout)); |
| 88 | timeout->tv_sec = 0; |
| 89 | timeout->tv_nsec = 42; |
| 90 | |
| 91 | const unsigned int big_size = 1024 / 8; |
| 92 | void *k_set = tail_alloc(big_size); |
| 93 | memset(k_set, 0, big_size); |
| 94 | |
| 95 | unsigned int set_size = big_size; |
| 96 | for (; set_size; set_size >>= 1, k_set += set_size) { |
| 97 | assert(k_sigtimedwait(k_set, NULL, timeout, set_size) == -1); |
| 98 | if (EAGAIN == errno) |
| 99 | break; |
| 100 | tprintf("rt_sigtimedwait(%p, NULL, {%jd, %jd}, %u)" |
| 101 | " = -1 EINVAL (%m)\n", |
| 102 | k_set, (intmax_t) timeout->tv_sec, |
| 103 | (intmax_t) timeout->tv_nsec, set_size); |
| 104 | } |
| 105 | if (!set_size) |
| 106 | perror_msg_and_fail("rt_sigtimedwait"); |
| 107 | tprintf("rt_sigtimedwait([], NULL, {%jd, %jd}, %u) = -1 EAGAIN (%m)\n", |
| 108 | (intmax_t) timeout->tv_sec, (intmax_t) timeout->tv_nsec, |
| 109 | set_size); |
| 110 | |
| 111 | sigset_t *const libc_set = tail_alloc(sizeof(sigset_t)); |
| 112 | sigemptyset(libc_set); |
| 113 | sigaddset(libc_set, SIGHUP); |
| 114 | memcpy(k_set, libc_set, set_size); |
| 115 | |
| 116 | assert(k_sigtimedwait(k_set, info, timeout, set_size) == -1); |
| 117 | assert(EAGAIN == errno); |
| 118 | tprintf("rt_sigtimedwait([HUP], %p, {%jd, %jd}, %u) = -1 EAGAIN (%m)\n", |
| 119 | info, (intmax_t) timeout->tv_sec, |
| 120 | (intmax_t) timeout->tv_nsec, set_size); |
| 121 | |
| 122 | sigaddset(libc_set, SIGINT); |
| 123 | memcpy(k_set, libc_set, set_size); |
| 124 | |
| 125 | assert(k_sigtimedwait(k_set, info, timeout, set_size) == -1); |
| 126 | assert(EAGAIN == errno); |
| 127 | tprintf("rt_sigtimedwait([HUP INT], %p, {%jd, %jd}, %u)" |
| 128 | " = -1 EAGAIN (%m)\n", |
| 129 | info, (intmax_t) timeout->tv_sec, |
| 130 | (intmax_t) timeout->tv_nsec, set_size); |
| 131 | |
| 132 | sigaddset(libc_set, SIGQUIT); |
| 133 | sigaddset(libc_set, SIGALRM); |
| 134 | sigaddset(libc_set, SIGTERM); |
| 135 | memcpy(k_set, libc_set, set_size); |
| 136 | |
| 137 | assert(k_sigtimedwait(k_set, info, timeout, set_size) == -1); |
| 138 | assert(EAGAIN == errno); |
| 139 | tprintf("rt_sigtimedwait(%s, %p, {%jd, %jd}, %u) = -1 EAGAIN (%m)\n", |
| 140 | "[HUP INT QUIT ALRM TERM]", |
| 141 | info, (intmax_t) timeout->tv_sec, |
| 142 | (intmax_t) timeout->tv_nsec, set_size); |
| 143 | |
| 144 | memset(k_set - set_size, -1, set_size); |
| 145 | assert(k_sigtimedwait(k_set - set_size, info, timeout, set_size) == -1); |
| 146 | assert(EAGAIN == errno); |
| 147 | tprintf("rt_sigtimedwait(~[], %p, {%jd, %jd}, %u) = -1 EAGAIN (%m)\n", |
| 148 | info, (intmax_t) timeout->tv_sec, |
| 149 | (intmax_t) timeout->tv_nsec, set_size); |
| 150 | |
| 151 | if (sigprocmask(SIG_SETMASK, libc_set, NULL)) |
| 152 | perror_msg_and_fail("sigprocmask"); |
| 153 | |
| 154 | assert(k_sigtimedwait(k_set - set_size, info, NULL, set_size << 1) == -1); |
| 155 | tprintf("rt_sigtimedwait(%p, %p, NULL, %u) = -1 EINVAL (%m)\n", |
| 156 | k_set - set_size, info, set_size << 1); |
| 157 | |
| 158 | iterate("~[]", k_set - set_size, timeout, set_size >> 1); |
| 159 | |
| 160 | timeout->tv_sec = 1; |
| 161 | raise(SIGALRM); |
| 162 | assert(k_sigtimedwait(k_set, info, timeout, set_size) == SIGALRM); |
| 163 | tprintf("rt_sigtimedwait(%s, {si_signo=%s, si_code=SI_TKILL" |
| 164 | ", si_pid=%d, si_uid=%d}, {%jd, %jd}, %u) = %d (%s)\n", |
| 165 | "[HUP INT QUIT ALRM TERM]", "SIGALRM", getpid(), getuid(), |
| 166 | (intmax_t) timeout->tv_sec, (intmax_t) timeout->tv_nsec, |
| 167 | set_size, SIGALRM, "SIGALRM"); |
| 168 | |
| 169 | raise(SIGALRM); |
| 170 | assert(k_sigtimedwait(k_set, NULL, NULL, set_size) == SIGALRM); |
| 171 | tprintf("rt_sigtimedwait(%s, NULL, NULL, %u) = %d (%s)\n", |
| 172 | "[HUP INT QUIT ALRM TERM]", set_size, SIGALRM, "SIGALRM"); |
| 173 | |
| 174 | tprintf("+++ exited with 0 +++\n"); |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | #else |
| 179 | |
| 180 | SKIP_MAIN_UNDEFINED("__NR_rt_sigtimedwait") |
| 181 | |
| 182 | #endif |