blob: d9a3510ed2e2f198b26846445eff73390cd7aa32 [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 Sievers4d17ffd2006-04-25 15:37:26 +020028#if defined(CONFIG_HOTPLUG)
Jun'ichi Nomura51107302006-03-15 08:28:55 -050029u64 uevent_seqnum;
30char uevent_helper[UEVENT_HELPER_PATH_LEN] = "/sbin/hotplug";
Kay Sievers0296b222005-11-11 05:33:52 +010031static DEFINE_SPINLOCK(sequence_lock);
Kay Sievers4d17ffd2006-04-25 15:37:26 +020032#if defined(CONFIG_NET)
Kay Sievers5f123fb2005-11-11 14:43:07 +010033static struct sock *uevent_sock;
Kay Sievers4d17ffd2006-04-25 15:37:26 +020034#endif
Kay Sievers0296b222005-11-11 05:33:52 +010035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036static char *action_to_string(enum kobject_action action)
37{
38 switch (action) {
39 case KOBJ_ADD:
40 return "add";
41 case KOBJ_REMOVE:
42 return "remove";
43 case KOBJ_CHANGE:
44 return "change";
Greg Kroah-Hartmanfa675762006-02-22 09:39:02 -080045 case KOBJ_MOUNT:
46 return "mount";
47 case KOBJ_UMOUNT:
48 return "umount";
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 case KOBJ_OFFLINE:
50 return "offline";
51 case KOBJ_ONLINE:
52 return "online";
Cornelia Huck8a824722006-11-20 17:07:51 +010053 case KOBJ_MOVE:
54 return "move";
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 default:
56 return NULL;
57 }
58}
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060/**
Cornelia Huck8a824722006-11-20 17:07:51 +010061 * kobject_uevent_env - send an uevent with environmental data
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 *
Cornelia Huck8a824722006-11-20 17:07:51 +010063 * @action: action that is happening (usually KOBJ_MOVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 * @kobj: struct kobject that the action is happening to
Cornelia Huck8a824722006-11-20 17:07:51 +010065 * @envp_ext: pointer to environmental data
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -080066 *
67 * Returns 0 if kobject_uevent() is completed with success or the
68 * corresponding error when it fails.
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 */
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -080070int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
Cornelia Huck8a824722006-11-20 17:07:51 +010071 char *envp_ext[])
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Kay Sievers5f123fb2005-11-11 14:43:07 +010073 char **envp;
74 char *buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 char *scratch;
Kay Sievers5f123fb2005-11-11 14:43:07 +010076 const char *action_string;
77 const char *devpath = NULL;
78 const char *subsystem;
79 struct kobject *top_kobj;
80 struct kset *kset;
Kay Sievers312c0042005-11-16 09:00:00 +010081 struct kset_uevent_ops *uevent_ops;
Kay Sievers5f123fb2005-11-11 14:43:07 +010082 u64 seq;
83 char *seq_buff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 int i = 0;
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -080085 int retval = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +010086 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Kay Sievers5f123fb2005-11-11 14:43:07 +010088 pr_debug("%s\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90 action_string = action_to_string(action);
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -080091 if (!action_string) {
92 pr_debug("kobject attempted to send uevent without action_string!\n");
93 return -EINVAL;
94 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Kay Sievers5f123fb2005-11-11 14:43:07 +010096 /* search the kset we belong to */
97 top_kobj = kobj;
John Anthony Kazos Jr14193fb2007-04-04 07:39:17 -040098 while (!top_kobj->kset && top_kobj->parent) {
99 top_kobj = top_kobj->parent;
Kay Sievers5f123fb2005-11-11 14:43:07 +0100100 }
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800101 if (!top_kobj->kset) {
102 pr_debug("kobject attempted to send uevent without kset!\n");
103 return -EINVAL;
104 }
Kay Sievers5f123fb2005-11-11 14:43:07 +0100105
106 kset = top_kobj->kset;
Kay Sievers312c0042005-11-16 09:00:00 +0100107 uevent_ops = kset->uevent_ops;
Kay Sievers5f123fb2005-11-11 14:43:07 +0100108
109 /* skip the event, if the filter returns zero. */
Kay Sievers312c0042005-11-16 09:00:00 +0100110 if (uevent_ops && uevent_ops->filter)
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800111 if (!uevent_ops->filter(kset, kobj)) {
112 pr_debug("kobject filter function caused the event to drop!\n");
113 return 0;
114 }
Kay Sievers5f123fb2005-11-11 14:43:07 +0100115
Kay Sievers86406242007-03-14 03:25:56 +0100116 /* originating subsystem */
117 if (uevent_ops && uevent_ops->name)
118 subsystem = uevent_ops->name(kset, kobj);
119 else
120 subsystem = kobject_name(&kset->kobj);
121 if (!subsystem) {
122 pr_debug("unset subsytem caused the event to drop!\n");
123 return 0;
124 }
125
Kay Sievers5f123fb2005-11-11 14:43:07 +0100126 /* environment index */
127 envp = kzalloc(NUM_ENVP * sizeof (char *), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 if (!envp)
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800129 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Kay Sievers5f123fb2005-11-11 14:43:07 +0100131 /* environment values */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800133 if (!buffer) {
134 retval = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 goto exit;
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Kay Sievers5f123fb2005-11-11 14:43:07 +0100138 /* complete object path */
139 devpath = kobject_get_path(kobj, GFP_KERNEL);
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800140 if (!devpath) {
141 retval = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 goto exit;
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Kay Sievers5f123fb2005-11-11 14:43:07 +0100145 /* event environemnt for helper process only */
146 envp[i++] = "HOME=/";
147 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Kay Sievers5f123fb2005-11-11 14:43:07 +0100149 /* default keys */
150 scratch = buffer;
151 envp [i++] = scratch;
152 scratch += sprintf(scratch, "ACTION=%s", action_string) + 1;
153 envp [i++] = scratch;
154 scratch += sprintf (scratch, "DEVPATH=%s", devpath) + 1;
155 envp [i++] = scratch;
156 scratch += sprintf(scratch, "SUBSYSTEM=%s", subsystem) + 1;
Cornelia Huck8a824722006-11-20 17:07:51 +0100157 for (j = 0; envp_ext && envp_ext[j]; j++)
158 envp[i++] = envp_ext[j];
Kay Sievers5f123fb2005-11-11 14:43:07 +0100159 /* just reserve the space, overwrite it after kset call has returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 envp[i++] = seq_buff = scratch;
161 scratch += strlen("SEQNUM=18446744073709551616") + 1;
162
Kay Sievers5f123fb2005-11-11 14:43:07 +0100163 /* let the kset specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100164 if (uevent_ops && uevent_ops->uevent) {
165 retval = uevent_ops->uevent(kset, kobj,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 &envp[i], NUM_ENVP - i, scratch,
167 BUFFER_SIZE - (scratch - buffer));
168 if (retval) {
Kay Sievers312c0042005-11-16 09:00:00 +0100169 pr_debug ("%s - uevent() returned %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 __FUNCTION__, retval);
171 goto exit;
172 }
173 }
174
Kay Sievers5f123fb2005-11-11 14:43:07 +0100175 /* we will send an event, request a new sequence number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 spin_lock(&sequence_lock);
Kay Sievers312c0042005-11-16 09:00:00 +0100177 seq = ++uevent_seqnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 spin_unlock(&sequence_lock);
179 sprintf(seq_buff, "SEQNUM=%llu", (unsigned long long)seq);
180
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200181#if defined(CONFIG_NET)
Kay Sievers5f123fb2005-11-11 14:43:07 +0100182 /* send netlink message */
183 if (uevent_sock) {
184 struct sk_buff *skb;
185 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Kay Sievers5f123fb2005-11-11 14:43:07 +0100187 /* allocate message with the maximum possible size */
188 len = strlen(action_string) + strlen(devpath) + 2;
189 skb = alloc_skb(len + BUFFER_SIZE, GFP_KERNEL);
190 if (skb) {
191 /* add header */
192 scratch = skb_put(skb, len);
193 sprintf(scratch, "%s@%s", action_string, devpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Kay Sievers5f123fb2005-11-11 14:43:07 +0100195 /* copy keys to our continuous event payload buffer */
196 for (i = 2; envp[i]; i++) {
197 len = strlen(envp[i]) + 1;
198 scratch = skb_put(skb, len);
199 strcpy(scratch, envp[i]);
200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Kay Sievers5f123fb2005-11-11 14:43:07 +0100202 NETLINK_CB(skb).dst_group = 1;
203 netlink_broadcast(uevent_sock, skb, 0, 1, GFP_KERNEL);
204 }
205 }
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200206#endif
Kay Sievers5f123fb2005-11-11 14:43:07 +0100207
208 /* call uevent_helper, usually only enabled during early boot */
Kay Sievers312c0042005-11-16 09:00:00 +0100209 if (uevent_helper[0]) {
Kay Sievers5f123fb2005-11-11 14:43:07 +0100210 char *argv [3];
211
Kay Sievers312c0042005-11-16 09:00:00 +0100212 argv [0] = uevent_helper;
Kay Sievers5f123fb2005-11-11 14:43:07 +0100213 argv [1] = (char *)subsystem;
214 argv [2] = NULL;
215 call_usermodehelper (argv[0], argv, envp, 0);
216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218exit:
Kay Sievers5f123fb2005-11-11 14:43:07 +0100219 kfree(devpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 kfree(buffer);
221 kfree(envp);
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800222 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
Cornelia Huck8a824722006-11-20 17:07:51 +0100224
225EXPORT_SYMBOL_GPL(kobject_uevent_env);
226
227/**
228 * kobject_uevent - notify userspace by ending an uevent
229 *
230 * @action: action that is happening (usually KOBJ_ADD and KOBJ_REMOVE)
231 * @kobj: struct kobject that the action is happening to
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800232 *
233 * Returns 0 if kobject_uevent() is completed with success or the
234 * corresponding error when it fails.
Cornelia Huck8a824722006-11-20 17:07:51 +0100235 */
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800236int kobject_uevent(struct kobject *kobj, enum kobject_action action)
Cornelia Huck8a824722006-11-20 17:07:51 +0100237{
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800238 return kobject_uevent_env(kobj, action, NULL);
Cornelia Huck8a824722006-11-20 17:07:51 +0100239}
240
Kay Sievers312c0042005-11-16 09:00:00 +0100241EXPORT_SYMBOL_GPL(kobject_uevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243/**
Kay Sievers312c0042005-11-16 09:00:00 +0100244 * add_uevent_var - helper for creating event variables
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 * @envp: Pointer to table of environment variables, as passed into
Kay Sievers312c0042005-11-16 09:00:00 +0100246 * uevent() method.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 * @num_envp: Number of environment variable slots available, as
Kay Sievers312c0042005-11-16 09:00:00 +0100248 * passed into uevent() method.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 * @cur_index: Pointer to current index into @envp. It should be
Kay Sievers312c0042005-11-16 09:00:00 +0100250 * initialized to 0 before the first call to add_uevent_var(),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 * and will be incremented on success.
252 * @buffer: Pointer to buffer for environment variables, as passed
Kay Sievers312c0042005-11-16 09:00:00 +0100253 * into uevent() method.
254 * @buffer_size: Length of @buffer, as passed into uevent() method.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 * @cur_len: Pointer to current length of space used in @buffer.
256 * Should be initialized to 0 before the first call to
Kay Sievers312c0042005-11-16 09:00:00 +0100257 * add_uevent_var(), and will be incremented on success.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 * @format: Format for creating environment variable (of the form
259 * "XXX=%x") for snprintf().
260 *
261 * Returns 0 if environment variable was added successfully or -ENOMEM
262 * if no space was available.
263 */
Kay Sievers312c0042005-11-16 09:00:00 +0100264int add_uevent_var(char **envp, int num_envp, int *cur_index,
265 char *buffer, int buffer_size, int *cur_len,
266 const char *format, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
268 va_list args;
269
270 /*
271 * We check against num_envp - 1 to make sure there is at
Kay Sievers312c0042005-11-16 09:00:00 +0100272 * least one slot left after we return, since kobject_uevent()
273 * needs to set the last slot to NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 */
275 if (*cur_index >= num_envp - 1)
276 return -ENOMEM;
277
278 envp[*cur_index] = buffer + *cur_len;
279
280 va_start(args, format);
281 *cur_len += vsnprintf(envp[*cur_index],
282 max(buffer_size - *cur_len, 0),
283 format, args) + 1;
284 va_end(args);
285
286 if (*cur_len > buffer_size)
287 return -ENOMEM;
288
289 (*cur_index)++;
290 return 0;
291}
Kay Sievers312c0042005-11-16 09:00:00 +0100292EXPORT_SYMBOL_GPL(add_uevent_var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200294#if defined(CONFIG_NET)
Kay Sievers5f123fb2005-11-11 14:43:07 +0100295static int __init kobject_uevent_init(void)
296{
297 uevent_sock = netlink_kernel_create(NETLINK_KOBJECT_UEVENT, 1, NULL,
Patrick McHardyaf65bdf2007-04-20 14:14:21 -0700298 NULL, THIS_MODULE);
Kay Sievers5f123fb2005-11-11 14:43:07 +0100299
300 if (!uevent_sock) {
301 printk(KERN_ERR
302 "kobject_uevent: unable to create netlink socket!\n");
303 return -ENODEV;
304 }
305
306 return 0;
307}
308
309postcore_initcall(kobject_uevent_init);
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200310#endif
Kay Sievers5f123fb2005-11-11 14:43:07 +0100311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312#endif /* CONFIG_HOTPLUG */