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