From patchwork Sun Dec 23 18:56:10 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Williams X-Patchwork-Id: 10741783 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 0BC251399 for ; Sun, 23 Dec 2018 19:08:50 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E287F26247 for ; Sun, 23 Dec 2018 19:08:49 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D0D4926255; Sun, 23 Dec 2018 19:08:49 +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 538A726247 for ; Sun, 23 Dec 2018 19:08:48 +0000 (UTC) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 9360F21A07A80; Sun, 23 Dec 2018 11:08:47 -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.43; helo=mga05.intel.com; envelope-from=dan.j.williams@intel.com; receiver=linux-nvdimm@lists.01.org Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) (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 ABF9621B02822 for ; Sun, 23 Dec 2018 11:08:46 -0800 (PST) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga105.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 23 Dec 2018 11:08:46 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.56,388,1539673200"; d="scan'208";a="104245439" Received: from dwillia2-desk3.jf.intel.com (HELO dwillia2-desk3.amr.corp.intel.com) ([10.54.39.16]) by orsmga008.jf.intel.com with ESMTP; 23 Dec 2018 11:08:46 -0800 Subject: [ndctl PATCH] ndctl/monitor: Drop 'struct ndctl_ctx *' casts From: Dan Williams To: linux-nvdimm@lists.01.org Date: Sun, 23 Dec 2018 10:56:10 -0800 Message-ID: <154559137045.4076221.14167624874916967075.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 Functions that take a 'struct ndctl_ctx *' are fine to take the 'void *' argument representing a library context that is passed to all commands. However, the shared logging macros need a defined type to dereference and retrieve the log_ctx. Introduce _gen_ctx to generically represent 'struct ndctl_ctx *' and 'struct daxctl_ctx *' to the log helpers, and drop the remaining casts in ndctl/monitor.c. Add a couple BUILD_BUG_ON() statements to backup the log_ctx expectations. Cc: QI Fuli Signed-off-by: Dan Williams --- daxctl/lib/libdaxctl.c | 4 ++++ ndctl/lib/libndctl.c | 4 ++++ ndctl/monitor.c | 22 +++++++++++----------- util/log.h | 16 ++++++++++++---- 4 files changed, 31 insertions(+), 15 deletions(-) diff --git a/daxctl/lib/libdaxctl.c b/daxctl/lib/libdaxctl.c index 22f4210a7ea0..7b233b8d43f1 100644 --- a/daxctl/lib/libdaxctl.c +++ b/daxctl/lib/libdaxctl.c @@ -23,6 +23,7 @@ #include #include +#include #include #include #include "libdaxctl-private.h" @@ -86,6 +87,9 @@ DAXCTL_EXPORT int daxctl_new(struct daxctl_ctx **ctx) { struct daxctl_ctx *c; + /* Sanity check for the expectations of the shared log functions */ + BUILD_BUG_ON(offsetof(struct daxctl_ctx, ctx) != 0); + c = calloc(1, sizeof(struct daxctl_ctx)); if (!c) return -ENOMEM; diff --git a/ndctl/lib/libndctl.c b/ndctl/lib/libndctl.c index 0c3a35e5bcc9..77b29454a253 100644 --- a/ndctl/lib/libndctl.c +++ b/ndctl/lib/libndctl.c @@ -31,6 +31,7 @@ #include #include +#include #include #include #include @@ -287,6 +288,9 @@ NDCTL_EXPORT int ndctl_new(struct ndctl_ctx **ctx) const char *env; int rc = 0; + /* Sanity check for the expectations of the shared log functions */ + BUILD_BUG_ON(offsetof(struct ndctl_ctx, ctx) != 0); + udev = udev_new(); if (check_udev(udev) != 0) return -ENXIO; diff --git a/ndctl/monitor.c b/ndctl/monitor.c index b92a133b7b94..73d75286e90b 100644 --- a/ndctl/monitor.c +++ b/ndctl/monitor.c @@ -623,14 +623,14 @@ int cmd_monitor(int argc, const char **argv, void *ctx) usage_with_options(u, options); /* default to log_standard */ - ndctl_set_log_fn((struct ndctl_ctx *)ctx, log_standard); + ndctl_set_log_fn(ctx, log_standard); if (monitor.verbose) - ndctl_set_log_priority((struct ndctl_ctx *)ctx, LOG_DEBUG); + ndctl_set_log_priority(ctx, LOG_DEBUG); else - ndctl_set_log_priority((struct ndctl_ctx *)ctx, LOG_INFO); + ndctl_set_log_priority(ctx, LOG_INFO); - rc = read_config_file((struct ndctl_ctx *)ctx, &monitor, ¶m); + rc = read_config_file(ctx, &monitor, ¶m); if (rc) goto out; @@ -638,7 +638,7 @@ int cmd_monitor(int argc, const char **argv, void *ctx) if (strncmp(monitor.log, "./", 2) != 0) fix_filename(prefix, (const char **)&monitor.log); if (strncmp(monitor.log, "./syslog", 8) == 0) - ndctl_set_log_fn((struct ndctl_ctx *)ctx, log_syslog); + ndctl_set_log_fn(ctx, log_syslog); else if (strncmp(monitor.log, "./standard", 10) == 0) ; /*default, already set */ else { @@ -649,21 +649,21 @@ int cmd_monitor(int argc, const char **argv, void *ctx) goto out; } fclose(f); - ndctl_set_log_fn((struct ndctl_ctx *)ctx, log_file); + ndctl_set_log_fn(ctx, log_file); } } if (monitor.daemon) { if (!monitor.log || strncmp(monitor.log, "./", 2) == 0) - ndctl_set_log_fn((struct ndctl_ctx *)ctx, log_syslog); + ndctl_set_log_fn(ctx, log_syslog); if (daemon(0, 0) != 0) { - err((struct ndctl_ctx *)ctx, "daemon start failed\n"); + err(ctx, "daemon start failed\n"); goto out; } - info((struct ndctl_ctx *)ctx, "ndctl monitor daemon started\n"); + info(ctx, "ndctl monitor daemon started\n"); } - if (parse_monitor_event(&monitor, (struct ndctl_ctx *)ctx)) + if (parse_monitor_event(&monitor, ctx)) goto out; fctx.filter_bus = filter_bus; @@ -681,7 +681,7 @@ int cmd_monitor(int argc, const char **argv, void *ctx) goto out; if (!mfa.num_dimm) { - dbg((struct ndctl_ctx *)ctx, "no dimms to monitor\n"); + dbg(ctx, "no dimms to monitor\n"); if (!monitor.daemon) rc = -ENXIO; goto out; diff --git a/util/log.h b/util/log.h index 495e0d33c7f5..881971522690 100644 --- a/util/log.h +++ b/util/log.h @@ -56,10 +56,18 @@ do { \ # define log_notice(ctx, arg...) log_null(ctx, ## arg) #endif -#define dbg(x, arg...) log_dbg(&(x)->ctx, ## arg) -#define info(x, arg...) log_info(&(x)->ctx, ## arg) -#define err(x, arg...) log_err(&(x)->ctx, ## arg) -#define notice(x, arg...) log_notice(&(x)->ctx, ## arg) +struct _gen_ctx { + /* + * Requires ndctl_ctx and daxctl_ctx keep their log_ctx as their + * first element + */ + struct log_ctx ctx; +}; + +#define dbg(x, arg...) log_dbg(&((struct _gen_ctx *)(x))->ctx, ## arg) +#define info(x, arg...) log_info(&((struct _gen_ctx *)(x))->ctx, ## arg) +#define err(x, arg...) log_err(&((struct _gen_ctx *)(x))->ctx, ## arg) +#define notice(x, arg...) log_notice(&((struct _gen_ctx *)(x))->ctx, ## arg) #ifndef HAVE_SECURE_GETENV # ifdef HAVE___SECURE_GETENV