Skip to contentSkip to conversation

lib: string: retire deprecated strtok() in favor of reentrant strsep()

PATCH Applied Ahmad Fatoum 3 participants 7 messages Sep 14, 2020 – Sep 15, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

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
index 763ef500e574..2b699957e824 100644
@@ -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
index 50f8e2f87c9f..733b56730070 100644
@@ -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
#1 lore raw

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
index 6a5475d094e5..59163f678d51 100644
@@ -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
#2 lore raw

PATCH 3/5 sandbox: implement simple, ^C-interruptible, restart handler

In reply to Ahmad Fatoum

Typing 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
index 6b5a6dff15f9..5072b756e108 100644
@@ -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
index 9759a376ecb3..f0a3a7b510fc 100644
@@ -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
index 437fe3ecdff8..bbab3bd2312d 100644
@@ -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
#3 lore raw

PATCH 4/5 sandbox: implement actual sandbox reset via exec(2)

In reply to Ahmad Fatoum

From: 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
index c4d0ab4dbcde..b5213c662b1b 100644
@@ -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
index 27021222dc48..eb678b72dbcd 100644
@@ -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
index 5072b756e108..8ce739af72c1 100644
@@ -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
index f0a3a7b510fc..1ab48e52a00a 100644
@@ -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
index 72b7fbb5ac4b..83b97ffd49c7 100644
@@ -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
#4 lore raw

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
index 778ac3d6e60f..d0addef5af49 100644
@@ -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
#5 lore raw

PATCH 5/5 sandbox: hostfile: support registering images as barebox block devices

In reply to Ahmad Fatoum

While 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
index b5213c662b1b..0c1c7bd73a01 100644
@@ -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
index e2f44c4f7b0c..c3f9af97c451 100644
@@ -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
index 63839a1ccc68..778ac3d6e60f 100644
@@ -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
Applied
#6 lore raw
In reply to Ahmad Fatoum
Quoted 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 reentrant strsep(), which has existed in-tree for as long.

Quoted 7 lines

---
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