[PATCH] DVB: frontend updates
[linux-2.6/history.git] / lib / kobject_uevent.c
bloba5ea7709667018014f4e8ff7dc3b28af07515f8e
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_uevent.h>
23 #include <linux/kobject.h>
24 #include <net/sock.h>
26 static char *action_to_string(enum kobject_action action)
28 switch (action) {
29 case KOBJ_ADD:
30 return "add";
31 case KOBJ_REMOVE:
32 return "remove";
33 case KOBJ_CHANGE:
34 return "change";
35 case KOBJ_MOUNT:
36 return "mount";
37 case KOBJ_UMOUNT:
38 return "umount";
39 default:
40 return NULL;
44 #ifdef CONFIG_KOBJECT_UEVENT
45 static struct sock *uevent_sock;
47 /**
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
53 * @buflen:
54 * gfp_mask:
56 static int send_uevent(const char *signal, const char *obj, const void *buf,
57 int buflen, int gfp_mask)
59 struct sk_buff *skb;
60 char *pos;
61 int len;
63 if (!uevent_sock)
64 return -EIO;
66 len = strlen(signal) + 1;
67 len += strlen(obj) + 1;
68 len += buflen;
70 skb = alloc_skb(len, gfp_mask);
71 if (!skb)
72 return -ENOMEM;
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)
85 char *path;
86 char *attrpath;
87 char *signal;
88 int len;
89 int rc = -ENOMEM;
91 path = kobject_get_path(kobj, gfp_mask);
92 if (!path)
93 return -ENOMEM;
95 signal = action_to_string(action);
96 if (!signal)
97 return -EINVAL;
99 if (attr) {
100 len = strlen(path);
101 len += strlen(attr->name) + 2;
102 attrpath = kmalloc(len, gfp_mask);
103 if (!attrpath)
104 goto exit;
105 sprintf(attrpath, "%s/%s", path, attr->name);
106 rc = send_uevent(signal, attrpath, NULL, 0, gfp_mask);
107 kfree(attrpath);
108 } else {
109 rc = send_uevent(signal, path, NULL, 0, gfp_mask);
112 exit:
113 kfree(path);
114 return rc;
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);
143 if (!uevent_sock) {
144 printk(KERN_ERR
145 "kobject_uevent: unable to create netlink socket!\n");
146 return -ENODEV;
149 return 0;
152 core_initcall(kobject_uevent_init);
154 #else
155 static inline int send_uevent(const char *signal, const char *obj,
156 const void *buf, int buflen, int gfp_mask)
158 return 0;
161 #endif /* CONFIG_KOBJECT_UEVENT */
164 #ifdef CONFIG_HOTPLUG
165 u64 hotplug_seqnum;
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)
178 char *argv [3];
179 char **envp = NULL;
180 char *buffer = NULL;
181 char *scratch;
182 int i = 0;
183 int retval;
184 char *kobj_path = NULL;
185 char *name = NULL;
186 char *action_string;
187 u64 seq;
188 struct kobject *top_kobj = kobj;
189 struct kset *kset;
191 if (!top_kobj->kset && top_kobj->parent) {
192 do {
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;
199 else
200 return;
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))
206 return;
209 pr_debug ("%s\n", __FUNCTION__);
211 action_string = action_to_string(action);
212 if (!action_string)
213 return;
215 envp = kmalloc(NUM_ENVP * sizeof (char *), GFP_KERNEL);
216 if (!envp)
217 return;
218 memset (envp, 0x00, NUM_ENVP * sizeof (char *));
220 buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
221 if (!buffer)
222 goto exit;
224 if (kset->hotplug_ops->name)
225 name = kset->hotplug_ops->name(kset, kobj);
226 if (name == NULL)
227 name = kset->kobj.name;
229 argv [0] = hotplug_path;
230 argv [1] = name;
231 argv [2] = NULL;
233 /* minimal command environment */
234 envp [i++] = "HOME=/";
235 envp [i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
237 scratch = buffer;
239 envp [i++] = scratch;
240 scratch += sprintf(scratch, "ACTION=%s", action_string) + 1;
242 kobj_path = kobject_get_path(kobj, GFP_KERNEL);
243 if (!kobj_path)
244 goto exit;
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));
264 if (retval) {
265 pr_debug ("%s - hotplug() returned %d\n",
266 __FUNCTION__, retval);
267 goto exit;
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])
277 goto exit;
279 retval = call_usermodehelper (argv[0], argv, envp, 0);
280 if (retval)
281 pr_debug ("%s - call_usermodehelper returned %d\n",
282 __FUNCTION__, retval);
284 exit:
285 kfree(kobj_path);
286 kfree(buffer);
287 kfree(envp);
288 return;
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
295 * hotplug() method.
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, ...)
317 va_list args;
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)
325 return -ENOMEM;
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),
332 format, args) + 1;
333 va_end(args);
335 if (*cur_len > buffer_size)
336 return -ENOMEM;
338 (*cur_index)++;
339 return 0;
341 EXPORT_SYMBOL(add_hotplug_env_var);
343 #endif /* CONFIG_HOTPLUG */