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