From aa49d86b2d80687c36897c3b9f91954d8f1ec9b0 Mon Sep 17 00:00:00 2001 From: Sascha Hauer Date: Tue, 8 Apr 2014 17:20:03 +0200 Subject: saveenv: Properly detect write errors The return value check of the write call is completely bogus. We check if we have written at minimum sizeof(struct envfs_super) bytes instead of all bytes. Properly check for all bytes written instead and allow write to write less bytes than requested. Do not use write_full because this file is compiled for userspace aswell. Signed-off-by: Sascha Hauer --- common/environment.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/common/environment.c b/common/environment.c index 9f4e098285..c834902727 100644 --- a/common/environment.c +++ b/common/environment.c @@ -167,7 +167,7 @@ int envfs_save(const char *filename, const char *dirname) struct envfs_super *super; int envfd, size, ret; struct action_data data; - void *buf = NULL; + void *buf = NULL, *wbuf; data.writep = NULL; data.base = dirname; @@ -201,11 +201,19 @@ int envfs_save(const char *filename, const char *dirname) goto out1; } - if (write(envfd, buf, size + sizeof(struct envfs_super)) < - sizeof(struct envfs_super)) { - perror("write"); - ret = -1; /* FIXME */ - goto out; + size += sizeof(struct envfs_super); + + wbuf = buf; + + while (size) { + ssize_t now = write(envfd, wbuf, size); + if (now < 0) { + ret = -errno; + goto out; + } + + wbuf += now; + size -= now; } ret = 0; -- cgit v1.2.3