summaryrefslogtreecommitdiff
path: root/src/shared/util.c
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2014-12-01 20:43:19 -0500
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2014-12-09 21:36:08 -0500
commit553acb7b6b8d4f16a4747b1f978e8b7888fbfb2c (patch)
treeb9a473c853c616b256ed3ea1dc5f8e9c7838b289 /src/shared/util.c
parentcb01aedc3b4ba70859267159fe716253e3551ec6 (diff)
treewide: sanitize loop_write
loop_write() didn't follow the usual systemd rules and returned status partially in errno and required extensive checks from callers. Some of the callers dealt with this properly, but many did not, treating partial writes as successful. Simplify things by conforming to usual rules.
Diffstat (limited to 'src/shared/util.c')
-rw-r--r--src/shared/util.c9
1 files changed, 6 insertions, 3 deletions
diff --git a/src/shared/util.c b/src/shared/util.c
index ff8835b72..26a4f72b4 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -2292,13 +2292,15 @@ ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll) {
return n;
}
-ssize_t loop_write(int fd, const void *buf, size_t nbytes, bool do_poll) {
+int loop_write(int fd, const void *buf, size_t nbytes, bool do_poll) {
const uint8_t *p = buf;
ssize_t n = 0;
assert(fd >= 0);
assert(buf);
+ errno = 0;
+
while (nbytes > 0) {
ssize_t k;
@@ -2317,14 +2319,15 @@ ssize_t loop_write(int fd, const void *buf, size_t nbytes, bool do_poll) {
}
if (k <= 0)
- return n > 0 ? n : (k < 0 ? -errno : 0);
+ /* We were not done yet, and a write error occured. */
+ return errno ? -errno : -EIO;
p += k;
nbytes -= k;
n += k;
}
- return n;
+ return 0;
}
int parse_size(const char *t, off_t base, off_t *size) {