xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/fanotify/fanotify20.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2021 Google. All Rights Reserved.
4  * Copyright (c) 2022 Petr Vorel <[email protected]>
5  *
6  * Started by Matthew Bobrowski <[email protected]>
7  */
8 
9 /*\
10  * [Description]
11  *
12  * This source file contains a test case which ensures that the fanotify API
13  * returns an expected error code when provided an invalid initialization flag
14  * alongside FAN_REPORT_PIDFD. Additionally, it checks that the operability with
15  * existing FAN_REPORT_* flags is maintained and functioning as intended.
16  *
17  * NOTE: FAN_REPORT_PIDFD support was added in v5.15-rc1 in af579beb666a
18  * ("fanotify: add pidfd support to the fanotify API").
19  */
20 
21 #define _GNU_SOURCE
22 #include "tst_test.h"
23 #include <errno.h>
24 
25 #ifdef HAVE_SYS_FANOTIFY_H
26 #include "fanotify.h"
27 
28 #define MOUNT_PATH	"fs_mnt"
29 #define FLAGS_DESC(x) .flags = x, .desc = #x
30 
31 static int fd;
32 
33 static struct test_case_t {
34 	unsigned int flags;
35 	char *desc;
36 	int exp_errno;
37 } test_cases[] = {
38 	{
39 		FLAGS_DESC(FAN_REPORT_PIDFD | FAN_REPORT_TID),
40 		.exp_errno = EINVAL,
41 	},
42 	{
43 		FLAGS_DESC(FAN_REPORT_PIDFD | FAN_REPORT_FID | FAN_REPORT_DFID_NAME),
44 	},
45 };
46 
do_setup(void)47 static void do_setup(void)
48 {
49 	/*
50 	 * An explicit check for FAN_REPORT_PIDFD is performed early on in the
51 	 * test initialization as it's a prerequisite for all test cases.
52 	 */
53 	REQUIRE_FANOTIFY_INIT_FLAGS_SUPPORTED_ON_FS(FAN_REPORT_PIDFD,
54 						    MOUNT_PATH);
55 }
56 
do_test(unsigned int i)57 static void do_test(unsigned int i)
58 {
59 	struct test_case_t *tc = &test_cases[i];
60 
61 	tst_res(TINFO, "Test %s on %s", tc->exp_errno ? "fail" : "pass",
62 		tc->desc);
63 
64 	TST_EXP_FD_OR_FAIL(fd = fanotify_init(tc->flags, O_RDONLY),
65 			   tc->exp_errno);
66 
67 	if (fd > 0)
68 		SAFE_CLOSE(fd);
69 }
70 
do_cleanup(void)71 static void do_cleanup(void)
72 {
73 	if (fd > 0)
74 		SAFE_CLOSE(fd);
75 }
76 
77 static struct tst_test test = {
78 	.setup = do_setup,
79 	.test = do_test,
80 	.tcnt = ARRAY_SIZE(test_cases),
81 	.cleanup = do_cleanup,
82 	.all_filesystems = 1,
83 	.needs_root = 1,
84 	.mntpoint = MOUNT_PATH,
85 };
86 
87 #else
88 	TST_TEST_TCONF("system doesn't have required fanotify support");
89 #endif /* HAVE_SYS_FANOTIFY_H */
90