lib: string: retire deprecated strtok() in favor of reentrant strsep()
Subsystems (2)
PATCH 1/5 lib: string: retire deprecated strtok() in favor of reentrant strsep()
With the recent changes to ARCH=sandbox, there are no remaining in-tree users for strtok() anymore. Out-of-tree users are better served by using the reentrant strsep(), which has existed in-tree for as long.
---
Should be applied after <[email protected]>
--- include/linux/string.h | 2 -- lib/string.c | 35 +++-------------------------------- 2 files changed, 3 insertions(+), 34 deletions(-)
include/linux/string.h +0 -2
@@ -8,9 +8,7 @@extern "C" {#endif -extern char * ___strtok;extern char *strpbrk (const char *,const char *); -extern char *strtok (char *,const char *);extern char *strsep (char **,const char *);extern char *strsep_unescaped (char **,const char *);extern __kernel_size_t strspn (const char *,const char *);
lib/string.c +3 -32
@@ -13,6 +13,9 @@* * Fri Jun 25 1999, Ingo Oeser <[email protected]> * - Added strsep() which will replace strtok() soon (because strsep() is * reentrant and should be faster). Use only strsep() in new code, please. +* * Mon Sep 14 2020, Ahmad Fatoum <[email protected]> +* - Kissed strtok() goodbye +* */ #include <linux/types.h>
@@ -20,8 +23,6 @@#include <linux/ctype.h> #include <malloc.h> -char * ___strtok; -#ifndef __HAVE_ARCH_STRNICMP/** * strnicmp - Case insensitive, length-limited string comparison
@@ -396,36 +397,6 @@ char * strpbrk(const char * cs,const char * ct)#endif EXPORT_SYMBOL (strpbrk); -#ifndef __HAVE_ARCH_STRTOK -/** -* strtok - Split a string into tokens -* @s: The string to be searched -* @ct: The characters to search for -* -* WARNING: strtok is deprecated, use strsep instead. -*/ -char *strtok (char * s,const char * ct) -{ -char *sbegin, *send; - - sbegin = s ? s : ___strtok; -if (!sbegin) { -return NULL ; - } - sbegin +=strspn (sbegin,ct); -if (*sbegin =='\0' ) { - ___strtok =NULL ; -return (NULL ); - } - send =strpbrk ( sbegin, ct); -if (send && *send !='\0' ) - *send++ ='\0' ; - ___strtok = send; -return (sbegin); -} -#endif -EXPORT_SYMBOL (strtok); -#ifndef __HAVE_ARCH_STRSEP/** * strsep - Split a string into tokens
Signature · 7 lines
2.28.0 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 2/5 ls: don't print . and .. on recursive ls
They are already omitted on normal ls and they don't really add a new information, so drop them for the recursive case as well.
--- commands/ls.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
commands/ls.c +6 -5
@@ -75,8 +75,13 @@ int ls(const char *path, ulong flags)if (!dir)return -errno; -while ((d =readdir (dir))) +while ((d =readdir (dir))) { +if (!strcmp (d->d_name,"." )) +continue ; +if (!strcmp (d->d_name,".." )) +continue ;string_list_add_sorted (&sl, d->d_name); + }closedir (dir);
@@ -99,10 +104,6 @@ int ls(const char *path, ulong flags) goto out;string_list_for_each_entry (entry, &sl) { -if (!strcmp (entry->str,"." )) -continue ; -if (!strcmp (entry->str,".." )) -continue ;sprintf (tmp,"%s/%s" , path, entry->str); ret =lstat (tmp, &s);
Signature · 7 lines
2.28.0 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 3/5 sandbox: implement simple, ^C-interruptible, restart handler
In reply to Ahmad FatoumTyping reset in sandbox results in hang() while the terminal is not cooked and ^C is ineffective. Only way to terminate barebox then is via kill. Reinstate cooked mode on reset, so ^C termination is possible.
--- arch/sandbox/board/poweroff.c | 12 ++++++++++++ arch/sandbox/mach-sandbox/include/mach/linux.h | 1 + arch/sandbox/os/common.c | 6 ++++++ 3 files changed, 19 insertions(+)
arch/sandbox/board/poweroff.c +12 -0
@@ -1,6 +1,7 @@#include <common.h> #include <init.h> #include <poweroff.h> +#include <restart.h> #include <mach/linux.h> static void sandbox_poweroff (struct poweroff_handler *poweroff)
@@ -8,9 +9,20 @@ static void sandbox_poweroff(struct poweroff_handler *poweroff)linux_exit (); } +static void sandbox_rst_hang (struct restart_handler *rst) +{ +linux_hang (); +} + +static struct restart_handler rst_hang = { + .name ="hang" , + .restart = sandbox_rst_hang +}; +static int poweroff_register_feature (void ) {poweroff_handler_register_fn (sandbox_poweroff); +restart_handler_register (&rst_hang);return 0 ; }
arch/sandbox/mach-sandbox/include/mach/linux.h +1 -0
@@ -17,6 +17,7 @@ ssize_t linux_write(int fd, const void *buf, size_t count);off_t linux_lseek (int fildes,off_t offset);int linux_tstc (int fd);void __attribute__((noreturn))linux_exit (void ); +void linux_hang (void );int linux_execve (const char * filename,char *const argv[],char *const envp[]);
arch/sandbox/os/common.c +6 -0
@@ -122,6 +122,12 @@ void __attribute__((noreturn)) linux_exit(void)exit (0 ); } +void linux_hang (void ) +{ +cookmode (); +/* falls through to generic hang() */ +} +int linux_open (const char *filename,int readwrite) {return open (filename, readwrite ?O_RDWR :O_RDONLY );
Signature · 7 lines
2.28.0 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 4/5 sandbox: implement actual sandbox reset via exec(2)
In reply to Ahmad FatoumFrom: Ahmad Fatoum <[email protected]>
On Linux, /proc/self/exe is a symlink to the originally exec(2)d executable. We can exec that with the original argv to simulate a reset. This is useful for shorter development cycles on sandbox and in future, could be used to test barebox behavior around resets (e.g. reset reason can be passed through via libc environment).
We leave the original hanging reset in place though, because:
- Many boards have multiple reset providers and incoming patches will allow users to select a specific one. Having this on sandbox as well makes testing easier.
- /proc/self/exe is Linux-specific and wouldn't work when being run on e.g. BSDs or macOS
--- arch/sandbox/Kconfig | 7 +++ arch/sandbox/Makefile | 2 +- arch/sandbox/board/poweroff.c | 13 ++++++ .../sandbox/mach-sandbox/include/mach/linux.h | 1 + arch/sandbox/os/common.c | 43 ++++++++++++++++--- arch/sandbox/os/tap.c | 2 +- 6 files changed, 59 insertions(+), 9 deletions(-)
arch/sandbox/Kconfig +7 -0
@@ -24,6 +24,13 @@ config SANDBOX_UNWIND select ARCH_HAS_STACK_DUMP depends on KASAN +config SANDBOX_REEXEC + prompt "exec(2) reset handler" + def_bool y + help + The normal reset handler hangs barebox. On Linux, barebox + instead can exec itself to simulate a reset. + config PHYS_ADDR_T_64BIT bool
arch/sandbox/Makefile +1 -1
@@ -25,7 +25,7 @@ KBUILD_CFLAGS += -Dmalloc=barebox_malloc -Dcalloc=barebox_calloc \ -Dglob=barebox_glob -Dglobfree=barebox_globfree \ -Dioctl=barebox_ioctl -Dfstat=barebox_fstat \ -Dopendir=barebox_opendir -Dreaddir=barebox_readdir \ - -Dclosedir=barebox_closedir \ + -Dclosedir=barebox_closedir -Dreadlink=barebox_readlink \ -Doptarg=barebox_optarg -Doptind=barebox_optind machdirs := $(patsubst %,arch/sandbox/mach-%/,$(machine-y))
arch/sandbox/board/poweroff.c +13 -0
@@ -19,11 +19,24 @@ static struct restart_handler rst_hang = { .restart = sandbox_rst_hang }; +static void sandbox_rst_reexec (struct restart_handler *rst) +{ +linux_reexec (); +} + +static struct restart_handler rst_reexec = { + .name ="reexec" , .priority =200 , + .restart = sandbox_rst_reexec, +}; +static int poweroff_register_feature (void ) {poweroff_handler_register_fn (sandbox_poweroff);restart_handler_register (&rst_hang); +if (IS_ENABLED (CONFIG_SANDBOX_REEXEC )) +restart_handler_register (&rst_reexec); +return 0 ; }coredevice_initcall (poweroff_register_feature);
arch/sandbox/mach-sandbox/include/mach/linux.h +1 -0
@@ -18,6 +18,7 @@ off_t linux_lseek(int fildes, off_t offset);int linux_tstc (int fd);void __attribute__((noreturn))linux_exit (void );void linux_hang (void ); +void linux_reexec (void );int linux_execve (const char * filename,char *const argv[],char *const envp[]);
arch/sandbox/os/common.c +36 -7
103 lines not on this page · raw message
arch/sandbox/os/tap.c +1 -1
@@ -30,7 +30,7 @@ int tap_alloc(const char *dev)struct ifreq ifr;int fd, err; -if ((fd =open ("/dev/net/tun" ,O_RDWR )) <0 ) { +if ((fd =open ("/dev/net/tun" ,O_RDWR |O_CLOEXEC )) <0 ) {perror ("could not open /dev/net/tun" );return -1 ; }
Signature · 7 lines
2.28.0 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 1/1 fixup! sandbox: implement actual sandbox reset via exec(2)
add_image modifies the argument passed to it, we want to reexec with the original argv however, thus make a copy of the argument.
--- arch/sandbox/os/common.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
arch/sandbox/os/common.c +4 -2
@@ -247,10 +247,10 @@ extern void mem_malloc_init(void *start, void *end);extern char *strsep_unescaped (char **s,const char *ct); -static int add_image (char *str,char *devname_template,int *devname_number) +static int add_image (const char *_str,char *devname_template,int *devname_number) {struct hf_info *hf =malloc (sizeof (struct hf_info )); -char *filename, *devname; +char *str, *filename, *devname;char tmp[16 ];int readonly =0 , cdev =0 , blkdev =0 ;struct stat s;
@@ -260,6 +260,8 @@ static int add_image(char *str, char *devname_template, int *devname_number)if (!hf)return -1 ; + str =strdup (_str); + filename =strsep_unescaped (&str,"," );while ((opt =strsep_unescaped (&str,"," ))) {if (!strcmp (opt,"ro" ))
Signature · 7 lines
2.28.0 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 5/5 sandbox: hostfile: support registering images as barebox block devices
In reply to Ahmad FatoumWhile we can mount file systems on cdevs in barebox, partition table parsing only works for block devices. Allow for --image=argument,blkdev to try to mount an image as block device.
This will fail for files that aren't of a multiple of the 512 byte block size. Host OS block devices are suitable for use as barebox block devices always, so that's the default unless overridden with a ,cdev suffix.
The initcall level has been changed to occur after fs initcall level. This is required, because we can't have automounts without / mounted.
--- arch/sandbox/Kconfig | 2 + arch/sandbox/board/hostfile.c | 116 ++++++++++++++---- .../mach-sandbox/include/mach/hostfile.h | 1 + arch/sandbox/os/common.c | 15 ++- 4 files changed, 108 insertions(+), 26 deletions(-)
arch/sandbox/Kconfig +2 -0
@@ -7,6 +7,8 @@ config SANDBOX select ARCH_HAS_UBSAN_SANITIZE_ALL select HAVE_ARCH_KASAN select HAS_DMA + select BLOCK + select BLOCK_WRITE default y config ARCH_TEXT_BASE
arch/sandbox/board/hostfile.c +91 -25
188 lines not on this page · raw message
arch/sandbox/mach-sandbox/include/mach/hostfile.h +1 -0
@@ -7,6 +7,7 @@ struct hf_info {unsigned long long size;const char *devname;const char *filename; + unsigned int is_blockdev:1; };int barebox_register_filedev (struct hf_info *hf);
arch/sandbox/os/common.c +14 -1
@@ -252,7 +252,7 @@ static int add_image(char *str, char *devname_template, int *devname_number)struct hf_info *hf =malloc (sizeof (struct hf_info ));char *filename, *devname;char tmp[16 ]; -int readonly =0 ; +int readonly =0 , cdev =0 , blkdev =0 ;struct stat s;char *opt;int fd, ret;
@@ -264,6 +264,10 @@ static int add_image(char *str, char *devname_template, int *devname_number)while ((opt =strsep_unescaped (&str,"," ))) {if (!strcmp (opt,"ro" )) readonly =1 ; +if (!strcmp (opt,"cdev" )) + cdev =1 ; +if (!strcmp (opt,"blkdev" )) + blkdev =1 ; }/* parses: "devname=filename" */
@@ -282,6 +286,7 @@ static int add_image(char *str, char *devname_template, int *devname_number) fd =open (filename, (readonly ?O_RDONLY :O_RDWR ) |O_CLOEXEC ); hf->fd = fd; hf->filename = filename; + hf->is_blockdev = blkdev;if (fd <0 ) {perror ("open" );
@@ -301,6 +306,8 @@ static int add_image(char *str, char *devname_template, int *devname_number)perror ("ioctl" ); goto err_out; } +if (!cdev) + hf->is_blockdev =1 ; }if (hf->size <=SIZE_MAX ) hf->base = (unsigned long)mmap(NULL, hf->size,
@@ -312,6 +319,12 @@ static int add_image(char *str, char *devname_template, int *devname_number)if (hf->base == (unsigned long )MAP_FAILED )printf ("warning: mmapping %s failed: %s\n" , filename,strerror (errno)); +if (blkdev && hf->size %512 !=0 ) { +printf ("warning: registering %s as block device failed: invalid block size\n" , + filename); +return -EINVAL ; + } + ret =barebox_register_filedev (hf);if (ret) goto err_out;
Signature · 7 lines
2.28.0 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
Sascha Hauer
AppliedQuoted 5 lines · Ahmad Fatoum
With the recent changes to ARCH=sandbox, there are no remaining in-tree users for
strtok()anymore. Out-of-tree users are better served by using the reentrantstrsep(), which has existed in-tree for as long.
Quoted 7 lines
Signed-off-by Ahmad Fatoum <[email protected]>---
Should be applied after <[email protected]>--- include/linux/string.h | 2 -- lib/string.c | 35 +++-------------------------------- 2 files changed, 3 insertions(+), 34 deletions(-)
Applied, thanks
Sascha
Signature · 9 lines
Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 | _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox