src/Makefile | 2 ++
src/delta_lease.c | 4 ++--
src/direct.c | 4 ++--
src/host_id.c | 12 ++++++------
src/paxos_lease.c | 16 ++++++++--------
src/sanlock_internal.h | 1 +
wdmd/Makefile | 2 +-
wdmd/main.c | 26 +++++++++++++++++---------
8 files changed, 39 insertions(+), 28 deletions(-)
New commits:
commit f95d08fd484e3c5caba4e2d7152e530d658c7af0
Author: David Teigland <teigland(a)redhat.com>
Date: Wed Aug 3 12:35:22 2011 -0500
sanlock/wdmd: use monotonic time
to avoid problems if the real time is changed
diff --git a/src/Makefile b/src/Makefile
index 3aadb5d..edc299b 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -26,6 +26,7 @@ CMD_SOURCE = \
task.c \
token_manager.c \
watchdog.c \
+ monotime.c \
sanlock_sock.c
LIB_SOURCE = \
@@ -39,6 +40,7 @@ LIB_SOURCE = \
direct.c \
task.c \
direct_lib.c \
+ monotime.c \
sanlock_sock.c
CFLAGS += -D_GNU_SOURCE -g \
diff --git a/src/delta_lease.c b/src/delta_lease.c
index a7f46d1..bf5ad82 100644
--- a/src/delta_lease.c
+++ b/src/delta_lease.c
@@ -286,7 +286,7 @@ int delta_lease_acquire(struct task *task,
}
write_new:
- new_ts = time(NULL);
+ new_ts = monotime();
leader.timestamp = new_ts;
leader.owner_id = host_id;
leader.owner_generation++;
@@ -402,7 +402,7 @@ int delta_lease_renew(struct task *task,
return SANLK_RENEW_DIFF;
}
- new_ts = time(NULL);
+ new_ts = monotime();
if (leader.timestamp >= new_ts) {
log_erros(sp, "delta_renew timestamp too small");
diff --git a/src/direct.c b/src/direct.c
index 03c6a16..8b09fe8 100644
--- a/src/direct.c
+++ b/src/direct.c
@@ -326,7 +326,7 @@ int direct_live_id(struct task *task,
if (rv < 0)
return rv;
- start = time(NULL);
+ start = monotime();
while (1) {
sleep(1);
@@ -350,7 +350,7 @@ int direct_live_id(struct task *task,
break;
}
- if (time(NULL) - start > task->host_dead_seconds) {
+ if (monotime() - start > task->host_dead_seconds) {
*live = 0;
break;
}
diff --git a/src/host_id.c b/src/host_id.c
index 822e8cb..7341d65 100644
--- a/src/host_id.c
+++ b/src/host_id.c
@@ -187,7 +187,7 @@ int host_id_check(struct task *task, struct space *sp)
return 0;
}
- gap = time(NULL) - last_success;
+ gap = monotime() - last_success;
if (gap >= task->id_renewal_fail_seconds) {
log_erros(sp, "host_id_check failed %d", gap);
@@ -245,7 +245,7 @@ static void *lockspace_thread(void *arg_in)
setup_task_aio(&task, main_task.use_aio, HOSTID_AIO_CB_SIZE);
memcpy(task.name, sp->space_name, NAME_ID_SIZE);
- last_attempt = time(NULL);
+ last_attempt = monotime();
rv = open_disk(&sp->host_id_disk);
if (rv < 0) {
@@ -259,7 +259,7 @@ static void *lockspace_thread(void *arg_in)
sp->space_name, our_host_name_global,
sp->host_id, &leader);
delta_result = result;
- delta_length = time(NULL) - last_attempt;
+ delta_length = monotime() - last_attempt;
if (result == SANLK_OK)
last_success = leader.timestamp;
@@ -303,7 +303,7 @@ static void *lockspace_thread(void *arg_in)
if (stop)
break;
- if (time(NULL) - last_success < task.id_renewal_seconds) {
+ if (monotime() - last_success < task.id_renewal_seconds) {
sleep(1);
continue;
} else {
@@ -312,13 +312,13 @@ static void *lockspace_thread(void *arg_in)
usleep(500000);
}
- last_attempt = time(NULL);
+ last_attempt = monotime();
result = delta_lease_renew(&task, sp, &sp->host_id_disk,
sp->space_name, delta_result,
&leader, &leader);
delta_result = result;
- delta_length = time(NULL) - last_attempt;
+ delta_length = monotime() - last_attempt;
if (result == SANLK_OK)
last_success = leader.timestamp;
diff --git a/src/paxos_lease.c b/src/paxos_lease.c
index 1069639..25165f1 100644
--- a/src/paxos_lease.c
+++ b/src/paxos_lease.c
@@ -446,7 +446,7 @@ static int run_ballot(struct task *task, struct token *token, int
num_hosts,
/* lver and mbal are already set */
dblock.inp = token->host_id;
dblock.inp2 = token->host_generation;
- dblock.inp3 = time(NULL);
+ dblock.inp3 = monotime();
}
dblock.bal = dblock.mbal;
dblock.checksum = dblock_checksum(&dblock);
@@ -1102,7 +1102,7 @@ int paxos_lease_acquire(struct task *task,
host_id_disk.fd);
}
- start = time(NULL);
+ start = monotime();
while (1) {
error = delta_lease_leader_read(task, &host_id_disk,
@@ -1157,15 +1157,15 @@ int paxos_lease_acquire(struct task *task,
by now
if we trust that the clocks are in sync among hosts, then this
- check could be: if (time(NULL) - host_id_leader.timestamp >
+ check could be: if (time() - host_id_leader.timestamp >
task->host_dead_seconds), but if the clocks are out of sync,
this check would easily give two hosts the lease.
- N.B. we need to be careful about ever comparing local time(NULL)
+ N.B. we need to be careful about ever comparing local time
to a time value we read off disk from another node that may
have different time. */
- if (time(NULL) - start > task->host_dead_seconds) {
+ if (monotime() - start > task->host_dead_seconds) {
log_token(token, "paxos_acquire host_id %llu expired %llu",
(unsigned long long)cur_leader.owner_id,
(unsigned long long)host_id_leader.timestamp);
@@ -1313,7 +1313,7 @@ int paxos_lease_acquire(struct task *task,
new_leader.write_id = token->host_id;
new_leader.write_generation = token->host_generation;
- new_leader.write_timestamp = time(NULL);
+ new_leader.write_timestamp = monotime();
if (new_num_hosts)
new_leader.num_hosts = new_num_hosts;
@@ -1376,7 +1376,7 @@ int paxos_lease_renew(struct task *task,
}
}
- new_leader.timestamp = time(NULL);
+ new_leader.timestamp = monotime();
new_leader.checksum = leader_checksum(&new_leader);
error = write_new_leader(task, token, &new_leader);
@@ -1450,7 +1450,7 @@ int paxos_lease_release(struct task *task,
leader.timestamp = LEASE_FREE;
leader.write_id = token->host_id;
leader.write_generation = token->host_generation;
- leader.write_timestamp = time(NULL);
+ leader.write_timestamp = monotime();
leader.checksum = leader_checksum(&leader);
error = write_new_leader(task, token, &leader, "paxos_release");
diff --git a/src/sanlock_internal.h b/src/sanlock_internal.h
index b25a8eb..833822e 100644
--- a/src/sanlock_internal.h
+++ b/src/sanlock_internal.h
@@ -26,6 +26,7 @@
#include "sanlock_rv.h"
#include "leader.h"
#include "list.h"
+#include "monotime.h"
#include <libaio.h>
diff --git a/wdmd/Makefile b/wdmd/Makefile
index be3f030..8b7330f 100644
--- a/wdmd/Makefile
+++ b/wdmd/Makefile
@@ -42,7 +42,7 @@ CFLAGS += -D_GNU_SOURCE -g \
-fdiagnostics-show-option \
-fPIE -DPIE
-CMD_LDFLAGS += -lwdmd
+CMD_LDFLAGS += -lwdmd -lrt
CMD_LDFLAGS += -Wl,-z,now -Wl,-z,relro -pie
LIB_LDFLAGS += -Wl,-z,relro -pie
diff --git a/wdmd/main.c b/wdmd/main.c
index d8994b4..8977a49 100644
--- a/wdmd/main.c
+++ b/wdmd/main.c
@@ -98,6 +98,14 @@ do { \
} while (0)
+static uint64_t monotime(void)
+{
+ struct timespec ts;
+
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+
+ return ts.tv_sec;
+}
/*
* test clients
@@ -341,7 +349,7 @@ static int test_clients(void)
int fail_count = 0;
int i;
- t = time(NULL);
+ t = monotime();
for (i = 0; i < client_size; i++) {
if (!client[i].used)
@@ -435,7 +443,7 @@ static int test_files(void)
if (rv < 0)
continue;
- t = time(NULL);
+ t = monotime();
if (t >= expire) {
log_error("test failed file %s renewal %llu expire %llu ",
@@ -523,7 +531,7 @@ static int check_script(int i)
if (!scripts[i].pid)
return 0;
- begin = time(NULL);
+ begin = monotime();
while (1) {
rv = waitpid(scripts[i].pid, &status, WNOHANG);
@@ -533,7 +541,7 @@ static int check_script(int i)
} else if (!rv) {
/* pid still running */
- if (time(NULL) - begin >= SCRIPT_WAIT_SECONDS) {
+ if (monotime() - begin >= SCRIPT_WAIT_SECONDS) {
rv = -ETIMEDOUT;
goto out;
}
@@ -549,7 +557,7 @@ static int check_script(int i)
} else {
/* pid state changed but still running */
- if (time(NULL) - begin >= 2) {
+ if (monotime() - begin >= 2) {
rv = -ETIMEDOUT;
goto out;
}
@@ -666,7 +674,7 @@ static void pet_watchdog(void)
rv = ioctl(dev_fd, WDIOC_KEEPALIVE, &unused);
- last_keepalive = time(NULL);
+ last_keepalive = monotime();
log_debug("keepalive %d", rv);
}
@@ -747,8 +755,8 @@ static int test_loop(void)
if (daemon_quit && !active_clients())
break;
- if (time(NULL) - test_time >= test_interval) {
- test_time = time(NULL);
+ if (monotime() - test_time >= test_interval) {
+ test_time = monotime();
log_debug("test_time %llu",
(unsigned long long)test_time);
@@ -761,7 +769,7 @@ static int test_loop(void)
pet_watchdog();
}
- sleep_seconds = test_time + test_interval - time(NULL);
+ sleep_seconds = test_time + test_interval - monotime();
poll_timeout = (sleep_seconds > 0) ? sleep_seconds * 1000 : 1;
log_debug("sleep_seconds %d", sleep_seconds);
}