blob: a8efb48dca5414a5f5c03b1d8d239e52f3d8f68b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/kobject.h>
23#include <net/sock.h>
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Cornelia Huckcd030c42007-07-20 13:58:13 +020026u64 uevent_seqnum;
Kay Sievers6a8d8ab2007-08-15 15:38:28 +020027char uevent_helper[UEVENT_HELPER_PATH_LEN] = CONFIG_UEVENT_HELPER_PATH;
Cornelia Huckcd030c42007-07-20 13:58:13 +020028static DEFINE_SPINLOCK(sequence_lock);
29#if defined(CONFIG_NET)
30static struct sock *uevent_sock;
31#endif
32
Kay Sievers5c5daf62007-08-12 20:43:55 +020033/* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/**
Cornelia Huck8a824722006-11-20 17:07:51 +010078 * kobject_uevent_env - send an uevent with environmental data
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 *
Cornelia Huck8a824722006-11-20 17:07:51 +010080 * @action: action that is happening (usually KOBJ_MOVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 * @kobj: struct kobject that the action is happening to
Cornelia Huck8a824722006-11-20 17:07:51 +010082 * @envp_ext: pointer to environmental data
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -080083 *
84 * Returns 0 if kobject_uevent() is completed with success or the
85 * corresponding error when it fails.
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 */
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -080087int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
Kay Sievers7eff2e72007-08-14 15:15:12 +020088 char *envp_ext[])
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Kay Sievers7eff2e72007-08-14 15:15:12 +020090 struct kobj_uevent_env *env;
91 const char *action_string = kobject_actions[action];
Kay Sievers5f123fb2005-11-11 14:43:07 +010092 const char *devpath = NULL;
93 const char *subsystem;
94 struct kobject *top_kobj;
95 struct kset *kset;
Kay Sievers312c0042005-11-16 09:00:00 +010096 struct kset_uevent_ops *uevent_ops;
Kay Sievers5f123fb2005-11-11 14:43:07 +010097 u64 seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 int i = 0;
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -080099 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Kay Sievers5f123fb2005-11-11 14:43:07 +0100101 pr_debug("%s\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Kay Sievers5f123fb2005-11-11 14:43:07 +0100103 /* search the kset we belong to */
104 top_kobj = kobj;
John Anthony Kazos Jr14193fb2007-04-04 07:39:17 -0400105 while (!top_kobj->kset && top_kobj->parent) {
106 top_kobj = top_kobj->parent;
Kay Sievers5f123fb2005-11-11 14:43:07 +0100107 }
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800108 if (!top_kobj->kset) {
109 pr_debug("kobject attempted to send uevent without kset!\n");
110 return -EINVAL;
111 }
Kay Sievers5f123fb2005-11-11 14:43:07 +0100112
113 kset = top_kobj->kset;
Kay Sievers312c0042005-11-16 09:00:00 +0100114 uevent_ops = kset->uevent_ops;
Kay Sievers5f123fb2005-11-11 14:43:07 +0100115
Kay Sievers7eff2e72007-08-14 15:15:12 +0200116 /* skip the event, if the filter returns zero. */
Kay Sievers312c0042005-11-16 09:00:00 +0100117 if (uevent_ops && uevent_ops->filter)
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800118 if (!uevent_ops->filter(kset, kobj)) {
119 pr_debug("kobject filter function caused the event to drop!\n");
120 return 0;
121 }
Kay Sievers5f123fb2005-11-11 14:43:07 +0100122
Kay Sievers86406242007-03-14 03:25:56 +0100123 /* 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) {
129 pr_debug("unset subsytem caused the event to drop!\n");
130 return 0;
131 }
132
Kay Sievers7eff2e72007-08-14 15:15:12 +0200133 /* environment buffer */
134 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
135 if (!env)
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800136 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Kay Sievers5f123fb2005-11-11 14:43:07 +0100138 /* complete object path */
139 devpath = kobject_get_path(kobj, GFP_KERNEL);
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800140 if (!devpath) {
141 retval = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 goto exit;
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Kay Sievers5f123fb2005-11-11 14:43:07 +0100145 /* default keys */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200146 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Kay Sievers5f123fb2005-11-11 14:43:07 +0100165 /* let the kset specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100166 if (uevent_ops && uevent_ops->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200167 retval = uevent_ops->uevent(kset, kobj, env);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 if (retval) {
Kay Sievers312c0042005-11-16 09:00:00 +0100169 pr_debug ("%s - uevent() returned %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 __FUNCTION__, retval);
171 goto exit;
172 }
173 }
174
Kay Sievers7eff2e72007-08-14 15:15:12 +0200175 /* we will send an event, so request a new sequence number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 spin_lock(&sequence_lock);
Kay Sievers312c0042005-11-16 09:00:00 +0100177 seq = ++uevent_seqnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 spin_unlock(&sequence_lock);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200179 retval = add_uevent_var(env, "SEQNUM=%llu", (unsigned long long)seq);
180 if (retval)
181 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200183#if defined(CONFIG_NET)
Kay Sievers5f123fb2005-11-11 14:43:07 +0100184 /* send netlink message */
185 if (uevent_sock) {
186 struct sk_buff *skb;
187 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Kay Sievers5f123fb2005-11-11 14:43:07 +0100189 /* allocate message with the maximum possible size */
190 len = strlen(action_string) + strlen(devpath) + 2;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200191 skb = alloc_skb(len + env->buflen, GFP_KERNEL);
Kay Sievers5f123fb2005-11-11 14:43:07 +0100192 if (skb) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200193 char *scratch;
194
Kay Sievers5f123fb2005-11-11 14:43:07 +0100195 /* add header */
196 scratch = skb_put(skb, len);
197 sprintf(scratch, "%s@%s", action_string, devpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Kay Sievers5f123fb2005-11-11 14:43:07 +0100199 /* copy keys to our continuous event payload buffer */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200200 for (i = 0; i < env->envp_idx; i++) {
201 len = strlen(env->envp[i]) + 1;
Kay Sievers5f123fb2005-11-11 14:43:07 +0100202 scratch = skb_put(skb, len);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200203 strcpy(scratch, env->envp[i]);
Kay Sievers5f123fb2005-11-11 14:43:07 +0100204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Kay Sievers5f123fb2005-11-11 14:43:07 +0100206 NETLINK_CB(skb).dst_group = 1;
207 netlink_broadcast(uevent_sock, skb, 0, 1, GFP_KERNEL);
208 }
209 }
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200210#endif
Kay Sievers5f123fb2005-11-11 14:43:07 +0100211
212 /* call uevent_helper, usually only enabled during early boot */
Kay Sievers312c0042005-11-16 09:00:00 +0100213 if (uevent_helper[0]) {
Kay Sievers5f123fb2005-11-11 14:43:07 +0100214 char *argv [3];
215
Kay Sievers312c0042005-11-16 09:00:00 +0100216 argv [0] = uevent_helper;
Kay Sievers5f123fb2005-11-11 14:43:07 +0100217 argv [1] = (char *)subsystem;
218 argv [2] = NULL;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200219 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);
Kay Sievers5f123fb2005-11-11 14:43:07 +0100227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229exit:
Kay Sievers5f123fb2005-11-11 14:43:07 +0100230 kfree(devpath);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200231 kfree(env);
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800232 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
Cornelia Huck8a824722006-11-20 17:07:51 +0100234
235EXPORT_SYMBOL_GPL(kobject_uevent_env);
236
237/**
238 * kobject_uevent - notify userspace by ending an uevent
239 *
240 * @action: action that is happening (usually KOBJ_ADD and KOBJ_REMOVE)
241 * @kobj: struct kobject that the action is happening to
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800242 *
243 * Returns 0 if kobject_uevent() is completed with success or the
244 * corresponding error when it fails.
Cornelia Huck8a824722006-11-20 17:07:51 +0100245 */
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800246int kobject_uevent(struct kobject *kobj, enum kobject_action action)
Cornelia Huck8a824722006-11-20 17:07:51 +0100247{
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800248 return kobject_uevent_env(kobj, action, NULL);
Cornelia Huck8a824722006-11-20 17:07:51 +0100249}
250
Kay Sievers312c0042005-11-16 09:00:00 +0100251EXPORT_SYMBOL_GPL(kobject_uevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253/**
Kay Sievers7eff2e72007-08-14 15:15:12 +0200254 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 *
258 * Returns 0 if environment variable was added successfully or -ENOMEM
259 * if no space was available.
260 */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200261int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
263 va_list args;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200264 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Kay Sievers7eff2e72007-08-14 15:15:12 +0200266 if (env->envp_idx >= ARRAY_SIZE(env->envp)) {
267 printk(KERN_ERR "add_uevent_var: too many keys\n");
268 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 va_start(args, format);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200273 len = vsnprintf(&env->buf[env->buflen],
274 sizeof(env->buf) - env->buflen,
275 format, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 va_end(args);
277
Kay Sievers7eff2e72007-08-14 15:15:12 +0200278 if (len >= (sizeof(env->buf) - env->buflen)) {
279 printk(KERN_ERR "add_uevent_var: buffer size too small\n");
280 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Kay Sievers7eff2e72007-08-14 15:15:12 +0200284 env->envp[env->envp_idx++] = &env->buf[env->buflen];
285 env->buflen += len + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return 0;
287}
Kay Sievers312c0042005-11-16 09:00:00 +0100288EXPORT_SYMBOL_GPL(add_uevent_var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200290#if defined(CONFIG_NET)
Kay Sievers5f123fb2005-11-11 14:43:07 +0100291static int __init kobject_uevent_init(void)
292{
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200293 uevent_sock = netlink_kernel_create(&init_net, NETLINK_KOBJECT_UEVENT,
294 1, NULL, NULL, THIS_MODULE);
Kay Sievers5f123fb2005-11-11 14:43:07 +0100295 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);
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200305#endif