ryantimwilson / rpms / systemd

Forked from rpms/systemd a month ago
Clone
7e7c9f
From c2975479c63cafea5a5f0254f4525136244f3301 Mon Sep 17 00:00:00 2001
7e7c9f
From: Lennart Poettering <lennart@poettering.net>
7e7c9f
Date: Mon, 13 Nov 2017 17:17:53 +0100
7e7c9f
Subject: [PATCH] run: add "-G" as shortcut for
7e7c9f
 "--property=CollectMode=inactive-or-failed"
7e7c9f
7e7c9f
This option is likely to be very useful for systemd-run invocations,
7e7c9f
hence let's add a shortcut for it.
7e7c9f
7e7c9f
With this new concepts it's now very easy to put together systemd-run
7e7c9f
invocations that leave zero artifacts in the system, including when they
7e7c9f
fail.
7e7c9f
7e7c9f
(cherry-picked from commit fe9d0be90ba142bf06d43a831d8be53283415caa)
7e7c9f
7e7c9f
Related: #1817576
7e7c9f
---
7e7c9f
 man/systemd-run.xml | 15 +++++++++++++++
7e7c9f
 src/run/run.c       | 15 ++++++++++++++-
7e7c9f
 2 files changed, 29 insertions(+), 1 deletion(-)
7e7c9f
7e7c9f
diff --git a/man/systemd-run.xml b/man/systemd-run.xml
7e7c9f
index f46fc3abf4..97cf2879eb 100644
7e7c9f
--- a/man/systemd-run.xml
7e7c9f
+++ b/man/systemd-run.xml
7e7c9f
@@ -294,6 +294,21 @@
7e7c9f
         <command>set-property</command> command.</para> </listitem>
7e7c9f
       </varlistentry>
7e7c9f
 
7e7c9f
+      <varlistentry>
7e7c9f
+        <term><option>-G</option></term>
7e7c9f
+        <term><option>--collect</option></term>
7e7c9f
+
7e7c9f
+        <listitem><para>Unload the transient unit after it completed, even if it failed. Normally, without this option,
7e7c9f
+        all units that ran and failed are kept in memory until the user explicitly resets their failure state with
7e7c9f
+        <command>systemctl reset-failed</command> or an equivalent command. On the other hand, units that ran
7e7c9f
+        successfully are unloaded immediately. If this option is turned on the "garbage collection" of units is more
7e7c9f
+        aggressive, and unloads units regardless if they exited successfully or failed. This option is a shortcut for
7e7c9f
+        <command>--property=CollectMode=inactive-or-failed</command>, see the explanation for
7e7c9f
+        <varname>CollectMode=</varname> in
7e7c9f
+        <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry> for further
7e7c9f
+        information.</para></listitem>
7e7c9f
+      </varlistentry>
7e7c9f
+
7e7c9f
       <xi:include href="user-system-options.xml" xpointer="system" />
7e7c9f
       <xi:include href="user-system-options.xml" xpointer="host" />
7e7c9f
       <xi:include href="user-system-options.xml" xpointer="machine" />
7e7c9f
diff --git a/src/run/run.c b/src/run/run.c
7e7c9f
index bbb542b65b..df97641f87 100644
7e7c9f
--- a/src/run/run.c
7e7c9f
+++ b/src/run/run.c
7e7c9f
@@ -60,6 +60,7 @@ static usec_t arg_on_unit_inactive = 0;
7e7c9f
 static char *arg_on_calendar = NULL;
7e7c9f
 static char **arg_timer_property = NULL;
7e7c9f
 static bool arg_quiet = false;
7e7c9f
+static bool arg_aggressive_gc = false;
7e7c9f
 
7e7c9f
 static void help(void) {
7e7c9f
         printf("%s [OPTIONS...] {COMMAND} [ARGS...]\n\n"
7e7c9f
@@ -84,6 +85,7 @@ static void help(void) {
7e7c9f
                "     --setenv=NAME=VALUE          Set environment\n"
7e7c9f
                "  -t --pty                        Run service on pseudo tty\n"
7e7c9f
                "  -q --quiet                      Suppress information messages during runtime\n\n"
7e7c9f
+               "  -G --collect                    Unload unit after it ran, even when failed\n\n"
7e7c9f
                "Timer options:\n\n"
7e7c9f
                "     --on-active=SECONDS          Run after SECONDS delay\n"
7e7c9f
                "     --on-boot=SECONDS            Run SECONDS after machine was booted up\n"
7e7c9f
@@ -153,6 +155,7 @@ static int parse_argv(int argc, char *argv[]) {
7e7c9f
                 { "on-unit-inactive",  required_argument, NULL, ARG_ON_UNIT_INACTIVE },
7e7c9f
                 { "on-calendar",       required_argument, NULL, ARG_ON_CALENDAR      },
7e7c9f
                 { "timer-property",    required_argument, NULL, ARG_TIMER_PROPERTY   },
7e7c9f
+                { "collect",           no_argument,       NULL, 'G'                  },
7e7c9f
                 {},
7e7c9f
         };
7e7c9f
 
7e7c9f
@@ -162,7 +165,7 @@ static int parse_argv(int argc, char *argv[]) {
7e7c9f
         assert(argc >= 0);
7e7c9f
         assert(argv);
7e7c9f
 
7e7c9f
-        while ((c = getopt_long(argc, argv, "+hrH:M:p:tq", options, NULL)) >= 0)
7e7c9f
+        while ((c = getopt_long(argc, argv, "+hrH:M:p:tqG", options, NULL)) >= 0)
7e7c9f
 
7e7c9f
                 switch (c) {
7e7c9f
 
7e7c9f
@@ -329,6 +332,10 @@ static int parse_argv(int argc, char *argv[]) {
7e7c9f
 
7e7c9f
                         break;
7e7c9f
 
7e7c9f
+                case 'G':
7e7c9f
+                        arg_aggressive_gc = true;
7e7c9f
+                        break;
7e7c9f
+
7e7c9f
                 case '?':
7e7c9f
                         return -EINVAL;
7e7c9f
 
7e7c9f
@@ -382,6 +389,12 @@ static int transient_unit_set_properties(sd_bus_message *m, char **properties) {
7e7c9f
         if (r < 0)
7e7c9f
                 return r;
7e7c9f
 
7e7c9f
+        if (arg_aggressive_gc) {
7e7c9f
+                r = sd_bus_message_append(m, "(sv)", "CollectMode", "s", "inactive-or-failed");
7e7c9f
+                if (r < 0)
7e7c9f
+                        return r;
7e7c9f
+        }
7e7c9f
+
7e7c9f
         STRV_FOREACH(i, properties) {
7e7c9f
                 r = sd_bus_message_open_container(m, 'r', "sv");
7e7c9f
                 if (r < 0)