blob: 04ca4429ddfaf4a077f48c45bb2b718d647b4e42 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * kernel userspace event delivery
3 *
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.
7 *
8 * Licensed under the GNU GPL v2.
9 *
10 * Authors:
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>
15 */
16
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>
24#include <net/sock.h>
25
26#define BUFFER_SIZE 1024 /* buffer for the hotplug env */
27#define NUM_ENVP 32 /* number of env pointers */
28
29#if defined(CONFIG_KOBJECT_UEVENT) || defined(CONFIG_HOTPLUG)
30static char *action_to_string(enum kobject_action action)
31{
32 switch (action) {
33 case KOBJ_ADD:
34 return "add";
35 case KOBJ_REMOVE:
36 return "remove";
37 case KOBJ_CHANGE:
38 return "change";
39 case KOBJ_MOUNT:
40 return "mount";
41 case KOBJ_UMOUNT:
42 return "umount";
43 case KOBJ_OFFLINE:
44 return "offline";
45 case KOBJ_ONLINE:
46 return "online";
47 default:
48 return NULL;
49 }
50}
51#endif
52
53#ifdef CONFIG_KOBJECT_UEVENT
54static struct sock *uevent_sock;
55
56/**
57 * send_uevent - notify userspace by sending event trough netlink socket
58 *
59 * @signal: signal name
60 * @obj: object path (kobject)
61 * @envp: possible hotplug environment to pass with the message
62 * @gfp_mask:
63 */
64static int send_uevent(const char *signal, const char *obj,
65 char **envp, int gfp_mask)
66{
67 struct sk_buff *skb;
68 char *pos;
69 int len;
70
71 if (!uevent_sock)
72 return -EIO;
73
74 len = strlen(signal) + 1;
75 len += strlen(obj) + 1;
76
77 /* allocate buffer with the maximum possible message size */
78 skb = alloc_skb(len + BUFFER_SIZE, gfp_mask);
79 if (!skb)
80 return -ENOMEM;
81
82 pos = skb_put(skb, len);
83 sprintf(pos, "%s@%s", signal, obj);
84
85 /* copy the environment key by key to our continuous buffer */
86 if (envp) {
87 int i;
88
89 for (i = 2; envp[i]; i++) {
90 len = strlen(envp[i]) + 1;
91 pos = skb_put(skb, len);
92 strcpy(pos, envp[i]);
93 }
94 }
95
Patrick McHardyac6d4392005-08-14 19:29:52 -070096 NETLINK_CB(skb).dst_group = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 return netlink_broadcast(uevent_sock, skb, 0, 1, gfp_mask);
98}
99
100static int do_kobject_uevent(struct kobject *kobj, enum kobject_action action,
101 struct attribute *attr, int gfp_mask)
102{
103 char *path;
104 char *attrpath;
105 char *signal;
106 int len;
107 int rc = -ENOMEM;
108
109 path = kobject_get_path(kobj, gfp_mask);
110 if (!path)
111 return -ENOMEM;
112
113 signal = action_to_string(action);
114 if (!signal)
115 return -EINVAL;
116
117 if (attr) {
118 len = strlen(path);
119 len += strlen(attr->name) + 2;
120 attrpath = kmalloc(len, gfp_mask);
121 if (!attrpath)
122 goto exit;
123 sprintf(attrpath, "%s/%s", path, attr->name);
124 rc = send_uevent(signal, attrpath, NULL, gfp_mask);
125 kfree(attrpath);
126 } else
127 rc = send_uevent(signal, path, NULL, gfp_mask);
128
129exit:
130 kfree(path);
131 return rc;
132}
133
134/**
135 * kobject_uevent - notify userspace by sending event through netlink socket
136 *
137 * @signal: signal name
138 * @kobj: struct kobject that the event is happening to
139 * @attr: optional struct attribute the event belongs to
140 */
141int kobject_uevent(struct kobject *kobj, enum kobject_action action,
142 struct attribute *attr)
143{
144 return do_kobject_uevent(kobj, action, attr, GFP_KERNEL);
145}
146EXPORT_SYMBOL_GPL(kobject_uevent);
147
148int kobject_uevent_atomic(struct kobject *kobj, enum kobject_action action,
149 struct attribute *attr)
150{
151 return do_kobject_uevent(kobj, action, attr, GFP_ATOMIC);
152}
153EXPORT_SYMBOL_GPL(kobject_uevent_atomic);
154
155static int __init kobject_uevent_init(void)
156{
Patrick McHardy06628602005-08-15 12:33:26 -0700157 uevent_sock = netlink_kernel_create(NETLINK_KOBJECT_UEVENT, 1, NULL,
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700158 THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160 if (!uevent_sock) {
161 printk(KERN_ERR
162 "kobject_uevent: unable to create netlink socket!\n");
163 return -ENODEV;
164 }
165
166 return 0;
167}
168
169postcore_initcall(kobject_uevent_init);
170
171#else
172static inline int send_uevent(const char *signal, const char *obj,
173 char **envp, int gfp_mask)
174{
175 return 0;
176}
177
178#endif /* CONFIG_KOBJECT_UEVENT */
179
180
181#ifdef CONFIG_HOTPLUG
182char hotplug_path[HOTPLUG_PATH_LEN] = "/sbin/hotplug";
183u64 hotplug_seqnum;
184static DEFINE_SPINLOCK(sequence_lock);
185
186/**
187 * kobject_hotplug - notify userspace by executing /sbin/hotplug
188 *
189 * @action: action that is happening (usually "ADD" or "REMOVE")
190 * @kobj: struct kobject that the action is happening to
191 */
192void kobject_hotplug(struct kobject *kobj, enum kobject_action action)
193{
194 char *argv [3];
195 char **envp = NULL;
196 char *buffer = NULL;
197 char *seq_buff;
198 char *scratch;
199 int i = 0;
200 int retval;
201 char *kobj_path = NULL;
Dmitry Torokhovf3b4f3c2005-04-26 02:32:00 -0500202 const char *name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 char *action_string;
204 u64 seq;
205 struct kobject *top_kobj = kobj;
206 struct kset *kset;
207 static struct kset_hotplug_ops null_hotplug_ops;
208 struct kset_hotplug_ops *hotplug_ops = &null_hotplug_ops;
209
210 /* If this kobj does not belong to a kset,
211 try to find a parent that does. */
212 if (!top_kobj->kset && top_kobj->parent) {
213 do {
214 top_kobj = top_kobj->parent;
215 } while (!top_kobj->kset && top_kobj->parent);
216 }
217
218 if (top_kobj->kset)
219 kset = top_kobj->kset;
220 else
221 return;
222
223 if (kset->hotplug_ops)
224 hotplug_ops = kset->hotplug_ops;
225
226 /* If the kset has a filter operation, call it.
227 Skip the event, if the filter returns zero. */
228 if (hotplug_ops->filter) {
229 if (!hotplug_ops->filter(kset, kobj))
230 return;
231 }
232
233 pr_debug ("%s\n", __FUNCTION__);
234
235 action_string = action_to_string(action);
236 if (!action_string)
237 return;
238
239 envp = kmalloc(NUM_ENVP * sizeof (char *), GFP_KERNEL);
240 if (!envp)
241 return;
242 memset (envp, 0x00, NUM_ENVP * sizeof (char *));
243
244 buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
245 if (!buffer)
246 goto exit;
247
248 if (hotplug_ops->name)
249 name = hotplug_ops->name(kset, kobj);
250 if (name == NULL)
Dmitry Torokhoveb11d8f2005-04-26 02:29:58 -0500251 name = kobject_name(&kset->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 argv [0] = hotplug_path;
Dmitry Torokhovf3b4f3c2005-04-26 02:32:00 -0500254 argv [1] = (char *)name; /* won't be changed but 'const' has to go */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 argv [2] = NULL;
256
257 /* minimal command environment */
258 envp [i++] = "HOME=/";
259 envp [i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
260
261 scratch = buffer;
262
263 envp [i++] = scratch;
264 scratch += sprintf(scratch, "ACTION=%s", action_string) + 1;
265
266 kobj_path = kobject_get_path(kobj, GFP_KERNEL);
267 if (!kobj_path)
268 goto exit;
269
270 envp [i++] = scratch;
271 scratch += sprintf (scratch, "DEVPATH=%s", kobj_path) + 1;
272
273 envp [i++] = scratch;
274 scratch += sprintf(scratch, "SUBSYSTEM=%s", name) + 1;
275
276 /* reserve space for the sequence,
277 * put the real one in after the hotplug call */
278 envp[i++] = seq_buff = scratch;
279 scratch += strlen("SEQNUM=18446744073709551616") + 1;
280
281 if (hotplug_ops->hotplug) {
282 /* have the kset specific function add its stuff */
283 retval = hotplug_ops->hotplug (kset, kobj,
284 &envp[i], NUM_ENVP - i, scratch,
285 BUFFER_SIZE - (scratch - buffer));
286 if (retval) {
287 pr_debug ("%s - hotplug() returned %d\n",
288 __FUNCTION__, retval);
289 goto exit;
290 }
291 }
292
293 spin_lock(&sequence_lock);
294 seq = ++hotplug_seqnum;
295 spin_unlock(&sequence_lock);
296 sprintf(seq_buff, "SEQNUM=%llu", (unsigned long long)seq);
297
298 pr_debug ("%s: %s %s seq=%llu %s %s %s %s %s\n",
299 __FUNCTION__, argv[0], argv[1], (unsigned long long)seq,
300 envp[0], envp[1], envp[2], envp[3], envp[4]);
301
302 send_uevent(action_string, kobj_path, envp, GFP_KERNEL);
303
304 if (!hotplug_path[0])
305 goto exit;
306
307 retval = call_usermodehelper (argv[0], argv, envp, 0);
308 if (retval)
309 pr_debug ("%s - call_usermodehelper returned %d\n",
310 __FUNCTION__, retval);
311
312exit:
313 kfree(kobj_path);
314 kfree(buffer);
315 kfree(envp);
316 return;
317}
318EXPORT_SYMBOL(kobject_hotplug);
319
320/**
321 * add_hotplug_env_var - helper for creating hotplug environment variables
322 * @envp: Pointer to table of environment variables, as passed into
323 * hotplug() method.
324 * @num_envp: Number of environment variable slots available, as
325 * passed into hotplug() method.
326 * @cur_index: Pointer to current index into @envp. It should be
327 * initialized to 0 before the first call to add_hotplug_env_var(),
328 * and will be incremented on success.
329 * @buffer: Pointer to buffer for environment variables, as passed
330 * into hotplug() method.
331 * @buffer_size: Length of @buffer, as passed into hotplug() method.
332 * @cur_len: Pointer to current length of space used in @buffer.
333 * Should be initialized to 0 before the first call to
334 * add_hotplug_env_var(), and will be incremented on success.
335 * @format: Format for creating environment variable (of the form
336 * "XXX=%x") for snprintf().
337 *
338 * Returns 0 if environment variable was added successfully or -ENOMEM
339 * if no space was available.
340 */
341int add_hotplug_env_var(char **envp, int num_envp, int *cur_index,
342 char *buffer, int buffer_size, int *cur_len,
343 const char *format, ...)
344{
345 va_list args;
346
347 /*
348 * We check against num_envp - 1 to make sure there is at
349 * least one slot left after we return, since the hotplug
350 * method needs to set the last slot to NULL.
351 */
352 if (*cur_index >= num_envp - 1)
353 return -ENOMEM;
354
355 envp[*cur_index] = buffer + *cur_len;
356
357 va_start(args, format);
358 *cur_len += vsnprintf(envp[*cur_index],
359 max(buffer_size - *cur_len, 0),
360 format, args) + 1;
361 va_end(args);
362
363 if (*cur_len > buffer_size)
364 return -ENOMEM;
365
366 (*cur_index)++;
367 return 0;
368}
369EXPORT_SYMBOL(add_hotplug_env_var);
370
371#endif /* CONFIG_HOTPLUG */