#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
+#include <dirent.h>
#include "manager.h"
#include "hashmap.h"
zero(sa);
sa.sa.sa_family = AF_UNIX;
- if (m->running_as == MANAGER_SESSION)
+ if (getpid() != 1)
snprintf(sa.un.sun_path+1, sizeof(sa.un.sun_path)-1, NOTIFY_SOCKET "/%llu", random_ull());
else
strncpy(sa.un.sun_path+1, NOTIFY_SOCKET, sizeof(sa.un.sun_path)-1);
- if (bind(m->notify_watch.fd, &sa.sa, sizeof(sa)) < 0) {
+ if (bind(m->notify_watch.fd, &sa.sa, sizeof(sa_family_t) + 1 + strlen(sa.un.sun_path+1)) < 0) {
log_error("bind() failed: %m");
return -errno;
}
int manager_new(ManagerRunningAs running_as, Manager **_m) {
Manager *m;
int r = -ENOMEM;
- char *p;
assert(_m);
assert(running_as >= 0);
if ((r = bus_init(m)) < 0)
goto fail;
- if (asprintf(&p, "%s/%s", m->cgroup_mount_point, m->cgroup_hierarchy) < 0) {
- r = -ENOMEM;
- goto fail;
- }
-
- m->pin_cgroupfs_fd = open(p, O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOCTTY|O_NONBLOCK);
- free(p);
-
*_m = m;
return 0;
/* If we reexecute ourselves, we keep the root cgroup
* around */
- if (m->exit_code != MANAGER_REEXECUTE)
- manager_shutdown_cgroup(m);
+ manager_shutdown_cgroup(m, m->exit_code != MANAGER_REEXECUTE);
bus_done(m);
lookup_paths_free(&m->lookup_paths);
strv_free(m->environment);
- free(m->cgroup_controller);
- free(m->cgroup_hierarchy);
- free(m->cgroup_mount_point);
-
hashmap_free(m->cgroup_bondings);
-
- if (m->pin_cgroupfs_fd >= 0)
- close_nointr_nofail(m->pin_cgroupfs_fd);
+ set_free_free(m->unit_path_cache);
free(m);
}
return r;
}
+static void manager_build_unit_path_cache(Manager *m) {
+ char **i;
+ DIR *d = NULL;
+ int r;
+
+ assert(m);
+
+ set_free_free(m->unit_path_cache);
+
+ if (!(m->unit_path_cache = set_new(string_hash_func, string_compare_func))) {
+ log_error("Failed to allocate unit path cache.");
+ return;
+ }
+
+ /* This simply builds a list of files we know exist, so that
+ * we don't always have to go to disk */
+
+ STRV_FOREACH(i, m->lookup_paths.unit_path) {
+ struct dirent *de;
+
+ if (!(d = opendir(*i))) {
+ log_error("Failed to open directory: %m");
+ continue;
+ }
+
+ while ((de = readdir(d))) {
+ char *p;
+
+ if (ignore_file(de->d_name))
+ continue;
+
+ if (asprintf(&p, "%s/%s", streq(*i, "/") ? "" : *i, de->d_name) < 0) {
+ r = -ENOMEM;
+ goto fail;
+ }
+
+ if ((r = set_put(m->unit_path_cache, p)) < 0) {
+ free(p);
+ goto fail;
+ }
+ }
+
+ closedir(d);
+ d = NULL;
+ }
+
+ return;
+
+fail:
+ log_error("Failed to build unit path cache: %s", strerror(-r));
+
+ set_free_free(m->unit_path_cache);
+ m->unit_path_cache = NULL;
+
+ if (d)
+ closedir(d);
+}
+
int manager_startup(Manager *m, FILE *serialization, FDSet *fds) {
int r, q;
assert(m);
+ manager_build_unit_path_cache(m);
+
+ /* If we will deserialize make sure that during enumeration
+ * this is already known, so we increase the counter here
+ * already */
+ if (serialization)
+ m->n_deserializing ++;
+
/* First, enumerate what we can from all config files */
r = manager_enumerate(m);
if ((q = manager_coldplug(m)) < 0)
r = q;
+ if (serialization) {
+ assert(m->n_deserializing > 0);
+ m->n_deserializing --;
+ }
+
/* Now that the initial devices are available, let's see if we
* can write the utmp file */
manager_write_utmp_reboot(m);
job_add_to_run_queue(j);
job_add_to_dbus_queue(j);
+ job_start_timer(j);
}
/* As last step, kill all remaining job dependencies. */
assert(type < _JOB_TYPE_MAX);
assert(unit);
- if (unit->meta.load_state != UNIT_LOADED) {
- dbus_set_error(e, BUS_ERROR_LOAD_FAILED, "Unit %s failed to load.", unit->meta.id);
+ if (type != JOB_STOP &&
+ unit->meta.load_state != UNIT_LOADED) {
+ dbus_set_error(e, BUS_ERROR_LOAD_FAILED, "Unit %s failed to load. See logs for details.", unit->meta.id);
return -EINVAL;
}
goto fail;
SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_REQUIRES_OVERRIDABLE], i)
- if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, !override, override, e, NULL)) < 0 && r != -EBADR)
- log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->meta.id, strerror(-r));
+ if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, !override, override, e, NULL)) < 0 && r != -EBADR) {
+ log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->meta.id, bus_error(e, r));
+ dbus_error_free(e);
+ }
SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_WANTS], i)
- if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, false, false, e, NULL)) < 0)
- log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->meta.id, strerror(-r));
+ if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, false, false, e, NULL)) < 0) {
+ log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->meta.id, bus_error(e, r));
+ dbus_error_free(e);
+ }
SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_REQUISITE], i)
if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_ACTIVE, dep, ret, true, override, e, NULL)) < 0 && r != -EBADR)
goto fail;
SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_REQUISITE_OVERRIDABLE], i)
- if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_ACTIVE, dep, ret, !override, override, e, NULL)) < 0 && r != -EBADR)
- log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->meta.id, strerror(-r));
+ if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_ACTIVE, dep, ret, !override, override, e, NULL)) < 0 && r != -EBADR) {
+ log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->meta.id, bus_error(e, r));
+ dbus_error_free(e);
+ }
SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_CONFLICTS], i)
if ((r = transaction_add_job_and_dependencies(m, JOB_STOP, dep, ret, true, override, e, NULL)) < 0 && r != -EBADR)
ucred = (struct ucred*) CMSG_DATA(&control.cmsghdr);
- if (!(u = hashmap_get(m->watch_pids, UINT32_TO_PTR(ucred->pid))))
+ if (!(u = hashmap_get(m->watch_pids, LONG_TO_PTR(ucred->pid))))
if (!(u = cgroup_unit_by_pid(m, ucred->pid))) {
log_warning("Cannot find unit for notify message of PID %lu.", (unsigned long) ucred->pid);
continue;
return r;
/* And now figure out the unit this belongs to */
- if (!(u = hashmap_get(m->watch_pids, UINT32_TO_PTR(si.si_pid))))
+ if (!(u = hashmap_get(m->watch_pids, LONG_TO_PTR(si.si_pid))))
u = cgroup_unit_by_pid(m, si.si_pid);
/* And now, we actually reap the zombie. */
(long unsigned) si.si_pid,
sigchld_code_to_string(si.si_code),
si.si_status,
- strna(si.si_code == CLD_EXITED ? exit_status_to_string(si.si_status) : strsignal(si.si_status)));
+ strna(si.si_code == CLD_EXITED ? exit_status_to_string(si.si_status) : signal_to_string(si.si_status)));
if (!u)
continue;
- log_debug("Child %llu belongs to %s", (long long unsigned) si.si_pid, u->meta.id);
+ log_debug("Child %lu belongs to %s", (long unsigned) si.si_pid, u->meta.id);
- hashmap_remove(m->watch_pids, UINT32_TO_PTR(si.si_pid));
+ hashmap_remove(m->watch_pids, LONG_TO_PTR(si.si_pid));
UNIT_VTABLE(u)->sigchld_event(u, si.si_pid, si.si_code, si.si_status);
}
dbus_error_init(&error);
+ log_info("Activating special unit %s", name);
+
if ((r = manager_add_job_by_name(m, JOB_START, name, mode, true, &error, NULL)) < 0)
log_error("Failed to enqueue %s job: %s", name, bus_error(&error, r));
return -errno;
}
+ log_debug("Received SIG%s", strna(signal_to_string(sfsi.ssi_signo)));
+
switch (sfsi.ssi_signo) {
case SIGCHLD:
static const char * const table[] = {
[0] = SPECIAL_DEFAULT_TARGET,
[1] = SPECIAL_RESCUE_TARGET,
- [2] = SPECIAL_EMERGENCY_SERVICE,
+ [2] = SPECIAL_EMERGENCY_TARGET,
[3] = SPECIAL_HALT_TARGET,
[4] = SPECIAL_POWEROFF_TARGET,
[5] = SPECIAL_REBOOT_TARGET
break;
}
- log_info("Got unhandled signal <%s>.", strsignal(sfsi.ssi_signo));
+ log_warning("Got unhandled signal <%s>.", strna(signal_to_string(sfsi.ssi_signo)));
}
}
}
UNIT_VTABLE(w->data.unit)->fd_event(w->data.unit, w->fd, ev->events, w);
break;
- case WATCH_TIMER: {
+ case WATCH_UNIT_TIMER:
+ case WATCH_JOB_TIMER: {
uint64_t v;
ssize_t k;
return k < 0 ? -errno : -EIO;
}
- UNIT_VTABLE(w->data.unit)->timer_event(w->data.unit, v, w);
+ if (w->type == WATCH_UNIT_TIMER)
+ UNIT_VTABLE(w->data.unit)->timer_event(w->data.unit, v, w);
+ else
+ job_timer_event(w->data.job, v, w);
break;
}
assert(m);
m->exit_code = MANAGER_RUNNING;
+ /* Release the path cache */
+ set_free_free(m->unit_path_cache);
+ m->unit_path_cache = NULL;
+
/* There might still be some zombies hanging around from
* before we were exec()'ed. Leat's reap them */
if ((r = manager_dispatch_sigchld(m)) < 0)
UNIT_VTABLE(u)->bus_query_pid_done(u, name, pid);
}
-int manager_open_serialization(FILE **_f) {
+int manager_open_serialization(Manager *m, FILE **_f) {
char *path;
mode_t saved_umask;
int fd;
assert(_f);
- if (asprintf(&path, "/dev/shm/systemd-%u.dump-XXXXXX", (unsigned) getpid()) < 0)
- return -ENOMEM;
+ if (m->running_as == MANAGER_SYSTEM) {
+ mkdir_p("/dev/.systemd", 0755);
+
+ if (asprintf(&path, "/dev/.systemd/dump-%lu-XXXXXX", (unsigned long) getpid()) < 0)
+ return -ENOMEM;
+ } else {
+ if (asprintf(&path, "/tmp/systemd-dump-%lu-XXXXXX", (unsigned long) getpid()) < 0)
+ return -ENOMEM;
+ }
saved_umask = umask(0077);
fd = mkostemp(path, O_RDWR|O_CLOEXEC);
log_debug("Deserializing state...");
+ m->n_deserializing ++;
+
for (;;) {
Unit *u;
char name[UNIT_NAME_MAX+2];
if (feof(f))
break;
- return -errno;
+ r = -errno;
+ goto finish;
}
char_array_0(name);
if ((r = manager_load_unit(m, strstrip(name), NULL, NULL, &u)) < 0)
- return r;
+ goto finish;
if ((r = unit_deserialize(u, f, fds)) < 0)
- return r;
+ goto finish;
}
- if (ferror(f))
- return -EIO;
+ if (ferror(f)) {
+ r = -EIO;
+ goto finish;
+ }
- return 0;
+ r = 0;
+
+finish:
+ assert(m->n_deserializing > 0);
+ m->n_deserializing --;
+
+ return r;
}
int manager_reload(Manager *m) {
assert(m);
- if ((r = manager_open_serialization(&f)) < 0)
+ if ((r = manager_open_serialization(m, &f)) < 0)
return r;
if (!(fds = fdset_new())) {
if ((q = lookup_paths_init(&m->lookup_paths, m->running_as)) < 0)
r = q;
+ m->n_deserializing ++;
+
/* First, enumerate what we can from all config files */
if ((q = manager_enumerate(m)) < 0)
r = q;
if ((q = manager_coldplug(m)) < 0)
r = q;
+ assert(m->n_deserializing > 0);
+ m->n_deserializing ++;
+
finish:
if (f)
fclose(f);
return false;
}
+void manager_reset_maintenance(Manager *m) {
+ Unit *u;
+ Iterator i;
+
+ assert(m);
+
+ HASHMAP_FOREACH(u, m->units, i)
+ unit_reset_maintenance(u);
+}
+
static const char* const manager_running_as_table[_MANAGER_RUNNING_AS_MAX] = {
[MANAGER_SYSTEM] = "system",
[MANAGER_SESSION] = "session"