blob: 0d56dad319ad33b8354c1396866158a6a0de4c91 [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>
21
22#ifdef CONFIG_NET
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>
Denis V. Lunev2d38f9a2008-03-27 14:26:30 -070027#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Cornelia Huckcd030c42007-07-20 13:58:13 +020030u64 uevent_seqnum;
Kay Sievers6a8d8ab2007-08-15 15:38:28 +020031char uevent_helper[UEVENT_HELPER_PATH_LEN] = CONFIG_UEVENT_HELPER_PATH;
Cornelia Huckcd030c42007-07-20 13:58:13 +020032static DEFINE_SPINLOCK(sequence_lock);
33#if defined(CONFIG_NET)
34static struct sock *uevent_sock;
35#endif
36
Kay Sievers5c5daf62007-08-12 20:43:55 +020037/* the strings here must match the enum in include/linux/kobject.h */
38static const char *kobject_actions[] = {
39 [KOBJ_ADD] = "add",
40 [KOBJ_REMOVE] = "remove",
41 [KOBJ_CHANGE] = "change",
42 [KOBJ_MOVE] = "move",
43 [KOBJ_ONLINE] = "online",
44 [KOBJ_OFFLINE] = "offline",
45};
46
47/**
48 * kobject_action_type - translate action string to numeric type
49 *
50 * @buf: buffer containing the action string, newline is ignored
51 * @len: length of buffer
52 * @type: pointer to the location to store the action type
53 *
54 * Returns 0 if the action string was recognized.
55 */
56int kobject_action_type(const char *buf, size_t count,
57 enum kobject_action *type)
58{
59 enum kobject_action action;
60 int ret = -EINVAL;
61
62 if (count && buf[count-1] == '\n')
63 count--;
64
65 if (!count)
66 goto out;
67
68 for (action = 0; action < ARRAY_SIZE(kobject_actions); action++) {
69 if (strncmp(kobject_actions[action], buf, count) != 0)
70 continue;
71 if (kobject_actions[action][count] != '\0')
72 continue;
73 *type = action;
74 ret = 0;
75 break;
76 }
77out:
78 return ret;
79}
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081/**
Cornelia Huck8a824722006-11-20 17:07:51 +010082 * kobject_uevent_env - send an uevent with environmental data
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 *
Kay Sieversccd490a2007-08-12 20:43:55 +020084 * @action: action that is happening
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 * @kobj: struct kobject that the action is happening to
Cornelia Huck8a824722006-11-20 17:07:51 +010086 * @envp_ext: pointer to environmental data
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -080087 *
88 * Returns 0 if kobject_uevent() is completed with success or the
89 * corresponding error when it fails.
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 */
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -080091int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
Kay Sievers7eff2e72007-08-14 15:15:12 +020092 char *envp_ext[])
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Kay Sievers7eff2e72007-08-14 15:15:12 +020094 struct kobj_uevent_env *env;
95 const char *action_string = kobject_actions[action];
Kay Sievers5f123fb2005-11-11 14:43:07 +010096 const char *devpath = NULL;
97 const char *subsystem;
98 struct kobject *top_kobj;
99 struct kset *kset;
Kay Sievers312c0042005-11-16 09:00:00 +0100100 struct kset_uevent_ops *uevent_ops;
Kay Sievers5f123fb2005-11-11 14:43:07 +0100101 u64 seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 int i = 0;
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800103 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800105 pr_debug("kobject: '%s' (%p): %s\n",
106 kobject_name(kobj), kobj, __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Kay Sievers5f123fb2005-11-11 14:43:07 +0100108 /* search the kset we belong to */
109 top_kobj = kobj;
Kay Sieversccd490a2007-08-12 20:43:55 +0200110 while (!top_kobj->kset && top_kobj->parent)
John Anthony Kazos Jr14193fb2007-04-04 07:39:17 -0400111 top_kobj = top_kobj->parent;
Kay Sieversccd490a2007-08-12 20:43:55 +0200112
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800113 if (!top_kobj->kset) {
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800114 pr_debug("kobject: '%s' (%p): %s: attempted to send uevent "
115 "without kset!\n", kobject_name(kobj), kobj,
116 __FUNCTION__);
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800117 return -EINVAL;
118 }
Kay Sievers5f123fb2005-11-11 14:43:07 +0100119
120 kset = top_kobj->kset;
Kay Sievers312c0042005-11-16 09:00:00 +0100121 uevent_ops = kset->uevent_ops;
Kay Sievers5f123fb2005-11-11 14:43:07 +0100122
Kay Sievers7eff2e72007-08-14 15:15:12 +0200123 /* skip the event, if the filter returns zero. */
Kay Sievers312c0042005-11-16 09:00:00 +0100124 if (uevent_ops && uevent_ops->filter)
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800125 if (!uevent_ops->filter(kset, kobj)) {
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800126 pr_debug("kobject: '%s' (%p): %s: filter function "
127 "caused the event to drop!\n",
128 kobject_name(kobj), kobj, __FUNCTION__);
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800129 return 0;
130 }
Kay Sievers5f123fb2005-11-11 14:43:07 +0100131
Kay Sievers86406242007-03-14 03:25:56 +0100132 /* originating subsystem */
133 if (uevent_ops && uevent_ops->name)
134 subsystem = uevent_ops->name(kset, kobj);
135 else
136 subsystem = kobject_name(&kset->kobj);
137 if (!subsystem) {
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800138 pr_debug("kobject: '%s' (%p): %s: unset subsystem caused the "
139 "event to drop!\n", kobject_name(kobj), kobj,
140 __FUNCTION__);
Kay Sievers86406242007-03-14 03:25:56 +0100141 return 0;
142 }
143
Kay Sievers7eff2e72007-08-14 15:15:12 +0200144 /* environment buffer */
145 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
146 if (!env)
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800147 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Kay Sievers5f123fb2005-11-11 14:43:07 +0100149 /* complete object path */
150 devpath = kobject_get_path(kobj, GFP_KERNEL);
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800151 if (!devpath) {
152 retval = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 goto exit;
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Kay Sievers5f123fb2005-11-11 14:43:07 +0100156 /* default keys */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200157 retval = add_uevent_var(env, "ACTION=%s", action_string);
158 if (retval)
159 goto exit;
160 retval = add_uevent_var(env, "DEVPATH=%s", devpath);
161 if (retval)
162 goto exit;
163 retval = add_uevent_var(env, "SUBSYSTEM=%s", subsystem);
164 if (retval)
165 goto exit;
166
167 /* keys passed in from the caller */
168 if (envp_ext) {
169 for (i = 0; envp_ext[i]; i++) {
170 retval = add_uevent_var(env, envp_ext[i]);
171 if (retval)
172 goto exit;
173 }
174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Kay Sievers5f123fb2005-11-11 14:43:07 +0100176 /* let the kset specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100177 if (uevent_ops && uevent_ops->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200178 retval = uevent_ops->uevent(kset, kobj, env);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 if (retval) {
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800180 pr_debug("kobject: '%s' (%p): %s: uevent() returned "
181 "%d\n", kobject_name(kobj), kobj,
182 __FUNCTION__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 goto exit;
184 }
185 }
186
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100187 /*
188 * Mark "add" and "remove" events in the object to ensure proper
189 * events to userspace during automatic cleanup. If the object did
190 * send an "add" event, "remove" will automatically generated by
191 * the core, if not already done by the caller.
192 */
193 if (action == KOBJ_ADD)
194 kobj->state_add_uevent_sent = 1;
195 else if (action == KOBJ_REMOVE)
196 kobj->state_remove_uevent_sent = 1;
197
Kay Sievers7eff2e72007-08-14 15:15:12 +0200198 /* we will send an event, so request a new sequence number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 spin_lock(&sequence_lock);
Kay Sievers312c0042005-11-16 09:00:00 +0100200 seq = ++uevent_seqnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 spin_unlock(&sequence_lock);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200202 retval = add_uevent_var(env, "SEQNUM=%llu", (unsigned long long)seq);
203 if (retval)
204 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200206#if defined(CONFIG_NET)
Kay Sievers5f123fb2005-11-11 14:43:07 +0100207 /* send netlink message */
208 if (uevent_sock) {
209 struct sk_buff *skb;
210 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Kay Sievers5f123fb2005-11-11 14:43:07 +0100212 /* allocate message with the maximum possible size */
213 len = strlen(action_string) + strlen(devpath) + 2;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200214 skb = alloc_skb(len + env->buflen, GFP_KERNEL);
Kay Sievers5f123fb2005-11-11 14:43:07 +0100215 if (skb) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200216 char *scratch;
217
Kay Sievers5f123fb2005-11-11 14:43:07 +0100218 /* add header */
219 scratch = skb_put(skb, len);
220 sprintf(scratch, "%s@%s", action_string, devpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Kay Sievers5f123fb2005-11-11 14:43:07 +0100222 /* copy keys to our continuous event payload buffer */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200223 for (i = 0; i < env->envp_idx; i++) {
224 len = strlen(env->envp[i]) + 1;
Kay Sievers5f123fb2005-11-11 14:43:07 +0100225 scratch = skb_put(skb, len);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200226 strcpy(scratch, env->envp[i]);
Kay Sievers5f123fb2005-11-11 14:43:07 +0100227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Kay Sievers5f123fb2005-11-11 14:43:07 +0100229 NETLINK_CB(skb).dst_group = 1;
230 netlink_broadcast(uevent_sock, skb, 0, 1, GFP_KERNEL);
231 }
232 }
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200233#endif
Kay Sievers5f123fb2005-11-11 14:43:07 +0100234
235 /* call uevent_helper, usually only enabled during early boot */
Kay Sievers312c0042005-11-16 09:00:00 +0100236 if (uevent_helper[0]) {
Kay Sievers5f123fb2005-11-11 14:43:07 +0100237 char *argv [3];
238
Kay Sievers312c0042005-11-16 09:00:00 +0100239 argv [0] = uevent_helper;
Kay Sievers5f123fb2005-11-11 14:43:07 +0100240 argv [1] = (char *)subsystem;
241 argv [2] = NULL;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200242 retval = add_uevent_var(env, "HOME=/");
243 if (retval)
244 goto exit;
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800245 retval = add_uevent_var(env,
246 "PATH=/sbin:/bin:/usr/sbin:/usr/bin");
Kay Sievers7eff2e72007-08-14 15:15:12 +0200247 if (retval)
248 goto exit;
249
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800250 call_usermodehelper(argv[0], argv, env->envp, UMH_WAIT_EXEC);
Kay Sievers5f123fb2005-11-11 14:43:07 +0100251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253exit:
Kay Sievers5f123fb2005-11-11 14:43:07 +0100254 kfree(devpath);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200255 kfree(env);
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800256 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
Cornelia Huck8a824722006-11-20 17:07:51 +0100258EXPORT_SYMBOL_GPL(kobject_uevent_env);
259
260/**
261 * kobject_uevent - notify userspace by ending an uevent
262 *
Kay Sieversccd490a2007-08-12 20:43:55 +0200263 * @action: action that is happening
Cornelia Huck8a824722006-11-20 17:07:51 +0100264 * @kobj: struct kobject that the action is happening to
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800265 *
266 * Returns 0 if kobject_uevent() is completed with success or the
267 * corresponding error when it fails.
Cornelia Huck8a824722006-11-20 17:07:51 +0100268 */
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800269int kobject_uevent(struct kobject *kobj, enum kobject_action action)
Cornelia Huck8a824722006-11-20 17:07:51 +0100270{
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800271 return kobject_uevent_env(kobj, action, NULL);
Cornelia Huck8a824722006-11-20 17:07:51 +0100272}
Kay Sievers312c0042005-11-16 09:00:00 +0100273EXPORT_SYMBOL_GPL(kobject_uevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275/**
Kay Sievers7eff2e72007-08-14 15:15:12 +0200276 * add_uevent_var - add key value string to the environment buffer
277 * @env: environment buffer structure
278 * @format: printf format for the key=value pair
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 *
280 * Returns 0 if environment variable was added successfully or -ENOMEM
281 * if no space was available.
282 */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200283int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
285 va_list args;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200286 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Kay Sievers7eff2e72007-08-14 15:15:12 +0200288 if (env->envp_idx >= ARRAY_SIZE(env->envp)) {
289 printk(KERN_ERR "add_uevent_var: too many keys\n");
290 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 va_start(args, format);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200295 len = vsnprintf(&env->buf[env->buflen],
296 sizeof(env->buf) - env->buflen,
297 format, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 va_end(args);
299
Kay Sievers7eff2e72007-08-14 15:15:12 +0200300 if (len >= (sizeof(env->buf) - env->buflen)) {
301 printk(KERN_ERR "add_uevent_var: buffer size too small\n");
302 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Kay Sievers7eff2e72007-08-14 15:15:12 +0200306 env->envp[env->envp_idx++] = &env->buf[env->buflen];
307 env->buflen += len + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 return 0;
309}
Kay Sievers312c0042005-11-16 09:00:00 +0100310EXPORT_SYMBOL_GPL(add_uevent_var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200312#if defined(CONFIG_NET)
Kay Sievers5f123fb2005-11-11 14:43:07 +0100313static int __init kobject_uevent_init(void)
314{
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200315 uevent_sock = netlink_kernel_create(&init_net, NETLINK_KOBJECT_UEVENT,
316 1, NULL, NULL, THIS_MODULE);
Kay Sievers5f123fb2005-11-11 14:43:07 +0100317 if (!uevent_sock) {
318 printk(KERN_ERR
319 "kobject_uevent: unable to create netlink socket!\n");
320 return -ENODEV;
321 }
322
323 return 0;
324}
325
326postcore_initcall(kobject_uevent_init);
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200327#endif