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_uevent.h>
23 #include <linux/kobject.h>
26 #define BUFFER_SIZE 1024 /* buffer for the hotplug env */
27 #define NUM_ENVP 32 /* number of env pointers */
29 #if defined(CONFIG_KOBJECT_UEVENT) || defined(CONFIG_HOTPLUG)
30 static char *action_to_string(enum kobject_action action
)
53 #ifdef CONFIG_KOBJECT_UEVENT
54 static struct sock
*uevent_sock
;
57 * send_uevent - notify userspace by sending event trough netlink socket
59 * @signal: signal name
60 * @obj: object path (kobject)
61 * @envp: possible hotplug environment to pass with the message
64 static int send_uevent(const char *signal
, const char *obj
,
65 char **envp
, int gfp_mask
)
74 len
= strlen(signal
) + 1;
75 len
+= strlen(obj
) + 1;
77 /* allocate buffer with the maximum possible message size */
78 skb
= alloc_skb(len
+ BUFFER_SIZE
, gfp_mask
);
82 pos
= skb_put(skb
, len
);
83 sprintf(pos
, "%s@%s", signal
, obj
);
85 /* copy the environment key by key to our continuous buffer */
89 for (i
= 2; envp
[i
]; i
++) {
90 len
= strlen(envp
[i
]) + 1;
91 pos
= skb_put(skb
, len
);
96 return netlink_broadcast(uevent_sock
, skb
, 0, 1, gfp_mask
);
99 static int do_kobject_uevent(struct kobject
*kobj
, enum kobject_action action
,
100 struct attribute
*attr
, int gfp_mask
)
108 path
= kobject_get_path(kobj
, gfp_mask
);
112 signal
= action_to_string(action
);
118 len
+= strlen(attr
->name
) + 2;
119 attrpath
= kmalloc(len
, gfp_mask
);
122 sprintf(attrpath
, "%s/%s", path
, attr
->name
);
123 rc
= send_uevent(signal
, attrpath
, NULL
, gfp_mask
);
126 rc
= send_uevent(signal
, path
, NULL
, gfp_mask
);
134 * kobject_uevent - notify userspace by sending event through netlink socket
136 * @signal: signal name
137 * @kobj: struct kobject that the event is happening to
138 * @attr: optional struct attribute the event belongs to
140 int kobject_uevent(struct kobject
*kobj
, enum kobject_action action
,
141 struct attribute
*attr
)
143 return do_kobject_uevent(kobj
, action
, attr
, GFP_KERNEL
);
145 EXPORT_SYMBOL_GPL(kobject_uevent
);
147 int kobject_uevent_atomic(struct kobject
*kobj
, enum kobject_action action
,
148 struct attribute
*attr
)
150 return do_kobject_uevent(kobj
, action
, attr
, GFP_ATOMIC
);
152 EXPORT_SYMBOL_GPL(kobject_uevent_atomic
);
154 static int __init
kobject_uevent_init(void)
156 uevent_sock
= netlink_kernel_create(NETLINK_KOBJECT_UEVENT
, NULL
);
160 "kobject_uevent: unable to create netlink socket!\n");
167 postcore_initcall(kobject_uevent_init
);
170 static inline int send_uevent(const char *signal
, const char *obj
,
171 char **envp
, int gfp_mask
)
176 #endif /* CONFIG_KOBJECT_UEVENT */
179 #ifdef CONFIG_HOTPLUG
180 char hotplug_path
[HOTPLUG_PATH_LEN
] = "/sbin/hotplug";
182 static DEFINE_SPINLOCK(sequence_lock
);
185 * kobject_hotplug - notify userspace by executing /sbin/hotplug
187 * @action: action that is happening (usually "ADD" or "REMOVE")
188 * @kobj: struct kobject that the action is happening to
190 void kobject_hotplug(struct kobject
*kobj
, enum kobject_action action
)
199 char *kobj_path
= NULL
;
200 const char *name
= NULL
;
203 struct kobject
*top_kobj
= kobj
;
205 static struct kset_hotplug_ops null_hotplug_ops
;
206 struct kset_hotplug_ops
*hotplug_ops
= &null_hotplug_ops
;
208 /* If this kobj does not belong to a kset,
209 try to find a parent that does. */
210 if (!top_kobj
->kset
&& top_kobj
->parent
) {
212 top_kobj
= top_kobj
->parent
;
213 } while (!top_kobj
->kset
&& top_kobj
->parent
);
217 kset
= top_kobj
->kset
;
221 if (kset
->hotplug_ops
)
222 hotplug_ops
= kset
->hotplug_ops
;
224 /* If the kset has a filter operation, call it.
225 Skip the event, if the filter returns zero. */
226 if (hotplug_ops
->filter
) {
227 if (!hotplug_ops
->filter(kset
, kobj
))
231 pr_debug ("%s\n", __FUNCTION__
);
233 action_string
= action_to_string(action
);
237 envp
= kmalloc(NUM_ENVP
* sizeof (char *), GFP_KERNEL
);
240 memset (envp
, 0x00, NUM_ENVP
* sizeof (char *));
242 buffer
= kmalloc(BUFFER_SIZE
, GFP_KERNEL
);
246 if (hotplug_ops
->name
)
247 name
= hotplug_ops
->name(kset
, kobj
);
249 name
= kobject_name(&kset
->kobj
);
251 argv
[0] = hotplug_path
;
252 argv
[1] = (char *)name
; /* won't be changed but 'const' has to go */
255 /* minimal command environment */
256 envp
[i
++] = "HOME=/";
257 envp
[i
++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
261 envp
[i
++] = scratch
;
262 scratch
+= sprintf(scratch
, "ACTION=%s", action_string
) + 1;
264 kobj_path
= kobject_get_path(kobj
, GFP_KERNEL
);
268 envp
[i
++] = scratch
;
269 scratch
+= sprintf (scratch
, "DEVPATH=%s", kobj_path
) + 1;
271 envp
[i
++] = scratch
;
272 scratch
+= sprintf(scratch
, "SUBSYSTEM=%s", name
) + 1;
274 /* reserve space for the sequence,
275 * put the real one in after the hotplug call */
276 envp
[i
++] = seq_buff
= scratch
;
277 scratch
+= strlen("SEQNUM=18446744073709551616") + 1;
279 if (hotplug_ops
->hotplug
) {
280 /* have the kset specific function add its stuff */
281 retval
= hotplug_ops
->hotplug (kset
, kobj
,
282 &envp
[i
], NUM_ENVP
- i
, scratch
,
283 BUFFER_SIZE
- (scratch
- buffer
));
285 pr_debug ("%s - hotplug() returned %d\n",
286 __FUNCTION__
, retval
);
291 spin_lock(&sequence_lock
);
292 seq
= ++hotplug_seqnum
;
293 spin_unlock(&sequence_lock
);
294 sprintf(seq_buff
, "SEQNUM=%llu", (unsigned long long)seq
);
296 pr_debug ("%s: %s %s seq=%llu %s %s %s %s %s\n",
297 __FUNCTION__
, argv
[0], argv
[1], (unsigned long long)seq
,
298 envp
[0], envp
[1], envp
[2], envp
[3], envp
[4]);
300 send_uevent(action_string
, kobj_path
, envp
, GFP_KERNEL
);
302 if (!hotplug_path
[0])
305 retval
= call_usermodehelper (argv
[0], argv
, envp
, 0);
307 pr_debug ("%s - call_usermodehelper returned %d\n",
308 __FUNCTION__
, retval
);
316 EXPORT_SYMBOL(kobject_hotplug
);
319 * add_hotplug_env_var - helper for creating hotplug environment variables
320 * @envp: Pointer to table of environment variables, as passed into
322 * @num_envp: Number of environment variable slots available, as
323 * passed into hotplug() method.
324 * @cur_index: Pointer to current index into @envp. It should be
325 * initialized to 0 before the first call to add_hotplug_env_var(),
326 * and will be incremented on success.
327 * @buffer: Pointer to buffer for environment variables, as passed
328 * into hotplug() method.
329 * @buffer_size: Length of @buffer, as passed into hotplug() method.
330 * @cur_len: Pointer to current length of space used in @buffer.
331 * Should be initialized to 0 before the first call to
332 * add_hotplug_env_var(), and will be incremented on success.
333 * @format: Format for creating environment variable (of the form
334 * "XXX=%x") for snprintf().
336 * Returns 0 if environment variable was added successfully or -ENOMEM
337 * if no space was available.
339 int add_hotplug_env_var(char **envp
, int num_envp
, int *cur_index
,
340 char *buffer
, int buffer_size
, int *cur_len
,
341 const char *format
, ...)
346 * We check against num_envp - 1 to make sure there is at
347 * least one slot left after we return, since the hotplug
348 * method needs to set the last slot to NULL.
350 if (*cur_index
>= num_envp
- 1)
353 envp
[*cur_index
] = buffer
+ *cur_len
;
355 va_start(args
, format
);
356 *cur_len
+= vsnprintf(envp
[*cur_index
],
357 max(buffer_size
- *cur_len
, 0),
361 if (*cur_len
> buffer_size
)
367 EXPORT_SYMBOL(add_hotplug_env_var
);
369 #endif /* CONFIG_HOTPLUG */