kobject_uevent.c 7.8 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * kernel userspace event delivery
 *
 * Copyright (C) 2004 Red Hat, Inc.  All rights reserved.
 * Copyright (C) 2004 Novell, Inc.  All rights reserved.
 * Copyright (C) 2004 IBM, Inc. All rights reserved.
 *
 * Licensed under the GNU GPL v2.
 *
 * Authors:
 *	Robert Love		<rml@novell.com>
 *	Kay Sievers		<kay.sievers@vrfy.org>
 *	Arjan van de Ven	<arjanv@redhat.com>
 *	Greg Kroah-Hartman	<greg@kroah.com>
 */

#include <linux/spinlock.h>
#include <linux/socket.h>
#include <linux/skbuff.h>
#include <linux/netlink.h>
#include <linux/string.h>
#include <linux/kobject.h>
#include <net/sock.h>

25
#define BUFFER_SIZE	2048	/* buffer for the variables */
L
Linus Torvalds 已提交
26 27
#define NUM_ENVP	32	/* number of env pointers */

K
Kay Sievers 已提交
28
#if defined(CONFIG_HOTPLUG)
29 30
u64 uevent_seqnum;
char uevent_helper[UEVENT_HELPER_PATH_LEN] = "/sbin/hotplug";
31
static DEFINE_SPINLOCK(sequence_lock);
K
Kay Sievers 已提交
32
#if defined(CONFIG_NET)
33
static struct sock *uevent_sock;
K
Kay Sievers 已提交
34
#endif
35

L
Linus Torvalds 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48
static char *action_to_string(enum kobject_action action)
{
	switch (action) {
	case KOBJ_ADD:
		return "add";
	case KOBJ_REMOVE:
		return "remove";
	case KOBJ_CHANGE:
		return "change";
	case KOBJ_OFFLINE:
		return "offline";
	case KOBJ_ONLINE:
		return "online";
49 50
	case KOBJ_MOVE:
		return "move";
L
Linus Torvalds 已提交
51 52 53 54 55 56
	default:
		return NULL;
	}
}

/**
57
 * kobject_uevent_env - send an uevent with environmental data
L
Linus Torvalds 已提交
58
 *
59
 * @action: action that is happening (usually KOBJ_MOVE)
L
Linus Torvalds 已提交
60
 * @kobj: struct kobject that the action is happening to
61
 * @envp_ext: pointer to environmental data
62 63 64
 *
 * Returns 0 if kobject_uevent() is completed with success or the
 * corresponding error when it fails.
L
Linus Torvalds 已提交
65
 */
66
int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
67
			char *envp_ext[])
