From 86beb21302082e9df0aa8576b89fd29c9e1a43be Mon Sep 17 00:00:00 2001 From: =?utf8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Sat, 13 Feb 2021 15:55:10 +0100 Subject: [PATCH] systemctl,loginctl,machinectl: use a shared helper for arg_signal 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 | 12 +----------- man/standard-options.xml | 15 +++++++++++++++ man/systemctl.xml | 14 +------------- src/login/loginctl.c | 12 ++++-------- src/machine/machinectl.c | 13 ++++--------- src/shared/meson.build | 2 ++ src/shared/parse-argument.c | 25 +++++++++++++++++++++++++ src/shared/parse-argument.h | 4 ++++ src/systemctl/systemctl.c | 14 ++++---------- 9 files changed, 60 insertions(+), 51 deletions(-) create mode 100644 src/shared/parse-argument.c create mode 100644 src/shared/parse-argument.h diff --git a/man/machinectl.xml b/man/machinectl.xml index 9026849559..556fe309dc 100644 --- a/man/machinectl.xml +++ b/man/machinectl.xml @@ -699,17 +699,7 @@ . - - - - - When used with kill, choose - which signal to send to selected processes. Must be one of the - well-known signal specifiers, such as - SIGTERM, SIGINT or - SIGSTOP. If omitted, defaults to - SIGTERM. - + diff --git a/man/standard-options.xml b/man/standard-options.xml index 8ba5b09df8..848439119e 100644 --- a/man/standard-options.xml +++ b/man/standard-options.xml @@ -61,4 +61,19 @@ (for a pretty version of the same, with indentation and line breaks) or off (to turn off JSON output, the default). + + + + + + + When used with kill, choose which signal to send to selected processes. Must + be one of the well-known signal specifiers such as SIGTERM, + SIGINT or SIGSTOP. If omitted, defaults to + . + + The special value help will list the known values and the program will exit + immediately. + + diff --git a/man/systemctl.xml b/man/systemctl.xml index db4e2b2c65..2ed58eb33b 100644 --- a/man/systemctl.xml +++ b/man/systemctl.xml @@ -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 . - - - - - - - When used with kill, choose which - signal to send to selected processes. Must be one of the - well-known signal specifiers such as SIGTERM, SIGINT or - SIGSTOP. If omitted, defaults to - . - - + diff --git a/src/login/loginctl.c b/src/login/loginctl.c index 918e05e1cd..3e135858a8 100644 --- a/src/login/loginctl.c +++ b/src/login/loginctl.c @@ -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': diff --git a/src/machine/machinectl.c b/src/machine/machinectl.c index 3d6048eaff..1ae10e5880 100644 --- a/src/machine/machinectl.c +++ b/src/machine/machinectl.c @@ -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: diff --git a/src/shared/meson.build b/src/shared/meson.build index 4e47e15c09..6d9a5c5b65 100644 --- a/src/shared/meson.build +++ b/src/shared/meson.build @@ -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 index 0000000000..a30d3adc13 --- /dev/null +++ b/src/shared/parse-argument.c @@ -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 index 0000000000..7a5419174d --- /dev/null +++ b/src/shared/parse-argument.h @@ -0,0 +1,4 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +#pragma once + +int parse_signal_argument(const char *s, int *ret); diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index 97a02ff035..0371ff355e 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -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: -- 2.25.1