From 69fb8c62a31e767be1464a7ccfe2a5bac331c4cc Mon Sep 17 00:00:00 2001
From: Laurent Bercot
Date: Mon, 18 Jul 2022 12:39:01 +0000
Subject: Fix allread errno management
Signed-off-by: Laurent Bercot
---
doc/libstddjb/allreadwrite.html | 2 +-
src/libposixplz/doublefork.c | 1 +
src/librandom/random_devurandom.c | 4 ++++
src/libstddjb/allreadwrite.c | 7 +------
src/libstddjb/fd_catn.c | 4 +++-
src/libstddjb/openreadfileclose.c | 8 +++++++-
6 files changed, 17 insertions(+), 9 deletions(-)
diff --git a/doc/libstddjb/allreadwrite.html b/doc/libstddjb/allreadwrite.html
index 1c31022..8003431 100644
--- a/doc/libstddjb/allreadwrite.html
+++ b/doc/libstddjb/allreadwrite.html
@@ -166,7 +166,7 @@ function.
Equivalent to allreadwrite(&fd_read, fd, s, len)
: attempts
to read len bytes from fd into s, looping around
fd_read() if necessary, until either len bytes are read or
-an error occurs. EOF is reported as EPIPE.
+an error occurs.
diff --git a/src/libposixplz/doublefork.c b/src/libposixplz/doublefork.c
index 6038a9f..c310a9f 100644
--- a/src/libposixplz/doublefork.c
+++ b/src/libposixplz/doublefork.c
@@ -3,6 +3,7 @@
#include
#include
#include
+
#include
#include
#include
diff --git a/src/librandom/random_devurandom.c b/src/librandom/random_devurandom.c
index 0ad752b..5f6b791 100644
--- a/src/librandom/random_devurandom.c
+++ b/src/librandom/random_devurandom.c
@@ -1,6 +1,7 @@
/* ISC license. */
#include
+#include
#include
#include
@@ -11,12 +12,15 @@ void random_devurandom (char *s, size_t n)
{
static int random_fd = -1 ;
size_t r ;
+ int e = errno ;
if (random_fd < 0)
{
random_fd = openbc_read("/dev/urandom") ;
if (random_fd < 0)
strerr_diefu2sys(111, "open ", "/dev/urandom") ;
}
+ errno = EPIPE ;
r = allread(random_fd, s, n) ;
if (r < n) strerr_diefu2sys(111, "read from ", "/dev/urandom") ;
+ errno = e ;
}
diff --git a/src/libstddjb/allreadwrite.c b/src/libstddjb/allreadwrite.c
index 0e9351a..19e4c1c 100644
--- a/src/libstddjb/allreadwrite.c
+++ b/src/libstddjb/allreadwrite.c
@@ -1,6 +1,5 @@
/* ISC license. */
-#include
#include
size_t allreadwrite (io_func_ref op, int fd, char *buf, size_t len)
@@ -9,11 +8,7 @@ size_t allreadwrite (io_func_ref op, int fd, char *buf, size_t len)
while (len)
{
ssize_t w = (*op)(fd, buf, len) ;
- if (w <= 0)
- {
- if (!w) errno = 0 ;
- break ;
- }
+ if (w <= 0) break ;
written += w ;
buf += w ;
len -= w ;
diff --git a/src/libstddjb/fd_catn.c b/src/libstddjb/fd_catn.c
index 182a054..4f1016e 100644
--- a/src/libstddjb/fd_catn.c
+++ b/src/libstddjb/fd_catn.c
@@ -14,10 +14,12 @@ off_t fd_catn (int from, int to, off_t n)
char buf[BSIZE] ;
while (n >= BSIZE)
{
+ size_t v ;
ssize_t r = fd_read(from, buf, BSIZE) ;
if (r == -1) return w ;
if (!r) return (errno = EPIPE, w) ;
- if (allwrite(to, buf, r) < r) return w ;
+ v = allwrite(to, buf, r) ;
+ if (v < r) return w + v ;
n -= r ; w += r ;
}
diff --git a/src/libstddjb/openreadfileclose.c b/src/libstddjb/openreadfileclose.c
index afa895a..144f264 100644
--- a/src/libstddjb/openreadfileclose.c
+++ b/src/libstddjb/openreadfileclose.c
@@ -1,6 +1,8 @@
/* ISC license. */
#include
+#include
+
#include
#include
#include
@@ -8,6 +10,7 @@
int openreadfileclose (char const *file, stralloc *sa, size_t limit)
{
size_t n ;
+ int e = errno ;
int fd = openbc_read(file) ;
if (fd < 0) return 0 ;
{
@@ -18,11 +21,14 @@ int openreadfileclose (char const *file, stralloc *sa, size_t limit)
if (limit && (limit < n)) n = limit ;
if (!stralloc_ready_tuned(sa, sa->len + n, 0, 0, 1)) goto err ;
{
- size_t r = allread(fd, sa->s + sa->len, n) ;
+ size_t r ;
+ errno = EPIPE ;
+ r = allread(fd, sa->s + sa->len, n) ;
sa->len += r ;
if (r < n) goto err ;
}
fd_close(fd) ;
+ errno = e ;
return 1 ;
err:
--
cgit v1.2.3