daandemeyer / rpms / systemd

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