]>
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
->units
= 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 ((j
= hashmap_first(m
->transaction_jobs
)))
74 while ((u
= hashmap_first(m
->units
)))
77 hashmap_free(m
->units
);
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_unit(Manager
*m
, Unit
*u
) {
105 /* Deletes all jobs associated with a certain unit from the
108 while ((j
= hashmap_get(m
->transaction_jobs
, u
)))
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 units
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. */
140 l
= m
->transaction_anchor
;
142 LIST_FOREACH(subject
, l
, l
) {
144 /* This link does not matter */
148 /* This unit has already been marked */
149 if (l
->object
->generation
== generation
)
152 l
->object
->matters_to_anchor
= true;
153 l
->object
->generation
= generation
;
155 transaction_find_jobs_that_matter_to_anchor(m
, l
->object
, generation
);
159 static void transaction_merge_and_delete_job(Manager
*m
, Job
*j
, Job
*other
, JobType t
) {
160 JobDependency
*l
, *last
;
164 assert(j
->unit
== other
->unit
);
165 assert(!j
->installed
);
167 /* Merges 'other' into 'j' and then deletes j. */
170 j
->state
= JOB_WAITING
;
171 j
->forced
= j
->forced
|| other
->forced
;
173 j
->matters_to_anchor
= j
->matters_to_anchor
|| other
->matters_to_anchor
;
175 /* Patch us in as new owner of the JobDependency objects */
177 LIST_FOREACH(subject
, l
, other
->subject_list
) {
178 assert(l
->subject
== other
);
183 /* Merge both lists */
185 last
->subject_next
= j
->subject_list
;
187 j
->subject_list
->subject_prev
= last
;
188 j
->subject_list
= other
->subject_list
;
191 /* Patch us in as new owner of the JobDependency objects */
193 LIST_FOREACH(object
, l
, other
->object_list
) {
194 assert(l
->object
== other
);
199 /* Merge both lists */
201 last
->object_next
= j
->object_list
;
203 j
->object_list
->object_prev
= last
;
204 j
->object_list
= other
->object_list
;
207 /* Kill the other job */
208 other
->subject_list
= NULL
;
209 other
->object_list
= NULL
;
210 transaction_delete_job(m
, other
);
213 static int delete_one_unmergeable_job(Manager
*m
, Job
*j
) {
218 /* Tries to delete one item in the linked list
219 * j->transaction_next->transaction_next->... that conflicts
220 * whith another one, in an attempt to make an inconsistent
221 * transaction work. */
223 /* We rely here on the fact that if a merged with b does not
224 * merge with c, either a or b merge with c neither */
225 LIST_FOREACH(transaction
, j
, j
)
226 LIST_FOREACH(transaction
, k
, j
->transaction_next
) {
229 /* Is this one mergeable? Then skip it */
230 if (job_type_is_mergeable(j
->type
, k
->type
))
233 /* Ok, we found two that conflict, let's see if we can
234 * drop one of them */
235 if (!j
->matters_to_anchor
)
237 else if (!k
->matters_to_anchor
)
242 /* Ok, we can drop one, so let's do so. */
243 log_debug("Try to fix job merging by deleting job %s/%s", unit_id(d
->unit
), job_type_to_string(d
->type
));
244 transaction_delete_job(m
, d
);
251 static int transaction_merge_jobs(Manager
*m
) {
258 /* First step, check whether any of the jobs for one specific
259 * task conflict. If so, try to drop one of them. */
260 HASHMAP_FOREACH(j
, m
->transaction_jobs
, i
) {
265 LIST_FOREACH(transaction
, k
, j
->transaction_next
) {
266 if ((r
= job_type_merge(&t
, k
->type
)) >= 0)
269 /* OK, we could not merge all jobs for this
270 * action. Let's see if we can get rid of one
273 if ((r
= delete_one_unmergeable_job(m
, j
)) >= 0)
274 /* Ok, we managed to drop one, now
275 * let's ask our callers to call us
276 * again after garbage collecting */
279 /* We couldn't merge anything. Failure */
284 /* Second step, merge the jobs. */
285 HASHMAP_FOREACH(j
, m
->transaction_jobs
, i
) {
289 /* Merge all transactions */
290 LIST_FOREACH(transaction
, k
, j
->transaction_next
)
291 assert_se(job_type_merge(&t
, k
->type
) == 0);
293 /* If an active job is mergeable, merge it too */
294 if (j
->unit
->meta
.job
)
295 job_type_merge(&t
, j
->unit
->meta
.job
->type
); /* Might fail. Which is OK */
297 while ((k
= j
->transaction_next
)) {
299 transaction_merge_and_delete_job(m
, k
, j
, t
);
302 transaction_merge_and_delete_job(m
, j
, k
, t
);
305 assert(!j
->transaction_next
);
306 assert(!j
->transaction_prev
);
312 static bool unit_matters_to_anchor(Unit
*u
, Job
*j
) {
314 assert(!j
->transaction_prev
);
316 /* Checks whether at least one of the jobs for this unit
317 * matters to the anchor. */
319 LIST_FOREACH(transaction
, j
, j
)
320 if (j
->matters_to_anchor
)
326 static int transaction_verify_order_one(Manager
*m
, Job
*j
, Job
*from
, unsigned generation
) {
333 assert(!j
->transaction_prev
);
335 /* Does a recursive sweep through the ordering graph, looking
336 * for a cycle. If we find cycle we try to break it. */
338 /* Did we find a cycle? */
339 if (j
->marker
&& j
->generation
== generation
) {
342 /* So, we already have been here. We have a
343 * cycle. Let's try to break it. We go backwards in
344 * our path and try to find a suitable job to
345 * remove. We use the marker to find our way back,
346 * since smart how we are we stored our way back in
349 for (k
= from
; k
; k
= (k
->generation
== generation
? k
->marker
: NULL
)) {
352 !unit_matters_to_anchor(k
->unit
, k
)) {
353 /* Ok, we can drop this one, so let's
355 log_debug("Breaking order cycle by deleting job %s/%s", unit_id(k
->unit
), job_type_to_string(k
->type
));
356 transaction_delete_unit(m
, k
->unit
);
360 /* Check if this in fact was the beginning of
369 /* Make the marker point to where we come from, so that we can
370 * find our way backwards if we want to break a cycle */
372 j
->generation
= generation
;
374 /* We assume that the the dependencies are bidirectional, and
375 * hence can ignore UNIT_AFTER */
376 SET_FOREACH(u
, j
->unit
->meta
.dependencies
[UNIT_BEFORE
], i
) {
379 /* Is there a job for this unit? */
380 if (!(o
= hashmap_get(m
->transaction_jobs
, u
)))
382 /* Ok, there is no job for this in the
383 * transaction, but maybe there is already one
385 if (!(o
= u
->meta
.job
))
388 if ((r
= transaction_verify_order_one(m
, o
, j
, generation
)) < 0)
395 static int transaction_verify_order(Manager
*m
, unsigned *generation
) {
403 /* Check if the ordering graph is cyclic. If it is, try to fix
404 * that up by dropping one of the jobs. */
406 HASHMAP_FOREACH(j
, m
->transaction_jobs
, i
)
407 if ((r
= transaction_verify_order_one(m
, j
, NULL
, (*generation
)++)) < 0)
413 static void transaction_collect_garbage(Manager
*m
) {
418 /* Drop jobs that are not required by any other job */
426 HASHMAP_FOREACH(j
, m
->transaction_jobs
, i
) {
430 log_debug("Garbage collecting job %s/%s", unit_id(j
->unit
), job_type_to_string(j
->type
));
431 transaction_delete_job(m
, j
);
439 static int transaction_is_destructive(Manager
*m
, JobMode mode
) {
445 /* Checks whether applying this transaction means that
446 * existing jobs would be replaced */
448 HASHMAP_FOREACH(j
, m
->transaction_jobs
, i
) {
451 assert(!j
->transaction_prev
);
452 assert(!j
->transaction_next
);
454 if (j
->unit
->meta
.job
&&
455 j
->unit
->meta
.job
!= j
&&
456 !job_type_is_superset(j
->type
, j
->unit
->meta
.job
->type
))
463 static void transaction_minimize_impact(Manager
*m
) {
467 /* Drops all unnecessary jobs that reverse already active jobs
468 * or that stop a running service. */
476 HASHMAP_FOREACH(j
, m
->transaction_jobs
, i
) {
477 LIST_FOREACH(transaction
, j
, j
) {
479 /* If it matters, we shouldn't drop it */
480 if (j
->matters_to_anchor
)
483 /* Would this stop a running service?
484 * Would this change an existing job?
485 * If so, let's drop this entry */
486 if ((j
->type
!= JOB_STOP
|| UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(j
->unit
))) &&
487 (!j
->unit
->meta
.job
|| job_type_is_conflicting(j
->type
, j
->unit
->meta
.job
->state
)))
490 /* Ok, let's get rid of this */
491 log_debug("Deleting %s/%s to minimize impact", unit_id(j
->unit
), job_type_to_string(j
->type
));
492 transaction_delete_job(m
, j
);
504 static int transaction_apply(Manager
*m
, JobMode mode
) {
509 /* Moves the transaction jobs to the set of active jobs */
511 HASHMAP_FOREACH(j
, m
->transaction_jobs
, i
) {
513 assert(!j
->transaction_prev
);
514 assert(!j
->transaction_next
);
519 if ((r
= hashmap_put(m
->jobs
, UINT32_TO_PTR(j
->id
), j
)) < 0)
523 while ((j
= hashmap_steal_first(m
->transaction_jobs
))) {
527 if (j
->unit
->meta
.job
)
528 job_free(j
->unit
->meta
.job
);
530 j
->unit
->meta
.job
= j
;
533 /* We're fully installed. Now let's free data we don't
536 assert(!j
->transaction_next
);
537 assert(!j
->transaction_prev
);
539 while (j
->subject_list
)
540 job_dependency_free(j
->subject_list
);
541 while (j
->object_list
)
542 job_dependency_free(j
->object_list
);
545 m
->transaction_anchor
= NULL
;
551 HASHMAP_FOREACH(j
, m
->transaction_jobs
, i
) {
555 hashmap_remove(m
->jobs
, UINT32_TO_PTR(j
->id
));
561 static int transaction_activate(Manager
*m
, JobMode mode
) {
563 unsigned generation
= 1;
567 /* This applies the changes recorded in transaction_jobs to
568 * the actual list of jobs, if possible. */
570 /* First step: figure out which jobs matter */
571 transaction_find_jobs_that_matter_to_anchor(m
, NULL
, generation
++);
573 /* Second step: Try not to stop any running services if
574 * we don't have to. Don't try to reverse running
575 * jobs if we don't have to. */
576 transaction_minimize_impact(m
);
579 /* Third step: Let's remove unneeded jobs that might
581 transaction_collect_garbage(m
);
583 /* Fourth step: verify order makes sense and correct
584 * cycles if necessary and possible */
585 if ((r
= transaction_verify_order(m
, &generation
)) >= 0)
591 /* Let's see if the resulting transaction ordering
592 * graph is still cyclic... */
596 /* Fifth step: let's drop unmergeable entries if
597 * necessary and possible, merge entries we can
599 if ((r
= transaction_merge_jobs(m
)) >= 0)
605 /* Sixth step: an entry got dropped, let's garbage
606 * collect its dependencies. */
607 transaction_collect_garbage(m
);
609 /* Let's see if the resulting transaction still has
610 * unmergeable entries ... */
613 /* Seventh step: check whether we can actually apply this */
614 if (mode
== JOB_FAIL
)
615 if ((r
= transaction_is_destructive(m
, mode
)) < 0)
618 /* Eights step: apply changes */
619 if ((r
= transaction_apply(m
, mode
)) < 0)
622 assert(hashmap_isempty(m
->transaction_jobs
));
623 assert(!m
->transaction_anchor
);
628 transaction_abort(m
);
632 static Job
* transaction_add_one_job(Manager
*m
, JobType type
, Unit
*unit
, bool force
, bool *is_new
) {
639 /* Looks for an axisting prospective job and returns that. If
640 * it doesn't exist it is created and added to the prospective
643 f
= hashmap_get(m
->transaction_jobs
, unit
);
645 LIST_FOREACH(transaction
, j
, f
) {
646 assert(j
->unit
== unit
);
648 if (j
->type
== type
) {
655 if (unit
->meta
.job
&& unit
->meta
.job
->type
== type
)
657 else if (!(j
= job_new(m
, type
, unit
)))
662 j
->matters_to_anchor
= false;
665 LIST_PREPEND(Job
, transaction
, f
, j
);
667 if ((r
= hashmap_replace(m
->transaction_jobs
, unit
, f
)) < 0) {
678 void manager_transaction_unlink_job(Manager
*m
, Job
*j
) {
682 if (j
->transaction_prev
)
683 j
->transaction_prev
->transaction_next
= j
->transaction_next
;
684 else if (j
->transaction_next
)
685 hashmap_replace(m
->transaction_jobs
, j
->unit
, j
->transaction_next
);
687 hashmap_remove_value(m
->transaction_jobs
, j
->unit
, j
);
689 if (j
->transaction_next
)
690 j
->transaction_next
->transaction_prev
= j
->transaction_prev
;
692 j
->transaction_prev
= j
->transaction_next
= NULL
;
694 while (j
->subject_list
)
695 job_dependency_free(j
->subject_list
);
697 while (j
->object_list
) {
698 Job
*other
= j
->object_list
->matters
? j
->object_list
->subject
: NULL
;
700 job_dependency_free(j
->object_list
);
703 log_debug("Deleting job %s/%s as dependency of job %s/%s",
704 unit_id(other
->unit
), job_type_to_string(other
->type
),
705 unit_id(j
->unit
), job_type_to_string(j
->type
));
706 transaction_delete_job(m
, other
);
711 static int transaction_add_job_and_dependencies(Manager
*m
, JobType type
, Unit
*unit
, Job
*by
, bool matters
, bool force
, Job
**_ret
) {
719 assert(type
< _JOB_TYPE_MAX
);
722 if (unit
->meta
.load_state
!= UNIT_LOADED
)
725 if (!unit_job_is_applicable(unit
, type
))
728 /* First add the job. */
729 if (!(ret
= transaction_add_one_job(m
, type
, unit
, force
, &is_new
)))
732 /* Then, add a link to the job. */
733 if (!job_dependency_new(by
, ret
, matters
))
737 /* Finally, recursively add in all dependencies. */
738 if (type
== JOB_START
|| type
== JOB_RELOAD_OR_START
) {
739 SET_FOREACH(dep
, ret
->unit
->meta
.dependencies
[UNIT_REQUIRES
], i
)
740 if ((r
= transaction_add_job_and_dependencies(m
, JOB_START
, dep
, ret
, true, force
, NULL
)) < 0 && r
!= -EBADR
)
742 SET_FOREACH(dep
, ret
->unit
->meta
.dependencies
[UNIT_SOFT_REQUIRES
], i
)
743 if ((r
= transaction_add_job_and_dependencies(m
, JOB_START
, dep
, ret
, !force
, force
, NULL
)) < 0 && r
!= -EBADR
)
745 SET_FOREACH(dep
, ret
->unit
->meta
.dependencies
[UNIT_WANTS
], i
)
746 if ((r
= transaction_add_job_and_dependencies(m
, JOB_START
, dep
, ret
, false, force
, NULL
)) < 0 && r
!= -EBADR
)
748 SET_FOREACH(dep
, ret
->unit
->meta
.dependencies
[UNIT_REQUISITE
], i
)
749 if ((r
= transaction_add_job_and_dependencies(m
, JOB_VERIFY_ACTIVE
, dep
, ret
, true, force
, NULL
)) < 0 && r
!= -EBADR
)
751 SET_FOREACH(dep
, ret
->unit
->meta
.dependencies
[UNIT_SOFT_REQUISITE
], i
)
752 if ((r
= transaction_add_job_and_dependencies(m
, JOB_VERIFY_ACTIVE
, dep
, ret
, !force
, force
, NULL
)) < 0 && r
!= -EBADR
)
754 SET_FOREACH(dep
, ret
->unit
->meta
.dependencies
[UNIT_CONFLICTS
], i
)
755 if ((r
= transaction_add_job_and_dependencies(m
, JOB_STOP
, dep
, ret
, true, force
, NULL
)) < 0 && r
!= -EBADR
)
758 } else if (type
== JOB_STOP
|| type
== JOB_RESTART
|| type
== JOB_TRY_RESTART
) {
760 SET_FOREACH(dep
, ret
->unit
->meta
.dependencies
[UNIT_REQUIRED_BY
], i
)
761 if ((r
= transaction_add_job_and_dependencies(m
, type
, dep
, ret
, true, force
, NULL
)) < 0 && r
!= -EBADR
)
765 /* JOB_VERIFY_STARTED, JOB_RELOAD require no dependency handling */
774 int manager_add_job(Manager
*m
, JobType type
, Unit
*unit
, JobMode mode
, bool force
, Job
**_ret
) {
779 assert(type
< _JOB_TYPE_MAX
);
781 assert(mode
< _JOB_MODE_MAX
);
783 if ((r
= transaction_add_job_and_dependencies(m
, type
, unit
, NULL
, true, force
, &ret
))) {
784 transaction_abort(m
);
788 if ((r
= transaction_activate(m
, mode
)) < 0)
797 Job
*manager_get_job(Manager
*m
, uint32_t id
) {
800 return hashmap_get(m
->jobs
, UINT32_TO_PTR(id
));
803 Unit
*manager_get_unit(Manager
*m
, const char *name
) {
807 return hashmap_get(m
->units
, name
);
810 static void dispatch_load_queue(Manager
*m
) {
815 /* Make sure we are not run recursively */
816 if (m
->dispatching_load_queue
)
819 m
->dispatching_load_queue
= true;
821 /* Dispatches the load queue. Takes a unit from the queue and
822 * tries to load its data until the queue is empty */
824 while ((meta
= m
->load_queue
)) {
825 assert(meta
->in_load_queue
);
827 unit_load(UNIT(meta
));
830 m
->dispatching_load_queue
= false;
833 int manager_load_unit(Manager
*m
, const char *name
, Unit
**_ret
) {
841 /* This will load the service information files, but not actually
842 * start any services or anything */
844 if ((ret
= manager_get_unit(m
, name
))) {
849 if (!(ret
= unit_new(m
)))
852 if ((r
= unit_add_name(ret
, name
)) < 0) {
857 unit_add_to_load_queue(ret
);
858 dispatch_load_queue(m
);
864 void manager_dump_jobs(Manager
*s
, FILE *f
, const char *prefix
) {
871 HASHMAP_FOREACH(j
, s
->jobs
, i
)
872 job_dump(j
, f
, prefix
);
875 void manager_dump_units(Manager
*s
, FILE *f
, const char *prefix
) {
883 HASHMAP_FOREACH_KEY(u
, t
, s
->units
, i
)
885 unit_dump(u
, f
, prefix
);
888 void manager_clear_jobs(Manager
*m
) {
893 transaction_abort(m
);
895 while ((j
= hashmap_first(m
->jobs
)))
899 void manager_dispatch_run_queue(Manager
*m
) {
902 if (m
->dispatching_run_queue
)
905 m
->dispatching_run_queue
= true;
907 while ((j
= m
->run_queue
)) {
908 assert(j
->installed
);
909 assert(j
->in_run_queue
);
911 job_run_and_invalidate(j
);
914 m
->dispatching_run_queue
= false;
917 static int manager_dispatch_sigchld(Manager
*m
) {
925 if (waitid(P_ALL
, 0, &si
, WNOHANG
) < 0)
931 if (si
.si_code
!= CLD_EXITED
&& si
.si_code
!= CLD_KILLED
&& si
.si_code
!= CLD_DUMPED
)
934 if (!(u
= hashmap_remove(m
->watch_pids
, UINT32_TO_PTR(si
.si_pid
))))
937 UNIT_VTABLE(u
)->sigchld_event(u
, si
.si_pid
, si
.si_code
, si
.si_status
);
943 static int manager_process_signal_fd(Manager
*m
) {
945 struct signalfd_siginfo sfsi
;
946 bool sigchld
= false;
951 if ((n
= read(m
->signal_fd
, &sfsi
, sizeof(sfsi
))) != sizeof(sfsi
)) {
962 if (sfsi
.ssi_signo
== SIGCHLD
)
967 return manager_dispatch_sigchld(m
);
972 static int process_event(Manager
*m
, struct epoll_event
*ev
) {
978 switch (ev
->data
.u32
) {
981 assert(ev
->data
.fd
== m
->signal_fd
);
983 /* An incoming signal? */
984 if (ev
->events
!= POLLIN
)
987 if ((r
= manager_process_signal_fd(m
)) < 0)
995 /* Some fd event, to be dispatched to the units */
996 assert_se(u
= ev
->data
.ptr
);
997 UNIT_VTABLE(u
)->fd_event(u
, ev
->data
.fd
, ev
->events
);
1001 case MANAGER_TIMER
: {
1006 /* Some timer event, to be dispatched to the units */
1007 if ((k
= read(ev
->data
.fd
, &v
, sizeof(v
))) != sizeof(v
)) {
1009 if (k
< 0 && (errno
== EINTR
|| errno
== EAGAIN
))
1012 return k
< 0 ? -errno
: -EIO
;
1015 assert_se(u
= ev
->data
.ptr
);
1016 UNIT_VTABLE(u
)->timer_event(u
, ev
->data
.fd
, v
);
1021 assert_not_reached("Unknown epoll event type.");
1027 int manager_loop(Manager
*m
) {
1033 struct epoll_event events
[32];
1036 manager_dispatch_run_queue(m
);
1038 if ((n
= epoll_wait(m
->epoll_fd
, events
, ELEMENTSOF(events
), -1)) < 0) {
1040 if (errno
== -EINTR
)
1046 for (i
= 0; i
< n
; i
++)
1047 if ((r
= process_event(m
, events
+ i
)) < 0)