ryantimwilson / rpms / systemd

Forked from rpms/systemd a month ago
Clone
923a60
From 65d36d59c936650e141fcbf38b287627fd0ba21a Mon Sep 17 00:00:00 2001
923a60
From: Jan Synacek <jsynacek@redhat.com>
923a60
Date: Fri, 15 May 2015 09:54:10 +0200
923a60
Subject: [PATCH] systemctl: introduce --now for enable, disable and mask
923a60
923a60
https://bugs.freedesktop.org/show_bug.cgi?id=42940
923a60
923a60
Conflicts:
923a60
	src/libsystemd/sd-bus/bus-util.c
923a60
	src/libsystemd/sd-bus/bus-util.h
923a60
	src/systemctl/systemctl.c
923a60
923a60
Cherry-picked from: 57ab2eabb8f92fad5239c7d4492e9c6e23ee0678
923a60
Resolves: #1233081
923a60
---
923a60
 Makefile.am                      |   1 +
923a60
 man/systemctl.xml                |  33 ++++++---
923a60
 src/libsystemd/sd-bus/bus-util.c |   6 +-
923a60
 src/libsystemd/sd-bus/bus-util.h |   3 +-
923a60
 src/machine/machinectl.c         |   2 +-
923a60
 src/shared/install.c             | 112 +++++++++++++++----------------
923a60
 src/shared/install.h             |   1 +
923a60
 src/systemctl/systemctl.c        |  28 ++++++--
923a60
 8 files changed, 114 insertions(+), 72 deletions(-)
923a60
923a60
diff --git a/Makefile.am b/Makefile.am
923a60
index 604eaf2f17..d3fb398fe5 100644
923a60
--- a/Makefile.am
923a60
+++ b/Makefile.am
923a60
@@ -5253,6 +5253,7 @@ machinectl_LDADD = \
923a60
 	libsystemd-internal.la \
923a60
 	libsystemd-logs.la \
923a60
 	libsystemd-journal-internal.la \
923a60
+	libsystemd-units.la \
923a60
 	libsystemd-shared.la
923a60
 
923a60
 rootbin_PROGRAMS += \
923a60
diff --git a/man/systemctl.xml b/man/systemctl.xml
923a60
index 3c4c9cb92c..44ec0d7bcf 100644
923a60
--- a/man/systemctl.xml
923a60
+++ b/man/systemctl.xml
923a60
@@ -455,6 +455,18 @@
923a60
         </listitem>
923a60
       </varlistentry>
923a60
 
923a60
+      <varlistentry>
923a60
+        <term><option>--now</option></term>
923a60
+
923a60
+        <listitem>
923a60
+          <para>When used with <command>enable</command>, the units
923a60
+          will also be started. When used with <command>disable</command> or
923a60
+          <command>mask</command>, the units will also be stopped. The start
923a60
+          or stop operation is only carried out when the respective enable or
923a60
+          disable operation has been successful.</para>
923a60
+        </listitem>
923a60
+      </varlistentry>
923a60
+
923a60
       <varlistentry>
923a60
         <term><option>--root=</option></term>
923a60
 
923a60
@@ -909,11 +921,12 @@ kobject-uevent 1 systemd-udevd-kernel.socket systemd-udevd.service
923a60
             the changes are taken into account immediately. Note that
923a60
             this does <emphasis>not</emphasis> have the effect of also
923a60
             starting any of the units being enabled. If this
923a60
-            is desired, a separate <command>start</command> command must
923a60
-            be invoked for the unit. Also note that in case of instance
923a60
-            enablement, symlinks named the same as instances are created in
923a60
-            the install location, however they all point to the same
923a60
-            template unit file.</para>
923a60
+            is desired, either <option>--now</option> should be used
923a60
+            together with this command, or an additional <command>start</command>
923a60
+            command must be invoked for the unit. Also note that in case of
923a60
+            instance enablement, symlinks named the same as instances
923a60
+            are created in the install location, however they all point to the
923a60
+            same template unit file.</para>
923a60
 
923a60
             <para>This command will print the actions executed. This
923a60
             output may be suppressed by passing <option>--quiet</option>.
923a60
@@ -968,9 +981,10 @@ kobject-uevent 1 systemd-udevd-kernel.socket systemd-udevd.service
923a60
             <command>enable</command>. This call implicitly reloads the
923a60
             systemd daemon configuration after completing the disabling
923a60
             of the units. Note that this command does not implicitly
