xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/poll/poll02.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) 2015-2017 Cyril Hrubis <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker  */
5*49cdfc7eSAndroid Build Coastguard Worker 
6*49cdfc7eSAndroid Build Coastguard Worker /*
7*49cdfc7eSAndroid Build Coastguard Worker  * Check that poll() timeouts correctly.
8*49cdfc7eSAndroid Build Coastguard Worker  */
9*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
10*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
11*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
12*49cdfc7eSAndroid Build Coastguard Worker #include <sys/poll.h>
13*49cdfc7eSAndroid Build Coastguard Worker 
14*49cdfc7eSAndroid Build Coastguard Worker #include "tst_timer_test.h"
15*49cdfc7eSAndroid Build Coastguard Worker 
16*49cdfc7eSAndroid Build Coastguard Worker static int fds[2];
17*49cdfc7eSAndroid Build Coastguard Worker 
sample_fn(int clk_id,long long usec)18*49cdfc7eSAndroid Build Coastguard Worker int sample_fn(int clk_id, long long usec)
19*49cdfc7eSAndroid Build Coastguard Worker {
20*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int sleep_ms = usec / 1000;
21*49cdfc7eSAndroid Build Coastguard Worker 
22*49cdfc7eSAndroid Build Coastguard Worker 	struct pollfd pfds[] = {
23*49cdfc7eSAndroid Build Coastguard Worker 		{.fd = fds[0], .events = POLLIN}
24*49cdfc7eSAndroid Build Coastguard Worker 	};
25*49cdfc7eSAndroid Build Coastguard Worker 
26*49cdfc7eSAndroid Build Coastguard Worker 	tst_timer_start(clk_id);
27*49cdfc7eSAndroid Build Coastguard Worker 	TEST(poll(pfds, 1, sleep_ms));
28*49cdfc7eSAndroid Build Coastguard Worker 	tst_timer_stop();
29*49cdfc7eSAndroid Build Coastguard Worker 	tst_timer_sample();
30*49cdfc7eSAndroid Build Coastguard Worker 
31*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET != 0) {
32*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO, "poll() returned %li", TST_RET);
33*49cdfc7eSAndroid Build Coastguard Worker 		return 1;
34*49cdfc7eSAndroid Build Coastguard Worker 	}
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
37*49cdfc7eSAndroid Build Coastguard Worker }
38*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)39*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
40*49cdfc7eSAndroid Build Coastguard Worker {
41*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_PIPE(fds);
42*49cdfc7eSAndroid Build Coastguard Worker }
43*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)44*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
45*49cdfc7eSAndroid Build Coastguard Worker {
46*49cdfc7eSAndroid Build Coastguard Worker 	if (fds[0] > 0)
47*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fds[0]);
48*49cdfc7eSAndroid Build Coastguard Worker 
49*49cdfc7eSAndroid Build Coastguard Worker 	if (fds[1] > 0)
50*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fds[1]);
51*49cdfc7eSAndroid Build Coastguard Worker }
52*49cdfc7eSAndroid Build Coastguard Worker 
53*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
54*49cdfc7eSAndroid Build Coastguard Worker 	.scall = "poll()",
55*49cdfc7eSAndroid Build Coastguard Worker 	.sample = sample_fn,
56*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
57*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
58*49cdfc7eSAndroid Build Coastguard Worker };
59