X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fcore%2Fcgroup.c;h=8d7d57e7a04499569f855065847d5744487731c0;hb=58172968ad2f9a350172f062cc459d070569916d;hp=c877c3c32a5f8393a9278b6e3ba963ee7cf7a461;hpb=d2338db3145922b1dfcb3b458e5bce2568a9347a;p=elogind.git diff --git a/src/core/cgroup.c b/src/core/cgroup.c index c877c3c32..8d7d57e7a 100644 --- a/src/core/cgroup.c +++ b/src/core/cgroup.c @@ -22,17 +22,22 @@ #include #include +#include "alloc-util.h" #include "cgroup-util.h" +#include "cgroup.h" +#include "fd-util.h" +#include "fileio.h" +#include "fs-util.h" +#include "parse-util.h" #include "path-util.h" #include "process-util.h" //#include "special.h" - -#include "cgroup.h" +#include "string-table.h" +#include "string-util.h" #define CGROUP_CPU_QUOTA_PERIOD_USEC ((usec_t) 100 * USEC_PER_MSEC) -// UNNEEDED by elogind -#if 0 +#if 0 /// UNNEEDED by elogind void cgroup_context_init(CGroupContext *c) { assert(c); @@ -49,8 +54,6 @@ void cgroup_context_init(CGroupContext *c) { c->startup_blockio_weight = CGROUP_BLKIO_WEIGHT_INVALID; c->tasks_max = (uint64_t) -1; - - c->netclass_type = CGROUP_NETCLASS_TYPE_NONE; } void cgroup_context_free_device_allow(CGroupContext *c, CGroupDeviceAllow *a) { @@ -213,7 +216,7 @@ static int whitelist_device(const char *path, const char *node, const char *acc) r = cg_set_attribute("devices", path, "devices.allow", buf); if (r < 0) - log_full_errno(IN_SET(r, -ENOENT, -EROFS, -EINVAL) ? LOG_DEBUG : LOG_WARNING, r, + log_full_errno(IN_SET(r, -ENOENT, -EROFS, -EINVAL, -EACCES) ? LOG_DEBUG : LOG_WARNING, r, "Failed to set devices.allow on %s: %m", path); return r; @@ -284,7 +287,7 @@ static int whitelist_major(const char *path, const char *name, char type, const r = cg_set_attribute("devices", path, "devices.allow", buf); if (r < 0) - log_full_errno(IN_SET(r, -ENOENT, -EROFS, -EINVAL) ? LOG_DEBUG : LOG_WARNING, r, + log_full_errno(IN_SET(r, -ENOENT, -EROFS, -EINVAL, -EACCES) ? LOG_DEBUG : LOG_WARNING, r, "Failed to set devices.allow on %s: %m", path); } @@ -295,7 +298,7 @@ fail: return -errno; } -void cgroup_context_apply(CGroupContext *c, CGroupMask mask, const char *path, uint32_t netclass, ManagerState state) { +void cgroup_context_apply(CGroupContext *c, CGroupMask mask, const char *path, ManagerState state) { bool is_root; int r; @@ -324,13 +327,13 @@ void cgroup_context_apply(CGroupContext *c, CGroupMask mask, const char *path, u c->cpu_shares != CGROUP_CPU_SHARES_INVALID ? c->cpu_shares : CGROUP_CPU_SHARES_DEFAULT); r = cg_set_attribute("cpu", path, "cpu.shares", buf); if (r < 0) - log_full_errno(IN_SET(r, -ENOENT, -EROFS) ? LOG_DEBUG : LOG_WARNING, r, + log_full_errno(IN_SET(r, -ENOENT, -EROFS, -EACCES) ? LOG_DEBUG : LOG_WARNING, r, "Failed to set cpu.shares on %s: %m", path); sprintf(buf, USEC_FMT "\n", CGROUP_CPU_QUOTA_PERIOD_USEC); r = cg_set_attribute("cpu", path, "cpu.cfs_period_us", buf); if (r < 0) - log_full_errno(IN_SET(r, -ENOENT, -EROFS) ? LOG_DEBUG : LOG_WARNING, r, + log_full_errno(IN_SET(r, -ENOENT, -EROFS, -EACCES) ? LOG_DEBUG : LOG_WARNING, r, "Failed to set cpu.cfs_period_us on %s: %m", path); if (c->cpu_quota_per_sec_usec != USEC_INFINITY) { @@ -339,7 +342,7 @@ void cgroup_context_apply(CGroupContext *c, CGroupMask mask, const char *path, u } else r = cg_set_attribute("cpu", path, "cpu.cfs_quota_us", "-1"); if (r < 0) - log_full_errno(IN_SET(r, -ENOENT, -EROFS) ? LOG_DEBUG : LOG_WARNING, r, + log_full_errno(IN_SET(r, -ENOENT, -EROFS, -EACCES) ? LOG_DEBUG : LOG_WARNING, r, "Failed to set cpu.cfs_quota_us on %s: %m", path); } @@ -355,7 +358,7 @@ void cgroup_context_apply(CGroupContext *c, CGroupMask mask, const char *path, u c->blockio_weight != CGROUP_BLKIO_WEIGHT_INVALID ? c->blockio_weight : CGROUP_BLKIO_WEIGHT_DEFAULT); r = cg_set_attribute("blkio", path, "blkio.weight", buf); if (r < 0) - log_full_errno(IN_SET(r, -ENOENT, -EROFS) ? LOG_DEBUG : LOG_WARNING, r, + log_full_errno(IN_SET(r, -ENOENT, -EROFS, -EACCES) ? LOG_DEBUG : LOG_WARNING, r, "Failed to set blkio.weight on %s: %m", path); /* FIXME: no way to reset this list */ @@ -369,7 +372,7 @@ void cgroup_context_apply(CGroupContext *c, CGroupMask mask, const char *path, u sprintf(buf, "%u:%u %" PRIu64 "\n", major(dev), minor(dev), w->weight); r = cg_set_attribute("blkio", path, "blkio.weight_device", buf); if (r < 0) - log_full_errno(IN_SET(r, -ENOENT, -EROFS) ? LOG_DEBUG : LOG_WARNING, r, + log_full_errno(IN_SET(r, -ENOENT, -EROFS, -EACCES) ? LOG_DEBUG : LOG_WARNING, r, "Failed to set blkio.weight_device on %s: %m", path); } } @@ -388,7 +391,7 @@ void cgroup_context_apply(CGroupContext *c, CGroupMask mask, const char *path, u sprintf(buf, "%u:%u %" PRIu64 "\n", major(dev), minor(dev), b->bandwidth); r = cg_set_attribute("blkio", path, a, buf); if (r < 0) - log_full_errno(IN_SET(r, -ENOENT, -EROFS) ? LOG_DEBUG : LOG_WARNING, r, + log_full_errno(IN_SET(r, -ENOENT, -EROFS, -EACCES) ? LOG_DEBUG : LOG_WARNING, r, "Failed to set %s on %s: %m", a, path); } } @@ -412,7 +415,7 @@ void cgroup_context_apply(CGroupContext *c, CGroupMask mask, const char *path, u } if (r < 0) - log_full_errno(IN_SET(r, -ENOENT, -EROFS) ? LOG_DEBUG : LOG_WARNING, r, + log_full_errno(IN_SET(r, -ENOENT, -EROFS, -EACCES) ? LOG_DEBUG : LOG_WARNING, r, "Failed to set memory.limit_in_bytes/memory.max on %s: %m", path); } @@ -428,7 +431,7 @@ void cgroup_context_apply(CGroupContext *c, CGroupMask mask, const char *path, u else r = cg_set_attribute("devices", path, "devices.allow", "a"); if (r < 0) - log_full_errno(IN_SET(r, -ENOENT, -EROFS, -EINVAL) ? LOG_DEBUG : LOG_WARNING, r, + log_full_errno(IN_SET(r, -ENOENT, -EROFS, -EINVAL, -EACCES) ? LOG_DEBUG : LOG_WARNING, r, "Failed to reset devices.list on %s: %m", path); if (c->device_policy == CGROUP_CLOSED || @@ -490,20 +493,9 @@ void cgroup_context_apply(CGroupContext *c, CGroupMask mask, const char *path, u r = cg_set_attribute("pids", path, "pids.max", "max"); if (r < 0) - log_full_errno(IN_SET(r, -ENOENT, -EROFS) ? LOG_DEBUG : LOG_WARNING, r, + log_full_errno(IN_SET(r, -ENOENT, -EROFS, -EACCES) ? LOG_DEBUG : LOG_WARNING, r, "Failed to set pids.max on %s: %m", path); } - - if (mask & CGROUP_MASK_NET_CLS) { - char buf[DECIMAL_STR_MAX(uint32_t)]; - - sprintf(buf, "%" PRIu32, netclass); - - r = cg_set_attribute("net_cls", path, "net_cls.classid", buf); - if (r < 0) - log_full_errno(IN_SET(r, -ENOENT, -EROFS) ? LOG_DEBUG : LOG_WARNING, r, - "Failed to set net_cls.classid on %s: %m", path); - } } CGroupMask cgroup_context_get_mask(CGroupContext *c) { @@ -536,9 +528,6 @@ CGroupMask cgroup_context_get_mask(CGroupContext *c) { c->tasks_max != (uint64_t) -1) mask |= CGROUP_MASK_PIDS; - if (c->netclass_type != CGROUP_NETCLASS_TYPE_NONE) - mask |= CGROUP_MASK_NET_CLS; - return mask; } @@ -906,103 +895,6 @@ static bool unit_has_mask_realized(Unit *u, CGroupMask target_mask) { return u->cgroup_realized && u->cgroup_realized_mask == target_mask; } -static int unit_find_free_netclass_cgroup(Unit *u, uint32_t *ret) { - - uint32_t start, i; - Manager *m; - - assert(u); - - m = u->manager; - - i = start = m->cgroup_netclass_registry_last; - - do { - i++; - - if (!hashmap_get(m->cgroup_netclass_registry, UINT_TO_PTR(i))) { - m->cgroup_netclass_registry_last = i; - *ret = i; - return 0; - } - - if (i == UINT32_MAX) - i = CGROUP_NETCLASS_FIXED_MAX; - - } while (i != start); - - return -ENOBUFS; -} - -int unit_add_to_netclass_cgroup(Unit *u) { - - CGroupContext *cc; - Unit *first; - void *key; - int r; - - assert(u); - - cc = unit_get_cgroup_context(u); - if (!cc) - return 0; - - switch (cc->netclass_type) { - case CGROUP_NETCLASS_TYPE_NONE: - return 0; - - case CGROUP_NETCLASS_TYPE_FIXED: - u->cgroup_netclass_id = cc->netclass_id; - break; - - case CGROUP_NETCLASS_TYPE_AUTO: - /* Allocate a new ID in case it was requested and not done yet */ - if (u->cgroup_netclass_id == 0) { - r = unit_find_free_netclass_cgroup(u, &u->cgroup_netclass_id); - if (r < 0) - return r; - - log_debug("Dynamically assigned netclass cgroup id %" PRIu32 " to %s", u->cgroup_netclass_id, u->id); - } - - break; - } - - r = hashmap_ensure_allocated(&u->manager->cgroup_netclass_registry, &trivial_hash_ops); - if (r < 0) - return r; - - key = UINT32_TO_PTR(u->cgroup_netclass_id); - first = hashmap_get(u->manager->cgroup_netclass_registry, key); - - if (first) { - LIST_PREPEND(cgroup_netclass, first, u); - return hashmap_replace(u->manager->cgroup_netclass_registry, key, u); - } - - return hashmap_put(u->manager->cgroup_netclass_registry, key, u); -} - -int unit_remove_from_netclass_cgroup(Unit *u) { - - Unit *head; - void *key; - - assert(u); - - key = UINT32_TO_PTR(u->cgroup_netclass_id); - - LIST_FIND_HEAD(cgroup_netclass, u, head); - LIST_REMOVE(cgroup_netclass, head, u); - - if (head) - return hashmap_replace(u->manager->cgroup_netclass_registry, key, head); - - hashmap_remove(u->manager->cgroup_netclass_registry, key); - - return 0; -} - /* Check if necessary controllers and attributes for a unit are in place. * * If so, do nothing. @@ -1038,7 +930,7 @@ static int unit_realize_cgroup_now(Unit *u, ManagerState state) { return r; /* Finally, apply the necessary attributes. */ - cgroup_context_apply(unit_get_cgroup_context(u), target_mask, u->cgroup_path, u->cgroup_netclass_id, state); + cgroup_context_apply(unit_get_cgroup_context(u), target_mask, u->cgroup_path, state); return 0; } @@ -1205,7 +1097,7 @@ int unit_search_main_pid(Unit *u, pid_t *ret) { continue; /* Ignore processes that aren't our kids */ - if (get_parent_of_pid(npid, &ppid) >= 0 && ppid != mypid) + if (get_process_ppid(npid, &ppid) >= 0 && ppid != mypid) continue; if (pid != 0) @@ -1370,8 +1262,7 @@ int manager_setup_cgroup(Manager *m) { if (r < 0) return log_error_errno(r, "Cannot determine cgroup we are running in: %m"); -/// elogind does not support systemd scopes and slices -#if 0 +#if 0 /// elogind does not support systemd scopes and slices /* Chop off the init scope, if we are already located in it */ e = endswith(m->cgroup_root, "/" SPECIAL_INIT_SCOPE); @@ -1418,8 +1309,7 @@ int manager_setup_cgroup(Manager *m) { /* In the unified hierarchy we can can get * cgroup empty notifications via inotify. */ -/// elogind does not support the unified hierarchy, yet. -#if 0 +#if 0 /// elogind does not support the unified hierarchy, yet. m->cgroup_inotify_event_source = sd_event_source_unref(m->cgroup_inotify_event_source); safe_close(m->cgroup_inotify_fd); @@ -1457,8 +1347,7 @@ int manager_setup_cgroup(Manager *m) { log_debug("Release agent already installed."); } -/// elogind is not meant to run in systemd init scope -#if 0 +#if 0 /// elogind is not meant to run in systemd init scope /* 4. Make sure we are in the special "init.scope" unit in the root slice. */ scope_path = strjoina(m->cgroup_root, "/" SPECIAL_INIT_SCOPE); r = cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER, scope_path, 0); @@ -1516,8 +1405,7 @@ void manager_shutdown_cgroup(Manager *m, bool delete) { if (delete && m->cgroup_root) (void) cg_trim(SYSTEMD_CGROUP_CONTROLLER, m->cgroup_root, false); -/// elogind does not support the unified hierarchy, yet. -#if 0 +#if 0 /// elogind does not support the unified hierarchy, yet. m->cgroup_inotify_wd_unit = hashmap_free(m->cgroup_inotify_wd_unit); m->cgroup_inotify_event_source = sd_event_source_unref(m->cgroup_inotify_event_source); @@ -1529,8 +1417,7 @@ void manager_shutdown_cgroup(Manager *m, bool delete) { m->cgroup_root = mfree(m->cgroup_root); } -/// UNNEEDED by elogind -#if 0 +#if 0 /// UNNEEDED by elogind Unit* manager_get_unit_by_cgroup(Manager *m, const char *cgroup) { char *p; Unit *u;