public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [patch] kernel events layer
@ 2004-07-23 17:41 Robert Love
  2004-07-23 18:25 ` Tim Hockin
                   ` (4 more replies)
  0 siblings, 5 replies; 56+ messages in thread
From: Robert Love @ 2004-07-23 17:41 UTC (permalink / raw)
  To: akpm, linux-kernel

Andrew, et al,

OK, Kernel Summit and my OLS talk are over, so here are the goods.

Following patch implements the kernel events layer, which is a simple
wrapper around netlink to allow asynchronous communication from the
kernel to user-space of events, errors, logging, and so on.

Current intention is to hook the kernel via this interface into D-BUS,
although the patch is intended to be agnostic to any of that and policy
free.

D-BUS can be found here:

	http://dbus.freedesktop.org/

Other user-space utilities (including code to utilize this) can be found
here:

	http://vrfy.org/projects/kdbusd/

This patch only implements a single event, processor temperature
detection.  Other useful events include md sync, filesystem mount,
driver errors, etc.  We can add those later, on a case-by-case basis.  I
would like to be more careful with adding events than we are with adding
printk's, with some aim toward a stable interface.

Usage of the new interface is simple:

	send_kmessage(group, interface, message, ...)

Credit to Arjan for the initial implementation, Kay Sievers for some
updates, and the netlink code.

	Robert Love


Kernel to user-space communication layer using netlink
X-Signed-Off-By: Robert Love <rml@ximian.com>

 arch/i386/kernel/cpu/mcheck/p4.c |    9 ++
 include/linux/kmessage.h         |   15 ++++
 include/linux/netlink.h          |    1 
 kernel/Makefile                  |    2 
 kernel/kmessage.c                |  134 +++++++++++++++++++++++++++++++++++++++
 5 files changed, 159 insertions(+), 2 deletions(-)

diff -urN linux-2.6.8-rc2/arch/i386/kernel/cpu/mcheck/p4.c linux/arch/i386/kernel/cpu/mcheck/p4.c
--- linux-2.6.8-rc2/arch/i386/kernel/cpu/mcheck/p4.c	2004-06-16 01:19:37.000000000 -0400
+++ linux/arch/i386/kernel/cpu/mcheck/p4.c	2004-07-23 11:56:43.443944768 -0400
@@ -9,6 +9,7 @@
 #include <linux/irq.h>
 #include <linux/interrupt.h>
 #include <linux/smp.h>
+#include <linux/kmessage.h>
 
 #include <asm/processor.h> 
 #include <asm/system.h>
@@ -59,9 +60,15 @@
 	if (l & 0x1) {
 		printk(KERN_EMERG "CPU%d: Temperature above threshold\n", cpu);
 		printk(KERN_EMERG "CPU%d: Running in modulated clock mode\n",
-				cpu);
+			cpu);
+		send_kmessage(KMSG_POWER,
+			"/org/kernel/devices/system/cpu/temperature", "high",
+			"Cpu: %d\n", cpu);
 	} else {
 		printk(KERN_INFO "CPU%d: Temperature/speed normal\n", cpu);
+		send_kmessage(KMSG_POWER,
+			"/org/kernel/devices/system/cpu/temperature", "normal",
+			"Cpu: %d\n", cpu);
 	}
 }
 
diff -urN linux-2.6.8-rc2/include/linux/kmessage.h linux/include/linux/kmessage.h
--- linux-2.6.8-rc2/include/linux/kmessage.h	1969-12-31 19:00:00.000000000 -0500
+++ linux/include/linux/kmessage.h	2004-07-23 11:56:43.443944768 -0400
@@ -0,0 +1,15 @@
+#ifndef _LINUX_KMESSAGE_H
+#define _LINUX_KMESSAGE_H
+
+void send_kmessage(int type, const char *object, const char *signal,
+		   const char *fmt, ...);
+
+/* kmessage types */
+
+#define KMSG_GENERAL	0
+#define KMSG_STORAGE	1
+#define KMSG_POWER	2
+#define KMSG_FS		3
+#define KMSG_HOTPLUG	4
+
+#endif	/* _LINUX_KMESSAGE_H */
diff -urN linux-2.6.8-rc2/include/linux/netlink.h linux/include/linux/netlink.h
--- linux-2.6.8-rc2/include/linux/netlink.h	2004-07-20 15:40:14.000000000 -0400
+++ linux/include/linux/netlink.h	2004-07-23 11:56:43.490937624 -0400
@@ -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_KMESSAGE	15	/* Kernel messages to userspace */
 #define NETLINK_TAPBASE		16	/* 16 to 31 are ethertap */
 
 #define MAX_LINKS 32		
