]>
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> | |
2d38f9a4 DL |
18 | #include <linux/string.h> |
19 | #include <linux/kobject.h> | |
20 | #include <linux/module.h> | |
5a0e3ad6 | 21 | #include <linux/slab.h> |
2d38f9a4 | 22 | |
1da177e4 LT |
23 | #include <linux/socket.h> |
24 | #include <linux/skbuff.h> | |
25 | #include <linux/netlink.h> | |
1da177e4 LT |
26 | #include <net/sock.h> |
27 | ||
1da177e4 | 28 | |
cd030c4c | 29 | u64 uevent_seqnum; |
6a8d8abb | 30 | char uevent_helper[UEVENT_HELPER_PATH_LEN] = CONFIG_UEVENT_HELPER_PATH; |
cd030c4c CH |
31 | static DEFINE_SPINLOCK(sequence_lock); |
32 | #if defined(CONFIG_NET) | |
33 | static struct sock *uevent_sock; | |
34 | #endif | |
35 | ||
5c5daf65 KS |
36 | /* the strings here must match the enum in include/linux/kobject.h */ |
37 | static const char *kobject_actions[] = { | |
38 | [KOBJ_ADD] = "add", | |
39 | [KOBJ_REMOVE] = "remove", | |
40 | [KOBJ_CHANGE] = "change", | |
41 | [KOBJ_MOVE] = "move", | |
42 | [KOBJ_ONLINE] = "online", | |
43 | [KOBJ_OFFLINE] = "offline", | |
44 | }; | |
45 | ||
46 | /** | |
47 | * kobject_action_type - translate action string to numeric type | |
48 | * | |
49 | * @buf: buffer containing the action string, newline is ignored | |
50 | * @len: length of buffer | |
51 | * @type: pointer to the location to store the action type | |
52 | * | |
53 | * Returns 0 if the action string was recognized. | |
54 | */ | |
55 | int kobject_action_type(const char *buf, size_t count, | |
56 | enum kobject_action *type) | |
57 | { | |
58 | enum kobject_action action; | |
59 | int ret = -EINVAL; | |
60 | ||
a9edadbf | 61 | if (count && (buf[count-1] == '\n' || buf[count-1] == '\0')) |
5c5daf65 KS |
62 | count--; |
63 | ||
64 | if (!count) | |
65 | goto out; | |
66 | ||
67 | for (action = 0; action < ARRAY_SIZE(kobject_actions); action++) { | |
68 | if (strncmp(kobject_actions[action], buf, count) != 0) | |
69 | continue; | |
70 | if (kobject_actions[action][count] != '\0') | |
71 | continue; | |
72 | *type = action; | |
73 | ret = 0; | |
74 | break; | |
75 | } | |
76 | out: | |
77 | return ret; | |
78 | } | |
79 | ||
1da177e4 | 80 | /** |
8a82472f | 81 | * kobject_uevent_env - send an uevent with environmental data |
1da177e4 | 82 | * |
ccd490a3 | 83 | * @action: action that is happening |
1da177e4 | 84 | * @kobj: struct kobject that the action is happening to |
8a82472f | 85 | * @envp_ext: pointer to environmental data |
542cfce6 AK |
86 | * |
87 | * Returns 0 if kobject_uevent() is completed with success or the | |
88 | * corresponding error when it fails. | |
1da177e4 | 89 | */ |
542cfce6 | 90 | int kobject_uevent_env(struct kobject *kobj, enum kobject_action action, |
7eff2e7a | 91 | char *envp_ext[]) |
1da177e4 | 92 | { |
7eff2e7a KS |
93 | struct kobj_uevent_env *env; |
94 | const char *action_string = kobject_actions[action]; | |
5f123fbd KS |
95 | const char *devpath = NULL; |
96 | const char *subsystem; | |
97 | struct kobject *top_kobj; | |
98 | struct kset *kset; | |
9cd43611 | 99 | const struct kset_uevent_ops *uevent_ops; |
5f123fbd | 100 | u64 seq; |
1da177e4 | 101 | int i = 0; |
542cfce6 | 102 | int retval = 0; |
1da177e4 | 103 | |
9f66fa2a | 104 | pr_debug("kobject: '%s' (%p): %s\n", |
810304db | 105 | kobject_name(kobj), kobj, __func__); |
5f123fbd | 106 | |
5f123fbd KS |
107 | /* search the kset we belong to */ |
108 | top_kobj = kobj; | |
ccd490a3 | 109 | while (!top_kobj->kset && top_kobj->parent) |
14193fb9 | 110 | top_kobj = top_kobj->parent; |
ccd490a3 | 111 | |
542cfce6 | 112 | if (!top_kobj->kset) { |
9f66fa2a GKH |
113 | pr_debug("kobject: '%s' (%p): %s: attempted to send uevent " |
114 | "without kset!\n", kobject_name(kobj), kobj, | |
810304db | 115 | __func__); |
542cfce6 AK |
116 | return -EINVAL; |
117 | } | |
1da177e4 | 118 | |
5f123fbd | 119 | kset = top_kobj->kset; |
312c004d | 120 | uevent_ops = kset->uevent_ops; |
1da177e4 | 121 | |
f67f129e ML |
122 | /* skip the event, if uevent_suppress is set*/ |
123 | if (kobj->uevent_suppress) { | |
124 | pr_debug("kobject: '%s' (%p): %s: uevent_suppress " | |
125 | "caused the event to drop!\n", | |
126 | kobject_name(kobj), kobj, __func__); | |
127 | return 0; | |
128 | } | |
7eff2e7a | 129 | /* skip the event, if the filter returns zero. */ |
312c004d | 130 | if (uevent_ops && uevent_ops->filter) |
542cfce6 | 131 | if (!uevent_ops->filter(kset, kobj)) { |
9f66fa2a GKH |
132 | pr_debug("kobject: '%s' (%p): %s: filter function " |
133 | "caused the event to drop!\n", | |
810304db | 134 | kobject_name(kobj), kobj, __func__); |
542cfce6 AK |
135 | return 0; |
136 | } | |
1da177e4 | 137 | |
86406245 KS |
138 | /* originating subsystem */ |
139 | if (uevent_ops && uevent_ops->name) | |
140 | subsystem = uevent_ops->name(kset, kobj); | |
141 | else | |
142 | subsystem = kobject_name(&kset->kobj); | |
143 | if (!subsystem) { | |
9f66fa2a GKH |
144 | pr_debug("kobject: '%s' (%p): %s: unset subsystem caused the " |
145 | "event to drop!\n", kobject_name(kobj), kobj, | |
810304db | 146 | __func__); |
86406245 KS |
147 | return 0; |
148 | } | |
149 | ||
7eff2e7a KS |
150 | /* environment buffer */ |
151 | env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL); | |
152 | if (!env) | |
542cfce6 | 153 | return -ENOMEM; |
1da177e4 | 154 | |
5f123fbd KS |
155 | /* complete object path */ |
156 | devpath = kobject_get_path(kobj, GFP_KERNEL); | |
542cfce6 AK |
157 | if (!devpath) { |
158 | retval = -ENOENT; | |
5f123fbd | 159 | goto exit; |
542cfce6 | 160 | } |
1da177e4 | 161 | |
5f123fbd | 162 | /* default keys */ |
7eff2e7a KS |
163 | retval = add_uevent_var(env, "ACTION=%s", action_string); |
164 | if (retval) | |
165 | goto exit; | |
166 | retval = add_uevent_var(env, "DEVPATH=%s", devpath); | |
167 | if (retval) | |
168 | goto exit; | |
169 | retval = add_uevent_var(env, "SUBSYSTEM=%s", subsystem); | |
170 | if (retval) | |
171 | goto exit; | |
172 | ||
173 | /* keys passed in from the caller */ | |
174 | if (envp_ext) { | |
175 | for (i = 0; envp_ext[i]; i++) { | |
c65b9145 | 176 | retval = add_uevent_var(env, "%s", envp_ext[i]); |
7eff2e7a KS |
177 | if (retval) |
178 | goto exit; | |
179 | } | |
180 | } | |
1da177e4 | 181 | |
5f123fbd | 182 | /* let the kset specific function add its stuff */ |
312c004d | 183 | if (uevent_ops && uevent_ops->uevent) { |
7eff2e7a | 184 | retval = uevent_ops->uevent(kset, kobj, env); |
1da177e4 | 185 | if (retval) { |
9f66fa2a GKH |
186 | pr_debug("kobject: '%s' (%p): %s: uevent() returned " |
187 | "%d\n", kobject_name(kobj), kobj, | |
810304db | 188 | __func__, retval); |
1da177e4 LT |
189 | goto exit; |
190 | } | |
191 | } | |
192 | ||
0f4dafc0 KS |
193 | /* |
194 | * Mark "add" and "remove" events in the object to ensure proper | |
195 | * events to userspace during automatic cleanup. If the object did | |
196 | * send an "add" event, "remove" will automatically generated by | |
197 | * the core, if not already done by the caller. | |
198 | */ | |
199 | if (action == KOBJ_ADD) | |
200 | kobj->state_add_uevent_sent = 1; | |
201 | else if (action == KOBJ_REMOVE) | |
202 | kobj->state_remove_uevent_sent = 1; | |
203 | ||
7eff2e7a | 204 | /* we will send an event, so request a new sequence number */ |
1da177e4 | 205 | spin_lock(&sequence_lock); |
312c004d | 206 | seq = ++uevent_seqnum; |
1da177e4 | 207 | spin_unlock(&sequence_lock); |
7eff2e7a KS |
208 | retval = add_uevent_var(env, "SEQNUM=%llu", (unsigned long long)seq); |
209 | if (retval) | |
210 | goto exit; | |
1da177e4 | 211 | |
4d17ffda | 212 | #if defined(CONFIG_NET) |
5f123fbd KS |
213 | /* send netlink message */ |
214 | if (uevent_sock) { | |
215 | struct sk_buff *skb; | |
216 | size_t len; | |
217 | ||
218 | /* allocate message with the maximum possible size */ | |
219 | len = strlen(action_string) + strlen(devpath) + 2; | |
7eff2e7a | 220 | skb = alloc_skb(len + env->buflen, GFP_KERNEL); |
5f123fbd | 221 | if (skb) { |
7eff2e7a KS |
222 | char *scratch; |
223 | ||
5f123fbd KS |
224 | /* add header */ |
225 | scratch = skb_put(skb, len); | |
226 | sprintf(scratch, "%s@%s", action_string, devpath); | |
227 | ||
228 | /* copy keys to our continuous event payload buffer */ | |
7eff2e7a KS |
229 | for (i = 0; i < env->envp_idx; i++) { |
230 | len = strlen(env->envp[i]) + 1; | |
5f123fbd | 231 | scratch = skb_put(skb, len); |
7eff2e7a | 232 | strcpy(scratch, env->envp[i]); |
5f123fbd KS |
233 | } |
234 | ||
235 | NETLINK_CB(skb).dst_group = 1; | |
e0d7bf5d ML |
236 | retval = netlink_broadcast(uevent_sock, skb, 0, 1, |
237 | GFP_KERNEL); | |
ff491a73 PNA |
238 | /* ENOBUFS should be handled in userspace */ |
239 | if (retval == -ENOBUFS) | |
240 | retval = 0; | |
e0d7bf5d ML |
241 | } else |
242 | retval = -ENOMEM; | |
5f123fbd | 243 | } |
4d17ffda | 244 | #endif |
1da177e4 | 245 | |
5f123fbd | 246 | /* call uevent_helper, usually only enabled during early boot */ |
312c004d | 247 | if (uevent_helper[0]) { |
5f123fbd | 248 | char *argv [3]; |
1da177e4 | 249 | |
312c004d | 250 | argv [0] = uevent_helper; |
5f123fbd KS |
251 | argv [1] = (char *)subsystem; |
252 | argv [2] = NULL; | |
7eff2e7a KS |
253 | retval = add_uevent_var(env, "HOME=/"); |
254 | if (retval) | |
255 | goto exit; | |
e374a2bf GKH |
256 | retval = add_uevent_var(env, |
257 | "PATH=/sbin:/bin:/usr/sbin:/usr/bin"); | |
7eff2e7a KS |
258 | if (retval) |
259 | goto exit; | |
260 | ||
0ad1d6f3 | 261 | retval = call_usermodehelper(argv[0], argv, |
05f54c13 | 262 | env->envp, UMH_WAIT_EXEC); |
5f123fbd | 263 | } |
1da177e4 LT |
264 | |
265 | exit: | |
5f123fbd | 266 | kfree(devpath); |
7eff2e7a | 267 | kfree(env); |
542cfce6 | 268 | return retval; |
1da177e4 | 269 | } |
8a82472f CH |
270 | EXPORT_SYMBOL_GPL(kobject_uevent_env); |
271 | ||
272 | /** | |
273 | * kobject_uevent - notify userspace by ending an uevent | |
274 | * | |
ccd490a3 | 275 | * @action: action that is happening |
8a82472f | 276 | * @kobj: struct kobject that the action is happening to |
542cfce6 AK |
277 | * |
278 | * Returns 0 if kobject_uevent() is completed with success or the | |
279 | * corresponding error when it fails. | |
8a82472f | 280 | */ |
542cfce6 | 281 | int kobject_uevent(struct kobject *kobj, enum kobject_action action) |
8a82472f | 282 | { |
542cfce6 | 283 | return kobject_uevent_env(kobj, action, NULL); |
8a82472f | 284 | } |
312c004d | 285 | EXPORT_SYMBOL_GPL(kobject_uevent); |
1da177e4 LT |
286 | |
287 | /** | |
7eff2e7a KS |
288 | * add_uevent_var - add key value string to the environment buffer |
289 | * @env: environment buffer structure | |
290 | * @format: printf format for the key=value pair | |
1da177e4 LT |
291 | * |
292 | * Returns 0 if environment variable was added successfully or -ENOMEM | |
293 | * if no space was available. | |
294 | */ | |
7eff2e7a | 295 | int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...) |
1da177e4 LT |
296 | { |
297 | va_list args; | |
7eff2e7a | 298 | int len; |
1da177e4 | 299 | |
7eff2e7a | 300 | if (env->envp_idx >= ARRAY_SIZE(env->envp)) { |
5cd2b459 | 301 | WARN(1, KERN_ERR "add_uevent_var: too many keys\n"); |
1da177e4 | 302 | return -ENOMEM; |
7eff2e7a | 303 | } |
1da177e4 LT |
304 | |
305 | va_start(args, format); | |
7eff2e7a KS |
306 | len = vsnprintf(&env->buf[env->buflen], |
307 | sizeof(env->buf) - env->buflen, | |
308 | format, args); | |
1da177e4 LT |
309 | va_end(args); |
310 | ||
7eff2e7a | 311 | if (len >= (sizeof(env->buf) - env->buflen)) { |
5cd2b459 | 312 | WARN(1, KERN_ERR "add_uevent_var: buffer size too small\n"); |
1da177e4 | 313 | return -ENOMEM; |
7eff2e7a | 314 | } |
1da177e4 | 315 | |
7eff2e7a KS |
316 | env->envp[env->envp_idx++] = &env->buf[env->buflen]; |
317 | env->buflen += len + 1; | |
1da177e4 LT |
318 | return 0; |
319 | } | |
312c004d | 320 | EXPORT_SYMBOL_GPL(add_uevent_var); |
1da177e4 | 321 | |
4d17ffda | 322 | #if defined(CONFIG_NET) |
5f123fbd KS |
323 | static int __init kobject_uevent_init(void) |
324 | { | |
b4b51029 EB |
325 | uevent_sock = netlink_kernel_create(&init_net, NETLINK_KOBJECT_UEVENT, |
326 | 1, NULL, NULL, THIS_MODULE); | |
5f123fbd KS |
327 | if (!uevent_sock) { |
328 | printk(KERN_ERR | |
329 | "kobject_uevent: unable to create netlink socket!\n"); | |
330 | return -ENODEV; | |
331 | } | |
d094cbe9 | 332 | netlink_set_nonroot(NETLINK_KOBJECT_UEVENT, NL_NONROOT_RECV); |
5f123fbd KS |
333 | return 0; |
334 | } | |
335 | ||
336 | postcore_initcall(kobject_uevent_init); | |
4d17ffda | 337 | #endif |