blob: 2b1530fc573bfaa4af0c7288eba1fce7af51513f [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";
Kristen Accardia6a888b2006-06-24 19:36:00 -040053 case KOBJ_DOCK:
54 return "dock";
55 case KOBJ_UNDOCK:
56 return "undock";
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 default:
58 return NULL;
59 }
60}
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062/**
Kay Sievers312c0042005-11-16 09:00:00 +010063 * kobject_uevent - notify userspace by ending an uevent
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 *
Kay Sievers312c0042005-11-16 09:00:00 +010065 * @action: action that is happening (usually KOBJ_ADD and KOBJ_REMOVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 * @kobj: struct kobject that the action is happening to
67 */
Kay Sievers312c0042005-11-16 09:00:00 +010068void kobject_uevent(struct kobject *kobj, enum kobject_action action)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Kay Sievers5f123fb2005-11-11 14:43:07 +010070 char **envp;
71 char *buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 char *scratch;
Kay Sievers5f123fb2005-11-11 14:43:07 +010073 const char *action_string;
74 const char *devpath = NULL;
75 const char *subsystem;
76 struct kobject *top_kobj;
77 struct kset *kset;
Kay Sievers312c0042005-11-16 09:00:00 +010078 struct kset_uevent_ops *uevent_ops;
Kay Sievers5f123fb2005-11-11 14:43:07 +010079 u64 seq;
80 char *seq_buff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 int i = 0;
82 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Kay Sievers5f123fb2005-11-11 14:43:07 +010084 pr_debug("%s\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86 action_string = action_to_string(action);
87 if (!action_string)
88 return;
89
Kay Sievers5f123fb2005-11-11 14:43:07 +010090 /* search the kset we belong to */
91 top_kobj = kobj;
92 if (!top_kobj->kset && top_kobj->parent) {
93 do {
94 top_kobj = top_kobj->parent;
95 } while (!top_kobj->kset && top_kobj->parent);
96 }
97 if (!top_kobj->kset)
98 return;
99
100 kset = top_kobj->kset;
Kay Sievers312c0042005-11-16 09:00:00 +0100101 uevent_ops = kset->uevent_ops;
Kay Sievers5f123fb2005-11-11 14:43:07 +0100102
103 /* skip the event, if the filter returns zero. */
Kay Sievers312c0042005-11-16 09:00:00 +0100104 if (uevent_ops && uevent_ops->filter)
105 if (!uevent_ops->filter(kset, kobj))
Kay Sievers5f123fb2005-11-11 14:43:07 +0100106 return;
107
108 /* environment index */
109 envp = kzalloc(NUM_ENVP * sizeof (char *), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 if (!envp)
111 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Kay Sievers5f123fb2005-11-11 14:43:07 +0100113 /* environment values */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
115 if (!buffer)
116 goto exit;
117
Kay Sievers5f123fb2005-11-11 14:43:07 +0100118 /* complete object path */
119 devpath = kobject_get_path(kobj, GFP_KERNEL);
120 if (!devpath)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 goto exit;
122
Kay Sievers5f123fb2005-11-11 14:43:07 +0100123 /* originating subsystem */
Kay Sievers312c0042005-11-16 09:00:00 +0100124 if (uevent_ops && uevent_ops->name)
125 subsystem = uevent_ops->name(kset, kobj);
Kay Sievers5f123fb2005-11-11 14:43:07 +0100126 else
127 subsystem = kobject_name(&kset->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Kay Sievers5f123fb2005-11-11 14:43:07 +0100129 /* event environemnt for helper process only */
130 envp[i++] = "HOME=/";
131 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Kay Sievers5f123fb2005-11-11 14:43:07 +0100133 /* default keys */
134 scratch = buffer;
135 envp [i++] = scratch;
136 scratch += sprintf(scratch, "ACTION=%s", action_string) + 1;
137 envp [i++] = scratch;
138 scratch += sprintf (scratch, "DEVPATH=%s", devpath) + 1;
139 envp [i++] = scratch;
140 scratch += sprintf(scratch, "SUBSYSTEM=%s", subsystem) + 1;
141
142 /* just reserve the space, overwrite it after kset call has returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 envp[i++] = seq_buff = scratch;
144 scratch += strlen("SEQNUM=18446744073709551616") + 1;
145
Kay Sievers5f123fb2005-11-11 14:43:07 +0100146 /* let the kset specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100147 if (uevent_ops && uevent_ops->uevent) {
148 retval = uevent_ops->uevent(kset, kobj,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 &envp[i], NUM_ENVP - i, scratch,
150 BUFFER_SIZE - (scratch - buffer));
151 if (retval) {
Kay Sievers312c0042005-11-16 09:00:00 +0100152 pr_debug ("%s - uevent() returned %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 __FUNCTION__, retval);
154 goto exit;
155 }
156 }
157
Kay Sievers5f123fb2005-11-11 14:43:07 +0100158 /* we will send an event, request a new sequence number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 spin_lock(&sequence_lock);
Kay Sievers312c0042005-11-16 09:00:00 +0100160 seq = ++uevent_seqnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 spin_unlock(&sequence_lock);
162 sprintf(seq_buff, "SEQNUM=%llu", (unsigned long long)seq);
163
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200164#if defined(CONFIG_NET)
Kay Sievers5f123fb2005-11-11 14:43:07 +0100165 /* send netlink message */
166 if (uevent_sock) {
167 struct sk_buff *skb;
168 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Kay Sievers5f123fb2005-11-11 14:43:07 +0100170 /* allocate message with the maximum possible size */
171 len = strlen(action_string) + strlen(devpath) + 2;
172 skb = alloc_skb(len + BUFFER_SIZE, GFP_KERNEL);
173 if (skb) {
174 /* add header */
175 scratch = skb_put(skb, len);
176 sprintf(scratch, "%s@%s", action_string, devpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Kay Sievers5f123fb2005-11-11 14:43:07 +0100178 /* copy keys to our continuous event payload buffer */
179 for (i = 2; envp[i]; i++) {
180 len = strlen(envp[i]) + 1;
181 scratch = skb_put(skb, len);
182 strcpy(scratch, envp[i]);
183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Kay Sievers5f123fb2005-11-11 14:43:07 +0100185 NETLINK_CB(skb).dst_group = 1;
186 netlink_broadcast(uevent_sock, skb, 0, 1, GFP_KERNEL);
187 }
188 }
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200189#endif
Kay Sievers5f123fb2005-11-11 14:43:07 +0100190
191 /* call uevent_helper, usually only enabled during early boot */
Kay Sievers312c0042005-11-16 09:00:00 +0100192 if (uevent_helper[0]) {
Kay Sievers5f123fb2005-11-11 14:43:07 +0100193 char *argv [3];
194
Kay Sievers312c0042005-11-16 09:00:00 +0100195 argv [0] = uevent_helper;
Kay Sievers5f123fb2005-11-11 14:43:07 +0100196 argv [1] = (char *)subsystem;
197 argv [2] = NULL;
198 call_usermodehelper (argv[0], argv, envp, 0);
199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201exit:
Kay Sievers5f123fb2005-11-11 14:43:07 +0100202 kfree(devpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 kfree(buffer);
204 kfree(envp);
205 return;
206}
Kay Sievers312c0042005-11-16 09:00:00 +0100207EXPORT_SYMBOL_GPL(kobject_uevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209/**
Kay Sievers312c0042005-11-16 09:00:00 +0100210 * add_uevent_var - helper for creating event variables
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 * @envp: Pointer to table of environment variables, as passed into
Kay Sievers312c0042005-11-16 09:00:00 +0100212 * uevent() method.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 * @num_envp: Number of environment variable slots available, as
Kay Sievers312c0042005-11-16 09:00:00 +0100214 * passed into uevent() method.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 * @cur_index: Pointer to current index into @envp. It should be
Kay Sievers312c0042005-11-16 09:00:00 +0100216 * initialized to 0 before the first call to add_uevent_var(),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 * and will be incremented on success.
218 * @buffer: Pointer to buffer for environment variables, as passed
Kay Sievers312c0042005-11-16 09:00:00 +0100219 * into uevent() method.
220 * @buffer_size: Length of @buffer, as passed into uevent() method.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 * @cur_len: Pointer to current length of space used in @buffer.
222 * Should be initialized to 0 before the first call to
Kay Sievers312c0042005-11-16 09:00:00 +0100223 * add_uevent_var(), and will be incremented on success.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 * @format: Format for creating environment variable (of the form
225 * "XXX=%x") for snprintf().
226 *
227 * Returns 0 if environment variable was added successfully or -ENOMEM
228 * if no space was available.
229 */
Kay Sievers312c0042005-11-16 09:00:00 +0100230int add_uevent_var(char **envp, int num_envp, int *cur_index,
231 char *buffer, int buffer_size, int *cur_len,
232 const char *format, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
234 va_list args;
235
236 /*
237 * We check against num_envp - 1 to make sure there is at
Kay Sievers312c0042005-11-16 09:00:00 +0100238 * least one slot left after we return, since kobject_uevent()
239 * needs to set the last slot to NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 */
241 if (*cur_index >= num_envp - 1)
242 return -ENOMEM;
243
244 envp[*cur_index] = buffer + *cur_len;
245
246 va_start(args, format);
247 *cur_len += vsnprintf(envp[*cur_index],
248 max(buffer_size - *cur_len, 0),
249 format, args) + 1;
250 va_end(args);
251
252 if (*cur_len > buffer_size)
253 return -ENOMEM;
254
255 (*cur_index)++;
256 return 0;
257}
Kay Sievers312c0042005-11-16 09:00:00 +0100258EXPORT_SYMBOL_GPL(add_uevent_var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200260#if defined(CONFIG_NET)
Kay Sievers5f123fb2005-11-11 14:43:07 +0100261static int __init kobject_uevent_init(void)
262{
263 uevent_sock = netlink_kernel_create(NETLINK_KOBJECT_UEVENT, 1, NULL,
264 THIS_MODULE);
265
266 if (!uevent_sock) {
267 printk(KERN_ERR
268 "kobject_uevent: unable to create netlink socket!\n");
269 return -ENODEV;
270 }
271
272 return 0;
273}
274
275postcore_initcall(kobject_uevent_init);
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200276#endif
Kay Sievers5f123fb2005-11-11 14:43:07 +0100277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278#endif /* CONFIG_HOTPLUG */