[PATCH 0/4] samples/cgroup: fix issues in the event listener samples
From: Song Hu
Date: Mon Nov 02 2026 - 07:35:03 EST
The two event-listener samples in samples/cgroup mishandle the
lifetime of the monitored file and poll() errors in several ways.
Patch 1 fixes the poll error guard in memcg_event_listener, which
tests "errno != EAGAIN", an error poll() never reports, so an
interrupted poll (EINTR) from a signal exits the listener instead of
resuming.
Patch 2 makes memcg_event_listener exit cleanly when the monitored
cgroup is removed. Since eea5d2bb34ba ("kernfs: Send IN_DELETE_SELF
and IN_IGNORED") kernfs delivers both events on rmdir; the sample
warns and keeps polling, so a supervisor still counts it as alive
while the monitored cgroup is gone. Verified against a real
container lifecycle: the unpatched listener stays alive across
docker rm -f, the patched one exits.
Patch 3 arms the inotify watch before reading the counter baseline in
memcg_event_listener, closing the window in which events were lost.
Patch 4 stops cgroup_event_listener from letting dirname(3) modify
argv[1] in place, which truncated the path reported in every event
line and made the two-listener setup taught by
Documentation/admin-guide/cgroup-v1/memory.rst produce
indistinguishable output.
Patches 1 and 2 were verified against a real container lifecycle
(docker rm -f) in a QEMU/KVM virtual machine; patch 3 runs on every
listener start; patch 4 is compile-tested only.
Song Hu (4):
samples/cgroup: fix poll error handling in memcg_event_listener
samples/cgroup: exit cleanly when the monitored memcg is removed
samples/cgroup: arm the inotify watch before reading the baseline
samples/cgroup: don't let dirname() clobber the monitored path
samples/cgroup/cgroup_event_listener.c | 18 +++++++---
samples/cgroup/memcg_event_listener.c | 50 +++++++++++++++++++-------
2 files changed, 51 insertions(+), 17 deletions(-)
--
2.53.0