1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2020 Linaro Ltd.
4*49cdfc7eSAndroid Build Coastguard Worker *
5*49cdfc7eSAndroid Build Coastguard Worker * Failure tests.
6*49cdfc7eSAndroid Build Coastguard Worker */
7*49cdfc7eSAndroid Build Coastguard Worker
8*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
9*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
10*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
11*49cdfc7eSAndroid Build Coastguard Worker #include <sys/time.h>
12*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
13*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
14*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
15*49cdfc7eSAndroid Build Coastguard Worker #include "select_var.h"
16*49cdfc7eSAndroid Build Coastguard Worker
17*49cdfc7eSAndroid Build Coastguard Worker static fd_set readfds_reg, writefds_reg, fds_closed;
18*49cdfc7eSAndroid Build Coastguard Worker static fd_set *preadfds_reg = &readfds_reg, *pwritefds_reg = &writefds_reg;
19*49cdfc7eSAndroid Build Coastguard Worker static fd_set *pfds_closed = &fds_closed, *nullfds = NULL, *faulty_fds;
20*49cdfc7eSAndroid Build Coastguard Worker static int fd_closed, fd[2];
21*49cdfc7eSAndroid Build Coastguard Worker static int negative_nfds = -1, maxfds;
22*49cdfc7eSAndroid Build Coastguard Worker static struct timeval timeout = {.tv_sec = 0, .tv_usec = 100000};
23*49cdfc7eSAndroid Build Coastguard Worker
24*49cdfc7eSAndroid Build Coastguard Worker static struct timeval *valid_to = &timeout, *invalid_to;
25*49cdfc7eSAndroid Build Coastguard Worker
26*49cdfc7eSAndroid Build Coastguard Worker static struct tcases {
27*49cdfc7eSAndroid Build Coastguard Worker char *name;
28*49cdfc7eSAndroid Build Coastguard Worker int *nfds;
29*49cdfc7eSAndroid Build Coastguard Worker fd_set **readfds;
30*49cdfc7eSAndroid Build Coastguard Worker fd_set **writefds;
31*49cdfc7eSAndroid Build Coastguard Worker fd_set **exceptfds;
32*49cdfc7eSAndroid Build Coastguard Worker struct timeval **timeout;
33*49cdfc7eSAndroid Build Coastguard Worker int exp_errno;
34*49cdfc7eSAndroid Build Coastguard Worker } tests[] = {
35*49cdfc7eSAndroid Build Coastguard Worker { "Negative nfds", &negative_nfds, &preadfds_reg, &pwritefds_reg, &nullfds, &valid_to, EINVAL },
36*49cdfc7eSAndroid Build Coastguard Worker { "Invalid readfds", &maxfds, &pfds_closed, &pwritefds_reg, &nullfds, &valid_to, EBADF },
37*49cdfc7eSAndroid Build Coastguard Worker { "Invalid writefds", &maxfds, &preadfds_reg, &pfds_closed, &nullfds, &valid_to, EBADF },
38*49cdfc7eSAndroid Build Coastguard Worker { "Invalid exceptfds", &maxfds, &preadfds_reg, &pwritefds_reg, &pfds_closed, &valid_to, EBADF },
39*49cdfc7eSAndroid Build Coastguard Worker { "Faulty readfds", &maxfds, &faulty_fds, &pwritefds_reg, &nullfds, &valid_to, EFAULT },
40*49cdfc7eSAndroid Build Coastguard Worker { "Faulty writefds", &maxfds, &preadfds_reg, &faulty_fds, &nullfds, &valid_to, EFAULT },
41*49cdfc7eSAndroid Build Coastguard Worker { "Faulty exceptfds", &maxfds, &preadfds_reg, &pwritefds_reg, &faulty_fds, &valid_to, EFAULT },
42*49cdfc7eSAndroid Build Coastguard Worker { "Faulty timeout", &maxfds, &preadfds_reg, &pwritefds_reg, &nullfds, &invalid_to, EFAULT },
43*49cdfc7eSAndroid Build Coastguard Worker };
44*49cdfc7eSAndroid Build Coastguard Worker
verify_select(unsigned int n)45*49cdfc7eSAndroid Build Coastguard Worker static void verify_select(unsigned int n)
46*49cdfc7eSAndroid Build Coastguard Worker {
47*49cdfc7eSAndroid Build Coastguard Worker struct tcases *tc = &tests[n];
48*49cdfc7eSAndroid Build Coastguard Worker
49*49cdfc7eSAndroid Build Coastguard Worker TEST(do_select_faulty_to(*tc->nfds, *tc->readfds, *tc->writefds,
50*49cdfc7eSAndroid Build Coastguard Worker *tc->exceptfds, *tc->timeout,
51*49cdfc7eSAndroid Build Coastguard Worker tc->timeout == &invalid_to));
52*49cdfc7eSAndroid Build Coastguard Worker
53*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET != -1) {
54*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "%s: select() passed unexpectedly with %ld",
55*49cdfc7eSAndroid Build Coastguard Worker tc->name, TST_RET);
56*49cdfc7eSAndroid Build Coastguard Worker return;
57*49cdfc7eSAndroid Build Coastguard Worker }
58*49cdfc7eSAndroid Build Coastguard Worker
59*49cdfc7eSAndroid Build Coastguard Worker if (tc->exp_errno != TST_ERR) {
60*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO, "%s: select()() should fail with %s",
61*49cdfc7eSAndroid Build Coastguard Worker tc->name, tst_strerrno(tc->exp_errno));
62*49cdfc7eSAndroid Build Coastguard Worker return;
63*49cdfc7eSAndroid Build Coastguard Worker }
64*49cdfc7eSAndroid Build Coastguard Worker
65*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS | TTERRNO, "%s: select() failed as expected", tc->name);
66*49cdfc7eSAndroid Build Coastguard Worker
67*49cdfc7eSAndroid Build Coastguard Worker exit(0);
68*49cdfc7eSAndroid Build Coastguard Worker }
69*49cdfc7eSAndroid Build Coastguard Worker
run(unsigned int n)70*49cdfc7eSAndroid Build Coastguard Worker static void run(unsigned int n)
71*49cdfc7eSAndroid Build Coastguard Worker {
72*49cdfc7eSAndroid Build Coastguard Worker int pid, status;
73*49cdfc7eSAndroid Build Coastguard Worker
74*49cdfc7eSAndroid Build Coastguard Worker pid = SAFE_FORK();
75*49cdfc7eSAndroid Build Coastguard Worker if (!pid)
76*49cdfc7eSAndroid Build Coastguard Worker verify_select(n);
77*49cdfc7eSAndroid Build Coastguard Worker
78*49cdfc7eSAndroid Build Coastguard Worker SAFE_WAITPID(pid, &status, 0);
79*49cdfc7eSAndroid Build Coastguard Worker
80*49cdfc7eSAndroid Build Coastguard Worker if (WIFEXITED(status))
81*49cdfc7eSAndroid Build Coastguard Worker return;
82*49cdfc7eSAndroid Build Coastguard Worker
83*49cdfc7eSAndroid Build Coastguard Worker if (tst_variant == GLIBC_SELECT_VARIANT &&
84*49cdfc7eSAndroid Build Coastguard Worker tests[n].timeout == &invalid_to &&
85*49cdfc7eSAndroid Build Coastguard Worker WIFSIGNALED(status) && WTERMSIG(status) == SIGSEGV) {
86*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "%s: select() killed by signal", tests[n].name);
87*49cdfc7eSAndroid Build Coastguard Worker return;
88*49cdfc7eSAndroid Build Coastguard Worker }
89*49cdfc7eSAndroid Build Coastguard Worker
90*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "Child %s", tst_strstatus(status));
91*49cdfc7eSAndroid Build Coastguard Worker }
92*49cdfc7eSAndroid Build Coastguard Worker
setup(void)93*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
94*49cdfc7eSAndroid Build Coastguard Worker {
95*49cdfc7eSAndroid Build Coastguard Worker void *faulty_address;
96*49cdfc7eSAndroid Build Coastguard Worker
97*49cdfc7eSAndroid Build Coastguard Worker select_info();
98*49cdfc7eSAndroid Build Coastguard Worker
99*49cdfc7eSAndroid Build Coastguard Worker /* Regular file */
100*49cdfc7eSAndroid Build Coastguard Worker fd_closed = SAFE_OPEN("tmpfile1", O_CREAT | O_RDWR, 0777);
101*49cdfc7eSAndroid Build Coastguard Worker FD_ZERO(&fds_closed);
102*49cdfc7eSAndroid Build Coastguard Worker FD_SET(fd_closed, &fds_closed);
103*49cdfc7eSAndroid Build Coastguard Worker
104*49cdfc7eSAndroid Build Coastguard Worker SAFE_PIPE(fd);
105*49cdfc7eSAndroid Build Coastguard Worker FD_ZERO(&readfds_reg);
106*49cdfc7eSAndroid Build Coastguard Worker FD_ZERO(&writefds_reg);
107*49cdfc7eSAndroid Build Coastguard Worker FD_SET(fd[0], &readfds_reg);
108*49cdfc7eSAndroid Build Coastguard Worker FD_SET(fd[1], &writefds_reg);
109*49cdfc7eSAndroid Build Coastguard Worker
110*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd_closed);
111*49cdfc7eSAndroid Build Coastguard Worker
112*49cdfc7eSAndroid Build Coastguard Worker maxfds = fd[1] + 1;
113*49cdfc7eSAndroid Build Coastguard Worker faulty_address = tst_get_bad_addr(NULL);
114*49cdfc7eSAndroid Build Coastguard Worker invalid_to = faulty_address;
115*49cdfc7eSAndroid Build Coastguard Worker faulty_fds = faulty_address;
116*49cdfc7eSAndroid Build Coastguard Worker }
117*49cdfc7eSAndroid Build Coastguard Worker
118*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
119*49cdfc7eSAndroid Build Coastguard Worker .test = run,
120*49cdfc7eSAndroid Build Coastguard Worker .tcnt = ARRAY_SIZE(tests),
121*49cdfc7eSAndroid Build Coastguard Worker .test_variants = TEST_VARIANTS,
122*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
123*49cdfc7eSAndroid Build Coastguard Worker .needs_tmpdir = 1,
124*49cdfc7eSAndroid Build Coastguard Worker .forks_child = 1,
125*49cdfc7eSAndroid Build Coastguard Worker };
126