blob: 7d8aeb301635715761f2e1c5b2870f9475727967 [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
Kay Sievers60a96a52007-07-08 22:29:26 +020026/* the strings here must match the enum in include/linux/kobject.h */
27const char *kobject_actions[] = {
28 "add",
29 "remove",
30 "change",
31 "move",
32 "online",
33 "offline",
34};
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Cornelia Huckcd030c42007-07-20 13:58:13 +020036#if defined(CONFIG_HOTPLUG)
37u64 uevent_seqnum;
38char uevent_helper[UEVENT_HELPER_PATH_LEN] = "/sbin/hotplug";
39static DEFINE_SPINLOCK(sequence_lock);
40#if defined(CONFIG_NET)
41static struct sock *uevent_sock;
42#endif
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/**
Cornelia Huck8a824722006-11-20 17:07:51 +010045 * kobject_uevent_env - send an uevent with environmental data
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 *
Cornelia Huck8a824722006-11-20 17:07:51 +010047 * @action: action that is happening (usually KOBJ_MOVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 * @kobj: struct kobject that the action is happening to
Cornelia Huck8a824722006-11-20 17:07:51 +010049 * @envp_ext: pointer to environmental data
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -080050 *
51 * Returns 0 if kobject_uevent() is completed with success or the
52 * corresponding error when it fails.
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 */
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -080054int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
Kay Sievers7eff2e72007-08-14 15:15:12 +020055 char *envp_ext[])
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Kay Sievers7eff2e72007-08-14 15:15:12 +020057 struct kobj_uevent_env *env;
58 const char *action_string = kobject_actions[action];
Kay Sievers5f123fb2005-11-11 14:43:07 +010059 const char *devpath = NULL;
60 const char *subsystem;
61 struct kobject *top_kobj;
62 struct kset *kset;
Kay Sievers312c0042005-11-16 09:00:00 +010063 struct kset_uevent_ops *uevent_ops;
Kay Sievers5f123fb2005-11-11 14:43:07 +010064 u64 seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 int i = 0;
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -080066 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Kay Sievers5f123fb2005-11-11 14:43:07 +010068 pr_debug("%s\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Kay Sievers5f123fb2005-11-11 14:43:07 +010070 /* search the kset we belong to */
71 top_kobj = kobj;
John Anthony Kazos Jr14193fb2007-04-04 07:39:17 -040072 while (!top_kobj->kset && top_kobj->parent) {
73 top_kobj = top_kobj->parent;
Kay Sievers5f123fb2005-11-11 14:43:07 +010074 }
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -080075 if (!top_kobj->kset) {
76 pr_debug("kobject attempted to send uevent without kset!\n");
77 return -EINVAL;
78 }
Kay Sievers5f123fb2005-11-11 14:43:07 +010079
80 kset = top_kobj->kset;
Kay Sievers312c0042005-11-16 09:00:00 +010081 uevent_ops = kset->uevent_ops;
Kay Sievers5f123fb2005-11-11 14:43:07 +010082
Kay Sievers7eff2e72007-08-14 15:15:12 +020083 /* skip the event, if the filter returns zero. */
Kay Sievers312c0042005-11-16 09:00:00 +010084 if (uevent_ops && uevent_ops->filter)
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -080085 if (!uevent_ops->filter(kset, kobj)) {
86 pr_debug("kobject filter function caused the event to drop!\n");
87 return 0;
88 }
Kay Sievers5f123fb2005-11-11 14:43:07 +010089
Kay Sievers86406242007-03-14 03:25:56 +010090 /* originating subsystem */
91 if (uevent_ops && uevent_ops->name)
92 subsystem = uevent_ops->name(kset, kobj);
93 else
94 subsystem = kobject_name(&kset->kobj);
95 if (!subsystem) {
96 pr_debug("unset subsytem caused the event to drop!\n");
97 return 0;
98 }
99
Kay Sievers7eff2e72007-08-14 15:15:12 +0200100 /* environment buffer */
101 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
102 if (!env)
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800103 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Kay Sievers5f123fb2005-11-11 14:43:07 +0100105 /* complete object path */
106 devpath = kobject_get_path(kobj, GFP_KERNEL);
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800107 if (!devpath) {
108 retval = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 goto exit;
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Kay Sievers5f123fb2005-11-11 14:43:07 +0100112 /* default keys */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200113 retval = add_uevent_var(env, "ACTION=%s", action_string);
114 if (retval)
115 goto exit;
116 retval = add_uevent_var(env, "DEVPATH=%s", devpath);
117 if (retval)
118 goto exit;
119 retval = add_uevent_var(env, "SUBSYSTEM=%s", subsystem);
120 if (retval)
121 goto exit;
122
123 /* keys passed in from the caller */
124 if (envp_ext) {
125 for (i = 0; envp_ext[i]; i++) {
126 retval = add_uevent_var(env, envp_ext[i]);
127 if (retval)
128 goto exit;
129 }
130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Kay Sievers5f123fb2005-11-11 14:43:07 +0100132 /* let the kset specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100133 if (uevent_ops && uevent_ops->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200134 retval = uevent_ops->uevent(kset, kobj, env);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 if (retval) {
Kay Sievers312c0042005-11-16 09:00:00 +0100136 pr_debug ("%s - uevent() returned %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 __FUNCTION__, retval);
138 goto exit;
139 }
140 }
141
Kay Sievers7eff2e72007-08-14 15:15:12 +0200142 /* we will send an event, so request a new sequence number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 spin_lock(&sequence_lock);
Kay Sievers312c0042005-11-16 09:00:00 +0100144 seq = ++uevent_seqnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 spin_unlock(&sequence_lock);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200146 retval = add_uevent_var(env, "SEQNUM=%llu", (unsigned long long)seq);
147 if (retval)
148 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200150#if defined(CONFIG_NET)
Kay Sievers5f123fb2005-11-11 14:43:07 +0100151 /* send netlink message */
152 if (uevent_sock) {
153 struct sk_buff *skb;
154 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Kay Sievers5f123fb2005-11-11 14:43:07 +0100156 /* allocate message with the maximum possible size */
157 len = strlen(action_string) + strlen(devpath) + 2;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200158 skb = alloc_skb(len + env->buflen, GFP_KERNEL);
Kay Sievers5f123fb2005-11-11 14:43:07 +0100159 if (skb) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200160 char *scratch;
161
Kay Sievers5f123fb2005-11-11 14:43:07 +0100162 /* add header */
163 scratch = skb_put(skb, len);
164 sprintf(scratch, "%s@%s", action_string, devpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Kay Sievers5f123fb2005-11-11 14:43:07 +0100166 /* copy keys to our continuous event payload buffer */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200167 for (i = 0; i < env->envp_idx; i++) {
168 len = strlen(env->envp[i]) + 1;
Kay Sievers5f123fb2005-11-11 14:43:07 +0100169 scratch = skb_put(skb, len);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200170 strcpy(scratch, env->envp[i]);
Kay Sievers5f123fb2005-11-11 14:43:07 +0100171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Kay Sievers5f123fb2005-11-11 14:43:07 +0100173 NETLINK_CB(skb).dst_group = 1;
174 netlink_broadcast(uevent_sock, skb, 0, 1, GFP_KERNEL);
175 }
176 }
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200177#endif
Kay Sievers5f123fb2005-11-11 14:43:07 +0100178
179 /* call uevent_helper, usually only enabled during early boot */
Kay Sievers312c0042005-11-16 09:00:00 +0100180 if (uevent_helper[0]) {
Kay Sievers5f123fb2005-11-11 14:43:07 +0100181 char *argv [3];
182
Kay Sievers312c0042005-11-16 09:00:00 +0100183 argv [0] = uevent_helper;
Kay Sievers5f123fb2005-11-11 14:43:07 +0100184 argv [1] = (char *)subsystem;
185 argv [2] = NULL;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200186 retval = add_uevent_var(env, "HOME=/");
187 if (retval)
188 goto exit;
189 retval = add_uevent_var(env, "PATH=/sbin:/bin:/usr/sbin:/usr/bin");
190 if (retval)
191 goto exit;
192
193 call_usermodehelper (argv[0], argv, env->envp, UMH_WAIT_EXEC);
Kay Sievers5f123fb2005-11-11 14:43:07 +0100194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196exit:
Kay Sievers5f123fb2005-11-11 14:43:07 +0100197 kfree(devpath);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200198 kfree(env);
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800199 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
Cornelia Huck8a824722006-11-20 17:07:51 +0100201
202EXPORT_SYMBOL_GPL(kobject_uevent_env);
203
204/**
205 * kobject_uevent - notify userspace by ending an uevent
206 *
207 * @action: action that is happening (usually KOBJ_ADD and KOBJ_REMOVE)
208 * @kobj: struct kobject that the action is happening to
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800209 *
210 * Returns 0 if kobject_uevent() is completed with success or the
211 * corresponding error when it fails.
Cornelia Huck8a824722006-11-20 17:07:51 +0100212 */
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800213int kobject_uevent(struct kobject *kobj, enum kobject_action action)
Cornelia Huck8a824722006-11-20 17:07:51 +0100214{
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800215 return kobject_uevent_env(kobj, action, NULL);
Cornelia Huck8a824722006-11-20 17:07:51 +0100216}
217
Kay Sievers312c0042005-11-16 09:00:00 +0100218EXPORT_SYMBOL_GPL(kobject_uevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220/**
Kay Sievers7eff2e72007-08-14 15:15:12 +0200221 * add_uevent_var - add key value string to the environment buffer
222 * @env: environment buffer structure
223 * @format: printf format for the key=value pair
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 *
225 * Returns 0 if environment variable was added successfully or -ENOMEM
226 * if no space was available.
227 */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200228int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230 va_list args;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200231 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Kay Sievers7eff2e72007-08-14 15:15:12 +0200233 if (env->envp_idx >= ARRAY_SIZE(env->envp)) {
234 printk(KERN_ERR "add_uevent_var: too many keys\n");
235 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 va_start(args, format);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200240 len = vsnprintf(&env->buf[env->buflen],
241 sizeof(env->buf) - env->buflen,
242 format, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 va_end(args);
244
Kay Sievers7eff2e72007-08-14 15:15:12 +0200245 if (len >= (sizeof(env->buf) - env->buflen)) {
246 printk(KERN_ERR "add_uevent_var: buffer size too small\n");
247 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Kay Sievers7eff2e72007-08-14 15:15:12 +0200251 env->envp[env->envp_idx++] = &env->buf[env->buflen];
252 env->buflen += len + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return 0;
254}
Kay Sievers312c0042005-11-16 09:00:00 +0100255EXPORT_SYMBOL_GPL(add_uevent_var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200257#if defined(CONFIG_NET)
Kay Sievers5f123fb2005-11-11 14:43:07 +0100258static int __init kobject_uevent_init(void)
259{
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200260 uevent_sock = netlink_kernel_create(&init_net, NETLINK_KOBJECT_UEVENT,
261 1, NULL, NULL, THIS_MODULE);
Kay Sievers5f123fb2005-11-11 14:43:07 +0100262 if (!uevent_sock) {
263 printk(KERN_ERR
264 "kobject_uevent: unable to create netlink socket!\n");
265 return -ENODEV;
266 }
267
268 return 0;
269}
270
271postcore_initcall(kobject_uevent_init);
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200272#endif
Kay Sievers5f123fb2005-11-11 14:43:07 +0100273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274#endif /* CONFIG_HOTPLUG */