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.
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>
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
;
33 /* the strings here must match the enum in include/linux/kobject.h */
34 static const char *kobject_actions
[] = {
36 [KOBJ_REMOVE
] = "remove",
37 [KOBJ_CHANGE
] = "change",
39 [KOBJ_ONLINE
] = "online",
40 [KOBJ_OFFLINE
] = "offline",
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
;
58 if (count
&& buf
[count
-1] == '\n')
64 for (action
= 0; action
< ARRAY_SIZE(kobject_actions
); action
++) {
65 if (strncmp(kobject_actions
[action
], buf
, count
) != 0)
67 if (kobject_actions
[action
][count
] != '\0')
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
,
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
;
96 struct kset_uevent_ops
*uevent_ops
;
101 pr_debug("kobject: '%s' (%p): %s\n",
102 kobject_name(kobj
), kobj
, __FUNCTION__
);
104 /* search the kset we belong to */
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
,
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__
);
128 /* originating subsystem */
129 if (uevent_ops
&& uevent_ops
->name
)
130 subsystem
= uevent_ops
->name(kset
, kobj
);
132 subsystem
= kobject_name(&kset
->kobj
);
134 pr_debug("kobject: '%s' (%p): %s: unset subsystem caused the "
135 "event to drop!\n", kobject_name(kobj
), kobj
,
140 /* environment buffer */
141 env
= kzalloc(sizeof(struct kobj_uevent_env
), GFP_KERNEL
);
145 /* complete object path */
146 devpath
= kobject_get_path(kobj
, GFP_KERNEL
);
153 retval
= add_uevent_var(env
, "ACTION=%s", action_string
);
156 retval
= add_uevent_var(env
, "DEVPATH=%s", devpath
);
159 retval
= add_uevent_var(env
, "SUBSYSTEM=%s", subsystem
);
163 /* keys passed in from the caller */
165 for (i
= 0; envp_ext
[i
]; i
++) {
166 retval
= add_uevent_var(env
, envp_ext
[i
]);
172 /* let the kset specific function add its stuff */
173 if (uevent_ops
&& uevent_ops
->uevent
) {
174 retval
= uevent_ops
->uevent(kset
, kobj
, env
);
176 pr_debug("kobject: '%s' (%p): %s: uevent() returned "
177 "%d\n", kobject_name(kobj
), kobj
,
178 __FUNCTION__
, retval
);
183 /* we will send an event, so request a new sequence number */
184 spin_lock(&sequence_lock
);
185 seq
= ++uevent_seqnum
;
186 spin_unlock(&sequence_lock
);
187 retval
= add_uevent_var(env
, "SEQNUM=%llu", (unsigned long long)seq
);
191 #if defined(CONFIG_NET)
192 /* send netlink message */
197 /* allocate message with the maximum possible size */
198 len
= strlen(action_string
) + strlen(devpath
) + 2;
199 skb
= alloc_skb(len
+ env
->buflen
, GFP_KERNEL
);
204 scratch
= skb_put(skb
, len
);
205 sprintf(scratch
, "%s@%s", action_string
, devpath
);
207 /* copy keys to our continuous event payload buffer */
208 for (i
= 0; i
< env
->envp_idx
; i
++) {
209 len
= strlen(env
->envp
[i
]) + 1;
210 scratch
= skb_put(skb
, len
);
211 strcpy(scratch
, env
->envp
[i
]);
214 NETLINK_CB(skb
).dst_group
= 1;
215 netlink_broadcast(uevent_sock
, skb
, 0, 1, GFP_KERNEL
);
220 /* call uevent_helper, usually only enabled during early boot */
221 if (uevent_helper
[0]) {
224 argv
[0] = uevent_helper
;
225 argv
[1] = (char *)subsystem
;
227 retval
= add_uevent_var(env
, "HOME=/");
230 retval
= add_uevent_var(env
, "PATH=/sbin:/bin:/usr/sbin:/usr/bin");
234 call_usermodehelper (argv
[0], argv
, env
->envp
, UMH_WAIT_EXEC
);
243 EXPORT_SYMBOL_GPL(kobject_uevent_env
);
246 * kobject_uevent - notify userspace by ending an uevent
248 * @action: action that is happening
249 * @kobj: struct kobject that the action is happening to
251 * Returns 0 if kobject_uevent() is completed with success or the
252 * corresponding error when it fails.
254 int kobject_uevent(struct kobject
*kobj
, enum kobject_action action
)
256 return kobject_uevent_env(kobj
, action
, NULL
);
259 EXPORT_SYMBOL_GPL(kobject_uevent
);
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
266 * Returns 0 if environment variable was added successfully or -ENOMEM
267 * if no space was available.
269 int add_uevent_var(struct kobj_uevent_env
*env
, const char *format
, ...)
274 if (env
->envp_idx
>= ARRAY_SIZE(env
->envp
)) {
275 printk(KERN_ERR
"add_uevent_var: too many keys\n");
280 va_start(args
, format
);
281 len
= vsnprintf(&env
->buf
[env
->buflen
],
282 sizeof(env
->buf
) - env
->buflen
,
286 if (len
>= (sizeof(env
->buf
) - env
->buflen
)) {
287 printk(KERN_ERR
"add_uevent_var: buffer size too small\n");
292 env
->envp
[env
->envp_idx
++] = &env
->buf
[env
->buflen
];
293 env
->buflen
+= len
+ 1;
296 EXPORT_SYMBOL_GPL(add_uevent_var
);
298 #if defined(CONFIG_NET)
299 static int __init
kobject_uevent_init(void)
301 uevent_sock
= netlink_kernel_create(&init_net
, NETLINK_KOBJECT_UEVENT
,
302 1, NULL
, NULL
, THIS_MODULE
);
305 "kobject_uevent: unable to create netlink socket!\n");
312 postcore_initcall(kobject_uevent_init
);