L
Linus Torvalds 已提交
68
{
69 70
	char **envp;
	char *buffer;
L
Linus Torvalds 已提交
71
	char *scratch;
72 73 74 75 76
	const char *action_string;
	const char *devpath = NULL;
	const char *subsystem;
	struct kobject *top_kobj;
	struct kset *kset;
77
	struct kset_uevent_ops *uevent_ops;
78 79
	u64 seq;
	char *seq_buff;
L
Linus Torvalds 已提交
80
	int i = 0;
81
	int retval = 0;
82
	int j;
L
Linus Torvalds 已提交
83

84 85 86
	pr_debug("%s\n", __FUNCTION__);

	action_string = action_to_string(action);
87 88 89 90
	if (!action_string) {
		pr_debug("kobject attempted to send uevent without action_string!\n");
		return -EINVAL;
	}
91 92 93

	/* search the kset we belong to */
	top_kobj = kobj;
94 95
	while (!top_kobj->kset && top_kobj->parent) {
		top_kobj = top_kobj->parent;
L
Linus Torvalds 已提交
96
	}
97 98 99 100
	if (!top_kobj->kset) {
		pr_debug("kobject attempted to send uevent without kset!\n");
		return -EINVAL;
	}
L
Linus Torvalds 已提交
101

102
	kset = top_kobj->kset;
103
	uevent_ops = kset->uevent_ops;
L
Linus Torvalds 已提交
104

105
	/*  skip the event, if the filter returns zero. */
106
	if (uevent_ops && uevent_ops->filter)
107 108 109 110
		if (!uevent_ops->filter(kset, kobj)) {
			pr_debug("kobject filter function caused the event to drop!\n");
			return 0;
		}
L
Linus Torvalds 已提交
111

112 113 114 115 116 117 118 119 120 121
	/* originating subsystem */
	if (uevent_ops && uevent_ops->name)
		subsystem = uevent_ops->name(kset, kobj);
	else
		subsystem = kobject_name(&kset->kobj);
	if (!subsystem) {
		pr_debug("unset subsytem caused the event to drop!\n");
		return 0;
	}

122 123
	/* environment index */
	envp = kzalloc(NUM_ENVP * sizeof (char *), GFP_KERNEL);
L
Linus Torvalds 已提交
124
	if (!envp)
125
		return -ENOMEM;
L
Linus Torvalds 已提交
126

127
	/* environment values */
L
Linus Torvalds 已提交
128
	buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
129 130
	if (!buffer) {
		retval = -ENOMEM;
L
Linus Torvalds 已提交
131
		goto exit;
132
	}
L
Linus Torvalds 已提交
133

134 135
	/* complete object path */
	devpath = kobject_get_path(kobj, GFP_KERNEL);
136 137
	if (!devpath) {
		retval = -ENOENT;
138
		goto exit;
139
	}
L
Linus Torvalds 已提交
140

141 142 143
	/* event environemnt for helper process only */
	envp[i++] = "HOME=/";
	envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
L
Linus Torvalds 已提交
144

145
	/* default keys */
L
Linus Torvalds 已提交
146 147 148 149
	scratch = buffer;
	envp [i++] = scratch;
	scratch += sprintf(scratch, "ACTION=%s", action_string) + 1;
	envp [i++] = scratch;
150
	scratch += sprintf (scratch, "DEVPATH=%s", devpath) + 1;
L
Linus Torvalds 已提交
151
	envp [i++] = scratch;
152
	scratch += sprintf(scratch, "SUBSYSTEM=%s", subsystem) + 1;
153 154
	for (j = 0; envp_ext && envp_ext[j]; j++)
		envp[i++] = envp_ext[j];
155
	/* just reserve the space, overwrite it after kset call has returned */
L
Linus Torvalds 已提交
156 157 158
	envp[i++] = seq_buff = scratch;
	scratch += strlen("SEQNUM=18446744073709551616") + 1;

159
	/* let the kset specific function add its stuff */
160 161
	if (uevent_ops && uevent_ops->uevent) {
		retval = uevent_ops->uevent(kset, kobj,
L
Linus Torvalds 已提交
162 163 164
				  &envp[i], NUM_ENVP - i, scratch,
				  BUFFER_SIZE - (scratch - buffer));
		if (retval) {
165
			pr_debug ("%s - uevent() returned %d\n",
L
Linus Torvalds 已提交
166 167 168 169 170
				  __FUNCTION__, retval);
			goto exit;
		}
	}

171
	/* we will send an event, request a new sequence number */
L
Linus Torvalds 已提交
172
	spin_lock(&sequence_lock);
173
	seq = ++uevent_seqnum;
L
Linus Torvalds 已提交
174 175 176
	spin_unlock(&sequence_lock);
	sprintf(seq_buff, "SEQNUM=%llu", (unsigned long long)seq);

K
Kay Sievers 已提交
177
#if defined(CONFIG_NET)
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
	/* send netlink message */
	if (uevent_sock) {
		struct sk_buff *skb;
		size_t len;

		/* allocate message with the maximum possible size */
		len = strlen(action_string) + strlen(devpath) + 2;
		skb = alloc_skb(len + BUFFER_SIZE, GFP_KERNEL);
		if (skb) {
			/* add header */
			scratch = skb_put(skb, len);
			sprintf(scratch, "%s@%s", action_string, devpath);

			/* copy keys to our continuous event payload buffer */
			for (i = 2; envp[i]; i++) {
				len = strlen(envp[i]) + 1;
				scratch = skb_put(skb, len);
				strcpy(scratch, envp[i]);
			}

			NETLINK_CB(skb).dst_group = 1;
			netlink_broadcast(uevent_sock, skb, 0, 1, GFP_KERNEL);
		}
	}
K
Kay Sievers 已提交
202
#endif
L
Linus Torvalds 已提交
203

204
	/* call uevent_helper, usually only enabled during early boot */
205
	if (uevent_helper[0]) {
206
		char *argv [3];
L
Linus Torvalds 已提交
207

208
		argv [0] = uevent_helper;
209 210
		argv [1] = (char *)subsystem;
		argv [2] = NULL;
211
		call_usermodehelper (argv[0], argv, envp, UMH_WAIT_EXEC);
212
	}
L
Linus Torvalds 已提交
213 214

exit:
215
	kfree(devpath);
L
Linus Torvalds 已提交
216 217
	kfree(buffer);
	kfree(envp);
218
	return retval;
L
Linus Torvalds 已提交
219
}
220 221 222 223 224 225 226 227

