rc80211-pid: export human-readable target_pf value to debugfs
[linux-2.6/next.git] / lib / kobject_uevent.c
blob5a402e2982afb4a1a82a28092260bb66bd921269
1 /*
2 * kernel userspace event delivery
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.
8 * Licensed under the GNU GPL v2.
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>
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>
22 #include <linux/kobject.h>
23 #include <net/sock.h>
26 u64 uevent_seqnum;
27 char uevent_helper[UEVENT_HELPER_PATH_LEN] = CONFIG_UEVENT_HELPER_PATH;
28 static DEFINE_SPINLOCK(sequence_lock);
29 #if defined(CONFIG_NET)
30 static struct sock *uevent_sock;
31 #endif
33 /* the strings here must match the enum in include/linux/kobject.h */
34 static 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",
43 /**
44 * kobject_action_type - translate action string to numeric type
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
50 * Returns 0 if the action string was recognized.
52 int kobject_action_type(const char *buf, size_t count,
53 enum kobject_action *type)
55 enum kobject_action action;
56 int ret = -EINVAL;
58 if (count && buf[count-1] == '\n')
59 count--;
61 if (!count)
62 goto out;
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;
73 out:
74 return ret;
77 /**
78 * kobject_uevent_env - send an uevent with environmental data
80 * @action: action that is happening
81 * @kobj: struct kobject that the action is happening to
82 * @envp_ext: pointer to environmental data
84 * Returns 0 if kobject_uevent() is completed with success or the
85 * corresponding error when it fails.
87 int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
88 char *envp_ext[])
90 struct kobj_uevent_env *env;
91 const char *action_string = kobject_actions[action];
92 const char *devpath = NULL;
93 const char *subsystem;
94 struct kobject *top_kobj;
95 struct kset *kset;
96 struct kset_uevent_ops *uevent_ops;
97 u64 seq;
98 int i = 0;
99 int retval = 0;
101 pr_debug("kobject: '%s' (%p): %s\n",
102 kobject_name(kobj), kobj, __FUNCTION__);
104 /* search the kset we belong to */
105 top_kobj = kobj;
106 while (!top_kobj->kset && top_kobj->parent)
107 top_kobj = top_kobj->parent;
109 if (!top_kobj->kset) {
110 pr_debug("kobject: '%s' (%p): %s: attempted to send uevent "
111 "without kset!\n", kobject_name(kobj), kobj,
112 __FUNCTION__);
113 return -EINVAL;
116 kset = top_kobj->kset;
117 uevent_ops = kset->uevent_ops;
119 /* skip the event, if the filter returns zero. */
120 if (uevent_ops && uevent_ops->filter)
121 if (!uevent_ops->filter(kset, kobj)) {
122 pr_debug("kobject: '%s' (%p): %s: filter function "
123 "caused the event to drop!\n",
124 kobject_name(kobj), kobj, __FUNCTION__);
125 return 0;
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) {
134 pr_debug("kobject: '%s' (%p): %s: unset subsystem caused the "
135 "event to drop!\n", kobject_name(kobj), kobj,
136 __FUNCTION__);
137 return 0;
140 /* environment buffer */
141 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
142 if (!env)
143 return -ENOMEM;
145 /* complete object path */
146 devpath = kobject_get_path(kobj, GFP_KERNEL);
147 if (!devpath) {
148 retval = -ENOENT;
149 goto exit;
152 /* default keys */
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;
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;
172 /* let the kset specific function add its stuff */
173 if (uevent_ops && uevent_ops->uevent) {
174 retval = uevent_ops->uevent(kset, kobj, env);
175 if (retval) {
176 pr_debug("kobject: '%s' (%p): %s: uevent() returned "
177 "%d\n", kobject_name(kobj), kobj,
178 __FUNCTION__, retval);
179 goto exit;
184 * Mark "add" and "remove" events in the object to ensure proper
185 * events to userspace during automatic cleanup. If the object did
186 * send an "add" event, "remove" will automatically generated by
187 * the core, if not already done by the caller.
189 if (action == KOBJ_ADD)
190 kobj->state_add_uevent_sent = 1;
191 else if (action == KOBJ_REMOVE)
192 kobj->state_remove_uevent_sent = 1;
194 /* we will send an event, so request a new sequence number */
195 spin_lock(&sequence_lock);
196 seq = ++uevent_seqnum;
197 spin_unlock(&sequence_lock);
198 retval = add_uevent_var(env, "SEQNUM=%llu", (unsigned long long)seq);
199 if (retval)
200 goto exit;
202 #if defined(CONFIG_NET)
203 /* send netlink message */
204 if (uevent_sock) {
205 struct sk_buff *skb;
206 size_t len;
208 /* allocate message with the maximum possible size */
209 len = strlen(action_string) + strlen(devpath) + 2;
210 skb = alloc_skb(len + env->buflen, GFP_KERNEL);
211 if (skb) {
212 char *scratch;
214 /* add header */
215 scratch = skb_put(skb, len);
216 sprintf(scratch, "%s@%s", action_string, devpath);
218 /* copy keys to our continuous event payload buffer */
219 for (i = 0; i < env->envp_idx; i++) {
220 len = strlen(env->envp[i]) + 1;
221 scratch = skb_put(skb, len);
222 strcpy(scratch, env->envp[i]);
225 NETLINK_CB(skb).dst_group = 1;
226 netlink_broadcast(uevent_sock, skb, 0, 1, GFP_KERNEL);
229 #endif
231 /* call uevent_helper, usually only enabled during early boot */
232 if (uevent_helper[0]) {
233 char *argv [3];
235 argv [0] = uevent_helper;
236 argv [1] = (char *)subsystem;
237 argv [2] = NULL;
238 retval = add_uevent_var(env, "HOME=/");
239 if (retval)
240 goto exit;
241 retval = add_uevent_var(env,
242 "PATH=/sbin:/bin:/usr/sbin:/usr/bin");
243 if (retval)
244 goto exit;
246 call_usermodehelper(argv[0], argv, env->envp, UMH_WAIT_EXEC);
249 exit:
250 kfree(devpath);
251 kfree(env);
252 return retval;
254 EXPORT_SYMBOL_GPL(kobject_uevent_env);
257 * kobject_uevent - notify userspace by ending an uevent
259 * @action: action that is happening
260 * @kobj: struct kobject that the action is happening to
262 * Returns 0 if kobject_uevent() is completed with success or the
263 * corresponding error when it fails.
265 int kobject_uevent(struct kobject *kobj, enum kobject_action action)
267 return kobject_uevent_env(kobj, action, NULL);
269 EXPORT_SYMBOL_GPL(kobject_uevent);
272 * add_uevent_var - add key value string to the environment buffer
273 * @env: environment buffer structure
274 * @format: printf format for the key=value pair
276 * Returns 0 if environment variable was added successfully or -ENOMEM
277 * if no space was available.
279 int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...)
281 va_list args;
282 int len;
284 if (env->envp_idx >= ARRAY_SIZE(env->envp)) {
285 printk(KERN_ERR "add_uevent_var: too many keys\n");
286 WARN_ON(1);
287 return -ENOMEM;
290 va_start(args, format);
291 len = vsnprintf(&env->buf[env->buflen],
292 sizeof(env->buf) - env->buflen,
293 format, args);
294 va_end(args);
296 if (len >= (sizeof(env->buf) - env->buflen)) {
297 printk(KERN_ERR "add_uevent_var: buffer size too small\n");
298 WARN_ON(1);
299 return -ENOMEM;
302 env->envp[env->envp_idx++] = &env->buf[env->buflen];
303 env->buflen += len + 1;
304 return 0;
306 EXPORT_SYMBOL_GPL(add_uevent_var);
308 #if defined(CONFIG_NET)
309 static int __init kobject_uevent_init(void)
311 uevent_sock = netlink_kernel_create(&init_net, NETLINK_KOBJECT_UEVENT,
312 1, NULL, NULL, THIS_MODULE);
313 if (!uevent_sock) {
314 printk(KERN_ERR
315 "kobject_uevent: unable to create netlink socket!\n");
316 return -ENODEV;
319 return 0;
322 postcore_initcall(kobject_uevent_init);
323 #endif