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