blob: 82fc1794b69113ab09266b2ca26534c194418e83 [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
118 /* environment index */
119 envp = kzalloc(NUM_ENVP * sizeof (char *), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 if (!envp)
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800121 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Kay Sievers5f123fb2005-11-11 14:43:07 +0100123 /* environment values */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800125 if (!buffer) {
126 retval = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 goto exit;
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Kay Sievers5f123fb2005-11-11 14:43:07 +0100130 /* complete object path */
131 devpath = kobject_get_path(kobj, GFP_KERNEL);
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800132 if (!devpath) {
133 retval = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 goto exit;
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Kay Sievers5f123fb2005-11-11 14:43:07 +0100137 /* originating subsystem */
Kay Sievers312c0042005-11-16 09:00:00 +0100138 if (uevent_ops && uevent_ops->name)
139 subsystem = uevent_ops->name(kset, kobj);
Kay Sievers5f123fb2005-11-11 14:43:07 +0100140 else
141 subsystem = kobject_name(&kset->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Kay Sievers5f123fb2005-11-11 14:43:07 +0100143 /* event environemnt for helper process only */
144 envp[i++] = "HOME=/";
145 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Kay Sievers5f123fb2005-11-11 14:43:07 +0100147 /* default keys */
148 scratch = buffer;
149 envp [i++] = scratch;
150 scratch += sprintf(scratch, "ACTION=%s", action_string) + 1;
151 envp [i++] = scratch;
152 scratch += sprintf (scratch, "DEVPATH=%s", devpath) + 1;
153 envp [i++] = scratch;
154 scratch += sprintf(scratch, "SUBSYSTEM=%s", subsystem) + 1;
Cornelia Huck8a824722006-11-20 17:07:51 +0100155 for (j = 0; envp_ext && envp_ext[j]; j++)
156 envp[i++] = envp_ext[j];
Kay Sievers5f123fb2005-11-11 14:43:07 +0100157 /* just reserve the space, overwrite it after kset call has returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 envp[i++] = seq_buff = scratch;
159 scratch += strlen("SEQNUM=18446744073709551616") + 1;
160
Kay Sievers5f123fb2005-11-11 14:43:07 +0100161 /* let the kset specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100162 if (uevent_ops && uevent_ops->uevent) {
163 retval = uevent_ops->uevent(kset, kobj,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 &envp[i], NUM_ENVP - i, scratch,
165 BUFFER_SIZE - (scratch - buffer));
166 if (retval) {
Kay Sievers312c0042005-11-16 09:00:00 +0100167 pr_debug ("%s - uevent() returned %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 __FUNCTION__, retval);
169 goto exit;
170 }
171 }
172
Kay Sievers5f123fb2005-11-11 14:43:07 +0100173 /* we will send an event, request a new sequence number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 spin_lock(&sequence_lock);
Kay Sievers312c0042005-11-16 09:00:00 +0100175 seq = ++uevent_seqnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 spin_unlock(&sequence_lock);
177 sprintf(seq_buff, "SEQNUM=%llu", (unsigned long long)seq);
178
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200179#if defined(CONFIG_NET)
Kay Sievers5f123fb2005-11-11 14:43:07 +0100180 /* send netlink message */
181 if (uevent_sock) {
182 struct sk_buff *skb;
183 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Kay Sievers5f123fb2005-11-11 14:43:07 +0100185 /* allocate message with the maximum possible size */
186 len = strlen(action_string) + strlen(devpath) + 2;
187 skb = alloc_skb(len + BUFFER_SIZE, GFP_KERNEL);
188 if (skb) {
189 /* add header */
190 scratch = skb_put(skb, len);
191 sprintf(scratch, "%s@%s", action_string, devpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Kay Sievers5f123fb2005-11-11 14:43:07 +0100193 /* copy keys to our continuous event payload buffer */
194 for (i = 2; envp[i]; i++) {
195 len = strlen(envp[i]) + 1;
196 scratch = skb_put(skb, len);
197 strcpy(scratch, envp[i]);
198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Kay Sievers5f123fb2005-11-11 14:43:07 +0100200 NETLINK_CB(skb).dst_group = 1;
201 netlink_broadcast(uevent_sock, skb, 0, 1, GFP_KERNEL);
202 }
203 }
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200204#endif
Kay Sievers5f123fb2005-11-11 14:43:07 +0100205
206 /* call uevent_helper, usually only enabled during early boot */
Kay Sievers312c0042005-11-16 09:00:00 +0100207 if (uevent_helper[0]) {
Kay Sievers5f123fb2005-11-11 14:43:07 +0100208 char *argv [3];
209
Kay Sievers312c0042005-11-16 09:00:00 +0100210 argv [0] = uevent_helper;
Kay Sievers5f123fb2005-11-11 14:43:07 +0100211 argv [1] = (char *)subsystem;
212 argv [2] = NULL;
213 call_usermodehelper (argv[0], argv, envp, 0);
214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216exit:
Kay Sievers5f123fb2005-11-11 14:43:07 +0100217 kfree(devpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 kfree(buffer);
219 kfree(envp);
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800220 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
Cornelia Huck8a824722006-11-20 17:07:51 +0100222
223EXPORT_SYMBOL_GPL(kobject_uevent_env);
224
225/**
226 * kobject_uevent - notify userspace by ending an uevent
227 *
228 * @action: action that is happening (usually KOBJ_ADD and KOBJ_REMOVE)
229 * @kobj: struct kobject that the action is happening to
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800230 *
231 * Returns 0 if kobject_uevent() is completed with success or the
232 * corresponding error when it fails.
Cornelia Huck8a824722006-11-20 17:07:51 +0100233 */
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800234int kobject_uevent(struct kobject *kobj, enum kobject_action action)
Cornelia Huck8a824722006-11-20 17:07:51 +0100235{
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800236 return kobject_uevent_env(kobj, action, NULL);
Cornelia Huck8a824722006-11-20 17:07:51 +0100237}
238
Kay Sievers312c0042005-11-16 09:00:00 +0100239EXPORT_SYMBOL_GPL(kobject_uevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241/**
Kay Sievers312c0042005-11-16 09:00:00 +0100242 * add_uevent_var - helper for creating event variables
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 * @envp: Pointer to table of environment variables, as passed into
Kay Sievers312c0042005-11-16 09:00:00 +0100244 * uevent() method.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 * @num_envp: Number of environment variable slots available, as
Kay Sievers312c0042005-11-16 09:00:00 +0100246 * passed into uevent() method.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 * @cur_index: Pointer to current index into @envp. It should be
Kay Sievers312c0042005-11-16 09:00:00 +0100248 * initialized to 0 before the first call to add_uevent_var(),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 * and will be incremented on success.
250 * @buffer: Pointer to buffer for environment variables, as passed
Kay Sievers312c0042005-11-16 09:00:00 +0100251 * into uevent() method.
252 * @buffer_size: Length of @buffer, as passed into uevent() method.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 * @cur_len: Pointer to current length of space used in @buffer.
254 * Should be initialized to 0 before the first call to
Kay Sievers312c0042005-11-16 09:00:00 +0100255 * add_uevent_var(), and will be incremented on success.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 * @format: Format for creating environment variable (of the form
257 * "XXX=%x") for snprintf().
258 *
259 * Returns 0 if environment variable was added successfully or -ENOMEM
260 * if no space was available.
261 */
Kay Sievers312c0042005-11-16 09:00:00 +0100262int add_uevent_var(char **envp, int num_envp, int *cur_index,
263 char *buffer, int buffer_size, int *cur_len,
264 const char *format, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
266 va_list args;
267
268 /*
269 * We check against num_envp - 1 to make sure there is at
Kay Sievers312c0042005-11-16 09:00:00 +0100270 * least one slot left after we return, since kobject_uevent()
271 * needs to set the last slot to NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 */
273 if (*cur_index >= num_envp - 1)
274 return -ENOMEM;
275
276 envp[*cur_index] = buffer + *cur_len;
277
278 va_start(args, format);
279 *cur_len += vsnprintf(envp[*cur_index],
280 max(buffer_size - *cur_len, 0),
281 format, args) + 1;
282 va_end(args);
283
284 if (*cur_len > buffer_size)
285 return -ENOMEM;
286
287 (*cur_index)++;
288 return 0;
289}
Kay Sievers312c0042005-11-16 09:00:00 +0100290EXPORT_SYMBOL_GPL(add_uevent_var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200292#if defined(CONFIG_NET)
Kay Sievers5f123fb2005-11-11 14:43:07 +0100293static int __init kobject_uevent_init(void)
294{
295 uevent_sock = netlink_kernel_create(NETLINK_KOBJECT_UEVENT, 1, NULL,
Patrick McHardyaf65bdf2007-04-20 14:14:21 -0700296 NULL, THIS_MODULE);
Kay Sievers5f123fb2005-11-11 14:43:07 +0100297
298 if (!uevent_sock) {
299 printk(KERN_ERR
300 "kobject_uevent: unable to create netlink socket!\n");
301 return -ENODEV;
302 }
303
304 return 0;
305}
306
307postcore_initcall(kobject_uevent_init);
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200308#endif
Kay Sievers5f123fb2005-11-11 14:43:07 +0100309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310#endif /* CONFIG_HOTPLUG */