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