1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2021 Google. All Rights Reserved.
4 * Copyright (c) 2022 Petr Vorel <pvorel@suse.cz>
5 *
6 * Started by Matthew Bobrowski <repnop@google.com>
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_BY_KERNEL(FAN_REPORT_PIDFD);
54 }
55
do_test(unsigned int i)56 static void do_test(unsigned int i)
57 {
58 struct test_case_t *tc = &test_cases[i];
59
60 tst_res(TINFO, "Test %s on %s", tc->exp_errno ? "fail" : "pass",
61 tc->desc);
62
63 TST_EXP_FD_OR_FAIL(fd = fanotify_init(tc->flags, O_RDONLY),
64 tc->exp_errno);
65
66 if (fd > 0)
67 SAFE_CLOSE(fd);
68 }
69
do_cleanup(void)70 static void do_cleanup(void)
71 {
72 if (fd > 0)
73 SAFE_CLOSE(fd);
74 }
75
76 static struct tst_test test = {
77 .setup = do_setup,
78 .test = do_test,
79 .tcnt = ARRAY_SIZE(test_cases),
80 .cleanup = do_cleanup,
81 .all_filesystems = 1,
82 .needs_root = 1,
83 .mntpoint = MOUNT_PATH,
84 };
85
86 #else
87 TST_TEST_TCONF("system doesn't have required fanotify support");
88 #endif /* HAVE_SYS_FANOTIFY_H */
89