OpenWRT kernel 2.6 patches
[tomato.git] / release / src-rt / linux / linux-2.6 / lib / kobject_uevent.c
blobf22d2b956a794b73dee15a3162a8d27aff09e7b9
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 2048 /* buffer for the variables */
26 #define NUM_ENVP 32 /* number of env pointers */
28 #if defined(CONFIG_HOTPLUG)
29 u64 uevent_seqnum;
30 char uevent_helper[UEVENT_HELPER_PATH_LEN] = "/sbin/hotplug";
31 static DEFINE_SPINLOCK(sequence_lock);
32 #if defined(CONFIG_NET)
33 struct sock *uevent_sock = NULL;
34 EXPORT_SYMBOL_GPL(uevent_sock);
35 #endif
37 u64 uevent_next_seqnum(void)
39 u64 seq;
41 spin_lock(&sequence_lock);
42 seq = ++uevent_seqnum;
43 spin_unlock(&sequence_lock);
45 return seq;
47 EXPORT_SYMBOL_GPL(uevent_next_seqnum);
49 static char *action_to_string(enum kobject_action action)
51 switch (action) {
52 case KOBJ_ADD:
53 return "add";
54 case KOBJ_REMOVE:
55 return "remove";
56 case KOBJ_CHANGE:
57 return "change";
58 case KOBJ_OFFLINE:
59 return "offline";
60 case KOBJ_ONLINE:
61 return "online";
62 case KOBJ_MOVE:
63 return "move";
64 default:
65 return NULL;
69 /**
70 * kobject_uevent_env - send an uevent with environmental data
72 * @action: action that is happening (usually KOBJ_MOVE)
73 * @kobj: struct kobject that the action is happening to
74 * @envp_ext: pointer to environmental data
76 * Returns 0 if kobject_uevent() is completed with success or the
77 * corresponding error when it fails.
79 int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
80 char *envp_ext[])
82 char **envp;
83 char *buffer;
84 char *scratch;
85 const char *action_string;
86 const char *devpath = NULL;
87 const char *subsystem;
88 struct kobject *top_kobj;
89 struct kset *kset;
90 struct kset_uevent_ops *uevent_ops;
91 u64 seq;
92 char *seq_buff;
93 int i = 0;
94 int retval = 0;
95 int j;
97 pr_debug("%s\n", __FUNCTION__);
99 action_string = action_to_string(action);
100 if (!action_string) {
101 pr_debug("kobject attempted to send uevent without action_string!\n");
102 return -EINVAL;
105 /* search the kset we belong to */
106 top_kobj = kobj;
107 while (!top_kobj->kset && top_kobj->parent) {
108 top_kobj = top_kobj->parent;
110 if (!top_kobj->kset) {
111 pr_debug("kobject attempted to send uevent without kset!\n");
112 return -EINVAL;
115 kset = top_kobj->kset;
116 uevent_ops = kset->uevent_ops;
118 /* skip the event, if the filter returns zero. */
119 if (uevent_ops && uevent_ops->filter)
120 if (!uevent_ops->filter(kset, kobj)) {
121 pr_debug("kobject filter function caused the event to drop!\n");
122 return 0;
125 /* originating subsystem */
126 if (uevent_ops && uevent_ops->name)
127 subsystem = uevent_ops->name(kset, kobj);
128 else
129 subsystem = kobject_name(&kset->kobj);
130 if (!subsystem) {
131 pr_debug("unset subsytem caused the event to drop!\n");
132 return 0;
135 /* environment index */
136 envp = kzalloc(NUM_ENVP * sizeof (char *), GFP_KERNEL);
137 if (!envp)
138 return -ENOMEM;
140 /* environment values */
141 buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
142 if (!buffer) {
143 retval = -ENOMEM;
144 goto exit;
147 /* complete object path */
148 devpath = kobject_get_path(kobj, GFP_KERNEL);
149 if (!devpath) {
150 retval = -ENOENT;
151 goto exit;
154 /* event environemnt for helper process only */
155 envp[i++] = "HOME=/";
156 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
158 /* default keys */
159 scratch = buffer;
160 envp [i++] = scratch;
161 scratch += sprintf(scratch, "ACTION=%s", action_string) + 1;
162 envp [i++] = scratch;
163 scratch += sprintf (scratch, "DEVPATH=%s", devpath) + 1;
164 envp [i++] = scratch;
165 scratch += sprintf(scratch, "SUBSYSTEM=%s", subsystem) + 1;
166 for (j = 0; envp_ext && envp_ext[j]; j++)
167 envp[i++] = envp_ext[j];
168 /* just reserve the space, overwrite it after kset call has returned */
169 envp[i++] = seq_buff = scratch;
170 scratch += strlen("SEQNUM=18446744073709551616") + 1;
172 /* let the kset specific function add its stuff */
173 if (uevent_ops && uevent_ops->uevent) {
174 retval = uevent_ops->uevent(kset, kobj,
175 &envp[i], NUM_ENVP - i, scratch,
176 BUFFER_SIZE - (scratch - buffer));
177 if (retval) {
178 pr_debug ("%s - uevent() returned %d\n",
179 __FUNCTION__, retval);
180 goto exit;
184 /* we will send an event, request a new sequence number */
185 seq = uevent_next_seqnum();
186 sprintf(seq_buff, "SEQNUM=%llu", (unsigned long long)seq);
188 #if defined(CONFIG_NET)
189 /* send netlink message */
190 if (uevent_sock) {
191 struct sk_buff *skb;
192 size_t len;
194 /* allocate message with the maximum possible size */
195 len = strlen(action_string) + strlen(devpath) + 2;
196 skb = alloc_skb(len + BUFFER_SIZE, GFP_KERNEL);
197 if (skb) {
198 /* add header */
199 scratch = skb_put(skb, len);
200 sprintf(scratch, "%s@%s", action_string, devpath);
202 /* copy keys to our continuous event payload buffer */
203 for (i = 2; envp[i]; i++) {
204 len = strlen(envp[i]) + 1;
205 scratch = skb_put(skb, len);
206 strcpy(scratch, envp[i]);
209 NETLINK_CB(skb).dst_group = 1;
210 netlink_broadcast(uevent_sock, skb, 0, 1, GFP_KERNEL);
213 #endif
215 /* call uevent_helper, usually only enabled during early boot */
216 if (uevent_helper[0]) {
217 char *argv [3];
219 argv [0] = uevent_helper;
220 argv [1] = (char *)subsystem;
221 argv [2] = NULL;
222 call_usermodehelper (argv[0], argv, envp, 0);
225 exit:
226 kfree(devpath);
227 kfree(buffer);
228 kfree(envp);
229 return retval;
232 EXPORT_SYMBOL_GPL(kobject_uevent_env);
235 * kobject_uevent - notify userspace by ending an uevent
237 * @action: action that is happening (usually KOBJ_ADD and KOBJ_REMOVE)
238 * @kobj: struct kobject that the action is happening to
240 * Returns 0 if kobject_uevent() is completed with success or the
241 * corresponding error when it fails.
243 int kobject_uevent(struct kobject *kobj, enum kobject_action action)
245 return kobject_uevent_env(kobj, action, NULL);
248 EXPORT_SYMBOL_GPL(kobject_uevent);
251 * add_uevent_var - helper for creating event variables
252 * @envp: Pointer to table of environment variables, as passed into
253 * uevent() method.
254 * @num_envp: Number of environment variable slots available, as
255 * passed into uevent() method.
256 * @cur_index: Pointer to current index into @envp. It should be
257 * initialized to 0 before the first call to add_uevent_var(),
258 * and will be incremented on success.
259 * @buffer: Pointer to buffer for environment variables, as passed
260 * into uevent() method.
261 * @buffer_size: Length of @buffer, as passed into uevent() method.
262 * @cur_len: Pointer to current length of space used in @buffer.
263 * Should be initialized to 0 before the first call to
264 * add_uevent_var(), and will be incremented on success.
265 * @format: Format for creating environment variable (of the form
266 * "XXX=%x") for snprintf().
268 * Returns 0 if environment variable was added successfully or -ENOMEM
269 * if no space was available.
271 int add_uevent_var(char **envp, int num_envp, int *cur_index,
272 char *buffer, int buffer_size, int *cur_len,
273 const char *format, ...)
275 va_list args;
278 * We check against num_envp - 1 to make sure there is at
279 * least one slot left after we return, since kobject_uevent()
280 * needs to set the last slot to NULL.
282 if (*cur_index >= num_envp - 1)
283 return -ENOMEM;
285 envp[*cur_index] = buffer + *cur_len;
287 va_start(args, format);
288 *cur_len += vsnprintf(envp[*cur_index],
289 max(buffer_size - *cur_len, 0),
290 format, args) + 1;
291 va_end(args);
293 if (*cur_len > buffer_size)
294 return -ENOMEM;
296 (*cur_index)++;
297 return 0;
299 EXPORT_SYMBOL_GPL(add_uevent_var);
301 #if defined(CONFIG_NET)
302 static int __init kobject_uevent_init(void)
304 uevent_sock = netlink_kernel_create(NETLINK_KOBJECT_UEVENT, 1, NULL,
305 NULL, THIS_MODULE);
307 if (!uevent_sock) {
308 printk(KERN_ERR
309 "kobject_uevent: unable to create netlink socket!\n");
310 return -ENODEV;
313 return 0;
316 postcore_initcall(kobject_uevent_init);
317 #endif
319 #endif /* CONFIG_HOTPLUG */