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