Jan Kara | f057c1c | 2013-11-20 23:43:13 +0100 | [diff] [blame] | 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" |
Jan Kara | f057c1c | 2013-11-20 23:43:13 +0100 | [diff] [blame] | 38 | #include "linux_syscall_numbers.h" |
| 39 | #include "fanotify.h" |
| 40 | #include "safe_macros.h" |
| 41 | |
| 42 | char *TCID = "fanotify04"; |
| 43 | int TST_TOTAL = 9; |
| 44 | |
| 45 | #if defined(HAVE_SYS_FANOTIFY_H) |
| 46 | #include <sys/fanotify.h> |
| 47 | |
| 48 | #define EVENT_MAX 1024 |
| 49 | /* size of the event structure, not counting name */ |
| 50 | #define EVENT_SIZE (sizeof (struct fanotify_event_metadata)) |
| 51 | /* reasonable guess as to size of 1024 events */ |
| 52 | #define EVENT_BUF_LEN (EVENT_MAX * EVENT_SIZE) |
| 53 | |
| 54 | static void setup(void); |
| 55 | static void cleanup(void); |
| 56 | |
| 57 | #define BUF_SIZE 256 |
| 58 | static char fname[BUF_SIZE]; |
| 59 | static char sname[BUF_SIZE]; |
| 60 | static char dir[BUF_SIZE]; |
| 61 | static int fd_notify; |
| 62 | |
| 63 | static int len; |
| 64 | static char event_buf[EVENT_BUF_LEN]; |
| 65 | |
| 66 | static char *expect_str_fail(int expect) |
| 67 | { |
| 68 | if (expect == 0) |
| 69 | return "failed"; |
| 70 | return "unexpectedly succeeded"; |
| 71 | } |
| 72 | |
| 73 | static char *expect_str_pass(int expect) |
| 74 | { |
| 75 | if (expect == 0) |
| 76 | return "succeeded"; |
| 77 | return "failed"; |
| 78 | } |
| 79 | |
| 80 | static void check_mark(char *file, unsigned long long flag, char *flagstr, |
| 81 | int expect, void (*test_event)(char *)) |
| 82 | { |
Helge Deller | ff96476 | 2014-07-29 18:10:40 +0200 | [diff] [blame] | 83 | if (fanotify_mark(fd_notify, FAN_MARK_ADD | flag, FAN_OPEN, AT_FDCWD, |
Jan Kara | f057c1c | 2013-11-20 23:43:13 +0100 | [diff] [blame] | 84 | file) != expect) { |
| 85 | tst_resm(TFAIL, |
| 86 | "fanotify_mark (%d, FAN_MARK_ADD | %s, FAN_OPEN, AT_FDCWD, " |
| 87 | "'%s') %s", fd_notify, flagstr, file, expect_str_fail(expect)); |
| 88 | } else { |
| 89 | tst_resm(TPASS, |
| 90 | "fanotify_mark (%d, FAN_MARK_ADD | %s, FAN_OPEN, AT_FDCWD, " |
| 91 | "'%s') %s", fd_notify, flagstr, file, expect_str_pass(expect)); |
| 92 | |
| 93 | /* If we expected failure there's nothing to clean up */ |
| 94 | if (expect == -1) |
| 95 | return; |
| 96 | |
| 97 | if (test_event) |
| 98 | test_event(file); |
| 99 | |
Helge Deller | ff96476 | 2014-07-29 18:10:40 +0200 | [diff] [blame] | 100 | if (fanotify_mark(fd_notify, FAN_MARK_REMOVE | flag, |
Jan Kara | f057c1c | 2013-11-20 23:43:13 +0100 | [diff] [blame] | 101 | FAN_OPEN, AT_FDCWD, file) < 0) { |
| 102 | tst_brkm(TBROK | TERRNO, cleanup, |
| 103 | "fanotify_mark (%d, FAN_MARK_REMOVE | %s, " |
| 104 | "FAN_OPEN, AT_FDCWD, '%s') failed", |
| 105 | fd_notify, flagstr, file); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | #define CHECK_MARK(file, flag, expect, func) check_mark(file, flag, #flag, expect, func) |
| 111 | |
| 112 | static void do_open(char *file, int flag, char *flagstr) |
| 113 | { |
| 114 | int fd; |
| 115 | |
| 116 | fd = SAFE_OPEN(cleanup, file, O_RDONLY | flag); |
| 117 | SAFE_CLOSE(cleanup, fd); |
| 118 | } |
| 119 | |
| 120 | #define DO_OPEN(file, flag) do_open(file, flag, #flag) |
| 121 | |
| 122 | static void open_file(char *file) |
| 123 | { |
| 124 | DO_OPEN(file, 0); |
| 125 | } |
| 126 | |
| 127 | static void open_dir(char *file) |
| 128 | { |
| 129 | DO_OPEN(file, O_DIRECTORY); |
| 130 | } |
| 131 | |
| 132 | static void verify_event(int mask) |
| 133 | { |
| 134 | int ret; |
| 135 | struct fanotify_event_metadata *event; |
| 136 | struct stat st; |
| 137 | |
| 138 | /* Read the event */ |
| 139 | ret = SAFE_READ(cleanup, 0, fd_notify, event_buf + len, |
| 140 | EVENT_BUF_LEN - len); |
| 141 | event = (struct fanotify_event_metadata *)&event_buf[len]; |
| 142 | len += ret; |
| 143 | |
| 144 | if (event->mask != FAN_OPEN) { |
| 145 | tst_resm(TFAIL, "got unexpected event %llx", |
| 146 | (unsigned long long)event->mask); |
| 147 | } else if (fstat(event->fd, &st) < 0) { |
| 148 | tst_resm(TFAIL, "failed to stat event->fd (%s)", |
| 149 | strerror(errno)); |
| 150 | } else if ((st.st_mode & S_IFMT) != mask) { |
| 151 | tst_resm(TFAIL, "event->fd points to object of different type " |
| 152 | "(%o != %o)", st.st_mode & S_IFMT, mask); |
| 153 | } else { |
| 154 | tst_resm(TPASS, "event generated properly for type %o", mask); |
| 155 | } |
| 156 | close(event->fd); |
| 157 | } |
| 158 | |
| 159 | static void do_open_test(char *file, int flag, char *flagstr, int mask) |
| 160 | { |
| 161 | do_open(file, flag, flagstr); |
| 162 | |
| 163 | verify_event(mask); |
| 164 | } |
| 165 | |
| 166 | #define DO_OPEN_TEST(file, flag, mask) do_open_test(file, flag, #flag, mask) |
| 167 | |
| 168 | static void test_open_file(char *file) |
| 169 | { |
| 170 | DO_OPEN_TEST(file, 0, S_IFREG); |
| 171 | } |
| 172 | |
| 173 | static void verify_no_event(void) |
| 174 | { |
| 175 | int ret; |
| 176 | |
| 177 | ret = read(fd_notify, event_buf + len, EVENT_BUF_LEN - len); |
| 178 | if (ret != -1) { |
| 179 | struct fanotify_event_metadata *event; |
| 180 | |
| 181 | event = (struct fanotify_event_metadata *)&event_buf[len]; |
| 182 | tst_resm(TFAIL, "seen unexpected event (mask %llx)", |
| 183 | (unsigned long long)event->mask); |
| 184 | /* Cleanup fd from the event */ |
| 185 | close(event->fd); |
| 186 | } else if (errno != EAGAIN) { |
| 187 | tst_resm(TFAIL | TERRNO, "read(%d, buf, %zu) failed", fd_notify, |
| 188 | EVENT_BUF_LEN); |
| 189 | } else { |
| 190 | tst_resm(TPASS, "No event as expected"); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | static void test_open_symlink(char *file) |
| 195 | { |
| 196 | /* Since mark is on a symlink, no event should be generated by opening a file */ |
| 197 | DO_OPEN(file, 0); |
| 198 | verify_no_event(); |
| 199 | } |
| 200 | |
| 201 | int main(int ac, char **av) |
| 202 | { |
| 203 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 204 | const char *msg; |
Jan Kara | f057c1c | 2013-11-20 23:43:13 +0100 | [diff] [blame] | 205 | |
| 206 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
| 207 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
| 208 | |
| 209 | setup(); |
| 210 | |
| 211 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 212 | /* Check ONLYDIR on a directory */ |
| 213 | CHECK_MARK(".", FAN_MARK_ONLYDIR, 0, NULL); |
| 214 | |
| 215 | /* Check ONLYDIR without a directory */ |
| 216 | CHECK_MARK(fname, FAN_MARK_ONLYDIR, -1, NULL); |
| 217 | |
| 218 | /* Check DONT_FOLLOW for a symlink */ |
| 219 | CHECK_MARK(sname, FAN_MARK_DONT_FOLLOW, 0, test_open_symlink); |
| 220 | |
| 221 | /* Check without DONT_FOLLOW for a symlink */ |
| 222 | CHECK_MARK(sname, 0, 0, test_open_file); |
| 223 | |
| 224 | /* Verify FAN_MARK_FLUSH destroys all inode marks */ |
Helge Deller | ff96476 | 2014-07-29 18:10:40 +0200 | [diff] [blame] | 225 | if (fanotify_mark(fd_notify, FAN_MARK_ADD, |
Jan Kara | f057c1c | 2013-11-20 23:43:13 +0100 | [diff] [blame] | 226 | FAN_OPEN, AT_FDCWD, fname) < 0) { |
| 227 | tst_brkm(TBROK | TERRNO, cleanup, |
| 228 | "fanotify_mark (%d, FAN_MARK_ADD, FAN_OPEN, " |
| 229 | "AT_FDCWD, '%s') failed", fd_notify, fname); |
| 230 | } |
Helge Deller | ff96476 | 2014-07-29 18:10:40 +0200 | [diff] [blame] | 231 | if (fanotify_mark(fd_notify, FAN_MARK_ADD, |
Jan Kara | f057c1c | 2013-11-20 23:43:13 +0100 | [diff] [blame] | 232 | FAN_OPEN | FAN_ONDIR, AT_FDCWD, dir) < 0) { |
| 233 | tst_brkm(TBROK | TERRNO, cleanup, |
| 234 | "fanotify_mark (%d, FAN_MARK_ADD, FAN_OPEN | " |
| 235 | "FAN_ONDIR, AT_FDCWD, '%s') failed", fd_notify, |
| 236 | dir); |
| 237 | } |
| 238 | open_file(fname); |
| 239 | verify_event(S_IFREG); |
| 240 | open_dir(dir); |
| 241 | verify_event(S_IFDIR); |
Helge Deller | ff96476 | 2014-07-29 18:10:40 +0200 | [diff] [blame] | 242 | if (fanotify_mark(fd_notify, FAN_MARK_FLUSH, |
Jan Kara | f057c1c | 2013-11-20 23:43:13 +0100 | [diff] [blame] | 243 | 0, AT_FDCWD, ".") < 0) { |
| 244 | tst_brkm(TBROK | TERRNO, cleanup, |
| 245 | "fanotify_mark (%d, FAN_MARK_FLUSH, 0, " |
| 246 | "AT_FDCWD, '.') failed", fd_notify); |
| 247 | } |
| 248 | |
| 249 | open_dir(dir); |
| 250 | verify_no_event(); |
| 251 | } |
| 252 | |
| 253 | cleanup(); |
| 254 | tst_exit(); |
| 255 | } |
| 256 | |
| 257 | static void setup(void) |
| 258 | { |
| 259 | int fd; |
| 260 | |
| 261 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 262 | |
| 263 | TEST_PAUSE; |
| 264 | |
| 265 | tst_tmpdir(); |
| 266 | sprintf(fname, "fname_%d", getpid()); |
| 267 | fd = SAFE_OPEN(cleanup, fname, O_RDWR | O_CREAT, 0644); |
| 268 | SAFE_CLOSE(cleanup, fd); |
| 269 | |
| 270 | sprintf(sname, "symlink_%d", getpid()); |
| 271 | SAFE_SYMLINK(cleanup, fname, sname); |
| 272 | |
| 273 | sprintf(dir, "dir_%d", getpid()); |
| 274 | SAFE_MKDIR(cleanup, dir, 0755); |
| 275 | |
Helge Deller | ff96476 | 2014-07-29 18:10:40 +0200 | [diff] [blame] | 276 | if ((fd_notify = fanotify_init(FAN_CLASS_NOTIF | FAN_NONBLOCK, |
Jan Kara | f057c1c | 2013-11-20 23:43:13 +0100 | [diff] [blame] | 277 | O_RDONLY)) < 0) { |
| 278 | if (errno == ENOSYS) { |
| 279 | tst_brkm(TCONF, cleanup, |
| 280 | "fanotify is not configured in this kernel."); |
| 281 | } else { |
| 282 | tst_brkm(TBROK | TERRNO, cleanup, |
| 283 | "fanotify_init failed"); |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | static void cleanup(void) |
| 289 | { |
| 290 | if (close(fd_notify) == -1) |
| 291 | tst_resm(TWARN, "close(%d) failed", fd_notify); |
| 292 | |
Jan Kara | f057c1c | 2013-11-20 23:43:13 +0100 | [diff] [blame] | 293 | tst_rmdir(); |
| 294 | } |
| 295 | |
| 296 | #else |
| 297 | |
| 298 | int main(void) |
| 299 | { |
| 300 | tst_brkm(TCONF, NULL, "system doesn't have required fanotify support"); |
| 301 | } |
| 302 | |
| 303 | #endif |