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

From: Thomas Weißschuh
Date: Thu May 16 2024 - 06:29:10 EST


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.

Enable these notifications, so that the "uevent" file can be used
together with inotify and friends.

Signed-off-by: Thomas Weißschuh <linux@xxxxxxxxxxxxxx>
---
My original usecase is to get updates for power supplies.
Current charge status and similar.
All this data is exposed via "uevent", and on updates an KOBJ_CHANGED
event is emitted.

To me, a general solution covering all devices looked the best.

Open questions for the RFC:
* Is this a good idea?
* How to handle other actions?
_ADD and _REMOVE should not call sysfs_notify() in any case, these
cases can be detected otherwise.
---
lib/kobject_uevent.c | 4 ++++
1 file changed, 4 insertions(+)

diff --git a/lib/kobject_uevent.c b/lib/kobject_uevent.c
index 03b427e2707e..74047a79cb13 100644
--- a/lib/kobject_uevent.c
+++ b/lib/kobject_uevent.c
@@ -24,6 +24,7 @@
#include <linux/netlink.h>
#include <linux/uidgid.h>
#include <linux/uuid.h>
+#include <linux/sysfs.h>
#include <linux/ctype.h>
#include <net/sock.h>
#include <net/netlink.h>
@@ -593,6 +594,9 @@ int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
retval = kobject_uevent_net_broadcast(kobj, env, action_string,
devpath);

+ if (action == KOBJ_CHANGE)
+ sysfs_notify(kobj, NULL, "uevent");
+
#ifdef CONFIG_UEVENT_HELPER
/* call uevent_helper, usually only enabled during early boot */
if (uevent_helper[0] && !kobj_usermode_filter(kobj)) {

---
base-commit: 3c999d1ae3c75991902a1a7dad0cb62c2a3008b4
change-id: 20240515-uevent-sysfs-notify-4eb9e95397fd

Best regards,
--
Thomas Weißschuh <linux@xxxxxxxxxxxxxx>