All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kay Sievers <kay.sievers@vrfy.org>
To: Greg KH <greg@kroah.com>
Cc: Robert Love <rml@ximian.com>,
	akpm@osdl.org, linux-kernel@vger.kernel.org
Subject: Re: [patch] kernel sysfs events layer
Date: Sun, 5 Sep 2004 04:18:30 +0200	[thread overview]
Message-ID: <20040905021830.GA534@vrfy.org> (raw)
In-Reply-To: <20040904005433.GA18229@kroah.com>

[-- Attachment #1: Type: text/plain, Size: 2803 bytes --]

On Sat, Sep 04, 2004 at 02:54:33AM +0200, Greg KH wrote:
> 
> How about you just add the ability to send hotplug calls across netlink?
> Make it so the kobject_hotplug() function does both the exec() call, and
> a netlink call (based on a config option for those people who like to
> configure such stuff.)
> 
> That way, programs who want to listen to netlink messages to get hotplug
> events do so, and so does programs who want to do the /etc/hotplug.d/
> type of notification?
> 
> Oh, and attributes.  How about we just change kobject_hotplug() to be:
> 	int kobject_hotplug(const char *action, struct kobject *kobj, struct attribute *attr);
> and if we set attr to be a valid pointer, we make the DEVPATH paramater
> contain the attribute name at the end of it.

If we add this to the kobject_hotplug function, how do we prevent the
execution of /sbin/hotplug for ksets that have positive hotplug filters?
So I've created a new function for now:
  int kobj_notify(const char *signal, struct kobject *kobj, struct attribute *attr)

which can be used from the subsystems. This function is also called for
the normal /sbin/hotplug event. (The subsystems may provide a additional
environment for the /sbin/hotplug events, this is ignored by now.)

Here is the debug output of a simple listener, while inserting a
USB-memory-stick, mounting, unmounting and removing it:

  [root@pim kdbusd]# ./kdbusd
  main: [1094349091] 'add' from '/org/kernel/devices/pci0000:00/0000:00:1d.1/usb3/3-1'
  main: [1094349091] 'add' from '/org/kernel/devices/pci0000:00/0000:00:1d.1/usb3/3-1/3-1:1.0'
  main: [1094349091] 'add' from '/org/kernel/class/scsi_host/host2'
  main: [1094349091] 'add' from '/org/kernel/devices/pci0000:00/0000:00:1d.1/usb3/3-1/3-1:1.0/host2/2:0:0:0'
  main: [1094349091] 'add' from '/org/kernel/block/sda'
  main: [1094349091] 'add' from '/org/kernel/class/scsi_device/2:0:0:0'
  main: [1094349091] 'add' from '/org/kernel/class/scsi_generic/sg0'
  main: [1094349092] 'add' from '/org/kernel/block/sda/sda1'

  main: [1094349094] 'claim' from '/org/kernel/block/sda/sda1'
  main: [1094349101] 'release' from '/org/kernel/block/sda/sda1'

  main: [1094349106] 'remove' from '/org/kernel/class/scsi_generic/sg0'
  main: [1094349106] 'remove' from '/org/kernel/class/scsi_device/2:0:0:0'
  main: [1094349106] 'remove' from '/org/kernel/block/sda/sda1'
  main: [1094349106] 'remove' from '/org/kernel/block/sda'
  main: [1094349106] 'remove' from '/org/kernel/devices/pci0000:00/0000:00:1d.1/usb3/3-1/3-1:1.0/host2/2:0:0:0'
  main: [1094349106] 'remove' from '/org/kernel/class/scsi_host/host2'
  main: [1094349106] 'remove' from '/org/kernel/devices/pci0000:00/0000:00:1d.1/usb3/3-1/3-1:1.0'
  main: [1094349106] 'remove' from '/org/kernel/devices/pci0000:00/0000:00:1d.1/usb3/3-1'

Thanks,
Kay

[-- Attachment #2: knotify-07.patch --]
[-- Type: text/plain, Size: 6743 bytes --]

diff -Nru a/fs/super.c b/fs/super.c
--- a/fs/super.c	2004-09-05 03:54:23 +02:00
+++ b/fs/super.c	2004-09-05 03:54:23 +02:00
@@ -35,6 +35,7 @@
 #include <linux/vfs.h>
 #include <linux/writeback.h>		/* for the emergency remount stuff */
 #include <linux/idr.h>
+#include <linux/kobj_notify.h>
 #include <asm/uaccess.h>
 
 
@@ -633,6 +634,16 @@
 	return (void *)s->s_bdev == data;
 }
 
+static void notify_device_claim(const char *action, struct block_device *bdev)
+{
+	if (bdev->bd_disk) {
+		if (bdev->bd_part)
+			kobj_notify(action, &bdev->bd_part->kobj, NULL);
+		else
+			kobj_notify(action, &bdev->bd_disk->kobj, NULL);
+	}
+}
+
 struct super_block *get_sb_bdev(struct file_system_type *fs_type,
 	int flags, const char *dev_name, void *data,
 	int (*fill_super)(struct super_block *, void *, int))
@@ -675,8 +686,10 @@
 			up_write(&s->s_umount);
 			deactivate_super(s);
 			s = ERR_PTR(error);
-		} else
+		} else {
 			s->s_flags |= MS_ACTIVE;
+			notify_device_claim("claim", bdev);
+		}
 	}
 
 	return s;
