From patchwork Fri Aug 19 06:56:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 12948442 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 559C6C32771 for ; Fri, 19 Aug 2022 06:58:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346746AbiHSG6d (ORCPT ); Fri, 19 Aug 2022 02:58:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47850 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346469AbiHSG6b (ORCPT ); Fri, 19 Aug 2022 02:58:31 -0400 Received: from conuserg-08.nifty.com (conuserg-08.nifty.com [210.131.2.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A7889BCA0; Thu, 18 Aug 2022 23:58:18 -0700 (PDT) Received: from grover.sesame ([133.106.49.178]) (authenticated) by conuserg-08.nifty.com with ESMTP id 27J6uUCN012757; Fri, 19 Aug 2022 15:56:32 +0900 DKIM-Filter: OpenDKIM Filter v2.10.3 conuserg-08.nifty.com 27J6uUCN012757 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.com; s=dec2015msa; t=1660892193; bh=NhUP1vLNnfADibxISBC03SpUDFkdK73SDeWcDjzouOw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=u0VLiN8PgP35sOrGjFCY4rF6Rc+YWUhvi1vAihvjdYE567Wc/V0zQ+b6WKiUaQNis c+I3LeB1kckoWnfhLm4G/6u3Zr7eVkEaWSfUWkuYrPlr8vSbyeU/1ZL5YTR2ilBA1L it7QDST4MJzMDBDTPNtyqfj/ku/g1IndEkHOp6jB4tlEBzw1N3U4OwfPZe02ZJDn50 EhuvibAmzpbiiEESaUYzvk8b37srF7UhfTYIN3gWyHPBzN8S9jAjPNeFA8gVoozSf8 /+ZxQVQ+0/A2yNwMP6Pw45JXH9XN/kNmef45fnmxOI0p6kmyGQXNulBdmpi1l7VsiN a7OSaBVdvEVYg== X-Nifty-SrcIP: [133.106.49.178] From: Masahiro Yamada To: linux-kbuild@vger.kernel.org Cc: Nick Desaulniers , Alexandre Belloni , Randy Dunlap , Richard Purdie , Masahiro Yamada , linux-kernel@vger.kernel.org Subject: [RFC PATCH 1/3] kconfig: move declarations for prepossessing to internal.h Date: Fri, 19 Aug 2022 15:56:01 +0900 Message-Id: <20220819065604.295572-2-masahiroy@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220819065604.295572-1-masahiroy@kernel.org> References: <20220819065604.295572-1-masahiroy@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org These are used by the kconfig core part. The user interface part (conf, mconf, nconf, gconf, or xconf variants) does not need them. Signed-off-by: Masahiro Yamada --- scripts/kconfig/confdata.c | 1 + scripts/kconfig/internal.h | 17 +++++++++++++++++ scripts/kconfig/lexer.l | 1 + scripts/kconfig/lkc_proto.h | 13 ------------- scripts/kconfig/preprocess.c | 1 + 5 files changed, 20 insertions(+), 13 deletions(-) diff --git a/scripts/kconfig/confdata.c b/scripts/kconfig/confdata.c index c4340c90e172..aa8e73828de3 100644 --- a/scripts/kconfig/confdata.c +++ b/scripts/kconfig/confdata.c @@ -19,6 +19,7 @@ #include #include "lkc.h" +#include "internal.h" /* return true if 'path' exists, false otherwise */ static bool is_present(const char *path) diff --git a/scripts/kconfig/internal.h b/scripts/kconfig/internal.h index 2f7298c21b64..8e0e6d315b6c 100644 --- a/scripts/kconfig/internal.h +++ b/scripts/kconfig/internal.h @@ -1,4 +1,8 @@ /* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Declarations necessary for the kconfig core, but not for + * conf, mconf, nconf, gconf, or xconf variants. + */ #ifndef INTERNAL_H #define INTERNAL_H @@ -6,4 +10,17 @@ struct menu; extern struct menu *current_menu, *current_entry; +/* preprocess.c */ +enum variable_flavor { + VAR_SIMPLE, + VAR_RECURSIVE, + VAR_APPEND, +}; +void env_write_dep(FILE *f, const char *auto_conf_name); +void variable_add(const char *name, const char *value, + enum variable_flavor flavor); +void variable_all_del(void); +char *expand_dollar(const char **str); +char *expand_one_token(const char **str); + #endif /* INTERNAL_H */ diff --git a/scripts/kconfig/lexer.l b/scripts/kconfig/lexer.l index cc386e443683..43e12d0945a7 100644 --- a/scripts/kconfig/lexer.l +++ b/scripts/kconfig/lexer.l @@ -14,6 +14,7 @@ #include #include "lkc.h" +#include "internal.h" #include "parser.tab.h" #define YY_DECL static int yylex1(void) diff --git a/scripts/kconfig/lkc_proto.h b/scripts/kconfig/lkc_proto.h index edd1e617b25c..7fb4953d16ca 100644 --- a/scripts/kconfig/lkc_proto.h +++ b/scripts/kconfig/lkc_proto.h @@ -35,18 +35,5 @@ const char * sym_get_string_value(struct symbol *sym); const char * prop_get_type_name(enum prop_type type); -/* preprocess.c */ -enum variable_flavor { - VAR_SIMPLE, - VAR_RECURSIVE, - VAR_APPEND, -}; -void env_write_dep(FILE *f, const char *auto_conf_name); -void variable_add(const char *name, const char *value, - enum variable_flavor flavor); -void variable_all_del(void); -char *expand_dollar(const char **str); -char *expand_one_token(const char **str); - /* expr.c */ void expr_print(struct expr *e, void (*fn)(void *, struct symbol *, const char *), void *data, int prevtoken); diff --git a/scripts/kconfig/preprocess.c b/scripts/kconfig/preprocess.c index 748da578b418..aeb3fe362c04 100644 --- a/scripts/kconfig/preprocess.c +++ b/scripts/kconfig/preprocess.c @@ -11,6 +11,7 @@ #include "list.h" #include "lkc.h" +#include "internal.h" #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) From patchwork Fri Aug 19 06:56:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 12948445 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 242C6C32792 for ; Fri, 19 Aug 2022 06:58:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346793AbiHSG6f (ORCPT ); Fri, 19 Aug 2022 02:58:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47984 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346757AbiHSG6e (ORCPT ); Fri, 19 Aug 2022 02:58:34 -0400 Received: from conuserg-08.nifty.com (conuserg-08.nifty.com [210.131.2.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E3D02764F; Thu, 18 Aug 2022 23:58:31 -0700 (PDT) Received: from grover.sesame ([133.106.49.178]) (authenticated) by conuserg-08.nifty.com with ESMTP id 27J6uUCO012757; Fri, 19 Aug 2022 15:56:34 +0900 DKIM-Filter: OpenDKIM Filter v2.10.3 conuserg-08.nifty.com 27J6uUCO012757 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.com; s=dec2015msa; t=1660892195; bh=FLXSTnFwujGa3rCeIE2bMjhYjFgNU8XCFwv3G9iMhZM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=T8Bd90crOnL14/xeeuc1U1N2lGENAclPMqXwT2iEYn7EVgcdQCPnLberVdMw3uYtz wHeBNo5JuYXDwGHar0S/TIAUXtyi88Zofef0rn0wgpfahb55KVADojoTNCCzxpg2e3 x0tPJ/Z282CjRLpxl1xCGIA4T4e8WXbGHSDqlXfu9DfNnK4Jbz+J4xtb8BzudBuro3 Wyz2ywK/+bug/Io/SLCrkiBZVbKxDF45Gcd7NY3sCtowfDv1NPk7DbaLjdW35dwBKr Stv7K3JOx9hA8g1Ho8BsaQrfKWXFIYfRZfLtBmtTzdnC11aGT6tolMrRMbNHHe+7i7 oyDEVjrPjywKw== X-Nifty-SrcIP: [133.106.49.178] From: Masahiro Yamada To: linux-kbuild@vger.kernel.org Cc: Nick Desaulniers , Alexandre Belloni , Randy Dunlap , Richard Purdie , Masahiro Yamada , Jonathan Corbet , Michal Marek , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 2/3] kconfig: allow to choose the shell for $(shell ) functions Date: Fri, 19 Aug 2022 15:56:02 +0900 Message-Id: <20220819065604.295572-3-masahiroy@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220819065604.295572-1-masahiroy@kernel.org> References: <20220819065604.295572-1-masahiroy@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org GNU Make uses /bin/sh by default for running recipe lines and $(shell ) functions. You can change the shell by setting the 'SHELL' variable. Unlike most variables, 'SHELL' is never set from the environment. [1] Currently, Kconfig does not provide any way to change the default shell. /bin/sh is always used for running $(shell,...) because do_shell() is implemented by using popen(3). This commit allows users to change the shell for Kconfig in a similar way to GNU Make; you can set the 'SHELL' variable in a Kconfig file to override the default shell. It is not taken from the environment. The change is effective only for $(shell,...) invocations called after the 'SHELL' assignment. [1]: https://www.gnu.org/software/make/manual/html_node/Choosing-the-Shell.html Signed-off-by: Masahiro Yamada --- .../kbuild/kconfig-macro-language.rst | 4 ++ scripts/kconfig/internal.h | 1 + scripts/kconfig/parser.y | 1 + scripts/kconfig/preprocess.c | 65 +++++++++++++++---- 4 files changed, 57 insertions(+), 14 deletions(-) diff --git a/Documentation/kbuild/kconfig-macro-language.rst b/Documentation/kbuild/kconfig-macro-language.rst index 6163467f6ae4..fe8c6982179e 100644 --- a/Documentation/kbuild/kconfig-macro-language.rst +++ b/Documentation/kbuild/kconfig-macro-language.rst @@ -112,6 +112,10 @@ Kconfig currently supports the following built-in functions. replaced with a space. Any trailing newlines are deleted. The standard error is not returned, nor is any program exit status. + The program used as the shell is taken from the variable SHELL. If it is not + set anywhere in your Kconfig file, /bin/sh is used as the shell. + Unlike most variables, the variable SHELL is never set from the environment. + - $(info,text) The "info" function takes a single argument and prints it to stdout. diff --git a/scripts/kconfig/internal.h b/scripts/kconfig/internal.h index 8e0e6d315b6c..c18017650e54 100644 --- a/scripts/kconfig/internal.h +++ b/scripts/kconfig/internal.h @@ -19,6 +19,7 @@ enum variable_flavor { void env_write_dep(FILE *f, const char *auto_conf_name); void variable_add(const char *name, const char *value, enum variable_flavor flavor); +void variable_init(void); void variable_all_del(void); char *expand_dollar(const char **str); char *expand_one_token(const char **str); diff --git a/scripts/kconfig/parser.y b/scripts/kconfig/parser.y index 2af7ce4e1531..436afaef9228 100644 --- a/scripts/kconfig/parser.y +++ b/scripts/kconfig/parser.y @@ -483,6 +483,7 @@ void conf_parse(const char *name) zconf_initscan(name); _menu_init(); + variable_init(); if (getenv("ZCONF_DEBUG")) yydebug = 1; diff --git a/scripts/kconfig/preprocess.c b/scripts/kconfig/preprocess.c index aeb3fe362c04..608a84e7f5d6 100644 --- a/scripts/kconfig/preprocess.c +++ b/scripts/kconfig/preprocess.c @@ -8,6 +8,7 @@ #include #include #include +#include #include "list.h" #include "lkc.h" @@ -141,24 +142,59 @@ static char *do_lineno(int argc, char *argv[]) static char *do_shell(int argc, char *argv[]) { - FILE *p; + int pipefd[2]; + pid_t pid; char buf[4096]; - char *cmd; - size_t nread; + ssize_t nread; int i; - cmd = argv[0]; - - p = popen(cmd, "r"); - if (!p) { - perror(cmd); + if (pipe(pipefd) < 0) { + perror("pipe"); + exit(1); + } + pid = fork(); + if (pid < -1) { + perror("fork"); exit(1); } - nread = fread(buf, 1, sizeof(buf), p); + if (pid == 0) { /* child */ + char *shell; + + /* duplicate the write end to stdout */ + if (dup2(pipefd[1], STDOUT_FILENO) < 0) { + perror("dup2"); + _exit(1); + } + + /* + * Do not leak file descriptors to the child process + * (including the read end of the pipe). + * Closing up to 15 is enough for us? + */ + for (i = STDERR_FILENO + 1; i < 16; i++) + close(i); + + shell = expand_string("$(SHELL)"); + + execl(shell, shell, "-c", argv[0], NULL); + perror("execl"); + + free(shell); + + _exit(1); + } + + /* parent */ + + close(pipefd[1]); /* the write end is unneeded */ + + nread = read(pipefd[0], buf, sizeof(buf)); if (nread == sizeof(buf)) nread--; + close(pipefd[0]); /* now close the read end */ + /* remove trailing new lines */ while (nread > 0 && buf[nread - 1] == '\n') nread--; @@ -171,11 +207,6 @@ static char *do_shell(int argc, char *argv[]) buf[i] = ' '; } - if (pclose(p) == -1) { - perror(cmd); - exit(1); - } - return xstrdup(buf); } @@ -330,6 +361,12 @@ static void variable_del(struct variable *v) free(v); } +void variable_init(void) +{ + /* Set the default shell */ + variable_add("SHELL", "/bin/sh", VAR_RECURSIVE); +} + void variable_all_del(void) { struct variable *v, *tmp; From patchwork Fri Aug 19 06:56:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 12948443 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2F872C25B0E for ; Fri, 19 Aug 2022 06:58:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346751AbiHSG6c (ORCPT ); Fri, 19 Aug 2022 02:58:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47854 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243232AbiHSG6b (ORCPT ); Fri, 19 Aug 2022 02:58:31 -0400 Received: from conuserg-08.nifty.com (conuserg-08.nifty.com [210.131.2.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 057A913D5D; Thu, 18 Aug 2022 23:58:18 -0700 (PDT) Received: from grover.sesame ([133.106.49.178]) (authenticated) by conuserg-08.nifty.com with ESMTP id 27J6uUCP012757; Fri, 19 Aug 2022 15:56:35 +0900 DKIM-Filter: OpenDKIM Filter v2.10.3 conuserg-08.nifty.com 27J6uUCP012757 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.com; s=dec2015msa; t=1660892196; bh=JKFoW0ol9y1fVwS1pROFaB3YGi1tPsHIYaMpxHnO61I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rRgAouaU74jlZAlDDqUnecfj0Os4Xdj9KELOPEeI0L/VecFDP5u9YfqKr8WDwsJNS ecK+LtvdMMDa2HP3jUK1cjSh/iMTvd1QOPFSqI6Ac9uDfPgBA48DYZ2sbU8dRDzoA9 U/hB/0e/O6vmgSboc4pe3fvFHUIvWeY0QXHr4Jow95Gts+5AEof3igIxi4Vy+gT156 hfBxf03yIsqX/30neGRcAccDwgxDwto7Npgzn8jJE6sYG96kdB8fCfP5Scuk+gNIDw pmEVs3rCzsFIZz6AcdAcIx88cZKVMmzs7WZKgzHHJQQc9vFTQHgSSd7l8k8NH9x1Mf y4qM3lv0O6F6Q== X-Nifty-SrcIP: [133.106.49.178] From: Masahiro Yamada To: linux-kbuild@vger.kernel.org Cc: Nick Desaulniers , Alexandre Belloni , Randy Dunlap , Richard Purdie , Masahiro Yamada , Michal Marek , linux-kernel@vger.kernel.org Subject: [RFC PATCH 3/3] kbuild: use bash as the default shell for Make and Kconfig Date: Fri, 19 Aug 2022 15:56:03 +0900 Message-Id: <20220819065604.295572-4-masahiroy@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220819065604.295572-1-masahiroy@kernel.org> References: <20220819065604.295572-1-masahiroy@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org GNU Make uses /bin/sh by default for running recipe lines and $(shell ), and so does Kconfig for $(shell,...). We are sometimes hit by subtle portability issues like [1] because the code was only tested on systems where /bin/sh is a symlink to bash. One way is to continue whack-a-mole game as somebody will eventually notice that the code is not working on their build environments. Another is a big hammer approach; force bash in the build system so that it works in the same way for everyone. In fact, there exists such a guideline that permits only Bash as the shell scripting language. [2] The kbuild test robot also specifies SHELL=/bin/bash to eliminate the shell portability issue. This commit changes the shell for Make and Kconfig to bash. As the GNU Make manual [3] says, the 'SHELL' is never set from the environment. You cannot do 'export SHELL:=/bin/bash' to propagate it to sub-makes. Instead, use MAKEOVERRIDES to make SHELL:=/bin/bash globally effective. [1]: https://lore.kernel.org/all/20220804190320.262510-1-alexandre.belloni@bootlin.com/ [2]: https://google.github.io/styleguide/shellguide.html [3]: https://www.gnu.org/software/make/manual/html_node/Choosing-the-Shell.html Signed-off-by: Masahiro Yamada --- Makefile | 7 +++++++ scripts/Kconfig.include | 3 +++ 2 files changed, 10 insertions(+) diff --git a/Makefile b/Makefile index f09673b6c11d..cc01987560b3 100644 --- a/Makefile +++ b/Makefile @@ -36,6 +36,13 @@ __all: ifneq ($(sub_make_done),1) +# Use /bin/bash for repices and $(shell ) invocations. +SHELL := /bin/bash + +# Unlike the other variables, the 'SHELL' is never set from the environment. +# Use MAKEOVERRIDES to pass 'SHELL' down to sub-makes. +MAKEOVERRIDES += SHELL=$(SHELL) + # Do not use make's built-in rules and variables # (this increases performance and avoids hard-to-debug behaviour) MAKEFLAGS += -rR diff --git a/scripts/Kconfig.include b/scripts/Kconfig.include index a0ccceb22cf8..d37b864ba1f2 100644 --- a/scripts/Kconfig.include +++ b/scripts/Kconfig.include @@ -1,6 +1,9 @@ # SPDX-License-Identifier: GPL-2.0-only # Kconfig helper macros +# Use /bin/bash to evaluate $(shell,) functions +SHELL := /bin/bash + # Convenient variables comma := , quote := "