]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
a7334b09 | 2 | |
1ffba6fe | 3 | #include <errno.h> |
60918275 | 4 | |
718db961 LP |
5 | #include "sd-id128.h" |
6 | #include "sd-messages.h" | |
4f5dd394 | 7 | |
b5efdb8a | 8 | #include "alloc-util.h" |
f485606b | 9 | #include "async.h" |
4f5dd394 | 10 | #include "dbus-job.h" |
8f8f05a9 | 11 | #include "dbus.h" |
4f5dd394 | 12 | #include "escape.h" |
8948b341 | 13 | #include "fileio.h" |
6bedfcbb | 14 | #include "job.h" |
4f5dd394 LP |
15 | #include "log.h" |
16 | #include "macro.h" | |
6bedfcbb | 17 | #include "parse-util.h" |
d68c645b | 18 | #include "serialize.h" |
4f5dd394 LP |
19 | #include "set.h" |
20 | #include "special.h" | |
d054f0a4 | 21 | #include "stdio-util.h" |
8b43440b | 22 | #include "string-table.h" |
07630cea | 23 | #include "string-util.h" |
4f5dd394 | 24 | #include "strv.h" |
288a74cc | 25 | #include "terminal-util.h" |
4f5dd394 LP |
26 | #include "unit.h" |
27 | #include "virt.h" | |
97e6a119 | 28 | |
39a18c60 | 29 | Job* job_new_raw(Unit *unit) { |
60918275 LP |
30 | Job *j; |
31 | ||
39a18c60 MS |
32 | /* used for deserialization */ |
33 | ||
87f0e418 | 34 | assert(unit); |
60918275 | 35 | |
08ac00f2 | 36 | j = new(Job, 1); |
39a18c60 | 37 | if (!j) |
60918275 LP |
38 | return NULL; |
39 | ||
08ac00f2 LP |
40 | *j = (Job) { |
41 | .manager = unit->manager, | |
42 | .unit = unit, | |
43 | .type = _JOB_TYPE_INVALID, | |
44 | }; | |
faf919f1 | 45 | |
39a18c60 MS |
46 | return j; |
47 | } | |
48 | ||
49 | Job* job_new(Unit *unit, JobType type) { | |
50 | Job *j; | |
51 | ||
52 | assert(type < _JOB_TYPE_MAX); | |
53 | ||
54 | j = job_new_raw(unit); | |
55 | if (!j) | |
56 | return NULL; | |
57 | ||
58 | j->id = j->manager->current_job_id++; | |
59 | j->type = type; | |
60 | ||
e5b5ae50 | 61 | /* We don't link it here, that's what job_dependency() is for */ |
60918275 LP |
62 | |
63 | return j; | |
64 | } | |
65 | ||
a7a7163d | 66 | void job_unlink(Job *j) { |
97e7d748 MS |
67 | assert(j); |
68 | assert(!j->installed); | |
02a3bcc6 MS |
69 | assert(!j->transaction_prev); |
70 | assert(!j->transaction_next); | |
71 | assert(!j->subject_list); | |
72 | assert(!j->object_list); | |
60918275 | 73 | |
a7a7163d | 74 | if (j->in_run_queue) { |
71fda00f | 75 | LIST_REMOVE(run_queue, j->manager->run_queue, j); |
a7a7163d DT |
76 | j->in_run_queue = false; |
77 | } | |
c1e1601e | 78 | |
a7a7163d | 79 | if (j->in_dbus_queue) { |
71fda00f | 80 | LIST_REMOVE(dbus_queue, j->manager->dbus_job_queue, j); |
a7a7163d DT |
81 | j->in_dbus_queue = false; |
82 | } | |
c1e1601e | 83 | |
a7a7163d | 84 | if (j->in_gc_queue) { |
c5a97ed1 | 85 | LIST_REMOVE(gc_queue, j->manager->gc_job_queue, j); |
a7a7163d DT |
86 | j->in_gc_queue = false; |
87 | } | |
c5a97ed1 | 88 | |
a7a7163d DT |
89 | j->timer_event_source = sd_event_source_unref(j->timer_event_source); |
90 | } | |
91 | ||
728ba51e | 92 | Job* job_free(Job *j) { |
a7a7163d DT |
93 | assert(j); |
94 | assert(!j->installed); | |
95 | assert(!j->transaction_prev); | |
96 | assert(!j->transaction_next); | |
97 | assert(!j->subject_list); | |
98 | assert(!j->object_list); | |
99 | ||
100 | job_unlink(j); | |
faf919f1 | 101 | |
1a465207 | 102 | sd_bus_track_unref(j->bus_track); |
b39a2770 | 103 | strv_free(j->deserialized_clients); |
faf919f1 | 104 | |
728ba51e | 105 | return mfree(j); |
60918275 | 106 | } |
a66d02c3 | 107 | |
9c3349e2 LP |
108 | static void job_set_state(Job *j, JobState state) { |
109 | assert(j); | |
110 | assert(state >= 0); | |
111 | assert(state < _JOB_STATE_MAX); | |
112 | ||
113 | if (j->state == state) | |
114 | return; | |
115 | ||
116 | j->state = state; | |
117 | ||
118 | if (!j->installed) | |
119 | return; | |
120 | ||
121 | if (j->state == JOB_RUNNING) | |
122 | j->unit->manager->n_running_jobs++; | |
123 | else { | |
124 | assert(j->state == JOB_WAITING); | |
125 | assert(j->unit->manager->n_running_jobs > 0); | |
126 | ||
127 | j->unit->manager->n_running_jobs--; | |
128 | ||
129 | if (j->unit->manager->n_running_jobs <= 0) | |
130 | j->unit->manager->jobs_in_progress_event_source = sd_event_source_unref(j->unit->manager->jobs_in_progress_event_source); | |
131 | } | |
132 | } | |
133 | ||
05d576f1 | 134 | void job_uninstall(Job *j) { |
e0209d83 MS |
135 | Job **pj; |
136 | ||
05d576f1 | 137 | assert(j->installed); |
e0209d83 | 138 | |
9c3349e2 LP |
139 | job_set_state(j, JOB_WAITING); |
140 | ||
e0209d83 MS |
141 | pj = (j->type == JOB_NOP) ? &j->unit->nop_job : &j->unit->job; |
142 | assert(*pj == j); | |
143 | ||
05d576f1 MS |
144 | /* Detach from next 'bigger' objects */ |
145 | ||
39a18c60 | 146 | /* daemon-reload should be transparent to job observers */ |
2c289ea8 | 147 | if (!MANAGER_IS_RELOADING(j->manager)) |
39a18c60 | 148 | bus_job_send_removed_signal(j); |
05d576f1 | 149 | |
e0209d83 MS |
150 | *pj = NULL; |
151 | ||
d6a093d0 | 152 | unit_add_to_gc_queue(j->unit); |
05d576f1 MS |
153 | |
154 | hashmap_remove(j->manager->jobs, UINT32_TO_PTR(j->id)); | |
155 | j->installed = false; | |
156 | } | |
157 | ||
656bbffc MS |
158 | static bool job_type_allows_late_merge(JobType t) { |
159 | /* Tells whether it is OK to merge a job of type 't' with an already | |
160 | * running job. | |
161 | * Reloads cannot be merged this way. Think of the sequence: | |
162 | * 1. Reload of a daemon is in progress; the daemon has already loaded | |
163 | * its config file, but hasn't completed the reload operation yet. | |
164 | * 2. Edit foo's config file. | |
165 | * 3. Trigger another reload to have the daemon use the new config. | |
166 | * Should the second reload job be merged into the first one, the daemon | |
167 | * would not know about the new config. | |
168 | * JOB_RESTART jobs on the other hand can be merged, because they get | |
169 | * patched into JOB_START after stopping the unit. So if we see a | |
170 | * JOB_RESTART running, it means the unit hasn't stopped yet and at | |
171 | * this time the merge is still allowed. */ | |
e0209d83 | 172 | return t != JOB_RELOAD; |
656bbffc MS |
173 | } |
174 | ||
175 | static void job_merge_into_installed(Job *j, Job *other) { | |
176 | assert(j->installed); | |
177 | assert(j->unit == other->unit); | |
178 | ||
e0209d83 | 179 | if (j->type != JOB_NOP) |
53a2383b | 180 | assert_se(job_type_merge_and_collapse(&j->type, other->type, j->unit) == 0); |
e0209d83 MS |
181 | else |
182 | assert(other->type == JOB_NOP); | |
656bbffc | 183 | |
23ade460 | 184 | j->irreversible = j->irreversible || other->irreversible; |
e45460d6 | 185 | j->ignore_order = j->ignore_order || other->ignore_order; |
656bbffc MS |
186 | } |
187 | ||
188 | Job* job_install(Job *j) { | |
e0209d83 MS |
189 | Job **pj; |
190 | Job *uj; | |
05d576f1 | 191 | |
656bbffc | 192 | assert(!j->installed); |
e0209d83 | 193 | assert(j->type < _JOB_TYPE_MAX_IN_TRANSACTION); |
9c3349e2 | 194 | assert(j->state == JOB_WAITING); |
e0209d83 MS |
195 | |
196 | pj = (j->type == JOB_NOP) ? &j->unit->nop_job : &j->unit->job; | |
197 | uj = *pj; | |
656bbffc | 198 | |
05d576f1 | 199 | if (uj) { |
61da906a | 200 | if (job_type_is_conflicting(uj->type, j->type)) |
833f92ad | 201 | job_finish_and_invalidate(uj, JOB_CANCELED, false, false); |
656bbffc MS |
202 | else { |
203 | /* not conflicting, i.e. mergeable */ | |
204 | ||
61da906a | 205 | if (uj->state == JOB_WAITING || |
656bbffc MS |
206 | (job_type_allows_late_merge(j->type) && job_type_is_superset(uj->type, j->type))) { |
207 | job_merge_into_installed(uj, j); | |
f2341e0a | 208 | log_unit_debug(uj->unit, |
66870f90 ZJS |
209 | "Merged into installed job %s/%s as %u", |
210 | uj->unit->id, job_type_to_string(uj->type), (unsigned) uj->id); | |
656bbffc MS |
211 | return uj; |
212 | } else { | |
213 | /* already running and not safe to merge into */ | |
214 | /* Patch uj to become a merged job and re-run it. */ | |
215 | /* XXX It should be safer to queue j to run after uj finishes, but it is | |
216 | * not currently possible to have more than one installed job per unit. */ | |
217 | job_merge_into_installed(uj, j); | |
f2341e0a | 218 | log_unit_debug(uj->unit, |
66870f90 ZJS |
219 | "Merged into running job, re-running: %s/%s as %u", |
220 | uj->unit->id, job_type_to_string(uj->type), (unsigned) uj->id); | |
9c3349e2 LP |
221 | |
222 | job_set_state(uj, JOB_WAITING); | |
656bbffc MS |
223 | return uj; |
224 | } | |
225 | } | |
05d576f1 MS |
226 | } |
227 | ||
656bbffc | 228 | /* Install the job */ |
e0209d83 | 229 | *pj = j; |
05d576f1 | 230 | j->installed = true; |
9c3349e2 | 231 | |
313cefa1 | 232 | j->manager->n_installed_jobs++; |
f2341e0a | 233 | log_unit_debug(j->unit, |
66870f90 ZJS |
234 | "Installed new job %s/%s as %u", |
235 | j->unit->id, job_type_to_string(j->type), (unsigned) j->id); | |
c5a97ed1 LP |
236 | |
237 | job_add_to_gc_queue(j); | |
238 | ||
e6d05912 LP |
239 | job_add_to_dbus_queue(j); /* announce this job to clients */ |
240 | unit_add_to_dbus_queue(j->unit); /* The Job property of the unit has changed now */ | |
241 | ||
656bbffc | 242 | return j; |
05d576f1 MS |
243 | } |
244 | ||
e0209d83 MS |
245 | int job_install_deserialized(Job *j) { |
246 | Job **pj; | |
247 | ||
39a18c60 MS |
248 | assert(!j->installed); |
249 | ||
baaa35ad ZJS |
250 | if (j->type < 0 || j->type >= _JOB_TYPE_MAX_IN_TRANSACTION) |
251 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), | |
252 | "Invalid job type %s in deserialization.", | |
253 | strna(job_type_to_string(j->type))); | |
e0209d83 MS |
254 | |
255 | pj = (j->type == JOB_NOP) ? &j->unit->nop_job : &j->unit->job; | |
e0209d83 | 256 | if (*pj) { |
f2341e0a | 257 | log_unit_debug(j->unit, "Unit already has a job installed. Not installing deserialized job."); |
e0209d83 | 258 | return -EEXIST; |
39a18c60 | 259 | } |
9c3349e2 | 260 | |
e0209d83 | 261 | *pj = j; |
39a18c60 | 262 | j->installed = true; |
a7a7163d | 263 | j->reloaded = true; |
9c3349e2 LP |
264 | |
265 | if (j->state == JOB_RUNNING) | |
266 | j->unit->manager->n_running_jobs++; | |
267 | ||
f2341e0a | 268 | log_unit_debug(j->unit, |
66870f90 ZJS |
269 | "Reinstalled deserialized job %s/%s as %u", |
270 | j->unit->id, job_type_to_string(j->type), (unsigned) j->id); | |
e0209d83 | 271 | return 0; |
39a18c60 MS |
272 | } |
273 | ||
1da4264f | 274 | JobDependency* job_dependency_new(Job *subject, Job *object, bool matters, bool conflicts) { |
e5b5ae50 LP |
275 | JobDependency *l; |
276 | ||
277 | assert(object); | |
278 | ||
279 | /* Adds a new job link, which encodes that the 'subject' job | |
280 | * needs the 'object' job in some way. If 'subject' is NULL | |
281 | * this means the 'anchor' job (i.e. the one the user | |
35b8ca3a | 282 | * explicitly asked for) is the requester. */ |
e5b5ae50 | 283 | |
0a23a627 LP |
284 | l = new0(JobDependency, 1); |
285 | if (!l) | |
e5b5ae50 LP |
286 | return NULL; |
287 | ||
288 | l->subject = subject; | |
289 | l->object = object; | |
290 | l->matters = matters; | |
69dd2852 | 291 | l->conflicts = conflicts; |
e5b5ae50 | 292 | |
44d8db9e | 293 | if (subject) |
71fda00f | 294 | LIST_PREPEND(subject, subject->subject_list, l); |
e5b5ae50 | 295 | |
71fda00f | 296 | LIST_PREPEND(object, object->object_list, l); |
e5b5ae50 LP |
297 | |
298 | return l; | |
299 | } | |
300 | ||
1da4264f | 301 | void job_dependency_free(JobDependency *l) { |
e5b5ae50 LP |
302 | assert(l); |
303 | ||
44d8db9e | 304 | if (l->subject) |
71fda00f | 305 | LIST_REMOVE(subject, l->subject->subject_list, l); |
e5b5ae50 | 306 | |
71fda00f | 307 | LIST_REMOVE(object, l->object->object_list, l); |
e5b5ae50 LP |
308 | |
309 | free(l); | |
310 | } | |
311 | ||
f2a3de01 | 312 | void job_dump(Job *j, FILE *f, const char *prefix) { |
a66d02c3 LP |
313 | assert(j); |
314 | assert(f); | |
315 | ||
ad5d4b17 | 316 | prefix = strempty(prefix); |
9eb63b3c | 317 | |
ceed3570 | 318 | fprintf(f, |
40d50879 LP |
319 | "%s-> Job %u:\n" |
320 | "%s\tAction: %s -> %s\n" | |
5cb5a6ff | 321 | "%s\tState: %s\n" |
f698d99c ZJS |
322 | "%s\tIrreversible: %s\n" |
323 | "%s\tMay GC: %s\n", | |
ceed3570 | 324 | prefix, j->id, |
ac155bb8 | 325 | prefix, j->unit->id, job_type_to_string(j->type), |
94f04347 | 326 | prefix, job_state_to_string(j->state), |
f698d99c ZJS |
327 | prefix, yes_no(j->irreversible), |
328 | prefix, yes_no(job_may_gc(j))); | |
a66d02c3 | 329 | } |
e5b5ae50 | 330 | |
348e27fe MS |
331 | /* |
332 | * Merging is commutative, so imagine the matrix as symmetric. We store only | |
333 | * its lower triangle to avoid duplication. We don't store the main diagonal, | |
334 | * because A merged with A is simply A. | |
335 | * | |
e0209d83 MS |
336 | * If the resulting type is collapsed immediately afterwards (to get rid of |
337 | * the JOB_RELOAD_OR_START, which lies outside the lookup function's domain), | |
338 | * the following properties hold: | |
339 | * | |
48b4eab4 | 340 | * Merging is associative! A merged with B, and then merged with C is the same |
103635db | 341 | * as A merged with the result of B merged with C. |
348e27fe MS |
342 | * |
343 | * Mergeability is transitive! If A can be merged with B and B with C then | |
344 | * A also with C. | |
345 | * | |
346 | * Also, if A merged with B cannot be merged with C, then either A or B cannot | |
347 | * be merged with C either. | |
348 | */ | |
349 | static const JobType job_merging_table[] = { | |
e0209d83 MS |
350 | /* What \ With * JOB_START JOB_VERIFY_ACTIVE JOB_STOP JOB_RELOAD */ |
351 | /*********************************************************************************/ | |
348e27fe MS |
352 | /*JOB_START */ |
353 | /*JOB_VERIFY_ACTIVE */ JOB_START, | |
354 | /*JOB_STOP */ -1, -1, | |
355 | /*JOB_RELOAD */ JOB_RELOAD_OR_START, JOB_RELOAD, -1, | |
e0209d83 | 356 | /*JOB_RESTART */ JOB_RESTART, JOB_RESTART, -1, JOB_RESTART, |
348e27fe MS |
357 | }; |
358 | ||
359 | JobType job_type_lookup_merge(JobType a, JobType b) { | |
e0209d83 MS |
360 | assert_cc(ELEMENTSOF(job_merging_table) == _JOB_TYPE_MAX_MERGING * (_JOB_TYPE_MAX_MERGING - 1) / 2); |
361 | assert(a >= 0 && a < _JOB_TYPE_MAX_MERGING); | |
362 | assert(b >= 0 && b < _JOB_TYPE_MAX_MERGING); | |
1ffba6fe LP |
363 | |
364 | if (a == b) | |
348e27fe | 365 | return a; |
1ffba6fe | 366 | |
348e27fe MS |
367 | if (a < b) { |
368 | JobType tmp = a; | |
369 | a = b; | |
370 | b = tmp; | |
1ffba6fe | 371 | } |
e094e853 | 372 | |
348e27fe | 373 | return job_merging_table[(a - 1) * a / 2 + b]; |
e094e853 | 374 | } |
cd2dbd7d | 375 | |
593fbdd2 LP |
376 | bool job_type_is_redundant(JobType a, UnitActiveState b) { |
377 | switch (a) { | |
378 | ||
379 | case JOB_START: | |
3742095b | 380 | return IN_SET(b, UNIT_ACTIVE, UNIT_RELOADING); |
593fbdd2 LP |
381 | |
382 | case JOB_STOP: | |
3742095b | 383 | return IN_SET(b, UNIT_INACTIVE, UNIT_FAILED); |
593fbdd2 LP |
384 | |
385 | case JOB_VERIFY_ACTIVE: | |
3742095b | 386 | return IN_SET(b, UNIT_ACTIVE, UNIT_RELOADING); |
593fbdd2 LP |
387 | |
388 | case JOB_RELOAD: | |
389 | return | |
032ff4af | 390 | b == UNIT_RELOADING; |
593fbdd2 | 391 | |
593fbdd2 LP |
392 | case JOB_RESTART: |
393 | return | |
394 | b == UNIT_ACTIVATING; | |
395 | ||
7e803f5e MS |
396 | case JOB_NOP: |
397 | return true; | |
398 | ||
e0209d83 MS |
399 | default: |
400 | assert_not_reached("Invalid job type"); | |
401 | } | |
402 | } | |
403 | ||
c6497ccb | 404 | JobType job_type_collapse(JobType t, Unit *u) { |
e0209d83 MS |
405 | UnitActiveState s; |
406 | ||
c6497ccb | 407 | switch (t) { |
e0209d83 | 408 | |
593fbdd2 | 409 | case JOB_TRY_RESTART: |
e0209d83 MS |
410 | s = unit_active_state(u); |
411 | if (UNIT_IS_INACTIVE_OR_DEACTIVATING(s)) | |
c6497ccb LP |
412 | return JOB_NOP; |
413 | ||
414 | return JOB_RESTART; | |
e0209d83 | 415 | |
3282591d LP |
416 | case JOB_TRY_RELOAD: |
417 | s = unit_active_state(u); | |
418 | if (UNIT_IS_INACTIVE_OR_DEACTIVATING(s)) | |
419 | return JOB_NOP; | |
420 | ||
421 | return JOB_RELOAD; | |
422 | ||
e0209d83 MS |
423 | case JOB_RELOAD_OR_START: |
424 | s = unit_active_state(u); | |
425 | if (UNIT_IS_INACTIVE_OR_DEACTIVATING(s)) | |
c6497ccb LP |
426 | return JOB_START; |
427 | ||
428 | return JOB_RELOAD; | |
593fbdd2 LP |
429 | |
430 | default: | |
c6497ccb | 431 | return t; |
593fbdd2 LP |
432 | } |
433 | } | |
434 | ||
e0209d83 | 435 | int job_type_merge_and_collapse(JobType *a, JobType b, Unit *u) { |
c6497ccb LP |
436 | JobType t; |
437 | ||
438 | t = job_type_lookup_merge(*a, b); | |
e0209d83 MS |
439 | if (t < 0) |
440 | return -EEXIST; | |
c6497ccb LP |
441 | |
442 | *a = job_type_collapse(t, u); | |
e0209d83 MS |
443 | return 0; |
444 | } | |
445 | ||
9588bc32 | 446 | static bool job_is_runnable(Job *j) { |
034c6ed7 | 447 | Iterator i; |
87f0e418 | 448 | Unit *other; |
eef85c4a | 449 | void *v; |
5cb5a6ff LP |
450 | |
451 | assert(j); | |
ac1135be | 452 | assert(j->installed); |
5cb5a6ff | 453 | |
87f0e418 | 454 | /* Checks whether there is any job running for the units this |
5cb5a6ff | 455 | * job needs to be running after (in the case of a 'positive' |
e67c3609 LP |
456 | * job type) or before (in the case of a 'negative' job |
457 | * type. */ | |
458 | ||
66ca4ec4 LP |
459 | /* Note that unit types have a say in what is runnable, |
460 | * too. For example, if they return -EAGAIN from | |
461 | * unit_start() they can indicate they are not | |
462 | * runnable yet. */ | |
463 | ||
e67c3609 | 464 | /* First check if there is an override */ |
cebe0d41 | 465 | if (j->ignore_order) |
e67c3609 | 466 | return true; |
5cb5a6ff | 467 | |
e0209d83 MS |
468 | if (j->type == JOB_NOP) |
469 | return true; | |
470 | ||
0a23a627 | 471 | if (IN_SET(j->type, JOB_START, JOB_VERIFY_ACTIVE, JOB_RELOAD)) { |
5cb5a6ff | 472 | /* Immediate result is that the job is or might be |
fc08079e | 473 | * started. In this case let's wait for the |
5cb5a6ff LP |
474 | * dependencies, regardless whether they are |
475 | * starting or stopping something. */ | |
476 | ||
eef85c4a | 477 | HASHMAP_FOREACH_KEY(v, other, j->unit->dependencies[UNIT_AFTER], i) |
ac155bb8 | 478 | if (other->job) |
5cb5a6ff LP |
479 | return false; |
480 | } | |
481 | ||
482 | /* Also, if something else is being stopped and we should | |
fc08079e | 483 | * change state after it, then let's wait. */ |
5cb5a6ff | 484 | |
eef85c4a | 485 | HASHMAP_FOREACH_KEY(v, other, j->unit->dependencies[UNIT_BEFORE], i) |
ac155bb8 | 486 | if (other->job && |
0a23a627 | 487 | IN_SET(other->job->type, JOB_STOP, JOB_RESTART)) |
5cb5a6ff LP |
488 | return false; |
489 | ||
490 | /* This means that for a service a and a service b where b | |
491 | * shall be started after a: | |
492 | * | |
493 | * start a + start b → 1st step start a, 2nd step start b | |
494 | * start a + stop b → 1st step stop b, 2nd step start a | |
495 | * stop a + start b → 1st step stop a, 2nd step start b | |
496 | * stop a + stop b → 1st step stop b, 2nd step stop a | |
497 | * | |
498 | * This has the side effect that restarts are properly | |
499 | * synchronized too. */ | |
500 | ||
501 | return true; | |
502 | } | |
503 | ||
bbd1a837 | 504 | static void job_change_type(Job *j, JobType newtype) { |
f2341e0a LP |
505 | assert(j); |
506 | ||
507 | log_unit_debug(j->unit, | |
66870f90 ZJS |
508 | "Converting job %s/%s -> %s/%s", |
509 | j->unit->id, job_type_to_string(j->type), | |
510 | j->unit->id, job_type_to_string(newtype)); | |
bbd1a837 MS |
511 | |
512 | j->type = newtype; | |
513 | } | |
514 | ||
33a3fdd9 | 515 | _pure_ static const char* job_get_begin_status_message_format(Unit *u, JobType t) { |
a69b3872 | 516 | const char *format; |
33a3fdd9 LP |
517 | |
518 | assert(u); | |
a69b3872 LP |
519 | |
520 | if (t == JOB_RELOAD) | |
521 | return "Reloading %s."; | |
522 | ||
523 | assert(IN_SET(t, JOB_START, JOB_STOP)); | |
524 | ||
ff30a86b LP |
525 | format = UNIT_VTABLE(u)->status_message_formats.starting_stopping[t == JOB_STOP]; |
526 | if (format) | |
527 | return format; | |
33a3fdd9 LP |
528 | |
529 | /* Return generic strings */ | |
530 | if (t == JOB_START) | |
531 | return "Starting %s."; | |
a69b3872 LP |
532 | else { |
533 | assert(t == JOB_STOP); | |
33a3fdd9 | 534 | return "Stopping %s."; |
a69b3872 | 535 | } |
33a3fdd9 LP |
536 | } |
537 | ||
538 | static void job_print_begin_status_message(Unit *u, JobType t) { | |
539 | const char *format; | |
540 | ||
541 | assert(u); | |
542 | ||
543 | /* Reload status messages have traditionally not been printed to console. */ | |
544 | if (!IN_SET(t, JOB_START, JOB_STOP)) | |
545 | return; | |
546 | ||
547 | format = job_get_begin_status_message_format(u, t); | |
548 | ||
549 | DISABLE_WARNING_FORMAT_NONLITERAL; | |
550 | unit_status_printf(u, "", format); | |
551 | REENABLE_WARNING; | |
552 | } | |
553 | ||
b344b363 | 554 | static void job_log_begin_status_message(Unit *u, uint32_t job_id, JobType t) { |
33a3fdd9 LP |
555 | const char *format, *mid; |
556 | char buf[LINE_MAX]; | |
557 | ||
558 | assert(u); | |
b344b363 LP |
559 | assert(t >= 0); |
560 | assert(t < _JOB_TYPE_MAX); | |
33a3fdd9 LP |
561 | |
562 | if (!IN_SET(t, JOB_START, JOB_STOP, JOB_RELOAD)) | |
563 | return; | |
564 | ||
0e2b4a82 | 565 | if (log_on_console()) /* Skip this if it would only go on the console anyway */ |
33a3fdd9 LP |
566 | return; |
567 | ||
568 | /* We log status messages for all units and all operations. */ | |
569 | ||
570 | format = job_get_begin_status_message_format(u, t); | |
571 | ||
572 | DISABLE_WARNING_FORMAT_NONLITERAL; | |
573 | (void) snprintf(buf, sizeof buf, format, unit_description(u)); | |
574 | REENABLE_WARNING; | |
575 | ||
576 | mid = t == JOB_START ? "MESSAGE_ID=" SD_MESSAGE_UNIT_STARTING_STR : | |
577 | t == JOB_STOP ? "MESSAGE_ID=" SD_MESSAGE_UNIT_STOPPING_STR : | |
578 | "MESSAGE_ID=" SD_MESSAGE_UNIT_RELOADING_STR; | |
579 | ||
580 | /* Note that we deliberately use LOG_MESSAGE() instead of | |
581 | * LOG_UNIT_MESSAGE() here, since this is supposed to mimic | |
582 | * closely what is written to screen using the status output, | |
583 | * which is supposed the highest level, friendliest output | |
584 | * possible, which means we should avoid the low-level unit | |
585 | * name. */ | |
586 | log_struct(LOG_INFO, | |
587 | LOG_MESSAGE("%s", buf), | |
b344b363 LP |
588 | "JOB_ID=%" PRIu32, job_id, |
589 | "JOB_TYPE=%s", job_type_to_string(t), | |
33a3fdd9 LP |
590 | LOG_UNIT_ID(u), |
591 | LOG_UNIT_INVOCATION_ID(u), | |
592 | mid); | |
593 | } | |
594 | ||
b344b363 | 595 | static void job_emit_begin_status_message(Unit *u, uint32_t job_id, JobType t) { |
33a3fdd9 LP |
596 | assert(u); |
597 | assert(t >= 0); | |
598 | assert(t < _JOB_TYPE_MAX); | |
599 | ||
b344b363 | 600 | job_log_begin_status_message(u, job_id, t); |
33a3fdd9 LP |
601 | job_print_begin_status_message(u, t); |
602 | } | |
603 | ||
d1a34ae9 | 604 | static int job_perform_on_unit(Job **j) { |
df446f96 LP |
605 | uint32_t id; |
606 | Manager *m; | |
607 | JobType t; | |
608 | Unit *u; | |
d1a34ae9 MS |
609 | int r; |
610 | ||
df446f96 LP |
611 | /* While we execute this operation the job might go away (for |
612 | * example: because it finishes immediately or is replaced by | |
613 | * a new, conflicting job.) To make sure we don't access a | |
614 | * freed job later on we store the id here, so that we can | |
615 | * verify the job is still valid. */ | |
616 | ||
617 | assert(j); | |
618 | assert(*j); | |
619 | ||
620 | m = (*j)->manager; | |
621 | u = (*j)->unit; | |
622 | t = (*j)->type; | |
623 | id = (*j)->id; | |
624 | ||
d1a34ae9 MS |
625 | switch (t) { |
626 | case JOB_START: | |
627 | r = unit_start(u); | |
628 | break; | |
629 | ||
630 | case JOB_RESTART: | |
631 | t = JOB_STOP; | |
4831981d | 632 | _fallthrough_; |
d1a34ae9 MS |
633 | case JOB_STOP: |
634 | r = unit_stop(u); | |
635 | break; | |
636 | ||
637 | case JOB_RELOAD: | |
638 | r = unit_reload(u); | |
639 | break; | |
640 | ||
641 | default: | |
642 | assert_not_reached("Invalid job type"); | |
643 | } | |
644 | ||
0e2b4a82 LP |
645 | /* Log if the job still exists and the start/stop/reload function actually did something. Note that this means |
646 | * for units for which there's no 'activating' phase (i.e. because we transition directly from 'inactive' to | |
647 | * 'active') we'll possibly skip the "Starting..." message. */ | |
d1a34ae9 MS |
648 | *j = manager_get_job(m, id); |
649 | if (*j && r > 0) | |
b344b363 | 650 | job_emit_begin_status_message(u, id, t); |
d1a34ae9 MS |
651 | |
652 | return r; | |
653 | } | |
654 | ||
5cb5a6ff LP |
655 | int job_run_and_invalidate(Job *j) { |
656 | int r; | |
ac1135be | 657 | |
5cb5a6ff | 658 | assert(j); |
ac1135be | 659 | assert(j->installed); |
e0209d83 | 660 | assert(j->type < _JOB_TYPE_MAX_IN_TRANSACTION); |
66aa6f7f | 661 | assert(j->in_run_queue); |
5cb5a6ff | 662 | |
71fda00f | 663 | LIST_REMOVE(run_queue, j->manager->run_queue, j); |
66aa6f7f | 664 | j->in_run_queue = false; |
5cb5a6ff LP |
665 | |
666 | if (j->state != JOB_WAITING) | |
667 | return 0; | |
668 | ||
034c6ed7 LP |
669 | if (!job_is_runnable(j)) |
670 | return -EAGAIN; | |
671 | ||
a2df3ea4 | 672 | job_start_timer(j, true); |
9c3349e2 | 673 | job_set_state(j, JOB_RUNNING); |
c1e1601e | 674 | job_add_to_dbus_queue(j); |
83c60c9f | 675 | |
5cb5a6ff LP |
676 | switch (j->type) { |
677 | ||
5cb5a6ff | 678 | case JOB_VERIFY_ACTIVE: { |
ea2c0e45 LP |
679 | UnitActiveState t; |
680 | ||
681 | t = unit_active_state(j->unit); | |
87f0e418 | 682 | if (UNIT_IS_ACTIVE_OR_RELOADING(t)) |
5cb5a6ff | 683 | r = -EALREADY; |
87f0e418 | 684 | else if (t == UNIT_ACTIVATING) |
5cb5a6ff LP |
685 | r = -EAGAIN; |
686 | else | |
6a371e23 | 687 | r = -EBADR; |
5cb5a6ff LP |
688 | break; |
689 | } | |
690 | ||
d1a34ae9 | 691 | case JOB_START: |
5cb5a6ff | 692 | case JOB_STOP: |
dd17d388 | 693 | case JOB_RESTART: |
d1a34ae9 | 694 | r = job_perform_on_unit(&j); |
57339f47 | 695 | |
ea2c0e45 | 696 | /* If the unit type does not support starting/stopping, then simply wait. */ |
57339f47 LP |
697 | if (r == -EBADR) |
698 | r = 0; | |
5cb5a6ff LP |
699 | break; |
700 | ||
701 | case JOB_RELOAD: | |
d1a34ae9 | 702 | r = job_perform_on_unit(&j); |
5cb5a6ff LP |
703 | break; |
704 | ||
e0209d83 MS |
705 | case JOB_NOP: |
706 | r = -EALREADY; | |
707 | break; | |
708 | ||
5cb5a6ff | 709 | default: |
44d8db9e | 710 | assert_not_reached("Unknown job type"); |
5cb5a6ff LP |
711 | } |
712 | ||
e0209d83 | 713 | if (j) { |
8ebd9175 LP |
714 | if (r == -EAGAIN) |
715 | job_set_state(j, JOB_WAITING); /* Hmm, not ready after all, let's return to JOB_WAITING state */ | |
6e64994d | 716 | else if (r == -EALREADY) /* already being executed */ |
833f92ad | 717 | r = job_finish_and_invalidate(j, JOB_DONE, true, true); |
6e64994d LP |
718 | else if (r == -ECOMM) /* condition failed, but all is good */ |
719 | r = job_finish_and_invalidate(j, JOB_DONE, true, false); | |
6a371e23 | 720 | else if (r == -EBADR) |
833f92ad | 721 | r = job_finish_and_invalidate(j, JOB_SKIPPED, true, false); |
6a371e23 | 722 | else if (r == -ENOEXEC) |
833f92ad | 723 | r = job_finish_and_invalidate(j, JOB_INVALID, true, false); |
59fccdc5 | 724 | else if (r == -EPROTO) |
833f92ad | 725 | r = job_finish_and_invalidate(j, JOB_ASSERT, true, false); |
15411c0c | 726 | else if (r == -EOPNOTSUPP) |
833f92ad | 727 | r = job_finish_and_invalidate(j, JOB_UNSUPPORTED, true, false); |
631b676b LP |
728 | else if (r == -ENOLINK) |
729 | r = job_finish_and_invalidate(j, JOB_DEPENDENCY, true, false); | |
d4fd1cf2 LP |
730 | else if (r == -ESTALE) |
731 | r = job_finish_and_invalidate(j, JOB_ONCE, true, false); | |
9c3349e2 | 732 | else if (r < 0) |
833f92ad | 733 | r = job_finish_and_invalidate(j, JOB_FAILED, true, false); |
2cf19a7a | 734 | } |
5cb5a6ff LP |
735 | |
736 | return r; | |
737 | } | |
738 | ||
33a3fdd9 | 739 | _pure_ static const char *job_get_done_status_message_format(Unit *u, JobType t, JobResult result) { |
df446f96 | 740 | |
aa49ab5f MS |
741 | static const char *const generic_finished_start_job[_JOB_RESULT_MAX] = { |
742 | [JOB_DONE] = "Started %s.", | |
743 | [JOB_TIMEOUT] = "Timed out starting %s.", | |
744 | [JOB_FAILED] = "Failed to start %s.", | |
745 | [JOB_DEPENDENCY] = "Dependency failed for %s.", | |
746 | [JOB_ASSERT] = "Assertion failed for %s.", | |
747 | [JOB_UNSUPPORTED] = "Starting of %s not supported.", | |
4332edf6 | 748 | [JOB_COLLECTED] = "Unnecessary job for %s was removed.", |
d4fd1cf2 | 749 | [JOB_ONCE] = "Unit %s has been started before and cannot be started again." |
aa49ab5f MS |
750 | }; |
751 | static const char *const generic_finished_stop_job[_JOB_RESULT_MAX] = { | |
752 | [JOB_DONE] = "Stopped %s.", | |
753 | [JOB_FAILED] = "Stopped (with error) %s.", | |
b59f0ecd | 754 | [JOB_TIMEOUT] = "Timed out stopping %s.", |
aa49ab5f MS |
755 | }; |
756 | static const char *const generic_finished_reload_job[_JOB_RESULT_MAX] = { | |
757 | [JOB_DONE] = "Reloaded %s.", | |
758 | [JOB_FAILED] = "Reload failed for %s.", | |
759 | [JOB_TIMEOUT] = "Timed out reloading %s.", | |
760 | }; | |
761 | /* When verify-active detects the unit is inactive, report it. | |
762 | * Most likely a DEPEND warning from a requisiting unit will | |
763 | * occur next and it's nice to see what was requisited. */ | |
764 | static const char *const generic_finished_verify_active_job[_JOB_RESULT_MAX] = { | |
765 | [JOB_SKIPPED] = "%s is not active.", | |
766 | }; | |
877d54e9 | 767 | |
df446f96 LP |
768 | const char *format; |
769 | ||
877d54e9 LP |
770 | assert(u); |
771 | assert(t >= 0); | |
772 | assert(t < _JOB_TYPE_MAX); | |
c6918296 | 773 | |
df446f96 | 774 | if (IN_SET(t, JOB_START, JOB_STOP, JOB_RESTART)) { |
ff30a86b LP |
775 | format = t == JOB_START ? |
776 | UNIT_VTABLE(u)->status_message_formats.finished_start_job[result] : | |
777 | UNIT_VTABLE(u)->status_message_formats.finished_stop_job[result]; | |
778 | if (format) | |
779 | return format; | |
aa49ab5f | 780 | } |
877d54e9 | 781 | |
aa49ab5f | 782 | /* Return generic strings */ |
877d54e9 | 783 | if (t == JOB_START) |
aa49ab5f | 784 | return generic_finished_start_job[result]; |
4c701096 | 785 | else if (IN_SET(t, JOB_STOP, JOB_RESTART)) |
aa49ab5f MS |
786 | return generic_finished_stop_job[result]; |
787 | else if (t == JOB_RELOAD) | |
788 | return generic_finished_reload_job[result]; | |
789 | else if (t == JOB_VERIFY_ACTIVE) | |
790 | return generic_finished_verify_active_job[result]; | |
877d54e9 LP |
791 | |
792 | return NULL; | |
793 | } | |
794 | ||
047d7219 ZJS |
795 | static const struct { |
796 | const char *color, *word; | |
33a3fdd9 | 797 | } job_print_done_status_messages[_JOB_RESULT_MAX] = { |
96164a39 | 798 | [JOB_DONE] = { ANSI_OK_COLOR, " OK " }, |
047d7219 ZJS |
799 | [JOB_TIMEOUT] = { ANSI_HIGHLIGHT_RED, " TIME " }, |
800 | [JOB_FAILED] = { ANSI_HIGHLIGHT_RED, "FAILED" }, | |
801 | [JOB_DEPENDENCY] = { ANSI_HIGHLIGHT_YELLOW, "DEPEND" }, | |
802 | [JOB_SKIPPED] = { ANSI_HIGHLIGHT, " INFO " }, | |
803 | [JOB_ASSERT] = { ANSI_HIGHLIGHT_YELLOW, "ASSERT" }, | |
804 | [JOB_UNSUPPORTED] = { ANSI_HIGHLIGHT_YELLOW, "UNSUPP" }, | |
805 | /* JOB_COLLECTED */ | |
d4fd1cf2 | 806 | [JOB_ONCE] = { ANSI_HIGHLIGHT_RED, " ONCE " }, |
047d7219 | 807 | }; |
e02cd6f7 | 808 | |
33a3fdd9 | 809 | static void job_print_done_status_message(Unit *u, JobType t, JobResult result) { |
df446f96 | 810 | const char *format; |
dc9b5816 | 811 | const char *status; |
df446f96 | 812 | |
877d54e9 LP |
813 | assert(u); |
814 | assert(t >= 0); | |
815 | assert(t < _JOB_TYPE_MAX); | |
816 | ||
df446f96 LP |
817 | /* Reload status messages have traditionally not been printed to console. */ |
818 | if (t == JOB_RELOAD) | |
819 | return; | |
820 | ||
9a80f2f4 LP |
821 | /* No message if the job did not actually do anything due to failed condition. */ |
822 | if (t == JOB_START && result == JOB_DONE && !u->condition_result) | |
823 | return; | |
824 | ||
33a3fdd9 | 825 | if (!job_print_done_status_messages[result].word) |
047d7219 ZJS |
826 | return; |
827 | ||
33a3fdd9 | 828 | format = job_get_done_status_message_format(u, t, result); |
aa49ab5f MS |
829 | if (!format) |
830 | return; | |
e02cd6f7 | 831 | |
dc9b5816 | 832 | if (log_get_show_color()) |
33a3fdd9 LP |
833 | status = strjoina(job_print_done_status_messages[result].color, |
834 | job_print_done_status_messages[result].word, | |
047d7219 | 835 | ANSI_NORMAL); |
dc9b5816 | 836 | else |
33a3fdd9 | 837 | status = job_print_done_status_messages[result].word; |
dc9b5816 | 838 | |
aa49ab5f MS |
839 | if (result != JOB_DONE) |
840 | manager_flip_auto_status(u->manager, true); | |
e02cd6f7 | 841 | |
aa49ab5f | 842 | DISABLE_WARNING_FORMAT_NONLITERAL; |
dc9b5816 | 843 | unit_status_printf(u, status, format); |
aa49ab5f | 844 | REENABLE_WARNING; |
7cf82e0b | 845 | |
aa49ab5f | 846 | if (t == JOB_START && result == JOB_FAILED) { |
df446f96 | 847 | _cleanup_free_ char *quoted; |
7cf82e0b | 848 | |
804ee07c | 849 | quoted = shell_maybe_quote(u->id, ESCAPE_BACKSLASH); |
df446f96 | 850 | manager_status_printf(u->manager, STATUS_TYPE_NORMAL, NULL, "See 'systemctl status %s' for details.", strna(quoted)); |
e02cd6f7 LP |
851 | } |
852 | } | |
853 | ||
33a3fdd9 | 854 | static void job_log_done_status_message(Unit *u, uint32_t job_id, JobType t, JobResult result) { |
2b044526 | 855 | const char *format, *mid; |
877d54e9 | 856 | char buf[LINE_MAX]; |
64f575d2 MS |
857 | static const int job_result_log_level[_JOB_RESULT_MAX] = { |
858 | [JOB_DONE] = LOG_INFO, | |
859 | [JOB_CANCELED] = LOG_INFO, | |
860 | [JOB_TIMEOUT] = LOG_ERR, | |
861 | [JOB_FAILED] = LOG_ERR, | |
862 | [JOB_DEPENDENCY] = LOG_WARNING, | |
863 | [JOB_SKIPPED] = LOG_NOTICE, | |
864 | [JOB_INVALID] = LOG_INFO, | |
865 | [JOB_ASSERT] = LOG_WARNING, | |
866 | [JOB_UNSUPPORTED] = LOG_WARNING, | |
c5a97ed1 | 867 | [JOB_COLLECTED] = LOG_INFO, |
d4fd1cf2 | 868 | [JOB_ONCE] = LOG_ERR, |
64f575d2 | 869 | }; |
877d54e9 LP |
870 | |
871 | assert(u); | |
872 | assert(t >= 0); | |
873 | assert(t < _JOB_TYPE_MAX); | |
874 | ||
047d7219 ZJS |
875 | /* Skip printing if output goes to the console, and job_print_status_message() |
876 | will actually print something to the console. */ | |
33a3fdd9 | 877 | if (log_on_console() && job_print_done_status_messages[result].word) |
81270860 LP |
878 | return; |
879 | ||
9a80f2f4 LP |
880 | /* Show condition check message if the job did not actually do anything due to failed condition. */ |
881 | if (t == JOB_START && result == JOB_DONE && !u->condition_result) { | |
882 | log_struct(LOG_INFO, | |
883 | "MESSAGE=Condition check resulted in %s being skipped.", unit_description(u), | |
884 | "JOB_ID=%" PRIu32, job_id, | |
885 | "JOB_TYPE=%s", job_type_to_string(t), | |
886 | "JOB_RESULT=%s", job_result_to_string(result), | |
887 | LOG_UNIT_ID(u), | |
888 | LOG_UNIT_INVOCATION_ID(u), | |
889 | "MESSAGE_ID=" SD_MESSAGE_UNIT_STARTED_STR); | |
890 | ||
891 | return; | |
892 | } | |
893 | ||
33a3fdd9 | 894 | format = job_get_done_status_message_format(u, t, result); |
877d54e9 LP |
895 | if (!format) |
896 | return; | |
897 | ||
574432f8 ILG |
898 | /* The description might be longer than the buffer, but that's OK, |
899 | * we'll just truncate it here. Note that we use snprintf() rather than | |
900 | * xsprintf() on purpose here: we are fine with truncation and don't | |
901 | * consider that an error. */ | |
bcfce235 | 902 | DISABLE_WARNING_FORMAT_NONLITERAL; |
574432f8 | 903 | (void) snprintf(buf, sizeof(buf), format, unit_description(u)); |
bcfce235 | 904 | REENABLE_WARNING; |
877d54e9 | 905 | |
df446f96 LP |
906 | switch (t) { |
907 | ||
908 | case JOB_START: | |
2b044526 ZJS |
909 | if (result == JOB_DONE) |
910 | mid = "MESSAGE_ID=" SD_MESSAGE_UNIT_STARTED_STR; | |
911 | else | |
912 | mid = "MESSAGE_ID=" SD_MESSAGE_UNIT_FAILED_STR; | |
df446f96 LP |
913 | break; |
914 | ||
915 | case JOB_RELOAD: | |
2b044526 | 916 | mid = "MESSAGE_ID=" SD_MESSAGE_UNIT_RELOADED_STR; |
df446f96 LP |
917 | break; |
918 | ||
919 | case JOB_STOP: | |
920 | case JOB_RESTART: | |
2b044526 | 921 | mid = "MESSAGE_ID=" SD_MESSAGE_UNIT_STOPPED_STR; |
df446f96 LP |
922 | break; |
923 | ||
924 | default: | |
64f575d2 | 925 | log_struct(job_result_log_level[result], |
4f29c6fe | 926 | LOG_MESSAGE("%s", buf), |
1cd81629 | 927 | "JOB_ID=%" PRIu32, job_id, |
646cc98d LP |
928 | "JOB_TYPE=%s", job_type_to_string(t), |
929 | "JOB_RESULT=%s", job_result_to_string(result), | |
ba360bb0 | 930 | LOG_UNIT_ID(u), |
a1230ff9 | 931 | LOG_UNIT_INVOCATION_ID(u)); |
b81bbe53 MS |
932 | return; |
933 | } | |
934 | ||
64f575d2 | 935 | log_struct(job_result_log_level[result], |
b81bbe53 | 936 | LOG_MESSAGE("%s", buf), |
1cd81629 | 937 | "JOB_ID=%" PRIu32, job_id, |
646cc98d LP |
938 | "JOB_TYPE=%s", job_type_to_string(t), |
939 | "JOB_RESULT=%s", job_result_to_string(result), | |
ba360bb0 | 940 | LOG_UNIT_ID(u), |
f1c50bec | 941 | LOG_UNIT_INVOCATION_ID(u), |
a1230ff9 | 942 | mid); |
877d54e9 | 943 | } |
877d54e9 | 944 | |
33a3fdd9 | 945 | static void job_emit_done_status_message(Unit *u, uint32_t job_id, JobType t, JobResult result) { |
646cc98d | 946 | assert(u); |
30961fa3 | 947 | |
33a3fdd9 LP |
948 | job_log_done_status_message(u, job_id, t, result); |
949 | job_print_done_status_message(u, t, result); | |
30961fa3 MS |
950 | } |
951 | ||
be7d9ff7 LP |
952 | static void job_fail_dependencies(Unit *u, UnitDependency d) { |
953 | Unit *other; | |
954 | Iterator i; | |
eef85c4a | 955 | void *v; |
be7d9ff7 LP |
956 | |
957 | assert(u); | |
958 | ||
eef85c4a | 959 | HASHMAP_FOREACH_KEY(v, other, u->dependencies[d], i) { |
be7d9ff7 LP |
960 | Job *j = other->job; |
961 | ||
962 | if (!j) | |
963 | continue; | |
964 | if (!IN_SET(j->type, JOB_START, JOB_VERIFY_ACTIVE)) | |
965 | continue; | |
966 | ||
833f92ad | 967 | job_finish_and_invalidate(j, JOB_DEPENDENCY, true, false); |
be7d9ff7 LP |
968 | } |
969 | } | |
970 | ||
a7a7163d DT |
971 | static int job_save_pending_finished_job(Job *j) { |
972 | int r; | |
973 | ||
974 | assert(j); | |
975 | ||
976 | r = set_ensure_allocated(&j->manager->pending_finished_jobs, NULL); | |
977 | if (r < 0) | |
978 | return r; | |
979 | ||
980 | job_unlink(j); | |
981 | return set_put(j->manager->pending_finished_jobs, j); | |
982 | } | |
983 | ||
833f92ad | 984 | int job_finish_and_invalidate(Job *j, JobResult result, bool recursive, bool already) { |
87f0e418 LP |
985 | Unit *u; |
986 | Unit *other; | |
b866264a | 987 | JobType t; |
034c6ed7 | 988 | Iterator i; |
eef85c4a | 989 | void *v; |
5cb5a6ff LP |
990 | |
991 | assert(j); | |
ac1135be | 992 | assert(j->installed); |
e0209d83 | 993 | assert(j->type < _JOB_TYPE_MAX_IN_TRANSACTION); |
5cb5a6ff | 994 | |
c6918296 MS |
995 | u = j->unit; |
996 | t = j->type; | |
997 | ||
998 | j->result = result; | |
999 | ||
1cd81629 | 1000 | log_unit_debug(u, "Job %" PRIu32 " %s/%s finished, result=%s", j->id, u->id, job_type_to_string(t), job_result_to_string(result)); |
c6918296 | 1001 | |
833f92ad MS |
1002 | /* If this job did nothing to respective unit we don't log the status message */ |
1003 | if (!already) | |
33a3fdd9 | 1004 | job_emit_done_status_message(u, j->id, t, result); |
c6918296 | 1005 | |
034c6ed7 | 1006 | /* Patch restart jobs so that they become normal start jobs */ |
c6918296 | 1007 | if (result == JOB_DONE && t == JOB_RESTART) { |
f50e0a01 | 1008 | |
bbd1a837 | 1009 | job_change_type(j, JOB_START); |
9c3349e2 | 1010 | job_set_state(j, JOB_WAITING); |
cc42e081 | 1011 | |
fec7615c | 1012 | job_add_to_dbus_queue(j); |
cc42e081 | 1013 | job_add_to_run_queue(j); |
c5a97ed1 | 1014 | job_add_to_gc_queue(j); |
57981b98 | 1015 | |
57981b98 | 1016 | goto finish; |
5cb5a6ff LP |
1017 | } |
1018 | ||
3742095b | 1019 | if (IN_SET(result, JOB_FAILED, JOB_INVALID)) |
313cefa1 | 1020 | j->manager->n_failed_jobs++; |
76bf48b7 | 1021 | |
97e7d748 | 1022 | job_uninstall(j); |
509ad789 ZJS |
1023 | /* Keep jobs started before the reload to send singal later, free all others */ |
1024 | if (!MANAGER_IS_RELOADING(j->manager) || | |
1025 | !j->reloaded || | |
1026 | job_save_pending_finished_job(j) < 0) | |
a7a7163d | 1027 | job_free(j); |
5cb5a6ff LP |
1028 | |
1029 | /* Fail depending jobs on failure */ | |
5273510e | 1030 | if (result != JOB_DONE && recursive) { |
be7d9ff7 LP |
1031 | if (IN_SET(t, JOB_START, JOB_VERIFY_ACTIVE)) { |
1032 | job_fail_dependencies(u, UNIT_REQUIRED_BY); | |
1033 | job_fail_dependencies(u, UNIT_REQUISITE_OF); | |
1034 | job_fail_dependencies(u, UNIT_BOUND_BY); | |
be7d9ff7 LP |
1035 | } else if (t == JOB_STOP) |
1036 | job_fail_dependencies(u, UNIT_CONFLICTED_BY); | |
5cb5a6ff LP |
1037 | } |
1038 | ||
c0daa706 | 1039 | /* Trigger OnFailure dependencies that are not generated by |
66870f90 | 1040 | * the unit itself. We don't treat JOB_CANCELED as failure in |
c0daa706 LP |
1041 | * this context. And JOB_FAILURE is already handled by the |
1042 | * unit itself. */ | |
646cc98d | 1043 | if (IN_SET(result, JOB_TIMEOUT, JOB_DEPENDENCY)) { |
f2341e0a LP |
1044 | log_struct(LOG_NOTICE, |
1045 | "JOB_TYPE=%s", job_type_to_string(t), | |
1046 | "JOB_RESULT=%s", job_result_to_string(result), | |
1047 | LOG_UNIT_ID(u), | |
1048 | LOG_UNIT_MESSAGE(u, "Job %s/%s failed with result '%s'.", | |
e2cc6eca LP |
1049 | u->id, |
1050 | job_type_to_string(t), | |
a1230ff9 | 1051 | job_result_to_string(result))); |
222ae6a8 | 1052 | |
3ecaa09b | 1053 | unit_start_on_failure(u); |
222ae6a8 | 1054 | } |
c0daa706 | 1055 | |
3ecaa09b LP |
1056 | unit_trigger_notify(u); |
1057 | ||
57981b98 | 1058 | finish: |
5cb5a6ff | 1059 | /* Try to start the next jobs that can be started */ |
eef85c4a | 1060 | HASHMAP_FOREACH_KEY(v, other, u->dependencies[UNIT_AFTER], i) |
c5a97ed1 | 1061 | if (other->job) { |
ac155bb8 | 1062 | job_add_to_run_queue(other->job); |
c5a97ed1 LP |
1063 | job_add_to_gc_queue(other->job); |
1064 | } | |
eef85c4a | 1065 | HASHMAP_FOREACH_KEY(v, other, u->dependencies[UNIT_BEFORE], i) |
c5a97ed1 | 1066 | if (other->job) { |
ac155bb8 | 1067 | job_add_to_run_queue(other->job); |
c5a97ed1 LP |
1068 | job_add_to_gc_queue(other->job); |
1069 | } | |
5cb5a6ff | 1070 | |
ac155bb8 | 1071 | manager_check_finished(u->manager); |
b0c918b9 | 1072 | |
5273510e | 1073 | return 0; |
5cb5a6ff | 1074 | } |
034c6ed7 | 1075 | |
718db961 LP |
1076 | static int job_dispatch_timer(sd_event_source *s, uint64_t monotonic, void *userdata) { |
1077 | Job *j = userdata; | |
f189ab18 | 1078 | Unit *u; |
faf919f1 | 1079 | |
718db961 LP |
1080 | assert(j); |
1081 | assert(s == j->timer_event_source); | |
faf919f1 | 1082 | |
f2341e0a | 1083 | log_unit_warning(j->unit, "Job %s/%s timed out.", j->unit->id, job_type_to_string(j->type)); |
faf919f1 | 1084 | |
f189ab18 | 1085 | u = j->unit; |
833f92ad | 1086 | job_finish_and_invalidate(j, JOB_TIMEOUT, true, false); |
f189ab18 | 1087 | |
c7adcb1a ZJS |
1088 | emergency_action(u->manager, u->job_timeout_action, |
1089 | EMERGENCY_ACTION_IS_WATCHDOG|EMERGENCY_ACTION_WARN, | |
7af67e9a | 1090 | u->job_timeout_reboot_arg, -1, "job timed out"); |
f189ab18 | 1091 | |
718db961 LP |
1092 | return 0; |
1093 | } | |
faf919f1 | 1094 | |
a2df3ea4 | 1095 | int job_start_timer(Job *j, bool job_running) { |
718db961 | 1096 | int r; |
171f12ce | 1097 | usec_t timeout_time, old_timeout_time; |
faf919f1 | 1098 | |
a2df3ea4 | 1099 | if (job_running) { |
171f12ce MK |
1100 | j->begin_running_usec = now(CLOCK_MONOTONIC); |
1101 | ||
a2df3ea4 MK |
1102 | if (j->unit->job_running_timeout == USEC_INFINITY) |
1103 | return 0; | |
faf919f1 | 1104 | |
171f12ce | 1105 | timeout_time = usec_add(j->begin_running_usec, j->unit->job_running_timeout); |
faf919f1 | 1106 | |
a2df3ea4 MK |
1107 | if (j->timer_event_source) { |
1108 | /* Update only if JobRunningTimeoutSec= results in earlier timeout */ | |
1109 | r = sd_event_source_get_time(j->timer_event_source, &old_timeout_time); | |
1110 | if (r < 0) | |
1111 | return r; | |
1112 | ||
1113 | if (old_timeout_time <= timeout_time) | |
1114 | return 0; | |
1115 | ||
1116 | return sd_event_source_set_time(j->timer_event_source, timeout_time); | |
1117 | } | |
1118 | } else { | |
1119 | if (j->timer_event_source) | |
1120 | return 0; | |
1121 | ||
1122 | j->begin_usec = now(CLOCK_MONOTONIC); | |
1123 | ||
1124 | if (j->unit->job_timeout == USEC_INFINITY) | |
1125 | return 0; | |
1126 | ||
1127 | timeout_time = usec_add(j->begin_usec, j->unit->job_timeout); | |
1128 | } | |
8bb310c3 | 1129 | |
6a0f1f6d LP |
1130 | r = sd_event_add_time( |
1131 | j->manager->event, | |
1132 | &j->timer_event_source, | |
1133 | CLOCK_MONOTONIC, | |
a2df3ea4 | 1134 | timeout_time, 0, |
6a0f1f6d | 1135 | job_dispatch_timer, j); |
718db961 LP |
1136 | if (r < 0) |
1137 | return r; | |
faf919f1 | 1138 | |
7dfbe2e3 TG |
1139 | (void) sd_event_source_set_description(j->timer_event_source, "job-start"); |
1140 | ||
718db961 | 1141 | return 0; |
faf919f1 LP |
1142 | } |
1143 | ||
c1e1601e | 1144 | void job_add_to_run_queue(Job *j) { |
f8c34706 LP |
1145 | int r; |
1146 | ||
034c6ed7 | 1147 | assert(j); |
ac1135be | 1148 | assert(j->installed); |
034c6ed7 LP |
1149 | |
1150 | if (j->in_run_queue) | |
1151 | return; | |
1152 | ||
f8c34706 LP |
1153 | if (!j->manager->run_queue) { |
1154 | r = sd_event_source_set_enabled(j->manager->run_queue_event_source, SD_EVENT_ONESHOT); | |
1155 | if (r < 0) | |
1156 | log_warning_errno(r, "Failed to enable job run queue event source, ignoring: %m"); | |
1157 | } | |
752b5905 | 1158 | |
71fda00f | 1159 | LIST_PREPEND(run_queue, j->manager->run_queue, j); |
034c6ed7 LP |
1160 | j->in_run_queue = true; |
1161 | } | |
94f04347 | 1162 | |
c1e1601e LP |
1163 | void job_add_to_dbus_queue(Job *j) { |
1164 | assert(j); | |
1165 | assert(j->installed); | |
1166 | ||
1167 | if (j->in_dbus_queue) | |
1168 | return; | |
1169 | ||
a567261a LP |
1170 | /* We don't check if anybody is subscribed here, since this |
1171 | * job might just have been created and not yet assigned to a | |
1172 | * connection/client. */ | |
94b6dfa2 | 1173 | |
71fda00f | 1174 | LIST_PREPEND(dbus_queue, j->manager->dbus_job_queue, j); |
c1e1601e LP |
1175 | j->in_dbus_queue = true; |
1176 | } | |
1177 | ||
ea430986 LP |
1178 | char *job_dbus_path(Job *j) { |
1179 | char *p; | |
1180 | ||
1181 | assert(j); | |
1182 | ||
ccd06097 | 1183 | if (asprintf(&p, "/org/freedesktop/systemd1/job/%"PRIu32, j->id) < 0) |
ea430986 LP |
1184 | return NULL; |
1185 | ||
1186 | return p; | |
1187 | } | |
1188 | ||
05a98afd LP |
1189 | int job_serialize(Job *j, FILE *f) { |
1190 | assert(j); | |
1191 | assert(f); | |
1192 | ||
d68c645b LP |
1193 | (void) serialize_item_format(f, "job-id", "%u", j->id); |
1194 | (void) serialize_item(f, "job-type", job_type_to_string(j->type)); | |
1195 | (void) serialize_item(f, "job-state", job_state_to_string(j->state)); | |
1196 | (void) serialize_bool(f, "job-irreversible", j->irreversible); | |
1197 | (void) serialize_bool(f, "job-sent-dbus-new-signal", j->sent_dbus_new_signal); | |
1198 | (void) serialize_bool(f, "job-ignore-order", j->ignore_order); | |
718db961 LP |
1199 | |
1200 | if (j->begin_usec > 0) | |
d68c645b | 1201 | (void) serialize_usec(f, "job-begin", j->begin_usec); |
171f12ce | 1202 | if (j->begin_running_usec > 0) |
d68c645b | 1203 | (void) serialize_usec(f, "job-begin-running", j->begin_running_usec); |
718db961 | 1204 | |
1a465207 | 1205 | bus_track_serialize(j->bus_track, f, "subscribed"); |
39a18c60 MS |
1206 | |
1207 | /* End marker */ | |
1208 | fputc('\n', f); | |
1209 | return 0; | |
1210 | } | |
1211 | ||
05a98afd | 1212 | int job_deserialize(Job *j, FILE *f) { |
8948b341 LP |
1213 | int r; |
1214 | ||
718db961 | 1215 | assert(j); |
05a98afd | 1216 | assert(f); |
718db961 | 1217 | |
39a18c60 | 1218 | for (;;) { |
8948b341 LP |
1219 | _cleanup_free_ char *line = NULL; |
1220 | char *l, *v; | |
39a18c60 MS |
1221 | size_t k; |
1222 | ||
8948b341 LP |
1223 | r = read_line(f, LONG_LINE_MAX, &line); |
1224 | if (r < 0) | |
1225 | return log_error_errno(r, "Failed to read serialization line: %m"); | |
1226 | if (r == 0) | |
1227 | return 0; | |
39a18c60 | 1228 | |
39a18c60 MS |
1229 | l = strstrip(line); |
1230 | ||
1231 | /* End marker */ | |
8948b341 | 1232 | if (isempty(l)) |
39a18c60 MS |
1233 | return 0; |
1234 | ||
1235 | k = strcspn(l, "="); | |
1236 | ||
1237 | if (l[k] == '=') { | |
1238 | l[k] = 0; | |
1239 | v = l+k+1; | |
1240 | } else | |
1241 | v = l+k; | |
1242 | ||
1243 | if (streq(l, "job-id")) { | |
718db961 | 1244 | |
39a18c60 | 1245 | if (safe_atou32(v, &j->id) < 0) |
15ec1021 | 1246 | log_debug("Failed to parse job id value: %s", v); |
718db961 | 1247 | |
39a18c60 | 1248 | } else if (streq(l, "job-type")) { |
718db961 LP |
1249 | JobType t; |
1250 | ||
1251 | t = job_type_from_string(v); | |
39a18c60 | 1252 | if (t < 0) |
15ec1021 | 1253 | log_debug("Failed to parse job type: %s", v); |
e0209d83 | 1254 | else if (t >= _JOB_TYPE_MAX_IN_TRANSACTION) |
15ec1021 | 1255 | log_debug("Cannot deserialize job of type: %s", v); |
39a18c60 MS |
1256 | else |
1257 | j->type = t; | |
718db961 | 1258 | |
39a18c60 | 1259 | } else if (streq(l, "job-state")) { |
718db961 LP |
1260 | JobState s; |
1261 | ||
1262 | s = job_state_from_string(v); | |
39a18c60 | 1263 | if (s < 0) |
15ec1021 | 1264 | log_debug("Failed to parse job state: %s", v); |
39a18c60 | 1265 | else |
9c3349e2 | 1266 | job_set_state(j, s); |
718db961 | 1267 | |
23ade460 | 1268 | } else if (streq(l, "job-irreversible")) { |
718db961 LP |
1269 | int b; |
1270 | ||
1271 | b = parse_boolean(v); | |
23ade460 | 1272 | if (b < 0) |
15ec1021 | 1273 | log_debug("Failed to parse job irreversible flag: %s", v); |
23ade460 MS |
1274 | else |
1275 | j->irreversible = j->irreversible || b; | |
718db961 | 1276 | |
39a18c60 | 1277 | } else if (streq(l, "job-sent-dbus-new-signal")) { |
718db961 LP |
1278 | int b; |
1279 | ||
1280 | b = parse_boolean(v); | |
39a18c60 | 1281 | if (b < 0) |
15ec1021 | 1282 | log_debug("Failed to parse job sent_dbus_new_signal flag: %s", v); |
39a18c60 MS |
1283 | else |
1284 | j->sent_dbus_new_signal = j->sent_dbus_new_signal || b; | |
718db961 | 1285 | |
39a18c60 | 1286 | } else if (streq(l, "job-ignore-order")) { |
718db961 LP |
1287 | int b; |
1288 | ||
1289 | b = parse_boolean(v); | |
39a18c60 | 1290 | if (b < 0) |
15ec1021 | 1291 | log_debug("Failed to parse job ignore_order flag: %s", v); |
39a18c60 MS |
1292 | else |
1293 | j->ignore_order = j->ignore_order || b; | |
718db961 | 1294 | |
d68c645b LP |
1295 | } else if (streq(l, "job-begin")) |
1296 | (void) deserialize_usec(v, &j->begin_usec); | |
718db961 | 1297 | |
d68c645b LP |
1298 | else if (streq(l, "job-begin-running")) |
1299 | (void) deserialize_usec(v, &j->begin_running_usec); | |
718db961 | 1300 | |
d68c645b | 1301 | else if (streq(l, "subscribed")) { |
b39a2770 | 1302 | if (strv_extend(&j->deserialized_clients, v) < 0) |
d68c645b LP |
1303 | return log_oom(); |
1304 | } else | |
1305 | log_debug("Unknown job serialization key: %s", l); | |
39a18c60 MS |
1306 | } |
1307 | } | |
1308 | ||
1309 | int job_coldplug(Job *j) { | |
718db961 | 1310 | int r; |
171f12ce | 1311 | usec_t timeout_time = USEC_INFINITY; |
718db961 LP |
1312 | |
1313 | assert(j); | |
39a18c60 | 1314 | |
8f8f05a9 LP |
1315 | /* After deserialization is complete and the bus connection |
1316 | * set up again, let's start watching our subscribers again */ | |
c5a97ed1 | 1317 | (void) bus_job_coldplug_bus_track(j); |
8f8f05a9 | 1318 | |
1727a595 MM |
1319 | if (j->state == JOB_WAITING) |
1320 | job_add_to_run_queue(j); | |
1321 | ||
c5a97ed1 LP |
1322 | /* Maybe due to new dependencies we don't actually need this job anymore? */ |
1323 | job_add_to_gc_queue(j); | |
1324 | ||
171f12ce MK |
1325 | /* Create timer only when job began or began running and the respective timeout is finite. |
1326 | * Follow logic of job_start_timer() if both timeouts are finite */ | |
1327 | if (j->begin_usec == 0) | |
1328 | return 0; | |
1329 | ||
1330 | if (j->unit->job_timeout != USEC_INFINITY) | |
1331 | timeout_time = usec_add(j->begin_usec, j->unit->job_timeout); | |
1332 | ||
1333 | if (j->begin_running_usec > 0 && j->unit->job_running_timeout != USEC_INFINITY) | |
1334 | timeout_time = MIN(timeout_time, usec_add(j->begin_running_usec, j->unit->job_running_timeout)); | |
1335 | ||
1336 | if (timeout_time == USEC_INFINITY) | |
39a18c60 MS |
1337 | return 0; |
1338 | ||
36c16a7c | 1339 | j->timer_event_source = sd_event_source_unref(j->timer_event_source); |
39a18c60 | 1340 | |
6a0f1f6d LP |
1341 | r = sd_event_add_time( |
1342 | j->manager->event, | |
1343 | &j->timer_event_source, | |
1344 | CLOCK_MONOTONIC, | |
171f12ce | 1345 | timeout_time, 0, |
6a0f1f6d | 1346 | job_dispatch_timer, j); |
718db961 | 1347 | if (r < 0) |
da927ba9 | 1348 | log_debug_errno(r, "Failed to restart timeout for job: %m"); |
718db961 | 1349 | |
7dfbe2e3 TG |
1350 | (void) sd_event_source_set_description(j->timer_event_source, "job-timeout"); |
1351 | ||
718db961 | 1352 | return r; |
39a18c60 MS |
1353 | } |
1354 | ||
c65eb836 LP |
1355 | void job_shutdown_magic(Job *j) { |
1356 | assert(j); | |
1357 | ||
1358 | /* The shutdown target gets some special treatment here: we | |
1359 | * tell the kernel to begin with flushing its disk caches, to | |
1360 | * optimize shutdown time a bit. Ideally we wouldn't hardcode | |
1361 | * this magic into PID 1. However all other processes aren't | |
1362 | * options either since they'd exit much sooner than PID 1 and | |
1363 | * asynchronous sync() would cause their exit to be | |
1364 | * delayed. */ | |
1365 | ||
c2756a68 | 1366 | if (j->type != JOB_START) |
c65eb836 LP |
1367 | return; |
1368 | ||
463d0d15 | 1369 | if (!MANAGER_IS_SYSTEM(j->unit->manager)) |
c2756a68 LP |
1370 | return; |
1371 | ||
1372 | if (!unit_has_name(j->unit, SPECIAL_SHUTDOWN_TARGET)) | |
c65eb836 LP |
1373 | return; |
1374 | ||
5b1869ea OB |
1375 | /* In case messages on console has been disabled on boot */ |
1376 | j->unit->manager->no_console_output = false; | |
1377 | ||
75f86906 | 1378 | if (detect_container() > 0) |
c65eb836 LP |
1379 | return; |
1380 | ||
d00c2631 | 1381 | (void) asynchronous_sync(NULL); |
c65eb836 LP |
1382 | } |
1383 | ||
7a7821c8 LP |
1384 | int job_get_timeout(Job *j, usec_t *timeout) { |
1385 | usec_t x = USEC_INFINITY, y = USEC_INFINITY; | |
68db7a3b | 1386 | Unit *u = j->unit; |
7a7821c8 | 1387 | int r; |
68db7a3b ZJS |
1388 | |
1389 | assert(u); | |
1390 | ||
1391 | if (j->timer_event_source) { | |
1392 | r = sd_event_source_get_time(j->timer_event_source, &x); | |
1393 | if (r < 0) | |
1394 | return r; | |
68db7a3b ZJS |
1395 | } |
1396 | ||
1397 | if (UNIT_VTABLE(u)->get_timeout) { | |
7a7821c8 LP |
1398 | r = UNIT_VTABLE(u)->get_timeout(u, &y); |
1399 | if (r < 0) | |
1400 | return r; | |
68db7a3b ZJS |
1401 | } |
1402 | ||
7a7821c8 | 1403 | if (x == USEC_INFINITY && y == USEC_INFINITY) |
68db7a3b ZJS |
1404 | return 0; |
1405 | ||
1406 | *timeout = MIN(x, y); | |
68db7a3b ZJS |
1407 | return 1; |
1408 | } | |
1409 | ||
2ab3050f | 1410 | bool job_may_gc(Job *j) { |
c5a97ed1 LP |
1411 | Unit *other; |
1412 | Iterator i; | |
eef85c4a | 1413 | void *v; |
c5a97ed1 LP |
1414 | |
1415 | assert(j); | |
1416 | ||
1417 | /* Checks whether this job should be GC'ed away. We only do this for jobs of units that have no effect on their | |
2ab3050f ZJS |
1418 | * own and just track external state. For now the only unit type that qualifies for this are .device units. |
1419 | * Returns true if the job can be collected. */ | |
c5a97ed1 LP |
1420 | |
1421 | if (!UNIT_VTABLE(j->unit)->gc_jobs) | |
2ab3050f | 1422 | return false; |
c5a97ed1 LP |
1423 | |
1424 | if (sd_bus_track_count(j->bus_track) > 0) | |
2ab3050f | 1425 | return false; |
c5a97ed1 LP |
1426 | |
1427 | /* FIXME: So this is a bit ugly: for now we don't properly track references made via private bus connections | |
1428 | * (because it's nasty, as sd_bus_track doesn't apply to it). We simply remember that the job was once | |
1429 | * referenced by one, and reset this whenever we notice that no private bus connections are around. This means | |
1430 | * the GC is a bit too conservative when it comes to jobs created by private bus connections. */ | |
1431 | if (j->ref_by_private_bus) { | |
1432 | if (set_isempty(j->unit->manager->private_buses)) | |
1433 | j->ref_by_private_bus = false; | |
1434 | else | |
2ab3050f | 1435 | return false; |
c5a97ed1 LP |
1436 | } |
1437 | ||
1438 | if (j->type == JOB_NOP) | |
2ab3050f | 1439 | return false; |
c5a97ed1 LP |
1440 | |
1441 | /* If a job is ordered after ours, and is to be started, then it needs to wait for us, regardless if we stop or | |
1442 | * start, hence let's not GC in that case. */ | |
eef85c4a | 1443 | HASHMAP_FOREACH_KEY(v, other, j->unit->dependencies[UNIT_BEFORE], i) { |
c5a97ed1 LP |
1444 | if (!other->job) |
1445 | continue; | |
1446 | ||
1447 | if (other->job->ignore_order) | |
1448 | continue; | |
1449 | ||
1450 | if (IN_SET(other->job->type, JOB_START, JOB_VERIFY_ACTIVE, JOB_RELOAD)) | |
2ab3050f | 1451 | return false; |
c5a97ed1 LP |
1452 | } |
1453 | ||
047d7219 ZJS |
1454 | /* If we are going down, but something else is ordered After= us, then it needs to wait for us */ |
1455 | if (IN_SET(j->type, JOB_STOP, JOB_RESTART)) | |
eef85c4a | 1456 | HASHMAP_FOREACH_KEY(v, other, j->unit->dependencies[UNIT_AFTER], i) { |
c5a97ed1 LP |
1457 | if (!other->job) |
1458 | continue; | |
1459 | ||
1460 | if (other->job->ignore_order) | |
1461 | continue; | |
1462 | ||
2ab3050f | 1463 | return false; |
c5a97ed1 | 1464 | } |
c5a97ed1 LP |
1465 | |
1466 | /* The logic above is kinda the inverse of the job_is_runnable() logic. Specifically, if the job "we" is | |
1467 | * ordered before the job "other": | |
1468 | * | |
1469 | * we start + other start → stay | |
1470 | * we start + other stop → gc | |
1471 | * we stop + other start → stay | |
1472 | * we stop + other stop → gc | |
1473 | * | |
1474 | * "we" are ordered after "other": | |
1475 | * | |
1476 | * we start + other start → gc | |
1477 | * we start + other stop → gc | |
1478 | * we stop + other start → stay | |
1479 | * we stop + other stop → stay | |
c5a97ed1 LP |
1480 | */ |
1481 | ||
2ab3050f | 1482 | return true; |
c5a97ed1 LP |
1483 | } |
1484 | ||
1485 | void job_add_to_gc_queue(Job *j) { | |
1486 | assert(j); | |
1487 | ||
1488 | if (j->in_gc_queue) | |
1489 | return; | |
1490 | ||
2ab3050f | 1491 | if (!job_may_gc(j)) |
c5a97ed1 LP |
1492 | return; |
1493 | ||
1494 | LIST_PREPEND(gc_queue, j->unit->manager->gc_job_queue, j); | |
1495 | j->in_gc_queue = true; | |
1496 | } | |
1497 | ||
93bab288 YW |
1498 | static int job_compare(Job * const *a, Job * const *b) { |
1499 | return CMP((*a)->id, (*b)->id); | |
15ea79f8 LP |
1500 | } |
1501 | ||
1502 | static size_t sort_job_list(Job **list, size_t n) { | |
1503 | Job *previous = NULL; | |
1504 | size_t a, b; | |
1505 | ||
1506 | /* Order by numeric IDs */ | |
93bab288 | 1507 | typesafe_qsort(list, n, job_compare); |
15ea79f8 LP |
1508 | |
1509 | /* Filter out duplicates */ | |
1510 | for (a = 0, b = 0; a < n; a++) { | |
1511 | ||
1512 | if (previous == list[a]) | |
1513 | continue; | |
1514 | ||
1515 | previous = list[b++] = list[a]; | |
1516 | } | |
1517 | ||
1518 | return b; | |
1519 | } | |
1520 | ||
1521 | int job_get_before(Job *j, Job*** ret) { | |
1522 | _cleanup_free_ Job** list = NULL; | |
1523 | size_t n = 0, n_allocated = 0; | |
1524 | Unit *other = NULL; | |
1525 | Iterator i; | |
eef85c4a | 1526 | void *v; |
15ea79f8 LP |
1527 | |
1528 | /* Returns a list of all pending jobs that need to finish before this job may be started. */ | |
1529 | ||
1530 | assert(j); | |
1531 | assert(ret); | |
1532 | ||
1533 | if (j->ignore_order) { | |
1534 | *ret = NULL; | |
1535 | return 0; | |
1536 | } | |
1537 | ||
1538 | if (IN_SET(j->type, JOB_START, JOB_VERIFY_ACTIVE, JOB_RELOAD)) { | |
1539 | ||
eef85c4a | 1540 | HASHMAP_FOREACH_KEY(v, other, j->unit->dependencies[UNIT_AFTER], i) { |
15ea79f8 LP |
1541 | if (!other->job) |
1542 | continue; | |
1543 | ||
1544 | if (!GREEDY_REALLOC(list, n_allocated, n+1)) | |
1545 | return -ENOMEM; | |
1546 | list[n++] = other->job; | |
1547 | } | |
1548 | } | |
1549 | ||
eef85c4a | 1550 | HASHMAP_FOREACH_KEY(v, other, j->unit->dependencies[UNIT_BEFORE], i) { |
15ea79f8 LP |
1551 | if (!other->job) |
1552 | continue; | |
1553 | ||
1554 | if (!IN_SET(other->job->type, JOB_STOP, JOB_RESTART)) | |
1555 | continue; | |
1556 | ||
1557 | if (!GREEDY_REALLOC(list, n_allocated, n+1)) | |
1558 | return -ENOMEM; | |
1559 | list[n++] = other->job; | |
1560 | } | |
1561 | ||
1562 | n = sort_job_list(list, n); | |
1563 | ||
1cc6c93a | 1564 | *ret = TAKE_PTR(list); |
15ea79f8 LP |
1565 | |
1566 | return (int) n; | |
1567 | } | |
1568 | ||
1569 | int job_get_after(Job *j, Job*** ret) { | |
1570 | _cleanup_free_ Job** list = NULL; | |
1571 | size_t n = 0, n_allocated = 0; | |
1572 | Unit *other = NULL; | |
eef85c4a | 1573 | void *v; |
15ea79f8 LP |
1574 | Iterator i; |
1575 | ||
1576 | assert(j); | |
1577 | assert(ret); | |
1578 | ||
1579 | /* Returns a list of all pending jobs that are waiting for this job to finish. */ | |
1580 | ||
eef85c4a | 1581 | HASHMAP_FOREACH_KEY(v, other, j->unit->dependencies[UNIT_BEFORE], i) { |
15ea79f8 LP |
1582 | if (!other->job) |
1583 | continue; | |
1584 | ||
1585 | if (other->job->ignore_order) | |
1586 | continue; | |
1587 | ||
1588 | if (!IN_SET(other->job->type, JOB_START, JOB_VERIFY_ACTIVE, JOB_RELOAD)) | |
1589 | continue; | |
1590 | ||
1591 | if (!GREEDY_REALLOC(list, n_allocated, n+1)) | |
1592 | return -ENOMEM; | |
1593 | list[n++] = other->job; | |
1594 | } | |
1595 | ||
1596 | if (IN_SET(j->type, JOB_STOP, JOB_RESTART)) { | |
1597 | ||
eef85c4a | 1598 | HASHMAP_FOREACH_KEY(v, other, j->unit->dependencies[UNIT_AFTER], i) { |
15ea79f8 LP |
1599 | if (!other->job) |
1600 | continue; | |
1601 | ||
1602 | if (other->job->ignore_order) | |
1603 | continue; | |
1604 | ||
1605 | if (!GREEDY_REALLOC(list, n_allocated, n+1)) | |
1606 | return -ENOMEM; | |
1607 | list[n++] = other->job; | |
1608 | } | |
1609 | } | |
1610 | ||
1611 | n = sort_job_list(list, n); | |
1612 | ||
1cc6c93a | 1613 | *ret = TAKE_PTR(list); |
15ea79f8 LP |
1614 | |
1615 | return (int) n; | |
1616 | } | |
1617 | ||
94f04347 LP |
1618 | static const char* const job_state_table[_JOB_STATE_MAX] = { |
1619 | [JOB_WAITING] = "waiting", | |
0a23a627 | 1620 | [JOB_RUNNING] = "running", |
94f04347 LP |
1621 | }; |
1622 | ||
1623 | DEFINE_STRING_TABLE_LOOKUP(job_state, JobState); | |
1624 | ||
1625 | static const char* const job_type_table[_JOB_TYPE_MAX] = { | |
1626 | [JOB_START] = "start", | |
1627 | [JOB_VERIFY_ACTIVE] = "verify-active", | |
1628 | [JOB_STOP] = "stop", | |
1629 | [JOB_RELOAD] = "reload", | |
1630 | [JOB_RELOAD_OR_START] = "reload-or-start", | |
1631 | [JOB_RESTART] = "restart", | |
1632 | [JOB_TRY_RESTART] = "try-restart", | |
3282591d | 1633 | [JOB_TRY_RELOAD] = "try-reload", |
e0209d83 | 1634 | [JOB_NOP] = "nop", |
94f04347 LP |
1635 | }; |
1636 | ||
1637 | DEFINE_STRING_TABLE_LOOKUP(job_type, JobType); | |
b548631a LP |
1638 | |
1639 | static const char* const job_mode_table[_JOB_MODE_MAX] = { | |
1640 | [JOB_FAIL] = "fail", | |
c497c7a9 | 1641 | [JOB_REPLACE] = "replace", |
23ade460 | 1642 | [JOB_REPLACE_IRREVERSIBLY] = "replace-irreversibly", |
e67c3609 | 1643 | [JOB_ISOLATE] = "isolate", |
2c5859af | 1644 | [JOB_FLUSH] = "flush", |
cebe0d41 | 1645 | [JOB_IGNORE_DEPENDENCIES] = "ignore-dependencies", |
255baef6 | 1646 | [JOB_IGNORE_REQUIREMENTS] = "ignore-requirements", |
b548631a LP |
1647 | }; |
1648 | ||
1649 | DEFINE_STRING_TABLE_LOOKUP(job_mode, JobMode); | |
5d44db4a LP |
1650 | |
1651 | static const char* const job_result_table[_JOB_RESULT_MAX] = { | |
1652 | [JOB_DONE] = "done", | |
1653 | [JOB_CANCELED] = "canceled", | |
1654 | [JOB_TIMEOUT] = "timeout", | |
1655 | [JOB_FAILED] = "failed", | |
d68201e9 | 1656 | [JOB_DEPENDENCY] = "dependency", |
6a371e23 ZJS |
1657 | [JOB_SKIPPED] = "skipped", |
1658 | [JOB_INVALID] = "invalid", | |
59fccdc5 | 1659 | [JOB_ASSERT] = "assert", |
0faacd47 | 1660 | [JOB_UNSUPPORTED] = "unsupported", |
c5a97ed1 | 1661 | [JOB_COLLECTED] = "collected", |
d4fd1cf2 | 1662 | [JOB_ONCE] = "once", |
5d44db4a LP |
1663 | }; |
1664 | ||
1665 | DEFINE_STRING_TABLE_LOOKUP(job_result, JobResult); | |
94bd7323 EV |
1666 | |
1667 | const char* job_type_to_access_method(JobType t) { | |
1668 | assert(t >= 0); | |
1669 | assert(t < _JOB_TYPE_MAX); | |
1670 | ||
1671 | if (IN_SET(t, JOB_START, JOB_RESTART, JOB_TRY_RESTART)) | |
1672 | return "start"; | |
1673 | else if (t == JOB_STOP) | |
1674 | return "stop"; | |
1675 | else | |
1676 | return "reload"; | |
1677 | } |