ryantimwilson / rpms / systemd

Forked from rpms/systemd 2 months ago
Clone
698723
From f875436b93c6c5e83f46ab32429c977db4f0b10c Mon Sep 17 00:00:00 2001
698723
From: Felix Stupp <felix.stupp@outlook.com>
698723
Date: Thu, 29 Oct 2020 12:48:48 +0100
698723
Subject: [PATCH] Added option --check-inhibitors for non-tty usage
698723
698723
As described in #2680, systemctl did ignore inhibitors if it is not
698723
attached to a tty to allow scripts to ignore inhibitors automatically.
698723
This pull request preserves this behavior but allows scripts to
698723
explicit check inhibitors if required.
698723
698723
The new parameter '--check-inhibitors=yes' enables this feature.
698723
The old parameter '-i'/'--ignore-inhibitors' was deprecated in favor
698723
of '--check-inhibitors=no', the default behaviour can be specified
698723
with '--check-inhibitors=auto'.
698723
The new parameter is also described in the documentations and shell
698723
completions found here.
698723
698723
(cherry picked from commit b8ebe378b49a31549b8531d4b3177095ef385d55)
698723
698723
Related: #1269726
698723
---
698723
 man/systemctl.xml                  | 38 ++++++++++++++++++++----------
698723
 shell-completion/bash/systemctl.in |  7 ++++--
698723
 shell-completion/zsh/_systemctl.in |  9 ++++++-
698723
 src/systemctl/systemctl.c          | 37 +++++++++++++++++++++--------
698723
 4 files changed, 65 insertions(+), 26 deletions(-)
698723
698723
diff --git a/man/systemctl.xml b/man/systemctl.xml
698723
index ed60a0739f..9f0f4d46ea 100644
698723
--- a/man/systemctl.xml
698723
+++ b/man/systemctl.xml
698723
@@ -323,23 +323,35 @@
698723
         </listitem>
698723
       </varlistentry>
698723
 
698723
+      <varlistentry>
698723
+        <term><option>--check-inhibitors=</option></term>
698723
+
698723
+        <listitem>
698723
+          <para>When system shutdown or sleep state is request, this option controls how to deal with
698723
+          inhibitor locks. It takes one of <literal>auto</literal>, <literal>yes</literal> or
698723
+          <literal>no</literal>. Defaults to <literal>auto</literal>, which will behave like
698723
+          <literal>yes</literal> for interactive invocations (i.e. from a TTY) and <literal>no</literal>
698723
+          for non-interactive invocations.
698723
+          <literal>yes</literal> will let the request respect inhibitor locks.
698723
+          <literal>no</literal> will let the request ignore inhibitor locks.
698723
+          </para>
698723
+          <para>Applications can establish inhibitor locks to avoid that certain important operations
698723
+          (such as CD burning or suchlike) are interrupted by system shutdown or a sleep state. Any user may
698723
+          take these locks and privileged users may override these locks.
698723
+          If any locks are taken, shutdown and sleep state requests will normally fail (unless privileged)
698723
+          and a list of active locks is printed.
698723
+          However, if <literal>no</literal> is specified or <literal>auto</literal> is specified on a
698723
+          non-interactive requests, the established locks are ignored and not shown, and the operation
698723
+          attempted anyway, possibly requiring additional privileges.
698723
+          May be overriden by <option>--force</option>.</para>
698723
+        </listitem>
698723
+      </varlistentry>
698723
+
698723
       <varlistentry>
698723
         <term><option>-i</option></term>
698723
-        <term><option>--ignore-inhibitors</option></term>
698723
 
698723
         <listitem>
698723
-          <para>When system shutdown or a sleep state is requested,
698723
-          ignore inhibitor locks. Applications can establish inhibitor
698723
-          locks to avoid that certain important operations (such as CD
698723
-          burning or suchlike) are interrupted by system shutdown or a
698723
-          sleep state. Any user may take these locks and privileged
698723
-          users may override these locks. If any locks are taken,
698723
-          shutdown and sleep state requests will normally fail
698723
-          (regardless of whether privileged or not) and a list of active locks
698723
-          is printed. However, if <option>--ignore-inhibitors</option>
698723
-          is specified, the locks are ignored and not printed, and the
698723
-          operation attempted anyway, possibly requiring additional
698723
-          privileges.</para>
698723
+          <para>Shortcut for <option>--check-inhibitors=no</option>.</para>
698723
         </listitem>
