]>
Commit | Line | Data |
---|---|---|
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 | ||
1da177e4 | 25 | |
60a96a59 KS |
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 | }; | |
1da177e4 | 35 | |
cd030c4c CH |
36 | #if defined(CONFIG_HOTPLUG) |
37 | u64 uevent_seqnum; | |
38 | char uevent_helper[UEVENT_HELPER_PATH_LEN] = "/sbin/hotplug"; | |
39 | static DEFINE_SPINLOCK(sequence_lock); | |
40 | #if defined(CONFIG_NET) | |
41 | static struct sock *uevent_sock; | |
42 | #endif | |
43 | ||
1da177e4 | 44 | /** |
8a82472f | 45 | * kobject_uevent_env - send an uevent with environmental data |
1da177e4 | 46 | * |
8a82472f | 47 | * @action: action that is happening (usually KOBJ_MOVE) |
1da177e4 | 48 | * @kobj: struct kobject that the action is happening to |
8a82472f | 49 | * @envp_ext: pointer to environmental data |
542cfce6 AK |
50 | * |
51 | * Returns 0 if kobject_uevent() is completed with success or the | |
52 | * corresponding error when it fails. | |
1da177e4 | 53 | */ |
542cfce6 | 54 | int kobject_uevent_env(struct kobject *kobj, enum kobject_action action, |
7eff2e7a | 55 | char *envp_ext[]) |
1da177e4 | 56 | { |
7eff2e7a KS |
57 | struct kobj_uevent_env *env; |
58 | const char *action_string = kobject_actions[action]; | |
5f123fbd KS |
59 | const char *devpath = NULL; |
60 | const char *subsystem; | |
61 | struct kobject *top_kobj; | |
62 | struct kset *kset; | |
312c004d | 63 | struct kset_uevent_ops *uevent_ops; |
5f123fbd | 64 | u64 seq; |
1da177e4 | 65 | int i = 0; |
542cfce6 | 66 | int retval = 0; |
1da177e4 | 67 | |
5f123fbd KS |
68 | pr_debug("%s\n", __FUNCTION__); |
69 | ||
5f123fbd KS |
70 | /* search the kset we belong to */ |
71 | top_kobj = kobj; | |
14193fb9 JAKJ |
72 | while (!top_kobj->kset && top_kobj->parent) { |
73 | top_kobj = top_kobj->parent; | |
1da177e4 | 74 | } |
542cfce6 AK |
75 | if (!top_kobj->kset) { |
76 | pr_debug("kobject attempted to send uevent without kset!\n"); | |
77 | return -EINVAL; | |
78 | } | |
1da177e4 | 79 | |
5f123fbd | 80 | kset = top_kobj->kset; |
312c004d | 81 | uevent_ops = kset->uevent_ops; |
1da177e4 | 82 | |
7eff2e7a | 83 | /* skip the event, if the filter returns zero. */ |
312c004d | 84 | if (uevent_ops && uevent_ops->filter) |
542cfce6 AK |
85 | if (!uevent_ops->filter(kset, kobj)) { |
86 | pr_debug("kobject filter function caused the event to drop!\n"); | |
87 | return 0; | |
88 | } | |
1da177e4 | 89 | |
86406245 KS |
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 | ||
7eff2e7a KS |
100 | /* environment buffer */ |
101 | env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL); | |
102 | if (!env) | |
542cfce6 | 103 | return -ENOMEM; |
1da177e4 | 104 | |
5f123fbd KS |
105 | /* complete object path */ |
106 | devpath = kobject_get_path(kobj, GFP_KERNEL); | |
542cfce6 AK |
107 | if (!devpath) { |
108 | retval = -ENOENT; | |
5f123fbd | 109 | goto exit; |
542cfce6 | 110 | } |
1da177e4 | 111 | |
5f123fbd | 112 | /* default keys */ |
7eff2e7a KS |
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 | } | |
1da177e4 | 131 | |
5f123fbd | 132 | /* let the kset specific function add its stuff */ |
312c004d | 133 | if (uevent_ops && uevent_ops->uevent) { |
7eff2e7a | 134 | retval = uevent_ops->uevent(kset, kobj, env); |
1da177e4 | 135 | if (retval) { |
312c004d | 136 | pr_debug ("%s - uevent() returned %d\n", |
1da177e4 LT |
137 | __FUNCTION__, retval); |
138 | goto exit; | |
139 | } | |
140 | } | |
141 | ||
7eff2e7a | 142 | /* we will send an event, so request a new sequence number */ |
1da177e4 | 143 | spin_lock(&sequence_lock); |
312c004d | 144 | seq = ++uevent_seqnum; |
1da177e4 | 145 | spin_unlock(&sequence_lock); |
7eff2e7a KS |
146 | retval = add_uevent_var(env, "SEQNUM=%llu", (unsigned long long)seq); |
147 | if (retval) | |
148 | goto exit; | |
1da177e4 | 149 | |
4d17ffda | 150 | #if defined(CONFIG_NET) |
5f123fbd KS |
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; | |
7eff2e7a | 158 | skb = alloc_skb(len + env->buflen, GFP_KERNEL); |
5f123fbd | 159 | if (skb) { |
7eff2e7a KS |
160 | char *scratch; |
161 | ||
5f123fbd KS |
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 */ | |
7eff2e7a KS |
167 | for (i = 0; i < env->envp_idx; i++) { |
168 | len = strlen(env->envp[i]) + 1; | |
5f123fbd | 169 | scratch = skb_put(skb, len); |
7eff2e7a | 170 | strcpy(scratch, env->envp[i]); |
5f123fbd KS |
171 | } |
172 | ||
173 | NETLINK_CB(skb).dst_group = 1; | |
174 | netlink_broadcast(uevent_sock, skb, 0, 1, GFP_KERNEL); | |
175 | } | |
176 | } | |
4d17ffda | 177 | #endif |
1da177e4 | 178 | |
5f123fbd | 179 | /* call uevent_helper, usually only enabled during early boot */ |
312c004d | 180 | if (uevent_helper[0]) { |
5f123fbd | 181 | char *argv [3]; |
1da177e4 | 182 | |
312c004d | 183 | argv [0] = uevent_helper; |
5f123fbd KS |
184 | argv [1] = (char *)subsystem; |
185 | argv [2] = NULL; | |
7eff2e7a KS |
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); | |
5f123fbd | 194 | } |
1da177e4 LT |
195 | |
196 | exit: | |
5f123fbd | 197 | kfree(devpath); |
7eff2e7a | 198 | kfree(env); |
542cfce6 | 199 | return retval; |
1da177e4 | 200 | } |
8a82472f CH |
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 | |
542cfce6 AK |
209 | * |
210 | * Returns 0 if kobject_uevent() is completed with success or the | |
211 | * corresponding error when it fails. | |
8a82472f | 212 | */ |
542cfce6 | 213 | int kobject_uevent(struct kobject *kobj, enum kobject_action action) |
8a82472f | 214 | { |
542cfce6 | 215 | return kobject_uevent_env(kobj, action, NULL); |
8a82472f CH |
216 | } |
217 | ||
312c004d | 218 | EXPORT_SYMBOL_GPL(kobject_uevent); |
1da177e4 LT |
219 | |
220 | /** | |
7eff2e7a KS |
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 | |
1da177e4 LT |
224 | * |
225 | * Returns 0 if environment variable was added successfully or -ENOMEM | |
226 | * if no space was available. | |
227 | */ | |
7eff2e7a | 228 | int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...) |
1da177e4 LT |
229 | { |
230 | va_list args; | |
7eff2e7a | 231 | int len; |
1da177e4 | 232 | |
7eff2e7a KS |
233 | if (env->envp_idx >= ARRAY_SIZE(env->envp)) { |
234 | printk(KERN_ERR "add_uevent_var: too many keys\n"); | |
235 | WARN_ON(1); | |
1da177e4 | 236 | return -ENOMEM; |
7eff2e7a | 237 | } |
1da177e4 LT |
238 | |
239 | va_start(args, format); | |
7eff2e7a KS |
240 | len = vsnprintf(&env->buf[env->buflen], |
241 | sizeof(env->buf) - env->buflen, | |
242 | format, args); | |
1da177e4 LT |
243 | va_end(args); |
244 | ||
7eff2e7a KS |
245 | if (len >= (sizeof(env->buf) - env->buflen)) { |
246 | printk(KERN_ERR "add_uevent_var: buffer size too small\n"); | |
247 | WARN_ON(1); | |
1da177e4 | 248 | return -ENOMEM; |
7eff2e7a | 249 | } |
1da177e4 | 250 | |
7eff2e7a KS |
251 | env->envp[env->envp_idx++] = &env->buf[env->buflen]; |
252 | env->buflen += len + 1; | |
1da177e4 LT |
253 | return 0; |
254 | } | |
312c004d | 255 | EXPORT_SYMBOL_GPL(add_uevent_var); |
1da177e4 | 256 | |
4d17ffda | 257 | #if defined(CONFIG_NET) |
5f123fbd KS |
258 | static int __init kobject_uevent_init(void) |
259 | { | |
b4b51029 EB |
260 | uevent_sock = netlink_kernel_create(&init_net, NETLINK_KOBJECT_UEVENT, |
261 | 1, NULL, NULL, THIS_MODULE); | |
5f123fbd KS |
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); | |
4d17ffda | 272 | #endif |
5f123fbd | 273 | |
1da177e4 | 274 | #endif /* CONFIG_HOTPLUG */ |