diff -urN linux-2.6.8-rc2/kernel/kmessage.c linux/kernel/kmessage.c
--- linux-2.6.8-rc2/kernel/kmessage.c	1969-12-31 19:00:00.000000000 -0500
+++ linux/kernel/kmessage.c	2004-07-23 11:58:25.704398816 -0400
@@ -0,0 +1,134 @@
+/*
+ * Kernel event delivery over a netlink socket
+ * 
+ * Copyright (C) 2004 Red Hat, Inc. All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2 as
+ *  published by the Free Software Foundation.
+ *  
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ *  Authors:
+ *	Arjan van de Ven	<arjanv@redhat.com>
+ *	Kay Sievers		<kay.sievers@vrfy.org>
+ *	Robert Love		<rml@novell.com>
+ *
+ */
+
+#include <linux/module.h>
+#include <linux/spinlock.h>
+#include <linux/socket.h>
+#include <linux/skbuff.h>
+#include <linux/netlink.h>
+#include <linux/string.h>
+#include <linux/kmessage.h>
+#include <net/sock.h>
+
+/* There is one global netlink socket */
+static struct sock *kmessage_sock = NULL;
+
+static void netlink_receive(struct sock *sk, int len)
+{
+	struct sk_buff *skb;
+
+	 /* just drop them all */
+	while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL)
+		kfree_skb(skb);
+}
+
+static int netlink_send(__u32 groups, const char *buffer, int len)
+{
+	struct sk_buff *skb;
+	char *data_start;
+
+	if (!kmessage_sock)
+		return -EIO;
+
+	if (!buffer)
+		return -EINVAL;
+
+	if (len > PAGE_SIZE)
+		return -EINVAL;
+
+	skb = alloc_skb(len, GFP_ATOMIC);
+	if (!skb)
+		return -ENOMEM;
+	data_start = skb_put(skb, len);
+	memcpy(data_start, buffer, len);
+
+	return netlink_broadcast(kmessage_sock, skb, 0, groups, GFP_ATOMIC);
+}
+
+void send_kmessage(int type, const char *object, const char *signal,
+		   const char *fmt, ...)
+{
+	char *buffer;
+	int len;
+	int ret;
+
+	if (!object)
+		return;
+
+	if (!signal)
+		return;
+
+	if (strlen(object) > PAGE_SIZE)
+		return;
+
+	buffer = (char *) get_zeroed_page(GFP_ATOMIC);
+	if (!buffer)
+		return;
+
+	len = sprintf(buffer, "From: %s\n", object);
+	len += sprintf(&buffer[len], "Signal: %s\n", signal);
+
+	/* possible anxiliary data */
+	if (fmt) {
+		va_list args;
+
+		va_start(args, fmt);
+		len += vscnprintf(&buffer[len], PAGE_SIZE-len-1, fmt, args);
+		va_end(args);
+	}
+	buffer[len++] = '\0';
+
+	ret = netlink_send((1 << type), buffer, len);
+	free_page((unsigned long) buffer);
+}
+
+EXPORT_SYMBOL_GPL(send_kmessage);
+
+static int kmessage_init(void)
+{
+	kmessage_sock = netlink_kernel_create(NETLINK_KMESSAGE,
+					      netlink_receive);
+
+	if (!kmessage_sock) {
+		printk(KERN_ERR "kmessage: "
+		       "unable to create netlink socket; aborting\n");
+		return -ENODEV;
+	}
+
+	return 0;
+}
+
+static void kmessage_exit(void)
+{
+	if (kmessage_sock)
+		sock_release(kmessage_sock->sk_socket);
+}
+
+MODULE_DESCRIPTION("Kernel message delivery to userspace");
+MODULE_AUTHOR("Arjan van de Ven <arjanv@redhat.com>");
+MODULE_LICENSE("GPL");
+
+module_init(kmessage_init);
+module_exit(kmessage_exit);
diff -urN linux-2.6.8-rc2/kernel/Makefile linux/kernel/Makefile
--- linux-2.6.8-rc2/kernel/Makefile	2004-07-20 15:40:14.000000000 -0400
+++ linux/kernel/Makefile	2004-07-23 11:56:43.490937624 -0400
@@ -3,7 +3,7 @@
 #
 
 obj-y     = sched.o fork.o exec_domain.o panic.o printk.o profile.o \
