blob: 9fc15d7a0efdd5dc2644a4298463fbc7d5c4274d [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
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 <rml@novell.com>
12 * Kay Sievers <kay.sievers@vrfy.org>
13 * Arjan van de Ven <arjanv@redhat.com>
14 * Greg Kroah-Hartman <greg@kroah.com>
15 */
16
17#include <linux/spinlock.h>
18#include <linux/string.h>
19#include <linux/kobject.h>
20#include <linux/export.h>
21#include <linux/kmod.h>
22#include <linux/slab.h>
23#include <linux/user_namespace.h>
24#include <linux/socket.h>
25#include <linux/skbuff.h>
26#include <linux/netlink.h>
27#include <net/sock.h>
28#include <net/net_namespace.h>
29
30
31u64 uevent_seqnum;
32char uevent_helper[UEVENT_HELPER_PATH_LEN] = CONFIG_UEVENT_HELPER_PATH;
33#ifdef CONFIG_NET
34struct uevent_sock {
35 struct list_head list;
36 struct sock *sk;
37};
38static LIST_HEAD(uevent_sock_list);
39#endif
40
41/* This lock protects uevent_seqnum and uevent_sock_list */
42static DEFINE_MUTEX(uevent_sock_mutex);
43
44/* the strings here must match the enum in include/linux/kobject.h */
45static const char *kobject_actions[] = {
46 [KOBJ_ADD] = "add",
47 [KOBJ_REMOVE] = "remove",
48 [KOBJ_CHANGE] = "change",
49 [KOBJ_MOVE] = "move",
50 [KOBJ_ONLINE] = "online",
51 [KOBJ_OFFLINE] = "offline",
52};
53
54/**
55 * kobject_action_type - translate action string to numeric type
56 *
57 * @buf: buffer containing the action string, newline is ignored
58 * @len: length of buffer
59 * @type: pointer to the location to store the action type
60 *
61 * Returns 0 if the action string was recognized.
62 */
63int kobject_action_type(const char *buf, size_t count,
64 enum kobject_action *type)
65{
66 enum kobject_action action;
67 int ret = -EINVAL;
68
69 if (count && (buf[count-1] == '\n' || buf[count-1] == '\0'))
70 count--;
71
72 if (!count)
73 goto out;
74
75 for (action = 0; action < ARRAY_SIZE(kobject_actions); action++) {
76 if (strncmp(kobject_actions[action], buf, count) != 0)
77 continue;
78 if (kobject_actions[action][count] != '\0')
79 continue;
80 *type = action;
81 ret = 0;
82 break;
83 }
84out:
85 return ret;
86}
87
88#ifdef CONFIG_NET
89static int kobj_bcast_filter(struct sock *dsk, struct sk_buff *skb, void *data)
90{
91 struct kobject *kobj = data;
92 const struct kobj_ns_type_operations *ops;
93
94 ops = kobj_ns_ops(kobj);
95 if (ops) {
96 const void *sock_ns, *ns;
97 ns = kobj->ktype->namespace(kobj);
98 sock_ns = ops->netlink_ns(dsk);
99 return sock_ns != ns;
100 }
101
102 return 0;
103}
104#endif
105
106static int kobj_usermode_filter(struct kobject *kobj)
107{
108 const struct kobj_ns_type_operations *ops;
109
110 ops = kobj_ns_ops(kobj);
111 if (ops) {
112 const void *init_ns, *ns;
113 ns = kobj->ktype->namespace(kobj);
114 init_ns = ops->initial_ns();
115 return ns != init_ns;
116 }
117
118 return 0;
119}
120#ifdef CONFIG_NET
121static int miss_message_num = 0;
122static char*miss_message[5] = {NULL,NULL,NULL,NULL,NULL};
123#endif
124/**
125 * kobject_uevent_env - send an uevent with environmental data
126 *
127 * @action: action that is happening
128 * @kobj: struct kobject that the action is happening to
129 * @envp_ext: pointer to environmental data
130 *
131 * Returns 0 if kobject_uevent_env() is completed with success or the
132 * corresponding error when it fails.
133 */
134int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
135 char *envp_ext[])
136{
137 struct kobj_uevent_env *env;
138 const char *action_string = kobject_actions[action];
139 const char *devpath = NULL;
140 const char *subsystem;
141 struct kobject *top_kobj;
142 struct kset *kset;
143 const struct kset_uevent_ops *uevent_ops;
144 int i = 0;
145 int retval = 0;
146#ifdef CONFIG_NET
147 struct uevent_sock *ue_sk;
148#endif
149
150 pr_debug("kobject: '%s' (%p): %s\n",
151 kobject_name(kobj), kobj, __func__);
152#ifdef CONFIG_NET
153 if(kobj->name)
154 {
155 print_sun(SUN_DBG,"kobject_uevent,name = %s ,action = %d, ref:KOBJ_ADD",kobj->name,action);
156 }
157 else
158 {
159 print_sun(SUN_DBG,"kobject_uevent,action = %d, ref:KOBJ_ADD",action);
160 }
161#endif
162 /* search the kset we belong to */
163 top_kobj = kobj;
164 while (!top_kobj->kset && top_kobj->parent)
165 top_kobj = top_kobj->parent;
166
167 if (!top_kobj->kset) {
168 pr_debug("kobject: '%s' (%p): %s: attempted to send uevent "
169 "without kset!\n", kobject_name(kobj), kobj,
170 __func__);
171 return -EINVAL;
172 }
173
174 kset = top_kobj->kset;
175 uevent_ops = kset->uevent_ops;
176
177 /* skip the event, if uevent_suppress is set*/
178 if (kobj->uevent_suppress) {
179 pr_debug("kobject: '%s' (%p): %s: uevent_suppress "
180 "caused the event to drop!\n",
181 kobject_name(kobj), kobj, __func__);
182 return 0;
183 }
184 /* skip the event, if the filter returns zero. */
185 if (uevent_ops && uevent_ops->filter)
186 if (!uevent_ops->filter(kset, kobj)) {
187 pr_debug("kobject: '%s' (%p): %s: filter function "
188 "caused the event to drop!\n",
189 kobject_name(kobj), kobj, __func__);
190 return 0;
191 }
192
193 /* originating subsystem */
194 if (uevent_ops && uevent_ops->name)
195 subsystem = uevent_ops->name(kset, kobj);
196 else
197 subsystem = kobject_name(&kset->kobj);
198 if (!subsystem) {
199 pr_debug("kobject: '%s' (%p): %s: unset subsystem caused the "
200 "event to drop!\n", kobject_name(kobj), kobj,
201 __func__);
202 return 0;
203 }
204
205 /* environment buffer */
206 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
207 if (!env)
208 return -ENOMEM;
209
210 /* complete object path */
211 devpath = kobject_get_path(kobj, GFP_KERNEL);
212 if (!devpath) {
213 retval = -ENOENT;
214 goto exit;
215 }
216
217 /* default keys */
218 retval = add_uevent_var(env, "ACTION=%s", action_string);
219 if (retval)
220 goto exit;
221 retval = add_uevent_var(env, "DEVPATH=%s", devpath);
222 if (retval)
223 goto exit;
224 retval = add_uevent_var(env, "SUBSYSTEM=%s", subsystem);
225 if (retval)
226 goto exit;
227
228 /* keys passed in from the caller */
229 if (envp_ext) {
230 for (i = 0; envp_ext[i]; i++) {
231 retval = add_uevent_var(env, "%s", envp_ext[i]);
232 if (retval)
233 goto exit;
234 }
235 }
236
237 /* let the kset specific function add its stuff */
238 if (uevent_ops && uevent_ops->uevent) {
239 retval = uevent_ops->uevent(kset, kobj, env);
240 if (retval) {
241 pr_debug("kobject: '%s' (%p): %s: uevent() returned "
242 "%d\n", kobject_name(kobj), kobj,
243 __func__, retval);
244 goto exit;
245 }
246 }
247
248 /*
249 * Mark "add" and "remove" events in the object to ensure proper
250 * events to userspace during automatic cleanup. If the object did
251 * send an "add" event, "remove" will automatically generated by
252 * the core, if not already done by the caller.
253 */
254 if (action == KOBJ_ADD)
255 kobj->state_add_uevent_sent = 1;
256 else if (action == KOBJ_REMOVE)
257 kobj->state_remove_uevent_sent = 1;
258
259 mutex_lock(&uevent_sock_mutex);
260 /* we will send an event, so request a new sequence number */
261 retval = add_uevent_var(env, "SEQNUM=%llu", (unsigned long long)++uevent_seqnum);
262 if (retval) {
263 mutex_unlock(&uevent_sock_mutex);
264 goto exit;
265 }
266
267#if defined(CONFIG_NET)
268 /* send netlink message */
269 list_for_each_entry(ue_sk, &uevent_sock_list, list) {
270 struct sock *uevent_sock = ue_sk->sk;
271 struct sk_buff *skb;
272 size_t len;
273
274 if (!netlink_has_listeners(uevent_sock, 1))
275 continue;
276
277 /* allocate message with the maximum possible size */
278 len = strlen(action_string) + strlen(devpath) + 2;
279 skb = alloc_skb(len + env->buflen, GFP_KERNEL);
280 if (skb) {
281 char *scratch;
282
283 /* add header */
284 scratch = skb_put(skb, len);
285 sprintf(scratch, "%s@%s", action_string, devpath);
286
287 /* copy keys to our continuous event payload buffer */
288 for (i = 0; i < env->envp_idx; i++) {
289 len = strlen(env->envp[i]) + 1;
290 scratch = skb_put(skb, len);
291 strcpy(scratch, env->envp[i]);
292 }
293
294 NETLINK_CB(skb).dst_group = 1;
295 retval = netlink_broadcast_filtered(uevent_sock, skb,
296 0, 1, GFP_KERNEL,
297 kobj_bcast_filter,
298 kobj);
299 /* ENOBUFS should be handled in userspace */
300 if (retval == -ENOBUFS || retval == -ESRCH){
301#ifdef CONFIG_NET
302 miss_message_num++;
303 printk(KERN_INFO"%s,retval:%d,action:%s,miss message num:%d,action:%s\n",__func__,retval,action_string,miss_message_num,action_string);
304 for(i=4;i>0;i--){
305 miss_message[i] = miss_message[i-1];
306 }
307 if (envp_ext)
308 miss_message[0] = envp_ext[0];
309 else
310 miss_message[0] = action_string;
311#endif
312 retval = 0;
313 }
314 } else
315 retval = -ENOMEM;
316 }
317#endif
318 mutex_unlock(&uevent_sock_mutex);
319
320 /* call uevent_helper, usually only enabled during early boot */
321 if (uevent_helper[0] && !kobj_usermode_filter(kobj)) {
322 char *argv [3];
323
324 argv [0] = uevent_helper;
325 argv [1] = (char *)subsystem;
326 argv [2] = NULL;
327 retval = add_uevent_var(env, "HOME=/");
328 if (retval)
329 goto exit;
330 retval = add_uevent_var(env,
331 "PATH=/sbin:/bin:/usr/sbin:/usr/bin");
332 if (retval)
333 goto exit;
334
335 retval = call_usermodehelper(argv[0], argv,
336 env->envp, UMH_WAIT_EXEC);
337 }
338
339exit:
340 kfree(devpath);
341 kfree(env);
342 return retval;
343}
344EXPORT_SYMBOL_GPL(kobject_uevent_env);
345
346/**
347 * kobject_uevent - notify userspace by sending an uevent
348 *
349 * @action: action that is happening
350 * @kobj: struct kobject that the action is happening to
351 *
352 * Returns 0 if kobject_uevent() is completed with success or the
353 * corresponding error when it fails.
354 */
355int kobject_uevent(struct kobject *kobj, enum kobject_action action)
356{
357 return kobject_uevent_env(kobj, action, NULL);
358}
359EXPORT_SYMBOL_GPL(kobject_uevent);
360
361/**
362 * add_uevent_var - add key value string to the environment buffer
363 * @env: environment buffer structure
364 * @format: printf format for the key=value pair
365 *
366 * Returns 0 if environment variable was added successfully or -ENOMEM
367 * if no space was available.
368 */
369int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...)
370{
371 va_list args;
372 int len;
373
374 if (env->envp_idx >= ARRAY_SIZE(env->envp)) {
375 WARN(1, KERN_ERR "add_uevent_var: too many keys\n");
376 return -ENOMEM;
377 }
378
379 va_start(args, format);
380 len = vsnprintf(&env->buf[env->buflen],
381 sizeof(env->buf) - env->buflen,
382 format, args);
383 va_end(args);
384
385 if (len >= (sizeof(env->buf) - env->buflen)) {
386 WARN(1, KERN_ERR "add_uevent_var: buffer size too small\n");
387 return -ENOMEM;
388 }
389
390 env->envp[env->envp_idx++] = &env->buf[env->buflen];
391 env->buflen += len + 1;
392 return 0;
393}
394EXPORT_SYMBOL_GPL(add_uevent_var);
395
396#if defined(CONFIG_NET)
397static int uevent_net_init(struct net *net)
398{
399 struct uevent_sock *ue_sk;
400
401 ue_sk = kzalloc(sizeof(*ue_sk), GFP_KERNEL);
402 if (!ue_sk)
403 return -ENOMEM;
404
405 ue_sk->sk = netlink_kernel_create(net, NETLINK_KOBJECT_UEVENT,
406 1, NULL, NULL, THIS_MODULE);
407 if (!ue_sk->sk) {
408 printk(KERN_ERR
409 "kobject_uevent: unable to create netlink socket!\n");
410 kfree(ue_sk);
411 return -ENODEV;
412 }
413 mutex_lock(&uevent_sock_mutex);
414 list_add_tail(&ue_sk->list, &uevent_sock_list);
415 mutex_unlock(&uevent_sock_mutex);
416 return 0;
417}
418
419static void uevent_net_exit(struct net *net)
420{
421 struct uevent_sock *ue_sk;
422
423 mutex_lock(&uevent_sock_mutex);
424 list_for_each_entry(ue_sk, &uevent_sock_list, list) {
425 if (sock_net(ue_sk->sk) == net)
426 goto found;
427 }
428 mutex_unlock(&uevent_sock_mutex);
429 return;
430
431found:
432 list_del(&ue_sk->list);
433 mutex_unlock(&uevent_sock_mutex);
434
435 netlink_kernel_release(ue_sk->sk);
436 kfree(ue_sk);
437}
438
439static struct pernet_operations uevent_net_ops = {
440 .init = uevent_net_init,
441 .exit = uevent_net_exit,
442};
443
444static int __init kobject_uevent_init(void)
445{
446 netlink_set_nonroot(NETLINK_KOBJECT_UEVENT, NL_NONROOT_RECV);
447 return register_pernet_subsys(&uevent_net_ops);
448}
449
450
451postcore_initcall(kobject_uevent_init);
452#endif