From patchwork Mon Jan 7 08:38:33 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: QI Fuli X-Patchwork-Id: 10750135 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id C196C14DE for ; Mon, 7 Jan 2019 08:38:56 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AFD162836F for ; Mon, 7 Jan 2019 08:38:56 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A29CF283E7; Mon, 7 Jan 2019 08:38:56 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from ml01.01.org (ml01.01.org [198.145.21.10]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 344FA2836F for ; Mon, 7 Jan 2019 08:38:56 +0000 (UTC) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 1D1F6211B112A; Mon, 7 Jan 2019 00:38:56 -0800 (PST) X-Original-To: linux-nvdimm@lists.01.org Delivered-To: linux-nvdimm@lists.01.org Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=211.128.242.41; helo=mgwym02.jp.fujitsu.com; envelope-from=qi.fuli@jp.fujitsu.com; receiver=linux-nvdimm@lists.01.org Received: from mgwym02.jp.fujitsu.com (mgwym02.jp.fujitsu.com [211.128.242.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id A4928211A757B for ; Mon, 7 Jan 2019 00:38:53 -0800 (PST) Received: from yt-mxauth.gw.nic.fujitsu.com (unknown [192.168.229.68]) by mgwym02.jp.fujitsu.com with smtp id 621c_a770_241f2fdf_cc3b_44d6_a896_00720d757c86; Mon, 07 Jan 2019 17:38:49 +0900 Received: from m3051.s.css.fujitsu.com (m3051.s.css.fujitsu.com [10.134.21.209]) by yt-mxauth.gw.nic.fujitsu.com (Postfix) with ESMTP id A00EEAC00F8 for ; Mon, 7 Jan 2019 17:38:48 +0900 (JST) Received: from qi-nvdimm.8.8.8.8 (unknown [10.124.196.110]) by m3051.s.css.fujitsu.com (Postfix) with ESMTP id 8BF2D9E; Mon, 7 Jan 2019 17:38:48 +0900 (JST) From: QI Fuli To: linux-nvdimm@lists.01.org Subject: [ndctl PATCH v2 2/2] ndctl, monitor: refactor read_config_file Date: Mon, 7 Jan 2019 17:38:33 +0900 Message-Id: <20190107083833.25941-3-qi.fuli@jp.fujitsu.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190107083833.25941-1-qi.fuli@jp.fujitsu.com> References: <20190107083833.25941-1-qi.fuli@jp.fujitsu.com> MIME-Version: 1.0 X-TM-AS-MML: disable X-BeenThere: linux-nvdimm@lists.01.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Linux-nvdimm developer list." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: linux-nvdimm-bounces@lists.01.org Sender: "Linux-nvdimm" X-Virus-Scanned: ClamAV using ClamSMTP This patch is used for refactoring read_config_file by replacing the open coded implementation with ccan/ciniparser library. Signed-off-by: QI Fuli --- ndctl/Makefile.am | 1 + ndctl/monitor.c | 115 ++++++++++++--------------------------------- ndctl/monitor.conf | 2 + 3 files changed, 32 insertions(+), 86 deletions(-) diff --git a/ndctl/Makefile.am b/ndctl/Makefile.am index ff01e06..2f00b65 100644 --- a/ndctl/Makefile.am +++ b/ndctl/Makefile.am @@ -28,6 +28,7 @@ ndctl_LDADD =\ lib/libndctl.la \ ../daxctl/lib/libdaxctl.la \ ../libutil.a \ + ../libccan.a \ $(UUID_LIBS) \ $(KMOD_LIBS) \ $(JSON_LIBS) diff --git a/ndctl/monitor.c b/ndctl/monitor.c index 233f2bb..8499fd4 100644 --- a/ndctl/monitor.c +++ b/ndctl/monitor.c @@ -13,6 +13,7 @@ #include #include #include +#include #define BUF_SIZE 2048 /* reuse the core log helpers for the monitor logger */ @@ -443,12 +444,12 @@ out: return rc; } -static void parse_config(const char **arg, char *key, char *val, char *ident) +static void parse_config(const char **arg, char *val) { struct strbuf value = STRBUF_INIT; size_t arg_len = *arg ? strlen(*arg) : 0; - if (!ident || !key || (strcmp(ident, key) != 0)) + if (!val) return; if (arg_len) { @@ -459,92 +460,31 @@ static void parse_config(const char **arg, char *key, char *val, char *ident) *arg = strbuf_detach(&value, NULL); } -static int read_config_file(struct ndctl_ctx *ctx, struct monitor *_monitor, - struct util_filter_params *_param) +static int read_config_file(const char *config_file) { - FILE *f; - size_t len = 0; - int line = 0, rc = 0; - char *buf = NULL, *seek, *value, *config_file; - - if (_monitor->config_file) - config_file = strdup(_monitor->config_file); - else - config_file = strdup(NDCTL_CONF_FILE); - if (!config_file) { - fail("strdup default config file failed\n"); - rc = -ENOMEM; - goto out; - } - - buf = malloc(BUF_SIZE); - if (!buf) { - fail("malloc read config-file buf error\n"); - rc = -ENOMEM; + dictionary *dic; + + dic = ciniparser_load(config_file); + if (!dic) + return -errno; + + parse_config(¶m.bus, + ciniparser_getstring(dic, "monitor:bus", NULL)); + parse_config(¶m.dimm, + ciniparser_getstring(dic, "monitor:dimm", NULL)); + parse_config(¶m.region, + ciniparser_getstring(dic, "monitor:region", NULL)); + parse_config(¶m.namespace, + ciniparser_getstring(dic, "monitor:namespace", NULL)); + parse_config(&monitor.dimm_event, + ciniparser_getstring(dic, "monitor:dimm-event", NULL)); + if (monitor.log) goto out; - } - seek = buf; - - f = fopen(config_file, "r"); - if (!f) { - err(&monitor, "config-file: %s cannot be opened\n", config_file); - rc = -errno; - goto out; - } - - while (fgets(seek, BUF_SIZE, f)) { - value = NULL; - line++; - - while (isspace(*seek)) - seek++; - - if (*seek == '#' || *seek == '\0') - continue; - - value = strchr(seek, '='); - if (!value) { - fail("config-file syntax error, skip line[%i]\n", line); - continue; - } - - value[0] = '\0'; - value++; - - while (isspace(value[0])) - value++; - - len = strlen(seek); - if (len == 0) - continue; - while (isspace(seek[len-1])) - len--; - seek[len] = '\0'; - - len = strlen(value); - if (len == 0) - continue; - while (isspace(value[len-1])) - len--; - value[len] = '\0'; - - if (len == 0) - continue; - - parse_config(&_param->bus, "bus", value, seek); - parse_config(&_param->dimm, "dimm", value, seek); - parse_config(&_param->region, "region", value, seek); - parse_config(&_param->namespace, "namespace", value, seek); - parse_config(&_monitor->dimm_event, "dimm-event", value, seek); - - if (!_monitor->log) - parse_config(&_monitor->log, "log", value, seek); - } - fclose(f); + parse_config(&monitor.log, + ciniparser_getstring(dic, "monitor:log", NULL)); out: - free(buf); - free(config_file); - return rc; + ciniparser_freedict(dic); + return 0; } int cmd_monitor(int argc, const char **argv, struct ndctl_ctx *ctx) @@ -596,7 +536,10 @@ int cmd_monitor(int argc, const char **argv, struct ndctl_ctx *ctx) else monitor.ctx.log_priority = LOG_INFO; - rc = read_config_file(ctx, &monitor, ¶m); + if (monitor.config_file) + rc = read_config_file(monitor.config_file); + else + rc = read_config_file(NDCTL_CONF_FILE); if (rc) goto out; diff --git a/ndctl/monitor.conf b/ndctl/monitor.conf index 934e2c0..edcf8e2 100644 --- a/ndctl/monitor.conf +++ b/ndctl/monitor.conf @@ -9,6 +9,8 @@ # Multiple space-separated values are allowed, but except the following # characters: : ? / \ % " ' $ & ! * { } [ ] ( ) = < > @ +[monitor] + # The objects to monitor are filtered via dimm's name by setting key "dimm". # If this value is different from the value of [--dimm=] option, # both of the values will work.