923a60
-            stop the units that are being disabled. If this is desired,
923a60
-            an additional <command>stop</command> command should be
923a60
-            executed afterwards.</para>
923a60
+            stop the units that are being disabled. If this is desired, either
923a60
+            <option>--now</option> should be used together with this command, or
923a60
+            an additional <command>stop</command> command should be executed
923a60
+            afterwards.</para>
923a60
 
923a60
             <para>This command will print the actions executed. This
923a60
             output may be suppressed by passing <option>--quiet</option>.
923a60
@@ -1116,7 +1130,8 @@ kobject-uevent 1 systemd-udevd-kernel.socket systemd-udevd.service
923a60
             activation of the unit, including enablement and manual
923a60
             activation. Use this option with care. This honors the
923a60
             <option>--runtime</option> option to only mask temporarily
923a60
-            until the next reboot of the system.</para>
923a60
+            until the next reboot of the system. The <option>--now</option>
923a60
+            option can be used to ensure that the units are also stopped.</para>
923a60
           </listitem>
923a60
         </varlistentry>
923a60
 
923a60
diff --git a/src/libsystemd/sd-bus/bus-util.c b/src/libsystemd/sd-bus/bus-util.c
923a60
index 2e6d889620..fff00d9f93 100644
923a60
--- a/src/libsystemd/sd-bus/bus-util.c
923a60
+++ b/src/libsystemd/sd-bus/bus-util.c
923a60
@@ -1849,7 +1849,7 @@ int bus_wait_for_jobs_add(BusWaitForJobs *d, const char *path) {
923a60
         return set_put_strdup(d->jobs, path);
923a60
 }
923a60
 
923a60
-int bus_deserialize_and_dump_unit_file_changes(sd_bus_message *m, bool quiet) {
923a60
+int bus_deserialize_and_dump_unit_file_changes(sd_bus_message *m, bool quiet, UnitFileChange **changes, unsigned *n_changes) {
923a60
         const char *type, *path, *source;
923a60
         int r;
923a60
 
923a60
@@ -1864,6 +1864,10 @@ int bus_deserialize_and_dump_unit_file_changes(sd_bus_message *m, bool quiet) {
923a60
                         else
923a60
                                 log_info("Removed symlink %s.", path);
923a60
                 }
923a60
+
923a60
+                r = unit_file_changes_add(changes, n_changes, streq(type, "symlink") ? UNIT_FILE_SYMLINK : UNIT_FILE_UNLINK, path, source);
923a60
+                if (r < 0)
923a60
+                        return r;
923a60
         }
923a60
         if (r < 0)
923a60
                 return bus_log_parse_error(r);
923a60
diff --git a/src/libsystemd/sd-bus/bus-util.h b/src/libsystemd/sd-bus/bus-util.h
923a60
index e8a97cef9e..21db982280 100644
923a60
--- a/src/libsystemd/sd-bus/bus-util.h
923a60
+++ b/src/libsystemd/sd-bus/bus-util.h
923a60
@@ -24,6 +24,7 @@
923a60
 #include "sd-event.h"
923a60
 #include "sd-bus.h"
923a60
 #include "hashmap.h"
923a60
+#include "install.h"
923a60
 #include "time-util.h"
923a60
 #include "util.h"
923a60
 
923a60
@@ -211,4 +212,4 @@ int bus_wait_for_jobs(BusWaitForJobs *d, bool quiet);
923a60
 
923a60
 DEFINE_TRIVIAL_CLEANUP_FUNC(BusWaitForJobs*, bus_wait_for_jobs_free);
923a60
 
923a60
-int bus_deserialize_and_dump_unit_file_changes(sd_bus_message *m, bool quiet);
923a60
+int bus_deserialize_and_dump_unit_file_changes(sd_bus_message *m, bool quiet, UnitFileChange **changes, unsigned *n_changes);
923a60
diff --git a/src/machine/machinectl.c b/src/machine/machinectl.c
923a60
index 9f8c68b184..f1910709d2 100644
923a60
--- a/src/machine/machinectl.c
923a60
+++ b/src/machine/machinectl.c
923a60
@@ -1709,7 +1709,7 @@ static int enable_machine(int argc, char *argv[], void *userdata) {
923a60
                         return bus_log_parse_error(r);
923a60
         }
923a60
 
923a60
-        r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet);
923a60
+        r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, NULL, NULL);
923a60
         if (r < 0)
923a60
                 return r;
923a60
 
923a60
diff --git a/src/shared/install.c b/src/shared/install.c
923a60
index efd489ec08..b62065be5c 100644
923a60
--- a/src/shared/install.c
923a60
+++ b/src/shared/install.c
923a60
@@ -113,51 +113,6 @@ static int get_config_path(UnitFileScope scope, bool runtime, const char *root_d
923a60
         return 0;
923a60
 }
