xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/timerfd/timerfd02.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) Ulrich Drepper <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) International Business Machines  Corp., 2009
5*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (C) 2023 SUSE LLC Andrea Cervesato <[email protected]>
6*49cdfc7eSAndroid Build Coastguard Worker  */
7*49cdfc7eSAndroid Build Coastguard Worker /*\
8*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  * This test verifies that:
11*49cdfc7eSAndroid Build Coastguard Worker  * - TFD_CLOEXEC sets the close-on-exec file status flag on the new open  file
12*49cdfc7eSAndroid Build Coastguard Worker  * - TFD_NONBLOCK sets the O_NONBLOCK file status flag on the new open file
13*49cdfc7eSAndroid Build Coastguard Worker  */
14*49cdfc7eSAndroid Build Coastguard Worker 
15*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
16*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_timerfd.h"
17*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/fcntl.h"
18*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/syscalls.h"
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker static int fdesc;
21*49cdfc7eSAndroid Build Coastguard Worker 
22*49cdfc7eSAndroid Build Coastguard Worker static struct test_case_t {
23*49cdfc7eSAndroid Build Coastguard Worker 	int flags;
24*49cdfc7eSAndroid Build Coastguard Worker 	int check;
25*49cdfc7eSAndroid Build Coastguard Worker 	int expected;
26*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
27*49cdfc7eSAndroid Build Coastguard Worker 	{ 0, F_GETFD, 0 },
28*49cdfc7eSAndroid Build Coastguard Worker 	{ TFD_CLOEXEC, F_GETFD, FD_CLOEXEC },
29*49cdfc7eSAndroid Build Coastguard Worker 	{ TFD_NONBLOCK, F_GETFL, O_NONBLOCK }
30*49cdfc7eSAndroid Build Coastguard Worker };
31*49cdfc7eSAndroid Build Coastguard Worker 
run(unsigned int i)32*49cdfc7eSAndroid Build Coastguard Worker static void run(unsigned int i)
33*49cdfc7eSAndroid Build Coastguard Worker {
34*49cdfc7eSAndroid Build Coastguard Worker 	struct test_case_t *tcase = &tcases[i];
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_FD(fdesc = SAFE_TIMERFD_CREATE(CLOCK_REALTIME, tcase->flags));
37*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_EQ_LI(SAFE_FCNTL(fdesc, tcase->check) & tcase->expected, tcase->expected);
38*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fdesc);
39*49cdfc7eSAndroid Build Coastguard Worker }
40*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)41*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
42*49cdfc7eSAndroid Build Coastguard Worker {
43*49cdfc7eSAndroid Build Coastguard Worker 	if (fcntl(fdesc, F_GETFD) != -1)
44*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fdesc);
45*49cdfc7eSAndroid Build Coastguard Worker }
46*49cdfc7eSAndroid Build Coastguard Worker 
47*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
48*49cdfc7eSAndroid Build Coastguard Worker 	.test = run,
49*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
50*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
51*49cdfc7eSAndroid Build Coastguard Worker };
52