diff --git a/src/logging/writers/ascii/Ascii.cc b/src/logging/writers/ascii/Ascii.cc index a53464f925..fe39b86c76 100644 --- a/src/logging/writers/ascii/Ascii.cc +++ b/src/logging/writers/ascii/Ascii.cc @@ -465,7 +465,7 @@ bool Ascii::DoInit(const WriterInfo& info, int num_fields, const threading::Fiel if ( sfd < 0 ) { - Error(Fmt("cannot open %s: %s", sfname.data(), Strerror(errno))); + Error(Fmt("cannot open %s: %s", tmp_sfname.data(), Strerror(errno))); return false; } @@ -479,6 +479,8 @@ bool Ascii::DoInit(const WriterInfo& info, int num_fields, const threading::Fiel util::safe_write(sfd, "\n", 1); + util::safe_fsync(sfd); + util::safe_close(sfd); if ( rename(tmp_sfname.data(), sfname.data()) == -1 ) diff --git a/src/util.cc b/src/util.cc index cfa612a864..51527303c0 100644 --- a/src/util.cc +++ b/src/util.cc @@ -2162,6 +2162,24 @@ bool safe_pwrite(int fd, const unsigned char* data, size_t len, size_t offset) return true; } +void safe_fsync(int fd) + { + int r; + + do + { + r = fsync(fd); + } while ( r < 0 && errno == EINTR ); + + if ( r < 0 ) + { + char buf[128]; + zeek_strerror_r(errno, buf, sizeof(buf)); + fprintf(stderr, "safe_fsync error %d: %s\n", errno, buf); + abort(); + } + } + void safe_close(int fd) { /* diff --git a/src/util.h b/src/util.h index fa07f585a7..851191246a 100644 --- a/src/util.h +++ b/src/util.h @@ -461,6 +461,10 @@ extern bool safe_write(int fd, const char* data, int len); // Same as safe_write(), but for pwrite(). extern bool safe_pwrite(int fd, const unsigned char* data, size_t len, size_t offset); +// Like fsync() but handles interrupted system calls by retrying and +// aborts on unrecoverable errors. +extern void safe_fsync(int fd); + // Wraps close(2) to emit error messages and abort on unrecoverable errors. extern void safe_close(int fd);