kobject: grab the kset reference in kobject_add, not kobject_init
[linux-2.6-block.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
1da177e4 25
cd030c4c 26u64 uevent_seqnum;
6a8d8abb 27char uevent_helper[UEVENT_HELPER_PATH_LEN] = CONFIG_UEVENT_HELPER_PATH;
cd030c4c
CH
28static DEFINE_SPINLOCK(sequence_lock);
29#if defined(CONFIG_NET)
30static struct sock *uevent_sock;
31#endif
32
5c5daf65
KS
33/* the strings here must match the enum in include/linux/kobject.h */
34static const char *kobject_actions[] = {
35 [KOBJ_ADD] = "add",
36 [KOBJ_REMOVE] = "remove",
37 [KOBJ_CHANGE] = "change",
38 [KOBJ_MOVE] = "move",
39 [KOBJ_ONLINE] = "online",
40 [KOBJ_OFFLINE] = "offline",
41};
42
43/**
44 * kobject_action_type - translate action string to numeric type
45 *
46 * @buf: buffer containing the action string, newline is ignored
47 * @len: length of buffer
48 * @type: pointer to the location to store the action type
49 *
50 * Returns 0 if the action string was recognized.
51 */
52int kobject_action_type(const char *buf, size_t count,
53 enum kobject_action *type)
54{
55 enum kobject_action action;
56 int ret = -EINVAL;
57
58 if (count && buf[count-1] == '\n')
59 count--;
60
61 if (!count)
62 goto out;
63
64 for (action = 0; action < ARRAY_SIZE(kobject_actions); action++) {
65 if (strncmp(kobject_actions[action], buf, count) != 0)
66 continue;
67 if (kobject_actions[action][count] != '\0')
68 continue;
69 *type = action;
70 ret = 0;
71 break;
72 }
73out:
74 return ret;
75}
76
1da177e4 77/**
8a82472f 78 * kobject_uevent_env - send an uevent with environmental data
1da177e4 79 *
ccd490a3 80 * @action: action that is happening
1da177e4 81 * @kobj: struct kobject that the action is happening to
8a82472f 82 * @envp_ext: pointer to environmental data
542cfce6
AK
83 *
84 * Returns 0 if kobject_uevent() is completed with success or the
85 * corresponding error when it fails.
1da177e4 86 */
542cfce6 87int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
7eff2e7a 88 char *envp_ext[])
1da177e4 89{
7eff2e7a
KS
90 struct kobj_uevent_env *env;
91 const char *action_string = kobject_actions[action];
5f123fbd
KS
92 const char *devpath = NULL;
93 const char *subsystem;
94 struct kobject *top_kobj;
95 struct kset *kset;
312c004d 96 struct kset_uevent_ops *uevent_ops;
5f123fbd 97 u64 seq;
1da177e4 98 int i = 0;
542cfce6 99 int retval = 0;
1da177e4 100
5f123fbd
KS
101 pr_debug("%s\n", __FUNCTION__);
102
5f123fbd
KS
103 /* search the kset we belong to */
104 top_kobj = kobj;
ccd490a3 105 while (!top_kobj->kset && top_kobj->parent)
14193fb9 106 top_kobj = top_kobj->parent;
ccd490a3 107
542cfce6
AK
108 if (!top_kobj->kset) {
109 pr_debug("kobject attempted to send uevent without kset!\n");
110 return -EINVAL;
111 }
1da177e4 112
5f123fbd 113 kset = top_kobj->kset;
312c004d 114 uevent_ops = kset->uevent_ops;
1da177e4 115
7eff2e7a 116 /* skip the event, if the filter returns zero. */
312c004d 117 if (uevent_ops && uevent_ops->filter)
542cfce6
AK
118 if (!uevent_ops->filter(kset, kobj)) {
119 pr_debug("kobject filter function caused the event to drop!\n");
120 return 0;
121 }
1da177e4 122
86406245
KS
123 /* originating subsystem */
124 if (uevent_ops && uevent_ops->name)
125 subsystem = uevent_ops->name(kset, kobj);
126 else
127 subsystem = kobject_name(&kset->kobj);
128 if (!subsystem) {
3a4fa0a2 129 pr_debug("unset subsystem caused the event to drop!\n");
86406245
KS
130 return 0;
131 }
132
7eff2e7a
KS
133 /* environment buffer */
134 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
135 if (!env)
542cfce6 136 return -ENOMEM;
1da177e4 137
5f123fbd
KS
138 /* complete object path */
139 devpath = kobject_get_path(kobj, GFP_KERNEL);
542cfce6
AK
140 if (!devpath) {
141 retval = -ENOENT;
5f123fbd 142 goto exit;
542cfce6 143 }
1da177e4 144
5f123fbd 145 /* default keys */
7eff2e7a
KS
146 retval = add_uevent_var(env, "ACTION=%s", action_string);
147 if (retval)
148 goto exit;
149 retval = add_uevent_var(env, "DEVPATH=%s", devpath);
150 if (retval)
151 goto exit;
152 retval = add_uevent_var(env, "SUBSYSTEM=%s", subsystem);
153 if (retval)
154 goto exit;
155
156 /* keys passed in from the caller */
157 if (envp_ext) {
158 for (i = 0; envp_ext[i]; i++) {
159 retval = add_uevent_var(env, envp_ext[i]);
160 if (retval)
161 goto exit;
162 }
163 }
1da177e4 164
5f123fbd 165 /* let the kset specific function add its stuff */
312c004d 166 if (uevent_ops && uevent_ops->uevent) {
7eff2e7a 167 retval = uevent_ops->uevent(kset, kobj, env);
1da177e4 168 if (retval) {
312c004d 169 pr_debug ("%s - uevent() returned %d\n",
1da177e4
LT
170 __FUNCTION__, retval);
171 goto exit;
172 }
173 }
174
7eff2e7a 175 /* we will send an event, so request a new sequence number */
1da177e4 176 spin_lock(&sequence_lock);
312c004d 177 seq = ++uevent_seqnum;
1da177e4 178 spin_unlock(&sequence_lock);
7eff2e7a
KS
179 retval = add_uevent_var(env, "SEQNUM=%llu", (unsigned long long)seq);
180 if (retval)
181 goto exit;
1da177e4 182
4d17ffda 183#if defined(CONFIG_NET)
5f123fbd
KS
184 /* send netlink message */
185 if (uevent_sock) {
186 struct sk_buff *skb;
187 size_t len;
188
189 /* allocate message with the maximum possible size */
190 len = strlen(action_string) + strlen(devpath) + 2;
7eff2e7a 191 skb = alloc_skb(len + env->buflen, GFP_KERNEL);
5f123fbd 192 if (skb) {
7eff2e7a
KS
193 char *scratch;
194
5f123fbd
KS
195 /* add header */
196 scratch = skb_put(skb, len);
197 sprintf(scratch, "%s@%s", action_string, devpath);
198
199 /* copy keys to our continuous event payload buffer */
7eff2e7a
KS
200 for (i = 0; i < env->envp_idx; i++) {
201 len = strlen(env->envp[i]) + 1;
5f123fbd 202 scratch = skb_put(skb, len);
7eff2e7a 203 strcpy(scratch, env->envp[i]);
5f123fbd
KS
204 }
205
206 NETLINK_CB(skb).dst_group = 1;
207 netlink_broadcast(uevent_sock, skb, 0, 1, GFP_KERNEL);
208 }
209 }
4d17ffda 210#endif
1da177e4 211
5f123fbd 212 /* call uevent_helper, usually only enabled during early boot */
312c004d 213 if (uevent_helper[0]) {
5f123fbd 214 char *argv [3];
1da177e4 215
312c004d 216 argv [0] = uevent_helper;
5f123fbd
KS
217 argv [1] = (char *)subsystem;
218 argv [2] = NULL;
7eff2e7a
KS
219 retval = add_uevent_var(env, "HOME=/");
220 if (retval)
221 goto exit;
222 retval = add_uevent_var(env, "PATH=/sbin:/bin:/usr/sbin:/usr/bin");
223 if (retval)
224 goto exit;
225
226 call_usermodehelper (argv[0], argv, env->envp, UMH_WAIT_EXEC);
5f123fbd 227 }
1da177e4
LT
228
229exit:
5f123fbd 230 kfree(devpath);
7eff2e7a 231 kfree(env);
542cfce6 232 return retval;
1da177e4 233}
8a82472f
CH
234
235EXPORT_SYMBOL_GPL(kobject_uevent_env);
236
237/**
238 * kobject_uevent - notify userspace by ending an uevent
239 *
ccd490a3 240 * @action: action that is happening
8a82472f 241 * @kobj: struct kobject that the action is happening to
542cfce6
AK
242 *
243 * Returns 0 if kobject_uevent() is completed with success or the
244 * corresponding error when it fails.
8a82472f 245 */
542cfce6 246int kobject_uevent(struct kobject *kobj, enum kobject_action action)
8a82472f 247{
542cfce6 248 return kobject_uevent_env(kobj, action, NULL);
8a82472f
CH
249}
250
312c004d 251EXPORT_SYMBOL_GPL(kobject_uevent);
1da177e4
LT
252
253/**
7eff2e7a
KS
254 * add_uevent_var - add key value string to the environment buffer
255 * @env: environment buffer structure
256 * @format: printf format for the key=value pair
1da177e4
LT
257 *
258 * Returns 0 if environment variable was added successfully or -ENOMEM
259 * if no space was available.
260 */
7eff2e7a 261int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...)
1da177e4
LT
262{
263 va_list args;
7eff2e7a 264 int len;
1da177e4 265
7eff2e7a
KS
266 if (env->envp_idx >= ARRAY_SIZE(env->envp)) {
267 printk(KERN_ERR "add_uevent_var: too many keys\n");
268 WARN_ON(1);
1da177e4 269 return -ENOMEM;
7eff2e7a 270 }
1da177e4
LT
271
272 va_start(args, format);
7eff2e7a
KS
273 len = vsnprintf(&env->buf[env->buflen],
274 sizeof(env->buf) - env->buflen,
275 format, args);
1da177e4
LT
276 va_end(args);
277
7eff2e7a
KS
278 if (len >= (sizeof(env->buf) - env->buflen)) {
279 printk(KERN_ERR "add_uevent_var: buffer size too small\n");
280 WARN_ON(1);
1da177e4 281 return -ENOMEM;
7eff2e7a 282 }
1da177e4 283
7eff2e7a
KS
284 env->envp[env->envp_idx++] = &env->buf[env->buflen];
285 env->buflen += len + 1;
1da177e4
LT
286 return 0;
287}
312c004d 288EXPORT_SYMBOL_GPL(add_uevent_var);
1da177e4 289
4d17ffda 290#if defined(CONFIG_NET)
5f123fbd
KS
291static int __init kobject_uevent_init(void)
292{
b4b51029
EB
293 uevent_sock = netlink_kernel_create(&init_net, NETLINK_KOBJECT_UEVENT,
294 1, NULL, NULL, THIS_MODULE);
5f123fbd
KS
295 if (!uevent_sock) {
296 printk(KERN_ERR
297 "kobject_uevent: unable to create netlink socket!\n");
298 return -ENODEV;
299 }
300
301 return 0;
302}
303
304postcore_initcall(kobject_uevent_init);
4d17ffda 305#endif