[NETLINK]: Add properly module refcounting for kernel netlink sockets.
[deliverable/linux.git] / lib / kobject_uevent.c
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_uevent.h>
23 #include <linux/kobject.h>
24 #include <net/sock.h>
25
26 #define BUFFER_SIZE 1024 /* buffer for the hotplug env */
27 #define NUM_ENVP 32 /* number of env pointers */
28
29 #if defined(CONFIG_KOBJECT_UEVENT) || defined(CONFIG_HOTPLUG)
30 static char *action_to_string(enum kobject_action action)
31 {
32 switch (action) {
33 case KOBJ_ADD:
34 return "add";
35 case KOBJ_REMOVE:
36 return "remove";
37 case KOBJ_CHANGE:
38 return "change";
39 case KOBJ_MOUNT:
40 return "mount";
41 case KOBJ_UMOUNT:
42 return "umount";
43 case KOBJ_OFFLINE:
44 return "offline";
45 case KOBJ_ONLINE:
46 return "online";
47 default:
48 return NULL;
49 }
50 }
51 #endif
52
53 #ifdef CONFIG_KOBJECT_UEVENT
54 static struct sock *uevent_sock;
55
56 /**
57 * send_uevent - notify userspace by sending event trough netlink socket
58 *
59 * @signal: signal name
60 * @obj: object path (kobject)
61 * @envp: possible hotplug environment to pass with the message
62 * @gfp_mask:
63 */
64 static int send_uevent(const char *signal, const char *obj,
65 char **envp, int gfp_mask)
66 {
67 struct sk_buff *skb;
68 char *pos;
69 int len;
70
71 if (!uevent_sock)
72 return -EIO;
73
74 len = strlen(signal) + 1;
75 len += strlen(obj) + 1;
76
77 /* allocate buffer with the maximum possible message size */
78 skb = alloc_skb(len + BUFFER_SIZE, gfp_mask);
79 if (!skb)
80 return -ENOMEM;
81
82 pos = skb_put(skb, len);
83 sprintf(pos, "%s@%s", signal, obj);
84
85 /* copy the environment key by key to our continuous buffer */
86 if (envp) {
87 int i;
88
89 for (i = 2; envp[i]; i++) {
90 len = strlen(envp[i]) + 1;
91 pos = skb_put(skb, len);
92 strcpy(pos, envp[i]);
93 }
94 }
95
96 return netlink_broadcast(uevent_sock, skb, 0, 1, gfp_mask);
97 }
98
99 static int do_kobject_uevent(struct kobject *kobj, enum kobject_action action,
100 struct attribute *attr, int gfp_mask)
101 {
102 char *path;
103 char *attrpath;
104 char *signal;
105 int len;
106 int rc = -ENOMEM;
107
108 path = kobject_get_path(kobj, gfp_mask);
109 if (!path)
110 return -ENOMEM;
111
112 signal = action_to_string(action);
113 if (!signal)
114 return -EINVAL;
115
116 if (attr) {
117 len = strlen(path);
118 len += strlen(attr->name) + 2;
119 attrpath = kmalloc(len, gfp_mask);
120 if (!attrpath)
121 goto exit;
122 sprintf(attrpath, "%s/%s", path, attr->name);
123 rc = send_uevent(signal, attrpath, NULL, gfp_mask);
124 kfree(attrpath);
125 } else
126 rc = send_uevent(signal, path, NULL, gfp_mask);
127
128 exit:
129 kfree(path);
130 return rc;
131 }
132
133 /**
134 * kobject_uevent - notify userspace by sending event through netlink socket
135 *
136 * @signal: signal name
137 * @kobj: struct kobject that the event is happening to
138 * @attr: optional struct attribute the event belongs to
139 */
140 int kobject_uevent(struct kobject *kobj, enum kobject_action action,
141 struct attribute *attr)
142 {
143 return do_kobject_uevent(kobj, action, attr, GFP_KERNEL);
144 }
145 EXPORT_SYMBOL_GPL(kobject_uevent);
146
147 int kobject_uevent_atomic(struct kobject *kobj, enum kobject_action action,
148 struct attribute *attr)
149 {
150 return do_kobject_uevent(kobj, action, attr, GFP_ATOMIC);
151 }
152 EXPORT_SYMBOL_GPL(kobject_uevent_atomic);
153
154 static int __init kobject_uevent_init(void)
155 {
156 uevent_sock = netlink_kernel_create(NETLINK_KOBJECT_UEVENT, NULL,
157 THIS_MODULE);
158
159 if (!uevent_sock) {
160 printk(KERN_ERR
161 "kobject_uevent: unable to create netlink socket!\n");
162 return -ENODEV;
163 }
164
165 return 0;
166 }
167
168 postcore_initcall(kobject_uevent_init);
169
170 #else
171 static inline int send_uevent(const char *signal, const char *obj,
172 char **envp, int gfp_mask)
173 {
174 return 0;
175 }
176
177 #endif /* CONFIG_KOBJECT_UEVENT */
178
179
180 #ifdef CONFIG_HOTPLUG
181 char hotplug_path[HOTPLUG_PATH_LEN] = "/sbin/hotplug";
182 u64 hotplug_seqnum;
183 static DEFINE_SPINLOCK(sequence_lock);
184
185 /**
186 * kobject_hotplug - notify userspace by executing /sbin/hotplug
187 *
188 * @action: action that is happening (usually "ADD" or "REMOVE")
189 * @kobj: struct kobject that the action is happening to
190 */
191 void kobject_hotplug(struct kobject *kobj, enum kobject_action action)
192 {
193 char *argv [3];
194 char **envp = NULL;
195 char *buffer = NULL;
196 char *seq_buff;
197 char *scratch;
198 int i = 0;
199 int retval;
200 char *kobj_path = NULL;
201 const char *name = NULL;
202 char *action_string;
203 u64 seq;
204 struct kobject *top_kobj = kobj;
205 struct kset *kset;
206 static struct kset_hotplug_ops null_hotplug_ops;
207 struct kset_hotplug_ops *hotplug_ops = &null_hotplug_ops;
208
209 /* If this kobj does not belong to a kset,
210 try to find a parent that does. */
211 if (!top_kobj->kset && top_kobj->parent) {
212 do {
213 top_kobj = top_kobj->parent;
214 } while (!top_kobj->kset && top_kobj->parent);
215 }
216
217 if (top_kobj->kset)
218 kset = top_kobj->kset;
219 else
220 return;
221
222 if (kset->hotplug_ops)
223 hotplug_ops = kset->hotplug_ops;
224
225 /* If the kset has a filter operation, call it.
226 Skip the event, if the filter returns zero. */
227 if (hotplug_ops->filter) {
228 if (!hotplug_ops->filter(kset, kobj))
229 return;
230 }
231
232 pr_debug ("%s\n", __FUNCTION__);
233
234 action_string = action_to_string(action);
235 if (!action_string)
236 return;
237
238 envp = kmalloc(NUM_ENVP * sizeof (char *), GFP_KERNEL);
239 if (!envp)
240 return;
241 memset (envp, 0x00, NUM_ENVP * sizeof (char *));
242
243 buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
244 if (!buffer)
245 goto exit;
246
247 if (hotplug_ops->name)
248 name = hotplug_ops->name(kset, kobj);
249 if (name == NULL)
250 name = kobject_name(&kset->kobj);
251
252 argv [0] = hotplug_path;
253 argv [1] = (char *)name; /* won't be changed but 'const' has to go */
254 argv [2] = NULL;
255
256 /* minimal command environment */
257 envp [i++] = "HOME=/";
258 envp [i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
259
260 scratch = buffer;
261
262 envp [i++] = scratch;
263 scratch += sprintf(scratch, "ACTION=%s", action_string) + 1;
264
265 kobj_path = kobject_get_path(kobj, GFP_KERNEL);
266 if (!kobj_path)
267 goto exit;
268
269 envp [i++] = scratch;
270 scratch += sprintf (scratch, "DEVPATH=%s", kobj_path) + 1;
271
272 envp [i++] = scratch;
273 scratch += sprintf(scratch, "SUBSYSTEM=%s", name) + 1;
274
275 /* reserve space for the sequence,
276 * put the real one in after the hotplug call */
277 envp[i++] = seq_buff = scratch;
278 scratch += strlen("SEQNUM=18446744073709551616") + 1;
279
280 if (hotplug_ops->hotplug) {
281 /* have the kset specific function add its stuff */
282 retval = hotplug_ops->hotplug (kset, kobj,
283 &envp[i], NUM_ENVP - i, scratch,
284 BUFFER_SIZE - (scratch - buffer));
285 if (retval) {
286 pr_debug ("%s - hotplug() returned %d\n",
287 __FUNCTION__, retval);
288 goto exit;
289 }
290 }
291
292 spin_lock(&sequence_lock);
293 seq = ++hotplug_seqnum;
294 spin_unlock(&sequence_lock);
295 sprintf(seq_buff, "SEQNUM=%llu", (unsigned long long)seq);
296
297 pr_debug ("%s: %s %s seq=%llu %s %s %s %s %s\n",
298 __FUNCTION__, argv[0], argv[1], (unsigned long long)seq,
299 envp[0], envp[1], envp[2], envp[3], envp[4]);
300
301 send_uevent(action_string, kobj_path, envp, GFP_KERNEL);
302
303 if (!hotplug_path[0])
304 goto exit;
305
306 retval = call_usermodehelper (argv[0], argv, envp, 0);
307 if (retval)
308 pr_debug ("%s - call_usermodehelper returned %d\n",
309 __FUNCTION__, retval);
310
311 exit:
312 kfree(kobj_path);
313 kfree(buffer);
314 kfree(envp);
315 return;
316 }
317 EXPORT_SYMBOL(kobject_hotplug);
318
319 /**
320 * add_hotplug_env_var - helper for creating hotplug environment variables
321 * @envp: Pointer to table of environment variables, as passed into
322 * hotplug() method.
323 * @num_envp: Number of environment variable slots available, as
324 * passed into hotplug() method.
325 * @cur_index: Pointer to current index into @envp. It should be
326 * initialized to 0 before the first call to add_hotplug_env_var(),
327 * and will be incremented on success.
328 * @buffer: Pointer to buffer for environment variables, as passed
329 * into hotplug() method.
330 * @buffer_size: Length of @buffer, as passed into hotplug() method.
331 * @cur_len: Pointer to current length of space used in @buffer.
332 * Should be initialized to 0 before the first call to
333 * add_hotplug_env_var(), and will be incremented on success.
334 * @format: Format for creating environment variable (of the form
335 * "XXX=%x") for snprintf().
336 *
337 * Returns 0 if environment variable was added successfully or -ENOMEM
338 * if no space was available.
339 */
340 int add_hotplug_env_var(char **envp, int num_envp, int *cur_index,
341 char *buffer, int buffer_size, int *cur_len,
342 const char *format, ...)
343 {
344 va_list args;
345
346 /*
347 * We check against num_envp - 1 to make sure there is at
348 * least one slot left after we return, since the hotplug
349 * method needs to set the last slot to NULL.
350 */
351 if (*cur_index >= num_envp - 1)
352 return -ENOMEM;
353
354 envp[*cur_index] = buffer + *cur_len;
355
356 va_start(args, format);
357 *cur_len += vsnprintf(envp[*cur_index],
358 max(buffer_size - *cur_len, 0),
359 format, args) + 1;
360 va_end(args);
361
362 if (*cur_len > buffer_size)
363 return -ENOMEM;
364
365 (*cur_index)++;
366 return 0;
367 }
368 EXPORT_SYMBOL(add_hotplug_env_var);
369
370 #endif /* CONFIG_HOTPLUG */
This page took 0.049064 seconds and 6 git commands to generate.