From 33d52ab92f2f0bfd706e6f343d172618d1e03f3d Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 12 Nov 2015 11:17:01 +0100 Subject: journald: rework --sync/--rotate logic to use CLOCK_MONOTONIC timestamp files Previously, we'd rely on the mtime timestamps of the touch files to see if our sync/rotation requests were already suppressed. This means we rely on CLOCK_REALTIME timestamps. With this patch we instead store the CLOCK_MONOTONIC timestamp *in* the touch files, and avoid relying on mtime. This should make things more reliable when the clock or underlying mtime granularity is not very good. This also adds warning messages if writing any of the flag files fails. --- src/basic/fileio.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'src/basic/fileio.c') diff --git a/src/basic/fileio.c b/src/basic/fileio.c index 619dafb517..be6e327690 100644 --- a/src/basic/fileio.c +++ b/src/basic/fileio.c @@ -28,8 +28,10 @@ #include "fileio.h" #include "fs-util.h" #include "hexdecoct.h" +#include "parse-util.h" #include "path-util.h" #include "random-util.h" +#include "stdio-util.h" #include "string-util.h" #include "strv.h" #include "umask-util.h" @@ -1149,3 +1151,37 @@ int tempfn_random_child(const char *p, const char *extra, char **ret) { *ret = path_kill_slashes(t); return 0; } + +int write_timestamp_file_atomic(const char *fn, usec_t n) { + char ln[DECIMAL_STR_MAX(n)+2]; + + /* Creates a "timestamp" file, that contains nothing but a + * usec_t timestamp, formatted in ASCII. */ + + if (n <= 0 || n >= USEC_INFINITY) + return -ERANGE; + + xsprintf(ln, USEC_FMT "\n", n); + + return write_string_file(fn, ln, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC); +} + +int read_timestamp_file(const char *fn, usec_t *ret) { + _cleanup_free_ char *ln = NULL; + uint64_t t; + int r; + + r = read_one_line_file(fn, &ln); + if (r < 0) + return r; + + r = safe_atou64(ln, &t); + if (r < 0) + return r; + + if (t <= 0 || t >= (uint64_t) USEC_INFINITY) + return -ERANGE; + + *ret = (usec_t) t; + return 0; +} -- cgit v1.2.3-54-g00ecf