]>
git.ipfire.org Git - people/ms/systemd.git/blob - manager.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
8 #include <sys/signalfd.h>
20 static int manager_setup_signals(Manager
*m
) {
22 struct epoll_event ev
;
26 assert_se(reset_all_signal_handlers() == 0);
28 assert_se(sigemptyset(&mask
) == 0);
29 assert_se(sigaddset(&mask
, SIGCHLD
) == 0);
30 assert_se(sigaddset(&mask
, SIGINT
) == 0); /* Kernel sends us this on control-alt-del */
31 assert_se(sigaddset(&mask
, SIGWINCH
) == 0); /* Kernel sends us this on kbrequest (alt-arrowup) */
32 assert_se(sigaddset(&mask
, SIGTERM
) == 0);
33 assert_se(sigaddset(&mask
, SIGHUP
) == 0);
34 assert_se(sigaddset(&mask
, SIGUSR1
) == 0);
35 assert_se(sigaddset(&mask
, SIGUSR2
) == 0);
36 assert_se(sigaddset(&mask
, SIGPIPE
) == 0);
37 assert_se(sigprocmask(SIG_SETMASK
, &mask
, NULL
) == 0);
39 m
->signal_watch
.type
= WATCH_SIGNAL
;
40 if ((m
->signal_watch
.fd
= signalfd(-1, &mask
, SFD_NONBLOCK
|SFD_CLOEXEC
)) < 0)
45 ev
.data
.ptr
= &m
->signal_watch
;
47 if (epoll_ctl(m
->epoll_fd
, EPOLL_CTL_ADD
, m
->signal_watch
.fd
, &ev
) < 0)
53 Manager
* manager_new(void) {
56 if (!(m
= new0(Manager
, 1)))
59 m
->signal_watch
.fd
= m
->mount_watch
.fd
= m
->udev_watch
.fd
= m
->epoll_fd
= -1;
61 if (!(m
->units
= hashmap_new(string_hash_func
, string_compare_func
)))
64 if (!(m
->jobs
= hashmap_new(trivial_hash_func
, trivial_compare_func
)))
67 if (!(m
->transaction_jobs
= hashmap_new(trivial_hash_func
, trivial_compare_func
)))
70 if (!(m
->watch_pids
= hashmap_new(trivial_hash_func
, trivial_compare_func
)))
73 if ((m
->epoll_fd
= epoll_create1(EPOLL_CLOEXEC
)) < 0)
76 if (manager_setup_signals(m
) < 0)
86 void manager_free(Manager
*m
) {
93 while ((j
= hashmap_first(m
->transaction_jobs
)))
96 while ((u
= hashmap_first(m
->units
)))
99 for (c
= 0; c
< _UNIT_TYPE_MAX
; c
++)
100 if (unit_vtable
[c
]->shutdown
)
101 unit_vtable
[c
]->shutdown(m
);
103 hashmap_free(m
->units
);
104 hashmap_free(m
->jobs
);
105 hashmap_free(m
->transaction_jobs
);
106 hashmap_free(m
->watch_pids
);
108 if (m
->epoll_fd
>= 0)
109 close_nointr(m
->epoll_fd
);
110 if (m
->signal_watch
.fd
>= 0)
111 close_nointr(m
->signal_watch
.fd
);
116 int manager_coldplug(Manager
*m
) {
125 /* First, let's ask every type to load all units from
126 * disk/kernel that it might know */
127 for (c
= 0; c
< _UNIT_TYPE_MAX
; c
++)
128 if (unit_vtable
[c
]->enumerate
)
129 if ((r
= unit_vtable
[c
]->enumerate(m
)) < 0)
132 manager_dispatch_load_queue(m
);
134 /* Then, let's set up their initial state. */
135 HASHMAP_FOREACH_KEY(u
, k
, m
->units
, i
) {
141 if (UNIT_VTABLE(u
)->coldplug
)
142 if ((r
= UNIT_VTABLE(u
)->coldplug(u
)) < 0)
149 static void transaction_delete_job(Manager
*m
, Job
*j
) {
153 /* Deletes one job from the transaction */
155 manager_transaction_unlink_job(m
, j
);
161 static void transaction_delete_unit(Manager
*m
, Unit
*u
) {
164 /* Deletes all jobs associated with a certain unit from the
167 while ((j
= hashmap_get(m
->transaction_jobs
, u
)))
168 transaction_delete_job(m
, j
);
171 static void transaction_clean_dependencies(Manager
*m
) {
177 /* Drops all dependencies of all installed jobs */
179 HASHMAP_FOREACH(j
, m
->jobs
, i
) {
180 while (j
->subject_list
)
181 job_dependency_free(j
->subject_list
);
182 while (j
->object_list
)
183 job_dependency_free(j
->object_list
);
186 assert(!m
->transaction_anchor
);
189 static void transaction_abort(Manager
*m
) {
194 while ((j
= hashmap_first(m
->transaction_jobs
)))
196 transaction_delete_job(m
, j
);
200 assert(hashmap_isempty(m
->transaction_jobs
));
202 transaction_clean_dependencies(m
);
205 static void transaction_find_jobs_that_matter_to_anchor(Manager
*m
, Job
*j
, unsigned generation
) {
210 /* A recursive sweep through the graph that marks all units
211 * that matter to the anchor job, i.e. are directly or
212 * indirectly a dependency of the anchor job via paths that
213 * are fully marked as mattering. */
218 l
= m
->transaction_anchor
;
220 LIST_FOREACH(subject
, l
, l
) {
222 /* This link does not matter */
226 /* This unit has already been marked */
227 if (l
->object
->generation
== generation
)
230 l
->object
->matters_to_anchor
= true;
231 l
->object
->generation
= generation
;
233 transaction_find_jobs_that_matter_to_anchor(m
, l
->object
, generation
);
237 static void transaction_merge_and_delete_job(Manager
*m
, Job
*j
, Job
*other
, JobType t
) {
238 JobDependency
*l
, *last
;
242 assert(j
->unit
== other
->unit
);
243 assert(!j
->installed
);
245 /* Merges 'other' into 'j' and then deletes j. */
248 j
->state
= JOB_WAITING
;
249 j
->forced
= j
->forced
|| other
->forced
;
251 j
->matters_to_anchor
= j
->matters_to_anchor
|| other
->matters_to_anchor
;
253 /* Patch us in as new owner of the JobDependency objects */
255 LIST_FOREACH(subject
, l
, other
->subject_list
) {
256 assert(l
->subject
== other
);
261 /* Merge both lists */
263 last
->subject_next
= j
->subject_list
;
265 j
->subject_list
->subject_prev
= last
;
266 j
->subject_list
= other
->subject_list
;
269 /* Patch us in as new owner of the JobDependency objects */
271 LIST_FOREACH(object
, l
, other
->object_list
) {
272 assert(l
->object
== other
);
277 /* Merge both lists */
279 last
->object_next
= j
->object_list
;
281 j
->object_list
->object_prev
= last
;
282 j
->object_list
= other
->object_list
;
285 /* Kill the other job */
286 other
->subject_list
= NULL
;
287 other
->object_list
= NULL
;
288 transaction_delete_job(m
, other
);
291 static int delete_one_unmergeable_job(Manager
*m
, Job
*j
) {
296 /* Tries to delete one item in the linked list
297 * j->transaction_next->transaction_next->... that conflicts
298 * whith another one, in an attempt to make an inconsistent
299 * transaction work. */
301 /* We rely here on the fact that if a merged with b does not
302 * merge with c, either a or b merge with c neither */
303 LIST_FOREACH(transaction
, j
, j
)
304 LIST_FOREACH(transaction
, k
, j
->transaction_next
) {
307 /* Is this one mergeable? Then skip it */
308 if (job_type_is_mergeable(j
->type
, k
->type
))
311 /* Ok, we found two that conflict, let's see if we can
312 * drop one of them */
313 if (!j
->matters_to_anchor
)
315 else if (!k
->matters_to_anchor
)
320 /* Ok, we can drop one, so let's do so. */
321 log_debug("Try to fix job merging by deleting job %s/%s", unit_id(d
->unit
), job_type_to_string(d
->type
));
322 transaction_delete_job(m
, d
);
329 static int transaction_merge_jobs(Manager
*m
) {
336 /* First step, check whether any of the jobs for one specific
337 * task conflict. If so, try to drop one of them. */
338 HASHMAP_FOREACH(j
, m
->transaction_jobs
, i
) {
343 LIST_FOREACH(transaction
, k
, j
->transaction_next
) {
344 if ((r
= job_type_merge(&t
, k
->type
)) >= 0)
347 /* OK, we could not merge all jobs for this
348 * action. Let's see if we can get rid of one
351 if ((r
= delete_one_unmergeable_job(m
, j
)) >= 0)
352 /* Ok, we managed to drop one, now
353 * let's ask our callers to call us
354 * again after garbage collecting */
357 /* We couldn't merge anything. Failure */
362 /* Second step, merge the jobs. */
363 HASHMAP_FOREACH(j
, m
->transaction_jobs
, i
) {
367 /* Merge all transactions */
368 LIST_FOREACH(transaction
, k
, j
->transaction_next
)
369 assert_se(job_type_merge(&t
, k
->type
) == 0);
371 /* If an active job is mergeable, merge it too */
372 if (j
->unit
->meta
.job
)
373 job_type_merge(&t
, j
->unit
->meta
.job
->type
); /* Might fail. Which is OK */
375 while ((k
= j
->transaction_next
)) {
377 transaction_merge_and_delete_job(m
, k
, j
, t
);
380 transaction_merge_and_delete_job(m
, j
, k
, t
);
383 assert(!j
->transaction_next
);
384 assert(!j
->transaction_prev
);
390 static bool unit_matters_to_anchor(Unit
*u
, Job
*j
) {
392 assert(!j
->transaction_prev
);
394 /* Checks whether at least one of the jobs for this unit
395 * matters to the anchor. */
397 LIST_FOREACH(transaction
, j
, j
)
398 if (j
->matters_to_anchor
)
404 static int transaction_verify_order_one(Manager
*m
, Job
*j
, Job
*from
, unsigned generation
) {
411 assert(!j
->transaction_prev
);
413 /* Does a recursive sweep through the ordering graph, looking
414 * for a cycle. If we find cycle we try to break it. */
416 /* Did we find a cycle? */
417 if (j
->marker
&& j
->generation
== generation
) {
420 /* So, we already have been here. We have a
421 * cycle. Let's try to break it. We go backwards in
422 * our path and try to find a suitable job to
423 * remove. We use the marker to find our way back,
424 * since smart how we are we stored our way back in
427 log_debug("Found cycle on %s/%s", unit_id(j
->unit
), job_type_to_string(j
->type
));
429 for (k
= from
; k
; k
= (k
->generation
== generation
? k
->marker
: NULL
)) {
431 log_debug("Walked on cycle path to %s/%s", unit_id(j
->unit
), job_type_to_string(j
->type
));
434 !unit_matters_to_anchor(k
->unit
, k
)) {
435 /* Ok, we can drop this one, so let's
437 log_debug("Breaking order cycle by deleting job %s/%s", unit_id(k
->unit
), job_type_to_string(k
->type
));
438 transaction_delete_unit(m
, k
->unit
);
442 /* Check if this in fact was the beginning of
448 log_debug("Unable to break cycle");
453 /* Make the marker point to where we come from, so that we can
454 * find our way backwards if we want to break a cycle */
456 j
->generation
= generation
;
458 /* We assume that the the dependencies are bidirectional, and
459 * hence can ignore UNIT_AFTER */
460 SET_FOREACH(u
, j
->unit
->meta
.dependencies
[UNIT_BEFORE
], i
) {
463 /* Is there a job for this unit? */
464 if (!(o
= hashmap_get(m
->transaction_jobs
, u
)))
466 /* Ok, there is no job for this in the
467 * transaction, but maybe there is already one
469 if (!(o
= u
->meta
.job
))
472 if ((r
= transaction_verify_order_one(m
, o
, j
, generation
)) < 0)
476 /* Ok, let's backtrack, and remember that this entry is not on
477 * our path anymore. */
483 static int transaction_verify_order(Manager
*m
, unsigned *generation
) {
491 /* Check if the ordering graph is cyclic. If it is, try to fix
492 * that up by dropping one of the jobs. */
494 HASHMAP_FOREACH(j
, m
->transaction_jobs
, i
)
495 if ((r
= transaction_verify_order_one(m
, j
, NULL
, (*generation
)++)) < 0)
501 static void transaction_collect_garbage(Manager
*m
) {
506 /* Drop jobs that are not required by any other job */
514 HASHMAP_FOREACH(j
, m
->transaction_jobs
, i
) {
518 log_debug("Garbage collecting job %s/%s", unit_id(j
->unit
), job_type_to_string(j
->type
));
519 transaction_delete_job(m
, j
);
527 static int transaction_is_destructive(Manager
*m
, JobMode mode
) {
533 /* Checks whether applying this transaction means that
534 * existing jobs would be replaced */
536 HASHMAP_FOREACH(j
, m
->transaction_jobs
, i
) {
539 assert(!j
->transaction_prev
);
540 assert(!j
->transaction_next
);
542 if (j
->unit
->meta
.job
&&
543 j
->unit
->meta
.job
!= j
&&
544 !job_type_is_superset(j
->type
, j
->unit
->meta
.job
->type
))
551 static void transaction_minimize_impact(Manager
*m
) {
555 /* Drops all unnecessary jobs that reverse already active jobs
556 * or that stop a running service. */
564 HASHMAP_FOREACH(j
, m
->transaction_jobs
, i
) {
565 LIST_FOREACH(transaction
, j
, j
) {
566 bool stops_running_service
, changes_existing_job
;
568 /* If it matters, we shouldn't drop it */
569 if (j
->matters_to_anchor
)
572 /* Would this stop a running service?
573 * Would this change an existing job?
574 * If so, let's drop this entry */
576 stops_running_service
=
577 j
->type
== JOB_STOP
&& UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(j
->unit
));
579 changes_existing_job
=
580 j
->unit
->meta
.job
&& job_type_is_conflicting(j
->type
, j
->unit
->meta
.job
->state
);
582 if (!stops_running_service
&& !changes_existing_job
)
585 if (stops_running_service
)
586 log_debug("%s/%s would stop a running service.", unit_id(j
->unit
), job_type_to_string(j
->type
));
588 if (changes_existing_job
)
589 log_debug("%s/%s would change existing job.", unit_id(j
->unit
), job_type_to_string(j
->type
));
591 /* Ok, let's get rid of this */
592 log_debug("Deleting %s/%s to minimize impact.", unit_id(j
->unit
), job_type_to_string(j
->type
));
594 transaction_delete_job(m
, j
);
606 static int transaction_apply(Manager
*m
, JobMode mode
) {
611 /* Moves the transaction jobs to the set of active jobs */
613 HASHMAP_FOREACH(j
, m
->transaction_jobs
, i
) {
615 assert(!j
->transaction_prev
);
616 assert(!j
->transaction_next
);
621 if ((r
= hashmap_put(m
->jobs
, UINT32_TO_PTR(j
->id
), j
)) < 0)
625 while ((j
= hashmap_steal_first(m
->transaction_jobs
))) {
629 if (j
->unit
->meta
.job
)
630 job_free(j
->unit
->meta
.job
);
632 j
->unit
->meta
.job
= j
;
635 /* We're fully installed. Now let's free data we don't
638 assert(!j
->transaction_next
);
639 assert(!j
->transaction_prev
);
644 /* As last step, kill all remaining job dependencies. */
645 transaction_clean_dependencies(m
);
651 HASHMAP_FOREACH(j
, m
->transaction_jobs
, i
) {
655 hashmap_remove(m
->jobs
, UINT32_TO_PTR(j
->id
));
661 static int transaction_activate(Manager
*m
, JobMode mode
) {
663 unsigned generation
= 1;
667 /* This applies the changes recorded in transaction_jobs to
668 * the actual list of jobs, if possible. */
670 /* First step: figure out which jobs matter */
671 transaction_find_jobs_that_matter_to_anchor(m
, NULL
, generation
++);
673 /* Second step: Try not to stop any running services if
674 * we don't have to. Don't try to reverse running
675 * jobs if we don't have to. */
676 transaction_minimize_impact(m
);
679 /* Third step: Let's remove unneeded jobs that might
681 transaction_collect_garbage(m
);
683 /* Fourth step: verify order makes sense and correct
684 * cycles if necessary and possible */
685 if ((r
= transaction_verify_order(m
, &generation
)) >= 0)
689 log_debug("Requested transaction contains an unfixable cyclic ordering dependency: %s", strerror(-r
));
693 /* Let's see if the resulting transaction ordering
694 * graph is still cyclic... */
698 /* Fifth step: let's drop unmergeable entries if
699 * necessary and possible, merge entries we can
701 if ((r
= transaction_merge_jobs(m
)) >= 0)
705 log_debug("Requested transaction contains unmergable jobs: %s", strerror(-r
));
709 /* Sixth step: an entry got dropped, let's garbage
710 * collect its dependencies. */
711 transaction_collect_garbage(m
);
713 /* Let's see if the resulting transaction still has
714 * unmergeable entries ... */
717 /* Seventh step: check whether we can actually apply this */
718 if (mode
== JOB_FAIL
)
719 if ((r
= transaction_is_destructive(m
, mode
)) < 0) {
720 log_debug("Requested transaction contradicts existing jobs: %s", strerror(-r
));
724 /* Eights step: apply changes */
725 if ((r
= transaction_apply(m
, mode
)) < 0) {
726 log_debug("Failed to apply transaction: %s", strerror(-r
));
730 assert(hashmap_isempty(m
->transaction_jobs
));
731 assert(!m
->transaction_anchor
);
736 transaction_abort(m
);
740 static Job
* transaction_add_one_job(Manager
*m
, JobType type
, Unit
*unit
, bool force
, bool *is_new
) {
747 /* Looks for an axisting prospective job and returns that. If
748 * it doesn't exist it is created and added to the prospective
751 f
= hashmap_get(m
->transaction_jobs
, unit
);
753 LIST_FOREACH(transaction
, j
, f
) {
754 assert(j
->unit
== unit
);
756 if (j
->type
== type
) {
763 if (unit
->meta
.job
&& unit
->meta
.job
->type
== type
)
765 else if (!(j
= job_new(m
, type
, unit
)))
770 j
->matters_to_anchor
= false;
773 LIST_PREPEND(Job
, transaction
, f
, j
);
775 if ((r
= hashmap_replace(m
->transaction_jobs
, unit
, f
)) < 0) {
786 void manager_transaction_unlink_job(Manager
*m
, Job
*j
) {
790 if (j
->transaction_prev
)
791 j
->transaction_prev
->transaction_next
= j
->transaction_next
;
792 else if (j
->transaction_next
)
793 hashmap_replace(m
->transaction_jobs
, j
->unit
, j
->transaction_next
);
795 hashmap_remove_value(m
->transaction_jobs
, j
->unit
, j
);
797 if (j
->transaction_next
)
798 j
->transaction_next
->transaction_prev
= j
->transaction_prev
;
800 j
->transaction_prev
= j
->transaction_next
= NULL
;
802 while (j
->subject_list
)
803 job_dependency_free(j
->subject_list
);
805 while (j
->object_list
) {
806 Job
*other
= j
->object_list
->matters
? j
->object_list
->subject
: NULL
;
808 job_dependency_free(j
->object_list
);
811 log_debug("Deleting job %s/%s as dependency of job %s/%s",
812 unit_id(other
->unit
), job_type_to_string(other
->type
),
813 unit_id(j
->unit
), job_type_to_string(j
->type
));
814 transaction_delete_job(m
, other
);
819 static int transaction_add_job_and_dependencies(Manager
*m
, JobType type
, Unit
*unit
, Job
*by
, bool matters
, bool force
, Job
**_ret
) {
827 assert(type
< _JOB_TYPE_MAX
);
830 if (unit
->meta
.load_state
!= UNIT_LOADED
)
833 if (!unit_job_is_applicable(unit
, type
))
836 /* First add the job. */
837 if (!(ret
= transaction_add_one_job(m
, type
, unit
, force
, &is_new
)))
840 /* Then, add a link to the job. */
841 if (!job_dependency_new(by
, ret
, matters
))
845 /* Finally, recursively add in all dependencies. */
846 if (type
== JOB_START
|| type
== JOB_RELOAD_OR_START
) {
847 SET_FOREACH(dep
, ret
->unit
->meta
.dependencies
[UNIT_REQUIRES
], i
)
848 if ((r
= transaction_add_job_and_dependencies(m
, JOB_START
, dep
, ret
, true, force
, NULL
)) < 0 && r
!= -EBADR
)
850 SET_FOREACH(dep
, ret
->unit
->meta
.dependencies
[UNIT_SOFT_REQUIRES
], i
)
851 if ((r
= transaction_add_job_and_dependencies(m
, JOB_START
, dep
, ret
, !force
, force
, NULL
)) < 0 && r
!= -EBADR
)
853 SET_FOREACH(dep
, ret
->unit
->meta
.dependencies
[UNIT_WANTS
], i
)
854 if ((r
= transaction_add_job_and_dependencies(m
, JOB_START
, dep
, ret
, false, force
, NULL
)) < 0 && r
!= -EBADR
)
856 SET_FOREACH(dep
, ret
->unit
->meta
.dependencies
[UNIT_REQUISITE
], i
)
857 if ((r
= transaction_add_job_and_dependencies(m
, JOB_VERIFY_ACTIVE
, dep
, ret
, true, force
, NULL
)) < 0 && r
!= -EBADR
)
859 SET_FOREACH(dep
, ret
->unit
->meta
.dependencies
[UNIT_SOFT_REQUISITE
], i
)
860 if ((r
= transaction_add_job_and_dependencies(m
, JOB_VERIFY_ACTIVE
, dep
, ret
, !force
, force
, NULL
)) < 0 && r
!= -EBADR
)
862 SET_FOREACH(dep
, ret
->unit
->meta
.dependencies
[UNIT_CONFLICTS
], i
)
863 if ((r
= transaction_add_job_and_dependencies(m
, JOB_STOP
, dep
, ret
, true, force
, NULL
)) < 0 && r
!= -EBADR
)
866 } else if (type
== JOB_STOP
|| type
== JOB_RESTART
|| type
== JOB_TRY_RESTART
) {
868 SET_FOREACH(dep
, ret
->unit
->meta
.dependencies
[UNIT_REQUIRED_BY
], i
)
869 if ((r
= transaction_add_job_and_dependencies(m
, type
, dep
, ret
, true, force
, NULL
)) < 0 && r
!= -EBADR
)
873 /* JOB_VERIFY_STARTED, JOB_RELOAD require no dependency handling */
882 int manager_add_job(Manager
*m
, JobType type
, Unit
*unit
, JobMode mode
, bool force
, Job
**_ret
) {
887 assert(type
< _JOB_TYPE_MAX
);
889 assert(mode
< _JOB_MODE_MAX
);
891 log_debug("Trying to enqueue job %s/%s", unit_id(unit
), job_type_to_string(type
));
893 if ((r
= transaction_add_job_and_dependencies(m
, type
, unit
, NULL
, true, force
, &ret
))) {
894 transaction_abort(m
);
898 if ((r
= transaction_activate(m
, mode
)) < 0)
901 log_debug("Enqueued job %s/%s", unit_id(unit
), job_type_to_string(type
));
909 Job
*manager_get_job(Manager
*m
, uint32_t id
) {
912 return hashmap_get(m
->jobs
, UINT32_TO_PTR(id
));
915 Unit
*manager_get_unit(Manager
*m
, const char *name
) {
919 return hashmap_get(m
->units
, name
);
922 void manager_dispatch_load_queue(Manager
*m
) {
927 /* Make sure we are not run recursively */
928 if (m
->dispatching_load_queue
)
931 m
->dispatching_load_queue
= true;
933 /* Dispatches the load queue. Takes a unit from the queue and
934 * tries to load its data until the queue is empty */
936 while ((meta
= m
->load_queue
)) {
937 assert(meta
->in_load_queue
);
939 unit_load(UNIT(meta
));
942 m
->dispatching_load_queue
= false;
945 int manager_load_unit(Manager
*m
, const char *path
, Unit
**_ret
) {
954 /* This will load the service information files, but not actually
955 * start any services or anything. */
957 name
= file_name_from_path(path
);
959 if ((ret
= manager_get_unit(m
, name
))) {
964 if (!(ret
= unit_new(m
)))
968 if (!(ret
->meta
.load_path
= strdup(path
))) {
974 if ((r
= unit_add_name(ret
, name
)) < 0) {
979 unit_add_to_load_queue(ret
);
980 manager_dispatch_load_queue(m
);
986 void manager_dump_jobs(Manager
*s
, FILE *f
, const char *prefix
) {
993 HASHMAP_FOREACH(j
, s
->jobs
, i
)
994 job_dump(j
, f
, prefix
);
997 void manager_dump_units(Manager
*s
, FILE *f
, const char *prefix
) {
1005 HASHMAP_FOREACH_KEY(u
, t
, s
->units
, i
)
1006 if (unit_id(u
) == t
)
1007 unit_dump(u
, f
, prefix
);
1010 void manager_clear_jobs(Manager
*m
) {
1015 transaction_abort(m
);
1017 while ((j
= hashmap_first(m
->jobs
)))
1021 void manager_dispatch_run_queue(Manager
*m
) {
1024 if (m
->dispatching_run_queue
)
1027 m
->dispatching_run_queue
= true;
1029 while ((j
= m
->run_queue
)) {
1030 assert(j
->installed
);
1031 assert(j
->in_run_queue
);
1033 job_run_and_invalidate(j
);
1036 m
->dispatching_run_queue
= false;
1039 static int manager_dispatch_sigchld(Manager
*m
) {
1042 log_debug("dispatching SIGCHLD");
1049 if (waitid(P_ALL
, 0, &si
, WEXITED
|WNOHANG
) < 0) {
1051 if (errno
== ECHILD
)
1060 if (si
.si_code
!= CLD_EXITED
&& si
.si_code
!= CLD_KILLED
&& si
.si_code
!= CLD_DUMPED
)
1063 log_debug("child %llu died (code=%s, status=%i)", (long long unsigned) si
.si_pid
, sigchld_code(si
.si_code
), si
.si_status
);
1065 if (!(u
= hashmap_remove(m
->watch_pids
, UINT32_TO_PTR(si
.si_pid
))))
1068 UNIT_VTABLE(u
)->sigchld_event(u
, si
.si_pid
, si
.si_code
, si
.si_status
);
1074 static int manager_process_signal_fd(Manager
*m
, bool *quit
) {
1076 struct signalfd_siginfo sfsi
;
1077 bool sigchld
= false;
1082 if ((n
= read(m
->signal_watch
.fd
, &sfsi
, sizeof(sfsi
))) != sizeof(sfsi
)) {
1087 if (errno
== EAGAIN
)
1093 switch (sfsi
.ssi_signo
) {
1105 log_info("Got unhandled signal <%s>.", strsignal(sfsi
.ssi_signo
));
1110 return manager_dispatch_sigchld(m
);
1115 static int process_event(Manager
*m
, struct epoll_event
*ev
, bool *quit
) {
1122 assert(w
= ev
->data
.ptr
);
1128 /* An incoming signal? */
1129 if (ev
->events
!= EPOLLIN
)
1132 if ((r
= manager_process_signal_fd(m
, quit
)) < 0)
1139 /* Some fd event, to be dispatched to the units */
1140 UNIT_VTABLE(w
->unit
)->fd_event(w
->unit
, w
->fd
, ev
->events
, w
);
1147 /* Some timer event, to be dispatched to the units */
1148 if ((k
= read(ev
->data
.fd
, &v
, sizeof(v
))) != sizeof(v
)) {
1150 if (k
< 0 && (errno
== EINTR
|| errno
== EAGAIN
))
1153 return k
< 0 ? -errno
: -EIO
;
1156 UNIT_VTABLE(w
->unit
)->timer_event(w
->unit
, v
, w
);
1161 /* Some mount table change, intended for the mount subsystem */
1162 mount_fd_event(m
, ev
->events
);
1166 /* Some notification from udev, intended for the device subsystem */
1167 device_fd_event(m
, ev
->events
);
1171 assert_not_reached("Unknown epoll event type.");
1177 int manager_loop(Manager
*m
) {
1184 struct epoll_event event
;
1187 manager_dispatch_run_queue(m
);
1189 if ((n
= epoll_wait(m
->epoll_fd
, &event
, 1, -1)) < 0) {
1191 if (errno
== -EINTR
)
1199 if ((r
= process_event(m
, &event
, &quit
)) < 0)