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>
25 #define BUFFER_SIZE 1024 /* buffer for the hotplug env */
26 #define NUM_ENVP 32 /* number of env pointers */
28 #if defined(CONFIG_HOTPLUG)
29 char hotplug_path
[HOTPLUG_PATH_LEN
] = "/sbin/hotplug";
31 static DEFINE_SPINLOCK(sequence_lock
);
33 static char *action_to_string(enum kobject_action action
)
51 static struct sock
*uevent_sock
;
54 * send_uevent - notify userspace by sending event through netlink socket
56 * @signal: signal name
57 * @obj: object path (kobject)
58 * @envp: possible hotplug environment to pass with the message
61 static int send_uevent(const char *signal
, const char *obj
,
62 char **envp
, gfp_t gfp_mask
)
71 len
= strlen(signal
) + 1;
72 len
+= strlen(obj
) + 1;
74 /* allocate buffer with the maximum possible message size */
75 skb
= alloc_skb(len
+ BUFFER_SIZE
, gfp_mask
);
79 pos
= skb_put(skb
, len
);
80 sprintf(pos
, "%s@%s", signal
, obj
);
82 /* copy the environment key by key to our continuous buffer */
86 for (i
= 2; envp
[i
]; i
++) {
87 len
= strlen(envp
[i
]) + 1;
88 pos
= skb_put(skb
, len
);
93 NETLINK_CB(skb
).dst_group
= 1;
94 return netlink_broadcast(uevent_sock
, skb
, 0, 1, gfp_mask
);
97 static int do_kobject_uevent(struct kobject
*kobj
, enum kobject_action action
,
98 struct attribute
*attr
, gfp_t gfp_mask
)
106 path
= kobject_get_path(kobj
, gfp_mask
);
110 signal
= action_to_string(action
);
116 len
+= strlen(attr
->name
) + 2;
117 attrpath
= kmalloc(len
, gfp_mask
);
120 sprintf(attrpath
, "%s/%s", path
, attr
->name
);
121 rc
= send_uevent(signal
, attrpath
, NULL
, gfp_mask
);
124 rc
= send_uevent(signal
, path
, NULL
, gfp_mask
);
132 * kobject_uevent - notify userspace by sending event through netlink socket
134 * @signal: signal name
135 * @kobj: struct kobject that the event is happening to
136 * @attr: optional struct attribute the event belongs to
138 int kobject_uevent(struct kobject
*kobj
, enum kobject_action action
,
139 struct attribute
*attr
)
141 return do_kobject_uevent(kobj
, action
, attr
, GFP_KERNEL
);
143 EXPORT_SYMBOL_GPL(kobject_uevent
);
145 int kobject_uevent_atomic(struct kobject
*kobj
, enum kobject_action action
,
146 struct attribute
*attr
)
148 return do_kobject_uevent(kobj
, action
, attr
, GFP_ATOMIC
);
150 EXPORT_SYMBOL_GPL(kobject_uevent_atomic
);
152 static int __init
kobject_uevent_init(void)
154 uevent_sock
= netlink_kernel_create(NETLINK_KOBJECT_UEVENT
, 1, NULL
,
159 "kobject_uevent: unable to create netlink socket!\n");
166 postcore_initcall(kobject_uevent_init
);
169 * kobject_hotplug - notify userspace by executing /sbin/hotplug
171 * @action: action that is happening (usually "ADD" or "REMOVE")
172 * @kobj: struct kobject that the action is happening to
174 void kobject_hotplug(struct kobject
*kobj
, enum kobject_action action
)
183 char *kobj_path
= NULL
;
184 const char *name
= NULL
;
187 struct kobject
*top_kobj
= kobj
;
189 static struct kset_hotplug_ops null_hotplug_ops
;
190 struct kset_hotplug_ops
*hotplug_ops
= &null_hotplug_ops
;
192 /* If this kobj does not belong to a kset,
193 try to find a parent that does. */
194 if (!top_kobj
->kset
&& top_kobj
->parent
) {
196 top_kobj
= top_kobj
->parent
;
197 } while (!top_kobj
->kset
&& top_kobj
->parent
);
201 kset
= top_kobj
->kset
;
205 if (kset
->hotplug_ops
)
206 hotplug_ops
= kset
->hotplug_ops
;
208 /* If the kset has a filter operation, call it.
209 Skip the event, if the filter returns zero. */
210 if (hotplug_ops
->filter
) {
211 if (!hotplug_ops
->filter(kset
, kobj
))
215 pr_debug ("%s\n", __FUNCTION__
);
217 action_string
= action_to_string(action
);
221 envp
= kmalloc(NUM_ENVP
* sizeof (char *), GFP_KERNEL
);
224 memset (envp
, 0x00, NUM_ENVP
* sizeof (char *));
226 buffer
= kmalloc(BUFFER_SIZE
, GFP_KERNEL
);
230 if (hotplug_ops
->name
)
231 name
= hotplug_ops
->name(kset
, kobj
);
233 name
= kobject_name(&kset
->kobj
);
235 argv
[0] = hotplug_path
;
236 argv
[1] = (char *)name
; /* won't be changed but 'const' has to go */
239 /* minimal command environment */
240 envp
[i
++] = "HOME=/";
241 envp
[i
++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
245 envp
[i
++] = scratch
;
246 scratch
+= sprintf(scratch
, "ACTION=%s", action_string
) + 1;
248 kobj_path
= kobject_get_path(kobj
, GFP_KERNEL
);
252 envp
[i
++] = scratch
;
253 scratch
+= sprintf (scratch
, "DEVPATH=%s", kobj_path
) + 1;
255 envp
[i
++] = scratch
;
256 scratch
+= sprintf(scratch
, "SUBSYSTEM=%s", name
) + 1;
258 /* reserve space for the sequence,
259 * put the real one in after the hotplug call */
260 envp
[i
++] = seq_buff
= scratch
;
261 scratch
+= strlen("SEQNUM=18446744073709551616") + 1;
263 if (hotplug_ops
->hotplug
) {
264 /* have the kset specific function add its stuff */
265 retval
= hotplug_ops
->hotplug (kset
, kobj
,
266 &envp
[i
], NUM_ENVP
- i
, scratch
,
267 BUFFER_SIZE
- (scratch
- buffer
));
269 pr_debug ("%s - hotplug() returned %d\n",
270 __FUNCTION__
, retval
);
275 spin_lock(&sequence_lock
);
276 seq
= ++hotplug_seqnum
;
277 spin_unlock(&sequence_lock
);
278 sprintf(seq_buff
, "SEQNUM=%llu", (unsigned long long)seq
);
280 pr_debug ("%s: %s %s seq=%llu %s %s %s %s %s\n",
281 __FUNCTION__
, argv
[0], argv
[1], (unsigned long long)seq
,
282 envp
[0], envp
[1], envp
[2], envp
[3], envp
[4]);
284 send_uevent(action_string
, kobj_path
, envp
, GFP_KERNEL
);
286 if (!hotplug_path
[0])
289 retval
= call_usermodehelper (argv
[0], argv
, envp
, 0);
291 pr_debug ("%s - call_usermodehelper returned %d\n",
292 __FUNCTION__
, retval
);
300 EXPORT_SYMBOL(kobject_hotplug
);
303 * add_hotplug_env_var - helper for creating hotplug environment variables
304 * @envp: Pointer to table of environment variables, as passed into
306 * @num_envp: Number of environment variable slots available, as
307 * passed into hotplug() method.
308 * @cur_index: Pointer to current index into @envp. It should be
309 * initialized to 0 before the first call to add_hotplug_env_var(),
310 * and will be incremented on success.
311 * @buffer: Pointer to buffer for environment variables, as passed
312 * into hotplug() method.
313 * @buffer_size: Length of @buffer, as passed into hotplug() method.
314 * @cur_len: Pointer to current length of space used in @buffer.
315 * Should be initialized to 0 before the first call to
316 * add_hotplug_env_var(), and will be incremented on success.
317 * @format: Format for creating environment variable (of the form
318 * "XXX=%x") for snprintf().
320 * Returns 0 if environment variable was added successfully or -ENOMEM
321 * if no space was available.
323 int add_hotplug_env_var(char **envp
, int num_envp
, int *cur_index
,
324 char *buffer
, int buffer_size
, int *cur_len
,
325 const char *format
, ...)
330 * We check against num_envp - 1 to make sure there is at
331 * least one slot left after we return, since the hotplug
332 * method needs to set the last slot to NULL.
334 if (*cur_index
>= num_envp
- 1)
337 envp
[*cur_index
] = buffer
+ *cur_len
;
339 va_start(args
, format
);
340 *cur_len
+= vsnprintf(envp
[*cur_index
],
341 max(buffer_size
- *cur_len
, 0),
345 if (*cur_len
> buffer_size
)
351 EXPORT_SYMBOL(add_hotplug_env_var
);
353 #endif /* CONFIG_HOTPLUG */