-	    exit.o itimer.o time.o softirq.o resource.o \
+	    exit.o itimer.o time.o softirq.o resource.o kmessage.o\
 	    sysctl.o capability.o ptrace.o timer.o user.o \
 	    signal.o sys.o kmod.o workqueue.o pid.o \
 	    rcupdate.o intermodule.o extable.o params.o posix-timers.o \



^ permalink raw reply	[flat|nested] 56+ messages in thread
* RE: [patch] kernel events layer
@ 2004-07-26  6:04 Perez-Gonzalez, Inaky
  2004-07-26  6:09 ` Andrew Morton
  0 siblings, 1 reply; 56+ messages in thread
From: Perez-Gonzalez, Inaky @ 2004-07-26  6:04 UTC (permalink / raw)
  To: Chris Wedgwood, Robert Love; +Cc: Andrew Morton, linux-kernel

> From: Chris Wedgwood
>
> This part worries me a lot.  I would alsmost rather all possible
> messages get stuck somewhere common so driver writes can't add these
> ad-hoc and we can avoid a proliferation of either similar or pointless
> messages.
> 
> Forcing these into a common place lets people eyeball if a new
> messages really is necessary --- and it makes writing applications to
> deal with these things easier (since you don't have to scan the entire
> kernel tree).

That sounds to me like the perfect job for grep. In fact, it is _very_
similar to the job the GNU guys did on the early days of i18n. They
had the tool that extracted all the strings in _("xlate me") 
[#define _(a) a ] and built a catalog.

If you guys are up to it, I volunteer to write/port such a tool to scan 
out the send_kevent{_atomic,}()s and make a catalog out of it.

Iñaky Pérez-González -- Not speaking for Intel -- all opinions are my own (and my fault)

^ permalink raw reply	[flat|nested] 56+ messages in thread
* RE: [patch] kernel events layer
@ 2004-07-26  7:31 Perez-Gonzalez, Inaky
  2004-07-26 14:50 ` Robert Love
  0 siblings, 1 reply; 56+ messages in thread
From: Perez-Gonzalez, Inaky @ 2004-07-26  7:31 UTC (permalink / raw)
  To: Andrew Morton; +Cc: cw, rml, linux-kernel

> From: Andrew Morton [mailto:akpm@osdl.org]
>
> I hope we'll hear more from Greg on this next week - see if we can come up
> with some way to use the kobject/sysfs namespace for this.
> 
> Although heaven knows how "tmpfs just ran out of space" would map onto
> kobject/sysfs.

methinks: if the message is related to some object that has a kobject
representation, use it. If not, come up with one on a case by case
basis [now this is the difficult one--is where it'd be difficult to
keep things on leash].

Iñaky Pérez-González -- Not speaking for Intel -- all opinions are my own (and my fault)


