blob: bbedaf359a1ec59d15dbb60e1d9dc54d5b87ecec [file] [log] [blame]
Greg Kroah-Hartmand9d16e12017-11-07 17:30:05 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * kernel userspace event delivery
4 *
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.
8 *
9 * Licensed under the GNU GPL v2.
10 *
11 * Authors:
12 * Robert Love <rml@novell.com>
13 * Kay Sievers <kay.sievers@vrfy.org>
14 * Arjan van de Ven <arjanv@redhat.com>
15 * Greg Kroah-Hartman <greg@kroah.com>
16 */
17
18#include <linux/spinlock.h>
Denis V. Lunev2d38f9a2008-03-27 14:26:30 -070019#include <linux/string.h>
20#include <linux/kobject.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050021#include <linux/export.h>
22#include <linux/kmod.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/socket.h>
25#include <linux/skbuff.h>
26#include <linux/netlink.h>
Peter Rajnohaf36776f2017-05-09 15:22:30 +020027#include <linux/uuid.h>
28#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <net/sock.h>
Eric W. Biederman07e98962010-05-04 17:36:44 -070030#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Cornelia Huckcd030c42007-07-20 13:58:13 +020033u64 uevent_seqnum;
Michael Marineau86d56132014-04-10 14:09:31 -070034#ifdef CONFIG_UEVENT_HELPER
Kay Sievers6a8d8ab2007-08-15 15:38:28 +020035char uevent_helper[UEVENT_HELPER_PATH_LEN] = CONFIG_UEVENT_HELPER_PATH;
Michael Marineau86d56132014-04-10 14:09:31 -070036#endif
Eric W. Biederman07e98962010-05-04 17:36:44 -070037#ifdef CONFIG_NET
38struct uevent_sock {
39 struct list_head list;
40 struct sock *sk;
41};
42static LIST_HEAD(uevent_sock_list);
Cornelia Huckcd030c42007-07-20 13:58:13 +020043#endif
44
Andrew Vagin7b60a182012-03-07 14:49:56 +040045/* This lock protects uevent_seqnum and uevent_sock_list */
46static DEFINE_MUTEX(uevent_sock_mutex);
47
Kay Sievers5c5daf62007-08-12 20:43:55 +020048/* the strings here must match the enum in include/linux/kobject.h */
49static const char *kobject_actions[] = {
50 [KOBJ_ADD] = "add",
51 [KOBJ_REMOVE] = "remove",
52 [KOBJ_CHANGE] = "change",
53 [KOBJ_MOVE] = "move",
54 [KOBJ_ONLINE] = "online",
55 [KOBJ_OFFLINE] = "offline",
Dmitry Torokhov1455cf82017-07-19 17:24:30 -070056 [KOBJ_BIND] = "bind",
57 [KOBJ_UNBIND] = "unbind",
Kay Sievers5c5daf62007-08-12 20:43:55 +020058};
59
Peter Rajnohaf36776f2017-05-09 15:22:30 +020060static int kobject_action_type(const char *buf, size_t count,
61 enum kobject_action *type,
62 const char **args)
Kay Sievers5c5daf62007-08-12 20:43:55 +020063{
64 enum kobject_action action;
Peter Rajnohaf36776f2017-05-09 15:22:30 +020065 size_t count_first;
66 const char *args_start;
Kay Sievers5c5daf62007-08-12 20:43:55 +020067 int ret = -EINVAL;
68
Mark Lorda9edadb2008-03-28 19:05:25 -040069 if (count && (buf[count-1] == '\n' || buf[count-1] == '\0'))
Kay Sievers5c5daf62007-08-12 20:43:55 +020070 count--;
71
72 if (!count)
73 goto out;
74
Peter Rajnohaf36776f2017-05-09 15:22:30 +020075 args_start = strnchr(buf, count, ' ');
76 if (args_start) {
77 count_first = args_start - buf;
78 args_start = args_start + 1;
79 } else
80 count_first = count;
81
Kay Sievers5c5daf62007-08-12 20:43:55 +020082 for (action = 0; action < ARRAY_SIZE(kobject_actions); action++) {
Peter Rajnohaf36776f2017-05-09 15:22:30 +020083 if (strncmp(kobject_actions[action], buf, count_first) != 0)
Kay Sievers5c5daf62007-08-12 20:43:55 +020084 continue;
Peter Rajnohaf36776f2017-05-09 15:22:30 +020085 if (kobject_actions[action][count_first] != '\0')
Kay Sievers5c5daf62007-08-12 20:43:55 +020086 continue;
Peter Rajnohaf36776f2017-05-09 15:22:30 +020087 if (args)
88 *args = args_start;
Kay Sievers5c5daf62007-08-12 20:43:55 +020089 *type = action;
90 ret = 0;
91 break;
92 }
93out:
94 return ret;
95}
96
Peter Rajnohaf36776f2017-05-09 15:22:30 +020097static const char *action_arg_word_end(const char *buf, const char *buf_end,
98 char delim)
99{
100 const char *next = buf;
101
102 while (next <= buf_end && *next != delim)
103 if (!isalnum(*next++))
104 return NULL;
105
106 if (next == buf)
107 return NULL;
108
109 return next;
110}
111
112static int kobject_action_args(const char *buf, size_t count,
113 struct kobj_uevent_env **ret_env)
114{
115 struct kobj_uevent_env *env = NULL;
116 const char *next, *buf_end, *key;
117 int key_len;
118 int r = -EINVAL;
119
120 if (count && (buf[count - 1] == '\n' || buf[count - 1] == '\0'))
121 count--;
122
123 if (!count)
124 return -EINVAL;
125
126 env = kzalloc(sizeof(*env), GFP_KERNEL);
127 if (!env)
128 return -ENOMEM;
129
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))
133 goto out;
134
135 /*
136 * the rest are custom environment variables in KEY=VALUE
137 * format with ' ' delimiter between each KEY=VALUE pair
138 */
139 next = buf + UUID_STRING_LEN;
140 buf_end = buf + count - 1;
141
142 while (next <= buf_end) {
143 if (*next != ' ')
144 goto out;
145
146 /* skip the ' ', key must follow */
147 key = ++next;
148 if (key > buf_end)
149 goto out;
150
151 buf = next;
152 next = action_arg_word_end(buf, buf_end, '=');
153 if (!next || next > buf_end || *next != '=')
154 goto out;
155 key_len = next - buf;
156
157 /* skip the '=', value must follow */
158 if (++next > buf_end)
159 goto out;
160
161 buf = next;
162 next = action_arg_word_end(buf, buf_end, ' ');
163 if (!next)
164 goto out;
165
166 if (add_uevent_var(env, "SYNTH_ARG_%.*s=%.*s",
167 key_len, key, (int) (next - buf), buf))
168 goto out;
169 }
170
171 r = 0;
172out:
173 if (r)
174 kfree(env);
175 else
176 *ret_env = env;
177 return r;
178}
179
180/**
181 * kobject_synth_uevent - send synthetic uevent with arguments
182 *
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
186 *
187 * Returns 0 if kobject_synthetic_uevent() is completed with success or the
188 * corresponding error when it fails.
189 */
190int kobject_synth_uevent(struct kobject *kobj, const char *buf, size_t count)
191{
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;
197 int r;
198
199 r = kobject_action_type(buf, count, &action, &action_args);
200 if (r) {
201 msg = "unknown uevent action string\n";
202 goto out;
203 }
204
205 if (!action_args) {
206 r = kobject_uevent_env(kobj, action, no_uuid_envp);
207 goto out;
208 }
209
210 r = kobject_action_args(action_args,
211 count - (action_args - buf), &env);
212 if (r == -EINVAL) {
213 msg = "incorrect uevent action arguments\n";
214 goto out;
215 }
216
217 if (r)
218 goto out;
219
220 r = kobject_uevent_env(kobj, action, env->envp);
221 kfree(env);
222out:
223 if (r) {
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");
228 kfree(devpath);
229 }
230 return r;
231}
232
Andrew Mortonc8421282010-05-21 15:05:21 -0700233#ifdef CONFIG_NET
Eric W. Biederman5f71a292010-05-04 17:36:47 -0700234static int kobj_bcast_filter(struct sock *dsk, struct sk_buff *skb, void *data)
235{
Weilong Chen82ef3d52014-01-16 17:24:31 +0800236 struct kobject *kobj = data, *ksobj;
Eric W. Biederman5f71a292010-05-04 17:36:47 -0700237 const struct kobj_ns_type_operations *ops;
238
239 ops = kobj_ns_ops(kobj);
Weilong Chen82ef3d52014-01-16 17:24:31 +0800240 if (!ops && kobj->kset) {
241 ksobj = &kobj->kset->kobj;
242 if (ksobj->parent != NULL)
243 ops = kobj_ns_ops(ksobj->parent);
244 }
245
246 if (ops && ops->netlink_ns && kobj->ktype->namespace) {
Eric W. Biederman5f71a292010-05-04 17:36:47 -0700247 const void *sock_ns, *ns;
248 ns = kobj->ktype->namespace(kobj);
249 sock_ns = ops->netlink_ns(dsk);
250 return sock_ns != ns;
251 }
252
253 return 0;
254}
Andrew Mortonc8421282010-05-21 15:05:21 -0700255#endif
Eric W. Biederman5f71a292010-05-04 17:36:47 -0700256
Michael Marineau86d56132014-04-10 14:09:31 -0700257#ifdef CONFIG_UEVENT_HELPER
Eric W. Biederman417daa12010-05-04 17:36:48 -0700258static int kobj_usermode_filter(struct kobject *kobj)
259{
260 const struct kobj_ns_type_operations *ops;
261
262 ops = kobj_ns_ops(kobj);
263 if (ops) {
264 const void *init_ns, *ns;
265 ns = kobj->ktype->namespace(kobj);
266 init_ns = ops->initial_ns();
267 return ns != init_ns;
268 }
269
270 return 0;
271}
272
Vladimir Davydovbcccff92014-04-03 14:48:21 -0700273static int init_uevent_argv(struct kobj_uevent_env *env, const char *subsystem)
274{
275 int len;
276
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");
281 return -ENOMEM;
282 }
283
284 env->argv[0] = uevent_helper;
285 env->argv[1] = &env->buf[env->buflen];
286 env->argv[2] = NULL;
287
288 env->buflen += len + 1;
289 return 0;
290}
291
292static void cleanup_uevent_env(struct subprocess_info *info)
293{
294 kfree(info->data);
295}
Michael Marineau86d56132014-04-10 14:09:31 -0700296#endif
Vladimir Davydovbcccff92014-04-03 14:48:21 -0700297
Eric Dumazet16dff332017-09-19 16:27:03 -0700298static int kobject_uevent_net_broadcast(struct kobject *kobj,
299 struct kobj_uevent_env *env,
300 const char *action_string,
301 const char *devpath)
302{
303 int retval = 0;
304#if defined(CONFIG_NET)
Eric Dumazetd464e842017-09-19 16:27:05 -0700305 struct sk_buff *skb = NULL;
Eric Dumazet16dff332017-09-19 16:27:03 -0700306 struct uevent_sock *ue_sk;
307
308 /* send netlink message */
309 list_for_each_entry(ue_sk, &uevent_sock_list, list) {
310 struct sock *uevent_sock = ue_sk->sk;
Eric Dumazet16dff332017-09-19 16:27:03 -0700311
312 if (!netlink_has_listeners(uevent_sock, 1))
313 continue;
314
Eric Dumazetd464e842017-09-19 16:27:05 -0700315 if (!skb) {
316 /* allocate message with the maximum possible size */
317 size_t len = strlen(action_string) + strlen(devpath) + 2;
Eric Dumazet16dff332017-09-19 16:27:03 -0700318 char *scratch;
Eric Dumazet16dff332017-09-19 16:27:03 -0700319
Eric Dumazetd464e842017-09-19 16:27:05 -0700320 retval = -ENOMEM;
321 skb = alloc_skb(len + env->buflen, GFP_KERNEL);
322 if (!skb)
323 continue;
324
Eric Dumazet16dff332017-09-19 16:27:03 -0700325 /* add header */
326 scratch = skb_put(skb, len);
327 sprintf(scratch, "%s@%s", action_string, devpath);
328
Eric Dumazet4a336a22017-09-19 16:27:04 -0700329 skb_put_data(skb, env->buf, env->buflen);
Eric Dumazet16dff332017-09-19 16:27:03 -0700330
331 NETLINK_CB(skb).dst_group = 1;
Eric Dumazetd464e842017-09-19 16:27:05 -0700332 }
333
334 retval = netlink_broadcast_filtered(uevent_sock, skb_get(skb),
335 0, 1, GFP_KERNEL,
336 kobj_bcast_filter,
337 kobj);
338 /* ENOBUFS should be handled in userspace */
339 if (retval == -ENOBUFS || retval == -ESRCH)
340 retval = 0;
Eric Dumazet16dff332017-09-19 16:27:03 -0700341 }
Eric Dumazetd464e842017-09-19 16:27:05 -0700342 consume_skb(skb);
Eric Dumazet16dff332017-09-19 16:27:03 -0700343#endif
344 return retval;
345}
346
Dmitry Torokhov6878e7d2017-09-13 16:29:48 -0700347static void zap_modalias_env(struct kobj_uevent_env *env)
348{
349 static const char modalias_prefix[] = "MODALIAS=";
350 int i;
351
352 for (i = 0; i < env->envp_idx;) {
353 if (strncmp(env->envp[i], modalias_prefix,
354 sizeof(modalias_prefix) - 1)) {
355 i++;
356 continue;
357 }
358
359 if (i != env->envp_idx - 1)
360 memmove(&env->envp[i], &env->envp[i + 1],
361 sizeof(env->envp[i]) * env->envp_idx - 1);
362
363 env->envp_idx--;
364 }
365}
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367/**
Cornelia Huck8a824722006-11-20 17:07:51 +0100368 * kobject_uevent_env - send an uevent with environmental data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 * @kobj: struct kobject that the action is happening to
Julia Lawalledbbf992016-10-01 21:46:28 +0200371 * @action: action that is happening
Cornelia Huck8a824722006-11-20 17:07:51 +0100372 * @envp_ext: pointer to environmental data
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800373 *
Xiaotian Fengf6e6e772010-08-13 18:58:10 +0800374 * Returns 0 if kobject_uevent_env() is completed with success or the
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800375 * corresponding error when it fails.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 */
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800377int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
Kay Sievers7eff2e72007-08-14 15:15:12 +0200378 char *envp_ext[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
Kay Sievers7eff2e72007-08-14 15:15:12 +0200380 struct kobj_uevent_env *env;
381 const char *action_string = kobject_actions[action];
Kay Sievers5f123fb2005-11-11 14:43:07 +0100382 const char *devpath = NULL;
383 const char *subsystem;
384 struct kobject *top_kobj;
385 struct kset *kset;
Emese Revfy9cd43612009-12-31 14:52:51 +0100386 const struct kset_uevent_ops *uevent_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 int i = 0;
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800388 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800390 pr_debug("kobject: '%s' (%p): %s\n",
Harvey Harrison810304d2008-04-30 00:55:08 -0700391 kobject_name(kobj), kobj, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Kay Sievers5f123fb2005-11-11 14:43:07 +0100393 /* search the kset we belong to */
394 top_kobj = kobj;
Kay Sieversccd490a2007-08-12 20:43:55 +0200395 while (!top_kobj->kset && top_kobj->parent)
John Anthony Kazos Jr14193fb2007-04-04 07:39:17 -0400396 top_kobj = top_kobj->parent;
Kay Sieversccd490a2007-08-12 20:43:55 +0200397
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800398 if (!top_kobj->kset) {
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800399 pr_debug("kobject: '%s' (%p): %s: attempted to send uevent "
400 "without kset!\n", kobject_name(kobj), kobj,
Harvey Harrison810304d2008-04-30 00:55:08 -0700401 __func__);
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800402 return -EINVAL;
403 }
Kay Sievers5f123fb2005-11-11 14:43:07 +0100404
405 kset = top_kobj->kset;
Kay Sievers312c0042005-11-16 09:00:00 +0100406 uevent_ops = kset->uevent_ops;
Kay Sievers5f123fb2005-11-11 14:43:07 +0100407
Ming Leif67f1292009-03-01 21:10:49 +0800408 /* skip the event, if uevent_suppress is set*/
409 if (kobj->uevent_suppress) {
410 pr_debug("kobject: '%s' (%p): %s: uevent_suppress "
411 "caused the event to drop!\n",
412 kobject_name(kobj), kobj, __func__);
413 return 0;
414 }
Kay Sievers7eff2e72007-08-14 15:15:12 +0200415 /* skip the event, if the filter returns zero. */
Kay Sievers312c0042005-11-16 09:00:00 +0100416 if (uevent_ops && uevent_ops->filter)
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800417 if (!uevent_ops->filter(kset, kobj)) {
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800418 pr_debug("kobject: '%s' (%p): %s: filter function "
419 "caused the event to drop!\n",
Harvey Harrison810304d2008-04-30 00:55:08 -0700420 kobject_name(kobj), kobj, __func__);
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800421 return 0;
422 }
Kay Sievers5f123fb2005-11-11 14:43:07 +0100423
Kay Sievers86406242007-03-14 03:25:56 +0100424 /* originating subsystem */
425 if (uevent_ops && uevent_ops->name)
426 subsystem = uevent_ops->name(kset, kobj);
427 else
428 subsystem = kobject_name(&kset->kobj);
429 if (!subsystem) {
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800430 pr_debug("kobject: '%s' (%p): %s: unset subsystem caused the "
431 "event to drop!\n", kobject_name(kobj), kobj,
Harvey Harrison810304d2008-04-30 00:55:08 -0700432 __func__);
Kay Sievers86406242007-03-14 03:25:56 +0100433 return 0;
434 }
435
Kay Sievers7eff2e72007-08-14 15:15:12 +0200436 /* environment buffer */
437 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
438 if (!env)
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800439 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Kay Sievers5f123fb2005-11-11 14:43:07 +0100441 /* complete object path */
442 devpath = kobject_get_path(kobj, GFP_KERNEL);
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800443 if (!devpath) {
444 retval = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 goto exit;
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Kay Sievers5f123fb2005-11-11 14:43:07 +0100448 /* default keys */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200449 retval = add_uevent_var(env, "ACTION=%s", action_string);
450 if (retval)
451 goto exit;
452 retval = add_uevent_var(env, "DEVPATH=%s", devpath);
453 if (retval)
454 goto exit;
455 retval = add_uevent_var(env, "SUBSYSTEM=%s", subsystem);
456 if (retval)
457 goto exit;
458
459 /* keys passed in from the caller */
460 if (envp_ext) {
461 for (i = 0; envp_ext[i]; i++) {
Tejun Heoc65b9142008-11-13 13:20:00 +0900462 retval = add_uevent_var(env, "%s", envp_ext[i]);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200463 if (retval)
464 goto exit;
465 }
466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Kay Sievers5f123fb2005-11-11 14:43:07 +0100468 /* let the kset specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100469 if (uevent_ops && uevent_ops->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200470 retval = uevent_ops->uevent(kset, kobj, env);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (retval) {
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800472 pr_debug("kobject: '%s' (%p): %s: uevent() returned "
473 "%d\n", kobject_name(kobj), kobj,
Harvey Harrison810304d2008-04-30 00:55:08 -0700474 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 goto exit;
476 }
477 }
478
Dmitry Torokhov6878e7d2017-09-13 16:29:48 -0700479 switch (action) {
480 case KOBJ_ADD:
481 /*
482 * Mark "add" event so we can make sure we deliver "remove"
483 * event to userspace during automatic cleanup. If
484 * the object did send an "add" event, "remove" will
485 * automatically generated by the core, if not already done
486 * by the caller.
487 */
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100488 kobj->state_add_uevent_sent = 1;
Dmitry Torokhov6878e7d2017-09-13 16:29:48 -0700489 break;
490
491 case KOBJ_REMOVE:
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100492 kobj->state_remove_uevent_sent = 1;
Dmitry Torokhov6878e7d2017-09-13 16:29:48 -0700493 break;
494
495 case KOBJ_UNBIND:
496 zap_modalias_env(env);
497 break;
498
499 default:
500 break;
501 }
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100502
Andrew Vagin7b60a182012-03-07 14:49:56 +0400503 mutex_lock(&uevent_sock_mutex);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200504 /* we will send an event, so request a new sequence number */
Andrew Vagin7b60a182012-03-07 14:49:56 +0400505 retval = add_uevent_var(env, "SEQNUM=%llu", (unsigned long long)++uevent_seqnum);
506 if (retval) {
507 mutex_unlock(&uevent_sock_mutex);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200508 goto exit;
Andrew Vagin7b60a182012-03-07 14:49:56 +0400509 }
Eric Dumazet16dff332017-09-19 16:27:03 -0700510 retval = kobject_uevent_net_broadcast(kobj, env, action_string,
511 devpath);
Andrew Vagin7b60a182012-03-07 14:49:56 +0400512 mutex_unlock(&uevent_sock_mutex);
Kay Sievers5f123fb2005-11-11 14:43:07 +0100513
Michael Marineau86d56132014-04-10 14:09:31 -0700514#ifdef CONFIG_UEVENT_HELPER
Kay Sievers5f123fb2005-11-11 14:43:07 +0100515 /* call uevent_helper, usually only enabled during early boot */
Eric W. Biederman417daa12010-05-04 17:36:48 -0700516 if (uevent_helper[0] && !kobj_usermode_filter(kobj)) {
Vladimir Davydovbcccff92014-04-03 14:48:21 -0700517 struct subprocess_info *info;
Kay Sievers5f123fb2005-11-11 14:43:07 +0100518
Kay Sievers7eff2e72007-08-14 15:15:12 +0200519 retval = add_uevent_var(env, "HOME=/");
520 if (retval)
521 goto exit;
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800522 retval = add_uevent_var(env,
523 "PATH=/sbin:/bin:/usr/sbin:/usr/bin");
Kay Sievers7eff2e72007-08-14 15:15:12 +0200524 if (retval)
525 goto exit;
Vladimir Davydovbcccff92014-04-03 14:48:21 -0700526 retval = init_uevent_argv(env, subsystem);
527 if (retval)
528 goto exit;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200529
Vladimir Davydovbcccff92014-04-03 14:48:21 -0700530 retval = -ENOMEM;
531 info = call_usermodehelper_setup(env->argv[0], env->argv,
532 env->envp, GFP_KERNEL,
533 NULL, cleanup_uevent_env, env);
534 if (info) {
535 retval = call_usermodehelper_exec(info, UMH_NO_WAIT);
536 env = NULL; /* freed by cleanup_uevent_env */
537 }
Kay Sievers5f123fb2005-11-11 14:43:07 +0100538 }
Michael Marineau86d56132014-04-10 14:09:31 -0700539#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541exit:
Kay Sievers5f123fb2005-11-11 14:43:07 +0100542 kfree(devpath);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200543 kfree(env);
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800544 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
Cornelia Huck8a824722006-11-20 17:07:51 +0100546EXPORT_SYMBOL_GPL(kobject_uevent_env);
547
548/**
Xiaotian Fengf6e6e772010-08-13 18:58:10 +0800549 * kobject_uevent - notify userspace by sending an uevent
Cornelia Huck8a824722006-11-20 17:07:51 +0100550 *
Cornelia Huck8a824722006-11-20 17:07:51 +0100551 * @kobj: struct kobject that the action is happening to
Julia Lawalledbbf992016-10-01 21:46:28 +0200552 * @action: action that is happening
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800553 *
554 * Returns 0 if kobject_uevent() is completed with success or the
555 * corresponding error when it fails.
Cornelia Huck8a824722006-11-20 17:07:51 +0100556 */
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800557int kobject_uevent(struct kobject *kobj, enum kobject_action action)
Cornelia Huck8a824722006-11-20 17:07:51 +0100558{
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800559 return kobject_uevent_env(kobj, action, NULL);
Cornelia Huck8a824722006-11-20 17:07:51 +0100560}
Kay Sievers312c0042005-11-16 09:00:00 +0100561EXPORT_SYMBOL_GPL(kobject_uevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563/**
Kay Sievers7eff2e72007-08-14 15:15:12 +0200564 * add_uevent_var - add key value string to the environment buffer
565 * @env: environment buffer structure
566 * @format: printf format for the key=value pair
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 *
568 * Returns 0 if environment variable was added successfully or -ENOMEM
569 * if no space was available.
570 */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200571int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
573 va_list args;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200574 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Kay Sievers7eff2e72007-08-14 15:15:12 +0200576 if (env->envp_idx >= ARRAY_SIZE(env->envp)) {
Arjan van de Ven5cd2b459d2008-07-25 19:45:39 -0700577 WARN(1, KERN_ERR "add_uevent_var: too many keys\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 va_start(args, format);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200582 len = vsnprintf(&env->buf[env->buflen],
583 sizeof(env->buf) - env->buflen,
584 format, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 va_end(args);
586
Kay Sievers7eff2e72007-08-14 15:15:12 +0200587 if (len >= (sizeof(env->buf) - env->buflen)) {
Arjan van de Ven5cd2b459d2008-07-25 19:45:39 -0700588 WARN(1, KERN_ERR "add_uevent_var: buffer size too small\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Kay Sievers7eff2e72007-08-14 15:15:12 +0200592 env->envp[env->envp_idx++] = &env->buf[env->buflen];
593 env->buflen += len + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return 0;
595}
Kay Sievers312c0042005-11-16 09:00:00 +0100596EXPORT_SYMBOL_GPL(add_uevent_var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200598#if defined(CONFIG_NET)
Eric W. Biederman07e98962010-05-04 17:36:44 -0700599static int uevent_net_init(struct net *net)
Kay Sievers5f123fb2005-11-11 14:43:07 +0100600{
Eric W. Biederman07e98962010-05-04 17:36:44 -0700601 struct uevent_sock *ue_sk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +0000602 struct netlink_kernel_cfg cfg = {
603 .groups = 1,
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +0000604 .flags = NL_CFG_F_NONROOT_RECV,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +0000605 };
Eric W. Biederman07e98962010-05-04 17:36:44 -0700606
607 ue_sk = kzalloc(sizeof(*ue_sk), GFP_KERNEL);
608 if (!ue_sk)
609 return -ENOMEM;
610
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +0000611 ue_sk->sk = netlink_kernel_create(net, NETLINK_KOBJECT_UEVENT, &cfg);
Eric W. Biederman07e98962010-05-04 17:36:44 -0700612 if (!ue_sk->sk) {
Kay Sievers5f123fb2005-11-11 14:43:07 +0100613 printk(KERN_ERR
614 "kobject_uevent: unable to create netlink socket!\n");
Dan Carpenter743db2d2010-05-25 11:51:10 +0200615 kfree(ue_sk);
Kay Sievers5f123fb2005-11-11 14:43:07 +0100616 return -ENODEV;
617 }
Eric W. Biederman07e98962010-05-04 17:36:44 -0700618 mutex_lock(&uevent_sock_mutex);
619 list_add_tail(&ue_sk->list, &uevent_sock_list);
620 mutex_unlock(&uevent_sock_mutex);
Kay Sievers5f123fb2005-11-11 14:43:07 +0100621 return 0;
622}
623
Eric W. Biederman07e98962010-05-04 17:36:44 -0700624static void uevent_net_exit(struct net *net)
625{
626 struct uevent_sock *ue_sk;
627
628 mutex_lock(&uevent_sock_mutex);
629 list_for_each_entry(ue_sk, &uevent_sock_list, list) {
630 if (sock_net(ue_sk->sk) == net)
631 goto found;
632 }
633 mutex_unlock(&uevent_sock_mutex);
634 return;
635
636found:
637 list_del(&ue_sk->list);
638 mutex_unlock(&uevent_sock_mutex);
639
640 netlink_kernel_release(ue_sk->sk);
641 kfree(ue_sk);
642}
643
644static struct pernet_operations uevent_net_ops = {
645 .init = uevent_net_init,
646 .exit = uevent_net_exit,
647};
648
649static int __init kobject_uevent_init(void)
650{
Eric W. Biederman07e98962010-05-04 17:36:44 -0700651 return register_pernet_subsys(&uevent_net_ops);
652}
653
654
Kay Sievers5f123fb2005-11-11 14:43:07 +0100655postcore_initcall(kobject_uevent_init);
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200656#endif