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/string.h>
19 #include <linux/kobject.h>
20 #include <linux/module.h>
22 #include <linux/socket.h>
23 #include <linux/skbuff.h>
24 #include <linux/netlink.h>
29 char uevent_helper
[UEVENT_HELPER_PATH_LEN
] = CONFIG_UEVENT_HELPER_PATH
;
30 static DEFINE_SPINLOCK(sequence_lock
);
31 #if defined(CONFIG_NET)
32 static struct sock
*uevent_sock
;
35 /* the strings here must match the enum in include/linux/kobject.h */
36 static const char *kobject_actions
[] = {
38 [KOBJ_REMOVE
] = "remove",
39 [KOBJ_CHANGE
] = "change",
41 [KOBJ_ONLINE
] = "online",
42 [KOBJ_OFFLINE
] = "offline",
46 * kobject_action_type - translate action string to numeric type
48 * @buf: buffer containing the action string, newline is ignored
49 * @len: length of buffer
50 * @type: pointer to the location to store the action type
52 * Returns 0 if the action string was recognized.
54 int kobject_action_type(const char *buf
, size_t count
,
55 enum kobject_action
*type
)
57 enum kobject_action action
;
60 if (count
&& (buf
[count
-1] == '\n' || buf
[count
-1] == '\0'))
66 for (action
= 0; action
< ARRAY_SIZE(kobject_actions
); action
++) {
67 if (strncmp(kobject_actions
[action
], buf
, count
) != 0)
69 if (kobject_actions
[action
][count
] != '\0')
80 * kobject_uevent_env - send an uevent with environmental data
82 * @action: action that is happening
83 * @kobj: struct kobject that the action is happening to
84 * @envp_ext: pointer to environmental data
86 * Returns 0 if kobject_uevent() is completed with success or the
87 * corresponding error when it fails.
89 int kobject_uevent_env(struct kobject
*kobj
, enum kobject_action action
,
92 struct kobj_uevent_env
*env
;
93 const char *action_string
= kobject_actions
[action
];
94 const char *devpath
= NULL
;
95 const char *subsystem
;
96 struct kobject
*top_kobj
;
98 struct kset_uevent_ops
*uevent_ops
;
103 pr_debug("kobject: '%s' (%p): %s\n",
104 kobject_name(kobj
), kobj
, __func__
);
106 /* search the kset we belong to */
108 while (!top_kobj
->kset
&& top_kobj
->parent
)
109 top_kobj
= top_kobj
->parent
;
111 if (!top_kobj
->kset
) {
112 pr_debug("kobject: '%s' (%p): %s: attempted to send uevent "
113 "without kset!\n", kobject_name(kobj
), kobj
,
118 kset
= top_kobj
->kset
;
119 uevent_ops
= kset
->uevent_ops
;
121 /* skip the event, if uevent_suppress is set*/
122 if (kobj
->uevent_suppress
) {
123 pr_debug("kobject: '%s' (%p): %s: uevent_suppress "
124 "caused the event to drop!\n",
125 kobject_name(kobj
), kobj
, __func__
);
128 /* skip the event, if the filter returns zero. */
129 if (uevent_ops
&& uevent_ops
->filter
)
130 if (!uevent_ops
->filter(kset
, kobj
)) {
131 pr_debug("kobject: '%s' (%p): %s: filter function "
132 "caused the event to drop!\n",
133 kobject_name(kobj
), kobj
, __func__
);
137 /* originating subsystem */
138 if (uevent_ops
&& uevent_ops
->name
)
139 subsystem
= uevent_ops
->name(kset
, kobj
);
141 subsystem
= kobject_name(&kset
->kobj
);
143 pr_debug("kobject: '%s' (%p): %s: unset subsystem caused the "
144 "event to drop!\n", kobject_name(kobj
), kobj
,
149 /* environment buffer */
150 env
= kzalloc(sizeof(struct kobj_uevent_env
), GFP_KERNEL
);
154 /* complete object path */
155 devpath
= kobject_get_path(kobj
, GFP_KERNEL
);
162 retval
= add_uevent_var(env
, "ACTION=%s", action_string
);
165 retval
= add_uevent_var(env
, "DEVPATH=%s", devpath
);
168 retval
= add_uevent_var(env
, "SUBSYSTEM=%s", subsystem
);
172 /* keys passed in from the caller */
174 for (i
= 0; envp_ext
[i
]; i
++) {
175 retval
= add_uevent_var(env
, "%s", envp_ext
[i
]);
181 /* let the kset specific function add its stuff */
182 if (uevent_ops
&& uevent_ops
->uevent
) {
183 retval
= uevent_ops
->uevent(kset
, kobj
, env
);
185 pr_debug("kobject: '%s' (%p): %s: uevent() returned "
186 "%d\n", kobject_name(kobj
), kobj
,
193 * Mark "add" and "remove" events in the object to ensure proper
194 * events to userspace during automatic cleanup. If the object did
195 * send an "add" event, "remove" will automatically generated by
196 * the core, if not already done by the caller.
198 if (action
== KOBJ_ADD
)
199 kobj
->state_add_uevent_sent
= 1;
200 else if (action
== KOBJ_REMOVE
)
201 kobj
->state_remove_uevent_sent
= 1;
203 /* we will send an event, so request a new sequence number */
204 spin_lock(&sequence_lock
);
205 seq
= ++uevent_seqnum
;
206 spin_unlock(&sequence_lock
);
207 retval
= add_uevent_var(env
, "SEQNUM=%llu", (unsigned long long)seq
);
211 #if defined(CONFIG_NET)
212 /* send netlink message */
217 /* allocate message with the maximum possible size */
218 len
= strlen(action_string
) + strlen(devpath
) + 2;
219 skb
= alloc_skb(len
+ env
->buflen
, GFP_KERNEL
);
224 scratch
= skb_put(skb
, len
);
225 sprintf(scratch
, "%s@%s", action_string
, devpath
);
227 /* copy keys to our continuous event payload buffer */
228 for (i
= 0; i
< env
->envp_idx
; i
++) {
229 len
= strlen(env
->envp
[i
]) + 1;
230 scratch
= skb_put(skb
, len
);
231 strcpy(scratch
, env
->envp
[i
]);
234 NETLINK_CB(skb
).dst_group
= 1;
235 retval
= netlink_broadcast(uevent_sock
, skb
, 0, 1,
237 /* ENOBUFS should be handled in userspace */
238 if (retval
== -ENOBUFS
)
245 /* call uevent_helper, usually only enabled during early boot */
246 if (uevent_helper
[0]) {
249 argv
[0] = uevent_helper
;
250 argv
[1] = (char *)subsystem
;
252 retval
= add_uevent_var(env
, "HOME=/");
255 retval
= add_uevent_var(env
,
256 "PATH=/sbin:/bin:/usr/sbin:/usr/bin");
260 retval
= call_usermodehelper(argv
[0], argv
,
261 env
->envp
, UMH_NO_WAIT
);
269 EXPORT_SYMBOL_GPL(kobject_uevent_env
);
272 * kobject_uevent - notify userspace by ending an uevent
274 * @action: action that is happening
275 * @kobj: struct kobject that the action is happening to
277 * Returns 0 if kobject_uevent() is completed with success or the
278 * corresponding error when it fails.
280 int kobject_uevent(struct kobject
*kobj
, enum kobject_action action
)
282 return kobject_uevent_env(kobj
, action
, NULL
);
284 EXPORT_SYMBOL_GPL(kobject_uevent
);
287 * add_uevent_var - add key value string to the environment buffer
288 * @env: environment buffer structure
289 * @format: printf format for the key=value pair
291 * Returns 0 if environment variable was added successfully or -ENOMEM
292 * if no space was available.
294 int add_uevent_var(struct kobj_uevent_env
*env
, const char *format
, ...)
299 if (env
->envp_idx
>= ARRAY_SIZE(env
->envp
)) {
300 WARN(1, KERN_ERR
"add_uevent_var: too many keys\n");
304 va_start(args
, format
);
305 len
= vsnprintf(&env
->buf
[env
->buflen
],
306 sizeof(env
->buf
) - env
->buflen
,
310 if (len
>= (sizeof(env
->buf
) - env
->buflen
)) {
311 WARN(1, KERN_ERR
"add_uevent_var: buffer size too small\n");
315 env
->envp
[env
->envp_idx
++] = &env
->buf
[env
->buflen
];
316 env
->buflen
+= len
+ 1;
319 EXPORT_SYMBOL_GPL(add_uevent_var
);
321 #if defined(CONFIG_NET)
322 static int __init
kobject_uevent_init(void)
324 uevent_sock
= netlink_kernel_create(&init_net
, NETLINK_KOBJECT_UEVENT
,
325 1, NULL
, NULL
, THIS_MODULE
);
328 "kobject_uevent: unable to create netlink socket!\n");
335 postcore_initcall(kobject_uevent_init
);