923a60
 
923a60
-static int add_file_change(
923a60
-                UnitFileChange **changes,
923a60
-                unsigned *n_changes,
923a60
-                UnitFileChangeType type,
923a60
-                const char *path,
923a60
-                const char *source) {
923a60
-
923a60
-        UnitFileChange *c;
923a60
-        unsigned i;
923a60
-
923a60
-        assert(path);
923a60
-        assert(!changes == !n_changes);
923a60
-
923a60
-        if (!changes)
923a60
-                return 0;
923a60
-
923a60
-        c = realloc(*changes, (*n_changes + 1) * sizeof(UnitFileChange));
923a60
-        if (!c)
923a60
-                return -ENOMEM;
923a60
-
923a60
-        *changes = c;
923a60
-        i = *n_changes;
923a60
-
923a60
-        c[i].type = type;
923a60
-        c[i].path = strdup(path);
923a60
-        if (!c[i].path)
923a60
-                return -ENOMEM;
923a60
-
923a60
-        path_kill_slashes(c[i].path);
923a60
-
923a60
-        if (source) {
923a60
-                c[i].source = strdup(source);
923a60
-                if (!c[i].source) {
923a60
-                        free(c[i].path);
923a60
-                        return -ENOMEM;
923a60
-                }
923a60
-
923a60
-                path_kill_slashes(c[i].path);
923a60
-        } else
923a60
-                c[i].source = NULL;
923a60
-
923a60
-        *n_changes = i+1;
923a60
-        return 0;
923a60
-}
923a60
-
923a60
 static int mark_symlink_for_removal(
923a60
                 Set **remove_symlinks_to,
923a60
                 const char *p) {
923a60
@@ -310,7 +265,7 @@ static int remove_marked_symlinks_fd(
923a60
 
923a60
                         path_kill_slashes(p);
923a60
                         rmdir_parents(p, config_path);
923a60
-                        add_file_change(changes, n_changes, UNIT_FILE_UNLINK, p, NULL);
923a60
+                        unit_file_changes_add(changes, n_changes, UNIT_FILE_UNLINK, p, NULL);
923a60
 
923a60
                         if (!set_get(remove_symlinks_to, p)) {
923a60
 
923a60
@@ -597,7 +552,7 @@ int unit_file_mask(
923a60
                 }
923a60
 
923a60
                 if (symlink("/dev/null", path) >= 0) {
923a60
-                        add_file_change(changes, n_changes, UNIT_FILE_SYMLINK, path, "/dev/null");
923a60
+                        unit_file_changes_add(changes, n_changes, UNIT_FILE_SYMLINK, path, "/dev/null");
923a60
                         continue;
923a60
                 }
923a60
 
923a60
@@ -608,8 +563,8 @@ int unit_file_mask(
923a60
 
923a60
                         if (force) {
923a60
                                 if (symlink_atomic("/dev/null", path) >= 0) {
923a60
-                                        add_file_change(changes, n_changes, UNIT_FILE_UNLINK, path, NULL);
923a60
-                                        add_file_change(changes, n_changes, UNIT_FILE_SYMLINK, path, "/dev/null");
923a60
+                                        unit_file_changes_add(changes, n_changes, UNIT_FILE_UNLINK, path, NULL);
923a60
+                                        unit_file_changes_add(changes, n_changes, UNIT_FILE_SYMLINK, path, "/dev/null");
923a60
                                         continue;
923a60
                                 }
923a60
                         }
923a60
@@ -665,7 +620,7 @@ int unit_file_unmask(
923a60
                                 q = -errno;
923a60
                         else {
923a60
                                 q = mark_symlink_for_removal(&remove_symlinks_to, path);
923a60
-                                add_file_change(changes, n_changes, UNIT_FILE_UNLINK, path, NULL);
923a60
+                                unit_file_changes_add(changes, n_changes, UNIT_FILE_UNLINK, path, NULL);
923a60
                         }
923a60
                 }
923a60
 
923a60
@@ -747,7 +702,7 @@ int unit_file_link(
923a60
                         return -ENOMEM;
923a60
 
923a60
                 if (symlink(*i, path) >= 0) {
923a60
-                        add_file_change(changes, n_changes, UNIT_FILE_SYMLINK, path, *i);
923a60
+                        unit_file_changes_add(changes, n_changes, UNIT_FILE_SYMLINK, path, *i);
923a60
                         continue;
923a60
                 }
923a60
 
923a60
@@ -766,8 +721,8 @@ int unit_file_link(
923a60
 
923a60
                         if (force) {
923a60
                                 if (symlink_atomic(*i, path) >= 0) {
923a60
-                                        add_file_change(changes, n_changes, UNIT_FILE_UNLINK, path, NULL);
923a60
-                                        add_file_change(changes, n_changes, UNIT_FILE_SYMLINK, path, *i);
923a60
+                                        unit_file_changes_add(changes, n_changes, UNIT_FILE_UNLINK, path, NULL);
923a60
+                                        unit_file_changes_add(changes, n_changes, UNIT_FILE_SYMLINK, path, *i);
923a60
                                         continue;
923a60
                                 }
923a60
                         }
923a60
@@ -794,6 +749,51 @@ void unit_file_list_free(Hashmap *h) {
923a60
         hashmap_free(h);
923a60
 }
923a60
 
923a60
+int unit_file_changes_add(
923a60
+                UnitFileChange **changes,
923a60
+                unsigned *n_changes,
923a60
+                UnitFileChangeType type,
923a60
+                const char *path,
923a60
+                const char *source) {
923a60
+
923a60
+        UnitFileChange *c;
923a60
+        unsigned i;
923a60
+
923a60
+        assert(path);
923a60
+        assert(!changes == !n_changes);
923a60
+
923a60
+        if (!changes)
923a60
+                return 0;
923a60
+
923a60
+        c = realloc(*changes, (*n_changes + 1) * sizeof(UnitFileChange));
923a60
+        if (!c)
923a60
+                return -ENOMEM;
923a60
+
923a60
+        *changes = c;
923a60
+        i = *n_changes;
923a60
+
923a60
+        c[i].type = type;
923a60
+        c[i].path = strdup(path);
923a60
+        if (!c[i].path)
923a60
+                return -ENOMEM;
923a60
+
923a60
+        path_kill_slashes(c[i].path);
923a60
+
923a60
+        if (source) {
923a60
+                c[i].source = strdup(source);
923a60
+                if (!c[i].source) {
923a60
+                        free(c[i].path);
923a60
+                        return -ENOMEM;
923a60
+                }
923a60
+
923a60
+                path_kill_slashes(c[i].path);
923a60
+        } else
923a60
+                c[i].source = NULL;
923a60
+
923a60
+        *n_changes = i+1;
923a60
+        return 0;
923a60
+}
923a60
+
923a60
 void unit_file_changes_free(UnitFileChange *changes, unsigned n_changes) {
923a60
         unsigned i;
923a60
 
923a60
@@ -1199,7 +1199,7 @@ static int create_symlink(
923a60
         mkdir_parents_label(new_path, 0755);
923a60
 
923a60
         if (symlink(old_path, new_path) >= 0) {
923a60
-                add_file_change(changes, n_changes, UNIT_FILE_SYMLINK, new_path, old_path);
923a60
+                unit_file_changes_add(changes, n_changes, UNIT_FILE_SYMLINK, new_path, old_path);
923a60
                 return 0;
923a60
         }
923a60
 
923a60
@@ -1220,8 +1220,8 @@ static int create_symlink(
923a60
         if (r < 0)
923a60
                 return r;
923a60
 
923a60
-        add_file_change(changes, n_changes, UNIT_FILE_UNLINK, new_path, NULL);
923a60
-        add_file_change(changes, n_changes, UNIT_FILE_SYMLINK, new_path, old_path);
923a60
+        unit_file_changes_add(changes, n_changes, UNIT_FILE_UNLINK, new_path, NULL);
923a60
+        unit_file_changes_add(changes, n_changes, UNIT_FILE_SYMLINK, new_path, old_path);
923a60
 
923a60
         return 0;
923a60
 }
923a60
diff --git a/src/shared/install.h b/src/shared/install.h
923a60
index 3ca39397e6..d729e6ed15 100644
923a60
--- a/src/shared/install.h
923a60
+++ b/src/shared/install.h
923a60
@@ -112,6 +112,7 @@ UnitFileState unit_file_get_state(
923a60
 int unit_file_get_list(UnitFileScope scope, const char *root_dir, Hashmap *h);
923a60
 
923a60
 void unit_file_list_free(Hashmap *h);
923a60
+int unit_file_changes_add(UnitFileChange **changes, unsigned *n_changes, UnitFileChangeType type, const char *path, const char *source);
923a60
 void unit_file_changes_free(UnitFileChange *changes, unsigned n_changes);
923a60
 
923a60
 int unit_file_query_preset(UnitFileScope scope, const char *root_dir, const char *name);
923a60
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
923a60
index 089c25f83f..9898694d75 100644
923a60
--- a/src/systemctl/systemctl.c
923a60
+++ b/src/systemctl/systemctl.c
923a60
@@ -138,6 +138,7 @@ static char *arg_host = NULL;
923a60
 static unsigned arg_lines = 10;
923a60
 static OutputMode arg_output = OUTPUT_SHORT;
923a60
 static bool arg_plain = false;
923a60
+static bool arg_now = false;
923a60
 
923a60
 static bool original_stdout_is_tty;
923a60
 
923a60
@@ -1979,7 +1980,7 @@ static int set_default(sd_bus *bus, char **args) {
923a60
                         return r;
923a60
                 }
923a60
 
923a60
-                r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet);
923a60
+                r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, NULL, NULL);
923a60
                 if (r < 0)
923a60
                         return r;
923a60
 
923a60
@@ -5415,7 +5416,7 @@ static int enable_unit(sd_bus *bus, char **args) {
923a60
                                 return bus_log_parse_error(r);
923a60
                 }
923a60
 
923a60
-                r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet);
923a60
+                r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, &changes, &n_changes);
923a60
                 if (r < 0)
923a60
                         return r;
923a60
 
923a60
@@ -5437,6 +5438,18 @@ static int enable_unit(sd_bus *bus, char **args) {
923a60
                             "3) A unit may be started when needed via activation (socket, path, timer,\n"
923a60
                             "   D-Bus, udev, scripted systemctl call, ...).\n");
923a60
 
923a60
+        if (arg_now && n_changes > 0 && STR_IN_SET(args[0], "enable", "disable", "mask")) {
923a60
+                char *new_args[n_changes + 2];
923a60
+                unsigned i;
923a60
+
923a60
+                new_args[0] = streq(args[0], "enable") ? (char *)"start" : (char *)"stop";
923a60
+                for (i = 0; i < n_changes; i++)
923a60
+                        new_args[i + 1] = basename(changes[i].path);
923a60
+                new_args[i + 1] = NULL;
923a60
+
923a60
+                r = start_unit(bus, new_args);
923a60
+        }
923a60
+
923a60
 finish:
923a60
         unit_file_changes_free(changes, n_changes);
923a60
 
923a60
@@ -5516,7 +5529,7 @@ static int add_dependency(sd_bus *bus, char **args) {
923a60
                         return r;
923a60
                 }
923a60
 
923a60
-                r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet);
923a60
+                r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, NULL, NULL);
923a60
                 if (r < 0)
923a60
                         return r;
923a60
 
923a60
@@ -5582,7 +5595,7 @@ static int preset_all(sd_bus *bus, char **args) {
923a60
                         return r;
923a60
                 }
923a60
 
923a60
-                r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet);
923a60
+                r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, NULL, NULL);
923a60
                 if (r < 0)
923a60
                         return r;
923a60
 
923a60
@@ -6058,6 +6071,7 @@ static void systemctl_help(void) {
923a60
                "                      When shutting down or sleeping, ignore inhibitors\n"
923a60
                "     --kill-who=WHO   Who to send signal to\n"
923a60
                "  -s --signal=SIGNAL  Which signal to send\n"
923a60
+               "     --now            Start or stop unit in addition to enabling or disabling it\n"
923a60
                "  -q --quiet          Suppress output\n"
923a60
                "     --no-block       Do not wait until operation finished\n"
923a60
                "     --no-wall        Don't send wall message before halt/power-off/reboot\n"
923a60
@@ -6255,6 +6269,7 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
923a60
                 ARG_STATE,
923a60
                 ARG_JOB_MODE,
923a60
                 ARG_PRESET_MODE,
923a60
+                ARG_NOW,
923a60
         };
923a60
 
923a60
         static const struct option options[] = {
923a60
@@ -6297,6 +6312,7 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
923a60
                 { "state",               required_argument, NULL, ARG_STATE               },
923a60
                 { "recursive",           no_argument,       NULL, 'r'                     },
923a60
                 { "preset-mode",         required_argument, NULL, ARG_PRESET_MODE         },
923a60
+                { "now",                 no_argument,       NULL, ARG_NOW                 },
923a60
                 {}
923a60
         };
923a60
 
923a60
@@ -6573,6 +6589,10 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
923a60
 
923a60
                         break;
923a60
 
923a60
+                case ARG_NOW:
923a60
+                        arg_now = true;
923a60
+                        break;
923a60
+
923a60
                 case '?':
923a60
                         return -EINVAL;
923a60