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