Received: by 10.223.176.5 with SMTP id f5csp267746wra; Thu, 8 Feb 2018 21:32:22 -0800 (PST) X-Google-Smtp-Source: AH8x226b7KDBivACkYk1wJR6Tpt7drarNvr42XtPvPncpbZvHF0zcCoGCHP63ui0ccy26EgituTj X-Received: by 10.98.78.148 with SMTP id c142mr1578528pfb.153.1518154342116; Thu, 08 Feb 2018 21:32:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518154342; cv=none; d=google.com; s=arc-20160816; b=c8i4h7TTYQE5v2ydG61nTqw1HxgqP4MQB+k3gxp1dOx4Sg1VJZDJlglaDoRb2USC+t JzfLhowQMuezmRvM+PLBTWhK23iIVD0jE5iZAKzZIOHsjGKTzl96QZxOqnS2QimwXri4 39jhQZiOjPeqIYQlhTCB4vwbSXcZAzxFuhR0gbkjNBq41I5FiQe+vkl+h1wyb0dV5z2P 7b+HNOET+RET1kSAghbDEz+92tnxcqxAjw17Fc+nPIVHtBBLReyhDnW+M+dNLm39KRbw EyV0St715AVI/cz1hZ7ZxwsoCfaQKK9zXoMevx3EyHbJ4Gf4JArRhGRDduYJK6koQ2D8 5caw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature:arc-authentication-results; bh=jwgcc99A0HFr3K6l4puxKtEmqA8GbbkjIjcV8M/uHm0=; b=IDVqT6w+dbq096Pvt7w3a6yCY/ny+jIBAsO9OHHZPo5Rzbn5k90Mid1eiew62h1qbQ FSDMqxGrzkyG+R3qYBUolx1nfarfWWGOr+pVNPXUyJ4OLgHuHdjUlc89XQOLZZ7k/oJ5 xdFuM9qatwavaZyayYRuhRPudS821gqWvIHKwOOQN1oilgMJuXEtKcx0e4zKj7ACKTBm n7fNq4XFD/L4uI0Q4Fz5H/x4rbQyU0GY7acX2DtpcYWDI9dzEZSJxkMal4YzIId2XZ2h 9KHwtsmOq60jfwRZsCpCAgjDELG2dtNYLzGzUyRPw+b1GZ7J2wpcs3WfaP6XdVEl6DVO N48g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=suB3NbFg; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j6si1185870pfg.44.2018.02.08.21.31.56; Thu, 08 Feb 2018 21:32:22 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=suB3NbFg; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1750942AbeBIFa5 (ORCPT + 99 others); Fri, 9 Feb 2018 00:30:57 -0500 Received: from mail-lf0-f66.google.com ([209.85.215.66]:43475 "EHLO mail-lf0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750778AbeBIFaz (ORCPT ); Fri, 9 Feb 2018 00:30:55 -0500 Received: by mail-lf0-f66.google.com with SMTP id a25so297483lfg.10; Thu, 08 Feb 2018 21:30:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=jwgcc99A0HFr3K6l4puxKtEmqA8GbbkjIjcV8M/uHm0=; b=suB3NbFgObjf2UU13JEjzKZ2CiwjxnK6GZLwPUya/9/6HR4O7kdxxFUQg/Q7HYaCdS JurFwA97MZ8ziYHTPComXUrQfE3dfvi73pDZvzQsOJuXMSIFEAfTotziN+0x5s4qIMuO ZFtIXas/jYXybBDfuYuVAN49Nw9O3G3OeQkW/1vyLntjBoyAUJgRa+X7D+QuToRZxH1k CiCjRNJnGTngTKcGeyz9p4x/qqPb5wHY2JsA/dD6auUSOIo7YlVglmSuyX4kv5lfXAlW Ht4Gb6i8BI679WfsPFhaQt2qGbtpzh7e6KcNdJGcHLNrxUgVvIEqPZnnV04BvEUWcgnf ycUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=jwgcc99A0HFr3K6l4puxKtEmqA8GbbkjIjcV8M/uHm0=; b=QVE2HCcRccqcrprjofX55G6QIxXS7Ngv8HyERbBqmUzPkcZduEx5P4xa6Ks/i7T5M9 o4qHTgdksJoCEp1TA2a1NChGAXz2Lz7Nqn9s7liMgHkJ9HVY9Ig4iZ4r9G4BE0lntOrR EPIuFZCDTKnfWmCziujRCRc9DkNgmYxEkdEqKQgLQRorgZK3IdRIklgFm7S/yReC2Q1o GeLjkhM6gqEJjBYjMvR4ge8+1Ho96Fc4kd+UxrLovV0ws3y1qk6Nyv+yKjZbZRZtm6x7 52TgyAGy1F78EB98xfsXwpb4806eEOQq5OKHkcA/2BMmPFLvzE0ZoJc0cqYi7PUx/OWb PhBA== X-Gm-Message-State: APf1xPBnzP0ZIxcICBzUsO7DH527lGJmBr+TrUxW/Tso3ro3yPOEIzjs O5vWg/3iXRr41TwUhU9yycw= X-Received: by 10.46.101.26 with SMTP id z26mr915782ljb.37.1518154253459; Thu, 08 Feb 2018 21:30:53 -0800 (PST) Received: from huvuddator (ua-213-113-106-221.cust.bredbandsbolaget.se. [213.113.106.221]) by smtp.gmail.com with ESMTPSA id t23sm273768ljd.67.2018.02.08.21.30.51 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 08 Feb 2018 21:30:52 -0800 (PST) Date: Fri, 9 Feb 2018 06:30:38 +0100 From: Ulf Magnusson To: Masahiro Yamada Cc: linux-kbuild@vger.kernel.org, Linus Torvalds , Greg Kroah-Hartman , Andrew Morton , Kees Cook , Nicolas Pitre , "Luis R . Rodriguez" , Randy Dunlap , Sam Ravnborg , Michal Marek , Martin Schwidefsky , Pavel Machek , linux-s390@vger.kernel.org, Jiri Kosina , linux-kernel@vger.kernel.org Subject: Re: [RFC PATCH 4/7] kconfig: support new special property shell= Message-ID: <20180209053038.pscoijvowmyudyzf@huvuddator> References: <1518106752-29228-1-git-send-email-yamada.masahiro@socionext.com> <1518106752-29228-5-git-send-email-yamada.masahiro@socionext.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <1518106752-29228-5-git-send-email-yamada.masahiro@socionext.com> User-Agent: NeoMutt/20170609 (1.8.3) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Feb 09, 2018 at 01:19:09AM +0900, Masahiro Yamada wrote: > This works with bool, int, hex, string types. > > For bool, the symbol is set to 'y' or 'n' depending on the exit value > of the command. > > For int, hex, string, the symbol is set to the value to the stdout > of the command. (only the first line of the stdout) > > The following shows how to write this and how it works. > > --------------------(example Kconfig)------------------ > config srctree > string > option env="srctree" > > config CC > string > option env="CC" > > config CC_HAS_STACKPROTECTOR > bool > option shell="$CC -Werror -fstack-protector -c -x c /dev/null" > > config CC_HAS_STACKPROTECTOR_STRONG > bool > option shell="$CC -Werror -fstack-protector-strong -c -x c /dev/null" > > config CC_VERSION > int > option shell="$srctree/scripts/gcc-version.sh $CC | sed 's/^0*//'" > help > gcc-version.sh returns 4 digits number. Unfortunately, the preceding > zero would cause 'number is invalid'. Cut it off. > > config CC_IS_CLANG > bool > option shell="$CC --version | grep -q clang" > > config CC_IS_GCC > bool > option shell="$CC --version | grep -q gcc" > ----------------------------------------------------- > > $ make alldefconfig > scripts/kconfig/conf --alldefconfig Kconfig > # > # configuration written to .config > # > $ cat .config > # > # Automatically generated file; DO NOT EDIT. > # Linux Kernel Configuration > # > CONFIG_CC_HAS_STACKPROTECTOR=y > CONFIG_CC_HAS_STACKPROTECTOR_STRONG=y > CONFIG_CC_VERSION=504 > # CONFIG_CC_IS_CLANG is not set > CONFIG_CC_IS_GCC=y > > Suggested-by: Linus Torvalds > Signed-off-by: Masahiro Yamada I know this is just an RFC/incomplete, but in case it's helpful: > --- > > scripts/kconfig/expr.h | 1 + > scripts/kconfig/kconf_id.c | 1 + > scripts/kconfig/lkc.h | 1 + > scripts/kconfig/menu.c | 3 ++ > scripts/kconfig/symbol.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++ > 5 files changed, 80 insertions(+) > > diff --git a/scripts/kconfig/expr.h b/scripts/kconfig/expr.h > index c16e82e..83029f92 100644 > --- a/scripts/kconfig/expr.h > +++ b/scripts/kconfig/expr.h > @@ -183,6 +183,7 @@ enum prop_type { > P_IMPLY, /* imply BAR */ > P_RANGE, /* range 7..100 (for a symbol) */ > P_ENV, /* value from environment variable */ > + P_SHELL, /* shell command */ > P_SYMBOL, /* where a symbol is defined */ > }; > > diff --git a/scripts/kconfig/kconf_id.c b/scripts/kconfig/kconf_id.c > index 3ea9c5f..0db9d1c 100644 > --- a/scripts/kconfig/kconf_id.c > +++ b/scripts/kconfig/kconf_id.c > @@ -34,6 +34,7 @@ static struct kconf_id kconf_id_array[] = { > { "defconfig_list", T_OPT_DEFCONFIG_LIST, TF_OPTION }, > { "env", T_OPT_ENV, TF_OPTION }, > { "allnoconfig_y", T_OPT_ALLNOCONFIG_Y, TF_OPTION }, > + { "shell", T_OPT_SHELL, TF_OPTION }, > }; > > #define KCONF_ID_ARRAY_SIZE (sizeof(kconf_id_array)/sizeof(struct kconf_id)) > diff --git a/scripts/kconfig/lkc.h b/scripts/kconfig/lkc.h > index 4e23feb..8d05042 100644 > --- a/scripts/kconfig/lkc.h > +++ b/scripts/kconfig/lkc.h > @@ -60,6 +60,7 @@ enum conf_def_mode { > #define T_OPT_DEFCONFIG_LIST 2 > #define T_OPT_ENV 3 > #define T_OPT_ALLNOCONFIG_Y 4 > +#define T_OPT_SHELL 5 > > struct kconf_id { > const char *name; > diff --git a/scripts/kconfig/menu.c b/scripts/kconfig/menu.c > index 9922285..6254dfb 100644 > --- a/scripts/kconfig/menu.c > +++ b/scripts/kconfig/menu.c > @@ -216,6 +216,9 @@ void menu_add_option(int token, char *arg) > case T_OPT_ENV: > prop_add_env(arg); > break; > + case T_OPT_SHELL: > + prop_add_shell(arg); > + break; > case T_OPT_ALLNOCONFIG_Y: > current_entry->sym->flags |= SYMBOL_ALLNOCONFIG_Y; > break; > diff --git a/scripts/kconfig/symbol.c b/scripts/kconfig/symbol.c > index 893eae6..02ac4f4 100644 > --- a/scripts/kconfig/symbol.c > +++ b/scripts/kconfig/symbol.c > @@ -4,6 +4,7 @@ > */ > > #include > +#include > #include > #include > #include > @@ -1370,6 +1371,8 @@ const char *prop_get_type_name(enum prop_type type) > return "prompt"; > case P_ENV: > return "env"; > + case P_SHELL: > + return "shell"; > case P_COMMENT: > return "comment"; > case P_MENU: > @@ -1420,3 +1423,74 @@ static void prop_add_env(const char *env) > else > menu_warn(current_entry, "environment variable %s undefined", env); > } > + > +static void prop_add_shell(const char *cmd) > +{ > + struct symbol *sym, *sym2; > + struct property *prop; > + char *expanded_cmd; > + FILE *p; > + char stdout[256]; Maybe this could be called stdout_buf to avoid confusing it with the stdio streams. Those are macros too, though glibc just does #define stdout stdout > + int ret, len; > + > + sym = current_entry->sym; > + for_all_properties(sym, prop, P_SHELL) { > + sym2 = prop_get_symbol(prop); > + if (strcmp(sym2->name, cmd)) > + menu_warn(current_entry, "redefining shell command symbol from %s", > + sym2->name); > + return; > + } > + > + prop = prop_alloc(P_SHELL, sym); > + prop->expr = expr_alloc_symbol(sym_lookup(cmd, SYMBOL_CONST)); > + > + expanded_cmd = sym_expand_string_value(cmd); > + > + /* surround the command with ( ) in case it is piped commands */ > + len = strlen(expanded_cmd); > + expanded_cmd = xrealloc(expanded_cmd, len + 3); > + memmove(expanded_cmd + 1, expanded_cmd, len); > + expanded_cmd[0] = '('; > + expanded_cmd[len + 1] = ')'; > + expanded_cmd[len + 2] = 0; > + > + switch (sym->type) { > + case S_BOOLEAN: > + /* suppress both stdout and stderr. */ > + len = strlen(expanded_cmd) + strlen(" >/dev/null 2>&1") + 1; > + expanded_cmd = realloc(expanded_cmd, len); Could use the new xrealloc(). > + strcat(expanded_cmd, " >/dev/null 2>&1"); > + > + ret = system(expanded_cmd); > + sym_add_default(sym, ret == 0 ? "y" : "n"); > + break; > + case S_INT: > + case S_HEX: > + case S_STRING: > + /* suppress only stderr. we want to get stdout. */ > + len = strlen(expanded_cmd) + strlen(" 2>/dev/null") + 1; > + expanded_cmd = realloc(expanded_cmd, len); Could use the new xrealloc(). > + strcat(expanded_cmd, " 2>/dev/null"); > + > + p = popen(expanded_cmd, "r"); Should be pclose()d. > + if (!p) Could warn. Maybe it'd be helpful to warn if pclose() != 0 as well (non-zero exit status or obscure error). > + goto free; > + if (fgets(stdout, sizeof(stdout), p)) { > + stdout[sizeof(stdout) - 1] = 0; fgets() already guarantees null termination if any characters are read. strncpy() is that evil one... > + len = strlen(stdout); > + if (stdout[len - 1] == '\n') > + stdout[len - 1] = 0; > + } else { > + stdout[0] = 0; > + } > + sym_add_default(sym, stdout); > + break; > + default: > + menu_warn(current_entry, "unsupported type for shell command\n"); > + break; > + } > + > +free: > + free(expanded_cmd); > +} > -- > 2.7.4 > I think the general behavior makes sense for user-assignable 'option shell' symbols too (though I don't know if they'd ever get used in practice): - The output of the shell command turns into a regular default on user-assignable symbols. User values can override that default. - For savedefconfig, user-assignable symbols get written out only if they have been changed from the default given by the shell command. They will be recalculated when the defconfig is used if they weren't changed. Maybe there's some too-obscure-to-worry about cases there, but it seems to work out pretty well. Cheers, Ulf