diff options
Diffstat (limited to 'lib/kobject_uevent.c')
-rw-r--r-- | lib/kobject_uevent.c | 216 |
1 files changed, 112 insertions, 104 deletions
diff --git a/lib/kobject_uevent.c b/lib/kobject_uevent.c index e06a8dcec0f0..2e4eae5b0824 100644 --- a/lib/kobject_uevent.c +++ b/lib/kobject_uevent.c | |||
@@ -22,31 +22,62 @@ | |||
22 | #include <linux/kobject.h> | 22 | #include <linux/kobject.h> |
23 | #include <net/sock.h> | 23 | #include <net/sock.h> |
24 | 24 | ||
25 | #define BUFFER_SIZE 2048 /* buffer for the variables */ | ||
26 | #define NUM_ENVP 32 /* number of env pointers */ | ||
27 | 25 | ||
28 | /* the strings here must match the enum in include/linux/kobject.h */ | ||
29 | const char *kobject_actions[] = { | ||
30 | "add", | ||
31 | "remove", | ||
32 | "change", | ||
33 | "move", | ||
34 | "online", | ||
35 | "offline", | ||
36 | }; | ||
37 | |||
38 | #if defined(CONFIG_HOTPLUG) | ||
39 | u64 uevent_seqnum; | 26 | u64 uevent_seqnum; |
40 | char uevent_helper[UEVENT_HELPER_PATH_LEN] = "/sbin/hotplug"; | 27 | char uevent_helper[UEVENT_HELPER_PATH_LEN] = CONFIG_UEVENT_HELPER_PATH; |
41 | static DEFINE_SPINLOCK(sequence_lock); | 28 | static DEFINE_SPINLOCK(sequence_lock); |
42 | #if defined(CONFIG_NET) | 29 | #if defined(CONFIG_NET) |
43 | static struct sock *uevent_sock; | 30 | static struct sock *uevent_sock; |
44 | #endif | 31 | #endif |
45 | 32 | ||
33 | /* the strings here must match the enum in include/linux/kobject.h */ | ||
34 | static const char *kobject_actions[] = { | ||
35 | [KOBJ_ADD] = "add", | ||
36 | [KOBJ_REMOVE] = "remove", | ||
37 | [KOBJ_CHANGE] = "change", | ||
38 | [KOBJ_MOVE] = "move", | ||
39 | [KOBJ_ONLINE] = "online", | ||
40 | [KOBJ_OFFLINE] = "offline", | ||
41 | }; | ||
42 | |||
43 | /** | ||
44 | * kobject_action_type - translate action string to numeric type | ||
45 | * | ||
46 | * @buf: buffer containing the action string, newline is ignored | ||
47 | * @len: length of buffer | ||
48 | * @type: pointer to the location to store the action type | ||
49 | * | ||
50 | * Returns 0 if the action string was recognized. | ||
51 | */ | ||
52 | int kobject_action_type(const char *buf, size_t count, | ||
53 | enum kobject_action *type) | ||
54 | { | ||
55 | enum kobject_action action; | ||
56 | int ret = -EINVAL; | ||
57 | |||
58 | if (count && buf[count-1] == '\n') | ||
59 | count--; | ||
60 | |||
61 | if (!count) | ||
62 | goto out; | ||
63 | |||
64 | for (action = 0; action < ARRAY_SIZE(kobject_actions); action++) { | ||
65 | if (strncmp(kobject_actions[action], buf, count) != 0) | ||
66 | continue; | ||
67 | if (kobject_actions[action][count] != '\0') | ||
68 | continue; | ||
69 | *type = action; | ||
70 | ret = 0; | ||
71 | break; | ||
72 | } | ||
73 | out: | ||
74 | return ret; | ||
75 | } | ||
76 | |||
46 | /** | 77 | /** |
47 | * kobject_uevent_env - send an uevent with environmental data | 78 | * kobject_uevent_env - send an uevent with environmental data |
48 | * | 79 | * |
49 | * @action: action that is happening (usually KOBJ_MOVE) | 80 | * @action: action that is happening |
50 | * @kobj: struct kobject that the action is happening to | 81 | * @kobj: struct kobject that the action is happening to |
51 | * @envp_ext: pointer to environmental data | 82 | * @envp_ext: pointer to environmental data |
52 | * | 83 | * |
@@ -54,36 +85,26 @@ static struct sock *uevent_sock; | |||
54 | * corresponding error when it fails. | 85 | * corresponding error when it fails. |
55 | */ | 86 | */ |
56 | int kobject_uevent_env(struct kobject *kobj, enum kobject_action action, | 87 | int kobject_uevent_env(struct kobject *kobj, enum kobject_action action, |
57 | char *envp_ext[]) | 88 | char *envp_ext[]) |
58 | { | 89 | { |
59 | char **envp; | 90 | struct kobj_uevent_env *env; |
60 | char *buffer; | 91 | const char *action_string = kobject_actions[action]; |
61 | char *scratch; | ||
62 | const char *action_string; | ||
63 | const char *devpath = NULL; | 92 | const char *devpath = NULL; |
64 | const char *subsystem; | 93 | const char *subsystem; |
65 | struct kobject *top_kobj; | 94 | struct kobject *top_kobj; |
66 | struct kset *kset; | 95 | struct kset *kset; |
67 | struct kset_uevent_ops *uevent_ops; | 96 | struct kset_uevent_ops *uevent_ops; |
68 | u64 seq; | 97 | u64 seq; |
69 | char *seq_buff; | ||
70 | int i = 0; | 98 | int i = 0; |
71 | int retval = 0; | 99 | int retval = 0; |
72 | int j; | ||
73 | 100 | ||
74 | pr_debug("%s\n", __FUNCTION__); | 101 | pr_debug("%s\n", __FUNCTION__); |
75 | 102 | ||
76 | action_string = kobject_actions[action]; | ||
77 | if (!action_string) { | ||
78 | pr_debug("kobject attempted to send uevent without action_string!\n"); | ||
79 | return -EINVAL; | ||
80 | } | ||
81 | |||
82 | /* search the kset we belong to */ | 103 | /* search the kset we belong to */ |
83 | top_kobj = kobj; | 104 | top_kobj = kobj; |
84 | while (!top_kobj->kset && top_kobj->parent) { | 105 | while (!top_kobj->kset && top_kobj->parent) |
85 | top_kobj = top_kobj->parent; | 106 | top_kobj = top_kobj->parent; |
86 | } | 107 | |
87 | if (!top_kobj->kset) { | 108 | if (!top_kobj->kset) { |
88 | pr_debug("kobject attempted to send uevent without kset!\n"); | 109 | pr_debug("kobject attempted to send uevent without kset!\n"); |
89 | return -EINVAL; | 110 | return -EINVAL; |
@@ -92,7 +113,7 @@ int kobject_uevent_env(struct kobject *kobj, enum kobject_action action, | |||
92 | kset = top_kobj->kset; | 113 | kset = top_kobj->kset; |
93 | uevent_ops = kset->uevent_ops; | 114 | uevent_ops = kset->uevent_ops; |
94 | 115 | ||
95 | /* skip the event, if the filter returns zero. */ | 116 | /* skip the event, if the filter returns zero. */ |
96 | if (uevent_ops && uevent_ops->filter) | 117 | if (uevent_ops && uevent_ops->filter) |
97 | if (!uevent_ops->filter(kset, kobj)) { | 118 | if (!uevent_ops->filter(kset, kobj)) { |
98 | pr_debug("kobject filter function caused the event to drop!\n"); | 119 | pr_debug("kobject filter function caused the event to drop!\n"); |
@@ -109,18 +130,11 @@ int kobject_uevent_env(struct kobject *kobj, enum kobject_action action, | |||
109 | return 0; | 130 | return 0; |
110 | } | 131 | } |
111 | 132 | ||
112 | /* environment index */ | 133 | /* environment buffer */ |
113 | envp = kzalloc(NUM_ENVP * sizeof (char *), GFP_KERNEL); | 134 | env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL); |
114 | if (!envp) | 135 | if (!env) |
115 | return -ENOMEM; | 136 | return -ENOMEM; |
116 | 137 | ||
117 | /* environment values */ | ||
118 | buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL); | ||
119 | if (!buffer) { | ||
120 | retval = -ENOMEM; | ||
121 | goto exit; | ||
122 | } | ||
123 | |||
124 | /* complete object path */ | 138 | /* complete object path */ |
125 | devpath = kobject_get_path(kobj, GFP_KERNEL); | 139 | devpath = kobject_get_path(kobj, GFP_KERNEL); |
126 | if (!devpath) { | 140 | if (!devpath) { |
@@ -128,29 +142,29 @@ int kobject_uevent_env(struct kobject *kobj, enum kobject_action action, | |||
128 | goto exit; | 142 | goto exit; |
129 | } | 143 | } |
130 | 144 | ||
131 | /* event environemnt for helper process only */ | ||
132 | envp[i++] = "HOME=/"; | ||
133 | envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin"; | ||
134 | |||
135 | /* default keys */ | 145 | /* default keys */ |
136 | scratch = buffer; | 146 | retval = add_uevent_var(env, "ACTION=%s", action_string); |
137 | envp [i++] = scratch; | 147 | if (retval) |
138 | scratch += sprintf(scratch, "ACTION=%s", action_string) + 1; | 148 | goto exit; |
139 | envp [i++] = scratch; | 149 | retval = add_uevent_var(env, "DEVPATH=%s", devpath); |
140 | scratch += sprintf (scratch, "DEVPATH=%s", devpath) + 1; | 150 | if (retval) |
141 | envp [i++] = scratch; | 151 | goto exit; |
142 | scratch += sprintf(scratch, "SUBSYSTEM=%s", subsystem) + 1; | 152 | retval = add_uevent_var(env, "SUBSYSTEM=%s", subsystem); |
143 | for (j = 0; envp_ext && envp_ext[j]; j++) | 153 | if (retval) |
144 | envp[i++] = envp_ext[j]; | 154 | goto exit; |
145 | /* just reserve the space, overwrite it after kset call has returned */ | 155 | |
146 | envp[i++] = seq_buff = scratch; | 156 | /* keys passed in from the caller */ |
147 | scratch += strlen("SEQNUM=18446744073709551616") + 1; | 157 | if (envp_ext) { |
158 | for (i = 0; envp_ext[i]; i++) { | ||
159 | retval = add_uevent_var(env, envp_ext[i]); | ||
160 | if (retval) | ||
161 | goto exit; | ||
162 | } | ||
163 | } | ||
148 | 164 | ||
149 | /* let the kset specific function add its stuff */ | 165 | /* let the kset specific function add its stuff */ |
150 | if (uevent_ops && uevent_ops->uevent) { | 166 | if (uevent_ops && uevent_ops->uevent) { |
151 | retval = uevent_ops->uevent(kset, kobj, | 167 | retval = uevent_ops->uevent(kset, kobj, env); |
152 | &envp[i], NUM_ENVP - i, scratch, | ||
153 | BUFFER_SIZE - (scratch - buffer)); | ||
154 | if (retval) { | 168 | if (retval) { |
155 | pr_debug ("%s - uevent() returned %d\n", | 169 | pr_debug ("%s - uevent() returned %d\n", |
156 | __FUNCTION__, retval); | 170 | __FUNCTION__, retval); |
@@ -158,11 +172,13 @@ int kobject_uevent_env(struct kobject *kobj, enum kobject_action action, | |||
158 | } | 172 | } |
159 | } | 173 | } |
160 | 174 | ||
161 | /* we will send an event, request a new sequence number */ | 175 | /* we will send an event, so request a new sequence number */ |
162 | spin_lock(&sequence_lock); | 176 | spin_lock(&sequence_lock); |
163 | seq = ++uevent_seqnum; | 177 | seq = ++uevent_seqnum; |
164 | spin_unlock(&sequence_lock); | 178 | spin_unlock(&sequence_lock); |
165 | sprintf(seq_buff, "SEQNUM=%llu", (unsigned long long)seq); | 179 | retval = add_uevent_var(env, "SEQNUM=%llu", (unsigned long long)seq); |
180 | if (retval) | ||
181 | goto exit; | ||
166 | 182 | ||
167 | #if defined(CONFIG_NET) | 183 | #if defined(CONFIG_NET) |
168 | /* send netlink message */ | 184 | /* send netlink message */ |
@@ -172,17 +188,19 @@ int kobject_uevent_env(struct kobject *kobj, enum kobject_action action, | |||
172 | 188 | ||
173 | /* allocate message with the maximum possible size */ | 189 | /* allocate message with the maximum possible size */ |
174 | len = strlen(action_string) + strlen(devpath) + 2; | 190 | len = strlen(action_string) + strlen(devpath) + 2; |
175 | skb = alloc_skb(len + BUFFER_SIZE, GFP_KERNEL); | 191 | skb = alloc_skb(len + env->buflen, GFP_KERNEL); |
176 | if (skb) { | 192 | if (skb) { |
193 | char *scratch; | ||
194 | |||
177 | /* add header */ | 195 | /* add header */ |
178 | scratch = skb_put(skb, len); | 196 | scratch = skb_put(skb, len); |
179 | sprintf(scratch, "%s@%s", action_string, devpath); | 197 | sprintf(scratch, "%s@%s", action_string, devpath); |
180 | 198 | ||
181 | /* copy keys to our continuous event payload buffer */ | 199 | /* copy keys to our continuous event payload buffer */ |
182 | for (i = 2; envp[i]; i++) { | 200 | for (i = 0; i < env->envp_idx; i++) { |
183 | len = strlen(envp[i]) + 1; | 201 | len = strlen(env->envp[i]) + 1; |
184 | scratch = skb_put(skb, len); | 202 | scratch = skb_put(skb, len); |
185 | strcpy(scratch, envp[i]); | 203 | strcpy(scratch, env->envp[i]); |
186 | } | 204 | } |
187 | 205 | ||
188 | NETLINK_CB(skb).dst_group = 1; | 206 | NETLINK_CB(skb).dst_group = 1; |
@@ -198,13 +216,19 @@ int kobject_uevent_env(struct kobject *kobj, enum kobject_action action, | |||
198 | argv [0] = uevent_helper; | 216 | argv [0] = uevent_helper; |
199 | argv [1] = (char *)subsystem; | 217 | argv [1] = (char *)subsystem; |
200 | argv [2] = NULL; | 218 | argv [2] = NULL; |
201 | call_usermodehelper (argv[0], argv, envp, UMH_WAIT_EXEC); | 219 | retval = add_uevent_var(env, "HOME=/"); |
220 | if (retval) | ||
221 | goto exit; | ||
222 | retval = add_uevent_var(env, "PATH=/sbin:/bin:/usr/sbin:/usr/bin"); | ||
223 | if (retval) | ||
224 | goto exit; | ||
225 | |||
226 | call_usermodehelper (argv[0], argv, env->envp, UMH_WAIT_EXEC); | ||
202 | } | 227 | } |
203 | 228 | ||
204 | exit: | 229 | exit: |
205 | kfree(devpath); | 230 | kfree(devpath); |
206 | kfree(buffer); | 231 | kfree(env); |
207 | kfree(envp); | ||
208 | return retval; | 232 | return retval; |
209 | } | 233 | } |
210 | 234 | ||
@@ -213,7 +237,7 @@ EXPORT_SYMBOL_GPL(kobject_uevent_env); | |||
213 | /** | 237 | /** |
214 | * kobject_uevent - notify userspace by ending an uevent | 238 | * kobject_uevent - notify userspace by ending an uevent |
215 | * | 239 | * |
216 | * @action: action that is happening (usually KOBJ_ADD and KOBJ_REMOVE) | 240 | * @action: action that is happening |
217 | * @kobj: struct kobject that the action is happening to | 241 | * @kobj: struct kobject that the action is happening to |
218 | * | 242 | * |
219 | * Returns 0 if kobject_uevent() is completed with success or the | 243 | * Returns 0 if kobject_uevent() is completed with success or the |
@@ -227,52 +251,38 @@ int kobject_uevent(struct kobject *kobj, enum kobject_action action) | |||
227 | EXPORT_SYMBOL_GPL(kobject_uevent); | 251 | EXPORT_SYMBOL_GPL(kobject_uevent); |
228 | 252 | ||
229 | /** | 253 | /** |
230 | * add_uevent_var - helper for creating event variables | 254 | * add_uevent_var - add key value string to the environment buffer |
231 | * @envp: Pointer to table of environment variables, as passed into | 255 | * @env: environment buffer structure |
232 | * uevent() method. | 256 | * @format: printf format for the key=value pair |
233 | * @num_envp: Number of environment variable slots available, as | ||
234 | * passed into uevent() method. | ||
235 | * @cur_index: Pointer to current index into @envp. It should be | ||
236 | * initialized to 0 before the first call to add_uevent_var(), | ||
237 | * and will be incremented on success. | ||
238 | * @buffer: Pointer to buffer for environment variables, as passed | ||
239 | * into uevent() method. | ||
240 | * @buffer_size: Length of @buffer, as passed into uevent() method. | ||
241 | * @cur_len: Pointer to current length of space used in @buffer. | ||
242 | * Should be initialized to 0 before the first call to | ||
243 | * add_uevent_var(), and will be incremented on success. | ||
244 | * @format: Format for creating environment variable (of the form | ||
245 | * "XXX=%x") for snprintf(). | ||
246 | * | 257 | * |
247 | * Returns 0 if environment variable was added successfully or -ENOMEM | 258 | * Returns 0 if environment variable was added successfully or -ENOMEM |
248 | * if no space was available. | 259 | * if no space was available. |
249 | */ | 260 | */ |
250 | int add_uevent_var(char **envp, int num_envp, int *cur_index, | 261 | int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...) |
251 | char *buffer, int buffer_size, int *cur_len, | ||
252 | const char *format, ...) | ||
253 | { | 262 | { |
254 | va_list args; | 263 | va_list args; |
264 | int len; | ||
255 | 265 | ||
256 | /* | 266 | if (env->envp_idx >= ARRAY_SIZE(env->envp)) { |
257 | * We check against num_envp - 1 to make sure there is at | 267 | printk(KERN_ERR "add_uevent_var: too many keys\n"); |
258 | * least one slot left after we return, since kobject_uevent() | 268 | WARN_ON(1); |
259 | * needs to set the last slot to NULL. | ||
260 | */ | ||
261 | if (*cur_index >= num_envp - 1) | ||
262 | return -ENOMEM; | 269 | return -ENOMEM; |
263 | 270 | } | |
264 | envp[*cur_index] = buffer + *cur_len; | ||
265 | 271 | ||
266 | va_start(args, format); | 272 | va_start(args, format); |
267 | *cur_len += vsnprintf(envp[*cur_index], | 273 | len = vsnprintf(&env->buf[env->buflen], |
268 | max(buffer_size - *cur_len, 0), | 274 | sizeof(env->buf) - env->buflen, |
269 | format, args) + 1; | 275 | format, args); |
270 | va_end(args); | 276 | va_end(args); |
271 | 277 | ||
272 | if (*cur_len > buffer_size) | 278 | if (len >= (sizeof(env->buf) - env->buflen)) { |
279 | printk(KERN_ERR "add_uevent_var: buffer size too small\n"); | ||
280 | WARN_ON(1); | ||
273 | return -ENOMEM; | 281 | return -ENOMEM; |
282 | } | ||
274 | 283 | ||
275 | (*cur_index)++; | 284 | env->envp[env->envp_idx++] = &env->buf[env->buflen]; |
285 | env->buflen += len + 1; | ||
276 | return 0; | 286 | return 0; |
277 | } | 287 | } |
278 | EXPORT_SYMBOL_GPL(add_uevent_var); | 288 | EXPORT_SYMBOL_GPL(add_uevent_var); |
@@ -293,5 +303,3 @@ static int __init kobject_uevent_init(void) | |||
293 | 303 | ||
294 | postcore_initcall(kobject_uevent_init); | 304 | postcore_initcall(kobject_uevent_init); |
295 | #endif | 305 | #endif |
296 | |||
297 | #endif /* CONFIG_HOTPLUG */ | ||