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