]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - lib/kobject_uevent.c
Kset: remove kset_add function
[mirror_ubuntu-artful-kernel.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
9f66fa2a
GKH
101 pr_debug("kobject: '%s' (%p): %s\n",
102 kobject_name(kobj), kobj, __FUNCTION__);
5f123fbd 103
5f123fbd
KS
104 /* search the kset we belong to */
105 top_kobj = kobj;
ccd490a3 106 while (!top_kobj->kset && top_kobj->parent)
14193fb9 107 top_kobj = top_kobj->parent;
ccd490a3 108
542cfce6 109 if (!top_kobj->kset) {
9f66fa2a
GKH
110 pr_debug("kobject: '%s' (%p): %s: attempted to send uevent "
111 "without kset!\n", kobject_name(kobj), kobj,
112 __FUNCTION__);
542cfce6
AK
113 return -EINVAL;
114 }
1da177e4 115
5f123fbd 116 kset = top_kobj->kset;
312c004d 117 uevent_ops = kset->uevent_ops;
1da177e4 118
7eff2e7a 119 /* skip the event, if the filter returns zero. */
312c004d 120 if (uevent_ops && uevent_ops->filter)
542cfce6 121 if (!uevent_ops->filter(kset, kobj)) {
9f66fa2a
GKH
122 pr_debug("kobject: '%s' (%p): %s: filter function "
123 "caused the event to drop!\n",
124 kobject_name(kobj), kobj, __FUNCTION__);
542cfce6
AK
125 return 0;
126 }
1da177e4 127
86406245
KS
128 /* originating subsystem */
129 if (uevent_ops && uevent_ops->name)
130 subsystem = uevent_ops->name(kset, kobj);
131 else
132 subsystem = kobject_name(&kset->kobj);
133 if (!subsystem) {
9f66fa2a
GKH
134 pr_debug("kobject: '%s' (%p): %s: unset subsystem caused the "
135 "event to drop!\n", kobject_name(kobj), kobj,
136 __FUNCTION__);
86406245
KS
137 return 0;
138 }
139
7eff2e7a
KS
140 /* environment buffer */
141 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
142 if (!env)
542cfce6 143 return -ENOMEM;
1da177e4 144
5f123fbd
KS
145 /* complete object path */
146 devpath = kobject_get_path(kobj, GFP_KERNEL);
542cfce6
AK
147 if (!devpath) {
148 retval = -ENOENT;
5f123fbd 149 goto exit;
542cfce6 150 }
1da177e4 151
5f123fbd 152 /* default keys */
7eff2e7a
KS
153 retval = add_uevent_var(env, "ACTION=%s", action_string);
154 if (retval)
155 goto exit;
156 retval = add_uevent_var(env, "DEVPATH=%s", devpath);
157 if (retval)
158 goto exit;
159 retval = add_uevent_var(env, "SUBSYSTEM=%s", subsystem);
160 if (retval)
161 goto exit;
162
163 /* keys passed in from the caller */
164 if (envp_ext) {
165 for (i = 0; envp_ext[i]; i++) {
166 retval = add_uevent_var(env, envp_ext[i]);
167 if (retval)
168 goto exit;
169 }
170 }
1da177e4 171
5f123fbd 172 /* let the kset specific function add its stuff */
312c004d 173 if (uevent_ops && uevent_ops->uevent) {
7eff2e7a 174 retval = uevent_ops->uevent(kset, kobj, env);
1da177e4 175 if (retval) {
9f66fa2a
GKH
176 pr_debug("kobject: '%s' (%p): %s: uevent() returned "
177 "%d\n", kobject_name(kobj), kobj,
178 __FUNCTION__, retval);
1da177e4
LT
179 goto exit;
180 }
181 }
182
7eff2e7a 183 /* we will send an event, so request a new sequence number */
1da177e4 184 spin_lock(&sequence_lock);
312c004d 185 seq = ++uevent_seqnum;
1da177e4 186 spin_unlock(&sequence_lock);
7eff2e7a
KS
187 retval = add_uevent_var(env, "SEQNUM=%llu", (unsigned long long)seq);
188 if (retval)
189 goto exit;
1da177e4 190
4d17ffda 191#if defined(CONFIG_NET)
5f123fbd
KS
192 /* send netlink message */
193 if (uevent_sock) {
194 struct sk_buff *skb;
195 size_t len;
196
197 /* allocate message with the maximum possible size */
198 len = strlen(action_string) + strlen(devpath) + 2;
7eff2e7a 199 skb = alloc_skb(len + env->buflen, GFP_KERNEL);
5f123fbd 200 if (skb) {
7eff2e7a
KS
201 char *scratch;
202
5f123fbd
KS
203 /* add header */
204 scratch = skb_put(skb, len);
205 sprintf(scratch, "%s@%s", action_string, devpath);
206
207 /* copy keys to our continuous event payload buffer */
7eff2e7a
KS
208 for (i = 0; i < env->envp_idx; i++) {
209 len = strlen(env->envp[i]) + 1;
5f123fbd 210 scratch = skb_put(skb, len);
7eff2e7a 211 strcpy(scratch, env->envp[i]);
5f123fbd
KS
212 }
213
214 NETLINK_CB(skb).dst_group = 1;
215 netlink_broadcast(uevent_sock, skb, 0, 1, GFP_KERNEL);
216 }
217 }
4d17ffda 218#endif
1da177e4 219
5f123fbd 220 /* call uevent_helper, usually only enabled during early boot */
312c004d 221 if (uevent_helper[0]) {
5f123fbd 222 char *argv [3];
1da177e4 223
312c004d 224 argv [0] = uevent_helper;
5f123fbd
KS
225 argv [1] = (char *)subsystem;
226 argv [2] = NULL;
7eff2e7a
KS
227 retval = add_uevent_var(env, "HOME=/");
228 if (retval)
229 goto exit;
230 retval = add_uevent_var(env, "PATH=/sbin:/bin:/usr/sbin:/usr/bin");
231 if (retval)
232 goto exit;
233
234 call_usermodehelper (argv[0], argv, env->envp, UMH_WAIT_EXEC);
5f123fbd 235 }
1da177e4
LT
236
237exit:
5f123fbd 238 kfree(devpath);
7eff2e7a 239 kfree(env);
542cfce6 240 return retval;
1da177e4 241}
8a82472f
CH
242
243EXPORT_SYMBOL_GPL(kobject_uevent_env);
244
245/**
246 * kobject_uevent - notify userspace by ending an uevent
247 *
ccd490a3 248 * @action: action that is happening
8a82472f 249 * @kobj: struct kobject that the action is happening to
542cfce6
AK
250 *
251 * Returns 0 if kobject_uevent() is completed with success or the
252 * corresponding error when it fails.
8a82472f 253 */
542cfce6 254int kobject_uevent(struct kobject *kobj, enum kobject_action action)
8a82472f 255{
542cfce6 256 return kobject_uevent_env(kobj, action, NULL);
8a82472f
CH
257}
258
312c004d 259EXPORT_SYMBOL_GPL(kobject_uevent);
1da177e4
LT
260
261/**
7eff2e7a
KS
262 * add_uevent_var - add key value string to the environment buffer
263 * @env: environment buffer structure
264 * @format: printf format for the key=value pair
1da177e4
LT
265 *
266 * Returns 0 if environment variable was added successfully or -ENOMEM
267 * if no space was available.
268 */
7eff2e7a 269int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...)
1da177e4
LT
270{
271 va_list args;
7eff2e7a 272 int len;
1da177e4 273
7eff2e7a
KS
274 if (env->envp_idx >= ARRAY_SIZE(env->envp)) {
275 printk(KERN_ERR "add_uevent_var: too many keys\n");
276 WARN_ON(1);
1da177e4 277 return -ENOMEM;
7eff2e7a 278 }
1da177e4
LT
279
280 va_start(args, format);
7eff2e7a
KS
281 len = vsnprintf(&env->buf[env->buflen],
282 sizeof(env->buf) - env->buflen,
283 format, args);
1da177e4
LT
284 va_end(args);
285
7eff2e7a
KS
286 if (len >= (sizeof(env->buf) - env->buflen)) {
287 printk(KERN_ERR "add_uevent_var: buffer size too small\n");
288 WARN_ON(1);
1da177e4 289 return -ENOMEM;
7eff2e7a 290 }
1da177e4 291
7eff2e7a
KS
292 env->envp[env->envp_idx++] = &env->buf[env->buflen];
293 env->buflen += len + 1;
1da177e4
LT
294 return 0;
295}
312c004d 296EXPORT_SYMBOL_GPL(add_uevent_var);
1da177e4 297
4d17ffda 298#if defined(CONFIG_NET)
5f123fbd
KS
299static int __init kobject_uevent_init(void)
300{
b4b51029
EB
301 uevent_sock = netlink_kernel_create(&init_net, NETLINK_KOBJECT_UEVENT,
302 1, NULL, NULL, THIS_MODULE);
5f123fbd
KS
303 if (!uevent_sock) {
304 printk(KERN_ERR
305 "kobject_uevent: unable to create netlink socket!\n");
306 return -ENODEV;
307 }
308
309 return 0;
310}
311
312postcore_initcall(kobject_uevent_init);
4d17ffda 313#endif