blob: e06a8dcec0f04ce9e566901be3fa52c786a74e41 [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
Benjamin Herrenschmidtd87499e2006-01-25 10:21:32 +110025#define BUFFER_SIZE 2048 /* buffer for the variables */
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#define NUM_ENVP 32 /* number of env pointers */
27
Kay Sievers60a96a52007-07-08 22:29:26 +020028/* the strings here must match the enum in include/linux/kobject.h */
29const char *kobject_actions[] = {
30 "add",
31 "remove",
32 "change",
33 "move",
34 "online",
35 "offline",
36};
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Cornelia Huckcd030c42007-07-20 13:58:13 +020038#if defined(CONFIG_HOTPLUG)
39u64 uevent_seqnum;
40char uevent_helper[UEVENT_HELPER_PATH_LEN] = "/sbin/hotplug";
41static DEFINE_SPINLOCK(sequence_lock);
42#if defined(CONFIG_NET)
43static struct sock *uevent_sock;
44#endif
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/**
Cornelia Huck8a824722006-11-20 17:07:51 +010047 * kobject_uevent_env - send an uevent with environmental data
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 *
Cornelia Huck8a824722006-11-20 17:07:51 +010049 * @action: action that is happening (usually KOBJ_MOVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 * @kobj: struct kobject that the action is happening to
Cornelia Huck8a824722006-11-20 17:07:51 +010051 * @envp_ext: pointer to environmental data
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -080052 *
53 * Returns 0 if kobject_uevent() is completed with success or the
54 * corresponding error when it fails.
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 */
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -080056int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
Cornelia Huck8a824722006-11-20 17:07:51 +010057 char *envp_ext[])
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
Kay Sievers5f123fb2005-11-11 14:43:07 +010059 char **envp;
60 char *buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 char *scratch;
Kay Sievers5f123fb2005-11-11 14:43:07 +010062 const char *action_string;
63 const char *devpath = NULL;
64 const char *subsystem;
65 struct kobject *top_kobj;
66 struct kset *kset;
Kay Sievers312c0042005-11-16 09:00:00 +010067 struct kset_uevent_ops *uevent_ops;
Kay Sievers5f123fb2005-11-11 14:43:07 +010068 u64 seq;
69 char *seq_buff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 int i = 0;
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -080071 int retval = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +010072 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Kay Sievers5f123fb2005-11-11 14:43:07 +010074 pr_debug("%s\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Kay Sievers60a96a52007-07-08 22:29:26 +020076 action_string = kobject_actions[action];
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -080077 if (!action_string) {
78 pr_debug("kobject attempted to send uevent without action_string!\n");
79 return -EINVAL;
80 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Kay Sievers5f123fb2005-11-11 14:43:07 +010082 /* search the kset we belong to */
83 top_kobj = kobj;
John Anthony Kazos Jr14193fb2007-04-04 07:39:17 -040084 while (!top_kobj->kset && top_kobj->parent) {
85 top_kobj = top_kobj->parent;
Kay Sievers5f123fb2005-11-11 14:43:07 +010086 }
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -080087 if (!top_kobj->kset) {
88 pr_debug("kobject attempted to send uevent without kset!\n");
89 return -EINVAL;
90 }
Kay Sievers5f123fb2005-11-11 14:43:07 +010091
92 kset = top_kobj->kset;
Kay Sievers312c0042005-11-16 09:00:00 +010093 uevent_ops = kset->uevent_ops;
Kay Sievers5f123fb2005-11-11 14:43:07 +010094
95 /* skip the event, if the filter returns zero. */
Kay Sievers312c0042005-11-16 09:00:00 +010096 if (uevent_ops && uevent_ops->filter)
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -080097 if (!uevent_ops->filter(kset, kobj)) {
98 pr_debug("kobject filter function caused the event to drop!\n");
99 return 0;
100 }
Kay Sievers5f123fb2005-11-11 14:43:07 +0100101
Kay Sievers86406242007-03-14 03:25:56 +0100102 /* originating subsystem */
103 if (uevent_ops && uevent_ops->name)
104 subsystem = uevent_ops->name(kset, kobj);
105 else
106 subsystem = kobject_name(&kset->kobj);
107 if (!subsystem) {
108 pr_debug("unset subsytem caused the event to drop!\n");
109 return 0;
110 }
111
Kay Sievers5f123fb2005-11-11 14:43:07 +0100112 /* environment index */
113 envp = kzalloc(NUM_ENVP * sizeof (char *), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 if (!envp)
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800115 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Kay Sievers5f123fb2005-11-11 14:43:07 +0100117 /* environment values */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800119 if (!buffer) {
120 retval = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 goto exit;
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Kay Sievers5f123fb2005-11-11 14:43:07 +0100124 /* complete object path */
125 devpath = kobject_get_path(kobj, GFP_KERNEL);
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800126 if (!devpath) {
127 retval = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 goto exit;
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Kay Sievers5f123fb2005-11-11 14:43:07 +0100131 /* event environemnt for helper process only */
132 envp[i++] = "HOME=/";
133 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Kay Sievers5f123fb2005-11-11 14:43:07 +0100135 /* default keys */
136 scratch = buffer;
137 envp [i++] = scratch;
138 scratch += sprintf(scratch, "ACTION=%s", action_string) + 1;
139 envp [i++] = scratch;
140 scratch += sprintf (scratch, "DEVPATH=%s", devpath) + 1;
141 envp [i++] = scratch;
142 scratch += sprintf(scratch, "SUBSYSTEM=%s", subsystem) + 1;
Cornelia Huck8a824722006-11-20 17:07:51 +0100143 for (j = 0; envp_ext && envp_ext[j]; j++)
144 envp[i++] = envp_ext[j];
Kay Sievers5f123fb2005-11-11 14:43:07 +0100145 /* just reserve the space, overwrite it after kset call has returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 envp[i++] = seq_buff = scratch;
147 scratch += strlen("SEQNUM=18446744073709551616") + 1;
148
Kay Sievers5f123fb2005-11-11 14:43:07 +0100149 /* let the kset specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100150 if (uevent_ops && uevent_ops->uevent) {
151 retval = uevent_ops->uevent(kset, kobj,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 &envp[i], NUM_ENVP - i, scratch,
153 BUFFER_SIZE - (scratch - buffer));
154 if (retval) {
Kay Sievers312c0042005-11-16 09:00:00 +0100155 pr_debug ("%s - uevent() returned %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 __FUNCTION__, retval);
157 goto exit;
158 }
159 }
160
Kay Sievers5f123fb2005-11-11 14:43:07 +0100161 /* we will send an event, request a new sequence number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 spin_lock(&sequence_lock);
Kay Sievers312c0042005-11-16 09:00:00 +0100163 seq = ++uevent_seqnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 spin_unlock(&sequence_lock);
165 sprintf(seq_buff, "SEQNUM=%llu", (unsigned long long)seq);
166
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200167#if defined(CONFIG_NET)
Kay Sievers5f123fb2005-11-11 14:43:07 +0100168 /* send netlink message */
169 if (uevent_sock) {
170 struct sk_buff *skb;
171 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Kay Sievers5f123fb2005-11-11 14:43:07 +0100173 /* allocate message with the maximum possible size */
174 len = strlen(action_string) + strlen(devpath) + 2;
175 skb = alloc_skb(len + BUFFER_SIZE, GFP_KERNEL);
176 if (skb) {
177 /* add header */
178 scratch = skb_put(skb, len);
179 sprintf(scratch, "%s@%s", action_string, devpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Kay Sievers5f123fb2005-11-11 14:43:07 +0100181 /* copy keys to our continuous event payload buffer */
182 for (i = 2; envp[i]; i++) {
183 len = strlen(envp[i]) + 1;
184 scratch = skb_put(skb, len);
185 strcpy(scratch, envp[i]);
186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Kay Sievers5f123fb2005-11-11 14:43:07 +0100188 NETLINK_CB(skb).dst_group = 1;
189 netlink_broadcast(uevent_sock, skb, 0, 1, GFP_KERNEL);
190 }
191 }
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200192#endif
Kay Sievers5f123fb2005-11-11 14:43:07 +0100193
194 /* call uevent_helper, usually only enabled during early boot */
Kay Sievers312c0042005-11-16 09:00:00 +0100195 if (uevent_helper[0]) {
Kay Sievers5f123fb2005-11-11 14:43:07 +0100196 char *argv [3];
197
Kay Sievers312c0042005-11-16 09:00:00 +0100198 argv [0] = uevent_helper;
Kay Sievers5f123fb2005-11-11 14:43:07 +0100199 argv [1] = (char *)subsystem;
200 argv [2] = NULL;
Jeremy Fitzhardinge86313c42007-07-17 18:37:03 -0700201 call_usermodehelper (argv[0], argv, envp, UMH_WAIT_EXEC);
Kay Sievers5f123fb2005-11-11 14:43:07 +0100202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204exit:
Kay Sievers5f123fb2005-11-11 14:43:07 +0100205 kfree(devpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 kfree(buffer);
207 kfree(envp);
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800208 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
Cornelia Huck8a824722006-11-20 17:07:51 +0100210
211EXPORT_SYMBOL_GPL(kobject_uevent_env);
212
213/**
214 * kobject_uevent - notify userspace by ending an uevent
215 *
216 * @action: action that is happening (usually KOBJ_ADD and KOBJ_REMOVE)
217 * @kobj: struct kobject that the action is happening to
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800218 *
219 * Returns 0 if kobject_uevent() is completed with success or the
220 * corresponding error when it fails.
Cornelia Huck8a824722006-11-20 17:07:51 +0100221 */
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800222int kobject_uevent(struct kobject *kobj, enum kobject_action action)
Cornelia Huck8a824722006-11-20 17:07:51 +0100223{
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800224 return kobject_uevent_env(kobj, action, NULL);
Cornelia Huck8a824722006-11-20 17:07:51 +0100225}
226
Kay Sievers312c0042005-11-16 09:00:00 +0100227EXPORT_SYMBOL_GPL(kobject_uevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229/**
Kay Sievers312c0042005-11-16 09:00:00 +0100230 * add_uevent_var - helper for creating event variables
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 * @envp: Pointer to table of environment variables, as passed into
Kay Sievers312c0042005-11-16 09:00:00 +0100232 * uevent() method.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 * @num_envp: Number of environment variable slots available, as
Kay Sievers312c0042005-11-16 09:00:00 +0100234 * passed into uevent() method.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 * @cur_index: Pointer to current index into @envp. It should be
Kay Sievers312c0042005-11-16 09:00:00 +0100236 * initialized to 0 before the first call to add_uevent_var(),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 * and will be incremented on success.
238 * @buffer: Pointer to buffer for environment variables, as passed
Kay Sievers312c0042005-11-16 09:00:00 +0100239 * into uevent() method.
240 * @buffer_size: Length of @buffer, as passed into uevent() method.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 * @cur_len: Pointer to current length of space used in @buffer.
242 * Should be initialized to 0 before the first call to
Kay Sievers312c0042005-11-16 09:00:00 +0100243 * add_uevent_var(), and will be incremented on success.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 * @format: Format for creating environment variable (of the form
245 * "XXX=%x") for snprintf().
246 *
247 * Returns 0 if environment variable was added successfully or -ENOMEM
248 * if no space was available.
249 */
Kay Sievers312c0042005-11-16 09:00:00 +0100250int add_uevent_var(char **envp, int num_envp, int *cur_index,
251 char *buffer, int buffer_size, int *cur_len,
252 const char *format, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 va_list args;
255
256 /*
257 * We check against num_envp - 1 to make sure there is at
Kay Sievers312c0042005-11-16 09:00:00 +0100258 * least one slot left after we return, since kobject_uevent()
259 * needs to set the last slot to NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 */
261 if (*cur_index >= num_envp - 1)
262 return -ENOMEM;
263
264 envp[*cur_index] = buffer + *cur_len;
265
266 va_start(args, format);
267 *cur_len += vsnprintf(envp[*cur_index],
268 max(buffer_size - *cur_len, 0),
269 format, args) + 1;
270 va_end(args);
271
272 if (*cur_len > buffer_size)
273 return -ENOMEM;
274
275 (*cur_index)++;
276 return 0;
277}
Kay Sievers312c0042005-11-16 09:00:00 +0100278EXPORT_SYMBOL_GPL(add_uevent_var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200280#if defined(CONFIG_NET)
Kay Sievers5f123fb2005-11-11 14:43:07 +0100281static int __init kobject_uevent_init(void)
282{
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200283 uevent_sock = netlink_kernel_create(&init_net, NETLINK_KOBJECT_UEVENT,
284 1, NULL, NULL, THIS_MODULE);
Kay Sievers5f123fb2005-11-11 14:43:07 +0100285 if (!uevent_sock) {
286 printk(KERN_ERR
287 "kobject_uevent: unable to create netlink socket!\n");
288 return -ENODEV;
289 }
290
291 return 0;
292}
293
294postcore_initcall(kobject_uevent_init);
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200295#endif
Kay Sievers5f123fb2005-11-11 14:43:07 +0100296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297#endif /* CONFIG_HOTPLUG */