bootconfig: do not put quotes on cmdline items unless necessary
authorRasmus Villemoes <linux@rasmusvillemoes.dk>
Wed, 20 Mar 2024 10:19:52 +0000 (11:19 +0100)
committerAndrew Morton <akpm@linux-foundation.org>
Fri, 26 Apr 2024 04:07:01 +0000 (21:07 -0700)
When trying to migrate to using bootconfig to embed the kernel's and
PID1's command line with the kernel image itself, and so allowing changing
that without modifying the bootloader, I noticed that /proc/cmdline
changed from e.g.

  console=ttymxc0,115200n8 cma=128M quiet -- --log-level=notice

to

  console="ttymxc0,115200n8" cma="128M" quiet -- --log-level="notice"

The kernel parameters are parsed just fine, and the quotes are indeed
stripped from the actual argv[] given to PID1.  However, the quoting
doesn't really serve any purpose and looks excessive, and might confuse
some (naive) userspace tool trying to parse /proc/cmdline.  So do not
quote the value unless it contains whitespace.

Link: https://lkml.kernel.org/r/20240320101952.62135-1-linux@rasmusvillemoes.dk
Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
init/main.c

index 881f6230ee59e9675eb98b62adf761ee74823a16..6b4b656cef1d9f90cef99d86a9189c528ba64703 100644 (file)
@@ -327,7 +327,7 @@ static int __init xbc_snprint_cmdline(char *buf, size_t size,
 {
        struct xbc_node *knode, *vnode;
        char *end = buf + size;
-       const char *val;
+       const char *val, *q;
        int ret;
 
        xbc_node_for_each_key_value(root, knode, val) {
@@ -345,8 +345,14 @@ static int __init xbc_snprint_cmdline(char *buf, size_t size,
                        continue;
                }
                xbc_array_for_each_value(vnode, val) {
-                       ret = snprintf(buf, rest(buf, end), "%s=\"%s\" ",
-                                      xbc_namebuf, val);
+                       /*
+                        * For prettier and more readable /proc/cmdline, only
+                        * quote the value when necessary, i.e. when it contains
+                        * whitespace.
+                        */
+                       q = strpbrk(val, " \t\r\n") ? "\"" : "";
+                       ret = snprintf(buf, rest(buf, end), "%s=%s%s%s ",
+                                      xbc_namebuf, q, val, q);
                        if (ret < 0)
                                return ret;
                        buf += ret;