mirror of
https://github.com/troglobit/finit.git
synced 2026-10-17 10:49:34 +00:00
cond: don't rely on mtime for condition management
TIL, using mtimes for tracking event orderings is a monumentally bad idea (queue the nodding UNIX-beards). Mtimes are in wallclock time which is not necessarily monotonically increasing. A user may adjust the time, an NTP daemon will continously tune the clock and so on. Instead, store an explicit generation number in each condition file, which will be monotonically increased by finit on each reconf.
This commit is contained in:
1 parent
d6b2ac94dd
commit
b0663d04e4
4 files changed
+76
-18
No files matched your search
+23
-14
@@ -28,14 +28,6 @@
|
||||
#include "cond.h"
|
||||
#include "service.h"
|
||||
|
||||
static inline int timespec_newer(const struct timespec *a,
|
||||
const struct timespec *b)
|
||||
{
|
||||
if (a->tv_sec != b->tv_sec)
|
||||
return a->tv_sec > b->tv_sec;
|
||||
|
||||
return a->tv_nsec >= b->tv_nsec;
|
||||
}
|
||||
|
||||
const char *condstr(enum cond_state s)
|
||||
{
|
||||
@@ -57,18 +49,35 @@ const char *cond_path(const char *name)
|
||||
return file;
|
||||
}
|
||||
|
||||
unsigned int cond_get_gen(const char *path)
|
||||
{
|
||||
unsigned int gen;
|
||||
FILE *fp;
|
||||
int ret;
|
||||
|
||||
fp = fopen(path, "r");
|
||||
if (!fp)
|
||||
return 0;
|
||||
|
||||
ret = fscanf(fp, "%u", &gen);
|
||||
fclose(fp);
|
||||
|
||||
return (ret == 1) ? gen : 0;
|
||||
}
|
||||
|
||||
enum cond_state cond_get_path(const char *path)
|
||||
{
|
||||
struct stat st, st_reconf;
|
||||
int cgen, rgen;
|
||||
|
||||
if (stat(path, &st))
|
||||
rgen = cond_get_gen(COND_RECONF);
|
||||
if (!rgen)
|
||||
return COND_OFF;
|
||||
|
||||
if (stat(COND_RECONF, &st_reconf) ||
|
||||
timespec_newer(&st.st_mtim, &st_reconf.st_mtim))
|
||||
return COND_ON;
|
||||
cgen = cond_get_gen(path);
|
||||
if (!cgen)
|
||||
return COND_OFF;
|
||||
|
||||
return COND_FLUX;
|
||||
return (cgen == rgen) ? COND_ON : COND_FLUX;
|
||||
}
|
||||
|
||||
enum cond_state cond_get(const char *name)
|
||||
|
||||
Reference in new issue
Block a user