analyze: merge {get,set}-log-{level,target} to log-{level,target} (#8020)
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 27 Jan 2018 12:51:32 +0000 (21:51 +0900)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sat, 27 Jan 2018 12:51:32 +0000 (13:51 +0100)
Also, service-watchdogs now shows current watchdog state when
no optional argument is provided.

man/systemd-analyze.xml
shell-completion/bash/systemd-analyze
shell-completion/zsh/_systemd-analyze
src/analyze/analyze.c

index 7e54531..4f7248c 100644 (file)
     <cmdsynopsis>
       <command>systemd-analyze</command>
       <arg choice="opt" rep="repeat">OPTIONS</arg>
-      <arg choice="plain">set-log-level</arg>
-      <arg choice="plain"><replaceable>LEVEL</replaceable></arg>
+      <arg choice="plain">log-level</arg>
+      <arg choice="opt"><replaceable>LEVEL</replaceable></arg>
     </cmdsynopsis>
     <cmdsynopsis>
       <command>systemd-analyze</command>
       <arg choice="opt" rep="repeat">OPTIONS</arg>
-      <arg choice="plain">set-log-target</arg>
-      <arg choice="plain"><replaceable>TARGET</replaceable></arg>
-    </cmdsynopsis>
-    <cmdsynopsis>
-      <command>systemd-analyze</command>
-      <arg choice="opt" rep="repeat">OPTIONS</arg>
-      <arg choice="plain">get-log-level</arg>
-    </cmdsynopsis>
-    <cmdsynopsis>
-      <command>systemd-analyze</command>
-      <arg choice="opt" rep="repeat">OPTIONS</arg>
-      <arg choice="plain">get-log-target</arg>
+      <arg choice="plain">log-target</arg>
+      <arg choice="opt"><replaceable>TARGET</replaceable></arg>
     </cmdsynopsis>
     <cmdsynopsis>
       <command>systemd-analyze</command>
       <command>systemd-analyze</command>
       <arg choice="opt" rep="repeat">OPTIONS</arg>
       <arg choice="plain">service-watchdogs</arg>
-      <arg choice="plain"><replaceable>STATE</replaceable></arg>
+      <arg choice="opt"><replaceable>BOOL</replaceable></arg>
     </cmdsynopsis>
   </refsynopsisdiv>
 
     state. Its format is subject to change without notice and should
     not be parsed by applications.</para>
 
-    <para><command>systemd-analyze set-log-level
-    <replaceable>LEVEL</replaceable></command> changes the current log
-    level of the <command>systemd</command> daemon to
-    <replaceable>LEVEL</replaceable> (accepts the same values as
+    <para><command>systemd-analyze log-level</command>
+    prints the current log level of the <command>systemd</command> daemon.
+    If an optional argument <replaceable>LEVEL</replaceable> is provided, then the command changes the current log
+    level of the <command>systemd</command> daemon to <replaceable>LEVEL</replaceable> (accepts the same values as
     <option>--log-level=</option> described in
     <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>).</para>
 
-    <para><command>systemd-analyze set-log-target
-    <replaceable>TARGET</replaceable></command> changes the current log
-    target of the <command>systemd</command> daemon to
-    <replaceable>TARGET</replaceable> (accepts the same values as
+    <para><command>systemd-analyze log-target</command>
+    prints the current log target of the <command>systemd</command> daemon.
+    If an optional argument <replaceable>TARGET</replaceable> is provided, then the command changes the current log
+    target of the <command>systemd</command> daemon to <replaceable>TARGET</replaceable> (accepts the same values as
     <option>--log-target=</option>, described in
     <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>).</para>
 
-    <para><command>systemd-analyze get-log-level</command>
-    prints the current log level of the <command>systemd</command> daemon.</para>
-
-    <para><command>systemd-analyze get-log-target</command>
-    prints the current log target of the <command>systemd</command> daemon.</para>
-
     <para><command>systemd-analyze syscall-filter <optional><replaceable>SET</replaceable>…</optional></command>
     will list system calls contained in the specified system call set <replaceable>SET</replaceable>,
     or all known sets if no sets are specified. Argument <replaceable>SET</replaceable> must include
     syntax described in
     <citerefentry><refentrytitle>systemd.time</refentrytitle><manvolnum>7</manvolnum></citerefentry>.</para>
 
-    <para><command>systemd-analyze service-watchdogs
-    <replaceable>STATE</replaceable></command> globally enables or disables the service
+    <para><command>systemd-analyze service-watchdogs</command>
+    prints the current state of service runtime watchdogs of the <command>systemd</command> daemon.
+    If an optional boolean argument is provided, then globally enables or disables the service
     runtime watchdogs (<option>WatchdogSec=</option>) and emergency actions (e.g.
     <option>OnFailure=</option> or <option>StartLimitAction=</option>); see
     <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>.
index 171802e..fb30487 100644 (file)
@@ -42,11 +42,11 @@ _systemd_analyze() {
         )
 
         local -A VERBS=(
-                [STANDALONE]='time blame plot dump get-log-level get-log-target calendar'
+                [STANDALONE]='time blame plot dump calendar'
                 [CRITICAL_CHAIN]='critical-chain'
                 [DOT]='dot'
-                [LOG_LEVEL]='set-log-level'
-                [LOG_TARGET]='set-log-target'
+                [LOG_LEVEL]='log-level'
+                [LOG_TARGET]='log-target'
                 [VERIFY]='verify'
                 [SECCOMP_FILTER]='syscall-filter'
                 [SERVICE_WATCHDOGS]='service-watchdogs'
index 85e46dc..ae13f4b 100644 (file)
@@ -1,13 +1,13 @@
 #compdef systemd-analyze
 # SPDX-License-Identifier: LGPL-2.1+
 
-_systemd_analyze_set-log-level() {
+_systemd_analyze_log-level() {
     local -a _levels
     _levels=(debug info notice warning err crit alert emerg)
     _describe -t level 'logging level' _levels || compadd "$@"
 }
 
-_systemd_analyze_set-log-target() {
+_systemd_analyze_log-target() {
     local -a _targets
     _targets=(console journal kmsg journal-or-kmsg null)
     _describe -t target 'logging target' _targets || compadd "$@"
@@ -33,10 +33,8 @@ _systemd_analyze_command(){
         'plot:Output SVG graphic showing service initialization'
         'dot:Dump dependency graph (in dot(1) format)'
         'dump:Dump server status'
-        'set-log-level:Set systemd log threshold'
-        'set-log-target:Set systemd log target'
-        'get-log-level:Get systemd log threshold'
-        'get-log-target:Get systemd log target'
+        'log-level:Get/set systemd log threshold'
+        'log-target:Get/set systemd log target'
         'service-watchdogs:Get/set service watchdog status'
         'syscall-filter:List syscalls in seccomp filter'
         'verify:Check unit files for correctness'
index 4611044..834620a 100644 (file)
@@ -1353,6 +1353,10 @@ static int get_log_level(int argc, char *argv[], void *userdata) {
         return 0;
 }
 
+static int get_or_set_log_level(int argc, char *argv[], void *userdata) {
+        return (argc == 1) ? get_log_level(argc, argv, userdata) : set_log_level(argc, argv, userdata);
+}
+
 static int set_log_target(int argc, char *argv[], void *userdata) {
         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
         _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
@@ -1405,6 +1409,10 @@ static int get_log_target(int argc, char *argv[], void *userdata) {
         return 0;
 }
 
+static int get_or_set_log_target(int argc, char *argv[], void *userdata) {
+        return (argc == 1) ? get_log_target(argc, argv, userdata) : set_log_target(argc, argv, userdata);
+}
+
 #if HAVE_SECCOMP
 static void dump_syscall_filter(const SyscallFilterSet *set) {
         const char *syscall;
@@ -1526,19 +1534,39 @@ static int service_watchdogs(int argc, char *argv[], void *userdata) {
         _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
         int b, r;
 
-        assert(argc == 2);
+        assert(IN_SET(argc, 1, 2));
         assert(argv);
 
+        r = acquire_bus(false, &bus);
+        if (r < 0)
+                return log_error_errno(r, "Failed to create bus connection: %m");
+
+        /* get ServiceWatchdogs */
+        if (argc == 1) {
+                r = sd_bus_get_property_trivial(
+                                bus,
+                                "org.freedesktop.systemd1",
+                                "/org/freedesktop/systemd1",
+                                "org.freedesktop.systemd1.Manager",
+                                "ServiceWatchdogs",
+                                &error,
+                                'b',
+                                &b);
+                if (r < 0)
+                        return log_error_errno(r, "Failed to get service-watchdog state: %s", bus_error_message(&error, r));
+
+                printf("%s\n", yes_no(!!b));
+
+                return 0;
+        }
+
+        /* set ServiceWatchdogs */
         b = parse_boolean(argv[1]);
         if (b < 0) {
                 log_error("Failed to parse service-watchdogs argument.");
                 return -EINVAL;
         }
 
-        r = acquire_bus(false, &bus);
-        if (r < 0)
-                return log_error_errno(r, "Failed to create bus connection: %m");
-
         r = sd_bus_set_property(
                         bus,
                         "org.freedesktop.systemd1",
@@ -1549,7 +1577,7 @@ static int service_watchdogs(int argc, char *argv[], void *userdata) {
                         "b",
                         b);
         if (r < 0)
-                return log_error_errno(r, "Failed to issue method call: %s", bus_error_message(&error, r));
+                return log_error_errno(r, "Failed to set service-watchdog state: %s", bus_error_message(&error, r));
 
         return 0;
 }
@@ -1588,15 +1616,13 @@ static int help(int argc, char *argv[], void *userdata) {
                "  critical-chain [UNIT...] Print a tree of the time critical chain of units\n"
                "  plot                     Output SVG graphic showing service initialization\n"
                "  dot [UNIT...]            Output dependency graph in man:dot(1) format\n"
-               "  set-log-level LEVEL      Set logging threshold for manager\n"
-               "  set-log-target TARGET    Set logging target for manager\n"
-               "  get-log-level            Get logging threshold for manager\n"
-               "  get-log-target           Get logging target for manager\n"
+               "  log-level [LEVEL]        Get/set logging threshold for manager\n"
+               "  log-target [TARGET]      Get/set logging target for manager\n"
                "  dump                     Output state serialization of service manager\n"
                "  syscall-filter [NAME...] Print list of syscalls in seccomp filter\n"
                "  verify FILE...           Check unit files for correctness\n"
                "  calendar SPEC...         Validate repetitive calendar time events\n"
-               "  service-watchdogs on/off Enable/disable service watchdogs\n"
+               "  service-watchdogs [BOOL] Get/set service watchdog state\n"
                , program_invocation_short_name);
 
         /* When updating this list, including descriptions, apply
@@ -1748,6 +1774,9 @@ int main(int argc, char *argv[]) {
                 { "critical-chain",    VERB_ANY, VERB_ANY, 0,            analyze_critical_chain },
                 { "plot",              VERB_ANY, 1,        0,            analyze_plot           },
                 { "dot",               VERB_ANY, VERB_ANY, 0,            dot                    },
+                { "log-level",         VERB_ANY, 2,        0,            get_or_set_log_level   },
+                { "log-target",        VERB_ANY, 2,        0,            get_or_set_log_target  },
+                /* The following four verbs are deprecated aliases */
                 { "set-log-level",     2,        2,        0,            set_log_level          },
                 { "get-log-level",     VERB_ANY, 1,        0,            get_log_level          },
                 { "set-log-target",    2,        2,        0,            set_log_target         },
@@ -1756,7 +1785,7 @@ int main(int argc, char *argv[]) {
                 { "syscall-filter",    VERB_ANY, VERB_ANY, 0,            dump_syscall_filters   },
                 { "verify",            2,        VERB_ANY, 0,            do_verify              },
                 { "calendar",          2,        VERB_ANY, 0,            test_calendar          },
-                { "service-watchdogs", 2,        2,        0,            service_watchdogs      },
+                { "service-watchdogs", VERB_ANY, 2,        0,            service_watchdogs      },
                 {}
         };