]>
git.ipfire.org Git - people/ms/systemd.git/blob - manager.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include <sys/epoll.h>
27 #include <sys/signalfd.h>
38 #include "ratelimit.h"
40 static int manager_setup_signals(Manager
*m
) {
42 struct epoll_event ev
;
46 assert_se(reset_all_signal_handlers() == 0);
48 assert_se(sigemptyset(&mask
) == 0);
49 assert_se(sigaddset(&mask
, SIGCHLD
) == 0);
50 assert_se(sigaddset(&mask
, SIGINT
) == 0); /* Kernel sends us this on control-alt-del */
51 assert_se(sigaddset(&mask
, SIGWINCH
) == 0); /* Kernel sends us this on kbrequest (alt-arrowup) */
52 assert_se(sigaddset(&mask
, SIGTERM
) == 0);
53 assert_se(sigaddset(&mask
, SIGHUP
) == 0);
54 assert_se(sigaddset(&mask
, SIGUSR1
) == 0);
55 assert_se(sigaddset(&mask
, SIGUSR2
) == 0);
56 assert_se(sigaddset(&mask
, SIGPIPE
) == 0);
57 assert_se(sigprocmask(SIG_SETMASK
, &mask
, NULL
) == 0);
59 m
->signal_watch
.type
= WATCH_SIGNAL
;
60 if ((m
->signal_watch
.fd
= signalfd(-1, &mask
, SFD_NONBLOCK
|SFD_CLOEXEC
)) < 0)
65 ev
.data
.ptr
= &m
->signal_watch
;
67 if (epoll_ctl(m
->epoll_fd
, EPOLL_CTL_ADD
, m
->signal_watch
.fd
, &ev
) < 0)
73 Manager
* manager_new(void) {
76 if (!(m
= new0(Manager
, 1)))
79 m
->signal_watch
.fd
= m
->mount_watch
.fd
= m
->udev_watch
.fd
= m
->epoll_fd
= -1;
80 m
->current_job_id
= 1; /* start as id #1, so that we can leave #0 around as "null-like" value */
82 if (!(m
->units
= hashmap_new(string_hash_func
, string_compare_func
)))
85 if (!(m
->jobs
= hashmap_new(trivial_hash_func
, trivial_compare_func
)))
88 if (!(m
->transaction_jobs
= hashmap_new(trivial_hash_func
, trivial_compare_func
)))
91 if (!(m
->watch_pids
= hashmap_new(trivial_hash_func
, trivial_compare_func
)))
94 if ((m
->epoll_fd
= epoll_create1(EPOLL_CLOEXEC
)) < 0)
97 if (manager_setup_signals(m
) < 0)
100 /* FIXME: this should be called only when the D-Bus bus daemon is running */
111 void manager_free(Manager
*m
) {
118 while ((j
= hashmap_first(m
->transaction_jobs
)))
121 while ((u
= hashmap_first(m
->units
)))
124 for (c
= 0; c
< _UNIT_TYPE_MAX
; c
++)
125 if (unit_vtable
[c
]->shutdown
)
126 unit_vtable
[c
]->shutdown(m
);
130 hashmap_free(m
->units
);
131 hashmap_free(m
->jobs
);
132 hashmap_free(m
->transaction_jobs
);
133 hashmap_free(m
->watch_pids
);
135 if (m
->epoll_fd
>= 0)
136 close_nointr(m
->epoll_fd
);
137 if (m
->signal_watch
.fd
>= 0)
138 close_nointr(m
->signal_watch
.fd
);
143 int manager_coldplug(Manager
*m
) {
152 /* First, let's ask every type to load all units from
153 * disk/kernel that it might know */
154 for (c
= 0; c
< _UNIT_TYPE_MAX
; c
++)
155 if (unit_vtable
[c
]->enumerate
)
156 if ((r
= unit_vtable
[c
]->enumerate(m
)) < 0)
159 manager_dispatch_load_queue(m
);
161 /* Then, let's set up their initial state. */
162 HASHMAP_FOREACH_KEY(u
, k
, m
->units
, i
) {
168 if (UNIT_VTABLE(u
)->coldplug
)
169 if ((r
= UNIT_VTABLE(u
)->coldplug(u
)) < 0)
176 static void transaction_delete_job(Manager
*m
, Job
*j
) {
180 /* Deletes one job from the transaction */
182 manager_transaction_unlink_job(m
, j
);
188 static void transaction_delete_unit(Manager
*m
, Unit
*u
) {
191 /* Deletes all jobs associated with a certain unit from the
194 while ((j
= hashmap_get(m
->transaction_jobs
, u
)))
195 transaction_delete_job(m
, j
);
198 static void transaction_clean_dependencies(Manager
*m
) {
204 /* Drops all dependencies of all installed jobs */
206 HASHMAP_FOREACH(j
, m
->jobs
, i
) {
207 while (j
->subject_list
)
208 job_dependency_free(j
->subject_list
);
209 while (j
->object_list
)
210 job_dependency_free(j
->object_list
);
213 assert(!m
->transaction_anchor
);
216 static void transaction_abort(Manager
*m
) {
221 while ((j
= hashmap_first(m
->transaction_jobs
)))
223 transaction_delete_job(m
, j
);
227 assert(hashmap_isempty(m
->transaction_jobs
));
229 transaction_clean_dependencies(m
);
232 static void transaction_find_jobs_that_matter_to_anchor(Manager
*m
, Job
*j
, unsigned generation
) {
237 /* A recursive sweep through the graph that marks all units
238 * that matter to the anchor job, i.e. are directly or
239 * indirectly a dependency of the anchor job via paths that
240 * are fully marked as mattering. */
245 l
= m
->transaction_anchor
;
247 LIST_FOREACH(subject
, l
, l
) {
249 /* This link does not matter */
253 /* This unit has already been marked */
254 if (l
->object
->generation
== generation
)
257 l
->object
->matters_to_anchor
= true;
258 l
->object
->generation
= generation
;
260 transaction_find_jobs_that_matter_to_anchor(m
, l
->object
, generation
);
264 static void transaction_merge_and_delete_job(Manager
*m
, Job
*j
, Job
*other
, JobType t
) {
265 JobDependency
*l
, *last
;
269 assert(j
->unit
== other
->unit
);
270 assert(!j
->installed
);
272 /* Merges 'other' into 'j' and then deletes j. */
275 j
->state
= JOB_WAITING
;
276 j
->forced
= j
->forced
|| other
->forced
;
278 j
->matters_to_anchor
= j
->matters_to_anchor
|| other
->matters_to_anchor
;
280 /* Patch us in as new owner of the JobDependency objects */
282 LIST_FOREACH(subject
, l
, other
->subject_list
) {
283 assert(l
->subject
== other
);
288 /* Merge both lists */
290 last
->subject_next
= j
->subject_list
;
292 j
->subject_list
->subject_prev
= last
;
293 j
->subject_list
= other
->subject_list
;
296 /* Patch us in as new owner of the JobDependency objects */
298 LIST_FOREACH(object
, l
, other
->object_list
) {
299 assert(l
->object
== other
);
304 /* Merge both lists */
306 last
->object_next
= j
->object_list
;
308 j
->object_list
->object_prev
= last
;
309 j
->object_list
= other
->object_list
;
312 /* Kill the other job */
313 other
->subject_list
= NULL
;
314 other
->object_list
= NULL
;
315 transaction_delete_job(m
, other
);
318 static int delete_one_unmergeable_job(Manager
*m
, Job
*j
) {
323 /* Tries to delete one item in the linked list
324 * j->transaction_next->transaction_next->... that conflicts
325 * whith another one, in an attempt to make an inconsistent
326 * transaction work. */
328 /* We rely here on the fact that if a merged with b does not
329 * merge with c, either a or b merge with c neither */
330 LIST_FOREACH(transaction
, j
, j
)
331 LIST_FOREACH(transaction
, k
, j
->transaction_next
) {
334 /* Is this one mergeable? Then skip it */
335 if (job_type_is_mergeable(j
->type
, k
->type
))
338 /* Ok, we found two that conflict, let's see if we can
339 * drop one of them */
340 if (!j
->matters_to_anchor
)
342 else if (!k
->matters_to_anchor
)
347 /* Ok, we can drop one, so let's do so. */
348 log_debug("Try to fix job merging by deleting job %s/%s", unit_id(d
->unit
), job_type_to_string(d
->type
));
349 transaction_delete_job(m
, d
);
356 static int transaction_merge_jobs(Manager
*m
) {
363 /* First step, check whether any of the jobs for one specific
364 * task conflict. If so, try to drop one of them. */
365 HASHMAP_FOREACH(j
, m
->transaction_jobs
, i
) {
370 LIST_FOREACH(transaction
, k
, j
->transaction_next
) {
371 if ((r
= job_type_merge(&t
, k
->type
)) >= 0)
374 /* OK, we could not merge all jobs for this
375 * action. Let's see if we can get rid of one
378 if ((r
= delete_one_unmergeable_job(m
, j
)) >= 0)
379 /* Ok, we managed to drop one, now
380 * let's ask our callers to call us
381 * again after garbage collecting */
384 /* We couldn't merge anything. Failure */
389 /* Second step, merge the jobs. */
390 HASHMAP_FOREACH(j
, m
->transaction_jobs
, i
) {
394 /* Merge all transactions */
395 LIST_FOREACH(transaction
, k
, j
->transaction_next
)
396 assert_se(job_type_merge(&t
, k
->type
) == 0);
398 /* If an active job is mergeable, merge it too */
399 if (j
->unit
->meta
.job
)
400 job_type_merge(&t
, j
->unit
->meta
.job
->type
); /* Might fail. Which is OK */
402 while ((k
= j
->transaction_next
)) {
404 transaction_merge_and_delete_job(m
, k
, j
, t
);
407 transaction_merge_and_delete_job(m
, j
, k
, t
);
410 assert(!j
->transaction_next
);
411 assert(!j
->transaction_prev
);
417 static bool unit_matters_to_anchor(Unit
*u
, Job
*j
) {
419 assert(!j
->transaction_prev
);
421 /* Checks whether at least one of the jobs for this unit
422 * matters to the anchor. */
424 LIST_FOREACH(transaction
, j
, j
)
425 if (j
->matters_to_anchor
)
431 static int transaction_verify_order_one(Manager
*m
, Job
*j
, Job
*from
, unsigned generation
) {
438 assert(!j
->transaction_prev
);
440 /* Does a recursive sweep through the ordering graph, looking
441 * for a cycle. If we find cycle we try to break it. */
443 /* Did we find a cycle? */
444 if (j
->marker
&& j
->generation
== generation
) {
447 /* So, we already have been here. We have a
448 * cycle. Let's try to break it. We go backwards in
449 * our path and try to find a suitable job to
450 * remove. We use the marker to find our way back,
451 * since smart how we are we stored our way back in
454 log_debug("Found cycle on %s/%s", unit_id(j
->unit
), job_type_to_string(j
->type
));
456 for (k
= from
; k
; k
= (k
->generation
== generation
? k
->marker
: NULL
)) {
458 log_debug("Walked on cycle path to %s/%s", unit_id(j
->unit
), job_type_to_string(j
->type
));
461 !unit_matters_to_anchor(k
->unit
, k
)) {
462 /* Ok, we can drop this one, so let's
464 log_debug("Breaking order cycle by deleting job %s/%s", unit_id(k
->unit
), job_type_to_string(k
->type
));
465 transaction_delete_unit(m
, k
->unit
);
469 /* Check if this in fact was the beginning of
475 log_debug("Unable to break cycle");
480 /* Make the marker point to where we come from, so that we can
481 * find our way backwards if we want to break a cycle */
483 j
->generation
= generation
;
485 /* We assume that the the dependencies are bidirectional, and
486 * hence can ignore UNIT_AFTER */
487 SET_FOREACH(u
, j
->unit
->meta
.dependencies
[UNIT_BEFORE
], i
) {
490 /* Is there a job for this unit? */
491 if (!(o
= hashmap_get(m
->transaction_jobs
, u
)))
493 /* Ok, there is no job for this in the
494 * transaction, but maybe there is already one
496 if (!(o
= u
->meta
.job
))
499 if ((r
= transaction_verify_order_one(m
, o
, j
, generation
)) < 0)
503 /* Ok, let's backtrack, and remember that this entry is not on
504 * our path anymore. */
510 static int transaction_verify_order(Manager
*m
, unsigned *generation
) {
518 /* Check if the ordering graph is cyclic. If it is, try to fix
519 * that up by dropping one of the jobs. */
521 HASHMAP_FOREACH(j
, m
->transaction_jobs
, i
)
522 if ((r
= transaction_verify_order_one(m
, j
, NULL
, (*generation
)++)) < 0)
528 static void transaction_collect_garbage(Manager
*m
) {
533 /* Drop jobs that are not required by any other job */
541 HASHMAP_FOREACH(j
, m
->transaction_jobs
, i
) {
545 log_debug("Garbage collecting job %s/%s", unit_id(j
->unit
), job_type_to_string(j
->type
));
546 transaction_delete_job(m
, j
);
554 static int transaction_is_destructive(Manager
*m
, JobMode mode
) {
560 /* Checks whether applying this transaction means that
561 * existing jobs would be replaced */
563 HASHMAP_FOREACH(j
, m
->transaction_jobs
, i
) {
566 assert(!j
->transaction_prev
);
567 assert(!j
->transaction_next
);
569 if (j
->unit
->meta
.job
&&
570 j
->unit
->meta
.job
!= j
&&
571 !job_type_is_superset(j
->type
, j
->unit
->meta
.job
->type
))
578 static void transaction_minimize_impact(Manager
*m
) {
582 /* Drops all unnecessary jobs that reverse already active jobs
583 * or that stop a running service. */
591 HASHMAP_FOREACH(j
, m
->transaction_jobs
, i
) {
592 LIST_FOREACH(transaction
, j
, j
) {
593 bool stops_running_service
, changes_existing_job
;
595 /* If it matters, we shouldn't drop it */
596 if (j
->matters_to_anchor
)
599 /* Would this stop a running service?
600 * Would this change an existing job?
601 * If so, let's drop this entry */
603 stops_running_service
=
604 j
->type
== JOB_STOP
&& UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(j
->unit
));
606 changes_existing_job
=
607 j
->unit
->meta
.job
&& job_type_is_conflicting(j
->type
, j
->unit
->meta
.job
->state
);
609 if (!stops_running_service
&& !changes_existing_job
)
612 if (stops_running_service
)
613 log_debug("%s/%s would stop a running service.", unit_id(j
->unit
), job_type_to_string(j
->type
));
615 if (changes_existing_job
)
616 log_debug("%s/%s would change existing job.", unit_id(j
->unit
), job_type_to_string(j
->type
));
618 /* Ok, let's get rid of this */
619 log_debug("Deleting %s/%s to minimize impact.", unit_id(j
->unit
), job_type_to_string(j
->type
));
621 transaction_delete_job(m
, j
);
633 static int transaction_apply(Manager
*m
, JobMode mode
) {
638 /* Moves the transaction jobs to the set of active jobs */
640 HASHMAP_FOREACH(j
, m
->transaction_jobs
, i
) {
642 assert(!j
->transaction_prev
);
643 assert(!j
->transaction_next
);
648 if ((r
= hashmap_put(m
->jobs
, UINT32_TO_PTR(j
->id
), j
)) < 0)
652 while ((j
= hashmap_steal_first(m
->transaction_jobs
))) {
656 if (j
->unit
->meta
.job
)
657 job_free(j
->unit
->meta
.job
);
659 j
->unit
->meta
.job
= j
;
662 /* We're fully installed. Now let's free data we don't
665 assert(!j
->transaction_next
);
666 assert(!j
->transaction_prev
);
671 /* As last step, kill all remaining job dependencies. */
672 transaction_clean_dependencies(m
);
678 HASHMAP_FOREACH(j
, m
->transaction_jobs
, i
) {
682 hashmap_remove(m
->jobs
, UINT32_TO_PTR(j
->id
));
688 static int transaction_activate(Manager
*m
, JobMode mode
) {
690 unsigned generation
= 1;
694 /* This applies the changes recorded in transaction_jobs to
695 * the actual list of jobs, if possible. */
697 /* First step: figure out which jobs matter */
698 transaction_find_jobs_that_matter_to_anchor(m
, NULL
, generation
++);
700 /* Second step: Try not to stop any running services if
701 * we don't have to. Don't try to reverse running
702 * jobs if we don't have to. */
703 transaction_minimize_impact(m
);
706 /* Third step: Let's remove unneeded jobs that might
708 transaction_collect_garbage(m
);
710 /* Fourth step: verify order makes sense and correct
711 * cycles if necessary and possible */
712 if ((r
= transaction_verify_order(m
, &generation
)) >= 0)
716 log_debug("Requested transaction contains an unfixable cyclic ordering dependency: %s", strerror(-r
));
720 /* Let's see if the resulting transaction ordering
721 * graph is still cyclic... */
725 /* Fifth step: let's drop unmergeable entries if
726 * necessary and possible, merge entries we can
728 if ((r
= transaction_merge_jobs(m
)) >= 0)
732 log_debug("Requested transaction contains unmergable jobs: %s", strerror(-r
));
736 /* Sixth step: an entry got dropped, let's garbage
737 * collect its dependencies. */
738 transaction_collect_garbage(m
);
740 /* Let's see if the resulting transaction still has
741 * unmergeable entries ... */
744 /* Seventh step: check whether we can actually apply this */
745 if (mode
== JOB_FAIL
)
746 if ((r
= transaction_is_destructive(m
, mode
)) < 0) {
747 log_debug("Requested transaction contradicts existing jobs: %s", strerror(-r
));
751 /* Eights step: apply changes */
752 if ((r
= transaction_apply(m
, mode
)) < 0) {
753 log_debug("Failed to apply transaction: %s", strerror(-r
));
757 assert(hashmap_isempty(m
->transaction_jobs
));
758 assert(!m
->transaction_anchor
);
763 transaction_abort(m
);
767 static Job
* transaction_add_one_job(Manager
*m
, JobType type
, Unit
*unit
, bool force
, bool *is_new
) {
774 /* Looks for an axisting prospective job and returns that. If
775 * it doesn't exist it is created and added to the prospective
778 f
= hashmap_get(m
->transaction_jobs
, unit
);
780 LIST_FOREACH(transaction
, j
, f
) {
781 assert(j
->unit
== unit
);
783 if (j
->type
== type
) {
790 if (unit
->meta
.job
&& unit
->meta
.job
->type
== type
)
792 else if (!(j
= job_new(m
, type
, unit
)))
797 j
->matters_to_anchor
= false;
800 LIST_PREPEND(Job
, transaction
, f
, j
);
802 if ((r
= hashmap_replace(m
->transaction_jobs
, unit
, f
)) < 0) {
813 void manager_transaction_unlink_job(Manager
*m
, Job
*j
) {
817 if (j
->transaction_prev
)
818 j
->transaction_prev
->transaction_next
= j
->transaction_next
;
819 else if (j
->transaction_next
)
820 hashmap_replace(m
->transaction_jobs
, j
->unit
, j
->transaction_next
);
822 hashmap_remove_value(m
->transaction_jobs
, j
->unit
, j
);
824 if (j
->transaction_next
)
825 j
->transaction_next
->transaction_prev
= j
->transaction_prev
;
827 j
->transaction_prev
= j
->transaction_next
= NULL
;
829 while (j
->subject_list
)
830 job_dependency_free(j
->subject_list
);
832 while (j
->object_list
) {
833 Job
*other
= j
->object_list
->matters
? j
->object_list
->subject
: NULL
;
835 job_dependency_free(j
->object_list
);
838 log_debug("Deleting job %s/%s as dependency of job %s/%s",
839 unit_id(other
->unit
), job_type_to_string(other
->type
),
840 unit_id(j
->unit
), job_type_to_string(j
->type
));
841 transaction_delete_job(m
, other
);
846 static int transaction_add_job_and_dependencies(Manager
*m
, JobType type
, Unit
*unit
, Job
*by
, bool matters
, bool force
, Job
**_ret
) {
854 assert(type
< _JOB_TYPE_MAX
);
857 if (unit
->meta
.load_state
!= UNIT_LOADED
)
860 if (!unit_job_is_applicable(unit
, type
))
863 /* First add the job. */
864 if (!(ret
= transaction_add_one_job(m
, type
, unit
, force
, &is_new
)))
867 /* Then, add a link to the job. */
868 if (!job_dependency_new(by
, ret
, matters
))
872 /* Finally, recursively add in all dependencies. */
873 if (type
== JOB_START
|| type
== JOB_RELOAD_OR_START
) {
874 SET_FOREACH(dep
, ret
->unit
->meta
.dependencies
[UNIT_REQUIRES
], i
)
875 if ((r
= transaction_add_job_and_dependencies(m
, JOB_START
, dep
, ret
, true, force
, NULL
)) < 0 && r
!= -EBADR
)
877 SET_FOREACH(dep
, ret
->unit
->meta
.dependencies
[UNIT_SOFT_REQUIRES
], i
)
878 if ((r
= transaction_add_job_and_dependencies(m
, JOB_START
, dep
, ret
, !force
, force
, NULL
)) < 0 && r
!= -EBADR
)
880 SET_FOREACH(dep
, ret
->unit
->meta
.dependencies
[UNIT_WANTS
], i
)
881 if ((r
= transaction_add_job_and_dependencies(m
, JOB_START
, dep
, ret
, false, force
, NULL
)) < 0 && r
!= -EBADR
)
883 SET_FOREACH(dep
, ret
->unit
->meta
.dependencies
[UNIT_REQUISITE
], i
)
884 if ((r
= transaction_add_job_and_dependencies(m
, JOB_VERIFY_ACTIVE
, dep
, ret
, true, force
, NULL
)) < 0 && r
!= -EBADR
)
886 SET_FOREACH(dep
, ret
->unit
->meta
.dependencies
[UNIT_SOFT_REQUISITE
], i
)
887 if ((r
= transaction_add_job_and_dependencies(m
, JOB_VERIFY_ACTIVE
, dep
, ret
, !force
, force
, NULL
)) < 0 && r
!= -EBADR
)
889 SET_FOREACH(dep
, ret
->unit
->meta
.dependencies
[UNIT_CONFLICTS
], i
)
890 if ((r
= transaction_add_job_and_dependencies(m
, JOB_STOP
, dep
, ret
, true, force
, NULL
)) < 0 && r
!= -EBADR
)
893 } else if (type
== JOB_STOP
|| type
== JOB_RESTART
|| type
== JOB_TRY_RESTART
) {
895 SET_FOREACH(dep
, ret
->unit
->meta
.dependencies
[UNIT_REQUIRED_BY
], i
)
896 if ((r
= transaction_add_job_and_dependencies(m
, type
, dep
, ret
, true, force
, NULL
)) < 0 && r
!= -EBADR
)
900 /* JOB_VERIFY_STARTED, JOB_RELOAD require no dependency handling */
909 int manager_add_job(Manager
*m
, JobType type
, Unit
*unit
, JobMode mode
, bool force
, Job
**_ret
) {
914 assert(type
< _JOB_TYPE_MAX
);
916 assert(mode
< _JOB_MODE_MAX
);
918 log_debug("Trying to enqueue job %s/%s", unit_id(unit
), job_type_to_string(type
));
920 if ((r
= transaction_add_job_and_dependencies(m
, type
, unit
, NULL
, true, force
, &ret
))) {
921 transaction_abort(m
);
925 if ((r
= transaction_activate(m
, mode
)) < 0)
928 log_debug("Enqueued job %s/%s", unit_id(unit
), job_type_to_string(type
));
936 Job
*manager_get_job(Manager
*m
, uint32_t id
) {
939 return hashmap_get(m
->jobs
, UINT32_TO_PTR(id
));
942 Unit
*manager_get_unit(Manager
*m
, const char *name
) {
946 return hashmap_get(m
->units
, name
);
949 void manager_dispatch_load_queue(Manager
*m
) {
954 /* Make sure we are not run recursively */
955 if (m
->dispatching_load_queue
)
958 m
->dispatching_load_queue
= true;
960 /* Dispatches the load queue. Takes a unit from the queue and
961 * tries to load its data until the queue is empty */
963 while ((meta
= m
->load_queue
)) {
964 assert(meta
->in_load_queue
);
966 unit_load(UNIT(meta
));
969 m
->dispatching_load_queue
= false;
972 int manager_load_unit(Manager
*m
, const char *path
, Unit
**_ret
) {
981 /* This will load the service information files, but not actually
982 * start any services or anything. */
984 name
= file_name_from_path(path
);
986 if ((ret
= manager_get_unit(m
, name
))) {
991 if (!(ret
= unit_new(m
)))
995 if (!(ret
->meta
.load_path
= strdup(path
))) {
1001 if ((r
= unit_add_name(ret
, name
)) < 0) {
1006 unit_add_to_load_queue(ret
);
1007 manager_dispatch_load_queue(m
);
1013 void manager_dump_jobs(Manager
*s
, FILE *f
, const char *prefix
) {
1020 HASHMAP_FOREACH(j
, s
->jobs
, i
)
1021 job_dump(j
, f
, prefix
);
1024 void manager_dump_units(Manager
*s
, FILE *f
, const char *prefix
) {
1032 HASHMAP_FOREACH_KEY(u
, t
, s
->units
, i
)
1033 if (unit_id(u
) == t
)
1034 unit_dump(u
, f
, prefix
);
1037 void manager_clear_jobs(Manager
*m
) {
1042 transaction_abort(m
);
1044 while ((j
= hashmap_first(m
->jobs
)))
1048 void manager_dispatch_run_queue(Manager
*m
) {
1051 if (m
->dispatching_run_queue
)
1054 m
->dispatching_run_queue
= true;
1056 while ((j
= m
->run_queue
)) {
1057 assert(j
->installed
);
1058 assert(j
->in_run_queue
);
1060 job_run_and_invalidate(j
);
1063 m
->dispatching_run_queue
= false;
1066 static int manager_dispatch_sigchld(Manager
*m
) {
1069 log_debug("dispatching SIGCHLD");
1076 if (waitid(P_ALL
, 0, &si
, WEXITED
|WNOHANG
) < 0) {
1078 if (errno
== ECHILD
)
1087 if (si
.si_code
!= CLD_EXITED
&& si
.si_code
!= CLD_KILLED
&& si
.si_code
!= CLD_DUMPED
)
1090 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
);
1092 if (!(u
= hashmap_remove(m
->watch_pids
, UINT32_TO_PTR(si
.si_pid
))))
1095 UNIT_VTABLE(u
)->sigchld_event(u
, si
.si_pid
, si
.si_code
, si
.si_status
);
1101 static int manager_process_signal_fd(Manager
*m
, bool *quit
) {
1103 struct signalfd_siginfo sfsi
;
1104 bool sigchld
= false;
1109 if ((n
= read(m
->signal_watch
.fd
, &sfsi
, sizeof(sfsi
))) != sizeof(sfsi
)) {
1114 if (errno
== EAGAIN
)
1120 switch (sfsi
.ssi_signo
) {
1132 log_info("Got unhandled signal <%s>.", strsignal(sfsi
.ssi_signo
));
1137 return manager_dispatch_sigchld(m
);
1142 static int process_event(Manager
*m
, struct epoll_event
*ev
, bool *quit
) {
1149 assert(w
= ev
->data
.ptr
);
1155 /* An incoming signal? */
1156 if (ev
->events
!= EPOLLIN
)
1159 if ((r
= manager_process_signal_fd(m
, quit
)) < 0)
1166 /* Some fd event, to be dispatched to the units */
1167 UNIT_VTABLE(w
->data
.unit
)->fd_event(w
->data
.unit
, w
->fd
, ev
->events
, w
);
1174 /* Some timer event, to be dispatched to the units */
1175 if ((k
= read(ev
->data
.fd
, &v
, sizeof(v
))) != sizeof(v
)) {
1177 if (k
< 0 && (errno
== EINTR
|| errno
== EAGAIN
))
1180 return k
< 0 ? -errno
: -EIO
;
1183 UNIT_VTABLE(w
->data
.unit
)->timer_event(w
->data
.unit
, v
, w
);
1188 /* Some mount table change, intended for the mount subsystem */
1189 mount_fd_event(m
, ev
->events
);
1193 /* Some notification from udev, intended for the device subsystem */
1194 device_fd_event(m
, ev
->events
);
1197 case WATCH_DBUS_WATCH
:
1198 bus_watch_event(m
, w
, ev
->events
);
1201 case WATCH_DBUS_TIMEOUT
:
1202 bus_timeout_event(m
, w
, ev
->events
);
1206 assert_not_reached("Unknown epoll event type.");
1212 int manager_loop(Manager
*m
) {
1216 RATELIMIT_DEFINE(rl
, 1*USEC_PER_SEC
, 1000);
1221 struct epoll_event event
;
1224 if (!ratelimit_test(&rl
)) {
1225 /* Yay, something is going seriously wrong, pause a little */
1226 log_warning("Looping too fast. Throttling execution a little.");
1230 manager_dispatch_run_queue(m
);
1232 if (m
->request_bus_dispatch
) {
1237 if ((n
= epoll_wait(m
->epoll_fd
, &event
, 1, -1)) < 0) {
1239 if (errno
== -EINTR
)
1247 if ((r
= process_event(m
, &event
, &quit
)) < 0)
1255 int manager_get_unit_from_dbus_path(Manager
*m
, const char *s
, Unit
**_u
) {
1263 if (!startswith(s
, "/org/freedesktop/systemd1/unit/"))
1266 if (!(n
= bus_path_unescape(s
+31)))
1269 u
= manager_get_unit(m
, n
);
1280 int manager_get_job_from_dbus_path(Manager
*m
, const char *s
, Job
**_j
) {
1289 if (!startswith(s
, "/org/freedesktop/systemd1/job/"))
1292 if ((r
= safe_atou(s
+ 30, &id
)) < 0)
1295 if (!(j
= manager_get_job(m
, id
)))