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