698723
       </varlistentry>
698723
 
698723
diff --git a/shell-completion/bash/systemctl.in b/shell-completion/bash/systemctl.in
698723
index ba51ae0d34..0c7afea57d 100644
698723
--- a/shell-completion/bash/systemctl.in
698723
+++ b/shell-completion/bash/systemctl.in
698723
@@ -111,9 +111,9 @@ _systemctl () {
698723
                [STANDALONE]='--all -a --reverse --after --before --defaults --force -f --full -l --global
698723
                              --help -h --no-ask-password --no-block --no-legend --no-pager --no-reload --no-wall --now
698723
                              --quiet -q --system --user --version --runtime --recursive -r --firmware-setup
698723
-                             --show-types -i --ignore-inhibitors --plain --failed --value --fail --dry-run --wait'
698723
+                             --show-types --plain --failed --value --fail --dry-run --wait'
698723
                       [ARG]='--host -H --kill-who --property -p --signal -s --type -t --state --job-mode --root
698723
-                             --preset-mode -n --lines -o --output -M --machine --message'
698723
+                             --preset-mode -n --lines -o --output -M --machine --message --check-inhibitors'
698723
         )
698723
 
698723
         if __contains_word "--user" ${COMP_WORDS[*]}; then
698723
@@ -163,6 +163,9 @@ _systemctl () {
698723
                         --machine|-M)
698723
                                 comps=$( __get_machines )
698723
                         ;;
698723
+                        --check-inhibitors)
698723
+                                comps='auto yes no'
698723
+                        ;;
698723
                 esac
698723
                 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
698723
                 return 0
698723
diff --git a/shell-completion/zsh/_systemctl.in b/shell-completion/zsh/_systemctl.in
698723
index 9f576ed77d..b3c51cc843 100644
698723
--- a/shell-completion/zsh/_systemctl.in
698723
+++ b/shell-completion/zsh/_systemctl.in
698723
@@ -363,6 +363,13 @@ _job_modes() {
698723
     _values -s , "${_modes[@]}"
698723
 }
698723
 
698723
+(( $+functions[_systemctl_check_inhibitors] )) ||
698723
+    _systemctl_check_inhibitors() {
698723
+        local -a _modes
698723
+        _modes=(auto yes no)
698723
+        _values -s , "${_modes[@]}"
698723
+    }
698723
+
698723
 # Build arguments for "systemctl" to be used in completion.
698723
 local -a _modes; _modes=("--user" "--system")
698723
 # Use the last mode (they are exclusive and the last one is used).
698723
@@ -380,7 +387,7 @@ _arguments -s \
698723
     '--before[Show units ordered before]' \
698723
     {-l,--full}"[Don't ellipsize unit names on output]" \
698723
     '--show-types[When showing sockets, show socket type]' \
698723
-    {-i,--ignore-inhibitors}'[When executing a job, ignore jobs dependencies]' \
698723
+    '--check-inhibitors[Specify if inhibitors should be checked]:mode:_systemctl_check_inhibitors' \
698723
     {-q,--quiet}'[Suppress output]' \
698723
     '--no-block[Do not wait until operation finished]' \
698723
     '--no-legend[Do not print a legend, i.e. the column headers and the footer with hints]' \
698723
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
698723
index 8bec798373..8bcbf6bf4b 100644
698723
--- a/src/systemctl/systemctl.c
698723
+++ b/src/systemctl/systemctl.c
698723
@@ -121,7 +121,7 @@ static bool arg_no_wall = false;
698723
 static bool arg_no_reload = false;
698723
 static bool arg_value = false;
698723
 static bool arg_show_types = false;
698723
-static bool arg_ignore_inhibitors = false;
698723
+static int arg_check_inhibitors = -1;
698723
 static bool arg_dry_run = false;
