]>
git.ipfire.org Git - people/ms/systemd.git/blob - manager.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
11 #include "load-fragment.h"
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_abort(Manager
*m
) {
59 while ((j
= hashmap_first(m
->transaction_jobs
)))
61 manager_transaction_delete_job(m
, j
);
65 assert(hashmap_isempty(m
->transaction_jobs
));
66 assert(!m
->transaction_anchor
);
69 static void transaction_find_jobs_that_matter_to_anchor(Manager
*m
, Job
*j
, unsigned generation
) {
74 for (l
= j
? j
->subject_list
: m
->transaction_anchor
; l
; l
= l
->subject_next
) {
76 /* This link does not matter */
80 /* This name has already been marked */
81 if (l
->object
->generation
== generation
)
84 l
->object
->matters_to_anchor
= true;
85 l
->object
->generation
= generation
;
87 transaction_find_jobs_that_matter_to_anchor(m
, l
->object
, generation
);
91 static bool types_match(JobType a
, JobType b
, JobType c
, JobType d
) {
97 static int types_merge(JobType
*a
, JobType b
) {
101 if (types_match(*a
, b
, JOB_START
, JOB_VERIFY_STARTED
))
103 else if (types_match(*a
, b
, JOB_START
, JOB_RELOAD
) ||
104 types_match(*a
, b
, JOB_START
, JOB_RELOAD_OR_START
) ||
105 types_match(*a
, b
, JOB_VERIFY_STARTED
, JOB_RELOAD_OR_START
) ||
106 types_match(*a
, b
, JOB_RELOAD
, JOB_RELOAD_OR_START
))
107 *a
= JOB_RELOAD_OR_START
;
108 else if (types_match(*a
, b
, JOB_START
, JOB_RESTART
) ||
109 types_match(*a
, b
, JOB_START
, JOB_TRY_RESTART
) ||
110 types_match(*a
, b
, JOB_VERIFY_STARTED
, JOB_RESTART
) ||
111 types_match(*a
, b
, JOB_RELOAD
, JOB_RESTART
) ||
112 types_match(*a
, b
, JOB_RELOAD_OR_START
, JOB_RESTART
) ||
113 types_match(*a
, b
, JOB_RELOAD_OR_START
, JOB_TRY_RESTART
) ||
114 types_match(*a
, b
, JOB_RESTART
, JOB_TRY_RESTART
))
116 else if (types_match(*a
, b
, JOB_VERIFY_STARTED
, JOB_RELOAD
))
118 else if (types_match(*a
, b
, JOB_VERIFY_STARTED
, JOB_TRY_RESTART
) ||
119 types_match(*a
, b
, JOB_RELOAD
, JOB_TRY_RESTART
))
120 *a
= JOB_TRY_RESTART
;
125 static void manager_merge_and_delete_prospective_job(Manager
*m
, Job
*j
, Job
*other
, JobType t
) {
126 JobDependency
*l
, *last
;
130 assert(j
->name
== other
->name
);
134 j
->state
= JOB_WAITING
;
136 j
->matters_to_anchor
= j
->matters_to_anchor
|| other
->matters_to_anchor
;
138 /* Patch us in as new owner of the JobDependency objects */
140 for (l
= other
->subject_list
; l
; l
= l
->subject_next
) {
141 assert(l
->subject
== other
);
146 /* Merge both lists */
148 last
->subject_next
= j
->subject_list
;
150 j
->subject_list
->subject_prev
= last
;
151 j
->subject_list
= other
->subject_list
;
154 /* Patch us in as new owner of the JobDependency objects */
156 for (l
= other
->object_list
; l
; l
= l
->object_next
) {
157 assert(l
->object
== other
);
162 /* Merge both lists */
164 last
->object_next
= j
->object_list
;
166 j
->object_list
->object_prev
= last
;
167 j
->object_list
= other
->object_list
;
171 /* Kill the other job */
172 other
->subject_list
= NULL
;
173 other
->object_list
= NULL
;
174 manager_transaction_delete_job(m
, other
);
177 static int transaction_merge_jobs(Manager
*m
) {
184 HASHMAP_FOREACH(j
, m
->transaction_jobs
, state
) {
188 for (k
= j
->transaction_next
; k
; k
= k
->transaction_next
)
189 if ((r
= types_merge(&t
, k
->type
)) < 0)
192 while ((k
= j
->transaction_next
)) {
194 manager_merge_and_delete_prospective_job(m
, k
, j
, t
);
197 manager_merge_and_delete_prospective_job(m
, j
, k
, t
);
200 assert(!j
->transaction_next
);
201 assert(!j
->transaction_prev
);
207 static int transaction_verify_order_one(Manager
*m
, Job
*j
, Job
*from
, unsigned generation
) {
215 /* Did we find a loop? */
216 if (j
->marker
&& j
->generation
== generation
) {
219 /* So, we already have been here. We have a
220 * loop. Let's try to break it. We go backwards in our
221 * path and try to find a suitable job to remove. */
223 for (k
= from
; k
; k
= (k
->generation
== generation
? k
->marker
: NULL
)) {
224 if (!k
->matters_to_anchor
) {
225 manager_transaction_delete_job(m
, k
);
229 /* Check if this in fact was the beginning of
239 j
->generation
= generation
;
241 /* We assume that the the dependencies are both-ways, and
242 * hence can ignore NAME_AFTER */
244 SET_FOREACH(n
, j
->name
->meta
.dependencies
[NAME_BEFORE
], state
) {
247 if (!(o
= hashmap_get(m
->transaction_jobs
, n
)))
248 if (!(o
= n
->meta
.job
))
251 if ((r
= transaction_verify_order_one(m
, o
, j
, generation
)) < 0)
258 static int transaction_verify_order(Manager
*m
, unsigned *generation
) {
270 HASHMAP_FOREACH(j
, m
->transaction_jobs
, state
) {
273 assert(!j
->transaction_next
);
274 assert(!j
->transaction_prev
);
276 if ((r
= transaction_verify_order_one(m
, j
, NULL
, (*generation
)++)) < 0) {
278 /* There was a loop, but it was fixed,
279 * we need to restart our algorithm */
293 static void transaction_collect_garbage(Manager
*m
) {
304 HASHMAP_FOREACH(j
, m
->transaction_jobs
, state
) {
308 manager_transaction_delete_job(m
, j
);
316 static int transaction_is_destructive(Manager
*m
, JobMode mode
) {
322 /* Checks whether applying this transaction means that
323 * existing jobs would be replaced */
325 HASHMAP_FOREACH(j
, m
->transaction_jobs
, state
)
326 if (j
->name
->meta
.job
&& j
->name
->meta
.job
!= j
)
332 static int transaction_apply(Manager
*m
, JobMode mode
) {
337 HASHMAP_FOREACH(j
, m
->transaction_jobs
, state
) {
341 if ((r
= hashmap_put(m
->jobs
, UINT32_TO_PTR(j
->id
), j
)) < 0)
345 while ((j
= hashmap_steal_first(m
->transaction_jobs
))) {
349 if (j
->name
->meta
.job
)
350 job_free(j
->name
->meta
.job
);
352 j
->name
->meta
.job
= j
;
355 /* We're fully installed. Now let's free data we don't
358 assert(!j
->transaction_next
);
359 assert(!j
->transaction_prev
);
361 while (j
->subject_list
)
362 job_dependency_free(j
->subject_list
);
363 while (j
->object_list
)
364 job_dependency_free(j
->object_list
);
371 HASHMAP_FOREACH(j
, m
->transaction_jobs
, state
) {
375 hashmap_remove(m
->jobs
, UINT32_TO_PTR(j
->id
));
382 static int transaction_activate(Manager
*m
, JobMode mode
) {
384 unsigned generation
= 1;
388 /* This applies the changes recorded in transaction_jobs to
389 * the actual list of jobs, if possible. */
391 /* First step: figure out which jobs matter */
392 transaction_find_jobs_that_matter_to_anchor(m
, NULL
, generation
++);
394 /* Second step: let's merge entries we can merge */
395 if ((r
= transaction_merge_jobs(m
)) < 0)
398 /* Third step: verify order makes sense */
399 if ((r
= transaction_verify_order(m
, &generation
)) < 0)
402 /* Third step: do garbage colletion */
403 transaction_collect_garbage(m
);
405 /* Fourth step: check whether we can actually apply this */
406 if (mode
== JOB_FAIL
)
407 if ((r
= transaction_is_destructive(m
, mode
)) < 0)
410 /* Fifth step: apply changes */
411 if ((r
= transaction_apply(m
, mode
)) < 0)
414 assert(hashmap_isempty(m
->transaction_jobs
));
415 assert(!m
->transaction_anchor
);
420 transaction_abort(m
);
424 static Job
* transaction_add_one_job(Manager
*m
, JobType type
, Name
*name
, bool *is_new
) {
431 /* Looks for an axisting prospective job and returns that. If
432 * it doesn't exist it is created and added to the prospective
435 f
= hashmap_get(m
->transaction_jobs
, name
);
437 for (j
= f
; j
; j
= j
->transaction_next
) {
438 assert(j
->name
== name
);
440 if (j
->type
== type
) {
447 if (name
->meta
.job
&& name
->meta
.job
->type
== type
)
449 else if (!(j
= job_new(m
, type
, name
)))
452 if ((r
= hashmap_replace(m
->transaction_jobs
, name
, j
)) < 0) {
457 j
->transaction_next
= f
;
460 f
->transaction_prev
= j
;
464 j
->matters_to_anchor
= false;
472 void manager_transaction_delete_job(Manager
*m
, Job
*j
) {
476 if (j
->transaction_prev
)
477 j
->transaction_prev
->transaction_next
= j
->transaction_next
;
478 else if (j
->transaction_next
)
479 hashmap_replace(m
->transaction_jobs
, j
->name
, j
->transaction_next
);
481 hashmap_remove_value(m
->transaction_jobs
, j
->name
, j
);
483 if (j
->transaction_next
)
484 j
->transaction_next
->transaction_prev
= j
->transaction_prev
;
486 j
->transaction_prev
= j
->transaction_next
= NULL
;
488 while (j
->subject_list
)
489 job_dependency_free(j
->subject_list
);
490 while (j
->object_list
)
491 job_dependency_free(j
->object_list
);
494 static int transaction_add_job_and_dependencies(Manager
*m
, JobType type
, Name
*name
, Job
*by
, bool matters
, bool force
, Job
**_ret
) {
502 assert(type
< _JOB_TYPE_MAX
);
505 /* First add the job. */
506 if (!(ret
= transaction_add_one_job(m
, type
, name
, &is_new
)))
509 /* Then, add a link to the job. */
510 if (!job_dependency_new(by
, ret
, matters
))
514 /* Finally, recursively add in all dependencies. */
515 if (type
== JOB_START
|| type
== JOB_RELOAD_OR_START
) {
516 SET_FOREACH(dep
, ret
->name
->meta
.dependencies
[NAME_REQUIRES
], state
)
517 if ((r
= transaction_add_job_and_dependencies(m
, JOB_START
, dep
, ret
, true, force
, NULL
)) < 0)
519 SET_FOREACH(dep
, ret
->name
->meta
.dependencies
[NAME_SOFT_REQUIRES
], state
)
520 if ((r
= transaction_add_job_and_dependencies(m
, JOB_START
, dep
, ret
, !force
, force
, NULL
)) < 0)
522 SET_FOREACH(dep
, ret
->name
->meta
.dependencies
[NAME_WANTS
], state
)
523 if ((r
= transaction_add_job_and_dependencies(m
, JOB_START
, dep
, ret
, false, force
, NULL
)) < 0)
525 SET_FOREACH(dep
, ret
->name
->meta
.dependencies
[NAME_REQUISITE
], state
)
526 if ((r
= transaction_add_job_and_dependencies(m
, JOB_VERIFY_STARTED
, dep
, ret
, true, force
, NULL
)) < 0)
528 SET_FOREACH(dep
, ret
->name
->meta
.dependencies
[NAME_SOFT_REQUISITE
], state
)
529 if ((r
= transaction_add_job_and_dependencies(m
, JOB_VERIFY_STARTED
, dep
, ret
, !force
, force
, NULL
)) < 0)
531 SET_FOREACH(dep
, ret
->name
->meta
.dependencies
[NAME_CONFLICTS
], state
)
532 if ((r
= transaction_add_job_and_dependencies(m
, JOB_STOP
, dep
, ret
, true, force
, NULL
)) < 0)
535 } else if (type
== JOB_STOP
|| type
== JOB_RESTART
|| type
== JOB_TRY_RESTART
) {
537 SET_FOREACH(dep
, ret
->name
->meta
.dependencies
[NAME_REQUIRED_BY
], state
)
538 if ((r
= transaction_add_job_and_dependencies(m
, type
, dep
, ret
, true, force
, NULL
)) < 0)
542 /* JOB_VERIFY_STARTED, JOB_RELOAD require no dependency handling */
551 int manager_add_job(Manager
*m
, JobType type
, Name
*name
, JobMode mode
, bool force
, Job
**_ret
) {
556 assert(type
< _JOB_TYPE_MAX
);
558 assert(mode
< _JOB_MODE_MAX
);
560 if ((r
= transaction_add_job_and_dependencies(m
, type
, name
, NULL
, true, force
, &ret
))) {
561 transaction_abort(m
);
565 if ((r
= transaction_activate(m
, mode
)) < 0)
574 Job
*manager_get_job(Manager
*m
, uint32_t id
) {
577 return hashmap_get(m
->jobs
, UINT32_TO_PTR(id
));
580 Name
*manager_get_name(Manager
*m
, const char *name
) {
584 return hashmap_get(m
->names
, name
);
587 static int verify_type(Name
*name
) {
593 /* Checks that all aliases of this name have the same and valid type */
595 SET_FOREACH(n
, name
->meta
.names
, state
) {
598 if ((t
= name_type_from_string(n
)) == _NAME_TYPE_INVALID
)
601 if (name
->meta
.type
== _NAME_TYPE_INVALID
) {
606 if (name
->meta
.type
!= t
)
610 if (name
->meta
.type
== _NAME_TYPE_INVALID
)
616 static int service_load_sysv(Service
*s
) {
619 /* Load service data from SysV init scripts, preferably with
625 static int name_load_fstab(Name
*n
) {
627 assert(n
->meta
.type
== NAME_MOUNT
|| n
->meta
.type
== NAME_AUTOMOUNT
);
629 /* Load mount data from /etc/fstab */
634 static int snapshot_load(Snapshot
*s
) {
637 /* Load snapshots from disk */
642 static int name_load_dropin(Name
*n
) {
645 /* Load dependencies from drop-in directories */
650 static int load(Name
*name
) {
655 if (name
->meta
.state
!= NAME_STUB
)
658 if ((r
= verify_type(name
)) < 0)
661 if (name
->meta
.type
== NAME_SERVICE
) {
663 /* Load a .service file */
664 if ((r
= name_load_fragment(name
)) == 0)
667 /* Load a classic init script */
669 if ((r
= service_load_sysv(SERVICE(name
))) == 0)
672 } else if (name
->meta
.type
== NAME_MOUNT
||
673 name
->meta
.type
== NAME_AUTOMOUNT
) {
675 if ((r
= name_load_fstab(name
)) == 0)
678 } else if (name
->meta
.type
== NAME_SNAPSHOT
) {
680 if ((r
= snapshot_load(SNAPSHOT(name
))) == 0)
684 if ((r
= name_load_fragment(name
)) == 0)
688 name
->meta
.state
= NAME_FAILED
;
692 if ((r
= name_load_dropin(name
)) < 0)
695 if ((r
= name_link_names(name
, true)) < 0)
698 name
->meta
.state
= NAME_LOADED
;
702 static int dispatch_load_queue(Manager
*m
) {
707 /* Make sure we are not run recursively */
708 if (m
->dispatching_load_queue
)
711 m
->dispatching_load_queue
= true;
713 /* Dispatches the load queue. Takes a name from the queue and
714 * tries to load its data until the queue is empty */
716 while ((meta
= m
->load_queue
)) {
718 LIST_REMOVE(Meta
, m
->load_queue
, meta
);
721 m
->dispatching_load_queue
= false;
726 int manager_load_name(Manager
*m
, const char *name
, Name
**_ret
) {
736 if (!name_is_valid(name
))
739 /* This will load the service information files, but not actually
740 * start any services or anything */
742 if ((ret
= manager_get_name(m
, name
)))
745 if ((t
= name_type_from_string(name
)) == _NAME_TYPE_INVALID
)
748 if (!(ret
= name_new(m
)))
753 if (!(n
= strdup(name
))) {
758 if (set_put(ret
->meta
.names
, n
) < 0) {
764 if ((r
= name_link(ret
)) < 0) {
769 /* At this point the new entry is created and linked. However,
770 * not loaded. Now load this entry and all its dependencies
773 dispatch_load_queue(m
);
781 void manager_dump_jobs(Manager
*s
, FILE *f
, const char *prefix
) {
788 HASHMAP_FOREACH(j
, s
->jobs
, state
)
789 job_dump(j
, f
, prefix
);
792 void manager_dump_names(Manager
*s
, FILE *f
, const char *prefix
) {
800 HASHMAP_FOREACH_KEY(n
, t
, s
->names
, state
)
802 name_dump(n
, f
, prefix
);