blob: 4122f38330d48557111c7cdc7f10ab8161623322 [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;
98 if (!top_kobj->kset && top_kobj->parent) {
99 do {
100 top_kobj = top_kobj->parent;
101 } while (!top_kobj->kset && top_kobj->parent);
102 }
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800103 if (!top_kobj->kset) {
104 pr_debug("kobject attempted to send uevent without kset!\n");
105 return -EINVAL;
106 }
Kay Sievers5f123fb2005-11-11 14:43:07 +0100107
108 kset = top_kobj->kset;
Kay Sievers312c0042005-11-16 09:00:00 +0100109 uevent_ops = kset->uevent_ops;
Kay Sievers5f123fb2005-11-11 14:43:07 +0100110
111 /* skip the event, if the filter returns zero. */
Kay Sievers312c0042005-11-16 09:00:00 +0100112 if (uevent_ops && uevent_ops->filter)
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800113 if (!uevent_ops->filter(kset, kobj)) {
114 pr_debug("kobject filter function caused the event to drop!\n");
115 return 0;
116 }
Kay Sievers5f123fb2005-11-11 14:43:07 +0100117
Kay Sievers86406242007-03-14 03:25:56 +0100118 /* originating subsystem */
119 if (uevent_ops && uevent_ops->name)
120 subsystem = uevent_ops->name(kset, kobj);
121 else
122 subsystem = kobject_name(&kset->kobj);
123 if (!subsystem) {
124 pr_debug("unset subsytem caused the event to drop!\n");
125 return 0;
126 }
127
Kay Sievers5f123fb2005-11-11 14:43:07 +0100128 /* environment index */
129 envp = kzalloc(NUM_ENVP * sizeof (char *), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 if (!envp)
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800131 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Kay Sievers5f123fb2005-11-11 14:43:07 +0100133 /* environment values */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800135 if (!buffer) {
136 retval = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 goto exit;
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Kay Sievers5f123fb2005-11-11 14:43:07 +0100140 /* complete object path */
141 devpath = kobject_get_path(kobj, GFP_KERNEL);
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800142 if (!devpath) {
143 retval = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 goto exit;
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Kay Sievers5f123fb2005-11-11 14:43:07 +0100147 /* event environemnt for helper process only */
148 envp[i++] = "HOME=/";
149 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Kay Sievers5f123fb2005-11-11 14:43:07 +0100151 /* default keys */
152 scratch = buffer;
153 envp [i++] = scratch;
154 scratch += sprintf(scratch, "ACTION=%s", action_string) + 1;
155 envp [i++] = scratch;
156 scratch += sprintf (scratch, "DEVPATH=%s", devpath) + 1;
157 envp [i++] = scratch;
158 scratch += sprintf(scratch, "SUBSYSTEM=%s", subsystem) + 1;
Cornelia Huck8a824722006-11-20 17:07:51 +0100159 for (j = 0; envp_ext && envp_ext[j]; j++)
160 envp[i++] = envp_ext[j];
Kay Sievers5f123fb2005-11-11 14:43:07 +0100161 /* just reserve the space, overwrite it after kset call has returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 envp[i++] = seq_buff = scratch;
163 scratch += strlen("SEQNUM=18446744073709551616") + 1;
164
Kay Sievers5f123fb2005-11-11 14:43:07 +0100165 /* let the kset specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100166 if (uevent_ops && uevent_ops->uevent) {
167 retval = uevent_ops->uevent(kset, kobj,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 &envp[i], NUM_ENVP - i, scratch,
169 BUFFER_SIZE - (scratch - buffer));
170 if (retval) {
Kay Sievers312c0042005-11-16 09:00:00 +0100171 pr_debug ("%s - uevent() returned %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 __FUNCTION__, retval);
173 goto exit;
174 }
175 }
176
Kay Sievers5f123fb2005-11-11 14:43:07 +0100177 /* we will send an event, request a new sequence number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 spin_lock(&sequence_lock);
Kay Sievers312c0042005-11-16 09:00:00 +0100179 seq = ++uevent_seqnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 spin_unlock(&sequence_lock);
181 sprintf(seq_buff, "SEQNUM=%llu", (unsigned long long)seq);
182
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200183#if defined(CONFIG_NET)
Kay Sievers5f123fb2005-11-11 14:43:07 +0100184 /* send netlink message */
185 if (uevent_sock) {
186 struct sk_buff *skb;
187 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Kay Sievers5f123fb2005-11-11 14:43:07 +0100189 /* allocate message with the maximum possible size */
190 len = strlen(action_string) + strlen(devpath) + 2;
191 skb = alloc_skb(len + BUFFER_SIZE, GFP_KERNEL);
192 if (skb) {
193 /* add header */
194 scratch = skb_put(skb, len);
195 sprintf(scratch, "%s@%s", action_string, devpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Kay Sievers5f123fb2005-11-11 14:43:07 +0100197 /* copy keys to our continuous event payload buffer */
198 for (i = 2; envp[i]; i++) {
199 len = strlen(envp[i]) + 1;
200 scratch = skb_put(skb, len);
201 strcpy(scratch, envp[i]);
202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Kay Sievers5f123fb2005-11-11 14:43:07 +0100204 NETLINK_CB(skb).dst_group = 1;
205 netlink_broadcast(uevent_sock, skb, 0, 1, GFP_KERNEL);
206 }
207 }
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200208#endif
Kay Sievers5f123fb2005-11-11 14:43:07 +0100209
210 /* call uevent_helper, usually only enabled during early boot */
Kay Sievers312c0042005-11-16 09:00:00 +0100211 if (uevent_helper[0]) {
Kay Sievers5f123fb2005-11-11 14:43:07 +0100212 char *argv [3];
213
Kay Sievers312c0042005-11-16 09:00:00 +0100214 argv [0] = uevent_helper;
Kay Sievers5f123fb2005-11-11 14:43:07 +0100215 argv [1] = (char *)subsystem;
216 argv [2] = NULL;
217 call_usermodehelper (argv[0], argv, envp, 0);
218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220exit:
Kay Sievers5f123fb2005-11-11 14:43:07 +0100221 kfree(devpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 kfree(buffer);
223 kfree(envp);
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800224 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
Cornelia Huck8a824722006-11-20 17:07:51 +0100226
227EXPORT_SYMBOL_GPL(kobject_uevent_env);
228
229/**
230 * kobject_uevent - notify userspace by ending an uevent
231 *
232 * @action: action that is happening (usually KOBJ_ADD and KOBJ_REMOVE)
233 * @kobj: struct kobject that the action is happening to
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800234 *
235 * Returns 0 if kobject_uevent() is completed with success or the
236 * corresponding error when it fails.
Cornelia Huck8a824722006-11-20 17:07:51 +0100237 */
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800238int kobject_uevent(struct kobject *kobj, enum kobject_action action)
Cornelia Huck8a824722006-11-20 17:07:51 +0100239{
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800240 return kobject_uevent_env(kobj, action, NULL);
Cornelia Huck8a824722006-11-20 17:07:51 +0100241}
242
Kay Sievers312c0042005-11-16 09:00:00 +0100243EXPORT_SYMBOL_GPL(kobject_uevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245/**
Kay Sievers312c0042005-11-16 09:00:00 +0100246 * add_uevent_var - helper for creating event variables
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 * @envp: Pointer to table of environment variables, as passed into
Kay Sievers312c0042005-11-16 09:00:00 +0100248 * uevent() method.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 * @num_envp: Number of environment variable slots available, as
Kay Sievers312c0042005-11-16 09:00:00 +0100250 * passed into uevent() method.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 * @cur_index: Pointer to current index into @envp. It should be
Kay Sievers312c0042005-11-16 09:00:00 +0100252 * initialized to 0 before the first call to add_uevent_var(),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 * and will be incremented on success.
254 * @buffer: Pointer to buffer for environment variables, as passed
Kay Sievers312c0042005-11-16 09:00:00 +0100255 * into uevent() method.
256 * @buffer_size: Length of @buffer, as passed into uevent() method.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 * @cur_len: Pointer to current length of space used in @buffer.
258 * Should be initialized to 0 before the first call to
Kay Sievers312c0042005-11-16 09:00:00 +0100259 * add_uevent_var(), and will be incremented on success.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 * @format: Format for creating environment variable (of the form
261 * "XXX=%x") for snprintf().
262 *
263 * Returns 0 if environment variable was added successfully or -ENOMEM
264 * if no space was available.
265 */
Kay Sievers312c0042005-11-16 09:00:00 +0100266int add_uevent_var(char **envp, int num_envp, int *cur_index,
267 char *buffer, int buffer_size, int *cur_len,
268 const char *format, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
270 va_list args;
271
272 /*
273 * We check against num_envp - 1 to make sure there is at
Kay Sievers312c0042005-11-16 09:00:00 +0100274 * least one slot left after we return, since kobject_uevent()
275 * needs to set the last slot to NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 */
277 if (*cur_index >= num_envp - 1)
278 return -ENOMEM;
279
280 envp[*cur_index] = buffer + *cur_len;
281
282 va_start(args, format);
283 *cur_len += vsnprintf(envp[*cur_index],
284 max(buffer_size - *cur_len, 0),
285 format, args) + 1;
286 va_end(args);
287
288 if (*cur_len > buffer_size)
289 return -ENOMEM;
290
291 (*cur_index)++;
292 return 0;
293}
Kay Sievers312c0042005-11-16 09:00:00 +0100294EXPORT_SYMBOL_GPL(add_uevent_var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200296#if defined(CONFIG_NET)
Kay Sievers5f123fb2005-11-11 14:43:07 +0100297static int __init kobject_uevent_init(void)
298{
299 uevent_sock = netlink_kernel_create(NETLINK_KOBJECT_UEVENT, 1, NULL,
Patrick McHardyaf65bdf2007-04-20 14:14:21 -0700300 NULL, THIS_MODULE);
Kay Sievers5f123fb2005-11-11 14:43:07 +0100301
302 if (!uevent_sock) {
303 printk(KERN_ERR
304 "kobject_uevent: unable to create netlink socket!\n");
305 return -ENODEV;
306 }
307
308 return 0;
309}
310
311postcore_initcall(kobject_uevent_init);
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200312#endif
Kay Sievers5f123fb2005-11-11 14:43:07 +0100313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314#endif /* CONFIG_HOTPLUG */