xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/select/select01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
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) 2000 Silicon Graphics, Inc.  All Rights Reserved.
4*49cdfc7eSAndroid Build Coastguard Worker  * http://www.sgi.com
5*49cdfc7eSAndroid Build Coastguard Worker  *
6*49cdfc7eSAndroid Build Coastguard Worker  * AUTHOR            : Richard Logan
7*49cdfc7eSAndroid Build Coastguard Worker  * CO-PILOT          : William Roske
8*49cdfc7eSAndroid Build Coastguard Worker  *
9*49cdfc7eSAndroid Build Coastguard Worker  * 1.) select(2) to fd of regular file with no I/O and small timeout
10*49cdfc7eSAndroid Build Coastguard Worker  * 2.) select(2) to fd of system pipe with no I/O and small timeout
11*49cdfc7eSAndroid Build Coastguard Worker  * 3.) select(2) of fd of a named-pipe (FIFO) with no I/O and small timeout value
12*49cdfc7eSAndroid Build Coastguard Worker  */
13*49cdfc7eSAndroid Build Coastguard Worker 
14*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
15*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include <sys/time.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include "select_var.h"
20*49cdfc7eSAndroid Build Coastguard Worker 
21*49cdfc7eSAndroid Build Coastguard Worker static fd_set readfds_reg, readfds_pipe, writefds_pipe, readfds_npipe, writefds_npipe;
22*49cdfc7eSAndroid Build Coastguard Worker static int fd_reg, fds_pipe[2], fd_npipe;
23*49cdfc7eSAndroid Build Coastguard Worker 
24*49cdfc7eSAndroid Build Coastguard Worker static struct tcases {
25*49cdfc7eSAndroid Build Coastguard Worker 	int *nfds;
26*49cdfc7eSAndroid Build Coastguard Worker 	fd_set *readfds;
27*49cdfc7eSAndroid Build Coastguard Worker 	fd_set *writefds;
28*49cdfc7eSAndroid Build Coastguard Worker 	int *readfd;
29*49cdfc7eSAndroid Build Coastguard Worker 	int *writefd;
30*49cdfc7eSAndroid Build Coastguard Worker 	char *desc;
31*49cdfc7eSAndroid Build Coastguard Worker } tests[] = {
32*49cdfc7eSAndroid Build Coastguard Worker 	{&fd_reg, &readfds_reg, NULL, &fd_reg, NULL, "with regular file"},
33*49cdfc7eSAndroid Build Coastguard Worker 	{&fds_pipe[1], &readfds_pipe, &writefds_pipe, &fds_pipe[0], &fds_pipe[1], "with system pipe"},
34*49cdfc7eSAndroid Build Coastguard Worker 	{&fd_npipe, &readfds_npipe, &writefds_npipe, &fd_npipe, &fd_npipe, "with named pipe (FIFO)"},
35*49cdfc7eSAndroid Build Coastguard Worker };
36*49cdfc7eSAndroid Build Coastguard Worker 
run(unsigned int n)37*49cdfc7eSAndroid Build Coastguard Worker static void run(unsigned int n)
38*49cdfc7eSAndroid Build Coastguard Worker {
39*49cdfc7eSAndroid Build Coastguard Worker 	struct tcases *tc = &tests[n];
40*49cdfc7eSAndroid Build Coastguard Worker 	struct timeval timeout;
41*49cdfc7eSAndroid Build Coastguard Worker 	char buf;
42*49cdfc7eSAndroid Build Coastguard Worker 	int exp_ret = 1;
43*49cdfc7eSAndroid Build Coastguard Worker 
44*49cdfc7eSAndroid Build Coastguard Worker 	timeout.tv_sec = 0;
45*49cdfc7eSAndroid Build Coastguard Worker 	timeout.tv_usec = 100000;
46*49cdfc7eSAndroid Build Coastguard Worker 
47*49cdfc7eSAndroid Build Coastguard Worker 	if (tc->writefd) {
48*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_WRITE(SAFE_WRITE_ANY, *tc->writefd, &buf, sizeof(buf));
49*49cdfc7eSAndroid Build Coastguard Worker 		exp_ret++;
50*49cdfc7eSAndroid Build Coastguard Worker 	}
51*49cdfc7eSAndroid Build Coastguard Worker 
52*49cdfc7eSAndroid Build Coastguard Worker 	TEST(do_select(*tc->nfds + 1, tc->readfds, tc->writefds, 0, &timeout));
53*49cdfc7eSAndroid Build Coastguard Worker 
54*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == -1) {
55*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO, "select() %s failed", tc->desc);
56*49cdfc7eSAndroid Build Coastguard Worker 		return;
57*49cdfc7eSAndroid Build Coastguard Worker 	}
58*49cdfc7eSAndroid Build Coastguard Worker 
59*49cdfc7eSAndroid Build Coastguard Worker 	if (!TST_RET) {
60*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "select() %s timed out", tc->desc);
61*49cdfc7eSAndroid Build Coastguard Worker 		return;
62*49cdfc7eSAndroid Build Coastguard Worker 	}
63*49cdfc7eSAndroid Build Coastguard Worker 
64*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET != exp_ret) {
65*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "select() %s returned %lu expected %d",
66*49cdfc7eSAndroid Build Coastguard Worker 		        tc->desc, TST_RET, exp_ret);
67*49cdfc7eSAndroid Build Coastguard Worker 		return;
68*49cdfc7eSAndroid Build Coastguard Worker 	}
69*49cdfc7eSAndroid Build Coastguard Worker 
70*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TPASS, "select() %s returned %i", tc->desc, exp_ret);
71*49cdfc7eSAndroid Build Coastguard Worker 
72*49cdfc7eSAndroid Build Coastguard Worker 	if (FD_ISSET(*tc->readfd, tc->readfds))
73*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "readfds bit %i is set", *tc->readfd);
74*49cdfc7eSAndroid Build Coastguard Worker 	else
75*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "readfds bit %i is not set", *tc->readfd);
76*49cdfc7eSAndroid Build Coastguard Worker 
77*49cdfc7eSAndroid Build Coastguard Worker 	if (!tc->writefd)
78*49cdfc7eSAndroid Build Coastguard Worker 		return;
79*49cdfc7eSAndroid Build Coastguard Worker 
80*49cdfc7eSAndroid Build Coastguard Worker 	if (FD_ISSET(*tc->writefd, tc->writefds))
81*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "writefds bit %i is set", *tc->writefd);
82*49cdfc7eSAndroid Build Coastguard Worker 	else
83*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "writefds bit %i is not set", *tc->writefd);
84*49cdfc7eSAndroid Build Coastguard Worker }
85*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)86*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
87*49cdfc7eSAndroid Build Coastguard Worker {
88*49cdfc7eSAndroid Build Coastguard Worker 	select_info();
89*49cdfc7eSAndroid Build Coastguard Worker 
90*49cdfc7eSAndroid Build Coastguard Worker 	/* Regular file */
91*49cdfc7eSAndroid Build Coastguard Worker 	fd_reg = SAFE_OPEN("tmpfile1", O_CREAT | O_RDWR, 0777);
92*49cdfc7eSAndroid Build Coastguard Worker 	FD_ZERO(&readfds_reg);
93*49cdfc7eSAndroid Build Coastguard Worker 	FD_SET(fd_reg, &readfds_reg);
94*49cdfc7eSAndroid Build Coastguard Worker 
95*49cdfc7eSAndroid Build Coastguard Worker 	/* System pipe*/
96*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_PIPE(fds_pipe);
97*49cdfc7eSAndroid Build Coastguard Worker 	FD_ZERO(&readfds_pipe);
98*49cdfc7eSAndroid Build Coastguard Worker 	FD_ZERO(&writefds_pipe);
99*49cdfc7eSAndroid Build Coastguard Worker 	FD_SET(fds_pipe[0], &readfds_pipe);
100*49cdfc7eSAndroid Build Coastguard Worker 	FD_SET(fds_pipe[1], &writefds_pipe);
101*49cdfc7eSAndroid Build Coastguard Worker 
102*49cdfc7eSAndroid Build Coastguard Worker 	/* Named pipe (FIFO) */
103*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MKFIFO("tmpfile2", 0666);
104*49cdfc7eSAndroid Build Coastguard Worker 	fd_npipe = SAFE_OPEN("tmpfile2", O_RDWR);
105*49cdfc7eSAndroid Build Coastguard Worker 	FD_ZERO(&readfds_npipe);
106*49cdfc7eSAndroid Build Coastguard Worker 	FD_ZERO(&writefds_npipe);
107*49cdfc7eSAndroid Build Coastguard Worker 	FD_SET(fd_npipe, &readfds_npipe);
108*49cdfc7eSAndroid Build Coastguard Worker 	FD_SET(fd_npipe, &writefds_npipe);
109*49cdfc7eSAndroid Build Coastguard Worker }
110*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)111*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
112*49cdfc7eSAndroid Build Coastguard Worker {
113*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_UNLINK("tmpfile2");
114*49cdfc7eSAndroid Build Coastguard Worker }
115*49cdfc7eSAndroid Build Coastguard Worker 
116*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
117*49cdfc7eSAndroid Build Coastguard Worker 	.test = run,
118*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tests),
119*49cdfc7eSAndroid Build Coastguard Worker 	.test_variants = TEST_VARIANTS,
120*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
121*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
122*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
123*49cdfc7eSAndroid Build Coastguard Worker };
124