From patchwork Fri Dec 28 03:30:25 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Williams X-Patchwork-Id: 10744139 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 9DF6613AD for ; Fri, 28 Dec 2018 03:43:03 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8E06028C94 for ; Fri, 28 Dec 2018 03:43:03 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7CDF928C99; Fri, 28 Dec 2018 03:43:03 +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 27DE928C94 for ; Fri, 28 Dec 2018 03:43:03 +0000 (UTC) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 1BF33211A2061; Thu, 27 Dec 2018 19:43:03 -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=192.55.52.93; helo=mga11.intel.com; envelope-from=dan.j.williams@intel.com; receiver=linux-nvdimm@lists.01.org Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) (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 88DB321198CC2 for ; Thu, 27 Dec 2018 19:43:01 -0800 (PST) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 27 Dec 2018 19:43:01 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.56,407,1539673200"; d="scan'208";a="121699024" Received: from dwillia2-desk3.jf.intel.com (HELO dwillia2-desk3.amr.corp.intel.com) ([10.54.39.16]) by FMSMGA003.fm.intel.com with ESMTP; 27 Dec 2018 19:43:00 -0800 Subject: [ndctl PATCH 5/7] ndctl/monitor: Fix / cleanup log_file() From: Dan Williams To: linux-nvdimm@lists.01.org Date: Thu, 27 Dec 2018 19:30:25 -0800 Message-ID: <154596782507.164521.14912344768619698318.stgit@dwillia2-desk3.amr.corp.intel.com> In-Reply-To: <154596779833.164521.12632457592535372923.stgit@dwillia2-desk3.amr.corp.intel.com> References: <154596779833.164521.12632457592535372923.stgit@dwillia2-desk3.amr.corp.intel.com> User-Agent: StGit/0.18-2-gc94f MIME-Version: 1.0 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 The log_file() helper opens the log file for each message. Avoid the scenario where the fopen() fails and just open it at init time. This usage is also broken because if the vasprintf() fails it will try to print via log_file again via fail(), and potentially lead to infinite recursion. Cc: Qi Fuli Signed-off-by: Dan Williams --- ndctl/monitor.c | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/ndctl/monitor.c b/ndctl/monitor.c index cef70d06beae..8a16c9664e0a 100644 --- a/ndctl/monitor.c +++ b/ndctl/monitor.c @@ -20,6 +20,7 @@ static struct monitor { const char *log; const char *config_file; const char *dimm_event; + FILE *log_file; bool daemon; bool human; bool verbose; @@ -82,7 +83,7 @@ static void log_standard(struct ndctl_ctx *ctx, int priority, const char *file, static void log_file(struct ndctl_ctx *ctx, int priority, const char *file, int line, const char *fn, const char *format, va_list args) { - FILE *f; + FILE *f = monitor.log_file; char *buf; struct timespec ts; char timestamp[32]; @@ -92,16 +93,6 @@ static void log_file(struct ndctl_ctx *ctx, int priority, const char *file, return; } - f = fopen(monitor.log, "a+"); - if (!f) { - ndctl_set_log_fn(ctx, log_syslog); - err(ctx, "open logfile %s failed, forward messages to syslog\n", - monitor.log); - did_fail = 1; - notice(ctx, "%s\n", buf); - goto end; - } - if (priority != LOG_NOTICE) { clock_gettime(CLOCK_REALTIME, &ts); sprintf(timestamp, "%10ld.%09ld", ts.tv_sec, ts.tv_nsec); @@ -110,8 +101,6 @@ static void log_file(struct ndctl_ctx *ctx, int priority, const char *file, fprintf(f, "%s", buf); fflush(f); - fclose(f); -end: free(buf); return; } @@ -613,7 +602,6 @@ int cmd_monitor(int argc, const char **argv, struct ndctl_ctx *ctx) struct util_filter_ctx fctx = { 0 }; struct monitor_filter_arg mfa = { 0 }; int i, rc; - FILE *f; argc = parse_options_prefix(argc, argv, prefix, options, u, 0); for (i = 0; i < argc; i++) { @@ -642,13 +630,12 @@ int cmd_monitor(int argc, const char **argv, struct ndctl_ctx *ctx) else if (strncmp(monitor.log, "./standard", 10) == 0) ; /*default, already set */ else { - f = fopen(monitor.log, "a+"); - if (!f) { + monitor.log_file = fopen(monitor.log, "a+"); + if (!monitor.log_file) { error("open %s failed\n", monitor.log); rc = -errno; goto out; } - fclose(f); ndctl_set_log_fn(ctx, log_file); } } @@ -689,5 +676,7 @@ int cmd_monitor(int argc, const char **argv, struct ndctl_ctx *ctx) rc = monitor_event(ctx, &mfa); out: + if (monitor.log_file) + fclose(monitor.log_file); return rc; }