timedatectl.c revision 7568345034f2890af745747783c5abfbf6eccf0f
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen This file is part of systemd.
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen Copyright 2012 Lennart Poettering
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen Copyright 2013 Kay Sievers
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen systemd is free software; you can redistribute it and/or modify it
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen under the terms of the GNU Lesser General Public License as published by
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen the Free Software Foundation; either version 2.1 of the License, or
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen (at your option) any later version.
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen systemd is distributed in the hope that it will be useful, but
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen WITHOUT ANY WARRANTY; without even the implied warranty of
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen Lesser General Public License for more details.
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen You should have received a copy of the GNU Lesser General Public License
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen along with systemd; If not, see <http://www.gnu.org/licenses/>.
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersenstatic bool arg_no_pager = false;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersenstatic bool arg_ask_password = true;
79008bddf679a5e0900369950eb346c9fa687107Lennart Poetteringstatic BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersenstatic bool arg_adjust_system_clock = false;
79008bddf679a5e0900369950eb346c9fa687107Lennart Poetteringstatic void pager_open_if_enabled(void) {
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersenstatic void polkit_agent_open_if_enabled(void) {
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen /* Open the polkit agent as a child process if necessary */
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersentypedef struct StatusInfo {
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersenstatic const char *jump_str(int delta_minutes, char *s, size_t size) {
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen return "one hour forward";
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen return "one hour backwards";
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering snprintf(s, size, "%i minutes backwards", -delta_minutes);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen snprintf(s, size, "%i minutes forward", delta_minutes);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersenstatic void print_status_info(const StatusInfo *i) {
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen /* Enforce the values of /etc/localtime */
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen fprintf(stderr, "Warning: Ignoring the TZ variable. Reading the system's time zone setting only.\n\n");
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen if (i->time != 0) {
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen } else if (arg_transport == BUS_TRANSPORT_LOCAL) {
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen fprintf(stderr, "Warning: Could not get time from timedated and not operating locally.\n\n");
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen assert_se(strftime(a, sizeof(a), "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&sec, &tm)) > 0);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen assert_se(strftime(a, sizeof(a), "%a %Y-%m-%d %H:%M:%S UTC", gmtime_r(&sec, &tm)) > 0);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen rtc_sec = (time_t)(i->rtc_time / USEC_PER_SEC);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen assert_se(strftime(a, sizeof(a), "%a %Y-%m-%d %H:%M:%S", gmtime_r(&rtc_sec, &tm)) > 0);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen assert_se(strftime(a, sizeof(a), "%Z, %z", localtime_r(&sec, &tm)) > 0);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen " NTP enabled: %s\n"
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen "NTP synchronized: %s\n"
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen " RTC in local TZ: %s\n",
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen i->ntp_capable ? yes_no(i->ntp_enabled) : "n/a",
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen assert_se(strftime(a, sizeof(a), "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&t, &tm)) > 0);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen assert_se(strftime(b, sizeof(b), "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&tc, &tm)) > 0);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen assert_se(strftime(a, sizeof(a), "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&t, &tm)) > 0);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen assert_se(strftime(b, sizeof(b), "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&tn, &tm)) > 0);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen printf(" Next DST change: DST %s (the clock jumps %s) at\n"
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering is_dstn ? "begins" : "ends", jump_str(dn, s, sizeof(s)), a, b);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen "Warning: The RTC is configured to maintain time in the local time zone. This\n"
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen " mode is not fully supported and will create various problems with time\n"
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen " zone changes and daylight saving time adjustments. If at all possible, use\n"
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen " RTC in UTC by calling 'timedatectl set-local-rtc 0'" ANSI_HIGHLIGHT_OFF ".\n", stdout);
e0ee46f29028e291eb67f435aff1b6202d75d9d6Lennart Poetteringstatic int show_status(sd_bus *bus, char **args, unsigned n) {
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen static const struct bus_properties_map map[] = {
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen { "Timezone", "s", NULL, offsetof(StatusInfo, timezone) },
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen { "LocalRTC", "b", NULL, offsetof(StatusInfo, rtc_local) },
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen { "NTP", "b", NULL, offsetof(StatusInfo, ntp_enabled) },
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen { "CanNTP", "b", NULL, offsetof(StatusInfo, ntp_capable) },
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen { "NTPSynchronized", "b", NULL, offsetof(StatusInfo, ntp_synced) },
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen { "TimeUSec", "t", NULL, offsetof(StatusInfo, time) },
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen { "RTCTimeUSec", "t", NULL, offsetof(StatusInfo, rtc_time) },
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen log_error("Failed to query server: %s", strerror(-r));
usec_t t;
"org.freedesktop.timedate1",
"/org/freedesktop/timedate1",
"org.freedesktop.timedate1",
&error,
NULL,
"org.freedesktop.timedate1",
"/org/freedesktop/timedate1",
"org.freedesktop.timedate1",
&error,
NULL,
"org.freedesktop.timedate1",
"/org/freedesktop/timedate1",
"org.freedesktop.timedate1",
&error,
NULL,
"org.freedesktop.timedate1",
"/org/freedesktop/timedate1",
"org.freedesktop.timedate1",
&error,
NULL,
static int help(void) {
return help();
case ARG_VERSION:
case ARG_NO_ASK_PASSWORD:
arg_ask_password = false;
case ARG_ADJUST_SYSTEM_CLOCK:
arg_adjust_system_clock = true;
case ARG_NO_PAGER:
arg_no_pager = true;
return -EINVAL;
const char* verb;
MORE,
LESS,
} argc_cmp;
const int argc;
} verbs[] = {
int left;
if (left <= 0)
help();
return -EINVAL;
case EQUAL:
return -EINVAL;
case MORE:
return -EINVAL;
case LESS:
return -EINVAL;
log_open();
goto finish;
goto finish;
pager_close();