Remove data loss possibility in the log_file struct (again) 14/243314/2
authorMateusz Majewski <m.majewski2@samsung.com>
Fri, 4 Sep 2020 12:25:08 +0000 (14:25 +0200)
committerMichal Bloch <m.bloch@partner.samsung.com>
Tue, 8 Sep 2020 07:17:04 +0000 (07:17 +0000)
This is the fixed version of the commit that was reverted. The
difference is in the

    if (written >= 0)

which used to be

    if (written > 0)

which is right if we are calling the write function. But the full_write
function is not the drop-in replacement; it returns 0 on success.

Change-Id: I68216849734b5c351b178613ff18636136aedb6d

Makefile.am
src/shared/log_file.c

index fea8f63..9e238c0 100644 (file)
@@ -468,7 +468,7 @@ src_tests_libdlog_prio_filter_pos_SOURCES = src/tests/libdlog_prio_filter_pos.c
 src_tests_libdlog_prio_filter_pos_CFLAGS = $(check_CFLAGS) -pthread
 src_tests_libdlog_prio_filter_pos_LDFLAGS = $(AM_LDFLAGS) -lpthread -Wl,--wrap=log_config_read
 
-src_tests_log_file_SOURCES = src/tests/log_file.c src/shared/log_file.c
+src_tests_log_file_SOURCES = src/tests/log_file.c src/shared/log_file.c src/shared/logcommon.c
 src_tests_log_file_CFLAGS = $(check_CFLAGS)
 src_tests_log_file_LDFLAGS = $(AM_LDFLAGS) -Wl,--wrap=strdup,--wrap=free,--wrap=memcpy,--wrap=snprintf,--wrap=open,--wrap=open64,--wrap=fstat,--wrap=fstat64,--wrap=rename,--wrap=dlogutil_entry_get_timestamp,--wrap=log_print_log_line,--wrap=dlogutil_entry_get_tag,--wrap=isatty
 
index 47f4ca1..46069b3 100644 (file)
@@ -261,9 +261,11 @@ int logfile_flush(struct log_file *file)
        if (!file->buffer.data)
                return 0;
 
-       int written = write(file->fd, file->buffer.data, file->buffer.position);
-       if (write > 0)
-               file->buffer.position = 0; // TODO: data loss possibility here
+       /* This can cause the logs to linger in the buffer in case of an error (TODO?), but it's better than
+        * data loss on partial write, and is not a real issue since everybody quits on write error. */
+       int written = full_write(file->fd, file->buffer.data, file->buffer.position);
+       if (written >= 0)
+               file->buffer.position = 0;
        return written;
 }