diff options
author | Roland Dreier <roland@topspin.com> | 2004-09-29 03:24:36 -0700 |
---|---|---|
committer | Greg Kroah-Hartman <greg@kroah.com> | 2004-09-29 03:24:36 -0700 |
commit | e2661e66605c57210ea9e689b51fc63ac001c8b3 (patch) | |
tree | 6b505138a674f09229e20729a2d4cb7eebd543a8 /lib | |
parent | e0dff4c88eb8c627cb9c34ad3a2e0eafae4f0165 (diff) | |
download | history-e2661e66605c57210ea9e689b51fc63ac001c8b3.tar.gz |
[PATCH] kobject: add add_hotplug_env_var()
Add a (non-inlined) add_hotplug_env_var() function to <linux/kobject.h>
and lib/kobject.c. There's a lot of boilerplate code involved in
setting environment variables in a hotplug method, so we should have a
convenience function to consolidate it (and avoid subtle bugs).
Signed-off-by: Roland Dreier <roland@topspin.com>
Signed-off-by: Greg Kroah-Hartman <greg@kroah.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/kobject_uevent.c | 52 |
1 files changed, 51 insertions, 1 deletions
diff --git a/lib/kobject_uevent.c b/lib/kobject_uevent.c index 33e0cef31ba869..67c1d11bf78898 100644 --- a/lib/kobject_uevent.c +++ b/lib/kobject_uevent.c @@ -290,6 +290,56 @@ exit: return; } EXPORT_SYMBOL(kobject_hotplug); -#endif /* CONFIG_HOTPLUG */ +/** + * add_hotplug_env_var - helper for creating hotplug environment variables + * @envp: Pointer to table of environment variables, as passed into + * hotplug() method. + * @num_envp: Number of environment variable slots available, as + * passed into hotplug() method. + * @cur_index: Pointer to current index into @envp. It should be + * initialized to 0 before the first call to add_hotplug_env_var(), + * and will be incremented on success. + * @buffer: Pointer to buffer for environment variables, as passed + * into hotplug() method. + * @buffer_size: Length of @buffer, as passed into hotplug() method. + * @cur_len: Pointer to current length of space used in @buffer. + * Should be initialized to 0 before the first call to + * add_hotplug_env_var(), and will be incremented on success. + * @format: Format for creating environment variable (of the form + * "XXX=%x") for snprintf(). + * + * Returns 0 if environment variable was added successfully or -ENOMEM + * if no space was available. + */ +int add_hotplug_env_var(char **envp, int num_envp, int *cur_index, + char *buffer, int buffer_size, int *cur_len, + const char *format, ...) +{ + va_list args; + + /* + * We check against num_envp - 1 to make sure there is at + * least one slot left after we return, since the hotplug + * method needs to set the last slot to NULL. + */ + if (*cur_index >= num_envp - 1) + return -ENOMEM; + + envp[*cur_index] = buffer + *cur_len; + va_start(args, format); + *cur_len += vsnprintf(envp[*cur_index], + max(buffer_size - *cur_len, 0), + format, args) + 1; + va_end(args); + + if (*cur_len > buffer_size) + return -ENOMEM; + + (*cur_index)++; + return 0; +} +EXPORT_SYMBOL(add_hotplug_env_var); + +#endif /* CONFIG_HOTPLUG */ |