blob: 38131028d16f87c9eb2685c08ce9a7af45fa2daa [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>
Denis V. Lunev2d38f9a2008-03-27 14:26:30 -070018#include <linux/string.h>
19#include <linux/kobject.h>
20#include <linux/module.h>
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/socket.h>
23#include <linux/skbuff.h>
24#include <linux/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <net/sock.h>
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Cornelia Huckcd030c42007-07-20 13:58:13 +020028u64 uevent_seqnum;
Kay Sievers6a8d8ab2007-08-15 15:38:28 +020029char uevent_helper[UEVENT_HELPER_PATH_LEN] = CONFIG_UEVENT_HELPER_PATH;
Cornelia Huckcd030c42007-07-20 13:58:13 +020030static DEFINE_SPINLOCK(sequence_lock);
31#if defined(CONFIG_NET)
32static struct sock *uevent_sock;
33#endif
34
Kay Sievers5c5daf62007-08-12 20:43:55 +020035/* the strings here must match the enum in include/linux/kobject.h */
36static const char *kobject_actions[] = {
37 [KOBJ_ADD] = "add",
38 [KOBJ_REMOVE] = "remove",
39 [KOBJ_CHANGE] = "change",
40 [KOBJ_MOVE] = "move",
41 [KOBJ_ONLINE] = "online",
42 [KOBJ_OFFLINE] = "offline",
43};
44
45/**
46 * kobject_action_type - translate action string to numeric type
47 *
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
51 *
52 * Returns 0 if the action string was recognized.
53 */
54int kobject_action_type(const char *buf, size_t count,
55 enum kobject_action *type)
56{
57 enum kobject_action action;
58 int ret = -EINVAL;
59
Mark Lorda9edadb2008-03-28 19:05:25 -040060 if (count && (buf[count-1] == '\n' || buf[count-1] == '\0'))
Kay Sievers5c5daf62007-08-12 20:43:55 +020061 count--;
62
63 if (!count)
64 goto out;
65
66 for (action = 0; action < ARRAY_SIZE(kobject_actions); action++) {
67 if (strncmp(kobject_actions[action], buf, count) != 0)
68 continue;
69 if (kobject_actions[action][count] != '\0')
70 continue;
71 *type = action;
72 ret = 0;
73 break;
74 }
75out:
76 return ret;
77}
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/**
Cornelia Huck8a824722006-11-20 17:07:51 +010080 * kobject_uevent_env - send an uevent with environmental data
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 *
Kay Sieversccd490a2007-08-12 20:43:55 +020082 * @action: action that is happening
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 * @kobj: struct kobject that the action is happening to
Cornelia Huck8a824722006-11-20 17:07:51 +010084 * @envp_ext: pointer to environmental data
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -080085 *
86 * Returns 0 if kobject_uevent() is completed with success or the
87 * corresponding error when it fails.
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 */
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -080089int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
Kay Sievers7eff2e72007-08-14 15:15:12 +020090 char *envp_ext[])
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Kay Sievers7eff2e72007-08-14 15:15:12 +020092 struct kobj_uevent_env *env;
93 const char *action_string = kobject_actions[action];
Kay Sievers5f123fb2005-11-11 14:43:07 +010094 const char *devpath = NULL;
95 const char *subsystem;
96 struct kobject *top_kobj;
97 struct kset *kset;
Kay Sievers312c0042005-11-16 09:00:00 +010098 struct kset_uevent_ops *uevent_ops;
Kay Sievers5f123fb2005-11-11 14:43:07 +010099 u64 seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 int i = 0;
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800101 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800103 pr_debug("kobject: '%s' (%p): %s\n",
Harvey Harrison810304d2008-04-30 00:55:08 -0700104 kobject_name(kobj), kobj, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Kay Sievers5f123fb2005-11-11 14:43:07 +0100106 /* search the kset we belong to */
107 top_kobj = kobj;
Kay Sieversccd490a2007-08-12 20:43:55 +0200108 while (!top_kobj->kset && top_kobj->parent)
John Anthony Kazos Jr14193fb2007-04-04 07:39:17 -0400109 top_kobj = top_kobj->parent;
Kay Sieversccd490a2007-08-12 20:43:55 +0200110
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800111 if (!top_kobj->kset) {
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800112 pr_debug("kobject: '%s' (%p): %s: attempted to send uevent "
113 "without kset!\n", kobject_name(kobj), kobj,
Harvey Harrison810304d2008-04-30 00:55:08 -0700114 __func__);
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800115 return -EINVAL;
116 }
Kay Sievers5f123fb2005-11-11 14:43:07 +0100117
118 kset = top_kobj->kset;
Kay Sievers312c0042005-11-16 09:00:00 +0100119 uevent_ops = kset->uevent_ops;
Kay Sievers5f123fb2005-11-11 14:43:07 +0100120
Kay Sievers7eff2e72007-08-14 15:15:12 +0200121 /* skip the event, if the filter returns zero. */
Kay Sievers312c0042005-11-16 09:00:00 +0100122 if (uevent_ops && uevent_ops->filter)
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800123 if (!uevent_ops->filter(kset, kobj)) {
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800124 pr_debug("kobject: '%s' (%p): %s: filter function "
125 "caused the event to drop!\n",
Harvey Harrison810304d2008-04-30 00:55:08 -0700126 kobject_name(kobj), kobj, __func__);
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800127 return 0;
128 }
Kay Sievers5f123fb2005-11-11 14:43:07 +0100129
Kay Sievers86406242007-03-14 03:25:56 +0100130 /* originating subsystem */
131 if (uevent_ops && uevent_ops->name)
132 subsystem = uevent_ops->name(kset, kobj);
133 else
134 subsystem = kobject_name(&kset->kobj);
135 if (!subsystem) {
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800136 pr_debug("kobject: '%s' (%p): %s: unset subsystem caused the "
137 "event to drop!\n", kobject_name(kobj), kobj,
Harvey Harrison810304d2008-04-30 00:55:08 -0700138 __func__);
Kay Sievers86406242007-03-14 03:25:56 +0100139 return 0;
140 }
141
Kay Sievers7eff2e72007-08-14 15:15:12 +0200142 /* environment buffer */
143 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
144 if (!env)
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800145 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Kay Sievers5f123fb2005-11-11 14:43:07 +0100147 /* complete object path */
148 devpath = kobject_get_path(kobj, GFP_KERNEL);
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800149 if (!devpath) {
150 retval = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 goto exit;
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Kay Sievers5f123fb2005-11-11 14:43:07 +0100154 /* default keys */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200155 retval = add_uevent_var(env, "ACTION=%s", action_string);
156 if (retval)
157 goto exit;
158 retval = add_uevent_var(env, "DEVPATH=%s", devpath);
159 if (retval)
160 goto exit;
161 retval = add_uevent_var(env, "SUBSYSTEM=%s", subsystem);
162 if (retval)
163 goto exit;
164
165 /* keys passed in from the caller */
166 if (envp_ext) {
167 for (i = 0; envp_ext[i]; i++) {
Tejun Heoc65b9142008-11-13 13:20:00 +0900168 retval = add_uevent_var(env, "%s", envp_ext[i]);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200169 if (retval)
170 goto exit;
171 }
172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Kay Sievers5f123fb2005-11-11 14:43:07 +0100174 /* let the kset specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100175 if (uevent_ops && uevent_ops->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200176 retval = uevent_ops->uevent(kset, kobj, env);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 if (retval) {
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800178 pr_debug("kobject: '%s' (%p): %s: uevent() returned "
179 "%d\n", kobject_name(kobj), kobj,
Harvey Harrison810304d2008-04-30 00:55:08 -0700180 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 goto exit;
182 }
183 }
184
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100185 /*
186 * Mark "add" and "remove" events in the object to ensure proper
187 * events to userspace during automatic cleanup. If the object did
188 * send an "add" event, "remove" will automatically generated by
189 * the core, if not already done by the caller.
190 */
191 if (action == KOBJ_ADD)
192 kobj->state_add_uevent_sent = 1;
193 else if (action == KOBJ_REMOVE)
194 kobj->state_remove_uevent_sent = 1;
195
Kay Sievers7eff2e72007-08-14 15:15:12 +0200196 /* we will send an event, so request a new sequence number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 spin_lock(&sequence_lock);
Kay Sievers312c0042005-11-16 09:00:00 +0100198 seq = ++uevent_seqnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 spin_unlock(&sequence_lock);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200200 retval = add_uevent_var(env, "SEQNUM=%llu", (unsigned long long)seq);
201 if (retval)
202 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200204#if defined(CONFIG_NET)
Kay Sievers5f123fb2005-11-11 14:43:07 +0100205 /* send netlink message */
206 if (uevent_sock) {
207 struct sk_buff *skb;
208 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Kay Sievers5f123fb2005-11-11 14:43:07 +0100210 /* allocate message with the maximum possible size */
211 len = strlen(action_string) + strlen(devpath) + 2;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200212 skb = alloc_skb(len + env->buflen, GFP_KERNEL);
Kay Sievers5f123fb2005-11-11 14:43:07 +0100213 if (skb) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200214 char *scratch;
215
Kay Sievers5f123fb2005-11-11 14:43:07 +0100216 /* add header */
217 scratch = skb_put(skb, len);
218 sprintf(scratch, "%s@%s", action_string, devpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Kay Sievers5f123fb2005-11-11 14:43:07 +0100220 /* copy keys to our continuous event payload buffer */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200221 for (i = 0; i < env->envp_idx; i++) {
222 len = strlen(env->envp[i]) + 1;
Kay Sievers5f123fb2005-11-11 14:43:07 +0100223 scratch = skb_put(skb, len);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200224 strcpy(scratch, env->envp[i]);
Kay Sievers5f123fb2005-11-11 14:43:07 +0100225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Kay Sievers5f123fb2005-11-11 14:43:07 +0100227 NETLINK_CB(skb).dst_group = 1;
Ming Leie0d7bf52008-11-16 18:23:27 +0800228 retval = netlink_broadcast(uevent_sock, skb, 0, 1,
229 GFP_KERNEL);
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -0800230 /* ENOBUFS should be handled in userspace */
231 if (retval == -ENOBUFS)
232 retval = 0;
Ming Leie0d7bf52008-11-16 18:23:27 +0800233 } else
234 retval = -ENOMEM;
Kay Sievers5f123fb2005-11-11 14:43:07 +0100235 }
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200236#endif
Kay Sievers5f123fb2005-11-11 14:43:07 +0100237
238 /* call uevent_helper, usually only enabled during early boot */
Kay Sievers312c0042005-11-16 09:00:00 +0100239 if (uevent_helper[0]) {
Kay Sievers5f123fb2005-11-11 14:43:07 +0100240 char *argv [3];
241
Kay Sievers312c0042005-11-16 09:00:00 +0100242 argv [0] = uevent_helper;
Kay Sievers5f123fb2005-11-11 14:43:07 +0100243 argv [1] = (char *)subsystem;
244 argv [2] = NULL;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200245 retval = add_uevent_var(env, "HOME=/");
246 if (retval)
247 goto exit;
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800248 retval = add_uevent_var(env,
249 "PATH=/sbin:/bin:/usr/sbin:/usr/bin");
Kay Sievers7eff2e72007-08-14 15:15:12 +0200250 if (retval)
251 goto exit;
252
Wang Chen0ad1d6f2008-06-24 16:59:02 +0800253 retval = call_usermodehelper(argv[0], argv,
254 env->envp, UMH_WAIT_EXEC);
Kay Sievers5f123fb2005-11-11 14:43:07 +0100255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257exit:
Kay Sievers5f123fb2005-11-11 14:43:07 +0100258 kfree(devpath);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200259 kfree(env);
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800260 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
Cornelia Huck8a824722006-11-20 17:07:51 +0100262EXPORT_SYMBOL_GPL(kobject_uevent_env);
263
264/**
265 * kobject_uevent - notify userspace by ending an uevent
266 *
Kay Sieversccd490a2007-08-12 20:43:55 +0200267 * @action: action that is happening
Cornelia Huck8a824722006-11-20 17:07:51 +0100268 * @kobj: struct kobject that the action is happening to
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800269 *
270 * Returns 0 if kobject_uevent() is completed with success or the
271 * corresponding error when it fails.
Cornelia Huck8a824722006-11-20 17:07:51 +0100272 */
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800273int kobject_uevent(struct kobject *kobj, enum kobject_action action)
Cornelia Huck8a824722006-11-20 17:07:51 +0100274{
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800275 return kobject_uevent_env(kobj, action, NULL);
Cornelia Huck8a824722006-11-20 17:07:51 +0100276}
Kay Sievers312c0042005-11-16 09:00:00 +0100277EXPORT_SYMBOL_GPL(kobject_uevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279/**
Kay Sievers7eff2e72007-08-14 15:15:12 +0200280 * add_uevent_var - add key value string to the environment buffer
281 * @env: environment buffer structure
282 * @format: printf format for the key=value pair
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 *
284 * Returns 0 if environment variable was added successfully or -ENOMEM
285 * if no space was available.
286 */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200287int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 va_list args;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200290 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Kay Sievers7eff2e72007-08-14 15:15:12 +0200292 if (env->envp_idx >= ARRAY_SIZE(env->envp)) {
Arjan van de Ven5cd2b452008-07-25 19:45:39 -0700293 WARN(1, KERN_ERR "add_uevent_var: too many keys\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 va_start(args, format);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200298 len = vsnprintf(&env->buf[env->buflen],
299 sizeof(env->buf) - env->buflen,
300 format, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 va_end(args);
302
Kay Sievers7eff2e72007-08-14 15:15:12 +0200303 if (len >= (sizeof(env->buf) - env->buflen)) {
Arjan van de Ven5cd2b452008-07-25 19:45:39 -0700304 WARN(1, KERN_ERR "add_uevent_var: buffer size too small\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Kay Sievers7eff2e72007-08-14 15:15:12 +0200308 env->envp[env->envp_idx++] = &env->buf[env->buflen];
309 env->buflen += len + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return 0;
311}
Kay Sievers312c0042005-11-16 09:00:00 +0100312EXPORT_SYMBOL_GPL(add_uevent_var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200314#if defined(CONFIG_NET)
Kay Sievers5f123fb2005-11-11 14:43:07 +0100315static int __init kobject_uevent_init(void)
316{
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200317 uevent_sock = netlink_kernel_create(&init_net, NETLINK_KOBJECT_UEVENT,
318 1, NULL, NULL, THIS_MODULE);
Kay Sievers5f123fb2005-11-11 14:43:07 +0100319 if (!uevent_sock) {
320 printk(KERN_ERR
321 "kobject_uevent: unable to create netlink socket!\n");
322 return -ENODEV;
323 }
324
325 return 0;
326}
327
328postcore_initcall(kobject_uevent_init);
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200329#endif