]> granicus.if.org Git - strace/blob - tests/pselect6.c
strace: terminate itself if interrupted by a signal
[strace] / tests / pselect6.c
1 /*
2  * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
3  * Copyright (c) 2015-2017 The strace developers.
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: GPL-2.0-or-later
7  */
8
9 /*
10  * Based on test by Dr. David Alan Gilbert <dave@treblig.org>
11  */
12
13 #include "tests.h"
14 #include "nsig.h"
15 #include <assert.h>
16 #include <stdio.h>
17 #include <unistd.h>
18 #include <sys/select.h>
19 #include <asm/unistd.h>
20 #include <sys/time.h>
21
22 #ifdef __NR_pselect6
23
24 static fd_set set[3][0x1000000 / sizeof(fd_set)];
25
26 static void
27 handler(int signo)
28 {
29 }
30
31 int main(int ac, char **av)
32 {
33         int fds[2];
34         struct {
35                 struct timespec ts;
36                 int pad[2];
37         } tm_in = {
38                 .ts = { .tv_sec = 0xc0de1, .tv_nsec = 0xc0de2 },
39                 .pad = { 0xdeadbeef, 0xbadc0ded }
40         }, tm = tm_in;
41         sigset_t mask;
42         const struct sigaction act = { .sa_handler = handler };
43         const struct itimerval itv = { .it_value.tv_usec = 111111 };
44
45         sigemptyset(&mask);
46         sigaddset(&mask, SIGHUP);
47         sigaddset(&mask, SIGCHLD);
48
49         if (pipe(fds))
50                 perror_msg_and_fail("pipe");
51
52         /*
53          * Start with a nice simple pselect.
54          */
55         FD_SET(fds[0], set[0]);
56         FD_SET(fds[1], set[0]);
57         FD_SET(fds[0], set[1]);
58         FD_SET(fds[1], set[1]);
59         FD_SET(1, set[2]);
60         FD_SET(2, set[2]);
61         int rc = pselect(fds[1] + 1, set[0], set[1], set[2], NULL, NULL);
62         if (rc < 0)
63                 perror_msg_and_skip("pselect");
64         assert(rc == 1);
65         printf("pselect6(%d, [%d %d], [%d %d], [1 2], NULL, {NULL, %u}) "
66                "= 1 (out [%d])\n",
67                fds[1] + 1, fds[0], fds[1],
68                fds[0], fds[1],
69                NSIG_BYTES, fds[1]);
70
71         /*
72          * Another simple one, with a timeout.
73          */
74         FD_SET(1, set[1]);
75         FD_SET(2, set[1]);
76         FD_SET(fds[0], set[1]);
77         FD_SET(fds[1], set[1]);
78         assert(syscall(__NR_pselect6, fds[1] + 1, NULL, set[1], NULL, &tm.ts, NULL) == 3);
79         printf("pselect6(%d, NULL, [1 2 %d %d], NULL"
80                ", {tv_sec=%lld, tv_nsec=%llu}, NULL) = 3 (out [1 2 %d]"
81                ", left {tv_sec=%lld, tv_nsec=%llu})\n",
82                fds[1] + 1, fds[0], fds[1], (long long) tm_in.ts.tv_sec,
83                zero_extend_signed_to_ull(tm_in.ts.tv_nsec),
84                fds[1], (long long) tm.ts.tv_sec,
85                zero_extend_signed_to_ull(tm.ts.tv_nsec));
86
87         /*
88          * Now the crash case that trinity found, negative nfds
89          * but with a pointer to a large chunk of valid memory.
90          */
91         FD_ZERO(set[0]);
92         FD_SET(fds[1], set[0]);
93         assert(pselect(-1, NULL, set[0], NULL, NULL, &mask) == -1);
94         printf("pselect6(-1, NULL, %p, NULL, NULL, {[HUP CHLD], %u}) "
95                "= -1 EINVAL (%m)\n", set[0], NSIG_BYTES);
96
97         /*
98          * Another variant, with nfds exceeding FD_SETSIZE limit.
99          */
100         FD_ZERO(set[0]);
101         FD_SET(fds[0], set[0]);
102         FD_ZERO(set[1]);
103         tm.ts.tv_sec = 0;
104         tm.ts.tv_nsec = 123;
105         assert(pselect(FD_SETSIZE + 1, set[0], set[1], NULL, &tm.ts, &mask) == 0);
106         printf("pselect6(%d, [%d], [], NULL, {tv_sec=0, tv_nsec=123}"
107                ", {[HUP CHLD], %u}) = 0 (Timeout)\n",
108                FD_SETSIZE + 1, fds[0], NSIG_BYTES);
109
110         /*
111          * See how timeouts are decoded.
112          */
113         tm.ts.tv_sec = 0xdeadbeefU;
114         tm.ts.tv_nsec = 0xfacefeedU;
115         assert(pselect(0, NULL, NULL, NULL, &tm.ts, NULL) == -1);
116         printf("pselect6(0, NULL, NULL, NULL"
117                ", {tv_sec=%lld, tv_nsec=%llu}, {NULL, %u}) = %s\n",
118                (long long) tm.ts.tv_sec,
119                zero_extend_signed_to_ull(tm.ts.tv_nsec),
120                NSIG_BYTES, sprintrc(-1));
121
122         tm.ts.tv_sec = (time_t) 0xcafef00ddeadbeefLL;
123         tm.ts.tv_nsec = (long) 0xbadc0dedfacefeedLL;
124         assert(pselect(0, NULL, NULL, NULL, &tm.ts, NULL) == -1);
125         printf("pselect6(0, NULL, NULL, NULL"
126                ", {tv_sec=%lld, tv_nsec=%llu}, {NULL, %u}) = %s\n",
127                (long long) tm.ts.tv_sec,
128                zero_extend_signed_to_ull(tm.ts.tv_nsec),
129                NSIG_BYTES, sprintrc(-1));
130
131         assert(sigaction(SIGALRM, &act, NULL) == 0);
132         assert(setitimer(ITIMER_REAL, &itv, NULL) == 0);
133
134         tm.ts.tv_sec = 0;
135         tm.ts.tv_nsec = 222222222;
136         assert(pselect(0, NULL, NULL, NULL, &tm.ts, &mask) == -1);
137         printf("pselect6(0, NULL, NULL, NULL, {tv_sec=0, tv_nsec=222222222}"
138                ", {[HUP CHLD], %u})"
139                " = ? ERESTARTNOHAND (To be restarted if no handler)\n",
140                NSIG_BYTES);
141         puts("--- SIGALRM {si_signo=SIGALRM, si_code=SI_KERNEL} ---");
142
143         puts("+++ exited with 0 +++");
144         return 0;
145 }
146
147 #else
148
149 SKIP_MAIN_UNDEFINED("__NR_pselect6")
150
151 #endif