KernFS: Missing IN_DELETE_SELF or IN_IGNORED inotify events

From: Josef Schönberger
Date: Wed May 24 2023 - 16:00:15 EST


Hi everybody,

I'm currently working on event listeners for cgroups for a long running
daemon. I found that the removal of cgroups that I'm currently listening
for produces neither an IN_DELETE_SELF nor an IN_IGNORED event; which is
unfortunate, since I need to maintain a map from watch descriptors to
paths, from which I would like to remove all obsolete entries.

Since this is not the documented behaviour and since I could not find
any material that would specify an intention behind this, I'm assuming
that this is a bug?

I've found that the same applies to the sysfs. Taking a look at the
kernel code, I'd guess that the issue might be in KernFS.

I've appended a minimal reproducible example.

Thanks
Josef



#include <stdlib.h>
#include <err.h>
#include <sys/inotify.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <signal.h>

void cleanup(int signo) {
(void) signo;
printf("Removing /sys/fs/cgroup/demo again...\n");
if (rmdir("/sys/fs/cgroup/demo"))
err(EXIT_FAILURE, "Could not remove /sys/fs/cgroup/demo");
exit(0);
}

int main(void) {
if (geteuid() != 0)
fprintf(stderr, "Need root privileges.\n"
"Will probably not be able to cgroup without.\n"
"Giving it a shot regardless...\n");

int fd = inotify_init();
if (fd < 0)
err(EXIT_FAILURE, "Could not create inotify fd");

if (mkdir("/sys/fs/cgroup/demo", 755))
err(EXIT_FAILURE, "Could not create cgroup @ /sys/fs/cgroup/demo");

signal(SIGINT, cleanup);

int wd = inotify_add_watch(fd, "/sys/fs/cgroup/demo", IN_DELETE_SELF);
if (wd < 0)
err(EXIT_FAILURE, "Could not add /sys/fs/cgroup/demo to inotify
fd");

printf("Expecting to read an IN_DELETE_SELF or IN_IGNORED event...\n");
char buf[512];
ssize_t nbytes = read(fd, buf, sizeof(buf));
if (nbytes < 0)
err(EXIT_FAILURE, "Could not read from inotify fd");

printf("Read %zd bytes\n", nbytes);
cleanup(0);
}