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 <asm/unistd.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
k_sigtimedwait(const sigset_t * const set,siginfo_t * const info,const struct timespec * const timeout,const unsigned long size)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
iterate(const char * const text,const void * set,const struct timespec * const timeout,unsigned int size)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, "
58 "{tv_sec=%jd, tv_nsec=%jd}, %u)"
59 " = -1 EAGAIN (%m)\n", text,
60 (intmax_t) timeout->tv_sec,
61 (intmax_t) timeout->tv_nsec,
62 size);
63 } else {
64 if (size < sizeof(long))
65 tprintf("rt_sigtimedwait(%p, NULL, "
66 "{tv_sec=%jd, tv_nsec=%jd}"
67 ", %u) = -1 EINVAL (%m)\n",
68 set, (intmax_t) timeout->tv_sec,
69 (intmax_t) timeout->tv_nsec, size);
70 else
71 tprintf("rt_sigtimedwait(%s, NULL, "
72 "{tv_sec=%jd, tv_nsec=%jd}"
73 ", %u) = -1 EINVAL (%m)\n",
74 text, (intmax_t) timeout->tv_sec,
75 (intmax_t) timeout->tv_nsec, size);
76 }
77 if (!size)
78 break;
79 size >>= 1;
80 set += size;
81 }
82 }
83
84 int
main(void)85 main(void)
86 {
87 tprintf("%s", "");
88
89 siginfo_t *const info = tail_alloc(sizeof(*info));
90 struct timespec *const timeout = tail_alloc(sizeof(*timeout));
91 timeout->tv_sec = 0;
92 timeout->tv_nsec = 42;
93
94 const unsigned int big_size = 1024 / 8;
95 void *k_set = tail_alloc(big_size);
96 memset(k_set, 0, big_size);
97
98 unsigned int set_size = big_size;
99 for (; set_size; set_size >>= 1, k_set += set_size) {
100 assert(k_sigtimedwait(k_set, NULL, timeout, set_size) == -1);
101 if (EAGAIN == errno)
102 break;
103 tprintf("rt_sigtimedwait(%p, NULL, {tv_sec=%jd, tv_nsec=%jd}, %u)"
104 " = -1 EINVAL (%m)\n",
105 k_set, (intmax_t) timeout->tv_sec,
106 (intmax_t) timeout->tv_nsec, set_size);
107 }
108 if (!set_size)
109 perror_msg_and_fail("rt_sigtimedwait");
110 tprintf("rt_sigtimedwait([], NULL, {tv_sec=%jd, tv_nsec=%jd}, %u) = -1 EAGAIN (%m)\n",
111 (intmax_t) timeout->tv_sec, (intmax_t) timeout->tv_nsec,
112 set_size);
113
114 sigset_t *const libc_set = tail_alloc(sizeof(sigset_t));
115 sigemptyset(libc_set);
116 sigaddset(libc_set, SIGHUP);
117 memcpy(k_set, libc_set, set_size);
118
119 assert(k_sigtimedwait(k_set, info, timeout, set_size) == -1);
120 assert(EAGAIN == errno);
121 tprintf("rt_sigtimedwait([HUP], %p, {tv_sec=%jd, tv_nsec=%jd}, %u) = -1 EAGAIN (%m)\n",
122 info, (intmax_t) timeout->tv_sec,
123 (intmax_t) timeout->tv_nsec, set_size);
124
125 sigaddset(libc_set, SIGINT);
126 memcpy(k_set, libc_set, set_size);
127
128 assert(k_sigtimedwait(k_set, info, timeout, set_size) == -1);
129 assert(EAGAIN == errno);
130 tprintf("rt_sigtimedwait([HUP INT], %p, {tv_sec=%jd, tv_nsec=%jd}, %u)"
131 " = -1 EAGAIN (%m)\n",
132 info, (intmax_t) timeout->tv_sec,
133 (intmax_t) timeout->tv_nsec, set_size);
134
135 sigaddset(libc_set, SIGQUIT);
136 sigaddset(libc_set, SIGALRM);
137 sigaddset(libc_set, SIGTERM);
138 memcpy(k_set, libc_set, set_size);
139
140 assert(k_sigtimedwait(k_set, info, timeout, set_size) == -1);
141 assert(EAGAIN == errno);
142 tprintf("rt_sigtimedwait(%s, %p, {tv_sec=%jd, tv_nsec=%jd}, %u) = -1 EAGAIN (%m)\n",
143 "[HUP INT QUIT ALRM TERM]",
144 info, (intmax_t) timeout->tv_sec,
145 (intmax_t) timeout->tv_nsec, set_size);
146
147 memset(k_set - set_size, -1, set_size);
148 assert(k_sigtimedwait(k_set - set_size, info, timeout, set_size) == -1);
149 assert(EAGAIN == errno);
150 tprintf("rt_sigtimedwait(~[], %p, {tv_sec=%jd, tv_nsec=%jd}, %u) = -1 EAGAIN (%m)\n",
151 info, (intmax_t) timeout->tv_sec,
152 (intmax_t) timeout->tv_nsec, set_size);
153
154 if (sigprocmask(SIG_SETMASK, libc_set, NULL))
155 perror_msg_and_fail("sigprocmask");
156
157 assert(k_sigtimedwait(k_set - set_size, info, NULL, set_size << 1) == -1);
158 tprintf("rt_sigtimedwait(%p, %p, NULL, %u) = -1 EINVAL (%m)\n",
159 k_set - set_size, info, set_size << 1);
160
161 iterate("~[]", k_set - set_size, timeout, set_size >> 1);
162
163 timeout->tv_sec = 1;
164 raise(SIGALRM);
165 assert(k_sigtimedwait(k_set, info, timeout, set_size) == SIGALRM);
166 tprintf("rt_sigtimedwait(%s, {si_signo=%s, si_code=SI_TKILL"
167 ", si_pid=%d, si_uid=%d}, {tv_sec=%jd, tv_nsec=%jd}, %u) = %d (%s)\n",
168 "[HUP INT QUIT ALRM TERM]", "SIGALRM", getpid(), getuid(),
169 (intmax_t) timeout->tv_sec, (intmax_t) timeout->tv_nsec,
170 set_size, SIGALRM, "SIGALRM");
171
172 raise(SIGALRM);
173 assert(k_sigtimedwait(k_set, NULL, NULL, set_size) == SIGALRM);
174 tprintf("rt_sigtimedwait(%s, NULL, NULL, %u) = %d (%s)\n",
175 "[HUP INT QUIT ALRM TERM]", set_size, SIGALRM, "SIGALRM");
176
177 tprintf("+++ exited with 0 +++\n");
178 return 0;
179 }
180
181 #else
182
183 SKIP_MAIN_UNDEFINED("__NR_rt_sigtimedwait")
184
185 #endif
186