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