From mboxrd@z Thu Jan 1 00:00:00 1970 From: Stephen Hemminger Subject: [PATCH] netdev: generate kobject uevent on network state transitions Date: Mon, 24 Nov 2008 15:03:47 -0800 Message-ID: <20081124150347.73050f98@extreme> References: <20081121234110.22b47154@extreme> <20081122223249.4be03e4e@extreme> <20081124114522.1e8de3fe@extreme> <1227557174.3162.7.camel@achroite> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Cc: Ben Hutchings , Kay Sievers , David Miller , Marcel Holtmann , linux-hotplug@vger.kernel.org, netdev@vger.kernel.org To: unlisted-recipients:; (no To-header on input) Return-path: Received: from mail.vyatta.com ([76.74.103.46]:58223 "EHLO mail.vyatta.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752344AbYKXXDw (ORCPT ); Mon, 24 Nov 2008 18:03:52 -0500 In-Reply-To: <1227557174.3162.7.camel@achroite> Sender: netdev-owner@vger.kernel.org List-ID: It is easier for some applications to deal with text based interfaces like uevent, rather than using netlink to listen for events. Note, this does not deal with network namespaces but that is a generic problem that already exists with kobjects (see rename events). Signed-off-by: Stephen Hemminger --- net/core/Makefile | 1 net/core/uevent.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) --- a/net/core/Makefile 2008-11-24 12:07:18.000000000 -0800 +++ b/net/core/Makefile 2008-11-24 12:07:22.000000000 -0800 @@ -17,3 +17,4 @@ obj-$(CONFIG_NET_PKTGEN) += pktgen.o obj-$(CONFIG_NETPOLL) += netpoll.o obj-$(CONFIG_NET_DMA) += user_dma.o obj-$(CONFIG_FIB_RULES) += fib_rules.o +obj-$(CONFIG_HOTPLUG) += uevent.o --- /dev/null 1970-01-01 00:00:00.000000000 +0000 +++ b/net/core/uevent.c 2008-11-24 12:11:46.000000000 -0800 @@ -0,0 +1,55 @@ +/* + * Linux network device event notification + * + * Author: + * Stephen Hemminger + */ + +#include +#include +#include +#include + +/* + * Generate uevent in response to network device state changes. + * Other events are already handled by device subsystem. + */ +static int netdev_event(struct notifier_block *this, unsigned long event, void *ptr) +{ + struct net_device *netdev = ptr; + + switch (event) { + case NETDEV_UP: + kobject_uevent(&netdev->dev.kobj, KOBJ_ONLINE); + break; + + case NETDEV_DOWN: + kobject_uevent(&netdev->dev.kobj, KOBJ_OFFLINE); + break; + + case NETDEV_CHANGE: { + char str[64] = "DEVSTATE=UP"; + char *envp[2] = { str, NULL }; + + if (netif_oper_up(netdev)) + strcat(str, ",RUNNING"); + if (netif_carrier_ok(netdev)) + strcat(str, ",LOWER_UP"); + if (netif_dormant(netdev)) + strcat(str, ",DORMANT"); + kobject_uevent_env(&netdev->dev.kobj, KOBJ_CHANGE, envp); + break; + } + } + return NOTIFY_DONE; +} + +static struct notifier_block netdev_uevent_notifier = { + .notifier_call = netdev_event, +}; + +static int __init netdev_uevent_init(void) +{ + return register_netdevice_notifier(&netdev_uevent_notifier); +} +device_initcall(netdev_uevent_init);