2023-05-24 20:34:06

by Josef Schönberger

[permalink] [raw]
Subject: KernFS: Missing IN_DELETE_SELF or IN_IGNORED inotify events

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);
}


2023-05-24 22:50:34

by Tejun Heo

[permalink] [raw]
Subject: Re: KernFS: Missing IN_DELETE_SELF or IN_IGNORED inotify events

On Wed, May 24, 2023 at 09:53:01PM +0200, Josef Sch?nberger wrote:
> 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.

Yeah, that'd be all kernfs. kernfs does have some explicit fsnotify hookups
so that kernfs users can generate custom modified events (e.g. for
cgroup.events file) but doesn't do anything else. If the generic layer
generates events, they'd go out. If not, they won't. I can't think of a
reason why hooking up the missing events would be difficult tho. I'm pretty
bandwidth constrained these days and don't know when I'll be able to get to
it but if you wanna take a stab at it, I'd be happy to review.

Thanks.

--
tejun