From patchwork Thu Oct 20 03:42:33 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Williams X-Patchwork-Id: 684424 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3szwHJ1SxWz9sCZ for ; Thu, 20 Oct 2016 15:06:52 +1100 (AEDT) Received: from localhost ([::1]:52288 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bx4cw-0005r7-VI for incoming@patchwork.ozlabs.org; Thu, 20 Oct 2016 00:06:46 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54158) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bx4IY-0005XB-6w for qemu-devel@nongnu.org; Wed, 19 Oct 2016 23:45:43 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bx4IT-0007Zm-B8 for qemu-devel@nongnu.org; Wed, 19 Oct 2016 23:45:42 -0400 Received: from mga09.intel.com ([134.134.136.24]:8191) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1bx4IS-0007Yl-UJ for qemu-devel@nongnu.org; Wed, 19 Oct 2016 23:45:37 -0400 Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga102.jf.intel.com with ESMTP; 19 Oct 2016 20:45:35 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.31,517,1473145200"; d="scan'208";a="181715375" Received: from dwillia2-desk3.jf.intel.com (HELO dwillia2-desk3.amr.corp.intel.com) ([10.54.39.14]) by fmsmga004.fm.intel.com with ESMTP; 19 Oct 2016 20:45:35 -0700 From: Dan Williams To: linux-nvdimm@lists.01.org Date: Wed, 19 Oct 2016 20:42:33 -0700 Message-ID: <147693495320.36426.9347832920609902979.stgit@dwillia2-desk3.amr.corp.intel.com> User-Agent: StGit/0.17.1-9-g687f MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 134.134.136.24 Subject: [Qemu-devel] [ndctl PATCH] ndctl: revert glob support for dimm commands X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" As pointed out by Eric, files in the local directory with names of the form 'nmemX' can break the recently added glob support by filtering otherwise valid dimm names in a glob like 'nmem[X-Y]'. For robustness, the glob argument would always need to be shell escaped, but at that point it is less characters to simply rely on shell expansion of the form nmem{X..Y}, if available. Reported-by: Eric Blake Signed-off-by: Dan Williams --- ndctl/builtin-dimm.c | 70 +++++++++----------------------------------------- 1 file changed, 13 insertions(+), 57 deletions(-) diff --git a/ndctl/builtin-dimm.c b/ndctl/builtin-dimm.c index 304bd83a33da..19036fd38b46 100644 --- a/ndctl/builtin-dimm.c +++ b/ndctl/builtin-dimm.c @@ -10,7 +10,6 @@ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for * more details. */ -#include #include #include #include @@ -774,72 +773,35 @@ static int dimm_action(int argc, const char **argv, struct ndctl_ctx *ctx, int (*action)(struct ndctl_dimm *dimm, struct action_context *actx), const struct option *options, const char *usage) { - int rc = 0, count, err = 0, glob_cnt = 0; struct action_context actx = { NULL, NULL }; + int i, rc = 0, count, err = 0; const char * const u[] = { usage, NULL }; - char *all[] = { "all " }; - glob_t glob_buf; - size_t i; + unsigned long id; argc = parse_options(argc, argv, options, u, 0); if (argc == 0) usage_with_options(u, options); - for (i = 0; i < (size_t) argc; i++) { - char *path; - + for (i = 0; i < argc; i++) { if (strcmp(argv[i], "all") == 0) { argv[0] = "all"; argc = 1; - glob_cnt = 0; break; } - rc = asprintf(&path, "/sys/bus/nd/devices/%s", argv[i]); - if (rc < 0) { - fprintf(stderr, "failed to parse %s\n", argv[i]); - usage_with_options(u, options); - } - rc = glob(path, glob_cnt++ ? GLOB_APPEND : 0, NULL, &glob_buf); - switch (rc) { - case GLOB_NOSPACE: - case GLOB_ABORTED: - fprintf(stderr, "failed to parse %s\n", argv[i]); - usage_with_options(u, options); - break; - case GLOB_NOMATCH: - case 0: - break; + if (sscanf(argv[i], "nmem%lu", &id) != 1) { + fprintf(stderr, "'%s' is not a valid dimm name\n", + argv[i]); + err++; } - free(path); - } - - if (!glob_cnt) - glob_buf.gl_pathc = 0; - count = 0; - for (i = 0; i < glob_buf.gl_pathc; i++) { - char *dimm_name = strrchr(glob_buf.gl_pathv[i], '/'); - unsigned long id; - - if (!dimm_name++) - continue; - if (sscanf(dimm_name, "nmem%lu", &id) == 1) - count++; } - if (strcmp(argv[0], "all") == 0) { - glob_buf.gl_pathc = ARRAY_SIZE(all); - glob_buf.gl_pathv = all; - } else if (!count) { - fprintf(stderr, "Error: ' "); - for (i = 0; i < (size_t) argc; i++) - fprintf(stderr, "%s ", argv[i]); - fprintf(stderr, "' does not specify any present devices\n"); - fprintf(stderr, "See 'ndctl list -D'\n"); + if (err == argc) { usage_with_options(u, options); + return -EINVAL; } if (param.json) { @@ -864,23 +826,20 @@ static int dimm_action(int argc, const char **argv, struct ndctl_ctx *ctx, ndctl_set_log_priority(ctx, LOG_DEBUG); rc = 0; + err = 0; count = 0; - for (i = 0; i < glob_buf.gl_pathc; i++) { - char *dimm_name = strrchr(glob_buf.gl_pathv[i], '/'); + for (i = 0; i < argc; i++) { struct ndctl_dimm *dimm; struct ndctl_bus *bus; - unsigned long id; - if (!dimm_name++) - continue; - if (sscanf(dimm_name, "nmem%lu", &id) != 1) + if (sscanf(argv[i], "nmem%lu", &id) != 1) continue; ndctl_bus_foreach(ctx, bus) { if (!util_bus_filter(bus, param.bus)) continue; ndctl_dimm_foreach(bus, dimm) { - if (!util_dimm_filter(dimm, dimm_name)) + if (!util_dimm_filter(dimm, argv[i])) continue; rc = action(dimm, &actx); if (rc == 0) @@ -902,9 +861,6 @@ static int dimm_action(int argc, const char **argv, struct ndctl_ctx *ctx, fclose(actx.f_out); out: - if (glob_cnt) - globfree(&glob_buf); - /* * count if some actions succeeded, 0 if none were attempted, * negative error code otherwise.