Skip to content

Commit a437718

Browse files
amir73ilpevik
authored andcommitted
fanotify21: Test reporting fd open errors with FAN_REPORT_FD_ERROR
Expect to get -EROFS as event->fd. Expect to get -ESRCH instead of FAN_NOPIDFD. Link: https://lore.kernel.org/ltp/[email protected]/ Reviewed-by: Petr Vorel <[email protected]> Signed-off-by: Amir Goldstein <[email protected]>
1 parent 3e19deb commit a437718

File tree

1 file changed

+51
-10
lines changed

1 file changed

+51
-10
lines changed

testcases/kernel/syscalls/fanotify/fanotify21.c

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ static struct test_case_t {
5757
{
5858
"return invalid pidfd for event created by terminated child",
5959
1,
60-
FAN_NOPIDFD,
60+
1,
6161
0,
6262
},
6363
{
@@ -72,6 +72,8 @@ static int fanotify_fd;
7272
static char event_buf[BUF_SZ];
7373
static struct pidfd_fdinfo_t *self_pidfd_fdinfo;
7474

75+
static int fd_error_unsupported;
76+
7577
static struct pidfd_fdinfo_t *read_pidfd_fdinfo(int pidfd)
7678
{
7779
char *fdinfo_path;
@@ -121,6 +123,15 @@ static void do_fork(void)
121123
static void do_setup(void)
122124
{
123125
int pidfd;
126+
int init_flags = FAN_REPORT_PIDFD;
127+
128+
if (tst_variant) {
129+
fanotify_fd = -1;
130+
fd_error_unsupported = fanotify_init_flags_supported_on_fs(FAN_REPORT_FD_ERROR, ".");
131+
if (fd_error_unsupported)
132+
return;
133+
init_flags |= FAN_REPORT_FD_ERROR;
134+
}
124135

125136
SAFE_TOUCH(TEST_FILE, 0666, NULL);
126137

@@ -132,7 +143,7 @@ static void do_setup(void)
132143
REQUIRE_FANOTIFY_INIT_FLAGS_SUPPORTED_ON_FS(FAN_REPORT_PIDFD,
133144
TEST_FILE);
134145

135-
fanotify_fd = SAFE_FANOTIFY_INIT(FAN_REPORT_PIDFD, O_RDWR);
146+
fanotify_fd = SAFE_FANOTIFY_INIT(init_flags, O_RDWR);
136147
SAFE_FANOTIFY_MARK(fanotify_fd, FAN_MARK_ADD, FAN_OPEN, AT_FDCWD,
137148
TEST_FILE);
138149

@@ -150,8 +161,17 @@ static void do_test(unsigned int num)
150161
{
151162
int i = 0, len;
152163
struct test_case_t *tc = &test_cases[num];
164+
int nopidfd_err = tc->want_pidfd_err ?
165+
(tst_variant ? -ESRCH : FAN_NOPIDFD) : 0;
166+
int fd_err = (tc->remount_ro && tst_variant) ? -EROFS : 0;
153167

154-
tst_res(TINFO, "Test #%d: %s", num, tc->name);
168+
tst_res(TINFO, "Test #%d.%d: %s %s", num, tst_variant, tc->name,
169+
tst_variant ? "(FAN_REPORT_FD_ERROR)" : "");
170+
171+
if (fd_error_unsupported && tst_variant) {
172+
FANOTIFY_INIT_FLAGS_ERR_MSG(FAN_REPORT_FD_ERROR, fd_error_unsupported);
173+
return;
174+
}
155175

156176
if (tc->remount_ro) {
157177
/* SAFE_MOUNT fails to remount FUSE */
@@ -179,12 +199,12 @@ static void do_test(unsigned int num)
179199
*/
180200
len = read(fanotify_fd, event_buf, sizeof(event_buf));
181201
if (len < 0) {
182-
if (tc->remount_ro && errno == EROFS) {
202+
if (tc->remount_ro && !fd_err && errno == EROFS) {
183203
tst_res(TPASS, "cannot read event with rw fd from a ro fs");
184204
return;
185205
}
186206
tst_brk(TBROK | TERRNO, "reading fanotify events failed");
187-
} else if (tc->remount_ro) {
207+
} else if (tc->remount_ro && !fd_err) {
188208
tst_res(TFAIL, "got unexpected event with rw fd from a ro fs");
189209
}
190210
while (i < len) {
@@ -218,6 +238,28 @@ static void do_test(unsigned int num)
218238
goto next_event;
219239
}
220240

241+
/*
242+
* Check if event->fd reported any errors during
243+
* creation and whether they're expected.
244+
*/
245+
if (!fd_err && event->fd >= 0) {
246+
tst_res(TPASS,
247+
"event->fd %d is valid as expected",
248+
event->fd);
249+
} else if (fd_err && event->fd == fd_err) {
250+
tst_res(TPASS,
251+
"event->fd is error %d as expected",
252+
event->fd);
253+
} else if (fd_err) {
254+
tst_res(TFAIL,
255+
"event->fd is %d, but expected error %d",
256+
event->fd, fd_err);
257+
} else {
258+
tst_res(TFAIL,
259+
"event->fd creation failed with error %d",
260+
event->fd);
261+
}
262+
221263
/*
222264
* Check if pidfd information object reported any errors during
223265
* creation and whether they're expected.
@@ -229,20 +271,18 @@ static void do_test(unsigned int num)
229271
(unsigned int)event->pid,
230272
info->pidfd);
231273
goto next_event;
232-
} else if (tc->want_pidfd_err &&
233-
info->pidfd != tc->want_pidfd_err) {
274+
} else if (tc->want_pidfd_err && info->pidfd != nopidfd_err) {
234275
tst_res(TFAIL,
235276
"pidfd set to an unexpected error: %d for pid: %u",
236277
info->pidfd,
237278
(unsigned int)event->pid);
238279
goto next_event;
239-
} else if (tc->want_pidfd_err &&
240-
info->pidfd == tc->want_pidfd_err) {
280+
} else if (tc->want_pidfd_err && info->pidfd == nopidfd_err) {
241281
tst_res(TPASS,
242282
"pid: %u terminated before pidfd was created, "
243283
"pidfd set to the value of: %d, as expected",
244284
(unsigned int)event->pid,
245-
FAN_NOPIDFD);
285+
nopidfd_err);
246286
goto next_event;
247287
}
248288

@@ -323,6 +363,7 @@ static struct tst_test test = {
323363
.setup = do_setup,
324364
.test = do_test,
325365
.tcnt = ARRAY_SIZE(test_cases),
366+
.test_variants = 2,
326367
.cleanup = do_cleanup,
327368
.all_filesystems = 1,
328369
.needs_root = 1,

0 commit comments

Comments
 (0)