21
21
#include <ctype.h>
22
22
#include <stdlib.h>
23
23
#include <string.h>
24
+ #include <sys/mount.h>
24
25
#include "tst_test.h"
25
26
#include "tst_safe_stdio.h"
26
27
#include "tst_safe_macros.h"
@@ -45,16 +46,25 @@ static struct test_case_t {
45
46
char * name ;
46
47
int fork ;
47
48
int want_pidfd_err ;
49
+ int remount_ro ;
48
50
} test_cases [] = {
49
51
{
50
52
"return a valid pidfd for event created by self" ,
51
53
0 ,
52
54
0 ,
55
+ 0 ,
53
56
},
54
57
{
55
58
"return invalid pidfd for event created by terminated child" ,
56
59
1 ,
57
60
FAN_NOPIDFD ,
61
+ 0 ,
62
+ },
63
+ {
64
+ "fail to open rw fd for event created on read-only mount" ,
65
+ 0 ,
66
+ 0 ,
67
+ 1 ,
58
68
},
59
69
};
60
70
@@ -122,7 +132,7 @@ static void do_setup(void)
122
132
REQUIRE_FANOTIFY_INIT_FLAGS_SUPPORTED_ON_FS (FAN_REPORT_PIDFD ,
123
133
TEST_FILE );
124
134
125
- fanotify_fd = SAFE_FANOTIFY_INIT (FAN_REPORT_PIDFD , O_RDONLY );
135
+ fanotify_fd = SAFE_FANOTIFY_INIT (FAN_REPORT_PIDFD , O_RDWR );
126
136
SAFE_FANOTIFY_MARK (fanotify_fd , FAN_MARK_ADD , FAN_OPEN , AT_FDCWD ,
127
137
TEST_FILE );
128
138
@@ -143,6 +153,16 @@ static void do_test(unsigned int num)
143
153
144
154
tst_res (TINFO , "Test #%d: %s" , num , tc -> name );
145
155
156
+ if (tc -> remount_ro ) {
157
+ /* SAFE_MOUNT fails to remount FUSE */
158
+ if (mount (tst_device -> dev , MOUNT_PATH , tst_device -> fs_type ,
159
+ MS_REMOUNT |MS_RDONLY , NULL ) != 0 ) {
160
+ tst_brk (TFAIL ,
161
+ "filesystem %s failed to remount readonly" ,
162
+ tst_device -> fs_type );
163
+ }
164
+ }
165
+
146
166
/*
147
167
* Generate the event in either self or a child process. Event
148
168
* generation in a child process is done so that the FAN_NOPIDFD case
@@ -157,7 +177,16 @@ static void do_test(unsigned int num)
157
177
* Read all of the queued events into the provided event
158
178
* buffer.
159
179
*/
160
- len = SAFE_READ (0 , fanotify_fd , event_buf , sizeof (event_buf ));
180
+ len = read (fanotify_fd , event_buf , sizeof (event_buf ));
181
+ if (len < 0 ) {
182
+ if (tc -> remount_ro && errno == EROFS ) {
183
+ tst_res (TPASS , "cannot read event with rw fd from a ro fs" );
184
+ return ;
185
+ }
186
+ tst_brk (TBROK | TERRNO , "reading fanotify events failed" );
187
+ } else if (tc -> remount_ro ) {
188
+ tst_res (TFAIL , "got unexpected event with rw fd from a ro fs" );
189
+ }
161
190
while (i < len ) {
162
191
struct fanotify_event_metadata * event ;
163
192
struct fanotify_event_info_pidfd * info ;
@@ -297,6 +326,7 @@ static struct tst_test test = {
297
326
.cleanup = do_cleanup ,
298
327
.all_filesystems = 1 ,
299
328
.needs_root = 1 ,
329
+ .mount_device = 1 ,
300
330
.mntpoint = MOUNT_PATH ,
301
331
.forks_child = 1 ,
302
332
};
0 commit comments