mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH] scripts: bareboximd: Fix -c option
@ 2022-02-28 10:15 Sascha Hauer
  2022-02-28 11:48 ` Andrej Picej
  0 siblings, 1 reply; 2+ messages in thread
From: Sascha Hauer @ 2022-02-28 10:15 UTC (permalink / raw)
  To: Barebox List; +Cc: Andrej Picej

Using mmap in read_file doesn't work when the same file is written
afterwards with write_file. This problem was fixed in 738601e125
("scripts/common: fix write_file when opened with mmap"). Using mmap in
read_file was removed in 07b87a0908 ("scripts/common: Do not mmap in
read_file_2()") but then re-introduced for bareboximd in 013e8ea757
("scripts: bareboximd: Use mmap when possible"). I'll put my brown paper
bag on for this :(

This patch fixes the issue by explicitly avoiding mmap when the file
is written later as done with the -c option to bareboximd

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Reported-by: Andrej Picej <andrej.picej@norik.com>
---
 common/imd.c         | 7 +++++--
 scripts/bareboximd.c | 9 ++++++---
 2 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/common/imd.c b/common/imd.c
index 9ca0248523..1100e6878a 100644
--- a/common/imd.c
+++ b/common/imd.c
@@ -22,7 +22,8 @@ static inline void read_file_2_free(void *buf)
 	free(buf);
 }
 
-static int imd_read_file(const char *filename, size_t *size, void **outbuf)
+static int imd_read_file(const char *filename, size_t *size, void **outbuf,
+			 bool allow_mmap)
 {
 	return read_file_2(filename, size, outbuf, 0x100000);
 }
@@ -439,6 +440,7 @@ int imd_command(int argc, char *argv[])
 	char *str;
 	uint32_t checksum = 0;
 	uint32_t verify = 0;
+	bool allow_mmap = true;
 
 	imd_command_verbose = 0;
 
@@ -462,6 +464,7 @@ int imd_command(int argc, char *argv[])
 			break;
 		case 'c':
 			checksum = 1;
+			allow_mmap = false;
 			break;
 		case 'V':
 			verify = 1;
@@ -478,7 +481,7 @@ int imd_command(int argc, char *argv[])
 
 	filename = argv[optind];
 
-	ret = imd_read_file(filename, &size, &buf);
+	ret = imd_read_file(filename, &size, &buf, allow_mmap);
 	if (ret && ret != -EFBIG)
 		return -errno;
 
diff --git a/scripts/bareboximd.c b/scripts/bareboximd.c
index 2d4750d7fb..d01bd27007 100644
--- a/scripts/bareboximd.c
+++ b/scripts/bareboximd.c
@@ -55,9 +55,10 @@ static unsigned long simple_strtoul(const char *cp, char **endp, unsigned int ba
 	return strtoul(cp, endp, base);
 }
 
-static int imd_read_file(const char *filename, size_t *size, void **outbuf)
+static int imd_read_file(const char *filename, size_t *size, void **outbuf,
+			 bool allow_mmap)
 {
-	void *buf;
+	void *buf = MAP_FAILED;
 	int fd, ret;
 	size_t fsize;
 
@@ -74,7 +75,9 @@ static int imd_read_file(const char *filename, size_t *size, void **outbuf)
 		goto close;
 	}
 
-	buf = mmap(NULL, fsize, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
+	if (allow_mmap)
+		buf = mmap(NULL, fsize, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
+
 	if (buf == MAP_FAILED) {
 		close(fd);
 		return read_file_2(filename, size, outbuf, 0x100000);
-- 
2.30.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] scripts: bareboximd: Fix -c option
  2022-02-28 10:15 [PATCH] scripts: bareboximd: Fix -c option Sascha Hauer
@ 2022-02-28 11:48 ` Andrej Picej
  0 siblings, 0 replies; 2+ messages in thread