@@ -691,6 +704,8 @@
 void kill_block_super(struct super_block *sb)
 {
 	struct block_device *bdev = sb->s_bdev;
+
+	notify_device_claim("release", bdev);
 	generic_shutdown_super(sb);
 	set_blocksize(bdev, sb->s_old_blocksize);
 	close_bdev_excl(bdev);
@@ -717,6 +732,7 @@
 		return ERR_PTR(error);
 	}
 	s->s_flags |= MS_ACTIVE;
+
 	return s;
 }
 
diff -Nru a/include/linux/kobj_notify.h b/include/linux/kobj_notify.h
--- /dev/null	Wed Dec 31 16:00:00 196900
+++ b/include/linux/kobj_notify.h	2004-09-05 03:54:23 +02:00
@@ -0,0 +1,31 @@
+#ifndef _KOBJ_NOTIFY_H_
+#define _KOBJ_NOTIFY_H_
+
+#ifdef __KERNEL__
+#ifdef CONFIG_KOBJ_NOTIFY
+
+extern int kobj_notify(const char *signal, struct kobject *kobj,
+		       struct attribute *attr);
+
+extern int kobj_notify_atomic(const char *signal, struct kobject *kobj,
+			      struct attribute *attr);
+
+#else
+
+static inline int kobj_notify(const char *signal, struct kobject *kobj,
+		       struct attribute *attr)
+{
+	return 0;
+}
+
+static inline int kobj_notify_atomic(const char *signal, struct kobject *kobj,
+			      struct attribute *attr)
+{
+	return 0;
+}
+
+
+#endif /* CONFIG_KOBJ_NOTIFY */
+#endif /* __KERNEL__ */
+
+#endif /* _KOBJ_NOTIFY_H_ */
diff -Nru a/include/linux/netlink.h b/include/linux/netlink.h
--- a/include/linux/netlink.h	2004-09-05 03:54:23 +02:00
+++ b/include/linux/netlink.h	2004-09-05 03:54:23 +02:00
@@ -17,6 +17,7 @@
 #define NETLINK_ROUTE6		11	/* af_inet6 route comm channel */
 #define NETLINK_IP6_FW		13
 #define NETLINK_DNRTMSG		14	/* DECnet routing messages */
+#define NETLINK_KOBJ_NOTIFY	15	/* Kernel messages to userspace */
 #define NETLINK_TAPBASE		16	/* 16 to 31 are ethertap */
 
 #define MAX_LINKS 32		
