open13: Convert to new API
PATCH 1/3 open13: Convert to new API
Signed-off-by Martin Doucha <[email protected]>
---
The original test created a file with christmas tree access flags. There's no need for such risky behavior here so I've changed it to standard 0644. Follow-up patches also add subtests for mmap() and ioctl() syscalls as well as a verification that a new file descriptor created with dup() will keep the O_PATH flag.
testcases/kernel/syscalls/open/open13.c | 153 ++++++++---------------- 1 file changed, 51 insertions(+), 102 deletions(-)
testcases/kernel/syscalls/open/open13.c +51 -102
@@ -1,21 +1,11 @@ +// SPDX-License-Identifier: GPL-2.0-only /* * Copyright (c) 2015 Fujitsu Ltd. * Author: Guangwen Feng <[email protected]> -* -* 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. -* -* You should have received a copy of the GNU General Public License along -* with this program. +* Copyright (c) 2025 SUSE LLC <[email protected]> */ -/* -* DESCRIPTION +/*\ * Basic test for O_PATH flag of open(2). * "Obtain a file descriptor that can be used to perform operations * that act purely at the file descriptor level, the file itself is
@@ -25,139 +15,98 @@* The operations include but are not limited to the syscalls above. */ -#define _GNU_SOURCE -#include "config.h" -#include <errno.h> #ifdef HAVE_SYS_XATTR_H #include <sys/types.h> #include <sys/xattr.h> #endif -#include "test.h" -#include "safe_macros.h" +#include "tst_test.h" +#include "tst_safe_macros.h" #include "lapi/fcntl.h" #define TESTFILE "testfile" -#define FILE_MODE (S_IRWXU | S_IRWXG | S_IRWXO | S_ISUID | S_ISGID) -static void setup (void ); -static void verify_read (void ); -static void verify_write (void ); -static void verify_fchmod (void ); -static void verify_fchown (void ); +static int path_fd =-1 ; + +static int verify_read (int fd); +static int verify_write (int fd); +static int verify_fchmod (int fd); +static int verify_fchown (int fd);#ifdef HAVE_SYS_XATTR_H -static void verify_fgetxattr (void ); +static int verify_fgetxattr (int fd);#endif -static void check_result (const char *call_name); -static void cleanup (void ); -static int fd; - -static void (*test_func[])(void ) = { - verify_read, - verify_write, - verify_fchmod, - verify_fchown, +static const struct { +int (*func)(int fd); +const char *name; +} testcases[] = { + {verify_read,"read" }, + {verify_write,"write" }, + {verify_fchmod,"fchmod" }, + {verify_fchown,"fchown" }, #ifdef HAVE_SYS_XATTR_H - verify_fgetxattr + {verify_fgetxattr,"fgetxattr" }, #endif + {} }; -char *TCID ="open13" ; -int TST_TOTAL =ARRAY_SIZE (test_func); - -int main (int ac,char **av) +static void setup (void ) { -int lc; -int tc; - -tst_parse_opts (ac, av,NULL ,NULL ); - -setup (); - -for (lc =0 ;TEST_LOOPING (lc); lc++) { - tst_count =0 ; - - fd =SAFE_OPEN (cleanup,TESTFILE ,O_RDWR |O_PATH ); - -for (tc =0 ; tc <TST_TOTAL ; tc++) - (*test_func[tc])(); - -SAFE_CLOSE (cleanup, fd); - fd =0 ; - } - -cleanup (); -tst_exit (); + path_fd =SAFE_OPEN (TESTFILE ,O_RDWR |O_CREAT ,0644 ); +SAFE_CLOSE (path_fd); + path_fd =SAFE_OPEN (TESTFILE ,O_PATH ); } -static void setup (void ) +static void run (void ) { -tst_sig (NOFORK ,DEF_HANDLER , cleanup); - -tst_tmpdir (); - -SAFE_TOUCH (cleanup,TESTFILE ,FILE_MODE ,NULL ); +int i; -TEST_PAUSE ; +for (i =0 ; testcases[i].func; i++) { +TST_EXP_FAIL (testcases[i].func (path_fd),EBADF ,"%s()" , + testcases[i].name); + } } -static void verify_read (void ) +static int verify_read (int fd) {char buf[255 ]; -TEST (read (fd, buf,sizeof (buf))); -check_result ("read(2)" ); +return read (fd, buf,sizeof (buf)); } -static void verify_write (void ) +static int verify_write (int fd) { -TEST (write (fd,"w" ,1 )); -check_result ("write(2)" ); +return write (fd,"w" ,1 ); } -static void verify_fchmod (void ) +static int verify_fchmod (int fd) { -TEST (fchmod (fd,0666 )); -check_result ("fchmod(2)" ); +return fchmod (fd,0666 ); } -static void verify_fchown (void ) +static int verify_fchown (int fd) { -TEST (fchown (fd,1000 ,1000 )); -check_result ("fchown(2)" ); +return fchown (fd,1000 ,1000 ); }#ifdef HAVE_SYS_XATTR_H -static void verify_fgetxattr (void ) +static int verify_fgetxattr (int fd) { -TEST (fgetxattr (fd,"tkey" ,NULL ,1 )); -check_result ("fgetxattr(2)" ); +return fgetxattr (fd,"tkey" ,NULL ,0 ); }#endif -static void check_result (const char *call_name) -{ -if (TEST_RETURN ==0 ) { -tst_resm (TFAIL ,"%s succeeded unexpectedly" , call_name); -return ; - } - -if (TEST_ERRNO !=EBADF ) { -tst_resm (TFAIL |TTERRNO ,"%s failed unexpectedly, " -"expected EBADF" , call_name); -return ; - } - -tst_resm (TPASS ,"%s failed with EBADF" , call_name); -} -static void cleanup (void ) { -if (fd >0 &&close (fd)) -tst_resm (TWARN |TERRNO ,"failed to close file" ); - -tst_rmdir (); +if (path_fd >=0 ) +SAFE_CLOSE (path_fd); } + +static struct tst_test test = { + .setup = setup, + .test_all = run, + .cleanup = cleanup, + .needs_tmpdir =1 +};
Signature · 5 lines
2.49.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH 2/3 open13: Add subtests for mmap() and ioctl()
Signed-off-by Martin Doucha <[email protected]>
--- testcases/kernel/syscalls/open/open13.c | 27 +++++++++++++++++++++++++ 1 file changed, 27 insertions(+)
testcases/kernel/syscalls/open/open13.c +27 -0
@@ -17,6 +17,9 @@#include "config.h" +#include <sys/ioctl.h> +#include <linux/fs.h> +#ifdef HAVE_SYS_XATTR_H #include <sys/types.h> #include <sys/xattr.h>
@@ -34,6 +37,8 @@ static int verify_read(int fd);static int verify_write (int fd);static int verify_fchmod (int fd);static int verify_fchown (int fd); +static int verify_ioctl (int fd); +static int verify_mmap (int fd);#ifdef HAVE_SYS_XATTR_H static int verify_fgetxattr (int fd);#endif
@@ -46,6 +51,8 @@ static const struct { {verify_write, "write"}, {verify_fchmod, "fchmod"}, {verify_fchown, "fchown"}, + {verify_ioctl, "ioctl"}, + {verify_mmap, "mmap"}, #ifdef HAVE_SYS_XATTR_H {verify_fgetxattr, "fgetxattr"}, #endif
@@ -91,6 +98,26 @@ static int verify_fchown(int fd)return fchown (fd,1000 ,1000 ); } +static int verify_ioctl (int fd) +{ +int arg; + +return ioctl (fd,FIGETBSZ , &arg); +} + +static int verify_mmap (int fd) +{ +void *ptr; + + ptr =mmap (NULL ,1 ,PROT_READ ,MAP_PRIVATE , fd,0 ); + +if (ptr ==MAP_FAILED ) +return -1 ; + +SAFE_MUNMAP (ptr,1 ); +return 0 ; +} +#ifdef HAVE_SYS_XATTR_H static int verify_fgetxattr (int fd) {
Signature · 5 lines
2.49.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH 3/3 open13: Verify that dup() will preserve O_PATH flag
In reply to Martin DouchaVerify that calling dup() on a file descriptor opened with O_PATH will return another file descriptor that is not usable for standard I/O operations.
Signed-off-by Martin Doucha <[email protected]>
--- testcases/kernel/syscalls/open/open13.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)
testcases/kernel/syscalls/open/open13.c +9 -3
@@ -31,7 +31,7 @@#define TESTFILE "testfile" -static int path_fd =-1 ; +static int path_fd =-1 , dup_fd =-1 ;static int verify_read (int fd);static int verify_write (int fd);
@@ -64,6 +64,7 @@ static void setup(void) path_fd =SAFE_OPEN (TESTFILE ,O_RDWR |O_CREAT ,0644 );SAFE_CLOSE (path_fd); path_fd =SAFE_OPEN (TESTFILE ,O_PATH ); + dup_fd =SAFE_DUP (path_fd); }static void run (void )
@@ -71,8 +72,10 @@ static void run(void)int i;for (i =0 ; testcases[i].func; i++) { -TST_EXP_FAIL (testcases[i].func (path_fd),EBADF ,"%s()" , - testcases[i].name); +TST_EXP_FAIL (testcases[i].func (path_fd),EBADF , +"%s() on original FD" , testcases[i].name); +TST_EXP_FAIL (testcases[i].func (dup_fd),EBADF , +"%s() on duplicated FD" , testcases[i].name); } }
@@ -129,6 +132,9 @@ static void cleanup(void) {if (path_fd >=0 )SAFE_CLOSE (path_fd); + +if (dup_fd >=0 ) +SAFE_CLOSE (dup_fd); }static struct tst_test test = {
Signature · 5 lines
2.49.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
Reviewed-by Andrea Cervesato <[email protected]>
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Merged thanks!
- Andrea
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp