[PATCH] remove mount/umount uevents from superblock handling
[linux-2.6-block.git] / lib / kobject_uevent.c
CommitLineData
1da177e4
LT
1/*
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>
1da177e4
LT
22#include <linux/kobject.h>
23#include <net/sock.h>
24
25#define BUFFER_SIZE 1024 /* buffer for the hotplug env */
26#define NUM_ENVP 32 /* number of env pointers */
27
0296b228
KS
28#if defined(CONFIG_HOTPLUG)
29char hotplug_path[HOTPLUG_PATH_LEN] = "/sbin/hotplug";
30u64 hotplug_seqnum;
31static DEFINE_SPINLOCK(sequence_lock);
32
1da177e4
LT
33static char *action_to_string(enum kobject_action action)
34{
35 switch (action) {
36 case KOBJ_ADD:
37 return "add";
38 case KOBJ_REMOVE:
39 return "remove";
40 case KOBJ_CHANGE:
41 return "change";
1da177e4
LT
42 case KOBJ_OFFLINE:
43 return "offline";
44 case KOBJ_ONLINE:
45 return "online";
46 default:
47 return NULL;
48 }
49}
1da177e4 50
1da177e4
LT
51static struct sock *uevent_sock;
52
53/**
4ed17dcc 54 * send_uevent - notify userspace by sending event through netlink socket
1da177e4
LT
55 *
56 * @signal: signal name
57 * @obj: object path (kobject)
58 * @envp: possible hotplug environment to pass with the message
59 * @gfp_mask:
60 */
61static int send_uevent(const char *signal, const char *obj,
fd4f2df2 62 char **envp, gfp_t gfp_mask)
1da177e4
LT
63{
64 struct sk_buff *skb;
65 char *pos;
66 int len;
67
68 if (!uevent_sock)
69 return -EIO;
70
71 len = strlen(signal) + 1;
72 len += strlen(obj) + 1;
73
74 /* allocate buffer with the maximum possible message size */
75 skb = alloc_skb(len + BUFFER_SIZE, gfp_mask);
76 if (!skb)
77 return -ENOMEM;
78
79 pos = skb_put(skb, len);
80 sprintf(pos, "%s@%s", signal, obj);
81
82 /* copy the environment key by key to our continuous buffer */
83 if (envp) {
84 int i;
85
86 for (i = 2; envp[i]; i++) {
87 len = strlen(envp[i]) + 1;
88 pos = skb_put(skb, len);
89 strcpy(pos, envp[i]);
90 }
91 }
92
ac6d439d 93 NETLINK_CB(skb).dst_group = 1;
1da177e4
LT
94 return netlink_broadcast(uevent_sock, skb, 0, 1, gfp_mask);
95}
96
97static int do_kobject_uevent(struct kobject *kobj, enum kobject_action action,
fd4f2df2 98 struct attribute *attr, gfp_t gfp_mask)
1da177e4
LT
99{
100 char *path;
101 char *attrpath;
102 char *signal;
103 int len;
104 int rc = -ENOMEM;
105
106 path = kobject_get_path(kobj, gfp_mask);
107 if (!path)
108 return -ENOMEM;
109
110 signal = action_to_string(action);
111 if (!signal)
112 return -EINVAL;
113
114 if (attr) {
115 len = strlen(path);
116 len += strlen(attr->name) + 2;
117 attrpath = kmalloc(len, gfp_mask);
118 if (!attrpath)
119 goto exit;
120 sprintf(attrpath, "%s/%s", path, attr->name);
121 rc = send_uevent(signal, attrpath, NULL, gfp_mask);
122 kfree(attrpath);
123 } else
124 rc = send_uevent(signal, path, NULL, gfp_mask);
125
126exit:
127 kfree(path);
128 return rc;
129}
130
131/**
132 * kobject_uevent - notify userspace by sending event through netlink socket
133 *
134 * @signal: signal name
135 * @kobj: struct kobject that the event is happening to
136 * @attr: optional struct attribute the event belongs to
137 */
138int kobject_uevent(struct kobject *kobj, enum kobject_action action,
139 struct attribute *attr)
140{
141 return do_kobject_uevent(kobj, action, attr, GFP_KERNEL);
142}
143EXPORT_SYMBOL_GPL(kobject_uevent);
144
145int kobject_uevent_atomic(struct kobject *kobj, enum kobject_action action,
146 struct attribute *attr)
147{
148 return do_kobject_uevent(kobj, action, attr, GFP_ATOMIC);
149}
150EXPORT_SYMBOL_GPL(kobject_uevent_atomic);
151
152static int __init kobject_uevent_init(void)
153{
06628607 154 uevent_sock = netlink_kernel_create(NETLINK_KOBJECT_UEVENT, 1, NULL,
4fdb3bb7 155 THIS_MODULE);
1da177e4
LT
156
157 if (!uevent_sock) {
158 printk(KERN_ERR
159 "kobject_uevent: unable to create netlink socket!\n");
160 return -ENODEV;
161 }
162
163 return 0;
164}
165
166postcore_initcall(kobject_uevent_init);
167
1da177e4
LT
168/**
169 * kobject_hotplug - notify userspace by executing /sbin/hotplug
170 *
171 * @action: action that is happening (usually "ADD" or "REMOVE")
172 * @kobj: struct kobject that the action is happening to
173 */
174void kobject_hotplug(struct kobject *kobj, enum kobject_action action)
175{
176 char *argv [3];
177 char **envp = NULL;
178 char *buffer = NULL;
179 char *seq_buff;
180 char *scratch;
181 int i = 0;
182 int retval;
183 char *kobj_path = NULL;
f3b4f3c6 184 const char *name = NULL;
1da177e4
LT
185 char *action_string;
186 u64 seq;
187 struct kobject *top_kobj = kobj;
188 struct kset *kset;
189 static struct kset_hotplug_ops null_hotplug_ops;
190 struct kset_hotplug_ops *hotplug_ops = &null_hotplug_ops;
191
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) {
195 do {
196 top_kobj = top_kobj->parent;
197 } while (!top_kobj->kset && top_kobj->parent);
198 }
199
200 if (top_kobj->kset)
201 kset = top_kobj->kset;
202 else
203 return;
204
205 if (kset->hotplug_ops)
206 hotplug_ops = kset->hotplug_ops;
207
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))
212 return;
213 }
214
215 pr_debug ("%s\n", __FUNCTION__);
216
217 action_string = action_to_string(action);
218 if (!action_string)
219 return;
220
221 envp = kmalloc(NUM_ENVP * sizeof (char *), GFP_KERNEL);
222 if (!envp)
223 return;
224 memset (envp, 0x00, NUM_ENVP * sizeof (char *));
225
226 buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
227 if (!buffer)
228 goto exit;
229
230 if (hotplug_ops->name)
231 name = hotplug_ops->name(kset, kobj);
232 if (name == NULL)
eb11d8ff 233 name = kobject_name(&kset->kobj);
1da177e4
LT
234
235 argv [0] = hotplug_path;
f3b4f3c6 236 argv [1] = (char *)name; /* won't be changed but 'const' has to go */
1da177e4
LT
237 argv [2] = NULL;
238
239 /* minimal command environment */
240 envp [i++] = "HOME=/";
241 envp [i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
242
243 scratch = buffer;
244
245 envp [i++] = scratch;
246 scratch += sprintf(scratch, "ACTION=%s", action_string) + 1;
247
248 kobj_path = kobject_get_path(kobj, GFP_KERNEL);
249 if (!kobj_path)
250 goto exit;
251
252 envp [i++] = scratch;
253 scratch += sprintf (scratch, "DEVPATH=%s", kobj_path) + 1;
254
255 envp [i++] = scratch;
256 scratch += sprintf(scratch, "SUBSYSTEM=%s", name) + 1;
257
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;
262
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));
268 if (retval) {
269 pr_debug ("%s - hotplug() returned %d\n",
270 __FUNCTION__, retval);
271 goto exit;
272 }
273 }
274
275 spin_lock(&sequence_lock);
276 seq = ++hotplug_seqnum;
277 spin_unlock(&sequence_lock);
278 sprintf(seq_buff, "SEQNUM=%llu", (unsigned long long)seq);
279
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]);
283
284 send_uevent(action_string, kobj_path, envp, GFP_KERNEL);
285
286 if (!hotplug_path[0])
287 goto exit;
288
289 retval = call_usermodehelper (argv[0], argv, envp, 0);
290 if (retval)
291 pr_debug ("%s - call_usermodehelper returned %d\n",
292 __FUNCTION__, retval);
293
294exit:
295 kfree(kobj_path);
296 kfree(buffer);
297 kfree(envp);
298 return;
299}
300EXPORT_SYMBOL(kobject_hotplug);
301
302/**
303 * add_hotplug_env_var - helper for creating hotplug environment variables
304 * @envp: Pointer to table of environment variables, as passed into
305 * hotplug() method.
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().
319 *
320 * Returns 0 if environment variable was added successfully or -ENOMEM
321 * if no space was available.
322 */
323int 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, ...)
326{
327 va_list args;
328
329 /*
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.
333 */
334 if (*cur_index >= num_envp - 1)
335 return -ENOMEM;
336
337 envp[*cur_index] = buffer + *cur_len;
338
339 va_start(args, format);
340 *cur_len += vsnprintf(envp[*cur_index],
341 max(buffer_size - *cur_len, 0),
342 format, args) + 1;
343 va_end(args);
344
345 if (*cur_len > buffer_size)
346 return -ENOMEM;
347
348 (*cur_index)++;
349 return 0;
350}
351EXPORT_SYMBOL(add_hotplug_env_var);
352
353#endif /* CONFIG_HOTPLUG */