blob: 7b48d44ced6ec2972fdd45cbc2c0d084dcf22f82 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -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>
Denis V. Lunev2d38f9a2008-03-27 14:26:30 -070018#include <linux/string.h>
19#include <linux/kobject.h>
20#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Denis V. Lunev2d38f9a2008-03-27 14:26:30 -070022
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/socket.h>
24#include <linux/skbuff.h>
25#include <linux/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <net/sock.h>
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Cornelia Huckcd030c42007-07-20 13:58:13 +020029u64 uevent_seqnum;
Kay Sievers6a8d8ab2007-08-15 15:38:28 +020030char uevent_helper[UEVENT_HELPER_PATH_LEN] = CONFIG_UEVENT_HELPER_PATH;
Cornelia Huckcd030c42007-07-20 13:58:13 +020031static DEFINE_SPINLOCK(sequence_lock);
32#if defined(CONFIG_NET)
33static struct sock *uevent_sock;
34#endif
35
Kay Sievers5c5daf62007-08-12 20:43:55 +020036/* the strings here must match the enum in include/linux/kobject.h */
37static const char *kobject_actions[] = {
38 [KOBJ_ADD] = "add",
39 [KOBJ_REMOVE] = "remove",
40 [KOBJ_CHANGE] = "change",
41 [KOBJ_MOVE] = "move",
42 [KOBJ_ONLINE] = "online",
43 [KOBJ_OFFLINE] = "offline",
44};
45
46/**
47 * kobject_action_type - translate action string to numeric type
48 *
49 * @buf: buffer containing the action string, newline is ignored
50 * @len: length of buffer
51 * @type: pointer to the location to store the action type
52 *
53 * Returns 0 if the action string was recognized.
54 */
55int kobject_action_type(const char *buf, size_t count,
56 enum kobject_action *type)
57{
58 enum kobject_action action;
59 int ret = -EINVAL;
60
Mark Lorda9edadb2008-03-28 19:05:25 -040061 if (count && (buf[count-1] == '\n' || buf[count-1] == '\0'))
Kay Sievers5c5daf62007-08-12 20:43:55 +020062 count--;
63
64 if (!count)
65 goto out;
66
67 for (action = 0; action < ARRAY_SIZE(kobject_actions); action++) {
68 if (strncmp(kobject_actions[action], buf, count) != 0)
69 continue;
70 if (kobject_actions[action][count] != '\0')
71 continue;
72 *type = action;
73 ret = 0;
74 break;
75 }
76out:
77 return ret;
78}
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/**
Cornelia Huck8a824722006-11-20 17:07:51 +010081 * kobject_uevent_env - send an uevent with environmental data
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 *
Kay Sieversccd490a2007-08-12 20:43:55 +020083 * @action: action that is happening
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 * @kobj: struct kobject that the action is happening to
Cornelia Huck8a824722006-11-20 17:07:51 +010085 * @envp_ext: pointer to environmental data
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -080086 *
87 * Returns 0 if kobject_uevent() is completed with success or the
88 * corresponding error when it fails.
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 */
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -080090int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
Kay Sievers7eff2e72007-08-14 15:15:12 +020091 char *envp_ext[])
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Kay Sievers7eff2e72007-08-14 15:15:12 +020093 struct kobj_uevent_env *env;
94 const char *action_string = kobject_actions[action];
Kay Sievers5f123fb2005-11-11 14:43:07 +010095 const char *devpath = NULL;
96 const char *subsystem;
97 struct kobject *top_kobj;
98 struct kset *kset;
Emese Revfy9cd43612009-12-31 14:52:51 +010099 const struct kset_uevent_ops *uevent_ops;
Kay Sievers5f123fb2005-11-11 14:43:07 +0100100 u64 seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 int i = 0;
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800102 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800104 pr_debug("kobject: '%s' (%p): %s\n",
Harvey Harrison810304d2008-04-30 00:55:08 -0700105 kobject_name(kobj), kobj, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Kay Sievers5f123fb2005-11-11 14:43:07 +0100107 /* search the kset we belong to */
108 top_kobj = kobj;
Kay Sieversccd490a2007-08-12 20:43:55 +0200109 while (!top_kobj->kset && top_kobj->parent)
John Anthony Kazos Jr14193fb2007-04-04 07:39:17 -0400110 top_kobj = top_kobj->parent;
Kay Sieversccd490a2007-08-12 20:43:55 +0200111
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800112 if (!top_kobj->kset) {
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800113 pr_debug("kobject: '%s' (%p): %s: attempted to send uevent "
114 "without kset!\n", kobject_name(kobj), kobj,
Harvey Harrison810304d2008-04-30 00:55:08 -0700115 __func__);
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800116 return -EINVAL;
117 }
Kay Sievers5f123fb2005-11-11 14:43:07 +0100118
119 kset = top_kobj->kset;
Kay Sievers312c0042005-11-16 09:00:00 +0100120 uevent_ops = kset->uevent_ops;
Kay Sievers5f123fb2005-11-11 14:43:07 +0100121
Ming Leif67f1292009-03-01 21:10:49 +0800122 /* skip the event, if uevent_suppress is set*/
123 if (kobj->uevent_suppress) {
124 pr_debug("kobject: '%s' (%p): %s: uevent_suppress "
125 "caused the event to drop!\n",
126 kobject_name(kobj), kobj, __func__);
127 return 0;
128 }
Kay Sievers7eff2e72007-08-14 15:15:12 +0200129 /* skip the event, if the filter returns zero. */
Kay Sievers312c0042005-11-16 09:00:00 +0100130 if (uevent_ops && uevent_ops->filter)
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800131 if (!uevent_ops->filter(kset, kobj)) {
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800132 pr_debug("kobject: '%s' (%p): %s: filter function "
133 "caused the event to drop!\n",
Harvey Harrison810304d2008-04-30 00:55:08 -0700134 kobject_name(kobj), kobj, __func__);
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800135 return 0;
136 }
Kay Sievers5f123fb2005-11-11 14:43:07 +0100137
Kay Sievers86406242007-03-14 03:25:56 +0100138 /* originating subsystem */
139 if (uevent_ops && uevent_ops->name)
140 subsystem = uevent_ops->name(kset, kobj);
141 else
142 subsystem = kobject_name(&kset->kobj);
143 if (!subsystem) {
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800144 pr_debug("kobject: '%s' (%p): %s: unset subsystem caused the "
145 "event to drop!\n", kobject_name(kobj), kobj,
Harvey Harrison810304d2008-04-30 00:55:08 -0700146 __func__);
Kay Sievers86406242007-03-14 03:25:56 +0100147 return 0;
148 }
149
Kay Sievers7eff2e72007-08-14 15:15:12 +0200150 /* environment buffer */
151 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
152 if (!env)
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800153 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Kay Sievers5f123fb2005-11-11 14:43:07 +0100155 /* complete object path */
156 devpath = kobject_get_path(kobj, GFP_KERNEL);
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800157 if (!devpath) {
158 retval = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 goto exit;
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Kay Sievers5f123fb2005-11-11 14:43:07 +0100162 /* default keys */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200163 retval = add_uevent_var(env, "ACTION=%s", action_string);
164 if (retval)
165 goto exit;
166 retval = add_uevent_var(env, "DEVPATH=%s", devpath);
167 if (retval)
168 goto exit;
169 retval = add_uevent_var(env, "SUBSYSTEM=%s", subsystem);
170 if (retval)
171 goto exit;
172
173 /* keys passed in from the caller */
174 if (envp_ext) {
175 for (i = 0; envp_ext[i]; i++) {
Tejun Heoc65b9142008-11-13 13:20:00 +0900176 retval = add_uevent_var(env, "%s", envp_ext[i]);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200177 if (retval)
178 goto exit;
179 }
180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Kay Sievers5f123fb2005-11-11 14:43:07 +0100182 /* let the kset specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100183 if (uevent_ops && uevent_ops->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200184 retval = uevent_ops->uevent(kset, kobj, env);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 if (retval) {
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800186 pr_debug("kobject: '%s' (%p): %s: uevent() returned "
187 "%d\n", kobject_name(kobj), kobj,
Harvey Harrison810304d2008-04-30 00:55:08 -0700188 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 goto exit;
190 }
191 }
192
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100193 /*
194 * Mark "add" and "remove" events in the object to ensure proper
195 * events to userspace during automatic cleanup. If the object did
196 * send an "add" event, "remove" will automatically generated by
197 * the core, if not already done by the caller.
198 */
199 if (action == KOBJ_ADD)
200 kobj->state_add_uevent_sent = 1;
201 else if (action == KOBJ_REMOVE)
202 kobj->state_remove_uevent_sent = 1;
203
Kay Sievers7eff2e72007-08-14 15:15:12 +0200204 /* we will send an event, so request a new sequence number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 spin_lock(&sequence_lock);
Kay Sievers312c0042005-11-16 09:00:00 +0100206 seq = ++uevent_seqnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 spin_unlock(&sequence_lock);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200208 retval = add_uevent_var(env, "SEQNUM=%llu", (unsigned long long)seq);
209 if (retval)
210 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200212#if defined(CONFIG_NET)
Kay Sievers5f123fb2005-11-11 14:43:07 +0100213 /* send netlink message */
214 if (uevent_sock) {
215 struct sk_buff *skb;
216 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Kay Sievers5f123fb2005-11-11 14:43:07 +0100218 /* allocate message with the maximum possible size */
219 len = strlen(action_string) + strlen(devpath) + 2;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200220 skb = alloc_skb(len + env->buflen, GFP_KERNEL);
Kay Sievers5f123fb2005-11-11 14:43:07 +0100221 if (skb) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200222 char *scratch;
223
Kay Sievers5f123fb2005-11-11 14:43:07 +0100224 /* add header */
225 scratch = skb_put(skb, len);
226 sprintf(scratch, "%s@%s", action_string, devpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Kay Sievers5f123fb2005-11-11 14:43:07 +0100228 /* copy keys to our continuous event payload buffer */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200229 for (i = 0; i < env->envp_idx; i++) {
230 len = strlen(env->envp[i]) + 1;
Kay Sievers5f123fb2005-11-11 14:43:07 +0100231 scratch = skb_put(skb, len);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200232 strcpy(scratch, env->envp[i]);
Kay Sievers5f123fb2005-11-11 14:43:07 +0100233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Kay Sievers5f123fb2005-11-11 14:43:07 +0100235 NETLINK_CB(skb).dst_group = 1;
Ming Leie0d7bf52008-11-16 18:23:27 +0800236 retval = netlink_broadcast(uevent_sock, skb, 0, 1,
237 GFP_KERNEL);
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -0800238 /* ENOBUFS should be handled in userspace */
239 if (retval == -ENOBUFS)
240 retval = 0;
Ming Leie0d7bf52008-11-16 18:23:27 +0800241 } else
242 retval = -ENOMEM;
Kay Sievers5f123fb2005-11-11 14:43:07 +0100243 }
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200244#endif
Kay Sievers5f123fb2005-11-11 14:43:07 +0100245
246 /* call uevent_helper, usually only enabled during early boot */
Kay Sievers312c0042005-11-16 09:00:00 +0100247 if (uevent_helper[0]) {
Kay Sievers5f123fb2005-11-11 14:43:07 +0100248 char *argv [3];
249
Kay Sievers312c0042005-11-16 09:00:00 +0100250 argv [0] = uevent_helper;
Kay Sievers5f123fb2005-11-11 14:43:07 +0100251 argv [1] = (char *)subsystem;
252 argv [2] = NULL;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200253 retval = add_uevent_var(env, "HOME=/");
254 if (retval)
255 goto exit;
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800256 retval = add_uevent_var(env,
257 "PATH=/sbin:/bin:/usr/sbin:/usr/bin");
Kay Sievers7eff2e72007-08-14 15:15:12 +0200258 if (retval)
259 goto exit;
260
Wang Chen0ad1d6f2008-06-24 16:59:02 +0800261 retval = call_usermodehelper(argv[0], argv,
Hugh Dickins05f54c12009-04-16 21:55:29 +0100262 env->envp, UMH_WAIT_EXEC);
Kay Sievers5f123fb2005-11-11 14:43:07 +0100263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265exit:
Kay Sievers5f123fb2005-11-11 14:43:07 +0100266 kfree(devpath);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200267 kfree(env);
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800268 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
Cornelia Huck8a824722006-11-20 17:07:51 +0100270EXPORT_SYMBOL_GPL(kobject_uevent_env);
271
272/**
273 * kobject_uevent - notify userspace by ending an uevent
274 *
Kay Sieversccd490a2007-08-12 20:43:55 +0200275 * @action: action that is happening
Cornelia Huck8a824722006-11-20 17:07:51 +0100276 * @kobj: struct kobject that the action is happening to
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800277 *
278 * Returns 0 if kobject_uevent() is completed with success or the
279 * corresponding error when it fails.
Cornelia Huck8a824722006-11-20 17:07:51 +0100280 */
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800281int kobject_uevent(struct kobject *kobj, enum kobject_action action)
Cornelia Huck8a824722006-11-20 17:07:51 +0100282{
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800283 return kobject_uevent_env(kobj, action, NULL);
Cornelia Huck8a824722006-11-20 17:07:51 +0100284}
Kay Sievers312c0042005-11-16 09:00:00 +0100285EXPORT_SYMBOL_GPL(kobject_uevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287/**
Kay Sievers7eff2e72007-08-14 15:15:12 +0200288 * add_uevent_var - add key value string to the environment buffer
289 * @env: environment buffer structure
290 * @format: printf format for the key=value pair
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 *
292 * Returns 0 if environment variable was added successfully or -ENOMEM
293 * if no space was available.
294 */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200295int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
297 va_list args;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200298 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Kay Sievers7eff2e72007-08-14 15:15:12 +0200300 if (env->envp_idx >= ARRAY_SIZE(env->envp)) {
Arjan van de Ven5cd2b452008-07-25 19:45:39 -0700301 WARN(1, KERN_ERR "add_uevent_var: too many keys\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 va_start(args, format);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200306 len = vsnprintf(&env->buf[env->buflen],
307 sizeof(env->buf) - env->buflen,
308 format, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 va_end(args);
310
Kay Sievers7eff2e72007-08-14 15:15:12 +0200311 if (len >= (sizeof(env->buf) - env->buflen)) {
Arjan van de Ven5cd2b452008-07-25 19:45:39 -0700312 WARN(1, KERN_ERR "add_uevent_var: buffer size too small\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Kay Sievers7eff2e72007-08-14 15:15:12 +0200316 env->envp[env->envp_idx++] = &env->buf[env->buflen];
317 env->buflen += len + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return 0;
319}
Kay Sievers312c0042005-11-16 09:00:00 +0100320EXPORT_SYMBOL_GPL(add_uevent_var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200322#if defined(CONFIG_NET)
Kay Sievers5f123fb2005-11-11 14:43:07 +0100323static int __init kobject_uevent_init(void)
324{
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200325 uevent_sock = netlink_kernel_create(&init_net, NETLINK_KOBJECT_UEVENT,
326 1, NULL, NULL, THIS_MODULE);
Kay Sievers5f123fb2005-11-11 14:43:07 +0100327 if (!uevent_sock) {
328 printk(KERN_ERR
329 "kobject_uevent: unable to create netlink socket!\n");
330 return -ENODEV;
331 }
Kay Sieversd094cbe2009-04-03 19:04:15 +0200332 netlink_set_nonroot(NETLINK_KOBJECT_UEVENT, NL_NONROOT_RECV);
Kay Sievers5f123fb2005-11-11 14:43:07 +0100333 return 0;
334}
335
336postcore_initcall(kobject_uevent_init);
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200337#endif