From: Andrej Picej @ 2022-02-28 11:48 UTC (permalink / raw)
  To: Sascha Hauer, Barebox List



On 28. 02. 22 11:15, Sascha Hauer wrote:
> Using mmap in read_file doesn't work when the same file is written
> afterwards with write_file. This problem was fixed in 738601e125
> ("scripts/common: fix write_file when opened with mmap"). Using mmap in
> read_file was removed in 07b87a0908 ("scripts/common: Do not mmap in
> read_file_2()") but then re-introduced for bareboximd in 013e8ea757
> ("scripts: bareboximd: Use mmap when possible"). I'll put my brown paper
> bag on for this :(
> 
> This patch fixes the issue by explicitly avoiding mmap when the file
> is written later as done with the -c option to bareboximd
> 
> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
> Reported-by: Andrej Picej <andrej.picej@norik.com>

You can add: Tested-by: Andrej Picej <andrej.picej@norik.com>
if you want.

Thanks.

> ---
>   common/imd.c         | 7 +++++--
>   scripts/bareboximd.c | 9 ++++++---
>   2 files changed, 11 insertions(+), 5 deletions(-)
> 
> diff --git a/common/imd.c b/common/imd.c
> index 9ca0248523..1100e6878a 100644
> --- a/common/imd.c
> +++ b/common/imd.c
> @@ -22,7 +22,8 @@ static inline void read_file_2_free(void *buf)
>   	free(buf);
>   }
>   
> -static int imd_read_file(const char *filename, size_t *size, void **outbuf)
> +static int imd_read_file(const char *filename, size_t *size, void **outbuf,
> +			 bool allow_mmap)
>   {
>   	return read_file_2(filename, size, outbuf, 0x100000);
>   }
> @@ -439,6 +440,7 @@ int imd_command(int argc, char *argv[])
>   	char *str;
>   	uint32_t checksum = 0;
>   	uint32_t verify = 0;
> +	bool allow_mmap = true;
>   
>   	imd_command_verbose = 0;
>   
> @@ -462,6 +464,7 @@ int imd_command(int argc, char *argv[])
>   			break;
>   		case 'c':
>   			checksum = 1;
> +			allow_mmap = false;
>   			break;
>   		case 'V':
>   			verify = 1;
> @@ -478,7 +481,7 @@ int imd_command(int argc, char *argv[])
>   
>   	filename = argv[optind];
>   
> -	ret = imd_read_file(filename, &size, &buf);
> +	ret = imd_read_file(filename, &size, &buf, allow_mmap);
>   	if (ret && ret != -EFBIG)
>   		return -errno;
>   
> diff --git a/scripts/bareboximd.c b/scripts/bareboximd.c
> index 2d4750d7fb..d01bd27007 100644
> --- a/scripts/bareboximd.c
> +++ b/scripts/bareboximd.c
> @@ -55,9 +55,10 @@ static unsigned long simple_strtoul(const char *cp, char **endp, unsigned int ba
>   	return strtoul(cp, endp, base);
>   }
>   
> -static int imd_read_file(const char *filename, size_t *size, void **outbuf)
> +static int imd_read_file(const char *filename, size_t *size, void **outbuf,
> +			 bool allow_mmap)
>   {
> -	void *buf;
> +	void *buf = MAP_FAILED;
>   	int fd, ret;
>   	size_t fsize;
>   
> @@ -74,7 +75,9 @@ static int imd_read_file(const char *filename, size_t *size, void **outbuf)
>   		goto close;
>   	}
>   
> -	buf = mmap(NULL, fsize, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
> +	if (allow_mmap)
> +		buf = mmap(NULL, fsize, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
> +
>   	if (buf == MAP_FAILED) {
>   		close(fd);
>   		return read_file_2(filename, size, outbuf, 0x100000);

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-02-28 12:24 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-28 10:15 [PATCH] scripts: bareboximd: Fix -c option Sascha Hauer
2022-02-28 11:48 ` Andrej Picej

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox