1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
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.
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.
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/>.
29 #include "load-fragment.h"
30 #include "load-dropin.h"
33 Job* job_new(Manager *m, JobType type, Unit *unit) {
37 assert(type < _JOB_TYPE_MAX);
40 if (!(j = new0(Job, 1)))
44 j->id = m->current_job_id++;
48 /* We don't link it here, that's what job_dependency() is for */
53 void job_free(Job *j) {
56 /* Detach from next 'bigger' objects */
58 bus_job_send_removed_signal(j);
60 if (j->unit->meta.job == j)
61 j->unit->meta.job = NULL;
63 hashmap_remove(j->manager->jobs, UINT32_TO_PTR(j->id));
67 /* Detach from next 'smaller' objects */
68 manager_transaction_unlink_job(j->manager, j);
71 LIST_REMOVE(Job, run_queue, j->manager->run_queue, j);
74 LIST_REMOVE(Job, dbus_queue, j->manager->dbus_job_queue, j);
79 JobDependency* job_dependency_new(Job *subject, Job *object, bool matters) {
84 /* Adds a new job link, which encodes that the 'subject' job
85 * needs the 'object' job in some way. If 'subject' is NULL
86 * this means the 'anchor' job (i.e. the one the user
87 * explcitily asked for) is the requester. */
89 if (!(l = new0(JobDependency, 1)))
97 LIST_PREPEND(JobDependency, subject, subject->subject_list, l);
99 LIST_PREPEND(JobDependency, subject, object->manager->transaction_anchor, l);
101 LIST_PREPEND(JobDependency, object, object->object_list, l);
106 void job_dependency_free(JobDependency *l) {
110 LIST_REMOVE(JobDependency, subject, l->subject->subject_list, l);
112 LIST_REMOVE(JobDependency, subject, l->object->manager->transaction_anchor, l);
114 LIST_REMOVE(JobDependency, object, l->object->object_list, l);
119 void job_dependency_delete(Job *subject, Job *object, bool *matters) {
124 LIST_FOREACH(object, l, object->object_list) {
125 assert(l->object == object);
127 if (l->subject == subject)
138 *matters = l->matters;
140 job_dependency_free(l);
143 void job_dump(Job *j, FILE*f, const char *prefix) {
151 "%s\tAction: %s ā %s\n"
155 prefix, unit_id(j->unit), job_type_to_string(j->type),
156 prefix, job_state_to_string(j->state),
157 prefix, yes_no(j->forced));
160 bool job_is_anchor(Job *j) {
165 LIST_FOREACH(object, l, j->object_list)
172 static bool types_match(JobType a, JobType b, JobType c, JobType d) {
174 (a == c && b == d) ||
178 int job_type_merge(JobType *a, JobType b) {
182 /* Merging is associative! a merged with b merged with c is
183 * the same as a merged with c merged with b. */
185 /* Mergeability is transitive! if a can be merged with b and b
186 * with c then a also with c */
188 /* Also, if a merged with b cannot be merged with c, then
189 * either a or b cannot be merged with c either */
191 if (types_match(*a, b, JOB_START, JOB_VERIFY_ACTIVE))
193 else if (types_match(*a, b, JOB_START, JOB_RELOAD) ||
194 types_match(*a, b, JOB_START, JOB_RELOAD_OR_START) ||
195 types_match(*a, b, JOB_VERIFY_ACTIVE, JOB_RELOAD_OR_START) ||
196 types_match(*a, b, JOB_RELOAD, JOB_RELOAD_OR_START))
197 *a = JOB_RELOAD_OR_START;
198 else if (types_match(*a, b, JOB_START, JOB_RESTART) ||
199 types_match(*a, b, JOB_START, JOB_TRY_RESTART) ||
200 types_match(*a, b, JOB_VERIFY_ACTIVE, JOB_RESTART) ||
201 types_match(*a, b, JOB_RELOAD, JOB_RESTART) ||
202 types_match(*a, b, JOB_RELOAD_OR_START, JOB_RESTART) ||
203 types_match(*a, b, JOB_RELOAD_OR_START, JOB_TRY_RESTART) ||
204 types_match(*a, b, JOB_RESTART, JOB_TRY_RESTART))
206 else if (types_match(*a, b, JOB_VERIFY_ACTIVE, JOB_RELOAD))
208 else if (types_match(*a, b, JOB_VERIFY_ACTIVE, JOB_TRY_RESTART) ||
209 types_match(*a, b, JOB_RELOAD, JOB_TRY_RESTART))
210 *a = JOB_TRY_RESTART;
217 bool job_type_is_mergeable(JobType a, JobType b) {
218 return job_type_merge(&a, b) >= 0;
221 bool job_type_is_superset(JobType a, JobType b) {
223 /* Checks whether operation a is a "superset" of b in its
231 return b == JOB_VERIFY_ACTIVE;
235 b == JOB_VERIFY_ACTIVE;
237 case JOB_RELOAD_OR_START:
241 b == JOB_VERIFY_ACTIVE;
246 b == JOB_VERIFY_ACTIVE ||
248 b == JOB_RELOAD_OR_START ||
249 b == JOB_TRY_RESTART;
251 case JOB_TRY_RESTART:
253 b == JOB_VERIFY_ACTIVE ||
261 bool job_type_is_conflicting(JobType a, JobType b) {
262 assert(a >= 0 && a < _JOB_TYPE_MAX);
263 assert(b >= 0 && b < _JOB_TYPE_MAX);
265 return (a == JOB_STOP) != (b == JOB_STOP);
268 bool job_is_runnable(Job *j) {
273 assert(j->installed);
275 /* Checks whether there is any job running for the units this
276 * job needs to be running after (in the case of a 'positive'
277 * job type) or before (in the case of a 'negative' job type
280 if (j->type == JOB_START ||
281 j->type == JOB_VERIFY_ACTIVE ||
282 j->type == JOB_RELOAD ||
283 j->type == JOB_RELOAD_OR_START) {
285 /* Immediate result is that the job is or might be
286 * started. In this case lets wait for the
287 * dependencies, regardless whether they are
288 * starting or stopping something. */
290 SET_FOREACH(other, j->unit->meta.dependencies[UNIT_AFTER], i)
295 /* Also, if something else is being stopped and we should
296 * change state after it, then lets wait. */
298 SET_FOREACH(other, j->unit->meta.dependencies[UNIT_BEFORE], i)
299 if (other->meta.job &&
300 (other->meta.job->type == JOB_STOP ||
301 other->meta.job->type == JOB_RESTART ||
302 other->meta.job->type == JOB_TRY_RESTART))
305 /* This means that for a service a and a service b where b
306 * shall be started after a:
308 * start a + start b ā 1st step start a, 2nd step start b
309 * start a + stop b ā 1st step stop b, 2nd step start a
310 * stop a + start b ā 1st step stop a, 2nd step start b
311 * stop a + stop b ā 1st step stop b, 2nd step stop a
313 * This has the side effect that restarts are properly
314 * synchronized too. */
319 int job_run_and_invalidate(Job *j) {
323 assert(j->installed);
325 if (j->in_run_queue) {
326 LIST_REMOVE(Job, run_queue, j->manager->run_queue, j);
327 j->in_run_queue = false;
330 if (j->state != JOB_WAITING)
333 if (!job_is_runnable(j))
336 j->state = JOB_RUNNING;
337 job_add_to_dbus_queue(j);
342 r = unit_start(j->unit);
347 case JOB_VERIFY_ACTIVE: {
348 UnitActiveState t = unit_active_state(j->unit);
349 if (UNIT_IS_ACTIVE_OR_RELOADING(t))
351 else if (t == UNIT_ACTIVATING)
359 r = unit_stop(j->unit);
363 r = unit_reload(j->unit);
366 case JOB_RELOAD_OR_START:
367 if (unit_active_state(j->unit) == UNIT_ACTIVE)
368 r = unit_reload(j->unit);
370 r = unit_start(j->unit);
374 UnitActiveState t = unit_active_state(j->unit);
375 if (t == UNIT_INACTIVE || t == UNIT_ACTIVATING) {
377 r = unit_start(j->unit);
379 r = unit_stop(j->unit);
383 case JOB_TRY_RESTART: {
384 UnitActiveState t = unit_active_state(j->unit);
385 if (t == UNIT_INACTIVE || t == UNIT_DEACTIVATING)
387 else if (t == UNIT_ACTIVATING) {
389 r = unit_start(j->unit);
391 r = unit_stop(j->unit);
396 assert_not_reached("Unknown job type");
400 r = job_finish_and_invalidate(j, true);
401 else if (r == -EAGAIN) {
402 j->state = JOB_WAITING;
405 r = job_finish_and_invalidate(j, false);
410 int job_finish_and_invalidate(Job *j, bool success) {
417 assert(j->installed);
419 log_debug("Job %s/%s finished, success=%s", unit_id(j->unit), job_type_to_string(j->type), yes_no(success));
420 job_add_to_dbus_queue(j);
422 /* Patch restart jobs so that they become normal start jobs */
423 if (success && (j->type == JOB_RESTART || j->type == JOB_TRY_RESTART)) {
425 log_debug("Converting job %s/%s ā %s/%s",
426 unit_id(j->unit), job_type_to_string(j->type),
427 unit_id(j->unit), job_type_to_string(JOB_START));
429 j->state = JOB_RUNNING;
432 job_add_to_run_queue(j);
440 /* Fail depending jobs on failure */
443 if (t == JOB_START ||
444 t == JOB_VERIFY_ACTIVE ||
445 t == JOB_RELOAD_OR_START) {
447 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i)
448 if (other->meta.job &&
449 (other->meta.type == JOB_START ||
450 other->meta.type == JOB_VERIFY_ACTIVE ||
451 other->meta.type == JOB_RELOAD_OR_START))
452 job_finish_and_invalidate(other->meta.job, false);
454 SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUIRED_BY], i)
455 if (other->meta.job &&
456 !other->meta.job->forced &&
457 (other->meta.type == JOB_START ||
458 other->meta.type == JOB_VERIFY_ACTIVE ||
459 other->meta.type == JOB_RELOAD_OR_START))
460 job_finish_and_invalidate(other->meta.job, false);
462 } else if (t == JOB_STOP) {
464 SET_FOREACH(other, u->meta.dependencies[UNIT_CONFLICTS], i)
465 if (other->meta.job &&
467 t == JOB_VERIFY_ACTIVE ||
468 t == JOB_RELOAD_OR_START))
469 job_finish_and_invalidate(other->meta.job, false);
473 /* Try to start the next jobs that can be started */
474 SET_FOREACH(other, u->meta.dependencies[UNIT_AFTER], i)
476 job_add_to_run_queue(other->meta.job);
477 SET_FOREACH(other, u->meta.dependencies[UNIT_BEFORE], i)
479 job_add_to_run_queue(other->meta.job);
484 void job_add_to_run_queue(Job *j) {
486 assert(j->installed);
491 LIST_PREPEND(Job, run_queue, j->manager->run_queue, j);
492 j->in_run_queue = true;
495 void job_add_to_dbus_queue(Job *j) {
497 assert(j->installed);
499 if (j->in_dbus_queue)
502 LIST_PREPEND(Job, dbus_queue, j->manager->dbus_job_queue, j);
503 j->in_dbus_queue = true;
506 char *job_dbus_path(Job *j) {
511 if (asprintf(&p, "/org/freedesktop/systemd1/job/%lu", (unsigned long) j->id) < 0)
517 static const char* const job_state_table[_JOB_STATE_MAX] = {
518 [JOB_WAITING] = "waiting",
519 [JOB_RUNNING] = "running"
522 DEFINE_STRING_TABLE_LOOKUP(job_state, JobState);
524 static const char* const job_type_table[_JOB_TYPE_MAX] = {
525 [JOB_START] = "start",
526 [JOB_VERIFY_ACTIVE] = "verify-active",
528 [JOB_RELOAD] = "reload",
529 [JOB_RELOAD_OR_START] = "reload-or-start",
530 [JOB_RESTART] = "restart",
531 [JOB_TRY_RESTART] = "try-restart",
534 DEFINE_STRING_TABLE_LOOKUP(job_type, JobType);
536 static const char* const job_mode_table[_JOB_MODE_MAX] = {
538 [JOB_REPLACE] = "replace"
541 DEFINE_STRING_TABLE_LOOKUP(job_mode, JobMode);