blob: 5a402e2982afb4a1a82a28092260bb66bd921269 [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>
18#include <linux/socket.h>
19#include <linux/skbuff.h>
20#include <linux/netlink.h>
21#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/kobject.h>
23#include <net/sock.h>
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Cornelia Huckcd030c42007-07-20 13:58:13 +020026u64 uevent_seqnum;
Kay Sievers6a8d8ab2007-08-15 15:38:28 +020027char uevent_helper[UEVENT_HELPER_PATH_LEN] = CONFIG_UEVENT_HELPER_PATH;
Cornelia Huckcd030c42007-07-20 13:58:13 +020028static DEFINE_SPINLOCK(sequence_lock);
29#if defined(CONFIG_NET)
30static struct sock *uevent_sock;
31#endif
32
Kay Sievers5c5daf62007-08-12 20:43:55 +020033/* the strings here must match the enum in include/linux/kobject.h */
34static const char *kobject_actions[] = {
35 [KOBJ_ADD] = "add",
36 [KOBJ_REMOVE] = "remove",
37 [KOBJ_CHANGE] = "change",
38 [KOBJ_MOVE] = "move",
39 [KOBJ_ONLINE] = "online",
40 [KOBJ_OFFLINE] = "offline",
41};
42
43/**
44 * kobject_action_type - translate action string to numeric type
45 *
46 * @buf: buffer containing the action string, newline is ignored
47 * @len: length of buffer
48 * @type: pointer to the location to store the action type
49 *
50 * Returns 0 if the action string was recognized.
51 */
52int kobject_action_type(const char *buf, size_t count,
53 enum kobject_action *type)
54{
55 enum kobject_action action;
56 int ret = -EINVAL;
57
58 if (count && buf[count-1] == '\n')
59 count--;
60
61 if (!count)
62 goto out;
63
64 for (action = 0; action < ARRAY_SIZE(kobject_actions); action++) {
65 if (strncmp(kobject_actions[action], buf, count) != 0)
66 continue;
67 if (kobject_actions[action][count] != '\0')
68 continue;
69 *type = action;
70 ret = 0;
71 break;
72 }
73out:
74 return ret;
75}
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/**
Cornelia Huck8a824722006-11-20 17:07:51 +010078 * kobject_uevent_env - send an uevent with environmental data
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 *
Kay Sieversccd490a2007-08-12 20:43:55 +020080 * @action: action that is happening
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 * @kobj: struct kobject that the action is happening to
Cornelia Huck8a824722006-11-20 17:07:51 +010082 * @envp_ext: pointer to environmental data
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -080083 *
84 * Returns 0 if kobject_uevent() is completed with success or the
85 * corresponding error when it fails.
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 */
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -080087int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
Kay Sievers7eff2e72007-08-14 15:15:12 +020088 char *envp_ext[])
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Kay Sievers7eff2e72007-08-14 15:15:12 +020090 struct kobj_uevent_env *env;
91 const char *action_string = kobject_actions[action];
Kay Sievers5f123fb2005-11-11 14:43:07 +010092 const char *devpath = NULL;
93 const char *subsystem;
94 struct kobject *top_kobj;
95 struct kset *kset;
Kay Sievers312c0042005-11-16 09:00:00 +010096 struct kset_uevent_ops *uevent_ops;
Kay Sievers5f123fb2005-11-11 14:43:07 +010097 u64 seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 int i = 0;
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -080099 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800101 pr_debug("kobject: '%s' (%p): %s\n",
102 kobject_name(kobj), kobj, __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Kay Sievers5f123fb2005-11-11 14:43:07 +0100104 /* search the kset we belong to */
105 top_kobj = kobj;
Kay Sieversccd490a2007-08-12 20:43:55 +0200106 while (!top_kobj->kset && top_kobj->parent)
John Anthony Kazos Jr14193fb2007-04-04 07:39:17 -0400107 top_kobj = top_kobj->parent;
Kay Sieversccd490a2007-08-12 20:43:55 +0200108
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800109 if (!top_kobj->kset) {
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800110 pr_debug("kobject: '%s' (%p): %s: attempted to send uevent "
111 "without kset!\n", kobject_name(kobj), kobj,
112 __FUNCTION__);
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800113 return -EINVAL;
114 }
Kay Sievers5f123fb2005-11-11 14:43:07 +0100115
116 kset = top_kobj->kset;
Kay Sievers312c0042005-11-16 09:00:00 +0100117 uevent_ops = kset->uevent_ops;
Kay Sievers5f123fb2005-11-11 14:43:07 +0100118
Kay Sievers7eff2e72007-08-14 15:15:12 +0200119 /* skip the event, if the filter returns zero. */
Kay Sievers312c0042005-11-16 09:00:00 +0100120 if (uevent_ops && uevent_ops->filter)
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800121 if (!uevent_ops->filter(kset, kobj)) {
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800122 pr_debug("kobject: '%s' (%p): %s: filter function "
123 "caused the event to drop!\n",
124 kobject_name(kobj), kobj, __FUNCTION__);
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800125 return 0;
126 }
Kay Sievers5f123fb2005-11-11 14:43:07 +0100127
Kay Sievers86406242007-03-14 03:25:56 +0100128 /* originating subsystem */
129 if (uevent_ops && uevent_ops->name)
130 subsystem = uevent_ops->name(kset, kobj);
131 else
132 subsystem = kobject_name(&kset->kobj);
133 if (!subsystem) {
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800134 pr_debug("kobject: '%s' (%p): %s: unset subsystem caused the "
135 "event to drop!\n", kobject_name(kobj), kobj,
136 __FUNCTION__);
Kay Sievers86406242007-03-14 03:25:56 +0100137 return 0;
138 }
139
Kay Sievers7eff2e72007-08-14 15:15:12 +0200140 /* environment buffer */
141 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
142 if (!env)
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800143 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Kay Sievers5f123fb2005-11-11 14:43:07 +0100145 /* complete object path */
146 devpath = kobject_get_path(kobj, GFP_KERNEL);
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800147 if (!devpath) {
148 retval = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 goto exit;
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Kay Sievers5f123fb2005-11-11 14:43:07 +0100152 /* default keys */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200153 retval = add_uevent_var(env, "ACTION=%s", action_string);
154 if (retval)
155 goto exit;
156 retval = add_uevent_var(env, "DEVPATH=%s", devpath);
157 if (retval)
158 goto exit;
159 retval = add_uevent_var(env, "SUBSYSTEM=%s", subsystem);
160 if (retval)
161 goto exit;
162
163 /* keys passed in from the caller */
164 if (envp_ext) {
165 for (i = 0; envp_ext[i]; i++) {
166 retval = add_uevent_var(env, envp_ext[i]);
167 if (retval)
168 goto exit;
169 }
170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Kay Sievers5f123fb2005-11-11 14:43:07 +0100172 /* let the kset specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100173 if (uevent_ops && uevent_ops->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200174 retval = uevent_ops->uevent(kset, kobj, env);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 if (retval) {
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800176 pr_debug("kobject: '%s' (%p): %s: uevent() returned "
177 "%d\n", kobject_name(kobj), kobj,
178 __FUNCTION__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 goto exit;
180 }
181 }
182
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100183 /*
184 * Mark "add" and "remove" events in the object to ensure proper
185 * events to userspace during automatic cleanup. If the object did
186 * send an "add" event, "remove" will automatically generated by
187 * the core, if not already done by the caller.
188 */
189 if (action == KOBJ_ADD)
190 kobj->state_add_uevent_sent = 1;
191 else if (action == KOBJ_REMOVE)
192 kobj->state_remove_uevent_sent = 1;
193
Kay Sievers7eff2e72007-08-14 15:15:12 +0200194 /* we will send an event, so request a new sequence number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 spin_lock(&sequence_lock);
Kay Sievers312c0042005-11-16 09:00:00 +0100196 seq = ++uevent_seqnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 spin_unlock(&sequence_lock);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200198 retval = add_uevent_var(env, "SEQNUM=%llu", (unsigned long long)seq);
199 if (retval)
200 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200202#if defined(CONFIG_NET)
Kay Sievers5f123fb2005-11-11 14:43:07 +0100203 /* send netlink message */
204 if (uevent_sock) {
205 struct sk_buff *skb;
206 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Kay Sievers5f123fb2005-11-11 14:43:07 +0100208 /* allocate message with the maximum possible size */
209 len = strlen(action_string) + strlen(devpath) + 2;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200210 skb = alloc_skb(len + env->buflen, GFP_KERNEL);
Kay Sievers5f123fb2005-11-11 14:43:07 +0100211 if (skb) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200212 char *scratch;
213
Kay Sievers5f123fb2005-11-11 14:43:07 +0100214 /* add header */
215 scratch = skb_put(skb, len);
216 sprintf(scratch, "%s@%s", action_string, devpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Kay Sievers5f123fb2005-11-11 14:43:07 +0100218 /* copy keys to our continuous event payload buffer */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200219 for (i = 0; i < env->envp_idx; i++) {
220 len = strlen(env->envp[i]) + 1;
Kay Sievers5f123fb2005-11-11 14:43:07 +0100221 scratch = skb_put(skb, len);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200222 strcpy(scratch, env->envp[i]);
Kay Sievers5f123fb2005-11-11 14:43:07 +0100223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Kay Sievers5f123fb2005-11-11 14:43:07 +0100225 NETLINK_CB(skb).dst_group = 1;
226 netlink_broadcast(uevent_sock, skb, 0, 1, GFP_KERNEL);
227 }
228 }
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200229#endif
Kay Sievers5f123fb2005-11-11 14:43:07 +0100230
231 /* call uevent_helper, usually only enabled during early boot */
Kay Sievers312c0042005-11-16 09:00:00 +0100232 if (uevent_helper[0]) {
Kay Sievers5f123fb2005-11-11 14:43:07 +0100233 char *argv [3];
234
Kay Sievers312c0042005-11-16 09:00:00 +0100235 argv [0] = uevent_helper;
Kay Sievers5f123fb2005-11-11 14:43:07 +0100236 argv [1] = (char *)subsystem;
237 argv [2] = NULL;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200238 retval = add_uevent_var(env, "HOME=/");
239 if (retval)
240 goto exit;
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800241 retval = add_uevent_var(env,
242 "PATH=/sbin:/bin:/usr/sbin:/usr/bin");
Kay Sievers7eff2e72007-08-14 15:15:12 +0200243 if (retval)
244 goto exit;
245
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800246 call_usermodehelper(argv[0], argv, env->envp, UMH_WAIT_EXEC);
Kay Sievers5f123fb2005-11-11 14:43:07 +0100247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249exit:
Kay Sievers5f123fb2005-11-11 14:43:07 +0100250 kfree(devpath);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200251 kfree(env);
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800252 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
Cornelia Huck8a824722006-11-20 17:07:51 +0100254EXPORT_SYMBOL_GPL(kobject_uevent_env);
255
256/**
257 * kobject_uevent - notify userspace by ending an uevent
258 *
Kay Sieversccd490a2007-08-12 20:43:55 +0200259 * @action: action that is happening
Cornelia Huck8a824722006-11-20 17:07:51 +0100260 * @kobj: struct kobject that the action is happening to
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800261 *
262 * Returns 0 if kobject_uevent() is completed with success or the
263 * corresponding error when it fails.
Cornelia Huck8a824722006-11-20 17:07:51 +0100264 */
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800265int kobject_uevent(struct kobject *kobj, enum kobject_action action)
Cornelia Huck8a824722006-11-20 17:07:51 +0100266{
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800267 return kobject_uevent_env(kobj, action, NULL);
Cornelia Huck8a824722006-11-20 17:07:51 +0100268}
Kay Sievers312c0042005-11-16 09:00:00 +0100269EXPORT_SYMBOL_GPL(kobject_uevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271/**
Kay Sievers7eff2e72007-08-14 15:15:12 +0200272 * add_uevent_var - add key value string to the environment buffer
273 * @env: environment buffer structure
274 * @format: printf format for the key=value pair
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 *
276 * Returns 0 if environment variable was added successfully or -ENOMEM
277 * if no space was available.
278 */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200279int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
281 va_list args;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200282 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Kay Sievers7eff2e72007-08-14 15:15:12 +0200284 if (env->envp_idx >= ARRAY_SIZE(env->envp)) {
285 printk(KERN_ERR "add_uevent_var: too many keys\n");
286 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 va_start(args, format);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200291 len = vsnprintf(&env->buf[env->buflen],
292 sizeof(env->buf) - env->buflen,
293 format, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 va_end(args);
295
Kay Sievers7eff2e72007-08-14 15:15:12 +0200296 if (len >= (sizeof(env->buf) - env->buflen)) {
297 printk(KERN_ERR "add_uevent_var: buffer size too small\n");
298 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Kay Sievers7eff2e72007-08-14 15:15:12 +0200302 env->envp[env->envp_idx++] = &env->buf[env->buflen];
303 env->buflen += len + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 return 0;
305}
Kay Sievers312c0042005-11-16 09:00:00 +0100306EXPORT_SYMBOL_GPL(add_uevent_var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200308#if defined(CONFIG_NET)
Kay Sievers5f123fb2005-11-11 14:43:07 +0100309static int __init kobject_uevent_init(void)
310{
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200311 uevent_sock = netlink_kernel_create(&init_net, NETLINK_KOBJECT_UEVENT,
312 1, NULL, NULL, THIS_MODULE);
Kay Sievers5f123fb2005-11-11 14:43:07 +0100313 if (!uevent_sock) {
314 printk(KERN_ERR
315 "kobject_uevent: unable to create netlink socket!\n");
316 return -ENODEV;
317 }
318
319 return 0;
320}
321
322postcore_initcall(kobject_uevent_init);
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200323#endif