+ assert(m);
+
+ assert_se(sigemptyset(&mask) == 0);
+ assert_se(sigaddset(&mask, SIGCHLD) == 0);
+ assert_se(sigaddset(&mask, SIGINT) == 0); /* Kernel sends us this on control-alt-del */
+ assert_se(sigaddset(&mask, SIGWINCH) == 0); /* Kernel sends us this on kbrequest (alt-arrowup) */
+ assert_se(sigaddset(&mask, SIGTERM) == 0);
+ assert_se(sigaddset(&mask, SIGHUP) == 0);
+ assert_se(sigaddset(&mask, SIGUSR1) == 0);
+ assert_se(sigaddset(&mask, SIGUSR2) == 0);
+ assert_se(sigaddset(&mask, SIGPIPE) == 0);
+ assert_se(sigaddset(&mask, SIGPWR) == 0);
+ assert_se(sigaddset(&mask, SIGTTIN) == 0);
+ assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
+
+ m->signal_watch.type = WATCH_SIGNAL;
+ if ((m->signal_watch.fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC)) < 0)
+ return -errno;
+
+ zero(ev);
+ ev.events = EPOLLIN;
+ ev.data.ptr = &m->signal_watch;
+
+ if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->signal_watch.fd, &ev) < 0)
+ return -errno;
+
+ if (m->running_as == MANAGER_INIT) {
+ /* Enable that we get SIGINT on control-alt-del */
+ if (reboot(RB_DISABLE_CAD) < 0)
+ log_warning("Failed to enable ctrl-alt-del handling: %s", strerror(errno));
+
+ /* Enable that we get SIGWINCH on kbrequest */
+ if (ioctl(0, KDSIGACCEPT, SIGWINCH) < 0)
+ log_warning("Failed to enable kbrequest handling: %s", strerror(errno));
+ }
+
+ return 0;
+}
+
+static char** session_dirs(void) {
+ const char *home, *e;
+ char *config_home = NULL, *data_home = NULL;
+ char **config_dirs = NULL, **data_dirs = NULL;
+ char **r = NULL, **t;
+
+ /* Implement the mechanisms defined in
+ *
+ * http://standards.freedesktop.org/basedir-spec/basedir-spec-0.6.html
+ *
+ * We look in both the config and the data dirs because we
+ * want to encourage that distributors ship their unit files
+ * as data, and allow overriding as configuration.
+ */
+
+ home = getenv("HOME");
+
+ if ((e = getenv("XDG_CONFIG_HOME"))) {
+ if (asprintf(&config_home, "%s/systemd/session", e) < 0)
+ goto fail;
+
+ } else if (home) {
+ if (asprintf(&config_home, "%s/.config/systemd/session", home) < 0)
+ goto fail;
+ }
+
+ if ((e = getenv("XDG_CONFIG_DIRS")))
+ config_dirs = strv_split(e, ":");
+ else
+ config_dirs = strv_new("/etc/xdg", NULL);
+
+ if (!config_dirs)
+ goto fail;
+
+ if ((e = getenv("XDG_DATA_HOME"))) {
+ if (asprintf(&data_home, "%s/systemd/session", e) < 0)
+ goto fail;
+
+ } else if (home) {
+ if (asprintf(&data_home, "%s/.local/share/systemd/session", home) < 0)
+ goto fail;
+ }
+
+ if ((e = getenv("XDG_DATA_DIRS")))
+ data_dirs = strv_split(e, ":");
+ else
+ data_dirs = strv_new("/usr/local/share", "/usr/share", NULL);
+
+ if (!data_dirs)
+ goto fail;
+
+ /* Now merge everything we found. */
+ if (config_home) {
+ if (!(t = strv_append(r, config_home)))
+ goto fail;
+ strv_free(r);
+ r = t;
+ }
+
+ if (!(t = strv_merge_concat(r, config_dirs, "/systemd/session")))
+ goto finish;
+ strv_free(r);
+ r = t;
+
+ if (!(t = strv_append(r, SESSION_CONFIG_UNIT_PATH)))
+ goto fail;
+ strv_free(r);
+ r = t;
+
+ if (data_home) {
+ if (!(t = strv_append(r, data_home)))
+ goto fail;
+ strv_free(r);
+ r = t;
+ }
+
+ if (!(t = strv_merge_concat(r, data_dirs, "/systemd/session")))
+ goto fail;
+ strv_free(r);
+ r = t;
+
+ if (!(t = strv_append(r, SESSION_DATA_UNIT_PATH)))
+ goto fail;
+ strv_free(r);
+ r = t;
+
+ if (!strv_path_make_absolute_cwd(r))
+ goto fail;
+
+finish:
+ free(config_home);
+ strv_free(config_dirs);
+ free(data_home);
+ strv_free(data_dirs);
+
+ return r;
+
+fail:
+ strv_free(r);
+ r = NULL;
+ goto finish;
+}
+
+static int manager_find_paths(Manager *m) {
+ const char *e;
+ char *t;
+
+ assert(m);
+
+ /* First priority is whatever has been passed to us via env
+ * vars */
+ if ((e = getenv("SYSTEMD_UNIT_PATH")))
+ if (!(m->unit_path = split_path_and_make_absolute(e)))
+ return -ENOMEM;
+
+ if (strv_isempty(m->unit_path)) {
+
+ /* Nothing is set, so let's figure something out. */
+ strv_free(m->unit_path);
+
+ if (m->running_as == MANAGER_SESSION) {
+ if (!(m->unit_path = session_dirs()))
+ return -ENOMEM;
+ } else
+ if (!(m->unit_path = strv_new(
+ SYSTEM_CONFIG_UNIT_PATH, /* /etc/systemd/system/ */
+ SYSTEM_DATA_UNIT_PATH, /* /lib/systemd/system/ */
+ NULL)))
+ return -ENOMEM;
+ }
+
+ if (m->running_as == MANAGER_INIT) {
+ /* /etc/init.d/ compativility does not matter to users */
+
+ if ((e = getenv("SYSTEMD_SYSVINIT_PATH")))
+ if (!(m->sysvinit_path = split_path_and_make_absolute(e)))
+ return -ENOMEM;
+
+ if (strv_isempty(m->sysvinit_path)) {
+ strv_free(m->sysvinit_path);
+
+ if (!(m->sysvinit_path = strv_new(
+ SYSTEM_SYSVINIT_PATH, /* /etc/init.d/ */
+ NULL)))
+ return -ENOMEM;
+ }
+
+ if ((e = getenv("SYSTEMD_SYSVRCND_PATH")))
+ if (!(m->sysvrcnd_path = split_path_and_make_absolute(e)))
+ return -ENOMEM;
+
+ if (strv_isempty(m->sysvrcnd_path)) {
+ strv_free(m->sysvrcnd_path);
+
+ if (!(m->sysvrcnd_path = strv_new(
+ SYSTEM_SYSVRCND_PATH, /* /etc/rcN.d/ */
+ NULL)))
+ return -ENOMEM;
+ }
+ }
+
+ strv_uniq(m->unit_path);
+ strv_uniq(m->sysvinit_path);
+ strv_uniq(m->sysvrcnd_path);
+
+ assert(!strv_isempty(m->unit_path));
+ if (!(t = strv_join(m->unit_path, "\n\t")))
+ return -ENOMEM;
+ log_debug("Looking for unit files in:\n\t%s", t);
+ free(t);
+
+ if (!strv_isempty(m->sysvinit_path)) {
+
+ if (!(t = strv_join(m->sysvinit_path, "\n\t")))
+ return -ENOMEM;
+
+ log_debug("Looking for SysV init scripts in:\n\t%s", t);
+ free(t);
+ } else
+ log_debug("Ignoring SysV init scripts.");
+
+ if (!strv_isempty(m->sysvrcnd_path)) {
+
+ if (!(t = strv_join(m->sysvrcnd_path, "\n\t")))
+ return -ENOMEM;
+
+ log_debug("Looking for SysV rcN.d links in:\n\t%s", t);
+ free(t);
+ } else
+ log_debug("Ignoring SysV rcN.d links.");
+
+ return 0;
+}
+
+int manager_new(ManagerRunningAs running_as, Manager **_m) {
+ Manager *m;
+ int r = -ENOMEM;
+
+ assert(_m);
+ assert(running_as >= 0);
+ assert(running_as < _MANAGER_RUNNING_AS_MAX);
+