803fb7
From fef4e6a045ae703de12ec271b0c8fd02d0bac0fc Mon Sep 17 00:00:00 2001
803fb7
From: Jan Synacek <jsynacek@redhat.com>
803fb7
Date: Thu, 20 Oct 2016 15:20:11 +0200
803fb7
Subject: [PATCH] shared, systemctl: teach is-enabled to show installation
803fb7
 targets
803fb7
803fb7
It may be desired by users to know what targets a particular service is
803fb7
installed into. Improve user friendliness by teaching the is-enabled
803fb7
command to show such information when used with --full.
803fb7
803fb7
This patch makes use of the newly added UnitFileFlags and adds
803fb7
UNIT_FILE_DRY_RUN flag into it. Since the API had already been modified,
803fb7
it's now easy to add the new dry-run feature for other commands as
803fb7
well. As a next step, --dry-run could be added to systemctl, which in
803fb7
turn might pave the way for a long requested dry-run feature when
803fb7
running systemctl start.
803fb7
803fb7
(cherry picked from commit 3b3557c410c7910fae0990599dcb82711cf5fbb7)
803fb7
Resolves: #1413041
803fb7
---
803fb7
 man/systemctl.xml                      |  3 ++
de8967
 src/core/dbus-manager.c                | 44 ++++++++++++++++
803fb7
 src/core/org.freedesktop.systemd1.conf |  4 ++
de8967
 src/shared/install.c                   | 35 +++++++-----
803fb7
 src/shared/install.h                   |  3 +-
de8967
 src/systemctl/systemctl.c              | 73 +++++++++++++++++++++++++-
803fb7
 6 files changed, 145 insertions(+), 17 deletions(-)
803fb7
803fb7
diff --git a/man/systemctl.xml b/man/systemctl.xml
803fb7
index bb21f3a88..4a1aff227 100644
803fb7
--- a/man/systemctl.xml
803fb7
+++ b/man/systemctl.xml
803fb7
@@ -223,6 +223,8 @@
803fb7
           of <command>status</command>, <command>list-units</command>,
803fb7
           <command>list-jobs</command>, and
803fb7
           <command>list-timers</command>.</para>
803fb7
+          <para>Also, show installation targets in the output of
803fb7
+          <command>is-enabled</command>.</para>
803fb7
         </listitem>
803fb7
       </varlistentry>
803fb7
 
803fb7
@@ -1054,6 +1056,7 @@ kobject-uevent 1 systemd-udevd-kernel.socket systemd-udevd.service
803fb7
             exit code of 0 if at least one is enabled, non-zero
803fb7
             otherwise. Prints the current enable status (see table).
803fb7
             To suppress this output, use <option>--quiet</option>.
803fb7
+            To show installation targets, use <option>--full</option>.
803fb7
             </para>
803fb7
 
803fb7
             
803fb7
diff --git a/src/core/dbus-manager.c b/src/core/dbus-manager.c
803fb7
index 5b40aa20f..7ba1b519e 100644
803fb7
--- a/src/core/dbus-manager.c
803fb7
+++ b/src/core/dbus-manager.c
803fb7
@@ -1958,6 +1958,49 @@ static int method_add_dependency_unit_files(sd_bus *bus, sd_bus_message *message
803fb7
         return reply_unit_file_changes_and_free(m, bus, message, -1, changes, n_changes);
803fb7
 }
803fb7
 
