chiark / gitweb /
timedated: when performing "SetTime" compensate for program lag
[elogind.git] / src / timedate / timedated.c
index 23efe832a74acb8a4cbb41e100a062fe71a73342..88d57e9d5432da609c5b989ed460aec6c500a304 100644 (file)
@@ -23,7 +23,6 @@
 #include <string.h>
 #include <unistd.h>
 
-#include "sd-id128.h"
 #include "sd-messages.h"
 #include "sd-event.h"
 #include "sd-bus.h"
 #include "strv.h"
 #include "def.h"
 #include "clock-util.h"
-#include "conf-files.h"
 #include "path-util.h"
 #include "fileio-label.h"
-#include "label.h"
 #include "bus-util.h"
 #include "bus-error.h"
 #include "bus-common-errors.h"
@@ -543,6 +540,7 @@ static int method_set_time(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bu
         Context *c = userdata;
         int64_t utc;
         struct timespec ts;
+        usec_t start;
         struct tm* tm;
         int r;
 
@@ -590,6 +588,13 @@ static int method_set_time(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bu
         if (r == 0)
                 return 1;
 
+        /* adjust ts for time spent in program */
+        r = sd_bus_message_get_monotonic_usec(m, &start);
+        if (r < 0 && r != -ENODATA)
+                return r;
+        if (r >= 0)
+                timespec_store(&ts, timespec_load(&ts) + (now(CLOCK_MONOTONIC) - start));
+
         /* Set system clock */
         if (clock_settime(CLOCK_REALTIME, &ts) < 0) {
                 log_error_errno(errno, "Failed to set local time: %m");
@@ -730,6 +735,8 @@ int main(int argc, char *argv[]) {
         if (r < 0)
                 goto finish;
 
+        (void)sd_bus_negotiate_timestamp(bus, true);
+
         r = context_read_data(&context);
         if (r < 0) {
                 log_error_errno(r, "Failed to read time zone data: %m");