Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1730979 > unrolled thread

[PATCH v8 6/6] powerpc/fadump: use the new parse_args callback arguments

Started byMichal Suchanek <msuchanek@suse.de>
First post2017-09-12 18:10 +0200
Last post2017-09-25 20:50 +0200
Articles 5 — 3 participants

Back to article view | Back to linux.kernel

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  [PATCH v8 6/6] powerpc/fadump: use the new parse_args callback arguments Michal Suchanek <msuchanek@suse.de> - 2017-09-12 18:10 +0200
    [PATCH 1/6] lib/cmdline.c: Add backslash support to kernel commandline parsing. Michal Suchanek <msuchanek@suse.de> - 2017-09-15 19:10 +0200
      Re: [PATCH 1/6] lib/cmdline.c: Add backslash support to kernel  commandline parsing. Al Viro <viro@ZenIV.linux.org.uk> - 2017-09-15 19:20 +0200
        Re: [PATCH 1/6] lib/cmdline.c: Add backslash support to kernel  commandline parsing. Michal Suchánek <msuchanek@suse.de> - 2017-09-15 19:30 +0200
          Re: [PATCH 1/6] lib/cmdline.c: Add backslash support to kernel  commandline parsing. Michal Suchánek <msuchanek@suse.de> - 2017-09-25 20:50 +0200

#1730979 — [PATCH v8 6/6] powerpc/fadump: use the new parse_args callback arguments

FromMichal Suchanek <msuchanek@suse.de>
Date2017-09-12 18:10 +0200
Subject[PATCH v8 6/6] powerpc/fadump: use the new parse_args callback arguments
Message-ID<uoVV8-3tT-21@gated-at.bofh.it>
Signed-off-by: Michal Suchanek <msuchanek@suse.de>
---
 arch/powerpc/kernel/fadump.c | 47 ++++++++++++--------------------------------
 1 file changed, 13 insertions(+), 34 deletions(-)

