Merge master.kernel.org:/pub/scm/linux/kernel/git/davej/cpufreq
[deliverable/linux.git] / lib / kobject_uevent.c
CommitLineData
1da177e4
LT
1/*
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>
1da177e4
LT
22#include <linux/kobject.h>
23#include <net/sock.h>
24
d87499ed 25#define BUFFER_SIZE 2048 /* buffer for the variables */
1da177e4
LT
26#define NUM_ENVP 32 /* number of env pointers */
27
4d17ffda 28#if defined(CONFIG_HOTPLUG)
51107301
JN
29u64 uevent_seqnum;
30char uevent_helper[UEVENT_HELPER_PATH_LEN] = "/sbin/hotplug";
0296b228 31static DEFINE_SPINLOCK(sequence_lock);
4d17ffda 32#if defined(CONFIG_NET)
5f123fbd 33static struct sock *uevent_sock;
4d17ffda 34#endif
0296b228 35
1da177e4
LT
36static 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";
fa675765
GKH
45 case KOBJ_MOUNT:
46 return "mount";
47 case KOBJ_UMOUNT:
48 return "umount";
1da177e4
LT
49 case KOBJ_OFFLINE:
50 return "offline";
51 case KOBJ_ONLINE:
52 return "online";
a6a888b3
KA
53 case KOBJ_DOCK:
54 return "dock";
55 case KOBJ_UNDOCK:
56 return "undock";
1da177e4
LT
57 default:
58 return NULL;
59 }
60}
1da177e4 61
1da177e4 62/**
312c004d 63 * kobject_uevent - notify userspace by ending an uevent
1da177e4 64 *
312c004d 65 * @action: action that is happening (usually KOBJ_ADD and KOBJ_REMOVE)
1da177e4
LT
66 * @kobj: struct kobject that the action is happening to
67 */
312c004d 68void kobject_uevent(struct kobject *kobj, enum kobject_action action)
1da177e4 69{
5f123fbd
KS
70 char **envp;
71 char *buffer;
1da177e4 72 char *scratch;
5f123fbd
KS
73 const char *action_string;
74 const char *devpath = NULL;
75 const char *subsystem;
76 struct kobject *top_kobj;
77 struct kset *kset;
312c004d 78 struct kset_uevent_ops *uevent_ops;
5f123fbd
KS
79 u64 seq;
80 char *seq_buff;
1da177e4
LT
81 int i = 0;
82 int retval;
1da177e4 83
5f123fbd
KS
84 pr_debug("%s\n", __FUNCTION__);
85
86 action_string = action_to_string(action);
87 if (!action_string)
88 return;
89
90 /* search the kset we belong to */
91 top_kobj = kobj;
1da177e4
LT
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 }
5f123fbd 97 if (!top_kobj->kset)
1da177e4
LT
98 return;
99
5f123fbd 100 kset = top_kobj->kset;
312c004d 101 uevent_ops = kset->uevent_ops;
1da177e4 102
5f123fbd 103 /* skip the event, if the filter returns zero. */
312c004d
KS
104 if (uevent_ops && uevent_ops->filter)
105 if (!uevent_ops->filter(kset, kobj))
1da177e4 106 return;
1da177e4 107
5f123fbd
KS
108 /* environment index */
109 envp = kzalloc(NUM_ENVP * sizeof (char *), GFP_KERNEL);
1da177e4
LT
110 if (!envp)
111 return;
1da177e4 112
5f123fbd 113 /* environment values */
1da177e4
LT
114 buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
115 if (!buffer)
116 goto exit;
117
5f123fbd
KS
118 /* complete object path */
119 devpath = kobject_get_path(kobj, GFP_KERNEL);
120 if (!devpath)
121 goto exit;
1da177e4 122
5f123fbd 123 /* originating subsystem */
312c004d
KS
124 if (uevent_ops && uevent_ops->name)
125 subsystem = uevent_ops->name(kset, kobj);
5f123fbd
KS
126 else
127 subsystem = kobject_name(&kset->kobj);
1da177e4 128
5f123fbd
KS
129 /* event environemnt for helper process only */
130 envp[i++] = "HOME=/";
131 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
1da177e4 132
5f123fbd 133 /* default keys */
1da177e4 134 scratch = buffer;
1da177e4
LT
135 envp [i++] = scratch;
136 scratch += sprintf(scratch, "ACTION=%s", action_string) + 1;
1da177e4 137 envp [i++] = scratch;
5f123fbd 138 scratch += sprintf (scratch, "DEVPATH=%s", devpath) + 1;
1da177e4 139 envp [i++] = scratch;
5f123fbd 140 scratch += sprintf(scratch, "SUBSYSTEM=%s", subsystem) + 1;
1da177e4 141
5f123fbd 142 /* just reserve the space, overwrite it after kset call has returned */
1da177e4
LT
143 envp[i++] = seq_buff = scratch;
144 scratch += strlen("SEQNUM=18446744073709551616") + 1;
145
5f123fbd 146 /* let the kset specific function add its stuff */
312c004d
KS
147 if (uevent_ops && uevent_ops->uevent) {
148 retval = uevent_ops->uevent(kset, kobj,
1da177e4
LT
149 &envp[i], NUM_ENVP - i, scratch,
150 BUFFER_SIZE - (scratch - buffer));
151 if (retval) {
312c004d 152 pr_debug ("%s - uevent() returned %d\n",
1da177e4
LT
153 __FUNCTION__, retval);
154 goto exit;
155 }
156 }
157
5f123fbd 158 /* we will send an event, request a new sequence number */
1da177e4 159 spin_lock(&sequence_lock);
312c004d 160 seq = ++uevent_seqnum;
1da177e4
LT
161 spin_unlock(&sequence_lock);
162 sprintf(seq_buff, "SEQNUM=%llu", (unsigned long long)seq);
163
4d17ffda 164#if defined(CONFIG_NET)
5f123fbd
KS
165 /* send netlink message */
166 if (uevent_sock) {
167 struct sk_buff *skb;
168 size_t len;
169
170 /* 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);
177
178 /* 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 }
184
185 NETLINK_CB(skb).dst_group = 1;
186 netlink_broadcast(uevent_sock, skb, 0, 1, GFP_KERNEL);
187 }
188 }
4d17ffda 189#endif
1da177e4 190
5f123fbd 191 /* call uevent_helper, usually only enabled during early boot */
312c004d 192 if (uevent_helper[0]) {
5f123fbd 193 char *argv [3];
1da177e4 194
312c004d 195 argv [0] = uevent_helper;
5f123fbd
KS
196 argv [1] = (char *)subsystem;
197 argv [2] = NULL;
198 call_usermodehelper (argv[0], argv, envp, 0);
199 }
1da177e4
LT
200
201exit:
5f123fbd 202 kfree(devpath);
1da177e4
LT
203 kfree(buffer);
204 kfree(envp);
205 return;
206}
312c004d 207EXPORT_SYMBOL_GPL(kobject_uevent);
1da177e4
LT
208
209/**
312c004d 210 * add_uevent_var - helper for creating event variables
1da177e4 211 * @envp: Pointer to table of environment variables, as passed into
312c004d 212 * uevent() method.
1da177e4 213 * @num_envp: Number of environment variable slots available, as
312c004d 214 * passed into uevent() method.
1da177e4 215 * @cur_index: Pointer to current index into @envp. It should be
312c004d 216 * initialized to 0 before the first call to add_uevent_var(),
1da177e4
LT
217 * and will be incremented on success.
218 * @buffer: Pointer to buffer for environment variables, as passed
312c004d
KS
219 * into uevent() method.
220 * @buffer_size: Length of @buffer, as passed into uevent() method.
1da177e4
LT
221 * @cur_len: Pointer to current length of space used in @buffer.
222 * Should be initialized to 0 before the first call to
312c004d 223 * add_uevent_var(), and will be incremented on success.
1da177e4
LT
224 * @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 */
312c004d
KS
230int add_uevent_var(char **envp, int num_envp, int *cur_index,
231 char *buffer, int buffer_size, int *cur_len,
232 const char *format, ...)
1da177e4
LT
233{
234 va_list args;
235
236 /*
237 * We check against num_envp - 1 to make sure there is at
312c004d
KS
238 * least one slot left after we return, since kobject_uevent()
239 * needs to set the last slot to NULL.
1da177e4
LT
240 */
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}
312c004d 258EXPORT_SYMBOL_GPL(add_uevent_var);
1da177e4 259
4d17ffda 260#if defined(CONFIG_NET)
5f123fbd
KS
261static 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);
4d17ffda 276#endif
5f123fbd 277
1da177e4 278#endif /* CONFIG_HOTPLUG */
This page took 0.150143 seconds and 5 git commands to generate.