1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2013 SUSE. All Rights Reserved.
4*49cdfc7eSAndroid Build Coastguard Worker *
5*49cdfc7eSAndroid Build Coastguard Worker * Started by Jan Kara <[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 * Check various fanotify special flags.
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 /* size of the event structure, not counting name */
28*49cdfc7eSAndroid Build Coastguard Worker #define EVENT_SIZE (sizeof(struct fanotify_event_metadata))
29*49cdfc7eSAndroid Build Coastguard Worker
30*49cdfc7eSAndroid Build Coastguard Worker #define BUF_SIZE 256
31*49cdfc7eSAndroid Build Coastguard Worker #define TST_TOTAL 9
32*49cdfc7eSAndroid Build Coastguard Worker
33*49cdfc7eSAndroid Build Coastguard Worker static char fname[BUF_SIZE];
34*49cdfc7eSAndroid Build Coastguard Worker static char sname[BUF_SIZE];
35*49cdfc7eSAndroid Build Coastguard Worker static char dir[BUF_SIZE];
36*49cdfc7eSAndroid Build Coastguard Worker static int fd_notify;
37*49cdfc7eSAndroid Build Coastguard Worker static char event_buf[EVENT_SIZE];
38*49cdfc7eSAndroid Build Coastguard Worker
expect_str_fail(int expect)39*49cdfc7eSAndroid Build Coastguard Worker static char *expect_str_fail(int expect)
40*49cdfc7eSAndroid Build Coastguard Worker {
41*49cdfc7eSAndroid Build Coastguard Worker if (expect == 0)
42*49cdfc7eSAndroid Build Coastguard Worker return "failed";
43*49cdfc7eSAndroid Build Coastguard Worker return "unexpectedly succeeded";
44*49cdfc7eSAndroid Build Coastguard Worker }
45*49cdfc7eSAndroid Build Coastguard Worker
expect_str_pass(int expect)46*49cdfc7eSAndroid Build Coastguard Worker static char *expect_str_pass(int expect)
47*49cdfc7eSAndroid Build Coastguard Worker {
48*49cdfc7eSAndroid Build Coastguard Worker if (expect == 0)
49*49cdfc7eSAndroid Build Coastguard Worker return "succeeded";
50*49cdfc7eSAndroid Build Coastguard Worker return "failed";
51*49cdfc7eSAndroid Build Coastguard Worker }
52*49cdfc7eSAndroid Build Coastguard Worker
check_mark(char * file,unsigned long long flag,char * flagstr,int expect,void (* test_event)(char *))53*49cdfc7eSAndroid Build Coastguard Worker static void check_mark(char *file, unsigned long long flag, char *flagstr,
54*49cdfc7eSAndroid Build Coastguard Worker int expect, void (*test_event)(char *))
55*49cdfc7eSAndroid Build Coastguard Worker {
56*49cdfc7eSAndroid Build Coastguard Worker if (fanotify_mark(fd_notify, FAN_MARK_ADD | flag, FAN_OPEN, AT_FDCWD,
57*49cdfc7eSAndroid Build Coastguard Worker file) != expect) {
58*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL,
59*49cdfc7eSAndroid Build Coastguard Worker "fanotify_mark (%d, FAN_MARK_ADD | %s, FAN_OPEN, "
60*49cdfc7eSAndroid Build Coastguard Worker "AT_FDCWD, '%s') %s", fd_notify, flagstr, file,
61*49cdfc7eSAndroid Build Coastguard Worker expect_str_fail(expect));
62*49cdfc7eSAndroid Build Coastguard Worker } else {
63*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS,
64*49cdfc7eSAndroid Build Coastguard Worker "fanotify_mark (%d, FAN_MARK_ADD | %s, FAN_OPEN, "
65*49cdfc7eSAndroid Build Coastguard Worker "AT_FDCWD, '%s') %s", fd_notify, flagstr, file,
66*49cdfc7eSAndroid Build Coastguard Worker expect_str_pass(expect));
67*49cdfc7eSAndroid Build Coastguard Worker
68*49cdfc7eSAndroid Build Coastguard Worker /* If we expected failure there's nothing to clean up */
69*49cdfc7eSAndroid Build Coastguard Worker if (expect == -1)
70*49cdfc7eSAndroid Build Coastguard Worker return;
71*49cdfc7eSAndroid Build Coastguard Worker
72*49cdfc7eSAndroid Build Coastguard Worker if (test_event)
73*49cdfc7eSAndroid Build Coastguard Worker test_event(file);
74*49cdfc7eSAndroid Build Coastguard Worker
75*49cdfc7eSAndroid Build Coastguard Worker SAFE_FANOTIFY_MARK(fd_notify, FAN_MARK_REMOVE | flag,
76*49cdfc7eSAndroid Build Coastguard Worker FAN_OPEN, AT_FDCWD, file);
77*49cdfc7eSAndroid Build Coastguard Worker }
78*49cdfc7eSAndroid Build Coastguard Worker }
79*49cdfc7eSAndroid Build Coastguard Worker
80*49cdfc7eSAndroid Build Coastguard Worker #define CHECK_MARK(file, flag, expect, func) check_mark(file, flag, #flag, expect, func)
81*49cdfc7eSAndroid Build Coastguard Worker
do_open(char * file,int flag)82*49cdfc7eSAndroid Build Coastguard Worker static void do_open(char *file, int flag)
83*49cdfc7eSAndroid Build Coastguard Worker {
84*49cdfc7eSAndroid Build Coastguard Worker int fd;
85*49cdfc7eSAndroid Build Coastguard Worker
86*49cdfc7eSAndroid Build Coastguard Worker fd = SAFE_OPEN(file, O_RDONLY | flag);
87*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd);
88*49cdfc7eSAndroid Build Coastguard Worker }
89*49cdfc7eSAndroid Build Coastguard Worker
open_file(char * file)90*49cdfc7eSAndroid Build Coastguard Worker static void open_file(char *file)
91*49cdfc7eSAndroid Build Coastguard Worker {
92*49cdfc7eSAndroid Build Coastguard Worker do_open(file, 0);
93*49cdfc7eSAndroid Build Coastguard Worker }
94*49cdfc7eSAndroid Build Coastguard Worker
open_dir(char * file)95*49cdfc7eSAndroid Build Coastguard Worker static void open_dir(char *file)
96*49cdfc7eSAndroid Build Coastguard Worker {
97*49cdfc7eSAndroid Build Coastguard Worker do_open(file, O_DIRECTORY);
98*49cdfc7eSAndroid Build Coastguard Worker }
99*49cdfc7eSAndroid Build Coastguard Worker
verify_event(int mask)100*49cdfc7eSAndroid Build Coastguard Worker static void verify_event(int mask)
101*49cdfc7eSAndroid Build Coastguard Worker {
102*49cdfc7eSAndroid Build Coastguard Worker struct fanotify_event_metadata *event;
103*49cdfc7eSAndroid Build Coastguard Worker struct stat st;
104*49cdfc7eSAndroid Build Coastguard Worker
105*49cdfc7eSAndroid Build Coastguard Worker /* Read the event */
106*49cdfc7eSAndroid Build Coastguard Worker SAFE_READ(0, fd_notify, event_buf, EVENT_SIZE);
107*49cdfc7eSAndroid Build Coastguard Worker event = (struct fanotify_event_metadata *)&event_buf;
108*49cdfc7eSAndroid Build Coastguard Worker if (event->mask != FAN_OPEN) {
109*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "got unexpected event %llx",
110*49cdfc7eSAndroid Build Coastguard Worker (unsigned long long)event->mask);
111*49cdfc7eSAndroid Build Coastguard Worker } else if (fstat(event->fd, &st) < 0) {
112*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "failed to stat event->fd (%s)",
113*49cdfc7eSAndroid Build Coastguard Worker strerror(errno));
114*49cdfc7eSAndroid Build Coastguard Worker } else if ((int)(st.st_mode & S_IFMT) != mask) {
115*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "event->fd points to object of different type "
116*49cdfc7eSAndroid Build Coastguard Worker "(%o != %o)", st.st_mode & S_IFMT, mask);
117*49cdfc7eSAndroid Build Coastguard Worker } else {
118*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "event generated properly for type %o", mask);
119*49cdfc7eSAndroid Build Coastguard Worker }
120*49cdfc7eSAndroid Build Coastguard Worker if (event->fd != FAN_NOFD)
121*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(event->fd);
122*49cdfc7eSAndroid Build Coastguard Worker }
123*49cdfc7eSAndroid Build Coastguard Worker
do_open_test(char * file,int flag,int mask)124*49cdfc7eSAndroid Build Coastguard Worker static void do_open_test(char *file, int flag, int mask)
125*49cdfc7eSAndroid Build Coastguard Worker {
126*49cdfc7eSAndroid Build Coastguard Worker do_open(file, flag);
127*49cdfc7eSAndroid Build Coastguard Worker
128*49cdfc7eSAndroid Build Coastguard Worker verify_event(mask);
129*49cdfc7eSAndroid Build Coastguard Worker }
130*49cdfc7eSAndroid Build Coastguard Worker
test_open_file(char * file)131*49cdfc7eSAndroid Build Coastguard Worker static void test_open_file(char *file)
132*49cdfc7eSAndroid Build Coastguard Worker {
133*49cdfc7eSAndroid Build Coastguard Worker do_open_test(file, 0, S_IFREG);
134*49cdfc7eSAndroid Build Coastguard Worker }
135*49cdfc7eSAndroid Build Coastguard Worker
verify_no_event(void)136*49cdfc7eSAndroid Build Coastguard Worker static void verify_no_event(void)
137*49cdfc7eSAndroid Build Coastguard Worker {
138*49cdfc7eSAndroid Build Coastguard Worker int ret;
139*49cdfc7eSAndroid Build Coastguard Worker
140*49cdfc7eSAndroid Build Coastguard Worker ret = read(fd_notify, event_buf, EVENT_SIZE);
141*49cdfc7eSAndroid Build Coastguard Worker if (ret != -1) {
142*49cdfc7eSAndroid Build Coastguard Worker struct fanotify_event_metadata *event;
143*49cdfc7eSAndroid Build Coastguard Worker
144*49cdfc7eSAndroid Build Coastguard Worker event = (struct fanotify_event_metadata *)&event_buf;
145*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "seen unexpected event (mask %llx)",
146*49cdfc7eSAndroid Build Coastguard Worker (unsigned long long)event->mask);
147*49cdfc7eSAndroid Build Coastguard Worker /* Cleanup fd from the event */
148*49cdfc7eSAndroid Build Coastguard Worker if (event->fd != FAN_NOFD)
149*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(event->fd);
150*49cdfc7eSAndroid Build Coastguard Worker } else if (errno != EAGAIN) {
151*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TERRNO, "read(%d, buf, %zu) failed", fd_notify,
152*49cdfc7eSAndroid Build Coastguard Worker EVENT_SIZE);
153*49cdfc7eSAndroid Build Coastguard Worker } else {
154*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "No event as expected");
155*49cdfc7eSAndroid Build Coastguard Worker }
156*49cdfc7eSAndroid Build Coastguard Worker }
157*49cdfc7eSAndroid Build Coastguard Worker
test_open_symlink(char * file)158*49cdfc7eSAndroid Build Coastguard Worker static void test_open_symlink(char *file)
159*49cdfc7eSAndroid Build Coastguard Worker {
160*49cdfc7eSAndroid Build Coastguard Worker /* Since mark is on a symlink, no event should be generated by opening a file */
161*49cdfc7eSAndroid Build Coastguard Worker do_open(file, 0);
162*49cdfc7eSAndroid Build Coastguard Worker verify_no_event();
163*49cdfc7eSAndroid Build Coastguard Worker }
164*49cdfc7eSAndroid Build Coastguard Worker
test01(void)165*49cdfc7eSAndroid Build Coastguard Worker static void test01(void)
166*49cdfc7eSAndroid Build Coastguard Worker {
167*49cdfc7eSAndroid Build Coastguard Worker /* Check ONLYDIR on a directory */
168*49cdfc7eSAndroid Build Coastguard Worker CHECK_MARK(".", FAN_MARK_ONLYDIR, 0, NULL);
169*49cdfc7eSAndroid Build Coastguard Worker
170*49cdfc7eSAndroid Build Coastguard Worker /* Check ONLYDIR without a directory */
171*49cdfc7eSAndroid Build Coastguard Worker CHECK_MARK(fname, FAN_MARK_ONLYDIR, -1, NULL);
172*49cdfc7eSAndroid Build Coastguard Worker
173*49cdfc7eSAndroid Build Coastguard Worker /* Check DONT_FOLLOW for a symlink */
174*49cdfc7eSAndroid Build Coastguard Worker CHECK_MARK(sname, FAN_MARK_DONT_FOLLOW, 0, test_open_symlink);
175*49cdfc7eSAndroid Build Coastguard Worker
176*49cdfc7eSAndroid Build Coastguard Worker /* Check without DONT_FOLLOW for a symlink */
177*49cdfc7eSAndroid Build Coastguard Worker CHECK_MARK(sname, 0, 0, test_open_file);
178*49cdfc7eSAndroid Build Coastguard Worker
179*49cdfc7eSAndroid Build Coastguard Worker /* Verify FAN_MARK_FLUSH destroys all inode marks */
180*49cdfc7eSAndroid Build Coastguard Worker SAFE_FANOTIFY_MARK(fd_notify, FAN_MARK_ADD, FAN_OPEN, AT_FDCWD, fname);
181*49cdfc7eSAndroid Build Coastguard Worker SAFE_FANOTIFY_MARK(fd_notify, FAN_MARK_ADD, FAN_OPEN | FAN_ONDIR,
182*49cdfc7eSAndroid Build Coastguard Worker AT_FDCWD, dir);
183*49cdfc7eSAndroid Build Coastguard Worker open_file(fname);
184*49cdfc7eSAndroid Build Coastguard Worker verify_event(S_IFREG);
185*49cdfc7eSAndroid Build Coastguard Worker open_dir(dir);
186*49cdfc7eSAndroid Build Coastguard Worker verify_event(S_IFDIR);
187*49cdfc7eSAndroid Build Coastguard Worker SAFE_FANOTIFY_MARK(fd_notify, FAN_MARK_FLUSH, 0, AT_FDCWD, ".");
188*49cdfc7eSAndroid Build Coastguard Worker
189*49cdfc7eSAndroid Build Coastguard Worker open_dir(dir);
190*49cdfc7eSAndroid Build Coastguard Worker verify_no_event();
191*49cdfc7eSAndroid Build Coastguard Worker }
192*49cdfc7eSAndroid Build Coastguard Worker
setup(void)193*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
194*49cdfc7eSAndroid Build Coastguard Worker {
195*49cdfc7eSAndroid Build Coastguard Worker int fd;
196*49cdfc7eSAndroid Build Coastguard Worker
197*49cdfc7eSAndroid Build Coastguard Worker sprintf(fname, "fname_%d", getpid());
198*49cdfc7eSAndroid Build Coastguard Worker fd = SAFE_OPEN(fname, O_RDWR | O_CREAT, 0644);
199*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd);
200*49cdfc7eSAndroid Build Coastguard Worker
201*49cdfc7eSAndroid Build Coastguard Worker sprintf(sname, "symlink_%d", getpid());
202*49cdfc7eSAndroid Build Coastguard Worker SAFE_SYMLINK(fname, sname);
203*49cdfc7eSAndroid Build Coastguard Worker
204*49cdfc7eSAndroid Build Coastguard Worker sprintf(dir, "dir_%d", getpid());
205*49cdfc7eSAndroid Build Coastguard Worker SAFE_MKDIR(dir, 0755);
206*49cdfc7eSAndroid Build Coastguard Worker
207*49cdfc7eSAndroid Build Coastguard Worker fd_notify = SAFE_FANOTIFY_INIT(FAN_CLASS_NOTIF | FAN_NONBLOCK,
208*49cdfc7eSAndroid Build Coastguard Worker O_RDONLY);
209*49cdfc7eSAndroid Build Coastguard Worker }
210*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)211*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
212*49cdfc7eSAndroid Build Coastguard Worker {
213*49cdfc7eSAndroid Build Coastguard Worker if (fd_notify > 0)
214*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd_notify);
215*49cdfc7eSAndroid Build Coastguard Worker }
216*49cdfc7eSAndroid Build Coastguard Worker
217*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
218*49cdfc7eSAndroid Build Coastguard Worker .test_all = test01,
219*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
220*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
221*49cdfc7eSAndroid Build Coastguard Worker .needs_tmpdir = 1,
222*49cdfc7eSAndroid Build Coastguard Worker .needs_root = 1
223*49cdfc7eSAndroid Build Coastguard Worker };
224*49cdfc7eSAndroid Build Coastguard Worker
225*49cdfc7eSAndroid Build Coastguard Worker #else
226*49cdfc7eSAndroid Build Coastguard Worker TST_TEST_TCONF("system doesn't have required fanotify support");
227*49cdfc7eSAndroid Build Coastguard Worker #endif
228