* [LTP] [PATCH] syscalls/fanotify08: add sanity check for FAN_CLOEXEC @ 2017-06-02 9:24 Xiong Zhou 2017-06-05 14:03 ` Cyril Hrubis 0 siblings, 1 reply; 20+ messages in thread From: Xiong Zhou @ 2017-06-02 9:24 UTC (permalink / raw) To: ltp Signed-off-by: Xiong Zhou <xzhou@redhat.com> --- testcases/kernel/syscalls/fanotify/fanotify08.c | 144 ++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 testcases/kernel/syscalls/fanotify/fanotify08.c diff --git a/testcases/kernel/syscalls/fanotify/fanotify08.c b/testcases/kernel/syscalls/fanotify/fanotify08.c new file mode 100644 index 0000000..1bcef7d --- /dev/null +++ b/testcases/kernel/syscalls/fanotify/fanotify08.c @@ -0,0 +1,144 @@ +/* + * Copyright (c) 2017 RedHat. All Rights Reserved. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it would be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * Further, this software is distributed without any warranty that it is + * free of the rightful claim of any third person regarding infringement + * or the like. Any license provided herein, whether implied or + * otherwise, applies only to this software file. Patent licenses, if + * any, provided herein do not apply to combinations of this program with + * other software, or any other product whatsoever. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Started by Xiong Zhou <xzhou@redhat.com> + * + * DESCRIPTION + * Check fanotify_init flag O_CLOEXEC. + */ +#define _GNU_SOURCE +#include "config.h" + +#include <stdio.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <fcntl.h> +#include <errno.h> +#include <string.h> +#include <sys/syscall.h> +#include "test.h" +#include "linux_syscall_numbers.h" +#include "fanotify.h" +#include "safe_macros.h" + +char *TCID = "fanotify01"; +int TST_TOTAL = 12; + +#if defined(HAVE_SYS_FANOTIFY_H) +#include <sys/fanotify.h> + +#define EVENT_MAX 1024 +/* size of the event structure, not counting name */ +#define EVENT_SIZE (sizeof (struct fanotify_event_metadata)) +/* reasonable guess as to size of 1024 events */ +#define EVENT_BUF_LEN (EVENT_MAX * EVENT_SIZE) + +static void setup(void); +static void cleanup(void); + +#define BUF_SIZE 256 +static int fd_notify; + +int main(int ac, char **av) +{ + int lc; + + tst_parse_opts(ac, av, NULL, NULL); + + setup(); + + for (lc = 0; TEST_LOOPING(lc); lc++) { + int coe; + + tst_count = 0; + + if ((fd_notify = fanotify_init(FAN_CLASS_NOTIF, + O_RDONLY)) < 0) { + if (errno == ENOSYS) { + tst_brkm(TCONF, cleanup, + "fanotify is not configured in this kernel."); + } else { + tst_brkm(TBROK | TERRNO, cleanup, + "fanotify_init failed"); + } + } + + coe = fcntl(fd_notify, F_GETFD); + if (coe == -1) { + tst_brkm(TBROK | TERRNO, cleanup, + "fcntl failed"); + } + if (coe & FD_CLOEXEC) { + tst_brkm(TFAIL, cleanup, + "fanotify_init set close-on-exit"); + } + close(fd_notify); + + if ((fd_notify = fanotify_init(FAN_CLASS_NOTIF|FAN_CLOEXEC, + O_RDONLY)) < 0) { + if (errno == ENOSYS) { + tst_brkm(TCONF, cleanup, + "fanotify is not configured in this kernel."); + } else { + tst_brkm(TBROK | TERRNO, cleanup, + "fanotify_init(FAN_CLOEXEC) failed"); + } + } + + coe = fcntl(fd_notify, F_GETFD); + if (coe == -1) { + tst_brkm(TBROK | TERRNO, cleanup, "fcntl failed"); + } else if ((coe & FD_CLOEXEC) == 0) { + tst_brkm(TFAIL, cleanup, + "fanotify_init set close-on-exit"); + } else { + tst_resm(TPASS, "fanotify_init(FAN_CLOEXEC) PASSED"); + } + close(fd_notify); + } + + cleanup(); + tst_exit(); +} + +static void setup(void) +{ + tst_sig(NOFORK, DEF_HANDLER, cleanup); + + TEST_PAUSE; + + tst_tmpdir(); +} + +static void cleanup(void) +{ + tst_rmdir(); +} + +#else + +int main(void) +{ + tst_brkm(TCONF, NULL, "system doesn't have required fanotify support"); +} + +#endif -- 1.8.3.1 ^ permalink raw reply related [flat|nested] 20+ messages in thread
* [LTP] [PATCH] syscalls/fanotify08: add sanity check for FAN_CLOEXEC 2017-06-02 9:24 [LTP] [PATCH] syscalls/fanotify08: add sanity check for FAN_CLOEXEC Xiong Zhou @ 2017-06-05 14:03 ` Cyril Hrubis 2017-08-11 3:13 ` [LTP] [PATCH v2] " Xiong Zhou 0 siblings, 1 reply; 20+ messages in thread From: Cyril Hrubis @ 2017-06-05 14:03 UTC (permalink / raw) To: ltp Hi! > Signed-off-by: Xiong Zhou <xzhou@redhat.com> First of all, new testcases has to be coded using the new library API, that's the one you get with including the tst_test.h header instead of test.h. > --- > testcases/kernel/syscalls/fanotify/fanotify08.c | 144 ++++++++++++++++++++++++ > 1 file changed, 144 insertions(+) > create mode 100644 testcases/kernel/syscalls/fanotify/fanotify08.c > > diff --git a/testcases/kernel/syscalls/fanotify/fanotify08.c b/testcases/kernel/syscalls/fanotify/fanotify08.c > new file mode 100644 > index 0000000..1bcef7d > --- /dev/null > +++ b/testcases/kernel/syscalls/fanotify/fanotify08.c > @@ -0,0 +1,144 @@ > +/* > + * Copyright (c) 2017 RedHat. All Rights Reserved. > + * > + * This program is free software; you can redistribute it and/or modify it > + * under the terms of version 2 of the GNU General Public License as > + * published by the Free Software Foundation. > + * > + * This program is distributed in the hope that it would be useful, but > + * WITHOUT ANY WARRANTY; without even the implied warranty of > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. > + * > + * Further, this software is distributed without any warranty that it is > + * free of the rightful claim of any third person regarding infringement > + * or the like. Any license provided herein, whether implied or > + * otherwise, applies only to this software file. Patent licenses, if > + * any, provided herein do not apply to combinations of this program with > + * other software, or any other product whatsoever. > + * > + * You should have received a copy of the GNU General Public License along > + * with this program; if not, write the Free Software Foundation, Inc., > + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. > + * > + * Started by Xiong Zhou <xzhou@redhat.com> > + * > + * DESCRIPTION > + * Check fanotify_init flag O_CLOEXEC. > + */ > +#define _GNU_SOURCE > +#include "config.h" > + > +#include <stdio.h> > +#include <sys/stat.h> > +#include <sys/types.h> > +#include <fcntl.h> > +#include <errno.h> > +#include <string.h> > +#include <sys/syscall.h> > +#include "test.h" > +#include "linux_syscall_numbers.h" > +#include "fanotify.h" > +#include "safe_macros.h" > + > +char *TCID = "fanotify01"; > +int TST_TOTAL = 12; > + > +#if defined(HAVE_SYS_FANOTIFY_H) > +#include <sys/fanotify.h> > + > +#define EVENT_MAX 1024 > +/* size of the event structure, not counting name */ > +#define EVENT_SIZE (sizeof (struct fanotify_event_metadata)) > +/* reasonable guess as to size of 1024 events */ > +#define EVENT_BUF_LEN (EVENT_MAX * EVENT_SIZE) These macros are unused. > +static void setup(void); > +static void cleanup(void); > + > +#define BUF_SIZE 256 > +static int fd_notify; > + > +int main(int ac, char **av) > +{ > + int lc; > + > + tst_parse_opts(ac, av, NULL, NULL); > + > + setup(); > + > + for (lc = 0; TEST_LOOPING(lc); lc++) { > + int coe; > + > + tst_count = 0; > + > + if ((fd_notify = fanotify_init(FAN_CLASS_NOTIF, > + O_RDONLY)) < 0) { > + if (errno == ENOSYS) { > + tst_brkm(TCONF, cleanup, > + "fanotify is not configured in this kernel."); > + } else { > + tst_brkm(TBROK | TERRNO, cleanup, > + "fanotify_init failed"); Else branch in a case that the if () branch calls tst_brkm() is useless. The tst_brkm() exit the test so the test will not exit the if () branch once it gets there. > + } > + } > + > + coe = fcntl(fd_notify, F_GETFD); > + if (coe == -1) { > + tst_brkm(TBROK | TERRNO, cleanup, > + "fcntl failed"); > + } Don't we have SAFE_FCNTL()? > + if (coe & FD_CLOEXEC) { > + tst_brkm(TFAIL, cleanup, > + "fanotify_init set close-on-exit"); This should be tst_res() instead. > + } And here should be tst_res() with TPASS. > + close(fd_notify); > + > + if ((fd_notify = fanotify_init(FAN_CLASS_NOTIF|FAN_CLOEXEC, > + O_RDONLY)) < 0) { > + if (errno == ENOSYS) { > + tst_brkm(TCONF, cleanup, > + "fanotify is not configured in this kernel."); > + } else { > + tst_brkm(TBROK | TERRNO, cleanup, > + "fanotify_init(FAN_CLOEXEC) failed"); > + } > + } > + > + coe = fcntl(fd_notify, F_GETFD); > + if (coe == -1) { > + tst_brkm(TBROK | TERRNO, cleanup, "fcntl failed"); > + } else if ((coe & FD_CLOEXEC) == 0) { > + tst_brkm(TFAIL, cleanup, > + "fanotify_init set close-on-exit"); > + } else { > + tst_resm(TPASS, "fanotify_init(FAN_CLOEXEC) PASSED"); > + } > + close(fd_notify); This part is more or less the same, maybe the code should be put into a function that gets either FAN_CLOEXEC or 0 as a parameter. > + } > + > + cleanup(); > + tst_exit(); > +} > + > +static void setup(void) > +{ > + tst_sig(NOFORK, DEF_HANDLER, cleanup); > + > + TEST_PAUSE; > + > + tst_tmpdir(); Do we really need to create temporary directory for this test? > +} > + > +static void cleanup(void) > +{ > + tst_rmdir(); > +} > + > +#else > + > +int main(void) > +{ > + tst_brkm(TCONF, NULL, "system doesn't have required fanotify support"); > +} > + > +#endif > -- > 1.8.3.1 > > > -- > Mailing list info: https://lists.linux.it/listinfo/ltp -- Cyril Hrubis chrubis@suse.cz ^ permalink raw reply [flat|nested] 20+ messages in thread
* [LTP] [PATCH v2] syscalls/fanotify08: add sanity check for FAN_CLOEXEC 2017-06-05 14:03 ` Cyril Hrubis @ 2017-08-11 3:13 ` Xiong Zhou 2017-08-11 8:41 ` Cyril Hrubis 0 siblings, 1 reply; 20+ messages in thread From: Xiong Zhou @ 2017-08-11 3:13 UTC (permalink / raw) To: ltp Signed-off-by: Xiong Zhou <xzhou@redhat.com> --- testcases/kernel/syscalls/fanotify/fanotify08.c | 101 ++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 testcases/kernel/syscalls/fanotify/fanotify08.c diff --git a/testcases/kernel/syscalls/fanotify/fanotify08.c b/testcases/kernel/syscalls/fanotify/fanotify08.c new file mode 100644 index 0000000..aa3a7c0 --- /dev/null +++ b/testcases/kernel/syscalls/fanotify/fanotify08.c @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2017 RedHat. All Rights Reserved. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it would be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * Further, this software is distributed without any warranty that it is + * free of the rightful claim of any third person regarding infringement + * or the like. Any license provided herein, whether implied or + * otherwise, applies only to this software file. Patent licenses, if + * any, provided herein do not apply to combinations of this program with + * other software, or any other product whatsoever. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Started by Xiong Zhou <xzhou@redhat.com> + * + * DESCRIPTION + * Check fanotify_init flag O_CLOEXEC. + */ +#define _GNU_SOURCE +#include "config.h" + +#include <stdio.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <fcntl.h> +#include <errno.h> +#include <string.h> +#include <sys/syscall.h> +#include "tst_test.h" +#include "fanotify.h" + +#if defined(HAVE_SYS_FANOTIFY_H) +#include <sys/fanotify.h> + +static void cleanup(void); +static void test01(int flag); +static int fd_notify; + +void test_coe(void) +{ + test01(FAN_CLOEXEC); + test01(0); +} + +static void test01(int flag) +{ + int coe; + + fd_notify = fanotify_init(FAN_CLASS_NOTIF|flag, O_RDONLY); + + if (fd_notify < 0) { + + if (errno == ENOSYS) { + tst_brk(TCONF, + "fanotify is not configured in this kernel."); + } else { + tst_brk(TBROK | TERRNO, "fanotify_init failed"); + } + } + + coe = SAFE_FCNTL(fd_notify, F_GETFD); + + if (coe & FD_CLOEXEC) { + if (flag == 0) + tst_res(TFAIL, "set close-on-exit"); + else + tst_res(TPASS, "set close-on-exit"); + } else { + if (flag == 0) + tst_res(TPASS, "not set close-on-exit"); + else + tst_res(TFAIL, "not set close-on-exit"); + } + + SAFE_CLOSE(fd_notify); +} + +static void cleanup(void) +{ + if (fd_notify > 0) + SAFE_CLOSE(fd_notify); +} + +static struct tst_test test = { + .test_all = test_coe, + .cleanup = cleanup, + .needs_root = 1, +}; + +#else + TST_TEST_TCONF("system doesn't have required fanotify support"); +#endif -- 1.8.3.1 ^ permalink raw reply related [flat|nested] 20+ messages in thread
* [LTP] [PATCH v2] syscalls/fanotify08: add sanity check for FAN_CLOEXEC 2017-08-11 3:13 ` [LTP] [PATCH v2] " Xiong Zhou @ 2017-08-11 8:41 ` Cyril Hrubis 2017-08-14 14:01 ` [LTP] [PATCH v3 0/3] add SAFE_FANOTIFY_INIT and FAN_CLOEXEC cases Xiong Zhou 0 siblings, 1 reply; 20+ messages in thread From: Cyril Hrubis @ 2017-08-11 8:41 UTC (permalink / raw) To: ltp Hi! > +#define _GNU_SOURCE > +#include "config.h" > + > +#include <stdio.h> > +#include <sys/stat.h> > +#include <sys/types.h> > +#include <fcntl.h> > +#include <errno.h> > +#include <string.h> > +#include <sys/syscall.h> > +#include "tst_test.h" > +#include "fanotify.h" > + > +#if defined(HAVE_SYS_FANOTIFY_H) > +#include <sys/fanotify.h> > + > +static void cleanup(void); This cleanup definition is completely useless here as the test structure is defined at the end of the code. > +static void test01(int flag); > +static int fd_notify; > + > +void test_coe(void) > +{ > + test01(FAN_CLOEXEC); > + test01(0); > +} Can we turn this into two separate testcases? > +static void test01(int flag) > +{ > + int coe; > + > + fd_notify = fanotify_init(FAN_CLASS_NOTIF|flag, O_RDONLY); > + > + if (fd_notify < 0) { > + > + if (errno == ENOSYS) { > + tst_brk(TCONF, > + "fanotify is not configured in this kernel."); > + } else { > + tst_brk(TBROK | TERRNO, "fanotify_init failed"); > + } > + } > + > + coe = SAFE_FCNTL(fd_notify, F_GETFD); > + > + if (coe & FD_CLOEXEC) { > + if (flag == 0) > + tst_res(TFAIL, "set close-on-exit"); > + else > + tst_res(TPASS, "set close-on-exit"); > + } else { > + if (flag == 0) > + tst_res(TPASS, "not set close-on-exit"); > + else > + tst_res(TFAIL, "not set close-on-exit"); > + } I would mildly prefer if we split this test functions into two one for FAN_CLOEXEC and one for case without the flag. All we need is to put the part that checks the fanotify_init() return value into a separate function and ideally we should put it into a header so that it could be reused in all fanotify testcases, but unfortunately we would have to convert the rest of the fanotify tests to the new library first... > + SAFE_CLOSE(fd_notify); > +} > + > +static void cleanup(void) > +{ > + if (fd_notify > 0) > + SAFE_CLOSE(fd_notify); > +} > + > +static struct tst_test test = { > + .test_all = test_coe, > + .cleanup = cleanup, > + .needs_root = 1, > +}; > + > +#else > + TST_TEST_TCONF("system doesn't have required fanotify support"); > +#endif > -- > 1.8.3.1 > -- Cyril Hrubis chrubis@suse.cz ^ permalink raw reply [flat|nested] 20+ messages in thread
* [LTP] [PATCH v3 0/3] add SAFE_FANOTIFY_INIT and FAN_CLOEXEC cases 2017-08-11 8:41 ` Cyril Hrubis @ 2017-08-14 14:01 ` Xiong Zhou 2017-08-14 14:01 ` [LTP] [PATCH v3 1/3] lib: add helper SAFE_FANOTIFY_INIT Xiong Zhou ` (2 more replies) 0 siblings, 3 replies; 20+ messages in thread From: Xiong Zhou @ 2017-08-14 14:01 UTC (permalink / raw) To: ltp v3: Add SAFE_FANOTIFY_INIT helper. Rewrite old cases. Too big a patch, we can also split it. Split FAN_CLOEXEC case to 2 separate cases. Thanks for reviewing! Xiong Zhou (3): lib: add helper SAFE_FANOTIFY_INIT fanotify: rewrite old cases with new lib syscalls/fanotify0[89]: add sanity check for FAN_CLOEXEC include/tst_safe_macros.h | 5 + lib/tst_safe_macros.c | 23 ++ runtest/syscalls | 2 + testcases/kernel/syscalls/fanotify/fanotify01.c | 520 +++++++++++------------- testcases/kernel/syscalls/fanotify/fanotify02.c | 320 +++++++-------- testcases/kernel/syscalls/fanotify/fanotify03.c | 248 +++++------ testcases/kernel/syscalls/fanotify/fanotify04.c | 159 +++----- testcases/kernel/syscalls/fanotify/fanotify05.c | 153 +++---- testcases/kernel/syscalls/fanotify/fanotify06.c | 191 ++++----- testcases/kernel/syscalls/fanotify/fanotify07.c | 9 +- testcases/kernel/syscalls/fanotify/fanotify08.c | 77 ++++ testcases/kernel/syscalls/fanotify/fanotify09.c | 77 ++++ 12 files changed, 891 insertions(+), 893 deletions(-) create mode 100644 testcases/kernel/syscalls/fanotify/fanotify08.c create mode 100644 testcases/kernel/syscalls/fanotify/fanotify09.c -- 1.8.3.1 ^ permalink raw reply [flat|nested] 20+ messages in thread
* [LTP] [PATCH v3 1/3] lib: add helper SAFE_FANOTIFY_INIT 2017-08-14 14:01 ` [LTP] [PATCH v3 0/3] add SAFE_FANOTIFY_INIT and FAN_CLOEXEC cases Xiong Zhou @ 2017-08-14 14:01 ` Xiong Zhou 2017-08-15 13:23 ` Cyril Hrubis 2017-08-14 14:01 ` [LTP] [PATCH v3 2/3] fanotify: rewrite old cases with new lib Xiong Zhou 2017-08-14 14:01 ` [LTP] [PATCH v3 3/3] syscalls/fanotify0[89]: add sanity check for FAN_CLOEXEC Xiong Zhou 2 siblings, 1 reply; 20+ messages in thread From: Xiong Zhou @ 2017-08-14 14:01 UTC (permalink / raw) To: ltp Signed-off-by: Xiong Zhou <xzhou@redhat.com> --- include/tst_safe_macros.h | 5 +++++ lib/tst_safe_macros.c | 23 +++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/include/tst_safe_macros.h b/include/tst_safe_macros.h index 8245b68..f23f765 100644 --- a/include/tst_safe_macros.h +++ b/include/tst_safe_macros.h @@ -438,4 +438,9 @@ int safe_mknod(const char *file, const int lineno, const char *pathname, #define SAFE_MKNOD(pathname, mode, dev) \ safe_mknod(__FILE__, __LINE__, (pathname), (mode), (dev)) +int safe_fanotify_init(const char *file, const int lineno, + unsigned int flags, unsigned int event_f_flags); +#define SAFE_FANOTIFY_INIT(fan, mode) \ + safe_fanotify_init(__FILE__, __LINE__, (fan), (mode)) + #endif /* SAFE_MACROS_H__ */ diff --git a/lib/tst_safe_macros.c b/lib/tst_safe_macros.c index e7f5095..47f87a7 100644 --- a/lib/tst_safe_macros.c +++ b/lib/tst_safe_macros.c @@ -17,6 +17,7 @@ #define _GNU_SOURCE #include <unistd.h> +#include <errno.h> #define TST_NO_DEFAULT_MAIN #include "tst_test.h" #include "tst_safe_macros.h" @@ -47,3 +48,25 @@ pid_t safe_getpgid(const char *file, const int lineno, pid_t pid) return pgid; } + +int safe_fanotify_init(const char *file, const int lineno, + unsigned int flags, unsigned int event_f_flags) +{ + int rval; + + rval = fanotify_init(flags, event_f_flags); + + if (rval == -1) { + + if (errno == ENOSYS) { + tst_brk(TCONF, + "fanotify is not configured in this kernel."); + } else { + tst_brk(TBROK | TERRNO, + "%s:%d: fanotify_init() failed", + file, lineno); + } + } + + return rval; +} -- 1.8.3.1 ^ permalink raw reply related [flat|nested] 20+ messages in thread
* [LTP] [PATCH v3 1/3] lib: add helper SAFE_FANOTIFY_INIT 2017-08-14 14:01 ` [LTP] [PATCH v3 1/3] lib: add helper SAFE_FANOTIFY_INIT Xiong Zhou @ 2017-08-15 13:23 ` Cyril Hrubis 0 siblings, 0 replies; 20+ messages in thread From: Cyril Hrubis @ 2017-08-15 13:23 UTC (permalink / raw) To: ltp Hi! > diff --git a/lib/tst_safe_macros.c b/lib/tst_safe_macros.c > index e7f5095..47f87a7 100644 > --- a/lib/tst_safe_macros.c > +++ b/lib/tst_safe_macros.c > @@ -17,6 +17,7 @@ > > #define _GNU_SOURCE > #include <unistd.h> > +#include <errno.h> > #define TST_NO_DEFAULT_MAIN > #include "tst_test.h" > #include "tst_safe_macros.h" > @@ -47,3 +48,25 @@ pid_t safe_getpgid(const char *file, const int lineno, pid_t pid) > > return pgid; > } > + > +int safe_fanotify_init(const char *file, const int lineno, > + unsigned int flags, unsigned int event_f_flags) > +{ > + int rval; > + > + rval = fanotify_init(flags, event_f_flags); > + > + if (rval == -1) { > + > + if (errno == ENOSYS) { > + tst_brk(TCONF, > + "fanotify is not configured in this kernel."); > + } else { > + tst_brk(TBROK | TERRNO, > + "%s:%d: fanotify_init() failed", > + file, lineno); > + } There is no need for the else branch here, the tst_brk() will exit the test execution. > + } > + > + return rval; > +} > -- > 1.8.3.1 > -- Cyril Hrubis chrubis@suse.cz ^ permalink raw reply [flat|nested] 20+ messages in thread
* [LTP] [PATCH v3 2/3] fanotify: rewrite old cases with new lib 2017-08-14 14:01 ` [LTP] [PATCH v3 0/3] add SAFE_FANOTIFY_INIT and FAN_CLOEXEC cases Xiong Zhou 2017-08-14 14:01 ` [LTP] [PATCH v3 1/3] lib: add helper SAFE_FANOTIFY_INIT Xiong Zhou @ 2017-08-14 14:01 ` Xiong Zhou 2017-08-15 13:33 ` Cyril Hrubis 2017-08-14 14:01 ` [LTP] [PATCH v3 3/3] syscalls/fanotify0[89]: add sanity check for FAN_CLOEXEC Xiong Zhou 2 siblings, 1 reply; 20+ messages in thread From: Xiong Zhou @ 2017-08-14 14:01 UTC (permalink / raw) To: ltp To use SAFE_FANOTIFY_INIT helper. Signed-off-by: Xiong Zhou <xzhou@redhat.com> --- testcases/kernel/syscalls/fanotify/fanotify01.c | 520 +++++++++++------------- testcases/kernel/syscalls/fanotify/fanotify02.c | 320 +++++++-------- testcases/kernel/syscalls/fanotify/fanotify03.c | 248 +++++------ testcases/kernel/syscalls/fanotify/fanotify04.c | 159 +++----- testcases/kernel/syscalls/fanotify/fanotify05.c | 153 +++---- testcases/kernel/syscalls/fanotify/fanotify06.c | 191 ++++----- testcases/kernel/syscalls/fanotify/fanotify07.c | 9 +- 7 files changed, 707 insertions(+), 893 deletions(-) diff --git a/testcases/kernel/syscalls/fanotify/fanotify01.c b/testcases/kernel/syscalls/fanotify/fanotify01.c index 4799d54..bca0f47 100644 --- a/testcases/kernel/syscalls/fanotify/fanotify01.c +++ b/testcases/kernel/syscalls/fanotify/fanotify01.c @@ -35,12 +35,9 @@ #include <errno.h> #include <string.h> #include <sys/syscall.h> -#include "test.h" -#include "lapi/syscalls.h" +#include "tst_test.h" #include "fanotify.h" -#include "safe_macros.h" -char *TCID = "fanotify01"; int TST_TOTAL = 12; #if defined(HAVE_SYS_FANOTIFY_H) @@ -52,9 +49,6 @@ int TST_TOTAL = 12; /* reasonable guess as to size of 1024 events */ #define EVENT_BUF_LEN (EVENT_MAX * EVENT_SIZE) -static void setup(void); -static void cleanup(void); - #define BUF_SIZE 256 static char fname[BUF_SIZE]; static char buf[BUF_SIZE]; @@ -64,308 +58,282 @@ static unsigned long long event_set[EVENT_MAX]; static char event_buf[EVENT_BUF_LEN]; -int main(int ac, char **av) +void test01(void) { - int lc; - - tst_parse_opts(ac, av, NULL, NULL); - - setup(); - - for (lc = 0; TEST_LOOPING(lc); lc++) { - int ret, len, i = 0, test_num = 0; - - tst_count = 0; - - if (fanotify_mark(fd_notify, FAN_MARK_ADD, FAN_ACCESS | FAN_MODIFY | - FAN_CLOSE | FAN_OPEN, AT_FDCWD, fname) < 0) { - tst_brkm(TBROK | TERRNO, cleanup, - "fanotify_mark (%d, FAN_MARK_ADD, FAN_ACCESS | " - "FAN_MODIFY | FAN_CLOSE | FAN_OPEN, AT_FDCWD, %s) " - "failed", fd_notify, fname); - } - - /* - * generate sequence of events - */ - fd = SAFE_OPEN(cleanup, fname, O_RDONLY); - event_set[tst_count] = FAN_OPEN; - tst_count++; - - SAFE_READ(cleanup, 0, fd, buf, BUF_SIZE); - event_set[tst_count] = FAN_ACCESS; - tst_count++; - - SAFE_CLOSE(cleanup, fd); - event_set[tst_count] = FAN_CLOSE_NOWRITE; - tst_count++; - - /* - * Get list of events so far. We get events here to avoid - * merging of following events with the previous ones. - */ - ret = SAFE_READ(cleanup, 0, fd_notify, event_buf, EVENT_BUF_LEN); - len = ret; - - fd = SAFE_OPEN(cleanup, fname, O_RDWR | O_CREAT, 0700); - event_set[tst_count] = FAN_OPEN; - tst_count++; - - SAFE_WRITE(cleanup, 1, fd, fname, strlen(fname)); - event_set[tst_count] = FAN_MODIFY; - tst_count++; - - SAFE_CLOSE(cleanup, fd); - event_set[tst_count] = FAN_CLOSE_WRITE; - tst_count++; + int ret, len, i = 0, test_num = 0; - /* - * get another list of events - */ - ret = SAFE_READ(cleanup, 0, fd_notify, event_buf + len, - EVENT_BUF_LEN - len); - len += ret; - - /* - * Ignore mask testing - */ - - /* Ignore access events */ - if (fanotify_mark(fd_notify, - FAN_MARK_ADD | FAN_MARK_IGNORED_MASK, - FAN_ACCESS, AT_FDCWD, fname) < 0) { - tst_brkm(TBROK | TERRNO, cleanup, - "fanotify_mark (%d, FAN_MARK_ADD | " - "FAN_MARK_IGNORED_MASK, FAN_ACCESS, " - "AT_FDCWD, %s) failed", fd_notify, fname); - } - - fd = SAFE_OPEN(cleanup, fname, O_RDWR); - event_set[tst_count] = FAN_OPEN; - tst_count++; - - /* This event should be ignored */ - SAFE_READ(cleanup, 0, fd, buf, BUF_SIZE); + int tst_count = 0; - /* - * get another list of events to verify the last one got ignored - */ - ret = SAFE_READ(cleanup, 0, fd_notify, event_buf + len, - EVENT_BUF_LEN - len); - len += ret; - - lseek(fd, 0, SEEK_SET); - /* Generate modify event to clear ignore mask */ - SAFE_WRITE(cleanup, 1, fd, fname, 1); - event_set[tst_count] = FAN_MODIFY; - tst_count++; - - /* - * This event shouldn't be ignored because previous modification - * should have removed the ignore mask - */ - SAFE_READ(cleanup, 0, fd, buf, BUF_SIZE); - event_set[tst_count] = FAN_ACCESS; - tst_count++; - - SAFE_CLOSE(cleanup, fd); - event_set[tst_count] = FAN_CLOSE_WRITE; - tst_count++; - - /* Read events to verify previous access was properly generated */ - ret = SAFE_READ(cleanup, 0, fd_notify, event_buf + len, - EVENT_BUF_LEN - len); - len += ret; + if (fanotify_mark(fd_notify, FAN_MARK_ADD, FAN_ACCESS | FAN_MODIFY | + FAN_CLOSE | FAN_OPEN, AT_FDCWD, fname) < 0) { + tst_brk(TBROK | TERRNO, + "fanotify_mark (%d, FAN_MARK_ADD, FAN_ACCESS | " + "FAN_MODIFY | FAN_CLOSE | FAN_OPEN, AT_FDCWD, %s) " + "failed", fd_notify, fname); + } - /* - * Now ignore open & close events regardless of file - * modifications - */ - if (fanotify_mark(fd_notify, - FAN_MARK_ADD | FAN_MARK_IGNORED_MASK | FAN_MARK_IGNORED_SURV_MODIFY, - FAN_OPEN | FAN_CLOSE, AT_FDCWD, fname) < 0) { - tst_brkm(TBROK | TERRNO, cleanup, - "fanotify_mark (%d, FAN_MARK_ADD | " - "FAN_MARK_IGNORED_MASK | " - "FAN_MARK_IGNORED_SURV_MODIFY, FAN_OPEN | " - "FAN_CLOSE, AT_FDCWD, %s) failed", fd_notify, - fname); - } + /* + * generate sequence of events + */ + fd = SAFE_OPEN(fname, O_RDONLY); + event_set[tst_count] = FAN_OPEN; + tst_count++; + + SAFE_READ(0, fd, buf, BUF_SIZE); + event_set[tst_count] = FAN_ACCESS; + tst_count++; + + SAFE_CLOSE(fd); + event_set[tst_count] = FAN_CLOSE_NOWRITE; + tst_count++; + + /* + * Get list of events so far. We get events here to avoid + * merging of following events with the previous ones. + */ + ret = SAFE_READ(0, fd_notify, event_buf, EVENT_BUF_LEN); + len = ret; + + fd = SAFE_OPEN(fname, O_RDWR | O_CREAT, 0700); + event_set[tst_count] = FAN_OPEN; + tst_count++; + + SAFE_WRITE(1, fd, fname, strlen(fname)); + event_set[tst_count] = FAN_MODIFY; + tst_count++; + + SAFE_CLOSE(fd); + event_set[tst_count] = FAN_CLOSE_WRITE; + tst_count++; + + /* + * get another list of events + */ + ret = SAFE_READ(0, fd_notify, event_buf + len, + EVENT_BUF_LEN - len); + len += ret; + + /* + * Ignore mask testing + */ + + /* Ignore access events */ + if (fanotify_mark(fd_notify, + FAN_MARK_ADD | FAN_MARK_IGNORED_MASK, + FAN_ACCESS, AT_FDCWD, fname) < 0) { + tst_brk(TBROK | TERRNO, + "fanotify_mark (%d, FAN_MARK_ADD | " + "FAN_MARK_IGNORED_MASK, FAN_ACCESS, " + "AT_FDCWD, %s) failed", fd_notify, fname); + } - /* This event should be ignored */ - fd = SAFE_OPEN(cleanup, fname, O_RDWR); - - SAFE_WRITE(cleanup, 1, fd, fname, 1); - event_set[tst_count] = FAN_MODIFY; - tst_count++; - - /* This event should be still ignored */ - SAFE_CLOSE(cleanup, fd); - - /* This event should still be ignored */ - fd = SAFE_OPEN(cleanup, fname, O_RDWR); - - /* Read events to verify open & close were ignored */ - ret = SAFE_READ(cleanup, 0, fd_notify, event_buf + len, - EVENT_BUF_LEN - len); - len += ret; - - /* Now remove open and close from ignored mask */ - if (fanotify_mark(fd_notify, - FAN_MARK_REMOVE | FAN_MARK_IGNORED_MASK, - FAN_OPEN | FAN_CLOSE, AT_FDCWD, fname) < 0) { - tst_brkm(TBROK | TERRNO, cleanup, - "fanotify_mark (%d, FAN_MARK_REMOVE | " - "FAN_MARK_IGNORED_MASK, FAN_OPEN | " - "FAN_CLOSE, AT_FDCWD, %s) failed", fd_notify, - fname); - } + fd = SAFE_OPEN(fname, O_RDWR); + event_set[tst_count] = FAN_OPEN; + tst_count++; + + /* This event should be ignored */ + SAFE_READ(0, fd, buf, BUF_SIZE); + + /* + * get another list of events to verify the last one got ignored + */ + ret = SAFE_READ(0, fd_notify, event_buf + len, + EVENT_BUF_LEN - len); + len += ret; + + lseek(fd, 0, SEEK_SET); + /* Generate modify event to clear ignore mask */ + SAFE_WRITE(1, fd, fname, 1); + event_set[tst_count] = FAN_MODIFY; + tst_count++; + + /* + * This event shouldn't be ignored because previous modification + * should have removed the ignore mask + */ + SAFE_READ(0, fd, buf, BUF_SIZE); + event_set[tst_count] = FAN_ACCESS; + tst_count++; + + SAFE_CLOSE(fd); + event_set[tst_count] = FAN_CLOSE_WRITE; + tst_count++; + + /* Read events to verify previous access was properly generated */ + ret = SAFE_READ(0, fd_notify, event_buf + len, + EVENT_BUF_LEN - len); + len += ret; + + /* + * Now ignore open & close events regardless of file + * modifications + */ + if (fanotify_mark(fd_notify, + FAN_MARK_ADD | FAN_MARK_IGNORED_MASK | FAN_MARK_IGNORED_SURV_MODIFY, + FAN_OPEN | FAN_CLOSE, AT_FDCWD, fname) < 0) { + tst_brk(TBROK | TERRNO, + "fanotify_mark (%d, FAN_MARK_ADD | " + "FAN_MARK_IGNORED_MASK | " + "FAN_MARK_IGNORED_SURV_MODIFY, FAN_OPEN | " + "FAN_CLOSE, AT_FDCWD, %s) failed", fd_notify, + fname); + } - SAFE_CLOSE(cleanup, fd); - event_set[tst_count] = FAN_CLOSE_WRITE; - tst_count++; + /* This event should be ignored */ + fd = SAFE_OPEN(fname, O_RDWR); + + SAFE_WRITE(1, fd, fname, 1); + event_set[tst_count] = FAN_MODIFY; + tst_count++; + + /* This event should be still ignored */ + SAFE_CLOSE(fd); + + /* This event should still be ignored */ + fd = SAFE_OPEN(fname, O_RDWR); + + /* Read events to verify open & close were ignored */ + ret = SAFE_READ(0, fd_notify, event_buf + len, + EVENT_BUF_LEN - len); + len += ret; + + /* Now remove open and close from ignored mask */ + if (fanotify_mark(fd_notify, + FAN_MARK_REMOVE | FAN_MARK_IGNORED_MASK, + FAN_OPEN | FAN_CLOSE, AT_FDCWD, fname) < 0) { + tst_brk(TBROK | TERRNO, + "fanotify_mark (%d, FAN_MARK_REMOVE | " + "FAN_MARK_IGNORED_MASK, FAN_OPEN | " + "FAN_CLOSE, AT_FDCWD, %s) failed", fd_notify, + fname); + } - /* Read events to verify close was generated */ - ret = SAFE_READ(cleanup, 0, fd_notify, event_buf + len, - EVENT_BUF_LEN - len); - len += ret; + SAFE_CLOSE(fd); + event_set[tst_count] = FAN_CLOSE_WRITE; + tst_count++; - if (TST_TOTAL != tst_count) { - tst_brkm(TBROK, cleanup, - "TST_TOTAL (%d) and tst_count (%d) are not " - "equal", TST_TOTAL, tst_count); - } - tst_count = 0; + /* Read events to verify close was generated */ + ret = SAFE_READ(0, fd_notify, event_buf + len, + EVENT_BUF_LEN - len); + len += ret; - /* - * check events - */ - while (i < len) { - struct fanotify_event_metadata *event; - - event = (struct fanotify_event_metadata *)&event_buf[i]; - if (test_num >= TST_TOTAL) { - tst_resm(TFAIL, - "get unnecessary event: mask=%llx " - "pid=%u fd=%u", - (unsigned long long)event->mask, - (unsigned)event->pid, event->fd); - } else if (!(event->mask & event_set[test_num])) { - tst_resm(TFAIL, - "get event: mask=%llx (expected %llx) " - "pid=%u fd=%u", + if (TST_TOTAL != tst_count) { + tst_brk(TBROK, + "TST_TOTAL (%d) and tst_count (%d) are not " + "equal", TST_TOTAL, tst_count); + } + tst_count = 0; + + /* + * check events + */ + while (i < len) { + struct fanotify_event_metadata *event; + + event = (struct fanotify_event_metadata *)&event_buf[i]; + if (test_num >= TST_TOTAL) { + tst_res(TFAIL, + "get unnecessary event: mask=%llx " + "pid=%u fd=%u", + (unsigned long long)event->mask, + (unsigned)event->pid, event->fd); + } else if (!(event->mask & event_set[test_num])) { + tst_res(TFAIL, + "get event: mask=%llx (expected %llx) " + "pid=%u fd=%u", + (unsigned long long)event->mask, + event_set[test_num], + (unsigned)event->pid, event->fd); + } else if (event->pid != getpid()) { + tst_res(TFAIL, + "get event: mask=%llx pid=%u " + "(expected %u) fd=%u", + (unsigned long long)event->mask, + (unsigned)event->pid, + (unsigned)getpid(), + event->fd); + } else { + if (event->fd == -2) + goto pass; + ret = read(event->fd, buf, BUF_SIZE); + if (ret != strlen(fname)) { + tst_res(TFAIL, + "cannot read from returned fd " + "of event: mask=%llx pid=%u " + "fd=%u ret=%d (errno=%d)", (unsigned long long)event->mask, - event_set[test_num], - (unsigned)event->pid, event->fd); - } else if (event->pid != getpid()) { - tst_resm(TFAIL, - "get event: mask=%llx pid=%u " - "(expected %u) fd=%u", + (unsigned)event->pid, + event->fd, ret, errno); + } else if (memcmp(buf, fname, strlen(fname))) { + tst_res(TFAIL, + "wrong data read from returned fd " + "of event: mask=%llx pid=%u " + "fd=%u", (unsigned long long)event->mask, (unsigned)event->pid, - (unsigned)getpid(), event->fd); } else { - if (event->fd == -2) - goto pass; - ret = read(event->fd, buf, BUF_SIZE); - if (ret != strlen(fname)) { - tst_resm(TFAIL, - "cannot read from returned fd " - "of event: mask=%llx pid=%u " - "fd=%u ret=%d (errno=%d)", - (unsigned long long)event->mask, - (unsigned)event->pid, - event->fd, ret, errno); - } else if (memcmp(buf, fname, strlen(fname))) { - tst_resm(TFAIL, - "wrong data read from returned fd " - "of event: mask=%llx pid=%u " - "fd=%u", - (unsigned long long)event->mask, - (unsigned)event->pid, - event->fd); - } else { pass: - tst_resm(TPASS, - "get event: mask=%llx pid=%u fd=%u", - (unsigned long long)event->mask, - (unsigned)event->pid, event->fd); - } + tst_res(TPASS, + "get event: mask=%llx pid=%u fd=%u", + (unsigned long long)event->mask, + (unsigned)event->pid, event->fd); } - /* - * We have verified the data now so close fd and - * invalidate it so that we don't check it again - * unnecessarily - */ - close(event->fd); - event->fd = -2; - event->mask &= ~event_set[test_num]; - /* No events left in current mask? Go for next event */ - if (event->mask == 0) { - i += event->event_len; - } - test_num++; - } - for (; test_num < TST_TOTAL; test_num++) { - tst_resm(TFAIL, "didn't get event: mask=%llx", - event_set[test_num]); - } - /* Remove mark to clear FAN_MARK_IGNORED_SURV_MODIFY */ - if (fanotify_mark(fd_notify, FAN_MARK_REMOVE, FAN_ACCESS | FAN_MODIFY | - FAN_CLOSE | FAN_OPEN, AT_FDCWD, fname) < 0) { - tst_brkm(TBROK | TERRNO, cleanup, - "fanotify_mark (%d, FAN_MARK_REMOVE, FAN_ACCESS | " - "FAN_MODIFY | FAN_CLOSE | FAN_OPEN, AT_FDCWD, %s) " - "failed", fd_notify, fname); + /* + * We have verified the data now so close fd and + * invalidate it so that we don't check it again + * unnecessarily + */ + close(event->fd); + event->fd = -2; + event->mask &= ~event_set[test_num]; + /* No events left in current mask? Go for next event */ + if (event->mask == 0) { + i += event->event_len; } - + test_num++; } + for (; test_num < TST_TOTAL; test_num++) { + tst_res(TFAIL, "didn't get event: mask=%llx", + event_set[test_num]); - cleanup(); - tst_exit(); + } + /* Remove mark to clear FAN_MARK_IGNORED_SURV_MODIFY */ + if (fanotify_mark(fd_notify, FAN_MARK_REMOVE, FAN_ACCESS | FAN_MODIFY | + FAN_CLOSE | FAN_OPEN, AT_FDCWD, fname) < 0) { + tst_brk(TBROK | TERRNO, + "fanotify_mark (%d, FAN_MARK_REMOVE, FAN_ACCESS | " + "FAN_MODIFY | FAN_CLOSE | FAN_OPEN, AT_FDCWD, %s) " + "failed", fd_notify, fname); + } } static void setup(void) { - tst_sig(NOFORK, DEF_HANDLER, cleanup); - - TEST_PAUSE; - - tst_tmpdir(); - sprintf(fname, "tfile_%d", getpid()); - fd = SAFE_OPEN(cleanup, fname, O_RDWR | O_CREAT, 0700); - SAFE_WRITE(cleanup, 1, fd, fname, 1); - + fd = SAFE_OPEN(fname, O_RDWR | O_CREAT, 0700); + SAFE_WRITE(1, fd, fname, 1); /* close the file we have open */ - SAFE_CLOSE(cleanup, fd); + SAFE_CLOSE(fd); - if ((fd_notify = fanotify_init(FAN_CLASS_NOTIF, O_RDONLY)) < 0) { - if (errno == ENOSYS) { - tst_brkm(TCONF, cleanup, - "fanotify is not configured in this kernel."); - } else { - tst_brkm(TBROK | TERRNO, cleanup, - "fanotify_init failed"); - } - } + fd_notify = SAFE_FANOTIFY_INIT(FAN_CLASS_NOTIF, O_RDONLY); } static void cleanup(void) { if (fd_notify > 0 && close(fd_notify)) - tst_resm(TWARN | TERRNO, "close(%d) failed", fd_notify); - - tst_rmdir(); + tst_res(TWARN | TERRNO, "close(%d) failed", fd_notify); } -#else - -int main(void) -{ - tst_brkm(TCONF, NULL, "system doesn't have required fanotify support"); -} +static struct tst_test test = { + .test_all = test01, + .setup = setup, + .cleanup = cleanup, + .needs_tmpdir = 1, + .needs_root = 1 +}; +#else + TST_TEST_TCONF("system doesn't have required fanotify support"); #endif diff --git a/testcases/kernel/syscalls/fanotify/fanotify02.c b/testcases/kernel/syscalls/fanotify/fanotify02.c index 29dbe4f..6cb7097 100644 --- a/testcases/kernel/syscalls/fanotify/fanotify02.c +++ b/testcases/kernel/syscalls/fanotify/fanotify02.c @@ -35,12 +35,9 @@ #include <errno.h> #include <string.h> #include <sys/syscall.h> -#include "test.h" -#include "lapi/syscalls.h" +#include "tst_test.h" #include "fanotify.h" -#include "safe_macros.h" -char *TCID = "fanotify02"; int TST_TOTAL = 8; #if defined(HAVE_SYS_FANOTIFY_H) @@ -52,9 +49,6 @@ int TST_TOTAL = 8; /* reasonable guess as to size of 1024 events */ #define EVENT_BUF_LEN (EVENT_MAX * EVENT_SIZE) -static void setup(void); -static void cleanup(void); - #define BUF_SIZE 256 static char fname[BUF_SIZE]; static char buf[BUF_SIZE]; @@ -64,197 +58,173 @@ static unsigned long long event_set[EVENT_MAX]; static char event_buf[EVENT_BUF_LEN]; -int main(int ac, char **av) +void test01(void) { - int lc; - - tst_parse_opts(ac, av, NULL, NULL); - - setup(); - - for (lc = 0; TEST_LOOPING(lc); lc++) { - int ret, len, i = 0, test_num = 0; - - tst_count = 0; - - if (fanotify_mark(fd_notify, FAN_MARK_ADD, FAN_ACCESS | - FAN_MODIFY | FAN_CLOSE | FAN_OPEN | - FAN_EVENT_ON_CHILD | FAN_ONDIR, AT_FDCWD, - ".") < 0) { - tst_brkm(TBROK | TERRNO, cleanup, - "fanotify_mark (%d, FAN_MARK_ADD, FAN_ACCESS | " - "FAN_MODIFY | FAN_CLOSE | FAN_OPEN | " - "FAN_EVENT_ON_CHILD | FAN_ONDIR, AT_FDCWD, '.') " - "failed", fd_notify); - } + int ret, len, i = 0, test_num = 0; + + int tst_count = 0; + + if (fanotify_mark(fd_notify, FAN_MARK_ADD, FAN_ACCESS | + FAN_MODIFY | FAN_CLOSE | FAN_OPEN | + FAN_EVENT_ON_CHILD | FAN_ONDIR, AT_FDCWD, + ".") < 0) { + tst_brk(TBROK | TERRNO, + "fanotify_mark (%d, FAN_MARK_ADD, FAN_ACCESS | " + "FAN_MODIFY | FAN_CLOSE | FAN_OPEN | " + "FAN_EVENT_ON_CHILD | FAN_ONDIR, AT_FDCWD, '.') " + "failed", fd_notify); + } - /* - * generate sequence of events - */ - fd = SAFE_OPEN(cleanup, fname, O_RDWR | O_CREAT, 0700); - event_set[tst_count] = FAN_OPEN; - tst_count++; - - SAFE_WRITE(cleanup, 1, fd, fname, strlen(fname)); - event_set[tst_count] = FAN_MODIFY; - tst_count++; - - SAFE_CLOSE(cleanup, fd); - event_set[tst_count] = FAN_CLOSE_WRITE; - tst_count++; - - /* - * Get list of events so far. We get events here to avoid - * merging of following events with the previous ones. - */ - ret = SAFE_READ(cleanup, 0, fd_notify, event_buf, - EVENT_BUF_LEN); - len = ret; - - fd = SAFE_OPEN(cleanup, fname, O_RDONLY); - event_set[tst_count] = FAN_OPEN; - tst_count++; - - SAFE_READ(cleanup, 0, fd, buf, BUF_SIZE); - event_set[tst_count] = FAN_ACCESS; - tst_count++; - - SAFE_CLOSE(cleanup, fd); - event_set[tst_count] = FAN_CLOSE_NOWRITE; - tst_count++; - - /* - * get next events - */ - ret = SAFE_READ(cleanup, 0, fd_notify, event_buf + len, - EVENT_BUF_LEN - len); - len += ret; - - /* - * now remove child mark - */ - if (fanotify_mark(fd_notify, FAN_MARK_REMOVE, - FAN_EVENT_ON_CHILD, AT_FDCWD, ".") < 0) { - tst_brkm(TBROK | TERRNO, cleanup, - "fanotify_mark (%d, FAN_MARK REMOVE, " - "FAN_EVENT_ON_CHILD, AT_FDCWD, '.') failed", - fd_notify); - } + /* + * generate sequence of events + */ + fd = SAFE_OPEN(fname, O_RDWR | O_CREAT, 0700); + event_set[tst_count] = FAN_OPEN; + tst_count++; + + SAFE_WRITE(1, fd, fname, strlen(fname)); + event_set[tst_count] = FAN_MODIFY; + tst_count++; + + SAFE_CLOSE(fd); + event_set[tst_count] = FAN_CLOSE_WRITE; + tst_count++; + + /* + * Get list of events so far. We get events here to avoid + * merging of following events with the previous ones. + */ + ret = SAFE_READ(0, fd_notify, event_buf, + EVENT_BUF_LEN); + len = ret; + + fd = SAFE_OPEN(fname, O_RDONLY); + event_set[tst_count] = FAN_OPEN; + tst_count++; + + SAFE_READ(0, fd, buf, BUF_SIZE); + event_set[tst_count] = FAN_ACCESS; + tst_count++; + + SAFE_CLOSE(fd); + event_set[tst_count] = FAN_CLOSE_NOWRITE; + tst_count++; + + /* + * get next events + */ + ret = SAFE_READ(0, fd_notify, event_buf + len, + EVENT_BUF_LEN - len); + len += ret; + + /* + * now remove child mark + */ + if (fanotify_mark(fd_notify, FAN_MARK_REMOVE, + FAN_EVENT_ON_CHILD, AT_FDCWD, ".") < 0) { + tst_brk(TBROK | TERRNO, + "fanotify_mark (%d, FAN_MARK REMOVE, " + "FAN_EVENT_ON_CHILD, AT_FDCWD, '.') failed", + fd_notify); + } - /* - * Do something to verify events didn't get generated - */ - fd = SAFE_OPEN(cleanup, fname, O_RDONLY); + /* + * Do something to verify events didn't get generated + */ + fd = SAFE_OPEN(fname, O_RDONLY); - SAFE_CLOSE(cleanup, fd); + SAFE_CLOSE(fd); - fd = SAFE_OPEN(cleanup, ".", O_RDONLY | O_DIRECTORY); - event_set[tst_count] = FAN_OPEN; - tst_count++; + fd = SAFE_OPEN(".", O_RDONLY | O_DIRECTORY); + event_set[tst_count] = FAN_OPEN; + tst_count++; - SAFE_CLOSE(cleanup, fd); - event_set[tst_count] = FAN_CLOSE_NOWRITE; - tst_count++; + SAFE_CLOSE(fd); + event_set[tst_count] = FAN_CLOSE_NOWRITE; + tst_count++; - /* - * Check events got generated only for the directory - */ - ret = SAFE_READ(cleanup, 0, fd_notify, event_buf + len, - EVENT_BUF_LEN - len); - len += ret; + /* + * Check events got generated only for the directory + */ + ret = SAFE_READ(0, fd_notify, event_buf + len, + EVENT_BUF_LEN - len); + len += ret; - if (TST_TOTAL != tst_count) { - tst_brkm(TBROK, cleanup, - "TST_TOTAL and tst_count are not equal"); - } - tst_count = 0; - - /* - * check events - */ - while (i < len) { - struct fanotify_event_metadata *event; - - event = (struct fanotify_event_metadata *)&event_buf[i]; - if (test_num >= TST_TOTAL) { - tst_resm(TFAIL, - "get unnecessary event: mask=%llx " - "pid=%u fd=%u", - (unsigned long long)event->mask, - (unsigned)event->pid, event->fd); - } else if (!(event->mask & event_set[test_num])) { - tst_resm(TFAIL, - "get event: mask=%llx (expected %llx) " - "pid=%u fd=%u", - (unsigned long long)event->mask, - event_set[test_num], - (unsigned)event->pid, event->fd); - } else if (event->pid != getpid()) { - tst_resm(TFAIL, - "get event: mask=%llx pid=%u " - "(expected %u) fd=%u", - (unsigned long long)event->mask, - (unsigned)event->pid, - (unsigned)getpid(), - event->fd); - } else { - tst_resm(TPASS, - "get event: mask=%llx pid=%u fd=%u", - (unsigned long long)event->mask, - (unsigned)event->pid, event->fd); - } - event->mask &= ~event_set[test_num]; - /* No events left in current mask? Go for next event */ - if (event->mask == 0) { - i += event->event_len; - close(event->fd); - } - test_num++; + if (TST_TOTAL != tst_count) { + tst_brk(TBROK, + "TST_TOTAL and tst_count are not equal"); + } + tst_count = 0; + + /* + * check events + */ + while (i < len) { + struct fanotify_event_metadata *event; + + event = (struct fanotify_event_metadata *)&event_buf[i]; + if (test_num >= TST_TOTAL) { + tst_res(TFAIL, + "get unnecessary event: mask=%llx " + "pid=%u fd=%u", + (unsigned long long)event->mask, + (unsigned)event->pid, event->fd); + } else if (!(event->mask & event_set[test_num])) { + tst_res(TFAIL, + "get event: mask=%llx (expected %llx) " + "pid=%u fd=%u", + (unsigned long long)event->mask, + event_set[test_num], + (unsigned)event->pid, event->fd); + } else if (event->pid != getpid()) { + tst_res(TFAIL, + "get event: mask=%llx pid=%u " + "(expected %u) fd=%u", + (unsigned long long)event->mask, + (unsigned)event->pid, + (unsigned)getpid(), + event->fd); + } else { + tst_res(TPASS, + "get event: mask=%llx pid=%u fd=%u", + (unsigned long long)event->mask, + (unsigned)event->pid, event->fd); } - for (; test_num < TST_TOTAL; test_num++) { - tst_resm(TFAIL, "didn't get event: mask=%llx", - event_set[test_num]); - + event->mask &= ~event_set[test_num]; + /* No events left in current mask? Go for next event */ + if (event->mask == 0) { + i += event->event_len; + close(event->fd); } + test_num++; } + for (; test_num < TST_TOTAL; test_num++) { + tst_res(TFAIL, "didn't get event: mask=%llx", + event_set[test_num]); - cleanup(); - tst_exit(); + } } static void setup(void) { - tst_sig(NOFORK, DEF_HANDLER, cleanup); - - TEST_PAUSE; - - tst_tmpdir(); sprintf(fname, "fname_%d", getpid()); - - if ((fd_notify = fanotify_init(FAN_CLASS_NOTIF, O_RDONLY)) < 0) { - if (errno == ENOSYS) { - tst_brkm(TCONF, cleanup, - "fanotify is not configured in this kernel."); - } else { - tst_brkm(TBROK | TERRNO, cleanup, - "fanotify_init failed"); - } - } + fd_notify = SAFE_FANOTIFY_INIT(FAN_CLASS_NOTIF, O_RDONLY); } static void cleanup(void) { if (fd_notify > 0 && close(fd_notify)) - tst_resm(TWARN | TERRNO, "close(%d) failed", fd_notify); - - tst_rmdir(); + tst_res(TWARN | TERRNO, "close(%d) failed", fd_notify); } -#else - -int main(void) -{ - tst_brkm(TCONF, NULL, "system doesn't have required fanotify support"); -} +static struct tst_test test = { + .test_all = test01, + .setup = setup, + .cleanup = cleanup, + .needs_tmpdir = 1, + .needs_root = 1 +}; +#else + TST_TEST_TCONF("system doesn't have required fanotify support"); #endif diff --git a/testcases/kernel/syscalls/fanotify/fanotify03.c b/testcases/kernel/syscalls/fanotify/fanotify03.c index 2aca2e1..0732146 100644 --- a/testcases/kernel/syscalls/fanotify/fanotify03.c +++ b/testcases/kernel/syscalls/fanotify/fanotify03.c @@ -38,12 +38,9 @@ #include <string.h> #include <signal.h> #include <sys/syscall.h> -#include "test.h" -#include "lapi/syscalls.h" +#include "tst_test.h" #include "fanotify.h" -#include "safe_macros.h" -char *TCID = "fanotify03"; int TST_TOTAL = 3; #if defined(HAVE_SYS_FANOTIFY_H) @@ -55,9 +52,6 @@ int TST_TOTAL = 3; /* reasonable guess as to size of 1024 events */ #define EVENT_BUF_LEN (EVENT_MAX * EVENT_SIZE) -static void setup(void); -static void cleanup(void); - #define BUF_SIZE 256 static char fname[BUF_SIZE]; static char buf[BUF_SIZE]; @@ -109,7 +103,7 @@ static void run_child(void) child_action.sa_flags = SA_NOCLDSTOP; if (sigaction(SIGCHLD, &child_action, NULL) < 0) { - tst_brkm(TBROK | TERRNO, cleanup, + tst_brk(TBROK | TERRNO, "sigaction(SIGCHLD, &child_action, NULL) failed"); } @@ -120,7 +114,7 @@ static void run_child(void) generate_events(); exit(0); case -1: - tst_brkm(TBROK | TERRNO, cleanup, "fork() failed"); + tst_brk(TBROK | TERRNO, "fork() failed"); } } @@ -133,171 +127,148 @@ static void check_child(void) sigemptyset(&child_action.sa_mask); child_action.sa_flags = SA_NOCLDSTOP; if (sigaction(SIGCHLD, &child_action, NULL) < 0) { - tst_brkm(TBROK | TERRNO, cleanup, + tst_brk(TBROK | TERRNO, "sigaction(SIGCHLD, &child_action, NULL) failed"); } if (waitpid(-1, &child_ret, 0) < 0) { - tst_brkm(TBROK | TERRNO, cleanup, + tst_brk(TBROK | TERRNO, "waitpid(-1, &child_ret, 0) failed"); } if (WIFSIGNALED(child_ret)) { - tst_resm(TFAIL, "child exited due to signal %d", + tst_res(TFAIL, "child exited due to signal %d", WTERMSIG(child_ret)); } else if (WIFEXITED(child_ret)) { if (WEXITSTATUS(child_ret) == 0) - tst_resm(TPASS, "child exited correctly"); + tst_res(TPASS, "child exited correctly"); else - tst_resm(TFAIL, "child exited with status %d", + tst_res(TFAIL, "child exited with status %d", WEXITSTATUS(child_ret)); } else { - tst_resm(TFAIL, "child exited for unknown reason (status %d)", + tst_res(TFAIL, "child exited for unknown reason (status %d)", child_ret); } } -int main(int ac, char **av) +void test01(void) { - int lc; - int fd_notify_backup = -1; - - tst_parse_opts(ac, av, NULL, NULL); + int tst_count, fd_notify_backup = -1; - setup(); + int ret, len = 0, i = 0, test_num = 0; - for (lc = 0; TEST_LOOPING(lc); lc++) { - int ret, len = 0, i = 0, test_num = 0; - - if (fd_notify_backup == -1) { - fd_notify_backup = dup(fd_notify); - if (fd_notify_backup < 0) - tst_brkm(TBROK | TERRNO, cleanup, - "dup(%d) failed", fd_notify); - } - run_child(); + if (fd_notify_backup == -1) { + fd_notify_backup = dup(fd_notify); + if (fd_notify_backup < 0) + tst_brk(TBROK | TERRNO, + "dup(%d) failed", fd_notify); + } + run_child(); - tst_count = 0; + tst_count = 0; - event_set[tst_count] = FAN_OPEN_PERM; - event_resp[tst_count++] = FAN_ALLOW; - event_set[tst_count] = FAN_ACCESS_PERM; - event_resp[tst_count++] = FAN_DENY; + event_set[tst_count] = FAN_OPEN_PERM; + event_resp[tst_count++] = FAN_ALLOW; + event_set[tst_count] = FAN_ACCESS_PERM; + event_resp[tst_count++] = FAN_DENY; - /* tst_count + 1 is for checking child return value */ - if (TST_TOTAL != tst_count + 1) { - tst_brkm(TBROK, cleanup, - "TST_TOTAL and tst_count do not match"); - } - tst_count = 0; - - /* - * check events - */ - while (test_num < TST_TOTAL && fd_notify != -1) { - struct fanotify_event_metadata *event; - - if (i == len) { - /* Get more events */ - ret = read(fd_notify, event_buf + len, - EVENT_BUF_LEN - len); - if (fd_notify == -1) - break; - if (ret < 0) { - tst_brkm(TBROK, cleanup, - "read(%d, buf, %zu) failed", - fd_notify, EVENT_BUF_LEN); - } - len += ret; - } + /* tst_count + 1 is for checking child return value */ + if (TST_TOTAL != tst_count + 1) { + tst_brk(TBROK, + "TST_TOTAL and tst_count do not match"); + } + tst_count = 0; - event = (struct fanotify_event_metadata *)&event_buf[i]; - if (!(event->mask & event_set[test_num])) { - tst_resm(TFAIL, - "get event: mask=%llx (expected %llx) " - "pid=%u fd=%u", - (unsigned long long)event->mask, - event_set[test_num], - (unsigned)event->pid, event->fd); - } else if (event->pid != child_pid) { - tst_resm(TFAIL, - "get event: mask=%llx pid=%u " - "(expected %u) fd=%u", - (unsigned long long)event->mask, - (unsigned)event->pid, - (unsigned)child_pid, - event->fd); - } else { - tst_resm(TPASS, - "get event: mask=%llx pid=%u fd=%u", - (unsigned long long)event->mask, - (unsigned)event->pid, event->fd); - } - /* Write response to permission event */ - if (event_set[test_num] & FAN_ALL_PERM_EVENTS) { - struct fanotify_response resp; - - resp.fd = event->fd; - resp.response = event_resp[test_num]; - SAFE_WRITE(cleanup, 1, fd_notify, &resp, - sizeof(resp)); - } - event->mask &= ~event_set[test_num]; - /* No events left in current mask? Go for next event */ - if (event->mask == 0) { - i += event->event_len; - close(event->fd); + /* + * check events + */ + while (test_num < TST_TOTAL && fd_notify != -1) { + struct fanotify_event_metadata *event; + + if (i == len) { + /* Get more events */ + ret = read(fd_notify, event_buf + len, + EVENT_BUF_LEN - len); + if (fd_notify == -1) + break; + if (ret < 0) { + tst_brk(TBROK, + "read(%d, buf, %zu) failed", + fd_notify, EVENT_BUF_LEN); } - test_num++; + len += ret; } - for (; test_num < TST_TOTAL - 1; test_num++) { - tst_resm(TFAIL, "didn't get event: mask=%llx", - event_set[test_num]); + event = (struct fanotify_event_metadata *)&event_buf[i]; + if (!(event->mask & event_set[test_num])) { + tst_res(TFAIL, + "get event: mask=%llx (expected %llx) " + "pid=%u fd=%u", + (unsigned long long)event->mask, + event_set[test_num], + (unsigned)event->pid, event->fd); + } else if (event->pid != child_pid) { + tst_res(TFAIL, + "get event: mask=%llx pid=%u " + "(expected %u) fd=%u", + (unsigned long long)event->mask, + (unsigned)event->pid, + (unsigned)child_pid, + event->fd); + } else { + tst_res(TPASS, + "get event: mask=%llx pid=%u fd=%u", + (unsigned long long)event->mask, + (unsigned)event->pid, event->fd); + } + /* Write response to permission event */ + if (event_set[test_num] & FAN_ALL_PERM_EVENTS) { + struct fanotify_response resp; + + resp.fd = event->fd; + resp.response = event_resp[test_num]; + SAFE_WRITE(1, fd_notify, &resp, + sizeof(resp)); } - check_child(); - /* We got SIGCHLD while running, resetup fd_notify */ - if (fd_notify == -1) { - fd_notify = fd_notify_backup; - fd_notify_backup = -1; + event->mask &= ~event_set[test_num]; + /* No events left in current mask? Go for next event */ + if (event->mask == 0) { + i += event->event_len; + close(event->fd); } + test_num++; } + for (; test_num < TST_TOTAL - 1; test_num++) { + tst_res(TFAIL, "didn't get event: mask=%llx", + event_set[test_num]); - cleanup(); - tst_exit(); + } + check_child(); + /* We got SIGCHLD while running, resetup fd_notify */ + if (fd_notify == -1) { + fd_notify = fd_notify_backup; + fd_notify_backup = -1; + } } static void setup(void) { int fd; - tst_sig(FORK, DEF_HANDLER, cleanup); - - TEST_PAUSE; - - tst_tmpdir(); sprintf(fname, "fname_%d", getpid()); - fd = SAFE_OPEN(cleanup, fname, O_CREAT | O_RDWR, 0644); - SAFE_WRITE(cleanup, 1, fd, fname, 1); - SAFE_CLOSE(cleanup, fd); - - if ((fd_notify = fanotify_init(FAN_CLASS_CONTENT, O_RDONLY)) < 0) { - if (errno == ENOSYS) { - tst_brkm(TCONF, cleanup, - "fanotify is not configured in this kernel."); - } else { - tst_brkm(TBROK | TERRNO, cleanup, - "fanotify_init failed"); - } - } + fd = SAFE_OPEN(fname, O_CREAT | O_RDWR, 0644); + SAFE_WRITE(1, fd, fname, 1); + SAFE_CLOSE(fd); + + fd_notify = SAFE_FANOTIFY_INIT(FAN_CLASS_CONTENT, O_RDONLY); if (fanotify_mark(fd_notify, FAN_MARK_ADD, FAN_ACCESS_PERM | FAN_OPEN_PERM, AT_FDCWD, fname) < 0) { if (errno == EINVAL) { - tst_brkm(TCONF | TERRNO, cleanup, + tst_brk(TCONF | TERRNO, "CONFIG_FANOTIFY_ACCESS_PERMISSIONS not " "configured in kernel?"); } else { - tst_brkm(TBROK | TERRNO, cleanup, + tst_brk(TBROK | TERRNO, "fanotify_mark (%d, FAN_MARK_ADD, FAN_ACCESS_PERM | " "FAN_OPEN_PERM, AT_FDCWD, %s) failed.", fd_notify, fname); } @@ -308,16 +279,17 @@ static void setup(void) static void cleanup(void) { if (fd_notify > 0 && close(fd_notify)) - tst_resm(TWARN | TERRNO, "close(%d) failed", fd_notify); - - tst_rmdir(); + tst_res(TWARN | TERRNO, "close(%d) failed", fd_notify); } -#else - -int main(void) -{ - tst_brkm(TCONF, NULL, "system doesn't have required fanotify support"); -} +static struct tst_test test = { + .test_all = test01, + .setup = setup, + .cleanup = cleanup, + .needs_tmpdir = 1, + .needs_root = 1 +}; +#else + TST_TEST_TCONF("system doesn't have required fanotify support"); #endif diff --git a/testcases/kernel/syscalls/fanotify/fanotify04.c b/testcases/kernel/syscalls/fanotify/fanotify04.c index dca52f0..091e51c 100644 --- a/testcases/kernel/syscalls/fanotify/fanotify04.c +++ b/testcases/kernel/syscalls/fanotify/fanotify04.c @@ -36,12 +36,9 @@ #include <errno.h> #include <string.h> #include <sys/syscall.h> -#include "test.h" -#include "lapi/syscalls.h" +#include "tst_test.h" #include "fanotify.h" -#include "safe_macros.h" -char *TCID = "fanotify04"; int TST_TOTAL = 9; #if defined(HAVE_SYS_FANOTIFY_H) @@ -53,9 +50,6 @@ int TST_TOTAL = 9; /* reasonable guess as to size of 1024 events */ #define EVENT_BUF_LEN (EVENT_MAX * EVENT_SIZE) -static void setup(void); -static void cleanup(void); - #define BUF_SIZE 256 static char fname[BUF_SIZE]; static char sname[BUF_SIZE]; @@ -84,11 +78,11 @@ static void check_mark(char *file, unsigned long long flag, char *flagstr, { if (fanotify_mark(fd_notify, FAN_MARK_ADD | flag, FAN_OPEN, AT_FDCWD, file) != expect) { - tst_resm(TFAIL, + tst_res(TFAIL, "fanotify_mark (%d, FAN_MARK_ADD | %s, FAN_OPEN, AT_FDCWD, " "'%s') %s", fd_notify, flagstr, file, expect_str_fail(expect)); } else { - tst_resm(TPASS, + tst_res(TPASS, "fanotify_mark (%d, FAN_MARK_ADD | %s, FAN_OPEN, AT_FDCWD, " "'%s') %s", fd_notify, flagstr, file, expect_str_pass(expect)); @@ -101,7 +95,7 @@ static void check_mark(char *file, unsigned long long flag, char *flagstr, if (fanotify_mark(fd_notify, FAN_MARK_REMOVE | flag, FAN_OPEN, AT_FDCWD, file) < 0) { - tst_brkm(TBROK | TERRNO, cleanup, + tst_brk(TBROK | TERRNO, "fanotify_mark (%d, FAN_MARK_REMOVE | %s, " "FAN_OPEN, AT_FDCWD, '%s') failed", fd_notify, flagstr, file); @@ -115,8 +109,8 @@ static void do_open(char *file, int flag, char *flagstr) { int fd; - fd = SAFE_OPEN(cleanup, file, O_RDONLY | flag); - SAFE_CLOSE(cleanup, fd); + fd = SAFE_OPEN(file, O_RDONLY | flag); + SAFE_CLOSE(fd); } #define DO_OPEN(file, flag) do_open(file, flag, #flag) @@ -138,22 +132,22 @@ static void verify_event(int mask) struct stat st; /* Read the event */ - ret = SAFE_READ(cleanup, 0, fd_notify, event_buf + len, + ret = SAFE_READ(0, fd_notify, event_buf + len, EVENT_BUF_LEN - len); event = (struct fanotify_event_metadata *)&event_buf[len]; len += ret; if (event->mask != FAN_OPEN) { - tst_resm(TFAIL, "got unexpected event %llx", + tst_res(TFAIL, "got unexpected event %llx", (unsigned long long)event->mask); } else if (fstat(event->fd, &st) < 0) { - tst_resm(TFAIL, "failed to stat event->fd (%s)", + tst_res(TFAIL, "failed to stat event->fd (%s)", strerror(errno)); } else if ((st.st_mode & S_IFMT) != mask) { - tst_resm(TFAIL, "event->fd points to object of different type " + tst_res(TFAIL, "event->fd points to object of different type " "(%o != %o)", st.st_mode & S_IFMT, mask); } else { - tst_resm(TPASS, "event generated properly for type %o", mask); + tst_res(TPASS, "event generated properly for type %o", mask); } close(event->fd); } @@ -181,15 +175,15 @@ static void verify_no_event(void) struct fanotify_event_metadata *event; event = (struct fanotify_event_metadata *)&event_buf[len]; - tst_resm(TFAIL, "seen unexpected event (mask %llx)", + tst_res(TFAIL, "seen unexpected event (mask %llx)", (unsigned long long)event->mask); /* Cleanup fd from the event */ close(event->fd); } else if (errno != EAGAIN) { - tst_resm(TFAIL | TERRNO, "read(%d, buf, %zu) failed", fd_notify, + tst_res(TFAIL | TERRNO, "read(%d, buf, %zu) failed", fd_notify, EVENT_BUF_LEN); } else { - tst_resm(TPASS, "No event as expected"); + tst_res(TPASS, "No event as expected"); } } @@ -200,104 +194,81 @@ static void test_open_symlink(char *file) verify_no_event(); } -int main(int ac, char **av) +void test01(void) { - int lc; - - tst_parse_opts(ac, av, NULL, NULL); - - setup(); + /* Check ONLYDIR on a directory */ + CHECK_MARK(".", FAN_MARK_ONLYDIR, 0, NULL); - for (lc = 0; TEST_LOOPING(lc); lc++) { - /* Check ONLYDIR on a directory */ - CHECK_MARK(".", FAN_MARK_ONLYDIR, 0, NULL); + /* Check ONLYDIR without a directory */ + CHECK_MARK(fname, FAN_MARK_ONLYDIR, -1, NULL); - /* Check ONLYDIR without a directory */ - CHECK_MARK(fname, FAN_MARK_ONLYDIR, -1, NULL); + /* Check DONT_FOLLOW for a symlink */ + CHECK_MARK(sname, FAN_MARK_DONT_FOLLOW, 0, test_open_symlink); - /* Check DONT_FOLLOW for a symlink */ - CHECK_MARK(sname, FAN_MARK_DONT_FOLLOW, 0, test_open_symlink); + /* Check without DONT_FOLLOW for a symlink */ + CHECK_MARK(sname, 0, 0, test_open_file); - /* Check without DONT_FOLLOW for a symlink */ - CHECK_MARK(sname, 0, 0, test_open_file); - - /* Verify FAN_MARK_FLUSH destroys all inode marks */ - if (fanotify_mark(fd_notify, FAN_MARK_ADD, - FAN_OPEN, AT_FDCWD, fname) < 0) { - tst_brkm(TBROK | TERRNO, cleanup, - "fanotify_mark (%d, FAN_MARK_ADD, FAN_OPEN, " - "AT_FDCWD, '%s') failed", fd_notify, fname); - } - if (fanotify_mark(fd_notify, FAN_MARK_ADD, - FAN_OPEN | FAN_ONDIR, AT_FDCWD, dir) < 0) { - tst_brkm(TBROK | TERRNO, cleanup, - "fanotify_mark (%d, FAN_MARK_ADD, FAN_OPEN | " - "FAN_ONDIR, AT_FDCWD, '%s') failed", fd_notify, - dir); - } - open_file(fname); - verify_event(S_IFREG); - open_dir(dir); - verify_event(S_IFDIR); - if (fanotify_mark(fd_notify, FAN_MARK_FLUSH, - 0, AT_FDCWD, ".") < 0) { - tst_brkm(TBROK | TERRNO, cleanup, - "fanotify_mark (%d, FAN_MARK_FLUSH, 0, " - "AT_FDCWD, '.') failed", fd_notify); - } - - open_dir(dir); - verify_no_event(); + /* Verify FAN_MARK_FLUSH destroys all inode marks */ + if (fanotify_mark(fd_notify, FAN_MARK_ADD, + FAN_OPEN, AT_FDCWD, fname) < 0) { + tst_brk(TBROK | TERRNO, + "fanotify_mark (%d, FAN_MARK_ADD, FAN_OPEN, " + "AT_FDCWD, '%s') failed", fd_notify, fname); + } + if (fanotify_mark(fd_notify, FAN_MARK_ADD, + FAN_OPEN | FAN_ONDIR, AT_FDCWD, dir) < 0) { + tst_brk(TBROK | TERRNO, + "fanotify_mark (%d, FAN_MARK_ADD, FAN_OPEN | " + "FAN_ONDIR, AT_FDCWD, '%s') failed", fd_notify, + dir); + } + open_file(fname); + verify_event(S_IFREG); + open_dir(dir); + verify_event(S_IFDIR); + if (fanotify_mark(fd_notify, FAN_MARK_FLUSH, + 0, AT_FDCWD, ".") < 0) { + tst_brk(TBROK | TERRNO, + "fanotify_mark (%d, FAN_MARK_FLUSH, 0, " + "AT_FDCWD, '.') failed", fd_notify); } - cleanup(); - tst_exit(); + open_dir(dir); + verify_no_event(); } static void setup(void) { int fd; - tst_sig(NOFORK, DEF_HANDLER, cleanup); - - TEST_PAUSE; - - tst_tmpdir(); sprintf(fname, "fname_%d", getpid()); - fd = SAFE_OPEN(cleanup, fname, O_RDWR | O_CREAT, 0644); - SAFE_CLOSE(cleanup, fd); + fd = SAFE_OPEN(fname, O_RDWR | O_CREAT, 0644); + SAFE_CLOSE(fd); sprintf(sname, "symlink_%d", getpid()); - SAFE_SYMLINK(cleanup, fname, sname); + SAFE_SYMLINK(fname, sname); sprintf(dir, "dir_%d", getpid()); - SAFE_MKDIR(cleanup, dir, 0755); - - if ((fd_notify = fanotify_init(FAN_CLASS_NOTIF | FAN_NONBLOCK, - O_RDONLY)) < 0) { - if (errno == ENOSYS) { - tst_brkm(TCONF, cleanup, - "fanotify is not configured in this kernel."); - } else { - tst_brkm(TBROK | TERRNO, cleanup, - "fanotify_init failed"); - } - } + SAFE_MKDIR(dir, 0755); + + fd_notify = SAFE_FANOTIFY_INIT(FAN_CLASS_NOTIF | FAN_NONBLOCK, + O_RDONLY); } static void cleanup(void) { if (fd_notify > 0 && close(fd_notify)) - tst_resm(TWARN | TERRNO, "close(%d) failed", fd_notify); - - tst_rmdir(); + tst_res(TWARN | TERRNO, "close(%d) failed", fd_notify); } -#else - -int main(void) -{ - tst_brkm(TCONF, NULL, "system doesn't have required fanotify support"); -} +static struct tst_test test = { + .test_all = test01, + .setup = setup, + .cleanup = cleanup, + .needs_tmpdir = 1, + .needs_root = 1 +}; +#else + TST_TEST_TCONF("system doesn't have required fanotify support"); #endif diff --git a/testcases/kernel/syscalls/fanotify/fanotify05.c b/testcases/kernel/syscalls/fanotify/fanotify05.c index 6063764..805fcd8 100644 --- a/testcases/kernel/syscalls/fanotify/fanotify05.c +++ b/testcases/kernel/syscalls/fanotify/fanotify05.c @@ -34,13 +34,8 @@ #include <errno.h> #include <string.h> #include <sys/syscall.h> -#include "test.h" -#include "lapi/syscalls.h" +#include "tst_test.h" #include "fanotify.h" -#include "safe_macros.h" - -char *TCID = "fanotify05"; -int TST_TOTAL = 1; #if defined(HAVE_SYS_FANOTIFY_H) #include <sys/fanotify.h> @@ -48,110 +43,84 @@ int TST_TOTAL = 1; /* Currently this is fixed in kernel... */ #define MAX_EVENTS 16384 -static void setup(void); -static void cleanup(void); - #define BUF_SIZE 256 static char fname[BUF_SIZE]; static int fd, fd_notify; struct fanotify_event_metadata event; -int main(int ac, char **av) +void test01(void) { - int lc, i; + int i; int len; - tst_parse_opts(ac, av, NULL, NULL); - - setup(); + /* + * generate events + */ + for (i = 0; i < MAX_EVENTS + 1; i++) { + sprintf(fname, "fname_%d", i); + fd = SAFE_OPEN(fname, O_RDWR | O_CREAT, 0644); + SAFE_CLOSE(fd); + } - for (lc = 0; TEST_LOOPING(lc); lc++) { + while (1) { /* - * generate events + * get list on events */ - for (i = 0; i < MAX_EVENTS + 1; i++) { - sprintf(fname, "fname_%d", i); - fd = SAFE_OPEN(cleanup, fname, O_RDWR | O_CREAT, 0644); - SAFE_CLOSE(cleanup, fd); - } - - while (1) { - /* - * get list on events - */ - len = read(fd_notify, &event, sizeof(event)); - if (len < 0) { - if (errno == -EAGAIN) { - tst_resm(TFAIL, "Overflow event not " - "generated!\n"); - break; - } - tst_brkm(TBROK | TERRNO, cleanup, - "read of notification event failed"); + len = read(fd_notify, &event, sizeof(event)); + if (len < 0) { + if (errno == -EAGAIN) { + tst_res(TFAIL, "Overflow event not " + "generated!\n"); break; } - if (event.fd != FAN_NOFD) - close(event.fd); + tst_brk(TBROK | TERRNO, + "read of notification event failed"); + break; + } + if (event.fd != FAN_NOFD) + close(event.fd); - /* - * check events - */ - if (event.mask != FAN_OPEN && - event.mask != FAN_Q_OVERFLOW) { - tst_resm(TFAIL, - "get event: mask=%llx (expected %llx)" - "pid=%u fd=%d", + /* + * check events + */ + if (event.mask != FAN_OPEN && + event.mask != FAN_Q_OVERFLOW) { + tst_res(TFAIL, + "get event: mask=%llx (expected %llx)" + "pid=%u fd=%d", + (unsigned long long)event.mask, + (unsigned long long)FAN_OPEN, + (unsigned)event.pid, event.fd); + break; + } + if (event.mask == FAN_Q_OVERFLOW) { + if (event.fd != FAN_NOFD) { + tst_res(TFAIL, + "invalid overflow event: " + "mask=%llx pid=%u fd=%d", (unsigned long long)event.mask, - (unsigned long long)FAN_OPEN, - (unsigned)event.pid, event.fd); + (unsigned)event.pid, + event.fd); break; } - if (event.mask == FAN_Q_OVERFLOW) { - if (event.fd != FAN_NOFD) { - tst_resm(TFAIL, - "invalid overflow event: " - "mask=%llx pid=%u fd=%d", - (unsigned long long)event.mask, - (unsigned)event.pid, - event.fd); - break; - } - tst_resm(TPASS, - "get event: mask=%llx pid=%u fd=%d", - (unsigned long long)event.mask, - (unsigned)event.pid, event.fd); - break; - } + tst_res(TPASS, + "get event: mask=%llx pid=%u fd=%d", + (unsigned long long)event.mask, + (unsigned)event.pid, event.fd); + break; } } - - cleanup(); - tst_exit(); } static void setup(void) { - tst_sig(NOFORK, DEF_HANDLER, cleanup); - - TEST_PAUSE; - - tst_tmpdir(); - - fd_notify = fanotify_init(FAN_CLASS_NOTIF | FAN_NONBLOCK, O_RDONLY); - if (fd_notify < 0) { - if (errno == ENOSYS) { - tst_brkm(TCONF, cleanup, - "fanotify is not configured in this kernel."); - } else { - tst_brkm(TBROK | TERRNO, cleanup, - "fanotify_init failed"); - } - } + fd_notify = SAFE_FANOTIFY_INIT(FAN_CLASS_NOTIF | FAN_NONBLOCK, + O_RDONLY); if (fanotify_mark(fd_notify, FAN_MARK_MOUNT | FAN_MARK_ADD, FAN_OPEN, AT_FDCWD, ".") < 0) { - tst_brkm(TBROK | TERRNO, cleanup, + tst_brk(TBROK | TERRNO, "fanotify_mark (%d, FAN_MARK_MOUNT | FAN_MARK_ADD, " "FAN_OPEN, AT_FDCWD, \".\") failed", fd_notify); @@ -161,16 +130,16 @@ static void setup(void) static void cleanup(void) { if (fd_notify > 0 && close(fd_notify)) - tst_resm(TWARN | TERRNO, "close(%d) failed", fd_notify); - - tst_rmdir(); + tst_res(TWARN | TERRNO, "close(%d) failed", fd_notify); } +static struct tst_test test = { + .test_all = test01, + .setup = setup, + .cleanup = cleanup, + .needs_root = 1, + .needs_tmpdir = 1, +}; #else - -int main(void) -{ - tst_brkm(TCONF, NULL, "system doesn't have required fanotify support"); -} - + TST_TEST_TCONF("system doesn't have required fanotify support"); #endif diff --git a/testcases/kernel/syscalls/fanotify/fanotify06.c b/testcases/kernel/syscalls/fanotify/fanotify06.c index d7fef6d..e30da8b 100644 --- a/testcases/kernel/syscalls/fanotify/fanotify06.c +++ b/testcases/kernel/syscalls/fanotify/fanotify06.c @@ -34,6 +34,7 @@ * * fanotify: fix notification of groups with inode & mount marks */ +#define _GNU_SOURCE #include "config.h" #include <stdio.h> @@ -44,12 +45,8 @@ #include <string.h> #include <sys/mount.h> #include <sys/syscall.h> -#include "test.h" -#include "lapi/syscalls.h" +#include "tst_test.h" #include "fanotify.h" -#include "safe_macros.h" - -char *TCID = "fanotify06"; #if defined(HAVE_SYS_FANOTIFY_H) #include <sys/fanotify.h> @@ -91,26 +88,17 @@ static void create_fanotify_groups(void) for (p = 0; p < FANOTIFY_PRIORITIES; p++) { for (i = 0; i < GROUPS_PER_PRIO; i++) { - fd_notify[p][i] = fanotify_init(fanotify_prio[p] | + fd_notify[p][i] = SAFE_FANOTIFY_INIT(fanotify_prio[p] | FAN_NONBLOCK, O_RDONLY); - if (fd_notify[p][i] < 0) { - if (errno == ENOSYS) { - tst_brkm(TCONF, cleanup, - "fanotify is not configured in" - " this kernel."); - } else { - tst_brkm(TBROK | TERRNO, cleanup, - "fanotify_init failed"); - } - } + /* Add mount mark for each group */ ret = fanotify_mark(fd_notify[p][i], FAN_MARK_ADD | FAN_MARK_MOUNT, FAN_MODIFY, AT_FDCWD, "."); if (ret < 0) { - tst_brkm(TBROK | TERRNO, cleanup, + tst_brk(TBROK | TERRNO, "fanotify_mark(%d, FAN_MARK_ADD | " "FAN_MARK_MOUNT, FAN_MODIFY, AT_FDCWD," " '.') failed", fd_notify[p][i]); @@ -124,7 +112,7 @@ static void create_fanotify_groups(void) FAN_MARK_IGNORED_SURV_MODIFY, FAN_MODIFY, AT_FDCWD, fname); if (ret < 0) { - tst_brkm(TBROK | TERRNO, cleanup, + tst_brk(TBROK | TERRNO, "fanotify_mark(%d, FAN_MARK_ADD | " "FAN_MARK_IGNORED_MASK | " "FAN_MARK_IGNORED_SURV_MODIFY, " @@ -143,7 +131,7 @@ static void cleanup_fanotify_groups(void) for (i = 0; i < GROUPS_PER_PRIO; i++) { if (fd_notify[p][i] && fd_notify[p][i] != -1) { if (close(fd_notify[p][i]) == -1) - tst_resm(TWARN, "close(%d) failed", + tst_res(TWARN, "close(%d) failed", fd_notify[p][i]); fd_notify[p][i] = 0; } @@ -154,115 +142,97 @@ static void cleanup_fanotify_groups(void) static void verify_event(int group, struct fanotify_event_metadata *event) { if (event->mask != FAN_MODIFY) { - tst_resm(TFAIL, "group %d get event: mask %llx (expected %llx) " + tst_res(TFAIL, "group %d get event: mask %llx (expected %llx) " "pid=%u fd=%u", group, (unsigned long long)event->mask, (unsigned long long)FAN_MODIFY, (unsigned)event->pid, event->fd); } else if (event->pid != getpid()) { - tst_resm(TFAIL, "group %d get event: mask %llx pid=%u " + tst_res(TFAIL, "group %d get event: mask %llx pid=%u " "(expected %u) fd=%u", group, (unsigned long long)event->mask, (unsigned)event->pid, (unsigned)getpid(), event->fd); } else { - tst_resm(TPASS, "group %d get event: mask %llx pid=%u fd=%u", + tst_res(TPASS, "group %d get event: mask %llx pid=%u fd=%u", group, (unsigned long long)event->mask, (unsigned)event->pid, event->fd); } } -int main(int ac, char **av) +void test01(void) { - int lc; - - tst_parse_opts(ac, av, NULL, NULL); - - setup(); - - for (lc = 0; TEST_LOOPING(lc); lc++) { - int ret; - unsigned int p, i; - struct fanotify_event_metadata *event; - - create_fanotify_groups(); - - /* - * generate sequence of events - */ - fd = SAFE_OPEN(cleanup, fname, O_RDWR); - SAFE_WRITE(cleanup, 1, fd, fname, strlen(fname)); - SAFE_CLOSE(cleanup, fd); - - /* First verify all groups without ignore mask got the event */ - for (i = 0; i < GROUPS_PER_PRIO; i++) { - ret = read(fd_notify[0][i], event_buf, EVENT_BUF_LEN); - if (ret < 0) { - if (errno == EAGAIN) { - tst_resm(TFAIL, "group %d did not get " - "event", i); - } - tst_brkm(TBROK | TERRNO, cleanup, - "reading fanotify events failed"); - } - if (ret < (int)FAN_EVENT_METADATA_LEN) { - tst_brkm(TBROK, cleanup, - "short read when reading fanotify " - "events (%d < %d)", ret, - (int)EVENT_BUF_LEN); + int ret; + unsigned int p, i; + struct fanotify_event_metadata *event; + + create_fanotify_groups(); + + /* + * generate sequence of events + */ + fd = SAFE_OPEN(fname, O_RDWR); + SAFE_WRITE(1, fd, fname, strlen(fname)); + SAFE_CLOSE(fd); + + /* First verify all groups without ignore mask got the event */ + for (i = 0; i < GROUPS_PER_PRIO; i++) { + ret = read(fd_notify[0][i], event_buf, EVENT_BUF_LEN); + if (ret < 0) { + if (errno == EAGAIN) { + tst_res(TFAIL, "group %d did not get " + "event", i); } - event = (struct fanotify_event_metadata *)event_buf; - if (ret > (int)event->event_len) { - tst_resm(TFAIL, "group %d got more than one " - "event (%d > %d)", i, ret, - event->event_len); - } else - verify_event(i, event); - close(event->fd); + tst_brk(TBROK | TERRNO, + "reading fanotify events failed"); + } + if (ret < (int)FAN_EVENT_METADATA_LEN) { + tst_brk(TBROK, + "short read when reading fanotify " + "events (%d < %d)", ret, + (int)EVENT_BUF_LEN); } - for (p = 1; p < FANOTIFY_PRIORITIES; p++) { - for (i = 0; i < GROUPS_PER_PRIO; i++) { - ret = read(fd_notify[p][i], event_buf, EVENT_BUF_LEN); - if (ret > 0) { - tst_resm(TFAIL, "group %d got event", - p*GROUPS_PER_PRIO + i); - } else if (ret == 0) { - tst_brkm(TBROK, cleanup, "zero length " - "read from fanotify fd"); - } else if (errno != EAGAIN) { - tst_brkm(TBROK | TERRNO, cleanup, - "reading fanotify events failed"); - } else { - tst_resm(TPASS, "group %d got no event", - p*GROUPS_PER_PRIO + i); - } + event = (struct fanotify_event_metadata *)event_buf; + if (ret > (int)event->event_len) { + tst_res(TFAIL, "group %d got more than one " + "event (%d > %d)", i, ret, + event->event_len); + } else + verify_event(i, event); + close(event->fd); + } + for (p = 1; p < FANOTIFY_PRIORITIES; p++) { + for (i = 0; i < GROUPS_PER_PRIO; i++) { + ret = read(fd_notify[p][i], event_buf, EVENT_BUF_LEN); + if (ret > 0) { + tst_res(TFAIL, "group %d got event", + p*GROUPS_PER_PRIO + i); + } else if (ret == 0) { + tst_brk(TBROK, "zero length " + "read from fanotify fd"); + } else if (errno != EAGAIN) { + tst_brk(TBROK | TERRNO, + "reading fanotify events failed"); + } else { + tst_res(TPASS, "group %d got no event", + p*GROUPS_PER_PRIO + i); } } - cleanup_fanotify_groups(); } - - cleanup(); - tst_exit(); + cleanup_fanotify_groups(); } static void setup(void) { - tst_sig(NOFORK, DEF_HANDLER, cleanup); - - TEST_PAUSE; - - tst_require_root(); - tst_tmpdir(); - - SAFE_MKDIR(cleanup, MOUNT_NAME, 0755); - SAFE_MOUNT(cleanup, MOUNT_NAME, MOUNT_NAME, NULL, MS_BIND, NULL); + SAFE_MKDIR(MOUNT_NAME, 0755); + SAFE_MOUNT(MOUNT_NAME, MOUNT_NAME, NULL, MS_BIND, NULL); mount_created = 1; - SAFE_CHDIR(cleanup, MOUNT_NAME); + SAFE_CHDIR(MOUNT_NAME); sprintf(fname, "tfile_%d", getpid()); - fd = SAFE_OPEN(cleanup, fname, O_RDWR | O_CREAT, 0700); - SAFE_WRITE(cleanup, 1, fd, fname, 1); + fd = SAFE_OPEN(fname, O_RDWR | O_CREAT, 0700); + SAFE_WRITE(1, fd, fname, 1); /* close the file we have open */ - SAFE_CLOSE(cleanup, fd); + SAFE_CLOSE(fd); } static void cleanup(void) @@ -270,19 +240,20 @@ static void cleanup(void) cleanup_fanotify_groups(); if (chdir(tst_get_tmpdir()) < 0) - tst_brkm(TBROK, NULL, "chdir(tmpdir) failed"); + tst_brk(TBROK, "chdir(tmpdir) failed"); if (mount_created && tst_umount(MOUNT_NAME) < 0) - tst_brkm(TBROK | TERRNO, NULL, "umount failed"); - - tst_rmdir(); + tst_brk(TBROK | TERRNO, "umount failed"); } -#else - -int main(void) -{ - tst_brkm(TCONF, NULL, "system doesn't have required fanotify support"); -} +static struct tst_test test = { + .test_all = test01, + .setup = setup, + .cleanup = cleanup, + .needs_tmpdir = 1, + .needs_root = 1 +}; +#else + TST_TEST_TCONF("system doesn't have required fanotify support"); #endif diff --git a/testcases/kernel/syscalls/fanotify/fanotify07.c b/testcases/kernel/syscalls/fanotify/fanotify07.c index 54e5a1f..af95c39 100644 --- a/testcases/kernel/syscalls/fanotify/fanotify07.c +++ b/testcases/kernel/syscalls/fanotify/fanotify07.c @@ -119,14 +119,7 @@ static int setup_instance(void) { int fd; - if ((fd = fanotify_init(FAN_CLASS_CONTENT, O_RDONLY)) < 0) { - if (errno == ENOSYS) { - tst_brk(TCONF, - "fanotify is not configured in this kernel."); - } else { - tst_brk(TBROK | TERRNO, "fanotify_init failed"); - } - } + fd = SAFE_FANOTIFY_INIT(FAN_CLASS_CONTENT, O_RDONLY); if (fanotify_mark(fd, FAN_MARK_ADD, FAN_ACCESS_PERM, AT_FDCWD, fname) < 0) { -- 1.8.3.1 ^ permalink raw reply related [flat|nested] 20+ messages in thread
* [LTP] [PATCH v3 2/3] fanotify: rewrite old cases with new lib 2017-08-14 14:01 ` [LTP] [PATCH v3 2/3] fanotify: rewrite old cases with new lib Xiong Zhou @ 2017-08-15 13:33 ` Cyril Hrubis 0 siblings, 0 replies; 20+ messages in thread From: Cyril Hrubis @ 2017-08-15 13:33 UTC (permalink / raw) To: ltp Hi! > diff --git a/testcases/kernel/syscalls/fanotify/fanotify01.c b/testcases/kernel/syscalls/fanotify/fanotify01.c > index 4799d54..bca0f47 100644 > --- a/testcases/kernel/syscalls/fanotify/fanotify01.c > +++ b/testcases/kernel/syscalls/fanotify/fanotify01.c > @@ -35,12 +35,9 @@ > #include <errno.h> > #include <string.h> > #include <sys/syscall.h> > -#include "test.h" > -#include "lapi/syscalls.h" > +#include "tst_test.h" > #include "fanotify.h" > -#include "safe_macros.h" > > -char *TCID = "fanotify01"; > int TST_TOTAL = 12; Can we declare this to be macro instead? Otherwise it looks like we left old library leftovers here. > #if defined(HAVE_SYS_FANOTIFY_H) > @@ -52,9 +49,6 @@ int TST_TOTAL = 12; > /* reasonable guess as to size of 1024 events */ > #define EVENT_BUF_LEN (EVENT_MAX * EVENT_SIZE) > > -static void setup(void); > -static void cleanup(void); > - > #define BUF_SIZE 256 > static char fname[BUF_SIZE]; > static char buf[BUF_SIZE]; > @@ -64,308 +58,282 @@ static unsigned long long event_set[EVENT_MAX]; > > static char event_buf[EVENT_BUF_LEN]; > > -int main(int ac, char **av) > +void test01(void) > { > - int lc; > - > - tst_parse_opts(ac, av, NULL, NULL); > - > - setup(); > - > - for (lc = 0; TEST_LOOPING(lc); lc++) { > - int ret, len, i = 0, test_num = 0; > - > - tst_count = 0; > - > - if (fanotify_mark(fd_notify, FAN_MARK_ADD, FAN_ACCESS | FAN_MODIFY | > - FAN_CLOSE | FAN_OPEN, AT_FDCWD, fname) < 0) { > - tst_brkm(TBROK | TERRNO, cleanup, > - "fanotify_mark (%d, FAN_MARK_ADD, FAN_ACCESS | " > - "FAN_MODIFY | FAN_CLOSE | FAN_OPEN, AT_FDCWD, %s) " > - "failed", fd_notify, fname); > - } > - > - /* > - * generate sequence of events > - */ > - fd = SAFE_OPEN(cleanup, fname, O_RDONLY); > - event_set[tst_count] = FAN_OPEN; > - tst_count++; > - > - SAFE_READ(cleanup, 0, fd, buf, BUF_SIZE); > - event_set[tst_count] = FAN_ACCESS; > - tst_count++; > - > - SAFE_CLOSE(cleanup, fd); > - event_set[tst_count] = FAN_CLOSE_NOWRITE; > - tst_count++; > - > - /* > - * Get list of events so far. We get events here to avoid > - * merging of following events with the previous ones. > - */ > - ret = SAFE_READ(cleanup, 0, fd_notify, event_buf, EVENT_BUF_LEN); > - len = ret; > - > - fd = SAFE_OPEN(cleanup, fname, O_RDWR | O_CREAT, 0700); > - event_set[tst_count] = FAN_OPEN; > - tst_count++; > - > - SAFE_WRITE(cleanup, 1, fd, fname, strlen(fname)); > - event_set[tst_count] = FAN_MODIFY; > - tst_count++; > - > - SAFE_CLOSE(cleanup, fd); > - event_set[tst_count] = FAN_CLOSE_WRITE; > - tst_count++; > + int ret, len, i = 0, test_num = 0; > > - /* > - * get another list of events > - */ > - ret = SAFE_READ(cleanup, 0, fd_notify, event_buf + len, > - EVENT_BUF_LEN - len); > - len += ret; > - > - /* > - * Ignore mask testing > - */ > - > - /* Ignore access events */ > - if (fanotify_mark(fd_notify, > - FAN_MARK_ADD | FAN_MARK_IGNORED_MASK, > - FAN_ACCESS, AT_FDCWD, fname) < 0) { > - tst_brkm(TBROK | TERRNO, cleanup, > - "fanotify_mark (%d, FAN_MARK_ADD | " > - "FAN_MARK_IGNORED_MASK, FAN_ACCESS, " > - "AT_FDCWD, %s) failed", fd_notify, fname); > - } > - > - fd = SAFE_OPEN(cleanup, fname, O_RDWR); > - event_set[tst_count] = FAN_OPEN; > - tst_count++; > - > - /* This event should be ignored */ > - SAFE_READ(cleanup, 0, fd, buf, BUF_SIZE); > + int tst_count = 0; > > - /* > - * get another list of events to verify the last one got ignored > - */ > - ret = SAFE_READ(cleanup, 0, fd_notify, event_buf + len, > - EVENT_BUF_LEN - len); > - len += ret; > - > - lseek(fd, 0, SEEK_SET); > - /* Generate modify event to clear ignore mask */ > - SAFE_WRITE(cleanup, 1, fd, fname, 1); > - event_set[tst_count] = FAN_MODIFY; > - tst_count++; > - > - /* > - * This event shouldn't be ignored because previous modification > - * should have removed the ignore mask > - */ > - SAFE_READ(cleanup, 0, fd, buf, BUF_SIZE); > - event_set[tst_count] = FAN_ACCESS; > - tst_count++; > - > - SAFE_CLOSE(cleanup, fd); > - event_set[tst_count] = FAN_CLOSE_WRITE; > - tst_count++; > - > - /* Read events to verify previous access was properly generated */ > - ret = SAFE_READ(cleanup, 0, fd_notify, event_buf + len, > - EVENT_BUF_LEN - len); > - len += ret; > + if (fanotify_mark(fd_notify, FAN_MARK_ADD, FAN_ACCESS | FAN_MODIFY | > + FAN_CLOSE | FAN_OPEN, AT_FDCWD, fname) < 0) { > + tst_brk(TBROK | TERRNO, > + "fanotify_mark (%d, FAN_MARK_ADD, FAN_ACCESS | " > + "FAN_MODIFY | FAN_CLOSE | FAN_OPEN, AT_FDCWD, %s) " > + "failed", fd_notify, fname); > + } > > - /* > - * Now ignore open & close events regardless of file > - * modifications > - */ > - if (fanotify_mark(fd_notify, > - FAN_MARK_ADD | FAN_MARK_IGNORED_MASK | FAN_MARK_IGNORED_SURV_MODIFY, > - FAN_OPEN | FAN_CLOSE, AT_FDCWD, fname) < 0) { > - tst_brkm(TBROK | TERRNO, cleanup, > - "fanotify_mark (%d, FAN_MARK_ADD | " > - "FAN_MARK_IGNORED_MASK | " > - "FAN_MARK_IGNORED_SURV_MODIFY, FAN_OPEN | " > - "FAN_CLOSE, AT_FDCWD, %s) failed", fd_notify, > - fname); > - } > + /* > + * generate sequence of events > + */ > + fd = SAFE_OPEN(fname, O_RDONLY); > + event_set[tst_count] = FAN_OPEN; > + tst_count++; > + > + SAFE_READ(0, fd, buf, BUF_SIZE); > + event_set[tst_count] = FAN_ACCESS; > + tst_count++; > + > + SAFE_CLOSE(fd); > + event_set[tst_count] = FAN_CLOSE_NOWRITE; > + tst_count++; > + > + /* > + * Get list of events so far. We get events here to avoid > + * merging of following events with the previous ones. > + */ > + ret = SAFE_READ(0, fd_notify, event_buf, EVENT_BUF_LEN); > + len = ret; > + > + fd = SAFE_OPEN(fname, O_RDWR | O_CREAT, 0700); > + event_set[tst_count] = FAN_OPEN; > + tst_count++; > + > + SAFE_WRITE(1, fd, fname, strlen(fname)); > + event_set[tst_count] = FAN_MODIFY; > + tst_count++; > + > + SAFE_CLOSE(fd); > + event_set[tst_count] = FAN_CLOSE_WRITE; > + tst_count++; > + > + /* > + * get another list of events > + */ > + ret = SAFE_READ(0, fd_notify, event_buf + len, > + EVENT_BUF_LEN - len); > + len += ret; > + > + /* > + * Ignore mask testing > + */ > + > + /* Ignore access events */ > + if (fanotify_mark(fd_notify, > + FAN_MARK_ADD | FAN_MARK_IGNORED_MASK, > + FAN_ACCESS, AT_FDCWD, fname) < 0) { > + tst_brk(TBROK | TERRNO, > + "fanotify_mark (%d, FAN_MARK_ADD | " > + "FAN_MARK_IGNORED_MASK, FAN_ACCESS, " > + "AT_FDCWD, %s) failed", fd_notify, fname); > + } > > - /* This event should be ignored */ > - fd = SAFE_OPEN(cleanup, fname, O_RDWR); > - > - SAFE_WRITE(cleanup, 1, fd, fname, 1); > - event_set[tst_count] = FAN_MODIFY; > - tst_count++; > - > - /* This event should be still ignored */ > - SAFE_CLOSE(cleanup, fd); > - > - /* This event should still be ignored */ > - fd = SAFE_OPEN(cleanup, fname, O_RDWR); > - > - /* Read events to verify open & close were ignored */ > - ret = SAFE_READ(cleanup, 0, fd_notify, event_buf + len, > - EVENT_BUF_LEN - len); > - len += ret; > - > - /* Now remove open and close from ignored mask */ > - if (fanotify_mark(fd_notify, > - FAN_MARK_REMOVE | FAN_MARK_IGNORED_MASK, > - FAN_OPEN | FAN_CLOSE, AT_FDCWD, fname) < 0) { > - tst_brkm(TBROK | TERRNO, cleanup, > - "fanotify_mark (%d, FAN_MARK_REMOVE | " > - "FAN_MARK_IGNORED_MASK, FAN_OPEN | " > - "FAN_CLOSE, AT_FDCWD, %s) failed", fd_notify, > - fname); > - } > + fd = SAFE_OPEN(fname, O_RDWR); > + event_set[tst_count] = FAN_OPEN; > + tst_count++; > + > + /* This event should be ignored */ > + SAFE_READ(0, fd, buf, BUF_SIZE); > + > + /* > + * get another list of events to verify the last one got ignored > + */ > + ret = SAFE_READ(0, fd_notify, event_buf + len, > + EVENT_BUF_LEN - len); > + len += ret; > + > + lseek(fd, 0, SEEK_SET); > + /* Generate modify event to clear ignore mask */ > + SAFE_WRITE(1, fd, fname, 1); > + event_set[tst_count] = FAN_MODIFY; > + tst_count++; > + > + /* > + * This event shouldn't be ignored because previous modification > + * should have removed the ignore mask > + */ > + SAFE_READ(0, fd, buf, BUF_SIZE); > + event_set[tst_count] = FAN_ACCESS; > + tst_count++; > + > + SAFE_CLOSE(fd); > + event_set[tst_count] = FAN_CLOSE_WRITE; > + tst_count++; > + > + /* Read events to verify previous access was properly generated */ > + ret = SAFE_READ(0, fd_notify, event_buf + len, > + EVENT_BUF_LEN - len); > + len += ret; > + > + /* > + * Now ignore open & close events regardless of file > + * modifications > + */ > + if (fanotify_mark(fd_notify, > + FAN_MARK_ADD | FAN_MARK_IGNORED_MASK | FAN_MARK_IGNORED_SURV_MODIFY, > + FAN_OPEN | FAN_CLOSE, AT_FDCWD, fname) < 0) { > + tst_brk(TBROK | TERRNO, > + "fanotify_mark (%d, FAN_MARK_ADD | " > + "FAN_MARK_IGNORED_MASK | " > + "FAN_MARK_IGNORED_SURV_MODIFY, FAN_OPEN | " > + "FAN_CLOSE, AT_FDCWD, %s) failed", fd_notify, > + fname); > + } > > - SAFE_CLOSE(cleanup, fd); > - event_set[tst_count] = FAN_CLOSE_WRITE; > - tst_count++; > + /* This event should be ignored */ > + fd = SAFE_OPEN(fname, O_RDWR); > + > + SAFE_WRITE(1, fd, fname, 1); > + event_set[tst_count] = FAN_MODIFY; > + tst_count++; > + > + /* This event should be still ignored */ > + SAFE_CLOSE(fd); > + > + /* This event should still be ignored */ > + fd = SAFE_OPEN(fname, O_RDWR); > + > + /* Read events to verify open & close were ignored */ > + ret = SAFE_READ(0, fd_notify, event_buf + len, > + EVENT_BUF_LEN - len); > + len += ret; > + > + /* Now remove open and close from ignored mask */ > + if (fanotify_mark(fd_notify, > + FAN_MARK_REMOVE | FAN_MARK_IGNORED_MASK, > + FAN_OPEN | FAN_CLOSE, AT_FDCWD, fname) < 0) { > + tst_brk(TBROK | TERRNO, > + "fanotify_mark (%d, FAN_MARK_REMOVE | " > + "FAN_MARK_IGNORED_MASK, FAN_OPEN | " > + "FAN_CLOSE, AT_FDCWD, %s) failed", fd_notify, > + fname); > + } > > - /* Read events to verify close was generated */ > - ret = SAFE_READ(cleanup, 0, fd_notify, event_buf + len, > - EVENT_BUF_LEN - len); > - len += ret; > + SAFE_CLOSE(fd); > + event_set[tst_count] = FAN_CLOSE_WRITE; > + tst_count++; > > - if (TST_TOTAL != tst_count) { > - tst_brkm(TBROK, cleanup, > - "TST_TOTAL (%d) and tst_count (%d) are not " > - "equal", TST_TOTAL, tst_count); > - } > - tst_count = 0; > + /* Read events to verify close was generated */ > + ret = SAFE_READ(0, fd_notify, event_buf + len, > + EVENT_BUF_LEN - len); > + len += ret; > > - /* > - * check events > - */ > - while (i < len) { > - struct fanotify_event_metadata *event; > - > - event = (struct fanotify_event_metadata *)&event_buf[i]; > - if (test_num >= TST_TOTAL) { > - tst_resm(TFAIL, > - "get unnecessary event: mask=%llx " > - "pid=%u fd=%u", > - (unsigned long long)event->mask, > - (unsigned)event->pid, event->fd); > - } else if (!(event->mask & event_set[test_num])) { > - tst_resm(TFAIL, > - "get event: mask=%llx (expected %llx) " > - "pid=%u fd=%u", > + if (TST_TOTAL != tst_count) { > + tst_brk(TBROK, > + "TST_TOTAL (%d) and tst_count (%d) are not " > + "equal", TST_TOTAL, tst_count); > + } > + tst_count = 0; > + > + /* > + * check events > + */ > + while (i < len) { > + struct fanotify_event_metadata *event; > + > + event = (struct fanotify_event_metadata *)&event_buf[i]; > + if (test_num >= TST_TOTAL) { > + tst_res(TFAIL, > + "get unnecessary event: mask=%llx " > + "pid=%u fd=%u", > + (unsigned long long)event->mask, > + (unsigned)event->pid, event->fd); > + } else if (!(event->mask & event_set[test_num])) { > + tst_res(TFAIL, > + "get event: mask=%llx (expected %llx) " > + "pid=%u fd=%u", > + (unsigned long long)event->mask, > + event_set[test_num], > + (unsigned)event->pid, event->fd); > + } else if (event->pid != getpid()) { > + tst_res(TFAIL, > + "get event: mask=%llx pid=%u " > + "(expected %u) fd=%u", > + (unsigned long long)event->mask, > + (unsigned)event->pid, > + (unsigned)getpid(), > + event->fd); > + } else { > + if (event->fd == -2) > + goto pass; > + ret = read(event->fd, buf, BUF_SIZE); > + if (ret != strlen(fname)) { > + tst_res(TFAIL, > + "cannot read from returned fd " > + "of event: mask=%llx pid=%u " > + "fd=%u ret=%d (errno=%d)", > (unsigned long long)event->mask, > - event_set[test_num], > - (unsigned)event->pid, event->fd); > - } else if (event->pid != getpid()) { > - tst_resm(TFAIL, > - "get event: mask=%llx pid=%u " > - "(expected %u) fd=%u", > + (unsigned)event->pid, > + event->fd, ret, errno); > + } else if (memcmp(buf, fname, strlen(fname))) { > + tst_res(TFAIL, > + "wrong data read from returned fd " > + "of event: mask=%llx pid=%u " > + "fd=%u", > (unsigned long long)event->mask, > (unsigned)event->pid, > - (unsigned)getpid(), > event->fd); > } else { > - if (event->fd == -2) > - goto pass; > - ret = read(event->fd, buf, BUF_SIZE); > - if (ret != strlen(fname)) { > - tst_resm(TFAIL, > - "cannot read from returned fd " > - "of event: mask=%llx pid=%u " > - "fd=%u ret=%d (errno=%d)", > - (unsigned long long)event->mask, > - (unsigned)event->pid, > - event->fd, ret, errno); > - } else if (memcmp(buf, fname, strlen(fname))) { > - tst_resm(TFAIL, > - "wrong data read from returned fd " > - "of event: mask=%llx pid=%u " > - "fd=%u", > - (unsigned long long)event->mask, > - (unsigned)event->pid, > - event->fd); > - } else { > pass: > - tst_resm(TPASS, > - "get event: mask=%llx pid=%u fd=%u", > - (unsigned long long)event->mask, > - (unsigned)event->pid, event->fd); > - } > + tst_res(TPASS, > + "get event: mask=%llx pid=%u fd=%u", > + (unsigned long long)event->mask, > + (unsigned)event->pid, event->fd); > } > - /* > - * We have verified the data now so close fd and > - * invalidate it so that we don't check it again > - * unnecessarily > - */ > - close(event->fd); > - event->fd = -2; > - event->mask &= ~event_set[test_num]; > - /* No events left in current mask? Go for next event */ > - if (event->mask == 0) { > - i += event->event_len; > - } > - test_num++; > - } > - for (; test_num < TST_TOTAL; test_num++) { > - tst_resm(TFAIL, "didn't get event: mask=%llx", > - event_set[test_num]); > - > } > - /* Remove mark to clear FAN_MARK_IGNORED_SURV_MODIFY */ > - if (fanotify_mark(fd_notify, FAN_MARK_REMOVE, FAN_ACCESS | FAN_MODIFY | > - FAN_CLOSE | FAN_OPEN, AT_FDCWD, fname) < 0) { > - tst_brkm(TBROK | TERRNO, cleanup, > - "fanotify_mark (%d, FAN_MARK_REMOVE, FAN_ACCESS | " > - "FAN_MODIFY | FAN_CLOSE | FAN_OPEN, AT_FDCWD, %s) " > - "failed", fd_notify, fname); > + /* > + * We have verified the data now so close fd and > + * invalidate it so that we don't check it again > + * unnecessarily > + */ > + close(event->fd); > + event->fd = -2; > + event->mask &= ~event_set[test_num]; > + /* No events left in current mask? Go for next event */ > + if (event->mask == 0) { > + i += event->event_len; > } > - > + test_num++; > } > + for (; test_num < TST_TOTAL; test_num++) { > + tst_res(TFAIL, "didn't get event: mask=%llx", > + event_set[test_num]); > > - cleanup(); > - tst_exit(); > + } > + /* Remove mark to clear FAN_MARK_IGNORED_SURV_MODIFY */ > + if (fanotify_mark(fd_notify, FAN_MARK_REMOVE, FAN_ACCESS | FAN_MODIFY | > + FAN_CLOSE | FAN_OPEN, AT_FDCWD, fname) < 0) { > + tst_brk(TBROK | TERRNO, > + "fanotify_mark (%d, FAN_MARK_REMOVE, FAN_ACCESS | " > + "FAN_MODIFY | FAN_CLOSE | FAN_OPEN, AT_FDCWD, %s) " > + "failed", fd_notify, fname); > + } > } > > static void setup(void) > { > - tst_sig(NOFORK, DEF_HANDLER, cleanup); > - > - TEST_PAUSE; > - > - tst_tmpdir(); > - > sprintf(fname, "tfile_%d", getpid()); > - fd = SAFE_OPEN(cleanup, fname, O_RDWR | O_CREAT, 0700); > - SAFE_WRITE(cleanup, 1, fd, fname, 1); > - > + fd = SAFE_OPEN(fname, O_RDWR | O_CREAT, 0700); > + SAFE_WRITE(1, fd, fname, 1); > /* close the file we have open */ > - SAFE_CLOSE(cleanup, fd); > + SAFE_CLOSE(fd); > > - if ((fd_notify = fanotify_init(FAN_CLASS_NOTIF, O_RDONLY)) < 0) { > - if (errno == ENOSYS) { > - tst_brkm(TCONF, cleanup, > - "fanotify is not configured in this kernel."); > - } else { > - tst_brkm(TBROK | TERRNO, cleanup, > - "fanotify_init failed"); > - } > - } > + fd_notify = SAFE_FANOTIFY_INIT(FAN_CLASS_NOTIF, O_RDONLY); > } > > static void cleanup(void) > { > if (fd_notify > 0 && close(fd_notify)) > - tst_resm(TWARN | TERRNO, "close(%d) failed", fd_notify); > - > - tst_rmdir(); > + tst_res(TWARN | TERRNO, "close(%d) failed", fd_notify); We can use safe macros in test cleanup now, so this should be changed to: if (fd_notify > 0) SAFE_CLOSE(fd_notify); And that applies to most of the tests, otherwise it looks pretty much straightforward. -- Cyril Hrubis chrubis@suse.cz ^ permalink raw reply [flat|nested] 20+ messages in thread
* [LTP] [PATCH v3 3/3] syscalls/fanotify0[89]: add sanity check for FAN_CLOEXEC 2017-08-14 14:01 ` [LTP] [PATCH v3 0/3] add SAFE_FANOTIFY_INIT and FAN_CLOEXEC cases Xiong Zhou 2017-08-14 14:01 ` [LTP] [PATCH v3 1/3] lib: add helper SAFE_FANOTIFY_INIT Xiong Zhou 2017-08-14 14:01 ` [LTP] [PATCH v3 2/3] fanotify: rewrite old cases with new lib Xiong Zhou @ 2017-08-14 14:01 ` Xiong Zhou 2017-08-15 13:36 ` Cyril Hrubis 2 siblings, 1 reply; 20+ messages in thread From: Xiong Zhou @ 2017-08-14 14:01 UTC (permalink / raw) To: ltp Signed-off-by: Xiong Zhou <xzhou@redhat.com> --- runtest/syscalls | 2 + testcases/kernel/syscalls/fanotify/fanotify08.c | 77 +++++++++++++++++++++++++ testcases/kernel/syscalls/fanotify/fanotify09.c | 77 +++++++++++++++++++++++++ 3 files changed, 156 insertions(+) create mode 100644 testcases/kernel/syscalls/fanotify/fanotify08.c create mode 100644 testcases/kernel/syscalls/fanotify/fanotify09.c diff --git a/runtest/syscalls b/runtest/syscalls index be617e3..4033a8f 100644 --- a/runtest/syscalls +++ b/runtest/syscalls @@ -479,6 +479,8 @@ fanotify04 fanotify04 fanotify05 fanotify05 fanotify06 fanotify06 fanotify07 fanotify07 +fanotify08 fanotify08 +fanotify09 fanotify09 ioperm01 ioperm01 ioperm02 ioperm02 diff --git a/testcases/kernel/syscalls/fanotify/fanotify08.c b/testcases/kernel/syscalls/fanotify/fanotify08.c new file mode 100644 index 0000000..82f3ff9 --- /dev/null +++ b/testcases/kernel/syscalls/fanotify/fanotify08.c @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2017 RedHat. All Rights Reserved. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it would be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * Further, this software is distributed without any warranty that it is + * free of the rightful claim of any third person regarding infringement + * or the like. Any license provided herein, whether implied or + * otherwise, applies only to this software file. Patent licenses, if + * any, provided herein do not apply to combinations of this program with + * other software, or any other product whatsoever. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Started by Xiong Zhou <xzhou@redhat.com> + * + * DESCRIPTION + * Check fanotify_init flag O_CLOEXEC. + */ +#define _GNU_SOURCE +#include "config.h" + +#include <stdio.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <fcntl.h> +#include <errno.h> +#include <string.h> +#include <sys/syscall.h> +#include "tst_test.h" +#include "fanotify.h" + +#if defined(HAVE_SYS_FANOTIFY_H) +#include <sys/fanotify.h> + +static int fd_notify; + +static void test01(void) +{ + int coe; + + fd_notify = SAFE_FANOTIFY_INIT(FAN_CLASS_NOTIF|FAN_CLOEXEC, O_RDONLY); + + coe = SAFE_FCNTL(fd_notify, F_GETFD); + + if (coe & FD_CLOEXEC) { + tst_res(TPASS, "set close-on-exit"); + } else { + tst_res(TFAIL, "not set close-on-exit"); + } + + SAFE_CLOSE(fd_notify); +} + +static void cleanup(void) +{ + if (fd_notify > 0) + SAFE_CLOSE(fd_notify); +} + +static struct tst_test test = { + .test_all = test01, + .cleanup = cleanup, + .needs_root = 1, +}; + +#else + TST_TEST_TCONF("system doesn't have required fanotify support"); +#endif diff --git a/testcases/kernel/syscalls/fanotify/fanotify09.c b/testcases/kernel/syscalls/fanotify/fanotify09.c new file mode 100644 index 0000000..26c0b00 --- /dev/null +++ b/testcases/kernel/syscalls/fanotify/fanotify09.c @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2017 RedHat. All Rights Reserved. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it would be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * Further, this software is distributed without any warranty that it is + * free of the rightful claim of any third person regarding infringement + * or the like. Any license provided herein, whether implied or + * otherwise, applies only to this software file. Patent licenses, if + * any, provided herein do not apply to combinations of this program with + * other software, or any other product whatsoever. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Started by Xiong Zhou <xzhou@redhat.com> + * + * DESCRIPTION + * Check fanotify_init flag O_CLOEXEC. + */ +#define _GNU_SOURCE +#include "config.h" + +#include <stdio.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <fcntl.h> +#include <errno.h> +#include <string.h> +#include <sys/syscall.h> +#include "tst_test.h" +#include "fanotify.h" + +#if defined(HAVE_SYS_FANOTIFY_H) +#include <sys/fanotify.h> + +static int fd_notify; + +static void test01(void) +{ + int coe; + + fd_notify = SAFE_FANOTIFY_INIT(FAN_CLASS_NOTIF, O_RDONLY); + + coe = SAFE_FCNTL(fd_notify, F_GETFD); + + if (coe & FD_CLOEXEC) { + tst_res(TFAIL, "set close-on-exit"); + } else { + tst_res(TPASS, "not set close-on-exit"); + } + + SAFE_CLOSE(fd_notify); +} + +static void cleanup(void) +{ + if (fd_notify > 0) + SAFE_CLOSE(fd_notify); +} + +static struct tst_test test = { + .test_all = test01, + .cleanup = cleanup, + .needs_root = 1, +}; + +#else + TST_TEST_TCONF("system doesn't have required fanotify support"); +#endif -- 1.8.3.1 ^ permalink raw reply related [flat|nested] 20+ messages in thread
* [LTP] [PATCH v3 3/3] syscalls/fanotify0[89]: add sanity check for FAN_CLOEXEC 2017-08-14 14:01 ` [LTP] [PATCH v3 3/3] syscalls/fanotify0[89]: add sanity check for FAN_CLOEXEC Xiong Zhou @ 2017-08-15 13:36 ` Cyril Hrubis 2017-08-16 7:15 ` [LTP] [PATCH v4 0/3] add SAFE_FANOTIFY_INIT and FAN_CLOEXEC check Xiong Zhou 0 siblings, 1 reply; 20+ messages in thread From: Cyril Hrubis @ 2017-08-15 13:36 UTC (permalink / raw) To: ltp Hi! > testcases/kernel/syscalls/fanotify/fanotify08.c | 77 +++++++++++++++++++++++++ > testcases/kernel/syscalls/fanotify/fanotify09.c | 77 +++++++++++++++++++++++++ There is no reason to put these tests into two separate files. We even have that described in the tutorial: https://github.com/linux-test-project/ltp/wiki/C-Test-Case-Tutorial#6-split-the-test -- Cyril Hrubis chrubis@suse.cz ^ permalink raw reply [flat|nested] 20+ messages in thread
* [LTP] [PATCH v4 0/3] add SAFE_FANOTIFY_INIT and FAN_CLOEXEC check 2017-08-15 13:36 ` Cyril Hrubis @ 2017-08-16 7:15 ` Xiong Zhou 2017-08-16 7:15 ` [LTP] [PATCH v4 1/3] lib: add helper SAFE_FANOTIFY_INIT Xiong Zhou ` (2 more replies) 0 siblings, 3 replies; 20+ messages in thread From: Xiong Zhou @ 2017-08-16 7:15 UTC (permalink / raw) To: ltp v4: Delete else branch in helper. Make TST_TOTAL a macro instead of integer. Use SAFE_CLOSE in cleanup in old cases. Use iteration of tst_test.test instead of 2 files. v3: Add SAFE_FANOTIFY_INIT helper. Rewrite old cases. Too big a patch, we can also split it. Split FAN_CLOEXEC case to 2 separate cases. Thanks for reviewing! Xiong Zhou (3): lib: add helper SAFE_FANOTIFY_INIT fanotify: rewrite old cases with new lib syscalls/fanotify08: add sanity check for FAN_CLOEXEC include/tst_safe_macros.h | 5 + lib/tst_safe_macros.c | 21 + runtest/syscalls | 1 + testcases/kernel/syscalls/fanotify/fanotify01.c | 526 +++++++++++------------- testcases/kernel/syscalls/fanotify/fanotify02.c | 326 +++++++-------- testcases/kernel/syscalls/fanotify/fanotify03.c | 254 +++++------- testcases/kernel/syscalls/fanotify/fanotify04.c | 165 +++----- testcases/kernel/syscalls/fanotify/fanotify05.c | 155 +++---- testcases/kernel/syscalls/fanotify/fanotify06.c | 193 ++++----- testcases/kernel/syscalls/fanotify/fanotify07.c | 9 +- testcases/kernel/syscalls/fanotify/fanotify08.c | 91 ++++ 11 files changed, 838 insertions(+), 908 deletions(-) create mode 100644 testcases/kernel/syscalls/fanotify/fanotify08.c -- 1.8.3.1 ^ permalink raw reply [flat|nested] 20+ messages in thread
* [LTP] [PATCH v4 1/3] lib: add helper SAFE_FANOTIFY_INIT 2017-08-16 7:15 ` [LTP] [PATCH v4 0/3] add SAFE_FANOTIFY_INIT and FAN_CLOEXEC check Xiong Zhou @ 2017-08-16 7:15 ` Xiong Zhou 2017-08-18 10:22 ` Cyril Hrubis 2017-08-16 7:15 ` [LTP] [PATCH v4 2/3] fanotify: rewrite old cases with new lib Xiong Zhou 2017-08-16 7:15 ` [LTP] [PATCH v4 3/3] syscalls/fanotify08: add sanity check for FAN_CLOEXEC Xiong Zhou 2 siblings, 1 reply; 20+ messages in thread From: Xiong Zhou @ 2017-08-16 7:15 UTC (permalink / raw) To: ltp Signed-off-by: Xiong Zhou <xzhou@redhat.com> --- include/tst_safe_macros.h | 5 +++++ lib/tst_safe_macros.c | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/include/tst_safe_macros.h b/include/tst_safe_macros.h index 8245b68..f23f765 100644 --- a/include/tst_safe_macros.h +++ b/include/tst_safe_macros.h @@ -438,4 +438,9 @@ int safe_mknod(const char *file, const int lineno, const char *pathname, #define SAFE_MKNOD(pathname, mode, dev) \ safe_mknod(__FILE__, __LINE__, (pathname), (mode), (dev)) +int safe_fanotify_init(const char *file, const int lineno, + unsigned int flags, unsigned int event_f_flags); +#define SAFE_FANOTIFY_INIT(fan, mode) \ + safe_fanotify_init(__FILE__, __LINE__, (fan), (mode)) + #endif /* SAFE_MACROS_H__ */ diff --git a/lib/tst_safe_macros.c b/lib/tst_safe_macros.c index e7f5095..ecd39f8 100644 --- a/lib/tst_safe_macros.c +++ b/lib/tst_safe_macros.c @@ -17,6 +17,7 @@ #define _GNU_SOURCE #include <unistd.h> +#include <errno.h> #define TST_NO_DEFAULT_MAIN #include "tst_test.h" #include "tst_safe_macros.h" @@ -47,3 +48,23 @@ pid_t safe_getpgid(const char *file, const int lineno, pid_t pid) return pgid; } + +int safe_fanotify_init(const char *file, const int lineno, + unsigned int flags, unsigned int event_f_flags) +{ + int rval; + + rval = fanotify_init(flags, event_f_flags); + + if (rval == -1) { + + if (errno == ENOSYS) { + tst_brk(TCONF, + "fanotify is not configured in this kernel."); + } + tst_brk(TBROK | TERRNO, + "%s:%d: fanotify_init() failed", file, lineno); + } + + return rval; +} -- 1.8.3.1 ^ permalink raw reply related [flat|nested] 20+ messages in thread
* [LTP] [PATCH v4 1/3] lib: add helper SAFE_FANOTIFY_INIT 2017-08-16 7:15 ` [LTP] [PATCH v4 1/3] lib: add helper SAFE_FANOTIFY_INIT Xiong Zhou @ 2017-08-18 10:22 ` Cyril Hrubis 2017-08-21 3:12 ` Xiong Zhou 0 siblings, 1 reply; 20+ messages in thread From: Cyril Hrubis @ 2017-08-18 10:22 UTC (permalink / raw) To: ltp Hi! Pushed with following patch (to fix warnings), thanks. diff --git a/lib/tst_safe_macros.c b/lib/tst_safe_macros.c index ecd39f8bc..d4d81c6cd 100644 --- a/lib/tst_safe_macros.c +++ b/lib/tst_safe_macros.c @@ -18,6 +18,10 @@ #define _GNU_SOURCE #include <unistd.h> #include <errno.h> +#include "config.h" +#ifdef HAVE_SYS_FANOTIFY_H +# include <sys/fanotify.h> +#endif #define TST_NO_DEFAULT_MAIN #include "tst_test.h" #include "tst_safe_macros.h" @@ -54,10 +58,10 @@ int safe_fanotify_init(const char *file, const int lineno, { int rval; +#ifdef HAVE_SYS_FANOTIFY_H rval = fanotify_init(flags, event_f_flags); if (rval == -1) { - if (errno == ENOSYS) { tst_brk(TCONF, "fanotify is not configured in this kernel."); @@ -65,6 +69,9 @@ int safe_fanotify_init(const char *file, const int lineno, tst_brk(TBROK | TERRNO, "%s:%d: fanotify_init() failed", file, lineno); } +#else + tst_brk(TCONF, "Header <sys/fanotify.h> is not present"); +#endif /* HAVE_SYS_FANOTIFY_H */ return rval; } -- Cyril Hrubis chrubis@suse.cz ^ permalink raw reply related [flat|nested] 20+ messages in thread
* [LTP] [PATCH v4 1/3] lib: add helper SAFE_FANOTIFY_INIT 2017-08-18 10:22 ` Cyril Hrubis @ 2017-08-21 3:12 ` Xiong Zhou 2017-08-21 11:45 ` Cyril Hrubis 0 siblings, 1 reply; 20+ messages in thread From: Xiong Zhou @ 2017-08-21 3:12 UTC (permalink / raw) To: ltp On Fri, Aug 18, 2017 at 12:22:52PM +0200, Cyril Hrubis wrote: > Hi! > Pushed with following patch (to fix warnings), thanks. Thanks very much! Do you have any tool detecting these warnings ? > ^ permalink raw reply [flat|nested] 20+ messages in thread
* [LTP] [PATCH v4 1/3] lib: add helper SAFE_FANOTIFY_INIT 2017-08-21 3:12 ` Xiong Zhou @ 2017-08-21 11:45 ` Cyril Hrubis 0 siblings, 0 replies; 20+ messages in thread From: Cyril Hrubis @ 2017-08-21 11:45 UTC (permalink / raw) To: ltp Hi! > Thanks very much! > > Do you have any tool detecting these warnings ? Just recent enough gcc (5.4 at this point), there was a quite a lot of work done on warnings and error message in gcc in the last few years. -- Cyril Hrubis chrubis@suse.cz ^ permalink raw reply [flat|nested] 20+ messages in thread
* [LTP] [PATCH v4 2/3] fanotify: rewrite old cases with new lib 2017-08-16 7:15 ` [LTP] [PATCH v4 0/3] add SAFE_FANOTIFY_INIT and FAN_CLOEXEC check Xiong Zhou 2017-08-16 7:15 ` [LTP] [PATCH v4 1/3] lib: add helper SAFE_FANOTIFY_INIT Xiong Zhou @ 2017-08-16 7:15 ` Xiong Zhou 2017-08-18 10:24 ` Cyril Hrubis 2017-08-16 7:15 ` [LTP] [PATCH v4 3/3] syscalls/fanotify08: add sanity check for FAN_CLOEXEC Xiong Zhou 2 siblings, 1 reply; 20+ messages in thread From: Xiong Zhou @ 2017-08-16 7:15 UTC (permalink / raw) To: ltp To use SAFE_FANOTIFY_INIT helper. Signed-off-by: Xiong Zhou <xzhou@redhat.com> --- testcases/kernel/syscalls/fanotify/fanotify01.c | 526 +++++++++++------------- testcases/kernel/syscalls/fanotify/fanotify02.c | 326 +++++++-------- testcases/kernel/syscalls/fanotify/fanotify03.c | 254 +++++------- testcases/kernel/syscalls/fanotify/fanotify04.c | 165 +++----- testcases/kernel/syscalls/fanotify/fanotify05.c | 155 +++---- testcases/kernel/syscalls/fanotify/fanotify06.c | 193 ++++----- testcases/kernel/syscalls/fanotify/fanotify07.c | 9 +- 7 files changed, 720 insertions(+), 908 deletions(-) diff --git a/testcases/kernel/syscalls/fanotify/fanotify01.c b/testcases/kernel/syscalls/fanotify/fanotify01.c index 4799d54..205a999 100644 --- a/testcases/kernel/syscalls/fanotify/fanotify01.c +++ b/testcases/kernel/syscalls/fanotify/fanotify01.c @@ -35,13 +35,8 @@ #include <errno.h> #include <string.h> #include <sys/syscall.h> -#include "test.h" -#include "lapi/syscalls.h" +#include "tst_test.h" #include "fanotify.h" -#include "safe_macros.h" - -char *TCID = "fanotify01"; -int TST_TOTAL = 12; #if defined(HAVE_SYS_FANOTIFY_H) #include <sys/fanotify.h> @@ -52,10 +47,9 @@ int TST_TOTAL = 12; /* reasonable guess as to size of 1024 events */ #define EVENT_BUF_LEN (EVENT_MAX * EVENT_SIZE) -static void setup(void); -static void cleanup(void); - #define BUF_SIZE 256 +#define TST_TOTAL 12 + static char fname[BUF_SIZE]; static char buf[BUF_SIZE]; static int fd, fd_notify; @@ -64,308 +58,282 @@ static unsigned long long event_set[EVENT_MAX]; static char event_buf[EVENT_BUF_LEN]; -int main(int ac, char **av) +void test01(void) { - int lc; - - tst_parse_opts(ac, av, NULL, NULL); - - setup(); - - for (lc = 0; TEST_LOOPING(lc); lc++) { - int ret, len, i = 0, test_num = 0; - - tst_count = 0; - - if (fanotify_mark(fd_notify, FAN_MARK_ADD, FAN_ACCESS | FAN_MODIFY | - FAN_CLOSE | FAN_OPEN, AT_FDCWD, fname) < 0) { - tst_brkm(TBROK | TERRNO, cleanup, - "fanotify_mark (%d, FAN_MARK_ADD, FAN_ACCESS | " - "FAN_MODIFY | FAN_CLOSE | FAN_OPEN, AT_FDCWD, %s) " - "failed", fd_notify, fname); - } - - /* - * generate sequence of events - */ - fd = SAFE_OPEN(cleanup, fname, O_RDONLY); - event_set[tst_count] = FAN_OPEN; - tst_count++; - - SAFE_READ(cleanup, 0, fd, buf, BUF_SIZE); - event_set[tst_count] = FAN_ACCESS; - tst_count++; - - SAFE_CLOSE(cleanup, fd); - event_set[tst_count] = FAN_CLOSE_NOWRITE; - tst_count++; - - /* - * Get list of events so far. We get events here to avoid - * merging of following events with the previous ones. - */ - ret = SAFE_READ(cleanup, 0, fd_notify, event_buf, EVENT_BUF_LEN); - len = ret; - - fd = SAFE_OPEN(cleanup, fname, O_RDWR | O_CREAT, 0700); - event_set[tst_count] = FAN_OPEN; - tst_count++; - - SAFE_WRITE(cleanup, 1, fd, fname, strlen(fname)); - event_set[tst_count] = FAN_MODIFY; - tst_count++; - - SAFE_CLOSE(cleanup, fd); - event_set[tst_count] = FAN_CLOSE_WRITE; - tst_count++; - - /* - * get another list of events - */ - ret = SAFE_READ(cleanup, 0, fd_notify, event_buf + len, - EVENT_BUF_LEN - len); - len += ret; - - /* - * Ignore mask testing - */ - - /* Ignore access events */ - if (fanotify_mark(fd_notify, - FAN_MARK_ADD | FAN_MARK_IGNORED_MASK, - FAN_ACCESS, AT_FDCWD, fname) < 0) { - tst_brkm(TBROK | TERRNO, cleanup, - "fanotify_mark (%d, FAN_MARK_ADD | " - "FAN_MARK_IGNORED_MASK, FAN_ACCESS, " - "AT_FDCWD, %s) failed", fd_notify, fname); - } - - fd = SAFE_OPEN(cleanup, fname, O_RDWR); - event_set[tst_count] = FAN_OPEN; - tst_count++; - - /* This event should be ignored */ - SAFE_READ(cleanup, 0, fd, buf, BUF_SIZE); - - /* - * get another list of events to verify the last one got ignored - */ - ret = SAFE_READ(cleanup, 0, fd_notify, event_buf + len, - EVENT_BUF_LEN - len); - len += ret; - - lseek(fd, 0, SEEK_SET); - /* Generate modify event to clear ignore mask */ - SAFE_WRITE(cleanup, 1, fd, fname, 1); - event_set[tst_count] = FAN_MODIFY; - tst_count++; - - /* - * This event shouldn't be ignored because previous modification - * should have removed the ignore mask - */ - SAFE_READ(cleanup, 0, fd, buf, BUF_SIZE); - event_set[tst_count] = FAN_ACCESS; - tst_count++; + int ret, len, i = 0, test_num = 0; - SAFE_CLOSE(cleanup, fd); - event_set[tst_count] = FAN_CLOSE_WRITE; - tst_count++; + int tst_count = 0; - /* Read events to verify previous access was properly generated */ - ret = SAFE_READ(cleanup, 0, fd_notify, event_buf + len, - EVENT_BUF_LEN - len); - len += ret; + if (fanotify_mark(fd_notify, FAN_MARK_ADD, FAN_ACCESS | FAN_MODIFY | + FAN_CLOSE | FAN_OPEN, AT_FDCWD, fname) < 0) { + tst_brk(TBROK | TERRNO, + "fanotify_mark (%d, FAN_MARK_ADD, FAN_ACCESS | " + "FAN_MODIFY | FAN_CLOSE | FAN_OPEN, AT_FDCWD, %s) " + "failed", fd_notify, fname); + } - /* - * Now ignore open & close events regardless of file - * modifications - */ - if (fanotify_mark(fd_notify, - FAN_MARK_ADD | FAN_MARK_IGNORED_MASK | FAN_MARK_IGNORED_SURV_MODIFY, - FAN_OPEN | FAN_CLOSE, AT_FDCWD, fname) < 0) { - tst_brkm(TBROK | TERRNO, cleanup, - "fanotify_mark (%d, FAN_MARK_ADD | " - "FAN_MARK_IGNORED_MASK | " - "FAN_MARK_IGNORED_SURV_MODIFY, FAN_OPEN | " - "FAN_CLOSE, AT_FDCWD, %s) failed", fd_notify, - fname); - } + /* + * generate sequence of events + */ + fd = SAFE_OPEN(fname, O_RDONLY); + event_set[tst_count] = FAN_OPEN; + tst_count++; + + SAFE_READ(0, fd, buf, BUF_SIZE); + event_set[tst_count] = FAN_ACCESS; + tst_count++; + + SAFE_CLOSE(fd); + event_set[tst_count] = FAN_CLOSE_NOWRITE; + tst_count++; + + /* + * Get list of events so far. We get events here to avoid + * merging of following events with the previous ones. + */ + ret = SAFE_READ(0, fd_notify, event_buf, EVENT_BUF_LEN); + len = ret; + + fd = SAFE_OPEN(fname, O_RDWR | O_CREAT, 0700); + event_set[tst_count] = FAN_OPEN; + tst_count++; + + SAFE_WRITE(1, fd, fname, strlen(fname)); + event_set[tst_count] = FAN_MODIFY; + tst_count++; + + SAFE_CLOSE(fd); + event_set[tst_count] = FAN_CLOSE_WRITE; + tst_count++; + + /* + * get another list of events + */ + ret = SAFE_READ(0, fd_notify, event_buf + len, + EVENT_BUF_LEN - len); + len += ret; + + /* + * Ignore mask testing + */ + + /* Ignore access events */ + if (fanotify_mark(fd_notify, + FAN_MARK_ADD | FAN_MARK_IGNORED_MASK, + FAN_ACCESS, AT_FDCWD, fname) < 0) { + tst_brk(TBROK | TERRNO, + "fanotify_mark (%d, FAN_MARK_ADD | " + "FAN_MARK_IGNORED_MASK, FAN_ACCESS, " + "AT_FDCWD, %s) failed", fd_notify, fname); + } - /* This event should be ignored */ - fd = SAFE_OPEN(cleanup, fname, O_RDWR); - - SAFE_WRITE(cleanup, 1, fd, fname, 1); - event_set[tst_count] = FAN_MODIFY; - tst_count++; - - /* This event should be still ignored */ - SAFE_CLOSE(cleanup, fd); - - /* This event should still be ignored */ - fd = SAFE_OPEN(cleanup, fname, O_RDWR); - - /* Read events to verify open & close were ignored */ - ret = SAFE_READ(cleanup, 0, fd_notify, event_buf + len, - EVENT_BUF_LEN - len); - len += ret; - - /* Now remove open and close from ignored mask */ - if (fanotify_mark(fd_notify, - FAN_MARK_REMOVE | FAN_MARK_IGNORED_MASK, - FAN_OPEN | FAN_CLOSE, AT_FDCWD, fname) < 0) { - tst_brkm(TBROK | TERRNO, cleanup, - "fanotify_mark (%d, FAN_MARK_REMOVE | " - "FAN_MARK_IGNORED_MASK, FAN_OPEN | " - "FAN_CLOSE, AT_FDCWD, %s) failed", fd_notify, - fname); - } + fd = SAFE_OPEN(fname, O_RDWR); + event_set[tst_count] = FAN_OPEN; + tst_count++; + + /* This event should be ignored */ + SAFE_READ(0, fd, buf, BUF_SIZE); + + /* + * get another list of events to verify the last one got ignored + */ + ret = SAFE_READ(0, fd_notify, event_buf + len, + EVENT_BUF_LEN - len); + len += ret; + + lseek(fd, 0, SEEK_SET); + /* Generate modify event to clear ignore mask */ + SAFE_WRITE(1, fd, fname, 1); + event_set[tst_count] = FAN_MODIFY; + tst_count++; + + /* + * This event shouldn't be ignored because previous modification + * should have removed the ignore mask + */ + SAFE_READ(0, fd, buf, BUF_SIZE); + event_set[tst_count] = FAN_ACCESS; + tst_count++; + + SAFE_CLOSE(fd); + event_set[tst_count] = FAN_CLOSE_WRITE; + tst_count++; + + /* Read events to verify previous access was properly generated */ + ret = SAFE_READ(0, fd_notify, event_buf + len, + EVENT_BUF_LEN - len); + len += ret; + + /* + * Now ignore open & close events regardless of file + * modifications + */ + if (fanotify_mark(fd_notify, + FAN_MARK_ADD | FAN_MARK_IGNORED_MASK | FAN_MARK_IGNORED_SURV_MODIFY, + FAN_OPEN | FAN_CLOSE, AT_FDCWD, fname) < 0) { + tst_brk(TBROK | TERRNO, + "fanotify_mark (%d, FAN_MARK_ADD | " + "FAN_MARK_IGNORED_MASK | " + "FAN_MARK_IGNORED_SURV_MODIFY, FAN_OPEN | " + "FAN_CLOSE, AT_FDCWD, %s) failed", fd_notify, + fname); + } - SAFE_CLOSE(cleanup, fd); - event_set[tst_count] = FAN_CLOSE_WRITE; - tst_count++; + /* This event should be ignored */ + fd = SAFE_OPEN(fname, O_RDWR); + + SAFE_WRITE(1, fd, fname, 1); + event_set[tst_count] = FAN_MODIFY; + tst_count++; + + /* This event should be still ignored */ + SAFE_CLOSE(fd); + + /* This event should still be ignored */ + fd = SAFE_OPEN(fname, O_RDWR); + + /* Read events to verify open & close were ignored */ + ret = SAFE_READ(0, fd_notify, event_buf + len, + EVENT_BUF_LEN - len); + len += ret; + + /* Now remove open and close from ignored mask */ + if (fanotify_mark(fd_notify, + FAN_MARK_REMOVE | FAN_MARK_IGNORED_MASK, + FAN_OPEN | FAN_CLOSE, AT_FDCWD, fname) < 0) { + tst_brk(TBROK | TERRNO, + "fanotify_mark (%d, FAN_MARK_REMOVE | " + "FAN_MARK_IGNORED_MASK, FAN_OPEN | " + "FAN_CLOSE, AT_FDCWD, %s) failed", fd_notify, + fname); + } - /* Read events to verify close was generated */ - ret = SAFE_READ(cleanup, 0, fd_notify, event_buf + len, - EVENT_BUF_LEN - len); - len += ret; + SAFE_CLOSE(fd); + event_set[tst_count] = FAN_CLOSE_WRITE; + tst_count++; - if (TST_TOTAL != tst_count) { - tst_brkm(TBROK, cleanup, - "TST_TOTAL (%d) and tst_count (%d) are not " - "equal", TST_TOTAL, tst_count); - } - tst_count = 0; + /* Read events to verify close was generated */ + ret = SAFE_READ(0, fd_notify, event_buf + len, + EVENT_BUF_LEN - len); + len += ret; - /* - * check events - */ - while (i < len) { - struct fanotify_event_metadata *event; - - event = (struct fanotify_event_metadata *)&event_buf[i]; - if (test_num >= TST_TOTAL) { - tst_resm(TFAIL, - "get unnecessary event: mask=%llx " - "pid=%u fd=%u", - (unsigned long long)event->mask, - (unsigned)event->pid, event->fd); - } else if (!(event->mask & event_set[test_num])) { - tst_resm(TFAIL, - "get event: mask=%llx (expected %llx) " - "pid=%u fd=%u", + if (TST_TOTAL != tst_count) { + tst_brk(TBROK, + "TST_TOTAL (%d) and tst_count (%d) are not " + "equal", TST_TOTAL, tst_count); + } + tst_count = 0; + + /* + * check events + */ + while (i < len) { + struct fanotify_event_metadata *event; + + event = (struct fanotify_event_metadata *)&event_buf[i]; + if (test_num >= TST_TOTAL) { + tst_res(TFAIL, + "get unnecessary event: mask=%llx " + "pid=%u fd=%u", + (unsigned long long)event->mask, + (unsigned)event->pid, event->fd); + } else if (!(event->mask & event_set[test_num])) { + tst_res(TFAIL, + "get event: mask=%llx (expected %llx) " + "pid=%u fd=%u", + (unsigned long long)event->mask, + event_set[test_num], + (unsigned)event->pid, event->fd); + } else if (event->pid != getpid()) { + tst_res(TFAIL, + "get event: mask=%llx pid=%u " + "(expected %u) fd=%u", + (unsigned long long)event->mask, + (unsigned)event->pid, + (unsigned)getpid(), + event->fd); + } else { + if (event->fd == -2) + goto pass; + ret = read(event->fd, buf, BUF_SIZE); + if (ret != strlen(fname)) { + tst_res(TFAIL, + "cannot read from returned fd " + "of event: mask=%llx pid=%u " + "fd=%u ret=%d (errno=%d)", (unsigned long long)event->mask, - event_set[test_num], - (unsigned)event->pid, event->fd); - } else if (event->pid != getpid()) { - tst_resm(TFAIL, - "get event: mask=%llx pid=%u " - "(expected %u) fd=%u", + (unsigned)event->pid, + event->fd, ret, errno); + } else if (memcmp(buf, fname, strlen(fname))) { + tst_res(TFAIL, + "wrong data read from returned fd " + "of event: mask=%llx pid=%u " + "fd=%u", (unsigned long long)event->mask, (unsigned)event->pid, - (unsigned)getpid(), event->fd); } else { - if (event->fd == -2) - goto pass; - ret = read(event->fd, buf, BUF_SIZE); - if (ret != strlen(fname)) { - tst_resm(TFAIL, - "cannot read from returned fd " - "of event: mask=%llx pid=%u " - "fd=%u ret=%d (errno=%d)", - (unsigned long long)event->mask, - (unsigned)event->pid, - event->fd, ret, errno); - } else if (memcmp(buf, fname, strlen(fname))) { - tst_resm(TFAIL, - "wrong data read from returned fd " - "of event: mask=%llx pid=%u " - "fd=%u", - (unsigned long long)event->mask, - (unsigned)event->pid, - event->fd); - } else { pass: - tst_resm(TPASS, - "get event: mask=%llx pid=%u fd=%u", - (unsigned long long)event->mask, - (unsigned)event->pid, event->fd); - } - } - /* - * We have verified the data now so close fd and - * invalidate it so that we don't check it again - * unnecessarily - */ - close(event->fd); - event->fd = -2; - event->mask &= ~event_set[test_num]; - /* No events left in current mask? Go for next event */ - if (event->mask == 0) { - i += event->event_len; + tst_res(TPASS, + "get event: mask=%llx pid=%u fd=%u", + (unsigned long long)event->mask, + (unsigned)event->pid, event->fd); } - test_num++; - } - for (; test_num < TST_TOTAL; test_num++) { - tst_resm(TFAIL, "didn't get event: mask=%llx", - event_set[test_num]); - } - /* Remove mark to clear FAN_MARK_IGNORED_SURV_MODIFY */ - if (fanotify_mark(fd_notify, FAN_MARK_REMOVE, FAN_ACCESS | FAN_MODIFY | - FAN_CLOSE | FAN_OPEN, AT_FDCWD, fname) < 0) { - tst_brkm(TBROK | TERRNO, cleanup, - "fanotify_mark (%d, FAN_MARK_REMOVE, FAN_ACCESS | " - "FAN_MODIFY | FAN_CLOSE | FAN_OPEN, AT_FDCWD, %s) " - "failed", fd_notify, fname); + /* + * We have verified the data now so close fd and + * invalidate it so that we don't check it again + * unnecessarily + */ + close(event->fd); + event->fd = -2; + event->mask &= ~event_set[test_num]; + /* No events left in current mask? Go for next event */ + if (event->mask == 0) { + i += event->event_len; } - + test_num++; } + for (; test_num < TST_TOTAL; test_num++) { + tst_res(TFAIL, "didn't get event: mask=%llx", + event_set[test_num]); - cleanup(); - tst_exit(); + } + /* Remove mark to clear FAN_MARK_IGNORED_SURV_MODIFY */ + if (fanotify_mark(fd_notify, FAN_MARK_REMOVE, FAN_ACCESS | FAN_MODIFY | + FAN_CLOSE | FAN_OPEN, AT_FDCWD, fname) < 0) { + tst_brk(TBROK | TERRNO, + "fanotify_mark (%d, FAN_MARK_REMOVE, FAN_ACCESS | " + "FAN_MODIFY | FAN_CLOSE | FAN_OPEN, AT_FDCWD, %s) " + "failed", fd_notify, fname); + } } static void setup(void) { - tst_sig(NOFORK, DEF_HANDLER, cleanup); - - TEST_PAUSE; - - tst_tmpdir(); - sprintf(fname, "tfile_%d", getpid()); - fd = SAFE_OPEN(cleanup, fname, O_RDWR | O_CREAT, 0700); - SAFE_WRITE(cleanup, 1, fd, fname, 1); - + fd = SAFE_OPEN(fname, O_RDWR | O_CREAT, 0700); + SAFE_WRITE(1, fd, fname, 1); /* close the file we have open */ - SAFE_CLOSE(cleanup, fd); + SAFE_CLOSE(fd); - if ((fd_notify = fanotify_init(FAN_CLASS_NOTIF, O_RDONLY)) < 0) { - if (errno == ENOSYS) { - tst_brkm(TCONF, cleanup, - "fanotify is not configured in this kernel."); - } else { - tst_brkm(TBROK | TERRNO, cleanup, - "fanotify_init failed"); - } - } + fd_notify = SAFE_FANOTIFY_INIT(FAN_CLASS_NOTIF, O_RDONLY); } static void cleanup(void) { - if (fd_notify > 0 && close(fd_notify)) - tst_resm(TWARN | TERRNO, "close(%d) failed", fd_notify); - - tst_rmdir(); + if (fd_notify > 0) + SAFE_CLOSE(fd_notify); } -#else - -int main(void) -{ - tst_brkm(TCONF, NULL, "system doesn't have required fanotify support"); -} +static struct tst_test test = { + .test_all = test01, + .setup = setup, + .cleanup = cleanup, + .needs_tmpdir = 1, + .needs_root = 1 +}; +#else + TST_TEST_TCONF("system doesn't have required fanotify support"); #endif diff --git a/testcases/kernel/syscalls/fanotify/fanotify02.c b/testcases/kernel/syscalls/fanotify/fanotify02.c index 29dbe4f..5aae777 100644 --- a/testcases/kernel/syscalls/fanotify/fanotify02.c +++ b/testcases/kernel/syscalls/fanotify/fanotify02.c @@ -35,13 +35,8 @@ #include <errno.h> #include <string.h> #include <sys/syscall.h> -#include "test.h" -#include "lapi/syscalls.h" +#include "tst_test.h" #include "fanotify.h" -#include "safe_macros.h" - -char *TCID = "fanotify02"; -int TST_TOTAL = 8; #if defined(HAVE_SYS_FANOTIFY_H) #include <sys/fanotify.h> @@ -52,10 +47,9 @@ int TST_TOTAL = 8; /* reasonable guess as to size of 1024 events */ #define EVENT_BUF_LEN (EVENT_MAX * EVENT_SIZE) -static void setup(void); -static void cleanup(void); - #define BUF_SIZE 256 +#define TST_TOTAL 8 + static char fname[BUF_SIZE]; static char buf[BUF_SIZE]; static int fd, fd_notify; @@ -64,197 +58,173 @@ static unsigned long long event_set[EVENT_MAX]; static char event_buf[EVENT_BUF_LEN]; -int main(int ac, char **av) +void test01(void) { - int lc; - - tst_parse_opts(ac, av, NULL, NULL); - - setup(); - - for (lc = 0; TEST_LOOPING(lc); lc++) { - int ret, len, i = 0, test_num = 0; - - tst_count = 0; - - if (fanotify_mark(fd_notify, FAN_MARK_ADD, FAN_ACCESS | - FAN_MODIFY | FAN_CLOSE | FAN_OPEN | - FAN_EVENT_ON_CHILD | FAN_ONDIR, AT_FDCWD, - ".") < 0) { - tst_brkm(TBROK | TERRNO, cleanup, - "fanotify_mark (%d, FAN_MARK_ADD, FAN_ACCESS | " - "FAN_MODIFY | FAN_CLOSE | FAN_OPEN | " - "FAN_EVENT_ON_CHILD | FAN_ONDIR, AT_FDCWD, '.') " - "failed", fd_notify); - } + int ret, len, i = 0, test_num = 0; + + int tst_count = 0; + + if (fanotify_mark(fd_notify, FAN_MARK_ADD, FAN_ACCESS | + FAN_MODIFY | FAN_CLOSE | FAN_OPEN | + FAN_EVENT_ON_CHILD | FAN_ONDIR, AT_FDCWD, + ".") < 0) { + tst_brk(TBROK | TERRNO, + "fanotify_mark (%d, FAN_MARK_ADD, FAN_ACCESS | " + "FAN_MODIFY | FAN_CLOSE | FAN_OPEN | " + "FAN_EVENT_ON_CHILD | FAN_ONDIR, AT_FDCWD, '.') " + "failed", fd_notify); + } - /* - * generate sequence of events - */ - fd = SAFE_OPEN(cleanup, fname, O_RDWR | O_CREAT, 0700); - event_set[tst_count] = FAN_OPEN; - tst_count++; - - SAFE_WRITE(cleanup, 1, fd, fname, strlen(fname)); - event_set[tst_count] = FAN_MODIFY; - tst_count++; - - SAFE_CLOSE(cleanup, fd); - event_set[tst_count] = FAN_CLOSE_WRITE; - tst_count++; - - /* - * Get list of events so far. We get events here to avoid - * merging of following events with the previous ones. - */ - ret = SAFE_READ(cleanup, 0, fd_notify, event_buf, - EVENT_BUF_LEN); - len = ret; - - fd = SAFE_OPEN(cleanup, fname, O_RDONLY); - event_set[tst_count] = FAN_OPEN; - tst_count++; - - SAFE_READ(cleanup, 0, fd, buf, BUF_SIZE); - event_set[tst_count] = FAN_ACCESS; - tst_count++; - - SAFE_CLOSE(cleanup, fd); - event_set[tst_count] = FAN_CLOSE_NOWRITE; - tst_count++; - - /* - * get next events - */ - ret = SAFE_READ(cleanup, 0, fd_notify, event_buf + len, - EVENT_BUF_LEN - len); - len += ret; - - /* - * now remove child mark - */ - if (fanotify_mark(fd_notify, FAN_MARK_REMOVE, - FAN_EVENT_ON_CHILD, AT_FDCWD, ".") < 0) { - tst_brkm(TBROK | TERRNO, cleanup, - "fanotify_mark (%d, FAN_MARK REMOVE, " - "FAN_EVENT_ON_CHILD, AT_FDCWD, '.') failed", - fd_notify); - } + /* + * generate sequence of events + */ + fd = SAFE_OPEN(fname, O_RDWR | O_CREAT, 0700); + event_set[tst_count] = FAN_OPEN; + tst_count++; + + SAFE_WRITE(1, fd, fname, strlen(fname)); + event_set[tst_count] = FAN_MODIFY; + tst_count++; + + SAFE_CLOSE(fd); + event_set[tst_count] = FAN_CLOSE_WRITE; + tst_count++; + + /* + * Get list of events so far. We get events here to avoid + * merging of following events with the previous ones. + */ + ret = SAFE_READ(0, fd_notify, event_buf, + EVENT_BUF_LEN); + len = ret; + + fd = SAFE_OPEN(fname, O_RDONLY); + event_set[tst_count] = FAN_OPEN; + tst_count++; + + SAFE_READ(0, fd, buf, BUF_SIZE); + event_set[tst_count] = FAN_ACCESS; + tst_count++; + + SAFE_CLOSE(fd); + event_set[tst_count] = FAN_CLOSE_NOWRITE; + tst_count++; + + /* + * get next events + */ + ret = SAFE_READ(0, fd_notify, event_buf + len, + EVENT_BUF_LEN - len); + len += ret; + + /* + * now remove child mark + */ + if (fanotify_mark(fd_notify, FAN_MARK_REMOVE, + FAN_EVENT_ON_CHILD, AT_FDCWD, ".") < 0) { + tst_brk(TBROK | TERRNO, + "fanotify_mark (%d, FAN_MARK REMOVE, " + "FAN_EVENT_ON_CHILD, AT_FDCWD, '.') failed", + fd_notify); + } - /* - * Do something to verify events didn't get generated - */ - fd = SAFE_OPEN(cleanup, fname, O_RDONLY); + /* + * Do something to verify events didn't get generated + */ + fd = SAFE_OPEN(fname, O_RDONLY); - SAFE_CLOSE(cleanup, fd); + SAFE_CLOSE(fd); - fd = SAFE_OPEN(cleanup, ".", O_RDONLY | O_DIRECTORY); - event_set[tst_count] = FAN_OPEN; - tst_count++; + fd = SAFE_OPEN(".", O_RDONLY | O_DIRECTORY); + event_set[tst_count] = FAN_OPEN; + tst_count++; - SAFE_CLOSE(cleanup, fd); - event_set[tst_count] = FAN_CLOSE_NOWRITE; - tst_count++; + SAFE_CLOSE(fd); + event_set[tst_count] = FAN_CLOSE_NOWRITE; + tst_count++; - /* - * Check events got generated only for the directory - */ - ret = SAFE_READ(cleanup, 0, fd_notify, event_buf + len, - EVENT_BUF_LEN - len); - len += ret; + /* + * Check events got generated only for the directory + */ + ret = SAFE_READ(0, fd_notify, event_buf + len, + EVENT_BUF_LEN - len); + len += ret; - if (TST_TOTAL != tst_count) { - tst_brkm(TBROK, cleanup, - "TST_TOTAL and tst_count are not equal"); + if (TST_TOTAL != tst_count) { + tst_brk(TBROK, + "TST_TOTAL and tst_count are not equal"); + } + tst_count = 0; + + /* + * check events + */ + while (i < len) { + struct fanotify_event_metadata *event; + + event = (struct fanotify_event_metadata *)&event_buf[i]; + if (test_num >= TST_TOTAL) { + tst_res(TFAIL, + "get unnecessary event: mask=%llx " + "pid=%u fd=%u", + (unsigned long long)event->mask, + (unsigned)event->pid, event->fd); + } else if (!(event->mask & event_set[test_num])) { + tst_res(TFAIL, + "get event: mask=%llx (expected %llx) " + "pid=%u fd=%u", + (unsigned long long)event->mask, + event_set[test_num], + (unsigned)event->pid, event->fd); + } else if (event->pid != getpid()) { + tst_res(TFAIL, + "get event: mask=%llx pid=%u " + "(expected %u) fd=%u", + (unsigned long long)event->mask, + (unsigned)event->pid, + (unsigned)getpid(), + event->fd); + } else { + tst_res(TPASS, + "get event: mask=%llx pid=%u fd=%u", + (unsigned long long)event->mask, + (unsigned)event->pid, event->fd); } - tst_count = 0; - - /* - * check events - */ - while (i < len) { - struct fanotify_event_metadata *event; - - event = (struct fanotify_event_metadata *)&event_buf[i]; - if (test_num >= TST_TOTAL) { - tst_resm(TFAIL, - "get unnecessary event: mask=%llx " - "pid=%u fd=%u", - (unsigned long long)event->mask, - (unsigned)event->pid, event->fd); - } else if (!(event->mask & event_set[test_num])) { - tst_resm(TFAIL, - "get event: mask=%llx (expected %llx) " - "pid=%u fd=%u", - (unsigned long long)event->mask, - event_set[test_num], - (unsigned)event->pid, event->fd); - } else if (event->pid != getpid()) { - tst_resm(TFAIL, - "get event: mask=%llx pid=%u " - "(expected %u) fd=%u", - (unsigned long long)event->mask, - (unsigned)event->pid, - (unsigned)getpid(), - event->fd); - } else { - tst_resm(TPASS, - "get event: mask=%llx pid=%u fd=%u", - (unsigned long long)event->mask, - (unsigned)event->pid, event->fd); - } - event->mask &= ~event_set[test_num]; - /* No events left in current mask? Go for next event */ - if (event->mask == 0) { - i += event->event_len; - close(event->fd); - } - test_num++; - } - for (; test_num < TST_TOTAL; test_num++) { - tst_resm(TFAIL, "didn't get event: mask=%llx", - event_set[test_num]); - + event->mask &= ~event_set[test_num]; + /* No events left in current mask? Go for next event */ + if (event->mask == 0) { + i += event->event_len; + close(event->fd); } + test_num++; } + for (; test_num < TST_TOTAL; test_num++) { + tst_res(TFAIL, "didn't get event: mask=%llx", + event_set[test_num]); - cleanup(); - tst_exit(); + } } static void setup(void) { - tst_sig(NOFORK, DEF_HANDLER, cleanup); - - TEST_PAUSE; - - tst_tmpdir(); sprintf(fname, "fname_%d", getpid()); - - if ((fd_notify = fanotify_init(FAN_CLASS_NOTIF, O_RDONLY)) < 0) { - if (errno == ENOSYS) { - tst_brkm(TCONF, cleanup, - "fanotify is not configured in this kernel."); - } else { - tst_brkm(TBROK | TERRNO, cleanup, - "fanotify_init failed"); - } - } + fd_notify = SAFE_FANOTIFY_INIT(FAN_CLASS_NOTIF, O_RDONLY); } static void cleanup(void) { - if (fd_notify > 0 && close(fd_notify)) - tst_resm(TWARN | TERRNO, "close(%d) failed", fd_notify); - - tst_rmdir(); + if (fd_notify > 0) + SAFE_CLOSE(fd_notify); } -#else - -int main(void) -{ - tst_brkm(TCONF, NULL, "system doesn't have required fanotify support"); -} +static struct tst_test test = { + .test_all = test01, + .setup = setup, + .cleanup = cleanup, + .needs_tmpdir = 1, + .needs_root = 1 +}; +#else + TST_TEST_TCONF("system doesn't have required fanotify support"); #endif diff --git a/testcases/kernel/syscalls/fanotify/fanotify03.c b/testcases/kernel/syscalls/fanotify/fanotify03.c index 2aca2e1..ce35661 100644 --- a/testcases/kernel/syscalls/fanotify/fanotify03.c +++ b/testcases/kernel/syscalls/fanotify/fanotify03.c @@ -38,13 +38,8 @@ #include <string.h> #include <signal.h> #include <sys/syscall.h> -#include "test.h" -#include "lapi/syscalls.h" +#include "tst_test.h" #include "fanotify.h" -#include "safe_macros.h" - -char *TCID = "fanotify03"; -int TST_TOTAL = 3; #if defined(HAVE_SYS_FANOTIFY_H) #include <sys/fanotify.h> @@ -55,10 +50,9 @@ int TST_TOTAL = 3; /* reasonable guess as to size of 1024 events */ #define EVENT_BUF_LEN (EVENT_MAX * EVENT_SIZE) -static void setup(void); -static void cleanup(void); - #define BUF_SIZE 256 +#define TST_TOTAL 3 + static char fname[BUF_SIZE]; static char buf[BUF_SIZE]; static volatile int fd_notify; @@ -109,7 +103,7 @@ static void run_child(void) child_action.sa_flags = SA_NOCLDSTOP; if (sigaction(SIGCHLD, &child_action, NULL) < 0) { - tst_brkm(TBROK | TERRNO, cleanup, + tst_brk(TBROK | TERRNO, "sigaction(SIGCHLD, &child_action, NULL) failed"); } @@ -120,7 +114,7 @@ static void run_child(void) generate_events(); exit(0); case -1: - tst_brkm(TBROK | TERRNO, cleanup, "fork() failed"); + tst_brk(TBROK | TERRNO, "fork() failed"); } } @@ -133,171 +127,148 @@ static void check_child(void) sigemptyset(&child_action.sa_mask); child_action.sa_flags = SA_NOCLDSTOP; if (sigaction(SIGCHLD, &child_action, NULL) < 0) { - tst_brkm(TBROK | TERRNO, cleanup, + tst_brk(TBROK | TERRNO, "sigaction(SIGCHLD, &child_action, NULL) failed"); } if (waitpid(-1, &child_ret, 0) < 0) { - tst_brkm(TBROK | TERRNO, cleanup, + tst_brk(TBROK | TERRNO, "waitpid(-1, &child_ret, 0) failed"); } if (WIFSIGNALED(child_ret)) { - tst_resm(TFAIL, "child exited due to signal %d", + tst_res(TFAIL, "child exited due to signal %d", WTERMSIG(child_ret)); } else if (WIFEXITED(child_ret)) { if (WEXITSTATUS(child_ret) == 0) - tst_resm(TPASS, "child exited correctly"); + tst_res(TPASS, "child exited correctly"); else - tst_resm(TFAIL, "child exited with status %d", + tst_res(TFAIL, "child exited with status %d", WEXITSTATUS(child_ret)); } else { - tst_resm(TFAIL, "child exited for unknown reason (status %d)", + tst_res(TFAIL, "child exited for unknown reason (status %d)", child_ret); } } -int main(int ac, char **av) +void test01(void) { - int lc; - int fd_notify_backup = -1; - - tst_parse_opts(ac, av, NULL, NULL); + int tst_count, fd_notify_backup = -1; - setup(); + int ret, len = 0, i = 0, test_num = 0; - for (lc = 0; TEST_LOOPING(lc); lc++) { - int ret, len = 0, i = 0, test_num = 0; - - if (fd_notify_backup == -1) { - fd_notify_backup = dup(fd_notify); - if (fd_notify_backup < 0) - tst_brkm(TBROK | TERRNO, cleanup, - "dup(%d) failed", fd_notify); - } - run_child(); + if (fd_notify_backup == -1) { + fd_notify_backup = dup(fd_notify); + if (fd_notify_backup < 0) + tst_brk(TBROK | TERRNO, + "dup(%d) failed", fd_notify); + } + run_child(); - tst_count = 0; + tst_count = 0; - event_set[tst_count] = FAN_OPEN_PERM; - event_resp[tst_count++] = FAN_ALLOW; - event_set[tst_count] = FAN_ACCESS_PERM; - event_resp[tst_count++] = FAN_DENY; + event_set[tst_count] = FAN_OPEN_PERM; + event_resp[tst_count++] = FAN_ALLOW; + event_set[tst_count] = FAN_ACCESS_PERM; + event_resp[tst_count++] = FAN_DENY; - /* tst_count + 1 is for checking child return value */ - if (TST_TOTAL != tst_count + 1) { - tst_brkm(TBROK, cleanup, - "TST_TOTAL and tst_count do not match"); - } - tst_count = 0; - - /* - * check events - */ - while (test_num < TST_TOTAL && fd_notify != -1) { - struct fanotify_event_metadata *event; - - if (i == len) { - /* Get more events */ - ret = read(fd_notify, event_buf + len, - EVENT_BUF_LEN - len); - if (fd_notify == -1) - break; - if (ret < 0) { - tst_brkm(TBROK, cleanup, - "read(%d, buf, %zu) failed", - fd_notify, EVENT_BUF_LEN); - } - len += ret; - } + /* tst_count + 1 is for checking child return value */ + if (TST_TOTAL != tst_count + 1) { + tst_brk(TBROK, + "TST_TOTAL and tst_count do not match"); + } + tst_count = 0; - event = (struct fanotify_event_metadata *)&event_buf[i]; - if (!(event->mask & event_set[test_num])) { - tst_resm(TFAIL, - "get event: mask=%llx (expected %llx) " - "pid=%u fd=%u", - (unsigned long long)event->mask, - event_set[test_num], - (unsigned)event->pid, event->fd); - } else if (event->pid != child_pid) { - tst_resm(TFAIL, - "get event: mask=%llx pid=%u " - "(expected %u) fd=%u", - (unsigned long long)event->mask, - (unsigned)event->pid, - (unsigned)child_pid, - event->fd); - } else { - tst_resm(TPASS, - "get event: mask=%llx pid=%u fd=%u", - (unsigned long long)event->mask, - (unsigned)event->pid, event->fd); - } - /* Write response to permission event */ - if (event_set[test_num] & FAN_ALL_PERM_EVENTS) { - struct fanotify_response resp; - - resp.fd = event->fd; - resp.response = event_resp[test_num]; - SAFE_WRITE(cleanup, 1, fd_notify, &resp, - sizeof(resp)); - } - event->mask &= ~event_set[test_num]; - /* No events left in current mask? Go for next event */ - if (event->mask == 0) { - i += event->event_len; - close(event->fd); + /* + * check events + */ + while (test_num < TST_TOTAL && fd_notify != -1) { + struct fanotify_event_metadata *event; + + if (i == len) { + /* Get more events */ + ret = read(fd_notify, event_buf + len, + EVENT_BUF_LEN - len); + if (fd_notify == -1) + break; + if (ret < 0) { + tst_brk(TBROK, + "read(%d, buf, %zu) failed", + fd_notify, EVENT_BUF_LEN); } - test_num++; + len += ret; } - for (; test_num < TST_TOTAL - 1; test_num++) { - tst_resm(TFAIL, "didn't get event: mask=%llx", - event_set[test_num]); + event = (struct fanotify_event_metadata *)&event_buf[i]; + if (!(event->mask & event_set[test_num])) { + tst_res(TFAIL, + "get event: mask=%llx (expected %llx) " + "pid=%u fd=%u", + (unsigned long long)event->mask, + event_set[test_num], + (unsigned)event->pid, event->fd); + } else if (event->pid != child_pid) { + tst_res(TFAIL, + "get event: mask=%llx pid=%u " + "(expected %u) fd=%u", + (unsigned long long)event->mask, + (unsigned)event->pid, + (unsigned)child_pid, + event->fd); + } else { + tst_res(TPASS, + "get event: mask=%llx pid=%u fd=%u", + (unsigned long long)event->mask, + (unsigned)event->pid, event->fd); } - check_child(); - /* We got SIGCHLD while running, resetup fd_notify */ - if (fd_notify == -1) { - fd_notify = fd_notify_backup; - fd_notify_backup = -1; + /* Write response to permission event */ + if (event_set[test_num] & FAN_ALL_PERM_EVENTS) { + struct fanotify_response resp; + + resp.fd = event->fd; + resp.response = event_resp[test_num]; + SAFE_WRITE(1, fd_notify, &resp, + sizeof(resp)); } + event->mask &= ~event_set[test_num]; + /* No events left in current mask? Go for next event */ + if (event->mask == 0) { + i += event->event_len; + close(event->fd); + } + test_num++; } + for (; test_num < TST_TOTAL - 1; test_num++) { + tst_res(TFAIL, "didn't get event: mask=%llx", + event_set[test_num]); - cleanup(); - tst_exit(); + } + check_child(); + /* We got SIGCHLD while running, resetup fd_notify */ + if (fd_notify == -1) { + fd_notify = fd_notify_backup; + fd_notify_backup = -1; + } } static void setup(void) { int fd; - tst_sig(FORK, DEF_HANDLER, cleanup); - - TEST_PAUSE; - - tst_tmpdir(); sprintf(fname, "fname_%d", getpid()); - fd = SAFE_OPEN(cleanup, fname, O_CREAT | O_RDWR, 0644); - SAFE_WRITE(cleanup, 1, fd, fname, 1); - SAFE_CLOSE(cleanup, fd); - - if ((fd_notify = fanotify_init(FAN_CLASS_CONTENT, O_RDONLY)) < 0) { - if (errno == ENOSYS) { - tst_brkm(TCONF, cleanup, - "fanotify is not configured in this kernel."); - } else { - tst_brkm(TBROK | TERRNO, cleanup, - "fanotify_init failed"); - } - } + fd = SAFE_OPEN(fname, O_CREAT | O_RDWR, 0644); + SAFE_WRITE(1, fd, fname, 1); + SAFE_CLOSE(fd); + + fd_notify = SAFE_FANOTIFY_INIT(FAN_CLASS_CONTENT, O_RDONLY); if (fanotify_mark(fd_notify, FAN_MARK_ADD, FAN_ACCESS_PERM | FAN_OPEN_PERM, AT_FDCWD, fname) < 0) { if (errno == EINVAL) { - tst_brkm(TCONF | TERRNO, cleanup, + tst_brk(TCONF | TERRNO, "CONFIG_FANOTIFY_ACCESS_PERMISSIONS not " "configured in kernel?"); } else { - tst_brkm(TBROK | TERRNO, cleanup, + tst_brk(TBROK | TERRNO, "fanotify_mark (%d, FAN_MARK_ADD, FAN_ACCESS_PERM | " "FAN_OPEN_PERM, AT_FDCWD, %s) failed.", fd_notify, fname); } @@ -307,17 +278,18 @@ static void setup(void) static void cleanup(void) { - if (fd_notify > 0 && close(fd_notify)) - tst_resm(TWARN | TERRNO, "close(%d) failed", fd_notify); - - tst_rmdir(); + if (fd_notify > 0) + SAFE_CLOSE(fd_notify); } -#else - -int main(void) -{ - tst_brkm(TCONF, NULL, "system doesn't have required fanotify support"); -} +static struct tst_test test = { + .test_all = test01, + .setup = setup, + .cleanup = cleanup, + .needs_tmpdir = 1, + .needs_root = 1 +}; +#else + TST_TEST_TCONF("system doesn't have required fanotify support"); #endif diff --git a/testcases/kernel/syscalls/fanotify/fanotify04.c b/testcases/kernel/syscalls/fanotify/fanotify04.c index dca52f0..302f124 100644 --- a/testcases/kernel/syscalls/fanotify/fanotify04.c +++ b/testcases/kernel/syscalls/fanotify/fanotify04.c @@ -36,13 +36,8 @@ #include <errno.h> #include <string.h> #include <sys/syscall.h> -#include "test.h" -#include "lapi/syscalls.h" +#include "tst_test.h" #include "fanotify.h" -#include "safe_macros.h" - -char *TCID = "fanotify04"; -int TST_TOTAL = 9; #if defined(HAVE_SYS_FANOTIFY_H) #include <sys/fanotify.h> @@ -53,10 +48,9 @@ int TST_TOTAL = 9; /* reasonable guess as to size of 1024 events */ #define EVENT_BUF_LEN (EVENT_MAX * EVENT_SIZE) -static void setup(void); -static void cleanup(void); - #define BUF_SIZE 256 +#define TST_TOTAL 9 + static char fname[BUF_SIZE]; static char sname[BUF_SIZE]; static char dir[BUF_SIZE]; @@ -84,11 +78,11 @@ static void check_mark(char *file, unsigned long long flag, char *flagstr, { if (fanotify_mark(fd_notify, FAN_MARK_ADD | flag, FAN_OPEN, AT_FDCWD, file) != expect) { - tst_resm(TFAIL, + tst_res(TFAIL, "fanotify_mark (%d, FAN_MARK_ADD | %s, FAN_OPEN, AT_FDCWD, " "'%s') %s", fd_notify, flagstr, file, expect_str_fail(expect)); } else { - tst_resm(TPASS, + tst_res(TPASS, "fanotify_mark (%d, FAN_MARK_ADD | %s, FAN_OPEN, AT_FDCWD, " "'%s') %s", fd_notify, flagstr, file, expect_str_pass(expect)); @@ -101,7 +95,7 @@ static void check_mark(char *file, unsigned long long flag, char *flagstr, if (fanotify_mark(fd_notify, FAN_MARK_REMOVE | flag, FAN_OPEN, AT_FDCWD, file) < 0) { - tst_brkm(TBROK | TERRNO, cleanup, + tst_brk(TBROK | TERRNO, "fanotify_mark (%d, FAN_MARK_REMOVE | %s, " "FAN_OPEN, AT_FDCWD, '%s') failed", fd_notify, flagstr, file); @@ -115,8 +109,8 @@ static void do_open(char *file, int flag, char *flagstr) { int fd; - fd = SAFE_OPEN(cleanup, file, O_RDONLY | flag); - SAFE_CLOSE(cleanup, fd); + fd = SAFE_OPEN(file, O_RDONLY | flag); + SAFE_CLOSE(fd); } #define DO_OPEN(file, flag) do_open(file, flag, #flag) @@ -138,22 +132,22 @@ static void verify_event(int mask) struct stat st; /* Read the event */ - ret = SAFE_READ(cleanup, 0, fd_notify, event_buf + len, + ret = SAFE_READ(0, fd_notify, event_buf + len, EVENT_BUF_LEN - len); event = (struct fanotify_event_metadata *)&event_buf[len]; len += ret; if (event->mask != FAN_OPEN) { - tst_resm(TFAIL, "got unexpected event %llx", + tst_res(TFAIL, "got unexpected event %llx", (unsigned long long)event->mask); } else if (fstat(event->fd, &st) < 0) { - tst_resm(TFAIL, "failed to stat event->fd (%s)", + tst_res(TFAIL, "failed to stat event->fd (%s)", strerror(errno)); } else if ((st.st_mode & S_IFMT) != mask) { - tst_resm(TFAIL, "event->fd points to object of different type " + tst_res(TFAIL, "event->fd points to object of different type " "(%o != %o)", st.st_mode & S_IFMT, mask); } else { - tst_resm(TPASS, "event generated properly for type %o", mask); + tst_res(TPASS, "event generated properly for type %o", mask); } close(event->fd); } @@ -181,15 +175,15 @@ static void verify_no_event(void) struct fanotify_event_metadata *event; event = (struct fanotify_event_metadata *)&event_buf[len]; - tst_resm(TFAIL, "seen unexpected event (mask %llx)", + tst_res(TFAIL, "seen unexpected event (mask %llx)", (unsigned long long)event->mask); /* Cleanup fd from the event */ close(event->fd); } else if (errno != EAGAIN) { - tst_resm(TFAIL | TERRNO, "read(%d, buf, %zu) failed", fd_notify, + tst_res(TFAIL | TERRNO, "read(%d, buf, %zu) failed", fd_notify, EVENT_BUF_LEN); } else { - tst_resm(TPASS, "No event as expected"); + tst_res(TPASS, "No event as expected"); } } @@ -200,104 +194,81 @@ static void test_open_symlink(char *file) verify_no_event(); } -int main(int ac, char **av) +void test01(void) { - int lc; - - tst_parse_opts(ac, av, NULL, NULL); - - setup(); - - for (lc = 0; TEST_LOOPING(lc); lc++) { - /* Check ONLYDIR on a directory */ - CHECK_MARK(".", FAN_MARK_ONLYDIR, 0, NULL); + /* Check ONLYDIR on a directory */ + CHECK_MARK(".", FAN_MARK_ONLYDIR, 0, NULL); - /* Check ONLYDIR without a directory */ - CHECK_MARK(fname, FAN_MARK_ONLYDIR, -1, NULL); + /* Check ONLYDIR without a directory */ + CHECK_MARK(fname, FAN_MARK_ONLYDIR, -1, NULL); - /* Check DONT_FOLLOW for a symlink */ - CHECK_MARK(sname, FAN_MARK_DONT_FOLLOW, 0, test_open_symlink); + /* Check DONT_FOLLOW for a symlink */ + CHECK_MARK(sname, FAN_MARK_DONT_FOLLOW, 0, test_open_symlink); - /* Check without DONT_FOLLOW for a symlink */ - CHECK_MARK(sname, 0, 0, test_open_file); + /* Check without DONT_FOLLOW for a symlink */ + CHECK_MARK(sname, 0, 0, test_open_file); - /* Verify FAN_MARK_FLUSH destroys all inode marks */ - if (fanotify_mark(fd_notify, FAN_MARK_ADD, - FAN_OPEN, AT_FDCWD, fname) < 0) { - tst_brkm(TBROK | TERRNO, cleanup, - "fanotify_mark (%d, FAN_MARK_ADD, FAN_OPEN, " - "AT_FDCWD, '%s') failed", fd_notify, fname); - } - if (fanotify_mark(fd_notify, FAN_MARK_ADD, - FAN_OPEN | FAN_ONDIR, AT_FDCWD, dir) < 0) { - tst_brkm(TBROK | TERRNO, cleanup, - "fanotify_mark (%d, FAN_MARK_ADD, FAN_OPEN | " - "FAN_ONDIR, AT_FDCWD, '%s') failed", fd_notify, - dir); - } - open_file(fname); - verify_event(S_IFREG); - open_dir(dir); - verify_event(S_IFDIR); - if (fanotify_mark(fd_notify, FAN_MARK_FLUSH, - 0, AT_FDCWD, ".") < 0) { - tst_brkm(TBROK | TERRNO, cleanup, - "fanotify_mark (%d, FAN_MARK_FLUSH, 0, " - "AT_FDCWD, '.') failed", fd_notify); - } - - open_dir(dir); - verify_no_event(); + /* Verify FAN_MARK_FLUSH destroys all inode marks */ + if (fanotify_mark(fd_notify, FAN_MARK_ADD, + FAN_OPEN, AT_FDCWD, fname) < 0) { + tst_brk(TBROK | TERRNO, + "fanotify_mark (%d, FAN_MARK_ADD, FAN_OPEN, " + "AT_FDCWD, '%s') failed", fd_notify, fname); + } + if (fanotify_mark(fd_notify, FAN_MARK_ADD, + FAN_OPEN | FAN_ONDIR, AT_FDCWD, dir) < 0) { + tst_brk(TBROK | TERRNO, + "fanotify_mark (%d, FAN_MARK_ADD, FAN_OPEN | " + "FAN_ONDIR, AT_FDCWD, '%s') failed", fd_notify, + dir); + } + open_file(fname); + verify_event(S_IFREG); + open_dir(dir); + verify_event(S_IFDIR); + if (fanotify_mark(fd_notify, FAN_MARK_FLUSH, + 0, AT_FDCWD, ".") < 0) { + tst_brk(TBROK | TERRNO, + "fanotify_mark (%d, FAN_MARK_FLUSH, 0, " + "AT_FDCWD, '.') failed", fd_notify); } - cleanup(); - tst_exit(); + open_dir(dir); + verify_no_event(); } static void setup(void) { int fd; - tst_sig(NOFORK, DEF_HANDLER, cleanup); - - TEST_PAUSE; - - tst_tmpdir(); sprintf(fname, "fname_%d", getpid()); - fd = SAFE_OPEN(cleanup, fname, O_RDWR | O_CREAT, 0644); - SAFE_CLOSE(cleanup, fd); + fd = SAFE_OPEN(fname, O_RDWR | O_CREAT, 0644); + SAFE_CLOSE(fd); sprintf(sname, "symlink_%d", getpid()); - SAFE_SYMLINK(cleanup, fname, sname); + SAFE_SYMLINK(fname, sname); sprintf(dir, "dir_%d", getpid()); - SAFE_MKDIR(cleanup, dir, 0755); - - if ((fd_notify = fanotify_init(FAN_CLASS_NOTIF | FAN_NONBLOCK, - O_RDONLY)) < 0) { - if (errno == ENOSYS) { - tst_brkm(TCONF, cleanup, - "fanotify is not configured in this kernel."); - } else { - tst_brkm(TBROK | TERRNO, cleanup, - "fanotify_init failed"); - } - } + SAFE_MKDIR(dir, 0755); + + fd_notify = SAFE_FANOTIFY_INIT(FAN_CLASS_NOTIF | FAN_NONBLOCK, + O_RDONLY); } static void cleanup(void) { - if (fd_notify > 0 && close(fd_notify)) - tst_resm(TWARN | TERRNO, "close(%d) failed", fd_notify); - - tst_rmdir(); + if (fd_notify > 0) + SAFE_CLOSE(fd_notify); } -#else - -int main(void) -{ - tst_brkm(TCONF, NULL, "system doesn't have required fanotify support"); -} +static struct tst_test test = { + .test_all = test01, + .setup = setup, + .cleanup = cleanup, + .needs_tmpdir = 1, + .needs_root = 1 +}; +#else + TST_TEST_TCONF("system doesn't have required fanotify support"); #endif diff --git a/testcases/kernel/syscalls/fanotify/fanotify05.c b/testcases/kernel/syscalls/fanotify/fanotify05.c index 6063764..ec615e6 100644 --- a/testcases/kernel/syscalls/fanotify/fanotify05.c +++ b/testcases/kernel/syscalls/fanotify/fanotify05.c @@ -34,13 +34,8 @@ #include <errno.h> #include <string.h> #include <sys/syscall.h> -#include "test.h" -#include "lapi/syscalls.h" +#include "tst_test.h" #include "fanotify.h" -#include "safe_macros.h" - -char *TCID = "fanotify05"; -int TST_TOTAL = 1; #if defined(HAVE_SYS_FANOTIFY_H) #include <sys/fanotify.h> @@ -48,110 +43,84 @@ int TST_TOTAL = 1; /* Currently this is fixed in kernel... */ #define MAX_EVENTS 16384 -static void setup(void); -static void cleanup(void); - #define BUF_SIZE 256 static char fname[BUF_SIZE]; static int fd, fd_notify; struct fanotify_event_metadata event; -int main(int ac, char **av) +void test01(void) { - int lc, i; + int i; int len; - tst_parse_opts(ac, av, NULL, NULL); - - setup(); + /* + * generate events + */ + for (i = 0; i < MAX_EVENTS + 1; i++) { + sprintf(fname, "fname_%d", i); + fd = SAFE_OPEN(fname, O_RDWR | O_CREAT, 0644); + SAFE_CLOSE(fd); + } - for (lc = 0; TEST_LOOPING(lc); lc++) { + while (1) { /* - * generate events + * get list on events */ - for (i = 0; i < MAX_EVENTS + 1; i++) { - sprintf(fname, "fname_%d", i); - fd = SAFE_OPEN(cleanup, fname, O_RDWR | O_CREAT, 0644); - SAFE_CLOSE(cleanup, fd); - } - - while (1) { - /* - * get list on events - */ - len = read(fd_notify, &event, sizeof(event)); - if (len < 0) { - if (errno == -EAGAIN) { - tst_resm(TFAIL, "Overflow event not " - "generated!\n"); - break; - } - tst_brkm(TBROK | TERRNO, cleanup, - "read of notification event failed"); + len = read(fd_notify, &event, sizeof(event)); + if (len < 0) { + if (errno == -EAGAIN) { + tst_res(TFAIL, "Overflow event not " + "generated!\n"); break; } - if (event.fd != FAN_NOFD) - close(event.fd); + tst_brk(TBROK | TERRNO, + "read of notification event failed"); + break; + } + if (event.fd != FAN_NOFD) + close(event.fd); - /* - * check events - */ - if (event.mask != FAN_OPEN && - event.mask != FAN_Q_OVERFLOW) { - tst_resm(TFAIL, - "get event: mask=%llx (expected %llx)" - "pid=%u fd=%d", + /* + * check events + */ + if (event.mask != FAN_OPEN && + event.mask != FAN_Q_OVERFLOW) { + tst_res(TFAIL, + "get event: mask=%llx (expected %llx)" + "pid=%u fd=%d", + (unsigned long long)event.mask, + (unsigned long long)FAN_OPEN, + (unsigned)event.pid, event.fd); + break; + } + if (event.mask == FAN_Q_OVERFLOW) { + if (event.fd != FAN_NOFD) { + tst_res(TFAIL, + "invalid overflow event: " + "mask=%llx pid=%u fd=%d", (unsigned long long)event.mask, - (unsigned long long)FAN_OPEN, - (unsigned)event.pid, event.fd); + (unsigned)event.pid, + event.fd); break; } - if (event.mask == FAN_Q_OVERFLOW) { - if (event.fd != FAN_NOFD) { - tst_resm(TFAIL, - "invalid overflow event: " - "mask=%llx pid=%u fd=%d", - (unsigned long long)event.mask, - (unsigned)event.pid, - event.fd); - break; - } - tst_resm(TPASS, - "get event: mask=%llx pid=%u fd=%d", - (unsigned long long)event.mask, - (unsigned)event.pid, event.fd); - break; - } + tst_res(TPASS, + "get event: mask=%llx pid=%u fd=%d", + (unsigned long long)event.mask, + (unsigned)event.pid, event.fd); + break; } } - - cleanup(); - tst_exit(); } static void setup(void) { - tst_sig(NOFORK, DEF_HANDLER, cleanup); - - TEST_PAUSE; - - tst_tmpdir(); - - fd_notify = fanotify_init(FAN_CLASS_NOTIF | FAN_NONBLOCK, O_RDONLY); - if (fd_notify < 0) { - if (errno == ENOSYS) { - tst_brkm(TCONF, cleanup, - "fanotify is not configured in this kernel."); - } else { - tst_brkm(TBROK | TERRNO, cleanup, - "fanotify_init failed"); - } - } + fd_notify = SAFE_FANOTIFY_INIT(FAN_CLASS_NOTIF | FAN_NONBLOCK, + O_RDONLY); if (fanotify_mark(fd_notify, FAN_MARK_MOUNT | FAN_MARK_ADD, FAN_OPEN, AT_FDCWD, ".") < 0) { - tst_brkm(TBROK | TERRNO, cleanup, + tst_brk(TBROK | TERRNO, "fanotify_mark (%d, FAN_MARK_MOUNT | FAN_MARK_ADD, " "FAN_OPEN, AT_FDCWD, \".\") failed", fd_notify); @@ -160,17 +129,17 @@ static void setup(void) static void cleanup(void) { - if (fd_notify > 0 && close(fd_notify)) - tst_resm(TWARN | TERRNO, "close(%d) failed", fd_notify); - - tst_rmdir(); + if (fd_notify > 0) + SAFE_CLOSE(fd_notify); } +static struct tst_test test = { + .test_all = test01, + .setup = setup, + .cleanup = cleanup, + .needs_root = 1, + .needs_tmpdir = 1, +}; #else - -int main(void) -{ - tst_brkm(TCONF, NULL, "system doesn't have required fanotify support"); -} - + TST_TEST_TCONF("system doesn't have required fanotify support"); #endif diff --git a/testcases/kernel/syscalls/fanotify/fanotify06.c b/testcases/kernel/syscalls/fanotify/fanotify06.c index d7fef6d..7b9d632 100644 --- a/testcases/kernel/syscalls/fanotify/fanotify06.c +++ b/testcases/kernel/syscalls/fanotify/fanotify06.c @@ -34,6 +34,7 @@ * * fanotify: fix notification of groups with inode & mount marks */ +#define _GNU_SOURCE #include "config.h" #include <stdio.h> @@ -44,12 +45,8 @@ #include <string.h> #include <sys/mount.h> #include <sys/syscall.h> -#include "test.h" -#include "lapi/syscalls.h" +#include "tst_test.h" #include "fanotify.h" -#include "safe_macros.h" - -char *TCID = "fanotify06"; #if defined(HAVE_SYS_FANOTIFY_H) #include <sys/fanotify.h> @@ -72,8 +69,6 @@ unsigned int fanotify_prio[] = { #define GROUPS_PER_PRIO 3 -int TST_TOTAL = GROUPS_PER_PRIO * FANOTIFY_PRIORITIES; - #define BUF_SIZE 256 static char fname[BUF_SIZE]; static int fd; @@ -91,26 +86,17 @@ static void create_fanotify_groups(void) for (p = 0; p < FANOTIFY_PRIORITIES; p++) { for (i = 0; i < GROUPS_PER_PRIO; i++) { - fd_notify[p][i] = fanotify_init(fanotify_prio[p] | + fd_notify[p][i] = SAFE_FANOTIFY_INIT(fanotify_prio[p] | FAN_NONBLOCK, O_RDONLY); - if (fd_notify[p][i] < 0) { - if (errno == ENOSYS) { - tst_brkm(TCONF, cleanup, - "fanotify is not configured in" - " this kernel."); - } else { - tst_brkm(TBROK | TERRNO, cleanup, - "fanotify_init failed"); - } - } + /* Add mount mark for each group */ ret = fanotify_mark(fd_notify[p][i], FAN_MARK_ADD | FAN_MARK_MOUNT, FAN_MODIFY, AT_FDCWD, "."); if (ret < 0) { - tst_brkm(TBROK | TERRNO, cleanup, + tst_brk(TBROK | TERRNO, "fanotify_mark(%d, FAN_MARK_ADD | " "FAN_MARK_MOUNT, FAN_MODIFY, AT_FDCWD," " '.') failed", fd_notify[p][i]); @@ -124,7 +110,7 @@ static void create_fanotify_groups(void) FAN_MARK_IGNORED_SURV_MODIFY, FAN_MODIFY, AT_FDCWD, fname); if (ret < 0) { - tst_brkm(TBROK | TERRNO, cleanup, + tst_brk(TBROK | TERRNO, "fanotify_mark(%d, FAN_MARK_ADD | " "FAN_MARK_IGNORED_MASK | " "FAN_MARK_IGNORED_SURV_MODIFY, " @@ -143,7 +129,7 @@ static void cleanup_fanotify_groups(void) for (i = 0; i < GROUPS_PER_PRIO; i++) { if (fd_notify[p][i] && fd_notify[p][i] != -1) { if (close(fd_notify[p][i]) == -1) - tst_resm(TWARN, "close(%d) failed", + tst_res(TWARN, "close(%d) failed", fd_notify[p][i]); fd_notify[p][i] = 0; } @@ -154,115 +140,97 @@ static void cleanup_fanotify_groups(void) static void verify_event(int group, struct fanotify_event_metadata *event) { if (event->mask != FAN_MODIFY) { - tst_resm(TFAIL, "group %d get event: mask %llx (expected %llx) " + tst_res(TFAIL, "group %d get event: mask %llx (expected %llx) " "pid=%u fd=%u", group, (unsigned long long)event->mask, (unsigned long long)FAN_MODIFY, (unsigned)event->pid, event->fd); } else if (event->pid != getpid()) { - tst_resm(TFAIL, "group %d get event: mask %llx pid=%u " + tst_res(TFAIL, "group %d get event: mask %llx pid=%u " "(expected %u) fd=%u", group, (unsigned long long)event->mask, (unsigned)event->pid, (unsigned)getpid(), event->fd); } else { - tst_resm(TPASS, "group %d get event: mask %llx pid=%u fd=%u", + tst_res(TPASS, "group %d get event: mask %llx pid=%u fd=%u", group, (unsigned long long)event->mask, (unsigned)event->pid, event->fd); } } -int main(int ac, char **av) +void test01(void) { - int lc; - - tst_parse_opts(ac, av, NULL, NULL); - - setup(); - - for (lc = 0; TEST_LOOPING(lc); lc++) { - int ret; - unsigned int p, i; - struct fanotify_event_metadata *event; - - create_fanotify_groups(); - - /* - * generate sequence of events - */ - fd = SAFE_OPEN(cleanup, fname, O_RDWR); - SAFE_WRITE(cleanup, 1, fd, fname, strlen(fname)); - SAFE_CLOSE(cleanup, fd); - - /* First verify all groups without ignore mask got the event */ - for (i = 0; i < GROUPS_PER_PRIO; i++) { - ret = read(fd_notify[0][i], event_buf, EVENT_BUF_LEN); - if (ret < 0) { - if (errno == EAGAIN) { - tst_resm(TFAIL, "group %d did not get " - "event", i); - } - tst_brkm(TBROK | TERRNO, cleanup, - "reading fanotify events failed"); - } - if (ret < (int)FAN_EVENT_METADATA_LEN) { - tst_brkm(TBROK, cleanup, - "short read when reading fanotify " - "events (%d < %d)", ret, - (int)EVENT_BUF_LEN); + int ret; + unsigned int p, i; + struct fanotify_event_metadata *event; + + create_fanotify_groups(); + + /* + * generate sequence of events + */ + fd = SAFE_OPEN(fname, O_RDWR); + SAFE_WRITE(1, fd, fname, strlen(fname)); + SAFE_CLOSE(fd); + + /* First verify all groups without ignore mask got the event */ + for (i = 0; i < GROUPS_PER_PRIO; i++) { + ret = read(fd_notify[0][i], event_buf, EVENT_BUF_LEN); + if (ret < 0) { + if (errno == EAGAIN) { + tst_res(TFAIL, "group %d did not get " + "event", i); } - event = (struct fanotify_event_metadata *)event_buf; - if (ret > (int)event->event_len) { - tst_resm(TFAIL, "group %d got more than one " - "event (%d > %d)", i, ret, - event->event_len); - } else - verify_event(i, event); - close(event->fd); + tst_brk(TBROK | TERRNO, + "reading fanotify events failed"); } - for (p = 1; p < FANOTIFY_PRIORITIES; p++) { - for (i = 0; i < GROUPS_PER_PRIO; i++) { - ret = read(fd_notify[p][i], event_buf, EVENT_BUF_LEN); - if (ret > 0) { - tst_resm(TFAIL, "group %d got event", - p*GROUPS_PER_PRIO + i); - } else if (ret == 0) { - tst_brkm(TBROK, cleanup, "zero length " - "read from fanotify fd"); - } else if (errno != EAGAIN) { - tst_brkm(TBROK | TERRNO, cleanup, - "reading fanotify events failed"); - } else { - tst_resm(TPASS, "group %d got no event", - p*GROUPS_PER_PRIO + i); - } + if (ret < (int)FAN_EVENT_METADATA_LEN) { + tst_brk(TBROK, + "short read when reading fanotify " + "events (%d < %d)", ret, + (int)EVENT_BUF_LEN); + } + event = (struct fanotify_event_metadata *)event_buf; + if (ret > (int)event->event_len) { + tst_res(TFAIL, "group %d got more than one " + "event (%d > %d)", i, ret, + event->event_len); + } else + verify_event(i, event); + close(event->fd); + } + for (p = 1; p < FANOTIFY_PRIORITIES; p++) { + for (i = 0; i < GROUPS_PER_PRIO; i++) { + ret = read(fd_notify[p][i], event_buf, EVENT_BUF_LEN); + if (ret > 0) { + tst_res(TFAIL, "group %d got event", + p*GROUPS_PER_PRIO + i); + } else if (ret == 0) { + tst_brk(TBROK, "zero length " + "read from fanotify fd"); + } else if (errno != EAGAIN) { + tst_brk(TBROK | TERRNO, + "reading fanotify events failed"); + } else { + tst_res(TPASS, "group %d got no event", + p*GROUPS_PER_PRIO + i); } } - cleanup_fanotify_groups(); } - - cleanup(); - tst_exit(); + cleanup_fanotify_groups(); } static void setup(void) { - tst_sig(NOFORK, DEF_HANDLER, cleanup); - - TEST_PAUSE; - - tst_require_root(); - tst_tmpdir(); - - SAFE_MKDIR(cleanup, MOUNT_NAME, 0755); - SAFE_MOUNT(cleanup, MOUNT_NAME, MOUNT_NAME, NULL, MS_BIND, NULL); + SAFE_MKDIR(MOUNT_NAME, 0755); + SAFE_MOUNT(MOUNT_NAME, MOUNT_NAME, NULL, MS_BIND, NULL); mount_created = 1; - SAFE_CHDIR(cleanup, MOUNT_NAME); + SAFE_CHDIR(MOUNT_NAME); sprintf(fname, "tfile_%d", getpid()); - fd = SAFE_OPEN(cleanup, fname, O_RDWR | O_CREAT, 0700); - SAFE_WRITE(cleanup, 1, fd, fname, 1); + fd = SAFE_OPEN(fname, O_RDWR | O_CREAT, 0700); + SAFE_WRITE(1, fd, fname, 1); /* close the file we have open */ - SAFE_CLOSE(cleanup, fd); + SAFE_CLOSE(fd); } static void cleanup(void) @@ -270,19 +238,20 @@ static void cleanup(void) cleanup_fanotify_groups(); if (chdir(tst_get_tmpdir()) < 0) - tst_brkm(TBROK, NULL, "chdir(tmpdir) failed"); + tst_brk(TBROK, "chdir(tmpdir) failed"); if (mount_created && tst_umount(MOUNT_NAME) < 0) - tst_brkm(TBROK | TERRNO, NULL, "umount failed"); - - tst_rmdir(); + tst_brk(TBROK | TERRNO, "umount failed"); } -#else - -int main(void) -{ - tst_brkm(TCONF, NULL, "system doesn't have required fanotify support"); -} +static struct tst_test test = { + .test_all = test01, + .setup = setup, + .cleanup = cleanup, + .needs_tmpdir = 1, + .needs_root = 1 +}; +#else + TST_TEST_TCONF("system doesn't have required fanotify support"); #endif diff --git a/testcases/kernel/syscalls/fanotify/fanotify07.c b/testcases/kernel/syscalls/fanotify/fanotify07.c index 54e5a1f..af95c39 100644 --- a/testcases/kernel/syscalls/fanotify/fanotify07.c +++ b/testcases/kernel/syscalls/fanotify/fanotify07.c @@ -119,14 +119,7 @@ static int setup_instance(void) { int fd; - if ((fd = fanotify_init(FAN_CLASS_CONTENT, O_RDONLY)) < 0) { - if (errno == ENOSYS) { - tst_brk(TCONF, - "fanotify is not configured in this kernel."); - } else { - tst_brk(TBROK | TERRNO, "fanotify_init failed"); - } - } + fd = SAFE_FANOTIFY_INIT(FAN_CLASS_CONTENT, O_RDONLY); if (fanotify_mark(fd, FAN_MARK_ADD, FAN_ACCESS_PERM, AT_FDCWD, fname) < 0) { -- 1.8.3.1 ^ permalink raw reply related [flat|nested] 20+ messages in thread
* [LTP] [PATCH v4 2/3] fanotify: rewrite old cases with new lib 2017-08-16 7:15 ` [LTP] [PATCH v4 2/3] fanotify: rewrite old cases with new lib Xiong Zhou @ 2017-08-18 10:24 ` Cyril Hrubis 0 siblings, 0 replies; 20+ messages in thread From: Cyril Hrubis @ 2017-08-18 10:24 UTC (permalink / raw) To: ltp Hi! Pushed with minor fixes (mostly warnings), thanks. diff --git a/testcases/kernel/syscalls/fanotify/fanotify01.c b/testcases/kernel/syscalls/fanotify/fanotify01.c index 205a999fe..24fc214f5 100644 --- a/testcases/kernel/syscalls/fanotify/fanotify01.c +++ b/testcases/kernel/syscalls/fanotify/fanotify01.c @@ -256,7 +256,7 @@ void test01(void) if (event->fd == -2) goto pass; ret = read(event->fd, buf, BUF_SIZE); - if (ret != strlen(fname)) { + if (ret != (int)strlen(fname)) { tst_res(TFAIL, "cannot read from returned fd " "of event: mask=%llx pid=%u " diff --git a/testcases/kernel/syscalls/fanotify/fanotify03.c b/testcases/kernel/syscalls/fanotify/fanotify03.c index ce3566167..90478bc5b 100644 --- a/testcases/kernel/syscalls/fanotify/fanotify03.c +++ b/testcases/kernel/syscalls/fanotify/fanotify03.c @@ -38,6 +38,7 @@ #include <string.h> #include <signal.h> #include <sys/syscall.h> +#include <stdlib.h> #include "tst_test.h" #include "fanotify.h" @@ -86,6 +87,7 @@ static void generate_events(void) static void child_handler(int tmp) { + (void)tmp; /* * Close notification fd so that we cannot block while reading * from it @@ -107,14 +109,12 @@ static void run_child(void) "sigaction(SIGCHLD, &child_action, NULL) failed"); } - switch (child_pid = fork()) { - case 0: + child_pid = SAFE_FORK(); + if (child_pid == 0) { /* Child will generate events now */ close(fd_notify); generate_events(); exit(0); - case -1: - tst_brk(TBROK | TERRNO, "fork() failed"); } } diff --git a/testcases/kernel/syscalls/fanotify/fanotify04.c b/testcases/kernel/syscalls/fanotify/fanotify04.c index 302f1246b..68f65da0c 100644 --- a/testcases/kernel/syscalls/fanotify/fanotify04.c +++ b/testcases/kernel/syscalls/fanotify/fanotify04.c @@ -105,7 +105,7 @@ static void check_mark(char *file, unsigned long long flag, char *flagstr, #define CHECK_MARK(file, flag, expect, func) check_mark(file, flag, #flag, expect, func) -static void do_open(char *file, int flag, char *flagstr) +static void do_open(char *file, int flag) { int fd; @@ -113,16 +113,14 @@ static void do_open(char *file, int flag, char *flagstr) SAFE_CLOSE(fd); } -#define DO_OPEN(file, flag) do_open(file, flag, #flag) - static void open_file(char *file) { - DO_OPEN(file, 0); + do_open(file, 0); } static void open_dir(char *file) { - DO_OPEN(file, O_DIRECTORY); + do_open(file, O_DIRECTORY); } static void verify_event(int mask) @@ -143,7 +141,7 @@ static void verify_event(int mask) } else if (fstat(event->fd, &st) < 0) { tst_res(TFAIL, "failed to stat event->fd (%s)", strerror(errno)); - } else if ((st.st_mode & S_IFMT) != mask) { + } else if ((int)(st.st_mode & S_IFMT) != mask) { tst_res(TFAIL, "event->fd points to object of different type " "(%o != %o)", st.st_mode & S_IFMT, mask); } else { @@ -152,18 +150,16 @@ static void verify_event(int mask) close(event->fd); } -static void do_open_test(char *file, int flag, char *flagstr, int mask) +static void do_open_test(char *file, int flag, int mask) { - do_open(file, flag, flagstr); + do_open(file, flag); verify_event(mask); } -#define DO_OPEN_TEST(file, flag, mask) do_open_test(file, flag, #flag, mask) - static void test_open_file(char *file) { - DO_OPEN_TEST(file, 0, S_IFREG); + do_open_test(file, 0, S_IFREG); } static void verify_no_event(void) @@ -190,7 +186,7 @@ static void verify_no_event(void) static void test_open_symlink(char *file) { /* Since mark is on a symlink, no event should be generated by opening a file */ - DO_OPEN(file, 0); + do_open(file, 0); verify_no_event(); } diff --git a/testcases/kernel/syscalls/fanotify/fanotify06.c b/testcases/kernel/syscalls/fanotify/fanotify06.c index 7b9d6325d..e63e457a1 100644 --- a/testcases/kernel/syscalls/fanotify/fanotify06.c +++ b/testcases/kernel/syscalls/fanotify/fanotify06.c @@ -237,8 +237,7 @@ static void cleanup(void) { cleanup_fanotify_groups(); - if (chdir(tst_get_tmpdir()) < 0) - tst_brk(TBROK, "chdir(tmpdir) failed"); + SAFE_CHDIR("../"); if (mount_created && tst_umount(MOUNT_NAME) < 0) tst_brk(TBROK | TERRNO, "umount failed"); -- Cyril Hrubis chrubis@suse.cz ^ permalink raw reply related [flat|nested] 20+ messages in thread
* [LTP] [PATCH v4 3/3] syscalls/fanotify08: add sanity check for FAN_CLOEXEC 2017-08-16 7:15 ` [LTP] [PATCH v4 0/3] add SAFE_FANOTIFY_INIT and FAN_CLOEXEC check Xiong Zhou 2017-08-16 7:15 ` [LTP] [PATCH v4 1/3] lib: add helper SAFE_FANOTIFY_INIT Xiong Zhou 2017-08-16 7:15 ` [LTP] [PATCH v4 2/3] fanotify: rewrite old cases with new lib Xiong Zhou @ 2017-08-16 7:15 ` Xiong Zhou 2017-08-18 10:25 ` Cyril Hrubis 2 siblings, 1 reply; 20+ messages in thread From: Xiong Zhou @ 2017-08-16 7:15 UTC (permalink / raw) To: ltp Signed-off-by: Xiong Zhou <xzhou@redhat.com> --- runtest/syscalls | 1 + testcases/kernel/syscalls/fanotify/fanotify08.c | 91 +++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 testcases/kernel/syscalls/fanotify/fanotify08.c diff --git a/runtest/syscalls b/runtest/syscalls index 4d3218f..73d6a78 100644 --- a/runtest/syscalls +++ b/runtest/syscalls @@ -481,6 +481,7 @@ fanotify04 fanotify04 fanotify05 fanotify05 fanotify06 fanotify06 fanotify07 fanotify07 +fanotify08 fanotify08 ioperm01 ioperm01 ioperm02 ioperm02 diff --git a/testcases/kernel/syscalls/fanotify/fanotify08.c b/testcases/kernel/syscalls/fanotify/fanotify08.c new file mode 100644 index 0000000..dc916ec --- /dev/null +++ b/testcases/kernel/syscalls/fanotify/fanotify08.c @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2017 RedHat. All Rights Reserved. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it would be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * Further, this software is distributed without any warranty that it is + * free of the rightful claim of any third person regarding infringement + * or the like. Any license provided herein, whether implied or + * otherwise, applies only to this software file. Patent licenses, if + * any, provided herein do not apply to combinations of this program with + * other software, or any other product whatsoever. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Started by Xiong Zhou <xzhou@redhat.com> + * + * DESCRIPTION + * Sanity check fanotify_init flag FAN_CLOEXEC by fcntl. + */ +#define _GNU_SOURCE +#include "config.h" + +#include <stdio.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <fcntl.h> +#include <errno.h> +#include <string.h> +#include <sys/syscall.h> +#include "tst_test.h" +#include "fanotify.h" + +#if defined(HAVE_SYS_FANOTIFY_H) +#include <sys/fanotify.h> + +static int fd_notify; + +static void test_init_bit(unsigned int fan_bit, + unsigned int fd_bit, char *msg) +{ + int ret; + + fd_notify = SAFE_FANOTIFY_INIT(FAN_CLASS_NOTIF|fan_bit, O_RDONLY); + + ret = SAFE_FCNTL(fd_notify, F_GETFD); + + if ((ret & FD_CLOEXEC) == fd_bit) { + tst_res(TPASS, msg); + } else { + tst_res(TFAIL, msg); + } + + SAFE_CLOSE(fd_notify); +} + +static void run(unsigned int i) +{ + switch (i) { + case 0: + test_init_bit(0, 0, "not set close_on_exec"); + break; + case 1: + test_init_bit(FAN_CLOEXEC, FD_CLOEXEC, "set close_on_exec"); + break; + } +} + +static void cleanup(void) +{ + if (fd_notify > 0) + SAFE_CLOSE(fd_notify); +} + +static struct tst_test test = { + .test = run, + .tcnt = 2, + .cleanup = cleanup, + .needs_root = 1, +}; + +#else + TST_TEST_TCONF("system doesn't have required fanotify support"); +#endif -- 1.8.3.1 ^ permalink raw reply related [flat|nested] 20+ messages in thread
* [LTP] [PATCH v4 3/3] syscalls/fanotify08: add sanity check for FAN_CLOEXEC 2017-08-16 7:15 ` [LTP] [PATCH v4 3/3] syscalls/fanotify08: add sanity check for FAN_CLOEXEC Xiong Zhou @ 2017-08-18 10:25 ` Cyril Hrubis 0 siblings, 0 replies; 20+ messages in thread From: Cyril Hrubis @ 2017-08-18 10:25 UTC (permalink / raw) To: ltp Hi! I've added fanotify08 into the .gitignore and pushed, thanks. -- Cyril Hrubis chrubis@suse.cz ^ permalink raw reply [flat|nested] 20+ messages in thread
end of thread, other threads:[~2017-08-21 11:45 UTC | newest] Thread overview: 20+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2017-06-02 9:24 [LTP] [PATCH] syscalls/fanotify08: add sanity check for FAN_CLOEXEC Xiong Zhou 2017-06-05 14:03 ` Cyril Hrubis 2017-08-11 3:13 ` [LTP] [PATCH v2] " Xiong Zhou 2017-08-11 8:41 ` Cyril Hrubis 2017-08-14 14:01 ` [LTP] [PATCH v3 0/3] add SAFE_FANOTIFY_INIT and FAN_CLOEXEC cases Xiong Zhou 2017-08-14 14:01 ` [LTP] [PATCH v3 1/3] lib: add helper SAFE_FANOTIFY_INIT Xiong Zhou 2017-08-15 13:23 ` Cyril Hrubis 2017-08-14 14:01 ` [LTP] [PATCH v3 2/3] fanotify: rewrite old cases with new lib Xiong Zhou 2017-08-15 13:33 ` Cyril Hrubis 2017-08-14 14:01 ` [LTP] [PATCH v3 3/3] syscalls/fanotify0[89]: add sanity check for FAN_CLOEXEC Xiong Zhou 2017-08-15 13:36 ` Cyril Hrubis 2017-08-16 7:15 ` [LTP] [PATCH v4 0/3] add SAFE_FANOTIFY_INIT and FAN_CLOEXEC check Xiong Zhou 2017-08-16 7:15 ` [LTP] [PATCH v4 1/3] lib: add helper SAFE_FANOTIFY_INIT Xiong Zhou 2017-08-18 10:22 ` Cyril Hrubis 2017-08-21 3:12 ` Xiong Zhou 2017-08-21 11:45 ` Cyril Hrubis 2017-08-16 7:15 ` [LTP] [PATCH v4 2/3] fanotify: rewrite old cases with new lib Xiong Zhou 2017-08-18 10:24 ` Cyril Hrubis 2017-08-16 7:15 ` [LTP] [PATCH v4 3/3] syscalls/fanotify08: add sanity check for FAN_CLOEXEC Xiong Zhou 2017-08-18 10:25 ` Cyril Hrubis
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox