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