chiark / gitweb /
manager: when breaking ordering cycle show full cycle loop
[elogind.git] / src / job.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
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 General Public License as published by
10   the Free Software Foundation; either version 2 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   General Public License for more details.
17
18   You should have received a copy of the GNU 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 "set.h"
28 #include "unit.h"
29 #include "macro.h"
30 #include "strv.h"
31 #include "load-fragment.h"
32 #include "load-dropin.h"
33 #include "log.h"
34 #include "dbus-job.h"
35
36 Job* job_new(Manager *m, JobType type, Unit *unit) {
37         Job *j;
38
39         assert(m);
40         assert(type < _JOB_TYPE_MAX);
41         assert(unit);
42
43         if (!(j = new0(Job, 1)))
44                 return NULL;
45
46         j->manager = m;
47         j->id = m->current_job_id++;
48         j->type = type;
49         j->unit = unit;
50
51         j->timer_watch.type = WATCH_INVALID;
52
53         /* We don't link it here, that's what job_dependency() is for */
54
55         return j;
56 }
57
58 void job_free(Job *j) {
59         assert(j);
60
61         /* Detach from next 'bigger' objects */
62         if (j->installed) {
63                 bus_job_send_removed_signal(j, !j->failed);
64
65                 if (j->unit->meta.job == j) {
66                         j->unit->meta.job = NULL;
67                         unit_add_to_gc_queue(j->unit);
68                 }
69
70                 hashmap_remove(j->manager->jobs, UINT32_TO_PTR(j->id));
71                 j->installed = false;
72         }
73
74         /* Detach from next 'smaller' objects */
75         manager_transaction_unlink_job(j->manager, j, true);
76
77         if (j->in_run_queue)
78                 LIST_REMOVE(Job, run_queue, j->manager->run_queue, j);
79
80         if (j->in_dbus_queue)
81                 LIST_REMOVE(Job, dbus_queue, j->manager->dbus_job_queue, j);
82
83         if (j->timer_watch.type != WATCH_INVALID) {
84                 assert(j->timer_watch.type == WATCH_JOB_TIMER);
85                 assert(j->timer_watch.data.job == j);
86                 assert(j->timer_watch.fd >= 0);
87
88                 assert_se(epoll_ctl(j->manager->epoll_fd, EPOLL_CTL_DEL, j->timer_watch.fd, NULL) >= 0);
89                 close_nointr_nofail(j->timer_watch.fd);
90         }
91
92         free(j->bus_client);
93         free(j);
94 }
95
96 JobDependency* job_dependency_new(Job *subject, Job *object, bool matters) {
97         JobDependency *l;
98
99         assert(object);
100
101         /* Adds a new job link, which encodes that the 'subject' job
102          * needs the 'object' job in some way. If 'subject' is NULL
103          * this means the 'anchor' job (i.e. the one the user
104          * explcitily asked for) is the requester. */
105
106         if (!(l = new0(JobDependency, 1)))
107                 return NULL;
108
109         l->subject = subject;
110         l->object = object;
111         l->matters = matters;
112
113         if (subject)
114                 LIST_PREPEND(JobDependency, subject, subject->subject_list, l);
115         else
116                 LIST_PREPEND(JobDependency, subject, object->manager->transaction_anchor, l);
117
118         LIST_PREPEND(JobDependency, object, object->object_list, l);
119
120         return l;
121 }
122
123 void job_dependency_free(JobDependency *l) {
124         assert(l);
125
126         if (l->subject)
127                 LIST_REMOVE(JobDependency, subject, l->subject->subject_list, l);
128         else
129                 LIST_REMOVE(JobDependency, subject, l->object->manager->transaction_anchor, l);
130
131         LIST_REMOVE(JobDependency, object, l->object->object_list, l);
132
133         free(l);
134 }
135
136 void job_dependency_delete(Job *subject, Job *object, bool *matters) {
137         JobDependency *l;
138
139         assert(object);
140
141         LIST_FOREACH(object, l, object->object_list) {
142                 assert(l->object == object);
143
144                 if (l->subject == subject)
145                         break;
146         }
147
148         if (!l) {
149                 if (matters)
150                         *matters = false;
151                 return;
152         }
153
154         if (matters)
155                 *matters = l->matters;
156
157         job_dependency_free(l);
158 }
159
160 void job_dump(Job *j, FILE*f, const char *prefix) {
161         assert(j);
162         assert(f);
163
164         if (!prefix)
165                 prefix = "";
166
167         fprintf(f,
168                 "%s-> Job %u:\n"
169                 "%s\tAction: %s -> %s\n"
170                 "%s\tState: %s\n"
171                 "%s\tForced: %s\n",
172                 prefix, j->id,
173                 prefix, j->unit->meta.id, job_type_to_string(j->type),
174                 prefix, job_state_to_string(j->state),
175                 prefix, yes_no(j->override));
176 }
177
178 bool job_is_anchor(Job *j) {
179         JobDependency *l;
180
181         assert(j);
182
183         LIST_FOREACH(object, l, j->object_list)
184                 if (!l->subject)
185                         return true;
186
187         return false;
188 }
189
190 static bool types_match(JobType a, JobType b, JobType c, JobType d) {
191         return
192                 (a == c && b == d) ||
193                 (a == d && b == c);
194 }
195
196 int job_type_merge(JobType *a, JobType b) {
197         if (*a == b)
198                 return 0;
199
200         /* Merging is associative! a merged with b merged with c is
201          * the same as a merged with c merged with b. */
202
203         /* Mergeability is transitive! if a can be merged with b and b
204          * with c then a also with c */
205
206         /* Also, if a merged with b cannot be merged with c, then
207          * either a or b cannot be merged with c either */
208
209         if (types_match(*a, b, JOB_START, JOB_VERIFY_ACTIVE))
210                 *a = JOB_START;
211         else if (types_match(*a, b, JOB_START, JOB_RELOAD) ||
212                  types_match(*a, b, JOB_START, JOB_RELOAD_OR_START) ||
213                  types_match(*a, b, JOB_VERIFY_ACTIVE, JOB_RELOAD_OR_START) ||
214                  types_match(*a, b, JOB_RELOAD, JOB_RELOAD_OR_START))
215                 *a = JOB_RELOAD_OR_START;
216         else if (types_match(*a, b, JOB_START, JOB_RESTART) ||
217                  types_match(*a, b, JOB_START, JOB_TRY_RESTART) ||
218                  types_match(*a, b, JOB_VERIFY_ACTIVE, JOB_RESTART) ||
219                  types_match(*a, b, JOB_RELOAD, JOB_RESTART) ||
220                  types_match(*a, b, JOB_RELOAD_OR_START, JOB_RESTART) ||
221                  types_match(*a, b, JOB_RELOAD_OR_START, JOB_TRY_RESTART) ||
222                  types_match(*a, b, JOB_RESTART, JOB_TRY_RESTART))
223                 *a = JOB_RESTART;
224         else if (types_match(*a, b, JOB_VERIFY_ACTIVE, JOB_RELOAD))
225                 *a = JOB_RELOAD;
226         else if (types_match(*a, b, JOB_VERIFY_ACTIVE, JOB_TRY_RESTART) ||
227                  types_match(*a, b, JOB_RELOAD, JOB_TRY_RESTART))
228                 *a = JOB_TRY_RESTART;
229         else
230                 return -EEXIST;
231
232         return 0;
233 }
234
235 bool job_type_is_mergeable(JobType a, JobType b) {
236         return job_type_merge(&a, b) >= 0;
237 }
238
239 bool job_type_is_superset(JobType a, JobType b) {
240
241         /* Checks whether operation a is a "superset" of b in its
242          * actions */
243
244         if (a == b)
245                 return true;
246
247         switch (a) {
248                 case JOB_START:
249                         return b == JOB_VERIFY_ACTIVE;
250
251                 case JOB_RELOAD:
252                         return
253                                 b == JOB_VERIFY_ACTIVE;
254
255                 case JOB_RELOAD_OR_START:
256                         return
257                                 b == JOB_RELOAD ||
258                                 b == JOB_START ||
259                                 b == JOB_VERIFY_ACTIVE;
260
261                 case JOB_RESTART:
262                         return
263                                 b == JOB_START ||
264                                 b == JOB_VERIFY_ACTIVE ||
265                                 b == JOB_RELOAD ||
266                                 b == JOB_RELOAD_OR_START ||
267                                 b == JOB_TRY_RESTART;
268
269                 case JOB_TRY_RESTART:
270                         return
271                                 b == JOB_VERIFY_ACTIVE ||
272                                 b == JOB_RELOAD;
273                 default:
274                         return false;
275
276         }
277 }
278
279 bool job_type_is_conflicting(JobType a, JobType b) {
280         assert(a >= 0 && a < _JOB_TYPE_MAX);
281         assert(b >= 0 && b < _JOB_TYPE_MAX);
282
283         return (a == JOB_STOP) != (b == JOB_STOP);
284 }
285
286 bool job_type_is_redundant(JobType a, UnitActiveState b) {
287         switch (a) {
288
289         case JOB_START:
290                 return
291                         b == UNIT_ACTIVE ||
292                         b == UNIT_RELOADING;
293
294         case JOB_STOP:
295                 return
296                         b == UNIT_INACTIVE ||
297                         b == UNIT_MAINTENANCE;
298
299         case JOB_VERIFY_ACTIVE:
300                 return
301                         b == UNIT_ACTIVE ||
302                         b == UNIT_RELOADING;
303
304         case JOB_RELOAD:
305                 return
306                         b == UNIT_RELOADING;
307
308         case JOB_RELOAD_OR_START:
309                 return
310                         b == UNIT_ACTIVATING ||
311                         b == UNIT_RELOADING;
312
313         case JOB_RESTART:
314                 return
315                         b == UNIT_ACTIVATING;
316
317         case JOB_TRY_RESTART:
318                 return
319                         b == UNIT_ACTIVATING;
320
321         default:
322                 assert_not_reached("Invalid job type");
323         }
324 }
325
326 bool job_is_runnable(Job *j) {
327         Iterator i;
328         Unit *other;
329
330         assert(j);
331         assert(j->installed);
332
333         /* Checks whether there is any job running for the units this
334          * job needs to be running after (in the case of a 'positive'
335          * job type) or before (in the case of a 'negative' job type
336          * . */
337
338         if (j->type == JOB_START ||
339             j->type == JOB_VERIFY_ACTIVE ||
340             j->type == JOB_RELOAD ||
341             j->type == JOB_RELOAD_OR_START) {
342
343                 /* Immediate result is that the job is or might be
344                  * started. In this case lets wait for the
345                  * dependencies, regardless whether they are
346                  * starting or stopping something. */
347
348                 SET_FOREACH(other, j->unit->meta.dependencies[UNIT_AFTER], i)
349                         if (other->meta.job)
350                                 return false;
351         }
352
353         /* Also, if something else is being stopped and we should
354          * change state after it, then lets wait. */
355
356         SET_FOREACH(other, j->unit->meta.dependencies[UNIT_BEFORE], i)
357                 if (other->meta.job &&
358                     (other->meta.job->type == JOB_STOP ||
359                      other->meta.job->type == JOB_RESTART ||
360                      other->meta.job->type == JOB_TRY_RESTART))
361                         return false;
362
363         /* This means that for a service a and a service b where b
364          * shall be started after a:
365          *
366          *  start a + start b â†’ 1st step start a, 2nd step start b
367          *  start a + stop b  â†’ 1st step stop b,  2nd step start a
368          *  stop a  + start b â†’ 1st step stop a,  2nd step start b
369          *  stop a  + stop b  â†’ 1st step stop b,  2nd step stop a
370          *
371          *  This has the side effect that restarts are properly
372          *  synchronized too. */
373
374         return true;
375 }
376
377 int job_run_and_invalidate(Job *j) {
378         int r;
379
380         assert(j);
381         assert(j->installed);
382
383         if (j->in_run_queue) {
384                 LIST_REMOVE(Job, run_queue, j->manager->run_queue, j);
385                 j->in_run_queue = false;
386         }
387
388         if (j->state != JOB_WAITING)
389                 return 0;
390
391         if (!job_is_runnable(j))
392                 return -EAGAIN;
393
394         j->state = JOB_RUNNING;
395         job_add_to_dbus_queue(j);
396
397         switch (j->type) {
398
399                 case JOB_START:
400                         r = unit_start(j->unit);
401                         if (r == -EBADR)
402                                 r = 0;
403                         break;
404
405                 case JOB_VERIFY_ACTIVE: {
406                         UnitActiveState t = unit_active_state(j->unit);
407                         if (UNIT_IS_ACTIVE_OR_RELOADING(t))
408                                 r = -EALREADY;
409                         else if (t == UNIT_ACTIVATING)
410                                 r = -EAGAIN;
411                         else
412                                 r = -ENOEXEC;
413                         break;
414                 }
415
416                 case JOB_STOP:
417                         r = unit_stop(j->unit);
418                         break;
419
420                 case JOB_RELOAD:
421                         r = unit_reload(j->unit);
422                         break;
423
424                 case JOB_RELOAD_OR_START:
425                         if (unit_active_state(j->unit) == UNIT_ACTIVE)
426                                 r = unit_reload(j->unit);
427                         else
428                                 r = unit_start(j->unit);
429                         break;
430
431                 case JOB_RESTART: {
432                         UnitActiveState t = unit_active_state(j->unit);
433                         if (t == UNIT_INACTIVE || t == UNIT_MAINTENANCE || t == UNIT_ACTIVATING) {
434                                 j->type = JOB_START;
435                                 r = unit_start(j->unit);
436                         } else
437                                 r = unit_stop(j->unit);
438                         break;
439                 }
440
441                 case JOB_TRY_RESTART: {
442                         UnitActiveState t = unit_active_state(j->unit);
443                         if (t == UNIT_INACTIVE || t == UNIT_MAINTENANCE || t == UNIT_DEACTIVATING)
444                                 r = -ENOEXEC;
445                         else if (t == UNIT_ACTIVATING) {
446                                 j->type = JOB_START;
447                                 r = unit_start(j->unit);
448                         } else
449                                 r = unit_stop(j->unit);
450                         break;
451                 }
452
453                 default:
454                         assert_not_reached("Unknown job type");
455         }
456
457         if (r == -EALREADY)
458                 r = job_finish_and_invalidate(j, true);
459         else if (r == -EAGAIN) {
460                 j->state = JOB_WAITING;
461                 return -EAGAIN;
462         } else if (r < 0)
463                 r = job_finish_and_invalidate(j, false);
464
465         return r;
466 }
467
468 int job_finish_and_invalidate(Job *j, bool success) {
469         Unit *u;
470         Unit *other;
471         JobType t;
472         Iterator i;
473
474         assert(j);
475         assert(j->installed);
476
477         job_add_to_dbus_queue(j);
478
479         /* Patch restart jobs so that they become normal start jobs */
480         if (success && (j->type == JOB_RESTART || j->type == JOB_TRY_RESTART)) {
481
482                 log_debug("Converting job %s/%s -> %s/%s",
483                           j->unit->meta.id, job_type_to_string(j->type),
484                           j->unit->meta.id, job_type_to_string(JOB_START));
485
486                 j->state = JOB_WAITING;
487                 j->type = JOB_START;
488
489                 job_add_to_run_queue(j);
490                 return 0;
491         }
492
493         log_debug("Job %s/%s finished, success=%s", j->unit->meta.id, job_type_to_string(j->type), yes_no(success));
494
495         j->failed = !success;
496         u = j->unit;
497         t = j->type;
498         job_free(j);
499
500         if (!success)
501                 unit_status_printf(u, "Starting %s " ANSI_HIGHLIGHT_ON "failed" ANSI_HIGHLIGHT_OFF ".\n", unit_description(u));
502
503         /* Fail depending jobs on failure */
504         if (!success) {
505
506                 if (t == JOB_START ||
507                     t == JOB_VERIFY_ACTIVE ||
508                     t == JOB_RELOAD_OR_START) {
509
510                         SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i)
511                                 if (!other->meta.ignore_dependency_failure &&
512                                     other->meta.job &&
513                                     (other->meta.job->type == JOB_START ||
514                                      other->meta.job->type == JOB_VERIFY_ACTIVE ||
515                                      other->meta.job->type == JOB_RELOAD_OR_START))
516                                         job_finish_and_invalidate(other->meta.job, false);
517
518                         SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY_OVERRIDABLE], i)
519                                 if (!other->meta.ignore_dependency_failure &&
520                                     other->meta.job &&
521                                     !other->meta.job->override &&
522                                     (other->meta.job->type == JOB_START ||
523                                      other->meta.job->type == JOB_VERIFY_ACTIVE ||
524                                      other->meta.job->type == JOB_RELOAD_OR_START))
525                                         job_finish_and_invalidate(other->meta.job, false);
526
527                 } else if (t == JOB_STOP) {
528
529                         SET_FOREACH(other, u->meta.dependencies[UNIT_CONFLICTS], i)
530                                 if (!other->meta.ignore_dependency_failure &&
531                                     other->meta.job &&
532                                     (other->meta.job->type == JOB_START ||
533                                      other->meta.job->type == JOB_VERIFY_ACTIVE ||
534                                      other->meta.job->type == JOB_RELOAD_OR_START))
535                                         job_finish_and_invalidate(other->meta.job, false);
536                 }
537         }
538
539         /* Try to start the next jobs that can be started */
540         SET_FOREACH(other, u->meta.dependencies[UNIT_AFTER], i)
541                 if (other->meta.job)
542                         job_add_to_run_queue(other->meta.job);
543         SET_FOREACH(other, u->meta.dependencies[UNIT_BEFORE], i)
544                 if (other->meta.job)
545                         job_add_to_run_queue(other->meta.job);
546
547         return 0;
548 }
549
550 int job_start_timer(Job *j) {
551         struct itimerspec its;
552         struct epoll_event ev;
553         int fd, r;
554         assert(j);
555
556         if (j->unit->meta.job_timeout <= 0 ||
557             j->timer_watch.type == WATCH_JOB_TIMER)
558                 return 0;
559
560         assert(j->timer_watch.type == WATCH_INVALID);
561
562         if ((fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC)) < 0) {
563                 r = -errno;
564                 goto fail;
565         }
566
567         zero(its);
568         timespec_store(&its.it_value, j->unit->meta.job_timeout);
569
570         if (timerfd_settime(fd, 0, &its, NULL) < 0) {
571                 r = -errno;
572                 goto fail;
573         }
574
575         zero(ev);
576         ev.data.ptr = &j->timer_watch;
577         ev.events = EPOLLIN;
578
579         if (epoll_ctl(j->manager->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) {
580                 r = -errno;
581                 goto fail;
582         }
583
584         j->timer_watch.type = WATCH_JOB_TIMER;
585         j->timer_watch.fd = fd;
586         j->timer_watch.data.job = j;
587
588         return 0;
589
590 fail:
591         if (fd >= 0)
592                 close_nointr_nofail(fd);
593
594         return r;
595 }
596
597 void job_add_to_run_queue(Job *j) {
598         assert(j);
599         assert(j->installed);
600
601         if (j->in_run_queue)
602                 return;
603
604         LIST_PREPEND(Job, run_queue, j->manager->run_queue, j);
605         j->in_run_queue = true;
606 }
607
608 void job_add_to_dbus_queue(Job *j) {
609         assert(j);
610         assert(j->installed);
611
612         if (j->in_dbus_queue)
613                 return;
614
615         /* We don't check if anybody is subscribed here, since this
616          * job might just have been created and not yet assigned to a
617          * connection/client. */
618
619         LIST_PREPEND(Job, dbus_queue, j->manager->dbus_job_queue, j);
620         j->in_dbus_queue = true;
621 }
622
623 char *job_dbus_path(Job *j) {
624         char *p;
625
626         assert(j);
627
628         if (asprintf(&p, "/org/freedesktop/systemd1/job/%lu", (unsigned long) j->id) < 0)
629                 return NULL;
630
631         return p;
632 }
633
634 void job_timer_event(Job *j, uint64_t n_elapsed, Watch *w) {
635         assert(j);
636         assert(w == &j->timer_watch);
637
638         log_warning("Job %s/%s timed out.", j->unit->meta.id, job_type_to_string(j->type));
639         job_finish_and_invalidate(j, false);
640 }
641
642 static const char* const job_state_table[_JOB_STATE_MAX] = {
643         [JOB_WAITING] = "waiting",
644         [JOB_RUNNING] = "running"
645 };
646
647 DEFINE_STRING_TABLE_LOOKUP(job_state, JobState);
648
649 static const char* const job_type_table[_JOB_TYPE_MAX] = {
650         [JOB_START] = "start",
651         [JOB_VERIFY_ACTIVE] = "verify-active",
652         [JOB_STOP] = "stop",
653         [JOB_RELOAD] = "reload",
654         [JOB_RELOAD_OR_START] = "reload-or-start",
655         [JOB_RESTART] = "restart",
656         [JOB_TRY_RESTART] = "try-restart",
657 };
658
659 DEFINE_STRING_TABLE_LOOKUP(job_type, JobType);
660
661 static const char* const job_mode_table[_JOB_MODE_MAX] = {
662         [JOB_FAIL] = "fail",
663         [JOB_REPLACE] = "replace",
664         [JOB_ISOLATE] = "isolate"
665 };
666
667 DEFINE_STRING_TABLE_LOOKUP(job_mode, JobMode);