diff -Nru a/init/Kconfig b/init/Kconfig
--- a/init/Kconfig	2004-09-05 03:54:23 +02:00
+++ b/init/Kconfig	2004-09-05 03:54:23 +02:00
@@ -195,6 +195,21 @@
 	  agent" (/sbin/hotplug) to load modules and set up software needed
 	  to use devices as you hotplug them.
 
+config KOBJ_NOTIFY
+	bool "Kernel Events Layer"
+	depends on NET && HOTPLUG
+	default y
+	help
+	  This option enables the kernel events layer, which is a simple
+	  mechanism for kernel-to-user communication over a netlink socket.
+	  The goal of the kernel events layer is to provide a simple and
+	  efficient events system, that notifies userspace about kobject state
+	  changes e.g. hotplug events, power state changes or block device
+	  claiming (mount/unmount).
+
+	  Say Y, unless you are building a system requiring minimal memory
+	  consumption.
+
 config IKCONFIG
 	bool "Kernel .config support"
 	---help---
diff -Nru a/kernel/Makefile b/kernel/Makefile
--- a/kernel/Makefile	2004-09-05 03:54:23 +02:00
+++ b/kernel/Makefile	2004-09-05 03:54:23 +02:00
@@ -24,6 +24,7 @@
 obj-$(CONFIG_AUDIT) += audit.o
 obj-$(CONFIG_AUDITSYSCALL) += auditsc.o
 obj-$(CONFIG_KPROBES) += kprobes.o
+obj-$(CONFIG_KOBJ_NOTIFY) += kobj_notify.o
 
 ifneq ($(CONFIG_IA64),y)
 # According to Alan Modra <alan@linuxcare.com.au>, the -fno-omit-frame-pointer is
diff -Nru a/kernel/kobj_notify.c b/kernel/kobj_notify.c
--- /dev/null	Wed Dec 31 16:00:00 196900
+++ b/kernel/kobj_notify.c	2004-09-05 03:54:23 +02:00
@@ -0,0 +1,88 @@
+/*
+ * kernel/kobj_notify.c - sysfs event delivery via netlink socket
+ *
+ * Copyright (C) 2004 Red Hat, Inc.  All rights reserved.
+ * Copyright (C) 2004 Novell, Inc.  All rights reserved.
+ *
+ * Licensed under the GNU GPL v2.
+ *
+ * Authors:
+ *	Robert Love		<rml@novell.com>
+ *	Kay Sievers		<kay.sievers@vrfy.org>
+ *	Arjan van de Ven	<arjanv@redhat.com>
+ */
+
+#include <linux/spinlock.h>
+#include <linux/socket.h>
+#include <linux/skbuff.h>
+#include <linux/netlink.h>
+#include <linux/string.h>
+#include <linux/kobject.h>
+#include <net/sock.h>
+
+static struct sock *kobj_notify_sock = NULL;
+
+static int do_kobj_notify(const char *signal, struct kobject *kobj,
+			  struct attribute *attr, int gfp_mask)
+{
+	const char *path;
+	struct sk_buff *skb;
+	char *buffer;
+	int len;
+
+	if (!kobj_notify_sock)
+		return -EIO;
+
+	path = kobject_get_path(NULL, kobj, gfp_mask);
+	if (!path)
+		return -ENOMEM;
+
+	len = strlen(signal) + 1;
+	len += strlen(path) + 1;
+	if (attr)
+		len += strlen(attr->name) + 1;
+
+	skb = alloc_skb(len, gfp_mask);
+	if (!skb)
+		return -ENOMEM;
+
+	buffer = skb_put(skb, len);
+	if (attr)
+		sprintf(buffer, "%s\n%s/%s", signal, path, attr->name);
+	else
+		sprintf(buffer, "%s\n%s", signal, path);
+	kfree(path);
+
+	return netlink_broadcast(kobj_notify_sock, skb, 0, 1, gfp_mask);
+}
+
+int kobj_notify(const char *signal, struct kobject *kobj,
+		struct attribute *attr)
+{
+	return do_kobj_notify(signal, kobj, attr, GFP_KERNEL);
+}
+
+EXPORT_SYMBOL(kobj_notify);
+
+int kobj_notify_atomic(const char *signal, struct kobject *kobj,
+		       struct attribute *attr)
+{
+	return do_kobj_notify(signal, kobj, attr, GFP_ATOMIC);
+}
+
+EXPORT_SYMBOL(kobj_notify_atomic);
+
+static int __init kobj_notify_init(void)
+{
+	kobj_notify_sock = netlink_kernel_create(NETLINK_KOBJ_NOTIFY, NULL);
+
+	if (!kobj_notify_sock) {
+		printk(KERN_ERR
+		       "kobj_notify: unable to create netlink socket!\n");
+		return -ENODEV;
+	}
+
+	return 0;
+}
+
+core_initcall(kobj_notify_init);
diff -Nru a/lib/kobject.c b/lib/kobject.c
--- a/lib/kobject.c	2004-09-05 03:54:23 +02:00
+++ b/lib/kobject.c	2004-09-05 03:54:23 +02:00
@@ -13,6 +13,7 @@
 #undef DEBUG
 
 #include <linux/kobject.h>
