Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / lib / kobject_uevent.c
blob9fe6ec8fda28ea732d41cad83fd639a82a41a69c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * kernel userspace event delivery
5 * Copyright (C) 2004 Red Hat, Inc. All rights reserved.
6 * Copyright (C) 2004 Novell, Inc. All rights reserved.
7 * Copyright (C) 2004 IBM, Inc. All rights reserved.
9 * Authors:
10 * Robert Love <rml@novell.com>
11 * Kay Sievers <kay.sievers@vrfy.org>
12 * Arjan van de Ven <arjanv@redhat.com>
13 * Greg Kroah-Hartman <greg@kroah.com>
16 #include <linux/spinlock.h>
17 #include <linux/string.h>
18 #include <linux/kobject.h>
19 #include <linux/export.h>
20 #include <linux/kmod.h>
21 #include <linux/slab.h>
22 #include <linux/socket.h>
23 #include <linux/skbuff.h>
24 #include <linux/netlink.h>
25 #include <linux/uuid.h>
26 #include <linux/ctype.h>
27 #include <net/sock.h>
28 #include <net/net_namespace.h>
31 u64 uevent_seqnum;
32 #ifdef CONFIG_UEVENT_HELPER
33 char uevent_helper[UEVENT_HELPER_PATH_LEN] = CONFIG_UEVENT_HELPER_PATH;
34 #endif
35 #ifdef CONFIG_NET
36 struct uevent_sock {
37 struct list_head list;
38 struct sock *sk;
40 static LIST_HEAD(uevent_sock_list);
41 #endif
43 /* This lock protects uevent_seqnum and uevent_sock_list */
44 static DEFINE_MUTEX(uevent_sock_mutex);
46 /* the strings here must match the enum in include/linux/kobject.h */
47 static const char *kobject_actions[] = {
48 [KOBJ_ADD] = "add",
49 [KOBJ_REMOVE] = "remove",
50 [KOBJ_CHANGE] = "change",
51 [KOBJ_MOVE] = "move",
52 [KOBJ_ONLINE] = "online",
53 [KOBJ_OFFLINE] = "offline",
54 [KOBJ_BIND] = "bind",
55 [KOBJ_UNBIND] = "unbind",
58 static int kobject_action_type(const char *buf, size_t count,
59 enum kobject_action *type,
60 const char **args)
62 enum kobject_action action;
63 size_t count_first;
64 const char *args_start;
65 int ret = -EINVAL;
67 if (count && (buf[count-1] == '\n' || buf[count-1] == '\0'))
68 count--;
70 if (!count)
71 goto out;
73 args_start = strnchr(buf, count, ' ');
74 if (args_start) {
75 count_first = args_start - buf;
76 args_start = args_start + 1;
77 } else
78 count_first = count;
80 for (action = 0; action < ARRAY_SIZE(kobject_actions); action++) {
81 if (strncmp(kobject_actions[action], buf, count_first) != 0)
82 continue;
83 if (kobject_actions[action][count_first] != '\0')
84 continue;
85 if (args)
86 *args = args_start;
87 *type = action;
88 ret = 0;
89 break;
91 out:
92 return ret;
95 static const char *action_arg_word_end(const char *buf, const char *buf_end,
96 char delim)
98 const char *next = buf;
100 while (next <= buf_end && *next != delim)
101 if (!isalnum(*next++))
102 return NULL;
104 if (next == buf)
105 return NULL;
107 return next;
110 static int kobject_action_args(const char *buf, size_t count,
111 struct kobj_uevent_env **ret_env)
113 struct kobj_uevent_env *env = NULL;
114 const char *next, *buf_end, *key;
115 int key_len;
116 int r = -EINVAL;
118 if (count && (buf[count - 1] == '\n' || buf[count - 1] == '\0'))
119 count--;
121 if (!count)
122 return -EINVAL;
124 env = kzalloc(sizeof(*env), GFP_KERNEL);
125 if (!env)
126 return -ENOMEM;
128 /* first arg is UUID */
129 if (count < UUID_STRING_LEN || !uuid_is_valid(buf) ||
130 add_uevent_var(env, "SYNTH_UUID=%.*s", UUID_STRING_LEN, buf))
131 goto out;
134 * the rest are custom environment variables in KEY=VALUE
135 * format with ' ' delimiter between each KEY=VALUE pair
137 next = buf + UUID_STRING_LEN;
138 buf_end = buf + count - 1;
140 while (next <= buf_end) {
141 if (*next != ' ')
142 goto out;
144 /* skip the ' ', key must follow */
145 key = ++next;
146 if (key > buf_end)
147 goto out;
149 buf = next;
150 next = action_arg_word_end(buf, buf_end, '=');
151 if (!next || next > buf_end || *next != '=')
152 goto out;
153 key_len = next - buf;
155 /* skip the '=', value must follow */
156 if (++next > buf_end)
157 goto out;
159 buf = next;
160 next = action_arg_word_end(buf, buf_end, ' ');
161 if (!next)
162 goto out;
164 if (add_uevent_var(env, "SYNTH_ARG_%.*s=%.*s",
165 key_len, key, (int) (next - buf), buf))
166 goto out;
169 r = 0;
170 out:
171 if (r)
172 kfree(env);
173 else
174 *ret_env = env;
175 return r;
179 * kobject_synth_uevent - send synthetic uevent with arguments
181 * @kobj: struct kobject for which synthetic uevent is to be generated
182 * @buf: buffer containing action type and action args, newline is ignored
183 * @count: length of buffer
185 * Returns 0 if kobject_synthetic_uevent() is completed with success or the
186 * corresponding error when it fails.
188 int kobject_synth_uevent(struct kobject *kobj, const char *buf, size_t count)
190 char *no_uuid_envp[] = { "SYNTH_UUID=0", NULL };
191 enum kobject_action action;
192 const char *action_args;
193 struct kobj_uevent_env *env;
194 const char *msg = NULL, *devpath;
195 int r;
197 r = kobject_action_type(buf, count, &action, &action_args);
198 if (r) {
199 msg = "unknown uevent action string\n";
200 goto out;
203 if (!action_args) {
204 r = kobject_uevent_env(kobj, action, no_uuid_envp);
205 goto out;
208 r = kobject_action_args(action_args,
209 count - (action_args - buf), &env);
210 if (r == -EINVAL) {
211 msg = "incorrect uevent action arguments\n";
212 goto out;
215 if (r)
216 goto out;
218 r = kobject_uevent_env(kobj, action, env->envp);
219 kfree(env);
220 out:
221 if (r) {
222 devpath = kobject_get_path(kobj, GFP_KERNEL);
223 printk(KERN_WARNING "synth uevent: %s: %s",
224 devpath ?: "unknown device",
225 msg ?: "failed to send uevent");
226 kfree(devpath);
228 return r;
231 #ifdef CONFIG_NET
232 static int kobj_bcast_filter(struct sock *dsk, struct sk_buff *skb, void *data)
234 struct kobject *kobj = data, *ksobj;
235 const struct kobj_ns_type_operations *ops;
237 ops = kobj_ns_ops(kobj);
238 if (!ops && kobj->kset) {
239 ksobj = &kobj->kset->kobj;
240 if (ksobj->parent != NULL)
241 ops = kobj_ns_ops(ksobj->parent);
244 if (ops && ops->netlink_ns && kobj->ktype->namespace) {
245 const void *sock_ns, *ns;
246 ns = kobj->ktype->namespace(kobj);
247 sock_ns = ops->netlink_ns(dsk);
248 return sock_ns != ns;
251 return 0;
253 #endif
255 #ifdef CONFIG_UEVENT_HELPER
256 static int kobj_usermode_filter(struct kobject *kobj)
258 const struct kobj_ns_type_operations *ops;
260 ops = kobj_ns_ops(kobj);
261 if (ops) {
262 const void *init_ns, *ns;
263 ns = kobj->ktype->namespace(kobj);
264 init_ns = ops->initial_ns();
265 return ns != init_ns;
268 return 0;
271 static int init_uevent_argv(struct kobj_uevent_env *env, const char *subsystem)
273 int len;
275 len = strlcpy(&env->buf[env->buflen], subsystem,
276 sizeof(env->buf) - env->buflen);
277 if (len >= (sizeof(env->buf) - env->buflen)) {
278 WARN(1, KERN_ERR "init_uevent_argv: buffer size too small\n");
279 return -ENOMEM;
282 env->argv[0] = uevent_helper;
283 env->argv[1] = &env->buf[env->buflen];
284 env->argv[2] = NULL;
286 env->buflen += len + 1;
287 return 0;
290 static void cleanup_uevent_env(struct subprocess_info *info)
292 kfree(info->data);
294 #endif
296 static int kobject_uevent_net_broadcast(struct kobject *kobj,
297 struct kobj_uevent_env *env,
298 const char *action_string,
299 const char *devpath)
301 int retval = 0;
302 #if defined(CONFIG_NET)
303 struct sk_buff *skb = NULL;
304 struct uevent_sock *ue_sk;
306 /* send netlink message */
307 list_for_each_entry(ue_sk, &uevent_sock_list, list) {
308 struct sock *uevent_sock = ue_sk->sk;
310 if (!netlink_has_listeners(uevent_sock, 1))
311 continue;
313 if (!skb) {
314 /* allocate message with the maximum possible size */
315 size_t len = strlen(action_string) + strlen(devpath) + 2;
316 char *scratch;
318 retval = -ENOMEM;
319 skb = alloc_skb(len + env->buflen, GFP_KERNEL);
320 if (!skb)
321 continue;
323 /* add header */
324 scratch = skb_put(skb, len);
325 sprintf(scratch, "%s@%s", action_string, devpath);
327 skb_put_data(skb, env->buf, env->buflen);
329 NETLINK_CB(skb).dst_group = 1;
332 retval = netlink_broadcast_filtered(uevent_sock, skb_get(skb),
333 0, 1, GFP_KERNEL,
334 kobj_bcast_filter,
335 kobj);
336 /* ENOBUFS should be handled in userspace */
337 if (retval == -ENOBUFS || retval == -ESRCH)
338 retval = 0;
340 consume_skb(skb);
341 #endif
342 return retval;
345 static void zap_modalias_env(struct kobj_uevent_env *env)
347 static const char modalias_prefix[] = "MODALIAS=";
348 size_t len;
349 int i, j;
351 for (i = 0; i < env->envp_idx;) {
352 if (strncmp(env->envp[i], modalias_prefix,
353 sizeof(modalias_prefix) - 1)) {
354 i++;
355 continue;
358 len = strlen(env->envp[i]) + 1;
360 if (i != env->envp_idx - 1) {
361 memmove(env->envp[i], env->envp[i + 1],
362 env->buflen - len);
364 for (j = i; j < env->envp_idx - 1; j++)
365 env->envp[j] = env->envp[j + 1] - len;
368 env->envp_idx--;
369 env->buflen -= len;
374 * kobject_uevent_env - send an uevent with environmental data
376 * @kobj: struct kobject that the action is happening to
377 * @action: action that is happening
378 * @envp_ext: pointer to environmental data
380 * Returns 0 if kobject_uevent_env() is completed with success or the
381 * corresponding error when it fails.
383 int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
384 char *envp_ext[])
386 struct kobj_uevent_env *env;
387 const char *action_string = kobject_actions[action];
388 const char *devpath = NULL;
389 const char *subsystem;
390 struct kobject *top_kobj;
391 struct kset *kset;
392 const struct kset_uevent_ops *uevent_ops;
393 int i = 0;
394 int retval = 0;
396 pr_debug("kobject: '%s' (%p): %s\n",
397 kobject_name(kobj), kobj, __func__);
399 /* search the kset we belong to */
400 top_kobj = kobj;
401 while (!top_kobj->kset && top_kobj->parent)
402 top_kobj = top_kobj->parent;
404 if (!top_kobj->kset) {
405 pr_debug("kobject: '%s' (%p): %s: attempted to send uevent "
406 "without kset!\n", kobject_name(kobj), kobj,
407 __func__);
408 return -EINVAL;
411 kset = top_kobj->kset;
412 uevent_ops = kset->uevent_ops;
414 /* skip the event, if uevent_suppress is set*/
415 if (kobj->uevent_suppress) {
416 pr_debug("kobject: '%s' (%p): %s: uevent_suppress "
417 "caused the event to drop!\n",
418 kobject_name(kobj), kobj, __func__);
419 return 0;
421 /* skip the event, if the filter returns zero. */
422 if (uevent_ops && uevent_ops->filter)
423 if (!uevent_ops->filter(kset, kobj)) {
424 pr_debug("kobject: '%s' (%p): %s: filter function "
425 "caused the event to drop!\n",
426 kobject_name(kobj), kobj, __func__);
427 return 0;
430 /* originating subsystem */
431 if (uevent_ops && uevent_ops->name)
432 subsystem = uevent_ops->name(kset, kobj);
433 else
434 subsystem = kobject_name(&kset->kobj);
435 if (!subsystem) {
436 pr_debug("kobject: '%s' (%p): %s: unset subsystem caused the "
437 "event to drop!\n", kobject_name(kobj), kobj,
438 __func__);
439 return 0;
442 /* environment buffer */
443 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
444 if (!env)
445 return -ENOMEM;
447 /* complete object path */
448 devpath = kobject_get_path(kobj, GFP_KERNEL);
449 if (!devpath) {
450 retval = -ENOENT;
451 goto exit;
454 /* default keys */
455 retval = add_uevent_var(env, "ACTION=%s", action_string);
456 if (retval)
457 goto exit;
458 retval = add_uevent_var(env, "DEVPATH=%s", devpath);
459 if (retval)
460 goto exit;
461 retval = add_uevent_var(env, "SUBSYSTEM=%s", subsystem);
462 if (retval)
463 goto exit;
465 /* keys passed in from the caller */
466 if (envp_ext) {
467 for (i = 0; envp_ext[i]; i++) {
468 retval = add_uevent_var(env, "%s", envp_ext[i]);
469 if (retval)
470 goto exit;
474 /* let the kset specific function add its stuff */
475 if (uevent_ops && uevent_ops->uevent) {
476 retval = uevent_ops->uevent(kset, kobj, env);
477 if (retval) {
478 pr_debug("kobject: '%s' (%p): %s: uevent() returned "
479 "%d\n", kobject_name(kobj), kobj,
480 __func__, retval);
481 goto exit;
485 switch (action) {
486 case KOBJ_ADD:
488 * Mark "add" event so we can make sure we deliver "remove"
489 * event to userspace during automatic cleanup. If
490 * the object did send an "add" event, "remove" will
491 * automatically generated by the core, if not already done
492 * by the caller.
494 kobj->state_add_uevent_sent = 1;
495 break;
497 case KOBJ_REMOVE:
498 kobj->state_remove_uevent_sent = 1;
499 break;
501 case KOBJ_UNBIND:
502 zap_modalias_env(env);
503 break;
505 default:
506 break;
509 mutex_lock(&uevent_sock_mutex);
510 /* we will send an event, so request a new sequence number */
511 retval = add_uevent_var(env, "SEQNUM=%llu", (unsigned long long)++uevent_seqnum);
512 if (retval) {
513 mutex_unlock(&uevent_sock_mutex);
514 goto exit;
516 retval = kobject_uevent_net_broadcast(kobj, env, action_string,
517 devpath);
518 mutex_unlock(&uevent_sock_mutex);
520 #ifdef CONFIG_UEVENT_HELPER
521 /* call uevent_helper, usually only enabled during early boot */
522 if (uevent_helper[0] && !kobj_usermode_filter(kobj)) {
523 struct subprocess_info *info;
525 retval = add_uevent_var(env, "HOME=/");
526 if (retval)
527 goto exit;
528 retval = add_uevent_var(env,
529 "PATH=/sbin:/bin:/usr/sbin:/usr/bin");
530 if (retval)
531 goto exit;
532 retval = init_uevent_argv(env, subsystem);
533 if (retval)
534 goto exit;
536 retval = -ENOMEM;
537 info = call_usermodehelper_setup(env->argv[0], env->argv,
538 env->envp, GFP_KERNEL,
539 NULL, cleanup_uevent_env, env);
540 if (info) {
541 retval = call_usermodehelper_exec(info, UMH_NO_WAIT);
542 env = NULL; /* freed by cleanup_uevent_env */
545 #endif
547 exit:
548 kfree(devpath);
549 kfree(env);
550 return retval;
552 EXPORT_SYMBOL_GPL(kobject_uevent_env);
555 * kobject_uevent - notify userspace by sending an uevent
557 * @kobj: struct kobject that the action is happening to
558 * @action: action that is happening
560 * Returns 0 if kobject_uevent() is completed with success or the
561 * corresponding error when it fails.
563 int kobject_uevent(struct kobject *kobj, enum kobject_action action)
565 return kobject_uevent_env(kobj, action, NULL);
567 EXPORT_SYMBOL_GPL(kobject_uevent);
570 * add_uevent_var - add key value string to the environment buffer
571 * @env: environment buffer structure
572 * @format: printf format for the key=value pair
574 * Returns 0 if environment variable was added successfully or -ENOMEM
575 * if no space was available.
577 int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...)
579 va_list args;
580 int len;
582 if (env->envp_idx >= ARRAY_SIZE(env->envp)) {
583 WARN(1, KERN_ERR "add_uevent_var: too many keys\n");
584 return -ENOMEM;
587 va_start(args, format);
588 len = vsnprintf(&env->buf[env->buflen],
589 sizeof(env->buf) - env->buflen,
590 format, args);
591 va_end(args);
593 if (len >= (sizeof(env->buf) - env->buflen)) {
594 WARN(1, KERN_ERR "add_uevent_var: buffer size too small\n");
595 return -ENOMEM;
598 env->envp[env->envp_idx++] = &env->buf[env->buflen];
599 env->buflen += len + 1;
600 return 0;
602 EXPORT_SYMBOL_GPL(add_uevent_var);
604 #if defined(CONFIG_NET)
605 static int uevent_net_init(struct net *net)
607 struct uevent_sock *ue_sk;
608 struct netlink_kernel_cfg cfg = {
609 .groups = 1,
610 .flags = NL_CFG_F_NONROOT_RECV,
613 ue_sk = kzalloc(sizeof(*ue_sk), GFP_KERNEL);
614 if (!ue_sk)
615 return -ENOMEM;
617 ue_sk->sk = netlink_kernel_create(net, NETLINK_KOBJECT_UEVENT, &cfg);
618 if (!ue_sk->sk) {
619 printk(KERN_ERR
620 "kobject_uevent: unable to create netlink socket!\n");
621 kfree(ue_sk);
622 return -ENODEV;
624 mutex_lock(&uevent_sock_mutex);
625 list_add_tail(&ue_sk->list, &uevent_sock_list);
626 mutex_unlock(&uevent_sock_mutex);
627 return 0;
630 static void uevent_net_exit(struct net *net)
632 struct uevent_sock *ue_sk;
634 mutex_lock(&uevent_sock_mutex);
635 list_for_each_entry(ue_sk, &uevent_sock_list, list) {
636 if (sock_net(ue_sk->sk) == net)
637 goto found;
639 mutex_unlock(&uevent_sock_mutex);
640 return;
642 found:
643 list_del(&ue_sk->list);
644 mutex_unlock(&uevent_sock_mutex);
646 netlink_kernel_release(ue_sk->sk);
647 kfree(ue_sk);
650 static struct pernet_operations uevent_net_ops = {
651 .init = uevent_net_init,
652 .exit = uevent_net_exit,
655 static int __init kobject_uevent_init(void)
657 return register_pernet_subsys(&uevent_net_ops);
661 postcore_initcall(kobject_uevent_init);
662 #endif