chiark
/
gitweb
/
~ianmdlvl
/
elogind.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
timedated: when performing "SetTime" compensate for program lag
[elogind.git]
/
src
/
timedate
/
timedated.c
diff --git
a/src/timedate/timedated.c
b/src/timedate/timedated.c
index fa3f947eaaa33425a7559a6af6c0e74115f99cf1..88d57e9d5432da609c5b989ed460aec6c500a304 100644
(file)
--- a/
src/timedate/timedated.c
+++ b/
src/timedate/timedated.c
@@
-22,9
+22,7
@@
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
-#include <sys/capability.h>
-#include "sd-id128.h"
#include "sd-messages.h"
#include "sd-event.h"
#include "sd-bus.h"
#include "sd-messages.h"
#include "sd-event.h"
#include "sd-bus.h"
@@
-33,17
+31,22
@@
#include "strv.h"
#include "def.h"
#include "clock-util.h"
#include "strv.h"
#include "def.h"
#include "clock-util.h"
-#include "conf-files.h"
#include "path-util.h"
#include "fileio-label.h"
#include "path-util.h"
#include "fileio-label.h"
-#include "label.h"
#include "bus-util.h"
#include "bus-util.h"
-#include "bus-errors.h"
+#include "bus-error.h"
+#include "bus-common-errors.h"
#include "event-util.h"
#include "event-util.h"
+#include "selinux-util.h"
#define NULL_ADJTIME_UTC "0.0 0 0\n0\nUTC\n"
#define NULL_ADJTIME_LOCAL "0.0 0 0\n0\nLOCAL\n"
#define NULL_ADJTIME_UTC "0.0 0 0\n0\nUTC\n"
#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_END
+};
+
typedef struct Context {
char *zone;
bool local_rtc;
typedef struct Context {
char *zone;
bool local_rtc;
@@
-70,7
+73,7
@@
static int context_read_data(Context *c) {
if (r == -EINVAL)
log_warning("/etc/localtime should be a symbolic link to a time zone data file in /usr/share/zoneinfo/.");
else
if (r == -EINVAL)
log_warning("/etc/localtime should be a symbolic link to a time zone data file in /usr/share/zoneinfo/.");
else
- log_warning
("Failed to get target of /etc/localtime: %s", strerror(-r)
);
+ log_warning
_errno(r, "Failed to get target of /etc/localtime: %m"
);
} else {
const char *e;
} else {
const char *e;
@@
-176,7
+179,7
@@
static int context_write_data_local_rtc(Context *c) {
}
}
}
}
-
label
_init("/etc");
+
mac_selinux
_init("/etc");
return write_string_file_atomic_label("/etc/adjtime", w);
}
return write_string_file_atomic_label("/etc/adjtime", w);
}
@@
-254,10
+257,8
@@
static int context_start_ntp(Context *c, sd_bus *bus, sd_bus_error *error) {
if (r < 0) {
if (sd_bus_error_has_name(error, SD_BUS_ERROR_FILE_NOT_FOUND) ||
sd_bus_error_has_name(error, "org.freedesktop.systemd1.LoadFailed") ||
if (r < 0) {
if (sd_bus_error_has_name(error, SD_BUS_ERROR_FILE_NOT_FOUND) ||
sd_bus_error_has_name(error, "org.freedesktop.systemd1.LoadFailed") ||
- sd_bus_error_has_name(error, "org.freedesktop.systemd1.NoSuchUnit")) {
- sd_bus_error_set_const(error, "org.freedesktop.timedate1.NoNTPSupport", "NTP not supported.");
- return -ENOTSUP;
- }
+ sd_bus_error_has_name(error, "org.freedesktop.systemd1.NoSuchUnit"))
+ return sd_bus_error_set_const(error, "org.freedesktop.timedate1.NoNTPSupport", "NTP not supported.");
return r;
}
return r;
}
@@
-298,10
+299,8
@@
static int context_enable_ntp(Context*c, sd_bus *bus, sd_bus_error *error) {
false);
if (r < 0) {
false);
if (r < 0) {
- if (sd_bus_error_has_name(error, SD_BUS_ERROR_FILE_NOT_FOUND)) {
- sd_bus_error_set_const(error, "org.freedesktop.timedate1.NoNTPSupport", "NTP not supported.");
- return -ENOTSUP;
- }
+ if (sd_bus_error_has_name(error, SD_BUS_ERROR_FILE_NOT_FOUND))
+ return sd_bus_error_set_const(error, "org.freedesktop.timedate1.NoNTPSupport", "NTP not supported.");
return r;
}
return r;
}
@@
-395,7
+394,14
@@
static int method_set_timezone(sd_bus *bus, sd_bus_message *m, void *userdata, s
if (streq_ptr(z, c->zone))
return sd_bus_reply_method_return(m, NULL);
if (streq_ptr(z, c->zone))
return sd_bus_reply_method_return(m, NULL);
- r = bus_verify_polkit_async(m, CAP_SYS_TIME, "org.freedesktop.timedate1.set-timezone", interactive, &c->polkit_registry, error);
+ r = bus_verify_polkit_async(
+ m,
+ CAP_SYS_TIME,
+ "org.freedesktop.timedate1.set-timezone",
+ interactive,
+ UID_INVALID,
+ &c->polkit_registry,
+ error);
if (r < 0)
return r;
if (r == 0)
if (r < 0)
return r;
if (r == 0)
@@
-411,7
+417,7
@@
static int method_set_timezone(sd_bus *bus, sd_bus_message *m, void *userdata, s
/* 1. Write new configuration file */
r = context_write_data_timezone(c);
if (r < 0) {
/* 1. Write new configuration file */
r = context_write_data_timezone(c);
if (r < 0) {
- log_error
("Failed to set time zone: %s", strerror(-r)
);
+ log_error
_errno(r, "Failed to set time zone: %m"
);
return sd_bus_error_set_errnof(error, r, "Failed to set time zone: %s", strerror(-r));
}
return sd_bus_error_set_errnof(error, r, "Failed to set time zone: %s", strerror(-r));
}
@@
-429,9
+435,9
@@
static int method_set_timezone(sd_bus *bus, sd_bus_message *m, void *userdata, s
}
log_struct(LOG_INFO,
}
log_struct(LOG_INFO,
- MESSAGE_ID(SD_MESSAGE_TIMEZONE_CHANGE),
+
LOG_
MESSAGE_ID(SD_MESSAGE_TIMEZONE_CHANGE),
"TIMEZONE=%s", c->zone,
"TIMEZONE=%s", c->zone,
-
"MESSAGE=Changed time zone to '%s'.", c->zone
,
+
LOG_MESSAGE("Changed time zone to '%s'.", c->zone)
,
NULL);
sd_bus_emit_properties_changed(bus, "/org/freedesktop/timedate1", "org.freedesktop.timedate1", "Timezone", NULL);
NULL);
sd_bus_emit_properties_changed(bus, "/org/freedesktop/timedate1", "org.freedesktop.timedate1", "Timezone", NULL);
@@
-456,7
+462,14
@@
static int method_set_local_rtc(sd_bus *bus, sd_bus_message *m, void *userdata,
if (lrtc == c->local_rtc)
return sd_bus_reply_method_return(m, NULL);
if (lrtc == c->local_rtc)
return sd_bus_reply_method_return(m, NULL);
- r = bus_verify_polkit_async(m, CAP_SYS_TIME, "org.freedesktop.timedate1.set-local-rtc", interactive, &c->polkit_registry, error);
+ r = bus_verify_polkit_async(
+ m,
+ CAP_SYS_TIME,
+ "org.freedesktop.timedate1.set-local-rtc",
+ interactive,
+ UID_INVALID,
+ &c->polkit_registry,
+ error);
if (r < 0)
return r;
if (r == 0)
if (r < 0)
return r;
if (r == 0)
@@
-467,7
+480,7
@@
static int method_set_local_rtc(sd_bus *bus, sd_bus_message *m, void *userdata,
/* 1. Write new configuration file */
r = context_write_data_local_rtc(c);
if (r < 0) {
/* 1. Write new configuration file */
r = context_write_data_local_rtc(c);
if (r < 0) {
- log_error
("Failed to set RTC to local/UTC: %s", strerror(-r)
);
+ log_error
_errno(r, "Failed to set RTC to local/UTC: %m"
);
return sd_bus_error_set_errnof(error, r, "Failed to set RTC to local/UTC: %s", strerror(-r));
}
return sd_bus_error_set_errnof(error, r, "Failed to set RTC to local/UTC: %s", strerror(-r));
}
@@
-527,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;
Context *c = userdata;
int64_t utc;
struct timespec ts;
+ usec_t start;
struct tm* tm;
int r;
struct tm* tm;
int r;
@@
-561,15
+575,29
@@
static int method_set_time(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bu
} else
timespec_store(&ts, (usec_t) utc);
} else
timespec_store(&ts, (usec_t) utc);
- r = bus_verify_polkit_async(m, CAP_SYS_TIME, "org.freedesktop.timedate1.set-time", interactive, &c->polkit_registry, error);
+ r = bus_verify_polkit_async(
+ m,
+ CAP_SYS_TIME,
+ "org.freedesktop.timedate1.set-time",
+ interactive,
+ UID_INVALID,
+ &c->polkit_registry,
+ error);
if (r < 0)
return r;
if (r == 0)
return 1;
if (r < 0)
return r;
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) {
/* Set system clock */
if (clock_settime(CLOCK_REALTIME, &ts) < 0) {
- log_error
(
"Failed to set local time: %m");
+ log_error
_errno(errno,
"Failed to set local time: %m");
return sd_bus_error_set_errnof(error, errno, "Failed to set local time: %m");
}
return sd_bus_error_set_errnof(error, errno, "Failed to set local time: %m");
}
@@
-581,9
+609,9
@@
static int method_set_time(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bu
clock_set_hwclock(tm);
log_struct(LOG_INFO,
clock_set_hwclock(tm);
log_struct(LOG_INFO,
- MESSAGE_ID(SD_MESSAGE_TIME_CHANGE),
+
LOG_
MESSAGE_ID(SD_MESSAGE_TIME_CHANGE),
"REALTIME="USEC_FMT, timespec_load(&ts),
"REALTIME="USEC_FMT, timespec_load(&ts),
-
"MESSAGE=Changed local time to %s", ctime(&ts.tv_sec
),
+
LOG_MESSAGE("Changed local time to %s", ctime(&ts.tv_sec)
),
NULL);
return sd_bus_reply_method_return(m, NULL);
NULL);
return sd_bus_reply_method_return(m, NULL);
@@
-601,7
+629,14
@@
static int method_set_ntp(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus
if ((bool)ntp == c->use_ntp)
return sd_bus_reply_method_return(m, NULL);
if ((bool)ntp == c->use_ntp)
return sd_bus_reply_method_return(m, NULL);
- r = bus_verify_polkit_async(m, CAP_SYS_TIME, "org.freedesktop.timedate1.set-ntp", interactive, &c->polkit_registry, error);
+ r = bus_verify_polkit_async(
+ m,
+ CAP_SYS_TIME,
+ "org.freedesktop.timedate1.set-ntp",
+ interactive,
+ UID_INVALID,
+ &c->polkit_registry,
+ error);
if (r < 0)
return r;
if (r == 0)
if (r < 0)
return r;
if (r == 0)
@@
-649,28
+684,20
@@
static int connect_bus(Context *c, sd_event *event, sd_bus **_bus) {
assert(_bus);
r = sd_bus_default_system(&bus);
assert(_bus);
r = sd_bus_default_system(&bus);
- if (r < 0) {
- log_error("Failed to get system bus connection: %s", strerror(-r));
- return r;
- }
+ if (r < 0)
+ return log_error_errno(r, "Failed to get system bus connection: %m");
r = sd_bus_add_object_vtable(bus, NULL, "/org/freedesktop/timedate1", "org.freedesktop.timedate1", timedate_vtable, c);
r = sd_bus_add_object_vtable(bus, NULL, "/org/freedesktop/timedate1", "org.freedesktop.timedate1", timedate_vtable, c);
- if (r < 0) {
- log_error("Failed to register object: %s", strerror(-r));
- return r;
- }
+ if (r < 0)
+ return log_error_errno(r, "Failed to register object: %m");
r = sd_bus_request_name(bus, "org.freedesktop.timedate1", 0);
r = sd_bus_request_name(bus, "org.freedesktop.timedate1", 0);
- if (r < 0) {
- log_error("Failed to register name: %s", strerror(-r));
- return r;
- }
+ if (r < 0)
+ return log_error_errno(r, "Failed to register name: %m");
r = sd_bus_attach_event(bus, event, 0);
r = sd_bus_attach_event(bus, event, 0);
- if (r < 0) {
- log_error("Failed to attach bus to event loop: %s", strerror(-r));
- return r;
- }
+ if (r < 0)
+ return log_error_errno(r, "Failed to attach bus to event loop: %m");
*_bus = bus;
bus = NULL;
*_bus = bus;
bus = NULL;
@@
-698,7
+725,7
@@
int main(int argc, char *argv[]) {
r = sd_event_default(&event);
if (r < 0) {
r = sd_event_default(&event);
if (r < 0) {
- log_error
("Failed to allocate event loop: %s", strerror(-r)
);
+ log_error
_errno(r, "Failed to allocate event loop: %m"
);
goto finish;
}
goto finish;
}
@@
-708,21
+735,23
@@
int main(int argc, char *argv[]) {
if (r < 0)
goto finish;
if (r < 0)
goto finish;
+ (void)sd_bus_negotiate_timestamp(bus, true);
+
r = context_read_data(&context);
if (r < 0) {
r = context_read_data(&context);
if (r < 0) {
- log_error
("Failed to read time zone data: %s", strerror(-r)
);
+ log_error
_errno(r, "Failed to read time zone data: %m"
);
goto finish;
}
r = context_read_ntp(&context, bus);
if (r < 0) {
goto finish;
}
r = context_read_ntp(&context, bus);
if (r < 0) {
- log_error
("Failed to determine whether NTP is enabled: %s", strerror(-r)
);
+ log_error
_errno(r, "Failed to determine whether NTP is enabled: %m"
);
goto finish;
}
r = bus_event_loop_with_idle(event, bus, "org.freedesktop.timedate1", DEFAULT_EXIT_USEC, NULL, NULL);
if (r < 0) {
goto finish;
}
r = bus_event_loop_with_idle(event, bus, "org.freedesktop.timedate1", DEFAULT_EXIT_USEC, NULL, NULL);
if (r < 0) {
- log_error
("Failed to run event loop: %s", strerror(-r)
);
+ log_error
_errno(r, "Failed to run event loop: %m"
);
goto finish;
}
goto finish;
}