[systemd-devel] [PATCH] systemctl: introduce -e and -d for start and stop

jsynacek at redhat.com jsynacek at redhat.com
Wed May 13 06:21:13 PDT 2015


From: Jan Synacek <jsynacek at redhat.com>

---
 man/systemctl.xml         | 26 ++++++++++++++++++++++++++
 src/systemctl/systemctl.c | 40 ++++++++++++++++++++++++++++++++++------
 2 files changed, 60 insertions(+), 6 deletions(-)

diff --git a/man/systemctl.xml b/man/systemctl.xml
index 4dbdfe1..51c19e1 100644
--- a/man/systemctl.xml
+++ b/man/systemctl.xml
@@ -432,6 +432,26 @@
       </varlistentry>
 
       <varlistentry>
+        <term><option>-e</option></term>
+        <term><option>--enable</option></term>
+
+        <listitem>
+          <para>When used with <command>start</command>, additionally
+          enable unit after it has been successfully started.</para>
+        </listitem>
+      </varlistentry>
+
+      <varlistentry>
+        <term><option>-d</option></term>
+        <term><option>--disable</option></term>
+
+        <listitem>
+          <para>When used with <command>stop</command>, additionally
+          disable unit after it has been successfully stopped.</para>
+        </listitem>
+      </varlistentry>
+
+      <varlistentry>
         <term><option>-f</option></term>
         <term><option>--force</option></term>
 
@@ -633,6 +653,9 @@ kobject-uevent 1 systemd-udevd-kernel.socket systemd-udevd.service
             instance name until the instance has been started. Therefore,
             using glob patterns with <command>start</command>
             has limited usefulness.</para>
+
+            <para>When used with <option>-e</option>, additional <command>enable</command>
+            operation is performed.</para>
           </listitem>
         </varlistentry>
         <varlistentry>
@@ -641,6 +664,9 @@ kobject-uevent 1 systemd-udevd-kernel.socket systemd-udevd.service
           <listitem>
             <para>Stop (deactivate) one or more units specified on the
             command line.</para>
+
+            <para>When used with <option>-d</option>, additional <command>disable</command>
+            operation is performed.</para>
           </listitem>
         </varlistentry>
         <varlistentry>
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
index 1f18f9c..5655c57 100644
--- a/src/systemctl/systemctl.c
+++ b/src/systemctl/systemctl.c
@@ -136,12 +136,15 @@ static unsigned arg_lines = 10;
 static OutputMode arg_output = OUTPUT_SHORT;
 static bool arg_plain = false;
 static bool arg_firmware_setup = false;
+static bool arg_startenable = false;
+static bool arg_stopdisable = false;
 
 static bool original_stdout_is_tty;
 
 static int daemon_reload(sd_bus *bus, char **args);
 static int halt_now(enum action a);
 static int check_one_unit(sd_bus *bus, const char *name, const char *good_states, bool quiet);
+static int enable_unit(sd_bus *bus, char **args);
 
 static char** strv_skip_first(char **strv) {
         if (strv_length(strv) > 0)
@@ -2677,7 +2680,7 @@ static int start_unit(sd_bus *bus, char **args) {
         const char *method, *mode, *one_name, *suffix = NULL;
         _cleanup_strv_free_ char **names = NULL;
         char **name;
-        int r = 0;
+        int r = 0, start_rc = 0;
 
         assert(bus);
 
@@ -2722,11 +2725,26 @@ static int start_unit(sd_bus *bus, char **args) {
 
         STRV_FOREACH(name, names) {
                 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
-                int q;
 
-                q = start_unit_one(bus, method, *name, mode, &error, w);
-                if (r >= 0 && q < 0)
-                        r = translate_bus_error_to_exit_status(q, &error);
+                start_rc = start_unit_one(bus, method, *name, mode, &error, w);
+                if (r >= 0 && start_rc < 0)
+                        r = translate_bus_error_to_exit_status(start_rc, &error);
+        }
+
+        if (((streq(args[0], "start") && arg_startenable)
+             || (streq(args[0], "stop") && arg_stopdisable))
+            && start_rc >= 0) {
+                _cleanup_strv_free_ char **newargs;
+
+                newargs = strv_copy(args);
+                if (!newargs)
+                        return -ENOMEM;
+                free(newargs[0]);
+                newargs[0] = streq(args[0], "start") ? strdup("enable") : strdup("disable");
+                if (!newargs[0])
+                        return -ENOMEM;
+
+                r = enable_unit(bus, newargs);
         }
 
         if (!arg_no_block) {
@@ -6257,6 +6275,8 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
                 { "recursive",           no_argument,       NULL, 'r'                     },
                 { "preset-mode",         required_argument, NULL, ARG_PRESET_MODE         },
                 { "firmware-setup",      no_argument,       NULL, ARG_FIRMWARE_SETUP      },
+                { "enable",              no_argument,       NULL, 'e'                     },
+                { "disable",             no_argument,       NULL, 'd'                     },
                 {}
         };
 
@@ -6265,7 +6285,7 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
         assert(argc >= 0);
         assert(argv);
 
-        while ((c = getopt_long(argc, argv, "ht:p:alqfs:H:M:n:o:ir", options, NULL)) >= 0)
+        while ((c = getopt_long(argc, argv, "ht:p:alqfs:H:M:n:o:ired", options, NULL)) >= 0)
 
                 switch (c) {
 
@@ -6537,6 +6557,14 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
 
                         break;
 
+                case 'e':
+                        arg_startenable = true;
+                        break;
+
+                case 'd':
+                        arg_stopdisable = true;
+                        break;
+
                 case '?':
                         return -EINVAL;
 
-- 
2.1.0



More information about the systemd-devel mailing list