chiark / gitweb /
bus: automatically flush bus queue when we exit the event loop
[elogind.git] / src / timedate / timedatectl.c
index 2b5822b54e4a634a44d412954a3a669e6b733bb6..80afc57131b68c0b295dffcd2767306654bb8df5 100644 (file)
@@ -23,6 +23,7 @@
 #include <stdbool.h>
 #include <unistd.h>
 #include <getopt.h>
+#include <locale.h>
 #include <string.h>
 #include <sys/timex.h>
 
@@ -33,8 +34,9 @@
 #include "hwclock.h"
 #include "strv.h"
 #include "pager.h"
+#include "time-dst.h"
 
-static bool arg_fix_system = false;
+static bool arg_adjust_system_clock = false;
 static bool arg_no_pager = false;
 static enum transport {
         TRANSPORT_NORMAL,
@@ -42,14 +44,15 @@ static enum transport {
         TRANSPORT_POLKIT
 } arg_transport = TRANSPORT_NORMAL;
 static bool arg_ask_password = true;
-static const char *arg_host = NULL;
+static char *arg_host = NULL;
+static char *arg_user = NULL;
 
 static void pager_open_if_enabled(void) {
 
         if (arg_no_pager)
                 return;
 
-        pager_open();
+        pager_open(false);
 }
 
 static void polkit_agent_open_if_enabled(void) {
@@ -66,42 +69,58 @@ typedef struct StatusInfo {
         const char *timezone;
         bool local_rtc;
         bool ntp;
+        bool can_ntp;
 } StatusInfo;
 
-static bool ntp_synced(void) {
-        struct timex txc;
-
-        zero(txc);
-        if (adjtimex(&txc) < 0)
-                return false;
-
-        if (txc.status & STA_UNSYNC)
-                return false;
-
-        return true;
+static const char *jump_str(int delta_minutes, char *s, size_t size) {
+        if (delta_minutes == 60)
+                return "one hour forward";
+        if (delta_minutes == -60)
+                return "one hour backwards";
+        if (delta_minutes < 0) {
+                snprintf(s, size, "%i minutes backwards", -delta_minutes);
+                return s;
+        }
+        if (delta_minutes > 0) {
+                snprintf(s, size, "%i minutes forward", delta_minutes);
+                return s;
+        }
+        return "";
 }
 
 static void print_status_info(StatusInfo *i) {
         usec_t n;
+        char a[FORMAT_TIMESTAMP_MAX];
         char b[FORMAT_TIMESTAMP_MAX];
+        char s[32];
         struct tm tm;
         time_t sec;
+        char *zc, *zn;
+        time_t t, tc, tn;
+        int dn;
+        bool is_dstc, is_dstn;
         int r;
 
         assert(i);
 
+        /* enforce the values of /etc/localtime */
+        if (getenv("TZ")) {
+                fprintf(stderr, "Warning: ignoring the TZ variable, reading the system's timezone setting only.\n\n");
+                unsetenv("TZ");
+        }
+
         n = now(CLOCK_REALTIME);
         sec = (time_t) (n / USEC_PER_SEC);
 
         zero(tm);
-        assert_se(strftime(b, sizeof(b), "%a, %Y-%m-%d %H:%M:%S %Z", localtime_r(&sec, &tm)) > 0);
-        char_array_0(b);
-        printf("      Local time: %s\n", b);
+        assert_se(strftime(a, sizeof(a), "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&sec, &tm)) > 0);
+        char_array_0(a);
+        printf("      Local time: %s\n", a);
 
         zero(tm);
-        assert_se(strftime(b, sizeof(b), "%a, %Y-%m-%d %H:%M:%S UTC", gmtime_r(&sec, &tm)) > 0);
-        char_array_0(b);
-        printf("  Universal time: %s\n", b);
+        assert_se(strftime(a, sizeof(a), "%a %Y-%m-%d %H:%M:%S UTC", gmtime_r(&sec, &tm)) > 0);
+        char_array_0(a);
+        printf("  Universal time: %s\n", a);
 
         zero(tm);
         r = hwclock_get_time(&tm);
@@ -109,23 +128,65 @@ static void print_status_info(StatusInfo *i) {
                 /* Calculcate the week-day */
                 mktime(&tm);
 
-                assert_se(strftime(b, sizeof(b), "%a, %Y-%m-%d %H:%M:%S", &tm) > 0);
-                char_array_0(b);
-                printf("        RTC time: %s\n", b);
+                assert_se(strftime(a, sizeof(a), "%a %Y-%m-%d %H:%M:%S", &tm) > 0);
+                char_array_0(a);
+                printf("        RTC time: %s\n", a);
         }
 
-        printf("        Timezone: %s\n"
+        zero(tm);
+        assert_se(strftime(a, sizeof(a), "%Z, %z", localtime_r(&sec, &tm)) > 0);
+        char_array_0(a);
+        printf("        Timezone: %s (%s)\n"
                "     NTP enabled: %s\n"
                "NTP synchronized: %s\n"
                " RTC in local TZ: %s\n",
                strna(i->timezone),
-               yes_no(i->ntp),
+               a,
+               i->can_ntp ? yes_no(i->ntp) : "n/a",
                yes_no(ntp_synced()),
                yes_no(i->local_rtc));
 
+        r = time_get_dst(sec, "/etc/localtime",
+                         &tc, &zc, &is_dstc,
+                         &tn, &dn, &zn, &is_dstn);
+        if (r < 0)
+                printf("      DST active: n/a\n");
+        else {
+                printf("      DST active: %s\n", yes_no(is_dstc));
+
+                t = tc - 1;
+                zero(tm);
+                assert_se(strftime(a, sizeof(a), "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&t, &tm)) > 0);
+                char_array_0(a);
+
+                zero(tm);
+                assert_se(strftime(b, sizeof(b), "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&tc, &tm)) > 0);
+                char_array_0(b);
+                printf(" Last DST change: DST %s at\n"
+                       "                  %s\n"
+                       "                  %s\n",
+                       is_dstc ? "began" : "ended", a, b);
+
+                t = tn - 1;
+                zero(tm);
+                assert_se(strftime(a, sizeof(a), "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&t, &tm)) > 0);
+                char_array_0(a);
+
+                zero(tm);
+                assert_se(strftime(b, sizeof(b), "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&tn, &tm)) > 0);
+                char_array_0(b);
+                printf(" Next DST change: DST %s (the clock jumps %s) at\n"
+                       "                  %s\n"
+                       "                  %s\n",
+                       is_dstn ? "begins" : "ends", jump_str(dn, s, sizeof(s)), a, b);
+
+                free(zc);
+                free(zn);
+        }
+
         if (i->local_rtc)
                 fputs("\n" ANSI_HIGHLIGHT_ON
-                      "Warning: The RTC is configured to maintain time in the local time zone. This\n"
+                      "Warning: The RTC is configured to maintain time in the local timezone. This\n"
                       "         mode is not fully supported and will create various problems with time\n"
                       "         zone changes and daylight saving adjustments. If at all possible use\n"
                       "         RTC in UTC, by calling 'timedatectl set-local-rtc 0'" ANSI_HIGHLIGHT_OFF ".\n", stdout);
@@ -156,6 +217,8 @@ static int status_property(const char *name, DBusMessageIter *iter, StatusInfo *
                         i->local_rtc = b;
                 else if (streq(name, "NTP"))
                         i->ntp = b;
+                else if (streq(name, "CanNTP"))
+                        i->can_ntp = b;
         }
         }
 
@@ -167,7 +230,7 @@ static int show_status(DBusConnection *bus, char **args, unsigned n) {
         const char *interface = "";
         int r;
         DBusMessageIter iter, sub, sub2, sub3;
-        StatusInfo info;
+        StatusInfo info = {};
 
         assert(args);
 
@@ -191,7 +254,6 @@ static int show_status(DBusConnection *bus, char **args, unsigned n) {
                 return -EIO;
         }
 
-        zero(info);
         dbus_message_iter_recurse(&iter, &sub);
 
         while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
@@ -231,7 +293,7 @@ static int show_status(DBusConnection *bus, char **args, unsigned n) {
 
 static int set_time(DBusConnection *bus, char **args, unsigned n) {
         _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
-        dbus_bool_t relative = false, interactive = true;
+        dbus_bool_t relative = false, interactive = arg_ask_password;
         usec_t t;
         dbus_int64_t u;
         int r;
@@ -265,7 +327,7 @@ static int set_time(DBusConnection *bus, char **args, unsigned n) {
 
 static int set_timezone(DBusConnection *bus, char **args, unsigned n) {
         _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
-        dbus_bool_t interactive = true;
+        dbus_bool_t interactive = arg_ask_password;
 
         assert(args);
         assert(n == 2);
@@ -287,7 +349,7 @@ static int set_timezone(DBusConnection *bus, char **args, unsigned n) {
 
 static int set_local_rtc(DBusConnection *bus, char **args, unsigned n) {
         _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
-        dbus_bool_t interactive = true, b, q;
+        dbus_bool_t interactive = arg_ask_password, b, q;
         int r;
 
         assert(args);
@@ -302,7 +364,7 @@ static int set_local_rtc(DBusConnection *bus, char **args, unsigned n) {
         }
 
         b = r;
-        q = arg_fix_system;
+        q = arg_adjust_system_clock;
 
         return bus_method_call_with_reply(
                         bus,
@@ -320,7 +382,7 @@ static int set_local_rtc(DBusConnection *bus, char **args, unsigned n) {
 
 static int set_ntp(DBusConnection *bus, char **args, unsigned n) {
         _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
-        dbus_bool_t interactive = true, b;
+        dbus_bool_t interactive = arg_ask_password, b;
         int r;
 
         assert(args);
@@ -349,17 +411,10 @@ static int set_ntp(DBusConnection *bus, char **args, unsigned n) {
                         DBUS_TYPE_INVALID);
 }
 
-static int zone_compare(const void *_a, const void *_b) {
-        const char **a = (const char**) _a, **b = (const char**) _b;
-
-        return strcmp(*a, *b);
-}
-
 static int list_timezones(DBusConnection *bus, char **args, unsigned n) {
         _cleanup_fclose_ FILE *f = NULL;
         _cleanup_strv_free_ char **zones = NULL;
         size_t n_zones = 0;
-        char **i;
 
         assert(args);
         assert(n == 1);
@@ -416,35 +471,35 @@ static int list_timezones(DBusConnection *bus, char **args, unsigned n) {
         }
 
         if (zones)
-                zones[n_zones] = 0;
-
-        qsort(zones, n_zones, sizeof(char*), zone_compare);
+                zones[n_zones] = NULL;
 
         pager_open_if_enabled();
 
-        STRV_FOREACH(i, zones)
-                puts(*i);
+        strv_sort(zones);
+        strv_print(zones);
 
         return 0;
 }
 
 static int help(void) {
 
-        printf("%s [OPTIONS...] {COMMAND} ...\n\n"
-               "Query or control system time and date settings.\n\n"
+        printf("%s [OPTIONS...] COMMAND ...\n\n"
+               "Query or change system time and date settings.\n\n"
                "  -h --help              Show this help\n"
                "     --version           Show package version\n"
-               "     --fix-system        Adjust system clock when changing local RTC mode\n"
+               "     --adjust-system-clock\n"
+               "                         Adjust system clock when changing local RTC mode\n"
                "     --no-pager          Do not pipe output into a pager\n"
+               "  -P --privileged        Acquire privileges before execution\n"
                "     --no-ask-password   Do not prompt for password\n"
                "  -H --host=[USER@]HOST  Operate on remote host\n\n"
                "Commands:\n"
-               "  status                          Show current time settings\n"
-               "  set-time [TIME]                 Set system time\n"
-               "  set-timezone [ZONE]             Set system timezone\n"
-               "  list-timezones                  Show known timezones\n"
-               "  set-local-rtc [BOOL]            Control whether RTC is in local time\n"
-               "  set-ntp [BOOL]                  Control whether NTP is enabled\n",
+               "  status                 Show current time settings\n"
+               "  set-time TIME          Set system time\n"
+               "  set-timezone ZONE      Set system timezone\n"
+               "  list-timezones         Show known timezones\n"
+               "  set-local-rtc BOOL     Control whether RTC is in local time\n"
+               "  set-ntp BOOL           Control whether NTP is enabled\n",
                program_invocation_short_name);
 
         return 0;
@@ -455,19 +510,19 @@ static int parse_argv(int argc, char *argv[]) {
         enum {
                 ARG_VERSION = 0x100,
                 ARG_NO_PAGER,
-                ARG_FIX_SYSTEM,
+                ARG_ADJUST_SYSTEM_CLOCK,
                 ARG_NO_ASK_PASSWORD
         };
 
         static const struct option options[] = {
-                { "help",            no_argument,       NULL, 'h'                 },
-                { "version",         no_argument,       NULL, ARG_VERSION         },
-                { "no-pager",        no_argument,       NULL, ARG_NO_PAGER        },
-                { "host",            required_argument, NULL, 'H'                 },
-                { "privileged",      no_argument,       NULL, 'P'                 },
-                { "no-ask-password", no_argument,       NULL, ARG_NO_ASK_PASSWORD },
-                { "fix-system",      no_argument,       NULL, ARG_FIX_SYSTEM      },
-                { NULL,              0,                 NULL, 0                   }
+                { "help",                no_argument,       NULL, 'h'                     },
+                { "version",             no_argument,       NULL, ARG_VERSION             },
+                { "no-pager",            no_argument,       NULL, ARG_NO_PAGER            },
+                { "host",                required_argument, NULL, 'H'                     },
+                { "privileged",          no_argument,       NULL, 'P'                     },
+                { "no-ask-password",     no_argument,       NULL, ARG_NO_ASK_PASSWORD     },
+                { "adjust-system-clock", no_argument,       NULL, ARG_ADJUST_SYSTEM_CLOCK },
+                { NULL,                  0,                 NULL, 0                       }
         };
 
         int c;
@@ -475,7 +530,7 @@ static int parse_argv(int argc, char *argv[]) {
         assert(argc >= 0);
         assert(argv);
 
-        while ((c = getopt_long(argc, argv, "+hp:as:H:P", options, NULL)) >= 0) {
+        while ((c = getopt_long(argc, argv, "hH:P", options, NULL)) >= 0) {
 
                 switch (c) {
 
@@ -485,7 +540,6 @@ static int parse_argv(int argc, char *argv[]) {
 
                 case ARG_VERSION:
                         puts(PACKAGE_STRING);
-                        puts(DISTRIBUTION);
                         puts(SYSTEMD_FEATURES);
                         return 0;
 
@@ -495,11 +549,15 @@ static int parse_argv(int argc, char *argv[]) {
 
                 case 'H':
                         arg_transport = TRANSPORT_SSH;
-                        arg_host = optarg;
+                        parse_user_at_host(optarg, &arg_user, &arg_host);
+                        break;
+
+                case ARG_NO_ASK_PASSWORD:
+                        arg_ask_password = false;
                         break;
 
-                case ARG_FIX_SYSTEM:
-                        arg_fix_system = true;
+                case ARG_ADJUST_SYSTEM_CLOCK:
+                        arg_adjust_system_clock = true;
                         break;
 
                 case ARG_NO_PAGER:
@@ -611,6 +669,7 @@ int main(int argc, char *argv[]) {
 
         dbus_error_init(&error);
 
+        setlocale(LC_ALL, "");
         log_parse_environment();
         log_open();