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