ia64/linux-2.6.18-xen.hg

annotate lib/kobject_uevent.c @ 782:9ab1c319531f

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