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