1 // SPDX-License-Identifier: GPL-2.0
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.
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>
28 #include <net/net_namespace.h>
32 #ifdef CONFIG_UEVENT_HELPER
33 char uevent_helper[UEVENT_HELPER_PATH_LEN] = CONFIG_UEVENT_HELPER_PATH;
37 struct list_head list;
42 static LIST_HEAD(uevent_sock_list);
45 /* This lock protects uevent_seqnum and uevent_sock_list */
46 static DEFINE_MUTEX(uevent_sock_mutex);
48 /* the strings here must match the enum in include/linux/kobject.h */
49 static const char *kobject_actions[] = {
51 [KOBJ_REMOVE] = "remove",
52 [KOBJ_CHANGE] = "change",
54 [KOBJ_ONLINE] = "online",
55 [KOBJ_OFFLINE] = "offline",
57 [KOBJ_UNBIND] = "unbind",
60 static int kobject_action_type(const char *buf, size_t count,
61 enum kobject_action *type,
64 enum kobject_action action;
66 const char *args_start;
69 if (count && (buf[count-1] == '\n' || buf[count-1] == '\0'))
75 args_start = strnchr(buf, count, ' ');
77 count_first = args_start - buf;
78 args_start = args_start + 1;
82 for (action = 0; action < ARRAY_SIZE(kobject_actions); action++) {
83 if (strncmp(kobject_actions[action], buf, count_first) != 0)
85 if (kobject_actions[action][count_first] != '\0')
97 static const char *action_arg_word_end(const char *buf, const char *buf_end,
100 const char *next = buf;
102 while (next <= buf_end && *next != delim)
103 if (!isalnum(*next++))
112 static int kobject_action_args(const char *buf, size_t count,
113 struct kobj_uevent_env **ret_env)
115 struct kobj_uevent_env *env = NULL;
116 const char *next, *buf_end, *key;
120 if (count && (buf[count - 1] == '\n' || buf[count - 1] == '\0'))
126 env = kzalloc(sizeof(*env), GFP_KERNEL);
130 /* first arg is UUID */
131 if (count < UUID_STRING_LEN || !uuid_is_valid(buf) ||
132 add_uevent_var(env, "SYNTH_UUID=%.*s", UUID_STRING_LEN, buf))
136 * the rest are custom environment variables in KEY=VALUE
137 * format with ' ' delimiter between each KEY=VALUE pair
139 next = buf + UUID_STRING_LEN;
140 buf_end = buf + count - 1;
142 while (next <= buf_end) {
146 /* skip the ' ', key must follow */
152 next = action_arg_word_end(buf, buf_end, '=');
153 if (!next || next > buf_end || *next != '=')
155 key_len = next - buf;
157 /* skip the '=', value must follow */
158 if (++next > buf_end)
162 next = action_arg_word_end(buf, buf_end, ' ');
166 if (add_uevent_var(env, "SYNTH_ARG_%.*s=%.*s",
167 key_len, key, (int) (next - buf), buf))
181 * kobject_synth_uevent - send synthetic uevent with arguments
183 * @kobj: struct kobject for which synthetic uevent is to be generated
184 * @buf: buffer containing action type and action args, newline is ignored
185 * @count: length of buffer
187 * Returns 0 if kobject_synthetic_uevent() is completed with success or the
188 * corresponding error when it fails.
190 int kobject_synth_uevent(struct kobject *kobj, const char *buf, size_t count)
192 char *no_uuid_envp[] = { "SYNTH_UUID=0", NULL };
193 enum kobject_action action;
194 const char *action_args;
195 struct kobj_uevent_env *env;
196 const char *msg = NULL, *devpath;
199 r = kobject_action_type(buf, count, &action, &action_args);
201 msg = "unknown uevent action string\n";
206 r = kobject_uevent_env(kobj, action, no_uuid_envp);
210 r = kobject_action_args(action_args,
211 count - (action_args - buf), &env);
213 msg = "incorrect uevent action arguments\n";
220 r = kobject_uevent_env(kobj, action, env->envp);
224 devpath = kobject_get_path(kobj, GFP_KERNEL);
225 printk(KERN_WARNING "synth uevent: %s: %s",
226 devpath ?: "unknown device",
227 msg ?: "failed to send uevent");
234 static int kobj_bcast_filter(struct sock *dsk, struct sk_buff *skb, void *data)
236 struct kobject *kobj = data, *ksobj;
237 const struct kobj_ns_type_operations *ops;
239 ops = kobj_ns_ops(kobj);
240 if (!ops && kobj->kset) {
241 ksobj = &kobj->kset->kobj;
242 if (ksobj->parent != NULL)
243 ops = kobj_ns_ops(ksobj->parent);
246 if (ops && ops->netlink_ns && kobj->ktype->namespace) {
247 const void *sock_ns, *ns;
248 ns = kobj->ktype->namespace(kobj);
249 sock_ns = ops->netlink_ns(dsk);
250 return sock_ns != ns;
257 #ifdef CONFIG_UEVENT_HELPER
258 static int kobj_usermode_filter(struct kobject *kobj)
260 const struct kobj_ns_type_operations *ops;
262 ops = kobj_ns_ops(kobj);
264 const void *init_ns, *ns;
265 ns = kobj->ktype->namespace(kobj);
266 init_ns = ops->initial_ns();
267 return ns != init_ns;
273 static int init_uevent_argv(struct kobj_uevent_env *env, const char *subsystem)
277 len = strlcpy(&env->buf[env->buflen], subsystem,
278 sizeof(env->buf) - env->buflen);
279 if (len >= (sizeof(env->buf) - env->buflen)) {
280 WARN(1, KERN_ERR "init_uevent_argv: buffer size too small\n");
284 env->argv[0] = uevent_helper;
285 env->argv[1] = &env->buf[env->buflen];
288 env->buflen += len + 1;
292 static void cleanup_uevent_env(struct subprocess_info *info)
298 static int kobject_uevent_net_broadcast(struct kobject *kobj,
299 struct kobj_uevent_env *env,
300 const char *action_string,
304 #if defined(CONFIG_NET)
305 struct sk_buff *skb = NULL;
306 struct uevent_sock *ue_sk;
308 /* send netlink message */
309 list_for_each_entry(ue_sk, &uevent_sock_list, list) {
310 struct sock *uevent_sock = ue_sk->sk;
312 if (!netlink_has_listeners(uevent_sock, 1))
316 /* allocate message with the maximum possible size */
317 size_t len = strlen(action_string) + strlen(devpath) + 2;
321 skb = alloc_skb(len + env->buflen, GFP_KERNEL);
326 scratch = skb_put(skb, len);
327 sprintf(scratch, "%s@%s", action_string, devpath);
329 skb_put_data(skb, env->buf, env->buflen);
331 NETLINK_CB(skb).dst_group = 1;
334 retval = netlink_broadcast_filtered(uevent_sock, skb_get(skb),
338 /* ENOBUFS should be handled in userspace */
339 if (retval == -ENOBUFS || retval == -ESRCH)
347 static void zap_modalias_env(struct kobj_uevent_env *env)
349 static const char modalias_prefix[] = "MODALIAS=";
353 for (i = 0; i < env->envp_idx;) {
354 if (strncmp(env->envp[i], modalias_prefix,
355 sizeof(modalias_prefix) - 1)) {
360 len = strlen(env->envp[i]) + 1;
362 if (i != env->envp_idx - 1) {
363 memmove(env->envp[i], env->envp[i + 1],
366 for (j = i; j < env->envp_idx - 1; j++)
367 env->envp[j] = env->envp[j + 1] - len;
376 * kobject_uevent_env - send an uevent with environmental data
378 * @kobj: struct kobject that the action is happening to
379 * @action: action that is happening
380 * @envp_ext: pointer to environmental data
382 * Returns 0 if kobject_uevent_env() is completed with success or the
383 * corresponding error when it fails.
385 int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
388 struct kobj_uevent_env *env;
389 const char *action_string = kobject_actions[action];
390 const char *devpath = NULL;
391 const char *subsystem;
392 struct kobject *top_kobj;
394 const struct kset_uevent_ops *uevent_ops;
398 pr_debug("kobject: '%s' (%p): %s\n",
399 kobject_name(kobj), kobj, __func__);
401 /* search the kset we belong to */
403 while (!top_kobj->kset && top_kobj->parent)
404 top_kobj = top_kobj->parent;
406 if (!top_kobj->kset) {
407 pr_debug("kobject: '%s' (%p): %s: attempted to send uevent "
408 "without kset!\n", kobject_name(kobj), kobj,
413 kset = top_kobj->kset;
414 uevent_ops = kset->uevent_ops;
416 /* skip the event, if uevent_suppress is set*/
417 if (kobj->uevent_suppress) {
418 pr_debug("kobject: '%s' (%p): %s: uevent_suppress "
419 "caused the event to drop!\n",
420 kobject_name(kobj), kobj, __func__);
423 /* skip the event, if the filter returns zero. */
424 if (uevent_ops && uevent_ops->filter)
425 if (!uevent_ops->filter(kset, kobj)) {
426 pr_debug("kobject: '%s' (%p): %s: filter function "
427 "caused the event to drop!\n",
428 kobject_name(kobj), kobj, __func__);
432 /* originating subsystem */
433 if (uevent_ops && uevent_ops->name)
434 subsystem = uevent_ops->name(kset, kobj);
436 subsystem = kobject_name(&kset->kobj);
438 pr_debug("kobject: '%s' (%p): %s: unset subsystem caused the "
439 "event to drop!\n", kobject_name(kobj), kobj,
444 /* environment buffer */
445 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
449 /* complete object path */
450 devpath = kobject_get_path(kobj, GFP_KERNEL);
457 retval = add_uevent_var(env, "ACTION=%s", action_string);
460 retval = add_uevent_var(env, "DEVPATH=%s", devpath);
463 retval = add_uevent_var(env, "SUBSYSTEM=%s", subsystem);
467 /* keys passed in from the caller */
469 for (i = 0; envp_ext[i]; i++) {
470 retval = add_uevent_var(env, "%s", envp_ext[i]);
476 /* let the kset specific function add its stuff */
477 if (uevent_ops && uevent_ops->uevent) {
478 retval = uevent_ops->uevent(kset, kobj, env);
480 pr_debug("kobject: '%s' (%p): %s: uevent() returned "
481 "%d\n", kobject_name(kobj), kobj,
490 * Mark "add" event so we can make sure we deliver "remove"
491 * event to userspace during automatic cleanup. If
492 * the object did send an "add" event, "remove" will
493 * automatically generated by the core, if not already done
496 kobj->state_add_uevent_sent = 1;
500 kobj->state_remove_uevent_sent = 1;
504 zap_modalias_env(env);
511 mutex_lock(&uevent_sock_mutex);
512 /* we will send an event, so request a new sequence number */
513 retval = add_uevent_var(env, "SEQNUM=%llu", (unsigned long long)++uevent_seqnum);
515 mutex_unlock(&uevent_sock_mutex);
518 retval = kobject_uevent_net_broadcast(kobj, env, action_string,
520 mutex_unlock(&uevent_sock_mutex);
522 #ifdef CONFIG_UEVENT_HELPER
523 /* call uevent_helper, usually only enabled during early boot */
524 if (uevent_helper[0] && !kobj_usermode_filter(kobj)) {
525 struct subprocess_info *info;
527 retval = add_uevent_var(env, "HOME=/");
530 retval = add_uevent_var(env,
531 "PATH=/sbin:/bin:/usr/sbin:/usr/bin");
534 retval = init_uevent_argv(env, subsystem);
539 info = call_usermodehelper_setup(env->argv[0], env->argv,
540 env->envp, GFP_KERNEL,
541 NULL, cleanup_uevent_env, env);
543 retval = call_usermodehelper_exec(info, UMH_NO_WAIT);
544 env = NULL; /* freed by cleanup_uevent_env */
554 EXPORT_SYMBOL_GPL(kobject_uevent_env);
557 * kobject_uevent - notify userspace by sending an uevent
559 * @kobj: struct kobject that the action is happening to
560 * @action: action that is happening
562 * Returns 0 if kobject_uevent() is completed with success or the
563 * corresponding error when it fails.
565 int kobject_uevent(struct kobject *kobj, enum kobject_action action)
567 return kobject_uevent_env(kobj, action, NULL);
569 EXPORT_SYMBOL_GPL(kobject_uevent);
572 * add_uevent_var - add key value string to the environment buffer
573 * @env: environment buffer structure
574 * @format: printf format for the key=value pair
576 * Returns 0 if environment variable was added successfully or -ENOMEM
577 * if no space was available.
579 int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...)
584 if (env->envp_idx >= ARRAY_SIZE(env->envp)) {
585 WARN(1, KERN_ERR "add_uevent_var: too many keys\n");
589 va_start(args, format);
590 len = vsnprintf(&env->buf[env->buflen],
591 sizeof(env->buf) - env->buflen,
595 if (len >= (sizeof(env->buf) - env->buflen)) {
596 WARN(1, KERN_ERR "add_uevent_var: buffer size too small\n");
600 env->envp[env->envp_idx++] = &env->buf[env->buflen];
601 env->buflen += len + 1;
604 EXPORT_SYMBOL_GPL(add_uevent_var);
606 #if defined(CONFIG_NET)
607 static int uevent_net_init(struct net *net)
609 struct uevent_sock *ue_sk;
610 struct netlink_kernel_cfg cfg = {
612 .flags = NL_CFG_F_NONROOT_RECV,
615 ue_sk = kzalloc(sizeof(*ue_sk), GFP_KERNEL);
619 ue_sk->sk = netlink_kernel_create(net, NETLINK_KOBJECT_UEVENT, &cfg);
622 "kobject_uevent: unable to create netlink socket!\n");
627 net->uevent_sock = ue_sk;
629 mutex_lock(&uevent_sock_mutex);
630 list_add_tail(&ue_sk->list, &uevent_sock_list);
631 mutex_unlock(&uevent_sock_mutex);
635 static void uevent_net_exit(struct net *net)
637 struct uevent_sock *ue_sk = net->uevent_sock;
639 mutex_lock(&uevent_sock_mutex);
640 list_del(&ue_sk->list);
641 mutex_unlock(&uevent_sock_mutex);
643 netlink_kernel_release(ue_sk->sk);
647 static struct pernet_operations uevent_net_ops = {
648 .init = uevent_net_init,
649 .exit = uevent_net_exit,
653 static int __init kobject_uevent_init(void)
655 return register_pernet_subsys(&uevent_net_ops);
659 postcore_initcall(kobject_uevent_init);