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 static char *action_to_string(enum kobject_action action
)
44 #ifdef CONFIG_KOBJECT_UEVENT
45 static struct sock
*uevent_sock
;
48 * send_uevent - notify userspace by sending event trough netlink socket
50 * @signal: signal name
51 * @obj: object path (kobject)
52 * @buf: buffer used to pass auxiliary data like the hotplug environment
56 static int send_uevent(const char *signal
, const char *obj
, const void *buf
,
57 int buflen
, int gfp_mask
)
66 len
= strlen(signal
) + 1;
67 len
+= strlen(obj
) + 1;
70 skb
= alloc_skb(len
, gfp_mask
);
74 pos
= skb_put(skb
, len
);
76 pos
+= sprintf(pos
, "%s@%s", signal
, obj
) + 1;
77 memcpy(pos
, buf
, buflen
);
79 return netlink_broadcast(uevent_sock
, skb
, 0, 1, gfp_mask
);
82 static int do_kobject_uevent(struct kobject
*kobj
, enum kobject_action action
,
83 struct attribute
*attr
, int gfp_mask
)
91 path
= kobject_get_path(kobj
, gfp_mask
);
95 signal
= action_to_string(action
);
101 len
+= strlen(attr
->name
) + 2;
102 attrpath
= kmalloc(len
, gfp_mask
);
105 sprintf(attrpath
, "%s/%s", path
, attr
->name
);
106 rc
= send_uevent(signal
, attrpath
, NULL
, 0, gfp_mask
);
109 rc
= send_uevent(signal
, path
, NULL
, 0, gfp_mask
);
118 * kobject_uevent - notify userspace by sending event through netlink socket
120 * @signal: signal name
121 * @kobj: struct kobject that the event is happening to
122 * @attr: optional struct attribute the event belongs to
124 int kobject_uevent(struct kobject
*kobj
, enum kobject_action action
,
125 struct attribute
*attr
)
127 return do_kobject_uevent(kobj
, action
, attr
, GFP_KERNEL
);
129 EXPORT_SYMBOL_GPL(kobject_uevent
);
131 int kobject_uevent_atomic(struct kobject
*kobj
, enum kobject_action action
,
132 struct attribute
*attr
)
134 return do_kobject_uevent(kobj
, action
, attr
, GFP_ATOMIC
);
137 EXPORT_SYMBOL_GPL(kobject_uevent_atomic
);
139 static int __init
kobject_uevent_init(void)
141 uevent_sock
= netlink_kernel_create(NETLINK_KOBJECT_UEVENT
, NULL
);
145 "kobject_uevent: unable to create netlink socket!\n");
152 core_initcall(kobject_uevent_init
);
155 static inline int send_uevent(const char *signal
, const char *obj
,
156 const void *buf
, int buflen
, int gfp_mask
)
161 #endif /* CONFIG_KOBJECT_UEVENT */
164 #ifdef CONFIG_HOTPLUG
166 static spinlock_t sequence_lock
= SPIN_LOCK_UNLOCKED
;
168 #define BUFFER_SIZE 1024 /* should be enough memory for the env */
169 #define NUM_ENVP 32 /* number of env pointers */
171 * kobject_hotplug - notify userspace by executing /sbin/hotplug
173 * @action: action that is happening (usually "ADD" or "REMOVE")
174 * @kobj: struct kobject that the action is happening to
176 void kobject_hotplug(struct kobject
*kobj
, enum kobject_action action
)
184 char *kobj_path
= NULL
;
188 struct kobject
*top_kobj
= kobj
;
191 if (!top_kobj
->kset
&& top_kobj
->parent
) {
193 top_kobj
= top_kobj
->parent
;
194 } while (!top_kobj
->kset
&& top_kobj
->parent
);
197 if (top_kobj
->kset
&& top_kobj
->kset
->hotplug_ops
)
198 kset
= top_kobj
->kset
;
202 /* If the kset has a filter operation, call it.
203 Skip the event, if the filter returns zero. */
204 if (kset
->hotplug_ops
->filter
) {
205 if (!kset
->hotplug_ops
->filter(kset
, kobj
))
209 pr_debug ("%s\n", __FUNCTION__
);
211 action_string
= action_to_string(action
);
215 envp
= kmalloc(NUM_ENVP
* sizeof (char *), GFP_KERNEL
);
218 memset (envp
, 0x00, NUM_ENVP
* sizeof (char *));
220 buffer
= kmalloc(BUFFER_SIZE
, GFP_KERNEL
);
224 if (kset
->hotplug_ops
->name
)
225 name
= kset
->hotplug_ops
->name(kset
, kobj
);
227 name
= kset
->kobj
.name
;
229 argv
[0] = hotplug_path
;
233 /* minimal command environment */
234 envp
[i
++] = "HOME=/";
235 envp
[i
++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
239 envp
[i
++] = scratch
;
240 scratch
+= sprintf(scratch
, "ACTION=%s", action_string
) + 1;
242 kobj_path
= kobject_get_path(kobj
, GFP_KERNEL
);
246 envp
[i
++] = scratch
;
247 scratch
+= sprintf (scratch
, "DEVPATH=%s", kobj_path
) + 1;
249 spin_lock(&sequence_lock
);
250 seq
= ++hotplug_seqnum
;
251 spin_unlock(&sequence_lock
);
253 envp
[i
++] = scratch
;
254 scratch
+= sprintf(scratch
, "SEQNUM=%lld", (long long)seq
) + 1;
256 envp
[i
++] = scratch
;
257 scratch
+= sprintf(scratch
, "SUBSYSTEM=%s", name
) + 1;
259 if (kset
->hotplug_ops
->hotplug
) {
260 /* have the kset specific function add its stuff */
261 retval
= kset
->hotplug_ops
->hotplug (kset
, kobj
,
262 &envp
[i
], NUM_ENVP
- i
, scratch
,
263 BUFFER_SIZE
- (scratch
- buffer
));
265 pr_debug ("%s - hotplug() returned %d\n",
266 __FUNCTION__
, retval
);
271 pr_debug ("%s: %s %s %s %s %s %s %s\n", __FUNCTION__
, argv
[0], argv
[1],
272 envp
[0], envp
[1], envp
[2], envp
[3], envp
[4]);
274 send_uevent(action_string
, kobj_path
, buffer
, scratch
- buffer
, GFP_KERNEL
);
276 if (!hotplug_path
[0])
279 retval
= call_usermodehelper (argv
[0], argv
, envp
, 0);
281 pr_debug ("%s - call_usermodehelper returned %d\n",
282 __FUNCTION__
, retval
);
290 EXPORT_SYMBOL(kobject_hotplug
);
293 * add_hotplug_env_var - helper for creating hotplug environment variables
294 * @envp: Pointer to table of environment variables, as passed into
296 * @num_envp: Number of environment variable slots available, as
297 * passed into hotplug() method.
298 * @cur_index: Pointer to current index into @envp. It should be
299 * initialized to 0 before the first call to add_hotplug_env_var(),
300 * and will be incremented on success.
301 * @buffer: Pointer to buffer for environment variables, as passed
302 * into hotplug() method.
303 * @buffer_size: Length of @buffer, as passed into hotplug() method.
304 * @cur_len: Pointer to current length of space used in @buffer.
305 * Should be initialized to 0 before the first call to
306 * add_hotplug_env_var(), and will be incremented on success.
307 * @format: Format for creating environment variable (of the form
308 * "XXX=%x") for snprintf().
310 * Returns 0 if environment variable was added successfully or -ENOMEM
311 * if no space was available.
313 int add_hotplug_env_var(char **envp
, int num_envp
, int *cur_index
,
314 char *buffer
, int buffer_size
, int *cur_len
,
315 const char *format
, ...)
320 * We check against num_envp - 1 to make sure there is at
321 * least one slot left after we return, since the hotplug
322 * method needs to set the last slot to NULL.
324 if (*cur_index
>= num_envp
- 1)
327 envp
[*cur_index
] = buffer
+ *cur_len
;
329 va_start(args
, format
);
330 *cur_len
+= vsnprintf(envp
[*cur_index
],
331 max(buffer_size
- *cur_len
, 0),
335 if (*cur_len
> buffer_size
)
341 EXPORT_SYMBOL(add_hotplug_env_var
);
343 #endif /* CONFIG_HOTPLUG */