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> |
Denis V. Lunev | 2d38f9a | 2008-03-27 14:26:30 -0700 | [diff] [blame] | 18 | #include <linux/string.h> |
| 19 | #include <linux/kobject.h> |
| 20 | #include <linux/module.h> |
| 21 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/socket.h> |
| 23 | #include <linux/skbuff.h> |
| 24 | #include <linux/netlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <net/sock.h> |
| 26 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
Cornelia Huck | cd030c4 | 2007-07-20 13:58:13 +0200 | [diff] [blame] | 28 | u64 uevent_seqnum; |
Kay Sievers | 6a8d8ab | 2007-08-15 15:38:28 +0200 | [diff] [blame] | 29 | char uevent_helper[UEVENT_HELPER_PATH_LEN] = CONFIG_UEVENT_HELPER_PATH; |
Cornelia Huck | cd030c4 | 2007-07-20 13:58:13 +0200 | [diff] [blame] | 30 | static DEFINE_SPINLOCK(sequence_lock); |
| 31 | #if defined(CONFIG_NET) |
| 32 | static struct sock *uevent_sock; |
| 33 | #endif |
| 34 | |
Kay Sievers | 5c5daf6 | 2007-08-12 20:43:55 +0200 | [diff] [blame] | 35 | /* the strings here must match the enum in include/linux/kobject.h */ |
| 36 | static const char *kobject_actions[] = { |
| 37 | [KOBJ_ADD] = "add", |
| 38 | [KOBJ_REMOVE] = "remove", |
| 39 | [KOBJ_CHANGE] = "change", |
| 40 | [KOBJ_MOVE] = "move", |
| 41 | [KOBJ_ONLINE] = "online", |
| 42 | [KOBJ_OFFLINE] = "offline", |
| 43 | }; |
| 44 | |
| 45 | /** |
| 46 | * kobject_action_type - translate action string to numeric type |
| 47 | * |
| 48 | * @buf: buffer containing the action string, newline is ignored |
| 49 | * @len: length of buffer |
| 50 | * @type: pointer to the location to store the action type |
| 51 | * |
| 52 | * Returns 0 if the action string was recognized. |
| 53 | */ |
| 54 | int kobject_action_type(const char *buf, size_t count, |
| 55 | enum kobject_action *type) |
| 56 | { |
| 57 | enum kobject_action action; |
| 58 | int ret = -EINVAL; |
| 59 | |
Mark Lord | a9edadb | 2008-03-28 19:05:25 -0400 | [diff] [blame] | 60 | if (count && (buf[count-1] == '\n' || buf[count-1] == '\0')) |
Kay Sievers | 5c5daf6 | 2007-08-12 20:43:55 +0200 | [diff] [blame] | 61 | count--; |
| 62 | |
| 63 | if (!count) |
| 64 | goto out; |
| 65 | |
| 66 | for (action = 0; action < ARRAY_SIZE(kobject_actions); action++) { |
| 67 | if (strncmp(kobject_actions[action], buf, count) != 0) |
| 68 | continue; |
| 69 | if (kobject_actions[action][count] != '\0') |
| 70 | continue; |
| 71 | *type = action; |
| 72 | ret = 0; |
| 73 | break; |
| 74 | } |
| 75 | out: |
| 76 | return ret; |
| 77 | } |
| 78 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | /** |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 80 | * kobject_uevent_env - send an uevent with environmental data |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | * |
Kay Sievers | ccd490a | 2007-08-12 20:43:55 +0200 | [diff] [blame] | 82 | * @action: action that is happening |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | * @kobj: struct kobject that the action is happening to |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 84 | * @envp_ext: pointer to environmental data |
Aneesh Kumar K.V | 542cfce | 2006-12-19 13:01:27 -0800 | [diff] [blame] | 85 | * |
| 86 | * Returns 0 if kobject_uevent() is completed with success or the |
| 87 | * corresponding error when it fails. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | */ |
Aneesh Kumar K.V | 542cfce | 2006-12-19 13:01:27 -0800 | [diff] [blame] | 89 | int kobject_uevent_env(struct kobject *kobj, enum kobject_action action, |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 90 | char *envp_ext[]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | { |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 92 | struct kobj_uevent_env *env; |
| 93 | const char *action_string = kobject_actions[action]; |
Kay Sievers | 5f123fb | 2005-11-11 14:43:07 +0100 | [diff] [blame] | 94 | const char *devpath = NULL; |
| 95 | const char *subsystem; |
| 96 | struct kobject *top_kobj; |
| 97 | struct kset *kset; |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 98 | struct kset_uevent_ops *uevent_ops; |
Kay Sievers | 5f123fb | 2005-11-11 14:43:07 +0100 | [diff] [blame] | 99 | u64 seq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | int i = 0; |
Aneesh Kumar K.V | 542cfce | 2006-12-19 13:01:27 -0800 | [diff] [blame] | 101 | int retval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
Greg Kroah-Hartman | 9f66fa2 | 2007-11-28 23:49:41 -0800 | [diff] [blame] | 103 | pr_debug("kobject: '%s' (%p): %s\n", |
Harvey Harrison | 810304d | 2008-04-30 00:55:08 -0700 | [diff] [blame] | 104 | kobject_name(kobj), kobj, __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | |
Kay Sievers | 5f123fb | 2005-11-11 14:43:07 +0100 | [diff] [blame] | 106 | /* search the kset we belong to */ |
| 107 | top_kobj = kobj; |
Kay Sievers | ccd490a | 2007-08-12 20:43:55 +0200 | [diff] [blame] | 108 | while (!top_kobj->kset && top_kobj->parent) |
John Anthony Kazos Jr | 14193fb | 2007-04-04 07:39:17 -0400 | [diff] [blame] | 109 | top_kobj = top_kobj->parent; |
Kay Sievers | ccd490a | 2007-08-12 20:43:55 +0200 | [diff] [blame] | 110 | |
Aneesh Kumar K.V | 542cfce | 2006-12-19 13:01:27 -0800 | [diff] [blame] | 111 | if (!top_kobj->kset) { |
Greg Kroah-Hartman | 9f66fa2 | 2007-11-28 23:49:41 -0800 | [diff] [blame] | 112 | pr_debug("kobject: '%s' (%p): %s: attempted to send uevent " |
| 113 | "without kset!\n", kobject_name(kobj), kobj, |
Harvey Harrison | 810304d | 2008-04-30 00:55:08 -0700 | [diff] [blame] | 114 | __func__); |
Aneesh Kumar K.V | 542cfce | 2006-12-19 13:01:27 -0800 | [diff] [blame] | 115 | return -EINVAL; |
| 116 | } |
Kay Sievers | 5f123fb | 2005-11-11 14:43:07 +0100 | [diff] [blame] | 117 | |
| 118 | kset = top_kobj->kset; |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 119 | uevent_ops = kset->uevent_ops; |
Kay Sievers | 5f123fb | 2005-11-11 14:43:07 +0100 | [diff] [blame] | 120 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 121 | /* skip the event, if the filter returns zero. */ |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 122 | if (uevent_ops && uevent_ops->filter) |
Aneesh Kumar K.V | 542cfce | 2006-12-19 13:01:27 -0800 | [diff] [blame] | 123 | if (!uevent_ops->filter(kset, kobj)) { |
Greg Kroah-Hartman | 9f66fa2 | 2007-11-28 23:49:41 -0800 | [diff] [blame] | 124 | pr_debug("kobject: '%s' (%p): %s: filter function " |
| 125 | "caused the event to drop!\n", |
Harvey Harrison | 810304d | 2008-04-30 00:55:08 -0700 | [diff] [blame] | 126 | kobject_name(kobj), kobj, __func__); |
Aneesh Kumar K.V | 542cfce | 2006-12-19 13:01:27 -0800 | [diff] [blame] | 127 | return 0; |
| 128 | } |
Kay Sievers | 5f123fb | 2005-11-11 14:43:07 +0100 | [diff] [blame] | 129 | |
Kay Sievers | 8640624 | 2007-03-14 03:25:56 +0100 | [diff] [blame] | 130 | /* originating subsystem */ |
| 131 | if (uevent_ops && uevent_ops->name) |
| 132 | subsystem = uevent_ops->name(kset, kobj); |
| 133 | else |
| 134 | subsystem = kobject_name(&kset->kobj); |
| 135 | if (!subsystem) { |
Greg Kroah-Hartman | 9f66fa2 | 2007-11-28 23:49:41 -0800 | [diff] [blame] | 136 | pr_debug("kobject: '%s' (%p): %s: unset subsystem caused the " |
| 137 | "event to drop!\n", kobject_name(kobj), kobj, |
Harvey Harrison | 810304d | 2008-04-30 00:55:08 -0700 | [diff] [blame] | 138 | __func__); |
Kay Sievers | 8640624 | 2007-03-14 03:25:56 +0100 | [diff] [blame] | 139 | return 0; |
| 140 | } |
| 141 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 142 | /* environment buffer */ |
| 143 | env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL); |
| 144 | if (!env) |
Aneesh Kumar K.V | 542cfce | 2006-12-19 13:01:27 -0800 | [diff] [blame] | 145 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
Kay Sievers | 5f123fb | 2005-11-11 14:43:07 +0100 | [diff] [blame] | 147 | /* complete object path */ |
| 148 | devpath = kobject_get_path(kobj, GFP_KERNEL); |
Aneesh Kumar K.V | 542cfce | 2006-12-19 13:01:27 -0800 | [diff] [blame] | 149 | if (!devpath) { |
| 150 | retval = -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | goto exit; |
Aneesh Kumar K.V | 542cfce | 2006-12-19 13:01:27 -0800 | [diff] [blame] | 152 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
Kay Sievers | 5f123fb | 2005-11-11 14:43:07 +0100 | [diff] [blame] | 154 | /* default keys */ |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 155 | retval = add_uevent_var(env, "ACTION=%s", action_string); |
| 156 | if (retval) |
| 157 | goto exit; |
| 158 | retval = add_uevent_var(env, "DEVPATH=%s", devpath); |
| 159 | if (retval) |
| 160 | goto exit; |
| 161 | retval = add_uevent_var(env, "SUBSYSTEM=%s", subsystem); |
| 162 | if (retval) |
| 163 | goto exit; |
| 164 | |
| 165 | /* keys passed in from the caller */ |
| 166 | if (envp_ext) { |
| 167 | for (i = 0; envp_ext[i]; i++) { |
| 168 | retval = add_uevent_var(env, envp_ext[i]); |
| 169 | if (retval) |
| 170 | goto exit; |
| 171 | } |
| 172 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | |
Kay Sievers | 5f123fb | 2005-11-11 14:43:07 +0100 | [diff] [blame] | 174 | /* let the kset specific function add its stuff */ |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 175 | if (uevent_ops && uevent_ops->uevent) { |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 176 | retval = uevent_ops->uevent(kset, kobj, env); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | if (retval) { |
Greg Kroah-Hartman | 9f66fa2 | 2007-11-28 23:49:41 -0800 | [diff] [blame] | 178 | pr_debug("kobject: '%s' (%p): %s: uevent() returned " |
| 179 | "%d\n", kobject_name(kobj), kobj, |
Harvey Harrison | 810304d | 2008-04-30 00:55:08 -0700 | [diff] [blame] | 180 | __func__, retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | goto exit; |
| 182 | } |
| 183 | } |
| 184 | |
Kay Sievers | 0f4dafc | 2007-12-19 01:40:42 +0100 | [diff] [blame] | 185 | /* |
| 186 | * Mark "add" and "remove" events in the object to ensure proper |
| 187 | * events to userspace during automatic cleanup. If the object did |
| 188 | * send an "add" event, "remove" will automatically generated by |
| 189 | * the core, if not already done by the caller. |
| 190 | */ |
| 191 | if (action == KOBJ_ADD) |
| 192 | kobj->state_add_uevent_sent = 1; |
| 193 | else if (action == KOBJ_REMOVE) |
| 194 | kobj->state_remove_uevent_sent = 1; |
| 195 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 196 | /* we will send an event, so request a new sequence number */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | spin_lock(&sequence_lock); |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 198 | seq = ++uevent_seqnum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | spin_unlock(&sequence_lock); |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 200 | retval = add_uevent_var(env, "SEQNUM=%llu", (unsigned long long)seq); |
| 201 | if (retval) |
| 202 | goto exit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | |
Kay Sievers | 4d17ffd | 2006-04-25 15:37:26 +0200 | [diff] [blame] | 204 | #if defined(CONFIG_NET) |
Kay Sievers | 5f123fb | 2005-11-11 14:43:07 +0100 | [diff] [blame] | 205 | /* send netlink message */ |
| 206 | if (uevent_sock) { |
| 207 | struct sk_buff *skb; |
| 208 | size_t len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | |
Kay Sievers | 5f123fb | 2005-11-11 14:43:07 +0100 | [diff] [blame] | 210 | /* allocate message with the maximum possible size */ |
| 211 | len = strlen(action_string) + strlen(devpath) + 2; |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 212 | skb = alloc_skb(len + env->buflen, GFP_KERNEL); |
Kay Sievers | 5f123fb | 2005-11-11 14:43:07 +0100 | [diff] [blame] | 213 | if (skb) { |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 214 | char *scratch; |
| 215 | |
Kay Sievers | 5f123fb | 2005-11-11 14:43:07 +0100 | [diff] [blame] | 216 | /* add header */ |
| 217 | scratch = skb_put(skb, len); |
| 218 | sprintf(scratch, "%s@%s", action_string, devpath); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | |
Kay Sievers | 5f123fb | 2005-11-11 14:43:07 +0100 | [diff] [blame] | 220 | /* copy keys to our continuous event payload buffer */ |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 221 | for (i = 0; i < env->envp_idx; i++) { |
| 222 | len = strlen(env->envp[i]) + 1; |
Kay Sievers | 5f123fb | 2005-11-11 14:43:07 +0100 | [diff] [blame] | 223 | scratch = skb_put(skb, len); |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 224 | strcpy(scratch, env->envp[i]); |
Kay Sievers | 5f123fb | 2005-11-11 14:43:07 +0100 | [diff] [blame] | 225 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | |
Kay Sievers | 5f123fb | 2005-11-11 14:43:07 +0100 | [diff] [blame] | 227 | NETLINK_CB(skb).dst_group = 1; |
| 228 | netlink_broadcast(uevent_sock, skb, 0, 1, GFP_KERNEL); |
| 229 | } |
| 230 | } |
Kay Sievers | 4d17ffd | 2006-04-25 15:37:26 +0200 | [diff] [blame] | 231 | #endif |
Kay Sievers | 5f123fb | 2005-11-11 14:43:07 +0100 | [diff] [blame] | 232 | |
| 233 | /* call uevent_helper, usually only enabled during early boot */ |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 234 | if (uevent_helper[0]) { |
Kay Sievers | 5f123fb | 2005-11-11 14:43:07 +0100 | [diff] [blame] | 235 | char *argv [3]; |
| 236 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 237 | argv [0] = uevent_helper; |
Kay Sievers | 5f123fb | 2005-11-11 14:43:07 +0100 | [diff] [blame] | 238 | argv [1] = (char *)subsystem; |
| 239 | argv [2] = NULL; |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 240 | retval = add_uevent_var(env, "HOME=/"); |
| 241 | if (retval) |
| 242 | goto exit; |
Greg Kroah-Hartman | e374a2b | 2008-01-24 21:59:04 -0800 | [diff] [blame] | 243 | retval = add_uevent_var(env, |
| 244 | "PATH=/sbin:/bin:/usr/sbin:/usr/bin"); |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 245 | if (retval) |
| 246 | goto exit; |
| 247 | |
Wang Chen | 0ad1d6f | 2008-06-24 16:59:02 +0800 | [diff] [blame] | 248 | retval = call_usermodehelper(argv[0], argv, |
| 249 | env->envp, UMH_WAIT_EXEC); |
Kay Sievers | 5f123fb | 2005-11-11 14:43:07 +0100 | [diff] [blame] | 250 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | |
| 252 | exit: |
Kay Sievers | 5f123fb | 2005-11-11 14:43:07 +0100 | [diff] [blame] | 253 | kfree(devpath); |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 254 | kfree(env); |
Aneesh Kumar K.V | 542cfce | 2006-12-19 13:01:27 -0800 | [diff] [blame] | 255 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | } |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 257 | EXPORT_SYMBOL_GPL(kobject_uevent_env); |
| 258 | |
| 259 | /** |
| 260 | * kobject_uevent - notify userspace by ending an uevent |
| 261 | * |
Kay Sievers | ccd490a | 2007-08-12 20:43:55 +0200 | [diff] [blame] | 262 | * @action: action that is happening |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 263 | * @kobj: struct kobject that the action is happening to |
Aneesh Kumar K.V | 542cfce | 2006-12-19 13:01:27 -0800 | [diff] [blame] | 264 | * |
| 265 | * Returns 0 if kobject_uevent() is completed with success or the |
| 266 | * corresponding error when it fails. |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 267 | */ |
Aneesh Kumar K.V | 542cfce | 2006-12-19 13:01:27 -0800 | [diff] [blame] | 268 | int kobject_uevent(struct kobject *kobj, enum kobject_action action) |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 269 | { |
Aneesh Kumar K.V | 542cfce | 2006-12-19 13:01:27 -0800 | [diff] [blame] | 270 | return kobject_uevent_env(kobj, action, NULL); |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 271 | } |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 272 | EXPORT_SYMBOL_GPL(kobject_uevent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | |
| 274 | /** |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 275 | * add_uevent_var - add key value string to the environment buffer |
| 276 | * @env: environment buffer structure |
| 277 | * @format: printf format for the key=value pair |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | * |
| 279 | * Returns 0 if environment variable was added successfully or -ENOMEM |
| 280 | * if no space was available. |
| 281 | */ |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 282 | int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | { |
| 284 | va_list args; |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 285 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 287 | if (env->envp_idx >= ARRAY_SIZE(env->envp)) { |
Arjan van de Ven | 5cd2b45 | 2008-07-25 19:45:39 -0700 | [diff] [blame] | 288 | WARN(1, KERN_ERR "add_uevent_var: too many keys\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | return -ENOMEM; |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 290 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | |
| 292 | va_start(args, format); |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 293 | len = vsnprintf(&env->buf[env->buflen], |
| 294 | sizeof(env->buf) - env->buflen, |
| 295 | format, args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | va_end(args); |
| 297 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 298 | if (len >= (sizeof(env->buf) - env->buflen)) { |
Arjan van de Ven | 5cd2b45 | 2008-07-25 19:45:39 -0700 | [diff] [blame] | 299 | WARN(1, KERN_ERR "add_uevent_var: buffer size too small\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | return -ENOMEM; |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 301 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 303 | env->envp[env->envp_idx++] = &env->buf[env->buflen]; |
| 304 | env->buflen += len + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | return 0; |
| 306 | } |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 307 | EXPORT_SYMBOL_GPL(add_uevent_var); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | |
Kay Sievers | 4d17ffd | 2006-04-25 15:37:26 +0200 | [diff] [blame] | 309 | #if defined(CONFIG_NET) |
Kay Sievers | 5f123fb | 2005-11-11 14:43:07 +0100 | [diff] [blame] | 310 | static int __init kobject_uevent_init(void) |
| 311 | { |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 312 | uevent_sock = netlink_kernel_create(&init_net, NETLINK_KOBJECT_UEVENT, |
| 313 | 1, NULL, NULL, THIS_MODULE); |
Kay Sievers | 5f123fb | 2005-11-11 14:43:07 +0100 | [diff] [blame] | 314 | if (!uevent_sock) { |
| 315 | printk(KERN_ERR |
| 316 | "kobject_uevent: unable to create netlink socket!\n"); |
| 317 | return -ENODEV; |
| 318 | } |
| 319 | |
| 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | postcore_initcall(kobject_uevent_init); |
Kay Sievers | 4d17ffd | 2006-04-25 15:37:26 +0200 | [diff] [blame] | 324 | #endif |