]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core: Add triggering job mode
authorKevin Kuehler <keur@xcf.berkeley.edu>
Fri, 1 Nov 2019 07:54:03 +0000 (00:54 -0700)
committerKevin Kuehler <keur@xcf.berkeley.edu>
Tue, 5 Nov 2019 19:17:38 +0000 (11:17 -0800)
When used with systemctl stop, follows TRIGGERED_BY dependencies and
adds them to the same transaction.

Fixes: #3043
TODO
src/core/job.c
src/core/job.h
src/core/manager.c
src/core/transaction.c
src/core/transaction.h

diff --git a/TODO b/TODO
index 03d38da9a0eec2d346f21b691449e8f452489b86..b86784bb28795ddbcf6951c3a8e7b144f3aad386 100644 (file)
--- a/TODO
+++ b/TODO
@@ -483,8 +483,6 @@ Features:
 
 * cache sd_event_now() result from before the first iteration...
 
-* add systemctl stop --job-mode=triggering that follows TRIGGERED_BY deps and adds them to the same transaction
-
 * PID1: find a way how we can reload unit file configuration for
   specific units only, without reloading the whole of systemd
 
index 9537366eb9aa0275651319cb985bda0f3abfe722..86185296dddb0b8a72b23b56780d79e914d0e75d 100644 (file)
@@ -1610,6 +1610,7 @@ static const char* const job_mode_table[_JOB_MODE_MAX] = {
         [JOB_FLUSH] = "flush",
         [JOB_IGNORE_DEPENDENCIES] = "ignore-dependencies",
         [JOB_IGNORE_REQUIREMENTS] = "ignore-requirements",
+        [JOB_TRIGGERING] = "triggering",
 };
 
 DEFINE_STRING_TABLE_LOOKUP(job_mode, JobMode);
index 0781328a562b221f27887606b114e1860ce1e0dc..03ad640618e7bf047f9139a2d9df59fa4134dd76 100644 (file)
@@ -75,6 +75,7 @@ enum JobMode {
         JOB_FLUSH,               /* Flush out all other queued jobs when queueing this one */
         JOB_IGNORE_DEPENDENCIES, /* Ignore both requirement and ordering dependencies */
         JOB_IGNORE_REQUIREMENTS, /* Ignore requirement dependencies */
+        JOB_TRIGGERING,          /* Adds TRIGGERED_BY dependencies to the same transaction */
         _JOB_MODE_MAX,
         _JOB_MODE_INVALID = -1
 };
index c9032fced8ea6720c318c30e7932123cde5ab88f..8f523e11518b4056edf603fa0488f51a2d96d46c 100644 (file)
@@ -1740,6 +1740,9 @@ int manager_add_job(
         if (mode == JOB_ISOLATE && !unit->allow_isolate)
                 return sd_bus_error_setf(error, BUS_ERROR_NO_ISOLATION, "Operation refused, unit may not be isolated.");
 
+        if (mode == JOB_TRIGGERING && type != JOB_STOP)
+                return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "--job-mode=triggering is only valid for stop.");
+
         log_unit_debug(unit, "Trying to enqueue job %s/%s/%s", unit->id, job_type_to_string(type), job_mode_to_string(mode));
 
         type = job_type_collapse(type, unit);
@@ -1760,6 +1763,12 @@ int manager_add_job(
                         goto tr_abort;
         }
 
+        if (mode == JOB_TRIGGERING) {
+                r = transaction_add_triggering_jobs(tr, unit);
+                if (r < 0)
+                        goto tr_abort;
+        }
+
         r = transaction_activate(tr, m, mode, affected_jobs, error);
         if (r < 0)
                 goto tr_abort;
index a0825c595cabb65bf505381d1c69a51845381246..34647281a453a4003bbc4241aa8ce6f582430a15 100644 (file)
@@ -1141,6 +1141,31 @@ int transaction_add_isolate_jobs(Transaction *tr, Manager *m) {
         return 0;
 }
 
+int transaction_add_triggering_jobs(Transaction *tr, Unit *u) {
+        Iterator i;
+        void *v;
+        Unit *trigger;
+        int r;
+
+        assert(tr);
+
+        HASHMAP_FOREACH_KEY(v, trigger, u->dependencies[UNIT_TRIGGERED_BY], i) {
+                /* No need to stop inactive jobs */
+                if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(trigger)) && !trigger->job)
+                        continue;
+
+                /* Is there already something listed for this? */
+                if (hashmap_get(tr->jobs, trigger))
+                        continue;
+
+                r = transaction_add_job_and_dependencies(tr, JOB_STOP, trigger, tr->anchor_job, true, false, false, false, NULL);
+                if (r < 0)
+                        log_unit_warning_errno(u, r, "Cannot add triggered by job, ignoring: %m");
+        }
+
+        return 0;
+}
+
 Transaction *transaction_new(bool irreversible) {
         Transaction *tr;
 
index 4b5620f5c86763501c3485bef312d781ae64051e..ba42f58e073736dbb97cd2e50bb805528326ccce 100644 (file)
@@ -31,4 +31,5 @@ int transaction_add_job_and_dependencies(
                 sd_bus_error *e);
 int transaction_activate(Transaction *tr, Manager *m, JobMode mode, Set *affected, sd_bus_error *e);
 int transaction_add_isolate_jobs(Transaction *tr, Manager *m);
+int transaction_add_triggering_jobs(Transaction *tr, Unit *u);
 void transaction_abort(Transaction *tr);