mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/3] scripts/common: Do not mmap in read_file_2()
@ 2022-02-11  9:42 Sascha Hauer
  2022-02-11  9:42 ` [PATCH 2/3] scripts: bareboximd: Use mmap when possible Sascha Hauer
  2022-02-11  9:42 ` [PATCH 3/3] Revert "scripts/common: fix write_file when opened with mmap" Sascha Hauer
  0 siblings, 2 replies; 6+ messages in thread
From: Sascha Hauer @ 2022-02-11  9:42 UTC (permalink / raw)
  To: Barebox List

Using mmap() in read_file_2() leads to problems because there are
cases where the buffer returned from read_file_2() is modified and
then written back to the same file. This doesn't work when the
original file has been mmapped instead of being read into an allocated
buffer.

Using mmap() was introduced for a usecase where the system is very tight
in memory and bareboximd ran out of memory. Support for this usecase is
removed here, we'll bring it back in the next patch.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 scripts/common.c | 53 +++++++++++++++++++++++-------------------------
 1 file changed, 25 insertions(+), 28 deletions(-)

diff --git a/scripts/common.c b/scripts/common.c
index 154d6dffcb..3d07be3630 100644
--- a/scripts/common.c
+++ b/scripts/common.c
@@ -17,7 +17,7 @@
 int read_file_2(const char *filename, size_t *size, void **outbuf, size_t max_size)
 {
 	off_t fsize;
-	ssize_t rsize;
+	ssize_t read_size, now;
 	int ret, fd;
 	void *buf;
 
@@ -37,8 +37,10 @@ int read_file_2(const char *filename, size_t *size, void **outbuf, size_t max_si
 		goto close;
 	}
 
-	if (fsize < max_size)
-		max_size = fsize;
+	if (max_size < fsize)
+		read_size = max_size;
+	else
+		read_size = fsize;
 
 	if (lseek(fd, 0, SEEK_SET) == -1) {
 		fprintf(stderr, "Cannot seek to start %s: %s\n", filename, strerror(errno));
@@ -46,35 +48,30 @@ int read_file_2(const char *filename, size_t *size, void **outbuf, size_t max_si
 		goto close;
 	}
 
-	buf = mmap(NULL, max_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
-	if (buf == MAP_FAILED ) {
-		buf = malloc(max_size);
-		if (!buf) {
-			fprintf(stderr, "Cannot allocate memory\n");
-			ret = -ENOMEM;
-			goto close;
-		}
-
-		*outbuf = buf;
+	buf = malloc(read_size);
+	if (!buf) {
+		fprintf(stderr, "Cannot allocate memory\n");
+		ret = -ENOMEM;
+		goto close;
+	}
 
-		while (*size < max_size) {
-			rsize = read(fd, buf, max_size - *size);
-			if (rsize == 0) {
-				ret = -EIO;
-				goto free;
-			}
+	*outbuf = buf;
 
-			if (rsize < 0) {
-				ret = -errno;
-				goto free;
-			}
+	while (read_size) {
+		now = read(fd, buf, read_size);
+		if (now == 0) {
+			ret = -EIO;
+			goto free;
+		}
 
-			buf += rsize;
-			*size += rsize;
+		if (now < 0) {
+			ret = -errno;
+			goto free;
 		}
-	} else {
-		*outbuf = buf;
-		*size = max_size;
+
+		buf += now;
+		*size += now;
+		read_size -= now;
 	}
 
 	ret = 0;
-- 
2.30.2


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


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

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

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-11  9:42 [PATCH 1/3] scripts/common: Do not mmap in read_file_2() Sascha Hauer
2022-02-11  9:42 ` [PATCH 2/3] scripts: bareboximd: Use mmap when possible Sascha Hauer
2022-02-11  9:42 ` [PATCH 3/3] Revert "scripts/common: fix write_file when opened with mmap" Sascha Hauer
2022-02-24 13:22   ` Andrej Picej
2022-02-28 10:16     ` Sascha Hauer
2022-02-28 11:40       ` Andrej Picej

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