Merge pull request #13904 from keur/job_mode_triggering

Job mode triggering
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2019-11-07 08:36:26 +01:00 committed by GitHub
commit 754499fab2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 48 additions and 4 deletions

2
TODO
View File

@ -491,8 +491,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

View File

@ -1601,8 +1601,9 @@ Jan 12 10:46:45 example.com bluetoothd[8900]: gatt-time-server: Input/output err
<literal>replace-irreversibly</literal>,
<literal>isolate</literal>,
<literal>ignore-dependencies</literal>,
<literal>ignore-requirements</literal> or
<literal>flush</literal>. Defaults to
<literal>ignore-requirements</literal>,
<literal>flush</literal>, or
<literal>triggering</literal>. Defaults to
<literal>replace</literal>, except when the
<command>isolate</command> command is used which implies the
<literal>isolate</literal> job mode.</para>
@ -1647,6 +1648,13 @@ Jan 12 10:46:45 example.com bluetoothd[8900]: gatt-time-server: Input/output err
dependencies will still be honored.</para>
</listitem>
<para><literal>triggering</literal> may only be used with
<command>systemctl stop</command>. In this mode, the specified
unit and any active units that trigger it are stopped. See the
discussion of
<varname>Triggers=</varname> in <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>
for more information about triggering units.</para>
</varlistentry>
<varlistentry>

View 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);

View 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
};

View File

@ -1738,6 +1738,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);
@ -1758,6 +1761,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;

View File

@ -1141,6 +1141,32 @@ 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);
assert(u);
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;

View 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);