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