+#include <linux/kobj_notify.h>
 #include <linux/string.h>
 #include <linux/module.h>
 #include <linux/stat.h>
@@ -202,6 +203,8 @@
 			goto exit;
 		}
 	}
+
+	kobj_notify(action, kobj, NULL);
 
 	pr_debug ("%s: %s %s %s %s %s %s %s\n", __FUNCTION__, argv[0], argv[1],
 		  envp[0], envp[1], envp[2], envp[3], envp[4]);

  reply	other threads:[~2004-09-05  2:18 UTC|newest]

Thread overview: 105+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-08-31 21:42 [patch] kernel sysfs events layer Robert Love
2004-08-31 21:56 ` Andrew Morton
2004-08-31 21:58   ` Robert Love
     [not found]     ` <20040831150645.4aa8fd27.akpm@osdl.org>
2004-08-31 22:05       ` Robert Love
2004-09-02  8:34         ` Greg KH
2004-09-02 12:02           ` Daniel Stekloff
2004-09-02 13:26             ` Kay Sievers
2004-09-02 16:27               ` Robert Love
2004-09-02 20:29                 ` Kay Sievers
2004-09-02 12:49           ` Kay Sievers
2004-09-02 16:25           ` Robert Love
2004-09-02 18:35             ` Daniel Stekloff
2004-09-02 18:41               ` Robert Love
2004-09-04  0:54             ` Greg KH
2004-09-05  2:18               ` Kay Sievers [this message]
2004-09-05  3:01                 ` Robert Love
2004-09-05  2:58               ` Robert Love
2004-09-05  7:35                 ` Arjan van de Ven
2004-09-05 12:18                 ` Kay Sievers
2004-09-06  2:06                   ` Kay Sievers
2004-09-10 23:54                     ` Greg KH
2004-09-11  0:18                       ` Tim Hockin
2004-09-11  0:48                         ` Greg KH
2004-09-11  1:23                           ` Daniel Stekloff
2004-09-11  4:45                             ` Robert Love
2004-09-11  1:45                           ` Tim Hockin
2004-09-11 16:56                             ` Greg KH
2004-09-11 11:35                           ` Dave Jones
2004-09-11 18:15                             ` Greg KH
2004-09-11  4:09                       ` Robert Love
2004-09-11 16:53                         ` Greg KH
2004-09-13 14:45                           ` Kay Sievers
2004-09-15  0:07                             ` Greg KH
2004-09-15  1:09                               ` Kay Sievers
2004-09-15  1:11                                 ` Tim Hockin
2004-09-15  2:10                                   ` Robert Love
2004-09-15  3:17                                     ` Tim Hockin
2004-09-15  3:42                                       ` Greg KH
2004-09-15  4:48                                         ` Tim Hockin
2004-09-15  5:09                                           ` Greg KH
2004-09-15  6:21                                             ` Tim Hockin
2004-09-15  6:45                                               ` Jan Dittmer
2004-09-15  6:47                                                 ` Tim Hockin
2004-09-15  6:50                                                   ` Jan Dittmer
     [not found]                                                     ` <20040915065515.GA11587@hockin.org>
2004-09-15  7:39                                                       ` Jan Dittmer
2004-09-15  7:56                                                         ` Paul Jackson
2004-09-15  8:32                                                           ` Jan Dittmer
2004-09-15 14:24                                                             ` Paul Jackson
2004-09-15  8:19                                                 ` Karol Kozimor
2004-09-15 15:48                                                   ` Tim Hockin
2004-09-15 16:11                                                     ` Jan Dittmer
2004-09-15 13:14                                               ` Kay Sievers
2004-09-15 21:27                                               ` Greg KH
2004-09-15  9:07                                           ` Andrew Grover
2004-09-15 18:58                                             ` Robert Love
2004-09-15  3:48                                 ` Greg KH
2004-09-15  1:19                               ` Robert Love
2004-09-15  3:44                                 ` Greg KH
2004-09-15 19:40                                   ` Greg KH
2004-09-15 20:10                                     ` Robert Love
2004-09-15 20:22                                       ` Tim Hockin
2004-09-15 20:26                                         ` Robert Love
2004-09-15 20:31                                           ` Tim Hockin
2004-09-15 20:33                                             ` Robert Love
2004-09-15 20:47                                               ` Tim Hockin
2004-09-15 20:49                                                 ` Robert Love
2004-09-15 20:56                                                   ` Tim Hockin
2004-09-15 21:01                                                     ` Robert Love
2004-09-15 21:03                                                     ` Kay Sievers
2004-09-15 21:23                                                     ` Greg KH
2004-09-15 21:26                                                       ` Robert Love
2004-09-15 21:34                                                         ` Tim Hockin
2004-09-15 21:38                                                           ` Robert Love
2004-09-16  1:21                                                             ` Herbert Poetzl
2004-09-16  4:08                                                               ` Greg KH
2004-09-16 14:10                                                                 ` Herbert Poetzl
2004-09-16 15:08                                                                   ` Greg KH
2004-09-16 18:33                                                                     ` Herbert Poetzl
2004-09-15 21:35                                                         ` Greg KH
2004-09-15 21:46                                                           ` Tim Hockin
2004-09-15 21:47                                                             ` Robert Love
2004-09-15 21:38                                                         ` Kay Sievers
2004-09-15 21:39                                                           ` Robert Love
2004-09-15 21:49                                                             ` Tim Hockin
2004-09-15 21:54                                                               ` Greg KH
2004-09-15 20:34                                             ` Kay Sievers
2004-09-15 21:21                                       ` Greg KH
2004-09-15 21:26                                         ` Robert Love
2004-09-15 21:34                                           ` Kay Sievers
2004-09-15 21:35                                           ` Greg KH
2005-07-06 22:02                     ` Mike Snitzer
2005-07-06 22:18                       ` Greg KH
2004-09-05  3:59               ` Robert Love
2004-08-31 21:58 ` Chris Wedgwood
2004-08-31 22:02   ` Robert Love
2004-08-31 22:00 ` Andrew Morton
2004-08-31 22:00   ` Robert Love
2004-08-31 22:10     ` Andrew Morton
2004-08-31 22:08       ` Robert Love
2004-09-01  2:05 ` Daniel Stekloff
2004-09-01 10:07   ` Kay Sievers
2004-09-02 20:45     ` Daniel Stekloff
2004-09-02 22:15       ` Kay Sievers
2004-09-03 23:59         ` Daniel Stekloff
2004-09-04  8:14           ` Greg KH

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20040905021830.GA534@vrfy.org \
    --to=kay.sievers@vrfy.org \
    --cc=akpm@osdl.org \
    --cc=greg@kroah.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rml@ximian.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.