xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/fanotify/fanotify08.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2017 RedHat.  All Rights Reserved.
4  *
5  * Started by Xiong Zhou <[email protected]>
6  */
7 
8 /*\
9  * [Description]
10  * Sanity check fanotify_init flag FAN_CLOEXEC by fcntl.
11  */
12 
13 #define _GNU_SOURCE
14 #include "config.h"
15 
16 #include <stdio.h>
17 #include <sys/stat.h>
18 #include <sys/types.h>
19 #include <errno.h>
20 #include <string.h>
21 #include <sys/syscall.h>
22 #include "tst_test.h"
23 
24 #ifdef HAVE_SYS_FANOTIFY_H
25 #include "fanotify.h"
26 
27 static int fd_notify;
28 
test_init_bit(unsigned int fan_bit,unsigned int fd_bit,char * msg)29 static void test_init_bit(unsigned int fan_bit,
30 			unsigned int fd_bit, char *msg)
31 {
32 	int ret;
33 
34 	fd_notify = SAFE_FANOTIFY_INIT(FAN_CLASS_NOTIF|fan_bit, O_RDONLY);
35 
36 	ret = SAFE_FCNTL(fd_notify, F_GETFD);
37 
38 	if ((ret & FD_CLOEXEC) == fd_bit)
39 		tst_res(TPASS, "%s", msg);
40 	else
41 		tst_res(TFAIL, "%s", msg);
42 
43 	SAFE_CLOSE(fd_notify);
44 }
45 
run(unsigned int i)46 static void run(unsigned int i)
47 {
48 	switch (i) {
49 	case 0:
50 		test_init_bit(0, 0, "not set close_on_exec");
51 		break;
52 	case 1:
53 		test_init_bit(FAN_CLOEXEC, FD_CLOEXEC, "set close_on_exec");
54 		break;
55 	}
56 }
57 
cleanup(void)58 static void cleanup(void)
59 {
60 	if (fd_notify > 0)
61 		SAFE_CLOSE(fd_notify);
62 }
63 
64 static struct tst_test test = {
65 	.test = run,
66 	.tcnt = 2,
67 	.cleanup = cleanup,
68 	.needs_root = 1,
69 };
70 
71 #else
72 	TST_TEST_TCONF("system doesn't have required fanotify support");
73 #endif
74