]>
git.ipfire.org Git - people/ms/systemd.git/blob - manager.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
12 Manager
* manager_new(void) {
15 if (!(m
= new0(Manager
, 1)))
18 if (!(m
->names
= hashmap_new(string_hash_func
, string_compare_func
)))
21 if (!(m
->jobs
= hashmap_new(trivial_hash_func
, trivial_compare_func
)))
24 if (!(m
->transaction_jobs
= hashmap_new(trivial_hash_func
, trivial_compare_func
)))
34 void manager_free(Manager
*m
) {
40 while ((n
= hashmap_first(m
->names
)))
43 while ((j
= hashmap_steal_first(m
->transaction_jobs
)))
46 hashmap_free(m
->names
);
47 hashmap_free(m
->jobs
);
48 hashmap_free(m
->transaction_jobs
);
53 static void transaction_abort(Manager
*m
) {
58 while ((j
= hashmap_first(m
->transaction_jobs
)))
60 manager_transaction_delete_job(m
, j
);
64 assert(hashmap_isempty(m
->transaction_jobs
));
65 assert(!m
->transaction_anchor
);
68 static void transaction_find_jobs_that_matter_to_anchor(Manager
*m
, Job
*j
, unsigned generation
) {
73 for (l
= j
? j
->subject_list
: m
->transaction_anchor
; l
; l
= l
->subject_next
) {
75 /* This link does not matter */
79 /* This name has already been marked */
80 if (l
->object
->generation
== generation
)
83 l
->object
->matters_to_anchor
= true;
84 l
->object
->generation
= generation
;
86 transaction_find_jobs_that_matter_to_anchor(m
, l
->object
, generation
);
90 static bool types_match(JobType a
, JobType b
, JobType c
, JobType d
) {
96 static int types_merge(JobType
*a
, JobType b
) {
100 if (types_match(*a
, b
, JOB_START
, JOB_VERIFY_STARTED
))
102 else if (types_match(*a
, b
, JOB_START
, JOB_RELOAD
) ||
103 types_match(*a
, b
, JOB_START
, JOB_RELOAD_OR_START
) ||
104 types_match(*a
, b
, JOB_VERIFY_STARTED
, JOB_RELOAD_OR_START
) ||
105 types_match(*a
, b
, JOB_RELOAD
, JOB_RELOAD_OR_START
))
106 *a
= JOB_RELOAD_OR_START
;
107 else if (types_match(*a
, b
, JOB_START
, JOB_RESTART
) ||
108 types_match(*a
, b
, JOB_START
, JOB_TRY_RESTART
) ||
109 types_match(*a
, b
, JOB_VERIFY_STARTED
, JOB_RESTART
) ||
110 types_match(*a
, b
, JOB_RELOAD
, JOB_RESTART
) ||
111 types_match(*a
, b
, JOB_RELOAD_OR_START
, JOB_RESTART
) ||
112 types_match(*a
, b
, JOB_RELOAD_OR_START
, JOB_TRY_RESTART
) ||
113 types_match(*a
, b
, JOB_RESTART
, JOB_TRY_RESTART
))
115 else if (types_match(*a
, b
, JOB_VERIFY_STARTED
, JOB_RELOAD
))
117 else if (types_match(*a
, b
, JOB_VERIFY_STARTED
, JOB_TRY_RESTART
) ||
118 types_match(*a
, b
, JOB_RELOAD
, JOB_TRY_RESTART
))
119 *a
= JOB_TRY_RESTART
;
124 static void transaction_merge_and_delete_job(Manager
*m
, Job
*j
, Job
*other
, JobType t
) {
125 JobDependency
*l
, *last
;
129 assert(j
->name
== other
->name
);
133 j
->state
= JOB_WAITING
;
135 j
->matters_to_anchor
= j
->matters_to_anchor
|| other
->matters_to_anchor
;
137 /* Patch us in as new owner of the JobDependency objects */
139 for (l
= other
->subject_list
; l
; l
= l
->subject_next
) {
140 assert(l
->subject
== other
);
145 /* Merge both lists */
147 last
->subject_next
= j
->subject_list
;
149 j
->subject_list
->subject_prev
= last
;
150 j
->subject_list
= other
->subject_list
;
153 /* Patch us in as new owner of the JobDependency objects */
155 for (l
= other
->object_list
; l
; l
= l
->object_next
) {
156 assert(l
->object
== other
);
161 /* Merge both lists */
163 last
->object_next
= j
->object_list
;
165 j
->object_list
->object_prev
= last
;
166 j
->object_list
= other
->object_list
;
170 /* Kill the other job */
171 other
->subject_list
= NULL
;
172 other
->object_list
= NULL
;
173 manager_transaction_delete_job(m
, other
);
176 static int transaction_merge_jobs(Manager
*m
) {
183 HASHMAP_FOREACH(j
, m
->transaction_jobs
, state
) {
187 for (k
= j
->transaction_next
; k
; k
= k
->transaction_next
)
188 if ((r
= types_merge(&t
, k
->type
)) < 0)
191 while ((k
= j
->transaction_next
)) {
193 transaction_merge_and_delete_job(m
, k
, j
, t
);
196 transaction_merge_and_delete_job(m
, j
, k
, t
);
199 assert(!j
->transaction_next
);
200 assert(!j
->transaction_prev
);
206 static int transaction_verify_order_one(Manager
*m
, Job
*j
, Job
*from
, unsigned generation
) {
214 /* Did we find a cycle? */
215 if (j
->marker
&& j
->generation
== generation
) {
218 /* So, we already have been here. We have a
219 * cycle. Let's try to break it. We go backwards in our
220 * path and try to find a suitable job to remove. */
222 for (k
= from
; k
; k
= (k
->generation
== generation
? k
->marker
: NULL
)) {
223 if (!k
->matters_to_anchor
) {
224 manager_transaction_delete_job(m
, k
);
228 /* Check if this in fact was the beginning of
238 j
->generation
= generation
;
240 /* We assume that the the dependencies are both-ways, and
241 * hence can ignore NAME_AFTER */
243 SET_FOREACH(n
, j
->name
->meta
.dependencies
[NAME_BEFORE
], state
) {
246 if (!(o
= hashmap_get(m
->transaction_jobs
, n
)))
247 if (!(o
= n
->meta
.job
))
250 if ((r
= transaction_verify_order_one(m
, o
, j
, generation
)) < 0)
257 static int transaction_verify_order(Manager
*m
, unsigned *generation
) {
269 HASHMAP_FOREACH(j
, m
->transaction_jobs
, state
) {
272 assert(!j
->transaction_next
);
273 assert(!j
->transaction_prev
);
275 if ((r
= transaction_verify_order_one(m
, j
, NULL
, (*generation
)++)) < 0) {
277 /* There was a cycleq, but it was fixed,
278 * we need to restart our algorithm */
292 static void transaction_collect_garbage(Manager
*m
) {
303 HASHMAP_FOREACH(j
, m
->transaction_jobs
, state
) {
307 manager_transaction_delete_job(m
, j
);
315 static int transaction_is_destructive(Manager
*m
, JobMode mode
) {
321 /* Checks whether applying this transaction means that
322 * existing jobs would be replaced */
324 HASHMAP_FOREACH(j
, m
->transaction_jobs
, state
)
325 if (j
->name
->meta
.job
&& j
->name
->meta
.job
!= j
)
331 static int transaction_apply(Manager
*m
, JobMode mode
) {
336 HASHMAP_FOREACH(j
, m
->transaction_jobs
, state
) {
340 if ((r
= hashmap_put(m
->jobs
, UINT32_TO_PTR(j
->id
), j
)) < 0)
344 while ((j
= hashmap_steal_first(m
->transaction_jobs
))) {
348 if (j
->name
->meta
.job
)
349 job_free(j
->name
->meta
.job
);
351 j
->name
->meta
.job
= j
;
354 /* We're fully installed. Now let's free data we don't
357 assert(!j
->transaction_next
);
358 assert(!j
->transaction_prev
);
360 while (j
->subject_list
)
361 job_dependency_free(j
->subject_list
);
362 while (j
->object_list
)
363 job_dependency_free(j
->object_list
);
370 HASHMAP_FOREACH(j
, m
->transaction_jobs
, state
) {
374 hashmap_remove(m
->jobs
, UINT32_TO_PTR(j
->id
));
381 static int transaction_activate(Manager
*m
, JobMode mode
) {
383 unsigned generation
= 1;
387 /* This applies the changes recorded in transaction_jobs to
388 * the actual list of jobs, if possible. */
390 /* First step: figure out which jobs matter */
391 transaction_find_jobs_that_matter_to_anchor(m
, NULL
, generation
++);
393 /* Second step: let's merge entries we can merge */
394 if ((r
= transaction_merge_jobs(m
)) < 0)
397 /* Third step: verify order makes sense */
398 if ((r
= transaction_verify_order(m
, &generation
)) < 0)
401 /* Third step: do garbage colletion */
402 transaction_collect_garbage(m
);
404 /* Fourth step: check whether we can actually apply this */
405 if (mode
== JOB_FAIL
)
406 if ((r
= transaction_is_destructive(m
, mode
)) < 0)
409 /* Fifth step: apply changes */
410 if ((r
= transaction_apply(m
, mode
)) < 0)
413 assert(hashmap_isempty(m
->transaction_jobs
));
414 assert(!m
->transaction_anchor
);
419 transaction_abort(m
);
423 static Job
* transaction_add_one_job(Manager
*m
, JobType type
, Name
*name
, bool *is_new
) {
430 /* Looks for an axisting prospective job and returns that. If
431 * it doesn't exist it is created and added to the prospective
434 f
= hashmap_get(m
->transaction_jobs
, name
);
436 for (j
= f
; j
; j
= j
->transaction_next
) {
437 assert(j
->name
== name
);
439 if (j
->type
== type
) {
446 if (name
->meta
.job
&& name
->meta
.job
->type
== type
)
448 else if (!(j
= job_new(m
, type
, name
)))
451 if ((r
= hashmap_replace(m
->transaction_jobs
, name
, j
)) < 0) {
456 j
->transaction_next
= f
;
459 f
->transaction_prev
= j
;
463 j
->matters_to_anchor
= false;
471 void manager_transaction_delete_job(Manager
*m
, Job
*j
) {
475 if (j
->transaction_prev
)
476 j
->transaction_prev
->transaction_next
= j
->transaction_next
;
477 else if (j
->transaction_next
)
478 hashmap_replace(m
->transaction_jobs
, j
->name
, j
->transaction_next
);
480 hashmap_remove_value(m
->transaction_jobs
, j
->name
, j
);
482 if (j
->transaction_next
)
483 j
->transaction_next
->transaction_prev
= j
->transaction_prev
;
485 j
->transaction_prev
= j
->transaction_next
= NULL
;
487 while (j
->subject_list
)
488 job_dependency_free(j
->subject_list
);
489 while (j
->object_list
)
490 job_dependency_free(j
->object_list
);
493 static int transaction_add_job_and_dependencies(Manager
*m
, JobType type
, Name
*name
, Job
*by
, bool matters
, bool force
, Job
**_ret
) {
501 assert(type
< _JOB_TYPE_MAX
);
504 /* First add the job. */
505 if (!(ret
= transaction_add_one_job(m
, type
, name
, &is_new
)))
508 /* Then, add a link to the job. */
509 if (!job_dependency_new(by
, ret
, matters
))
513 /* Finally, recursively add in all dependencies. */
514 if (type
== JOB_START
|| type
== JOB_RELOAD_OR_START
) {
515 SET_FOREACH(dep
, ret
->name
->meta
.dependencies
[NAME_REQUIRES
], state
)
516 if ((r
= transaction_add_job_and_dependencies(m
, JOB_START
, dep
, ret
, true, force
, NULL
)) < 0)
518 SET_FOREACH(dep
, ret
->name
->meta
.dependencies
[NAME_SOFT_REQUIRES
], state
)
519 if ((r
= transaction_add_job_and_dependencies(m
, JOB_START
, dep
, ret
, !force
, force
, NULL
)) < 0)
521 SET_FOREACH(dep
, ret
->name
->meta
.dependencies
[NAME_WANTS
], state
)
522 if ((r
= transaction_add_job_and_dependencies(m
, JOB_START
, dep
, ret
, false, force
, NULL
)) < 0)
524 SET_FOREACH(dep
, ret
->name
->meta
.dependencies
[NAME_REQUISITE
], state
)
525 if ((r
= transaction_add_job_and_dependencies(m
, JOB_VERIFY_STARTED
, dep
, ret
, true, force
, NULL
)) < 0)
527 SET_FOREACH(dep
, ret
->name
->meta
.dependencies
[NAME_SOFT_REQUISITE
], state
)
528 if ((r
= transaction_add_job_and_dependencies(m
, JOB_VERIFY_STARTED
, dep
, ret
, !force
, force
, NULL
)) < 0)
530 SET_FOREACH(dep
, ret
->name
->meta
.dependencies
[NAME_CONFLICTS
], state
)
531 if ((r
= transaction_add_job_and_dependencies(m
, JOB_STOP
, dep
, ret
, true, force
, NULL
)) < 0)
534 } else if (type
== JOB_STOP
|| type
== JOB_RESTART
|| type
== JOB_TRY_RESTART
) {
536 SET_FOREACH(dep
, ret
->name
->meta
.dependencies
[NAME_REQUIRED_BY
], state
)
537 if ((r
= transaction_add_job_and_dependencies(m
, type
, dep
, ret
, true, force
, NULL
)) < 0)
541 /* JOB_VERIFY_STARTED, JOB_RELOAD require no dependency handling */
550 int manager_add_job(Manager
*m
, JobType type
, Name
*name
, JobMode mode
, bool force
, Job
**_ret
) {
555 assert(type
< _JOB_TYPE_MAX
);
557 assert(mode
< _JOB_MODE_MAX
);
559 if ((r
= transaction_add_job_and_dependencies(m
, type
, name
, NULL
, true, force
, &ret
))) {
560 transaction_abort(m
);
564 if ((r
= transaction_activate(m
, mode
)) < 0)
573 Job
*manager_get_job(Manager
*m
, uint32_t id
) {
576 return hashmap_get(m
->jobs
, UINT32_TO_PTR(id
));
579 Name
*manager_get_name(Manager
*m
, const char *name
) {
583 return hashmap_get(m
->names
, name
);
586 static int dispatch_load_queue(Manager
*m
) {
591 /* Make sure we are not run recursively */
592 if (m
->dispatching_load_queue
)
595 m
->dispatching_load_queue
= true;
597 /* Dispatches the load queue. Takes a name from the queue and
598 * tries to load its data until the queue is empty */
600 while ((meta
= m
->load_queue
)) {
601 name_load(NAME(meta
));
602 LIST_REMOVE(Meta
, m
->load_queue
, meta
);
605 m
->dispatching_load_queue
= false;
610 int manager_load_name(Manager
*m
, const char *name
, Name
**_ret
) {
620 if (!name_is_valid(name
))
623 /* This will load the service information files, but not actually
624 * start any services or anything */
626 if ((ret
= manager_get_name(m
, name
)))
629 if ((t
= name_type_from_string(name
)) == _NAME_TYPE_INVALID
)
632 if (!(ret
= name_new(m
)))
637 if (!(n
= strdup(name
))) {
642 if (set_put(ret
->meta
.names
, n
) < 0) {
648 if ((r
= name_link(ret
)) < 0) {
653 /* At this point the new entry is created and linked. However,
654 * not loaded. Now load this entry and all its dependencies
657 dispatch_load_queue(m
);
665 void manager_dump_jobs(Manager
*s
, FILE *f
, const char *prefix
) {
672 HASHMAP_FOREACH(j
, s
->jobs
, state
)
673 job_dump(j
, f
, prefix
);
676 void manager_dump_names(Manager
*s
, FILE *f
, const char *prefix
) {
684 HASHMAP_FOREACH_KEY(n
, t
, s
->names
, state
)
686 name_dump(n
, f
, prefix
);
689 void manager_clear_jobs(Manager
*m
) {
694 transaction_abort(m
);
696 while ((j
= hashmap_first(m
->jobs
)))