EXPORT_SYMBOL_GPL(kobject_uevent_env);

/**
 * kobject_uevent - notify userspace by ending an uevent
 *
 * @action: action that is happening (usually KOBJ_ADD and KOBJ_REMOVE)
 * @kobj: struct kobject that the action is happening to
228 229 230
 *
 * Returns 0 if kobject_uevent() is completed with success or the
 * corresponding error when it fails.
231
 */
232
int kobject_uevent(struct kobject *kobj, enum kobject_action action)
233
{
234
	return kobject_uevent_env(kobj, action, NULL);
235 236
}

237
EXPORT_SYMBOL_GPL(kobject_uevent);
L
Linus Torvalds 已提交
238 239

/**
240
 * add_uevent_var - helper for creating event variables
L
Linus Torvalds 已提交
241
 * @envp: Pointer to table of environment variables, as passed into
242
 * uevent() method.
L
Linus Torvalds 已提交
243
 * @num_envp: Number of environment variable slots available, as
244
 * passed into uevent() method.
L
Linus Torvalds 已提交
245
 * @cur_index: Pointer to current index into @envp.  It should be
246
 * initialized to 0 before the first call to add_uevent_var(),
L
Linus Torvalds 已提交
247 248
 * and will be incremented on success.
 * @buffer: Pointer to buffer for environment variables, as passed
249 250
 * into uevent() method.
 * @buffer_size: Length of @buffer, as passed into uevent() method.
L
Linus Torvalds 已提交
251 252
 * @cur_len: Pointer to current length of space used in @buffer.
 * Should be initialized to 0 before the first call to
253
 * add_uevent_var(), and will be incremented on success.
L
Linus Torvalds 已提交
254 255 256 257 258 259
 * @format: Format for creating environment variable (of the form
 * "XXX=%x") for snprintf().
 *
 * Returns 0 if environment variable was added successfully or -ENOMEM
 * if no space was available.
 */
260 261 262
int add_uevent_var(char **envp, int num_envp, int *cur_index,
		   char *buffer, int buffer_size, int *cur_len,
		   const char *format, ...)
L
Linus Torvalds 已提交
263 264 265 266 267
{
	va_list args;

	/*
	 * We check against num_envp - 1 to make sure there is at
268 269
	 * least one slot left after we return, since kobject_uevent()
	 * needs to set the last slot to NULL.
L
Linus Torvalds 已提交
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
	 */
	if (*cur_index >= num_envp - 1)
		return -ENOMEM;

	envp[*cur_index] = buffer + *cur_len;

	va_start(args, format);
	*cur_len += vsnprintf(envp[*cur_index],
			      max(buffer_size - *cur_len, 0),
			      format, args) + 1;
	va_end(args);

	if (*cur_len > buffer_size)
		return -ENOMEM;

	(*cur_index)++;
	return 0;
}
288
EXPORT_SYMBOL_GPL(add_uevent_var);
L
Linus Torvalds 已提交
289

K
Kay Sievers 已提交
290
#if defined(CONFIG_NET)
291 292 293
static int __init kobject_uevent_init(void)
{
	uevent_sock = netlink_kernel_create(NETLINK_KOBJECT_UEVENT, 1, NULL,
294
					    NULL, THIS_MODULE);
295 296 297 298 299 300 301 302 303 304 305

	if (!uevent_sock) {
		printk(KERN_ERR
		       "kobject_uevent: unable to create netlink socket!\n");
		return -ENODEV;
	}

	return 0;
}

postcore_initcall(kobject_uevent_init);
K
Kay Sievers 已提交
306
#endif
307

L
Linus Torvalds 已提交
308
#endif /* CONFIG_HOTPLUG */