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)
55 static struct sock *uevent_sock;
58 * send_uevent - notify userspace by sending event through netlink socket
60 * @signal: signal name
61 * @obj: object path (kobject)
62 * @envp: possible hotplug environment to pass with the message
65 static int send_uevent(const char *signal, const char *obj,
66 char **envp, gfp_t gfp_mask)
75 len = strlen(signal) + 1;
76 len += strlen(obj) + 1;
78 /* allocate buffer with the maximum possible message size */
79 skb = alloc_skb(len + BUFFER_SIZE, gfp_mask);
83 pos = skb_put(skb, len);
84 sprintf(pos, "%s@%s", signal, obj);
86 /* copy the environment key by key to our continuous buffer */
90 for (i = 2; envp[i]; i++) {
91 len = strlen(envp[i]) + 1;
92 pos = skb_put(skb, len);
97 NETLINK_CB(skb).dst_group = 1;
98 return netlink_broadcast(uevent_sock, skb, 0, 1, gfp_mask);
101 static int do_kobject_uevent(struct kobject *kobj, enum kobject_action action,
102 struct attribute *attr, gfp_t gfp_mask)
110 path = kobject_get_path(kobj, gfp_mask);
114 signal = action_to_string(action);
120 len += strlen(attr->name) + 2;
121 attrpath = kmalloc(len, gfp_mask);
124 sprintf(attrpath, "%s/%s", path, attr->name);
125 rc = send_uevent(signal, attrpath, NULL, gfp_mask);
128 rc = send_uevent(signal, path, NULL, gfp_mask);
136 * kobject_uevent - notify userspace by sending event through netlink socket
138 * @signal: signal name
139 * @kobj: struct kobject that the event is happening to
140 * @attr: optional struct attribute the event belongs to
142 int kobject_uevent(struct kobject *kobj, enum kobject_action action,
143 struct attribute *attr)
145 return do_kobject_uevent(kobj, action, attr, GFP_KERNEL);
147 EXPORT_SYMBOL_GPL(kobject_uevent);
149 int kobject_uevent_atomic(struct kobject *kobj, enum kobject_action action,
150 struct attribute *attr)
152 return do_kobject_uevent(kobj, action, attr, GFP_ATOMIC);
154 EXPORT_SYMBOL_GPL(kobject_uevent_atomic);
156 static int __init kobject_uevent_init(void)
158 uevent_sock = netlink_kernel_create(NETLINK_KOBJECT_UEVENT, 1, NULL,
163 "kobject_uevent: unable to create netlink socket!\n");
170 postcore_initcall(kobject_uevent_init);
173 * kobject_hotplug - notify userspace by executing /sbin/hotplug
175 * @action: action that is happening (usually "ADD" or "REMOVE")
176 * @kobj: struct kobject that the action is happening to
178 void kobject_hotplug(struct kobject *kobj, enum kobject_action action)
187 char *kobj_path = NULL;
188 const char *name = NULL;
191 struct kobject *top_kobj = kobj;
193 static struct kset_hotplug_ops null_hotplug_ops;
194 struct kset_hotplug_ops *hotplug_ops = &null_hotplug_ops;
196 /* If this kobj does not belong to a kset,
197 try to find a parent that does. */
198 if (!top_kobj->kset && top_kobj->parent) {
200 top_kobj = top_kobj->parent;
201 } while (!top_kobj->kset && top_kobj->parent);
205 kset = top_kobj->kset;
209 if (kset->hotplug_ops)
210 hotplug_ops = kset->hotplug_ops;
212 /* If the kset has a filter operation, call it.
213 Skip the event, if the filter returns zero. */
214 if (hotplug_ops->filter) {
215 if (!hotplug_ops->filter(kset, kobj))
219 pr_debug ("%s\n", __FUNCTION__);
221 action_string = action_to_string(action);
225 envp = kmalloc(NUM_ENVP * sizeof (char *), GFP_KERNEL);
228 memset (envp, 0x00, NUM_ENVP * sizeof (char *));
230 buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
234 if (hotplug_ops->name)
235 name = hotplug_ops->name(kset, kobj);
237 name = kobject_name(&kset->kobj);
239 argv [0] = hotplug_path;
240 argv [1] = (char *)name; /* won't be changed but 'const' has to go */
243 /* minimal command environment */
244 envp [i++] = "HOME=/";
245 envp [i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
249 envp [i++] = scratch;
250 scratch += sprintf(scratch, "ACTION=%s", action_string) + 1;
252 kobj_path = kobject_get_path(kobj, GFP_KERNEL);
256 envp [i++] = scratch;
257 scratch += sprintf (scratch, "DEVPATH=%s", kobj_path) + 1;
259 envp [i++] = scratch;
260 scratch += sprintf(scratch, "SUBSYSTEM=%s", name) + 1;
262 /* reserve space for the sequence,
263 * put the real one in after the hotplug call */
264 envp[i++] = seq_buff = scratch;
265 scratch += strlen("SEQNUM=18446744073709551616") + 1;
267 if (hotplug_ops->hotplug) {
268 /* have the kset specific function add its stuff */
269 retval = hotplug_ops->hotplug (kset, kobj,
270 &envp[i], NUM_ENVP - i, scratch,
271 BUFFER_SIZE - (scratch - buffer));
273 pr_debug ("%s - hotplug() returned %d\n",
274 __FUNCTION__, retval);
279 spin_lock(&sequence_lock);
280 seq = ++hotplug_seqnum;
281 spin_unlock(&sequence_lock);
282 sprintf(seq_buff, "SEQNUM=%llu", (unsigned long long)seq);
284 pr_debug ("%s: %s %s seq=%llu %s %s %s %s %s\n",
285 __FUNCTION__, argv[0], argv[1], (unsigned long long)seq,
286 envp[0], envp[1], envp[2], envp[3], envp[4]);
288 send_uevent(action_string, kobj_path, envp, GFP_KERNEL);
290 if (!hotplug_path[0])
293 retval = call_usermodehelper (argv[0], argv, envp, 0);
295 pr_debug ("%s - call_usermodehelper returned %d\n",
296 __FUNCTION__, retval);
304 EXPORT_SYMBOL(kobject_hotplug);
307 * add_hotplug_env_var - helper for creating hotplug environment variables
308 * @envp: Pointer to table of environment variables, as passed into
310 * @num_envp: Number of environment variable slots available, as
311 * passed into hotplug() method.
312 * @cur_index: Pointer to current index into @envp. It should be
313 * initialized to 0 before the first call to add_hotplug_env_var(),
314 * and will be incremented on success.
315 * @buffer: Pointer to buffer for environment variables, as passed
316 * into hotplug() method.
317 * @buffer_size: Length of @buffer, as passed into hotplug() method.
318 * @cur_len: Pointer to current length of space used in @buffer.
319 * Should be initialized to 0 before the first call to
320 * add_hotplug_env_var(), and will be incremented on success.
321 * @format: Format for creating environment variable (of the form
322 * "XXX=%x") for snprintf().
324 * Returns 0 if environment variable was added successfully or -ENOMEM
325 * if no space was available.
327 int add_hotplug_env_var(char **envp, int num_envp, int *cur_index,
328 char *buffer, int buffer_size, int *cur_len,
329 const char *format, ...)
334 * We check against num_envp - 1 to make sure there is at
335 * least one slot left after we return, since the hotplug
336 * method needs to set the last slot to NULL.
338 if (*cur_index >= num_envp - 1)
341 envp[*cur_index] = buffer + *cur_len;
343 va_start(args, format);
344 *cur_len += vsnprintf(envp[*cur_index],
345 max(buffer_size - *cur_len, 0),
349 if (*cur_len > buffer_size)
355 EXPORT_SYMBOL(add_hotplug_env_var);
357 #endif /* CONFIG_HOTPLUG */