ryantimwilson / rpms / systemd

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