xref: /linux/lib/kobject_uevent.c (revision d8327c784b51b57dac2c26cfad87dce0d68dfd98)
1 /*
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>
22 #include <linux/kobject.h>
23 #include <net/sock.h>
24 
25 #define BUFFER_SIZE	2048	/* buffer for the variables */
26 #define NUM_ENVP	32	/* number of env pointers */
27 
28 #if defined(CONFIG_HOTPLUG) && defined(CONFIG_NET)
29 static DEFINE_SPINLOCK(sequence_lock);
30 static struct sock *uevent_sock;
31 
32 static char *action_to_string(enum kobject_action action)
33 {
34 	switch (action) {
35 	case KOBJ_ADD:
36 		return "add";
37 	case KOBJ_REMOVE:
38 		return "remove";
39 	case KOBJ_CHANGE:
40 		return "change";
41 	case KOBJ_MOUNT:
42 		return "mount";
43 	case KOBJ_UMOUNT:
44 		return "umount";
45 	case KOBJ_OFFLINE:
46 		return "offline";
47 	case KOBJ_ONLINE:
48 		return "online";
49 	default:
50 		return NULL;
51 	}
52 }
53 
54 /**
55  * kobject_uevent - notify userspace by ending an uevent
56  *
57  * @action: action that is happening (usually KOBJ_ADD and KOBJ_REMOVE)
58  * @kobj: struct kobject that the action is happening to
59  */
60 void kobject_uevent(struct kobject *kobj, enum kobject_action action)
61 {
62 	char **envp;
63 	char *buffer;
64 	char *scratch;
65 	const char *action_string;
66 	const char *devpath = NULL;
67 	const char *subsystem;
68 	struct kobject *top_kobj;
69 	struct kset *kset;
70 	struct kset_uevent_ops *uevent_ops;
71 	u64 seq;
72 	char *seq_buff;
73 	int i = 0;
74 	int retval;
75 
76 	pr_debug("%s\n", __FUNCTION__);
77 
78 	action_string = action_to_string(action);
79 	if (!action_string)
80 		return;
81 
82 	/* search the kset we belong to */
83 	top_kobj = kobj;
84 	if (!top_kobj->kset && top_kobj->parent) {
85 		do {
86 			top_kobj = top_kobj->parent;
87 		} while (!top_kobj->kset && top_kobj->parent);
88 	}
89 	if (!top_kobj->kset)
90 		return;
91 
92 	kset = top_kobj->kset;
93 	uevent_ops = kset->uevent_ops;
94 
95 	/*  skip the event, if the filter returns zero. */
96 	if (uevent_ops && uevent_ops->filter)
97 		if (!uevent_ops->filter(kset, kobj))
98 			return;
99 
100 	/* environment index */
101 	envp = kzalloc(NUM_ENVP * sizeof (char *), GFP_KERNEL);
102 	if (!envp)
103 		return;
104 
105 	/* environment values */
106 	buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
107 	if (!buffer)
108 		goto exit;
109 
110 	/* complete object path */
111 	devpath = kobject_get_path(kobj, GFP_KERNEL);
112 	if (!devpath)
113 		goto exit;
114 
115 	/* originating subsystem */
116 	if (uevent_ops && uevent_ops->name)
117 		subsystem = uevent_ops->name(kset, kobj);
118 	else
119 		subsystem = kobject_name(&kset->kobj);
120 
121 	/* event environemnt for helper process only */
122 	envp[i++] = "HOME=/";
123 	envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
124 
125 	/* default keys */
126 	scratch = buffer;
127 	envp [i++] = scratch;
128 	scratch += sprintf(scratch, "ACTION=%s", action_string) + 1;
129 	envp [i++] = scratch;
130 	scratch += sprintf (scratch, "DEVPATH=%s", devpath) + 1;
131 	envp [i++] = scratch;
132 	scratch += sprintf(scratch, "SUBSYSTEM=%s", subsystem) + 1;
133 
134 	/* just reserve the space, overwrite it after kset call has returned */
135 	envp[i++] = seq_buff = scratch;
136 	scratch += strlen("SEQNUM=18446744073709551616") + 1;
137 
138 	/* let the kset specific function add its stuff */
139 	if (uevent_ops && uevent_ops->uevent) {
140 		retval = uevent_ops->uevent(kset, kobj,
141 				  &envp[i], NUM_ENVP - i, scratch,
142 				  BUFFER_SIZE - (scratch - buffer));
143 		if (retval) {
144 			pr_debug ("%s - uevent() returned %d\n",
145 				  __FUNCTION__, retval);
146 			goto exit;
147 		}
148 	}
149 
150 	/* we will send an event, request a new sequence number */
151 	spin_lock(&sequence_lock);
152 	seq = ++uevent_seqnum;
153 	spin_unlock(&sequence_lock);
154 	sprintf(seq_buff, "SEQNUM=%llu", (unsigned long long)seq);
155 
156 	/* send netlink message */
157 	if (uevent_sock) {
158 		struct sk_buff *skb;
159 		size_t len;
160 
161 		/* allocate message with the maximum possible size */
162 		len = strlen(action_string) + strlen(devpath) + 2;
163 		skb = alloc_skb(len + BUFFER_SIZE, GFP_KERNEL);
164 		if (skb) {
165 			/* add header */
166 			scratch = skb_put(skb, len);
167 			sprintf(scratch, "%s@%s", action_string, devpath);
168 
169 			/* copy keys to our continuous event payload buffer */
170 			for (i = 2; envp[i]; i++) {
171 				len = strlen(envp[i]) + 1;
172 				scratch = skb_put(skb, len);
173 				strcpy(scratch, envp[i]);
174 			}
175 
176 			NETLINK_CB(skb).dst_group = 1;
177 			netlink_broadcast(uevent_sock, skb, 0, 1, GFP_KERNEL);
178 		}
179 	}
180 
181 	/* call uevent_helper, usually only enabled during early boot */
182 	if (uevent_helper[0]) {
183 		char *argv [3];
184 
185 		argv [0] = uevent_helper;
186 		argv [1] = (char *)subsystem;
187 		argv [2] = NULL;
188 		call_usermodehelper (argv[0], argv, envp, 0);
189 	}
190 
191 exit:
192 	kfree(devpath);
193 	kfree(buffer);
194 	kfree(envp);
195 	return;
196 }
197 EXPORT_SYMBOL_GPL(kobject_uevent);
198 
199 /**
200  * add_uevent_var - helper for creating event variables
201  * @envp: Pointer to table of environment variables, as passed into
202  * uevent() method.
203  * @num_envp: Number of environment variable slots available, as
204  * passed into uevent() method.
205  * @cur_index: Pointer to current index into @envp.  It should be
206  * initialized to 0 before the first call to add_uevent_var(),
207  * and will be incremented on success.
208  * @buffer: Pointer to buffer for environment variables, as passed
209  * into uevent() method.
210  * @buffer_size: Length of @buffer, as passed into uevent() method.
211  * @cur_len: Pointer to current length of space used in @buffer.
212  * Should be initialized to 0 before the first call to
213  * add_uevent_var(), and will be incremented on success.
214  * @format: Format for creating environment variable (of the form
215  * "XXX=%x") for snprintf().
216  *
217  * Returns 0 if environment variable was added successfully or -ENOMEM
218  * if no space was available.
219  */
220 int add_uevent_var(char **envp, int num_envp, int *cur_index,
221 		   char *buffer, int buffer_size, int *cur_len,
222 		   const char *format, ...)
223 {
224 	va_list args;
225 
226 	/*
227 	 * We check against num_envp - 1 to make sure there is at
228 	 * least one slot left after we return, since kobject_uevent()
229 	 * needs to set the last slot to NULL.
230 	 */
231 	if (*cur_index >= num_envp - 1)
232 		return -ENOMEM;
233 
234 	envp[*cur_index] = buffer + *cur_len;
235 
236 	va_start(args, format);
237 	*cur_len += vsnprintf(envp[*cur_index],
238 			      max(buffer_size - *cur_len, 0),
239 			      format, args) + 1;
240 	va_end(args);
241 
242 	if (*cur_len > buffer_size)
243 		return -ENOMEM;
244 
245 	(*cur_index)++;
246 	return 0;
247 }
248 EXPORT_SYMBOL_GPL(add_uevent_var);
249 
250 static int __init kobject_uevent_init(void)
251 {
252 	uevent_sock = netlink_kernel_create(NETLINK_KOBJECT_UEVENT, 1, NULL,
253 					    THIS_MODULE);
254 
255 	if (!uevent_sock) {
256 		printk(KERN_ERR
257 		       "kobject_uevent: unable to create netlink socket!\n");
258 		return -ENODEV;
259 	}
260 
261 	return 0;
262 }
263 
264 postcore_initcall(kobject_uevent_init);
265 
266 #endif /* CONFIG_HOTPLUG */
267