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