1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
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>
31 #include <sys/reboot.h>
32 #include <sys/ioctl.h>
36 #include <sys/types.h>
50 #include "ratelimit.h"
52 #include "mount-setup.h"
53 #include "unit-name.h"
54 #include "dbus-unit.h"
57 #include "path-lookup.h"
59 #include "bus-errors.h"
60 #include "exit-status.h"
61 #include "sd-daemon.h"
63 /* As soon as 16 units are in our GC queue, make sure to run a gc sweep */
64 #define GC_QUEUE_ENTRIES_MAX 16
66 /* As soon as 5s passed since a unit was added to our GC queue, make sure to run a gc sweep */
67 #define GC_QUEUE_USEC_MAX (10*USEC_PER_SEC)
69 /* Where clients shall send notification messages to */
70 #define NOTIFY_SOCKET_SYSTEM "/run/systemd/notify"
71 #define NOTIFY_SOCKET_USER "@/org/freedesktop/systemd1/notify"
73 static int manager_setup_notify(Manager
*m
) {
76 struct sockaddr_un un
;
78 struct epoll_event ev
;
83 m
->notify_watch
.type
= WATCH_NOTIFY
;
84 if ((m
->notify_watch
.fd
= socket(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0)) < 0) {
85 log_error("Failed to allocate notification socket: %m");
90 sa
.sa
.sa_family
= AF_UNIX
;
93 snprintf(sa
.un
.sun_path
, sizeof(sa
.un
.sun_path
), NOTIFY_SOCKET_USER
"/%llu", random_ull());
95 unlink(NOTIFY_SOCKET_SYSTEM
);
96 strncpy(sa
.un
.sun_path
, NOTIFY_SOCKET_SYSTEM
, sizeof(sa
.un
.sun_path
));
99 if (sa
.un
.sun_path
[0] == '@')
100 sa
.un
.sun_path
[0] = 0;
102 if (bind(m
->notify_watch
.fd
, &sa
.sa
, offsetof(struct sockaddr_un
, sun_path
) + 1 + strlen(sa
.un
.sun_path
+1)) < 0) {
103 log_error("bind() failed: %m");
107 if (setsockopt(m
->notify_watch
.fd
, SOL_SOCKET
, SO_PASSCRED
, &one
, sizeof(one
)) < 0) {
108 log_error("SO_PASSCRED failed: %m");
114 ev
.data
.ptr
= &m
->notify_watch
;
116 if (epoll_ctl(m
->epoll_fd
, EPOLL_CTL_ADD
, m
->notify_watch
.fd
, &ev
) < 0)
119 if (sa
.un
.sun_path
[0] == 0)
120 sa
.un
.sun_path
[0] = '@';
122 if (!(m
->notify_socket
= strdup(sa
.un
.sun_path
)))
125 log_debug("Using notification socket %s", m
->notify_socket
);
130 static int enable_special_signals(Manager
*m
) {
135 /* Enable that we get SIGINT on control-alt-del */
136 if (reboot(RB_DISABLE_CAD
) < 0)
137 log_warning("Failed to enable ctrl-alt-del handling: %m");
139 if ((fd
= open_terminal("/dev/tty0", O_RDWR
|O_NOCTTY
|O_CLOEXEC
)) < 0)
140 log_warning("Failed to open /dev/tty0: %m");
142 /* Enable that we get SIGWINCH on kbrequest */
143 if (ioctl(fd
, KDSIGACCEPT
, SIGWINCH
) < 0)
144 log_warning("Failed to enable kbrequest handling: %s", strerror(errno
));
146 close_nointr_nofail(fd
);
152 static int manager_setup_signals(Manager
*m
) {
154 struct epoll_event ev
;
159 /* We are not interested in SIGSTOP and friends. */
161 sa
.sa_handler
= SIG_DFL
;
162 sa
.sa_flags
= SA_NOCLDSTOP
|SA_RESTART
;
163 assert_se(sigaction(SIGCHLD
, &sa
, NULL
) == 0);
165 assert_se(sigemptyset(&mask
) == 0);
167 sigset_add_many(&mask
,
168 SIGCHLD
, /* Child died */
169 SIGTERM
, /* Reexecute daemon */
170 SIGHUP
, /* Reload configuration */
171 SIGUSR1
, /* systemd/upstart: reconnect to D-Bus */
172 SIGUSR2
, /* systemd: dump status */
173 SIGINT
, /* Kernel sends us this on control-alt-del */
174 SIGWINCH
, /* Kernel sends us this on kbrequest (alt-arrowup) */
175 SIGPWR
, /* Some kernel drivers and upsd send us this on power failure */
176 SIGRTMIN
+0, /* systemd: start default.target */
177 SIGRTMIN
+1, /* systemd: isolate rescue.target */
178 SIGRTMIN
+2, /* systemd: isolate emergency.target */
179 SIGRTMIN
+3, /* systemd: start halt.target */
180 SIGRTMIN
+4, /* systemd: start poweroff.target */
181 SIGRTMIN
+5, /* systemd: start reboot.target */
182 SIGRTMIN
+6, /* systemd: start kexec.target */
183 SIGRTMIN
+13, /* systemd: Immediate halt */
184 SIGRTMIN
+14, /* systemd: Immediate poweroff */
185 SIGRTMIN
+15, /* systemd: Immediate reboot */
186 SIGRTMIN
+16, /* systemd: Immediate kexec */
187 SIGRTMIN
+20, /* systemd: enable status messages */
188 SIGRTMIN
+21, /* systemd: disable status messages */
189 SIGRTMIN
+22, /* systemd: set log level to LOG_DEBUG */
190 SIGRTMIN
+23, /* systemd: set log level to LOG_INFO */
191 SIGRTMIN
+27, /* systemd: set log target to console */
192 SIGRTMIN
+28, /* systemd: set log target to kmsg */
193 SIGRTMIN
+29, /* systemd: set log target to syslog-or-kmsg */
195 assert_se(sigprocmask(SIG_SETMASK
, &mask
, NULL
) == 0);
197 m
->signal_watch
.type
= WATCH_SIGNAL
;
198 if ((m
->signal_watch
.fd
= signalfd(-1, &mask
, SFD_NONBLOCK
|SFD_CLOEXEC
)) < 0)
203 ev
.data
.ptr
= &m
->signal_watch
;
205 if (epoll_ctl(m
->epoll_fd
, EPOLL_CTL_ADD
, m
->signal_watch
.fd
, &ev
) < 0)
208 if (m
->running_as
== MANAGER_SYSTEM
)
209 return enable_special_signals(m
);
214 int manager_new(ManagerRunningAs running_as
, Manager
**_m
) {
219 assert(running_as
>= 0);
220 assert(running_as
< _MANAGER_RUNNING_AS_MAX
);
222 if (!(m
= new0(Manager
, 1)))
225 dual_timestamp_get(&m
->startup_timestamp
);
227 m
->running_as
= running_as
;
228 m
->name_data_slot
= m
->subscribed_data_slot
= -1;
229 m
->exit_code
= _MANAGER_EXIT_CODE_INVALID
;
230 m
->pin_cgroupfs_fd
= -1;
236 m
->signal_watch
.fd
= m
->mount_watch
.fd
= m
->udev_watch
.fd
= m
->epoll_fd
= m
->dev_autofs_fd
= m
->swap_watch
.fd
= -1;
237 m
->current_job_id
= 1; /* start as id #1, so that we can leave #0 around as "null-like" value */
239 if (!(m
->environment
= strv_copy(environ
)))
242 if (!(m
->default_controllers
= strv_new("cpu", NULL
)))
245 if (!(m
->units
= hashmap_new(string_hash_func
, string_compare_func
)))
248 if (!(m
->jobs
= hashmap_new(trivial_hash_func
, trivial_compare_func
)))
251 if (!(m
->transaction_jobs
= hashmap_new(trivial_hash_func
, trivial_compare_func
)))
254 if (!(m
->watch_pids
= hashmap_new(trivial_hash_func
, trivial_compare_func
)))
257 if (!(m
->cgroup_bondings
= hashmap_new(string_hash_func
, string_compare_func
)))
260 if (!(m
->watch_bus
= hashmap_new(string_hash_func
, string_compare_func
)))
263 if ((m
->epoll_fd
= epoll_create1(EPOLL_CLOEXEC
)) < 0)
266 if ((r
= lookup_paths_init(&m
->lookup_paths
, m
->running_as
, true)) < 0)
269 if ((r
= manager_setup_signals(m
)) < 0)
272 if ((r
= manager_setup_cgroup(m
)) < 0)
275 if ((r
= manager_setup_notify(m
)) < 0)
278 /* Try to connect to the busses, if possible. */
279 if ((r
= bus_init(m
, running_as
!= MANAGER_SYSTEM
)) < 0)
283 if ((m
->audit_fd
= audit_open()) < 0)
284 log_error("Failed to connect to audit log: %m");
287 m
->taint_usr
= dir_is_empty("/usr") > 0;
297 static unsigned manager_dispatch_cleanup_queue(Manager
*m
) {
303 while ((meta
= m
->cleanup_queue
)) {
304 assert(meta
->in_cleanup_queue
);
306 unit_free((Unit
*) meta
);
314 GC_OFFSET_IN_PATH
, /* This one is on the path we were traveling */
315 GC_OFFSET_UNSURE
, /* No clue */
316 GC_OFFSET_GOOD
, /* We still need this unit */
317 GC_OFFSET_BAD
, /* We don't need this unit anymore */
321 static void unit_gc_sweep(Unit
*u
, unsigned gc_marker
) {
328 if (u
->meta
.gc_marker
== gc_marker
+ GC_OFFSET_GOOD
||
329 u
->meta
.gc_marker
== gc_marker
+ GC_OFFSET_BAD
||
330 u
->meta
.gc_marker
== gc_marker
+ GC_OFFSET_IN_PATH
)
333 if (u
->meta
.in_cleanup_queue
)
336 if (unit_check_gc(u
))
339 u
->meta
.gc_marker
= gc_marker
+ GC_OFFSET_IN_PATH
;
343 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_REFERENCED_BY
], i
) {
344 unit_gc_sweep(other
, gc_marker
);
346 if (other
->meta
.gc_marker
== gc_marker
+ GC_OFFSET_GOOD
)
349 if (other
->meta
.gc_marker
!= gc_marker
+ GC_OFFSET_BAD
)
356 /* We were unable to find anything out about this entry, so
357 * let's investigate it later */
358 u
->meta
.gc_marker
= gc_marker
+ GC_OFFSET_UNSURE
;
359 unit_add_to_gc_queue(u
);
363 /* We definitely know that this one is not useful anymore, so
364 * let's mark it for deletion */
365 u
->meta
.gc_marker
= gc_marker
+ GC_OFFSET_BAD
;
366 unit_add_to_cleanup_queue(u
);
370 u
->meta
.gc_marker
= gc_marker
+ GC_OFFSET_GOOD
;
373 static unsigned manager_dispatch_gc_queue(Manager
*m
) {
380 if ((m
->n_in_gc_queue
< GC_QUEUE_ENTRIES_MAX
) &&
381 (m
->gc_queue_timestamp
<= 0 ||
382 (m
->gc_queue_timestamp
+ GC_QUEUE_USEC_MAX
) > now(CLOCK_MONOTONIC
)))
385 log_debug("Running GC...");
387 m
->gc_marker
+= _GC_OFFSET_MAX
;
388 if (m
->gc_marker
+ _GC_OFFSET_MAX
<= _GC_OFFSET_MAX
)
391 gc_marker
= m
->gc_marker
;
393 while ((meta
= m
->gc_queue
)) {
394 assert(meta
->in_gc_queue
);
396 unit_gc_sweep((Unit
*) meta
, gc_marker
);
398 LIST_REMOVE(Meta
, gc_queue
, m
->gc_queue
, meta
);
399 meta
->in_gc_queue
= false;
403 if (meta
->gc_marker
== gc_marker
+ GC_OFFSET_BAD
||
404 meta
->gc_marker
== gc_marker
+ GC_OFFSET_UNSURE
) {
405 log_debug("Collecting %s", meta
->id
);
406 meta
->gc_marker
= gc_marker
+ GC_OFFSET_BAD
;
407 unit_add_to_cleanup_queue((Unit
*) meta
);
411 m
->n_in_gc_queue
= 0;
412 m
->gc_queue_timestamp
= 0;
417 static void manager_clear_jobs_and_units(Manager
*m
) {
423 while ((j
= hashmap_first(m
->transaction_jobs
)))
426 while ((u
= hashmap_first(m
->units
)))
429 manager_dispatch_cleanup_queue(m
);
431 assert(!m
->load_queue
);
432 assert(!m
->run_queue
);
433 assert(!m
->dbus_unit_queue
);
434 assert(!m
->dbus_job_queue
);
435 assert(!m
->cleanup_queue
);
436 assert(!m
->gc_queue
);
438 assert(hashmap_isempty(m
->transaction_jobs
));
439 assert(hashmap_isempty(m
->jobs
));
440 assert(hashmap_isempty(m
->units
));
443 void manager_free(Manager
*m
) {
448 manager_clear_jobs_and_units(m
);
450 for (c
= 0; c
< _UNIT_TYPE_MAX
; c
++)
451 if (unit_vtable
[c
]->shutdown
)
452 unit_vtable
[c
]->shutdown(m
);
454 /* If we reexecute ourselves, we keep the root cgroup
456 manager_shutdown_cgroup(m
, m
->exit_code
!= MANAGER_REEXECUTE
);
458 manager_undo_generators(m
);
462 hashmap_free(m
->units
);
463 hashmap_free(m
->jobs
);
464 hashmap_free(m
->transaction_jobs
);
465 hashmap_free(m
->watch_pids
);
466 hashmap_free(m
->watch_bus
);
468 if (m
->epoll_fd
>= 0)
469 close_nointr_nofail(m
->epoll_fd
);
470 if (m
->signal_watch
.fd
>= 0)
471 close_nointr_nofail(m
->signal_watch
.fd
);
472 if (m
->notify_watch
.fd
>= 0)
473 close_nointr_nofail(m
->notify_watch
.fd
);
476 if (m
->audit_fd
>= 0)
477 audit_close(m
->audit_fd
);
480 free(m
->notify_socket
);
482 lookup_paths_free(&m
->lookup_paths
);
483 strv_free(m
->environment
);
485 strv_free(m
->default_controllers
);
487 hashmap_free(m
->cgroup_bondings
);
488 set_free_free(m
->unit_path_cache
);
493 int manager_enumerate(Manager
*m
) {
499 /* Let's ask every type to load all units from disk/kernel
500 * that it might know */
501 for (c
= 0; c
< _UNIT_TYPE_MAX
; c
++)
502 if (unit_vtable
[c
]->enumerate
)
503 if ((q
= unit_vtable
[c
]->enumerate(m
)) < 0)
506 manager_dispatch_load_queue(m
);
510 int manager_coldplug(Manager
*m
) {
518 /* Then, let's set up their initial state. */
519 HASHMAP_FOREACH_KEY(u
, k
, m
->units
, i
) {
525 if ((q
= unit_coldplug(u
)) < 0)
532 static void manager_build_unit_path_cache(Manager
*m
) {
539 set_free_free(m
->unit_path_cache
);
541 if (!(m
->unit_path_cache
= set_new(string_hash_func
, string_compare_func
))) {
542 log_error("Failed to allocate unit path cache.");
546 /* This simply builds a list of files we know exist, so that
547 * we don't always have to go to disk */
549 STRV_FOREACH(i
, m
->lookup_paths
.unit_path
) {
552 if (!(d
= opendir(*i
))) {
553 log_error("Failed to open directory: %m");
557 while ((de
= readdir(d
))) {
560 if (ignore_file(de
->d_name
))
563 if (asprintf(&p
, "%s/%s", streq(*i
, "/") ? "" : *i
, de
->d_name
) < 0) {
568 if ((r
= set_put(m
->unit_path_cache
, p
)) < 0) {
581 log_error("Failed to build unit path cache: %s", strerror(-r
));
583 set_free_free(m
->unit_path_cache
);
584 m
->unit_path_cache
= NULL
;
590 int manager_startup(Manager
*m
, FILE *serialization
, FDSet
*fds
) {
595 manager_run_generators(m
);
597 manager_build_unit_path_cache(m
);
599 /* If we will deserialize make sure that during enumeration
600 * this is already known, so we increase the counter here
605 /* First, enumerate what we can from all config files */
606 r
= manager_enumerate(m
);
608 /* Second, deserialize if there is something to deserialize */
610 if ((q
= manager_deserialize(m
, serialization
, fds
)) < 0)
613 /* Third, fire things up! */
614 if ((q
= manager_coldplug(m
)) < 0)
618 assert(m
->n_reloading
> 0);
625 static void transaction_delete_job(Manager
*m
, Job
*j
, bool delete_dependencies
) {
629 /* Deletes one job from the transaction */
631 manager_transaction_unlink_job(m
, j
, delete_dependencies
);
637 static void transaction_delete_unit(Manager
*m
, Unit
*u
) {
640 /* Deletes all jobs associated with a certain unit from the
643 while ((j
= hashmap_get(m
->transaction_jobs
, u
)))
644 transaction_delete_job(m
, j
, true);
647 static void transaction_clean_dependencies(Manager
*m
) {
653 /* Drops all dependencies of all installed jobs */
655 HASHMAP_FOREACH(j
, m
->jobs
, i
) {
656 while (j
->subject_list
)
657 job_dependency_free(j
->subject_list
);
658 while (j
->object_list
)
659 job_dependency_free(j
->object_list
);
662 assert(!m
->transaction_anchor
);
665 static void transaction_abort(Manager
*m
) {
670 while ((j
= hashmap_first(m
->transaction_jobs
)))
672 transaction_delete_job(m
, j
, true);
676 assert(hashmap_isempty(m
->transaction_jobs
));
678 transaction_clean_dependencies(m
);
681 static void transaction_find_jobs_that_matter_to_anchor(Manager
*m
, Job
*j
, unsigned generation
) {
686 /* A recursive sweep through the graph that marks all units
687 * that matter to the anchor job, i.e. are directly or
688 * indirectly a dependency of the anchor job via paths that
689 * are fully marked as mattering. */
694 l
= m
->transaction_anchor
;
696 LIST_FOREACH(subject
, l
, l
) {
698 /* This link does not matter */
702 /* This unit has already been marked */
703 if (l
->object
->generation
== generation
)
706 l
->object
->matters_to_anchor
= true;
707 l
->object
->generation
= generation
;
709 transaction_find_jobs_that_matter_to_anchor(m
, l
->object
, generation
);
713 static void transaction_merge_and_delete_job(Manager
*m
, Job
*j
, Job
*other
, JobType t
) {
714 JobDependency
*l
, *last
;
718 assert(j
->unit
== other
->unit
);
719 assert(!j
->installed
);
721 /* Merges 'other' into 'j' and then deletes j. */
724 j
->state
= JOB_WAITING
;
725 j
->override
= j
->override
|| other
->override
;
727 j
->matters_to_anchor
= j
->matters_to_anchor
|| other
->matters_to_anchor
;
729 /* Patch us in as new owner of the JobDependency objects */
731 LIST_FOREACH(subject
, l
, other
->subject_list
) {
732 assert(l
->subject
== other
);
737 /* Merge both lists */
739 last
->subject_next
= j
->subject_list
;
741 j
->subject_list
->subject_prev
= last
;
742 j
->subject_list
= other
->subject_list
;
745 /* Patch us in as new owner of the JobDependency objects */
747 LIST_FOREACH(object
, l
, other
->object_list
) {
748 assert(l
->object
== other
);
753 /* Merge both lists */
755 last
->object_next
= j
->object_list
;
757 j
->object_list
->object_prev
= last
;
758 j
->object_list
= other
->object_list
;
761 /* Kill the other job */
762 other
->subject_list
= NULL
;
763 other
->object_list
= NULL
;
764 transaction_delete_job(m
, other
, true);
766 static bool job_is_conflicted_by(Job
*j
) {
771 /* Returns true if this job is pulled in by a least one
772 * ConflictedBy dependency. */
774 LIST_FOREACH(object
, l
, j
->object_list
)
781 static int delete_one_unmergeable_job(Manager
*m
, Job
*j
) {
786 /* Tries to delete one item in the linked list
787 * j->transaction_next->transaction_next->... that conflicts
788 * with another one, in an attempt to make an inconsistent
789 * transaction work. */
791 /* We rely here on the fact that if a merged with b does not
792 * merge with c, either a or b merge with c neither */
793 LIST_FOREACH(transaction
, j
, j
)
794 LIST_FOREACH(transaction
, k
, j
->transaction_next
) {
797 /* Is this one mergeable? Then skip it */
798 if (job_type_is_mergeable(j
->type
, k
->type
))
801 /* Ok, we found two that conflict, let's see if we can
802 * drop one of them */
803 if (!j
->matters_to_anchor
&& !k
->matters_to_anchor
) {
805 /* Both jobs don't matter, so let's
806 * find the one that is smarter to
807 * remove. Let's think positive and
808 * rather remove stops then starts --
809 * except if something is being
810 * stopped because it is conflicted by
811 * another unit in which case we
812 * rather remove the start. */
814 log_debug("Looking at job %s/%s conflicted_by=%s", j
->unit
->meta
.id
, job_type_to_string(j
->type
), yes_no(j
->type
== JOB_STOP
&& job_is_conflicted_by(j
)));
815 log_debug("Looking at job %s/%s conflicted_by=%s", k
->unit
->meta
.id
, job_type_to_string(k
->type
), yes_no(k
->type
== JOB_STOP
&& job_is_conflicted_by(k
)));
817 if (j
->type
== JOB_STOP
) {
819 if (job_is_conflicted_by(j
))
824 } else if (k
->type
== JOB_STOP
) {
826 if (job_is_conflicted_by(k
))
833 } else if (!j
->matters_to_anchor
)
835 else if (!k
->matters_to_anchor
)
840 /* Ok, we can drop one, so let's do so. */
841 log_debug("Fixing conflicting jobs by deleting job %s/%s", d
->unit
->meta
.id
, job_type_to_string(d
->type
));
842 transaction_delete_job(m
, d
, true);
849 static int transaction_merge_jobs(Manager
*m
, DBusError
*e
) {
856 /* First step, check whether any of the jobs for one specific
857 * task conflict. If so, try to drop one of them. */
858 HASHMAP_FOREACH(j
, m
->transaction_jobs
, i
) {
863 LIST_FOREACH(transaction
, k
, j
->transaction_next
) {
864 if (job_type_merge(&t
, k
->type
) >= 0)
867 /* OK, we could not merge all jobs for this
868 * action. Let's see if we can get rid of one
871 if ((r
= delete_one_unmergeable_job(m
, j
)) >= 0)
872 /* Ok, we managed to drop one, now
873 * let's ask our callers to call us
874 * again after garbage collecting */
877 /* We couldn't merge anything. Failure */
878 dbus_set_error(e
, BUS_ERROR_TRANSACTION_JOBS_CONFLICTING
, "Transaction contains conflicting jobs '%s' and '%s' for %s. Probably contradicting requirement dependencies configured.",
879 job_type_to_string(t
), job_type_to_string(k
->type
), k
->unit
->meta
.id
);
884 /* Second step, merge the jobs. */
885 HASHMAP_FOREACH(j
, m
->transaction_jobs
, i
) {
889 /* Merge all transactions */
890 LIST_FOREACH(transaction
, k
, j
->transaction_next
)
891 assert_se(job_type_merge(&t
, k
->type
) == 0);
893 /* If an active job is mergeable, merge it too */
894 if (j
->unit
->meta
.job
)
895 job_type_merge(&t
, j
->unit
->meta
.job
->type
); /* Might fail. Which is OK */
897 while ((k
= j
->transaction_next
)) {
899 transaction_merge_and_delete_job(m
, k
, j
, t
);
902 transaction_merge_and_delete_job(m
, j
, k
, t
);
905 if (j
->unit
->meta
.job
&& !j
->installed
)
906 transaction_merge_and_delete_job(m
, j
, j
->unit
->meta
.job
, t
);
908 assert(!j
->transaction_next
);
909 assert(!j
->transaction_prev
);
915 static void transaction_drop_redundant(Manager
*m
) {
920 /* Goes through the transaction and removes all jobs that are
929 HASHMAP_FOREACH(j
, m
->transaction_jobs
, i
) {
930 bool changes_something
= false;
933 LIST_FOREACH(transaction
, k
, j
) {
935 if (!job_is_anchor(k
) &&
936 (k
->installed
|| job_type_is_redundant(k
->type
, unit_active_state(k
->unit
))) &&
937 (!k
->unit
->meta
.job
|| !job_type_is_conflicting(k
->type
, k
->unit
->meta
.job
->type
)))
940 changes_something
= true;
944 if (changes_something
)
947 /* log_debug("Found redundant job %s/%s, dropping.", j->unit->meta.id, job_type_to_string(j->type)); */
948 transaction_delete_job(m
, j
, false);
956 static bool unit_matters_to_anchor(Unit
*u
, Job
*j
) {
958 assert(!j
->transaction_prev
);
960 /* Checks whether at least one of the jobs for this unit
961 * matters to the anchor. */
963 LIST_FOREACH(transaction
, j
, j
)
964 if (j
->matters_to_anchor
)
970 static int transaction_verify_order_one(Manager
*m
, Job
*j
, Job
*from
, unsigned generation
, DBusError
*e
) {
977 assert(!j
->transaction_prev
);
979 /* Does a recursive sweep through the ordering graph, looking
980 * for a cycle. If we find cycle we try to break it. */
982 /* Have we seen this before? */
983 if (j
->generation
== generation
) {
986 /* If the marker is NULL we have been here already and
987 * decided the job was loop-free from here. Hence
988 * shortcut things and return right-away. */
992 /* So, the marker is not NULL and we already have been
993 * here. We have a cycle. Let's try to break it. We go
994 * backwards in our path and try to find a suitable
995 * job to remove. We use the marker to find our way
996 * back, since smart how we are we stored our way back
998 log_warning("Found ordering cycle on %s/%s", j
->unit
->meta
.id
, job_type_to_string(j
->type
));
1001 for (k
= from
; k
; k
= ((k
->generation
== generation
&& k
->marker
!= k
) ? k
->marker
: NULL
)) {
1003 log_info("Walked on cycle path to %s/%s", k
->unit
->meta
.id
, job_type_to_string(k
->type
));
1007 !unit_matters_to_anchor(k
->unit
, k
)) {
1008 /* Ok, we can drop this one, so let's
1013 /* Check if this in fact was the beginning of
1021 log_warning("Breaking ordering cycle by deleting job %s/%s", delete->unit
->meta
.id
, job_type_to_string(delete->type
));
1022 transaction_delete_unit(m
, delete->unit
);
1026 log_error("Unable to break cycle");
1028 dbus_set_error(e
, BUS_ERROR_TRANSACTION_ORDER_IS_CYCLIC
, "Transaction order is cyclic. See system logs for details.");
1032 /* Make the marker point to where we come from, so that we can
1033 * find our way backwards if we want to break a cycle. We use
1034 * a special marker for the beginning: we point to
1036 j
->marker
= from
? from
: j
;
1037 j
->generation
= generation
;
1039 /* We assume that the the dependencies are bidirectional, and
1040 * hence can ignore UNIT_AFTER */
1041 SET_FOREACH(u
, j
->unit
->meta
.dependencies
[UNIT_BEFORE
], i
) {
1044 /* Is there a job for this unit? */
1045 if (!(o
= hashmap_get(m
->transaction_jobs
, u
)))
1047 /* Ok, there is no job for this in the
1048 * transaction, but maybe there is already one
1050 if (!(o
= u
->meta
.job
))
1053 if ((r
= transaction_verify_order_one(m
, o
, j
, generation
, e
)) < 0)
1057 /* Ok, let's backtrack, and remember that this entry is not on
1058 * our path anymore. */
1064 static int transaction_verify_order(Manager
*m
, unsigned *generation
, DBusError
*e
) {
1073 /* Check if the ordering graph is cyclic. If it is, try to fix
1074 * that up by dropping one of the jobs. */
1076 g
= (*generation
)++;
1078 HASHMAP_FOREACH(j
, m
->transaction_jobs
, i
)
1079 if ((r
= transaction_verify_order_one(m
, j
, NULL
, g
, e
)) < 0)
1085 static void transaction_collect_garbage(Manager
*m
) {
1090 /* Drop jobs that are not required by any other job */
1098 HASHMAP_FOREACH(j
, m
->transaction_jobs
, i
) {
1099 if (j
->object_list
) {
1100 /* log_debug("Keeping job %s/%s because of %s/%s", */
1101 /* j->unit->meta.id, job_type_to_string(j->type), */
1102 /* j->object_list->subject ? j->object_list->subject->unit->meta.id : "root", */
1103 /* j->object_list->subject ? job_type_to_string(j->object_list->subject->type) : "root"); */
1107 /* log_debug("Garbage collecting job %s/%s", j->unit->meta.id, job_type_to_string(j->type)); */
1108 transaction_delete_job(m
, j
, true);
1116 static int transaction_is_destructive(Manager
*m
, DBusError
*e
) {
1122 /* Checks whether applying this transaction means that
1123 * existing jobs would be replaced */
1125 HASHMAP_FOREACH(j
, m
->transaction_jobs
, i
) {
1128 assert(!j
->transaction_prev
);
1129 assert(!j
->transaction_next
);
1131 if (j
->unit
->meta
.job
&&
1132 j
->unit
->meta
.job
!= j
&&
1133 !job_type_is_superset(j
->type
, j
->unit
->meta
.job
->type
)) {
1135 dbus_set_error(e
, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE
, "Transaction is destructive.");
1143 static void transaction_minimize_impact(Manager
*m
) {
1147 /* Drops all unnecessary jobs that reverse already active jobs
1148 * or that stop a running service. */
1156 HASHMAP_FOREACH(j
, m
->transaction_jobs
, i
) {
1157 LIST_FOREACH(transaction
, j
, j
) {
1158 bool stops_running_service
, changes_existing_job
;
1160 /* If it matters, we shouldn't drop it */
1161 if (j
->matters_to_anchor
)
1164 /* Would this stop a running service?
1165 * Would this change an existing job?
1166 * If so, let's drop this entry */
1168 stops_running_service
=
1169 j
->type
== JOB_STOP
&& UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(j
->unit
));
1171 changes_existing_job
=
1172 j
->unit
->meta
.job
&&
1173 job_type_is_conflicting(j
->type
, j
->unit
->meta
.job
->type
);
1175 if (!stops_running_service
&& !changes_existing_job
)
1178 if (stops_running_service
)
1179 log_debug("%s/%s would stop a running service.", j
->unit
->meta
.id
, job_type_to_string(j
->type
));
1181 if (changes_existing_job
)
1182 log_debug("%s/%s would change existing job.", j
->unit
->meta
.id
, job_type_to_string(j
->type
));
1184 /* Ok, let's get rid of this */
1185 log_debug("Deleting %s/%s to minimize impact.", j
->unit
->meta
.id
, job_type_to_string(j
->type
));
1187 transaction_delete_job(m
, j
, true);
1199 static int transaction_apply(Manager
*m
, JobMode mode
) {
1204 /* Moves the transaction jobs to the set of active jobs */
1206 if (mode
== JOB_ISOLATE
) {
1208 /* When isolating first kill all installed jobs which
1209 * aren't part of the new transaction */
1210 HASHMAP_FOREACH(j
, m
->jobs
, i
) {
1211 assert(j
->installed
);
1213 if (hashmap_get(m
->transaction_jobs
, j
->unit
))
1216 job_finish_and_invalidate(j
, JOB_CANCELED
);
1220 HASHMAP_FOREACH(j
, m
->transaction_jobs
, i
) {
1222 assert(!j
->transaction_prev
);
1223 assert(!j
->transaction_next
);
1228 if ((r
= hashmap_put(m
->jobs
, UINT32_TO_PTR(j
->id
), j
)) < 0)
1232 while ((j
= hashmap_steal_first(m
->transaction_jobs
))) {
1234 /* log_debug("Skipping already installed job %s/%s as %u", j->unit->meta.id, job_type_to_string(j->type), (unsigned) j->id); */
1238 if (j
->unit
->meta
.job
)
1239 job_free(j
->unit
->meta
.job
);
1241 j
->unit
->meta
.job
= j
;
1242 j
->installed
= true;
1243 m
->n_installed_jobs
++;
1245 /* We're fully installed. Now let's free data we don't
1248 assert(!j
->transaction_next
);
1249 assert(!j
->transaction_prev
);
1251 job_add_to_run_queue(j
);
1252 job_add_to_dbus_queue(j
);
1255 log_debug("Installed new job %s/%s as %u", j
->unit
->meta
.id
, job_type_to_string(j
->type
), (unsigned) j
->id
);
1258 /* As last step, kill all remaining job dependencies. */
1259 transaction_clean_dependencies(m
);
1265 HASHMAP_FOREACH(j
, m
->transaction_jobs
, i
) {
1269 hashmap_remove(m
->jobs
, UINT32_TO_PTR(j
->id
));
1275 static int transaction_activate(Manager
*m
, JobMode mode
, DBusError
*e
) {
1277 unsigned generation
= 1;
1281 /* This applies the changes recorded in transaction_jobs to
1282 * the actual list of jobs, if possible. */
1284 /* First step: figure out which jobs matter */
1285 transaction_find_jobs_that_matter_to_anchor(m
, NULL
, generation
++);
1287 /* Second step: Try not to stop any running services if
1288 * we don't have to. Don't try to reverse running
1289 * jobs if we don't have to. */
1290 if (mode
== JOB_FAIL
)
1291 transaction_minimize_impact(m
);
1293 /* Third step: Drop redundant jobs */
1294 transaction_drop_redundant(m
);
1297 /* Fourth step: Let's remove unneeded jobs that might
1299 if (mode
!= JOB_ISOLATE
)
1300 transaction_collect_garbage(m
);
1302 /* Fifth step: verify order makes sense and correct
1303 * cycles if necessary and possible */
1304 if ((r
= transaction_verify_order(m
, &generation
, e
)) >= 0)
1308 log_warning("Requested transaction contains an unfixable cyclic ordering dependency: %s", bus_error(e
, r
));
1312 /* Let's see if the resulting transaction ordering
1313 * graph is still cyclic... */
1317 /* Sixth step: let's drop unmergeable entries if
1318 * necessary and possible, merge entries we can
1320 if ((r
= transaction_merge_jobs(m
, e
)) >= 0)
1324 log_warning("Requested transaction contains unmergeable jobs: %s", bus_error(e
, r
));
1328 /* Seventh step: an entry got dropped, let's garbage
1329 * collect its dependencies. */
1330 if (mode
!= JOB_ISOLATE
)
1331 transaction_collect_garbage(m
);
1333 /* Let's see if the resulting transaction still has
1334 * unmergeable entries ... */
1337 /* Eights step: Drop redundant jobs again, if the merging now allows us to drop more. */
1338 transaction_drop_redundant(m
);
1340 /* Ninth step: check whether we can actually apply this */
1341 if (mode
== JOB_FAIL
)
1342 if ((r
= transaction_is_destructive(m
, e
)) < 0) {
1343 log_notice("Requested transaction contradicts existing jobs: %s", bus_error(e
, r
));
1347 /* Tenth step: apply changes */
1348 if ((r
= transaction_apply(m
, mode
)) < 0) {
1349 log_warning("Failed to apply transaction: %s", strerror(-r
));
1353 assert(hashmap_isempty(m
->transaction_jobs
));
1354 assert(!m
->transaction_anchor
);
1359 transaction_abort(m
);
1363 static Job
* transaction_add_one_job(Manager
*m
, JobType type
, Unit
*unit
, bool override
, bool *is_new
) {
1369 /* Looks for an existing prospective job and returns that. If
1370 * it doesn't exist it is created and added to the prospective
1373 f
= hashmap_get(m
->transaction_jobs
, unit
);
1375 LIST_FOREACH(transaction
, j
, f
) {
1376 assert(j
->unit
== unit
);
1378 if (j
->type
== type
) {
1385 if (unit
->meta
.job
&& unit
->meta
.job
->type
== type
)
1387 else if (!(j
= job_new(m
, type
, unit
)))
1392 j
->matters_to_anchor
= false;
1393 j
->override
= override
;
1395 LIST_PREPEND(Job
, transaction
, f
, j
);
1397 if (hashmap_replace(m
->transaction_jobs
, unit
, f
) < 0) {
1405 /* log_debug("Added job %s/%s to transaction.", unit->meta.id, job_type_to_string(type)); */
1410 void manager_transaction_unlink_job(Manager
*m
, Job
*j
, bool delete_dependencies
) {
1414 if (j
->transaction_prev
)
1415 j
->transaction_prev
->transaction_next
= j
->transaction_next
;
1416 else if (j
->transaction_next
)
1417 hashmap_replace(m
->transaction_jobs
, j
->unit
, j
->transaction_next
);
1419 hashmap_remove_value(m
->transaction_jobs
, j
->unit
, j
);
1421 if (j
->transaction_next
)
1422 j
->transaction_next
->transaction_prev
= j
->transaction_prev
;
1424 j
->transaction_prev
= j
->transaction_next
= NULL
;
1426 while (j
->subject_list
)
1427 job_dependency_free(j
->subject_list
);
1429 while (j
->object_list
) {
1430 Job
*other
= j
->object_list
->matters
? j
->object_list
->subject
: NULL
;
1432 job_dependency_free(j
->object_list
);
1434 if (other
&& delete_dependencies
) {
1435 log_debug("Deleting job %s/%s as dependency of job %s/%s",
1436 other
->unit
->meta
.id
, job_type_to_string(other
->type
),
1437 j
->unit
->meta
.id
, job_type_to_string(j
->type
));
1438 transaction_delete_job(m
, other
, delete_dependencies
);
1443 static int transaction_add_job_and_dependencies(
1451 bool ignore_requirements
,
1462 assert(type
< _JOB_TYPE_MAX
);
1465 /* log_debug("Pulling in %s/%s from %s/%s", */
1466 /* unit->meta.id, job_type_to_string(type), */
1467 /* by ? by->unit->meta.id : "NA", */
1468 /* by ? job_type_to_string(by->type) : "NA"); */
1470 if (unit
->meta
.load_state
!= UNIT_LOADED
&&
1471 unit
->meta
.load_state
!= UNIT_ERROR
&&
1472 unit
->meta
.load_state
!= UNIT_MASKED
) {
1473 dbus_set_error(e
, BUS_ERROR_LOAD_FAILED
, "Unit %s is not loaded properly.", unit
->meta
.id
);
1477 if (type
!= JOB_STOP
&& unit
->meta
.load_state
== UNIT_ERROR
) {
1478 dbus_set_error(e
, BUS_ERROR_LOAD_FAILED
,
1479 "Unit %s failed to load: %s. "
1480 "See system logs and 'systemctl status %s' for details.",
1482 strerror(-unit
->meta
.load_error
),
1487 if (type
!= JOB_STOP
&& unit
->meta
.load_state
== UNIT_MASKED
) {
1488 dbus_set_error(e
, BUS_ERROR_MASKED
, "Unit %s is masked.", unit
->meta
.id
);
1492 if (!unit_job_is_applicable(unit
, type
)) {
1493 dbus_set_error(e
, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE
, "Job type %s is not applicable for unit %s.", job_type_to_string(type
), unit
->meta
.id
);
1497 /* First add the job. */
1498 if (!(ret
= transaction_add_one_job(m
, type
, unit
, override
, &is_new
)))
1501 ret
->ignore_order
= ret
->ignore_order
|| ignore_order
;
1503 /* Then, add a link to the job. */
1504 if (!job_dependency_new(by
, ret
, matters
, conflicts
))
1507 if (is_new
&& !ignore_requirements
) {
1510 /* If we are following some other unit, make sure we
1511 * add all dependencies of everybody following. */
1512 if (unit_following_set(ret
->unit
, &following
) > 0) {
1513 SET_FOREACH(dep
, following
, i
)
1514 if ((r
= transaction_add_job_and_dependencies(m
, type
, dep
, ret
, false, override
, false, false, ignore_order
, e
, NULL
)) < 0) {
1515 log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep
->meta
.id
, bus_error(e
, r
));
1521 set_free(following
);
1524 /* Finally, recursively add in all dependencies. */
1525 if (type
== JOB_START
|| type
== JOB_RELOAD_OR_START
) {
1526 SET_FOREACH(dep
, ret
->unit
->meta
.dependencies
[UNIT_REQUIRES
], i
)
1527 if ((r
= transaction_add_job_and_dependencies(m
, JOB_START
, dep
, ret
, true, override
, false, false, ignore_order
, e
, NULL
)) < 0) {
1535 SET_FOREACH(dep
, ret
->unit
->meta
.dependencies
[UNIT_BIND_TO
], i
)
1536 if ((r
= transaction_add_job_and_dependencies(m
, JOB_START
, dep
, ret
, true, override
, false, false, ignore_order
, e
, NULL
)) < 0) {
1545 SET_FOREACH(dep
, ret
->unit
->meta
.dependencies
[UNIT_REQUIRES_OVERRIDABLE
], i
)
1546 if ((r
= transaction_add_job_and_dependencies(m
, JOB_START
, dep
, ret
, !override
, override
, false, false, ignore_order
, e
, NULL
)) < 0) {
1547 log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep
->meta
.id
, bus_error(e
, r
));
1553 SET_FOREACH(dep
, ret
->unit
->meta
.dependencies
[UNIT_WANTS
], i
)
1554 if ((r
= transaction_add_job_and_dependencies(m
, JOB_START
, dep
, ret
, false, false, false, false, ignore_order
, e
, NULL
)) < 0) {
1555 log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep
->meta
.id
, bus_error(e
, r
));
1561 SET_FOREACH(dep
, ret
->unit
->meta
.dependencies
[UNIT_REQUISITE
], i
)
1562 if ((r
= transaction_add_job_and_dependencies(m
, JOB_VERIFY_ACTIVE
, dep
, ret
, true, override
, false, false, ignore_order
, e
, NULL
)) < 0) {
1571 SET_FOREACH(dep
, ret
->unit
->meta
.dependencies
[UNIT_REQUISITE_OVERRIDABLE
], i
)
1572 if ((r
= transaction_add_job_and_dependencies(m
, JOB_VERIFY_ACTIVE
, dep
, ret
, !override
, override
, false, false, ignore_order
, e
, NULL
)) < 0) {
1573 log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep
->meta
.id
, bus_error(e
, r
));
1579 SET_FOREACH(dep
, ret
->unit
->meta
.dependencies
[UNIT_CONFLICTS
], i
)
1580 if ((r
= transaction_add_job_and_dependencies(m
, JOB_STOP
, dep
, ret
, true, override
, true, false, ignore_order
, e
, NULL
)) < 0) {
1589 SET_FOREACH(dep
, ret
->unit
->meta
.dependencies
[UNIT_CONFLICTED_BY
], i
)
1590 if ((r
= transaction_add_job_and_dependencies(m
, JOB_STOP
, dep
, ret
, false, override
, false, false, ignore_order
, e
, NULL
)) < 0) {
1591 log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep
->meta
.id
, bus_error(e
, r
));
1597 } else if (type
== JOB_STOP
|| type
== JOB_RESTART
|| type
== JOB_TRY_RESTART
) {
1599 SET_FOREACH(dep
, ret
->unit
->meta
.dependencies
[UNIT_REQUIRED_BY
], i
)
1600 if ((r
= transaction_add_job_and_dependencies(m
, type
, dep
, ret
, true, override
, false, false, ignore_order
, e
, NULL
)) < 0) {
1609 SET_FOREACH(dep
, ret
->unit
->meta
.dependencies
[UNIT_BOUND_BY
], i
)
1610 if ((r
= transaction_add_job_and_dependencies(m
, type
, dep
, ret
, true, override
, false, false, ignore_order
, e
, NULL
)) < 0) {
1620 /* JOB_VERIFY_STARTED, JOB_RELOAD require no dependency handling */
1632 static int transaction_add_isolate_jobs(Manager
*m
) {
1640 HASHMAP_FOREACH_KEY(u
, k
, m
->units
, i
) {
1642 /* ignore aliases */
1643 if (u
->meta
.id
!= k
)
1646 if (u
->meta
.ignore_on_isolate
)
1649 /* No need to stop inactive jobs */
1650 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(u
)) && !u
->meta
.job
)
1653 /* Is there already something listed for this? */
1654 if (hashmap_get(m
->transaction_jobs
, u
))
1657 if ((r
= transaction_add_job_and_dependencies(m
, JOB_STOP
, u
, NULL
, true, false, false, false, false, NULL
, NULL
)) < 0)
1658 log_warning("Cannot add isolate job for unit %s, ignoring: %s", u
->meta
.id
, strerror(-r
));
1664 int manager_add_job(Manager
*m
, JobType type
, Unit
*unit
, JobMode mode
, bool override
, DBusError
*e
, Job
**_ret
) {
1669 assert(type
< _JOB_TYPE_MAX
);
1671 assert(mode
< _JOB_MODE_MAX
);
1673 if (mode
== JOB_ISOLATE
&& type
!= JOB_START
) {
1674 dbus_set_error(e
, BUS_ERROR_INVALID_JOB_MODE
, "Isolate is only valid for start.");
1678 if (mode
== JOB_ISOLATE
&& !unit
->meta
.allow_isolate
) {
1679 dbus_set_error(e
, BUS_ERROR_NO_ISOLATION
, "Operation refused, unit may not be isolated.");
1683 log_debug("Trying to enqueue job %s/%s/%s", unit
->meta
.id
, job_type_to_string(type
), job_mode_to_string(mode
));
1685 if ((r
= transaction_add_job_and_dependencies(m
, type
, unit
, NULL
, true, override
, false,
1686 mode
== JOB_IGNORE_DEPENDENCIES
|| mode
== JOB_IGNORE_REQUIREMENTS
,
1687 mode
== JOB_IGNORE_DEPENDENCIES
, e
, &ret
)) < 0) {
1688 transaction_abort(m
);
1692 if (mode
== JOB_ISOLATE
)
1693 if ((r
= transaction_add_isolate_jobs(m
)) < 0) {
1694 transaction_abort(m
);
1698 if ((r
= transaction_activate(m
, mode
, e
)) < 0)
1701 log_debug("Enqueued job %s/%s as %u", unit
->meta
.id
, job_type_to_string(type
), (unsigned) ret
->id
);
1709 int manager_add_job_by_name(Manager
*m
, JobType type
, const char *name
, JobMode mode
, bool override
, DBusError
*e
, Job
**_ret
) {
1714 assert(type
< _JOB_TYPE_MAX
);
1716 assert(mode
< _JOB_MODE_MAX
);
1718 if ((r
= manager_load_unit(m
, name
, NULL
, NULL
, &unit
)) < 0)
1721 return manager_add_job(m
, type
, unit
, mode
, override
, e
, _ret
);
1724 Job
*manager_get_job(Manager
*m
, uint32_t id
) {
1727 return hashmap_get(m
->jobs
, UINT32_TO_PTR(id
));
1730 Unit
*manager_get_unit(Manager
*m
, const char *name
) {
1734 return hashmap_get(m
->units
, name
);
1737 unsigned manager_dispatch_load_queue(Manager
*m
) {
1743 /* Make sure we are not run recursively */
1744 if (m
->dispatching_load_queue
)
1747 m
->dispatching_load_queue
= true;
1749 /* Dispatches the load queue. Takes a unit from the queue and
1750 * tries to load its data until the queue is empty */
1752 while ((meta
= m
->load_queue
)) {
1753 assert(meta
->in_load_queue
);
1755 unit_load((Unit
*) meta
);
1759 m
->dispatching_load_queue
= false;
1763 int manager_load_unit_prepare(Manager
*m
, const char *name
, const char *path
, DBusError
*e
, Unit
**_ret
) {
1768 assert(name
|| path
);
1770 /* This will prepare the unit for loading, but not actually
1771 * load anything from disk. */
1773 if (path
&& !is_path(path
)) {
1774 dbus_set_error(e
, BUS_ERROR_INVALID_PATH
, "Path %s is not absolute.", path
);
1779 name
= file_name_from_path(path
);
1781 if (!unit_name_is_valid(name
, false)) {
1782 dbus_set_error(e
, BUS_ERROR_INVALID_NAME
, "Unit name %s is not valid.", name
);
1786 if ((ret
= manager_get_unit(m
, name
))) {
1791 if (!(ret
= unit_new(m
)))
1795 if (!(ret
->meta
.fragment_path
= strdup(path
))) {
1800 if ((r
= unit_add_name(ret
, name
)) < 0) {
1805 unit_add_to_load_queue(ret
);
1806 unit_add_to_dbus_queue(ret
);
1807 unit_add_to_gc_queue(ret
);
1815 int manager_load_unit(Manager
*m
, const char *name
, const char *path
, DBusError
*e
, Unit
**_ret
) {
1820 /* This will load the service information files, but not actually
1821 * start any services or anything. */
1823 if ((r
= manager_load_unit_prepare(m
, name
, path
, e
, _ret
)) != 0)
1826 manager_dispatch_load_queue(m
);
1829 *_ret
= unit_follow_merge(*_ret
);
1834 void manager_dump_jobs(Manager
*s
, FILE *f
, const char *prefix
) {
1841 HASHMAP_FOREACH(j
, s
->jobs
, i
)
1842 job_dump(j
, f
, prefix
);
1845 void manager_dump_units(Manager
*s
, FILE *f
, const char *prefix
) {
1853 HASHMAP_FOREACH_KEY(u
, t
, s
->units
, i
)
1854 if (u
->meta
.id
== t
)
1855 unit_dump(u
, f
, prefix
);
1858 void manager_clear_jobs(Manager
*m
) {
1863 transaction_abort(m
);
1865 while ((j
= hashmap_first(m
->jobs
)))
1866 job_finish_and_invalidate(j
, JOB_CANCELED
);
1869 unsigned manager_dispatch_run_queue(Manager
*m
) {
1873 if (m
->dispatching_run_queue
)
1876 m
->dispatching_run_queue
= true;
1878 while ((j
= m
->run_queue
)) {
1879 assert(j
->installed
);
1880 assert(j
->in_run_queue
);
1882 job_run_and_invalidate(j
);
1886 m
->dispatching_run_queue
= false;
1890 unsigned manager_dispatch_dbus_queue(Manager
*m
) {
1897 if (m
->dispatching_dbus_queue
)
1900 m
->dispatching_dbus_queue
= true;
1902 while ((meta
= m
->dbus_unit_queue
)) {
1903 assert(meta
->in_dbus_queue
);
1905 bus_unit_send_change_signal((Unit
*) meta
);
1909 while ((j
= m
->dbus_job_queue
)) {
1910 assert(j
->in_dbus_queue
);
1912 bus_job_send_change_signal(j
);
1916 m
->dispatching_dbus_queue
= false;
1920 static int manager_process_notify_fd(Manager
*m
) {
1927 struct msghdr msghdr
;
1929 struct ucred
*ucred
;
1931 struct cmsghdr cmsghdr
;
1932 uint8_t buf
[CMSG_SPACE(sizeof(struct ucred
))];
1938 iovec
.iov_base
= buf
;
1939 iovec
.iov_len
= sizeof(buf
)-1;
1943 msghdr
.msg_iov
= &iovec
;
1944 msghdr
.msg_iovlen
= 1;
1945 msghdr
.msg_control
= &control
;
1946 msghdr
.msg_controllen
= sizeof(control
);
1948 if ((n
= recvmsg(m
->notify_watch
.fd
, &msghdr
, MSG_DONTWAIT
)) <= 0) {
1952 if (errno
== EAGAIN
|| errno
== EINTR
)
1958 if (msghdr
.msg_controllen
< CMSG_LEN(sizeof(struct ucred
)) ||
1959 control
.cmsghdr
.cmsg_level
!= SOL_SOCKET
||
1960 control
.cmsghdr
.cmsg_type
!= SCM_CREDENTIALS
||
1961 control
.cmsghdr
.cmsg_len
!= CMSG_LEN(sizeof(struct ucred
))) {
1962 log_warning("Received notify message without credentials. Ignoring.");
1966 ucred
= (struct ucred
*) CMSG_DATA(&control
.cmsghdr
);
1968 if (!(u
= hashmap_get(m
->watch_pids
, LONG_TO_PTR(ucred
->pid
))))
1969 if (!(u
= cgroup_unit_by_pid(m
, ucred
->pid
))) {
1970 log_warning("Cannot find unit for notify message of PID %lu.", (unsigned long) ucred
->pid
);
1974 assert((size_t) n
< sizeof(buf
));
1976 if (!(tags
= strv_split(buf
, "\n\r")))
1979 log_debug("Got notification message for unit %s", u
->meta
.id
);
1981 if (UNIT_VTABLE(u
)->notify_message
)
1982 UNIT_VTABLE(u
)->notify_message(u
, ucred
->pid
, tags
);
1990 static int manager_dispatch_sigchld(Manager
*m
) {
2000 /* First we call waitd() for a PID and do not reap the
2001 * zombie. That way we can still access /proc/$PID for
2002 * it while it is a zombie. */
2003 if (waitid(P_ALL
, 0, &si
, WEXITED
|WNOHANG
|WNOWAIT
) < 0) {
2005 if (errno
== ECHILD
)
2017 if (si
.si_code
== CLD_EXITED
|| si
.si_code
== CLD_KILLED
|| si
.si_code
== CLD_DUMPED
) {
2020 get_process_name(si
.si_pid
, &name
);
2021 log_debug("Got SIGCHLD for process %lu (%s)", (unsigned long) si
.si_pid
, strna(name
));
2025 /* Let's flush any message the dying child might still
2026 * have queued for us. This ensures that the process
2027 * still exists in /proc so that we can figure out
2028 * which cgroup and hence unit it belongs to. */
2029 if ((r
= manager_process_notify_fd(m
)) < 0)
2032 /* And now figure out the unit this belongs to */
2033 if (!(u
= hashmap_get(m
->watch_pids
, LONG_TO_PTR(si
.si_pid
))))
2034 u
= cgroup_unit_by_pid(m
, si
.si_pid
);
2036 /* And now, we actually reap the zombie. */
2037 if (waitid(P_PID
, si
.si_pid
, &si
, WEXITED
) < 0) {
2044 if (si
.si_code
!= CLD_EXITED
&& si
.si_code
!= CLD_KILLED
&& si
.si_code
!= CLD_DUMPED
)
2047 log_debug("Child %lu died (code=%s, status=%i/%s)",
2048 (long unsigned) si
.si_pid
,
2049 sigchld_code_to_string(si
.si_code
),
2051 strna(si
.si_code
== CLD_EXITED
2052 ? exit_status_to_string(si
.si_status
, EXIT_STATUS_FULL
)
2053 : signal_to_string(si
.si_status
)));
2058 log_debug("Child %lu belongs to %s", (long unsigned) si
.si_pid
, u
->meta
.id
);
2060 hashmap_remove(m
->watch_pids
, LONG_TO_PTR(si
.si_pid
));
2061 UNIT_VTABLE(u
)->sigchld_event(u
, si
.si_pid
, si
.si_code
, si
.si_status
);
2067 static int manager_start_target(Manager
*m
, const char *name
, JobMode mode
) {
2071 dbus_error_init(&error
);
2073 log_debug("Activating special unit %s", name
);
2075 if ((r
= manager_add_job_by_name(m
, JOB_START
, name
, mode
, true, &error
, NULL
)) < 0)
2076 log_error("Failed to enqueue %s job: %s", name
, bus_error(&error
, r
));
2078 dbus_error_free(&error
);
2083 static int manager_process_signal_fd(Manager
*m
) {
2085 struct signalfd_siginfo sfsi
;
2086 bool sigchld
= false;
2091 if ((n
= read(m
->signal_watch
.fd
, &sfsi
, sizeof(sfsi
))) != sizeof(sfsi
)) {
2096 if (errno
== EINTR
|| errno
== EAGAIN
)
2102 if (sfsi
.ssi_pid
> 0) {
2105 get_process_name(sfsi
.ssi_pid
, &p
);
2107 log_debug("Received SIG%s from PID %lu (%s).",
2108 strna(signal_to_string(sfsi
.ssi_signo
)),
2109 (unsigned long) sfsi
.ssi_pid
, strna(p
));
2112 log_debug("Received SIG%s.", strna(signal_to_string(sfsi
.ssi_signo
)));
2114 switch (sfsi
.ssi_signo
) {
2121 if (m
->running_as
== MANAGER_SYSTEM
) {
2122 /* This is for compatibility with the
2123 * original sysvinit */
2124 m
->exit_code
= MANAGER_REEXECUTE
;
2131 if (m
->running_as
== MANAGER_SYSTEM
) {
2132 manager_start_target(m
, SPECIAL_CTRL_ALT_DEL_TARGET
, JOB_REPLACE
);
2136 /* Run the exit target if there is one, if not, just exit. */
2137 if (manager_start_target(m
, SPECIAL_EXIT_TARGET
, JOB_REPLACE
) < 0) {
2138 m
->exit_code
= MANAGER_EXIT
;
2145 if (m
->running_as
== MANAGER_SYSTEM
)
2146 manager_start_target(m
, SPECIAL_KBREQUEST_TARGET
, JOB_REPLACE
);
2148 /* This is a nop on non-init */
2152 if (m
->running_as
== MANAGER_SYSTEM
)
2153 manager_start_target(m
, SPECIAL_SIGPWR_TARGET
, JOB_REPLACE
);
2155 /* This is a nop on non-init */
2161 u
= manager_get_unit(m
, SPECIAL_DBUS_SERVICE
);
2163 if (!u
|| UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u
))) {
2164 log_info("Trying to reconnect to bus...");
2168 if (!u
|| !UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u
))) {
2169 log_info("Loading D-Bus service...");
2170 manager_start_target(m
, SPECIAL_DBUS_SERVICE
, JOB_REPLACE
);
2181 if (!(f
= open_memstream(&dump
, &size
))) {
2182 log_warning("Failed to allocate memory stream.");
2186 manager_dump_units(m
, f
, "\t");
2187 manager_dump_jobs(m
, f
, "\t");
2192 log_warning("Failed to write status stream");
2197 log_dump(LOG_INFO
, dump
);
2204 m
->exit_code
= MANAGER_RELOAD
;
2209 /* Starting SIGRTMIN+0 */
2210 static const char * const target_table
[] = {
2211 [0] = SPECIAL_DEFAULT_TARGET
,
2212 [1] = SPECIAL_RESCUE_TARGET
,
2213 [2] = SPECIAL_EMERGENCY_TARGET
,
2214 [3] = SPECIAL_HALT_TARGET
,
2215 [4] = SPECIAL_POWEROFF_TARGET
,
2216 [5] = SPECIAL_REBOOT_TARGET
,
2217 [6] = SPECIAL_KEXEC_TARGET
2220 /* Starting SIGRTMIN+13, so that target halt and system halt are 10 apart */
2221 static const ManagerExitCode code_table
[] = {
2223 [1] = MANAGER_POWEROFF
,
2224 [2] = MANAGER_REBOOT
,
2228 if ((int) sfsi
.ssi_signo
>= SIGRTMIN
+0 &&
2229 (int) sfsi
.ssi_signo
< SIGRTMIN
+(int) ELEMENTSOF(target_table
)) {
2230 manager_start_target(m
, target_table
[sfsi
.ssi_signo
- SIGRTMIN
],
2231 (sfsi
.ssi_signo
== 1 || sfsi
.ssi_signo
== 2) ? JOB_ISOLATE
: JOB_REPLACE
);
2235 if ((int) sfsi
.ssi_signo
>= SIGRTMIN
+13 &&
2236 (int) sfsi
.ssi_signo
< SIGRTMIN
+13+(int) ELEMENTSOF(code_table
)) {
2237 m
->exit_code
= code_table
[sfsi
.ssi_signo
- SIGRTMIN
- 13];
2241 switch (sfsi
.ssi_signo
- SIGRTMIN
) {
2244 log_debug("Enabling showing of status.");
2245 m
->show_status
= true;
2249 log_debug("Disabling showing of status.");
2250 m
->show_status
= false;
2254 log_set_max_level(LOG_DEBUG
);
2255 log_notice("Setting log level to debug.");
2259 log_set_max_level(LOG_INFO
);
2260 log_notice("Setting log level to info.");
2264 log_set_target(LOG_TARGET_CONSOLE
);
2265 log_notice("Setting log target to console.");
2269 log_set_target(LOG_TARGET_KMSG
);
2270 log_notice("Setting log target to kmsg.");
2274 log_set_target(LOG_TARGET_SYSLOG_OR_KMSG
);
2275 log_notice("Setting log target to syslog-or-kmsg.");
2279 log_warning("Got unhandled signal <%s>.", strna(signal_to_string(sfsi
.ssi_signo
)));
2286 return manager_dispatch_sigchld(m
);
2291 static int process_event(Manager
*m
, struct epoll_event
*ev
) {
2298 assert_se(w
= ev
->data
.ptr
);
2300 if (w
->type
== WATCH_INVALID
)
2307 /* An incoming signal? */
2308 if (ev
->events
!= EPOLLIN
)
2311 if ((r
= manager_process_signal_fd(m
)) < 0)
2318 /* An incoming daemon notification event? */
2319 if (ev
->events
!= EPOLLIN
)
2322 if ((r
= manager_process_notify_fd(m
)) < 0)
2329 /* Some fd event, to be dispatched to the units */
2330 UNIT_VTABLE(w
->data
.unit
)->fd_event(w
->data
.unit
, w
->fd
, ev
->events
, w
);
2333 case WATCH_UNIT_TIMER
:
2334 case WATCH_JOB_TIMER
: {
2338 /* Some timer event, to be dispatched to the units */
2339 if ((k
= read(w
->fd
, &v
, sizeof(v
))) != sizeof(v
)) {
2341 if (k
< 0 && (errno
== EINTR
|| errno
== EAGAIN
))
2344 return k
< 0 ? -errno
: -EIO
;
2347 if (w
->type
== WATCH_UNIT_TIMER
)
2348 UNIT_VTABLE(w
->data
.unit
)->timer_event(w
->data
.unit
, v
, w
);
2350 job_timer_event(w
->data
.job
, v
, w
);
2355 /* Some mount table change, intended for the mount subsystem */
2356 mount_fd_event(m
, ev
->events
);
2360 /* Some swap table change, intended for the swap subsystem */
2361 swap_fd_event(m
, ev
->events
);
2365 /* Some notification from udev, intended for the device subsystem */
2366 device_fd_event(m
, ev
->events
);
2369 case WATCH_DBUS_WATCH
:
2370 bus_watch_event(m
, w
, ev
->events
);
2373 case WATCH_DBUS_TIMEOUT
:
2374 bus_timeout_event(m
, w
, ev
->events
);
2378 log_error("event type=%i", w
->type
);
2379 assert_not_reached("Unknown epoll event type.");
2385 int manager_loop(Manager
*m
) {
2388 RATELIMIT_DEFINE(rl
, 1*USEC_PER_SEC
, 50000);
2391 m
->exit_code
= MANAGER_RUNNING
;
2393 /* Release the path cache */
2394 set_free_free(m
->unit_path_cache
);
2395 m
->unit_path_cache
= NULL
;
2397 manager_check_finished(m
);
2399 /* There might still be some zombies hanging around from
2400 * before we were exec()'ed. Leat's reap them */
2401 if ((r
= manager_dispatch_sigchld(m
)) < 0)
2404 while (m
->exit_code
== MANAGER_RUNNING
) {
2405 struct epoll_event event
;
2408 if (!ratelimit_test(&rl
)) {
2409 /* Yay, something is going seriously wrong, pause a little */
2410 log_warning("Looping too fast. Throttling execution a little.");
2414 if (manager_dispatch_load_queue(m
) > 0)
2417 if (manager_dispatch_run_queue(m
) > 0)
2420 if (bus_dispatch(m
) > 0)
2423 if (manager_dispatch_cleanup_queue(m
) > 0)
2426 if (manager_dispatch_gc_queue(m
) > 0)
2429 if (manager_dispatch_dbus_queue(m
) > 0)
2432 if (swap_dispatch_reload(m
) > 0)
2435 if ((n
= epoll_wait(m
->epoll_fd
, &event
, 1, -1)) < 0) {
2445 if ((r
= process_event(m
, &event
)) < 0)
2449 return m
->exit_code
;
2452 int manager_get_unit_from_dbus_path(Manager
*m
, const char *s
, Unit
**_u
) {
2460 if (!startswith(s
, "/org/freedesktop/systemd1/unit/"))
2463 if (!(n
= bus_path_unescape(s
+31)))
2466 u
= manager_get_unit(m
, n
);
2477 int manager_get_job_from_dbus_path(Manager
*m
, const char *s
, Job
**_j
) {
2486 if (!startswith(s
, "/org/freedesktop/systemd1/job/"))
2489 if ((r
= safe_atou(s
+ 30, &id
)) < 0)
2492 if (!(j
= manager_get_job(m
, id
)))
2500 void manager_send_unit_audit(Manager
*m
, Unit
*u
, int type
, bool success
) {
2505 if (m
->audit_fd
< 0)
2508 /* Don't generate audit events if the service was already
2509 * started and we're just deserializing */
2510 if (m
->n_reloading
> 0)
2513 if (m
->running_as
!= MANAGER_SYSTEM
)
2516 if (u
->meta
.type
!= UNIT_SERVICE
)
2519 if (!(p
= unit_name_to_prefix_and_instance(u
->meta
.id
))) {
2520 log_error("Failed to allocate unit name for audit message: %s", strerror(ENOMEM
));
2524 if (audit_log_user_comm_message(m
->audit_fd
, type
, "", p
, NULL
, NULL
, NULL
, success
) < 0) {
2525 log_warning("Failed to send audit message: %m");
2527 if (errno
== EPERM
) {
2528 /* We aren't allowed to send audit messages?
2529 * Then let's not retry again, to avoid
2530 * spamming the user with the same and same
2531 * messages over and over. */
2533 audit_close(m
->audit_fd
);
2543 void manager_send_unit_plymouth(Manager
*m
, Unit
*u
) {
2545 union sockaddr_union sa
;
2547 char *message
= NULL
;
2549 /* Don't generate plymouth events if the service was already
2550 * started and we're just deserializing */
2551 if (m
->n_reloading
> 0)
2554 if (m
->running_as
!= MANAGER_SYSTEM
)
2557 if (u
->meta
.type
!= UNIT_SERVICE
&&
2558 u
->meta
.type
!= UNIT_MOUNT
&&
2559 u
->meta
.type
!= UNIT_SWAP
)
2562 /* We set SOCK_NONBLOCK here so that we rather drop the
2563 * message then wait for plymouth */
2564 if ((fd
= socket(AF_UNIX
, SOCK_STREAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0)) < 0) {
2565 log_error("socket() failed: %m");
2570 sa
.sa
.sa_family
= AF_UNIX
;
2571 strncpy(sa
.un
.sun_path
+1, "/org/freedesktop/plymouthd", sizeof(sa
.un
.sun_path
)-1);
2572 if (connect(fd
, &sa
.sa
, offsetof(struct sockaddr_un
, sun_path
) + 1 + strlen(sa
.un
.sun_path
+1)) < 0) {
2574 if (errno
!= EPIPE
&&
2577 errno
!= ECONNREFUSED
&&
2578 errno
!= ECONNRESET
&&
2579 errno
!= ECONNABORTED
)
2580 log_error("connect() failed: %m");
2585 if (asprintf(&message
, "U\002%c%s%n", (int) (strlen(u
->meta
.id
) + 1), u
->meta
.id
, &n
) < 0) {
2586 log_error("Out of memory");
2591 if (write(fd
, message
, n
+ 1) != n
+ 1) {
2593 if (errno
!= EPIPE
&&
2596 errno
!= ECONNREFUSED
&&
2597 errno
!= ECONNRESET
&&
2598 errno
!= ECONNABORTED
)
2599 log_error("Failed to write Plymouth message: %m");
2606 close_nointr_nofail(fd
);
2611 void manager_dispatch_bus_name_owner_changed(
2614 const char* old_owner
,
2615 const char *new_owner
) {
2622 if (!(u
= hashmap_get(m
->watch_bus
, name
)))
2625 UNIT_VTABLE(u
)->bus_name_owner_change(u
, name
, old_owner
, new_owner
);
2628 void manager_dispatch_bus_query_pid_done(
2639 if (!(u
= hashmap_get(m
->watch_bus
, name
)))
2642 UNIT_VTABLE(u
)->bus_query_pid_done(u
, name
, pid
);
2645 int manager_open_serialization(Manager
*m
, FILE **_f
) {
2653 if (m
->running_as
== MANAGER_SYSTEM
)
2654 asprintf(&path
, "/run/systemd/dump-%lu-XXXXXX", (unsigned long) getpid());
2656 asprintf(&path
, "/tmp/systemd-dump-%lu-XXXXXX", (unsigned long) getpid());
2661 saved_umask
= umask(0077);
2662 fd
= mkostemp(path
, O_RDWR
|O_CLOEXEC
);
2672 log_debug("Serializing state to %s", path
);
2675 if (!(f
= fdopen(fd
, "w+")))
2683 int manager_serialize(Manager
*m
, FILE *f
, FDSet
*fds
) {
2695 fprintf(f
, "current-job-id=%i\n", m
->current_job_id
);
2696 fprintf(f
, "taint-usr=%s\n", yes_no(m
->taint_usr
));
2698 dual_timestamp_serialize(f
, "initrd-timestamp", &m
->initrd_timestamp
);
2699 dual_timestamp_serialize(f
, "startup-timestamp", &m
->startup_timestamp
);
2700 dual_timestamp_serialize(f
, "finish-timestamp", &m
->finish_timestamp
);
2704 HASHMAP_FOREACH_KEY(u
, t
, m
->units
, i
) {
2705 if (u
->meta
.id
!= t
)
2708 if (!unit_can_serialize(u
))
2712 fputs(u
->meta
.id
, f
);
2715 if ((r
= unit_serialize(u
, f
, fds
)) < 0) {
2721 assert(m
->n_reloading
> 0);
2727 r
= bus_fdset_add_all(m
, fds
);
2734 int manager_deserialize(Manager
*m
, FILE *f
, FDSet
*fds
) {
2740 log_debug("Deserializing state...");
2745 char line
[LINE_MAX
], *l
;
2747 if (!fgets(line
, sizeof(line
), f
)) {
2762 if (startswith(l
, "current-job-id=")) {
2765 if (safe_atou32(l
+15, &id
) < 0)
2766 log_debug("Failed to parse current job id value %s", l
+15);
2768 m
->current_job_id
= MAX(m
->current_job_id
, id
);
2769 } else if (startswith(l
, "taint-usr=")) {
2772 if ((b
= parse_boolean(l
+10)) < 0)
2773 log_debug("Failed to parse taint /usr flag %s", l
+10);
2775 m
->taint_usr
= m
->taint_usr
|| b
;
2776 } else if (startswith(l
, "initrd-timestamp="))
2777 dual_timestamp_deserialize(l
+17, &m
->initrd_timestamp
);
2778 else if (startswith(l
, "startup-timestamp="))
2779 dual_timestamp_deserialize(l
+18, &m
->startup_timestamp
);
2780 else if (startswith(l
, "finish-timestamp="))
2781 dual_timestamp_deserialize(l
+17, &m
->finish_timestamp
);
2783 log_debug("Unknown serialization item '%s'", l
);
2788 char name
[UNIT_NAME_MAX
+2];
2791 if (!fgets(name
, sizeof(name
), f
)) {
2802 if ((r
= manager_load_unit(m
, strstrip(name
), NULL
, NULL
, &u
)) < 0)
2805 if ((r
= unit_deserialize(u
, f
, fds
)) < 0)
2815 assert(m
->n_reloading
> 0);
2821 int manager_reload(Manager
*m
) {
2828 if ((r
= manager_open_serialization(m
, &f
)) < 0)
2833 if (!(fds
= fdset_new())) {
2839 if ((r
= manager_serialize(m
, f
, fds
)) < 0) {
2844 if (fseeko(f
, 0, SEEK_SET
) < 0) {
2850 /* From here on there is no way back. */
2851 manager_clear_jobs_and_units(m
);
2852 manager_undo_generators(m
);
2854 /* Find new unit paths */
2855 lookup_paths_free(&m
->lookup_paths
);
2856 if ((q
= lookup_paths_init(&m
->lookup_paths
, m
->running_as
, true)) < 0)
2859 manager_run_generators(m
);
2861 manager_build_unit_path_cache(m
);
2863 /* First, enumerate what we can from all config files */
2864 if ((q
= manager_enumerate(m
)) < 0)
2867 /* Second, deserialize our stored data */
2868 if ((q
= manager_deserialize(m
, f
, fds
)) < 0)
2874 /* Third, fire things up! */
2875 if ((q
= manager_coldplug(m
)) < 0)
2878 assert(m
->n_reloading
> 0);
2891 bool manager_is_booting_or_shutting_down(Manager
*m
) {
2896 /* Is the initial job still around? */
2897 if (manager_get_job(m
, 1))
2900 /* Is there a job for the shutdown target? */
2901 if (((u
= manager_get_unit(m
, SPECIAL_SHUTDOWN_TARGET
))))
2902 return !!u
->meta
.job
;
2907 void manager_reset_failed(Manager
*m
) {
2913 HASHMAP_FOREACH(u
, m
->units
, i
)
2914 unit_reset_failed(u
);
2917 bool manager_unit_pending_inactive(Manager
*m
, const char *name
) {
2923 /* Returns true if the unit is inactive or going down */
2924 if (!(u
= manager_get_unit(m
, name
)))
2927 return unit_pending_inactive(u
);
2930 void manager_check_finished(Manager
*m
) {
2931 char userspace
[FORMAT_TIMESPAN_MAX
], initrd
[FORMAT_TIMESPAN_MAX
], kernel
[FORMAT_TIMESPAN_MAX
], sum
[FORMAT_TIMESPAN_MAX
];
2932 usec_t kernel_usec
= 0, initrd_usec
= 0, userspace_usec
= 0, total_usec
= 0;
2936 if (dual_timestamp_is_set(&m
->finish_timestamp
))
2939 if (hashmap_size(m
->jobs
) > 0)
2942 dual_timestamp_get(&m
->finish_timestamp
);
2944 if (m
->running_as
== MANAGER_SYSTEM
&& detect_container(NULL
) <= 0) {
2946 userspace_usec
= m
->finish_timestamp
.monotonic
- m
->startup_timestamp
.monotonic
;
2947 total_usec
= m
->finish_timestamp
.monotonic
;
2949 if (dual_timestamp_is_set(&m
->initrd_timestamp
)) {
2951 kernel_usec
= m
->initrd_timestamp
.monotonic
;
2952 initrd_usec
= m
->startup_timestamp
.monotonic
- m
->initrd_timestamp
.monotonic
;
2954 log_info("Startup finished in %s (kernel) + %s (initrd) + %s (userspace) = %s.",
2955 format_timespan(kernel
, sizeof(kernel
), kernel_usec
),
2956 format_timespan(initrd
, sizeof(initrd
), initrd_usec
),
2957 format_timespan(userspace
, sizeof(userspace
), userspace_usec
),
2958 format_timespan(sum
, sizeof(sum
), total_usec
));
2960 kernel_usec
= m
->startup_timestamp
.monotonic
;
2963 log_info("Startup finished in %s (kernel) + %s (userspace) = %s.",
2964 format_timespan(kernel
, sizeof(kernel
), kernel_usec
),
2965 format_timespan(userspace
, sizeof(userspace
), userspace_usec
),
2966 format_timespan(sum
, sizeof(sum
), total_usec
));
2969 userspace_usec
= initrd_usec
= kernel_usec
= 0;
2970 total_usec
= m
->finish_timestamp
.monotonic
- m
->startup_timestamp
.monotonic
;
2972 log_debug("Startup finished in %s.",
2973 format_timespan(sum
, sizeof(sum
), total_usec
));
2976 bus_broadcast_finished(m
, kernel_usec
, initrd_usec
, userspace_usec
, total_usec
);
2979 "READY=1\nSTATUS=Startup finished in %s.",
2980 format_timespan(sum
, sizeof(sum
), total_usec
));
2983 void manager_run_generators(Manager
*m
) {
2985 const char *generator_path
;
2986 const char *argv
[3];
2990 generator_path
= m
->running_as
== MANAGER_SYSTEM
? SYSTEM_GENERATOR_PATH
: USER_GENERATOR_PATH
;
2991 if (!(d
= opendir(generator_path
))) {
2993 if (errno
== ENOENT
)
2996 log_error("Failed to enumerate generator directory: %m");
3000 if (!m
->generator_unit_path
) {
3002 char user_path
[] = "/tmp/systemd-generator-XXXXXX";
3004 if (m
->running_as
== MANAGER_SYSTEM
&& getpid() == 1) {
3005 p
= "/run/systemd/generator";
3007 if (mkdir_p(p
, 0755) < 0) {
3008 log_error("Failed to create generator directory: %m");
3013 if (!(p
= mkdtemp(user_path
))) {
3014 log_error("Failed to create generator directory: %m");
3019 if (!(m
->generator_unit_path
= strdup(p
))) {
3020 log_error("Failed to allocate generator unit path.");
3025 argv
[0] = NULL
; /* Leave this empty, execute_directory() will fill something in */
3026 argv
[1] = m
->generator_unit_path
;
3029 execute_directory(generator_path
, d
, (char**) argv
);
3031 if (rmdir(m
->generator_unit_path
) >= 0) {
3032 /* Uh? we were able to remove this dir? I guess that
3033 * means the directory was empty, hence let's shortcut
3036 free(m
->generator_unit_path
);
3037 m
->generator_unit_path
= NULL
;
3041 if (!strv_find(m
->lookup_paths
.unit_path
, m
->generator_unit_path
)) {
3044 if (!(l
= strv_append(m
->lookup_paths
.unit_path
, m
->generator_unit_path
))) {
3045 log_error("Failed to add generator directory to unit search path: %m");
3049 strv_free(m
->lookup_paths
.unit_path
);
3050 m
->lookup_paths
.unit_path
= l
;
3052 log_debug("Added generator unit path %s to search path.", m
->generator_unit_path
);
3060 void manager_undo_generators(Manager
*m
) {
3063 if (!m
->generator_unit_path
)
3066 strv_remove(m
->lookup_paths
.unit_path
, m
->generator_unit_path
);
3067 rm_rf(m
->generator_unit_path
, false, true);
3069 free(m
->generator_unit_path
);
3070 m
->generator_unit_path
= NULL
;
3073 int manager_set_default_controllers(Manager
*m
, char **controllers
) {
3078 if (!(l
= strv_copy(controllers
)))
3081 strv_free(m
->default_controllers
);
3082 m
->default_controllers
= l
;
3087 void manager_recheck_syslog(Manager
*m
) {
3092 if (m
->running_as
!= MANAGER_SYSTEM
)
3095 if ((u
= manager_get_unit(m
, SPECIAL_SYSLOG_SOCKET
))) {
3098 state
= SOCKET(u
)->state
;
3100 if (state
!= SOCKET_DEAD
&&
3101 state
!= SOCKET_FAILED
&&
3102 state
!= SOCKET_RUNNING
) {
3104 /* Hmm, the socket is not set up, or is still
3105 * listening, let's better not try to use
3106 * it. Note that we have no problem if the
3107 * socket is completely down, since there
3108 * might be a foreign /dev/log socket around
3109 * and we want to make use of that.
3117 if ((u
= manager_get_unit(m
, SPECIAL_SYSLOG_TARGET
)))
3118 if (TARGET(u
)->state
!= TARGET_ACTIVE
) {
3123 /* Hmm, OK, so the socket is either fully up, or fully down,
3124 * and the target is up, then let's make use of the socket */
3128 static const char* const manager_running_as_table
[_MANAGER_RUNNING_AS_MAX
] = {
3129 [MANAGER_SYSTEM
] = "system",
3130 [MANAGER_USER
] = "user"
3133 DEFINE_STRING_TABLE_LOOKUP(manager_running_as
, ManagerRunningAs
);