]>
Commit | Line | Data |
---|---|---|
d6c9574f | 1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
60918275 | 2 | |
a7334b09 LP |
3 | /*** |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2010 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
5430f7f2 LP |
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 | |
a7334b09 LP |
11 | (at your option) any later version. |
12 | ||
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 | |
5430f7f2 | 16 | Lesser General Public License for more details. |
a7334b09 | 17 | |
5430f7f2 | 18 | You should have received a copy of the GNU Lesser General Public License |
a7334b09 LP |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. |
20 | ***/ | |
21 | ||
60918275 LP |
22 | #include <assert.h> |
23 | #include <errno.h> | |
87d1515d | 24 | #include <string.h> |
9152c765 LP |
25 | #include <sys/epoll.h> |
26 | #include <signal.h> | |
27 | #include <sys/signalfd.h> | |
28 | #include <sys/wait.h> | |
29 | #include <unistd.h> | |
30 | #include <sys/poll.h> | |
e1414003 LP |
31 | #include <sys/reboot.h> |
32 | #include <sys/ioctl.h> | |
33 | #include <linux/kd.h> | |
80876c20 LP |
34 | #include <termios.h> |
35 | #include <fcntl.h> | |
a16e1123 LP |
36 | #include <sys/types.h> |
37 | #include <sys/stat.h> | |
fe51822e | 38 | #include <dirent.h> |
8742514c | 39 | #include <sys/timerfd.h> |
830f6caa LP |
40 | |
41 | #ifdef HAVE_AUDIT | |
4927fcae | 42 | #include <libaudit.h> |
830f6caa | 43 | #endif |
60918275 | 44 | |
877d54e9 LP |
45 | #include "systemd/sd-daemon.h" |
46 | #include "systemd/sd-id128.h" | |
47 | #include "systemd/sd-messages.h" | |
81527be1 | 48 | |
60918275 | 49 | #include "manager.h" |
75778e21 | 50 | #include "transaction.h" |
60918275 LP |
51 | #include "hashmap.h" |
52 | #include "macro.h" | |
53 | #include "strv.h" | |
16354eff | 54 | #include "log.h" |
2a987ee8 | 55 | #include "util.h" |
49e942b2 | 56 | #include "mkdir.h" |
ea430986 | 57 | #include "ratelimit.h" |
8e274523 LP |
58 | #include "cgroup.h" |
59 | #include "mount-setup.h" | |
9e2f7c11 | 60 | #include "unit-name.h" |
4139c1b2 LP |
61 | #include "dbus-unit.h" |
62 | #include "dbus-job.h" | |
1137a57c | 63 | #include "missing.h" |
84e3543e | 64 | #include "path-lookup.h" |
514f4ef5 | 65 | #include "special.h" |
398ef8ba | 66 | #include "bus-errors.h" |
d06dacd0 | 67 | #include "exit-status.h" |
5dc4c17f | 68 | #include "virt.h" |
e96d6be7 | 69 | #include "watchdog.h" |
b59e2465 | 70 | #include "cgroup-util.h" |
9eb977db | 71 | #include "path-util.h" |
c1165f82 | 72 | #include "audit-fd.h" |
2e3d0692 | 73 | #include "efivars.h" |
60918275 | 74 | |
701cc384 LP |
75 | /* As soon as 16 units are in our GC queue, make sure to run a gc sweep */ |
76 | #define GC_QUEUE_ENTRIES_MAX 16 | |
77 | ||
78 | /* As soon as 5s passed since a unit was added to our GC queue, make sure to run a gc sweep */ | |
94b6dfa2 | 79 | #define GC_QUEUE_USEC_MAX (10*USEC_PER_SEC) |
701cc384 | 80 | |
8c47c732 | 81 | /* Where clients shall send notification messages to */ |
29252e9e | 82 | #define NOTIFY_SOCKET "@/org/freedesktop/systemd1/notify" |
8c47c732 LP |
83 | |
84 | static int manager_setup_notify(Manager *m) { | |
85 | union { | |
86 | struct sockaddr sa; | |
87 | struct sockaddr_un un; | |
88 | } sa; | |
89 | struct epoll_event ev; | |
29252e9e | 90 | int one = 1; |
8c47c732 LP |
91 | |
92 | assert(m); | |
93 | ||
94 | m->notify_watch.type = WATCH_NOTIFY; | |
29252e9e LP |
95 | m->notify_watch.fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); |
96 | if (m->notify_watch.fd < 0) { | |
8c47c732 LP |
97 | log_error("Failed to allocate notification socket: %m"); |
98 | return -errno; | |
99 | } | |
100 | ||
101 | zero(sa); | |
102 | sa.sa.sa_family = AF_UNIX; | |
103 | ||
31f92a7d | 104 | if (getpid() != 1 || detect_container(NULL) > 0) |
29252e9e LP |
105 | snprintf(sa.un.sun_path, sizeof(sa.un.sun_path), NOTIFY_SOCKET "/%llu", random_ull()); |
106 | else | |
107 | strncpy(sa.un.sun_path, NOTIFY_SOCKET, sizeof(sa.un.sun_path)); | |
8c47c732 | 108 | |
29252e9e | 109 | sa.un.sun_path[0] = 0; |
1d6702e8 | 110 | |
29252e9e | 111 | if (bind(m->notify_watch.fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + 1 + strlen(sa.un.sun_path+1)) < 0) { |
8c47c732 LP |
112 | log_error("bind() failed: %m"); |
113 | return -errno; | |
114 | } | |
115 | ||
116 | if (setsockopt(m->notify_watch.fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)) < 0) { | |
117 | log_error("SO_PASSCRED failed: %m"); | |
118 | return -errno; | |
119 | } | |
120 | ||
121 | zero(ev); | |
122 | ev.events = EPOLLIN; | |
123 | ev.data.ptr = &m->notify_watch; | |
124 | ||
8742514c LP |
125 | if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->notify_watch.fd, &ev) < 0) { |
126 | log_error("Failed to add timer change fd to epoll: %m"); | |
8c47c732 | 127 | return -errno; |
8742514c | 128 | } |
8c47c732 | 129 | |
29252e9e LP |
130 | sa.un.sun_path[0] = '@'; |
131 | m->notify_socket = strdup(sa.un.sun_path); | |
132 | if (!m->notify_socket) | |
8742514c | 133 | return log_oom(); |
8c47c732 | 134 | |
b2bb3dbe LP |
135 | log_debug("Using notification socket %s", m->notify_socket); |
136 | ||
8c47c732 LP |
137 | return 0; |
138 | } | |
139 | ||
8742514c LP |
140 | static int manager_setup_time_change(Manager *m) { |
141 | struct epoll_event ev; | |
142 | struct itimerspec its; | |
143 | ||
144 | assert(m); | |
145 | assert(m->time_change_watch.type == WATCH_INVALID); | |
146 | ||
147 | /* Uses TFD_TIMER_CANCEL_ON_SET to get notifications whenever | |
148 | * CLOCK_REALTIME makes a jump relative to CLOCK_MONOTONIC */ | |
149 | ||
150 | m->time_change_watch.type = WATCH_TIME_CHANGE; | |
151 | m->time_change_watch.fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK|TFD_CLOEXEC); | |
152 | if (m->time_change_watch.fd < 0) { | |
153 | log_error("Failed to create timerfd: %m"); | |
154 | return -errno; | |
155 | } | |
156 | ||
157 | zero(its); | |
8a3a1704 LP |
158 | |
159 | /* We only care for the cancellation event, hence we set the | |
160 | * timeout to the latest possible value. */ | |
d6a195a3 ZJS |
161 | assert_cc(sizeof(time_t) == sizeof(TIME_T_MAX)); |
162 | its.it_value.tv_sec = TIME_T_MAX; | |
8a3a1704 | 163 | |
8742514c LP |
164 | if (timerfd_settime(m->time_change_watch.fd, TFD_TIMER_ABSTIME|TFD_TIMER_CANCEL_ON_SET, &its, NULL) < 0) { |
165 | log_debug("Failed to set up TFD_TIMER_CANCEL_ON_SET, ignoring: %m"); | |
166 | close_nointr_nofail(m->time_change_watch.fd); | |
167 | watch_init(&m->time_change_watch); | |
168 | return 0; | |
169 | } | |
170 | ||
171 | zero(ev); | |
172 | ev.events = EPOLLIN; | |
173 | ev.data.ptr = &m->time_change_watch; | |
174 | ||
175 | if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->time_change_watch.fd, &ev) < 0) { | |
176 | log_error("Failed to add timer change fd to epoll: %m"); | |
177 | return -errno; | |
178 | } | |
179 | ||
180 | log_debug("Set up TFD_TIMER_CANCEL_ON_SET timerfd."); | |
181 | ||
182 | return 0; | |
183 | } | |
184 | ||
80876c20 | 185 | static int enable_special_signals(Manager *m) { |
4466ee6a | 186 | int fd; |
80876c20 LP |
187 | |
188 | assert(m); | |
189 | ||
a41b539e | 190 | /* Enable that we get SIGINT on control-alt-del. In containers |
c9999773 LP |
191 | * this will fail with EPERM (older) or EINVAL (newer), so |
192 | * ignore that. */ | |
193 | if (reboot(RB_DISABLE_CAD) < 0 && errno != EPERM && errno != EINVAL) | |
80876c20 LP |
194 | log_warning("Failed to enable ctrl-alt-del handling: %m"); |
195 | ||
a41b539e LP |
196 | fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC); |
197 | if (fd < 0) { | |
198 | /* Support systems without virtual console */ | |
199 | if (fd != -ENOENT) | |
200 | log_warning("Failed to open /dev/tty0: %m"); | |
201 | } else { | |
80876c20 LP |
202 | /* Enable that we get SIGWINCH on kbrequest */ |
203 | if (ioctl(fd, KDSIGACCEPT, SIGWINCH) < 0) | |
204 | log_warning("Failed to enable kbrequest handling: %s", strerror(errno)); | |
205 | ||
206 | close_nointr_nofail(fd); | |
207 | } | |
208 | ||
209 | return 0; | |
210 | } | |
211 | ||
ce578209 | 212 | static int manager_setup_signals(Manager *m) { |
9152c765 LP |
213 | sigset_t mask; |
214 | struct epoll_event ev; | |
57c0c30e | 215 | struct sigaction sa; |
60918275 | 216 | |
ce578209 LP |
217 | assert(m); |
218 | ||
57c0c30e LP |
219 | /* We are not interested in SIGSTOP and friends. */ |
220 | zero(sa); | |
221 | sa.sa_handler = SIG_DFL; | |
222 | sa.sa_flags = SA_NOCLDSTOP|SA_RESTART; | |
223 | assert_se(sigaction(SIGCHLD, &sa, NULL) == 0); | |
224 | ||
ce578209 | 225 | assert_se(sigemptyset(&mask) == 0); |
7d793605 LP |
226 | |
227 | sigset_add_many(&mask, | |
228 | SIGCHLD, /* Child died */ | |
229 | SIGTERM, /* Reexecute daemon */ | |
230 | SIGHUP, /* Reload configuration */ | |
231 | SIGUSR1, /* systemd/upstart: reconnect to D-Bus */ | |
232 | SIGUSR2, /* systemd: dump status */ | |
233 | SIGINT, /* Kernel sends us this on control-alt-del */ | |
234 | SIGWINCH, /* Kernel sends us this on kbrequest (alt-arrowup) */ | |
235 | SIGPWR, /* Some kernel drivers and upsd send us this on power failure */ | |
236 | SIGRTMIN+0, /* systemd: start default.target */ | |
0003d1ab | 237 | SIGRTMIN+1, /* systemd: isolate rescue.target */ |
7d793605 LP |
238 | SIGRTMIN+2, /* systemd: isolate emergency.target */ |
239 | SIGRTMIN+3, /* systemd: start halt.target */ | |
240 | SIGRTMIN+4, /* systemd: start poweroff.target */ | |
241 | SIGRTMIN+5, /* systemd: start reboot.target */ | |
0003d1ab LP |
242 | SIGRTMIN+6, /* systemd: start kexec.target */ |
243 | SIGRTMIN+13, /* systemd: Immediate halt */ | |
244 | SIGRTMIN+14, /* systemd: Immediate poweroff */ | |
245 | SIGRTMIN+15, /* systemd: Immediate reboot */ | |
246 | SIGRTMIN+16, /* systemd: Immediate kexec */ | |
0658666b LP |
247 | SIGRTMIN+20, /* systemd: enable status messages */ |
248 | SIGRTMIN+21, /* systemd: disable status messages */ | |
253ee27a LP |
249 | SIGRTMIN+22, /* systemd: set log level to LOG_DEBUG */ |
250 | SIGRTMIN+23, /* systemd: set log level to LOG_INFO */ | |
600b704e | 251 | SIGRTMIN+24, /* systemd: Immediate exit (--user only) */ |
4cfa2c99 | 252 | SIGRTMIN+26, /* systemd: set log target to journal-or-kmsg */ |
253ee27a LP |
253 | SIGRTMIN+27, /* systemd: set log target to console */ |
254 | SIGRTMIN+28, /* systemd: set log target to kmsg */ | |
255 | SIGRTMIN+29, /* systemd: set log target to syslog-or-kmsg */ | |
7d793605 | 256 | -1); |
ce578209 LP |
257 | assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0); |
258 | ||
ef734fd6 | 259 | m->signal_watch.type = WATCH_SIGNAL; |
8742514c LP |
260 | m->signal_watch.fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC); |
261 | if (m->signal_watch.fd < 0) | |
ce578209 LP |
262 | return -errno; |
263 | ||
264 | zero(ev); | |
265 | ev.events = EPOLLIN; | |
266 | ev.data.ptr = &m->signal_watch; | |
267 | ||
268 | if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->signal_watch.fd, &ev) < 0) | |
269 | return -errno; | |
270 | ||
67445f4e | 271 | if (m->running_as == SYSTEMD_SYSTEM) |
80876c20 | 272 | return enable_special_signals(m); |
e1414003 | 273 | |
ce578209 LP |
274 | return 0; |
275 | } | |
276 | ||
71ecc858 LP |
277 | static void manager_strip_environment(Manager *m) { |
278 | assert(m); | |
279 | ||
280 | /* Remove variables from the inherited set that are part of | |
281 | * the container interface: | |
282 | * http://www.freedesktop.org/wiki/Software/systemd/ContainerInterface */ | |
283 | strv_remove_prefix(m->environment, "container="); | |
284 | strv_remove_prefix(m->environment, "container_"); | |
285 | ||
286 | /* Remove variables from the inherited set that are part of | |
287 | * the initrd interface: | |
288 | * http://www.freedesktop.org/wiki/Software/systemd/InitrdInterface */ | |
289 | strv_remove_prefix(m->environment, "RD_"); | |
290 | } | |
291 | ||
67445f4e | 292 | int manager_new(SystemdRunningAs running_as, Manager **_m) { |
ce578209 | 293 | Manager *m; |
8e274523 LP |
294 | int r = -ENOMEM; |
295 | ||
296 | assert(_m); | |
a5dab5ce | 297 | assert(running_as >= 0); |
67445f4e | 298 | assert(running_as < _SYSTEMD_RUNNING_AS_MAX); |
ce578209 | 299 | |
915b3753 LP |
300 | m = new0(Manager, 1); |
301 | if (!m) | |
8e274523 | 302 | return -ENOMEM; |
60918275 | 303 | |
915b3753 | 304 | dual_timestamp_get(&m->userspace_timestamp); |
cae0c5e0 | 305 | dual_timestamp_from_monotonic(&m->kernel_timestamp, 0); |
2e3d0692 | 306 | efi_get_boot_timestamps(&m->userspace_timestamp, &m->firmware_timestamp, &m->loader_timestamp); |
e537352b | 307 | |
a5dab5ce | 308 | m->running_as = running_as; |
cbd37330 | 309 | m->name_data_slot = m->conn_data_slot = m->subscribed_data_slot = -1; |
a16e1123 | 310 | m->exit_code = _MANAGER_EXIT_CODE_INVALID; |
33be102a | 311 | m->pin_cgroupfs_fd = -1; |
f2b68789 | 312 | m->idle_pipe[0] = m->idle_pipe[1] = -1; |
80876c20 | 313 | |
8742514c LP |
314 | watch_init(&m->signal_watch); |
315 | watch_init(&m->mount_watch); | |
316 | watch_init(&m->swap_watch); | |
317 | watch_init(&m->udev_watch); | |
318 | watch_init(&m->time_change_watch); | |
319 | ||
320 | m->epoll_fd = m->dev_autofs_fd = -1; | |
ea430986 | 321 | m->current_job_id = 1; /* start as id #1, so that we can leave #0 around as "null-like" value */ |
9152c765 | 322 | |
71ecc858 LP |
323 | m->environment = strv_copy(environ); |
324 | if (!m->environment) | |
1137a57c LP |
325 | goto fail; |
326 | ||
71ecc858 LP |
327 | manager_strip_environment(m); |
328 | ||
67445f4e | 329 | if (running_as == SYSTEMD_SYSTEM) { |
88f06645 LP |
330 | m->default_controllers = strv_new("cpu", NULL); |
331 | if (!m->default_controllers) | |
332 | goto fail; | |
333 | } | |
06d4c99a | 334 | |
87f0e418 | 335 | if (!(m->units = hashmap_new(string_hash_func, string_compare_func))) |
60918275 LP |
336 | goto fail; |
337 | ||
338 | if (!(m->jobs = hashmap_new(trivial_hash_func, trivial_compare_func))) | |
339 | goto fail; | |
340 | ||
9152c765 LP |
341 | if (!(m->watch_pids = hashmap_new(trivial_hash_func, trivial_compare_func))) |
342 | goto fail; | |
343 | ||
8e274523 LP |
344 | if (!(m->cgroup_bondings = hashmap_new(string_hash_func, string_compare_func))) |
345 | goto fail; | |
346 | ||
05e343b7 LP |
347 | if (!(m->watch_bus = hashmap_new(string_hash_func, string_compare_func))) |
348 | goto fail; | |
349 | ||
8742514c LP |
350 | m->epoll_fd = epoll_create1(EPOLL_CLOEXEC); |
351 | if (m->epoll_fd < 0) | |
352 | goto fail; | |
353 | ||
354 | r = manager_setup_signals(m); | |
355 | if (r < 0) | |
9152c765 LP |
356 | goto fail; |
357 | ||
8742514c LP |
358 | r = manager_setup_cgroup(m); |
359 | if (r < 0) | |
8e274523 LP |
360 | goto fail; |
361 | ||
8742514c LP |
362 | r = manager_setup_notify(m); |
363 | if (r < 0) | |
9152c765 LP |
364 | goto fail; |
365 | ||
8742514c LP |
366 | r = manager_setup_time_change(m); |
367 | if (r < 0) | |
8c47c732 LP |
368 | goto fail; |
369 | ||
f278026d | 370 | /* Try to connect to the busses, if possible. */ |
8742514c LP |
371 | r = bus_init(m, running_as != SYSTEMD_SYSTEM); |
372 | if (r < 0) | |
ea430986 LP |
373 | goto fail; |
374 | ||
72bc8d00 LP |
375 | m->taint_usr = dir_is_empty("/usr") > 0; |
376 | ||
8e274523 LP |
377 | *_m = m; |
378 | return 0; | |
60918275 LP |
379 | |
380 | fail: | |
381 | manager_free(m); | |
8e274523 | 382 | return r; |
60918275 LP |
383 | } |
384 | ||
23a177ef | 385 | static unsigned manager_dispatch_cleanup_queue(Manager *m) { |
595ed347 | 386 | Unit *u; |
23a177ef LP |
387 | unsigned n = 0; |
388 | ||
389 | assert(m); | |
390 | ||
595ed347 MS |
391 | while ((u = m->cleanup_queue)) { |
392 | assert(u->in_cleanup_queue); | |
23a177ef | 393 | |
595ed347 | 394 | unit_free(u); |
23a177ef LP |
395 | n++; |
396 | } | |
397 | ||
398 | return n; | |
399 | } | |
400 | ||
eced69b3 | 401 | enum { |
35b8ca3a | 402 | GC_OFFSET_IN_PATH, /* This one is on the path we were traveling */ |
eced69b3 LP |
403 | GC_OFFSET_UNSURE, /* No clue */ |
404 | GC_OFFSET_GOOD, /* We still need this unit */ | |
405 | GC_OFFSET_BAD, /* We don't need this unit anymore */ | |
406 | _GC_OFFSET_MAX | |
407 | }; | |
408 | ||
409 | static void unit_gc_sweep(Unit *u, unsigned gc_marker) { | |
701cc384 LP |
410 | Iterator i; |
411 | Unit *other; | |
eced69b3 | 412 | bool is_bad; |
701cc384 LP |
413 | |
414 | assert(u); | |
415 | ||
ac155bb8 MS |
416 | if (u->gc_marker == gc_marker + GC_OFFSET_GOOD || |
417 | u->gc_marker == gc_marker + GC_OFFSET_BAD || | |
418 | u->gc_marker == gc_marker + GC_OFFSET_IN_PATH) | |
701cc384 LP |
419 | return; |
420 | ||
ac155bb8 | 421 | if (u->in_cleanup_queue) |
701cc384 LP |
422 | goto bad; |
423 | ||
424 | if (unit_check_gc(u)) | |
425 | goto good; | |
426 | ||
ac155bb8 | 427 | u->gc_marker = gc_marker + GC_OFFSET_IN_PATH; |
eced69b3 LP |
428 | |
429 | is_bad = true; | |
430 | ||
ac155bb8 | 431 | SET_FOREACH(other, u->dependencies[UNIT_REFERENCED_BY], i) { |
701cc384 LP |
432 | unit_gc_sweep(other, gc_marker); |
433 | ||
ac155bb8 | 434 | if (other->gc_marker == gc_marker + GC_OFFSET_GOOD) |
701cc384 | 435 | goto good; |
eced69b3 | 436 | |
ac155bb8 | 437 | if (other->gc_marker != gc_marker + GC_OFFSET_BAD) |
eced69b3 | 438 | is_bad = false; |
701cc384 LP |
439 | } |
440 | ||
eced69b3 LP |
441 | if (is_bad) |
442 | goto bad; | |
443 | ||
444 | /* We were unable to find anything out about this entry, so | |
445 | * let's investigate it later */ | |
ac155bb8 | 446 | u->gc_marker = gc_marker + GC_OFFSET_UNSURE; |
eced69b3 LP |
447 | unit_add_to_gc_queue(u); |
448 | return; | |
449 | ||
701cc384 | 450 | bad: |
eced69b3 LP |
451 | /* We definitely know that this one is not useful anymore, so |
452 | * let's mark it for deletion */ | |
ac155bb8 | 453 | u->gc_marker = gc_marker + GC_OFFSET_BAD; |
eced69b3 | 454 | unit_add_to_cleanup_queue(u); |
701cc384 LP |
455 | return; |
456 | ||
457 | good: | |
ac155bb8 | 458 | u->gc_marker = gc_marker + GC_OFFSET_GOOD; |
701cc384 LP |
459 | } |
460 | ||
461 | static unsigned manager_dispatch_gc_queue(Manager *m) { | |
595ed347 | 462 | Unit *u; |
701cc384 | 463 | unsigned n = 0; |
eced69b3 | 464 | unsigned gc_marker; |
701cc384 LP |
465 | |
466 | assert(m); | |
467 | ||
468 | if ((m->n_in_gc_queue < GC_QUEUE_ENTRIES_MAX) && | |
469 | (m->gc_queue_timestamp <= 0 || | |
470 | (m->gc_queue_timestamp + GC_QUEUE_USEC_MAX) > now(CLOCK_MONOTONIC))) | |
471 | return 0; | |
472 | ||
473 | log_debug("Running GC..."); | |
474 | ||
eced69b3 LP |
475 | m->gc_marker += _GC_OFFSET_MAX; |
476 | if (m->gc_marker + _GC_OFFSET_MAX <= _GC_OFFSET_MAX) | |
c9c0cadb | 477 | m->gc_marker = 1; |
701cc384 | 478 | |
eced69b3 LP |
479 | gc_marker = m->gc_marker; |
480 | ||
595ed347 MS |
481 | while ((u = m->gc_queue)) { |
482 | assert(u->in_gc_queue); | |
701cc384 | 483 | |
595ed347 | 484 | unit_gc_sweep(u, gc_marker); |
eced69b3 | 485 | |
595ed347 MS |
486 | LIST_REMOVE(Unit, gc_queue, m->gc_queue, u); |
487 | u->in_gc_queue = false; | |
701cc384 LP |
488 | |
489 | n++; | |
490 | ||
595ed347 MS |
491 | if (u->gc_marker == gc_marker + GC_OFFSET_BAD || |
492 | u->gc_marker == gc_marker + GC_OFFSET_UNSURE) { | |
66870f90 | 493 | log_debug_unit(u->id, "Collecting %s", u->id); |
595ed347 MS |
494 | u->gc_marker = gc_marker + GC_OFFSET_BAD; |
495 | unit_add_to_cleanup_queue(u); | |
701cc384 LP |
496 | } |
497 | } | |
498 | ||
499 | m->n_in_gc_queue = 0; | |
500 | m->gc_queue_timestamp = 0; | |
501 | ||
502 | return n; | |
503 | } | |
504 | ||
a16e1123 | 505 | static void manager_clear_jobs_and_units(Manager *m) { |
a16e1123 | 506 | Unit *u; |
60918275 LP |
507 | |
508 | assert(m); | |
509 | ||
87f0e418 LP |
510 | while ((u = hashmap_first(m->units))) |
511 | unit_free(u); | |
964e0949 LP |
512 | |
513 | manager_dispatch_cleanup_queue(m); | |
514 | ||
515 | assert(!m->load_queue); | |
516 | assert(!m->run_queue); | |
517 | assert(!m->dbus_unit_queue); | |
518 | assert(!m->dbus_job_queue); | |
519 | assert(!m->cleanup_queue); | |
520 | assert(!m->gc_queue); | |
521 | ||
964e0949 LP |
522 | assert(hashmap_isempty(m->jobs)); |
523 | assert(hashmap_isempty(m->units)); | |
a16e1123 LP |
524 | } |
525 | ||
526 | void manager_free(Manager *m) { | |
527 | UnitType c; | |
c93ff2e9 | 528 | int i; |
87f0e418 | 529 | |
a16e1123 LP |
530 | assert(m); |
531 | ||
532 | manager_clear_jobs_and_units(m); | |
23a177ef | 533 | |
7824bbeb LP |
534 | for (c = 0; c < _UNIT_TYPE_MAX; c++) |
535 | if (unit_vtable[c]->shutdown) | |
536 | unit_vtable[c]->shutdown(m); | |
537 | ||
a16e1123 LP |
538 | /* If we reexecute ourselves, we keep the root cgroup |
539 | * around */ | |
c6c18be3 | 540 | manager_shutdown_cgroup(m, m->exit_code != MANAGER_REEXECUTE); |
8e274523 | 541 | |
5a1e9937 LP |
542 | manager_undo_generators(m); |
543 | ||
5e8d1c9a | 544 | bus_done(m); |
ea430986 | 545 | |
87f0e418 | 546 | hashmap_free(m->units); |
60918275 | 547 | hashmap_free(m->jobs); |
9152c765 | 548 | hashmap_free(m->watch_pids); |
05e343b7 | 549 | hashmap_free(m->watch_bus); |
9152c765 LP |
550 | |
551 | if (m->epoll_fd >= 0) | |
a16e1123 | 552 | close_nointr_nofail(m->epoll_fd); |
acbb0225 | 553 | if (m->signal_watch.fd >= 0) |
a16e1123 | 554 | close_nointr_nofail(m->signal_watch.fd); |
8c47c732 LP |
555 | if (m->notify_watch.fd >= 0) |
556 | close_nointr_nofail(m->notify_watch.fd); | |
8742514c LP |
557 | if (m->time_change_watch.fd >= 0) |
558 | close_nointr_nofail(m->time_change_watch.fd); | |
60918275 | 559 | |
c952c6ec LP |
560 | free(m->notify_socket); |
561 | ||
84e3543e | 562 | lookup_paths_free(&m->lookup_paths); |
1137a57c | 563 | strv_free(m->environment); |
036643a2 | 564 | |
06d4c99a LP |
565 | strv_free(m->default_controllers); |
566 | ||
8e274523 | 567 | hashmap_free(m->cgroup_bondings); |
c6c18be3 | 568 | set_free_free(m->unit_path_cache); |
33be102a | 569 | |
f2b68789 LP |
570 | close_pipe(m->idle_pipe); |
571 | ||
664f88a7 LP |
572 | free(m->switch_root); |
573 | free(m->switch_root_init); | |
574 | ||
c93ff2e9 FC |
575 | for (i = 0; i < RLIMIT_NLIMITS; i++) |
576 | free(m->rlimit[i]); | |
577 | ||
60918275 LP |
578 | free(m); |
579 | } | |
580 | ||
a16e1123 LP |
581 | int manager_enumerate(Manager *m) { |
582 | int r = 0, q; | |
f50e0a01 | 583 | UnitType c; |
f50e0a01 LP |
584 | |
585 | assert(m); | |
586 | ||
a16e1123 LP |
587 | /* Let's ask every type to load all units from disk/kernel |
588 | * that it might know */ | |
f50e0a01 LP |
589 | for (c = 0; c < _UNIT_TYPE_MAX; c++) |
590 | if (unit_vtable[c]->enumerate) | |
a16e1123 LP |
591 | if ((q = unit_vtable[c]->enumerate(m)) < 0) |
592 | r = q; | |
f50e0a01 LP |
593 | |
594 | manager_dispatch_load_queue(m); | |
a16e1123 LP |
595 | return r; |
596 | } | |
597 | ||
598 | int manager_coldplug(Manager *m) { | |
599 | int r = 0, q; | |
600 | Iterator i; | |
601 | Unit *u; | |
602 | char *k; | |
603 | ||
604 | assert(m); | |
f50e0a01 LP |
605 | |
606 | /* Then, let's set up their initial state. */ | |
607 | HASHMAP_FOREACH_KEY(u, k, m->units, i) { | |
608 | ||
609 | /* ignore aliases */ | |
ac155bb8 | 610 | if (u->id != k) |
f50e0a01 LP |
611 | continue; |
612 | ||
cca098b0 LP |
613 | if ((q = unit_coldplug(u)) < 0) |
614 | r = q; | |
f50e0a01 LP |
615 | } |
616 | ||
a16e1123 LP |
617 | return r; |
618 | } | |
619 | ||
fe51822e LP |
620 | static void manager_build_unit_path_cache(Manager *m) { |
621 | char **i; | |
622 | DIR *d = NULL; | |
623 | int r; | |
624 | ||
625 | assert(m); | |
626 | ||
627 | set_free_free(m->unit_path_cache); | |
628 | ||
629 | if (!(m->unit_path_cache = set_new(string_hash_func, string_compare_func))) { | |
630 | log_error("Failed to allocate unit path cache."); | |
631 | return; | |
632 | } | |
633 | ||
634 | /* This simply builds a list of files we know exist, so that | |
635 | * we don't always have to go to disk */ | |
636 | ||
637 | STRV_FOREACH(i, m->lookup_paths.unit_path) { | |
638 | struct dirent *de; | |
639 | ||
bd0af849 ZJS |
640 | d = opendir(*i); |
641 | if (!d) { | |
fe51822e LP |
642 | log_error("Failed to open directory: %m"); |
643 | continue; | |
644 | } | |
645 | ||
646 | while ((de = readdir(d))) { | |
647 | char *p; | |
648 | ||
649 | if (ignore_file(de->d_name)) | |
650 | continue; | |
651 | ||
b7def684 | 652 | p = strjoin(streq(*i, "/") ? "" : *i, "/", de->d_name, NULL); |
44d91056 | 653 | if (!p) { |
fe51822e LP |
654 | r = -ENOMEM; |
655 | goto fail; | |
656 | } | |
657 | ||
658 | if ((r = set_put(m->unit_path_cache, p)) < 0) { | |
659 | free(p); | |
660 | goto fail; | |
661 | } | |
662 | } | |
663 | ||
664 | closedir(d); | |
665 | d = NULL; | |
666 | } | |
667 | ||
668 | return; | |
669 | ||
670 | fail: | |
671 | log_error("Failed to build unit path cache: %s", strerror(-r)); | |
672 | ||
673 | set_free_free(m->unit_path_cache); | |
674 | m->unit_path_cache = NULL; | |
675 | ||
676 | if (d) | |
677 | closedir(d); | |
678 | } | |
679 | ||
a16e1123 LP |
680 | int manager_startup(Manager *m, FILE *serialization, FDSet *fds) { |
681 | int r, q; | |
682 | ||
683 | assert(m); | |
684 | ||
5a1e9937 LP |
685 | manager_run_generators(m); |
686 | ||
07719a21 LP |
687 | r = lookup_paths_init( |
688 | &m->lookup_paths, m->running_as, true, | |
689 | m->generator_unit_path, | |
690 | m->generator_unit_path_early, | |
691 | m->generator_unit_path_late); | |
692 | if (r < 0) | |
693 | return r; | |
694 | ||
fe51822e LP |
695 | manager_build_unit_path_cache(m); |
696 | ||
9f611ad8 LP |
697 | /* If we will deserialize make sure that during enumeration |
698 | * this is already known, so we increase the counter here | |
699 | * already */ | |
700 | if (serialization) | |
a7556052 | 701 | m->n_reloading ++; |
9f611ad8 | 702 | |
a16e1123 LP |
703 | /* First, enumerate what we can from all config files */ |
704 | r = manager_enumerate(m); | |
705 | ||
706 | /* Second, deserialize if there is something to deserialize */ | |
07719a21 LP |
707 | if (serialization) { |
708 | q = manager_deserialize(m, serialization, fds); | |
709 | if (q < 0) | |
a16e1123 | 710 | r = q; |
07719a21 | 711 | } |
a16e1123 | 712 | |
01e10de3 LP |
713 | /* Any fds left? Find some unit which wants them. This is |
714 | * useful to allow container managers to pass some file | |
715 | * descriptors to us pre-initialized. This enables | |
716 | * socket-based activation of entire containers. */ | |
717 | if (fdset_size(fds) > 0) { | |
718 | q = manager_distribute_fds(m, fds); | |
719 | if (q < 0) | |
720 | r = q; | |
721 | } | |
722 | ||
a16e1123 | 723 | /* Third, fire things up! */ |
07719a21 LP |
724 | q = manager_coldplug(m); |
725 | if (q < 0) | |
a16e1123 LP |
726 | r = q; |
727 | ||
9f611ad8 | 728 | if (serialization) { |
a7556052 LP |
729 | assert(m->n_reloading > 0); |
730 | m->n_reloading --; | |
9f611ad8 LP |
731 | } |
732 | ||
a16e1123 | 733 | return r; |
f50e0a01 LP |
734 | } |
735 | ||
398ef8ba | 736 | int manager_add_job(Manager *m, JobType type, Unit *unit, JobMode mode, bool override, DBusError *e, Job **_ret) { |
e5b5ae50 | 737 | int r; |
7527cb52 | 738 | Transaction *tr; |
e5b5ae50 LP |
739 | |
740 | assert(m); | |
741 | assert(type < _JOB_TYPE_MAX); | |
87f0e418 | 742 | assert(unit); |
e5b5ae50 | 743 | assert(mode < _JOB_MODE_MAX); |
60918275 | 744 | |
398ef8ba LP |
745 | if (mode == JOB_ISOLATE && type != JOB_START) { |
746 | dbus_set_error(e, BUS_ERROR_INVALID_JOB_MODE, "Isolate is only valid for start."); | |
c497c7a9 | 747 | return -EINVAL; |
398ef8ba | 748 | } |
c497c7a9 | 749 | |
ac155bb8 | 750 | if (mode == JOB_ISOLATE && !unit->allow_isolate) { |
2528a7a6 LP |
751 | dbus_set_error(e, BUS_ERROR_NO_ISOLATION, "Operation refused, unit may not be isolated."); |
752 | return -EPERM; | |
753 | } | |
754 | ||
66870f90 ZJS |
755 | log_debug_unit(unit->id, |
756 | "Trying to enqueue job %s/%s/%s", unit->id, | |
757 | job_type_to_string(type), job_mode_to_string(mode)); | |
9f04bd52 | 758 | |
e0209d83 MS |
759 | job_type_collapse(&type, unit); |
760 | ||
7527cb52 MS |
761 | tr = transaction_new(); |
762 | if (!tr) | |
763 | return -ENOMEM; | |
11dd41ce | 764 | |
7527cb52 MS |
765 | r = transaction_add_job_and_dependencies(tr, type, unit, NULL, true, override, false, |
766 | mode == JOB_IGNORE_DEPENDENCIES || mode == JOB_IGNORE_REQUIREMENTS, | |
b94fbd30 | 767 | mode == JOB_IGNORE_DEPENDENCIES, e); |
7527cb52 MS |
768 | if (r < 0) |
769 | goto tr_abort; | |
c497c7a9 | 770 | |
7527cb52 MS |
771 | if (mode == JOB_ISOLATE) { |
772 | r = transaction_add_isolate_jobs(tr, m); | |
773 | if (r < 0) | |
774 | goto tr_abort; | |
775 | } | |
776 | ||
777 | r = transaction_activate(tr, m, mode, e); | |
778 | if (r < 0) | |
779 | goto tr_abort; | |
e5b5ae50 | 780 | |
66870f90 ZJS |
781 | log_debug_unit(unit->id, |
782 | "Enqueued job %s/%s as %u", unit->id, | |
783 | job_type_to_string(type), (unsigned) tr->anchor_job->id); | |
f50e0a01 | 784 | |
e5b5ae50 | 785 | if (_ret) |
b94fbd30 | 786 | *_ret = tr->anchor_job; |
60918275 | 787 | |
7527cb52 | 788 | transaction_free(tr); |
e5b5ae50 | 789 | return 0; |
7527cb52 MS |
790 | |
791 | tr_abort: | |
792 | transaction_abort(tr); | |
793 | transaction_free(tr); | |
794 | return r; | |
e5b5ae50 | 795 | } |
60918275 | 796 | |
398ef8ba | 797 | int manager_add_job_by_name(Manager *m, JobType type, const char *name, JobMode mode, bool override, DBusError *e, Job **_ret) { |
28247076 LP |
798 | Unit *unit; |
799 | int r; | |
800 | ||
801 | assert(m); | |
802 | assert(type < _JOB_TYPE_MAX); | |
803 | assert(name); | |
804 | assert(mode < _JOB_MODE_MAX); | |
805 | ||
c3090674 LP |
806 | r = manager_load_unit(m, name, NULL, NULL, &unit); |
807 | if (r < 0) | |
28247076 LP |
808 | return r; |
809 | ||
398ef8ba | 810 | return manager_add_job(m, type, unit, mode, override, e, _ret); |
28247076 LP |
811 | } |
812 | ||
60918275 LP |
813 | Job *manager_get_job(Manager *m, uint32_t id) { |
814 | assert(m); | |
815 | ||
816 | return hashmap_get(m->jobs, UINT32_TO_PTR(id)); | |
817 | } | |
818 | ||
87f0e418 | 819 | Unit *manager_get_unit(Manager *m, const char *name) { |
60918275 LP |
820 | assert(m); |
821 | assert(name); | |
822 | ||
87f0e418 | 823 | return hashmap_get(m->units, name); |
60918275 LP |
824 | } |
825 | ||
c1e1601e | 826 | unsigned manager_dispatch_load_queue(Manager *m) { |
595ed347 | 827 | Unit *u; |
c1e1601e | 828 | unsigned n = 0; |
60918275 LP |
829 | |
830 | assert(m); | |
831 | ||
223dabab LP |
832 | /* Make sure we are not run recursively */ |
833 | if (m->dispatching_load_queue) | |
c1e1601e | 834 | return 0; |
223dabab LP |
835 | |
836 | m->dispatching_load_queue = true; | |
837 | ||
87f0e418 | 838 | /* Dispatches the load queue. Takes a unit from the queue and |
60918275 LP |
839 | * tries to load its data until the queue is empty */ |
840 | ||
595ed347 MS |
841 | while ((u = m->load_queue)) { |
842 | assert(u->in_load_queue); | |
034c6ed7 | 843 | |
595ed347 | 844 | unit_load(u); |
c1e1601e | 845 | n++; |
60918275 LP |
846 | } |
847 | ||
223dabab | 848 | m->dispatching_load_queue = false; |
c1e1601e | 849 | return n; |
60918275 LP |
850 | } |
851 | ||
398ef8ba | 852 | int manager_load_unit_prepare(Manager *m, const char *name, const char *path, DBusError *e, Unit **_ret) { |
87f0e418 | 853 | Unit *ret; |
7d17cfbc | 854 | UnitType t; |
60918275 LP |
855 | int r; |
856 | ||
857 | assert(m); | |
9e2f7c11 | 858 | assert(name || path); |
60918275 | 859 | |
db06e3b6 LP |
860 | /* This will prepare the unit for loading, but not actually |
861 | * load anything from disk. */ | |
0301abf4 | 862 | |
398ef8ba LP |
863 | if (path && !is_path(path)) { |
864 | dbus_set_error(e, BUS_ERROR_INVALID_PATH, "Path %s is not absolute.", path); | |
9e2f7c11 | 865 | return -EINVAL; |
398ef8ba | 866 | } |
9e2f7c11 LP |
867 | |
868 | if (!name) | |
9eb977db | 869 | name = path_get_file_name(path); |
9e2f7c11 | 870 | |
7d17cfbc MS |
871 | t = unit_name_to_type(name); |
872 | ||
5f739699 | 873 | if (t == _UNIT_TYPE_INVALID || !unit_name_is_valid(name, false)) { |
398ef8ba | 874 | dbus_set_error(e, BUS_ERROR_INVALID_NAME, "Unit name %s is not valid.", name); |
9e2f7c11 | 875 | return -EINVAL; |
398ef8ba | 876 | } |
60918275 | 877 | |
7d17cfbc MS |
878 | ret = manager_get_unit(m, name); |
879 | if (ret) { | |
034c6ed7 | 880 | *_ret = ret; |
413d6313 | 881 | return 1; |
034c6ed7 | 882 | } |
60918275 | 883 | |
7d17cfbc MS |
884 | ret = unit_new(m, unit_vtable[t]->object_size); |
885 | if (!ret) | |
60918275 LP |
886 | return -ENOMEM; |
887 | ||
7d17cfbc | 888 | if (path) { |
ac155bb8 MS |
889 | ret->fragment_path = strdup(path); |
890 | if (!ret->fragment_path) { | |
0301abf4 LP |
891 | unit_free(ret); |
892 | return -ENOMEM; | |
893 | } | |
7d17cfbc | 894 | } |
0301abf4 | 895 | |
87f0e418 LP |
896 | if ((r = unit_add_name(ret, name)) < 0) { |
897 | unit_free(ret); | |
1ffba6fe | 898 | return r; |
60918275 LP |
899 | } |
900 | ||
87f0e418 | 901 | unit_add_to_load_queue(ret); |
c1e1601e | 902 | unit_add_to_dbus_queue(ret); |
949061f0 | 903 | unit_add_to_gc_queue(ret); |
c1e1601e | 904 | |
db06e3b6 LP |
905 | if (_ret) |
906 | *_ret = ret; | |
907 | ||
908 | return 0; | |
909 | } | |
910 | ||
398ef8ba | 911 | int manager_load_unit(Manager *m, const char *name, const char *path, DBusError *e, Unit **_ret) { |
db06e3b6 LP |
912 | int r; |
913 | ||
914 | assert(m); | |
915 | ||
916 | /* This will load the service information files, but not actually | |
917 | * start any services or anything. */ | |
918 | ||
c3090674 LP |
919 | r = manager_load_unit_prepare(m, name, path, e, _ret); |
920 | if (r != 0) | |
db06e3b6 LP |
921 | return r; |
922 | ||
f50e0a01 | 923 | manager_dispatch_load_queue(m); |
60918275 | 924 | |
9e2f7c11 | 925 | if (_ret) |
413d6313 | 926 | *_ret = unit_follow_merge(*_ret); |
9e2f7c11 | 927 | |
60918275 LP |
928 | return 0; |
929 | } | |
a66d02c3 | 930 | |
cea8e32e | 931 | void manager_dump_jobs(Manager *s, FILE *f, const char *prefix) { |
034c6ed7 | 932 | Iterator i; |
a66d02c3 LP |
933 | Job *j; |
934 | ||
935 | assert(s); | |
936 | assert(f); | |
937 | ||
034c6ed7 | 938 | HASHMAP_FOREACH(j, s->jobs, i) |
cea8e32e | 939 | job_dump(j, f, prefix); |
a66d02c3 LP |
940 | } |
941 | ||
87f0e418 | 942 | void manager_dump_units(Manager *s, FILE *f, const char *prefix) { |
034c6ed7 | 943 | Iterator i; |
87f0e418 | 944 | Unit *u; |
11dd41ce | 945 | const char *t; |
a66d02c3 LP |
946 | |
947 | assert(s); | |
948 | assert(f); | |
949 | ||
87f0e418 | 950 | HASHMAP_FOREACH_KEY(u, t, s->units, i) |
ac155bb8 | 951 | if (u->id == t) |
87f0e418 | 952 | unit_dump(u, f, prefix); |
a66d02c3 | 953 | } |
7fad411c LP |
954 | |
955 | void manager_clear_jobs(Manager *m) { | |
956 | Job *j; | |
957 | ||
958 | assert(m); | |
959 | ||
7fad411c | 960 | while ((j = hashmap_first(m->jobs))) |
5273510e MS |
961 | /* No need to recurse. We're cancelling all jobs. */ |
962 | job_finish_and_invalidate(j, JOB_CANCELED, false); | |
7fad411c | 963 | } |
83c60c9f | 964 | |
c1e1601e | 965 | unsigned manager_dispatch_run_queue(Manager *m) { |
83c60c9f | 966 | Job *j; |
c1e1601e | 967 | unsigned n = 0; |
83c60c9f | 968 | |
034c6ed7 | 969 | if (m->dispatching_run_queue) |
c1e1601e | 970 | return 0; |
034c6ed7 LP |
971 | |
972 | m->dispatching_run_queue = true; | |
9152c765 | 973 | |
034c6ed7 | 974 | while ((j = m->run_queue)) { |
ac1135be | 975 | assert(j->installed); |
034c6ed7 LP |
976 | assert(j->in_run_queue); |
977 | ||
978 | job_run_and_invalidate(j); | |
c1e1601e | 979 | n++; |
9152c765 | 980 | } |
034c6ed7 LP |
981 | |
982 | m->dispatching_run_queue = false; | |
c1e1601e LP |
983 | return n; |
984 | } | |
985 | ||
986 | unsigned manager_dispatch_dbus_queue(Manager *m) { | |
987 | Job *j; | |
595ed347 | 988 | Unit *u; |
c1e1601e LP |
989 | unsigned n = 0; |
990 | ||
991 | assert(m); | |
992 | ||
993 | if (m->dispatching_dbus_queue) | |
994 | return 0; | |
995 | ||
996 | m->dispatching_dbus_queue = true; | |
997 | ||
595ed347 MS |
998 | while ((u = m->dbus_unit_queue)) { |
999 | assert(u->in_dbus_queue); | |
c1e1601e | 1000 | |
595ed347 | 1001 | bus_unit_send_change_signal(u); |
c1e1601e LP |
1002 | n++; |
1003 | } | |
1004 | ||
1005 | while ((j = m->dbus_job_queue)) { | |
1006 | assert(j->in_dbus_queue); | |
1007 | ||
1008 | bus_job_send_change_signal(j); | |
1009 | n++; | |
1010 | } | |
1011 | ||
1012 | m->dispatching_dbus_queue = false; | |
1013 | return n; | |
9152c765 LP |
1014 | } |
1015 | ||
8c47c732 LP |
1016 | static int manager_process_notify_fd(Manager *m) { |
1017 | ssize_t n; | |
1018 | ||
1019 | assert(m); | |
1020 | ||
1021 | for (;;) { | |
1022 | char buf[4096]; | |
1023 | struct msghdr msghdr; | |
1024 | struct iovec iovec; | |
1025 | struct ucred *ucred; | |
1026 | union { | |
1027 | struct cmsghdr cmsghdr; | |
1028 | uint8_t buf[CMSG_SPACE(sizeof(struct ucred))]; | |
1029 | } control; | |
1030 | Unit *u; | |
1031 | char **tags; | |
1032 | ||
1033 | zero(iovec); | |
1034 | iovec.iov_base = buf; | |
1035 | iovec.iov_len = sizeof(buf)-1; | |
1036 | ||
1037 | zero(control); | |
1038 | zero(msghdr); | |
1039 | msghdr.msg_iov = &iovec; | |
1040 | msghdr.msg_iovlen = 1; | |
1041 | msghdr.msg_control = &control; | |
1042 | msghdr.msg_controllen = sizeof(control); | |
1043 | ||
bd0af849 ZJS |
1044 | n = recvmsg(m->notify_watch.fd, &msghdr, MSG_DONTWAIT); |
1045 | if (n <= 0) { | |
8c47c732 LP |
1046 | if (n >= 0) |
1047 | return -EIO; | |
1048 | ||
f6144808 | 1049 | if (errno == EAGAIN || errno == EINTR) |
8c47c732 LP |
1050 | break; |
1051 | ||
1052 | return -errno; | |
1053 | } | |
1054 | ||
1055 | if (msghdr.msg_controllen < CMSG_LEN(sizeof(struct ucred)) || | |
1056 | control.cmsghdr.cmsg_level != SOL_SOCKET || | |
1057 | control.cmsghdr.cmsg_type != SCM_CREDENTIALS || | |
1058 | control.cmsghdr.cmsg_len != CMSG_LEN(sizeof(struct ucred))) { | |
1059 | log_warning("Received notify message without credentials. Ignoring."); | |
1060 | continue; | |
1061 | } | |
1062 | ||
1063 | ucred = (struct ucred*) CMSG_DATA(&control.cmsghdr); | |
1064 | ||
bd0af849 ZJS |
1065 | u = hashmap_get(m->watch_pids, LONG_TO_PTR(ucred->pid)); |
1066 | if (!u) { | |
1067 | u = cgroup_unit_by_pid(m, ucred->pid); | |
1068 | if (!u) { | |
8c47c732 LP |
1069 | log_warning("Cannot find unit for notify message of PID %lu.", (unsigned long) ucred->pid); |
1070 | continue; | |
1071 | } | |
bd0af849 | 1072 | } |
8c47c732 | 1073 | |
8c40acf7 LP |
1074 | assert((size_t) n < sizeof(buf)); |
1075 | buf[n] = 0; | |
bd0af849 ZJS |
1076 | tags = strv_split(buf, "\n\r"); |
1077 | if (!tags) | |
1078 | return log_oom(); | |
8c47c732 | 1079 | |
66870f90 | 1080 | log_debug_unit(u->id, "Got notification message for unit %s", u->id); |
8c47c732 LP |
1081 | |
1082 | if (UNIT_VTABLE(u)->notify_message) | |
c952c6ec | 1083 | UNIT_VTABLE(u)->notify_message(u, ucred->pid, tags); |
8c47c732 LP |
1084 | |
1085 | strv_free(tags); | |
1086 | } | |
1087 | ||
1088 | return 0; | |
1089 | } | |
1090 | ||
034c6ed7 | 1091 | static int manager_dispatch_sigchld(Manager *m) { |
9152c765 LP |
1092 | assert(m); |
1093 | ||
1094 | for (;;) { | |
1095 | siginfo_t si; | |
87f0e418 | 1096 | Unit *u; |
8c47c732 | 1097 | int r; |
9152c765 LP |
1098 | |
1099 | zero(si); | |
4112df16 LP |
1100 | |
1101 | /* First we call waitd() for a PID and do not reap the | |
1102 | * zombie. That way we can still access /proc/$PID for | |
1103 | * it while it is a zombie. */ | |
1104 | if (waitid(P_ALL, 0, &si, WEXITED|WNOHANG|WNOWAIT) < 0) { | |
acbb0225 LP |
1105 | |
1106 | if (errno == ECHILD) | |
1107 | break; | |
1108 | ||
4112df16 LP |
1109 | if (errno == EINTR) |
1110 | continue; | |
1111 | ||
9152c765 | 1112 | return -errno; |
acbb0225 | 1113 | } |
9152c765 | 1114 | |
4112df16 | 1115 | if (si.si_pid <= 0) |
9152c765 LP |
1116 | break; |
1117 | ||
15d5d9d9 | 1118 | if (si.si_code == CLD_EXITED || si.si_code == CLD_KILLED || si.si_code == CLD_DUMPED) { |
bd0af849 | 1119 | char _cleanup_free_ *name = NULL; |
4112df16 | 1120 | |
87d2c1ff | 1121 | get_process_comm(si.si_pid, &name); |
bb00e604 | 1122 | log_debug("Got SIGCHLD for process %lu (%s)", (unsigned long) si.si_pid, strna(name)); |
4112df16 LP |
1123 | } |
1124 | ||
8c47c732 LP |
1125 | /* Let's flush any message the dying child might still |
1126 | * have queued for us. This ensures that the process | |
1127 | * still exists in /proc so that we can figure out | |
1128 | * which cgroup and hence unit it belongs to. */ | |
bd0af849 ZJS |
1129 | r = manager_process_notify_fd(m); |
1130 | if (r < 0) | |
8c47c732 LP |
1131 | return r; |
1132 | ||
1133 | /* And now figure out the unit this belongs to */ | |
bd0af849 ZJS |
1134 | u = hashmap_get(m->watch_pids, LONG_TO_PTR(si.si_pid)); |
1135 | if (!u) | |
8c47c732 LP |
1136 | u = cgroup_unit_by_pid(m, si.si_pid); |
1137 | ||
4112df16 LP |
1138 | /* And now, we actually reap the zombie. */ |
1139 | if (waitid(P_PID, si.si_pid, &si, WEXITED) < 0) { | |
1140 | if (errno == EINTR) | |
1141 | continue; | |
1142 | ||
1143 | return -errno; | |
1144 | } | |
1145 | ||
034c6ed7 LP |
1146 | if (si.si_code != CLD_EXITED && si.si_code != CLD_KILLED && si.si_code != CLD_DUMPED) |
1147 | continue; | |
1148 | ||
bb00e604 LP |
1149 | log_debug("Child %lu died (code=%s, status=%i/%s)", |
1150 | (long unsigned) si.si_pid, | |
4112df16 LP |
1151 | sigchld_code_to_string(si.si_code), |
1152 | si.si_status, | |
d06dacd0 LP |
1153 | strna(si.si_code == CLD_EXITED |
1154 | ? exit_status_to_string(si.si_status, EXIT_STATUS_FULL) | |
1155 | : signal_to_string(si.si_status))); | |
acbb0225 | 1156 | |
8c47c732 | 1157 | if (!u) |
9152c765 LP |
1158 | continue; |
1159 | ||
66870f90 ZJS |
1160 | log_debug_unit(u->id, |
1161 | "Child %lu belongs to %s", (long unsigned) si.si_pid, u->id); | |
6c1a0478 | 1162 | |
c6c18be3 | 1163 | hashmap_remove(m->watch_pids, LONG_TO_PTR(si.si_pid)); |
87f0e418 | 1164 | UNIT_VTABLE(u)->sigchld_event(u, si.si_pid, si.si_code, si.si_status); |
9152c765 LP |
1165 | } |
1166 | ||
1167 | return 0; | |
1168 | } | |
1169 | ||
7d793605 | 1170 | static int manager_start_target(Manager *m, const char *name, JobMode mode) { |
28247076 | 1171 | int r; |
398ef8ba LP |
1172 | DBusError error; |
1173 | ||
1174 | dbus_error_init(&error); | |
1175 | ||
66870f90 | 1176 | log_debug_unit(name, "Activating special unit %s", name); |
1e001f52 | 1177 | |
bd0af849 ZJS |
1178 | r = manager_add_job_by_name(m, JOB_START, name, mode, true, &error, NULL); |
1179 | if (r < 0) | |
66870f90 ZJS |
1180 | log_error_unit(name, |
1181 | "Failed to enqueue %s job: %s", name, bus_error(&error, r)); | |
28247076 | 1182 | |
398ef8ba | 1183 | dbus_error_free(&error); |
a1b256b0 LP |
1184 | |
1185 | return r; | |
28247076 LP |
1186 | } |
1187 | ||
a16e1123 | 1188 | static int manager_process_signal_fd(Manager *m) { |
9152c765 LP |
1189 | ssize_t n; |
1190 | struct signalfd_siginfo sfsi; | |
1191 | bool sigchld = false; | |
1192 | ||
1193 | assert(m); | |
1194 | ||
1195 | for (;;) { | |
57cb4adf LP |
1196 | n = read(m->signal_watch.fd, &sfsi, sizeof(sfsi)); |
1197 | if (n != sizeof(sfsi)) { | |
9152c765 LP |
1198 | |
1199 | if (n >= 0) | |
1200 | return -EIO; | |
1201 | ||
63090775 | 1202 | if (errno == EINTR || errno == EAGAIN) |
acbb0225 | 1203 | break; |
9152c765 LP |
1204 | |
1205 | return -errno; | |
1206 | } | |
1207 | ||
67370238 LP |
1208 | if (sfsi.ssi_pid > 0) { |
1209 | char *p = NULL; | |
1210 | ||
87d2c1ff | 1211 | get_process_comm(sfsi.ssi_pid, &p); |
dfa7f7e1 | 1212 | |
67370238 | 1213 | log_debug("Received SIG%s from PID %lu (%s).", |
4e240ab0 | 1214 | signal_to_string(sfsi.ssi_signo), |
67370238 LP |
1215 | (unsigned long) sfsi.ssi_pid, strna(p)); |
1216 | free(p); | |
1217 | } else | |
4e240ab0 | 1218 | log_debug("Received SIG%s.", signal_to_string(sfsi.ssi_signo)); |
1e001f52 | 1219 | |
b9cd2ec1 LP |
1220 | switch (sfsi.ssi_signo) { |
1221 | ||
4112df16 | 1222 | case SIGCHLD: |
9152c765 | 1223 | sigchld = true; |
b9cd2ec1 LP |
1224 | break; |
1225 | ||
6632c602 | 1226 | case SIGTERM: |
67445f4e | 1227 | if (m->running_as == SYSTEMD_SYSTEM) { |
db06e3b6 LP |
1228 | /* This is for compatibility with the |
1229 | * original sysvinit */ | |
e11dc4a2 | 1230 | m->exit_code = MANAGER_REEXECUTE; |
a1b256b0 LP |
1231 | break; |
1232 | } | |
84e9af1e | 1233 | |
a1b256b0 | 1234 | /* Fall through */ |
e11dc4a2 LP |
1235 | |
1236 | case SIGINT: | |
67445f4e | 1237 | if (m->running_as == SYSTEMD_SYSTEM) { |
7d793605 | 1238 | manager_start_target(m, SPECIAL_CTRL_ALT_DEL_TARGET, JOB_REPLACE); |
84e9af1e LP |
1239 | break; |
1240 | } | |
1241 | ||
a1b256b0 | 1242 | /* Run the exit target if there is one, if not, just exit. */ |
0003d1ab | 1243 | if (manager_start_target(m, SPECIAL_EXIT_TARGET, JOB_REPLACE) < 0) { |
a1b256b0 LP |
1244 | m->exit_code = MANAGER_EXIT; |
1245 | return 0; | |
1246 | } | |
1247 | ||
1248 | break; | |
84e9af1e | 1249 | |
28247076 | 1250 | case SIGWINCH: |
67445f4e | 1251 | if (m->running_as == SYSTEMD_SYSTEM) |
7d793605 | 1252 | manager_start_target(m, SPECIAL_KBREQUEST_TARGET, JOB_REPLACE); |
84e9af1e | 1253 | |
28247076 LP |
1254 | /* This is a nop on non-init */ |
1255 | break; | |
84e9af1e | 1256 | |
28247076 | 1257 | case SIGPWR: |
67445f4e | 1258 | if (m->running_as == SYSTEMD_SYSTEM) |
7d793605 | 1259 | manager_start_target(m, SPECIAL_SIGPWR_TARGET, JOB_REPLACE); |
84e9af1e | 1260 | |
28247076 | 1261 | /* This is a nop on non-init */ |
84e9af1e | 1262 | break; |
6632c602 | 1263 | |
1005d14f | 1264 | case SIGUSR1: { |
57ee42ce LP |
1265 | Unit *u; |
1266 | ||
1267 | u = manager_get_unit(m, SPECIAL_DBUS_SERVICE); | |
1268 | ||
1269 | if (!u || UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u))) { | |
1270 | log_info("Trying to reconnect to bus..."); | |
3996fbe2 | 1271 | bus_init(m, true); |
57ee42ce LP |
1272 | } |
1273 | ||
1274 | if (!u || !UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u))) { | |
1275 | log_info("Loading D-Bus service..."); | |
7d793605 | 1276 | manager_start_target(m, SPECIAL_DBUS_SERVICE, JOB_REPLACE); |
57ee42ce LP |
1277 | } |
1278 | ||
1279 | break; | |
1280 | } | |
1281 | ||
2149e37c LP |
1282 | case SIGUSR2: { |
1283 | FILE *f; | |
1284 | char *dump = NULL; | |
1285 | size_t size; | |
1286 | ||
1287 | if (!(f = open_memstream(&dump, &size))) { | |
1288 | log_warning("Failed to allocate memory stream."); | |
1289 | break; | |
1290 | } | |
1291 | ||
1292 | manager_dump_units(m, f, "\t"); | |
1293 | manager_dump_jobs(m, f, "\t"); | |
1294 | ||
1295 | if (ferror(f)) { | |
1296 | fclose(f); | |
1297 | free(dump); | |
1298 | log_warning("Failed to write status stream"); | |
1299 | break; | |
1300 | } | |
1301 | ||
1302 | fclose(f); | |
1303 | log_dump(LOG_INFO, dump); | |
1304 | free(dump); | |
1305 | ||
1005d14f | 1306 | break; |
2149e37c | 1307 | } |
1005d14f | 1308 | |
a16e1123 LP |
1309 | case SIGHUP: |
1310 | m->exit_code = MANAGER_RELOAD; | |
1311 | break; | |
1312 | ||
7d793605 | 1313 | default: { |
253ee27a | 1314 | |
0003d1ab LP |
1315 | /* Starting SIGRTMIN+0 */ |
1316 | static const char * const target_table[] = { | |
7d793605 LP |
1317 | [0] = SPECIAL_DEFAULT_TARGET, |
1318 | [1] = SPECIAL_RESCUE_TARGET, | |
f057408c | 1319 | [2] = SPECIAL_EMERGENCY_TARGET, |
7d793605 LP |
1320 | [3] = SPECIAL_HALT_TARGET, |
1321 | [4] = SPECIAL_POWEROFF_TARGET, | |
0003d1ab LP |
1322 | [5] = SPECIAL_REBOOT_TARGET, |
1323 | [6] = SPECIAL_KEXEC_TARGET | |
1324 | }; | |
1325 | ||
1326 | /* Starting SIGRTMIN+13, so that target halt and system halt are 10 apart */ | |
1327 | static const ManagerExitCode code_table[] = { | |
1328 | [0] = MANAGER_HALT, | |
1329 | [1] = MANAGER_POWEROFF, | |
1330 | [2] = MANAGER_REBOOT, | |
1331 | [3] = MANAGER_KEXEC | |
7d793605 LP |
1332 | }; |
1333 | ||
1334 | if ((int) sfsi.ssi_signo >= SIGRTMIN+0 && | |
0003d1ab | 1335 | (int) sfsi.ssi_signo < SIGRTMIN+(int) ELEMENTSOF(target_table)) { |
764e9b5f MS |
1336 | int idx = (int) sfsi.ssi_signo - SIGRTMIN; |
1337 | manager_start_target(m, target_table[idx], | |
1338 | (idx == 1 || idx == 2) ? JOB_ISOLATE : JOB_REPLACE); | |
7d793605 LP |
1339 | break; |
1340 | } | |
1341 | ||
0003d1ab LP |
1342 | if ((int) sfsi.ssi_signo >= SIGRTMIN+13 && |
1343 | (int) sfsi.ssi_signo < SIGRTMIN+13+(int) ELEMENTSOF(code_table)) { | |
1344 | m->exit_code = code_table[sfsi.ssi_signo - SIGRTMIN - 13]; | |
1345 | break; | |
1346 | } | |
1347 | ||
0658666b LP |
1348 | switch (sfsi.ssi_signo - SIGRTMIN) { |
1349 | ||
1350 | case 20: | |
1351 | log_debug("Enabling showing of status."); | |
27d340c7 | 1352 | manager_set_show_status(m, true); |
0658666b LP |
1353 | break; |
1354 | ||
1355 | case 21: | |
1356 | log_debug("Disabling showing of status."); | |
27d340c7 | 1357 | manager_set_show_status(m, false); |
0658666b LP |
1358 | break; |
1359 | ||
253ee27a LP |
1360 | case 22: |
1361 | log_set_max_level(LOG_DEBUG); | |
1362 | log_notice("Setting log level to debug."); | |
1363 | break; | |
1364 | ||
1365 | case 23: | |
1366 | log_set_max_level(LOG_INFO); | |
1367 | log_notice("Setting log level to info."); | |
1368 | break; | |
1369 | ||
600b704e LP |
1370 | case 24: |
1371 | if (m->running_as == SYSTEMD_USER) { | |
1372 | m->exit_code = MANAGER_EXIT; | |
1373 | return 0; | |
1374 | } | |
1375 | ||
1376 | /* This is a nop on init */ | |
1377 | break; | |
1378 | ||
4cfa2c99 LP |
1379 | case 26: |
1380 | log_set_target(LOG_TARGET_JOURNAL_OR_KMSG); | |
1381 | log_notice("Setting log target to journal-or-kmsg."); | |
1382 | break; | |
1383 | ||
253ee27a LP |
1384 | case 27: |
1385 | log_set_target(LOG_TARGET_CONSOLE); | |
1386 | log_notice("Setting log target to console."); | |
1387 | break; | |
1388 | ||
1389 | case 28: | |
1390 | log_set_target(LOG_TARGET_KMSG); | |
1391 | log_notice("Setting log target to kmsg."); | |
1392 | break; | |
1393 | ||
1394 | case 29: | |
1395 | log_set_target(LOG_TARGET_SYSLOG_OR_KMSG); | |
1396 | log_notice("Setting log target to syslog-or-kmsg."); | |
1397 | break; | |
1398 | ||
0658666b | 1399 | default: |
4e240ab0 | 1400 | log_warning("Got unhandled signal <%s>.", signal_to_string(sfsi.ssi_signo)); |
0658666b | 1401 | } |
b9cd2ec1 | 1402 | } |
7d793605 | 1403 | } |
9152c765 LP |
1404 | } |
1405 | ||
1406 | if (sigchld) | |
034c6ed7 LP |
1407 | return manager_dispatch_sigchld(m); |
1408 | ||
1409 | return 0; | |
1410 | } | |
1411 | ||
a16e1123 | 1412 | static int process_event(Manager *m, struct epoll_event *ev) { |
034c6ed7 | 1413 | int r; |
acbb0225 | 1414 | Watch *w; |
034c6ed7 LP |
1415 | |
1416 | assert(m); | |
1417 | assert(ev); | |
1418 | ||
3df5bf61 | 1419 | assert_se(w = ev->data.ptr); |
034c6ed7 | 1420 | |
40dde66f LP |
1421 | if (w->type == WATCH_INVALID) |
1422 | return 0; | |
1423 | ||
acbb0225 | 1424 | switch (w->type) { |
034c6ed7 | 1425 | |
ef734fd6 | 1426 | case WATCH_SIGNAL: |
034c6ed7 | 1427 | |
acbb0225 | 1428 | /* An incoming signal? */ |
f94ea366 | 1429 | if (ev->events != EPOLLIN) |
acbb0225 | 1430 | return -EINVAL; |
034c6ed7 | 1431 | |
a16e1123 | 1432 | if ((r = manager_process_signal_fd(m)) < 0) |
acbb0225 | 1433 | return r; |
034c6ed7 | 1434 | |
acbb0225 | 1435 | break; |
034c6ed7 | 1436 | |
8c47c732 LP |
1437 | case WATCH_NOTIFY: |
1438 | ||
1439 | /* An incoming daemon notification event? */ | |
1440 | if (ev->events != EPOLLIN) | |
1441 | return -EINVAL; | |
1442 | ||
1443 | if ((r = manager_process_notify_fd(m)) < 0) | |
1444 | return r; | |
1445 | ||
1446 | break; | |
1447 | ||
acbb0225 | 1448 | case WATCH_FD: |
034c6ed7 | 1449 | |
acbb0225 | 1450 | /* Some fd event, to be dispatched to the units */ |
ea430986 | 1451 | UNIT_VTABLE(w->data.unit)->fd_event(w->data.unit, w->fd, ev->events, w); |
acbb0225 | 1452 | break; |
034c6ed7 | 1453 | |
faf919f1 LP |
1454 | case WATCH_UNIT_TIMER: |
1455 | case WATCH_JOB_TIMER: { | |
acbb0225 LP |
1456 | uint64_t v; |
1457 | ssize_t k; | |
034c6ed7 | 1458 | |
acbb0225 | 1459 | /* Some timer event, to be dispatched to the units */ |
68b29a9f LP |
1460 | k = read(w->fd, &v, sizeof(v)); |
1461 | if (k != sizeof(v)) { | |
034c6ed7 | 1462 | |
acbb0225 LP |
1463 | if (k < 0 && (errno == EINTR || errno == EAGAIN)) |
1464 | break; | |
034c6ed7 | 1465 | |
8742514c | 1466 | log_error("Failed to read timer event counter: %s", k < 0 ? strerror(-k) : "Short read"); |
acbb0225 | 1467 | return k < 0 ? -errno : -EIO; |
034c6ed7 LP |
1468 | } |
1469 | ||
faf919f1 LP |
1470 | if (w->type == WATCH_UNIT_TIMER) |
1471 | UNIT_VTABLE(w->data.unit)->timer_event(w->data.unit, v, w); | |
1472 | else | |
1473 | job_timer_event(w->data.job, v, w); | |
acbb0225 LP |
1474 | break; |
1475 | } | |
1476 | ||
ef734fd6 LP |
1477 | case WATCH_MOUNT: |
1478 | /* Some mount table change, intended for the mount subsystem */ | |
1479 | mount_fd_event(m, ev->events); | |
1480 | break; | |
1481 | ||
4e434314 LP |
1482 | case WATCH_SWAP: |
1483 | /* Some swap table change, intended for the swap subsystem */ | |
1484 | swap_fd_event(m, ev->events); | |
1485 | break; | |
1486 | ||
f94ea366 LP |
1487 | case WATCH_UDEV: |
1488 | /* Some notification from udev, intended for the device subsystem */ | |
1489 | device_fd_event(m, ev->events); | |
1490 | break; | |
1491 | ||
ea430986 LP |
1492 | case WATCH_DBUS_WATCH: |
1493 | bus_watch_event(m, w, ev->events); | |
1494 | break; | |
1495 | ||
1496 | case WATCH_DBUS_TIMEOUT: | |
1497 | bus_timeout_event(m, w, ev->events); | |
1498 | break; | |
1499 | ||
8742514c LP |
1500 | case WATCH_TIME_CHANGE: { |
1501 | Unit *u; | |
1502 | Iterator i; | |
1503 | ||
1504 | log_struct(LOG_INFO, | |
1505 | MESSAGE_ID(SD_MESSAGE_TIME_CHANGE), | |
1506 | "MESSAGE=Time has been changed", | |
1507 | NULL); | |
1508 | ||
1509 | /* Restart the watch */ | |
1510 | close_nointr_nofail(m->time_change_watch.fd); | |
1511 | watch_init(&m->time_change_watch); | |
1512 | manager_setup_time_change(m); | |
1513 | ||
1514 | HASHMAP_FOREACH(u, m->units, i) { | |
1515 | if (UNIT_VTABLE(u)->time_change) | |
1516 | UNIT_VTABLE(u)->time_change(u); | |
1517 | } | |
1518 | ||
1519 | break; | |
1520 | } | |
1521 | ||
acbb0225 | 1522 | default: |
c5fd1e57 | 1523 | log_error("event type=%i", w->type); |
acbb0225 | 1524 | assert_not_reached("Unknown epoll event type."); |
034c6ed7 | 1525 | } |
9152c765 LP |
1526 | |
1527 | return 0; | |
1528 | } | |
1529 | ||
1530 | int manager_loop(Manager *m) { | |
1531 | int r; | |
9152c765 | 1532 | |
fac9f8df | 1533 | RATELIMIT_DEFINE(rl, 1*USEC_PER_SEC, 50000); |
ea430986 | 1534 | |
9152c765 | 1535 | assert(m); |
a16e1123 | 1536 | m->exit_code = MANAGER_RUNNING; |
9152c765 | 1537 | |
fe51822e LP |
1538 | /* Release the path cache */ |
1539 | set_free_free(m->unit_path_cache); | |
1540 | m->unit_path_cache = NULL; | |
1541 | ||
b0c918b9 LP |
1542 | manager_check_finished(m); |
1543 | ||
a4312405 LP |
1544 | /* There might still be some zombies hanging around from |
1545 | * before we were exec()'ed. Leat's reap them */ | |
e96d6be7 LP |
1546 | r = manager_dispatch_sigchld(m); |
1547 | if (r < 0) | |
a4312405 LP |
1548 | return r; |
1549 | ||
a16e1123 | 1550 | while (m->exit_code == MANAGER_RUNNING) { |
957ca890 LP |
1551 | struct epoll_event event; |
1552 | int n; | |
c757a65b | 1553 | int wait_msec = -1; |
9152c765 | 1554 | |
67445f4e | 1555 | if (m->runtime_watchdog > 0 && m->running_as == SYSTEMD_SYSTEM) |
e96d6be7 LP |
1556 | watchdog_ping(); |
1557 | ||
ea430986 LP |
1558 | if (!ratelimit_test(&rl)) { |
1559 | /* Yay, something is going seriously wrong, pause a little */ | |
1560 | log_warning("Looping too fast. Throttling execution a little."); | |
1561 | sleep(1); | |
e96d6be7 | 1562 | continue; |
ea430986 LP |
1563 | } |
1564 | ||
37a8e683 | 1565 | if (manager_dispatch_load_queue(m) > 0) |
23a177ef LP |
1566 | continue; |
1567 | ||
37a8e683 | 1568 | if (manager_dispatch_run_queue(m) > 0) |
701cc384 LP |
1569 | continue; |
1570 | ||
37a8e683 | 1571 | if (bus_dispatch(m) > 0) |
c1e1601e | 1572 | continue; |
034c6ed7 | 1573 | |
37a8e683 | 1574 | if (manager_dispatch_cleanup_queue(m) > 0) |
c1e1601e LP |
1575 | continue; |
1576 | ||
37a8e683 | 1577 | if (manager_dispatch_gc_queue(m) > 0) |
c1e1601e LP |
1578 | continue; |
1579 | ||
1580 | if (manager_dispatch_dbus_queue(m) > 0) | |
ea430986 | 1581 | continue; |
ea430986 | 1582 | |
e04aad61 LP |
1583 | if (swap_dispatch_reload(m) > 0) |
1584 | continue; | |
1585 | ||
c757a65b | 1586 | /* Sleep for half the watchdog time */ |
67445f4e | 1587 | if (m->runtime_watchdog > 0 && m->running_as == SYSTEMD_SYSTEM) { |
c757a65b LP |
1588 | wait_msec = (int) (m->runtime_watchdog / 2 / USEC_PER_MSEC); |
1589 | if (wait_msec <= 0) | |
1590 | wait_msec = 1; | |
1591 | } else | |
1592 | wait_msec = -1; | |
1593 | ||
e96d6be7 LP |
1594 | n = epoll_wait(m->epoll_fd, &event, 1, wait_msec); |
1595 | if (n < 0) { | |
9152c765 | 1596 | |
6089f4a9 | 1597 | if (errno == EINTR) |
9152c765 LP |
1598 | continue; |
1599 | ||
1600 | return -errno; | |
e96d6be7 LP |
1601 | } else if (n == 0) |
1602 | continue; | |
9152c765 | 1603 | |
957ca890 | 1604 | assert(n == 1); |
b9cd2ec1 | 1605 | |
e96d6be7 LP |
1606 | r = process_event(m, &event); |
1607 | if (r < 0) | |
957ca890 | 1608 | return r; |
a16e1123 | 1609 | } |
957ca890 | 1610 | |
a16e1123 | 1611 | return m->exit_code; |
83c60c9f | 1612 | } |
ea430986 | 1613 | |
80fbf05e | 1614 | int manager_load_unit_from_dbus_path(Manager *m, const char *s, DBusError *e, Unit **_u) { |
ea430986 LP |
1615 | char *n; |
1616 | Unit *u; | |
80fbf05e | 1617 | int r; |
ea430986 LP |
1618 | |
1619 | assert(m); | |
1620 | assert(s); | |
1621 | assert(_u); | |
1622 | ||
1623 | if (!startswith(s, "/org/freedesktop/systemd1/unit/")) | |
1624 | return -EINVAL; | |
1625 | ||
80fbf05e MS |
1626 | n = bus_path_unescape(s+31); |
1627 | if (!n) | |
ea430986 LP |
1628 | return -ENOMEM; |
1629 | ||
80fbf05e | 1630 | r = manager_load_unit(m, n, NULL, e, &u); |
ea430986 LP |
1631 | free(n); |
1632 | ||
80fbf05e MS |
1633 | if (r < 0) |
1634 | return r; | |
ea430986 LP |
1635 | |
1636 | *_u = u; | |
1637 | ||
1638 | return 0; | |
1639 | } | |
86fbf370 LP |
1640 | |
1641 | int manager_get_job_from_dbus_path(Manager *m, const char *s, Job **_j) { | |
1642 | Job *j; | |
1643 | unsigned id; | |
1644 | int r; | |
1645 | ||
1646 | assert(m); | |
1647 | assert(s); | |
1648 | assert(_j); | |
1649 | ||
1650 | if (!startswith(s, "/org/freedesktop/systemd1/job/")) | |
1651 | return -EINVAL; | |
1652 | ||
8742514c LP |
1653 | r = safe_atou(s + 30, &id); |
1654 | if (r < 0) | |
86fbf370 LP |
1655 | return r; |
1656 | ||
8742514c LP |
1657 | j = manager_get_job(m, id); |
1658 | if (!j) | |
86fbf370 LP |
1659 | return -ENOENT; |
1660 | ||
1661 | *_j = j; | |
1662 | ||
1663 | return 0; | |
1664 | } | |
dfcd764e | 1665 | |
4927fcae | 1666 | void manager_send_unit_audit(Manager *m, Unit *u, int type, bool success) { |
e537352b | 1667 | |
4927fcae LP |
1668 | #ifdef HAVE_AUDIT |
1669 | char *p; | |
c1165f82 | 1670 | int audit_fd; |
e537352b | 1671 | |
c1165f82 LP |
1672 | audit_fd = get_audit_fd(); |
1673 | if (audit_fd < 0) | |
e537352b LP |
1674 | return; |
1675 | ||
bbd3a7ba LP |
1676 | /* Don't generate audit events if the service was already |
1677 | * started and we're just deserializing */ | |
a7556052 | 1678 | if (m->n_reloading > 0) |
bbd3a7ba LP |
1679 | return; |
1680 | ||
67445f4e | 1681 | if (m->running_as != SYSTEMD_SYSTEM) |
f1dd0c3f LP |
1682 | return; |
1683 | ||
ac155bb8 | 1684 | if (u->type != UNIT_SERVICE) |
f1dd0c3f LP |
1685 | return; |
1686 | ||
bd0af849 ZJS |
1687 | p = unit_name_to_prefix_and_instance(u->id); |
1688 | if (!p) { | |
66870f90 ZJS |
1689 | log_error_unit(u->id, |
1690 | "Failed to allocate unit name for audit message: %s", strerror(ENOMEM)); | |
e537352b LP |
1691 | return; |
1692 | } | |
1693 | ||
c1165f82 | 1694 | if (audit_log_user_comm_message(audit_fd, type, "", p, NULL, NULL, NULL, success) < 0) { |
391ade86 LP |
1695 | if (errno == EPERM) { |
1696 | /* We aren't allowed to send audit messages? | |
44785992 | 1697 | * Then let's not retry again. */ |
c1165f82 | 1698 | close_audit_fd(); |
44785992 LP |
1699 | } else |
1700 | log_warning("Failed to send audit message: %m"); | |
391ade86 | 1701 | } |
e537352b | 1702 | |
4927fcae LP |
1703 | free(p); |
1704 | #endif | |
e537352b | 1705 | |
e537352b LP |
1706 | } |
1707 | ||
e983b760 LP |
1708 | void manager_send_unit_plymouth(Manager *m, Unit *u) { |
1709 | int fd = -1; | |
1710 | union sockaddr_union sa; | |
1711 | int n = 0; | |
1712 | char *message = NULL; | |
e983b760 LP |
1713 | |
1714 | /* Don't generate plymouth events if the service was already | |
1715 | * started and we're just deserializing */ | |
a7556052 | 1716 | if (m->n_reloading > 0) |
e983b760 LP |
1717 | return; |
1718 | ||
67445f4e | 1719 | if (m->running_as != SYSTEMD_SYSTEM) |
e983b760 LP |
1720 | return; |
1721 | ||
ac155bb8 MS |
1722 | if (u->type != UNIT_SERVICE && |
1723 | u->type != UNIT_MOUNT && | |
1724 | u->type != UNIT_SWAP) | |
e983b760 LP |
1725 | return; |
1726 | ||
1727 | /* We set SOCK_NONBLOCK here so that we rather drop the | |
1728 | * message then wait for plymouth */ | |
1729 | if ((fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0)) < 0) { | |
1730 | log_error("socket() failed: %m"); | |
1731 | return; | |
1732 | } | |
1733 | ||
1734 | zero(sa); | |
1735 | sa.sa.sa_family = AF_UNIX; | |
96707269 LP |
1736 | strncpy(sa.un.sun_path+1, "/org/freedesktop/plymouthd", sizeof(sa.un.sun_path)-1); |
1737 | if (connect(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + 1 + strlen(sa.un.sun_path+1)) < 0) { | |
e983b760 LP |
1738 | |
1739 | if (errno != EPIPE && | |
1740 | errno != EAGAIN && | |
1741 | errno != ENOENT && | |
1742 | errno != ECONNREFUSED && | |
1743 | errno != ECONNRESET && | |
1744 | errno != ECONNABORTED) | |
1745 | log_error("connect() failed: %m"); | |
1746 | ||
1747 | goto finish; | |
1748 | } | |
1749 | ||
ac155bb8 | 1750 | if (asprintf(&message, "U\002%c%s%n", (int) (strlen(u->id) + 1), u->id, &n) < 0) { |
0d0f0c50 | 1751 | log_oom(); |
e983b760 LP |
1752 | goto finish; |
1753 | } | |
1754 | ||
1755 | errno = 0; | |
bd40a2d8 | 1756 | if (write(fd, message, n + 1) != n + 1) { |
e983b760 LP |
1757 | |
1758 | if (errno != EPIPE && | |
1759 | errno != EAGAIN && | |
1760 | errno != ENOENT && | |
1761 | errno != ECONNREFUSED && | |
1762 | errno != ECONNRESET && | |
1763 | errno != ECONNABORTED) | |
1764 | log_error("Failed to write Plymouth message: %m"); | |
1765 | ||
1766 | goto finish; | |
1767 | } | |
1768 | ||
1769 | finish: | |
1770 | if (fd >= 0) | |
1771 | close_nointr_nofail(fd); | |
1772 | ||
1773 | free(message); | |
1774 | } | |
1775 | ||
05e343b7 LP |
1776 | void manager_dispatch_bus_name_owner_changed( |
1777 | Manager *m, | |
1778 | const char *name, | |
1779 | const char* old_owner, | |
1780 | const char *new_owner) { | |
1781 | ||
1782 | Unit *u; | |
1783 | ||
1784 | assert(m); | |
1785 | assert(name); | |
1786 | ||
1787 | if (!(u = hashmap_get(m->watch_bus, name))) | |
1788 | return; | |
1789 | ||
1790 | UNIT_VTABLE(u)->bus_name_owner_change(u, name, old_owner, new_owner); | |
1791 | } | |
1792 | ||
1793 | void manager_dispatch_bus_query_pid_done( | |
1794 | Manager *m, | |
1795 | const char *name, | |
1796 | pid_t pid) { | |
1797 | ||
1798 | Unit *u; | |
1799 | ||
1800 | assert(m); | |
1801 | assert(name); | |
1802 | assert(pid >= 1); | |
1803 | ||
1804 | if (!(u = hashmap_get(m->watch_bus, name))) | |
1805 | return; | |
1806 | ||
1807 | UNIT_VTABLE(u)->bus_query_pid_done(u, name, pid); | |
1808 | } | |
1809 | ||
d8d5ab98 | 1810 | int manager_open_serialization(Manager *m, FILE **_f) { |
b925e726 | 1811 | char *path = NULL; |
a16e1123 LP |
1812 | mode_t saved_umask; |
1813 | int fd; | |
1814 | FILE *f; | |
1815 | ||
1816 | assert(_f); | |
1817 | ||
67445f4e | 1818 | if (m->running_as == SYSTEMD_SYSTEM) |
2b583ce6 | 1819 | asprintf(&path, "/run/systemd/dump-%lu-XXXXXX", (unsigned long) getpid()); |
b925e726 LP |
1820 | else |
1821 | asprintf(&path, "/tmp/systemd-dump-%lu-XXXXXX", (unsigned long) getpid()); | |
d8d5ab98 | 1822 | |
b925e726 LP |
1823 | if (!path) |
1824 | return -ENOMEM; | |
a16e1123 LP |
1825 | |
1826 | saved_umask = umask(0077); | |
1827 | fd = mkostemp(path, O_RDWR|O_CLOEXEC); | |
1828 | umask(saved_umask); | |
1829 | ||
1830 | if (fd < 0) { | |
1831 | free(path); | |
1832 | return -errno; | |
1833 | } | |
1834 | ||
1835 | unlink(path); | |
1836 | ||
1837 | log_debug("Serializing state to %s", path); | |
1838 | free(path); | |
1839 | ||
01e10de3 LP |
1840 | f = fdopen(fd, "w+"); |
1841 | if (!f) | |
a16e1123 LP |
1842 | return -errno; |
1843 | ||
1844 | *_f = f; | |
1845 | ||
1846 | return 0; | |
1847 | } | |
1848 | ||
6b78f9b4 | 1849 | int manager_serialize(Manager *m, FILE *f, FDSet *fds, bool serialize_jobs) { |
a16e1123 LP |
1850 | Iterator i; |
1851 | Unit *u; | |
1852 | const char *t; | |
1853 | int r; | |
1854 | ||
1855 | assert(m); | |
1856 | assert(f); | |
1857 | assert(fds); | |
1858 | ||
a7556052 | 1859 | m->n_reloading ++; |
38c52d46 | 1860 | |
01d67b43 LP |
1861 | fprintf(f, "current-job-id=%i\n", m->current_job_id); |
1862 | fprintf(f, "taint-usr=%s\n", yes_no(m->taint_usr)); | |
33c5fae9 LP |
1863 | fprintf(f, "n-installed-jobs=%u\n", m->n_installed_jobs); |
1864 | fprintf(f, "n-failed-jobs=%u\n", m->n_failed_jobs); | |
01d67b43 | 1865 | |
915b3753 LP |
1866 | dual_timestamp_serialize(f, "firmware-timestamp", &m->firmware_timestamp); |
1867 | dual_timestamp_serialize(f, "kernel-timestamp", &m->kernel_timestamp); | |
1868 | dual_timestamp_serialize(f, "loader-timestamp", &m->loader_timestamp); | |
e9ddabc2 | 1869 | dual_timestamp_serialize(f, "initrd-timestamp", &m->initrd_timestamp); |
f38ed060 | 1870 | |
26a1efdf | 1871 | if (!in_initrd()) { |
915b3753 | 1872 | dual_timestamp_serialize(f, "userspace-timestamp", &m->userspace_timestamp); |
f38ed060 HH |
1873 | dual_timestamp_serialize(f, "finish-timestamp", &m->finish_timestamp); |
1874 | } | |
47a483a1 | 1875 | |
f2382a94 LP |
1876 | fputc('\n', f); |
1877 | ||
a16e1123 | 1878 | HASHMAP_FOREACH_KEY(u, t, m->units, i) { |
ac155bb8 | 1879 | if (u->id != t) |
a16e1123 LP |
1880 | continue; |
1881 | ||
1882 | if (!unit_can_serialize(u)) | |
1883 | continue; | |
1884 | ||
1885 | /* Start marker */ | |
ac155bb8 | 1886 | fputs(u->id, f); |
a16e1123 LP |
1887 | fputc('\n', f); |
1888 | ||
6b78f9b4 | 1889 | if ((r = unit_serialize(u, f, fds, serialize_jobs)) < 0) { |
a7556052 | 1890 | m->n_reloading --; |
a16e1123 | 1891 | return r; |
38c52d46 | 1892 | } |
a16e1123 LP |
1893 | } |
1894 | ||
a7556052 LP |
1895 | assert(m->n_reloading > 0); |
1896 | m->n_reloading --; | |
38c52d46 | 1897 | |
a16e1123 LP |
1898 | if (ferror(f)) |
1899 | return -EIO; | |
1900 | ||
b23de6af LP |
1901 | r = bus_fdset_add_all(m, fds); |
1902 | if (r < 0) | |
1903 | return r; | |
1904 | ||
a16e1123 LP |
1905 | return 0; |
1906 | } | |
1907 | ||
1908 | int manager_deserialize(Manager *m, FILE *f, FDSet *fds) { | |
1909 | int r = 0; | |
1910 | ||
1911 | assert(m); | |
1912 | assert(f); | |
1913 | ||
1914 | log_debug("Deserializing state..."); | |
1915 | ||
a7556052 | 1916 | m->n_reloading ++; |
82c64bf5 | 1917 | |
10f8e83c | 1918 | for (;;) { |
20c03b7b | 1919 | char line[LINE_MAX], *l; |
10f8e83c LP |
1920 | |
1921 | if (!fgets(line, sizeof(line), f)) { | |
1922 | if (feof(f)) | |
1923 | r = 0; | |
1924 | else | |
1925 | r = -errno; | |
1926 | ||
1927 | goto finish; | |
1928 | } | |
1929 | ||
1930 | char_array_0(line); | |
1931 | l = strstrip(line); | |
1932 | ||
1933 | if (l[0] == 0) | |
1934 | break; | |
1935 | ||
01d67b43 LP |
1936 | if (startswith(l, "current-job-id=")) { |
1937 | uint32_t id; | |
1938 | ||
1939 | if (safe_atou32(l+15, &id) < 0) | |
1940 | log_debug("Failed to parse current job id value %s", l+15); | |
1941 | else | |
1942 | m->current_job_id = MAX(m->current_job_id, id); | |
33c5fae9 LP |
1943 | } else if (startswith(l, "n-installed-jobs=")) { |
1944 | uint32_t n; | |
1945 | ||
1946 | if (safe_atou32(l+17, &n) < 0) | |
1947 | log_debug("Failed to parse installed jobs counter %s", l+17); | |
1948 | else | |
1949 | m->n_installed_jobs += n; | |
1950 | } else if (startswith(l, "n-failed-jobs=")) { | |
1951 | uint32_t n; | |
1952 | ||
1953 | if (safe_atou32(l+14, &n) < 0) | |
1954 | log_debug("Failed to parse failed jobs counter %s", l+14); | |
1955 | else | |
1956 | m->n_failed_jobs += n; | |
01d67b43 LP |
1957 | } else if (startswith(l, "taint-usr=")) { |
1958 | int b; | |
1959 | ||
1960 | if ((b = parse_boolean(l+10)) < 0) | |
1961 | log_debug("Failed to parse taint /usr flag %s", l+10); | |
1962 | else | |
1963 | m->taint_usr = m->taint_usr || b; | |
915b3753 LP |
1964 | } else if (startswith(l, "firmware-timestamp=")) |
1965 | dual_timestamp_deserialize(l+19, &m->firmware_timestamp); | |
1966 | else if (startswith(l, "loader-timestamp=")) | |
1967 | dual_timestamp_deserialize(l+17, &m->loader_timestamp); | |
1968 | else if (startswith(l, "kernel-timestamp=")) | |
1969 | dual_timestamp_deserialize(l+17, &m->kernel_timestamp); | |
1970 | else if (startswith(l, "initrd-timestamp=")) | |
e9ddabc2 | 1971 | dual_timestamp_deserialize(l+17, &m->initrd_timestamp); |
915b3753 LP |
1972 | else if (startswith(l, "userspace-timestamp=")) |
1973 | dual_timestamp_deserialize(l+20, &m->userspace_timestamp); | |
10717a1a | 1974 | else if (startswith(l, "finish-timestamp=")) |
799fd0fd | 1975 | dual_timestamp_deserialize(l+17, &m->finish_timestamp); |
10717a1a | 1976 | else |
10f8e83c LP |
1977 | log_debug("Unknown serialization item '%s'", l); |
1978 | } | |
1979 | ||
a16e1123 LP |
1980 | for (;;) { |
1981 | Unit *u; | |
1982 | char name[UNIT_NAME_MAX+2]; | |
1983 | ||
1984 | /* Start marker */ | |
1985 | if (!fgets(name, sizeof(name), f)) { | |
1986 | if (feof(f)) | |
10f8e83c LP |
1987 | r = 0; |
1988 | else | |
1989 | r = -errno; | |
a16e1123 | 1990 | |
82c64bf5 | 1991 | goto finish; |
a16e1123 LP |
1992 | } |
1993 | ||
1994 | char_array_0(name); | |
1995 | ||
bd0af849 ZJS |
1996 | r = manager_load_unit(m, strstrip(name), NULL, NULL, &u); |
1997 | if (r < 0) | |
82c64bf5 | 1998 | goto finish; |
a16e1123 | 1999 | |
01e10de3 LP |
2000 | r = unit_deserialize(u, f, fds); |
2001 | if (r < 0) | |
82c64bf5 | 2002 | goto finish; |
a16e1123 LP |
2003 | } |
2004 | ||
10f8e83c | 2005 | finish: |
82c64bf5 LP |
2006 | if (ferror(f)) { |
2007 | r = -EIO; | |
2008 | goto finish; | |
2009 | } | |
a16e1123 | 2010 | |
a7556052 LP |
2011 | assert(m->n_reloading > 0); |
2012 | m->n_reloading --; | |
82c64bf5 LP |
2013 | |
2014 | return r; | |
a16e1123 LP |
2015 | } |
2016 | ||
01e10de3 LP |
2017 | int manager_distribute_fds(Manager *m, FDSet *fds) { |
2018 | Unit *u; | |
2019 | Iterator i; | |
2020 | int r; | |
2021 | ||
2022 | assert(m); | |
2023 | ||
2024 | HASHMAP_FOREACH(u, m->units, i) { | |
2025 | ||
2026 | if (fdset_size(fds) <= 0) | |
2027 | break; | |
2028 | ||
2029 | if (UNIT_VTABLE(u)->distribute_fds) { | |
2030 | r = UNIT_VTABLE(u)->distribute_fds(u, fds); | |
2031 | if (r < 0) | |
2032 | return r; | |
2033 | } | |
2034 | } | |
2035 | ||
2036 | return 0; | |
2037 | } | |
2038 | ||
a16e1123 LP |
2039 | int manager_reload(Manager *m) { |
2040 | int r, q; | |
2041 | FILE *f; | |
2042 | FDSet *fds; | |
2043 | ||
2044 | assert(m); | |
2045 | ||
07719a21 LP |
2046 | r = manager_open_serialization(m, &f); |
2047 | if (r < 0) | |
a16e1123 LP |
2048 | return r; |
2049 | ||
a7556052 | 2050 | m->n_reloading ++; |
38c52d46 | 2051 | |
07719a21 LP |
2052 | fds = fdset_new(); |
2053 | if (!fds) { | |
a7556052 | 2054 | m->n_reloading --; |
a16e1123 LP |
2055 | r = -ENOMEM; |
2056 | goto finish; | |
2057 | } | |
2058 | ||
6b78f9b4 | 2059 | r = manager_serialize(m, f, fds, true); |
07719a21 | 2060 | if (r < 0) { |
a7556052 | 2061 | m->n_reloading --; |
a16e1123 | 2062 | goto finish; |
38c52d46 | 2063 | } |
a16e1123 LP |
2064 | |
2065 | if (fseeko(f, 0, SEEK_SET) < 0) { | |
a7556052 | 2066 | m->n_reloading --; |
a16e1123 LP |
2067 | r = -errno; |
2068 | goto finish; | |
2069 | } | |
2070 | ||
2071 | /* From here on there is no way back. */ | |
2072 | manager_clear_jobs_and_units(m); | |
5a1e9937 | 2073 | manager_undo_generators(m); |
84e3543e | 2074 | lookup_paths_free(&m->lookup_paths); |
2ded0c04 | 2075 | |
07719a21 | 2076 | /* Find new unit paths */ |
5a1e9937 LP |
2077 | manager_run_generators(m); |
2078 | ||
07719a21 LP |
2079 | q = lookup_paths_init( |
2080 | &m->lookup_paths, m->running_as, true, | |
2081 | m->generator_unit_path, | |
2082 | m->generator_unit_path_early, | |
2083 | m->generator_unit_path_late); | |
2084 | if (q < 0) | |
2085 | r = q; | |
2086 | ||
5a1e9937 LP |
2087 | manager_build_unit_path_cache(m); |
2088 | ||
a16e1123 | 2089 | /* First, enumerate what we can from all config files */ |
07719a21 LP |
2090 | q = manager_enumerate(m); |
2091 | if (q < 0) | |
a16e1123 LP |
2092 | r = q; |
2093 | ||
2094 | /* Second, deserialize our stored data */ | |
07719a21 LP |
2095 | q = manager_deserialize(m, f, fds); |
2096 | if (q < 0) | |
a16e1123 LP |
2097 | r = q; |
2098 | ||
2099 | fclose(f); | |
2100 | f = NULL; | |
2101 | ||
2102 | /* Third, fire things up! */ | |
07719a21 LP |
2103 | q = manager_coldplug(m); |
2104 | if (q < 0) | |
a16e1123 LP |
2105 | r = q; |
2106 | ||
a7556052 LP |
2107 | assert(m->n_reloading > 0); |
2108 | m->n_reloading--; | |
9f611ad8 | 2109 | |
a16e1123 LP |
2110 | finish: |
2111 | if (f) | |
2112 | fclose(f); | |
2113 | ||
2114 | if (fds) | |
2115 | fdset_free(fds); | |
2116 | ||
2117 | return r; | |
2118 | } | |
2119 | ||
9e58ff9c LP |
2120 | bool manager_is_booting_or_shutting_down(Manager *m) { |
2121 | Unit *u; | |
2122 | ||
2123 | assert(m); | |
2124 | ||
2125 | /* Is the initial job still around? */ | |
bacbccb7 | 2126 | if (manager_get_job(m, m->default_unit_job_id)) |
9e58ff9c LP |
2127 | return true; |
2128 | ||
2129 | /* Is there a job for the shutdown target? */ | |
27d340c7 LP |
2130 | u = manager_get_unit(m, SPECIAL_SHUTDOWN_TARGET); |
2131 | if (u) | |
ac155bb8 | 2132 | return !!u->job; |
9e58ff9c LP |
2133 | |
2134 | return false; | |
2135 | } | |
2136 | ||
fdf20a31 | 2137 | void manager_reset_failed(Manager *m) { |
5632e374 LP |
2138 | Unit *u; |
2139 | Iterator i; | |
2140 | ||
2141 | assert(m); | |
2142 | ||
2143 | HASHMAP_FOREACH(u, m->units, i) | |
fdf20a31 | 2144 | unit_reset_failed(u); |
5632e374 LP |
2145 | } |
2146 | ||
8f6df3fa LP |
2147 | bool manager_unit_pending_inactive(Manager *m, const char *name) { |
2148 | Unit *u; | |
2149 | ||
2150 | assert(m); | |
2151 | assert(name); | |
2152 | ||
2153 | /* Returns true if the unit is inactive or going down */ | |
bd0af849 ZJS |
2154 | u = manager_get_unit(m, name); |
2155 | if (!u) | |
8f6df3fa LP |
2156 | return true; |
2157 | ||
18ffdfda | 2158 | return unit_pending_inactive(u); |
8f6df3fa LP |
2159 | } |
2160 | ||
b0c918b9 | 2161 | void manager_check_finished(Manager *m) { |
7ceba241 | 2162 | char userspace[FORMAT_TIMESPAN_MAX], initrd[FORMAT_TIMESPAN_MAX], kernel[FORMAT_TIMESPAN_MAX], sum[FORMAT_TIMESPAN_MAX]; |
915b3753 | 2163 | usec_t firmware_usec, loader_usec, kernel_usec, initrd_usec, userspace_usec, total_usec; |
b0c918b9 LP |
2164 | |
2165 | assert(m); | |
2166 | ||
f2b68789 | 2167 | if (hashmap_size(m->jobs) > 0) |
b0c918b9 LP |
2168 | return; |
2169 | ||
f2b68789 LP |
2170 | /* Notify Type=idle units that we are done now */ |
2171 | close_pipe(m->idle_pipe); | |
2172 | ||
af6da548 LP |
2173 | /* Turn off confirm spawn now */ |
2174 | m->confirm_spawn = false; | |
2175 | ||
f2b68789 | 2176 | if (dual_timestamp_is_set(&m->finish_timestamp)) |
b0c918b9 LP |
2177 | return; |
2178 | ||
2179 | dual_timestamp_get(&m->finish_timestamp); | |
2180 | ||
67445f4e | 2181 | if (m->running_as == SYSTEMD_SYSTEM && detect_container(NULL) <= 0) { |
e03ae661 | 2182 | |
915b3753 LP |
2183 | /* Note that m->kernel_usec.monotonic is always at 0, |
2184 | * and m->firmware_usec.monotonic and | |
2185 | * m->loader_usec.monotonic should be considered | |
2186 | * negative values. */ | |
2187 | ||
7ceba241 LP |
2188 | firmware_usec = m->firmware_timestamp.monotonic - m->loader_timestamp.monotonic; |
2189 | loader_usec = m->loader_timestamp.monotonic - m->kernel_timestamp.monotonic; | |
915b3753 | 2190 | userspace_usec = m->finish_timestamp.monotonic - m->userspace_timestamp.monotonic; |
7ceba241 | 2191 | total_usec = m->firmware_timestamp.monotonic + m->finish_timestamp.monotonic; |
18fa6b27 | 2192 | |
e9ddabc2 | 2193 | if (dual_timestamp_is_set(&m->initrd_timestamp)) { |
18fa6b27 | 2194 | |
915b3753 LP |
2195 | kernel_usec = m->initrd_timestamp.monotonic - m->kernel_timestamp.monotonic; |
2196 | initrd_usec = m->userspace_timestamp.monotonic - m->initrd_timestamp.monotonic; | |
18fa6b27 | 2197 | |
81270860 LP |
2198 | if (!log_on_console()) |
2199 | log_struct(LOG_INFO, | |
1ca6783f | 2200 | MESSAGE_ID(SD_MESSAGE_STARTUP_FINISHED), |
81270860 LP |
2201 | "KERNEL_USEC=%llu", (unsigned long long) kernel_usec, |
2202 | "INITRD_USEC=%llu", (unsigned long long) initrd_usec, | |
2203 | "USERSPACE_USEC=%llu", (unsigned long long) userspace_usec, | |
2204 | "MESSAGE=Startup finished in %s (kernel) + %s (initrd) + %s (userspace) = %s.", | |
2205 | format_timespan(kernel, sizeof(kernel), kernel_usec), | |
2206 | format_timespan(initrd, sizeof(initrd), initrd_usec), | |
2207 | format_timespan(userspace, sizeof(userspace), userspace_usec), | |
2208 | format_timespan(sum, sizeof(sum), total_usec), | |
2209 | NULL); | |
18fa6b27 | 2210 | } else { |
915b3753 | 2211 | kernel_usec = m->userspace_timestamp.monotonic - m->kernel_timestamp.monotonic; |
18fa6b27 LP |
2212 | initrd_usec = 0; |
2213 | ||
81270860 LP |
2214 | if (!log_on_console()) |
2215 | log_struct(LOG_INFO, | |
1ca6783f | 2216 | MESSAGE_ID(SD_MESSAGE_STARTUP_FINISHED), |
81270860 LP |
2217 | "KERNEL_USEC=%llu", (unsigned long long) kernel_usec, |
2218 | "USERSPACE_USEC=%llu", (unsigned long long) userspace_usec, | |
2219 | "MESSAGE=Startup finished in %s (kernel) + %s (userspace) = %s.", | |
2220 | format_timespan(kernel, sizeof(kernel), kernel_usec), | |
2221 | format_timespan(userspace, sizeof(userspace), userspace_usec), | |
2222 | format_timespan(sum, sizeof(sum), total_usec), | |
2223 | NULL); | |
18fa6b27 LP |
2224 | } |
2225 | } else { | |
915b3753 LP |
2226 | firmware_usec = loader_usec = initrd_usec = kernel_usec = 0; |
2227 | total_usec = userspace_usec = m->finish_timestamp.monotonic - m->userspace_timestamp.monotonic; | |
877d54e9 | 2228 | |
81270860 LP |
2229 | if (!log_on_console()) |
2230 | log_struct(LOG_INFO, | |
1ca6783f | 2231 | MESSAGE_ID(SD_MESSAGE_STARTUP_FINISHED), |
81270860 LP |
2232 | "USERSPACE_USEC=%llu", (unsigned long long) userspace_usec, |
2233 | "MESSAGE=Startup finished in %s.", | |
2234 | format_timespan(sum, sizeof(sum), total_usec), | |
2235 | NULL); | |
18fa6b27 | 2236 | } |
b0c918b9 | 2237 | |
915b3753 | 2238 | bus_broadcast_finished(m, firmware_usec, loader_usec, kernel_usec, initrd_usec, userspace_usec, total_usec); |
530345e7 LP |
2239 | |
2240 | sd_notifyf(false, | |
2241 | "READY=1\nSTATUS=Startup finished in %s.", | |
2242 | format_timespan(sum, sizeof(sum), total_usec)); | |
b0c918b9 LP |
2243 | } |
2244 | ||
07719a21 LP |
2245 | static int create_generator_dir(Manager *m, char **generator, const char *name) { |
2246 | char *p; | |
2247 | int r; | |
2248 | ||
2249 | assert(m); | |
2250 | assert(generator); | |
2251 | assert(name); | |
2252 | ||
2253 | if (*generator) | |
2254 | return 0; | |
2255 | ||
67445f4e | 2256 | if (m->running_as == SYSTEMD_SYSTEM && getpid() == 1) { |
07719a21 LP |
2257 | |
2258 | p = strappend("/run/systemd/", name); | |
0d0f0c50 SL |
2259 | if (!p) |
2260 | return log_oom(); | |
07719a21 | 2261 | |
d2e54fae | 2262 | r = mkdir_p_label(p, 0755); |
07719a21 LP |
2263 | if (r < 0) { |
2264 | log_error("Failed to create generator directory: %s", strerror(-r)); | |
2265 | free(p); | |
2266 | return r; | |
2267 | } | |
2268 | } else { | |
b7def684 | 2269 | p = strjoin("/tmp/systemd-", name, ".XXXXXX", NULL); |
0d0f0c50 SL |
2270 | if (!p) |
2271 | return log_oom(); | |
07719a21 LP |
2272 | |
2273 | if (!mkdtemp(p)) { | |
2274 | free(p); | |
2275 | log_error("Failed to create generator directory: %m"); | |
2276 | return -errno; | |
2277 | } | |
2278 | } | |
2279 | ||
2280 | *generator = p; | |
2281 | return 0; | |
2282 | } | |
2283 | ||
2284 | static void trim_generator_dir(Manager *m, char **generator) { | |
2285 | assert(m); | |
2286 | assert(generator); | |
2287 | ||
2288 | if (!*generator) | |
2289 | return; | |
2290 | ||
2291 | if (rmdir(*generator) >= 0) { | |
2292 | free(*generator); | |
2293 | *generator = NULL; | |
2294 | } | |
2295 | ||
2296 | return; | |
2297 | } | |
2298 | ||
5a1e9937 LP |
2299 | void manager_run_generators(Manager *m) { |
2300 | DIR *d = NULL; | |
5a1e9937 | 2301 | const char *generator_path; |
07719a21 | 2302 | const char *argv[5]; |
07f8a4aa | 2303 | mode_t u; |
07719a21 | 2304 | int r; |
5a1e9937 LP |
2305 | |
2306 | assert(m); | |
2307 | ||
67445f4e | 2308 | generator_path = m->running_as == SYSTEMD_SYSTEM ? SYSTEM_GENERATOR_PATH : USER_GENERATOR_PATH; |
07719a21 LP |
2309 | d = opendir(generator_path); |
2310 | if (!d) { | |
5a1e9937 LP |
2311 | if (errno == ENOENT) |
2312 | return; | |
2313 | ||
2314 | log_error("Failed to enumerate generator directory: %m"); | |
2315 | return; | |
2316 | } | |
2317 | ||
07719a21 LP |
2318 | r = create_generator_dir(m, &m->generator_unit_path, "generator"); |
2319 | if (r < 0) | |
2320 | goto finish; | |
f1d19aa4 | 2321 | |
07719a21 LP |
2322 | r = create_generator_dir(m, &m->generator_unit_path_early, "generator.early"); |
2323 | if (r < 0) | |
2324 | goto finish; | |
5a1e9937 | 2325 | |
07719a21 LP |
2326 | r = create_generator_dir(m, &m->generator_unit_path_late, "generator.late"); |
2327 | if (r < 0) | |
2328 | goto finish; | |
5a1e9937 | 2329 | |
83cc030f LP |
2330 | argv[0] = NULL; /* Leave this empty, execute_directory() will fill something in */ |
2331 | argv[1] = m->generator_unit_path; | |
07719a21 LP |
2332 | argv[2] = m->generator_unit_path_early; |
2333 | argv[3] = m->generator_unit_path_late; | |
2334 | argv[4] = NULL; | |
5a1e9937 | 2335 | |
07f8a4aa | 2336 | u = umask(0022); |
83cc030f | 2337 | execute_directory(generator_path, d, (char**) argv); |
07f8a4aa | 2338 | umask(u); |
5a1e9937 | 2339 | |
07719a21 LP |
2340 | trim_generator_dir(m, &m->generator_unit_path); |
2341 | trim_generator_dir(m, &m->generator_unit_path_early); | |
2342 | trim_generator_dir(m, &m->generator_unit_path_late); | |
5a1e9937 LP |
2343 | |
2344 | finish: | |
2345 | if (d) | |
2346 | closedir(d); | |
5a1e9937 LP |
2347 | } |
2348 | ||
07719a21 | 2349 | static void remove_generator_dir(Manager *m, char **generator) { |
5a1e9937 | 2350 | assert(m); |
07719a21 | 2351 | assert(generator); |
5a1e9937 | 2352 | |
07719a21 | 2353 | if (!*generator) |
5a1e9937 LP |
2354 | return; |
2355 | ||
07719a21 LP |
2356 | strv_remove(m->lookup_paths.unit_path, *generator); |
2357 | rm_rf(*generator, false, true, false); | |
5a1e9937 | 2358 | |
07719a21 LP |
2359 | free(*generator); |
2360 | *generator = NULL; | |
2361 | } | |
2362 | ||
2363 | void manager_undo_generators(Manager *m) { | |
2364 | assert(m); | |
2365 | ||
2366 | remove_generator_dir(m, &m->generator_unit_path); | |
2367 | remove_generator_dir(m, &m->generator_unit_path_early); | |
2368 | remove_generator_dir(m, &m->generator_unit_path_late); | |
5a1e9937 LP |
2369 | } |
2370 | ||
06d4c99a LP |
2371 | int manager_set_default_controllers(Manager *m, char **controllers) { |
2372 | char **l; | |
2373 | ||
2374 | assert(m); | |
2375 | ||
9156e799 LP |
2376 | l = strv_copy(controllers); |
2377 | if (!l) | |
06d4c99a LP |
2378 | return -ENOMEM; |
2379 | ||
2380 | strv_free(m->default_controllers); | |
2381 | m->default_controllers = l; | |
2382 | ||
b59e2465 | 2383 | cg_shorten_controllers(m->default_controllers); |
9156e799 | 2384 | |
06d4c99a LP |
2385 | return 0; |
2386 | } | |
2387 | ||
c93ff2e9 FC |
2388 | int manager_set_default_rlimits(Manager *m, struct rlimit **default_rlimit) { |
2389 | int i; | |
2390 | ||
2391 | assert(m); | |
2392 | ||
2393 | for (i = 0; i < RLIMIT_NLIMITS; i++) { | |
07719a21 LP |
2394 | if (!default_rlimit[i]) |
2395 | continue; | |
c93ff2e9 | 2396 | |
07719a21 LP |
2397 | m->rlimit[i] = newdup(struct rlimit, default_rlimit[i], 1); |
2398 | if (!m->rlimit[i]) | |
2399 | return -ENOMEM; | |
c93ff2e9 FC |
2400 | } |
2401 | ||
2402 | return 0; | |
2403 | } | |
2404 | ||
4cfa2c99 | 2405 | void manager_recheck_journal(Manager *m) { |
f1dd0c3f LP |
2406 | Unit *u; |
2407 | ||
2408 | assert(m); | |
2409 | ||
67445f4e | 2410 | if (m->running_as != SYSTEMD_SYSTEM) |
f1dd0c3f LP |
2411 | return; |
2412 | ||
731a676c LP |
2413 | u = manager_get_unit(m, SPECIAL_JOURNALD_SOCKET); |
2414 | if (u && SOCKET(u)->state != SOCKET_RUNNING) { | |
4cfa2c99 | 2415 | log_close_journal(); |
731a676c | 2416 | return; |
f1dd0c3f LP |
2417 | } |
2418 | ||
731a676c LP |
2419 | u = manager_get_unit(m, SPECIAL_JOURNALD_SERVICE); |
2420 | if (u && SERVICE(u)->state != SERVICE_RUNNING) { | |
4cfa2c99 | 2421 | log_close_journal(); |
731a676c LP |
2422 | return; |
2423 | } | |
f1dd0c3f | 2424 | |
731a676c LP |
2425 | /* Hmm, OK, so the socket is fully up and the service is up |
2426 | * too, then let's make use of the thing. */ | |
f1dd0c3f LP |
2427 | log_open(); |
2428 | } | |
2429 | ||
27d340c7 LP |
2430 | void manager_set_show_status(Manager *m, bool b) { |
2431 | assert(m); | |
2432 | ||
67445f4e | 2433 | if (m->running_as != SYSTEMD_SYSTEM) |
27d340c7 LP |
2434 | return; |
2435 | ||
2436 | m->show_status = b; | |
2437 | ||
2438 | if (b) | |
2439 | touch("/run/systemd/show-status"); | |
2440 | else | |
2441 | unlink("/run/systemd/show-status"); | |
2442 | } | |
2443 | ||
2444 | bool manager_get_show_status(Manager *m) { | |
2445 | assert(m); | |
2446 | ||
67445f4e | 2447 | if (m->running_as != SYSTEMD_SYSTEM) |
27d340c7 LP |
2448 | return false; |
2449 | ||
2450 | if (m->show_status) | |
2451 | return true; | |
2452 | ||
2453 | /* If Plymouth is running make sure we show the status, so | |
2454 | * that there's something nice to see when people press Esc */ | |
2455 | ||
2456 | return plymouth_running(); | |
2457 | } | |
68b29a9f LP |
2458 | |
2459 | void watch_init(Watch *w) { | |
2460 | assert(w); | |
2461 | ||
2462 | w->type = WATCH_INVALID; | |
2463 | w->fd = -1; | |
2464 | } |