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