X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fmanager.c;h=58848356ea9aec577068650514cfe5b4df30b766;hp=a643263213a2988c563a03f5f6d7b7f9d76075d5;hb=ad780f1991d81c8013cc345488b9a035bf30aae7;hpb=1e001f52d20a4685c9e8cf3cfa690021ca05c9e7 diff --git a/src/manager.c b/src/manager.c index a64326321..58848356e 100644 --- a/src/manager.c +++ b/src/manager.c @@ -36,6 +36,7 @@ #include #include #include +#include #include "manager.h" #include "hashmap.h" @@ -88,7 +89,7 @@ static int manager_setup_notify(Manager *m) { 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; } @@ -186,7 +187,6 @@ static int manager_setup_signals(Manager *m) { int manager_new(ManagerRunningAs running_as, Manager **_m) { Manager *m; int r = -ENOMEM; - char *p; assert(_m); assert(running_as >= 0); @@ -245,14 +245,6 @@ int manager_new(ManagerRunningAs running_as, Manager **_m) { 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; @@ -420,8 +412,7 @@ void manager_free(Manager *m) { /* 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); @@ -443,14 +434,8 @@ void manager_free(Manager *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); } @@ -494,11 +479,77 @@ int manager_coldplug(Manager *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); @@ -511,6 +562,11 @@ int manager_startup(Manager *m, FILE *serialization, FDSet *fds) { 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); @@ -1272,7 +1328,8 @@ static int transaction_add_job_and_dependencies( assert(type < _JOB_TYPE_MAX); assert(unit); - if (unit->meta.load_state != UNIT_LOADED) { + 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; } @@ -1298,20 +1355,26 @@ static int transaction_add_job_and_dependencies( 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) @@ -1665,7 +1728,7 @@ static int manager_process_notify_fd(Manager *m) { 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; @@ -1730,7 +1793,7 @@ static int manager_dispatch_sigchld(Manager *m) { 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. */ @@ -1748,14 +1811,14 @@ static int manager_dispatch_sigchld(Manager *m) { (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); } @@ -1896,7 +1959,7 @@ static int manager_process_signal_fd(Manager *m) { 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 @@ -2009,6 +2072,10 @@ int manager_loop(Manager *m) { 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) @@ -2279,6 +2346,8 @@ int manager_deserialize(Manager *m, FILE *f, FDSet *fds) { log_debug("Deserializing state..."); + m->n_deserializing ++; + for (;;) { Unit *u; char name[UNIT_NAME_MAX+2]; @@ -2288,22 +2357,31 @@ int manager_deserialize(Manager *m, FILE *f, FDSet *fds) { 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) { @@ -2337,6 +2415,8 @@ int manager_reload(Manager *m) { 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; @@ -2352,6 +2432,9 @@ int manager_reload(Manager *m) { if ((q = manager_coldplug(m)) < 0) r = q; + assert(m->n_deserializing > 0); + m->n_deserializing ++; + finish: if (f) fclose(f);