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