]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/job.c
sd-event: instrument sd_event_run() for profiling delays
[thirdparty/systemd.git] / src / core / job.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <errno.h>
23
24 #include "sd-id128.h"
25 #include "sd-messages.h"
26
27 #include "alloc-util.h"
28 #include "async.h"
29 #include "dbus-job.h"
30 #include "dbus.h"
31 #include "escape.h"
32 #include "job.h"
33 #include "log.h"
34 #include "macro.h"
35 #include "parse-util.h"
36 #include "set.h"
37 #include "special.h"
38 #include "string-table.h"
39 #include "string-util.h"
40 #include "strv.h"
41 #include "terminal-util.h"
42 #include "unit.h"
43 #include "virt.h"
44
45 Job* job_new_raw(Unit *unit) {
46 Job *j;
47
48 /* used for deserialization */
49
50 assert(unit);
51
52 j = new0(Job, 1);
53 if (!j)
54 return NULL;
55
56 j->manager = unit->manager;
57 j->unit = unit;
58 j->type = _JOB_TYPE_INVALID;
59
60 return j;
61 }
62
63 Job* job_new(Unit *unit, JobType type) {
64 Job *j;
65
66 assert(type < _JOB_TYPE_MAX);
67
68 j = job_new_raw(unit);
69 if (!j)
70 return NULL;
71
72 j->id = j->manager->current_job_id++;
73 j->type = type;
74
75 /* We don't link it here, that's what job_dependency() is for */
76
77 return j;
78 }
79
80 void job_free(Job *j) {
81 assert(j);
82 assert(!j->installed);
83 assert(!j->transaction_prev);
84 assert(!j->transaction_next);
85 assert(!j->subject_list);
86 assert(!j->object_list);
87
88 if (j->in_run_queue)
89 LIST_REMOVE(run_queue, j->manager->run_queue, j);
90
91 if (j->in_dbus_queue)
92 LIST_REMOVE(dbus_queue, j->manager->dbus_job_queue, j);
93
94 sd_event_source_unref(j->timer_event_source);
95
96 sd_bus_track_unref(j->clients);
97 strv_free(j->deserialized_clients);
98
99 free(j);
100 }
101
102 static void job_set_state(Job *j, JobState state) {
103 assert(j);
104 assert(state >= 0);
105 assert(state < _JOB_STATE_MAX);
106
107 if (j->state == state)
108 return;
109
110 j->state = state;
111
112 if (!j->installed)
113 return;
114
115 if (j->state == JOB_RUNNING)
116 j->unit->manager->n_running_jobs++;
117 else {
118 assert(j->state == JOB_WAITING);
119 assert(j->unit->manager->n_running_jobs > 0);
120
121 j->unit->manager->n_running_jobs--;
122
123 if (j->unit->manager->n_running_jobs <= 0)
124 j->unit->manager->jobs_in_progress_event_source = sd_event_source_unref(j->unit->manager->jobs_in_progress_event_source);
125 }
126 }
127
128 void job_uninstall(Job *j) {
129 Job **pj;
130
131 assert(j->installed);
132
133 job_set_state(j, JOB_WAITING);
134
135 pj = (j->type == JOB_NOP) ? &j->unit->nop_job : &j->unit->job;
136 assert(*pj == j);
137
138 /* Detach from next 'bigger' objects */
139
140 /* daemon-reload should be transparent to job observers */
141 if (j->manager->n_reloading <= 0)
142 bus_job_send_removed_signal(j);
143
144 *pj = NULL;
145
146 unit_add_to_gc_queue(j->unit);
147
148 hashmap_remove(j->manager->jobs, UINT32_TO_PTR(j->id));
149 j->installed = false;
150 }
151
152 static bool job_type_allows_late_merge(JobType t) {
153 /* Tells whether it is OK to merge a job of type 't' with an already
154 * running job.
155 * Reloads cannot be merged this way. Think of the sequence:
156 * 1. Reload of a daemon is in progress; the daemon has already loaded
157 * its config file, but hasn't completed the reload operation yet.
158 * 2. Edit foo's config file.
159 * 3. Trigger another reload to have the daemon use the new config.
160 * Should the second reload job be merged into the first one, the daemon
161 * would not know about the new config.
162 * JOB_RESTART jobs on the other hand can be merged, because they get
163 * patched into JOB_START after stopping the unit. So if we see a
164 * JOB_RESTART running, it means the unit hasn't stopped yet and at
165 * this time the merge is still allowed. */
166 return t != JOB_RELOAD;
167 }
168
169 static void job_merge_into_installed(Job *j, Job *other) {
170 assert(j->installed);
171 assert(j->unit == other->unit);
172
173 if (j->type != JOB_NOP)
174 job_type_merge_and_collapse(&j->type, other->type, j->unit);
175 else
176 assert(other->type == JOB_NOP);
177
178 j->irreversible = j->irreversible || other->irreversible;
179 j->ignore_order = j->ignore_order || other->ignore_order;
180 }
181
182 Job* job_install(Job *j) {
183 Job **pj;
184 Job *uj;
185
186 assert(!j->installed);
187 assert(j->type < _JOB_TYPE_MAX_IN_TRANSACTION);
188 assert(j->state == JOB_WAITING);
189
190 pj = (j->type == JOB_NOP) ? &j->unit->nop_job : &j->unit->job;
191 uj = *pj;
192
193 if (uj) {
194 if (job_type_is_conflicting(uj->type, j->type))
195 job_finish_and_invalidate(uj, JOB_CANCELED, false);
196 else {
197 /* not conflicting, i.e. mergeable */
198
199 if (uj->state == JOB_WAITING ||
200 (job_type_allows_late_merge(j->type) && job_type_is_superset(uj->type, j->type))) {
201 job_merge_into_installed(uj, j);
202 log_unit_debug(uj->unit,
203 "Merged into installed job %s/%s as %u",
204 uj->unit->id, job_type_to_string(uj->type), (unsigned) uj->id);
205 return uj;
206 } else {
207 /* already running and not safe to merge into */
208 /* Patch uj to become a merged job and re-run it. */
209 /* XXX It should be safer to queue j to run after uj finishes, but it is
210 * not currently possible to have more than one installed job per unit. */
211 job_merge_into_installed(uj, j);
212 log_unit_debug(uj->unit,
213 "Merged into running job, re-running: %s/%s as %u",
214 uj->unit->id, job_type_to_string(uj->type), (unsigned) uj->id);
215
216 job_set_state(uj, JOB_WAITING);
217 return uj;
218 }
219 }
220 }
221
222 /* Install the job */
223 *pj = j;
224 j->installed = true;
225
226 j->manager->n_installed_jobs ++;
227 log_unit_debug(j->unit,
228 "Installed new job %s/%s as %u",
229 j->unit->id, job_type_to_string(j->type), (unsigned) j->id);
230 return j;
231 }
232
233 int job_install_deserialized(Job *j) {
234 Job **pj;
235
236 assert(!j->installed);
237
238 if (j->type < 0 || j->type >= _JOB_TYPE_MAX_IN_TRANSACTION) {
239 log_debug("Invalid job type %s in deserialization.", strna(job_type_to_string(j->type)));
240 return -EINVAL;
241 }
242
243 pj = (j->type == JOB_NOP) ? &j->unit->nop_job : &j->unit->job;
244 if (*pj) {
245 log_unit_debug(j->unit, "Unit already has a job installed. Not installing deserialized job.");
246 return -EEXIST;
247 }
248
249 *pj = j;
250 j->installed = true;
251
252 if (j->state == JOB_RUNNING)
253 j->unit->manager->n_running_jobs++;
254
255 log_unit_debug(j->unit,
256 "Reinstalled deserialized job %s/%s as %u",
257 j->unit->id, job_type_to_string(j->type), (unsigned) j->id);
258 return 0;
259 }
260
261 JobDependency* job_dependency_new(Job *subject, Job *object, bool matters, bool conflicts) {
262 JobDependency *l;
263
264 assert(object);
265
266 /* Adds a new job link, which encodes that the 'subject' job
267 * needs the 'object' job in some way. If 'subject' is NULL
268 * this means the 'anchor' job (i.e. the one the user
269 * explicitly asked for) is the requester. */
270
271 if (!(l = new0(JobDependency, 1)))
272 return NULL;
273
274 l->subject = subject;
275 l->object = object;
276 l->matters = matters;
277 l->conflicts = conflicts;
278
279 if (subject)
280 LIST_PREPEND(subject, subject->subject_list, l);
281
282 LIST_PREPEND(object, object->object_list, l);
283
284 return l;
285 }
286
287 void job_dependency_free(JobDependency *l) {
288 assert(l);
289
290 if (l->subject)
291 LIST_REMOVE(subject, l->subject->subject_list, l);
292
293 LIST_REMOVE(object, l->object->object_list, l);
294
295 free(l);
296 }
297
298 void job_dump(Job *j, FILE*f, const char *prefix) {
299 assert(j);
300 assert(f);
301
302 if (!prefix)
303 prefix = "";
304
305 fprintf(f,
306 "%s-> Job %u:\n"
307 "%s\tAction: %s -> %s\n"
308 "%s\tState: %s\n"
309 "%s\tIrreversible: %s\n",
310 prefix, j->id,
311 prefix, j->unit->id, job_type_to_string(j->type),
312 prefix, job_state_to_string(j->state),
313 prefix, yes_no(j->irreversible));
314 }
315
316 /*
317 * Merging is commutative, so imagine the matrix as symmetric. We store only
318 * its lower triangle to avoid duplication. We don't store the main diagonal,
319 * because A merged with A is simply A.
320 *
321 * If the resulting type is collapsed immediately afterwards (to get rid of
322 * the JOB_RELOAD_OR_START, which lies outside the lookup function's domain),
323 * the following properties hold:
324 *
325 * Merging is associative! A merged with B, and then merged with C is the same
326 * as A merged with the result of B merged with C.
327 *
328 * Mergeability is transitive! If A can be merged with B and B with C then
329 * A also with C.
330 *
331 * Also, if A merged with B cannot be merged with C, then either A or B cannot
332 * be merged with C either.
333 */
334 static const JobType job_merging_table[] = {
335 /* What \ With * JOB_START JOB_VERIFY_ACTIVE JOB_STOP JOB_RELOAD */
336 /*********************************************************************************/
337 /*JOB_START */
338 /*JOB_VERIFY_ACTIVE */ JOB_START,
339 /*JOB_STOP */ -1, -1,
340 /*JOB_RELOAD */ JOB_RELOAD_OR_START, JOB_RELOAD, -1,
341 /*JOB_RESTART */ JOB_RESTART, JOB_RESTART, -1, JOB_RESTART,
342 };
343
344 JobType job_type_lookup_merge(JobType a, JobType b) {
345 assert_cc(ELEMENTSOF(job_merging_table) == _JOB_TYPE_MAX_MERGING * (_JOB_TYPE_MAX_MERGING - 1) / 2);
346 assert(a >= 0 && a < _JOB_TYPE_MAX_MERGING);
347 assert(b >= 0 && b < _JOB_TYPE_MAX_MERGING);
348
349 if (a == b)
350 return a;
351
352 if (a < b) {
353 JobType tmp = a;
354 a = b;
355 b = tmp;
356 }
357
358 return job_merging_table[(a - 1) * a / 2 + b];
359 }
360
361 bool job_type_is_redundant(JobType a, UnitActiveState b) {
362 switch (a) {
363
364 case JOB_START:
365 return
366 b == UNIT_ACTIVE ||
367 b == UNIT_RELOADING;
368
369 case JOB_STOP:
370 return
371 b == UNIT_INACTIVE ||
372 b == UNIT_FAILED;
373
374 case JOB_VERIFY_ACTIVE:
375 return
376 b == UNIT_ACTIVE ||
377 b == UNIT_RELOADING;
378
379 case JOB_RELOAD:
380 return
381 b == UNIT_RELOADING;
382
383 case JOB_RESTART:
384 return
385 b == UNIT_ACTIVATING;
386
387 case JOB_NOP:
388 return true;
389
390 default:
391 assert_not_reached("Invalid job type");
392 }
393 }
394
395 JobType job_type_collapse(JobType t, Unit *u) {
396 UnitActiveState s;
397
398 switch (t) {
399
400 case JOB_TRY_RESTART:
401 s = unit_active_state(u);
402 if (UNIT_IS_INACTIVE_OR_DEACTIVATING(s))
403 return JOB_NOP;
404
405 return JOB_RESTART;
406
407 case JOB_RELOAD_OR_START:
408 s = unit_active_state(u);
409 if (UNIT_IS_INACTIVE_OR_DEACTIVATING(s))
410 return JOB_START;
411
412 return JOB_RELOAD;
413
414 default:
415 return t;
416 }
417 }
418
419 int job_type_merge_and_collapse(JobType *a, JobType b, Unit *u) {
420 JobType t;
421
422 t = job_type_lookup_merge(*a, b);
423 if (t < 0)
424 return -EEXIST;
425
426 *a = job_type_collapse(t, u);
427 return 0;
428 }
429
430 static bool job_is_runnable(Job *j) {
431 Iterator i;
432 Unit *other;
433
434 assert(j);
435 assert(j->installed);
436
437 /* Checks whether there is any job running for the units this
438 * job needs to be running after (in the case of a 'positive'
439 * job type) or before (in the case of a 'negative' job
440 * type. */
441
442 /* Note that unit types have a say in what is runnable,
443 * too. For example, if they return -EAGAIN from
444 * unit_start() they can indicate they are not
445 * runnable yet. */
446
447 /* First check if there is an override */
448 if (j->ignore_order)
449 return true;
450
451 if (j->type == JOB_NOP)
452 return true;
453
454 if (j->type == JOB_START ||
455 j->type == JOB_VERIFY_ACTIVE ||
456 j->type == JOB_RELOAD) {
457
458 /* Immediate result is that the job is or might be
459 * started. In this case let's wait for the
460 * dependencies, regardless whether they are
461 * starting or stopping something. */
462
463 SET_FOREACH(other, j->unit->dependencies[UNIT_AFTER], i)
464 if (other->job)
465 return false;
466 }
467
468 /* Also, if something else is being stopped and we should
469 * change state after it, then let's wait. */
470
471 SET_FOREACH(other, j->unit->dependencies[UNIT_BEFORE], i)
472 if (other->job &&
473 (other->job->type == JOB_STOP ||
474 other->job->type == JOB_RESTART))
475 return false;
476
477 /* This means that for a service a and a service b where b
478 * shall be started after a:
479 *
480 * start a + start b → 1st step start a, 2nd step start b
481 * start a + stop b → 1st step stop b, 2nd step start a
482 * stop a + start b → 1st step stop a, 2nd step start b
483 * stop a + stop b → 1st step stop b, 2nd step stop a
484 *
485 * This has the side effect that restarts are properly
486 * synchronized too. */
487
488 return true;
489 }
490
491 static void job_change_type(Job *j, JobType newtype) {
492 assert(j);
493
494 log_unit_debug(j->unit,
495 "Converting job %s/%s -> %s/%s",
496 j->unit->id, job_type_to_string(j->type),
497 j->unit->id, job_type_to_string(newtype));
498
499 j->type = newtype;
500 }
501
502 static int job_perform_on_unit(Job **j) {
503 uint32_t id;
504 Manager *m;
505 JobType t;
506 Unit *u;
507 int r;
508
509 /* While we execute this operation the job might go away (for
510 * example: because it finishes immediately or is replaced by
511 * a new, conflicting job.) To make sure we don't access a
512 * freed job later on we store the id here, so that we can
513 * verify the job is still valid. */
514
515 assert(j);
516 assert(*j);
517
518 m = (*j)->manager;
519 u = (*j)->unit;
520 t = (*j)->type;
521 id = (*j)->id;
522
523 switch (t) {
524 case JOB_START:
525 r = unit_start(u);
526 break;
527
528 case JOB_RESTART:
529 t = JOB_STOP;
530 /* fall through */
531 case JOB_STOP:
532 r = unit_stop(u);
533 break;
534
535 case JOB_RELOAD:
536 r = unit_reload(u);
537 break;
538
539 default:
540 assert_not_reached("Invalid job type");
541 }
542
543 /* Log if the job still exists and the start/stop/reload function
544 * actually did something. */
545 *j = manager_get_job(m, id);
546 if (*j && r > 0)
547 unit_status_emit_starting_stopping_reloading(u, t);
548
549 return r;
550 }
551
552 int job_run_and_invalidate(Job *j) {
553 int r;
554
555 assert(j);
556 assert(j->installed);
557 assert(j->type < _JOB_TYPE_MAX_IN_TRANSACTION);
558 assert(j->in_run_queue);
559
560 LIST_REMOVE(run_queue, j->manager->run_queue, j);
561 j->in_run_queue = false;
562
563 if (j->state != JOB_WAITING)
564 return 0;
565
566 if (!job_is_runnable(j))
567 return -EAGAIN;
568
569 job_set_state(j, JOB_RUNNING);
570 job_add_to_dbus_queue(j);
571
572
573 switch (j->type) {
574
575 case JOB_VERIFY_ACTIVE: {
576 UnitActiveState t = unit_active_state(j->unit);
577 if (UNIT_IS_ACTIVE_OR_RELOADING(t))
578 r = -EALREADY;
579 else if (t == UNIT_ACTIVATING)
580 r = -EAGAIN;
581 else
582 r = -EBADR;
583 break;
584 }
585
586 case JOB_START:
587 case JOB_STOP:
588 case JOB_RESTART:
589 r = job_perform_on_unit(&j);
590
591 /* If the unit type does not support starting/stopping,
592 * then simply wait. */
593 if (r == -EBADR)
594 r = 0;
595 break;
596
597 case JOB_RELOAD:
598 r = job_perform_on_unit(&j);
599 break;
600
601 case JOB_NOP:
602 r = -EALREADY;
603 break;
604
605 default:
606 assert_not_reached("Unknown job type");
607 }
608
609 if (j) {
610 if (r == -EALREADY)
611 r = job_finish_and_invalidate(j, JOB_DONE, true);
612 else if (r == -EBADR)
613 r = job_finish_and_invalidate(j, JOB_SKIPPED, true);
614 else if (r == -ENOEXEC)
615 r = job_finish_and_invalidate(j, JOB_INVALID, true);
616 else if (r == -EPROTO)
617 r = job_finish_and_invalidate(j, JOB_ASSERT, true);
618 else if (r == -EOPNOTSUPP)
619 r = job_finish_and_invalidate(j, JOB_UNSUPPORTED, true);
620 else if (r == -EAGAIN)
621 job_set_state(j, JOB_WAITING);
622 else if (r < 0)
623 r = job_finish_and_invalidate(j, JOB_FAILED, true);
624 }
625
626 return r;
627 }
628
629 _pure_ static const char *job_get_status_message_format(Unit *u, JobType t, JobResult result) {
630
631 static const char *const generic_finished_start_job[_JOB_RESULT_MAX] = {
632 [JOB_DONE] = "Started %s.",
633 [JOB_TIMEOUT] = "Timed out starting %s.",
634 [JOB_FAILED] = "Failed to start %s.",
635 [JOB_DEPENDENCY] = "Dependency failed for %s.",
636 [JOB_ASSERT] = "Assertion failed for %s.",
637 [JOB_UNSUPPORTED] = "Starting of %s not supported.",
638 };
639 static const char *const generic_finished_stop_job[_JOB_RESULT_MAX] = {
640 [JOB_DONE] = "Stopped %s.",
641 [JOB_FAILED] = "Stopped (with error) %s.",
642 [JOB_TIMEOUT] = "Timed out stoppping %s.",
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.",
648 };
649 /* When verify-active detects the unit is inactive, report it.
650 * Most likely a DEPEND warning from a requisiting unit will
651 * occur next and it's nice to see what was requisited. */
652 static const char *const generic_finished_verify_active_job[_JOB_RESULT_MAX] = {
653 [JOB_SKIPPED] = "%s is not active.",
654 };
655
656 const UnitStatusMessageFormats *format_table;
657 const char *format;
658
659 assert(u);
660 assert(t >= 0);
661 assert(t < _JOB_TYPE_MAX);
662
663 if (IN_SET(t, JOB_START, JOB_STOP, JOB_RESTART)) {
664 format_table = &UNIT_VTABLE(u)->status_message_formats;
665 if (format_table) {
666 format = t == JOB_START ? format_table->finished_start_job[result] :
667 format_table->finished_stop_job[result];
668 if (format)
669 return format;
670 }
671 }
672
673 /* Return generic strings */
674 if (t == JOB_START)
675 return generic_finished_start_job[result];
676 else if (t == JOB_STOP || t == JOB_RESTART)
677 return generic_finished_stop_job[result];
678 else if (t == JOB_RELOAD)
679 return generic_finished_reload_job[result];
680 else if (t == JOB_VERIFY_ACTIVE)
681 return generic_finished_verify_active_job[result];
682
683 return NULL;
684 }
685
686 static void job_print_status_message(Unit *u, JobType t, JobResult result) {
687 static const char* const job_result_status_table[_JOB_RESULT_MAX] = {
688 [JOB_DONE] = ANSI_GREEN " OK " ANSI_NORMAL,
689 [JOB_TIMEOUT] = ANSI_HIGHLIGHT_RED " TIME " ANSI_NORMAL,
690 [JOB_FAILED] = ANSI_HIGHLIGHT_RED "FAILED" ANSI_NORMAL,
691 [JOB_DEPENDENCY] = ANSI_HIGHLIGHT_YELLOW "DEPEND" ANSI_NORMAL,
692 [JOB_SKIPPED] = ANSI_HIGHLIGHT " INFO " ANSI_NORMAL,
693 [JOB_ASSERT] = ANSI_HIGHLIGHT_YELLOW "ASSERT" ANSI_NORMAL,
694 [JOB_UNSUPPORTED] = ANSI_HIGHLIGHT_YELLOW "UNSUPP" ANSI_NORMAL,
695 };
696
697 const char *format;
698
699 assert(u);
700 assert(t >= 0);
701 assert(t < _JOB_TYPE_MAX);
702
703 /* Reload status messages have traditionally not been printed to console. */
704 if (t == JOB_RELOAD)
705 return;
706
707 format = job_get_status_message_format(u, t, result);
708 if (!format)
709 return;
710
711 if (result != JOB_DONE)
712 manager_flip_auto_status(u->manager, true);
713
714 DISABLE_WARNING_FORMAT_NONLITERAL;
715 unit_status_printf(u, job_result_status_table[result], format);
716 REENABLE_WARNING;
717
718 if (t == JOB_START && result == JOB_FAILED) {
719 _cleanup_free_ char *quoted;
720
721 quoted = shell_maybe_quote(u->id);
722 manager_status_printf(u->manager, STATUS_TYPE_NORMAL, NULL, "See 'systemctl status %s' for details.", strna(quoted));
723 }
724 }
725
726 static void job_log_status_message(Unit *u, JobType t, JobResult result) {
727 const char *format;
728 char buf[LINE_MAX];
729 sd_id128_t mid;
730 static const int job_result_log_level[_JOB_RESULT_MAX] = {
731 [JOB_DONE] = LOG_INFO,
732 [JOB_CANCELED] = LOG_INFO,
733 [JOB_TIMEOUT] = LOG_ERR,
734 [JOB_FAILED] = LOG_ERR,
735 [JOB_DEPENDENCY] = LOG_WARNING,
736 [JOB_SKIPPED] = LOG_NOTICE,
737 [JOB_INVALID] = LOG_INFO,
738 [JOB_ASSERT] = LOG_WARNING,
739 [JOB_UNSUPPORTED] = LOG_WARNING,
740 };
741
742 assert(u);
743 assert(t >= 0);
744 assert(t < _JOB_TYPE_MAX);
745
746 /* Skip this if it goes to the console. since we already print
747 * to the console anyway... */
748
749 if (log_on_console())
750 return;
751
752 format = job_get_status_message_format(u, t, result);
753 if (!format)
754 return;
755
756 DISABLE_WARNING_FORMAT_NONLITERAL;
757 snprintf(buf, sizeof(buf), format, unit_description(u));
758 REENABLE_WARNING;
759
760 switch (t) {
761
762 case JOB_START:
763 mid = result == JOB_DONE ? SD_MESSAGE_UNIT_STARTED : SD_MESSAGE_UNIT_FAILED;
764 break;
765
766 case JOB_RELOAD:
767 mid = SD_MESSAGE_UNIT_RELOADED;
768 break;
769
770 case JOB_STOP:
771 case JOB_RESTART:
772 mid = SD_MESSAGE_UNIT_STOPPED;
773 break;
774
775 default:
776 log_struct(job_result_log_level[result],
777 LOG_UNIT_ID(u),
778 LOG_MESSAGE("%s", buf),
779 "RESULT=%s", job_result_to_string(result),
780 NULL);
781 return;
782 }
783
784 log_struct(job_result_log_level[result],
785 LOG_MESSAGE_ID(mid),
786 LOG_UNIT_ID(u),
787 LOG_MESSAGE("%s", buf),
788 "RESULT=%s", job_result_to_string(result),
789 NULL);
790 }
791
792 static void job_emit_status_message(Unit *u, JobType t, JobResult result) {
793
794 /* No message if the job did not actually do anything due to failed condition. */
795 if (t == JOB_START && result == JOB_DONE && !u->condition_result)
796 return;
797
798 job_log_status_message(u, t, result);
799 job_print_status_message(u, t, result);
800 }
801
802 static void job_fail_dependencies(Unit *u, UnitDependency d) {
803 Unit *other;
804 Iterator i;
805
806 assert(u);
807
808 SET_FOREACH(other, u->dependencies[d], i) {
809 Job *j = other->job;
810
811 if (!j)
812 continue;
813 if (!IN_SET(j->type, JOB_START, JOB_VERIFY_ACTIVE))
814 continue;
815
816 job_finish_and_invalidate(j, JOB_DEPENDENCY, true);
817 }
818 }
819
820 int job_finish_and_invalidate(Job *j, JobResult result, bool recursive) {
821 Unit *u;
822 Unit *other;
823 JobType t;
824 Iterator i;
825
826 assert(j);
827 assert(j->installed);
828 assert(j->type < _JOB_TYPE_MAX_IN_TRANSACTION);
829
830 u = j->unit;
831 t = j->type;
832
833 j->result = result;
834
835 log_unit_debug(u, "Job %s/%s finished, result=%s", u->id, job_type_to_string(t), job_result_to_string(result));
836
837 job_emit_status_message(u, t, result);
838
839 job_add_to_dbus_queue(j);
840
841 /* Patch restart jobs so that they become normal start jobs */
842 if (result == JOB_DONE && t == JOB_RESTART) {
843
844 job_change_type(j, JOB_START);
845 job_set_state(j, JOB_WAITING);
846
847 job_add_to_run_queue(j);
848
849 goto finish;
850 }
851
852 if (result == JOB_FAILED || result == JOB_INVALID)
853 j->manager->n_failed_jobs ++;
854
855 job_uninstall(j);
856 job_free(j);
857
858 /* Fail depending jobs on failure */
859 if (result != JOB_DONE && recursive) {
860 if (IN_SET(t, JOB_START, JOB_VERIFY_ACTIVE)) {
861 job_fail_dependencies(u, UNIT_REQUIRED_BY);
862 job_fail_dependencies(u, UNIT_REQUISITE_OF);
863 job_fail_dependencies(u, UNIT_BOUND_BY);
864 } else if (t == JOB_STOP)
865 job_fail_dependencies(u, UNIT_CONFLICTED_BY);
866 }
867
868 /* Trigger OnFailure dependencies that are not generated by
869 * the unit itself. We don't treat JOB_CANCELED as failure in
870 * this context. And JOB_FAILURE is already handled by the
871 * unit itself. */
872 if (result == JOB_TIMEOUT || result == JOB_DEPENDENCY) {
873 log_struct(LOG_NOTICE,
874 "JOB_TYPE=%s", job_type_to_string(t),
875 "JOB_RESULT=%s", job_result_to_string(result),
876 LOG_UNIT_ID(u),
877 LOG_UNIT_MESSAGE(u, "Job %s/%s failed with result '%s'.",
878 u->id,
879 job_type_to_string(t),
880 job_result_to_string(result)),
881 NULL);
882
883 unit_start_on_failure(u);
884 }
885
886 unit_trigger_notify(u);
887
888 finish:
889 /* Try to start the next jobs that can be started */
890 SET_FOREACH(other, u->dependencies[UNIT_AFTER], i)
891 if (other->job)
892 job_add_to_run_queue(other->job);
893 SET_FOREACH(other, u->dependencies[UNIT_BEFORE], i)
894 if (other->job)
895 job_add_to_run_queue(other->job);
896
897 manager_check_finished(u->manager);
898
899 return 0;
900 }
901
902 static int job_dispatch_timer(sd_event_source *s, uint64_t monotonic, void *userdata) {
903 Job *j = userdata;
904 Unit *u;
905
906 assert(j);
907 assert(s == j->timer_event_source);
908
909 log_unit_warning(j->unit, "Job %s/%s timed out.", j->unit->id, job_type_to_string(j->type));
910
911 u = j->unit;
912 job_finish_and_invalidate(j, JOB_TIMEOUT, true);
913
914 failure_action(u->manager, u->job_timeout_action, u->job_timeout_reboot_arg);
915
916 return 0;
917 }
918
919 int job_start_timer(Job *j) {
920 int r;
921
922 if (j->timer_event_source)
923 return 0;
924
925 j->begin_usec = now(CLOCK_MONOTONIC);
926
927 if (j->unit->job_timeout <= 0)
928 return 0;
929
930 r = sd_event_add_time(
931 j->manager->event,
932 &j->timer_event_source,
933 CLOCK_MONOTONIC,
934 j->begin_usec + j->unit->job_timeout, 0,
935 job_dispatch_timer, j);
936 if (r < 0)
937 return r;
938
939 (void) sd_event_source_set_description(j->timer_event_source, "job-start");
940
941 return 0;
942 }
943
944 void job_add_to_run_queue(Job *j) {
945 assert(j);
946 assert(j->installed);
947
948 if (j->in_run_queue)
949 return;
950
951 if (!j->manager->run_queue)
952 sd_event_source_set_enabled(j->manager->run_queue_event_source, SD_EVENT_ONESHOT);
953
954 LIST_PREPEND(run_queue, j->manager->run_queue, j);
955 j->in_run_queue = true;
956 }
957
958 void job_add_to_dbus_queue(Job *j) {
959 assert(j);
960 assert(j->installed);
961
962 if (j->in_dbus_queue)
963 return;
964
965 /* We don't check if anybody is subscribed here, since this
966 * job might just have been created and not yet assigned to a
967 * connection/client. */
968
969 LIST_PREPEND(dbus_queue, j->manager->dbus_job_queue, j);
970 j->in_dbus_queue = true;
971 }
972
973 char *job_dbus_path(Job *j) {
974 char *p;
975
976 assert(j);
977
978 if (asprintf(&p, "/org/freedesktop/systemd1/job/%"PRIu32, j->id) < 0)
979 return NULL;
980
981 return p;
982 }
983
984 int job_serialize(Job *j, FILE *f, FDSet *fds) {
985 fprintf(f, "job-id=%u\n", j->id);
986 fprintf(f, "job-type=%s\n", job_type_to_string(j->type));
987 fprintf(f, "job-state=%s\n", job_state_to_string(j->state));
988 fprintf(f, "job-irreversible=%s\n", yes_no(j->irreversible));
989 fprintf(f, "job-sent-dbus-new-signal=%s\n", yes_no(j->sent_dbus_new_signal));
990 fprintf(f, "job-ignore-order=%s\n", yes_no(j->ignore_order));
991
992 if (j->begin_usec > 0)
993 fprintf(f, "job-begin="USEC_FMT"\n", j->begin_usec);
994
995 bus_track_serialize(j->clients, f);
996
997 /* End marker */
998 fputc('\n', f);
999 return 0;
1000 }
1001
1002 int job_deserialize(Job *j, FILE *f, FDSet *fds) {
1003 assert(j);
1004
1005 for (;;) {
1006 char line[LINE_MAX], *l, *v;
1007 size_t k;
1008
1009 if (!fgets(line, sizeof(line), f)) {
1010 if (feof(f))
1011 return 0;
1012 return -errno;
1013 }
1014
1015 char_array_0(line);
1016 l = strstrip(line);
1017
1018 /* End marker */
1019 if (l[0] == 0)
1020 return 0;
1021
1022 k = strcspn(l, "=");
1023
1024 if (l[k] == '=') {
1025 l[k] = 0;
1026 v = l+k+1;
1027 } else
1028 v = l+k;
1029
1030 if (streq(l, "job-id")) {
1031
1032 if (safe_atou32(v, &j->id) < 0)
1033 log_debug("Failed to parse job id value %s", v);
1034
1035 } else if (streq(l, "job-type")) {
1036 JobType t;
1037
1038 t = job_type_from_string(v);
1039 if (t < 0)
1040 log_debug("Failed to parse job type %s", v);
1041 else if (t >= _JOB_TYPE_MAX_IN_TRANSACTION)
1042 log_debug("Cannot deserialize job of type %s", v);
1043 else
1044 j->type = t;
1045
1046 } else if (streq(l, "job-state")) {
1047 JobState s;
1048
1049 s = job_state_from_string(v);
1050 if (s < 0)
1051 log_debug("Failed to parse job state %s", v);
1052 else
1053 job_set_state(j, s);
1054
1055 } else if (streq(l, "job-irreversible")) {
1056 int b;
1057
1058 b = parse_boolean(v);
1059 if (b < 0)
1060 log_debug("Failed to parse job irreversible flag %s", v);
1061 else
1062 j->irreversible = j->irreversible || b;
1063
1064 } else if (streq(l, "job-sent-dbus-new-signal")) {
1065 int b;
1066
1067 b = parse_boolean(v);
1068 if (b < 0)
1069 log_debug("Failed to parse job sent_dbus_new_signal flag %s", v);
1070 else
1071 j->sent_dbus_new_signal = j->sent_dbus_new_signal || b;
1072
1073 } else if (streq(l, "job-ignore-order")) {
1074 int b;
1075
1076 b = parse_boolean(v);
1077 if (b < 0)
1078 log_debug("Failed to parse job ignore_order flag %s", v);
1079 else
1080 j->ignore_order = j->ignore_order || b;
1081
1082 } else if (streq(l, "job-begin")) {
1083 unsigned long long ull;
1084
1085 if (sscanf(v, "%llu", &ull) != 1)
1086 log_debug("Failed to parse job-begin value %s", v);
1087 else
1088 j->begin_usec = ull;
1089
1090 } else if (streq(l, "subscribed")) {
1091
1092 if (strv_extend(&j->deserialized_clients, v) < 0)
1093 return log_oom();
1094 }
1095 }
1096 }
1097
1098 int job_coldplug(Job *j) {
1099 int r;
1100
1101 assert(j);
1102
1103 /* After deserialization is complete and the bus connection
1104 * set up again, let's start watching our subscribers again */
1105 r = bus_track_coldplug(j->manager, &j->clients, &j->deserialized_clients);
1106 if (r < 0)
1107 return r;
1108
1109 if (j->state == JOB_WAITING)
1110 job_add_to_run_queue(j);
1111
1112 if (j->begin_usec == 0 || j->unit->job_timeout == 0)
1113 return 0;
1114
1115 if (j->timer_event_source)
1116 j->timer_event_source = sd_event_source_unref(j->timer_event_source);
1117
1118 r = sd_event_add_time(
1119 j->manager->event,
1120 &j->timer_event_source,
1121 CLOCK_MONOTONIC,
1122 j->begin_usec + j->unit->job_timeout, 0,
1123 job_dispatch_timer, j);
1124 if (r < 0)
1125 log_debug_errno(r, "Failed to restart timeout for job: %m");
1126
1127 (void) sd_event_source_set_description(j->timer_event_source, "job-timeout");
1128
1129 return r;
1130 }
1131
1132 void job_shutdown_magic(Job *j) {
1133 assert(j);
1134
1135 /* The shutdown target gets some special treatment here: we
1136 * tell the kernel to begin with flushing its disk caches, to
1137 * optimize shutdown time a bit. Ideally we wouldn't hardcode
1138 * this magic into PID 1. However all other processes aren't
1139 * options either since they'd exit much sooner than PID 1 and
1140 * asynchronous sync() would cause their exit to be
1141 * delayed. */
1142
1143 if (j->type != JOB_START)
1144 return;
1145
1146 if (j->unit->manager->running_as != MANAGER_SYSTEM)
1147 return;
1148
1149 if (!unit_has_name(j->unit, SPECIAL_SHUTDOWN_TARGET))
1150 return;
1151
1152 /* In case messages on console has been disabled on boot */
1153 j->unit->manager->no_console_output = false;
1154
1155 if (detect_container() > 0)
1156 return;
1157
1158 asynchronous_sync();
1159 }
1160
1161 int job_get_timeout(Job *j, uint64_t *timeout) {
1162 Unit *u = j->unit;
1163 uint64_t x = -1, y = -1;
1164 int r = 0, q = 0;
1165
1166 assert(u);
1167
1168 if (j->timer_event_source) {
1169 r = sd_event_source_get_time(j->timer_event_source, &x);
1170 if (r < 0)
1171 return r;
1172 r = 1;
1173 }
1174
1175 if (UNIT_VTABLE(u)->get_timeout) {
1176 q = UNIT_VTABLE(u)->get_timeout(u, &y);
1177 if (q < 0)
1178 return q;
1179 }
1180
1181 if (r == 0 && q == 0)
1182 return 0;
1183
1184 *timeout = MIN(x, y);
1185
1186 return 1;
1187 }
1188
1189 static const char* const job_state_table[_JOB_STATE_MAX] = {
1190 [JOB_WAITING] = "waiting",
1191 [JOB_RUNNING] = "running"
1192 };
1193
1194 DEFINE_STRING_TABLE_LOOKUP(job_state, JobState);
1195
1196 static const char* const job_type_table[_JOB_TYPE_MAX] = {
1197 [JOB_START] = "start",
1198 [JOB_VERIFY_ACTIVE] = "verify-active",
1199 [JOB_STOP] = "stop",
1200 [JOB_RELOAD] = "reload",
1201 [JOB_RELOAD_OR_START] = "reload-or-start",
1202 [JOB_RESTART] = "restart",
1203 [JOB_TRY_RESTART] = "try-restart",
1204 [JOB_NOP] = "nop",
1205 };
1206
1207 DEFINE_STRING_TABLE_LOOKUP(job_type, JobType);
1208
1209 static const char* const job_mode_table[_JOB_MODE_MAX] = {
1210 [JOB_FAIL] = "fail",
1211 [JOB_REPLACE] = "replace",
1212 [JOB_REPLACE_IRREVERSIBLY] = "replace-irreversibly",
1213 [JOB_ISOLATE] = "isolate",
1214 [JOB_FLUSH] = "flush",
1215 [JOB_IGNORE_DEPENDENCIES] = "ignore-dependencies",
1216 [JOB_IGNORE_REQUIREMENTS] = "ignore-requirements",
1217 };
1218
1219 DEFINE_STRING_TABLE_LOOKUP(job_mode, JobMode);
1220
1221 static const char* const job_result_table[_JOB_RESULT_MAX] = {
1222 [JOB_DONE] = "done",
1223 [JOB_CANCELED] = "canceled",
1224 [JOB_TIMEOUT] = "timeout",
1225 [JOB_FAILED] = "failed",
1226 [JOB_DEPENDENCY] = "dependency",
1227 [JOB_SKIPPED] = "skipped",
1228 [JOB_INVALID] = "invalid",
1229 [JOB_ASSERT] = "assert",
1230 [JOB_UNSUPPORTED] = "unsupported",
1231 };
1232
1233 DEFINE_STRING_TABLE_LOOKUP(job_result, JobResult);