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