Expand selftest utils
PATCH 3/5 selftests/powerpc: Parse long/unsigned long value safely
In reply to Benjamin GrayOften a file is expected to hold an integral value. Existing functions will use a C stdlib function like atoi or strtol to parse the file. These operations are error prone, with complicated error conditions (atoi returns 0 if not a number, and is undefined behaviour if not in range. strtol returns 0 if not a number, and LONG_MIN/MAX if not in range + sets errno to ERANGE).
---
v4: * Implement as C functions, remove macros
* Declare parsers for intmax_t, uintmax_t
* Stop validating the remaining buffer after a null terminator
* Set errno to match the return code
--- .../testing/selftests/powerpc/include/utils.h | 7 + tools/testing/selftests/powerpc/pmu/lib.c | 6 +- tools/testing/selftests/powerpc/utils.c | 126 +++++++++++++++++- 3 files changed, 132 insertions(+), 7 deletions(-)
tools/testing/selftests/powerpc/include/utils.h +7 -0
@@ -33,6 +33,13 @@ void *get_auxv_entry(int type);int pick_online_cpu (void ); +int parse_intmax (const char *buffer,size_t count,intmax_t *result,int base); +int parse_uintmax (const char *buffer,size_t count,uintmax_t *result,int base); +int parse_int (const char *buffer,size_t count,int *result,int base); +int parse_uint (const char *buffer,size_t count,unsigned int *result,int base); +int parse_long (const char *buffer,size_t count,long *result,int base); +int parse_ulong (const char *buffer,size_t count,unsigned long *result,int base); +int read_file (const char *path,char *buf,size_t count,size_t *len);int write_file (const char *path,const char *buf,size_t count);int read_debugfs_file (const char *debugfs_file,char *buf,size_t count);
tools/testing/selftests/powerpc/pmu/lib.c +2 -4
@@ -192,7 +192,6 @@ bool require_paranoia_below(int level) {int err;long current; -char *end;char buf[16 ] = {0 }; err =read_file (PARANOID_PATH , buf,sizeof (buf) -1 ,NULL );
@@ -201,9 +200,8 @@ bool require_paranoia_below(int level)return false; } - current =strtol (buf, &end,10 ); - -if (end == buf) { + err =parse_long (buf,sizeof (buf), ¤t,10 ); +if (err) {printf ("Couldn't parse " PARANOID_PATH "?\n" );return false; }
tools/testing/selftests/powerpc/utils.c +123 -3
144 lines not on this page · raw message
Signature · 1 line
2.39.1
PATCH 2/5 selftests/powerpc: Add read/write debugfs file, int
In reply to Benjamin GrayDebugfs files are not always integers, so make *_file return/write a byte buffer, and *_int deal with int values specifically. This increases consistency with the other file read/write helpers.
---
v4: * Add reviewed-by
--- .../testing/selftests/powerpc/include/utils.h | 6 ++-- .../selftests/powerpc/security/entry_flush.c | 12 +++---- .../selftests/powerpc/security/rfi_flush.c | 12 +++---- .../powerpc/security/uaccess_flush.c | 18 +++++----- tools/testing/selftests/powerpc/utils.c | 34 ++++++++++++------- 5 files changed, 47 insertions(+), 35 deletions(-)
tools/testing/selftests/powerpc/include/utils.h +4 -2
@@ -35,8 +35,10 @@ int pick_online_cpu(void);int read_file (const char *path,char *buf,size_t count,size_t *len);int write_file (const char *path,const char *buf,size_t count); -int read_debugfs_file (char *debugfs_file,int *result); -int write_debugfs_file (char *debugfs_file,int result); +int read_debugfs_file (const char *debugfs_file,char *buf,size_t count); +int write_debugfs_file (const char *debugfs_file,const char *buf,size_t count); +int read_debugfs_int (const char *debugfs_file,int *result); +int write_debugfs_int (const char *debugfs_file,int result);int read_sysfs_file (char *debugfs_file,char *result,size_t result_size);int perf_event_open_counter (unsigned int type,unsigned long config,int group_fd);
tools/testing/selftests/powerpc/security/entry_flush.c +6 -6
@@ -34,18 +34,18 @@ int entry_flush_test(void)// The PMU event we use only works on Power7 or later SKIP_IF (!have_hwcap (PPC_FEATURE_ARCH_2_06 )); -if (read_debugfs_file ("powerpc/rfi_flush" , &rfi_flush_orig) <0 ) { +if (read_debugfs_int ("powerpc/rfi_flush" , &rfi_flush_orig) <0 ) {perror ("Unable to read powerpc/rfi_flush debugfs file" );SKIP_IF (1 ); } -if (read_debugfs_file ("powerpc/entry_flush" , &entry_flush_orig) <0 ) { +if (read_debugfs_int ("powerpc/entry_flush" , &entry_flush_orig) <0 ) {perror ("Unable to read powerpc/entry_flush debugfs file" );SKIP_IF (1 ); }if (rfi_flush_orig !=0 ) { -if (write_debugfs_file ("powerpc/rfi_flush" ,0 ) <0 ) { +if (write_debugfs_int ("powerpc/rfi_flush" ,0 ) <0 ) {perror ("error writing to powerpc/rfi_flush debugfs file" );FAIL_IF (1 ); }
@@ -105,7 +105,7 @@ int entry_flush_test(void)if (entry_flush == entry_flush_orig) { entry_flush = !entry_flush_orig; -if (write_debugfs_file ("powerpc/entry_flush" , entry_flush) <0 ) { +if (write_debugfs_int ("powerpc/entry_flush" , entry_flush) <0 ) {perror ("error writing to powerpc/entry_flush debugfs file" );return 1 ; }
@@ -120,12 +120,12 @@ int entry_flush_test(void)set_dscr (0 ); -if (write_debugfs_file ("powerpc/rfi_flush" , rfi_flush_orig) <0 ) { +if (write_debugfs_int ("powerpc/rfi_flush" , rfi_flush_orig) <0 ) {perror ("unable to restore original value of powerpc/rfi_flush debugfs file" );return 1 ; } -if (write_debugfs_file ("powerpc/entry_flush" , entry_flush_orig) <0 ) { +if (write_debugfs_int ("powerpc/entry_flush" , entry_flush_orig) <0 ) {perror ("unable to restore original value of powerpc/entry_flush debugfs file" );return 1 ; }
tools/testing/selftests/powerpc/security/rfi_flush.c +6 -6
@@ -34,18 +34,18 @@ int rfi_flush_test(void)// The PMU event we use only works on Power7 or later SKIP_IF (!have_hwcap (PPC_FEATURE_ARCH_2_06 )); -if (read_debugfs_file ("powerpc/rfi_flush" , &rfi_flush_orig) <0 ) { +if (read_debugfs_int ("powerpc/rfi_flush" , &rfi_flush_orig) <0 ) {perror ("Unable to read powerpc/rfi_flush debugfs file" );SKIP_IF (1 ); } -if (read_debugfs_file ("powerpc/entry_flush" , &entry_flush_orig) <0 ) { +if (read_debugfs_int ("powerpc/entry_flush" , &entry_flush_orig) <0 ) { have_entry_flush =0 ; }else { have_entry_flush =1 ;if (entry_flush_orig !=0 ) { -if (write_debugfs_file ("powerpc/entry_flush" ,0 ) <0 ) { +if (write_debugfs_int ("powerpc/entry_flush" ,0 ) <0 ) {perror ("error writing to powerpc/entry_flush debugfs file" );return 1 ; }
@@ -105,7 +105,7 @@ int rfi_flush_test(void)if (rfi_flush == rfi_flush_orig) { rfi_flush = !rfi_flush_orig; -if (write_debugfs_file ("powerpc/rfi_flush" , rfi_flush) <0 ) { +if (write_debugfs_int ("powerpc/rfi_flush" , rfi_flush) <0 ) {perror ("error writing to powerpc/rfi_flush debugfs file" );return 1 ; }
@@ -120,13 +120,13 @@ int rfi_flush_test(void)set_dscr (0 ); -if (write_debugfs_file ("powerpc/rfi_flush" , rfi_flush_orig) <0 ) { +if (write_debugfs_int ("powerpc/rfi_flush" , rfi_flush_orig) <0 ) {perror ("unable to restore original value of powerpc/rfi_flush debugfs file" );return 1 ; }if (have_entry_flush) { -if (write_debugfs_file ("powerpc/entry_flush" , entry_flush_orig) <0 ) { +if (write_debugfs_int ("powerpc/entry_flush" , entry_flush_orig) <0 ) {perror ("unable to restore original value of powerpc/entry_flush " "debugfs file" );return 1 ;
tools/testing/selftests/powerpc/security/uaccess_flush.c +9 -9
@@ -36,30 +36,30 @@ int uaccess_flush_test(void)// The PMU event we use only works on Power7 or later SKIP_IF (!have_hwcap (PPC_FEATURE_ARCH_2_06 )); -if (read_debugfs_file ("powerpc/rfi_flush" , &rfi_flush_orig) <0 ) { +if (read_debugfs_int ("powerpc/rfi_flush" , &rfi_flush_orig) <0 ) {perror ("Unable to read powerpc/rfi_flush debugfs file" );SKIP_IF (1 ); } -if (read_debugfs_file ("powerpc/entry_flush" , &entry_flush_orig) <0 ) { +if (read_debugfs_int ("powerpc/entry_flush" , &entry_flush_orig) <0 ) {perror ("Unable to read powerpc/entry_flush debugfs file" );SKIP_IF (1 ); } -if (read_debugfs_file ("powerpc/uaccess_flush" , &uaccess_flush_orig) <0 ) { +if (read_debugfs_int ("powerpc/uaccess_flush" , &uaccess_flush_orig) <0 ) {perror ("Unable to read powerpc/entry_flush debugfs file" );SKIP_IF (1 ); }if (rfi_flush_orig !=0 ) { -if (write_debugfs_file ("powerpc/rfi_flush" ,0 ) <0 ) { +if (write_debugfs_int ("powerpc/rfi_flush" ,0 ) <0 ) {perror ("error writing to powerpc/rfi_flush debugfs file" );FAIL_IF (1 ); } }if (entry_flush_orig !=0 ) { -if (write_debugfs_file ("powerpc/entry_flush" ,0 ) <0 ) { +if (write_debugfs_int ("powerpc/entry_flush" ,0 ) <0 ) {perror ("error writing to powerpc/entry_flush debugfs file" );FAIL_IF (1 ); }
@@ -119,7 +119,7 @@ int uaccess_flush_test(void)if (uaccess_flush == uaccess_flush_orig) { uaccess_flush = !uaccess_flush_orig; -if (write_debugfs_file ("powerpc/uaccess_flush" , uaccess_flush) <0 ) { +if (write_debugfs_int ("powerpc/uaccess_flush" , uaccess_flush) <0 ) {perror ("error writing to powerpc/uaccess_flush debugfs file" );return 1 ; }
@@ -134,17 +134,17 @@ int uaccess_flush_test(void)set_dscr (0 ); -if (write_debugfs_file ("powerpc/rfi_flush" , rfi_flush_orig) <0 ) { +if (write_debugfs_int ("powerpc/rfi_flush" , rfi_flush_orig) <0 ) {perror ("unable to restore original value of powerpc/rfi_flush debugfs file" );return 1 ; } -if (write_debugfs_file ("powerpc/entry_flush" , entry_flush_orig) <0 ) { +if (write_debugfs_int ("powerpc/entry_flush" , entry_flush_orig) <0 ) {perror ("unable to restore original value of powerpc/entry_flush debugfs file" );return 1 ; } -if (write_debugfs_file ("powerpc/uaccess_flush" , uaccess_flush_orig) <0 ) { +if (write_debugfs_int ("powerpc/uaccess_flush" , uaccess_flush_orig) <0 ) {perror ("unable to restore original value of powerpc/uaccess_flush debugfs file" );return 1 ; }
tools/testing/selftests/powerpc/utils.c +22 -12
@@ -105,6 +105,24 @@ int read_auxv(char *buf, ssize_t buf_size)return 0 ; } +int read_debugfs_file (const char *subpath,char *buf,size_t count) +{ +char path[PATH_MAX ] ="/sys/kernel/debug/" ; + +strncat (path, subpath,sizeof (path) -strlen (path) -1 ); + +return read_file (path, buf, count,NULL ); +} + +int write_debugfs_file (const char *subpath,const char *buf,size_t count) +{ +char path[PATH_MAX ] ="/sys/kernel/debug/" ; + +strncat (path, subpath,sizeof (path) -strlen (path) -1 ); + +return write_file (path, buf, count); +} +void *find_auxv_entry (int type,char *auxv) {ElfW (auxv_t) *p;
@@ -197,16 +215,12 @@ int read_sysfs_file(char *fpath, char *result, size_t result_size)return read_file (path, result, result_size,NULL ); } -int read_debugfs_file (char *debugfs_file,int *result) +int read_debugfs_int (const char *debugfs_file,int *result) {int err; -char path[PATH_MAX ];char value[16 ] = {0 }; -strcpy (path,"/sys/kernel/debug/" ); -strncat (path, debugfs_file,PATH_MAX -strlen (path) -1 ); - - err =read_file (path, value,sizeof (value) -1 ,NULL ); + err =read_debugfs_file (debugfs_file, value,sizeof (value) -1 );if (err)return err;
@@ -215,17 +229,13 @@ int read_debugfs_file(char *debugfs_file, int *result)return 0 ; } -int write_debugfs_file (char *debugfs_file,int result) +int write_debugfs_int (const char *debugfs_file,int result) { -char path[PATH_MAX ];char value[16 ]; -strcpy (path,"/sys/kernel/debug/" ); -strncat (path, debugfs_file,PATH_MAX -strlen (path) -1 ); -snprintf (value,16 ,"%d" , result); -return write_file (path, value,strlen (value)); +return write_debugfs_file (debugfs_file, value,strlen (value)); } static long perf_event_open(struct perf_event_attr *hw_event, pid_t pid,
Signature · 1 line
2.39.1
PATCH 5/5 selftests/powerpc: Add automatically allocating read_file
In reply to Benjamin GrayA couple of tests roll their own auto-allocating file read logic.
Add a generic implementation and convert them to use it.
---
v4: * Make allocation more concise using realloc(NULL, length)
without special casing initial malloc(length)
--- .../testing/selftests/powerpc/include/utils.h | 1 + .../selftests/powerpc/nx-gzip/gzfht_test.c | 38 +-------- .../selftests/powerpc/syscalls/Makefile | 2 +- .../selftests/powerpc/syscalls/rtas_filter.c | 81 +++---------------- tools/testing/selftests/powerpc/utils.c | 58 +++++++++++++ 5 files changed, 71 insertions(+), 109 deletions(-)
tools/testing/selftests/powerpc/include/utils.h +1 -0
@@ -42,6 +42,7 @@ int parse_ulong(const char *buffer, size_t count, unsigned long *result, int basint read_file (const char *path,char *buf,size_t count,size_t *len);int write_file (const char *path,const char *buf,size_t count); +int read_file_alloc (const char *path,char **buf,size_t *len);int read_long (const char *path,long *result,int base);int write_long (const char *path,long result,int base);int read_ulong (const char *path,unsigned long *result,int base);
tools/testing/selftests/powerpc/nx-gzip/gzfht_test.c +1 -37
@@ -143,42 +143,6 @@ int gzip_header_blank(char *buf)return i; } -/* Caller must free the allocated buffer return nonzero on error. */ -int read_alloc_input_file (char *fname,char **buf,size_t *bufsize) -{ -int err; -struct stat statbuf; -char *p; -size_t num_bytes; - -if (stat (fname, &statbuf)) { -perror (fname); -return -1 ; - } - -assert (NULL != (p = (char *)malloc (statbuf.st_size))); - - err =read_file (fname, p, statbuf.st_size, &num_bytes); -if (err) { -perror (fname); - goto fail; - } - -if (num_bytes != statbuf.st_size) { -fprintf (stderr,"Actual bytes != expected bytes\n" ); - err =-1 ; - goto fail; - } - - *buf = p; - *bufsize = num_bytes; -return 0 ; - -fail: -free (p); -return err; -} -/* * Z_SYNC_FLUSH as described in zlib.h. * Returns number of appended bytes
@@ -245,7 +209,7 @@ int compress_file(int argc, char **argv, void *handle)fprintf (stderr,"usage: %s <fname>\n" , argv[0 ]);exit (-1 ); } -if (read_alloc_input_file (argv[1 ], &inbuf, &inlen)) +if (read_file_alloc (argv[1 ], &inbuf, &inlen))exit (-1 );fprintf (stderr,"file %s read, %ld bytes\n" , argv[1 ], inlen);
tools/testing/selftests/powerpc/syscalls/Makefile +1 -1
@@ -6,4 +6,4 @@ CFLAGS += -I../../../../../usr/include top_srcdir = ../../../../.. include ../../lib.mk -$(TEST_GEN_PROGS): ../harness.c +$(TEST_GEN_PROGS): ../harness.c ../utils.c
tools/testing/selftests/powerpc/syscalls/rtas_filter.c +10 -71
108 lines not on this page · raw message
tools/testing/selftests/powerpc/utils.c +58 -0
64 lines not on this page · raw message
Signature · 1 line
2.39.1
cover Expand selftest utils
Started this when writing tests for a feature I'm working on, needing a way to read/write numbers to system files. After writing some utils to safely handle file IO and parsing, I realised I'd made the ~6th file read/write implementation and only(?) number parser that checks all the failure modes when expecting to parse a single number from a file.
So these utils ended up becoming this series. I also modified some other test utils I came across while doing so. My understanding is selftests are not expected to be backported, so I wasn't concerned about only introducing new utils and leaving the existing implementations be.
V3: * Add reviewed-by from previous version
* Fix write(2) call to include creation mode
V4: * Drop patches merged in v3
* Miscellaneous refactoring
* Bigger changes mentioned on the relevant patch
Benjamin Gray (5):
selftests/powerpc: Add generic read/write file util selftests/powerpc: Add read/write debugfs file, int selftests/powerpc: Parse long/unsigned long value safely selftests/powerpc: Add {read,write}_{long,ulong} selftests/powerpc: Add automatically allocating read_file
tools/testing/selftests/powerpc/dscr/dscr.h | 34 +- .../selftests/powerpc/dscr/dscr_sysfs_test.c | 25 +- .../testing/selftests/powerpc/include/utils.h | 20 +- .../selftests/powerpc/nx-gzip/gzfht_test.c | 52 +-- tools/testing/selftests/powerpc/pmu/lib.c | 34 +- .../selftests/powerpc/ptrace/core-pkey.c | 28 +- .../selftests/powerpc/security/entry_flush.c | 12 +- .../selftests/powerpc/security/rfi_flush.c | 12 +- .../powerpc/security/uaccess_flush.c | 18 +- .../selftests/powerpc/syscalls/Makefile | 2 +- .../selftests/powerpc/syscalls/rtas_filter.c | 81 +--- tools/testing/selftests/powerpc/utils.c | 412 +++++++++++++++--- 12 files changed, 431 insertions(+), 299 deletions(-)
base-commit: ca272751ba18ca8f137af631cbc9f3f987fab6e3 --
2.39.1
PATCH 4/5 selftests/powerpc: Add {read,write}_{long,ulong}
Add helper functions to read and write (unsigned) long values directly from/to files. One of the kernel interfaces uses hex strings, so we need to allow passing a base too.
---
v4: * Set errno where it isn't already set
--- tools/testing/selftests/powerpc/dscr/dscr.h | 9 +-- .../selftests/powerpc/dscr/dscr_sysfs_test.c | 14 ++-- .../testing/selftests/powerpc/include/utils.h | 4 + tools/testing/selftests/powerpc/pmu/lib.c | 9 +-- tools/testing/selftests/powerpc/utils.c | 81 +++++++++++++++++++ 5 files changed, 95 insertions(+), 22 deletions(-)
tools/testing/selftests/powerpc/dscr/dscr.h +2 -7
@@ -65,26 +65,21 @@ inline void set_dscr_usr(unsigned long val)unsigned long get_default_dscr (void ) {int err; -char buf[16 ] = {0 };unsigned long val; - err =read_file (DSCR_DEFAULT , buf,sizeof (buf) -1 ,NULL ); + err =read_ulong (DSCR_DEFAULT , &val,16 );if (err) {perror ("read() failed" );exit (1 ); } -sscanf (buf,"%lx" , &val);return val; }void set_default_dscr (unsigned long val) {int err; -char buf[16 ]; -sprintf (buf,"%lx\n" , val); - - err =write_file (DSCR_DEFAULT , buf,strlen (buf)); + err =write_ulong (DSCR_DEFAULT , val,16 );if (err) {perror ("write() failed" );exit (1 );
tools/testing/selftests/powerpc/dscr/dscr_sysfs_test.c +7 -7
@@ -12,16 +12,16 @@static int check_cpu_dscr_default (char *file,unsigned long val) { -char buf[10 ] = {0 }; -int rc; +unsigned long cpu_dscr; +int err; - rc =read_file (file, buf,sizeof (buf) -1 ,NULL ); -if (rc) -return rc; + err =read_ulong (file, &cpu_dscr,16 ); +if (err) +return err; -if (strtol (buf,NULL ,16 ) != val) { +if (cpu_dscr != val) {printf ("DSCR match failed: %ld (system) %ld (cpu)\n" , - val,strtol (buf,NULL ,16 )); + val, cpu_dscr);return 1 ; }return 0 ;
tools/testing/selftests/powerpc/include/utils.h +4 -0
@@ -42,6 +42,10 @@ int parse_ulong(const char *buffer, size_t count, unsigned long *result, int basint read_file (const char *path,char *buf,size_t count,size_t *len);int write_file (const char *path,const char *buf,size_t count); +int read_long (const char *path,long *result,int base); +int write_long (const char *path,long result,int base); +int read_ulong (const char *path,unsigned long *result,int base); +int write_ulong (const char *path,unsigned long result,int base);int read_debugfs_file (const char *debugfs_file,char *buf,size_t count);int write_debugfs_file (const char *debugfs_file,const char *buf,size_t count);int read_debugfs_int (const char *debugfs_file,int *result);
tools/testing/selftests/powerpc/pmu/lib.c +1 -8
@@ -192,15 +192,8 @@ bool require_paranoia_below(int level) {int err;long current; -char buf[16 ] = {0 }; - err =read_file (PARANOID_PATH , buf,sizeof (buf) -1 ,NULL ); -if (err) { -printf ("Couldn't read " PARANOID_PATH "?\n" ); -return false; - } - - err =parse_long (buf,sizeof (buf), ¤t,10 ); + err =read_long (PARANOID_PATH , ¤t,10 );if (err) {printf ("Couldn't parse " PARANOID_PATH "?\n" );return false;
tools/testing/selftests/powerpc/utils.c +81 -0
87 lines not on this page · raw message
Signature · 1 line
2.39.1
PATCH 1/5 selftests/powerpc: Add generic read/write file util
In reply to Benjamin GrayFile read/write is reimplemented in about 5 different ways in the various PowerPC selftests. This indicates it should be a common util.
Add a common read_file / write_file implementation and convert users to it where (easily) possible.
---
v4: * Set errno as expected by many users that call perror() if
it fails (e.g., read_debugfs_file users in powerpc/security)
--- tools/testing/selftests/powerpc/dscr/dscr.h | 33 ++--- .../selftests/powerpc/dscr/dscr_sysfs_test.c | 21 +-- .../testing/selftests/powerpc/include/utils.h | 2 + .../selftests/powerpc/nx-gzip/gzfht_test.c | 48 +++---- tools/testing/selftests/powerpc/pmu/lib.c | 31 ++-- .../selftests/powerpc/ptrace/core-pkey.c | 28 ++-- tools/testing/selftests/powerpc/utils.c | 133 +++++++++++------- 7 files changed, 128 insertions(+), 168 deletions(-)
tools/testing/selftests/powerpc/dscr/dscr.h +8 -25
@@ -64,48 +64,31 @@ inline void set_dscr_usr(unsigned long val)/* Default DSCR access */ unsigned long get_default_dscr (void ) { -int fd =-1 , ret; -char buf[16 ]; +int err; +char buf[16 ] = {0 };unsigned long val; -if (fd ==-1 ) { - fd =open (DSCR_DEFAULT ,O_RDONLY ); -if (fd ==-1 ) { -perror ("open() failed" ); -exit (1 ); - } - } -memset (buf,0 ,sizeof (buf)); -lseek (fd,0 ,SEEK_SET ); - ret =read (fd, buf,sizeof (buf)); -if (ret ==-1 ) { + err =read_file (DSCR_DEFAULT , buf,sizeof (buf) -1 ,NULL ); +if (err) {perror ("read() failed" );exit (1 ); }sscanf (buf,"%lx" , &val); -close (fd);return val; }void set_default_dscr (unsigned long val) { -int fd =-1 , ret; +int err;char buf[16 ]; -if (fd ==-1 ) { - fd =open (DSCR_DEFAULT ,O_RDWR ); -if (fd ==-1 ) { -perror ("open() failed" ); -exit (1 ); - } - }sprintf (buf,"%lx\n" , val); - ret =write (fd, buf,strlen (buf)); -if (ret ==-1 ) { + + err =write_file (DSCR_DEFAULT , buf,strlen (buf)); +if (err) {perror ("write() failed" );exit (1 ); } -close (fd); }double uniform_deviate (int seed)
tools/testing/selftests/powerpc/dscr/dscr_sysfs_test.c +5 -16
@@ -12,24 +12,13 @@static int check_cpu_dscr_default (char *file,unsigned long val) { -char buf[10 ]; -int fd, rc; +char buf[10 ] = {0 }; +int rc; - fd =open (file,O_RDWR ); -if (fd ==-1 ) { -perror ("open() failed" ); -return 1 ; - } + rc =read_file (file, buf,sizeof (buf) -1 ,NULL ); +if (rc) +return rc; - rc =read (fd, buf,sizeof (buf)); -if (rc ==-1 ) { -perror ("read() failed" ); -close (fd); -return 1 ; - } -close (fd); - - buf[rc] ='\0' ;if (strtol (buf,NULL ,16 ) != val) {printf ("DSCR match failed: %ld (system) %ld (cpu)\n" , val,strtol (buf,NULL ,16 ));
tools/testing/selftests/powerpc/include/utils.h +2 -0
@@ -33,6 +33,8 @@ void *get_auxv_entry(int type);int pick_online_cpu (void ); +int read_file (const char *path,char *buf,size_t count,size_t *len); +int write_file (const char *path,const char *buf,size_t count);int read_debugfs_file (char *debugfs_file,int *result);int write_debugfs_file (char *debugfs_file,int result);int read_sysfs_file (char *debugfs_file,char *result,size_t result_size);
tools/testing/selftests/powerpc/nx-gzip/gzfht_test.c +18 -30
74 lines not on this page · raw message
tools/testing/selftests/powerpc/pmu/lib.c +8 -23
@@ -190,38 +190,23 @@ int parse_proc_maps(void)bool require_paranoia_below (int level) { +int err;long current; -char *end, buf[16 ]; -FILE *f; -bool rc; +char *end; +char buf[16 ] = {0 }; - rc = false; - - f =fopen (PARANOID_PATH ,"r" ); -if (!f) { -perror ("fopen" ); - goto out; - } - -if (!fgets (buf,sizeof (buf), f)) { + err =read_file (PARANOID_PATH , buf,sizeof (buf) -1 ,NULL ); +if (err) {printf ("Couldn't read " PARANOID_PATH "?\n" ); - goto out_close; +return false; } current =strtol (buf, &end,10 );if (end == buf) {printf ("Couldn't parse " PARANOID_PATH "?\n" ); - goto out_close; +return false; } -if (current >= level) - goto out_close; - - rc = true; -out_close: -fclose (f); -out: -return rc; +return current < level; } -
tools/testing/selftests/powerpc/ptrace/core-pkey.c +8 -20
@@ -348,15 +348,11 @@ static int parent(struct shared_info *info, pid_t pid)static int write_core_pattern (const char *core_pattern) { -size_t len =strlen (core_pattern), ret; -FILE *f; +int err; - f =fopen (core_pattern_file,"w" ); -SKIP_IF_MSG (!f,"Try with root privileges" ); - - ret =fwrite (core_pattern,1 , len, f); -fclose (f); -if (ret != len) { + err =write_file (core_pattern_file, core_pattern,strlen (core_pattern)); +if (err) { +SKIP_IF_MSG (err == -EPERM ,"Try with root privileges" );perror ("Error writing to core_pattern file" );return TEST_FAIL ; }
@@ -366,8 +362,8 @@ static int write_core_pattern(const char *core_pattern)static int setup_core_pattern (char **core_pattern_,bool *changed_) { -FILE *f;char *core_pattern; +size_t len;int ret; core_pattern =malloc (PATH_MAX );
@@ -376,22 +372,14 @@ static int setup_core_pattern(char **core_pattern_, bool *changed_)return TEST_FAIL ; } - f =fopen (core_pattern_file,"r" ); -if (!f) { -perror ("Error opening core_pattern file" ); - ret =TEST_FAIL ; - goto out; - } - - ret =fread (core_pattern,1 ,PATH_MAX -1 , f); -fclose (f); -if (!ret) { + ret =read_file (core_pattern_file, core_pattern,PATH_MAX -1 , &len); +if (ret) {perror ("Error reading core_pattern file" ); ret =TEST_FAIL ; goto out; } - core_pattern[ret] ='\0' ; + core_pattern[len] ='\0' ;/* Check whether we can predict the name of the core file. */ if (!strcmp (core_pattern,"core" ) || !strcmp (core_pattern,"core.%p" ))
tools/testing/selftests/powerpc/utils.c +79 -54
178 lines not on this page · raw message
Signature · 1 line
2.39.1
Michael Ellerman
AppliedQuoted 6 lines · Benjamin Gray
Started this when writing tests for a feature I'm working on, needing a way to read/write numbers to system files. After writing some utils to safely handle file IO and parsing, I realised I'd made the ~6th file read/write implementation and only(?) number parser that checks all the failure modes when expecting to parse a single number from a file.
Quoted 6 lines
So these utils ended up becoming this series. I also modified some other test utils I came across while doing so. My understanding is selftests are not expected to be backported, so I wasn't concerned about only introducing new utils and leaving the existing implementations be.
[...]
Applied to powerpc/next.
[1/5] selftests/powerpc: Add generic read/write file util
https://git.kernel.org/powerpc/c/a974f0c131891027fe8490e654a220151b4caa82 [2/5] selftests/powerpc: Add read/write debugfs file, int https://git.kernel.org/powerpc/c/121d340be9a17ed89d523c56203908c01e09a306 [3/5] selftests/powerpc: Parse long/unsigned long value safely https://git.kernel.org/powerpc/c/d1bc05b7bf02f8635fe6c445f67d78f85234cbb7 [4/5] selftests/powerpc: Add {read,write}_{long,ulong} https://git.kernel.org/powerpc/c/5c20de57888f0962e25a0eeec1a59c98056fc42e [5/5] selftests/powerpc: Add automatically allocating read_file https://git.kernel.org/powerpc/c/8d7253dc447473dfcf3f09fb0fa2bd6f7d05b43b
cheers