803fb7
+static int method_get_unit_file_links(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
803fb7
+        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
803fb7
+        UnitFileChange *changes = NULL;
803fb7
+        unsigned n_changes = 0, i;
803fb7
+        UnitFileFlags flags;
803fb7
+        const char *name;
803fb7
+        char **p;
803fb7
+        int runtime, r;
803fb7
+
803fb7
+        r = sd_bus_message_read(message, "sb", &name, &runtime);
803fb7
+        if (r < 0)
803fb7
+                return r;
803fb7
+
803fb7
+        r = sd_bus_message_new_method_return(message, &reply);
803fb7
+        if (r < 0)
803fb7
+                return r;
803fb7
+
803fb7
+        r = sd_bus_message_open_container(reply, SD_BUS_TYPE_ARRAY, "s");
803fb7
+        if (r < 0)
803fb7
+                return r;
803fb7
+
803fb7
+        p = STRV_MAKE(name);
803fb7
+        flags = UNIT_FILE_DRY_RUN |
803fb7
+                (runtime ? UNIT_FILE_RUNTIME : 0);
803fb7
+
803fb7
+        r = unit_file_disable(UNIT_FILE_SYSTEM, flags, NULL, p, &changes, &n_changes);
803fb7
+        if (r < 0)
803fb7
+                return log_error_errno(r, "Failed to get file links for %s: %m", name);
803fb7
+
803fb7
+        for (i = 0; i < n_changes; i++)
803fb7
+                if (changes[i].type == UNIT_FILE_UNLINK) {
803fb7
+                        r = sd_bus_message_append(reply, "s", changes[i].path);
803fb7
+                        if (r < 0)
803fb7
+                                return r;
803fb7
+                }
803fb7
+
803fb7
+        r = sd_bus_message_close_container(reply);
803fb7
+        if (r < 0)
803fb7
+                return r;
803fb7
+
803fb7
+        return sd_bus_send(bus, reply, NULL);
803fb7
+}
803fb7
+
803fb7
 const sd_bus_vtable bus_manager_vtable[] = {
803fb7
         SD_BUS_VTABLE_START(0),
803fb7
 
803fb7
@@ -2049,6 +2092,7 @@ const sd_bus_vtable bus_manager_vtable[] = {
803fb7
         SD_BUS_METHOD("GetDefaultTarget", NULL, "s", method_get_default_target, SD_BUS_VTABLE_UNPRIVILEGED),
803fb7
         SD_BUS_METHOD("PresetAllUnitFiles", "sbb", "a(sss)", method_preset_all_unit_files, SD_BUS_VTABLE_UNPRIVILEGED),
803fb7
         SD_BUS_METHOD("AddDependencyUnitFiles", "asssbb", "a(sss)", method_add_dependency_unit_files, SD_BUS_VTABLE_UNPRIVILEGED),
803fb7
+        SD_BUS_METHOD("GetUnitFileLinks", "sb", "as", method_get_unit_file_links, SD_BUS_VTABLE_UNPRIVILEGED),
803fb7
 
803fb7
         SD_BUS_SIGNAL("UnitNew", "so", 0),
803fb7
         SD_BUS_SIGNAL("UnitRemoved", "so", 0),
803fb7
diff --git a/src/core/org.freedesktop.systemd1.conf b/src/core/org.freedesktop.systemd1.conf
803fb7
index 6a7a37ee9..3997dd0b4 100644
803fb7
--- a/src/core/org.freedesktop.systemd1.conf
803fb7
+++ b/src/core/org.freedesktop.systemd1.conf
803fb7
@@ -76,6 +76,10 @@
803fb7
                        send_interface="org.freedesktop.systemd1.Manager"
803fb7
                        send_member="GetUnitFileState"/>
803fb7
 
803fb7
+                
803fb7
+                       send_interface="org.freedesktop.systemd1.Manager"
803fb7
+                       send_member="GetUnitFileLinks"/>
803fb7
+
803fb7
                 
803fb7
                        send_interface="org.freedesktop.systemd1.Manager"
803fb7
                        send_member="ListJobs"/>
803fb7
diff --git a/src/shared/install.c b/src/shared/install.c
803fb7
index b3df6b35c..bdfd7b96a 100644
803fb7
--- a/src/shared/install.c
803fb7
+++ b/src/shared/install.c
803fb7
@@ -340,6 +340,7 @@ static int remove_marked_symlinks_fd(
803fb7
                 int fd,
803fb7
                 const char *path,
803fb7
                 const char *config_path,
803fb7
+                bool dry_run,
803fb7
                 bool *restart,
803fb7
                 UnitFileChange **changes,
803fb7
                 unsigned *n_changes) {
803fb7
@@ -400,7 +401,7 @@ static int remove_marked_symlinks_fd(
803fb7
                         }
803fb7
 
803fb7
                         /* This will close nfd, regardless whether it succeeds or not */
803fb7
-                        q = remove_marked_symlinks_fd(remove_symlinks_to, nfd, p, config_path, restart, changes, n_changes);
803fb7
+                        q = remove_marked_symlinks_fd(remove_symlinks_to, nfd, p, config_path, dry_run, restart, changes, n_changes);
803fb7
                         if (q < 0 && r == 0)
803fb7
                                 r = q;
803fb7
 
803fb7
@@ -439,21 +440,23 @@ static int remove_marked_symlinks_fd(
803fb7
                         if (!found)
803fb7
                                 continue;
803fb7
 
803fb7
-                        if (unlink(p) < 0 && errno != ENOENT) {
803fb7
-                                if (r == 0)
803fb7
-                                        r = -errno;
803fb7
-                                continue;
803fb7
-                        }
803fb7
+                        if (!dry_run) {
803fb7
+                                if (unlink(p) < 0 && errno != ENOENT) {
803fb7
+                                        if (r == 0)
803fb7
+                                                r = -errno;
803fb7
+                                        continue;
803fb7
+                                }
803fb7
 
803fb7
-                        path_kill_slashes(p);
803fb7
-                        (void) rmdir_parents(p, config_path);
803fb7
+                                path_kill_slashes(p);
803fb7
+                                (void) rmdir_parents(p, config_path);
803fb7
+                        }
803fb7
 
803fb7
                         unit_file_changes_add(changes, n_changes, UNIT_FILE_UNLINK, p, NULL);
803fb7
 
803fb7
                         q = mark_symlink_for_removal(&remove_symlinks_to, p);
803fb7
                         if (q < 0)
803fb7
                                 return q;
803fb7
-                        if (q > 0)
803fb7
+                        if (q > 0 && !dry_run)
803fb7
                                 *restart = true;
803fb7
                 }
803fb7
         }
803fb7
@@ -464,6 +467,7 @@ static int remove_marked_symlinks_fd(
803fb7
 static int remove_marked_symlinks(
803fb7
                 Set *remove_symlinks_to,
803fb7
                 const char *config_path,
803fb7
+                bool dry_run,
803fb7
                 UnitFileChange **changes,
803fb7
                 unsigned *n_changes) {
803fb7
 
803fb7
@@ -491,7 +495,7 @@ static int remove_marked_symlinks(
803fb7
                 }
803fb7
 
803fb7
                 /* This takes possession of cfd and closes it */
803fb7
-                q = remove_marked_symlinks_fd(remove_symlinks_to, cfd, config_path, config_path, &restart, changes, n_changes);
803fb7
+                q = remove_marked_symlinks_fd(remove_symlinks_to, cfd, config_path, config_path, dry_run, &restart, changes, n_changes);
803fb7
                 if (r == 0)
803fb7
                         r = q;
803fb7
         } while (restart);
803fb7
@@ -1604,6 +1608,7 @@ int unit_file_unmask(
803fb7
         _cleanup_strv_free_ char **todo = NULL;
803fb7
         size_t n_todo = 0, n_allocated = 0;
803fb7
         char **i;
803fb7
+        bool dry_run;
803fb7
         int r, q;
803fb7
 
803fb7
         assert(scope >= 0);
803fb7
@@ -1617,6 +1622,8 @@ int unit_file_unmask(
803fb7
         if (r < 0)
803fb7
                 return r;
803fb7
 
803fb7
+        dry_run = !!(flags & UNIT_FILE_DRY_RUN);
803fb7
+
803fb7
         STRV_FOREACH(i, files) {
803fb7
                 _cleanup_free_ char *path = NULL;
803fb7
 
803fb7
@@ -1655,7 +1662,7 @@ int unit_file_unmask(
803fb7
                 if (!path)
803fb7
                         return -ENOMEM;
803fb7
 
803fb7
-                if (unlink(path) < 0) {
803fb7
+                if (!dry_run && unlink(path) < 0) {
803fb7
                         if (errno != -ENOENT && r >= 0)
803fb7
                                 r = -errno;
803fb7
                 } else {
803fb7
@@ -1667,7 +1674,7 @@ int unit_file_unmask(
803fb7
                 }
803fb7
         }
803fb7
 
803fb7
-        q = remove_marked_symlinks(remove_symlinks_to, config_path, changes, n_changes);
803fb7
+        q = remove_marked_symlinks(remove_symlinks_to, config_path, dry_run, changes, n_changes);
803fb7
         if (r >= 0)
803fb7
                 r = q;
803fb7
 
803fb7
@@ -1931,7 +1938,7 @@ int unit_file_disable(
803fb7
         if (r < 0)
803fb7
                 return r;
803fb7
 
803fb7
-        return remove_marked_symlinks(remove_symlinks_to, config_path, changes, n_changes);
803fb7
+        return remove_marked_symlinks(remove_symlinks_to, config_path, !!(flags & UNIT_FILE_DRY_RUN), changes, n_changes);
803fb7
 }
803fb7
 
803fb7
 int unit_file_reenable(
803fb7
@@ -2243,7 +2250,7 @@ static int execute_preset(
803fb7
                 if (r < 0)
803fb7
                         return r;
803fb7
 
803fb7
-                r = remove_marked_symlinks(remove_symlinks_to, config_path, changes, n_changes);
803fb7
+                r = remove_marked_symlinks(remove_symlinks_to, config_path, false, changes, n_changes);
803fb7
         } else
803fb7
                 r = 0;
803fb7
 
803fb7
diff --git a/src/shared/install.h b/src/shared/install.h
803fb7
index c961b53d0..c236dcfd8 100644
803fb7
--- a/src/shared/install.h
803fb7
+++ b/src/shared/install.h
803fb7
@@ -68,7 +68,8 @@ typedef enum UnitFileChangeType {
803fb7
 
803fb7
 typedef enum UnitFileFlags {
803fb7
         UNIT_FILE_RUNTIME = 1,
803fb7
-        UNIT_FILE_FORCE = 1 << 1
803fb7
+        UNIT_FILE_FORCE = 1 << 1,
803fb7
+        UNIT_FILE_DRY_RUN = 1 << 2
803fb7
 } UnitFileFlags;
803fb7
 
803fb7
 static inline bool unit_file_change_is_modification(UnitFileChangeType type) {
803fb7
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
803fb7
index e0dbf0fda..ff8b4e978 100644
803fb7
--- a/src/systemctl/systemctl.c
803fb7
+++ b/src/systemctl/systemctl.c
803fb7
@@ -5722,6 +5722,63 @@ finish:
803fb7
         return r;
803fb7
 }
803fb7
 
803fb7
+static int show_installation_targets_client_side(const char *name) {
803fb7
+        UnitFileChange *changes = NULL;
803fb7
+        unsigned n_changes = 0, i;
803fb7
+        UnitFileFlags flags;
803fb7
+        char **p;
803fb7
+        int r;
803fb7
+
803fb7
+        p = STRV_MAKE(name);
803fb7
+        flags = UNIT_FILE_DRY_RUN |
803fb7
+                (arg_runtime ? UNIT_FILE_RUNTIME : 0);
803fb7
+
803fb7
+        r = unit_file_disable(UNIT_FILE_SYSTEM, flags, NULL, p, &changes, &n_changes);
803fb7
+        if (r < 0)
803fb7
+                return log_error_errno(r, "Failed to get file links for %s: %m", name);
803fb7
+
803fb7
+        for (i = 0; i < n_changes; i++)
803fb7
+                if (changes[i].type == UNIT_FILE_UNLINK)
803fb7
+                        printf("  %s\n", changes[i].path);
803fb7
+
803fb7
+        return 0;
803fb7
+}
803fb7
+
803fb7
+static int show_installation_targets(sd_bus *bus, const char *name) {
803fb7
+        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
803fb7
+        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
803fb7
+        const char *link;
803fb7
+        int r;
803fb7
+
803fb7
+        r = sd_bus_call_method(
803fb7
+                        bus,
803fb7
+                        "org.freedesktop.systemd1",
803fb7
+                        "/org/freedesktop/systemd1",
803fb7
+                        "org.freedesktop.systemd1.Manager",
803fb7
+                        "GetUnitFileLinks",
803fb7
+                        &error,
803fb7
+                        &reply,
803fb7
+                        "sb", name, arg_runtime);
803fb7
+        if (r < 0)
803fb7
+                return log_error_errno(r, "Failed to get unit file links for %s: %s", name, bus_error_message(&error, r));
803fb7
+
803fb7
+        r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "s");
803fb7
+        if (r < 0)
803fb7
+                return bus_log_parse_error(r);
803fb7
+
803fb7
+        while ((r = sd_bus_message_read(reply, "s", &link)) > 0)
803fb7
+                printf("  %s\n", link);
803fb7
+
803fb7
+        if (r < 0)
803fb7
+                return bus_log_parse_error(r);
803fb7
+
803fb7
+        r = sd_bus_message_exit_container(reply);
803fb7
+        if (r < 0)
803fb7
+                return bus_log_parse_error(r);
803fb7
+
803fb7
+        return 0;
803fb7
+}
803fb7
+
803fb7
 static int unit_is_enabled(sd_bus *bus, char **args) {
803fb7
 
803fb7
         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
803fb7
@@ -5755,8 +5812,14 @@ static int unit_is_enabled(sd_bus *bus, char **args) {
803fb7
                             state == UNIT_FILE_INDIRECT)
803fb7
                                 enabled = true;
803fb7
 
803fb7
-                        if (!arg_quiet)
803fb7
+                        if (!arg_quiet) {
803fb7
                                 puts(unit_file_state_to_string(state));
803fb7
+                                if (arg_full) {
803fb7
+                                        r = show_installation_targets_client_side(*name);
803fb7
+                                        if (r < 0)
803fb7
+                                                return r;
803fb7
+                                }
803fb7
+                        }
803fb7
                 }
803fb7
 
803fb7
         } else {
803fb7
@@ -5785,8 +5848,14 @@ static int unit_is_enabled(sd_bus *bus, char **args) {
803fb7
                         if (STR_IN_SET(s, "enabled", "enabled-runtime", "static", "indirect"))
803fb7
                                 enabled = true;
803fb7
 
803fb7
-                        if (!arg_quiet)
803fb7
+                        if (!arg_quiet) {
803fb7
                                 puts(s);
803fb7
+                                if (arg_full) {
803fb7
+                                        r = show_installation_targets(bus, *name);
803fb7
+                                        if (r < 0)
803fb7
+                                                return r;
803fb7
+                                }
803fb7
+                        }
803fb7
                 }
803fb7
         }
803fb7