fanotify04.c revision ff96476bbaaa4685a94813e370bfa4de47884976
1/* 2 * Copyright (c) 2013 SUSE. All Rights Reserved. 3 * 4 * This program is free software; you can redistribute it and/or modify it 5 * under the terms of version 2 of the GNU General Public License as 6 * published by the Free Software Foundation. 7 * 8 * This program is distributed in the hope that it would be useful, but 9 * WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 11 * 12 * Further, this software is distributed without any warranty that it is 13 * free of the rightful claim of any third person regarding infringement 14 * or the like. Any license provided herein, whether implied or 15 * otherwise, applies only to this software file. Patent licenses, if 16 * any, provided herein do not apply to combinations of this program with 17 * other software, or any other product whatsoever. 18 * 19 * You should have received a copy of the GNU General Public License along 20 * with this program; if not, write the Free Software Foundation, Inc., 21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 22 * 23 * Started by Jan Kara <jack@suse.cz> 24 * 25 * DESCRIPTION 26 * Check various fanotify special flags 27 */ 28#include "config.h" 29 30#include <stdio.h> 31#include <sys/stat.h> 32#include <sys/types.h> 33#include <sys/fcntl.h> 34#include <errno.h> 35#include <string.h> 36#include <sys/syscall.h> 37#include "test.h" 38#include "usctest.h" 39#include "linux_syscall_numbers.h" 40#include "fanotify.h" 41#include "safe_macros.h" 42 43char *TCID = "fanotify04"; 44int TST_TOTAL = 9; 45 46#if defined(HAVE_SYS_FANOTIFY_H) 47#include <sys/fanotify.h> 48 49#define EVENT_MAX 1024 50/* size of the event structure, not counting name */ 51#define EVENT_SIZE (sizeof (struct fanotify_event_metadata)) 52/* reasonable guess as to size of 1024 events */ 53#define EVENT_BUF_LEN (EVENT_MAX * EVENT_SIZE) 54 55static void setup(void); 56static void cleanup(void); 57 58#define BUF_SIZE 256 59static char fname[BUF_SIZE]; 60static char sname[BUF_SIZE]; 61static char dir[BUF_SIZE]; 62static int fd_notify; 63 64static int len; 65static char event_buf[EVENT_BUF_LEN]; 66 67static char *expect_str_fail(int expect) 68{ 69 if (expect == 0) 70 return "failed"; 71 return "unexpectedly succeeded"; 72} 73 74static char *expect_str_pass(int expect) 75{ 76 if (expect == 0) 77 return "succeeded"; 78 return "failed"; 79} 80 81static void check_mark(char *file, unsigned long long flag, char *flagstr, 82 int expect, void (*test_event)(char *)) 83{ 84 if (fanotify_mark(fd_notify, FAN_MARK_ADD | flag, FAN_OPEN, AT_FDCWD, 85 file) != expect) { 86 tst_resm(TFAIL, 87 "fanotify_mark (%d, FAN_MARK_ADD | %s, FAN_OPEN, AT_FDCWD, " 88 "'%s') %s", fd_notify, flagstr, file, expect_str_fail(expect)); 89 } else { 90 tst_resm(TPASS, 91 "fanotify_mark (%d, FAN_MARK_ADD | %s, FAN_OPEN, AT_FDCWD, " 92 "'%s') %s", fd_notify, flagstr, file, expect_str_pass(expect)); 93 94 /* If we expected failure there's nothing to clean up */ 95 if (expect == -1) 96 return; 97 98 if (test_event) 99 test_event(file); 100 101 if (fanotify_mark(fd_notify, FAN_MARK_REMOVE | flag, 102 FAN_OPEN, AT_FDCWD, file) < 0) { 103 tst_brkm(TBROK | TERRNO, cleanup, 104 "fanotify_mark (%d, FAN_MARK_REMOVE | %s, " 105 "FAN_OPEN, AT_FDCWD, '%s') failed", 106 fd_notify, flagstr, file); 107 } 108 } 109} 110 111#define CHECK_MARK(file, flag, expect, func) check_mark(file, flag, #flag, expect, func) 112 113static void do_open(char *file, int flag, char *flagstr) 114{ 115 int fd; 116 117 fd = SAFE_OPEN(cleanup, file, O_RDONLY | flag); 118 SAFE_CLOSE(cleanup, fd); 119} 120 121#define DO_OPEN(file, flag) do_open(file, flag, #flag) 122 123static void open_file(char *file) 124{ 125 DO_OPEN(file, 0); 126} 127 128static void open_dir(char *file) 129{ 130 DO_OPEN(file, O_DIRECTORY); 131} 132 133static void verify_event(int mask) 134{ 135 int ret; 136 struct fanotify_event_metadata *event; 137 struct stat st; 138 139 /* Read the event */ 140 ret = SAFE_READ(cleanup, 0, fd_notify, event_buf + len, 141 EVENT_BUF_LEN - len); 142 event = (struct fanotify_event_metadata *)&event_buf[len]; 143 len += ret; 144 145 if (event->mask != FAN_OPEN) { 146 tst_resm(TFAIL, "got unexpected event %llx", 147 (unsigned long long)event->mask); 148 } else if (fstat(event->fd, &st) < 0) { 149 tst_resm(TFAIL, "failed to stat event->fd (%s)", 150 strerror(errno)); 151 } else if ((st.st_mode & S_IFMT) != mask) { 152 tst_resm(TFAIL, "event->fd points to object of different type " 153 "(%o != %o)", st.st_mode & S_IFMT, mask); 154 } else { 155 tst_resm(TPASS, "event generated properly for type %o", mask); 156 } 157 close(event->fd); 158} 159 160static void do_open_test(char *file, int flag, char *flagstr, int mask) 161{ 162 do_open(file, flag, flagstr); 163 164 verify_event(mask); 165} 166 167#define DO_OPEN_TEST(file, flag, mask) do_open_test(file, flag, #flag, mask) 168 169static void test_open_file(char *file) 170{ 171 DO_OPEN_TEST(file, 0, S_IFREG); 172} 173 174static void verify_no_event(void) 175{ 176 int ret; 177 178 ret = read(fd_notify, event_buf + len, EVENT_BUF_LEN - len); 179 if (ret != -1) { 180 struct fanotify_event_metadata *event; 181 182 event = (struct fanotify_event_metadata *)&event_buf[len]; 183 tst_resm(TFAIL, "seen unexpected event (mask %llx)", 184 (unsigned long long)event->mask); 185 /* Cleanup fd from the event */ 186 close(event->fd); 187 } else if (errno != EAGAIN) { 188 tst_resm(TFAIL | TERRNO, "read(%d, buf, %zu) failed", fd_notify, 189 EVENT_BUF_LEN); 190 } else { 191 tst_resm(TPASS, "No event as expected"); 192 } 193} 194 195static void test_open_symlink(char *file) 196{ 197 /* Since mark is on a symlink, no event should be generated by opening a file */ 198 DO_OPEN(file, 0); 199 verify_no_event(); 200} 201 202int main(int ac, char **av) 203{ 204 int lc; 205 const char *msg; 206 207 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) 208 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); 209 210 setup(); 211 212 for (lc = 0; TEST_LOOPING(lc); lc++) { 213 /* Check ONLYDIR on a directory */ 214 CHECK_MARK(".", FAN_MARK_ONLYDIR, 0, NULL); 215 216 /* Check ONLYDIR without a directory */ 217 CHECK_MARK(fname, FAN_MARK_ONLYDIR, -1, NULL); 218 219 /* Check DONT_FOLLOW for a symlink */ 220 CHECK_MARK(sname, FAN_MARK_DONT_FOLLOW, 0, test_open_symlink); 221 222 /* Check without DONT_FOLLOW for a symlink */ 223 CHECK_MARK(sname, 0, 0, test_open_file); 224 225 /* Verify FAN_MARK_FLUSH destroys all inode marks */ 226 if (fanotify_mark(fd_notify, FAN_MARK_ADD, 227 FAN_OPEN, AT_FDCWD, fname) < 0) { 228 tst_brkm(TBROK | TERRNO, cleanup, 229 "fanotify_mark (%d, FAN_MARK_ADD, FAN_OPEN, " 230 "AT_FDCWD, '%s') failed", fd_notify, fname); 231 } 232 if (fanotify_mark(fd_notify, FAN_MARK_ADD, 233 FAN_OPEN | FAN_ONDIR, AT_FDCWD, dir) < 0) { 234 tst_brkm(TBROK | TERRNO, cleanup, 235 "fanotify_mark (%d, FAN_MARK_ADD, FAN_OPEN | " 236 "FAN_ONDIR, AT_FDCWD, '%s') failed", fd_notify, 237 dir); 238 } 239 open_file(fname); 240 verify_event(S_IFREG); 241 open_dir(dir); 242 verify_event(S_IFDIR); 243 if (fanotify_mark(fd_notify, FAN_MARK_FLUSH, 244 0, AT_FDCWD, ".") < 0) { 245 tst_brkm(TBROK | TERRNO, cleanup, 246 "fanotify_mark (%d, FAN_MARK_FLUSH, 0, " 247 "AT_FDCWD, '.') failed", fd_notify); 248 } 249 250 open_dir(dir); 251 verify_no_event(); 252 } 253 254 cleanup(); 255 tst_exit(); 256} 257 258static void setup(void) 259{ 260 int fd; 261 262 tst_sig(NOFORK, DEF_HANDLER, cleanup); 263 264 TEST_PAUSE; 265 266 tst_tmpdir(); 267 sprintf(fname, "fname_%d", getpid()); 268 fd = SAFE_OPEN(cleanup, fname, O_RDWR | O_CREAT, 0644); 269 SAFE_CLOSE(cleanup, fd); 270 271 sprintf(sname, "symlink_%d", getpid()); 272 SAFE_SYMLINK(cleanup, fname, sname); 273 274 sprintf(dir, "dir_%d", getpid()); 275 SAFE_MKDIR(cleanup, dir, 0755); 276 277 if ((fd_notify = fanotify_init(FAN_CLASS_NOTIF | FAN_NONBLOCK, 278 O_RDONLY)) < 0) { 279 if (errno == ENOSYS) { 280 tst_brkm(TCONF, cleanup, 281 "fanotify is not configured in this kernel."); 282 } else { 283 tst_brkm(TBROK | TERRNO, cleanup, 284 "fanotify_init failed"); 285 } 286 } 287} 288 289static void cleanup(void) 290{ 291 if (close(fd_notify) == -1) 292 tst_resm(TWARN, "close(%d) failed", fd_notify); 293 294 TEST_CLEANUP; 295 tst_rmdir(); 296} 297 298#else 299 300int main(void) 301{ 302 tst_brkm(TCONF, NULL, "system doesn't have required fanotify support"); 303} 304 305#endif 306