xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/select/select02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2015-2017 Cyril Hrubis <[email protected]>
4  */
5 
6 /*
7  * Check that select() timeouts correctly.
8  */
9 #include <unistd.h>
10 #include <errno.h>
11 #include <sys/time.h>
12 #include <sys/types.h>
13 #include <fcntl.h>
14 
15 #include "tst_timer_test.h"
16 
17 #include "select_var.h"
18 
19 static int fds[2];
20 
sample_fn(int clk_id,long long usec)21 static int sample_fn(int clk_id, long long usec)
22 {
23 	struct timeval timeout = tst_us_to_timeval(usec);
24 	fd_set sfds;
25 
26 	FD_ZERO(&sfds);
27 
28 	FD_SET(fds[0], &sfds);
29 
30 	tst_timer_start(clk_id);
31 	TEST(do_select(1, &sfds, NULL, NULL, &timeout));
32 	tst_timer_stop();
33 	tst_timer_sample();
34 
35 	if (TST_RET != 0) {
36 		tst_res(TFAIL | TTERRNO, "select() returned %li", TST_RET);
37 		return 1;
38 	}
39 
40 	return 0;
41 }
42 
setup(void)43 static void setup(void)
44 {
45 	select_info();
46 
47 	SAFE_PIPE(fds);
48 }
49 
cleanup(void)50 static void cleanup(void)
51 {
52 	if (fds[0] > 0)
53 		SAFE_CLOSE(fds[0]);
54 
55 	if (fds[1] > 0)
56 		SAFE_CLOSE(fds[1]);
57 }
58 
59 static struct tst_test test = {
60 	.scall = "select()",
61 	.sample = sample_fn,
62 	.setup = setup,
63 	.test_variants = TEST_VARIANTS,
64 	.cleanup = cleanup,
65 };
66