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