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