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
;
40 static LIST_HEAD(uevent_sock_list
);
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
[] = {
49 [KOBJ_REMOVE
] = "remove",
50 [KOBJ_CHANGE
] = "change",
52 [KOBJ_ONLINE
] = "online",
53 [KOBJ_OFFLINE
] = "offline",
55 [KOBJ_UNBIND
] = "unbind",
58 static int kobject_action_type(const char *buf
, size_t count
,
59 enum kobject_action
*type
,
62 enum kobject_action action
;
64 const char *args_start
;
67 if (count
&& (buf
[count
-1] == '\n' || buf
[count
-1] == '\0'))
73 args_start
= strnchr(buf
, count
, ' ');
75 count_first
= args_start
- buf
;
76 args_start
= args_start
+ 1;
80 for (action
= 0; action
< ARRAY_SIZE(kobject_actions
); action
++) {
81 if (strncmp(kobject_actions
[action
], buf
, count_first
) != 0)
83 if (kobject_actions
[action
][count_first
] != '\0')
95 static const char *action_arg_word_end(const char *buf
, const char *buf_end
,
98 const char *next
= buf
;
100 while (next
<= buf_end
&& *next
!= delim
)
101 if (!isalnum(*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
;
118 if (count
&& (buf
[count
- 1] == '\n' || buf
[count
- 1] == '\0'))
124 env
= kzalloc(sizeof(*env
), GFP_KERNEL
);
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
))
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
) {
144 /* skip the ' ', key must follow */
150 next
= action_arg_word_end(buf
, buf_end
, '=');
151 if (!next
|| next
> buf_end
|| *next
!= '=')
153 key_len
= next
- buf
;
155 /* skip the '=', value must follow */
156 if (++next
> buf_end
)
160 next
= action_arg_word_end(buf
, buf_end
, ' ');
164 if (add_uevent_var(env
, "SYNTH_ARG_%.*s=%.*s",
165 key_len
, key
, (int) (next
- buf
), buf
))
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
;
197 r
= kobject_action_type(buf
, count
, &action
, &action_args
);
199 msg
= "unknown uevent action string\n";
204 r
= kobject_uevent_env(kobj
, action
, no_uuid_envp
);
208 r
= kobject_action_args(action_args
,
209 count
- (action_args
- buf
), &env
);
211 msg
= "incorrect uevent action arguments\n";
218 r
= kobject_uevent_env(kobj
, action
, env
->envp
);
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");
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
;
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
);
262 const void *init_ns
, *ns
;
263 ns
= kobj
->ktype
->namespace(kobj
);
264 init_ns
= ops
->initial_ns();
265 return ns
!= init_ns
;
271 static int init_uevent_argv(struct kobj_uevent_env
*env
, const char *subsystem
)
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");
282 env
->argv
[0] = uevent_helper
;
283 env
->argv
[1] = &env
->buf
[env
->buflen
];
286 env
->buflen
+= len
+ 1;
290 static void cleanup_uevent_env(struct subprocess_info
*info
)
296 static int kobject_uevent_net_broadcast(struct kobject
*kobj
,
297 struct kobj_uevent_env
*env
,
298 const char *action_string
,
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))
314 /* allocate message with the maximum possible size */
315 size_t len
= strlen(action_string
) + strlen(devpath
) + 2;
319 skb
= alloc_skb(len
+ env
->buflen
, GFP_KERNEL
);
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
),
336 /* ENOBUFS should be handled in userspace */
337 if (retval
== -ENOBUFS
|| retval
== -ESRCH
)
345 static void zap_modalias_env(struct kobj_uevent_env
*env
)
347 static const char modalias_prefix
[] = "MODALIAS=";
351 for (i
= 0; i
< env
->envp_idx
;) {
352 if (strncmp(env
->envp
[i
], modalias_prefix
,
353 sizeof(modalias_prefix
) - 1)) {
358 len
= strlen(env
->envp
[i
]) + 1;
360 if (i
!= env
->envp_idx
- 1) {
361 memmove(env
->envp
[i
], env
->envp
[i
+ 1],
364 for (j
= i
; j
< env
->envp_idx
- 1; j
++)
365 env
->envp
[j
] = env
->envp
[j
+ 1] - 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
,
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
;
392 const struct kset_uevent_ops
*uevent_ops
;
396 pr_debug("kobject: '%s' (%p): %s\n",
397 kobject_name(kobj
), kobj
, __func__
);
399 /* search the kset we belong to */
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
,
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__
);
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__
);
430 /* originating subsystem */
431 if (uevent_ops
&& uevent_ops
->name
)
432 subsystem
= uevent_ops
->name(kset
, kobj
);
434 subsystem
= kobject_name(&kset
->kobj
);
436 pr_debug("kobject: '%s' (%p): %s: unset subsystem caused the "
437 "event to drop!\n", kobject_name(kobj
), kobj
,
442 /* environment buffer */
443 env
= kzalloc(sizeof(struct kobj_uevent_env
), GFP_KERNEL
);
447 /* complete object path */
448 devpath
= kobject_get_path(kobj
, GFP_KERNEL
);
455 retval
= add_uevent_var(env
, "ACTION=%s", action_string
);
458 retval
= add_uevent_var(env
, "DEVPATH=%s", devpath
);
461 retval
= add_uevent_var(env
, "SUBSYSTEM=%s", subsystem
);
465 /* keys passed in from the caller */
467 for (i
= 0; envp_ext
[i
]; i
++) {
468 retval
= add_uevent_var(env
, "%s", envp_ext
[i
]);
474 /* let the kset specific function add its stuff */
475 if (uevent_ops
&& uevent_ops
->uevent
) {
476 retval
= uevent_ops
->uevent(kset
, kobj
, env
);
478 pr_debug("kobject: '%s' (%p): %s: uevent() returned "
479 "%d\n", kobject_name(kobj
), kobj
,
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
494 kobj
->state_add_uevent_sent
= 1;
498 kobj
->state_remove_uevent_sent
= 1;
502 zap_modalias_env(env
);
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
);
513 mutex_unlock(&uevent_sock_mutex
);
516 retval
= kobject_uevent_net_broadcast(kobj
, env
, action_string
,
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=/");
528 retval
= add_uevent_var(env
,
529 "PATH=/sbin:/bin:/usr/sbin:/usr/bin");
532 retval
= init_uevent_argv(env
, subsystem
);
537 info
= call_usermodehelper_setup(env
->argv
[0], env
->argv
,
538 env
->envp
, GFP_KERNEL
,
539 NULL
, cleanup_uevent_env
, env
);
541 retval
= call_usermodehelper_exec(info
, UMH_NO_WAIT
);
542 env
= NULL
; /* freed by cleanup_uevent_env */
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
, ...)
582 if (env
->envp_idx
>= ARRAY_SIZE(env
->envp
)) {
583 WARN(1, KERN_ERR
"add_uevent_var: too many keys\n");
587 va_start(args
, format
);
588 len
= vsnprintf(&env
->buf
[env
->buflen
],
589 sizeof(env
->buf
) - env
->buflen
,
593 if (len
>= (sizeof(env
->buf
) - env
->buflen
)) {
594 WARN(1, KERN_ERR
"add_uevent_var: buffer size too small\n");
598 env
->envp
[env
->envp_idx
++] = &env
->buf
[env
->buflen
];
599 env
->buflen
+= len
+ 1;
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
= {
610 .flags
= NL_CFG_F_NONROOT_RECV
,
613 ue_sk
= kzalloc(sizeof(*ue_sk
), GFP_KERNEL
);
617 ue_sk
->sk
= netlink_kernel_create(net
, NETLINK_KOBJECT_UEVENT
, &cfg
);
620 "kobject_uevent: unable to create netlink socket!\n");
624 mutex_lock(&uevent_sock_mutex
);
625 list_add_tail(&ue_sk
->list
, &uevent_sock_list
);
626 mutex_unlock(&uevent_sock_mutex
);
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
)
639 mutex_unlock(&uevent_sock_mutex
);
643 list_del(&ue_sk
->list
);
644 mutex_unlock(&uevent_sock_mutex
);
646 netlink_kernel_release(ue_sk
->sk
);
650 static struct pernet_operations uevent_net_ops
= {
651 .init
= uevent_net_init
,
652 .exit
= uevent_net_exit
,
656 static int __init
kobject_uevent_init(void)
658 return register_pernet_subsys(&uevent_net_ops
);
662 postcore_initcall(kobject_uevent_init
);