2 * kernel userspace event delivery
4 * Copyright (C) 2004 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2004 Novell, Inc. All rights reserved.
6 * Copyright (C) 2004 IBM, Inc. All rights reserved.
8 * Licensed under the GNU GPL v2.
11 * Robert Love <rml@novell.com>
12 * Kay Sievers <kay.sievers@vrfy.org>
13 * Arjan van de Ven <arjanv@redhat.com>
14 * Greg Kroah-Hartman <greg@kroah.com>
17 #include <linux/spinlock.h>
18 #include <linux/socket.h>
19 #include <linux/skbuff.h>
20 #include <linux/netlink.h>
21 #include <linux/string.h>
22 #include <linux/kobject.h>
26 /* the strings here must match the enum in include/linux/kobject.h */
27 const char *kobject_actions[] = {
36 #if defined(CONFIG_HOTPLUG)
38 char uevent_helper[UEVENT_HELPER_PATH_LEN] = "/sbin/hotplug";
39 static DEFINE_SPINLOCK(sequence_lock);
40 #if defined(CONFIG_NET)
41 static struct sock *uevent_sock;
45 * kobject_uevent_env - send an uevent with environmental data
47 * @action: action that is happening (usually KOBJ_MOVE)
48 * @kobj: struct kobject that the action is happening to
49 * @envp_ext: pointer to environmental data
51 * Returns 0 if kobject_uevent() is completed with success or the
52 * corresponding error when it fails.
54 int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
57 struct kobj_uevent_env *env;
58 const char *action_string = kobject_actions[action];
59 const char *devpath = NULL;
60 const char *subsystem;
61 struct kobject *top_kobj;
63 struct kset_uevent_ops *uevent_ops;
68 pr_debug("%s\n", __FUNCTION__);
70 /* search the kset we belong to */
72 while (!top_kobj->kset && top_kobj->parent) {
73 top_kobj = top_kobj->parent;
75 if (!top_kobj->kset) {
76 pr_debug("kobject attempted to send uevent without kset!\n");
80 kset = top_kobj->kset;
81 uevent_ops = kset->uevent_ops;
83 /* skip the event, if the filter returns zero. */
84 if (uevent_ops && uevent_ops->filter)
85 if (!uevent_ops->filter(kset, kobj)) {
86 pr_debug("kobject filter function caused the event to drop!\n");
90 /* originating subsystem */
91 if (uevent_ops && uevent_ops->name)
92 subsystem = uevent_ops->name(kset, kobj);
94 subsystem = kobject_name(&kset->kobj);
96 pr_debug("unset subsytem caused the event to drop!\n");
100 /* environment buffer */
101 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
105 /* complete object path */
106 devpath = kobject_get_path(kobj, GFP_KERNEL);
113 retval = add_uevent_var(env, "ACTION=%s", action_string);
116 retval = add_uevent_var(env, "DEVPATH=%s", devpath);
119 retval = add_uevent_var(env, "SUBSYSTEM=%s", subsystem);
123 /* keys passed in from the caller */
125 for (i = 0; envp_ext[i]; i++) {
126 retval = add_uevent_var(env, envp_ext[i]);
132 /* let the kset specific function add its stuff */
133 if (uevent_ops && uevent_ops->uevent) {
134 retval = uevent_ops->uevent(kset, kobj, env);
136 pr_debug ("%s - uevent() returned %d\n",
137 __FUNCTION__, retval);
142 /* we will send an event, so request a new sequence number */
143 spin_lock(&sequence_lock);
144 seq = ++uevent_seqnum;
145 spin_unlock(&sequence_lock);
146 retval = add_uevent_var(env, "SEQNUM=%llu", (unsigned long long)seq);
150 #if defined(CONFIG_NET)
151 /* send netlink message */
156 /* allocate message with the maximum possible size */
157 len = strlen(action_string) + strlen(devpath) + 2;
158 skb = alloc_skb(len + env->buflen, GFP_KERNEL);
163 scratch = skb_put(skb, len);
164 sprintf(scratch, "%s@%s", action_string, devpath);
166 /* copy keys to our continuous event payload buffer */
167 for (i = 0; i < env->envp_idx; i++) {
168 len = strlen(env->envp[i]) + 1;
169 scratch = skb_put(skb, len);
170 strcpy(scratch, env->envp[i]);
173 NETLINK_CB(skb).dst_group = 1;
174 netlink_broadcast(uevent_sock, skb, 0, 1, GFP_KERNEL);
179 /* call uevent_helper, usually only enabled during early boot */
180 if (uevent_helper[0]) {
183 argv [0] = uevent_helper;
184 argv [1] = (char *)subsystem;
186 retval = add_uevent_var(env, "HOME=/");
189 retval = add_uevent_var(env, "PATH=/sbin:/bin:/usr/sbin:/usr/bin");
193 call_usermodehelper (argv[0], argv, env->envp, UMH_WAIT_EXEC);
202 EXPORT_SYMBOL_GPL(kobject_uevent_env);
205 * kobject_uevent - notify userspace by ending an uevent
207 * @action: action that is happening (usually KOBJ_ADD and KOBJ_REMOVE)
208 * @kobj: struct kobject that the action is happening to
210 * Returns 0 if kobject_uevent() is completed with success or the
211 * corresponding error when it fails.
213 int kobject_uevent(struct kobject *kobj, enum kobject_action action)
215 return kobject_uevent_env(kobj, action, NULL);
218 EXPORT_SYMBOL_GPL(kobject_uevent);
221 * add_uevent_var - add key value string to the environment buffer
222 * @env: environment buffer structure
223 * @format: printf format for the key=value pair
225 * Returns 0 if environment variable was added successfully or -ENOMEM
226 * if no space was available.
228 int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...)
233 if (env->envp_idx >= ARRAY_SIZE(env->envp)) {
234 printk(KERN_ERR "add_uevent_var: too many keys\n");
239 va_start(args, format);
240 len = vsnprintf(&env->buf[env->buflen],
241 sizeof(env->buf) - env->buflen,
245 if (len >= (sizeof(env->buf) - env->buflen)) {
246 printk(KERN_ERR "add_uevent_var: buffer size too small\n");
251 env->envp[env->envp_idx++] = &env->buf[env->buflen];
252 env->buflen += len + 1;
255 EXPORT_SYMBOL_GPL(add_uevent_var);
257 #if defined(CONFIG_NET)
258 static int __init kobject_uevent_init(void)
260 uevent_sock = netlink_kernel_create(&init_net, NETLINK_KOBJECT_UEVENT,
261 1, NULL, NULL, THIS_MODULE);
264 "kobject_uevent: unable to create netlink socket!\n");
271 postcore_initcall(kobject_uevent_init);
274 #endif /* CONFIG_HOTPLUG */