1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
10 Job* job_new(Manager *m, JobType type, Unit *unit) {
14 assert(type < _JOB_TYPE_MAX);
17 if (!(j = new0(Job, 1)))
21 j->id = m->current_job_id++;
25 /* We don't link it here, that's what job_dependency() is for */
30 void job_free(Job *j) {
33 /* Detach from next 'bigger' objects */
35 if (j->unit->meta.job == j)
36 j->unit->meta.job = NULL;
38 hashmap_remove(j->manager->jobs, UINT32_TO_PTR(j->id));
42 /* Detach from next 'smaller' objects */
43 manager_transaction_unlink_job(j->manager, j);
48 JobDependency* job_dependency_new(Job *subject, Job *object, bool matters) {
53 /* Adds a new job link, which encodes that the 'subject' job
54 * needs the 'object' job in some way. If 'subject' is NULL
55 * this means the 'anchor' job (i.e. the one the user
56 * explcitily asked for) is the requester. */
58 if (!(l = new0(JobDependency, 1)))
66 LIST_PREPEND(JobDependency, subject, subject->subject_list, l);
68 LIST_PREPEND(JobDependency, subject, object->manager->transaction_anchor, l);
70 LIST_PREPEND(JobDependency, object, object->object_list, l);
75 void job_dependency_free(JobDependency *l) {
79 LIST_REMOVE(JobDependency, subject, l->subject->subject_list, l);
81 LIST_REMOVE(JobDependency, subject, l->object->manager->transaction_anchor, l);
83 LIST_REMOVE(JobDependency, object, l->object->object_list, l);
88 void job_dependency_delete(Job *subject, Job *object, bool *matters) {
93 LIST_FOREACH(object, l, object->object_list) {
94 assert(l->object == object);
96 if (l->subject == subject)
107 *matters = l->matters;
109 job_dependency_free(l);
112 const char* job_type_to_string(JobType t) {
114 static const char* const job_type_table[_JOB_TYPE_MAX] = {
115 [JOB_START] = "start",
116 [JOB_VERIFY_ACTIVE] = "verify-active",
118 [JOB_RELOAD] = "reload",
119 [JOB_RELOAD_OR_START] = "reload-or-start",
120 [JOB_RESTART] = "restart",
121 [JOB_TRY_RESTART] = "try-restart",
124 if (t < 0 || t >= _JOB_TYPE_MAX)
127 return job_type_table[t];
130 void job_dump(Job *j, FILE*f, const char *prefix) {
132 static const char* const job_state_table[_JOB_STATE_MAX] = {
133 [JOB_WAITING] = "waiting",
134 [JOB_RUNNING] = "running"
142 "%s\tAction: %s → %s\n"
146 prefix, unit_id(j->unit), job_type_to_string(j->type),
147 prefix, job_state_table[j->state],
148 prefix, yes_no(j->forced));
151 bool job_is_anchor(Job *j) {
156 LIST_FOREACH(object, l, j->object_list)
163 static bool types_match(JobType a, JobType b, JobType c, JobType d) {
165 (a == c && b == d) ||
169 int job_type_merge(JobType *a, JobType b) {
173 /* Merging is associative! a merged with b merged with c is
174 * the same as a merged with c merged with b. */
176 /* Mergeability is transitive! if a can be merged with b and b
177 * with c then a also with c */
179 /* Also, if a merged with b cannot be merged with c, then
180 * either a or b cannot be merged with c either */
182 if (types_match(*a, b, JOB_START, JOB_VERIFY_ACTIVE))
184 else if (types_match(*a, b, JOB_START, JOB_RELOAD) ||
185 types_match(*a, b, JOB_START, JOB_RELOAD_OR_START) ||
186 types_match(*a, b, JOB_VERIFY_ACTIVE, JOB_RELOAD_OR_START) ||
187 types_match(*a, b, JOB_RELOAD, JOB_RELOAD_OR_START))
188 *a = JOB_RELOAD_OR_START;
189 else if (types_match(*a, b, JOB_START, JOB_RESTART) ||
190 types_match(*a, b, JOB_START, JOB_TRY_RESTART) ||
191 types_match(*a, b, JOB_VERIFY_ACTIVE, JOB_RESTART) ||
192 types_match(*a, b, JOB_RELOAD, JOB_RESTART) ||
193 types_match(*a, b, JOB_RELOAD_OR_START, JOB_RESTART) ||
194 types_match(*a, b, JOB_RELOAD_OR_START, JOB_TRY_RESTART) ||
195 types_match(*a, b, JOB_RESTART, JOB_TRY_RESTART))
197 else if (types_match(*a, b, JOB_VERIFY_ACTIVE, JOB_RELOAD))
199 else if (types_match(*a, b, JOB_VERIFY_ACTIVE, JOB_TRY_RESTART) ||
200 types_match(*a, b, JOB_RELOAD, JOB_TRY_RESTART))
201 *a = JOB_TRY_RESTART;
208 bool job_type_is_mergeable(JobType a, JobType b) {
209 return job_type_merge(&a, b) >= 0;
212 bool job_type_is_superset(JobType a, JobType b) {
214 /* Checks whether operation a is a "superset" of b in its
222 return b == JOB_VERIFY_ACTIVE;
226 b == JOB_VERIFY_ACTIVE;
228 case JOB_RELOAD_OR_START:
232 b == JOB_VERIFY_ACTIVE;
237 b == JOB_VERIFY_ACTIVE ||
239 b == JOB_RELOAD_OR_START ||
240 b == JOB_TRY_RESTART;
242 case JOB_TRY_RESTART:
244 b == JOB_VERIFY_ACTIVE ||
252 bool job_type_is_conflicting(JobType a, JobType b) {
253 assert(a >= 0 && a < _JOB_TYPE_MAX);
254 assert(b >= 0 && b < _JOB_TYPE_MAX);
256 return (a == JOB_STOP) != (b == JOB_STOP);
259 bool job_is_runnable(Job *j) {
264 assert(j->installed);
266 /* Checks whether there is any job running for the units this
267 * job needs to be running after (in the case of a 'positive'
268 * job type) or before (in the case of a 'negative' job type
271 if (j->type == JOB_START ||
272 j->type == JOB_VERIFY_ACTIVE ||
273 j->type == JOB_RELOAD ||
274 j->type == JOB_RELOAD_OR_START) {
276 /* Immediate result is that the job is or might be
277 * started. In this case lets wait for the
278 * dependencies, regardless whether they are
279 * starting or stopping something. */
281 SET_FOREACH(other, j->unit->meta.dependencies[UNIT_AFTER], i)
286 /* Also, if something else is being stopped and we should
287 * change state after it, then lets wait. */
289 SET_FOREACH(other, j->unit->meta.dependencies[UNIT_BEFORE], i)
290 if (other->meta.job &&
291 (other->meta.job->type == JOB_STOP ||
292 other->meta.job->type == JOB_RESTART ||
293 other->meta.job->type == JOB_TRY_RESTART))
296 /* This means that for a service a and a service b where b
297 * shall be started after a:
299 * start a + start b → 1st step start a, 2nd step start b
300 * start a + stop b → 1st step stop b, 2nd step start a
301 * stop a + start b → 1st step stop a, 2nd step start b
302 * stop a + stop b → 1st step stop b, 2nd step stop a
304 * This has the side effect that restarts are properly
305 * synchronized too. */
310 int job_run_and_invalidate(Job *j) {
314 assert(j->installed);
316 if (j->in_run_queue) {
317 LIST_REMOVE(Job, run_queue, j->manager->run_queue, j);
318 j->in_run_queue = false;
321 if (j->state != JOB_WAITING)
324 if (!job_is_runnable(j))
327 j->state = JOB_RUNNING;
332 r = unit_start(j->unit);
337 case JOB_VERIFY_ACTIVE: {
338 UnitActiveState t = unit_active_state(j->unit);
339 if (UNIT_IS_ACTIVE_OR_RELOADING(t))
341 else if (t == UNIT_ACTIVATING)
349 r = unit_stop(j->unit);
353 r = unit_reload(j->unit);
356 case JOB_RELOAD_OR_START:
357 if (unit_active_state(j->unit) == UNIT_ACTIVE)
358 r = unit_reload(j->unit);
360 r = unit_start(j->unit);
364 UnitActiveState t = unit_active_state(j->unit);
365 if (t == UNIT_INACTIVE || t == UNIT_ACTIVATING) {
367 r = unit_start(j->unit);
369 r = unit_stop(j->unit);
373 case JOB_TRY_RESTART: {
374 UnitActiveState t = unit_active_state(j->unit);
375 if (t == UNIT_INACTIVE || t == UNIT_DEACTIVATING)
377 else if (t == UNIT_ACTIVATING) {
379 r = unit_start(j->unit);
381 r = unit_stop(j->unit);
386 assert_not_reached("Unknown job type");
390 r = job_finish_and_invalidate(j, true);
391 else if (r == -EAGAIN) {
392 j->state = JOB_WAITING;
395 r = job_finish_and_invalidate(j, false);
400 int job_finish_and_invalidate(Job *j, bool success) {
407 assert(j->installed);
409 log_debug("Job %s/%s finished, success=%s", unit_id(j->unit), job_type_to_string(j->type), yes_no(success));
411 /* Patch restart jobs so that they become normal start jobs */
412 if (success && (j->type == JOB_RESTART || j->type == JOB_TRY_RESTART)) {
414 log_debug("Converting job %s/%s → %s/%s",
415 unit_id(j->unit), job_type_to_string(j->type),
416 unit_id(j->unit), job_type_to_string(JOB_START));
418 j->state = JOB_RUNNING;
429 /* Fail depending jobs on failure */
432 if (t == JOB_START ||
433 t == JOB_VERIFY_ACTIVE ||
434 t == JOB_RELOAD_OR_START) {
436 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i)
437 if (other->meta.job &&
438 (other->meta.type == JOB_START ||
439 other->meta.type == JOB_VERIFY_ACTIVE ||
440 other->meta.type == JOB_RELOAD_OR_START))
441 job_finish_and_invalidate(other->meta.job, false);
443 SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUIRED_BY], i)
444 if (other->meta.job &&
445 !other->meta.job->forced &&
446 (other->meta.type == JOB_START ||
447 other->meta.type == JOB_VERIFY_ACTIVE ||
448 other->meta.type == JOB_RELOAD_OR_START))
449 job_finish_and_invalidate(other->meta.job, false);
451 } else if (t == JOB_STOP) {
453 SET_FOREACH(other, u->meta.dependencies[UNIT_CONFLICTS], i)
454 if (other->meta.job &&
456 t == JOB_VERIFY_ACTIVE ||
457 t == JOB_RELOAD_OR_START))
458 job_finish_and_invalidate(other->meta.job, false);
462 /* Try to start the next jobs that can be started */
463 SET_FOREACH(other, u->meta.dependencies[UNIT_AFTER], i)
465 job_schedule_run(other->meta.job);
466 SET_FOREACH(other, u->meta.dependencies[UNIT_BEFORE], i)
468 job_schedule_run(other->meta.job);
473 void job_schedule_run(Job *j) {
475 assert(j->installed);
480 LIST_PREPEND(Job, run_queue, j->manager->run_queue, j);
481 j->in_run_queue = true;