From 91e4a1760dee22e9a40ca52f1e5a1b549d9e066d Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 5 May 2010 15:37:18 +0930 Subject: tdb: fix short write logic in tdb_new_database Commit 207a213c/24fed55d purported to fix the problem of signals during tdb_new_database (which could cause a spurious short write, hence a failure). However, the code is wrong: newdb+written is not correct. Fix this by introducing a general tdb_write_all() and using it here and in the tracing code. Cc: Stefan Metzmacher Signed-off-by: Rusty Russell --- lib/tdb/common/tdb.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'lib/tdb/common/tdb.c') diff --git a/lib/tdb/common/tdb.c b/lib/tdb/common/tdb.c index dac3f4e666..4d8c5fc59c 100644 --- a/lib/tdb/common/tdb.c +++ b/lib/tdb/common/tdb.c @@ -989,10 +989,24 @@ int tdb_repack(struct tdb_context *tdb) return 0; } +/* Even on files, we can get partial writes due to signals. */ +bool tdb_write_all(int fd, const void *buf, size_t count) +{ + while (count) { + size_t ret; + ret = write(fd, buf, count); + if (ret < 0) + return false; + buf = (const char *)buf + ret; + count -= ret; + } + return true; +} + #ifdef TDB_TRACE static void tdb_trace_write(struct tdb_context *tdb, const char *str) { - if (write(tdb->tracefd, str, strlen(str)) != strlen(str)) { + if (!tdb_write_alltdb->tracefd, str, strlen(str)) { close(tdb->tracefd); tdb->tracefd = -1; } -- cgit