2 * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
3 * Copyright (c) 2015-2018 The strace developers.
6 * SPDX-License-Identifier: GPL-2.0-or-later
10 * Based on test by Dr. David Alan Gilbert <dave@treblig.org>
18 #include <sys/select.h>
19 #include <asm/unistd.h>
24 static fd_set set[3][0x1000000 / sizeof(fd_set)];
31 int main(int ac, char **av)
38 .ts = { .tv_sec = 0xc0de1, .tv_nsec = 0xc0de2 },
39 .pad = { 0xdeadbeef, 0xbadc0ded }
42 const struct sigaction act = { .sa_handler = handler };
43 const struct itimerval itv = { .it_value.tv_usec = 111111 };
46 sigaddset(&mask, SIGHUP);
47 sigaddset(&mask, SIGCHLD);
50 perror_msg_and_fail("pipe");
53 * Start with a nice simple pselect.
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]);
61 int rc = pselect(fds[1] + 1, set[0], set[1], set[2], NULL, NULL);
63 perror_msg_and_skip("pselect");
65 printf("pselect6(%d, [%d %d], [%d %d], [1 2], NULL, {NULL, %u}) "
67 fds[1] + 1, fds[0], fds[1],
72 * Another simple one, with a timeout.
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));
88 * Now the crash case that trinity found, negative nfds
89 * but with a pointer to a large chunk of valid memory.
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);
98 * Another variant, with nfds exceeding FD_SETSIZE limit.
101 FD_SET(fds[0], set[0]);
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);
111 * See how timeouts are decoded.
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));
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));
131 assert(sigaction(SIGALRM, &act, NULL) == 0);
132 assert(setitimer(ITIMER_REAL, &itv, NULL) == 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",
141 puts("--- SIGALRM {si_signo=SIGALRM, si_code=SI_KERNEL} ---");
143 puts("+++ exited with 0 +++");
149 SKIP_MAIN_UNDEFINED("__NR_pselect6")