blob: 9fe6ec8fda28ea732d41cad83fd639a82a41a69c [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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * 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>
14 */
15
16#include <linux/spinlock.h>
Denis V. Lunev2d38f9a2008-03-27 14:26:30 -070017#include <linux/string.h>
18#include <linux/kobject.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050019#include <linux/export.h>
20#include <linux/kmod.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/socket.h>
23#include <linux/skbuff.h>
24#include <linux/netlink.h>
Peter Rajnohaf36776f2017-05-09 15:22:30 +020025#include <linux/uuid.h>
26#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <net/sock.h>
Eric W. Biederman07e98962010-05-04 17:36:44 -070028#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Cornelia Huckcd030c42007-07-20 13:58:13 +020031u64 uevent_seqnum;
Michael Marineau86d56132014-04-10 14:09:31 -070032#ifdef CONFIG_UEVENT_HELPER
Kay Sievers6a8d8ab2007-08-15 15:38:28 +020033char uevent_helper[UEVENT_HELPER_PATH_LEN] = CONFIG_UEVENT_HELPER_PATH;
Michael Marineau86d56132014-04-10 14:09:31 -070034#endif
Eric W. Biederman07e98962010-05-04 17:36:44 -070035#ifdef CONFIG_NET
36struct uevent_sock {
37 struct list_head list;
38 struct sock *sk;
39};
40static LIST_HEAD(uevent_sock_list);
Cornelia Huckcd030c42007-07-20 13:58:13 +020041#endif
42
Andrew Vagin7b60a182012-03-07 14:49:56 +040043/* This lock protects uevent_seqnum and uevent_sock_list */
44static DEFINE_MUTEX(uevent_sock_mutex);
45
Kay Sievers5c5daf62007-08-12 20:43:55 +020046/* the strings here must match the enum in include/linux/kobject.h */
47static 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",
Dmitry Torokhov1455cf82017-07-19 17:24:30 -070054 [KOBJ_BIND] = "bind",
55 [KOBJ_UNBIND] = "unbind",
Kay Sievers5c5daf62007-08-12 20:43:55 +020056};
57
Peter Rajnohaf36776f2017-05-09 15:22:30 +020058static int kobject_action_type(const char *buf, size_t count,
59 enum kobject_action *type,
60 const char **args)
Kay Sievers5c5daf62007-08-12 20:43:55 +020061{
62 enum kobject_action action;
Peter Rajnohaf36776f2017-05-09 15:22:30 +020063 size_t count_first;
64 const char *args_start;
Kay Sievers5c5daf62007-08-12 20:43:55 +020065 int ret = -EINVAL;
66
Mark Lorda9edadb2008-03-28 19:05:25 -040067 if (count && (buf[count-1] == '\n' || buf[count-1] == '\0'))
Kay Sievers5c5daf62007-08-12 20:43:55 +020068 count--;
69
70 if (!count)
71 goto out;
72
Peter Rajnohaf36776f2017-05-09 15:22:30 +020073 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;
79
Kay Sievers5c5daf62007-08-12 20:43:55 +020080 for (action = 0; action < ARRAY_SIZE(kobject_actions); action++) {
Peter Rajnohaf36776f2017-05-09 15:22:30 +020081 if (strncmp(kobject_actions[action], buf, count_first) != 0)
Kay Sievers5c5daf62007-08-12 20:43:55 +020082 continue;
Peter Rajnohaf36776f2017-05-09 15:22:30 +020083 if (kobject_actions[action][count_first] != '\0')
Kay Sievers5c5daf62007-08-12 20:43:55 +020084 continue;
Peter Rajnohaf36776f2017-05-09 15:22:30 +020085 if (args)
86 *args = args_start;
Kay Sievers5c5daf62007-08-12 20:43:55 +020087 *type = action;
88 ret = 0;
89 break;
90 }
91out:
92 return ret;
93}
94
Peter Rajnohaf36776f2017-05-09 15:22:30 +020095static const char *action_arg_word_end(const char *buf, const char *buf_end,
96 char delim)
97{
98 const char *next = buf;
99
100 while (next <= buf_end && *next != delim)
101 if (!isalnum(*next++))
102 return NULL;
103
104 if (next == buf)
105 return NULL;
106
107 return next;
108}
109
110static int kobject_action_args(const char *buf, size_t count,
111 struct kobj_uevent_env **ret_env)
112{
113 struct kobj_uevent_env *env = NULL;
114 const char *next, *buf_end, *key;
115 int key_len;
116 int r = -EINVAL;
117
118 if (count && (buf[count - 1] == '\n' || buf[count - 1] == '\0'))
119 count--;
120
121 if (!count)
122 return -EINVAL;
123
124 env = kzalloc(sizeof(*env), GFP_KERNEL);
125 if (!env)
126 return -ENOMEM;
127
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;
132
133 /*
134 * the rest are custom environment variables in KEY=VALUE
135 * format with ' ' delimiter between each KEY=VALUE pair
136 */
137 next = buf + UUID_STRING_LEN;
138 buf_end = buf + count - 1;
139
140 while (next <= buf_end) {
141 if (*next != ' ')
142 goto out;
143
144 /* skip the ' ', key must follow */
145 key = ++next;
146 if (key > buf_end)
147 goto out;
148
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;
154
155 /* skip the '=', value must follow */
156 if (++next > buf_end)
157 goto out;
158
159 buf = next;
160 next = action_arg_word_end(buf, buf_end, ' ');
161 if (!next)
162 goto out;
163
164 if (add_uevent_var(env, "SYNTH_ARG_%.*s=%.*s",
165 key_len, key, (int) (next - buf), buf))
166 goto out;
167 }
168
169 r = 0;
170out:
171 if (r)
172 kfree(env);
173 else
174 *ret_env = env;
175 return r;
176}
177
178/**
179 * kobject_synth_uevent - send synthetic uevent with arguments
180 *
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
184 *
185 * Returns 0 if kobject_synthetic_uevent() is completed with success or the
186 * corresponding error when it fails.
187 */
188int kobject_synth_uevent(struct kobject *kobj, const char *buf, size_t count)
189{
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;
196
197 r = kobject_action_type(buf, count, &action, &action_args);
198 if (r) {
199 msg = "unknown uevent action string\n";
200 goto out;
201 }
202
203 if (!action_args) {
204 r = kobject_uevent_env(kobj, action, no_uuid_envp);
205 goto out;
206 }
207
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;
213 }
214
215 if (r)
216 goto out;
217
218 r = kobject_uevent_env(kobj, action, env->envp);
219 kfree(env);
220out:
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);
227 }
228 return r;
229}
230
Andrew Mortonc8421282010-05-21 15:05:21 -0700231#ifdef CONFIG_NET
Eric W. Biederman5f71a292010-05-04 17:36:47 -0700232static int kobj_bcast_filter(struct sock *dsk, struct sk_buff *skb, void *data)
233{
Weilong Chen82ef3d52014-01-16 17:24:31 +0800234 struct kobject *kobj = data, *ksobj;
Eric W. Biederman5f71a292010-05-04 17:36:47 -0700235 const struct kobj_ns_type_operations *ops;
236
237 ops = kobj_ns_ops(kobj);
Weilong Chen82ef3d52014-01-16 17:24:31 +0800238 if (!ops && kobj->kset) {
239 ksobj = &kobj->kset->kobj;
240 if (ksobj->parent != NULL)
241 ops = kobj_ns_ops(ksobj->parent);
242 }
243
244 if (ops && ops->netlink_ns && kobj->ktype->namespace) {
Eric W. Biederman5f71a292010-05-04 17:36:47 -0700245 const void *sock_ns, *ns;
246 ns = kobj->ktype->namespace(kobj);
247 sock_ns = ops->netlink_ns(dsk);
248 return sock_ns != ns;
249 }
250
251 return 0;
252}
Andrew Mortonc8421282010-05-21 15:05:21 -0700253#endif
Eric W. Biederman5f71a292010-05-04 17:36:47 -0700254
Michael Marineau86d56132014-04-10 14:09:31 -0700255#ifdef CONFIG_UEVENT_HELPER
Eric W. Biederman417daa12010-05-04 17:36:48 -0700256static int kobj_usermode_filter(struct kobject *kobj)
257{
258 const struct kobj_ns_type_operations *ops;
259
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;
266 }
267
268 return 0;
269}
270
Vladimir Davydovbcccff92014-04-03 14:48:21 -0700271static int init_uevent_argv(struct kobj_uevent_env *env, const char *subsystem)
272{
273 int len;
274
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;
280 }
281
282 env->argv[0] = uevent_helper;
283 env->argv[1] = &env->buf[env->buflen];
284 env->argv[2] = NULL;
285
286 env->buflen += len + 1;
287 return 0;
288}
289
290static void cleanup_uevent_env(struct subprocess_info *info)
291{
292 kfree(info->data);
293}
Michael Marineau86d56132014-04-10 14:09:31 -0700294#endif
Vladimir Davydovbcccff92014-04-03 14:48:21 -0700295
Eric Dumazet16dff332017-09-19 16:27:03 -0700296static int kobject_uevent_net_broadcast(struct kobject *kobj,
297 struct kobj_uevent_env *env,
298 const char *action_string,
299 const char *devpath)
300{
301 int retval = 0;
302#if defined(CONFIG_NET)
Eric Dumazetd464e842017-09-19 16:27:05 -0700303 struct sk_buff *skb = NULL;
Eric Dumazet16dff332017-09-19 16:27:03 -0700304 struct uevent_sock *ue_sk;
305
306 /* send netlink message */
307 list_for_each_entry(ue_sk, &uevent_sock_list, list) {
308 struct sock *uevent_sock = ue_sk->sk;
Eric Dumazet16dff332017-09-19 16:27:03 -0700309
310 if (!netlink_has_listeners(uevent_sock, 1))
311 continue;
312
Eric Dumazetd464e842017-09-19 16:27:05 -0700313 if (!skb) {
314 /* allocate message with the maximum possible size */
315 size_t len = strlen(action_string) + strlen(devpath) + 2;
Eric Dumazet16dff332017-09-19 16:27:03 -0700316 char *scratch;
Eric Dumazet16dff332017-09-19 16:27:03 -0700317
Eric Dumazetd464e842017-09-19 16:27:05 -0700318 retval = -ENOMEM;
319 skb = alloc_skb(len + env->buflen, GFP_KERNEL);
320 if (!skb)
321 continue;
322
Eric Dumazet16dff332017-09-19 16:27:03 -0700323 /* add header */
324 scratch = skb_put(skb, len);
325 sprintf(scratch, "%s@%s", action_string, devpath);
326
Eric Dumazet4a336a22017-09-19 16:27:04 -0700327 skb_put_data(skb, env->buf, env->buflen);
Eric Dumazet16dff332017-09-19 16:27:03 -0700328
329 NETLINK_CB(skb).dst_group = 1;
Eric Dumazetd464e842017-09-19 16:27:05 -0700330 }
331
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;
Eric Dumazet16dff332017-09-19 16:27:03 -0700339 }
Eric Dumazetd464e842017-09-19 16:27:05 -0700340 consume_skb(skb);
Eric Dumazet16dff332017-09-19 16:27:03 -0700341#endif
342 return retval;
343}
344
Dmitry Torokhov6878e7d2017-09-13 16:29:48 -0700345static void zap_modalias_env(struct kobj_uevent_env *env)
346{
347 static const char modalias_prefix[] = "MODALIAS=";
Dmitry Torokhov9b3fa472017-12-13 15:21:22 -0800348 size_t len;
349 int i, j;
Dmitry Torokhov6878e7d2017-09-13 16:29:48 -0700350
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;
356 }
357
Dmitry Torokhov9b3fa472017-12-13 15:21:22 -0800358 len = strlen(env->envp[i]) + 1;
359
360 if (i != env->envp_idx - 1) {
361 memmove(env->envp[i], env->envp[i + 1],
362 env->buflen - len);
363
364 for (j = i; j < env->envp_idx - 1; j++)
365 env->envp[j] = env->envp[j + 1] - len;
366 }
Dmitry Torokhov6878e7d2017-09-13 16:29:48 -0700367
368 env->envp_idx--;
Dmitry Torokhov9b3fa472017-12-13 15:21:22 -0800369 env->buflen -= len;
Dmitry Torokhov6878e7d2017-09-13 16:29:48 -0700370 }
371}
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373/**
Cornelia Huck8a824722006-11-20 17:07:51 +0100374 * kobject_uevent_env - send an uevent with environmental data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 * @kobj: struct kobject that the action is happening to
Julia Lawalledbbf992016-10-01 21:46:28 +0200377 * @action: action that is happening
Cornelia Huck8a824722006-11-20 17:07:51 +0100378 * @envp_ext: pointer to environmental data
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800379 *
Xiaotian Fengf6e6e772010-08-13 18:58:10 +0800380 * Returns 0 if kobject_uevent_env() is completed with success or the
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800381 * corresponding error when it fails.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 */
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800383int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
Kay Sievers7eff2e72007-08-14 15:15:12 +0200384 char *envp_ext[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Kay Sievers7eff2e72007-08-14 15:15:12 +0200386 struct kobj_uevent_env *env;
387 const char *action_string = kobject_actions[action];
Kay Sievers5f123fb2005-11-11 14:43:07 +0100388 const char *devpath = NULL;
389 const char *subsystem;
390 struct kobject *top_kobj;
391 struct kset *kset;
Emese Revfy9cd43612009-12-31 14:52:51 +0100392 const struct kset_uevent_ops *uevent_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 int i = 0;
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800394 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800396 pr_debug("kobject: '%s' (%p): %s\n",
Harvey Harrison810304d2008-04-30 00:55:08 -0700397 kobject_name(kobj), kobj, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Kay Sievers5f123fb2005-11-11 14:43:07 +0100399 /* search the kset we belong to */
400 top_kobj = kobj;
Kay Sieversccd490a2007-08-12 20:43:55 +0200401 while (!top_kobj->kset && top_kobj->parent)
John Anthony Kazos Jr14193fb2007-04-04 07:39:17 -0400402 top_kobj = top_kobj->parent;
Kay Sieversccd490a2007-08-12 20:43:55 +0200403
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800404 if (!top_kobj->kset) {
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800405 pr_debug("kobject: '%s' (%p): %s: attempted to send uevent "
406 "without kset!\n", kobject_name(kobj), kobj,
Harvey Harrison810304d2008-04-30 00:55:08 -0700407 __func__);
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800408 return -EINVAL;
409 }
Kay Sievers5f123fb2005-11-11 14:43:07 +0100410
411 kset = top_kobj->kset;
Kay Sievers312c0042005-11-16 09:00:00 +0100412 uevent_ops = kset->uevent_ops;
Kay Sievers5f123fb2005-11-11 14:43:07 +0100413
Ming Leif67f1292009-03-01 21:10:49 +0800414 /* 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;
420 }
Kay Sievers7eff2e72007-08-14 15:15:12 +0200421 /* skip the event, if the filter returns zero. */
Kay Sievers312c0042005-11-16 09:00:00 +0100422 if (uevent_ops && uevent_ops->filter)
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800423 if (!uevent_ops->filter(kset, kobj)) {
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800424 pr_debug("kobject: '%s' (%p): %s: filter function "
425 "caused the event to drop!\n",
Harvey Harrison810304d2008-04-30 00:55:08 -0700426 kobject_name(kobj), kobj, __func__);
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800427 return 0;
428 }
Kay Sievers5f123fb2005-11-11 14:43:07 +0100429
Kay Sievers86406242007-03-14 03:25:56 +0100430 /* 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) {
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800436 pr_debug("kobject: '%s' (%p): %s: unset subsystem caused the "
437 "event to drop!\n", kobject_name(kobj), kobj,
Harvey Harrison810304d2008-04-30 00:55:08 -0700438 __func__);
Kay Sievers86406242007-03-14 03:25:56 +0100439 return 0;
440 }
441
Kay Sievers7eff2e72007-08-14 15:15:12 +0200442 /* environment buffer */
443 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
444 if (!env)
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800445 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Kay Sievers5f123fb2005-11-11 14:43:07 +0100447 /* complete object path */
448 devpath = kobject_get_path(kobj, GFP_KERNEL);
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800449 if (!devpath) {
450 retval = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 goto exit;
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Kay Sievers5f123fb2005-11-11 14:43:07 +0100454 /* default keys */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200455 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;
464
465 /* keys passed in from the caller */
466 if (envp_ext) {
467 for (i = 0; envp_ext[i]; i++) {
Tejun Heoc65b9142008-11-13 13:20:00 +0900468 retval = add_uevent_var(env, "%s", envp_ext[i]);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200469 if (retval)
470 goto exit;
471 }
472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Kay Sievers5f123fb2005-11-11 14:43:07 +0100474 /* let the kset specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100475 if (uevent_ops && uevent_ops->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200476 retval = uevent_ops->uevent(kset, kobj, env);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 if (retval) {
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800478 pr_debug("kobject: '%s' (%p): %s: uevent() returned "
479 "%d\n", kobject_name(kobj), kobj,
Harvey Harrison810304d2008-04-30 00:55:08 -0700480 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 goto exit;
482 }
483 }
484
Dmitry Torokhov6878e7d2017-09-13 16:29:48 -0700485 switch (action) {
486 case KOBJ_ADD:
487 /*
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.
493 */
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100494 kobj->state_add_uevent_sent = 1;
Dmitry Torokhov6878e7d2017-09-13 16:29:48 -0700495 break;
496
497 case KOBJ_REMOVE:
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100498 kobj->state_remove_uevent_sent = 1;
Dmitry Torokhov6878e7d2017-09-13 16:29:48 -0700499 break;
500
501 case KOBJ_UNBIND:
502 zap_modalias_env(env);
503 break;
504
505 default:
506 break;
507 }
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100508
Andrew Vagin7b60a182012-03-07 14:49:56 +0400509 mutex_lock(&uevent_sock_mutex);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200510 /* we will send an event, so request a new sequence number */
Andrew Vagin7b60a182012-03-07 14:49:56 +0400511 retval = add_uevent_var(env, "SEQNUM=%llu", (unsigned long long)++uevent_seqnum);
512 if (retval) {
513 mutex_unlock(&uevent_sock_mutex);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200514 goto exit;
Andrew Vagin7b60a182012-03-07 14:49:56 +0400515 }
Eric Dumazet16dff332017-09-19 16:27:03 -0700516 retval = kobject_uevent_net_broadcast(kobj, env, action_string,
517 devpath);
Andrew Vagin7b60a182012-03-07 14:49:56 +0400518 mutex_unlock(&uevent_sock_mutex);
Kay Sievers5f123fb2005-11-11 14:43:07 +0100519
Michael Marineau86d56132014-04-10 14:09:31 -0700520#ifdef CONFIG_UEVENT_HELPER
Kay Sievers5f123fb2005-11-11 14:43:07 +0100521 /* call uevent_helper, usually only enabled during early boot */
Eric W. Biederman417daa12010-05-04 17:36:48 -0700522 if (uevent_helper[0] && !kobj_usermode_filter(kobj)) {
Vladimir Davydovbcccff92014-04-03 14:48:21 -0700523 struct subprocess_info *info;
Kay Sievers5f123fb2005-11-11 14:43:07 +0100524
Kay Sievers7eff2e72007-08-14 15:15:12 +0200525 retval = add_uevent_var(env, "HOME=/");
526 if (retval)
527 goto exit;
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800528 retval = add_uevent_var(env,
529 "PATH=/sbin:/bin:/usr/sbin:/usr/bin");
Kay Sievers7eff2e72007-08-14 15:15:12 +0200530 if (retval)
531 goto exit;
Vladimir Davydovbcccff92014-04-03 14:48:21 -0700532 retval = init_uevent_argv(env, subsystem);
533 if (retval)
534 goto exit;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200535
Vladimir Davydovbcccff92014-04-03 14:48:21 -0700536 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 */
543 }
Kay Sievers5f123fb2005-11-11 14:43:07 +0100544 }
Michael Marineau86d56132014-04-10 14:09:31 -0700545#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547exit:
Kay Sievers5f123fb2005-11-11 14:43:07 +0100548 kfree(devpath);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200549 kfree(env);
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800550 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
Cornelia Huck8a824722006-11-20 17:07:51 +0100552EXPORT_SYMBOL_GPL(kobject_uevent_env);
553
554/**
Xiaotian Fengf6e6e772010-08-13 18:58:10 +0800555 * kobject_uevent - notify userspace by sending an uevent
Cornelia Huck8a824722006-11-20 17:07:51 +0100556 *
Cornelia Huck8a824722006-11-20 17:07:51 +0100557 * @kobj: struct kobject that the action is happening to
Julia Lawalledbbf992016-10-01 21:46:28 +0200558 * @action: action that is happening
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800559 *
560 * Returns 0 if kobject_uevent() is completed with success or the
561 * corresponding error when it fails.
Cornelia Huck8a824722006-11-20 17:07:51 +0100562 */
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800563int kobject_uevent(struct kobject *kobj, enum kobject_action action)
Cornelia Huck8a824722006-11-20 17:07:51 +0100564{
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800565 return kobject_uevent_env(kobj, action, NULL);
Cornelia Huck8a824722006-11-20 17:07:51 +0100566}
Kay Sievers312c0042005-11-16 09:00:00 +0100567EXPORT_SYMBOL_GPL(kobject_uevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569/**
Kay Sievers7eff2e72007-08-14 15:15:12 +0200570 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 *
574 * Returns 0 if environment variable was added successfully or -ENOMEM
575 * if no space was available.
576 */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200577int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
579 va_list args;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200580 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Kay Sievers7eff2e72007-08-14 15:15:12 +0200582 if (env->envp_idx >= ARRAY_SIZE(env->envp)) {
Arjan van de Ven5cd2b452008-07-25 19:45:39 -0700583 WARN(1, KERN_ERR "add_uevent_var: too many keys\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 va_start(args, format);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200588 len = vsnprintf(&env->buf[env->buflen],
589 sizeof(env->buf) - env->buflen,
590 format, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 va_end(args);
592
Kay Sievers7eff2e72007-08-14 15:15:12 +0200593 if (len >= (sizeof(env->buf) - env->buflen)) {
Arjan van de Ven5cd2b452008-07-25 19:45:39 -0700594 WARN(1, KERN_ERR "add_uevent_var: buffer size too small\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Kay Sievers7eff2e72007-08-14 15:15:12 +0200598 env->envp[env->envp_idx++] = &env->buf[env->buflen];
599 env->buflen += len + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 return 0;
601}
Kay Sievers312c0042005-11-16 09:00:00 +0100602EXPORT_SYMBOL_GPL(add_uevent_var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200604#if defined(CONFIG_NET)
Eric W. Biederman07e98962010-05-04 17:36:44 -0700605static int uevent_net_init(struct net *net)
Kay Sievers5f123fb2005-11-11 14:43:07 +0100606{
Eric W. Biederman07e98962010-05-04 17:36:44 -0700607 struct uevent_sock *ue_sk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +0000608 struct netlink_kernel_cfg cfg = {
609 .groups = 1,
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +0000610 .flags = NL_CFG_F_NONROOT_RECV,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +0000611 };
Eric W. Biederman07e98962010-05-04 17:36:44 -0700612
613 ue_sk = kzalloc(sizeof(*ue_sk), GFP_KERNEL);
614 if (!ue_sk)
615 return -ENOMEM;
616
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +0000617 ue_sk->sk = netlink_kernel_create(net, NETLINK_KOBJECT_UEVENT, &cfg);
Eric W. Biederman07e98962010-05-04 17:36:44 -0700618 if (!ue_sk->sk) {
Kay Sievers5f123fb2005-11-11 14:43:07 +0100619 printk(KERN_ERR
620 "kobject_uevent: unable to create netlink socket!\n");
Dan Carpenter743db2d2010-05-25 11:51:10 +0200621 kfree(ue_sk);
Kay Sievers5f123fb2005-11-11 14:43:07 +0100622 return -ENODEV;
623 }
Eric W. Biederman07e98962010-05-04 17:36:44 -0700624 mutex_lock(&uevent_sock_mutex);
625 list_add_tail(&ue_sk->list, &uevent_sock_list);
626 mutex_unlock(&uevent_sock_mutex);
Kay Sievers5f123fb2005-11-11 14:43:07 +0100627 return 0;
628}
629
Eric W. Biederman07e98962010-05-04 17:36:44 -0700630static void uevent_net_exit(struct net *net)
631{
632 struct uevent_sock *ue_sk;
633
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;
638 }
639 mutex_unlock(&uevent_sock_mutex);
640 return;
641
642found:
643 list_del(&ue_sk->list);
644 mutex_unlock(&uevent_sock_mutex);
645
646 netlink_kernel_release(ue_sk->sk);
647 kfree(ue_sk);
648}
649
650static struct pernet_operations uevent_net_ops = {
651 .init = uevent_net_init,
652 .exit = uevent_net_exit,
653};
654
655static int __init kobject_uevent_init(void)
656{
Eric W. Biederman07e98962010-05-04 17:36:44 -0700657 return register_pernet_subsys(&uevent_net_ops);
658}
659
660
Kay Sievers5f123fb2005-11-11 14:43:07 +0100661postcore_initcall(kobject_uevent_init);
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200662#endif