diff --git a/arch/powerpc/kernel/fadump.c b/arch/powerpc/kernel/fadump.c
index 8778e1cc0380..1678d99ea835 100644
--- a/arch/powerpc/kernel/fadump.c
+++ b/arch/powerpc/kernel/fadump.c
@@ -481,33 +481,19 @@ struct param_info {
 };
 
 static void __init fadump_update_params(struct param_info *param_info,
-					char *param, char *val)
+					char *param, char *val,
+					char *currant, char *next)
 {
-	ptrdiff_t param_offset = param - param_info->tmp_cmdline;
+	ptrdiff_t param_offset = currant - param_info->tmp_cmdline;
 	size_t vallen = val ? strlen(val) : 0;
 	char *tgt = param_info->cmdline + param_offset
 				- param_info->shortening;
-	int shortening = 0;
-	int quoted = 0;
+	int shortening = ((next - 1) - (currant))
+		- (FADUMP_EXTRA_ARGS_LEN + 1 + vallen);
 
 	if (!val)
 		return;
 
-	/* leading '"' removed from parameter */
-	if ((param > param_info->tmp_cmdline) && *(param - 1) == '"') {
-		quoted = 1;
-		shortening += 1;
-		tgt--;
-	}
-
-	/* next_arg removes one leading and one trailing '"' */
-	if ((*(tgt + FADUMP_EXTRA_ARGS_LEN + 1 + vallen + shortening) == '"') &&
-	    (quoted || (*(tgt + FADUMP_EXTRA_ARGS_LEN + 1) == '"'))) {
-		shortening += 1;
-		if (!quoted)
-			shortening += 1;
-	}
-
 	/* remove one leading and one trailing quote if both are present */
 	if ((val[0] == '"') && (val[vallen - 1] == '"')) {
 		shortening += 2;
@@ -515,22 +501,15 @@ static void __init fadump_update_params(struct param_info *param_info,
 		val++;
 	}
 
-	/* some characters were removed - move the trailing part of cmdline */
-	if (shortening) {
-		char *src;
+	strncpy(tgt, FADUMP_EXTRA_ARGS_PARAM, FADUMP_EXTRA_ARGS_LEN);
+	tgt += FADUMP_EXTRA_ARGS_LEN;
+	*tgt++ = ' ';
+	strncpy(tgt, val, vallen);
+	tgt += vallen;
 
-		strncpy(tgt, FADUMP_EXTRA_ARGS_PARAM, FADUMP_EXTRA_ARGS_LEN);
-		tgt += FADUMP_EXTRA_ARGS_LEN;
-		*tgt++ = ' ';
-
-		strncpy(tgt, val, vallen);
-		tgt += vallen;
-
-		src = tgt + shortening;
+	if (shortening) {
+		char *src = tgt + shortening;
 		memmove(tgt, src, strlen(src) + 1);
-	} else {
-		/* remove the '=' */
-		*(tgt + FADUMP_EXTRA_ARGS_LEN) = ' ';
 	}
 
 	param_info->shortening += shortening;
@@ -550,7 +529,7 @@ static int __init fadump_rework_cmdline_params(char *param, char *val,
 		     strlen(FADUMP_EXTRA_ARGS_PARAM) - 1))
 		return 0;
 
-	fadump_update_params(param_info, param, val);
+	fadump_update_params(param_info, param, val, currant, next);
 
 	return 0;
 }
-- 
2.10.2

[toc] | [next] | [standalone]


#1732964 — [PATCH 1/6] lib/cmdline.c: Add backslash support to kernel commandline parsing.

FromMichal Suchanek <msuchanek@suse.de>
Date2017-09-15 19:10 +0200
Subject[PATCH 1/6] lib/cmdline.c: Add backslash support to kernel commandline parsing.
Message-ID<uq2hP-5TN-3@gated-at.bofh.it>
In reply to#1730979
This allows passing quotes in kernel arguments. It is useful for passing
fadump nested arguemnts in fadump_extra_args and might be useful if
somebody wanted to pass a double quote directly as part of an argument.

It is also useful to have quoting grammar more similar to shells and
bootloaders.

Signed-off-by: Michal Suchanek <msuchanek@suse.de>
---
 lib/cmdline.c | 41 ++++++++++++++++++++---------------------
 1 file changed, 20 insertions(+), 21 deletions(-)

diff --git a/lib/cmdline.c b/lib/cmdline.c
index 6d398a8b63fc..d98bdc017545 100644
--- a/lib/cmdline.c
+++ b/lib/cmdline.c
@@ -193,30 +193,36 @@ bool parse_option_str(const char *str, const char *option)
 
 /*
  * Parse a string to get a param value pair.
- * You can use " around spaces, but can't escape ".
+ * You can use " around spaces, and you can escape with \
  * Hyphens and underscores equivalent in parameter names.
  */
 char *next_arg(char *args, char **param, char **val)
 {
 	unsigned int i, equals = 0;
-	int in_quote = 0, quoted = 0;
+	int in_quote = 0, backslash = 0;
 	char *next;
 
-	if (*args == '"') {
-		args++;
-		in_quote = 1;
-		quoted = 1;
-	}
-
 	for (i = 0; args[i]; i++) {
-		if (isspace(args[i]) && !in_quote)
+		if (isspace(args[i]) && !in_quote && !backslash)
 			break;
-		if (equals == 0) {
-			if (args[i] == '=')
-				equals = i;
+
+		if ((equals == 0) && (args[i] == '='))
+			equals = i;
+
+		if (!backslash) {
+			if ((args[i] == '"') || (args[i] == '\\')) {
+				if (args[i] == '"')
+					in_quote = !in_quote;
+				if (args[i] == '\\')
+					backslash = 1;
+
+				memmove(args + 1, args, i);
+				args++;
+				i--;
+			}
+		} else {
+			backslash = 0;
 		}
-		if (args[i] == '"')
-			in_quote = !in_quote;
 	}
 
 	*param = args;
@@ -225,13 +231,6 @@ char *next_arg(char *args, char **param, char **val)
 	else {
 		args[equals] = '\0';
 		*val = args + equals + 1;
-
-		/* Don't include quotes in value. */
-		if ((args[i-1] == '"') && ((quoted) || (**val == '"'))) {
-			args[i-1] = '\0';
-			if (!quoted)
-				(*val)++;
-		}
 	}
 
 	if (args[i]) {
-- 
2.10.2

[toc] | [prev] | [next] | [standalone]


#1732965 — Re: [PATCH 1/6] lib/cmdline.c: Add backslash support to kernel commandline parsing.

FromAl Viro <viro@ZenIV.linux.org.uk>
Date2017-09-15 19:20 +0200
SubjectRe: [PATCH 1/6] lib/cmdline.c: Add backslash support to kernel commandline parsing.
Message-ID<uq2rv-5Xm-3@gated-at.bofh.it>
In reply to#1732964
On Fri, Sep 15, 2017 at 07:02:46PM +0200, Michal Suchanek wrote:

>  	for (i = 0; args[i]; i++) {
> -		if (isspace(args[i]) && !in_quote)
> +		if (isspace(args[i]) && !in_quote && !backslash)
>  			break;
> -		if (equals == 0) {
> -			if (args[i] == '=')
> -				equals = i;
> +
> +		if ((equals == 0) && (args[i] == '='))
> +			equals = i;
> +
> +		if (!backslash) {
> +			if ((args[i] == '"') || (args[i] == '\\')) {
> +				if (args[i] == '"')
> +					in_quote = !in_quote;
> +				if (args[i] == '\\')
> +					backslash = 1;
> +
> +				memmove(args + 1, args, i);
> +				args++;
> +				i--;
> +			}
> +		} else {
> +			backslash = 0;
>  		}
> -		if (args[i] == '"')
> -			in_quote = !in_quote;
>  	}

... and that makes for Unidiomatic Work With Strings Award for this September.
Using memmove() for string rewrite is almost always bad taste; in this case
it's also (as usual) broken.

[toc] | [prev] | [next] | [standalone]


#1732973 — Re: [PATCH 1/6] lib/cmdline.c: Add backslash support to kernel commandline parsing.

FromMichal Suchánek <msuchanek@suse.de>
Date2017-09-15 19:30 +0200
SubjectRe: [PATCH 1/6] lib/cmdline.c: Add backslash support to kernel commandline parsing.
Message-ID<uq2Bb-61b-11@gated-at.bofh.it>
In reply to#1732965
On Fri, 15 Sep 2017 18:14:09 +0100
Al Viro <viro@ZenIV.linux.org.uk> wrote:

> On Fri, Sep 15, 2017 at 07:02:46PM +0200, Michal Suchanek wrote:
> 
> >  	for (i = 0; args[i]; i++) {
> > -		if (isspace(args[i]) && !in_quote)
> > +		if (isspace(args[i]) && !in_quote && !backslash)
> >  			break;
> > -		if (equals == 0) {
> > -			if (args[i] == '=')
> > -				equals = i;
> > +
> > +		if ((equals == 0) && (args[i] == '='))
> > +			equals = i;
> > +
> > +		if (!backslash) {
> > +			if ((args[i] == '"') || (args[i] == '\\'))
> > {
> > +				if (args[i] == '"')
> > +					in_quote = !in_quote;
> > +				if (args[i] == '\\')
> > +					backslash = 1;
> > +
> > +				memmove(args + 1, args, i);
> > +				args++;
> > +				i--;
> > +			}
> > +		} else {
> > +			backslash = 0;
> >  		}
> > -		if (args[i] == '"')
> > -			in_quote = !in_quote;
> >  	}  
> 
> ... and that makes for Unidiomatic Work With Strings Award for this
> September. Using memmove() for string rewrite is almost always bad
> taste; in this case it's also (as usual) broken.

Care to share how it is broken?

Thanks

Michal

[toc] | [prev] | [next] | [standalone]


#1739213 — Re: [PATCH 1/6] lib/cmdline.c: Add backslash support to kernel commandline parsing.

FromMichal Suchánek <msuchanek@suse.de>
Date2017-09-25 20:50 +0200
SubjectRe: [PATCH 1/6] lib/cmdline.c: Add backslash support to kernel commandline parsing.
Message-ID<utGC5-1Kz-1@gated-at.bofh.it>
In reply to#1732973
On Fri, 15 Sep 2017 19:28:56 +0200
Michal Suchánek <msuchanek@suse.de> wrote:

> On Fri, 15 Sep 2017 18:14:09 +0100
> Al Viro <viro@ZenIV.linux.org.uk> wrote:
> 
> > On Fri, Sep 15, 2017 at 07:02:46PM +0200, Michal Suchanek wrote:
> >   
> > >  	for (i = 0; args[i]; i++) {
> > > -		if (isspace(args[i]) && !in_quote)
> > > +		if (isspace(args[i]) && !in_quote && !backslash)
> > >  			break;
> > > -		if (equals == 0) {
> > > -			if (args[i] == '=')
> > > -				equals = i;
> > > +
> > > +		if ((equals == 0) && (args[i] == '='))
> > > +			equals = i;
> > > +
> > > +		if (!backslash) {
> > > +			if ((args[i] == '"') || (args[i] ==
> > > '\\')) {
> > > +				if (args[i] == '"')
> > > +					in_quote = !in_quote;
> > > +				if (args[i] == '\\')
> > > +					backslash = 1;
> > > +
> > > +				memmove(args + 1, args, i);
> > > +				args++;
> > > +				i--;
> > > +			}
> > > +		} else {
> > > +			backslash = 0;
> > >  		}
> > > -		if (args[i] == '"')
> > > -			in_quote = !in_quote;
> > >  	}    
> > 
> > ... and that makes for Unidiomatic Work With Strings Award for this
> > September. Using memmove() for string rewrite is almost always bad
> > taste; in this case it's also (as usual) broken.  
> 
> Care to share how it is broken?

Guess not. I will assume it is perfectly fine then. It works perfectly
fine in my testing.

Using memmove for string rewrite is not a matter of taste. It is the
only library function with sane semantics for rewrite of anything. Then
again open-coding it is always an option and maybe in better taste for
some :->

Michal

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web