aboutsummaryrefslogtreecommitdiff
path: root/testcases/kernel/syscalls/fanotify/fanotify20.c
blob: 1d249ac9cfcc17a153ce362cb763cfa10f6cca13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (c) 2021 Google. All Rights Reserved.
 * Copyright (c) 2022 Petr Vorel <pvorel@suse.cz>
 *
 * Started by Matthew Bobrowski <repnop@google.com>
 */

/*\
 * [Description]
 *
 * This source file contains a test case which ensures that the fanotify API
 * returns an expected error code when provided an invalid initialization flag
 * alongside FAN_REPORT_PIDFD. Additionally, it checks that the operability with
 * existing FAN_REPORT_* flags is maintained and functioning as intended.
 *
 * NOTE: FAN_REPORT_PIDFD support was added in v5.15-rc1 in af579beb666a
 * ("fanotify: add pidfd support to the fanotify API").
 */

#define _GNU_SOURCE
#include "tst_test.h"
#include <errno.h>

#ifdef HAVE_SYS_FANOTIFY_H
#include "fanotify.h"

#define MOUNT_PATH	"fs_mnt"
#define FLAGS_DESC(x) .flags = x, .desc = #x

static int fd;

static struct test_case_t {
	unsigned int flags;
	char *desc;
	int exp_errno;
} test_cases[] = {
	{
		FLAGS_DESC(FAN_REPORT_PIDFD | FAN_REPORT_TID),
		.exp_errno = EINVAL,
	},
	{
		FLAGS_DESC(FAN_REPORT_PIDFD | FAN_REPORT_FID | FAN_REPORT_DFID_NAME),
	},
};

static void do_setup(void)
{
	/*
	 * An explicit check for FAN_REPORT_PIDFD is performed early on in the
	 * test initialization as it's a prerequisite for all test cases.
	 */
	REQUIRE_FANOTIFY_INIT_FLAGS_SUPPORTED_ON_FS(FAN_REPORT_PIDFD,
						    MOUNT_PATH);
}

static void do_test(unsigned int i)
{
	struct test_case_t *tc = &test_cases[i];

	tst_res(TINFO, "Test %s on %s", tc->exp_errno ? "fail" : "pass",
		tc->desc);

	TST_EXP_FD_OR_FAIL(fd = fanotify_init(tc->flags, O_RDONLY),
			   tc->exp_errno);

	if (fd > 0)
		SAFE_CLOSE(fd);
}

static void do_cleanup(void)
{
	if (fd > 0)
		SAFE_CLOSE(fd);
}

static struct tst_test test = {
	.setup = do_setup,
	.test = do_test,
	.tcnt = ARRAY_SIZE(test_cases),
	.cleanup = do_cleanup,
	.all_filesystems = 1,
	.needs_root = 1,
	.mntpoint = MOUNT_PATH,
};

#else
	TST_TEST_TCONF("system doesn't have required fanotify support");
#endif /* HAVE_SYS_FANOTIFY_H */