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 Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser 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>
44 #include <systemd/sd-daemon.h>
47 #include "transaction.h"
54 #include "ratelimit.h"
56 #include "mount-setup.h"
57 #include "unit-name.h"
58 #include "dbus-unit.h"
61 #include "path-lookup.h"
63 #include "bus-errors.h"
64 #include "exit-status.h"
67 #include "cgroup-util.h"
69 /* As soon as 16 units are in our GC queue, make sure to run a gc sweep */
70 #define GC_QUEUE_ENTRIES_MAX 16
72 /* As soon as 5s passed since a unit was added to our GC queue, make sure to run a gc sweep */
73 #define GC_QUEUE_USEC_MAX (10*USEC_PER_SEC)
75 /* Where clients shall send notification messages to */
76 #define NOTIFY_SOCKET_SYSTEM "/run/systemd/notify"
77 #define NOTIFY_SOCKET_USER "@/org/freedesktop/systemd1/notify"
79 static int manager_setup_notify(Manager
*m
) {
82 struct sockaddr_un un
;
84 struct epoll_event ev
;
90 m
->notify_watch
.type
= WATCH_NOTIFY
;
91 if ((m
->notify_watch
.fd
= socket(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0)) < 0) {
92 log_error("Failed to allocate notification socket: %m");
97 sa
.sa
.sa_family
= AF_UNIX
;
100 snprintf(sa
.un
.sun_path
, sizeof(sa
.un
.sun_path
), NOTIFY_SOCKET_USER
"/%llu", random_ull());
102 unlink(NOTIFY_SOCKET_SYSTEM
);
103 strncpy(sa
.un
.sun_path
, NOTIFY_SOCKET_SYSTEM
, sizeof(sa
.un
.sun_path
));
106 if (sa
.un
.sun_path
[0] == '@')
107 sa
.un
.sun_path
[0] = 0;
110 r
= bind(m
->notify_watch
.fd
, &sa
.sa
, offsetof(struct sockaddr_un
, sun_path
) + 1 + strlen(sa
.un
.sun_path
+1));
114 log_error("bind() failed: %m");
118 if (setsockopt(m
->notify_watch
.fd
, SOL_SOCKET
, SO_PASSCRED
, &one
, sizeof(one
)) < 0) {
119 log_error("SO_PASSCRED failed: %m");
125 ev
.data
.ptr
= &m
->notify_watch
;
127 if (epoll_ctl(m
->epoll_fd
, EPOLL_CTL_ADD
, m
->notify_watch
.fd
, &ev
) < 0)
130 if (sa
.un
.sun_path
[0] == 0)
131 sa
.un
.sun_path
[0] = '@';
133 if (!(m
->notify_socket
= strdup(sa
.un
.sun_path
)))
136 log_debug("Using notification socket %s", m
->notify_socket
);
141 static int enable_special_signals(Manager
*m
) {
146 /* Enable that we get SIGINT on control-alt-del. In containers
147 * this will fail with EPERM, so ignore that. */
148 if (reboot(RB_DISABLE_CAD
) < 0 && errno
!= EPERM
)
149 log_warning("Failed to enable ctrl-alt-del handling: %m");
151 fd
= open_terminal("/dev/tty0", O_RDWR
|O_NOCTTY
|O_CLOEXEC
);
153 /* Support systems without virtual console */
155 log_warning("Failed to open /dev/tty0: %m");
157 /* Enable that we get SIGWINCH on kbrequest */
158 if (ioctl(fd
, KDSIGACCEPT
, SIGWINCH
) < 0)
159 log_warning("Failed to enable kbrequest handling: %s", strerror(errno
));
161 close_nointr_nofail(fd
);
167 static int manager_setup_signals(Manager
*m
) {
169 struct epoll_event ev
;
174 /* We are not interested in SIGSTOP and friends. */
176 sa
.sa_handler
= SIG_DFL
;
177 sa
.sa_flags
= SA_NOCLDSTOP
|SA_RESTART
;
178 assert_se(sigaction(SIGCHLD
, &sa
, NULL
) == 0);
180 assert_se(sigemptyset(&mask
) == 0);
182 sigset_add_many(&mask
,
183 SIGCHLD
, /* Child died */
184 SIGTERM
, /* Reexecute daemon */
185 SIGHUP
, /* Reload configuration */
186 SIGUSR1
, /* systemd/upstart: reconnect to D-Bus */
187 SIGUSR2
, /* systemd: dump status */
188 SIGINT
, /* Kernel sends us this on control-alt-del */
189 SIGWINCH
, /* Kernel sends us this on kbrequest (alt-arrowup) */
190 SIGPWR
, /* Some kernel drivers and upsd send us this on power failure */
191 SIGRTMIN
+0, /* systemd: start default.target */
192 SIGRTMIN
+1, /* systemd: isolate rescue.target */
193 SIGRTMIN
+2, /* systemd: isolate emergency.target */
194 SIGRTMIN
+3, /* systemd: start halt.target */
195 SIGRTMIN
+4, /* systemd: start poweroff.target */
196 SIGRTMIN
+5, /* systemd: start reboot.target */
197 SIGRTMIN
+6, /* systemd: start kexec.target */
198 SIGRTMIN
+13, /* systemd: Immediate halt */
199 SIGRTMIN
+14, /* systemd: Immediate poweroff */
200 SIGRTMIN
+15, /* systemd: Immediate reboot */
201 SIGRTMIN
+16, /* systemd: Immediate kexec */
202 SIGRTMIN
+20, /* systemd: enable status messages */
203 SIGRTMIN
+21, /* systemd: disable status messages */
204 SIGRTMIN
+22, /* systemd: set log level to LOG_DEBUG */
205 SIGRTMIN
+23, /* systemd: set log level to LOG_INFO */
206 SIGRTMIN
+26, /* systemd: set log target to journal-or-kmsg */
207 SIGRTMIN
+27, /* systemd: set log target to console */
208 SIGRTMIN
+28, /* systemd: set log target to kmsg */
209 SIGRTMIN
+29, /* systemd: set log target to syslog-or-kmsg */
211 assert_se(sigprocmask(SIG_SETMASK
, &mask
, NULL
) == 0);
213 m
->signal_watch
.type
= WATCH_SIGNAL
;
214 if ((m
->signal_watch
.fd
= signalfd(-1, &mask
, SFD_NONBLOCK
|SFD_CLOEXEC
)) < 0)
219 ev
.data
.ptr
= &m
->signal_watch
;
221 if (epoll_ctl(m
->epoll_fd
, EPOLL_CTL_ADD
, m
->signal_watch
.fd
, &ev
) < 0)
224 if (m
->running_as
== MANAGER_SYSTEM
)
225 return enable_special_signals(m
);
230 static void manager_strip_environment(Manager
*m
) {
233 /* Remove variables from the inherited set that are part of
234 * the container interface:
235 * http://www.freedesktop.org/wiki/Software/systemd/ContainerInterface */
236 strv_remove_prefix(m
->environment
, "container=");
237 strv_remove_prefix(m
->environment
, "container_");
239 /* Remove variables from the inherited set that are part of
240 * the initrd interface:
241 * http://www.freedesktop.org/wiki/Software/systemd/InitrdInterface */
242 strv_remove_prefix(m
->environment
, "RD_");
245 int manager_new(ManagerRunningAs running_as
, Manager
**_m
) {
250 assert(running_as
>= 0);
251 assert(running_as
< _MANAGER_RUNNING_AS_MAX
);
253 if (!(m
= new0(Manager
, 1)))
256 dual_timestamp_get(&m
->startup_timestamp
);
258 m
->running_as
= running_as
;
259 m
->name_data_slot
= m
->conn_data_slot
= m
->subscribed_data_slot
= -1;
260 m
->exit_code
= _MANAGER_EXIT_CODE_INVALID
;
261 m
->pin_cgroupfs_fd
= -1;
262 m
->idle_pipe
[0] = m
->idle_pipe
[1] = -1;
268 m
->signal_watch
.fd
= m
->mount_watch
.fd
= m
->udev_watch
.fd
= m
->epoll_fd
= m
->dev_autofs_fd
= m
->swap_watch
.fd
= -1;
269 m
->current_job_id
= 1; /* start as id #1, so that we can leave #0 around as "null-like" value */
271 m
->environment
= strv_copy(environ
);
275 manager_strip_environment(m
);
277 if (running_as
== MANAGER_SYSTEM
) {
278 m
->default_controllers
= strv_new("cpu", NULL
);
279 if (!m
->default_controllers
)
283 if (!(m
->units
= hashmap_new(string_hash_func
, string_compare_func
)))
286 if (!(m
->jobs
= hashmap_new(trivial_hash_func
, trivial_compare_func
)))
289 if (!(m
->watch_pids
= hashmap_new(trivial_hash_func
, trivial_compare_func
)))
292 if (!(m
->cgroup_bondings
= hashmap_new(string_hash_func
, string_compare_func
)))
295 if (!(m
->watch_bus
= hashmap_new(string_hash_func
, string_compare_func
)))
298 if ((m
->epoll_fd
= epoll_create1(EPOLL_CLOEXEC
)) < 0)
301 if ((r
= lookup_paths_init(&m
->lookup_paths
, m
->running_as
, true)) < 0)
304 if ((r
= manager_setup_signals(m
)) < 0)
307 if ((r
= manager_setup_cgroup(m
)) < 0)
310 if ((r
= manager_setup_notify(m
)) < 0)
313 /* Try to connect to the busses, if possible. */
314 if ((r
= bus_init(m
, running_as
!= MANAGER_SYSTEM
)) < 0)
318 if ((m
->audit_fd
= audit_open()) < 0 &&
319 /* If the kernel lacks netlink or audit support,
320 * don't worry about it. */
321 errno
!= EAFNOSUPPORT
&& errno
!= EPROTONOSUPPORT
)
322 log_error("Failed to connect to audit log: %m");
325 m
->taint_usr
= dir_is_empty("/usr") > 0;
335 static unsigned manager_dispatch_cleanup_queue(Manager
*m
) {
341 while ((u
= m
->cleanup_queue
)) {
342 assert(u
->in_cleanup_queue
);
352 GC_OFFSET_IN_PATH
, /* This one is on the path we were traveling */
353 GC_OFFSET_UNSURE
, /* No clue */
354 GC_OFFSET_GOOD
, /* We still need this unit */
355 GC_OFFSET_BAD
, /* We don't need this unit anymore */
359 static void unit_gc_sweep(Unit
*u
, unsigned gc_marker
) {
366 if (u
->gc_marker
== gc_marker
+ GC_OFFSET_GOOD
||
367 u
->gc_marker
== gc_marker
+ GC_OFFSET_BAD
||
368 u
->gc_marker
== gc_marker
+ GC_OFFSET_IN_PATH
)
371 if (u
->in_cleanup_queue
)
374 if (unit_check_gc(u
))
377 u
->gc_marker
= gc_marker
+ GC_OFFSET_IN_PATH
;
381 SET_FOREACH(other
, u
->dependencies
[UNIT_REFERENCED_BY
], i
) {
382 unit_gc_sweep(other
, gc_marker
);
384 if (other
->gc_marker
== gc_marker
+ GC_OFFSET_GOOD
)
387 if (other
->gc_marker
!= gc_marker
+ GC_OFFSET_BAD
)
394 /* We were unable to find anything out about this entry, so
395 * let's investigate it later */
396 u
->gc_marker
= gc_marker
+ GC_OFFSET_UNSURE
;
397 unit_add_to_gc_queue(u
);
401 /* We definitely know that this one is not useful anymore, so
402 * let's mark it for deletion */
403 u
->gc_marker
= gc_marker
+ GC_OFFSET_BAD
;
404 unit_add_to_cleanup_queue(u
);
408 u
->gc_marker
= gc_marker
+ GC_OFFSET_GOOD
;
411 static unsigned manager_dispatch_gc_queue(Manager
*m
) {
418 if ((m
->n_in_gc_queue
< GC_QUEUE_ENTRIES_MAX
) &&
419 (m
->gc_queue_timestamp
<= 0 ||
420 (m
->gc_queue_timestamp
+ GC_QUEUE_USEC_MAX
) > now(CLOCK_MONOTONIC
)))
423 log_debug("Running GC...");
425 m
->gc_marker
+= _GC_OFFSET_MAX
;
426 if (m
->gc_marker
+ _GC_OFFSET_MAX
<= _GC_OFFSET_MAX
)
429 gc_marker
= m
->gc_marker
;
431 while ((u
= m
->gc_queue
)) {
432 assert(u
->in_gc_queue
);
434 unit_gc_sweep(u
, gc_marker
);
436 LIST_REMOVE(Unit
, gc_queue
, m
->gc_queue
, u
);
437 u
->in_gc_queue
= false;
441 if (u
->gc_marker
== gc_marker
+ GC_OFFSET_BAD
||
442 u
->gc_marker
== gc_marker
+ GC_OFFSET_UNSURE
) {
443 log_debug("Collecting %s", u
->id
);
444 u
->gc_marker
= gc_marker
+ GC_OFFSET_BAD
;
445 unit_add_to_cleanup_queue(u
);
449 m
->n_in_gc_queue
= 0;
450 m
->gc_queue_timestamp
= 0;
455 static void manager_clear_jobs_and_units(Manager
*m
) {
460 while ((u
= hashmap_first(m
->units
)))
463 manager_dispatch_cleanup_queue(m
);
465 assert(!m
->load_queue
);
466 assert(!m
->run_queue
);
467 assert(!m
->dbus_unit_queue
);
468 assert(!m
->dbus_job_queue
);
469 assert(!m
->cleanup_queue
);
470 assert(!m
->gc_queue
);
472 assert(hashmap_isempty(m
->jobs
));
473 assert(hashmap_isempty(m
->units
));
476 void manager_free(Manager
*m
) {
481 manager_clear_jobs_and_units(m
);
483 for (c
= 0; c
< _UNIT_TYPE_MAX
; c
++)
484 if (unit_vtable
[c
]->shutdown
)
485 unit_vtable
[c
]->shutdown(m
);
487 /* If we reexecute ourselves, we keep the root cgroup
489 manager_shutdown_cgroup(m
, m
->exit_code
!= MANAGER_REEXECUTE
);
491 manager_undo_generators(m
);
495 hashmap_free(m
->units
);
496 hashmap_free(m
->jobs
);
497 hashmap_free(m
->watch_pids
);
498 hashmap_free(m
->watch_bus
);
500 if (m
->epoll_fd
>= 0)
501 close_nointr_nofail(m
->epoll_fd
);
502 if (m
->signal_watch
.fd
>= 0)
503 close_nointr_nofail(m
->signal_watch
.fd
);
504 if (m
->notify_watch
.fd
>= 0)
505 close_nointr_nofail(m
->notify_watch
.fd
);
508 if (m
->audit_fd
>= 0)
509 audit_close(m
->audit_fd
);
512 free(m
->notify_socket
);
514 lookup_paths_free(&m
->lookup_paths
);
515 strv_free(m
->environment
);
517 strv_free(m
->default_controllers
);
519 hashmap_free(m
->cgroup_bondings
);
520 set_free_free(m
->unit_path_cache
);
522 close_pipe(m
->idle_pipe
);
527 int manager_enumerate(Manager
*m
) {
533 /* Let's ask every type to load all units from disk/kernel
534 * that it might know */
535 for (c
= 0; c
< _UNIT_TYPE_MAX
; c
++)
536 if (unit_vtable
[c
]->enumerate
)
537 if ((q
= unit_vtable
[c
]->enumerate(m
)) < 0)
540 manager_dispatch_load_queue(m
);
544 int manager_coldplug(Manager
*m
) {
552 /* Then, let's set up their initial state. */
553 HASHMAP_FOREACH_KEY(u
, k
, m
->units
, i
) {
559 if ((q
= unit_coldplug(u
)) < 0)
566 static void manager_build_unit_path_cache(Manager
*m
) {
573 set_free_free(m
->unit_path_cache
);
575 if (!(m
->unit_path_cache
= set_new(string_hash_func
, string_compare_func
))) {
576 log_error("Failed to allocate unit path cache.");
580 /* This simply builds a list of files we know exist, so that
581 * we don't always have to go to disk */
583 STRV_FOREACH(i
, m
->lookup_paths
.unit_path
) {
586 if (!(d
= opendir(*i
))) {
587 log_error("Failed to open directory: %m");
591 while ((de
= readdir(d
))) {
594 if (ignore_file(de
->d_name
))
597 p
= join(streq(*i
, "/") ? "" : *i
, "/", de
->d_name
, NULL
);
603 if ((r
= set_put(m
->unit_path_cache
, p
)) < 0) {
616 log_error("Failed to build unit path cache: %s", strerror(-r
));
618 set_free_free(m
->unit_path_cache
);
619 m
->unit_path_cache
= NULL
;
625 int manager_startup(Manager
*m
, FILE *serialization
, FDSet
*fds
) {
630 manager_run_generators(m
);
632 manager_build_unit_path_cache(m
);
634 /* If we will deserialize make sure that during enumeration
635 * this is already known, so we increase the counter here
640 /* First, enumerate what we can from all config files */
641 r
= manager_enumerate(m
);
643 /* Second, deserialize if there is something to deserialize */
645 if ((q
= manager_deserialize(m
, serialization
, fds
)) < 0)
648 /* Third, fire things up! */
649 if ((q
= manager_coldplug(m
)) < 0)
653 assert(m
->n_reloading
> 0);
660 int manager_add_job(Manager
*m
, JobType type
, Unit
*unit
, JobMode mode
, bool override
, DBusError
*e
, Job
**_ret
) {
665 assert(type
< _JOB_TYPE_MAX
);
667 assert(mode
< _JOB_MODE_MAX
);
669 if (mode
== JOB_ISOLATE
&& type
!= JOB_START
) {
670 dbus_set_error(e
, BUS_ERROR_INVALID_JOB_MODE
, "Isolate is only valid for start.");
674 if (mode
== JOB_ISOLATE
&& !unit
->allow_isolate
) {
675 dbus_set_error(e
, BUS_ERROR_NO_ISOLATION
, "Operation refused, unit may not be isolated.");
679 log_debug("Trying to enqueue job %s/%s/%s", unit
->id
, job_type_to_string(type
), job_mode_to_string(mode
));
681 tr
= transaction_new();
685 r
= transaction_add_job_and_dependencies(tr
, type
, unit
, NULL
, true, override
, false,
686 mode
== JOB_IGNORE_DEPENDENCIES
|| mode
== JOB_IGNORE_REQUIREMENTS
,
687 mode
== JOB_IGNORE_DEPENDENCIES
, e
);
691 if (mode
== JOB_ISOLATE
) {
692 r
= transaction_add_isolate_jobs(tr
, m
);
697 r
= transaction_activate(tr
, m
, mode
, e
);
701 log_debug("Enqueued job %s/%s as %u", unit
->id
, job_type_to_string(type
), (unsigned) tr
->anchor_job
->id
);
704 *_ret
= tr
->anchor_job
;
706 transaction_free(tr
);
710 transaction_abort(tr
);
711 transaction_free(tr
);
715 int manager_add_job_by_name(Manager
*m
, JobType type
, const char *name
, JobMode mode
, bool override
, DBusError
*e
, Job
**_ret
) {
720 assert(type
< _JOB_TYPE_MAX
);
722 assert(mode
< _JOB_MODE_MAX
);
724 if ((r
= manager_load_unit(m
, name
, NULL
, NULL
, &unit
)) < 0)
727 return manager_add_job(m
, type
, unit
, mode
, override
, e
, _ret
);
730 Job
*manager_get_job(Manager
*m
, uint32_t id
) {
733 return hashmap_get(m
->jobs
, UINT32_TO_PTR(id
));
736 Unit
*manager_get_unit(Manager
*m
, const char *name
) {
740 return hashmap_get(m
->units
, name
);
743 unsigned manager_dispatch_load_queue(Manager
*m
) {
749 /* Make sure we are not run recursively */
750 if (m
->dispatching_load_queue
)
753 m
->dispatching_load_queue
= true;
755 /* Dispatches the load queue. Takes a unit from the queue and
756 * tries to load its data until the queue is empty */
758 while ((u
= m
->load_queue
)) {
759 assert(u
->in_load_queue
);
765 m
->dispatching_load_queue
= false;
769 int manager_load_unit_prepare(Manager
*m
, const char *name
, const char *path
, DBusError
*e
, Unit
**_ret
) {
775 assert(name
|| path
);
777 /* This will prepare the unit for loading, but not actually
778 * load anything from disk. */
780 if (path
&& !is_path(path
)) {
781 dbus_set_error(e
, BUS_ERROR_INVALID_PATH
, "Path %s is not absolute.", path
);
786 name
= file_name_from_path(path
);
788 t
= unit_name_to_type(name
);
790 if (t
== _UNIT_TYPE_INVALID
|| !unit_name_is_valid_no_type(name
, false)) {
791 dbus_set_error(e
, BUS_ERROR_INVALID_NAME
, "Unit name %s is not valid.", name
);
795 ret
= manager_get_unit(m
, name
);
801 ret
= unit_new(m
, unit_vtable
[t
]->object_size
);
806 ret
->fragment_path
= strdup(path
);
807 if (!ret
->fragment_path
) {
813 if ((r
= unit_add_name(ret
, name
)) < 0) {
818 unit_add_to_load_queue(ret
);
819 unit_add_to_dbus_queue(ret
);
820 unit_add_to_gc_queue(ret
);
828 int manager_load_unit(Manager
*m
, const char *name
, const char *path
, DBusError
*e
, Unit
**_ret
) {
833 /* This will load the service information files, but not actually
834 * start any services or anything. */
836 if ((r
= manager_load_unit_prepare(m
, name
, path
, e
, _ret
)) != 0)
839 manager_dispatch_load_queue(m
);
842 *_ret
= unit_follow_merge(*_ret
);
847 void manager_dump_jobs(Manager
*s
, FILE *f
, const char *prefix
) {
854 HASHMAP_FOREACH(j
, s
->jobs
, i
)
855 job_dump(j
, f
, prefix
);
858 void manager_dump_units(Manager
*s
, FILE *f
, const char *prefix
) {
866 HASHMAP_FOREACH_KEY(u
, t
, s
->units
, i
)
868 unit_dump(u
, f
, prefix
);
871 void manager_clear_jobs(Manager
*m
) {
876 while ((j
= hashmap_first(m
->jobs
)))
877 /* No need to recurse. We're cancelling all jobs. */
878 job_finish_and_invalidate(j
, JOB_CANCELED
, false);
881 unsigned manager_dispatch_run_queue(Manager
*m
) {
885 if (m
->dispatching_run_queue
)
888 m
->dispatching_run_queue
= true;
890 while ((j
= m
->run_queue
)) {
891 assert(j
->installed
);
892 assert(j
->in_run_queue
);
894 job_run_and_invalidate(j
);
898 m
->dispatching_run_queue
= false;
902 unsigned manager_dispatch_dbus_queue(Manager
*m
) {
909 if (m
->dispatching_dbus_queue
)
912 m
->dispatching_dbus_queue
= true;
914 while ((u
= m
->dbus_unit_queue
)) {
915 assert(u
->in_dbus_queue
);
917 bus_unit_send_change_signal(u
);
921 while ((j
= m
->dbus_job_queue
)) {
922 assert(j
->in_dbus_queue
);
924 bus_job_send_change_signal(j
);
928 m
->dispatching_dbus_queue
= false;
932 static int manager_process_notify_fd(Manager
*m
) {
939 struct msghdr msghdr
;
943 struct cmsghdr cmsghdr
;
944 uint8_t buf
[CMSG_SPACE(sizeof(struct ucred
))];
950 iovec
.iov_base
= buf
;
951 iovec
.iov_len
= sizeof(buf
)-1;
955 msghdr
.msg_iov
= &iovec
;
956 msghdr
.msg_iovlen
= 1;
957 msghdr
.msg_control
= &control
;
958 msghdr
.msg_controllen
= sizeof(control
);
960 if ((n
= recvmsg(m
->notify_watch
.fd
, &msghdr
, MSG_DONTWAIT
)) <= 0) {
964 if (errno
== EAGAIN
|| errno
== EINTR
)
970 if (msghdr
.msg_controllen
< CMSG_LEN(sizeof(struct ucred
)) ||
971 control
.cmsghdr
.cmsg_level
!= SOL_SOCKET
||
972 control
.cmsghdr
.cmsg_type
!= SCM_CREDENTIALS
||
973 control
.cmsghdr
.cmsg_len
!= CMSG_LEN(sizeof(struct ucred
))) {
974 log_warning("Received notify message without credentials. Ignoring.");
978 ucred
= (struct ucred
*) CMSG_DATA(&control
.cmsghdr
);
980 if (!(u
= hashmap_get(m
->watch_pids
, LONG_TO_PTR(ucred
->pid
))))
981 if (!(u
= cgroup_unit_by_pid(m
, ucred
->pid
))) {
982 log_warning("Cannot find unit for notify message of PID %lu.", (unsigned long) ucred
->pid
);
986 assert((size_t) n
< sizeof(buf
));
988 if (!(tags
= strv_split(buf
, "\n\r")))
991 log_debug("Got notification message for unit %s", u
->id
);
993 if (UNIT_VTABLE(u
)->notify_message
)
994 UNIT_VTABLE(u
)->notify_message(u
, ucred
->pid
, tags
);
1002 static int manager_dispatch_sigchld(Manager
*m
) {
1012 /* First we call waitd() for a PID and do not reap the
1013 * zombie. That way we can still access /proc/$PID for
1014 * it while it is a zombie. */
1015 if (waitid(P_ALL
, 0, &si
, WEXITED
|WNOHANG
|WNOWAIT
) < 0) {
1017 if (errno
== ECHILD
)
1029 if (si
.si_code
== CLD_EXITED
|| si
.si_code
== CLD_KILLED
|| si
.si_code
== CLD_DUMPED
) {
1032 get_process_comm(si
.si_pid
, &name
);
1033 log_debug("Got SIGCHLD for process %lu (%s)", (unsigned long) si
.si_pid
, strna(name
));
1037 /* Let's flush any message the dying child might still
1038 * have queued for us. This ensures that the process
1039 * still exists in /proc so that we can figure out
1040 * which cgroup and hence unit it belongs to. */
1041 if ((r
= manager_process_notify_fd(m
)) < 0)
1044 /* And now figure out the unit this belongs to */
1045 if (!(u
= hashmap_get(m
->watch_pids
, LONG_TO_PTR(si
.si_pid
))))
1046 u
= cgroup_unit_by_pid(m
, si
.si_pid
);
1048 /* And now, we actually reap the zombie. */
1049 if (waitid(P_PID
, si
.si_pid
, &si
, WEXITED
) < 0) {
1056 if (si
.si_code
!= CLD_EXITED
&& si
.si_code
!= CLD_KILLED
&& si
.si_code
!= CLD_DUMPED
)
1059 log_debug("Child %lu died (code=%s, status=%i/%s)",
1060 (long unsigned) si
.si_pid
,
1061 sigchld_code_to_string(si
.si_code
),
1063 strna(si
.si_code
== CLD_EXITED
1064 ? exit_status_to_string(si
.si_status
, EXIT_STATUS_FULL
)
1065 : signal_to_string(si
.si_status
)));
1070 log_debug("Child %lu belongs to %s", (long unsigned) si
.si_pid
, u
->id
);
1072 hashmap_remove(m
->watch_pids
, LONG_TO_PTR(si
.si_pid
));
1073 UNIT_VTABLE(u
)->sigchld_event(u
, si
.si_pid
, si
.si_code
, si
.si_status
);
1079 static int manager_start_target(Manager
*m
, const char *name
, JobMode mode
) {
1083 dbus_error_init(&error
);
1085 log_debug("Activating special unit %s", name
);
1087 if ((r
= manager_add_job_by_name(m
, JOB_START
, name
, mode
, true, &error
, NULL
)) < 0)
1088 log_error("Failed to enqueue %s job: %s", name
, bus_error(&error
, r
));
1090 dbus_error_free(&error
);
1095 static int manager_process_signal_fd(Manager
*m
) {
1097 struct signalfd_siginfo sfsi
;
1098 bool sigchld
= false;
1103 if ((n
= read(m
->signal_watch
.fd
, &sfsi
, sizeof(sfsi
))) != sizeof(sfsi
)) {
1108 if (errno
== EINTR
|| errno
== EAGAIN
)
1114 if (sfsi
.ssi_pid
> 0) {
1117 get_process_comm(sfsi
.ssi_pid
, &p
);
1119 log_debug("Received SIG%s from PID %lu (%s).",
1120 signal_to_string(sfsi
.ssi_signo
),
1121 (unsigned long) sfsi
.ssi_pid
, strna(p
));
1124 log_debug("Received SIG%s.", signal_to_string(sfsi
.ssi_signo
));
1126 switch (sfsi
.ssi_signo
) {
1133 if (m
->running_as
== MANAGER_SYSTEM
) {
1134 /* This is for compatibility with the
1135 * original sysvinit */
1136 m
->exit_code
= MANAGER_REEXECUTE
;
1143 if (m
->running_as
== MANAGER_SYSTEM
) {
1144 manager_start_target(m
, SPECIAL_CTRL_ALT_DEL_TARGET
, JOB_REPLACE
);
1148 /* Run the exit target if there is one, if not, just exit. */
1149 if (manager_start_target(m
, SPECIAL_EXIT_TARGET
, JOB_REPLACE
) < 0) {
1150 m
->exit_code
= MANAGER_EXIT
;
1157 if (m
->running_as
== MANAGER_SYSTEM
)
1158 manager_start_target(m
, SPECIAL_KBREQUEST_TARGET
, JOB_REPLACE
);
1160 /* This is a nop on non-init */
1164 if (m
->running_as
== MANAGER_SYSTEM
)
1165 manager_start_target(m
, SPECIAL_SIGPWR_TARGET
, JOB_REPLACE
);
1167 /* This is a nop on non-init */
1173 u
= manager_get_unit(m
, SPECIAL_DBUS_SERVICE
);
1175 if (!u
|| UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u
))) {
1176 log_info("Trying to reconnect to bus...");
1180 if (!u
|| !UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u
))) {
1181 log_info("Loading D-Bus service...");
1182 manager_start_target(m
, SPECIAL_DBUS_SERVICE
, JOB_REPLACE
);
1193 if (!(f
= open_memstream(&dump
, &size
))) {
1194 log_warning("Failed to allocate memory stream.");
1198 manager_dump_units(m
, f
, "\t");
1199 manager_dump_jobs(m
, f
, "\t");
1204 log_warning("Failed to write status stream");
1209 log_dump(LOG_INFO
, dump
);
1216 m
->exit_code
= MANAGER_RELOAD
;
1221 /* Starting SIGRTMIN+0 */
1222 static const char * const target_table
[] = {
1223 [0] = SPECIAL_DEFAULT_TARGET
,
1224 [1] = SPECIAL_RESCUE_TARGET
,
1225 [2] = SPECIAL_EMERGENCY_TARGET
,
1226 [3] = SPECIAL_HALT_TARGET
,
1227 [4] = SPECIAL_POWEROFF_TARGET
,
1228 [5] = SPECIAL_REBOOT_TARGET
,
1229 [6] = SPECIAL_KEXEC_TARGET
1232 /* Starting SIGRTMIN+13, so that target halt and system halt are 10 apart */
1233 static const ManagerExitCode code_table
[] = {
1235 [1] = MANAGER_POWEROFF
,
1236 [2] = MANAGER_REBOOT
,
1240 if ((int) sfsi
.ssi_signo
>= SIGRTMIN
+0 &&
1241 (int) sfsi
.ssi_signo
< SIGRTMIN
+(int) ELEMENTSOF(target_table
)) {
1242 int idx
= (int) sfsi
.ssi_signo
- SIGRTMIN
;
1243 manager_start_target(m
, target_table
[idx
],
1244 (idx
== 1 || idx
== 2) ? JOB_ISOLATE
: JOB_REPLACE
);
1248 if ((int) sfsi
.ssi_signo
>= SIGRTMIN
+13 &&
1249 (int) sfsi
.ssi_signo
< SIGRTMIN
+13+(int) ELEMENTSOF(code_table
)) {
1250 m
->exit_code
= code_table
[sfsi
.ssi_signo
- SIGRTMIN
- 13];
1254 switch (sfsi
.ssi_signo
- SIGRTMIN
) {
1257 log_debug("Enabling showing of status.");
1258 manager_set_show_status(m
, true);
1262 log_debug("Disabling showing of status.");
1263 manager_set_show_status(m
, false);
1267 log_set_max_level(LOG_DEBUG
);
1268 log_notice("Setting log level to debug.");
1272 log_set_max_level(LOG_INFO
);
1273 log_notice("Setting log level to info.");
1277 log_set_target(LOG_TARGET_JOURNAL_OR_KMSG
);
1278 log_notice("Setting log target to journal-or-kmsg.");
1282 log_set_target(LOG_TARGET_CONSOLE
);
1283 log_notice("Setting log target to console.");
1287 log_set_target(LOG_TARGET_KMSG
);
1288 log_notice("Setting log target to kmsg.");
1292 log_set_target(LOG_TARGET_SYSLOG_OR_KMSG
);
1293 log_notice("Setting log target to syslog-or-kmsg.");
1297 log_warning("Got unhandled signal <%s>.", signal_to_string(sfsi
.ssi_signo
));
1304 return manager_dispatch_sigchld(m
);
1309 static int process_event(Manager
*m
, struct epoll_event
*ev
) {
1316 assert_se(w
= ev
->data
.ptr
);
1318 if (w
->type
== WATCH_INVALID
)
1325 /* An incoming signal? */
1326 if (ev
->events
!= EPOLLIN
)
1329 if ((r
= manager_process_signal_fd(m
)) < 0)
1336 /* An incoming daemon notification event? */
1337 if (ev
->events
!= EPOLLIN
)
1340 if ((r
= manager_process_notify_fd(m
)) < 0)
1347 /* Some fd event, to be dispatched to the units */
1348 UNIT_VTABLE(w
->data
.unit
)->fd_event(w
->data
.unit
, w
->fd
, ev
->events
, w
);
1351 case WATCH_UNIT_TIMER
:
1352 case WATCH_JOB_TIMER
: {
1356 /* Some timer event, to be dispatched to the units */
1357 if ((k
= read(w
->fd
, &v
, sizeof(v
))) != sizeof(v
)) {
1359 if (k
< 0 && (errno
== EINTR
|| errno
== EAGAIN
))
1362 return k
< 0 ? -errno
: -EIO
;
1365 if (w
->type
== WATCH_UNIT_TIMER
)
1366 UNIT_VTABLE(w
->data
.unit
)->timer_event(w
->data
.unit
, v
, w
);
1368 job_timer_event(w
->data
.job
, v
, w
);
1373 /* Some mount table change, intended for the mount subsystem */
1374 mount_fd_event(m
, ev
->events
);
1378 /* Some swap table change, intended for the swap subsystem */
1379 swap_fd_event(m
, ev
->events
);
1383 /* Some notification from udev, intended for the device subsystem */
1384 device_fd_event(m
, ev
->events
);
1387 case WATCH_DBUS_WATCH
:
1388 bus_watch_event(m
, w
, ev
->events
);
1391 case WATCH_DBUS_TIMEOUT
:
1392 bus_timeout_event(m
, w
, ev
->events
);
1396 log_error("event type=%i", w
->type
);
1397 assert_not_reached("Unknown epoll event type.");
1403 int manager_loop(Manager
*m
) {
1406 RATELIMIT_DEFINE(rl
, 1*USEC_PER_SEC
, 50000);
1409 m
->exit_code
= MANAGER_RUNNING
;
1411 /* Release the path cache */
1412 set_free_free(m
->unit_path_cache
);
1413 m
->unit_path_cache
= NULL
;
1415 manager_check_finished(m
);
1417 /* There might still be some zombies hanging around from
1418 * before we were exec()'ed. Leat's reap them */
1419 r
= manager_dispatch_sigchld(m
);
1423 while (m
->exit_code
== MANAGER_RUNNING
) {
1424 struct epoll_event event
;
1428 if (m
->runtime_watchdog
> 0 && m
->running_as
== MANAGER_SYSTEM
)
1431 if (!ratelimit_test(&rl
)) {
1432 /* Yay, something is going seriously wrong, pause a little */
1433 log_warning("Looping too fast. Throttling execution a little.");
1438 if (manager_dispatch_load_queue(m
) > 0)
1441 if (manager_dispatch_run_queue(m
) > 0)
1444 if (bus_dispatch(m
) > 0)
1447 if (manager_dispatch_cleanup_queue(m
) > 0)
1450 if (manager_dispatch_gc_queue(m
) > 0)
1453 if (manager_dispatch_dbus_queue(m
) > 0)
1456 if (swap_dispatch_reload(m
) > 0)
1459 /* Sleep for half the watchdog time */
1460 if (m
->runtime_watchdog
> 0 && m
->running_as
== MANAGER_SYSTEM
) {
1461 wait_msec
= (int) (m
->runtime_watchdog
/ 2 / USEC_PER_MSEC
);
1467 n
= epoll_wait(m
->epoll_fd
, &event
, 1, wait_msec
);
1479 r
= process_event(m
, &event
);
1484 return m
->exit_code
;
1487 int manager_get_unit_from_dbus_path(Manager
*m
, const char *s
, Unit
**_u
) {
1495 if (!startswith(s
, "/org/freedesktop/systemd1/unit/"))
1498 if (!(n
= bus_path_unescape(s
+31)))
1501 u
= manager_get_unit(m
, n
);
1512 int manager_get_job_from_dbus_path(Manager
*m
, const char *s
, Job
**_j
) {
1521 if (!startswith(s
, "/org/freedesktop/systemd1/job/"))
1524 if ((r
= safe_atou(s
+ 30, &id
)) < 0)
1527 if (!(j
= manager_get_job(m
, id
)))
1535 void manager_send_unit_audit(Manager
*m
, Unit
*u
, int type
, bool success
) {
1540 if (m
->audit_fd
< 0)
1543 /* Don't generate audit events if the service was already
1544 * started and we're just deserializing */
1545 if (m
->n_reloading
> 0)
1548 if (m
->running_as
!= MANAGER_SYSTEM
)
1551 if (u
->type
!= UNIT_SERVICE
)
1554 if (!(p
= unit_name_to_prefix_and_instance(u
->id
))) {
1555 log_error("Failed to allocate unit name for audit message: %s", strerror(ENOMEM
));
1559 if (audit_log_user_comm_message(m
->audit_fd
, type
, "", p
, NULL
, NULL
, NULL
, success
) < 0) {
1560 if (errno
== EPERM
) {
1561 /* We aren't allowed to send audit messages?
1562 * Then let's not retry again. */
1563 audit_close(m
->audit_fd
);
1566 log_warning("Failed to send audit message: %m");
1574 void manager_send_unit_plymouth(Manager
*m
, Unit
*u
) {
1576 union sockaddr_union sa
;
1578 char *message
= NULL
;
1580 /* Don't generate plymouth events if the service was already
1581 * started and we're just deserializing */
1582 if (m
->n_reloading
> 0)
1585 if (m
->running_as
!= MANAGER_SYSTEM
)
1588 if (u
->type
!= UNIT_SERVICE
&&
1589 u
->type
!= UNIT_MOUNT
&&
1590 u
->type
!= UNIT_SWAP
)
1593 /* We set SOCK_NONBLOCK here so that we rather drop the
1594 * message then wait for plymouth */
1595 if ((fd
= socket(AF_UNIX
, SOCK_STREAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0)) < 0) {
1596 log_error("socket() failed: %m");
1601 sa
.sa
.sa_family
= AF_UNIX
;
1602 strncpy(sa
.un
.sun_path
+1, "/org/freedesktop/plymouthd", sizeof(sa
.un
.sun_path
)-1);
1603 if (connect(fd
, &sa
.sa
, offsetof(struct sockaddr_un
, sun_path
) + 1 + strlen(sa
.un
.sun_path
+1)) < 0) {
1605 if (errno
!= EPIPE
&&
1608 errno
!= ECONNREFUSED
&&
1609 errno
!= ECONNRESET
&&
1610 errno
!= ECONNABORTED
)
1611 log_error("connect() failed: %m");
1616 if (asprintf(&message
, "U\002%c%s%n", (int) (strlen(u
->id
) + 1), u
->id
, &n
) < 0) {
1617 log_error("Out of memory");
1622 if (write(fd
, message
, n
+ 1) != n
+ 1) {
1624 if (errno
!= EPIPE
&&
1627 errno
!= ECONNREFUSED
&&
1628 errno
!= ECONNRESET
&&
1629 errno
!= ECONNABORTED
)
1630 log_error("Failed to write Plymouth message: %m");
1637 close_nointr_nofail(fd
);
1642 void manager_dispatch_bus_name_owner_changed(
1645 const char* old_owner
,
1646 const char *new_owner
) {
1653 if (!(u
= hashmap_get(m
->watch_bus
, name
)))
1656 UNIT_VTABLE(u
)->bus_name_owner_change(u
, name
, old_owner
, new_owner
);
1659 void manager_dispatch_bus_query_pid_done(
1670 if (!(u
= hashmap_get(m
->watch_bus
, name
)))
1673 UNIT_VTABLE(u
)->bus_query_pid_done(u
, name
, pid
);
1676 int manager_open_serialization(Manager
*m
, FILE **_f
) {
1684 if (m
->running_as
== MANAGER_SYSTEM
)
1685 asprintf(&path
, "/run/systemd/dump-%lu-XXXXXX", (unsigned long) getpid());
1687 asprintf(&path
, "/tmp/systemd-dump-%lu-XXXXXX", (unsigned long) getpid());
1692 saved_umask
= umask(0077);
1693 fd
= mkostemp(path
, O_RDWR
|O_CLOEXEC
);
1703 log_debug("Serializing state to %s", path
);
1706 if (!(f
= fdopen(fd
, "w+")))
1714 int manager_serialize(Manager
*m
, FILE *f
, FDSet
*fds
) {
1726 fprintf(f
, "current-job-id=%i\n", m
->current_job_id
);
1727 fprintf(f
, "taint-usr=%s\n", yes_no(m
->taint_usr
));
1729 dual_timestamp_serialize(f
, "initrd-timestamp", &m
->initrd_timestamp
);
1730 dual_timestamp_serialize(f
, "startup-timestamp", &m
->startup_timestamp
);
1731 dual_timestamp_serialize(f
, "finish-timestamp", &m
->finish_timestamp
);
1735 HASHMAP_FOREACH_KEY(u
, t
, m
->units
, i
) {
1739 if (!unit_can_serialize(u
))
1746 if ((r
= unit_serialize(u
, f
, fds
)) < 0) {
1752 assert(m
->n_reloading
> 0);
1758 r
= bus_fdset_add_all(m
, fds
);
1765 int manager_deserialize(Manager
*m
, FILE *f
, FDSet
*fds
) {
1771 log_debug("Deserializing state...");
1776 char line
[LINE_MAX
], *l
;
1778 if (!fgets(line
, sizeof(line
), f
)) {
1793 if (startswith(l
, "current-job-id=")) {
1796 if (safe_atou32(l
+15, &id
) < 0)
1797 log_debug("Failed to parse current job id value %s", l
+15);
1799 m
->current_job_id
= MAX(m
->current_job_id
, id
);
1800 } else if (startswith(l
, "taint-usr=")) {
1803 if ((b
= parse_boolean(l
+10)) < 0)
1804 log_debug("Failed to parse taint /usr flag %s", l
+10);
1806 m
->taint_usr
= m
->taint_usr
|| b
;
1807 } else if (startswith(l
, "initrd-timestamp="))
1808 dual_timestamp_deserialize(l
+17, &m
->initrd_timestamp
);
1809 else if (startswith(l
, "startup-timestamp="))
1810 dual_timestamp_deserialize(l
+18, &m
->startup_timestamp
);
1811 else if (startswith(l
, "finish-timestamp="))
1812 dual_timestamp_deserialize(l
+17, &m
->finish_timestamp
);
1814 log_debug("Unknown serialization item '%s'", l
);
1819 char name
[UNIT_NAME_MAX
+2];
1822 if (!fgets(name
, sizeof(name
), f
)) {
1833 if ((r
= manager_load_unit(m
, strstrip(name
), NULL
, NULL
, &u
)) < 0)
1836 if ((r
= unit_deserialize(u
, f
, fds
)) < 0)
1846 assert(m
->n_reloading
> 0);
1852 int manager_reload(Manager
*m
) {
1859 if ((r
= manager_open_serialization(m
, &f
)) < 0)
1864 if (!(fds
= fdset_new())) {
1870 if ((r
= manager_serialize(m
, f
, fds
)) < 0) {
1875 if (fseeko(f
, 0, SEEK_SET
) < 0) {
1881 /* From here on there is no way back. */
1882 manager_clear_jobs_and_units(m
);
1883 manager_undo_generators(m
);
1885 /* Find new unit paths */
1886 lookup_paths_free(&m
->lookup_paths
);
1887 if ((q
= lookup_paths_init(&m
->lookup_paths
, m
->running_as
, true)) < 0)
1890 manager_run_generators(m
);
1892 manager_build_unit_path_cache(m
);
1894 /* First, enumerate what we can from all config files */
1895 if ((q
= manager_enumerate(m
)) < 0)
1898 /* Second, deserialize our stored data */
1899 if ((q
= manager_deserialize(m
, f
, fds
)) < 0)
1905 /* Third, fire things up! */
1906 if ((q
= manager_coldplug(m
)) < 0)
1909 assert(m
->n_reloading
> 0);
1922 bool manager_is_booting_or_shutting_down(Manager
*m
) {
1927 /* Is the initial job still around? */
1928 if (manager_get_job(m
, m
->default_unit_job_id
))
1931 /* Is there a job for the shutdown target? */
1932 u
= manager_get_unit(m
, SPECIAL_SHUTDOWN_TARGET
);
1939 void manager_reset_failed(Manager
*m
) {
1945 HASHMAP_FOREACH(u
, m
->units
, i
)
1946 unit_reset_failed(u
);
1949 bool manager_unit_pending_inactive(Manager
*m
, const char *name
) {
1955 /* Returns true if the unit is inactive or going down */
1956 if (!(u
= manager_get_unit(m
, name
)))
1959 return unit_pending_inactive(u
);
1962 void manager_check_finished(Manager
*m
) {
1963 char userspace
[FORMAT_TIMESPAN_MAX
], initrd
[FORMAT_TIMESPAN_MAX
], kernel
[FORMAT_TIMESPAN_MAX
], sum
[FORMAT_TIMESPAN_MAX
];
1964 usec_t kernel_usec
, initrd_usec
, userspace_usec
, total_usec
;
1968 if (hashmap_size(m
->jobs
) > 0)
1971 /* Notify Type=idle units that we are done now */
1972 close_pipe(m
->idle_pipe
);
1974 if (dual_timestamp_is_set(&m
->finish_timestamp
))
1977 dual_timestamp_get(&m
->finish_timestamp
);
1979 if (m
->running_as
== MANAGER_SYSTEM
&& detect_container(NULL
) <= 0) {
1981 userspace_usec
= m
->finish_timestamp
.monotonic
- m
->startup_timestamp
.monotonic
;
1982 total_usec
= m
->finish_timestamp
.monotonic
;
1984 if (dual_timestamp_is_set(&m
->initrd_timestamp
)) {
1986 kernel_usec
= m
->initrd_timestamp
.monotonic
;
1987 initrd_usec
= m
->startup_timestamp
.monotonic
- m
->initrd_timestamp
.monotonic
;
1989 log_info("Startup finished in %s (kernel) + %s (initrd) + %s (userspace) = %s.",
1990 format_timespan(kernel
, sizeof(kernel
), kernel_usec
),
1991 format_timespan(initrd
, sizeof(initrd
), initrd_usec
),
1992 format_timespan(userspace
, sizeof(userspace
), userspace_usec
),
1993 format_timespan(sum
, sizeof(sum
), total_usec
));
1995 kernel_usec
= m
->startup_timestamp
.monotonic
;
1998 log_info("Startup finished in %s (kernel) + %s (userspace) = %s.",
1999 format_timespan(kernel
, sizeof(kernel
), kernel_usec
),
2000 format_timespan(userspace
, sizeof(userspace
), userspace_usec
),
2001 format_timespan(sum
, sizeof(sum
), total_usec
));
2004 userspace_usec
= initrd_usec
= kernel_usec
= 0;
2005 total_usec
= m
->finish_timestamp
.monotonic
- m
->startup_timestamp
.monotonic
;
2007 log_debug("Startup finished in %s.",
2008 format_timespan(sum
, sizeof(sum
), total_usec
));
2011 bus_broadcast_finished(m
, kernel_usec
, initrd_usec
, userspace_usec
, total_usec
);
2014 "READY=1\nSTATUS=Startup finished in %s.",
2015 format_timespan(sum
, sizeof(sum
), total_usec
));
2018 void manager_run_generators(Manager
*m
) {
2020 const char *generator_path
;
2021 const char *argv
[3];
2026 generator_path
= m
->running_as
== MANAGER_SYSTEM
? SYSTEM_GENERATOR_PATH
: USER_GENERATOR_PATH
;
2027 if (!(d
= opendir(generator_path
))) {
2029 if (errno
== ENOENT
)
2032 log_error("Failed to enumerate generator directory: %m");
2036 if (!m
->generator_unit_path
) {
2038 char user_path
[] = "/tmp/systemd-generator-XXXXXX";
2040 if (m
->running_as
== MANAGER_SYSTEM
&& getpid() == 1) {
2041 p
= "/run/systemd/generator";
2043 if (mkdir_p(p
, 0755) < 0) {
2044 log_error("Failed to create generator directory: %m");
2049 if (!(p
= mkdtemp(user_path
))) {
2050 log_error("Failed to create generator directory: %m");
2055 if (!(m
->generator_unit_path
= strdup(p
))) {
2056 log_error("Failed to allocate generator unit path.");
2061 argv
[0] = NULL
; /* Leave this empty, execute_directory() will fill something in */
2062 argv
[1] = m
->generator_unit_path
;
2066 execute_directory(generator_path
, d
, (char**) argv
);
2069 if (rmdir(m
->generator_unit_path
) >= 0) {
2070 /* Uh? we were able to remove this dir? I guess that
2071 * means the directory was empty, hence let's shortcut
2074 free(m
->generator_unit_path
);
2075 m
->generator_unit_path
= NULL
;
2079 if (!strv_find(m
->lookup_paths
.unit_path
, m
->generator_unit_path
)) {
2082 if (!(l
= strv_append(m
->lookup_paths
.unit_path
, m
->generator_unit_path
))) {
2083 log_error("Failed to add generator directory to unit search path: %m");
2087 strv_free(m
->lookup_paths
.unit_path
);
2088 m
->lookup_paths
.unit_path
= l
;
2090 log_debug("Added generator unit path %s to search path.", m
->generator_unit_path
);
2098 void manager_undo_generators(Manager
*m
) {
2101 if (!m
->generator_unit_path
)
2104 strv_remove(m
->lookup_paths
.unit_path
, m
->generator_unit_path
);
2105 rm_rf(m
->generator_unit_path
, false, true, false);
2107 free(m
->generator_unit_path
);
2108 m
->generator_unit_path
= NULL
;
2111 int manager_set_default_controllers(Manager
*m
, char **controllers
) {
2116 l
= strv_copy(controllers
);
2120 strv_free(m
->default_controllers
);
2121 m
->default_controllers
= l
;
2123 cg_shorten_controllers(m
->default_controllers
);
2128 void manager_recheck_journal(Manager
*m
) {
2133 if (m
->running_as
!= MANAGER_SYSTEM
)
2136 u
= manager_get_unit(m
, SPECIAL_JOURNALD_SOCKET
);
2137 if (u
&& SOCKET(u
)->state
!= SOCKET_RUNNING
) {
2138 log_close_journal();
2142 u
= manager_get_unit(m
, SPECIAL_JOURNALD_SERVICE
);
2143 if (u
&& SERVICE(u
)->state
!= SERVICE_RUNNING
) {
2144 log_close_journal();
2148 /* Hmm, OK, so the socket is fully up and the service is up
2149 * too, then let's make use of the thing. */
2153 void manager_set_show_status(Manager
*m
, bool b
) {
2156 if (m
->running_as
!= MANAGER_SYSTEM
)
2162 touch("/run/systemd/show-status");
2164 unlink("/run/systemd/show-status");
2167 bool manager_get_show_status(Manager
*m
) {
2170 if (m
->running_as
!= MANAGER_SYSTEM
)
2176 /* If Plymouth is running make sure we show the status, so
2177 * that there's something nice to see when people press Esc */
2179 return plymouth_running();
2182 static const char* const manager_running_as_table
[_MANAGER_RUNNING_AS_MAX
] = {
2183 [MANAGER_SYSTEM
] = "system",
2184 [MANAGER_USER
] = "user"
2187 DEFINE_STRING_TABLE_LOOKUP(manager_running_as
, ManagerRunningAs
);