Driver core: add CONFIG_UEVENT_HELPER_PATH
[safe/jmp/linux-2.6] / lib / kobject_uevent.c
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>
22 #include <linux/kobject.h>
23 #include <net/sock.h>
24
25
26 /* the strings here must match the enum in include/linux/kobject.h */
27 const char *kobject_actions[] = {
28         "add",
29         "remove",
30         "change",
31         "move",
32         "online",
33         "offline",
34 };
35
36 #if defined(CONFIG_HOTPLUG)
37 u64 uevent_seqnum;
38 char uevent_helper[UEVENT_HELPER_PATH_LEN] = CONFIG_UEVENT_HELPER_PATH;
39 static DEFINE_SPINLOCK(sequence_lock);
40 #if defined(CONFIG_NET)
41 static struct sock *uevent_sock;
42 #endif
43
44 /**
45  * kobject_uevent_env - send an uevent with environmental data
46  *
47  * @action: action that is happening (usually KOBJ_MOVE)
48  * @kobj: struct kobject that the action is happening to
49  * @envp_ext: pointer to environmental data
50  *
51  * Returns 0 if kobject_uevent() is completed with success or the
52  * corresponding error when it fails.
53  */
54 int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
55                        char *envp_ext[])
56 {
57         struct kobj_uevent_env *env;
58         const char *action_string = kobject_actions[action];
59         const char *devpath = NULL;
60         const char *subsystem;
61         struct kobject *top_kobj;
62         struct kset *kset;
63         struct kset_uevent_ops *uevent_ops;
64         u64 seq;
65         int i = 0;
66         int retval = 0;
67
68         pr_debug("%s\n", __FUNCTION__);
69
70         /* search the kset we belong to */
71         top_kobj = kobj;
72         while (!top_kobj->kset && top_kobj->parent) {
73                 top_kobj = top_kobj->parent;
74         }
75         if (!top_kobj->kset) {
76                 pr_debug("kobject attempted to send uevent without kset!\n");
77                 return -EINVAL;
78         }
79
80         kset = top_kobj->kset;
81         uevent_ops = kset->uevent_ops;
82
83         /* skip the event, if the filter returns zero. */
84         if (uevent_ops && uevent_ops->filter)
85                 if (!uevent_ops->filter(kset, kobj)) {
86                         pr_debug("kobject filter function caused the event to drop!\n");
87                         return 0;
88                 }
89
90         /* originating subsystem */
91         if (uevent_ops && uevent_ops->name)
92                 subsystem = uevent_ops->name(kset, kobj);
93         else
94                 subsystem = kobject_name(&kset->kobj);
95         if (!subsystem) {
96                 pr_debug("unset subsytem caused the event to drop!\n");
97                 return 0;
98         }
99
100         /* environment buffer */
101         env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
102         if (!env)
103                 return -ENOMEM;
104
105         /* complete object path */
106         devpath = kobject_get_path(kobj, GFP_KERNEL);
107         if (!devpath) {
108                 retval = -ENOENT;
109                 goto exit;
110         }
111
112         /* default keys */
113         retval = add_uevent_var(env, "ACTION=%s", action_string);
114         if (retval)
115                 goto exit;
116         retval = add_uevent_var(env, "DEVPATH=%s", devpath);
117         if (retval)
118                 goto exit;
119         retval = add_uevent_var(env, "SUBSYSTEM=%s", subsystem);
120         if (retval)
121                 goto exit;
122
123         /* keys passed in from the caller */
124         if (envp_ext) {
125                 for (i = 0; envp_ext[i]; i++) {
126                         retval = add_uevent_var(env, envp_ext[i]);
127                         if (retval)
128                                 goto exit;
129                 }
130         }
131
132         /* let the kset specific function add its stuff */
133         if (uevent_ops && uevent_ops->uevent) {
134                 retval = uevent_ops->uevent(kset, kobj, env);
135                 if (retval) {
136                         pr_debug ("%s - uevent() returned %d\n",
137                                   __FUNCTION__, retval);
138                         goto exit;
139                 }
140         }
141
142         /* we will send an event, so request a new sequence number */
143         spin_lock(&sequence_lock);
144         seq = ++uevent_seqnum;
145         spin_unlock(&sequence_lock);
146         retval = add_uevent_var(env, "SEQNUM=%llu", (unsigned long long)seq);
147         if (retval)
148                 goto exit;
149
150 #if defined(CONFIG_NET)
151         /* send netlink message */
152         if (uevent_sock) {
153                 struct sk_buff *skb;
154                 size_t len;
155
156                 /* allocate message with the maximum possible size */
157                 len = strlen(action_string) + strlen(devpath) + 2;
158                 skb = alloc_skb(len + env->buflen, GFP_KERNEL);
159                 if (skb) {
160                         char *scratch;
161
162                         /* add header */
163                         scratch = skb_put(skb, len);
164                         sprintf(scratch, "%s@%s", action_string, devpath);
165
166                         /* copy keys to our continuous event payload buffer */
167                         for (i = 0; i < env->envp_idx; i++) {
168                                 len = strlen(env->envp[i]) + 1;
169                                 scratch = skb_put(skb, len);
170                                 strcpy(scratch, env->envp[i]);
171                         }
172
173                         NETLINK_CB(skb).dst_group = 1;
174                         netlink_broadcast(uevent_sock, skb, 0, 1, GFP_KERNEL);
175                 }
176         }
177 #endif
178
179         /* call uevent_helper, usually only enabled during early boot */
180         if (uevent_helper[0]) {
181                 char *argv [3];
182
183                 argv [0] = uevent_helper;
184                 argv [1] = (char *)subsystem;
185                 argv [2] = NULL;
186                 retval = add_uevent_var(env, "HOME=/");
187                 if (retval)
188                         goto exit;
189                 retval = add_uevent_var(env, "PATH=/sbin:/bin:/usr/sbin:/usr/bin");
190                 if (retval)
191                         goto exit;
192
193                 call_usermodehelper (argv[0], argv, env->envp, UMH_WAIT_EXEC);
194         }
195
196 exit:
197         kfree(devpath);
198         kfree(env);
199         return retval;
200 }
201
202 EXPORT_SYMBOL_GPL(kobject_uevent_env);
203
204 /**
205  * kobject_uevent - notify userspace by ending an uevent
206  *
207  * @action: action that is happening (usually KOBJ_ADD and KOBJ_REMOVE)
208  * @kobj: struct kobject that the action is happening to
209  *
210  * Returns 0 if kobject_uevent() is completed with success or the
211  * corresponding error when it fails.
212  */
213 int kobject_uevent(struct kobject *kobj, enum kobject_action action)
214 {
215         return kobject_uevent_env(kobj, action, NULL);
216 }
217
218 EXPORT_SYMBOL_GPL(kobject_uevent);
219
220 /**
221  * add_uevent_var - add key value string to the environment buffer
222  * @env: environment buffer structure
223  * @format: printf format for the key=value pair
224  *
225  * Returns 0 if environment variable was added successfully or -ENOMEM
226  * if no space was available.
227  */
228 int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...)
229 {
230         va_list args;
231         int len;
232
233         if (env->envp_idx >= ARRAY_SIZE(env->envp)) {
234                 printk(KERN_ERR "add_uevent_var: too many keys\n");
235                 WARN_ON(1);
236                 return -ENOMEM;
237         }
238
239         va_start(args, format);
240         len = vsnprintf(&env->buf[env->buflen],
241                         sizeof(env->buf) - env->buflen,
242                         format, args);
243         va_end(args);
244
245         if (len >= (sizeof(env->buf) - env->buflen)) {
246                 printk(KERN_ERR "add_uevent_var: buffer size too small\n");
247                 WARN_ON(1);
248                 return -ENOMEM;
249         }
250
251         env->envp[env->envp_idx++] = &env->buf[env->buflen];
252         env->buflen += len + 1;
253         return 0;
254 }
255 EXPORT_SYMBOL_GPL(add_uevent_var);
256
257 #if defined(CONFIG_NET)
258 static int __init kobject_uevent_init(void)
259 {
260         uevent_sock = netlink_kernel_create(&init_net, NETLINK_KOBJECT_UEVENT,
261                                             1, NULL, NULL, THIS_MODULE);
262         if (!uevent_sock) {
263                 printk(KERN_ERR
264                        "kobject_uevent: unable to create netlink socket!\n");
265                 return -ENODEV;
266         }
267
268         return 0;
269 }
270
271 postcore_initcall(kobject_uevent_init);
272 #endif
273
274 #endif /* CONFIG_HOTPLUG */