syscalls/inotify: Test generation of overflow events
A message that is not in the archive (sent off-list or not yet received): [email protected]
Hi!
Quoted 4 lines
Test whether overflow event is properly generated when we don't read inotify descriptor and generate enough events.
Signed-off-by Jan Kara <[email protected]>
You are missing runtest files and it would be better to update syscalls/.gitignore too.
I've wrote a short introduction on writing LTP testcases (it's not finished yet but it contains quite a lot of good information). And there is a contribution checklist at the bottom:
https://github.com/linux-test-project/ltp/wiki/Test-Writing-Guidelines
Quoted 133 lines
--- testcases/kernel/syscalls/inotify/inotify05.c | 224 ++++++++++++++++++++++++++ 1 file changed, 224 insertions(+) create mode 100644 testcases/kernel/syscalls/inotify/inotify05.ctestcases/kernel/syscalls/inotify/inotify05.c new file +122
-0@@ -0,0 +1,224 @@ +/* +* Copyright (c) 2014 SUSE Linux. 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 Jan Kara <[email protected]> +* +* DESCRIPTION +* Check that inotify overflow event is properly generated +* +* ALGORITHM +* Generate enough events without reading them and check that overflow +* event is generated. +*/ +#include "config.h" + +#include <stdio.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <sys/fcntl.h> +#include <errno.h> +#include <string.h> +#include <sys/syscall.h> +#include "test.h" +#include "usctest.h" +#include "linux_syscall_numbers.h" +#include "inotify.h" +#include "safe_macros.h" + +char *TCID ="inotify05" ; +int TST_TOTAL =1 ; + +#if defined(HAVE_SYS_INOTIFY_H ) +#include <sys/inotify.h> + +/* size of the event structure, not counting name */ +#define EVENT_SIZE (sizeof (struct inotify_event)) +#define EVENT_BUF_LEN (EVENT_SIZE * 16) + +static void setup (void ); +static void cleanup (void ); + +#define BUF_SIZE 256 +static char fname[BUF_SIZE ]; +static char buf[BUF_SIZE ]; +static int fd, fd_notify; +static int wd; +static int max_events; + +static char event_buf[EVENT_BUF_LEN ]; + +int main (int ac,char **av) +{ +int lc, i; +char *msg; +int len, stop; + +if ((msg =parse_opts (ac, av,NULL ,NULL )) !=NULL ) +tst_brkm (TBROK ,NULL ,"OPTION PARSING ERROR - %s" , msg); + +setup (); + +for (lc =0 ;TEST_LOOPING (lc); lc++) { +/* +* generate events +*/ + fd =SAFE_OPEN (cleanup, fname,O_RDWR ); + +for (i =0 ; i < max_events; i++) { +SAFE_LSEEK (cleanup, fd,0 ,SEEK_SET ); +SAFE_READ (cleanup,1 , fd, buf,BUF_SIZE ); +SAFE_LSEEK (cleanup, fd,0 ,SEEK_SET ); +SAFE_WRITE (cleanup,1 , fd, buf,BUF_SIZE ); + } + +SAFE_CLOSE (cleanup, fd); + + stop =0 ; +while (!stop) { +/* +* get list on events +*/ + len =read (fd_notify, event_buf,EVENT_BUF_LEN ); +if (len <0 ) { +tst_brkm (TBROK |TERRNO , cleanup, +"read(%d, buf, %zu) failed" , + fd_notify,EVENT_BUF_LEN ); + } + +/* +* check events +*/ + i =0 ; +while (i < len) { +struct inotify_event *event; + + event = (struct inotify_event *)&event_buf[i]; +if (event->mask !=IN_ACCESS && + event->mask !=IN_MODIFY && + event->mask !=IN_OPEN && + event->mask !=IN_Q_OVERFLOW ) { + tst_resm(TFAIL, + "get event: wd=%d mask=%x " + "cookie=%u (expected 0) len=%u", + event->wd, event->mask,
testcases/kernel/syscalls/inotify/inotify05.c line 132
Quoted 10 lines
testcases/kernel/syscalls/inotify/inotify05.c new file +10
-0+ event->cookie, event->len); + stop =1 ; +break ; + } +if (event->mask ==IN_Q_OVERFLOW ) { +if (event->len !=0 || + event->cookie !=0 || + event->wd !=-1 ) { +tst_resm (TFAIL , + "invalid overflow event: "
There is a space before the tab.
testcases/kernel/syscalls/inotify/inotify05.c line 143
Quoted 11 lines
testcases/kernel/syscalls/inotify/inotify05.c new file +11
-0+"wd=%d mask=%x " +"cookie=%u len=%u" , + event->wd, event->mask, + event->cookie, + event->len); + stop =1 ; +break ; + } +if ((int )(i +EVENT_SIZE ) != len) { +tst_resm (TFAIL , +"overflow event is not last" );
Here as well.
Quoted 21 lines
testcases/kernel/syscalls/inotify/inotify05.c new file +21
-0+ stop =1 ; +break ; + } +tst_resm (TPASS ,"get event: wd=%d " +"mask=%x cookie=%u len=%u" , + event->wd, event->mask, + event->cookie, event->len); + stop =1 ; +break ; + } + i +=EVENT_SIZE + event->len; + } + } + } + +cleanup (); +tst_exit (); +} + +static void setup (void ) +{
testcases/kernel/syscalls/inotify/inotify05.c line 174
Quoted 10 lines
testcases/kernel/syscalls/inotify/inotify05.c new file +10
-0+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, buf,BUF_SIZE ); +SAFE_CLOSE (cleanup, fd);
I've created SAFE_FILE_PRINT() and SAFE_FILE_SCANF() to simplify reading and writing files with printf and scanf like interface. See include/safe_file_ops.h
Quoted 13 lines
testcases/kernel/syscalls/inotify/inotify05.c new file +13
-0+if ((fd_notify =syscall (__NR_inotify_init1,O_NONBLOCK )) <0 ) { +if (errno ==ENOSYS ) { +tst_brkm (TCONF , cleanup, +"inotify is not configured in this kernel." ); + }else { +tst_brkm (TBROK |TERRNO , cleanup, +"inotify_init failed" ); + } + } + +if ((wd =myinotify_add_watch (fd_notify, fname,IN_ALL_EVENTS )) <0 ) { + tst_brkm(TBROK | TERRNO, cleanup, + "inotify_add_watch (%d, %s, IN_ALL_EVENTS) failed",
testcases/kernel/syscalls/inotify/inotify05.c line 197
Quoted 10 lines
testcases/kernel/syscalls/inotify/inotify05.c new file +10
-0+ fd_notify, fname); + }; + + fd =SAFE_OPEN (cleanup,"/proc/sys/fs/inotify/max_queued_events" ,O_RDONLY ); +SAFE_READ (cleanup,0 , fd, buf,BUF_SIZE ); +if (sscanf (buf,"%d" , &max_events) !=1 ) { +tst_brkm (TBROK , cleanup, +"cannot parse max_queued_events file" ); + } +SAFE_CLOSE (cleanup, fd);
SAFE_FILE_SCANF() ;)
Quoted 26 lines
testcases/kernel/syscalls/inotify/inotify05.c new file +26
-0+} + +static void cleanup (void ) +{ +if (myinotify_rm_watch (fd_notify, wd) <0 ) { +tst_resm (TWARN |TERRNO ,"inotify_rm_watch (%d, %d) failed" , + fd_notify, wd); + + } + +if (close (fd_notify) ==-1 ) { +tst_resm (TWARN ,"close(%d) failed" , fd_notify); + } + +TEST_CLEANUP ; +tst_rmdir (); +} + +#else + +int main (void ) +{ +tst_brkm (TCONF ,NULL ,"system doesn't have required inotify support" ); +} + +#endif
Signature · 13 lines
Cyril Hrubis [email protected] ------------------------------------------------------------------------------ Flow-based real-time traffic analytics software. Cisco certified tool. Monitor traffic, SLAs, QoS, Medianet, WAAS etc. with NetFlow Analyzer Customize your own dashboards, set traffic alerts and generate reports. Network behavioral analysis & security monitoring. All-in-one tool. http://pubads.g.doubleclick.net/gampad/clk?id=126839071&iu=/4140/ostg.clktrk _______________________________________________ Ltp-list mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/ltp-list
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: [LTP] [PATCH 2/2] syscalls/fanotify: Test generation of overflow events
Hi!
Quoted 2 lines
Test whether overflow event is properly generated when we don't read fanotify descriptor and generate enough events.
Missing runtest and .gitignore changes as well.
Quoted 83 lines
testcases/kernel/syscalls/fanotify/fanotify05.c | 185 ++++++++++++++++++++++++ 1 file changed, 185 insertions(+) create mode 100644 testcases/kernel/syscalls/fanotify/fanotify05.ctestcases/kernel/syscalls/fanotify/fanotify05.c new file +73
-0@@ -0,0 +1,185 @@ +/* +* Copyright (c) 2014 SUSE Linux. 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 Jan Kara <[email protected]> +* +* DESCRIPTION +* Check that fanotify overflow event is properly generated +* +* ALGORITHM +* Generate enough events without reading them and check that overflow +* event is generated. +*/ +#include "config.h" + +#include <stdio.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <sys/fcntl.h> +#include <errno.h> +#include <string.h> +#include <sys/syscall.h> +#include "test.h" +#include "usctest.h" +#include "linux_syscall_numbers.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> + +/* 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) +{ +int lc, i; +char *msg; +int len; + +if ((msg =parse_opts (ac, av,NULL ,NULL )) !=NULL ) +tst_brkm (TBROK ,NULL ,"OPTION PARSING ERROR - %s" , msg); +
testcases/kernel/syscalls/fanotify/fanotify05.c line 83
Quoted 10 lines
testcases/kernel/syscalls/fanotify/fanotify05.c new file +10
-0+setup (); + +for (lc =0 ;TEST_LOOPING (lc); lc++) { +/* +* generate 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);
SAFE_FILE_TOUCH(cleanup, fname, 0, NULL)
Quoted 5 lines
testcases/kernel/syscalls/fanotify/fanotify05.c new file +5
-0+ } + +while (1 ) { +/* +* get list on events
testcases/kernel/syscalls/fanotify/fanotify05.c line 98
Quoted 10 lines
testcases/kernel/syscalls/fanotify/fanotify05.c new file +10
-0+*/ + 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" );
Space befor tab.
Quoted 12 lines
testcases/kernel/syscalls/fanotify/fanotify05.c new file +12
-0+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)"
testcases/kernel/syscalls/fanotify/fanotify05.c line 120
Quoted 10 lines
testcases/kernel/syscalls/fanotify/fanotify05.c new file +10
-0+"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_resm (TFAIL , + "invalid overflow event: "
Here as well.
Quoted 19 lines
testcases/kernel/syscalls/fanotify/fanotify05.c new file +19
-0+"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 ; + } + } + } + +cleanup (); +tst_exit (); +} +
testcases/kernel/syscalls/fanotify/fanotify05.c line 149
Quoted 10 lines
testcases/kernel/syscalls/fanotify/fanotify05.c new file +10
-0+static void setup (void ) +{ +tst_sig (NOFORK ,DEF_HANDLER , cleanup); + +TEST_PAUSE ; + +tst_tmpdir (); + +if ((fd_notify =myfanotify_init (FAN_CLASS_NOTIF |FAN_NONBLOCK , +O_RDONLY )) <0 ) {
I would move the assigment out of the if conditional here and then do just if (fd_notify < 0) but that is just aesthetic change.
Quoted 13 lines
testcases/kernel/syscalls/fanotify/fanotify05.c new file +13
-0+if (errno ==ENOSYS ) { +tst_brkm (TCONF , cleanup, +"fanotify is not configured in this kernel." ); + }else { +tst_brkm (TBROK |TERRNO , cleanup, +"fanotify_init failed" ); + } + } + +if (myfanotify_mark (fd_notify,FAN_MARK_MOUNT |FAN_MARK_ADD ,FAN_OPEN , +AT_FDCWD ,"." ) <0 ) { +tst_brkm (TBROK |TERRNO , cleanup, + "fanotify_mark (%d, FAN_MARK_MOUNT | FAN_MARK_ADD, "
testcases/kernel/syscalls/fanotify/fanotify05.c line 172
Quoted 10 lines
testcases/kernel/syscalls/fanotify/fanotify05.c new file +10
-0+"FAN_OPEN, AT_FDCWD, \".\") failed" , + fd_notify); + } +} + +static void cleanup (void ) +{ +if (close (fd_notify) ==-1 ) { +tst_resm (TWARN ,"close(%d) failed" , fd_notify); + }
And I tend not to add curly brackets around one line statements...
Quoted 15 lines
testcases/kernel/syscalls/fanotify/fanotify05.c new file +12
-0+TEST_CLEANUP ; + tst_rmdir(); +} + +#else + +int main (void ) +{ +tst_brkm (TCONF ,NULL ,"system doesn't have required fanotify support" ); +} + +#endif Signature · 1 line
1.8.1.4
Signature · 13 lines
Cyril Hrubis [email protected] ------------------------------------------------------------------------------ Flow-based real-time traffic analytics software. Cisco certified tool. Monitor traffic, SLAs, QoS, Medianet, WAAS etc. with NetFlow Analyzer Customize your own dashboards, set traffic alerts and generate reports. Network behavioral analysis & security monitoring. All-in-one tool. http://pubads.g.doubleclick.net/gampad/clk?id=126839071&iu=/4140/ostg.clktrk _______________________________________________ Ltp-list mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/ltp-list
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: [LTP] [PATCH 2/2] syscalls/fanotify: Test generation of overflow events
Hi!
Quoted 12 lines
Quoted 4 lines
Quoted 2 lines
Test whether overflow event is properly generated when we don't read fanotify descriptor and generate enough events.
Missing runtest and .gitignore changes as well.
Fixed in v2.
Quoted 6 lines
Review
Quoted 4 lines
(quoted diff) +4
-0+ 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);
SAFE_FILE_TOUCH(cleanup, fname, 0, NULL)
Quoted 4 lines
I was considering this but since I really want syscall to be
open()I
thought usingSAFE_OPENdirectly is more future-proof in case someone would like to do some tricks withSAFE_FILE_TOUCH(). And we won't really save much with usingSAFE_FILE_TOUCH()...
Ah, this loop generates the events, so it's better to have it exactly under control. Ok.
Signature · 13 lines
Cyril Hrubis [email protected] ------------------------------------------------------------------------------ Flow-based real-time traffic analytics software. Cisco certified tool. Monitor traffic, SLAs, QoS, Medianet, WAAS etc. with NetFlow Analyzer Customize your own dashboards, set traffic alerts and generate reports. Network behavioral analysis & security monitoring. All-in-one tool. http://pubads.g.doubleclick.net/gampad/clk?id=126839071&iu=/4140/ostg.clktrk _______________________________________________ Ltp-list mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/ltp-list
Hi!
Quoted 39 lines
(quoted diff) +39
-0+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, buf, BUF_SIZE); + SAFE_CLOSE(cleanup, fd); + + if ((fd_notify = syscall(__NR_inotify_init1, O_NONBLOCK)) < 0) { + if (errno == ENOSYS) { + tst_brkm(TCONF, cleanup, + "inotify is not configured in this kernel."); + } else { + tst_brkm(TBROK | TERRNO, cleanup, + "inotify_init failed"); + } + } + + if ((wd = myinotify_add_watch(fd_notify, fname, IN_ALL_EVENTS)) < 0) { + tst_brkm(TBROK | TERRNO, cleanup, + "inotify_add_watch (%d, %s, IN_ALL_EVENTS) failed", + fd_notify, fname); + }; + + fd = SAFE_OPEN(cleanup, "/proc/sys/fs/inotify/max_queued_events", O_RDONLY); + SAFE_READ(cleanup, 0, fd, buf, BUF_SIZE); + if (sscanf(buf, "%d", &max_events) != 1) { + tst_brkm(TBROK, cleanup, + "cannot parse max_queued_events file"); + } + SAFE_CLOSE(cleanup, fd); +} + +static void cleanup(void)
Review
Quoted 10 lines
(quoted diff) +10
-0+{ + if (myinotify_rm_watch(fd_notify, wd) < 0) { + tst_resm(TWARN | TERRNO, "inotify_rm_watch (%d, %d) failed", + fd_notify, wd); + + } + + if (close(fd_notify) == -1) { + tst_resm(TWARN, "close(%d) failed", fd_notify); + }
And I guess that we need to change this to:
if (fd_notify > 0 && close(fd_notify) == -1)
because the fd_notify may be set to -1 in case that the syscall above failed with ENOSYS which would generate a bogus warning. And I guess that the same goes for the removing the inotify watch.
Quoted 15 lines
(quoted diff) +12
-0+ TEST_CLEANUP; + tst_rmdir(); +} + +#else + +int main(void) +{ + tst_brkm(TCONF, NULL, "system doesn't have required inotify support"); +} + +#endifSignature · 1 line
1.8.1.4
Signature · 13 lines
Cyril Hrubis [email protected] ------------------------------------------------------------------------------ Flow-based real-time traffic analytics software. Cisco certified tool. Monitor traffic, SLAs, QoS, Medianet, WAAS etc. with NetFlow Analyzer Customize your own dashboards, set traffic alerts and generate reports. Network behavioral analysis & security monitoring. All-in-one tool. http://pubads.g.doubleclick.net/gampad/clk?id=126839071&iu=/4140/ostg.clktrk _______________________________________________ Ltp-list mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/ltp-list
A message that is not in the archive (sent off-list or not yet received): [email protected]
Hi!
Quoted 23 lines
Quoted 23 lines
Quoted 9 lines
(quoted diff) +9
-0+ SAFE_READ(cleanup, 0, fd, buf, BUF_SIZE); + if (sscanf(buf, "%d", &max_events) != 1) { + tst_brkm(TBROK, cleanup, + "cannot parse max_queued_events file"); + } + SAFE_CLOSE(cleanup, fd); +} + +static void cleanup(void)Review
Quoted 10 lines
(quoted diff) +10
-0+{ + if (myinotify_rm_watch(fd_notify, wd) < 0) { + tst_resm(TWARN | TERRNO, "inotify_rm_watch (%d, %d) failed", + fd_notify, wd); + + } + + if (close(fd_notify) == -1) { + tst_resm(TWARN, "close(%d) failed", fd_notify); + }And I guess that we need to change this to:
Quoted 8 lines
Quoted 6 lines
if (fd_notify > 0 && close(fd_notify) == -1)because the
fd_notifymay be set to -1 in case that the syscall above failed with ENOSYS which would generate a bogus warning. And I guess that the same goes for the removing the inotify watch.Well, yes. I can make that less verbose in case of missing inotify
support. I was just following what is done in other inotify tests...
The problem is that TWARN gets propagated into the overall test result so the test will not endup with clean TCONF in this case.
And unfortunately quite a lot of test cleanups are coded incorrectly in LTP. :(
Signature · 13 lines
Cyril Hrubis [email protected] ------------------------------------------------------------------------------ Flow-based real-time traffic analytics software. Cisco certified tool. Monitor traffic, SLAs, QoS, Medianet, WAAS etc. with NetFlow Analyzer Customize your own dashboards, set traffic alerts and generate reports. Network behavioral analysis & security monitoring. All-in-one tool. http://pubads.g.doubleclick.net/gampad/clk?id=126839071&iu=/4140/ostg.clktrk _______________________________________________ Ltp-list mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/ltp-list