]>
git.ipfire.org Git - thirdparty/systemd.git/blob - manager.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
13 Manager
* manager_new(void) {
16 if (!(m
= new0(Manager
, 1)))
19 if (!(m
->names
= hashmap_new(string_hash_func
, string_compare_func
)))
22 if (!(m
->jobs
= hashmap_new(trivial_hash_func
, trivial_compare_func
)))
25 if (!(m
->transaction_jobs
= hashmap_new(trivial_hash_func
, trivial_compare_func
)))
35 void manager_free(Manager
*m
) {
41 while ((n
= hashmap_first(m
->names
)))
44 while ((j
= hashmap_steal_first(m
->transaction_jobs
)))
47 hashmap_free(m
->names
);
48 hashmap_free(m
->jobs
);
49 hashmap_free(m
->transaction_jobs
);
54 static void transaction_delete_job(Manager
*m
, Job
*j
) {
58 /* Deletes one job from the transaction */
60 manager_transaction_unlink_job(m
, j
);
66 static void transaction_delete_name(Manager
*m
, Name
*n
) {
69 /* Deletes all jobs associated with a certain name from the
72 while ((j
= hashmap_get(m
->transaction_jobs
, n
)))
73 transaction_delete_job(m
, j
);
76 static void transaction_abort(Manager
*m
) {
81 while ((j
= hashmap_first(m
->transaction_jobs
)))
83 transaction_delete_job(m
, j
);
87 assert(hashmap_isempty(m
->transaction_jobs
));
88 assert(!m
->transaction_anchor
);
91 static void transaction_find_jobs_that_matter_to_anchor(Manager
*m
, Job
*j
, unsigned generation
) {
96 /* A recursive sweep through the graph that marks all names
97 * that matter to the anchor job, i.e. are directly or
98 * indirectly a dependency of the anchor job via paths that
99 * are fully marked as mattering. */
101 for (l
= j
? j
->subject_list
: m
->transaction_anchor
; l
; l
= l
->subject_next
) {
103 /* This link does not matter */
107 /* This name has already been marked */
108 if (l
->object
->generation
== generation
)
111 l
->object
->matters_to_anchor
= true;
112 l
->object
->generation
= generation
;
114 transaction_find_jobs_that_matter_to_anchor(m
, l
->object
, generation
);
118 static void transaction_merge_and_delete_job(Manager
*m
, Job
*j
, Job
*other
, JobType t
) {
119 JobDependency
*l
, *last
;
123 assert(j
->name
== other
->name
);
126 /* Merges 'other' into 'j' and then deletes j. */
129 j
->state
= JOB_WAITING
;
131 j
->matters_to_anchor
= j
->matters_to_anchor
|| other
->matters_to_anchor
;
133 /* Patch us in as new owner of the JobDependency objects */
135 for (l
= other
->subject_list
; l
; l
= l
->subject_next
) {
136 assert(l
->subject
== other
);
141 /* Merge both lists */
143 last
->subject_next
= j
->subject_list
;
145 j
->subject_list
->subject_prev
= last
;
146 j
->subject_list
= other
->subject_list
;
149 /* Patch us in as new owner of the JobDependency objects */
151 for (l
= other
->object_list
; l
; l
= l
->object_next
) {
152 assert(l
->object
== other
);
157 /* Merge both lists */
159 last
->object_next
= j
->object_list
;
161 j
->object_list
->object_prev
= last
;
162 j
->object_list
= other
->object_list
;
165 /* Kill the other job */
166 other
->subject_list
= NULL
;
167 other
->object_list
= NULL
;
168 transaction_delete_job(m
, other
);
171 static int delete_one_unmergable_job(Manager
*m
, Job
*j
) {
176 /* Tries to delete one item in the linked list
177 * j->transaction_next->transaction_next->... that conflicts
178 * whith another one, in an attempt to make an inconsistent
179 * transaction work. */
181 /* We rely here on the fact that if a merged with b does not
182 * merge with c, either a or b merge with c neither */
183 for (; j
; j
= j
->transaction_next
)
184 for (k
= j
->transaction_next
; k
; k
= k
->transaction_next
) {
187 /* Is this one mergeable? Then skip it */
188 if (job_type_mergeable(j
->type
, k
->type
))
191 /* Ok, we found two that conflict, let's see if we can
192 * drop one of them */
193 if (!j
->matters_to_anchor
)
195 else if (!k
->matters_to_anchor
)
200 /* Ok, we can drop one, so let's do so. */
201 log_debug("Try to fix job merging by deleting job %s", name_id(d
->name
));
202 transaction_delete_job(m
, d
);
209 static int transaction_merge_jobs(Manager
*m
) {
216 /* First step, check whether any of the jobs for one specific
217 * task conflict. If so, try to drop one of them. */
218 HASHMAP_FOREACH(j
, m
->transaction_jobs
, state
) {
223 for (k
= j
->transaction_next
; k
; k
= k
->transaction_next
) {
224 if ((r
= job_type_merge(&t
, k
->type
)) >= 0)
227 /* OK, we could not merge all jobs for this
228 * action. Let's see if we can get rid of one
231 if ((r
= delete_one_unmergable_job(m
, j
)) >= 0)
232 /* Ok, we managed to drop one, now
233 * let's ask our callers to call us
234 * again after garbage collecting */
237 /* We couldn't merge anything. Failure */
242 /* Second step, merge the jobs. */
243 HASHMAP_FOREACH(j
, m
->transaction_jobs
, state
) {
247 /* Merge all transactions */
248 for (k
= j
->transaction_next
; k
; k
= k
->transaction_next
)
249 assert_se(job_type_merge(&t
, k
->type
) == 0);
251 /* If an active job is mergable, merge it too */
252 if (j
->name
->meta
.job
)
253 job_type_merge(&t
, j
->name
->meta
.job
->type
); /* Might fail. Which is OK */
255 while ((k
= j
->transaction_next
)) {
257 transaction_merge_and_delete_job(m
, k
, j
, t
);
260 transaction_merge_and_delete_job(m
, j
, k
, t
);
263 assert(!j
->transaction_next
);
264 assert(!j
->transaction_prev
);
270 static bool name_matters_to_anchor(Name
*n
, Job
*j
) {
272 assert(!j
->transaction_prev
);
274 /* Checks whether at least one of the jobs for this name
275 * matters to the anchor. */
277 for (; j
; j
= j
->transaction_next
)
278 if (j
->matters_to_anchor
)
284 static int transaction_verify_order_one(Manager
*m
, Job
*j
, Job
*from
, unsigned generation
) {
291 assert(!j
->transaction_prev
);
293 /* Does a recursive sweep through the ordering graph, looking
294 * for a cycle. If we find cycle we try to break it. */
296 /* Did we find a cycle? */
297 if (j
->marker
&& j
->generation
== generation
) {
300 /* So, we already have been here. We have a
301 * cycle. Let's try to break it. We go backwards in
302 * our path and try to find a suitable job to
303 * remove. We use the marker to find our way back,
304 * since smart how we are we stored our way back in
307 for (k
= from
; k
; k
= (k
->generation
== generation
? k
->marker
: NULL
)) {
310 !name_matters_to_anchor(k
->name
, k
)) {
311 /* Ok, we can drop this one, so let's
313 log_debug("Breaking order cycle by deleting job %s", name_id(k
->name
));
314 transaction_delete_name(m
, k
->name
);
318 /* Check if this in fact was the beginning of
327 /* Make the marker point to where we come from, so that we can
328 * find our way backwards if we want to break a cycle */
330 j
->generation
= generation
;
332 /* We assume that the the dependencies are bidirectional, and
333 * hence can ignore NAME_AFTER */
334 SET_FOREACH(n
, j
->name
->meta
.dependencies
[NAME_BEFORE
], state
) {
337 /* Is there a job for this name? */
338 if (!(o
= hashmap_get(m
->transaction_jobs
, n
)))
340 /* Ok, there is no job for this in the
341 * transaction, but maybe there is already one
343 if (!(o
= n
->meta
.job
))
346 if ((r
= transaction_verify_order_one(m
, o
, j
, generation
)) < 0)
353 static int transaction_verify_order(Manager
*m
, unsigned *generation
) {
361 /* Check if the ordering graph is cyclic. If it is, try to fix
362 * that up by dropping one of the jobs. */
364 HASHMAP_FOREACH(j
, m
->transaction_jobs
, state
)
365 if ((r
= transaction_verify_order_one(m
, j
, NULL
, (*generation
)++)) < 0)
371 static void transaction_collect_garbage(Manager
*m
) {
376 /* Drop jobs that are not required by any other job */
384 HASHMAP_FOREACH(j
, m
->transaction_jobs
, state
) {
388 log_debug("Garbage collecting job %s", name_id(j
->name
));
390 transaction_delete_job(m
, j
);
398 static int transaction_is_destructive(Manager
*m
, JobMode mode
) {
404 /* Checks whether applying this transaction means that
405 * existing jobs would be replaced */
407 HASHMAP_FOREACH(j
, m
->transaction_jobs
, state
) {
410 assert(!j
->transaction_prev
);
411 assert(!j
->transaction_next
);
413 if (j
->name
->meta
.job
&&
414 j
->name
->meta
.job
!= j
&&
415 !job_type_is_superset(j
->type
, j
->name
->meta
.job
->type
))
422 static void transaction_minimize_impact(Manager
*m
) {
426 /* Drops all unnecessary jobs that reverse already active jobs
427 * or that stop a running service. */
435 HASHMAP_FOREACH(j
, m
->transaction_jobs
, state
) {
436 for (; j
; j
= j
->transaction_next
) {
438 /* If it matters, we shouldn't drop it */
439 if (j
->matters_to_anchor
)
442 /* Would this stop a running service?
443 * Would this change an existing job?
444 * If so, let's drop this entry */
445 if ((j
->type
!= JOB_STOP
|| name_is_dead(j
->name
)) &&
446 (!j
->name
->meta
.job
|| job_type_is_conflicting(j
->type
, j
->name
->meta
.job
->state
)))
449 /* Ok, let's get rid of this */
450 transaction_delete_job(m
, j
);
462 static int transaction_apply(Manager
*m
, JobMode mode
) {
467 /* Moves the transaction jobs to the set of active jobs */
469 HASHMAP_FOREACH(j
, m
->transaction_jobs
, state
) {
471 assert(!j
->transaction_prev
);
472 assert(!j
->transaction_next
);
477 if ((r
= hashmap_put(m
->jobs
, UINT32_TO_PTR(j
->id
), j
)) < 0)
481 while ((j
= hashmap_steal_first(m
->transaction_jobs
))) {
485 if (j
->name
->meta
.job
)
486 job_free(j
->name
->meta
.job
);
488 j
->name
->meta
.job
= j
;
491 /* We're fully installed. Now let's free data we don't
494 assert(!j
->transaction_next
);
495 assert(!j
->transaction_prev
);
497 while (j
->subject_list
)
498 job_dependency_free(j
->subject_list
);
499 while (j
->object_list
)
500 job_dependency_free(j
->object_list
);
503 m
->transaction_anchor
= NULL
;
509 HASHMAP_FOREACH(j
, m
->transaction_jobs
, state
) {
513 hashmap_remove(m
->jobs
, UINT32_TO_PTR(j
->id
));
519 static int transaction_activate(Manager
*m
, JobMode mode
) {
521 unsigned generation
= 1;
525 /* This applies the changes recorded in transaction_jobs to
526 * the actual list of jobs, if possible. */
528 /* First step: figure out which jobs matter */
529 transaction_find_jobs_that_matter_to_anchor(m
, NULL
, generation
++);
531 /* Second step: Try not to stop any running services if
532 * we don't have to. Don't try to reverse running
533 * jobs if we don't have to. */
534 transaction_minimize_impact(m
);
537 /* Third step: Let's remove unneeded jobs that might
539 transaction_collect_garbage(m
);
541 /* Fourth step: verify order makes sense and correct
542 * cycles if necessary and possible */
543 if ((r
= transaction_verify_order(m
, &generation
)) >= 0)
549 /* Let's see if the resulting transaction ordering
550 * graph is still cyclic... */
554 /* Fifth step: let's drop unmergable entries if
555 * necessary and possible, merge entries we can
557 if ((r
= transaction_merge_jobs(m
)) >= 0)
563 /* Sixth step: an entry got dropped, let's garbage
564 * collect its dependencies. */
565 transaction_collect_garbage(m
);
567 /* Let's see if the resulting transaction still has
568 * unmergable entries ... */
571 /* Seventh step: check whether we can actually apply this */
572 if (mode
== JOB_FAIL
)
573 if ((r
= transaction_is_destructive(m
, mode
)) < 0)
576 /* Eights step: apply changes */
577 if ((r
= transaction_apply(m
, mode
)) < 0)
580 assert(hashmap_isempty(m
->transaction_jobs
));
581 assert(!m
->transaction_anchor
);
586 transaction_abort(m
);
590 static Job
* transaction_add_one_job(Manager
*m
, JobType type
, Name
*name
, bool *is_new
) {
597 /* Looks for an axisting prospective job and returns that. If
598 * it doesn't exist it is created and added to the prospective
601 f
= hashmap_get(m
->transaction_jobs
, name
);
603 for (j
= f
; j
; j
= j
->transaction_next
) {
604 assert(j
->name
== name
);
606 if (j
->type
== type
) {
613 if (name
->meta
.job
&& name
->meta
.job
->type
== type
)
615 else if (!(j
= job_new(m
, type
, name
)))
618 if ((r
= hashmap_replace(m
->transaction_jobs
, name
, j
)) < 0) {
623 j
->transaction_next
= f
;
626 f
->transaction_prev
= j
;
630 j
->matters_to_anchor
= false;
638 void manager_transaction_unlink_job(Manager
*m
, Job
*j
) {
642 if (j
->transaction_prev
)
643 j
->transaction_prev
->transaction_next
= j
->transaction_next
;
644 else if (j
->transaction_next
)
645 hashmap_replace(m
->transaction_jobs
, j
->name
, j
->transaction_next
);
647 hashmap_remove_value(m
->transaction_jobs
, j
->name
, j
);
649 if (j
->transaction_next
)
650 j
->transaction_next
->transaction_prev
= j
->transaction_prev
;
652 j
->transaction_prev
= j
->transaction_next
= NULL
;
654 while (j
->subject_list
)
655 job_dependency_free(j
->subject_list
);
657 while (j
->object_list
) {
658 Job
*other
= j
->object_list
->matters
? j
->object_list
->subject
: NULL
;
660 job_dependency_free(j
->object_list
);
663 log_debug("Deleting job %s as dependency of job %s", name_id(other
->name
), name_id(j
->name
));
664 transaction_delete_job(m
, other
);
669 static int transaction_add_job_and_dependencies(Manager
*m
, JobType type
, Name
*name
, Job
*by
, bool matters
, bool force
, Job
**_ret
) {
677 assert(type
< _JOB_TYPE_MAX
);
680 if (name
->meta
.state
!= NAME_LOADED
)
683 /* First add the job. */
684 if (!(ret
= transaction_add_one_job(m
, type
, name
, &is_new
)))
687 /* Then, add a link to the job. */
688 if (!job_dependency_new(by
, ret
, matters
))
692 /* Finally, recursively add in all dependencies. */
693 if (type
== JOB_START
|| type
== JOB_RELOAD_OR_START
) {
694 SET_FOREACH(dep
, ret
->name
->meta
.dependencies
[NAME_REQUIRES
], state
)
695 if ((r
= transaction_add_job_and_dependencies(m
, JOB_START
, dep
, ret
, true, force
, NULL
)) < 0)
697 SET_FOREACH(dep
, ret
->name
->meta
.dependencies
[NAME_SOFT_REQUIRES
], state
)
698 if ((r
= transaction_add_job_and_dependencies(m
, JOB_START
, dep
, ret
, !force
, force
, NULL
)) < 0)
700 SET_FOREACH(dep
, ret
->name
->meta
.dependencies
[NAME_WANTS
], state
)
701 if ((r
= transaction_add_job_and_dependencies(m
, JOB_START
, dep
, ret
, false, force
, NULL
)) < 0)
703 SET_FOREACH(dep
, ret
->name
->meta
.dependencies
[NAME_REQUISITE
], state
)
704 if ((r
= transaction_add_job_and_dependencies(m
, JOB_VERIFY_STARTED
, dep
, ret
, true, force
, NULL
)) < 0)
706 SET_FOREACH(dep
, ret
->name
->meta
.dependencies
[NAME_SOFT_REQUISITE
], state
)
707 if ((r
= transaction_add_job_and_dependencies(m
, JOB_VERIFY_STARTED
, dep
, ret
, !force
, force
, NULL
)) < 0)
709 SET_FOREACH(dep
, ret
->name
->meta
.dependencies
[NAME_CONFLICTS
], state
)
710 if ((r
= transaction_add_job_and_dependencies(m
, JOB_STOP
, dep
, ret
, true, force
, NULL
)) < 0)
713 } else if (type
== JOB_STOP
|| type
== JOB_RESTART
|| type
== JOB_TRY_RESTART
) {
715 SET_FOREACH(dep
, ret
->name
->meta
.dependencies
[NAME_REQUIRED_BY
], state
)
716 if ((r
= transaction_add_job_and_dependencies(m
, type
, dep
, ret
, true, force
, NULL
)) < 0)
720 /* JOB_VERIFY_STARTED, JOB_RELOAD require no dependency handling */
729 int manager_add_job(Manager
*m
, JobType type
, Name
*name
, JobMode mode
, bool force
, Job
**_ret
) {
734 assert(type
< _JOB_TYPE_MAX
);
736 assert(mode
< _JOB_MODE_MAX
);
738 if ((r
= transaction_add_job_and_dependencies(m
, type
, name
, NULL
, true, force
, &ret
))) {
739 transaction_abort(m
);
743 if ((r
= transaction_activate(m
, mode
)) < 0)
752 Job
*manager_get_job(Manager
*m
, uint32_t id
) {
755 return hashmap_get(m
->jobs
, UINT32_TO_PTR(id
));
758 Name
*manager_get_name(Manager
*m
, const char *name
) {
762 return hashmap_get(m
->names
, name
);
765 static int dispatch_load_queue(Manager
*m
) {
770 /* Make sure we are not run recursively */
771 if (m
->dispatching_load_queue
)
774 m
->dispatching_load_queue
= true;
776 /* Dispatches the load queue. Takes a name from the queue and
777 * tries to load its data until the queue is empty */
779 while ((meta
= m
->load_queue
)) {
780 name_load(NAME(meta
));
781 LIST_REMOVE(Meta
, m
->load_queue
, meta
);
784 m
->dispatching_load_queue
= false;
789 int manager_load_name(Manager
*m
, const char *name
, Name
**_ret
) {
799 if (!name_is_valid(name
))
802 /* This will load the service information files, but not actually
803 * start any services or anything */
805 if ((ret
= manager_get_name(m
, name
)))
808 if ((t
= name_type_from_string(name
)) == _NAME_TYPE_INVALID
)
811 if (!(ret
= name_new(m
)))
816 if (!(n
= strdup(name
))) {
821 if ((r
= set_put(ret
->meta
.names
, n
)) < 0) {
827 if ((r
= name_link(ret
)) < 0) {
832 /* At this point the new entry is created and linked. However,
833 * not loaded. Now load this entry and all its dependencies
836 dispatch_load_queue(m
);
844 void manager_dump_jobs(Manager
*s
, FILE *f
, const char *prefix
) {
851 HASHMAP_FOREACH(j
, s
->jobs
, state
)
852 job_dump(j
, f
, prefix
);
855 void manager_dump_names(Manager
*s
, FILE *f
, const char *prefix
) {
863 HASHMAP_FOREACH_KEY(n
, t
, s
->names
, state
)
865 name_dump(n
, f
, prefix
);
868 void manager_clear_jobs(Manager
*m
) {
873 transaction_abort(m
);
875 while ((j
= hashmap_first(m
->jobs
)))