[PATCH] remove mount/umount uevents from superblock handling
[linux-2.6/cjktty.git] / lib / kobject_uevent.c
blob845bf67d94ca1d0fe4244d5746e8ca6c657b12a8
1 /*
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.
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>
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>
23 #include <net/sock.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";
30 u64 hotplug_seqnum;
31 static DEFINE_SPINLOCK(sequence_lock);
33 static char *action_to_string(enum kobject_action action)
35 switch (action) {
36 case KOBJ_ADD:
37 return "add";
38 case KOBJ_REMOVE:
39 return "remove";
40 case KOBJ_CHANGE:
41 return "change";
42 case KOBJ_OFFLINE:
43 return "offline";
44 case KOBJ_ONLINE:
45 return "online";
46 default:
47 return NULL;
51 static struct sock *uevent_sock;
53 /**
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
59 * @gfp_mask:
61 static int send_uevent(const char *signal, const char *obj,
62 char **envp, gfp_t gfp_mask)
64 struct sk_buff *skb;
65 char *pos;
66 int len;
68 if (!uevent_sock)
69 return -EIO;
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);
76 if (!skb)
77 return -ENOMEM;
79 pos = skb_put(skb, len);
80 sprintf(pos, "%s@%s", signal, obj);
82 /* copy the environment key by key to our continuous buffer */
83 if (envp) {
84 int i;
86 for (i = 2; envp[i]; i++) {
87 len = strlen(envp[i]) + 1;
88 pos = skb_put(skb, len);
89 strcpy(pos, envp[i]);
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)
100 char *path;
101 char *attrpath;
102 char *signal;
103 int len;
104 int rc = -ENOMEM;
106 path = kobject_get_path(kobj, gfp_mask);
107 if (!path)
108 return -ENOMEM;
110 signal = action_to_string(action);
111 if (!signal)
112 return -EINVAL;
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);
126 exit:
127 kfree(path);
128 return rc;
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,
155 THIS_MODULE);
157 if (!uevent_sock) {
158 printk(KERN_ERR
159 "kobject_uevent: unable to create netlink socket!\n");
160 return -ENODEV;
163 return 0;
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)
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;
184 const char *name = NULL;
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;
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);
200 if (top_kobj->kset)
201 kset = top_kobj->kset;
202 else
203 return;
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))
212 return;
215 pr_debug ("%s\n", __FUNCTION__);
217 action_string = action_to_string(action);
218 if (!action_string)
219 return;
221 envp = kmalloc(NUM_ENVP * sizeof (char *), GFP_KERNEL);
222 if (!envp)
223 return;
224 memset (envp, 0x00, NUM_ENVP * sizeof (char *));
226 buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
227 if (!buffer)
228 goto exit;
230 if (hotplug_ops->name)
231 name = hotplug_ops->name(kset, kobj);
232 if (name == NULL)
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 */
237 argv [2] = NULL;
239 /* minimal command environment */
240 envp [i++] = "HOME=/";
241 envp [i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
243 scratch = buffer;
245 envp [i++] = scratch;
246 scratch += sprintf(scratch, "ACTION=%s", action_string) + 1;
248 kobj_path = kobject_get_path(kobj, GFP_KERNEL);
249 if (!kobj_path)
250 goto exit;
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));
268 if (retval) {
269 pr_debug ("%s - hotplug() returned %d\n",
270 __FUNCTION__, retval);
271 goto exit;
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])
287 goto exit;
289 retval = call_usermodehelper (argv[0], argv, envp, 0);
290 if (retval)
291 pr_debug ("%s - call_usermodehelper returned %d\n",
292 __FUNCTION__, retval);
294 exit:
295 kfree(kobj_path);
296 kfree(buffer);
297 kfree(envp);
298 return;
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
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().
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, ...)
327 va_list args;
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)
335 return -ENOMEM;
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),
342 format, args) + 1;
343 va_end(args);
345 if (*cur_len > buffer_size)
346 return -ENOMEM;
348 (*cur_index)++;
349 return 0;
351 EXPORT_SYMBOL(add_hotplug_env_var);
353 #endif /* CONFIG_HOTPLUG */