timedatectl.c revision e1636421f46db6d06fbd028ef20a3113fa3e11f8
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/>.
920b52e4909d9dc812817fd8b82f83ca23a11c91Thomas Hindoe Paaboel Andersen#include "spawn-polkit-agent.h"
79008bddf679a5e0900369950eb346c9fa687107Lennart Poetteringstatic bool arg_no_pager = false;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersenstatic bool arg_ask_password = true;
f0213e3796b4dd66e546e2de4d677db319f9171bTom Gundersenstatic BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
e53fc357a9bb9d0a5362ccc4246d598cb0febd5eLennart Poetteringstatic bool arg_adjust_system_clock = false;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersenstatic 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 */
86655331bc28887def7998d321b14ef8fccbeaf9Tom 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";
e53fc357a9bb9d0a5362ccc4246d598cb0febd5eLennart Poettering snprintf(s, size, "%i minutes backwards", -delta_minutes);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen snprintf(s, size, "%i minutes forward", delta_minutes);
200a0868fcdf7b95f3d8d1fda3aa2aef48d84fddTom Gundersen /* Enforce the values of /etc/localtime */
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen fprintf(stderr, "Warning: ignoring the TZ variable, reading the system's timezone setting only.\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",
86655331bc28887def7998d321b14ef8fccbeaf9Tom 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"
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen 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 timezone. This\n"
be19c5b5e0c0f78b8429b126936fa15856550a23David Herrmann " mode is not fully supported and will create various problems with time\n"
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen " zone changes and daylight saving adjustments. If at all possible use\n"
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen " RTC in UTC, by calling 'timedatectl set-local-rtc 0'" ANSI_HIGHLIGHT_OFF ".\n", stdout);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersenstatic int get_timedate_property_bool(sd_bus *bus, const char *name, bool *target) {
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
0bc70f1d9c5453ba614ec0ed041dc30b9cd52071Tom Gundersen log_error("Failed to get property: %s %s", name, bus_error_message(&error, -r));
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersenstatic int get_timedate_property_usec(sd_bus *bus, const char *name, usec_t *target) {
e3dca0089b7b50e2ec21409d1292727921d06102Tom Gundersen _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
e3dca0089b7b50e2ec21409d1292727921d06102Tom Gundersen _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen log_error("Failed to get property: %s %s", name, bus_error_message(&error, -r));
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersenstatic int show_status(sd_bus *bus, char **args, unsigned n) {
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
r = sd_bus_get_property(
bus,
"org.freedesktop.timedate1",
"/org/freedesktop/timedate1",
"org.freedesktop.timedate1",
&error,
&reply,
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,
return -errno;
char l[LINE_MAX], *p, **z, *w;
size_t k;
if (!fgets(l, sizeof(l), f)) {
if (feof(f))
return -errno;
p = strstrip(l);
w = strndup(p, k);
return log_oom();
free(w);
return log_oom();
zones = z;
if (zones)
static int help(void) {
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;
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;
goto finish;
pager_close();
return ret;