Re: [PATCH RFC] kobject_uevent: notify uevent sysfs file on changes

From: Greg Kroah-Hartman
Date: Thu May 16 2024 - 14:31:56 EST


On Thu, May 16, 2024 at 07:41:07PM +0200, Thomas Weißschuh wrote:
> On 2024-05-16 13:17:34+0000, Greg Kroah-Hartman wrote:
> > On Thu, May 16, 2024 at 12:27:58PM +0200, Thomas Weißschuh wrote:
> > > The sysfs file "uevent" that exists for each device
> > > contains the same information that is attached to uevents emitted via
> > > netlink (or the usermode helper).
> > > This is useful for userspace which interacts with sysfs directly,
> > > without using (lib)udev.
> > >
> > > However it is not possible to actually get notified when the data in
> > > the "uevent" file changes.
> >
> > What is wrong with listening to the uevent that is happening when the
> > file changes?
>
> It requires netlink or libudev which is not available or ergonimic for
> all userspace programs.

But that's the api involved for this thing.

> Shellscripts, simple C applications, programing languages without
> access to netlink/libudev.

You can have a shellscript run for every uevent if that's all you have,
why not just do that?

> I noticed this when using the "Waybar" application[0] and missing power
> supply event updates. Both me and the authors of Waybar expected sysfs
> notifications to work for uevent files.
>
> Using sysfs notifications looks like an easy quality-of-life
> improvement.
>
> > > Enable these notifications, so that the "uevent" file can be used
> > > together with inotify and friends.
> >
> > uevent files are meant to be listened to by the uevent itself, why not do
> > that?
>
> I can't parse this sentence, sorry. Could you elaborate?

The uevent file just mirrors what was sent in the uevent. It isn't
there to be polled, it's just there to be read if you want the
information later on for some reason (i.e. coldplugging).

I recommend just using libudev to register for the events, don't try to
parse the files yourself, that way is madness :)

Or register for the netlink events you care about, you can filter very
easily that way to only get the ones you want, that's why the netlink
interface is used.

thanks,

greg k-h