698723
 static bool arg_quiet = false;
698723
 static bool arg_full = false;
698723
@@ -3313,17 +3313,19 @@ static int logind_check_inhibitors(enum action a) {
698723
         char **s;
698723
         int r;
698723
 
698723
-        if (arg_ignore_inhibitors || arg_force > 0)
698723
+        if (arg_check_inhibitors == 0 || arg_force > 0)
698723
                 return 0;
698723
 
698723
         if (arg_when > 0)
698723
                 return 0;
698723
 
698723
-        if (geteuid() == 0)
698723
-                return 0;
698723
+        if (arg_check_inhibitors < 0) {
698723
+                if (geteuid() == 0)
698723
+                        return 0;
698723
 
698723
-        if (!on_tty())
698723
-                return 0;
698723
+                if (!on_tty())
698723
+                        return 0;
698723
+        }
698723
 
698723
         if (arg_transport != BUS_TRANSPORT_LOCAL)
698723
                 return 0;
698723
@@ -7237,8 +7239,10 @@ static void systemctl_help(void) {
698723
                "                      When enqueuing a unit job, show full transaction\n"
698723
                "     --show-types     When showing sockets, explicitly show their type\n"
698723
                "     --value          When showing properties, only print the value\n"
698723
-               "  -i --ignore-inhibitors\n"
698723
-               "                      When shutting down or sleeping, ignore inhibitors\n"
698723
+               "     --check-inhibitors=MODE\n"
698723
+               "                      Specify if checking inhibitors before shutting down,\n"
698723
+               "                      sleeping or hibernating\n"
698723
+               "  -i                  Shortcut for --check-inhibitors=no\n"
698723
                "     --kill-who=WHO   Who to send signal to\n"
698723
                "  -s --signal=SIGNAL  Which signal to send\n"
698723
                "     --now            Start or stop unit in addition to enabling or disabling it\n"
698723
@@ -7475,6 +7479,7 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
698723
                 ARG_REVERSE,
698723
                 ARG_AFTER,
698723
                 ARG_BEFORE,
698723
+                ARG_CHECK_INHIBITORS,
698723
                 ARG_DRY_RUN,
698723
                 ARG_SHOW_TYPES,
698723
                 ARG_IRREVERSIBLE,
698723
@@ -7520,7 +7525,8 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
698723
                 { "fail",                no_argument,       NULL, ARG_FAIL                }, /* compatibility only */
698723
                 { "irreversible",        no_argument,       NULL, ARG_IRREVERSIBLE        }, /* compatibility only */
698723
                 { "ignore-dependencies", no_argument,       NULL, ARG_IGNORE_DEPENDENCIES }, /* compatibility only */
698723
-                { "ignore-inhibitors",   no_argument,       NULL, 'i'                     },
698723
+                { "ignore-inhibitors",   no_argument,       NULL, 'i'                     }, /* compatibility only */
698723
+                { "check-inhibitors",    required_argument, NULL, ARG_CHECK_INHIBITORS    },
698723
                 { "value",               no_argument,       NULL, ARG_VALUE               },
698723
                 { "user",                no_argument,       NULL, ARG_USER                },
698723
                 { "system",              no_argument,       NULL, ARG_SYSTEM              },
698723
@@ -7813,7 +7819,18 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
698723
                         break;
698723
 
698723
                 case 'i':
698723
-                        arg_ignore_inhibitors = true;
698723
+                        arg_check_inhibitors = 0;
698723
+                        break;
698723
+
698723
+                case ARG_CHECK_INHIBITORS:
698723
+                        if (streq(optarg, "auto"))
698723
+                                arg_check_inhibitors = -1;
698723
+                        else {
698723
+                                r = parse_boolean(optarg);
698723
+                                if (r < 0)
698723
+                                        return log_error_errno(r, "Failed to parse --check-inhibitors= argument: %s", optarg);
698723
+                                arg_check_inhibitors = r;
698723
+                        }
698723
                         break;
698723
 
698723
                 case ARG_PLAIN: