kobject_uevent.c 6.6 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	1024	/* buffer for the variables */
L
Linus Torvalds 已提交
26 27
#define NUM_ENVP	32	/* number of env pointers */

28
#if defined(CONFIG_HOTPLUG)
29 30
char uevent_helper[UEVENT_HELPER_PATH_LEN] = "/sbin/hotplug";
u64 uevent_seqnum;
31
static DEFINE_SPINLOCK(sequence_lock);
32
static struct sock *uevent_sock;
33

L
Linus Torvalds 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
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";
	default:
		return NULL;
	}
}

/**
53
 * kobject_uevent - notify userspace by ending an uevent
L
Linus Torvalds 已提交
54
 *
55
 * @action: action that is happening (usually KOBJ_ADD and KOBJ_REMOVE)
L
Linus Torvalds 已提交
56 57
 * @kobj: struct kobject that the action is happening to
 */
58
void kobject_uevent(struct kobject *kobj, enum kobject_action action)
L
Linus Torvalds 已提交
59
{
60 61
	char **envp;
	char *buffer;
L
Linus Torvalds 已提交
62
	char *scratch;
63 64 65 66 67
	const char *action_string;
	const char *devpath = NULL;
	const char *subsystem;
	struct kobject *top_kobj;
	struct kset *kset;
68
	struct kset_uevent_ops *uevent_ops;
69 70
	u64 seq;
	char *seq_buff;
L
Linus Torvalds 已提交
71 72 73
	int i = 0;
	int retval;

74 75 76 77 78 79 80 81
	pr_debug("%s\n", __FUNCTION__);

	action_string = action_to_string(action);
	if (!action_string)
		return;

	/* search the kset we belong to */
	top_kobj = kobj;
L
Linus Torvalds 已提交
82 83 84 85 86
	if (!top_kobj->kset && top_kobj->parent) {
		do {
			top_kobj = top_kobj->parent;
		} while (!top_kobj->kset && top_kobj->parent);
	}
87
	if (!top_kobj->kset)
L
Linus Torvalds 已提交
88 89
		return;

90
	kset = top_kobj->kset;
91
	uevent_ops = kset->uevent_ops;
L
Linus Torvalds 已提交
92

93
	/*  skip the event, if the filter returns zero. */
94 95
	if (uevent_ops && uevent_ops->filter)
		if (!uevent_ops->filter(kset, kobj))
L
Linus Torvalds 已提交
96 97
			return;

98 99
	/* environment index */
	envp = kzalloc(NUM_ENVP * sizeof (char *), GFP_KERNEL);
L
Linus Torvalds 已提交
100 101 102
	if (!envp)
		return;

103
	/* environment values */
L
Linus Torvalds 已提交
104 105 106 107
	buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
	if (!buffer)
		goto exit;

108 109 110 111
	/* complete object path */
	devpath = kobject_get_path(kobj, GFP_KERNEL);
	if (!devpath)
		goto exit;
L
Linus Torvalds 已提交
112

113
	/* originating subsystem */
114 115
	if (uevent_ops && uevent_ops->name)
		subsystem = uevent_ops->name(kset, kobj);
116 117
	else
		subsystem = kobject_name(&kset->kobj);
L
Linus Torvalds 已提交
118

119 120 121
	/* event environemnt for helper process only */
	envp[i++] = "HOME=/";
	envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
L
Linus Torvalds 已提交
122

123
	/* default keys */
L
Linus Torvalds 已提交
124 125 126 127
	scratch = buffer;
	envp [i++] = scratch;
	scratch += sprintf(scratch, "ACTION=%s", action_string) + 1;
	envp [i++] = scratch;
128
	scratch += sprintf (scratch, "DEVPATH=%s", devpath) + 1;
L
Linus Torvalds 已提交
129
	envp [i++] = scratch;
130
	scratch += sprintf(scratch, "SUBSYSTEM=%s", subsystem) + 1;
L
Linus Torvalds 已提交
131

132
	/* just reserve the space, overwrite it after kset call has returned */
L
Linus Torvalds 已提交
133 134 135
	envp[i++] = seq_buff = scratch;
	scratch += strlen("SEQNUM=18446744073709551616") + 1;

136
	/* let the kset specific function add its stuff */
137 138
	if (uevent_ops && uevent_ops->uevent) {
		retval = uevent_ops->uevent(kset, kobj,
L
Linus Torvalds 已提交
139 140 141
				  &envp[i], NUM_ENVP - i, scratch,
				  BUFFER_SIZE - (scratch - buffer));
		if (retval) {
142
			pr_debug ("%s - uevent() returned %d\n",
L
Linus Torvalds 已提交
143 144 145 146 147
				  __FUNCTION__, retval);
			goto exit;
		}
	}

148
	/* we will send an event, request a new sequence number */
L
Linus Torvalds 已提交
149
	spin_lock(&sequence_lock);
150
	seq = ++uevent_seqnum;
L
Linus Torvalds 已提交
151 152 153
	spin_unlock(&sequence_lock);
	sprintf(seq_buff, "SEQNUM=%llu", (unsigned long long)seq);

154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
	/* 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);
		}
	}
L
Linus Torvalds 已提交
178

179
	/* call uevent_helper, usually only enabled during early boot */
180
	if (uevent_helper[0]) {
181
		char *argv [3];
L
Linus Torvalds 已提交
182

183
		argv [0] = uevent_helper;
184 185 186 187
		argv [1] = (char *)subsystem;
		argv [2] = NULL;
		call_usermodehelper (argv[0], argv, envp, 0);
	}
L
Linus Torvalds 已提交
188 189

exit:
190
	kfree(devpath);
L
Linus Torvalds 已提交
191 192 193 194
	kfree(buffer);
	kfree(envp);
	return;
}
195
EXPORT_SYMBOL_GPL(kobject_uevent);
L
Linus Torvalds 已提交
196 197

/**
198
 * add_uevent_var - helper for creating event variables
L
Linus Torvalds 已提交
199
 * @envp: Pointer to table of environment variables, as passed into
200
 * uevent() method.
L
Linus Torvalds 已提交
201
 * @num_envp: Number of environment variable slots available, as
202
 * passed into uevent() method.
L
Linus Torvalds 已提交
203
 * @cur_index: Pointer to current index into @envp.  It should be
204
 * initialized to 0 before the first call to add_uevent_var(),
L
Linus Torvalds 已提交
205 206
 * and will be incremented on success.
 * @buffer: Pointer to buffer for environment variables, as passed
207 208
 * into uevent() method.
 * @buffer_size: Length of @buffer, as passed into uevent() method.
L
Linus Torvalds 已提交
209 210
 * @cur_len: Pointer to current length of space used in @buffer.
 * Should be initialized to 0 before the first call to
211
 * add_uevent_var(), and will be incremented on success.
L
Linus Torvalds 已提交
212 213 214 215 216 217
 * @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.
 */
218 219 220
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 已提交
221 222 223 224 225
{
	va_list args;

	/*
	 * We check against num_envp - 1 to make sure there is at
226 227
	 * least one slot left after we return, since kobject_uevent()
	 * needs to set the last slot to NULL.
L
Linus Torvalds 已提交
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
	 */
	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;
}
246
EXPORT_SYMBOL_GPL(add_uevent_var);
L
Linus Torvalds 已提交
247

248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
static int __init kobject_uevent_init(void)
{
	uevent_sock = netlink_kernel_create(NETLINK_KOBJECT_UEVENT, 1, NULL,
					    THIS_MODULE);

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

	return 0;
}

postcore_initcall(kobject_uevent_init);

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