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