^ permalink raw reply	[flat|nested] 56+ messages in thread
* RE: [patch] kernel events layer
@ 2004-07-26 22:58 Perez-Gonzalez, Inaky
  2004-07-27  7:08 ` Deepak Saxena
  0 siblings, 1 reply; 56+ messages in thread
From: Perez-Gonzalez, Inaky @ 2004-07-26 22:58 UTC (permalink / raw)
  To: Robert Love; +Cc: Andrew Morton, cw, linux-kernel

> From: Robert Love [mailto:rml@ximian.com]
>
> > methinks: if the message is related to some object that has a kobject
> > representation, use it. If not, come up with one on a case by case
> > basis [now this is the difficult one--is where it'd be difficult to
> > keep things on leash].
> 
> That introduces two orthogonal name spaces, and that really doesn't cut
> it.
> 
> If Greg can come up with a solution for using kobjects, I am all for
> that - that would be great - but I really do not see kobject paths
> working out.  I think the best we have is the file path in the tree.

Agreed -- I guess what I am looking for is a regular way to link the 
instance of the object (if any) that caused the message, so it is easier 
to take action.

For a silly example, IDE, I want to know which hard drive had a read 
error; knowing that it came from drivers/ide/ide-disk.c is useful, but
quite limited; it doesn't tell me which drive I need to babysit and 
maybe swap. Certainly the message can print that information as part of
the text, but chances up we'll end up with something like printk again
if following that path.

Ok, I did my share of non-very-constructive criticism already--shutting up :)

Thx,

Iñaky Pérez-González -- Not speaking for Intel -- all opinions are my own (and my fault) 

^ permalink raw reply	[flat|nested] 56+ messages in thread

end of thread, other threads:[~2004-08-09 19:49 UTC | newest]

Thread overview: 56+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-07-23 17:41 [patch] kernel events layer Robert Love
2004-07-23 18:25 ` Tim Hockin
2004-07-23 18:31 ` Muli Ben-Yehuda
2004-07-23 18:35   ` Robert Love
2004-07-23 21:32 ` Dan Aloni
2004-07-24  2:47   ` Robert Love
2004-07-24  4:42     ` Keith Owens
2004-07-24  5:00       ` Robert Love
2004-07-24  8:11         ` Andrew Morton
2004-07-24  5:37           ` Robert Love
2004-07-24  6:02             ` Robert Love
2004-07-24  9:43               ` Wichert Akkerman
2004-07-24 20:21               ` James Morris
2004-07-25  2:12                 ` Robert Love
2004-07-24  6:53       ` Paul Jackson
2004-07-24 11:37       ` Bernd Petrovitsch
2004-07-24  3:02 ` Michael Clark
2004-07-24  3:14   ` Robert Love
2004-07-24  9:15     ` Michael Clark
2004-07-24 15:08     ` Deepak Saxena
2004-07-24 15:45       ` Robert Love
2004-07-24 17:33         ` Ryan Anderson
2004-07-24 17:46         ` Tim Hockin
2004-07-24 18:19           ` Robert Love
2004-07-25 18:11             ` Tim Hockin
2004-07-25 19:08               ` Robert Love
2004-07-27  5:09                 ` Daniel Stekloff
2004-07-24 17:54         ` Deepak Saxena
2004-07-24 18:13           ` Robert Love
2004-07-26 20:08             ` Rutger Nijlunsing
2004-07-26 20:10               ` Robert Love
2004-08-09 13:29     ` Pavel Machek
2004-08-09 19:47       ` Robert Love
2004-07-24  3:03 ` Andrew Morton
2004-07-24  2:14   ` Robert Love
2004-07-24  5:15     ` Chris Wedgwood
2004-07-24  5:41       ` Robert Love
2004-07-24  5:45         ` Chris Wedgwood
2004-07-24  3:11   ` [patch] kernel events layer, updated Robert Love
2004-07-24  7:58     ` Deepak Saxena
2004-07-24  8:23       ` Deepak Saxena
  -- strict thread matches above, loose matches on Subject: below --
2004-07-26  6:04 [patch] kernel events layer Perez-Gonzalez, Inaky
2004-07-26  6:09 ` Andrew Morton
2004-07-26 23:00   ` Matt Mackall
2004-07-26  7:31 Perez-Gonzalez, Inaky
2004-07-26 14:50 ` Robert Love
2004-07-26 16:12   ` Greg KH
2004-07-26 18:13     ` Oliver Neukum
2004-07-26 18:15       ` Robert Love
2004-07-26 19:03       ` Greg KH
2004-07-26 20:44         ` Tim Hockin
2004-07-27 18:15           ` Mike Waychison
2004-07-27 18:35             ` Oliver Neukum
2004-07-27 18:37             ` Tim Hockin
2004-07-26 22:58 Perez-Gonzalez, Inaky
2004-07-27  7:08 ` Deepak Saxena

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox