chiark / gitweb /
Use space after a silencing (void)
[elogind.git] / src / timedate / timedated.c
index 23efe832a74acb8a4cbb41e100a062fe71a73342..ca771d5b33241215e4987fb06e53f33e171b73f2 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"
@@ -46,7 +43,7 @@
 #define NULL_ADJTIME_LOCAL "0.0 0 0\n0\nLOCAL\n"
 
 static BUS_ERROR_MAP_ELF_REGISTER const sd_bus_error_map timedated_errors[] = {
-        SD_BUS_ERROR_MAP("org.freedesktop.timedate1.NoNTPSupport", ENOTSUP),
+        SD_BUS_ERROR_MAP("org.freedesktop.timedate1.NoNTPSupport", EOPNOTSUPP),
         SD_BUS_ERROR_MAP_END
 };
 
@@ -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;
 
@@ -553,6 +551,9 @@ static int method_set_time(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bu
         if (c->use_ntp)
                 return sd_bus_error_setf(error, BUS_ERROR_AUTOMATIC_TIME_SYNC_ENABLED, "Automatic time synchronization is enabled");
 
+        /* this only gets used if dbus does not provide a timestamp */
+        start = now(CLOCK_MONOTONIC);
+
         r = sd_bus_message_read(m, "xbb", &utc, &relative, &interactive);
         if (r < 0)
                 return r;
@@ -590,6 +591,14 @@ 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);
+        /* when sd_bus_message_get_monotonic_usec() returns -ENODATA it does not modify &start */
+        if (r < 0 && r != -ENODATA)
+                return r;
+
+        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 +739,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");