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.
This commit is contained in:
Lennart Poettering
2015-11-12 11:17:01 +01:00
parent b97e7fabae
commit 33d52ab92f
4 changed files with 61 additions and 13 deletions
+36
View File
@@ -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;
}
+4
View File
@@ -28,6 +28,7 @@
#include <sys/types.h>
#include "macro.h"
#include "time-util.h"
typedef enum {
WRITE_STRING_FILE_CREATE = 1,
@@ -77,3 +78,6 @@ int open_tmpfile(const char *path, int flags);
int tempfn_xxxxxx(const char *p, const char *extra, char **ret);
int tempfn_random(const char *p, const char *extra, char **ret);
int tempfn_random_child(const char *p, const char *extra, char **ret);
int write_timestamp_file_atomic(const char *fn, usec_t n);
int read_timestamp_file(const char *fn, usec_t *ret);