diff --git a/src/core/cgroup.c b/src/core/cgroup.c index 01d72a3908..8d27d26505 100644 --- a/src/core/cgroup.c +++ b/src/core/cgroup.c @@ -1595,10 +1595,13 @@ int unit_watch_cgroup(Unit *u) { assert(u); + /* Watches the "cgroups.events" attribute of this unit's cgroup for "empty" events, but only if + * cgroupv2 is available. */ + if (!u->cgroup_path) return 0; - if (u->cgroup_inotify_wd >= 0) + if (u->cgroup_control_inotify_wd >= 0) return 0; /* Only applies to the unified hierarchy */ @@ -1608,11 +1611,11 @@ int unit_watch_cgroup(Unit *u) { if (r == 0) return 0; - /* Don't watch the root slice, it's pointless. */ + /* No point in watch the top-level slice, it's never going to run empty. */ if (unit_has_name(u, SPECIAL_ROOT_SLICE)) return 0; - r = hashmap_ensure_allocated(&u->manager->cgroup_inotify_wd_unit, &trivial_hash_ops); + r = hashmap_ensure_allocated(&u->manager->cgroup_control_inotify_wd_unit, &trivial_hash_ops); if (r < 0) return log_oom(); @@ -1620,20 +1623,19 @@ int unit_watch_cgroup(Unit *u) { if (r < 0) return log_oom(); - u->cgroup_inotify_wd = inotify_add_watch(u->manager->cgroup_inotify_fd, events, IN_MODIFY); - if (u->cgroup_inotify_wd < 0) { + u->cgroup_control_inotify_wd = inotify_add_watch(u->manager->cgroup_inotify_fd, events, IN_MODIFY); + if (u->cgroup_control_inotify_wd < 0) { - if (errno == ENOENT) /* If the directory is already - * gone we don't need to track - * it, so this is not an error */ + if (errno == ENOENT) /* If the directory is already gone we don't need to track it, so this + * is not an error */ return 0; - return log_unit_error_errno(u, errno, "Failed to add inotify watch descriptor for control group %s: %m", u->cgroup_path); + return log_unit_error_errno(u, errno, "Failed to add control inotify watch descriptor for control group %s: %m", u->cgroup_path); } - r = hashmap_put(u->manager->cgroup_inotify_wd_unit, INT_TO_PTR(u->cgroup_inotify_wd), u); + r = hashmap_put(u->manager->cgroup_control_inotify_wd_unit, INT_TO_PTR(u->cgroup_control_inotify_wd), u); if (r < 0) - return log_unit_error_errno(u, r, "Failed to add inotify watch descriptor to hash map: %m"); + return log_unit_error_errno(u, r, "Failed to add control inotify watch descriptor to hash map: %m"); return 0; } @@ -2223,12 +2225,12 @@ void unit_release_cgroup(Unit *u) { u->cgroup_path = mfree(u->cgroup_path); } - if (u->cgroup_inotify_wd >= 0) { - if (inotify_rm_watch(u->manager->cgroup_inotify_fd, u->cgroup_inotify_wd) < 0) - log_unit_debug_errno(u, errno, "Failed to remove cgroup inotify watch %i for %s, ignoring: %m", u->cgroup_inotify_wd, u->id); + if (u->cgroup_control_inotify_wd >= 0) { + if (inotify_rm_watch(u->manager->cgroup_inotify_fd, u->cgroup_control_inotify_wd) < 0) + log_unit_debug_errno(u, errno, "Failed to remove cgroup control inotify watch %i for %s, ignoring: %m", u->cgroup_control_inotify_wd, u->id); - (void) hashmap_remove(u->manager->cgroup_inotify_wd_unit, INT_TO_PTR(u->cgroup_inotify_wd)); - u->cgroup_inotify_wd = -1; + (void) hashmap_remove(u->manager->cgroup_control_inotify_wd_unit, INT_TO_PTR(u->cgroup_control_inotify_wd)); + u->cgroup_control_inotify_wd = -1; } } @@ -2508,7 +2510,7 @@ static int on_cgroup_inotify_event(sd_event_source *s, int fd, uint32_t revents, /* The watch was just removed */ continue; - u = hashmap_get(m->cgroup_inotify_wd_unit, INT_TO_PTR(e->wd)); + u = hashmap_get(m->cgroup_control_inotify_wd_unit, INT_TO_PTR(e->wd)); if (!u) /* Not that inotify might deliver * events for a watch even after it * was removed, because it was queued @@ -2706,7 +2708,7 @@ void manager_shutdown_cgroup(Manager *m, bool delete) { m->cgroup_empty_event_source = sd_event_source_unref(m->cgroup_empty_event_source); - m->cgroup_inotify_wd_unit = hashmap_free(m->cgroup_inotify_wd_unit); + m->cgroup_control_inotify_wd_unit = hashmap_free(m->cgroup_control_inotify_wd_unit); m->cgroup_inotify_event_source = sd_event_source_unref(m->cgroup_inotify_event_source); m->cgroup_inotify_fd = safe_close(m->cgroup_inotify_fd); diff --git a/src/core/manager.h b/src/core/manager.h index cdd4882a6a..ea442596ae 100644 --- a/src/core/manager.h +++ b/src/core/manager.h @@ -268,7 +268,7 @@ struct Manager { /* Notifications from cgroups, when the unified hierarchy is used is done via inotify. */ int cgroup_inotify_fd; sd_event_source *cgroup_inotify_event_source; - Hashmap *cgroup_inotify_wd_unit; + Hashmap *cgroup_control_inotify_wd_unit; /* A defer event for handling cgroup empty events and processing them after SIGCHLD in all cases. */ sd_event_source *cgroup_empty_event_source; diff --git a/src/core/unit.c b/src/core/unit.c index 2cde494a7e..c8586d16cf 100644 --- a/src/core/unit.c +++ b/src/core/unit.c @@ -91,7 +91,7 @@ Unit *unit_new(Manager *m, size_t size) { u->unit_file_state = _UNIT_FILE_STATE_INVALID; u->unit_file_preset = -1; u->on_failure_job_mode = JOB_REPLACE; - u->cgroup_inotify_wd = -1; + u->cgroup_control_inotify_wd = -1; u->job_timeout = USEC_INFINITY; u->job_running_timeout = USEC_INFINITY; u->ref_uid = UID_INVALID; diff --git a/src/core/unit.h b/src/core/unit.h index 28878b5f4f..dc2278c32d 100644 --- a/src/core/unit.h +++ b/src/core/unit.h @@ -252,7 +252,7 @@ typedef struct Unit { CGroupMask cgroup_enabled_mask; /* Which controllers are enabled (or more correctly: enabled for the children) for this unit's cgroup? (only relevant on cgroup v2) */ CGroupMask cgroup_invalidated_mask; /* A mask specifiying controllers which shall be considered invalidated, and require re-realization */ CGroupMask cgroup_members_mask; /* A cache for the controllers required by all children of this cgroup (only relevant for slice units) */ - int cgroup_inotify_wd; + int cgroup_control_inotify_wd; /* Device Controller BPF program */ BPFProgram *bpf_device_control_installed;