]> git.ipfire.org Git - thirdparty/systemd.git/blame - job.c
dbus: greatly extend dbus coverage
[thirdparty/systemd.git] / job.c
CommitLineData
60918275
LP
1/*-*- Mode: C; c-basic-offset: 8 -*-*/
2
a7334b09
LP
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
60918275 22#include <assert.h>
1ffba6fe 23#include <errno.h>
60918275 24
94f04347
LP
25#include "set.h"
26#include "unit.h"
60918275 27#include "macro.h"
94f04347
LP
28#include "strv.h"
29#include "load-fragment.h"
30#include "load-dropin.h"
f50e0a01 31#include "log.h"
4139c1b2 32#include "dbus-job.h"
60918275 33
87f0e418 34Job* job_new(Manager *m, JobType type, Unit *unit) {
60918275
LP
35 Job *j;
36
37 assert(m);
38 assert(type < _JOB_TYPE_MAX);
87f0e418 39 assert(unit);
60918275
LP
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;
87f0e418 47 j->unit = unit;
60918275 48
e5b5ae50 49 /* We don't link it here, that's what job_dependency() is for */
60918275
LP
50
51 return j;
52}
53
60918275
LP
54void job_free(Job *j) {
55 assert(j);
56
57 /* Detach from next 'bigger' objects */
ac1135be 58 if (j->installed) {
c1e1601e
LP
59 bus_job_send_removed_signal(j);
60
87f0e418
LP
61 if (j->unit->meta.job == j)
62 j->unit->meta.job = NULL;
60918275
LP
63
64 hashmap_remove(j->manager->jobs, UINT32_TO_PTR(j->id));
ac1135be 65 j->installed = false;
60918275
LP
66 }
67
5cb5a6ff 68 /* Detach from next 'smaller' objects */
23a177ef 69 manager_transaction_unlink_job(j->manager, j, true);
60918275 70
c1e1601e
LP
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
60918275
LP
77 free(j);
78}
a66d02c3 79
e5b5ae50
LP
80JobDependency* 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
ceed3570 90 if (!(l = new0(JobDependency, 1)))
e5b5ae50
LP
91 return NULL;
92
93 l->subject = subject;
94 l->object = object;
95 l->matters = matters;
96
44d8db9e
LP
97 if (subject)
98 LIST_PREPEND(JobDependency, subject, subject->subject_list, l);
99 else
100 LIST_PREPEND(JobDependency, subject, object->manager->transaction_anchor, l);
e5b5ae50 101
44d8db9e 102 LIST_PREPEND(JobDependency, object, object->object_list, l);
e5b5ae50
LP
103
104 return l;
105}
106
107void job_dependency_free(JobDependency *l) {
108 assert(l);
109
44d8db9e
LP
110 if (l->subject)
111 LIST_REMOVE(JobDependency, subject, l->subject->subject_list, l);
e5b5ae50 112 else
44d8db9e 113 LIST_REMOVE(JobDependency, subject, l->object->manager->transaction_anchor, l);
e5b5ae50 114
44d8db9e 115 LIST_REMOVE(JobDependency, object, l->object->object_list, l);
e5b5ae50
LP
116
117 free(l);
118}
119
120void job_dependency_delete(Job *subject, Job *object, bool *matters) {
121 JobDependency *l;
122
123 assert(object);
124
44d8db9e 125 LIST_FOREACH(object, l, object->object_list) {
e5b5ae50
LP
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
1ffba6fe
LP
144void job_dump(Job *j, FILE*f, const char *prefix) {
145
a66d02c3
LP
146
147 assert(j);
148 assert(f);
149
ceed3570 150 fprintf(f,
44d8db9e 151 "%sā†’ Job %u:\n"
ceed3570 152 "%s\tAction: %s ā†’ %s\n"
5cb5a6ff
LP
153 "%s\tState: %s\n"
154 "%s\tForced: %s\n",
ceed3570 155 prefix, j->id,
9e2f7c11 156 prefix, j->unit->meta.id, job_type_to_string(j->type),
94f04347 157 prefix, job_state_to_string(j->state),
9e2f7c11 158 prefix, yes_no(j->override));
a66d02c3 159}
e5b5ae50
LP
160
161bool job_is_anchor(Job *j) {
162 JobDependency *l;
163
164 assert(j);
165
44d8db9e 166 LIST_FOREACH(object, l, j->object_list)
e5b5ae50
LP
167 if (!l->subject)
168 return true;
169
170 return false;
171}
1ffba6fe
LP
172
173static 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
179int 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
5cb5a6ff 192 if (types_match(*a, b, JOB_START, JOB_VERIFY_ACTIVE))
1ffba6fe
LP
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) ||
5cb5a6ff 196 types_match(*a, b, JOB_VERIFY_ACTIVE, JOB_RELOAD_OR_START) ||
1ffba6fe
LP
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) ||
5cb5a6ff 201 types_match(*a, b, JOB_VERIFY_ACTIVE, JOB_RESTART) ||
1ffba6fe
LP
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;
5cb5a6ff 207 else if (types_match(*a, b, JOB_VERIFY_ACTIVE, JOB_RELOAD))
1ffba6fe 208 *a = JOB_RELOAD;
5cb5a6ff 209 else if (types_match(*a, b, JOB_VERIFY_ACTIVE, JOB_TRY_RESTART) ||
1ffba6fe
LP
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
5cb5a6ff 218bool job_type_is_mergeable(JobType a, JobType b) {
1ffba6fe
LP
219 return job_type_merge(&a, b) >= 0;
220}
221
222bool job_type_is_superset(JobType a, JobType b) {
223
5cb5a6ff
LP
224 /* Checks whether operation a is a "superset" of b in its
225 * actions */
1ffba6fe
LP
226
227 if (a == b)
228 return true;
229
230 switch (a) {
231 case JOB_START:
5cb5a6ff 232 return b == JOB_VERIFY_ACTIVE;
1ffba6fe
LP
233
234 case JOB_RELOAD:
5cb5a6ff
LP
235 return
236 b == JOB_VERIFY_ACTIVE;
1ffba6fe
LP
237
238 case JOB_RELOAD_OR_START:
239 return
240 b == JOB_RELOAD ||
5cb5a6ff
LP
241 b == JOB_START ||
242 b == JOB_VERIFY_ACTIVE;
1ffba6fe
LP
243
244 case JOB_RESTART:
245 return
246 b == JOB_START ||
5cb5a6ff 247 b == JOB_VERIFY_ACTIVE ||
1ffba6fe
LP
248 b == JOB_RELOAD ||
249 b == JOB_RELOAD_OR_START ||
250 b == JOB_TRY_RESTART;
251
252 case JOB_TRY_RESTART:
253 return
5cb5a6ff 254 b == JOB_VERIFY_ACTIVE ||
1ffba6fe
LP
255 b == JOB_RELOAD;
256 default:
257 return false;
258
259 }
260}
e094e853
LP
261
262bool job_type_is_conflicting(JobType a, JobType b) {
cd2dbd7d
LP
263 assert(a >= 0 && a < _JOB_TYPE_MAX);
264 assert(b >= 0 && b < _JOB_TYPE_MAX);
e094e853 265
5cb5a6ff 266 return (a == JOB_STOP) != (b == JOB_STOP);
e094e853 267}
cd2dbd7d 268
593fbdd2
LP
269bool 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
5cb5a6ff 308bool job_is_runnable(Job *j) {
034c6ed7 309 Iterator i;
87f0e418 310 Unit *other;
5cb5a6ff
LP
311
312 assert(j);
ac1135be 313 assert(j->installed);
5cb5a6ff 314
87f0e418 315 /* Checks whether there is any job running for the units this
5cb5a6ff
LP
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
87f0e418 330 SET_FOREACH(other, j->unit->meta.dependencies[UNIT_AFTER], i)
5cb5a6ff
LP
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
87f0e418 338 SET_FOREACH(other, j->unit->meta.dependencies[UNIT_BEFORE], i)
5cb5a6ff
LP
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
359int job_run_and_invalidate(Job *j) {
360 int r;
ac1135be 361
5cb5a6ff 362 assert(j);
ac1135be 363 assert(j->installed);
5cb5a6ff 364
034c6ed7
LP
365 if (j->in_run_queue) {
366 LIST_REMOVE(Job, run_queue, j->manager->run_queue, j);
367 j->in_run_queue = false;
368 }
5cb5a6ff
LP
369
370 if (j->state != JOB_WAITING)
371 return 0;
372
034c6ed7
LP
373 if (!job_is_runnable(j))
374 return -EAGAIN;
375
83c60c9f 376 j->state = JOB_RUNNING;
c1e1601e 377 job_add_to_dbus_queue(j);
83c60c9f 378
5cb5a6ff
LP
379 switch (j->type) {
380
381 case JOB_START:
87f0e418 382 r = unit_start(j->unit);
5cb5a6ff
LP
383 if (r == -EBADR)
384 r = 0;
385 break;
386
387 case JOB_VERIFY_ACTIVE: {
87f0e418
LP
388 UnitActiveState t = unit_active_state(j->unit);
389 if (UNIT_IS_ACTIVE_OR_RELOADING(t))
5cb5a6ff 390 r = -EALREADY;
87f0e418 391 else if (t == UNIT_ACTIVATING)
5cb5a6ff
LP
392 r = -EAGAIN;
393 else
394 r = -ENOEXEC;
395 break;
396 }
397
398 case JOB_STOP:
87f0e418 399 r = unit_stop(j->unit);
5cb5a6ff
LP
400 break;
401
402 case JOB_RELOAD:
87f0e418 403 r = unit_reload(j->unit);
5cb5a6ff
LP
404 break;
405
406 case JOB_RELOAD_OR_START:
87f0e418
LP
407 if (unit_active_state(j->unit) == UNIT_ACTIVE)
408 r = unit_reload(j->unit);
5cb5a6ff 409 else
87f0e418 410 r = unit_start(j->unit);
5cb5a6ff
LP
411 break;
412
413 case JOB_RESTART: {
87f0e418
LP
414 UnitActiveState t = unit_active_state(j->unit);
415 if (t == UNIT_INACTIVE || t == UNIT_ACTIVATING) {
5cb5a6ff 416 j->type = JOB_START;
87f0e418 417 r = unit_start(j->unit);
5cb5a6ff 418 } else
87f0e418 419 r = unit_stop(j->unit);
5cb5a6ff
LP
420 break;
421 }
422
423 case JOB_TRY_RESTART: {
87f0e418
LP
424 UnitActiveState t = unit_active_state(j->unit);
425 if (t == UNIT_INACTIVE || t == UNIT_DEACTIVATING)
5cb5a6ff 426 r = -ENOEXEC;
87f0e418 427 else if (t == UNIT_ACTIVATING) {
5cb5a6ff 428 j->type = JOB_START;
87f0e418 429 r = unit_start(j->unit);
5cb5a6ff 430 } else
87f0e418 431 r = unit_stop(j->unit);
5cb5a6ff
LP
432 break;
433 }
434
435 default:
44d8db9e 436 assert_not_reached("Unknown job type");
5cb5a6ff
LP
437 }
438
83c60c9f 439 if (r == -EALREADY)
5cb5a6ff 440 r = job_finish_and_invalidate(j, true);
83c60c9f
LP
441 else if (r == -EAGAIN) {
442 j->state = JOB_WAITING;
443 return -EAGAIN;
444 } else if (r < 0)
5cb5a6ff
LP
445 r = job_finish_and_invalidate(j, false);
446
447 return r;
448}
449
450int job_finish_and_invalidate(Job *j, bool success) {
87f0e418
LP
451 Unit *u;
452 Unit *other;
b866264a 453 JobType t;
034c6ed7 454 Iterator i;
5cb5a6ff
LP
455
456 assert(j);
ac1135be 457 assert(j->installed);
5cb5a6ff 458
9e2f7c11 459 log_debug("Job %s/%s finished, success=%s", j->unit->meta.id, job_type_to_string(j->type), yes_no(success));
c1e1601e 460 job_add_to_dbus_queue(j);
f50e0a01 461
034c6ed7 462 /* Patch restart jobs so that they become normal start jobs */
5cb5a6ff 463 if (success && (j->type == JOB_RESTART || j->type == JOB_TRY_RESTART)) {
f50e0a01
LP
464
465 log_debug("Converting job %s/%s ā†’ %s/%s",
9e2f7c11
LP
466 j->unit->meta.id, job_type_to_string(j->type),
467 j->unit->meta.id, job_type_to_string(JOB_START));
f50e0a01 468
5cb5a6ff
LP
469 j->state = JOB_RUNNING;
470 j->type = JOB_START;
f50e0a01 471
c1e1601e 472 job_add_to_run_queue(j);
034c6ed7 473 return 0;
5cb5a6ff
LP
474 }
475
87f0e418 476 u = j->unit;
5cb5a6ff
LP
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
87f0e418 487 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i)
5cb5a6ff 488 if (other->meta.job &&
dda5a135
LP
489 (other->meta.job->type == JOB_START ||
490 other->meta.job->type == JOB_VERIFY_ACTIVE ||
491 other->meta.job->type == JOB_RELOAD_OR_START))
5cb5a6ff
LP
492 job_finish_and_invalidate(other->meta.job, false);
493
9e2f7c11 494 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY_OVERRIDABLE], i)
5cb5a6ff 495 if (other->meta.job &&
9e2f7c11 496 !other->meta.job->override &&
dda5a135
LP
497 (other->meta.job->type == JOB_START ||
498 other->meta.job->type == JOB_VERIFY_ACTIVE ||
499 other->meta.job->type == JOB_RELOAD_OR_START))
5cb5a6ff
LP
500 job_finish_and_invalidate(other->meta.job, false);
501
502 } else if (t == JOB_STOP) {
503
87f0e418 504 SET_FOREACH(other, u->meta.dependencies[UNIT_CONFLICTS], i)
5cb5a6ff 505 if (other->meta.job &&
dda5a135
LP
506 (other->meta.job->type == JOB_START ||
507 other->meta.job->type == JOB_VERIFY_ACTIVE ||
508 other->meta.job->type == JOB_RELOAD_OR_START))
5cb5a6ff
LP
509 job_finish_and_invalidate(other->meta.job, false);
510 }
511 }
512
513 /* Try to start the next jobs that can be started */
87f0e418 514 SET_FOREACH(other, u->meta.dependencies[UNIT_AFTER], i)
5cb5a6ff 515 if (other->meta.job)
c1e1601e 516 job_add_to_run_queue(other->meta.job);
87f0e418 517 SET_FOREACH(other, u->meta.dependencies[UNIT_BEFORE], i)
5cb5a6ff 518 if (other->meta.job)
c1e1601e 519 job_add_to_run_queue(other->meta.job);
5cb5a6ff
LP
520
521 return 0;
522}
034c6ed7 523
c1e1601e 524void job_add_to_run_queue(Job *j) {
034c6ed7 525 assert(j);
ac1135be 526 assert(j->installed);
034c6ed7
LP
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}
94f04347 534
c1e1601e
LP
535void 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
ea430986
LP
546char *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
94f04347
LP
557static const char* const job_state_table[_JOB_STATE_MAX] = {
558 [JOB_WAITING] = "waiting",
559 [JOB_RUNNING] = "running"
560};
561
562DEFINE_STRING_TABLE_LOOKUP(job_state, JobState);
563
564static 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
574DEFINE_STRING_TABLE_LOOKUP(job_type, JobType);
b548631a
LP
575
576static const char* const job_mode_table[_JOB_MODE_MAX] = {
577 [JOB_FAIL] = "fail",
578 [JOB_REPLACE] = "replace"
579};
580
581DEFINE_STRING_TABLE_LOOKUP(job_mode, JobMode);