[PATCH] modutils: Add /proc/cmdline module option parsing support

Denys Vlasenko vda.linux at googlemail.com
Sun Jan 24 14:44:19 UTC 2010


On Saturday 23 January 2010 00:50, Ozan Çağlayan wrote:
> This adds module option parsing support from /proc/cmdline like in
> module-init-tools.
> 
> The options passed through /proc/cmdline are always squeezed by the ones
> provided with command line arguments.
> 
> It's tested against single module loading through module name and alias.
> 
> Signed-off-by: Ozan Çağlayan <ozan at pardus.org.tr>
> ---
>  modutils/modprobe.c |    3 ++-
>  modutils/modutils.c |   42 ++++++++++++++++++++++++++++++++++++++++++
>  modutils/modutils.h |    1 +
>  3 files changed, 45 insertions(+), 1 deletions(-)
> 
> diff --git a/modutils/modprobe.c b/modutils/modprobe.c
> index 2860ae0..ca4d75a 100644
> --- a/modutils/modprobe.c
> +++ b/modutils/modprobe.c
> @@ -82,7 +82,7 @@ static char *gather_options_str(char *opts, const char *append)
>  	/* Speed-optimized. We call gather_options_str many times. */
>  	if (opts == NULL) {
>  		opts = xstrdup(append);
> -	} else {
> +	} else if (append) {
>  		int optlen = strlen(opts);
>  		opts = xrealloc(opts, optlen + strlen(append) + 2);
>  		sprintf(opts + optlen, " %s", append);
> @@ -286,6 +286,7 @@ static int do_modprobe(struct module_entry *m)
>  
>  		options = m2->options;
>  		m2->options = NULL;
> +		options = gather_options_str(options, parse_kcmdline_module_options(m2->modname));

You are using parse_kcmdline_module_options only in modprobe.c,
it makes sense to make it a static function in this file,
not keep it in modutils.c

>  		if (m == m2)
>  			options = gather_options_str(options, G.cmdline_mopts);
>  		rc = bb_init_module(fn, options);
> diff --git a/modutils/modutils.c b/modutils/modutils.c
> index 850a868..98fac74 100644
> --- a/modutils/modutils.c
> +++ b/modutils/modutils.c
> @@ -111,6 +111,48 @@ void* FAST_FUNC try_to_mmap_module(const char *filename, size_t *image_size_p)
>  }
>  #endif
>  
> +char * FAST_FUNC parse_kcmdline_module_options(char *modulename)
> +{
> +	char *kcmdline_buf;
> +	char *options;
> +	char *token, *sep, *modname, *modoption;
> +	int len;
> +
> +	/* defined in arch/<architecture>/include/asm/setup.h
> +	 * maximum is 2048 for IA64 and SPARC */
> +	kcmdline_buf = xmalloc(2048);
> +
> +	len = open_read_close("/proc/cmdline", kcmdline_buf, 2047);
> +	if (len < 0)
> +		return NULL;
> +
> +	/* Read was succesful */
> +	kcmdline_buf[len] = '\0';
> +
> +	/* Initial allocation */
> +	options = xzalloc(1);
> +	len = 0;
> +
> +	while ((token = strsep(&kcmdline_buf, "\n\t ")) != NULL) {
> +		if ((sep = strchr(token, '.')) && strchr(sep, '=')) {

Please.
                sep = strchr(token, '.');
                if (sep && strchr(sep, '=')) ...

> +			modname = token;
> +			*sep = '\0';
> +			modoption = ++sep;
> +			if (!strcmp(modulename, modname)) {

"!" means "not". "if not compare modulename with modname" ?!
You meant if (strcmp(modulename, modname) == 0). Yes, for compiler
it is the same. For human reader, 2nd form is better.

> +				/* Found parameters for the relevant module */
> +				options = xrealloc(options, len + strlen(modoption) + 2);
> +				len += sprintf(options + len, "%s ", modoption);

Reuse gather_options_str(options, modoption) for this?

> +			}
> +		}
> +	}
> +	free(kcmdline_buf);
> +
> +	/* Strip last space */
> +	if (len > 0)
> +	    options[len-1] = '\0';
> +	return options;
> +}
> +
>  /* Return:
>   * 0 on success,
>   * -errno on open/read error,
> diff --git a/modutils/modutils.h b/modutils/modutils.h
> index 131a508..1f787a4 100644
> --- a/modutils/modutils.h
> +++ b/modutils/modutils.h
> @@ -22,6 +22,7 @@ char *replace_underscores(char *s) FAST_FUNC;
>  int string_to_llist(char *string, llist_t **llist, const char *delim) FAST_FUNC;
>  char *filename2modname(const char *filename, char *modname) FAST_FUNC;
>  char *parse_cmdline_module_options(char **argv) FAST_FUNC;
> +char *parse_kcmdline_module_options(char *modulename) FAST_FUNC;
>  
>  #define INSMOD_OPTS \
>  	"vq" \


--
vda


More information about the busybox mailing list