ryantimwilson / rpms / systemd

Forked from rpms/systemd a month ago
Clone
1abbee
From 1f8b1e35e3ec80c50201403171b7375ff14c808c Mon Sep 17 00:00:00 2001
1abbee
From: Michal Sekletar <msekletar@users.noreply.github.com>
1abbee
Date: Tue, 26 Jul 2016 14:25:52 +0200
1abbee
Subject: [PATCH] systemctl: allow disable on the unit file path, but warn
1abbee
 about it (#3806)
1abbee
1abbee
systemd now returns an error when it is asked to perform disable on the
1abbee
unit file path. In the past this was allowed, but systemd never really
1abbee
considered an actual content of the [Install] section of the unit
1abbee
file. Instead it performed disable on the unit name, i.e. purged all
1abbee
symlinks pointing to the given unit file (undo of implicit link action
1abbee
done by systemd when enable is called on the unit file path) and all
1abbee
symlinks that have the same basename as the given unit file.
1abbee
1abbee
However, to notice that [Install] info of the file is not consulted one
1abbee
must create additional symlinks manually. I argue that in most cases
1abbee
users do not create such links. Let's be nice to our users and don't
1abbee
break existing scripts that expect disable to work with the unit file
1abbee
path.
1abbee
1abbee
Fixes #3706.
1abbee
1abbee
IMPORTANT
1abbee
=========
1abbee
Note that in this downstream backport we actually pass false to
1abbee
normalize_names(), hence it will not produce any warning when full path
1abbee
is passed in. This is because we need to preserve behavior compatible
1abbee
with prior systemd versions shipped in RHEL.
1abbee
1abbee
Cherry-picked from: 1d3c86c06fca8311923fcf81af0ab0bbb66e1edd
1abbee
Resolves: #1348208
1abbee
---
1abbee
 src/systemctl/systemctl.c | 29 +++++++++++++++++++++++++++++
1abbee
 1 file changed, 29 insertions(+)
1abbee
1abbee
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
1abbee
index b7496c0..5899818 100644
1abbee
--- a/src/systemctl/systemctl.c
1abbee
+++ b/src/systemctl/systemctl.c
1abbee
@@ -5333,6 +5333,29 @@ static int mangle_names(char **original_names, char ***mangled_names) {
1abbee
         return 0;
1abbee
 }
1abbee
 
1abbee
+static int normalize_names(char **names, bool warn_if_path) {
1abbee
+        char **u;
1abbee
+        bool was_path = false;
1abbee
+
1abbee
+        STRV_FOREACH(u, names) {
1abbee
+                int r;
1abbee
+
1abbee
+                if (!is_path(*u))
1abbee
+                        continue;
1abbee
+
1abbee
+                r = free_and_strdup(u, basename(*u));
1abbee
+                if (r < 0)
1abbee
+                        return log_error_errno(r, "Failed to normalize unit file path: %m");
1abbee
+
1abbee
+                was_path = true;
1abbee
+        }
1abbee
+
1abbee
+        if (warn_if_path && was_path)
1abbee
+                log_warning("Warning: Can't execute disable on the unit file path. Proceeding with the unit name.");
1abbee
+
1abbee
+        return 0;
1abbee
+}
1abbee
+
1abbee
 static int enable_unit(sd_bus *bus, char **args) {
1abbee
         _cleanup_strv_free_ char **names = NULL;
1abbee
         const char *verb = args[0];
1abbee
@@ -5357,6 +5380,12 @@ static int enable_unit(sd_bus *bus, char **args) {
1abbee
         if (strv_isempty(names))
1abbee
                 return 0;
1abbee
 
1abbee
+        if (streq(verb, "disable")) {
1abbee
+                r = normalize_names(names, false);
1abbee
+                if (r < 0)
1abbee
+                        return r;
1abbee
+        }
1abbee
+
1abbee
         if (!bus || avoid_bus()) {
1abbee
                 if (streq(verb, "enable")) {
1abbee
                         r = unit_file_enable(arg_scope, arg_runtime, arg_root, names, arg_force, &changes, &n_changes);