]>
git.ipfire.org Git - people/ms/systemd.git/blob - manager.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
8 #include <sys/signalfd.h>
19 #include "ratelimit.h"
21 static int manager_setup_signals(Manager
*m
) {
23 struct epoll_event ev
;
27 assert_se(reset_all_signal_handlers() == 0);
29 assert_se(sigemptyset(&mask
) == 0);
30 assert_se(sigaddset(&mask
, SIGCHLD
) == 0);
31 assert_se(sigaddset(&mask
, SIGINT
) == 0); /* Kernel sends us this on control-alt-del */
32 assert_se(sigaddset(&mask
, SIGWINCH
) == 0); /* Kernel sends us this on kbrequest (alt-arrowup) */
33 assert_se(sigaddset(&mask
, SIGTERM
) == 0);
34 assert_se(sigaddset(&mask
, SIGHUP
) == 0);
35 assert_se(sigaddset(&mask
, SIGUSR1
) == 0);
36 assert_se(sigaddset(&mask
, SIGUSR2
) == 0);
37 assert_se(sigaddset(&mask
, SIGPIPE
) == 0);
38 assert_se(sigprocmask(SIG_SETMASK
, &mask
, NULL
) == 0);
40 m
->signal_watch
.type
= WATCH_SIGNAL
;
41 if ((m
->signal_watch
.fd
= signalfd(-1, &mask
, SFD_NONBLOCK
|SFD_CLOEXEC
)) < 0)
46 ev
.data
.ptr
= &m
->signal_watch
;
48 if (epoll_ctl(m
->epoll_fd
, EPOLL_CTL_ADD
, m
->signal_watch
.fd
, &ev
) < 0)
54 Manager
* manager_new(void) {
57 if (!(m
= new0(Manager
, 1)))
60 m
->signal_watch
.fd
= m
->mount_watch
.fd
= m
->udev_watch
.fd
= m
->epoll_fd
= -1;
61 m
->current_job_id
= 1; /* start as id #1, so that we can leave #0 around as "null-like" value */
63 if (!(m
->units
= hashmap_new(string_hash_func
, string_compare_func
)))
66 if (!(m
->jobs
= hashmap_new(trivial_hash_func
, trivial_compare_func
)))
69 if (!(m
->transaction_jobs
= hashmap_new(trivial_hash_func
, trivial_compare_func
)))
72 if (!(m
->watch_pids
= hashmap_new(trivial_hash_func
, trivial_compare_func
)))
75 if ((m
->epoll_fd
= epoll_create1(EPOLL_CLOEXEC
)) < 0)
78 if (manager_setup_signals(m
) < 0)
81 /* FIXME: this should be called only when the D-Bus bus daemon is running */
92 void manager_free(Manager
*m
) {
99 while ((j
= hashmap_first(m
->transaction_jobs
)))
102 while ((u
= hashmap_first(m
->units
)))
105 for (c
= 0; c
< _UNIT_TYPE_MAX
; c
++)
106 if (unit_vtable
[c
]->shutdown
)
107 unit_vtable
[c
]->shutdown(m
);
111 hashmap_free(m
->units
);
112 hashmap_free(m
->jobs
);
113 hashmap_free(m
->transaction_jobs
);
114 hashmap_free(m
->watch_pids
);
116 if (m
->epoll_fd
>= 0)
117 close_nointr(m
->epoll_fd
);
118 if (m
->signal_watch
.fd
>= 0)
119 close_nointr(m
->signal_watch
.fd
);
124 int manager_coldplug(Manager
*m
) {
133 /* First, let's ask every type to load all units from
134 * disk/kernel that it might know */
135 for (c
= 0; c
< _UNIT_TYPE_MAX
; c
++)
136 if (unit_vtable
[c
]->enumerate
)
137 if ((r
= unit_vtable
[c
]->enumerate(m
)) < 0)
140 manager_dispatch_load_queue(m
);
142 /* Then, let's set up their initial state. */
143 HASHMAP_FOREACH_KEY(u
, k
, m
->units
, i
) {
149 if (UNIT_VTABLE(u
)->coldplug
)
150 if ((r
= UNIT_VTABLE(u
)->coldplug(u
)) < 0)
157 static void transaction_delete_job(Manager
*m
, Job
*j
) {
161 /* Deletes one job from the transaction */
163 manager_transaction_unlink_job(m
, j
);
169 static void transaction_delete_unit(Manager
*m
, Unit
*u
) {
172 /* Deletes all jobs associated with a certain unit from the
175 while ((j
= hashmap_get(m
->transaction_jobs
, u
)))
176 transaction_delete_job(m
, j
);
179 static void transaction_clean_dependencies(Manager
*m
) {
185 /* Drops all dependencies of all installed jobs */
187 HASHMAP_FOREACH(j
, m
->jobs
, i
) {
188 while (j
->subject_list
)
189 job_dependency_free(j
->subject_list
);
190 while (j
->object_list
)
191 job_dependency_free(j
->object_list
);
194 assert(!m
->transaction_anchor
);
197 static void transaction_abort(Manager
*m
) {
202 while ((j
= hashmap_first(m
->transaction_jobs
)))
204 transaction_delete_job(m
, j
);
208 assert(hashmap_isempty(m
->transaction_jobs
));
210 transaction_clean_dependencies(m
);
213 static void transaction_find_jobs_that_matter_to_anchor(Manager
*m
, Job
*j
, unsigned generation
) {
218 /* A recursive sweep through the graph that marks all units
219 * that matter to the anchor job, i.e. are directly or
220 * indirectly a dependency of the anchor job via paths that
221 * are fully marked as mattering. */
226 l
= m
->transaction_anchor
;
228 LIST_FOREACH(subject
, l
, l
) {
230 /* This link does not matter */
234 /* This unit has already been marked */
235 if (l
->object
->generation
== generation
)
238 l
->object
->matters_to_anchor
= true;
239 l
->object
->generation
= generation
;
241 transaction_find_jobs_that_matter_to_anchor(m
, l
->object
, generation
);
245 static void transaction_merge_and_delete_job(Manager
*m
, Job
*j
, Job
*other
, JobType t
) {
246 JobDependency
*l
, *last
;
250 assert(j
->unit
== other
->unit
);
251 assert(!j
->installed
);
253 /* Merges 'other' into 'j' and then deletes j. */
256 j
->state
= JOB_WAITING
;
257 j
->forced
= j
->forced
|| other
->forced
;
259 j
->matters_to_anchor
= j
->matters_to_anchor
|| other
->matters_to_anchor
;
261 /* Patch us in as new owner of the JobDependency objects */
263 LIST_FOREACH(subject
, l
, other
->subject_list
) {
264 assert(l
->subject
== other
);
269 /* Merge both lists */
271 last
->subject_next
= j
->subject_list
;
273 j
->subject_list
->subject_prev
= last
;
274 j
->subject_list
= other
->subject_list
;
277 /* Patch us in as new owner of the JobDependency objects */
279 LIST_FOREACH(object
, l
, other
->object_list
) {
280 assert(l
->object
== other
);
285 /* Merge both lists */
287 last
->object_next
= j
->object_list
;
289 j
->object_list
->object_prev
= last
;
290 j
->object_list
= other
->object_list
;
293 /* Kill the other job */
294 other
->subject_list
= NULL
;
295 other
->object_list
= NULL
;
296 transaction_delete_job(m
, other
);
299 static int delete_one_unmergeable_job(Manager
*m
, Job
*j
) {
304 /* Tries to delete one item in the linked list
305 * j->transaction_next->transaction_next->... that conflicts
306 * whith another one, in an attempt to make an inconsistent
307 * transaction work. */
309 /* We rely here on the fact that if a merged with b does not
310 * merge with c, either a or b merge with c neither */
311 LIST_FOREACH(transaction
, j
, j
)
312 LIST_FOREACH(transaction
, k
, j
->transaction_next
) {
315 /* Is this one mergeable? Then skip it */
316 if (job_type_is_mergeable(j
->type
, k
->type
))
319 /* Ok, we found two that conflict, let's see if we can
320 * drop one of them */
321 if (!j
->matters_to_anchor
)
323 else if (!k
->matters_to_anchor
)
328 /* Ok, we can drop one, so let's do so. */
329 log_debug("Try to fix job merging by deleting job %s/%s", unit_id(d
->unit
), job_type_to_string(d
->type
));
330 transaction_delete_job(m
, d
);
337 static int transaction_merge_jobs(Manager
*m
) {
344 /* First step, check whether any of the jobs for one specific
345 * task conflict. If so, try to drop one of them. */
346 HASHMAP_FOREACH(j
, m
->transaction_jobs
, i
) {
351 LIST_FOREACH(transaction
, k
, j
->transaction_next
) {
352 if ((r
= job_type_merge(&t
, k
->type
)) >= 0)
355 /* OK, we could not merge all jobs for this
356 * action. Let's see if we can get rid of one
359 if ((r
= delete_one_unmergeable_job(m
, j
)) >= 0)
360 /* Ok, we managed to drop one, now
361 * let's ask our callers to call us
362 * again after garbage collecting */
365 /* We couldn't merge anything. Failure */
370 /* Second step, merge the jobs. */
371 HASHMAP_FOREACH(j
, m
->transaction_jobs
, i
) {
375 /* Merge all transactions */
376 LIST_FOREACH(transaction
, k
, j
->transaction_next
)
377 assert_se(job_type_merge(&t
, k
->type
) == 0);
379 /* If an active job is mergeable, merge it too */
380 if (j
->unit
->meta
.job
)
381 job_type_merge(&t
, j
->unit
->meta
.job
->type
); /* Might fail. Which is OK */
383 while ((k
= j
->transaction_next
)) {
385 transaction_merge_and_delete_job(m
, k
, j
, t
);
388 transaction_merge_and_delete_job(m
, j
, k
, t
);
391 assert(!j
->transaction_next
);
392 assert(!j
->transaction_prev
);
398 static bool unit_matters_to_anchor(Unit
*u
, Job
*j
) {
400 assert(!j
->transaction_prev
);
402 /* Checks whether at least one of the jobs for this unit
403 * matters to the anchor. */
405 LIST_FOREACH(transaction
, j
, j
)
406 if (j
->matters_to_anchor
)
412 static int transaction_verify_order_one(Manager
*m
, Job
*j
, Job
*from
, unsigned generation
) {
419 assert(!j
->transaction_prev
);
421 /* Does a recursive sweep through the ordering graph, looking
422 * for a cycle. If we find cycle we try to break it. */
424 /* Did we find a cycle? */
425 if (j
->marker
&& j
->generation
== generation
) {
428 /* So, we already have been here. We have a
429 * cycle. Let's try to break it. We go backwards in
430 * our path and try to find a suitable job to
431 * remove. We use the marker to find our way back,
432 * since smart how we are we stored our way back in
435 log_debug("Found cycle on %s/%s", unit_id(j
->unit
), job_type_to_string(j
->type
));
437 for (k
= from
; k
; k
= (k
->generation
== generation
? k
->marker
: NULL
)) {
439 log_debug("Walked on cycle path to %s/%s", unit_id(j
->unit
), job_type_to_string(j
->type
));
442 !unit_matters_to_anchor(k
->unit
, k
)) {
443 /* Ok, we can drop this one, so let's
445 log_debug("Breaking order cycle by deleting job %s/%s", unit_id(k
->unit
), job_type_to_string(k
->type
));
446 transaction_delete_unit(m
, k
->unit
);
450 /* Check if this in fact was the beginning of
456 log_debug("Unable to break cycle");
461 /* Make the marker point to where we come from, so that we can
462 * find our way backwards if we want to break a cycle */
464 j
->generation
= generation
;
466 /* We assume that the the dependencies are bidirectional, and
467 * hence can ignore UNIT_AFTER */
468 SET_FOREACH(u
, j
->unit
->meta
.dependencies
[UNIT_BEFORE
], i
) {
471 /* Is there a job for this unit? */
472 if (!(o
= hashmap_get(m
->transaction_jobs
, u
)))
474 /* Ok, there is no job for this in the
475 * transaction, but maybe there is already one
477 if (!(o
= u
->meta
.job
))
480 if ((r
= transaction_verify_order_one(m
, o
, j
, generation
)) < 0)
484 /* Ok, let's backtrack, and remember that this entry is not on
485 * our path anymore. */
491 static int transaction_verify_order(Manager
*m
, unsigned *generation
) {
499 /* Check if the ordering graph is cyclic. If it is, try to fix
500 * that up by dropping one of the jobs. */
502 HASHMAP_FOREACH(j
, m
->transaction_jobs
, i
)
503 if ((r
= transaction_verify_order_one(m
, j
, NULL
, (*generation
)++)) < 0)
509 static void transaction_collect_garbage(Manager
*m
) {
514 /* Drop jobs that are not required by any other job */
522 HASHMAP_FOREACH(j
, m
->transaction_jobs
, i
) {
526 log_debug("Garbage collecting job %s/%s", unit_id(j
->unit
), job_type_to_string(j
->type
));
527 transaction_delete_job(m
, j
);
535 static int transaction_is_destructive(Manager
*m
, JobMode mode
) {
541 /* Checks whether applying this transaction means that
542 * existing jobs would be replaced */
544 HASHMAP_FOREACH(j
, m
->transaction_jobs
, i
) {
547 assert(!j
->transaction_prev
);
548 assert(!j
->transaction_next
);
550 if (j
->unit
->meta
.job
&&
551 j
->unit
->meta
.job
!= j
&&
552 !job_type_is_superset(j
->type
, j
->unit
->meta
.job
->type
))
559 static void transaction_minimize_impact(Manager
*m
) {
563 /* Drops all unnecessary jobs that reverse already active jobs
564 * or that stop a running service. */
572 HASHMAP_FOREACH(j
, m
->transaction_jobs
, i
) {
573 LIST_FOREACH(transaction
, j
, j
) {
574 bool stops_running_service
, changes_existing_job
;
576 /* If it matters, we shouldn't drop it */
577 if (j
->matters_to_anchor
)
580 /* Would this stop a running service?
581 * Would this change an existing job?
582 * If so, let's drop this entry */
584 stops_running_service
=
585 j
->type
== JOB_STOP
&& UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(j
->unit
));
587 changes_existing_job
=
588 j
->unit
->meta
.job
&& job_type_is_conflicting(j
->type
, j
->unit
->meta
.job
->state
);
590 if (!stops_running_service
&& !changes_existing_job
)
593 if (stops_running_service
)
594 log_debug("%s/%s would stop a running service.", unit_id(j
->unit
), job_type_to_string(j
->type
));
596 if (changes_existing_job
)
597 log_debug("%s/%s would change existing job.", unit_id(j
->unit
), job_type_to_string(j
->type
));
599 /* Ok, let's get rid of this */
600 log_debug("Deleting %s/%s to minimize impact.", unit_id(j
->unit
), job_type_to_string(j
->type
));
602 transaction_delete_job(m
, j
);
614 static int transaction_apply(Manager
*m
, JobMode mode
) {
619 /* Moves the transaction jobs to the set of active jobs */
621 HASHMAP_FOREACH(j
, m
->transaction_jobs
, i
) {
623 assert(!j
->transaction_prev
);
624 assert(!j
->transaction_next
);
629 if ((r
= hashmap_put(m
->jobs
, UINT32_TO_PTR(j
->id
), j
)) < 0)
633 while ((j
= hashmap_steal_first(m
->transaction_jobs
))) {
637 if (j
->unit
->meta
.job
)
638 job_free(j
->unit
->meta
.job
);
640 j
->unit
->meta
.job
= j
;
643 /* We're fully installed. Now let's free data we don't
646 assert(!j
->transaction_next
);
647 assert(!j
->transaction_prev
);
652 /* As last step, kill all remaining job dependencies. */
653 transaction_clean_dependencies(m
);
659 HASHMAP_FOREACH(j
, m
->transaction_jobs
, i
) {
663 hashmap_remove(m
->jobs
, UINT32_TO_PTR(j
->id
));
669 static int transaction_activate(Manager
*m
, JobMode mode
) {
671 unsigned generation
= 1;
675 /* This applies the changes recorded in transaction_jobs to
676 * the actual list of jobs, if possible. */
678 /* First step: figure out which jobs matter */
679 transaction_find_jobs_that_matter_to_anchor(m
, NULL
, generation
++);
681 /* Second step: Try not to stop any running services if
682 * we don't have to. Don't try to reverse running
683 * jobs if we don't have to. */
684 transaction_minimize_impact(m
);
687 /* Third step: Let's remove unneeded jobs that might
689 transaction_collect_garbage(m
);
691 /* Fourth step: verify order makes sense and correct
692 * cycles if necessary and possible */
693 if ((r
= transaction_verify_order(m
, &generation
)) >= 0)
697 log_debug("Requested transaction contains an unfixable cyclic ordering dependency: %s", strerror(-r
));
701 /* Let's see if the resulting transaction ordering
702 * graph is still cyclic... */
706 /* Fifth step: let's drop unmergeable entries if
707 * necessary and possible, merge entries we can
709 if ((r
= transaction_merge_jobs(m
)) >= 0)
713 log_debug("Requested transaction contains unmergable jobs: %s", strerror(-r
));
717 /* Sixth step: an entry got dropped, let's garbage
718 * collect its dependencies. */
719 transaction_collect_garbage(m
);
721 /* Let's see if the resulting transaction still has
722 * unmergeable entries ... */
725 /* Seventh step: check whether we can actually apply this */
726 if (mode
== JOB_FAIL
)
727 if ((r
= transaction_is_destructive(m
, mode
)) < 0) {
728 log_debug("Requested transaction contradicts existing jobs: %s", strerror(-r
));
732 /* Eights step: apply changes */
733 if ((r
= transaction_apply(m
, mode
)) < 0) {
734 log_debug("Failed to apply transaction: %s", strerror(-r
));
738 assert(hashmap_isempty(m
->transaction_jobs
));
739 assert(!m
->transaction_anchor
);
744 transaction_abort(m
);
748 static Job
* transaction_add_one_job(Manager
*m
, JobType type
, Unit
*unit
, bool force
, bool *is_new
) {
755 /* Looks for an axisting prospective job and returns that. If
756 * it doesn't exist it is created and added to the prospective
759 f
= hashmap_get(m
->transaction_jobs
, unit
);
761 LIST_FOREACH(transaction
, j
, f
) {
762 assert(j
->unit
== unit
);
764 if (j
->type
== type
) {
771 if (unit
->meta
.job
&& unit
->meta
.job
->type
== type
)
773 else if (!(j
= job_new(m
, type
, unit
)))
778 j
->matters_to_anchor
= false;
781 LIST_PREPEND(Job
, transaction
, f
, j
);
783 if ((r
= hashmap_replace(m
->transaction_jobs
, unit
, f
)) < 0) {
794 void manager_transaction_unlink_job(Manager
*m
, Job
*j
) {
798 if (j
->transaction_prev
)
799 j
->transaction_prev
->transaction_next
= j
->transaction_next
;
800 else if (j
->transaction_next
)
801 hashmap_replace(m
->transaction_jobs
, j
->unit
, j
->transaction_next
);
803 hashmap_remove_value(m
->transaction_jobs
, j
->unit
, j
);
805 if (j
->transaction_next
)
806 j
->transaction_next
->transaction_prev
= j
->transaction_prev
;
808 j
->transaction_prev
= j
->transaction_next
= NULL
;
810 while (j
->subject_list
)
811 job_dependency_free(j
->subject_list
);
813 while (j
->object_list
) {
814 Job
*other
= j
->object_list
->matters
? j
->object_list
->subject
: NULL
;
816 job_dependency_free(j
->object_list
);
819 log_debug("Deleting job %s/%s as dependency of job %s/%s",
820 unit_id(other
->unit
), job_type_to_string(other
->type
),
821 unit_id(j
->unit
), job_type_to_string(j
->type
));
822 transaction_delete_job(m
, other
);
827 static int transaction_add_job_and_dependencies(Manager
*m
, JobType type
, Unit
*unit
, Job
*by
, bool matters
, bool force
, Job
**_ret
) {
835 assert(type
< _JOB_TYPE_MAX
);
838 if (unit
->meta
.load_state
!= UNIT_LOADED
)
841 if (!unit_job_is_applicable(unit
, type
))
844 /* First add the job. */
845 if (!(ret
= transaction_add_one_job(m
, type
, unit
, force
, &is_new
)))
848 /* Then, add a link to the job. */
849 if (!job_dependency_new(by
, ret
, matters
))
853 /* Finally, recursively add in all dependencies. */
854 if (type
== JOB_START
|| type
== JOB_RELOAD_OR_START
) {
855 SET_FOREACH(dep
, ret
->unit
->meta
.dependencies
[UNIT_REQUIRES
], i
)
856 if ((r
= transaction_add_job_and_dependencies(m
, JOB_START
, dep
, ret
, true, force
, NULL
)) < 0 && r
!= -EBADR
)
858 SET_FOREACH(dep
, ret
->unit
->meta
.dependencies
[UNIT_SOFT_REQUIRES
], i
)
859 if ((r
= transaction_add_job_and_dependencies(m
, JOB_START
, dep
, ret
, !force
, force
, NULL
)) < 0 && r
!= -EBADR
)
861 SET_FOREACH(dep
, ret
->unit
->meta
.dependencies
[UNIT_WANTS
], i
)
862 if ((r
= transaction_add_job_and_dependencies(m
, JOB_START
, dep
, ret
, false, force
, NULL
)) < 0 && r
!= -EBADR
)
864 SET_FOREACH(dep
, ret
->unit
->meta
.dependencies
[UNIT_REQUISITE
], i
)
865 if ((r
= transaction_add_job_and_dependencies(m
, JOB_VERIFY_ACTIVE
, dep
, ret
, true, force
, NULL
)) < 0 && r
!= -EBADR
)
867 SET_FOREACH(dep
, ret
->unit
->meta
.dependencies
[UNIT_SOFT_REQUISITE
], i
)
868 if ((r
= transaction_add_job_and_dependencies(m
, JOB_VERIFY_ACTIVE
, dep
, ret
, !force
, force
, NULL
)) < 0 && r
!= -EBADR
)
870 SET_FOREACH(dep
, ret
->unit
->meta
.dependencies
[UNIT_CONFLICTS
], i
)
871 if ((r
= transaction_add_job_and_dependencies(m
, JOB_STOP
, dep
, ret
, true, force
, NULL
)) < 0 && r
!= -EBADR
)
874 } else if (type
== JOB_STOP
|| type
== JOB_RESTART
|| type
== JOB_TRY_RESTART
) {
876 SET_FOREACH(dep
, ret
->unit
->meta
.dependencies
[UNIT_REQUIRED_BY
], i
)
877 if ((r
= transaction_add_job_and_dependencies(m
, type
, dep
, ret
, true, force
, NULL
)) < 0 && r
!= -EBADR
)
881 /* JOB_VERIFY_STARTED, JOB_RELOAD require no dependency handling */
890 int manager_add_job(Manager
*m
, JobType type
, Unit
*unit
, JobMode mode
, bool force
, Job
**_ret
) {
895 assert(type
< _JOB_TYPE_MAX
);
897 assert(mode
< _JOB_MODE_MAX
);
899 log_debug("Trying to enqueue job %s/%s", unit_id(unit
), job_type_to_string(type
));
901 if ((r
= transaction_add_job_and_dependencies(m
, type
, unit
, NULL
, true, force
, &ret
))) {
902 transaction_abort(m
);
906 if ((r
= transaction_activate(m
, mode
)) < 0)
909 log_debug("Enqueued job %s/%s", unit_id(unit
), job_type_to_string(type
));
917 Job
*manager_get_job(Manager
*m
, uint32_t id
) {
920 return hashmap_get(m
->jobs
, UINT32_TO_PTR(id
));
923 Unit
*manager_get_unit(Manager
*m
, const char *name
) {
927 return hashmap_get(m
->units
, name
);
930 void manager_dispatch_load_queue(Manager
*m
) {
935 /* Make sure we are not run recursively */
936 if (m
->dispatching_load_queue
)
939 m
->dispatching_load_queue
= true;
941 /* Dispatches the load queue. Takes a unit from the queue and
942 * tries to load its data until the queue is empty */
944 while ((meta
= m
->load_queue
)) {
945 assert(meta
->in_load_queue
);
947 unit_load(UNIT(meta
));
950 m
->dispatching_load_queue
= false;
953 int manager_load_unit(Manager
*m
, const char *path
, Unit
**_ret
) {
962 /* This will load the service information files, but not actually
963 * start any services or anything. */
965 name
= file_name_from_path(path
);
967 if ((ret
= manager_get_unit(m
, name
))) {
972 if (!(ret
= unit_new(m
)))
976 if (!(ret
->meta
.load_path
= strdup(path
))) {
982 if ((r
= unit_add_name(ret
, name
)) < 0) {
987 unit_add_to_load_queue(ret
);
988 manager_dispatch_load_queue(m
);
994 void manager_dump_jobs(Manager
*s
, FILE *f
, const char *prefix
) {
1001 HASHMAP_FOREACH(j
, s
->jobs
, i
)
1002 job_dump(j
, f
, prefix
);
1005 void manager_dump_units(Manager
*s
, FILE *f
, const char *prefix
) {
1013 HASHMAP_FOREACH_KEY(u
, t
, s
->units
, i
)
1014 if (unit_id(u
) == t
)
1015 unit_dump(u
, f
, prefix
);
1018 void manager_clear_jobs(Manager
*m
) {
1023 transaction_abort(m
);
1025 while ((j
= hashmap_first(m
->jobs
)))
1029 void manager_dispatch_run_queue(Manager
*m
) {
1032 if (m
->dispatching_run_queue
)
1035 m
->dispatching_run_queue
= true;
1037 while ((j
= m
->run_queue
)) {
1038 assert(j
->installed
);
1039 assert(j
->in_run_queue
);
1041 job_run_and_invalidate(j
);
1044 m
->dispatching_run_queue
= false;
1047 static int manager_dispatch_sigchld(Manager
*m
) {
1050 log_debug("dispatching SIGCHLD");
1057 if (waitid(P_ALL
, 0, &si
, WEXITED
|WNOHANG
) < 0) {
1059 if (errno
== ECHILD
)
1068 if (si
.si_code
!= CLD_EXITED
&& si
.si_code
!= CLD_KILLED
&& si
.si_code
!= CLD_DUMPED
)
1071 log_debug("child %llu died (code=%s, status=%i)", (long long unsigned) si
.si_pid
, sigchld_code_to_string(si
.si_code
), si
.si_status
);
1073 if (!(u
= hashmap_remove(m
->watch_pids
, UINT32_TO_PTR(si
.si_pid
))))
1076 UNIT_VTABLE(u
)->sigchld_event(u
, si
.si_pid
, si
.si_code
, si
.si_status
);
1082 static int manager_process_signal_fd(Manager
*m
, bool *quit
) {
1084 struct signalfd_siginfo sfsi
;
1085 bool sigchld
= false;
1090 if ((n
= read(m
->signal_watch
.fd
, &sfsi
, sizeof(sfsi
))) != sizeof(sfsi
)) {
1095 if (errno
== EAGAIN
)
1101 switch (sfsi
.ssi_signo
) {
1113 log_info("Got unhandled signal <%s>.", strsignal(sfsi
.ssi_signo
));
1118 return manager_dispatch_sigchld(m
);
1123 static int process_event(Manager
*m
, struct epoll_event
*ev
, bool *quit
) {
1130 assert(w
= ev
->data
.ptr
);
1136 /* An incoming signal? */
1137 if (ev
->events
!= EPOLLIN
)
1140 if ((r
= manager_process_signal_fd(m
, quit
)) < 0)
1147 /* Some fd event, to be dispatched to the units */
1148 UNIT_VTABLE(w
->data
.unit
)->fd_event(w
->data
.unit
, w
->fd
, ev
->events
, w
);
1155 /* Some timer event, to be dispatched to the units */
1156 if ((k
= read(ev
->data
.fd
, &v
, sizeof(v
))) != sizeof(v
)) {
1158 if (k
< 0 && (errno
== EINTR
|| errno
== EAGAIN
))
1161 return k
< 0 ? -errno
: -EIO
;
1164 UNIT_VTABLE(w
->data
.unit
)->timer_event(w
->data
.unit
, v
, w
);
1169 /* Some mount table change, intended for the mount subsystem */
1170 mount_fd_event(m
, ev
->events
);
1174 /* Some notification from udev, intended for the device subsystem */
1175 device_fd_event(m
, ev
->events
);
1178 case WATCH_DBUS_WATCH
:
1179 bus_watch_event(m
, w
, ev
->events
);
1182 case WATCH_DBUS_TIMEOUT
:
1183 bus_timeout_event(m
, w
, ev
->events
);
1187 assert_not_reached("Unknown epoll event type.");
1193 int manager_loop(Manager
*m
) {
1197 RATELIMIT_DEFINE(rl
, 1*USEC_PER_SEC
, 1000);
1202 struct epoll_event event
;
1205 if (!ratelimit_test(&rl
)) {
1206 /* Yay, something is going seriously wrong, pause a little */
1207 log_warning("Looping too fast. Throttling execution a little.");
1211 manager_dispatch_run_queue(m
);
1213 if (m
->request_bus_dispatch
) {
1218 if ((n
= epoll_wait(m
->epoll_fd
, &event
, 1, -1)) < 0) {
1220 if (errno
== -EINTR
)
1228 if ((r
= process_event(m
, &event
, &quit
)) < 0)
1236 int manager_get_unit_from_dbus_path(Manager
*m
, const char *s
, Unit
**_u
) {
1244 if (!startswith(s
, "/org/freedesktop/systemd1/unit/"))
1247 if (!(n
= bus_path_unescape(s
+31)))
1250 u
= manager_get_unit(m
, n
);