summaryrefslogtreecommitdiffstats
path: root/src/systemctl
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2023-12-02 16:25:15 +0100
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2023-12-05 18:47:19 +0100
commit329050c5e2c7e9561699f87b5edb72edd0d54c96 (patch)
treeab988f2bdfca93855b95a42791510b5e59ffb6e3 /src/systemctl
parentsystemctl: add message when edit is aborted (diff)
downloadsystemd-329050c5e2c7e9561699f87b5edb72edd0d54c96.tar.xz
systemd-329050c5e2c7e9561699f87b5edb72edd0d54c96.zip
systemctl: add "edit --stdin"
This is a fancy wrapper around "cat <<EOF", but: - the user doesn't need to figure out the file name, - parent directories are created automatically, - daemon-reload is implied, so it's a convenient way to create units or drop-ins. Closes https://github.com/systemd/systemd/issues/21862.
Diffstat (limited to 'src/systemctl')
-rw-r--r--src/systemctl/systemctl-edit.c7
-rw-r--r--src/systemctl/systemctl.c11
-rw-r--r--src/systemctl/systemctl.h1
3 files changed, 17 insertions, 2 deletions
diff --git a/src/systemctl/systemctl-edit.c b/src/systemctl/systemctl-edit.c
index 367afa20f7..d967b1649e 100644
--- a/src/systemctl/systemctl-edit.c
+++ b/src/systemctl/systemctl-edit.c
@@ -317,12 +317,13 @@ int verb_edit(int argc, char *argv[], void *userdata) {
.marker_end = DROPIN_MARKER_END,
.remove_parent = !arg_full,
.overwrite_with_origin = true,
+ .stdin = arg_stdin,
};
_cleanup_strv_free_ char **names = NULL;
sd_bus *bus;
int r;
- if (!on_tty())
+ if (!on_tty() && !arg_stdin)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot edit units if not on a tty.");
if (arg_transport != BUS_TRANSPORT_LOCAL)
@@ -342,6 +343,10 @@ int verb_edit(int argc, char *argv[], void *userdata) {
if (strv_isempty(names))
return log_error_errno(SYNTHETIC_ERRNO(ENOENT), "No units matched the specified patterns.");
+ if (arg_stdin && arg_full && strv_length(names) != 1)
+ return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
+ "With 'edit --stdin --full', exactly one unit for editing must be specified.");
+
STRV_FOREACH(tmp, names) {
r = unit_is_masked(bus, *tmp);
if (r < 0)
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
index dd6f6c9873..2a1d2d7967 100644
--- a/src/systemctl/systemctl.c
+++ b/src/systemctl/systemctl.c
@@ -103,6 +103,7 @@ bool arg_kill_value_set = false;
char *arg_root = NULL;
char *arg_image = NULL;
usec_t arg_when = 0;
+bool arg_stdin = false;
const char *arg_reboot_argument = NULL;
enum action arg_action = ACTION_SYSTEMCTL;
BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
@@ -335,6 +336,7 @@ static int systemctl_help(void) {
" --drop-in=NAME Edit unit files using the specified drop-in file name\n"
" --when=TIME Schedule halt/power-off/reboot/kexec action after\n"
" a certain timestamp\n"
+ " --stdin Read contents of edited file from stdin\n"
"\nSee the %2$s for details.\n",
program_invocation_short_name,
link,
@@ -461,6 +463,7 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
ARG_NO_WARN,
ARG_DROP_IN,
ARG_WHEN,
+ ARG_STDIN,
};
static const struct option options[] = {
@@ -527,6 +530,7 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
{ "marked", no_argument, NULL, ARG_MARKED },
{ "drop-in", required_argument, NULL, ARG_DROP_IN },
{ "when", required_argument, NULL, ARG_WHEN },
+ { "stdin", no_argument, NULL, ARG_STDIN },
{}
};
@@ -1017,6 +1021,10 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
break;
+ case ARG_STDIN:
+ arg_stdin = true;
+ break;
+
case '.':
/* Output an error mimicking getopt, and print a hint afterwards */
log_error("%s: invalid option -- '.'", program_invocation_name);
@@ -1067,7 +1075,8 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
}
if (arg_image && arg_root)
- return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Please specify either --root= or --image=, the combination of both is not supported.");
+ return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
+ "Please specify either --root= or --image=, the combination of both is not supported.");
return 1;
}
diff --git a/src/systemctl/systemctl.h b/src/systemctl/systemctl.h
index e8ba8f76a0..9f63443fde 100644
--- a/src/systemctl/systemctl.h
+++ b/src/systemctl/systemctl.h
@@ -83,6 +83,7 @@ extern int arg_kill_value;
extern bool arg_kill_value_set;
extern char *arg_root;
extern usec_t arg_when;
+extern bool arg_stdin;
extern const char *arg_reboot_argument;
extern enum action arg_action;
extern BusTransport arg_transport;