X-Git-Url: http://git.ipfire.org/?a=blobdiff_plain;f=src%2Ftimedate%2Ftimedatectl.c;h=d80a9178701e427d92d6e262d29d121846864f6c;hb=53e1b683907c2f12330f00feb9630150196f064d;hp=b7871f81aa91e4d62217664d906784a657c43976;hpb=54f8c958f1ebff12b961a1029a2aec451587c206;p=thirdparty%2Fsystemd.git diff --git a/src/timedate/timedatectl.c b/src/timedate/timedatectl.c index b7871f81aa9..d80a9178701 100644 --- a/src/timedate/timedatectl.c +++ b/src/timedate/timedatectl.c @@ -1,3 +1,4 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ /*** This file is part of systemd. @@ -40,28 +41,16 @@ static BusTransport arg_transport = BUS_TRANSPORT_LOCAL; static char *arg_host = NULL; static bool arg_adjust_system_clock = false; -static void polkit_agent_open_if_enabled(void) { - - /* Open the polkit agent as a child process if necessary */ - if (!arg_ask_password) - return; - - if (arg_transport != BUS_TRANSPORT_LOCAL) - return; - - polkit_agent_open(); -} - typedef struct StatusInfo { usec_t time; char *timezone; usec_t rtc_time; - bool rtc_local; + int rtc_local; - bool ntp_enabled; - bool ntp_capable; - bool ntp_synced; + int ntp_enabled; + int ntp_capable; + int ntp_synced; } StatusInfo; static void status_info_clear(StatusInfo *info) { @@ -72,12 +61,13 @@ static void status_info_clear(StatusInfo *info) { } static void print_status_info(const StatusInfo *i) { - char a[FORMAT_TIMESTAMP_MAX]; + char a[LINE_MAX]; struct tm tm; time_t sec; bool have_time = false; const char *old_tz = NULL, *tz; int r; + size_t n; assert(i); @@ -102,27 +92,27 @@ static void print_status_info(const StatusInfo *i) { log_warning("Could not get time from timedated and not operating locally, ignoring."); if (have_time) { - xstrftime(a, "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&sec, &tm)); - printf(" Local time: %.*s\n", (int) sizeof(a), a); + n = strftime(a, sizeof a, "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&sec, &tm)); + printf(" Local time: %s\n", n > 0 ? a : "n/a"); - xstrftime(a, "%a %Y-%m-%d %H:%M:%S UTC", gmtime_r(&sec, &tm)); - printf(" Universal time: %.*s\n", (int) sizeof(a), a); + n = strftime(a, sizeof a, "%a %Y-%m-%d %H:%M:%S UTC", gmtime_r(&sec, &tm)); + printf(" Universal time: %s\n", n > 0 ? a : "n/a"); } else { - printf(" Local time: %s\n", "n/a"); - printf(" Universal time: %s\n", "n/a"); + printf(" Local time: %s\n", "n/a"); + printf(" Universal time: %s\n", "n/a"); } if (i->rtc_time > 0) { time_t rtc_sec; rtc_sec = (time_t) (i->rtc_time / USEC_PER_SEC); - xstrftime(a, "%a %Y-%m-%d %H:%M:%S", gmtime_r(&rtc_sec, &tm)); - printf(" RTC time: %.*s\n", (int) sizeof(a), a); + n = strftime(a, sizeof a, "%a %Y-%m-%d %H:%M:%S", gmtime_r(&rtc_sec, &tm)); + printf(" RTC time: %s\n", n > 0 ? a : "n/a"); } else - printf(" RTC time: %s\n", "n/a"); + printf(" RTC time: %s\n", "n/a"); if (have_time) - xstrftime(a, "%Z, %z", localtime_r(&sec, &tm)); + n = strftime(a, sizeof a, "%Z, %z", localtime_r(&sec, &tm)); /* Restore the $TZ */ if (old_tz) @@ -134,11 +124,11 @@ static void print_status_info(const StatusInfo *i) { else tzset(); - printf(" Time zone: %s (%.*s)\n" - " Network time on: %s\n" - "NTP synchronized: %s\n" - " RTC in local TZ: %s\n", - strna(i->timezone), (int) sizeof(a), have_time ? a : "n/a", + printf(" Time zone: %s (%s)\n" + " System clock synchronized: %s\n" + "systemd-timesyncd.service active: %s\n" + " RTC in local TZ: %s\n", + strna(i->timezone), have_time && n > 0 ? a : "n/a", i->ntp_capable ? yes_no(i->ntp_enabled) : "n/a", yes_no(i->ntp_synced), yes_no(i->rtc_local)); @@ -165,6 +155,8 @@ static int show_status(sd_bus *bus, char **args, unsigned n) { { "RTCTimeUSec", "t", NULL, offsetof(StatusInfo, rtc_time) }, {} }; + + _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; int r; assert(bus); @@ -173,9 +165,10 @@ static int show_status(sd_bus *bus, char **args, unsigned n) { "org.freedesktop.timedate1", "/org/freedesktop/timedate1", map, + &error, &info); if (r < 0) - return log_error_errno(r, "Failed to query server: %m"); + return log_error_errno(r, "Failed to query server: %s", bus_error_message(&error, r)); print_status_info(&info); @@ -191,7 +184,7 @@ static int set_time(sd_bus *bus, char **args, unsigned n) { assert(args); assert(n == 2); - polkit_agent_open_if_enabled(); + polkit_agent_open_if_enabled(arg_transport, arg_ask_password); r = parse_timestamp(args[1], &t); if (r < 0) { @@ -220,7 +213,7 @@ static int set_timezone(sd_bus *bus, char **args, unsigned n) { assert(args); assert(n == 2); - polkit_agent_open_if_enabled(); + polkit_agent_open_if_enabled(arg_transport, arg_ask_password); r = sd_bus_call_method(bus, "org.freedesktop.timedate1", @@ -243,7 +236,7 @@ static int set_local_rtc(sd_bus *bus, char **args, unsigned n) { assert(args); assert(n == 2); - polkit_agent_open_if_enabled(); + polkit_agent_open_if_enabled(arg_transport, arg_ask_password); b = parse_boolean(args[1]); if (b < 0) { @@ -272,7 +265,7 @@ static int set_ntp(sd_bus *bus, char **args, unsigned n) { assert(args); assert(n == 2); - polkit_agent_open_if_enabled(); + polkit_agent_open_if_enabled(arg_transport, arg_ask_password); b = parse_boolean(args[1]); if (b < 0) {