1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
/* SPDX-License-Identifier: MIT */
/*
* Description: test fsnotify access off O_DIRECT read
*/
#include "helpers.h"
#ifdef CONFIG_HAVE_FANOTIFY
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/fanotify.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include "liburing.h"
int main(int argc, char *argv[])
{
struct io_uring_sqe *sqe;
struct io_uring_cqe *cqe;
struct io_uring ring;
int fan, ret, fd, err;
char fname[64], *f;
struct stat sb;
void *buf;
fan = fanotify_init(FAN_CLASS_NOTIF|FAN_CLASS_CONTENT, 0);
if (fan < 0) {
if (errno == ENOSYS)
return T_EXIT_SKIP;
if (geteuid())
return T_EXIT_SKIP;
perror("fanotify_init");
return T_EXIT_FAIL;
}
err = T_EXIT_FAIL;
if (argc > 1) {
f = argv[1];
fd = open(argv[1], O_RDONLY | O_DIRECT);
if (fd < 0 && errno == EINVAL)
return T_EXIT_SKIP;
} else {
sprintf(fname, ".fsnotify.%d", getpid());
f = fname;
t_create_file(fname, 8192);
fd = open(fname, O_RDONLY | O_DIRECT);
if (fd < 0 && errno == EINVAL) {
unlink(fname);
return T_EXIT_SKIP;
}
}
if (fd < 0) {
perror("open");
goto out;
}
if (fstat(fd, &sb) < 0) {
perror("fstat");
goto out;
}
if ((sb.st_mode & S_IFMT) != S_IFREG) {
err = T_EXIT_SKIP;
close(fd);
goto out;
}
ret = fanotify_mark(fan, FAN_MARK_ADD, FAN_ACCESS|FAN_MODIFY, fd, NULL);
if (ret < 0) {
perror("fanotify_mark");
goto out;
}
if (fork()) {
int wstat;
io_uring_queue_init(1, &ring, 0);
if (posix_memalign(&buf, 4096, 4096))
goto out;
sqe = io_uring_get_sqe(&ring);
io_uring_prep_read(sqe, fd, buf, 4096, 0);
io_uring_submit(&ring);
ret = io_uring_wait_cqe(&ring, &cqe);
if (ret) {
fprintf(stderr, "wait_ret=%d\n", ret);
goto out;
}
wait(&wstat);
if (!WEXITSTATUS(wstat))
err = T_EXIT_PASS;
free(buf);
} else {
struct fanotify_event_metadata m;
int fret;
fret = read(fan, &m, sizeof(m));
if (fret < 0)
perror("fanotify read");
/* fail if mask isn't right or pid indicates non-task context */
else if (!(m.mask & 1) || !m.pid)
exit(1);
exit(0);
}
out:
if (f == fname)
unlink(fname);
return err;
}
#else /* #ifdef CONFIG_HAVE_FANOTIFY */
int main(void)
{
return T_EXIT_SKIP;
}
#endif /* #ifdef CONFIG_HAVE_FANOTIFY */
|