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