]> granicus.if.org Git - strace/blob - tests/ppoll.c
tests: use TAIL_ALLOC_OBJECT_CONST_PTR
[strace] / tests / ppoll.c
1 /*
2  * Check decoding of ppoll syscall.
3  *
4  * Copyright (c) 2015-2017 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_ppoll
34
35 # include <errno.h>
36 # include <poll.h>
37 # include <signal.h>
38 # include <stdio.h>
39 # include <string.h>
40 # include <unistd.h>
41
42 static const char *errstr;
43
44 static long
45 sys_ppoll(const kernel_ulong_t ufds,
46           const kernel_ulong_t nfds,
47           const kernel_ulong_t tsp,
48           const kernel_ulong_t sigmask,
49           const kernel_ulong_t sigsetsize)
50 {
51         long rc = syscall(__NR_ppoll, ufds, nfds, tsp, sigmask, sigsetsize);
52         errstr = sprintrc(rc);
53         return rc;
54 }
55
56 int
57 main(void)
58 {
59         static const kernel_ulong_t bogus_nfds =
60                 (kernel_ulong_t) 0xdeadbeeffacefeedULL;
61         static const kernel_ulong_t bogus_sigsetsize =
62                 (kernel_ulong_t) 0xdeadbeefbadc0dedULL;
63         static const char *const POLLWRNORM_str =
64                 (POLLWRNORM == POLLOUT) ? "" : "|POLLWRNORM";
65         static const char *const USR2_CHLD_str =
66                 (SIGUSR2 < SIGCHLD) ? "USR2 CHLD" : "CHLD USR2";
67         void *const efault = tail_alloc(1024) + 1024;
68         TAIL_ALLOC_OBJECT_CONST_PTR(struct timespec, ts);
69         const unsigned int sigset_size = get_sigset_size();
70         void *const sigmask = tail_alloc(sigset_size);
71         struct pollfd *fds;
72         sigset_t mask;
73         int pipe_fd[4];
74         long rc;
75
76         sys_ppoll(0, bogus_nfds, 0, 0, bogus_sigsetsize);
77         if (ENOSYS == errno)
78                 perror_msg_and_skip("ppoll");
79         printf("ppoll(NULL, %u, NULL, NULL, %llu) = %s\n",
80                (unsigned) bogus_nfds, (unsigned long long) bogus_sigsetsize,
81                errstr);
82
83         sys_ppoll((unsigned long) efault, 42, (unsigned long) efault + 8,
84                   (unsigned long) efault + 16, sigset_size);
85         printf("ppoll(%p, %u, %p, %p, %u) = %s\n",
86                efault, 42, efault + 8, efault + 16, sigset_size, errstr);
87
88         if (pipe(pipe_fd) || pipe(pipe_fd + 2))
89                 perror_msg_and_fail("pipe");
90
91         ts->tv_sec = 42;
92         ts->tv_nsec = 999999999;
93
94         const struct pollfd fds1[] = {
95                 { .fd = pipe_fd[0], .events = POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND },
96                 { .fd = pipe_fd[1], .events = POLLOUT | POLLWRNORM | POLLWRBAND },
97                 { .fd = pipe_fd[2], .events = POLLIN | POLLPRI },
98                 { .fd = pipe_fd[3], .events = POLLOUT }
99         };
100         fds = efault - sizeof(fds1);
101         memcpy(fds, fds1, sizeof(fds1));
102
103         sigemptyset(&mask);
104         sigaddset(&mask, SIGUSR2);
105         sigaddset(&mask, SIGCHLD);
106         memcpy(sigmask, &mask, sigset_size);
107
108         rc = sys_ppoll((unsigned long) fds,
109                        F8ILL_KULONG_MASK | ARRAY_SIZE(fds1), (unsigned long) ts,
110                        (unsigned long) sigmask, sigset_size);
111         if (rc != 2)
112                 perror_msg_and_fail("ppoll 1");
113         printf("ppoll([{fd=%d, events=POLLIN|POLLPRI|POLLRDNORM|POLLRDBAND}"
114                ", {fd=%d, events=POLLOUT%s|POLLWRBAND}"
115 #if VERBOSE
116                ", {fd=%d, events=POLLIN|POLLPRI}, {fd=%d, events=POLLOUT}]"
117 #else
118                ", ...]"
119 #endif
120                ", %u, {tv_sec=42, tv_nsec=999999999}, [%s], %u) = %ld"
121                " ([{fd=%d, revents=POLLOUT%s}, {fd=%d, revents=POLLOUT}]"
122                ", left {tv_sec=%u, tv_nsec=%u})\n",
123                pipe_fd[0], pipe_fd[1], POLLWRNORM_str,
124 #if VERBOSE
125                pipe_fd[2], pipe_fd[3],
126 #endif
127                (unsigned) ARRAY_SIZE(fds1), USR2_CHLD_str,
128                (unsigned) sigset_size, rc, pipe_fd[1], POLLWRNORM_str,
129                pipe_fd[3], (unsigned ) ts->tv_sec, (unsigned) ts->tv_nsec);
130
131         ts->tv_sec = 0;
132         ts->tv_nsec = 999;
133         const struct pollfd fds2[] = {
134                 { .fd = pipe_fd[1], .events = POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND },
135                 { .fd = pipe_fd[0], .events = POLLOUT | POLLWRNORM | POLLWRBAND }
136         };
137         fds = efault - sizeof(fds2);
138         memcpy(fds, fds2, sizeof(fds2));
139
140         memset(&mask, -1, sizeof(mask));
141         sigdelset(&mask, SIGHUP);
142         sigdelset(&mask, SIGKILL);
143         sigdelset(&mask, SIGSTOP);
144         memcpy(sigmask, &mask, sigset_size);
145
146         rc = sys_ppoll((unsigned long) fds,
147                        F8ILL_KULONG_MASK | ARRAY_SIZE(fds2), (unsigned long) ts,
148                        (unsigned long) sigmask, sigset_size);
149         if (rc != 0)
150                 perror_msg_and_fail("ppoll 2");
151         printf("ppoll([{fd=%d, events=POLLIN|POLLPRI|POLLRDNORM|POLLRDBAND}"
152                ", {fd=%d, events=POLLOUT%s|POLLWRBAND}], %u"
153                ", {tv_sec=0, tv_nsec=999}, ~[HUP KILL STOP], %u)"
154                " = %ld (Timeout)\n",
155                pipe_fd[1], pipe_fd[0], POLLWRNORM_str,
156                (unsigned) ARRAY_SIZE(fds2), sigset_size, rc);
157
158         if (F8ILL_KULONG_SUPPORTED) {
159                 sys_ppoll(f8ill_ptr_to_kulong(fds), ARRAY_SIZE(fds2),
160                           f8ill_ptr_to_kulong(ts), f8ill_ptr_to_kulong(sigmask),
161                           sigset_size);
162                 printf("ppoll(%#llx, %u, %#llx, %#llx, %u) = %s\n",
163                        (unsigned long long) f8ill_ptr_to_kulong(fds),
164                        (unsigned) ARRAY_SIZE(fds2),
165                        (unsigned long long) f8ill_ptr_to_kulong(ts),
166                        (unsigned long long) f8ill_ptr_to_kulong(sigmask),
167                        (unsigned) sigset_size, errstr);
168         }
169
170         puts("+++ exited with 0 +++");
171         return 0;
172 }
173
174 #else
175
176 SKIP_MAIN_UNDEFINED("__NR_ppoll")
177
178 #endif