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