]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
systemctl,loginctl,machinectl: use a shared helper for arg_signal
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sat, 13 Feb 2021 14:55:10 +0000 (15:55 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sun, 14 Feb 2021 14:59:09 +0000 (15:59 +0100)
I seems frivolous to yet another two -util.[ch] files, but the helper
should be in shared/ and it doesn't seem to fit anywhere else.

man/machinectl.xml
man/standard-options.xml
man/systemctl.xml
src/login/loginctl.c
src/machine/machinectl.c
src/shared/meson.build
src/shared/parse-argument.c [new file with mode: 0644]
src/shared/parse-argument.h [new file with mode: 0644]
src/systemctl/systemctl.c

index 9026849559b57ba4c1f6380948a8d9fb7752e9ad..556fe309dc469a9a4e73a7a5e0043aa270e2ddbb 100644 (file)
         <option>all</option>.</para></listitem>
       </varlistentry>
 
-      <varlistentry>
-        <term><option>-s</option></term>
-        <term><option>--signal=</option></term>
-
-        <listitem><para>When used with <command>kill</command>, choose
-        which signal to send to selected processes. Must be one of the
-        well-known signal specifiers, such as
-        <constant>SIGTERM</constant>, <constant>SIGINT</constant> or
-        <constant>SIGSTOP</constant>. If omitted, defaults to
-        <constant>SIGTERM</constant>.</para></listitem>
-      </varlistentry>
+      <xi:include href="standard-options.xml" xpointer="signal" />
 
       <varlistentry>
          <term><option>--uid=</option></term>
index 8ba5b09df85f291e4f0803b034085e5e3e1b8494..848439119e9c8f1ae909183fdfa0324bc3546bef 100644 (file)
     (for a pretty version of the same, with indentation and line breaks) or <literal>off</literal> (to turn
     off JSON output, the default).</para></listitem>
   </varlistentry>
+
+  <varlistentry id='signal'>
+    <term><option>-s</option></term>
+    <term><option>--signal=</option></term>
+
+    <listitem>
+      <para>When used with <command>kill</command>, choose which signal to send to selected processes. Must
+      be one of the well-known signal specifiers such as <constant>SIGTERM</constant>,
+      <constant>SIGINT</constant> or <constant>SIGSTOP</constant>. If omitted, defaults to
+      <option>SIGTERM</option>.</para>
+
+      <para>The special value <literal>help</literal> will list the known values and the program will exit
+      immediately.</para>
+    </listitem>
+  </varlistentry>
 </variablelist>
index db4e2b2c65c551d37990cf53ac888e569d087ef5..2ed58eb33be95a3d64d14efdc8cb4589dceabc19 100644 (file)
@@ -2067,21 +2067,9 @@ Jan 12 10:46:45 example.com bluetoothd[8900]: gatt-time-server: Input/output err
           is defined. If omitted, defaults to
           <option>all</option>.</para>
         </listitem>
-
       </varlistentry>
 
-      <varlistentry>
-        <term><option>-s</option></term>
-        <term><option>--signal=</option></term>
-
-        <listitem>
-          <para>When used with <command>kill</command>, choose which
-          signal to send to selected processes. Must be one of the
-          well-known signal specifiers such as <constant>SIGTERM</constant>, <constant>SIGINT</constant> or
-          <constant>SIGSTOP</constant>. If omitted, defaults to
-          <option>SIGTERM</option>.</para>
-        </listitem>
-      </varlistentry>
+      <xi:include href="standard-options.xml" xpointer="signal" />
 
       <varlistentry>
         <term><option>--what=</option></term>
index 918e05e1cdd98c4448bdd40ef7aa9efcc90d1c79..3e135858a83c5d0b311779eda85b4852fbfce3d4 100644 (file)
@@ -22,6 +22,7 @@
 #include "main-func.h"
 #include "memory-util.h"
 #include "pager.h"
+#include "parse-argument.h"
 #include "parse-util.h"
 #include "pretty-print.h"
 #include "process-util.h"
@@ -1395,14 +1396,9 @@ static int parse_argv(int argc, char *argv[]) {
                         break;
 
                 case 's':
-                        if (streq(optarg, "help")) {
-                                DUMP_STRING_TABLE(signal, int, _NSIG);
-                                return 0;
-                        }
-
-                        arg_signal = signal_from_string(optarg);
-                        if (arg_signal < 0)
-                                return log_error_errno(arg_signal, "Failed to parse signal string %s.", optarg);
+                        r = parse_signal_argument(optarg, &arg_signal);
+                        if (r <= 0)
+                                return r;
                         break;
 
                 case 'H':
index 3d6048eaff7c979faffc89cb043e8ea3534bd3ba..1ae10e5880a1d3ce6cc2a9d9a2db027c04094a3c 100644 (file)
@@ -39,6 +39,7 @@
 #include "mkdir.h"
 #include "nulstr-util.h"
 #include "pager.h"
+#include "parse-argument.h"
 #include "parse-util.h"
 #include "path-util.h"
 #include "pretty-print.h"
@@ -2715,15 +2716,9 @@ static int parse_argv(int argc, char *argv[]) {
                         break;
 
                 case 's':
-                        if (streq(optarg, "help")) {
-                                DUMP_STRING_TABLE(signal, int, _NSIG);
-                                return 0;
-                        }
-
-                        r = signal_from_string(optarg);
-                        if (r < 0)
-                                return log_error_errno(r, "Failed to parse signal string %s.", optarg);
-                        arg_signal = r;
+                        r = parse_signal_argument(optarg, &arg_signal);
+                        if (r <= 0)
+                                return r;
                         break;
 
                 case ARG_NO_ASK_PASSWORD:
index 4e47e15c09dff26b1b7de90aaa64b4d98d82029e..6d9a5c5b65c2e8279c47deb78724fb9a945fce1a 100644 (file)
@@ -202,6 +202,8 @@ shared_sources = files('''
         output-mode.h
         pager.c
         pager.h
+        parse-argument.c
+        parse-argument.h
         pe-header.h
         pkcs11-util.c
         pkcs11-util.h
diff --git a/src/shared/parse-argument.c b/src/shared/parse-argument.c
new file mode 100644 (file)
index 0000000..a30d3ad
--- /dev/null
@@ -0,0 +1,25 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include "parse-argument.h"
+#include "signal-util.h"
+#include "string-table.h"
+#include "string-util.h"
+
+int parse_signal_argument(const char *s, int *ret) {
+        int r;
+
+        assert(s);
+        assert(ret);
+
+        if (streq(s, "help")) {
+                DUMP_STRING_TABLE(signal, int, _NSIG);
+                return 0;
+        }
+
+        r = signal_from_string(s);
+        if (r < 0)
+                return log_error_errno(r, "Failed to parse signal string \"%s\".", s);
+
+        *ret = r;
+        return 1; /* work to do */
+}
diff --git a/src/shared/parse-argument.h b/src/shared/parse-argument.h
new file mode 100644 (file)
index 0000000..7a54191
--- /dev/null
@@ -0,0 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+#pragma once
+
+int parse_signal_argument(const char *s, int *ret);
index 97a02ff035e4c271dd99dedfdef7ea725d38dcc4..0371ff355ec4b9edd72708d799eff06ba0a55bc6 100644 (file)
@@ -11,6 +11,7 @@
 #include "main-func.h"
 #include "output-mode.h"
 #include "pager.h"
+#include "parse-argument.h"
 #include "path-util.h"
 #include "pretty-print.h"
 #include "rlimit-util.h"
@@ -673,16 +674,9 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
                         break;
 
                 case 's':
-                        if (streq(optarg, "help")) {
-                                DUMP_STRING_TABLE(signal, int, _NSIG);
-                                return 0;
-                        }
-
-                        arg_signal = signal_from_string(optarg);
-                        if (arg_signal < 0)
-                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
-                                                       "Failed to parse signal string %s.",
-                                                       optarg);
+                        r = parse_signal_argument(optarg, &arg_signal);
+                        if (r <= 0)
+                                return r;
                         break;
 
                 case ARG_NO_ASK_PASSWORD: