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