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