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