daandemeyer / rpms / systemd

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