]>
git.ipfire.org Git - people/ms/systemd.git/blob - manager.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
8 #include <sys/signalfd.h>
19 Manager
* manager_new(void) {
22 struct epoll_event ev
;
24 if (!(m
= new0(Manager
, 1)))
27 m
->signal_fd
= m
->epoll_fd
= -1;
29 if (!(m
->names
= hashmap_new(string_hash_func
, string_compare_func
)))
32 if (!(m
->jobs
= hashmap_new(trivial_hash_func
, trivial_compare_func
)))
35 if (!(m
->transaction_jobs
= hashmap_new(trivial_hash_func
, trivial_compare_func
)))
38 if (!(m
->watch_pids
= hashmap_new(trivial_hash_func
, trivial_compare_func
)))
41 if ((m
->epoll_fd
= epoll_create1(EPOLL_CLOEXEC
)) < 0)
44 assert_se(sigemptyset(&mask
) == 0);
45 assert_se(sigaddset(&mask
, SIGCHLD
) == 0);
46 assert_se(sigprocmask(SIG_SETMASK
, &mask
, NULL
) == 0);
48 if ((m
->signal_fd
= signalfd(-1, &mask
, SFD_NONBLOCK
|SFD_CLOEXEC
)) < 0)
53 ev
.data
.fd
= m
->signal_fd
;
55 if (epoll_ctl(m
->epoll_fd
, EPOLL_CTL_ADD
, m
->signal_fd
, &ev
) < 0)
65 void manager_free(Manager
*m
) {
71 while ((n
= hashmap_first(m
->names
)))
74 while ((j
= hashmap_steal_first(m
->transaction_jobs
)))
77 hashmap_free(m
->names
);
78 hashmap_free(m
->jobs
);
79 hashmap_free(m
->transaction_jobs
);
80 hashmap_free(m
->watch_pids
);
83 close_nointr(m
->epoll_fd
);
84 if (m
->signal_fd
>= 0)
85 close_nointr(m
->signal_fd
);
90 static void transaction_delete_job(Manager
*m
, Job
*j
) {
94 /* Deletes one job from the transaction */
96 manager_transaction_unlink_job(m
, j
);
102 static void transaction_delete_name(Manager
*m
, Name
*n
) {
105 /* Deletes all jobs associated with a certain name from the
108 while ((j
= hashmap_get(m
->transaction_jobs
, n
)))
109 transaction_delete_job(m
, j
);
112 static void transaction_abort(Manager
*m
) {
117 while ((j
= hashmap_first(m
->transaction_jobs
)))
119 transaction_delete_job(m
, j
);
123 assert(hashmap_isempty(m
->transaction_jobs
));
124 assert(!m
->transaction_anchor
);
127 static void transaction_find_jobs_that_matter_to_anchor(Manager
*m
, Job
*j
, unsigned generation
) {
132 /* A recursive sweep through the graph that marks all names
133 * that matter to the anchor job, i.e. are directly or
134 * indirectly a dependency of the anchor job via paths that
135 * are fully marked as mattering. */
137 for (l
= j
? j
->subject_list
: m
->transaction_anchor
; l
; l
= l
->subject_next
) {
139 /* This link does not matter */
143 /* This name has already been marked */
144 if (l
->object
->generation
== generation
)
147 l
->object
->matters_to_anchor
= true;
148 l
->object
->generation
= generation
;
150 transaction_find_jobs_that_matter_to_anchor(m
, l
->object
, generation
);
154 static void transaction_merge_and_delete_job(Manager
*m
, Job
*j
, Job
*other
, JobType t
) {
155 JobDependency
*l
, *last
;
159 assert(j
->name
== other
->name
);
162 /* Merges 'other' into 'j' and then deletes j. */
165 j
->state
= JOB_WAITING
;
166 j
->forced
= j
->forced
|| other
->forced
;
168 j
->matters_to_anchor
= j
->matters_to_anchor
|| other
->matters_to_anchor
;
170 /* Patch us in as new owner of the JobDependency objects */
172 for (l
= other
->subject_list
; l
; l
= l
->subject_next
) {
173 assert(l
->subject
== other
);
178 /* Merge both lists */
180 last
->subject_next
= j
->subject_list
;
182 j
->subject_list
->subject_prev
= last
;
183 j
->subject_list
= other
->subject_list
;
186 /* Patch us in as new owner of the JobDependency objects */
188 for (l
= other
->object_list
; l
; l
= l
->object_next
) {
189 assert(l
->object
== other
);
194 /* Merge both lists */
196 last
->object_next
= j
->object_list
;
198 j
->object_list
->object_prev
= last
;
199 j
->object_list
= other
->object_list
;
202 /* Kill the other job */
203 other
->subject_list
= NULL
;
204 other
->object_list
= NULL
;
205 transaction_delete_job(m
, other
);
208 static int delete_one_unmergeable_job(Manager
*m
, Job
*j
) {
213 /* Tries to delete one item in the linked list
214 * j->transaction_next->transaction_next->... that conflicts
215 * whith another one, in an attempt to make an inconsistent
216 * transaction work. */
218 /* We rely here on the fact that if a merged with b does not
219 * merge with c, either a or b merge with c neither */
220 for (; j
; j
= j
->transaction_next
)
221 for (k
= j
->transaction_next
; k
; k
= k
->transaction_next
) {
224 /* Is this one mergeable? Then skip it */
225 if (job_type_is_mergeable(j
->type
, k
->type
))
228 /* Ok, we found two that conflict, let's see if we can
229 * drop one of them */
230 if (!j
->matters_to_anchor
)
232 else if (!k
->matters_to_anchor
)
237 /* Ok, we can drop one, so let's do so. */
238 log_debug("Try to fix job merging by deleting job %s/%s", name_id(d
->name
), job_type_to_string(d
->type
));
239 transaction_delete_job(m
, d
);
246 static int transaction_merge_jobs(Manager
*m
) {
253 /* First step, check whether any of the jobs for one specific
254 * task conflict. If so, try to drop one of them. */
255 HASHMAP_FOREACH(j
, m
->transaction_jobs
, state
) {
260 for (k
= j
->transaction_next
; k
; k
= k
->transaction_next
) {
261 if ((r
= job_type_merge(&t
, k
->type
)) >= 0)
264 /* OK, we could not merge all jobs for this
265 * action. Let's see if we can get rid of one
268 if ((r
= delete_one_unmergeable_job(m
, j
)) >= 0)
269 /* Ok, we managed to drop one, now
270 * let's ask our callers to call us
271 * again after garbage collecting */
274 /* We couldn't merge anything. Failure */
279 /* Second step, merge the jobs. */
280 HASHMAP_FOREACH(j
, m
->transaction_jobs
, state
) {
284 /* Merge all transactions */
285 for (k
= j
->transaction_next
; k
; k
= k
->transaction_next
)
286 assert_se(job_type_merge(&t
, k
->type
) == 0);
288 /* If an active job is mergeable, merge it too */
289 if (j
->name
->meta
.job
)
290 job_type_merge(&t
, j
->name
->meta
.job
->type
); /* Might fail. Which is OK */
292 while ((k
= j
->transaction_next
)) {
294 transaction_merge_and_delete_job(m
, k
, j
, t
);
297 transaction_merge_and_delete_job(m
, j
, k
, t
);
300 assert(!j
->transaction_next
);
301 assert(!j
->transaction_prev
);
307 static bool name_matters_to_anchor(Name
*n
, Job
*j
) {
309 assert(!j
->transaction_prev
);
311 /* Checks whether at least one of the jobs for this name
312 * matters to the anchor. */
314 for (; j
; j
= j
->transaction_next
)
315 if (j
->matters_to_anchor
)
321 static int transaction_verify_order_one(Manager
*m
, Job
*j
, Job
*from
, unsigned generation
) {
328 assert(!j
->transaction_prev
);
330 /* Does a recursive sweep through the ordering graph, looking
331 * for a cycle. If we find cycle we try to break it. */
333 /* Did we find a cycle? */
334 if (j
->marker
&& j
->generation
== generation
) {
337 /* So, we already have been here. We have a
338 * cycle. Let's try to break it. We go backwards in
339 * our path and try to find a suitable job to
340 * remove. We use the marker to find our way back,
341 * since smart how we are we stored our way back in
344 for (k
= from
; k
; k
= (k
->generation
== generation
? k
->marker
: NULL
)) {
347 !name_matters_to_anchor(k
->name
, k
)) {
348 /* Ok, we can drop this one, so let's
350 log_debug("Breaking order cycle by deleting job %s/%s", name_id(k
->name
), job_type_to_string(k
->type
));
351 transaction_delete_name(m
, k
->name
);
355 /* Check if this in fact was the beginning of
364 /* Make the marker point to where we come from, so that we can
365 * find our way backwards if we want to break a cycle */
367 j
->generation
= generation
;
369 /* We assume that the the dependencies are bidirectional, and
370 * hence can ignore NAME_AFTER */
371 SET_FOREACH(n
, j
->name
->meta
.dependencies
[NAME_BEFORE
], state
) {
374 /* Is there a job for this name? */
375 if (!(o
= hashmap_get(m
->transaction_jobs
, n
)))
377 /* Ok, there is no job for this in the
378 * transaction, but maybe there is already one
380 if (!(o
= n
->meta
.job
))
383 if ((r
= transaction_verify_order_one(m
, o
, j
, generation
)) < 0)
390 static int transaction_verify_order(Manager
*m
, unsigned *generation
) {
398 /* Check if the ordering graph is cyclic. If it is, try to fix
399 * that up by dropping one of the jobs. */
401 HASHMAP_FOREACH(j
, m
->transaction_jobs
, state
)
402 if ((r
= transaction_verify_order_one(m
, j
, NULL
, (*generation
)++)) < 0)
408 static void transaction_collect_garbage(Manager
*m
) {
413 /* Drop jobs that are not required by any other job */
421 HASHMAP_FOREACH(j
, m
->transaction_jobs
, state
) {
425 log_debug("Garbage collecting job %s/%s", name_id(j
->name
), job_type_to_string(j
->type
));
426 transaction_delete_job(m
, j
);
434 static int transaction_is_destructive(Manager
*m
, JobMode mode
) {
440 /* Checks whether applying this transaction means that
441 * existing jobs would be replaced */
443 HASHMAP_FOREACH(j
, m
->transaction_jobs
, state
) {
446 assert(!j
->transaction_prev
);
447 assert(!j
->transaction_next
);
449 if (j
->name
->meta
.job
&&
450 j
->name
->meta
.job
!= j
&&
451 !job_type_is_superset(j
->type
, j
->name
->meta
.job
->type
))
458 static void transaction_minimize_impact(Manager
*m
) {
462 /* Drops all unnecessary jobs that reverse already active jobs
463 * or that stop a running service. */
471 HASHMAP_FOREACH(j
, m
->transaction_jobs
, state
) {
472 for (; j
; j
= j
->transaction_next
) {
474 /* If it matters, we shouldn't drop it */
475 if (j
->matters_to_anchor
)
478 /* Would this stop a running service?
479 * Would this change an existing job?
480 * If so, let's drop this entry */
481 if ((j
->type
!= JOB_STOP
|| NAME_IS_INACTIVE_OR_DEACTIVATING(name_active_state(j
->name
))) &&
482 (!j
->name
->meta
.job
|| job_type_is_conflicting(j
->type
, j
->name
->meta
.job
->state
)))
485 /* Ok, let's get rid of this */
486 log_debug("Deleting %s/%s to minimize impact", name_id(j
->name
), job_type_to_string(j
->type
));
487 transaction_delete_job(m
, j
);
499 static int transaction_apply(Manager
*m
, JobMode mode
) {
504 /* Moves the transaction jobs to the set of active jobs */
506 HASHMAP_FOREACH(j
, m
->transaction_jobs
, state
) {
508 assert(!j
->transaction_prev
);
509 assert(!j
->transaction_next
);
514 if ((r
= hashmap_put(m
->jobs
, UINT32_TO_PTR(j
->id
), j
)) < 0)
518 while ((j
= hashmap_steal_first(m
->transaction_jobs
))) {
522 if (j
->name
->meta
.job
)
523 job_free(j
->name
->meta
.job
);
525 j
->name
->meta
.job
= j
;
528 /* We're fully installed. Now let's free data we don't
531 assert(!j
->transaction_next
);
532 assert(!j
->transaction_prev
);
534 while (j
->subject_list
)
535 job_dependency_free(j
->subject_list
);
536 while (j
->object_list
)
537 job_dependency_free(j
->object_list
);
540 m
->transaction_anchor
= NULL
;
546 HASHMAP_FOREACH(j
, m
->transaction_jobs
, state
) {
550 hashmap_remove(m
->jobs
, UINT32_TO_PTR(j
->id
));
556 static int transaction_activate(Manager
*m
, JobMode mode
) {
558 unsigned generation
= 1;
562 /* This applies the changes recorded in transaction_jobs to
563 * the actual list of jobs, if possible. */
565 /* First step: figure out which jobs matter */
566 transaction_find_jobs_that_matter_to_anchor(m
, NULL
, generation
++);
568 /* Second step: Try not to stop any running services if
569 * we don't have to. Don't try to reverse running
570 * jobs if we don't have to. */
571 transaction_minimize_impact(m
);
574 /* Third step: Let's remove unneeded jobs that might
576 transaction_collect_garbage(m
);
578 /* Fourth step: verify order makes sense and correct
579 * cycles if necessary and possible */
580 if ((r
= transaction_verify_order(m
, &generation
)) >= 0)
586 /* Let's see if the resulting transaction ordering
587 * graph is still cyclic... */
591 /* Fifth step: let's drop unmergeable entries if
592 * necessary and possible, merge entries we can
594 if ((r
= transaction_merge_jobs(m
)) >= 0)
600 /* Sixth step: an entry got dropped, let's garbage
601 * collect its dependencies. */
602 transaction_collect_garbage(m
);
604 /* Let's see if the resulting transaction still has
605 * unmergeable entries ... */
608 /* Seventh step: check whether we can actually apply this */
609 if (mode
== JOB_FAIL
)
610 if ((r
= transaction_is_destructive(m
, mode
)) < 0)
613 /* Eights step: apply changes */
614 if ((r
= transaction_apply(m
, mode
)) < 0)
617 assert(hashmap_isempty(m
->transaction_jobs
));
618 assert(!m
->transaction_anchor
);
623 transaction_abort(m
);
627 static Job
* transaction_add_one_job(Manager
*m
, JobType type
, Name
*name
, bool force
, bool *is_new
) {
634 /* Looks for an axisting prospective job and returns that. If
635 * it doesn't exist it is created and added to the prospective
638 f
= hashmap_get(m
->transaction_jobs
, name
);
640 for (j
= f
; j
; j
= j
->transaction_next
) {
641 assert(j
->name
== name
);
643 if (j
->type
== type
) {
650 if (name
->meta
.job
&& name
->meta
.job
->type
== type
)
652 else if (!(j
= job_new(m
, type
, name
)))
655 if ((r
= hashmap_replace(m
->transaction_jobs
, name
, j
)) < 0) {
660 j
->transaction_next
= f
;
663 f
->transaction_prev
= j
;
667 j
->matters_to_anchor
= false;
676 void manager_transaction_unlink_job(Manager
*m
, Job
*j
) {
680 if (j
->transaction_prev
)
681 j
->transaction_prev
->transaction_next
= j
->transaction_next
;
682 else if (j
->transaction_next
)
683 hashmap_replace(m
->transaction_jobs
, j
->name
, j
->transaction_next
);
685 hashmap_remove_value(m
->transaction_jobs
, j
->name
, j
);
687 if (j
->transaction_next
)
688 j
->transaction_next
->transaction_prev
= j
->transaction_prev
;
690 j
->transaction_prev
= j
->transaction_next
= NULL
;
692 while (j
->subject_list
)
693 job_dependency_free(j
->subject_list
);
695 while (j
->object_list
) {
696 Job
*other
= j
->object_list
->matters
? j
->object_list
->subject
: NULL
;
698 job_dependency_free(j
->object_list
);
701 log_debug("Deleting job %s/%s as dependency of job %s/%s",
702 name_id(other
->name
), job_type_to_string(other
->type
),
703 name_id(j
->name
), job_type_to_string(j
->type
));
704 transaction_delete_job(m
, other
);
709 static int transaction_add_job_and_dependencies(Manager
*m
, JobType type
, Name
*name
, Job
*by
, bool matters
, bool force
, Job
**_ret
) {
717 assert(type
< _JOB_TYPE_MAX
);
720 if (name
->meta
.load_state
!= NAME_LOADED
)
723 if (!job_type_is_applicable(type
, name
->meta
.type
))
726 /* First add the job. */
727 if (!(ret
= transaction_add_one_job(m
, type
, name
, force
, &is_new
)))
730 /* Then, add a link to the job. */
731 if (!job_dependency_new(by
, ret
, matters
))
735 /* Finally, recursively add in all dependencies. */
736 if (type
== JOB_START
|| type
== JOB_RELOAD_OR_START
) {
737 SET_FOREACH(dep
, ret
->name
->meta
.dependencies
[NAME_REQUIRES
], state
)
738 if ((r
= transaction_add_job_and_dependencies(m
, JOB_START
, dep
, ret
, true, force
, NULL
)) < 0 && r
!= -EBADR
)
740 SET_FOREACH(dep
, ret
->name
->meta
.dependencies
[NAME_SOFT_REQUIRES
], state
)
741 if ((r
= transaction_add_job_and_dependencies(m
, JOB_START
, dep
, ret
, !force
, force
, NULL
)) < 0 && r
!= -EBADR
)
743 SET_FOREACH(dep
, ret
->name
->meta
.dependencies
[NAME_WANTS
], state
)
744 if ((r
= transaction_add_job_and_dependencies(m
, JOB_START
, dep
, ret
, false, force
, NULL
)) < 0 && r
!= -EBADR
)
746 SET_FOREACH(dep
, ret
->name
->meta
.dependencies
[NAME_REQUISITE
], state
)
747 if ((r
= transaction_add_job_and_dependencies(m
, JOB_VERIFY_ACTIVE
, dep
, ret
, true, force
, NULL
)) < 0 && r
!= -EBADR
)
749 SET_FOREACH(dep
, ret
->name
->meta
.dependencies
[NAME_SOFT_REQUISITE
], state
)
750 if ((r
= transaction_add_job_and_dependencies(m
, JOB_VERIFY_ACTIVE
, dep
, ret
, !force
, force
, NULL
)) < 0 && r
!= -EBADR
)
752 SET_FOREACH(dep
, ret
->name
->meta
.dependencies
[NAME_CONFLICTS
], state
)
753 if ((r
= transaction_add_job_and_dependencies(m
, JOB_STOP
, dep
, ret
, true, force
, NULL
)) < 0 && r
!= -EBADR
)
756 } else if (type
== JOB_STOP
|| type
== JOB_RESTART
|| type
== JOB_TRY_RESTART
) {
758 SET_FOREACH(dep
, ret
->name
->meta
.dependencies
[NAME_REQUIRED_BY
], state
)
759 if ((r
= transaction_add_job_and_dependencies(m
, type
, dep
, ret
, true, force
, NULL
)) < 0 && r
!= -EBADR
)
763 /* JOB_VERIFY_STARTED, JOB_RELOAD require no dependency handling */
772 int manager_add_job(Manager
*m
, JobType type
, Name
*name
, JobMode mode
, bool force
, Job
**_ret
) {
777 assert(type
< _JOB_TYPE_MAX
);
779 assert(mode
< _JOB_MODE_MAX
);
781 if ((r
= transaction_add_job_and_dependencies(m
, type
, name
, NULL
, true, force
, &ret
))) {
782 transaction_abort(m
);
786 if ((r
= transaction_activate(m
, mode
)) < 0)
795 Job
*manager_get_job(Manager
*m
, uint32_t id
) {
798 return hashmap_get(m
->jobs
, UINT32_TO_PTR(id
));
801 Name
*manager_get_name(Manager
*m
, const char *name
) {
805 return hashmap_get(m
->names
, name
);
808 static int dispatch_load_queue(Manager
*m
) {
813 /* Make sure we are not run recursively */
814 if (m
->dispatching_load_queue
)
817 m
->dispatching_load_queue
= true;
819 /* Dispatches the load queue. Takes a name from the queue and
820 * tries to load its data until the queue is empty */
822 while ((meta
= m
->load_queue
)) {
823 name_load(NAME(meta
));
824 LIST_REMOVE(Meta
, m
->load_queue
, meta
);
827 m
->dispatching_load_queue
= false;
832 int manager_load_name(Manager
*m
, const char *name
, Name
**_ret
) {
842 if (!name_is_valid(name
))
845 /* This will load the service information files, but not actually
846 * start any services or anything */
848 if ((ret
= manager_get_name(m
, name
)))
851 if ((t
= name_type_from_string(name
)) == _NAME_TYPE_INVALID
)
854 if (!(ret
= name_new(m
)))
859 if (!(n
= strdup(name
))) {
864 if ((r
= set_put(ret
->meta
.names
, n
)) < 0) {
870 if ((r
= name_link(ret
)) < 0) {
875 /* At this point the new entry is created and linked. However,
876 * not loaded. Now load this entry and all its dependencies
879 dispatch_load_queue(m
);
887 void manager_dump_jobs(Manager
*s
, FILE *f
, const char *prefix
) {
894 HASHMAP_FOREACH(j
, s
->jobs
, state
)
895 job_dump(j
, f
, prefix
);
898 void manager_dump_names(Manager
*s
, FILE *f
, const char *prefix
) {
906 HASHMAP_FOREACH_KEY(n
, t
, s
->names
, state
)
908 name_dump(n
, f
, prefix
);
911 void manager_clear_jobs(Manager
*m
) {
916 transaction_abort(m
);
918 while ((j
= hashmap_first(m
->jobs
)))
922 void manager_run_jobs(Manager
*m
) {
927 HASHMAP_FOREACH(j
, m
->jobs
, state
) {
928 r
= job_run_and_invalidate(j
);
930 /* FIXME... the list of jobs might have changed */
934 int manager_dispatch_sigchld(Manager
*m
) {
942 if (waitid(P_ALL
, 0, &si
, WNOHANG
) < 0)
948 if (!(n
= hashmap_remove(m
->watch_pids
, UINT32_TO_PTR(si
.si_pid
))))
951 NAME_VTABLE(n
)->sigchld_event(n
, si
.si_pid
, si
.si_code
, si
.si_status
);
957 int manager_process_signal_fd(Manager
*m
) {
959 struct signalfd_siginfo sfsi
;
960 bool sigchld
= false;
965 if ((n
= read(m
->signal_fd
, &sfsi
, sizeof(sfsi
))) != sizeof(sfsi
)) {
976 if (sfsi
.ssi_signo
== SIGCHLD
)
981 manager_dispatch_sigchld(m
);
986 int manager_loop(Manager
*m
) {
988 struct epoll_event events
[32];
995 if ((n
= epoll_wait(m
->epoll_fd
, events
, ELEMENTSOF(events
), -1)) < 0) {
1003 for (i
= 0; i
< n
; i
++) {
1005 if (events
[i
].data
.fd
== m
->signal_fd
) {
1007 /* An incoming signal? */
1008 if (events
[i
].events
!= POLLIN
)
1011 if ((r
= manager_process_signal_fd(m
)) < 0)
1016 /* Some other fd event, to be dispatched to the names */
1017 assert_se(n
= events
[i
].data
.ptr
);
1018 NAME_VTABLE(n
)->fd_event(n
, events
[i
].data
.fd
, events
[i
].events
);