From patchwork Fri Jun 8 10:20:31 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Wilck X-Patchwork-Id: 10454119 X-Patchwork-Delegate: christophe.varoqui@free.fr Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 1FD5A60467 for ; Fri, 8 Jun 2018 10:22:13 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1A03829514 for ; Fri, 8 Jun 2018 10:22:13 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0E2F92951C; Fri, 8 Jun 2018 10:22:13 +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=-7.9 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) (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 4F60429514 for ; Fri, 8 Jun 2018 10:22:12 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.27]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 07CBE308FB98; Fri, 8 Jun 2018 10:22:11 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C8A61A113A; Fri, 8 Jun 2018 10:22:10 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 7B1AE180BA84; Fri, 8 Jun 2018 10:22:10 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w58ALkY1018015 for ; Fri, 8 Jun 2018 06:21:46 -0400 Received: by smtp.corp.redhat.com (Postfix) id 5EF7562527; Fri, 8 Jun 2018 10:21:46 +0000 (UTC) Delivered-To: dm-devel@redhat.com Received: from mx1.redhat.com (ext-mx18.extmail.prod.ext.phx2.redhat.com [10.5.110.47]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 4BAEE6252B; Fri, 8 Jun 2018 10:21:46 +0000 (UTC) Received: from smtp2.provo.novell.com (smtp2.provo.novell.com [137.65.250.81]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B0EF530820C9; Fri, 8 Jun 2018 10:21:44 +0000 (UTC) Received: from apollon.suse.de.de (prv-ext-foundry1int.gns.novell.com [137.65.251.240]) by smtp2.provo.novell.com with ESMTP (TLS encrypted); Fri, 08 Jun 2018 04:21:34 -0600 From: Martin Wilck To: Christophe Varoqui Date: Fri, 8 Jun 2018 12:20:31 +0200 Message-Id: <20180608102041.22904-19-mwilck@suse.com> In-Reply-To: <20180608102041.22904-1-mwilck@suse.com> References: <20180608102041.22904-1-mwilck@suse.com> X-Greylist: Sender passed SPF test, Sender IP whitelisted by DNSRBL, ACL 207 matched, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.47]); Fri, 08 Jun 2018 10:21:45 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.47]); Fri, 08 Jun 2018 10:21:45 +0000 (UTC) for IP:'137.65.250.81' DOMAIN:'smtp2.provo.novell.com' HELO:'smtp2.provo.novell.com' FROM:'mwilck@suse.com' RCPT:'' X-RedHat-Spam-Score: -2.301 (RCVD_IN_DNSWL_MED, SPF_PASS) 137.65.250.81 smtp2.provo.novell.com 137.65.250.81 smtp2.provo.novell.com X-Scanned-By: MIMEDefang 2.84 on 10.5.110.47 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-loop: dm-devel@redhat.com Cc: dm-devel@redhat.com, Xose Vazquez Perez , Martin Wilck Subject: [dm-devel] [PATCH 18/28] multipath, multipathd: consolidate config dumping X-BeenThere: dm-devel@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: device-mapper development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: dm-devel-bounces@redhat.com Errors-To: dm-devel-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.27 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.43]); Fri, 08 Jun 2018 10:22:11 +0000 (UTC) X-Virus-Scanned: ClamAV using ClamSMTP "multipath -t" and "multipathd show config" use very similar code. Consolidate it into a libmultipath function. Simplify it a bit in the process, and do some "const" and "static" cleanup. Signed-off-by: Martin Wilck --- libmultipath/print.c | 76 ++++++++++++++++++++++++++++++++++----- libmultipath/print.h | 7 +--- multipath/main.c | 63 ++++---------------------------- multipathd/cli_handlers.c | 52 ++------------------------- 4 files changed, 78 insertions(+), 120 deletions(-) diff --git a/libmultipath/print.c b/libmultipath/print.c index b1844c98..7d4e2ace 100644 --- a/libmultipath/print.c +++ b/libmultipath/print.c @@ -1342,7 +1342,8 @@ snprint_multipath_topology_json (char * buff, int len, const struct vectors * ve } static int -snprint_hwentry (struct config *conf, char * buff, int len, const struct hwentry * hwe) +snprint_hwentry (const struct config *conf, + char * buff, int len, const struct hwentry * hwe) { int i; int fwd = 0; @@ -1374,7 +1375,8 @@ snprint_hwentry (struct config *conf, char * buff, int len, const struct hwentry return fwd; } -int snprint_hwtable(struct config *conf, char *buff, int len, vector hwtable) +static int snprint_hwtable(const struct config *conf, + char *buff, int len, vector hwtable) { int fwd = 0; int i; @@ -1400,7 +1402,8 @@ int snprint_hwtable(struct config *conf, char *buff, int len, vector hwtable) } static int -snprint_mpentry (struct config *conf, char * buff, int len, const struct mpentry * mpe) +snprint_mpentry (const struct config *conf, char * buff, int len, + const struct mpentry * mpe) { int i; int fwd = 0; @@ -1426,7 +1429,8 @@ snprint_mpentry (struct config *conf, char * buff, int len, const struct mpentry return fwd; } -int snprint_mptable(struct config *conf, char *buff, int len, vector mptable) +static int snprint_mptable(const struct config *conf, + char *buff, int len, vector mptable) { int fwd = 0; int i; @@ -1451,8 +1455,8 @@ int snprint_mptable(struct config *conf, char *buff, int len, vector mptable) return fwd; } -int snprint_overrides(struct config *conf, char * buff, int len, - const struct hwentry *overrides) +static int snprint_overrides(const struct config *conf, char * buff, int len, + const struct hwentry *overrides) { int fwd = 0; int i; @@ -1481,7 +1485,7 @@ out: return fwd; } -int snprint_defaults(struct config *conf, char *buff, int len) +static int snprint_defaults(const struct config *conf, char *buff, int len) { int fwd = 0; int i; @@ -1624,7 +1628,7 @@ int snprint_blacklist_report(struct config *conf, char *buff, int len) return fwd; } -int snprint_blacklist(struct config *conf, char *buff, int len) +static int snprint_blacklist(const struct config *conf, char *buff, int len) { int i; struct blentry * ble; @@ -1700,7 +1704,8 @@ int snprint_blacklist(struct config *conf, char *buff, int len) return fwd; } -int snprint_blacklist_except(struct config *conf, char *buff, int len) +static int snprint_blacklist_except(const struct config *conf, + char *buff, int len) { int i; struct blentry * ele; @@ -1776,6 +1781,59 @@ int snprint_blacklist_except(struct config *conf, char *buff, int len) return fwd; } +char *snprint_config(const struct config *conf, int *len) +{ + char *reply; + /* built-in config is >20kB already */ + unsigned int maxlen = 32768; + + for (reply = NULL; maxlen <= UINT_MAX/2; maxlen *= 2) { + char *c, *tmp = reply; + + reply = REALLOC(reply, maxlen); + if (!reply) { + if (tmp) + free(tmp); + return NULL; + } + + c = reply + snprint_defaults(conf, reply, maxlen); + if ((c - reply) == maxlen) + continue; + + c += snprint_blacklist(conf, c, reply + maxlen - c); + if ((c - reply) == maxlen) + continue; + + c += snprint_blacklist_except(conf, c, reply + maxlen - c); + if ((c - reply) == maxlen) + continue; + + c += snprint_hwtable(conf, c, reply + maxlen - c, + conf->hwtable); + if ((c - reply) == maxlen) + continue; + + c += snprint_overrides(conf, c, reply + maxlen - c, + conf->overrides); + if ((c - reply) == maxlen) + continue; + + if (VECTOR_SIZE(conf->mptable) > 0) + c += snprint_mptable(conf, c, reply + maxlen - c, + conf->mptable); + + if ((c - reply) < maxlen) { + if (len) + *len = c - reply; + return reply; + } + } + + free(reply); + return NULL; +} + int snprint_status(char *buff, int len, const struct vectors *vecs) { int fwd = 0; diff --git a/libmultipath/print.h b/libmultipath/print.h index 9b5a23aa..fed80d55 100644 --- a/libmultipath/print.h +++ b/libmultipath/print.h @@ -119,18 +119,13 @@ int _snprint_multipath_topology (const struct gen_multipath *, char *, int, _snprint_multipath_topology (dm_multipath_to_gen(mpp), buf, len, v) int snprint_multipath_topology_json (char * buff, int len, const struct vectors * vecs); +char *snprint_config(const struct config *conf, int *len); int snprint_multipath_map_json (char * buff, int len, const struct multipath * mpp, int last); -int snprint_defaults (struct config *, char *, int); -int snprint_blacklist (struct config *, char *, int); -int snprint_blacklist_except (struct config *, char *, int); int snprint_blacklist_report (struct config *, char *, int); int snprint_wildcards (char *, int); int snprint_status (char *, int, const struct vectors *); int snprint_devices (struct config *, char *, int, const struct vectors *); -int snprint_hwtable (struct config *, char *, int, const vector); -int snprint_mptable (struct config *, char *, int, const vector); -int snprint_overrides (struct config *, char *, int, const struct hwentry *); int snprint_path_serial (char *, size_t, const struct path *); int snprint_host_wwnn (char *, size_t, const struct path *); int snprint_host_wwpn (char *, size_t, const struct path *); diff --git a/multipath/main.c b/multipath/main.c index 3f0a6aa7..288251c3 100644 --- a/multipath/main.c +++ b/multipath/main.c @@ -753,63 +753,14 @@ out: static int dump_config (struct config *conf) { - char * c, * tmp = NULL; - char * reply; - unsigned int maxlen = 256; - int again = 1; - - reply = MALLOC(maxlen); - - while (again) { - if (!reply) { - if (tmp) - free(tmp); - return 1; - } - c = tmp = reply; - c += snprint_defaults(conf, c, reply + maxlen - c); - again = ((c - reply) == maxlen); - if (again) { - reply = REALLOC(reply, maxlen *= 2); - continue; - } - c += snprint_blacklist(conf, c, reply + maxlen - c); - again = ((c - reply) == maxlen); - if (again) { - reply = REALLOC(reply, maxlen *= 2); - continue; - } - c += snprint_blacklist_except(conf, c, reply + maxlen - c); - again = ((c - reply) == maxlen); - if (again) { - reply = REALLOC(reply, maxlen *= 2); - continue; - } - c += snprint_hwtable(conf, c, reply + maxlen - c, conf->hwtable); - again = ((c - reply) == maxlen); - if (again) { - reply = REALLOC(reply, maxlen *= 2); - continue; - } - c += snprint_overrides(conf, c, reply + maxlen - c, - conf->overrides); - again = ((c - reply) == maxlen); - if (again) { - reply = REALLOC(reply, maxlen *= 2); - continue; - } - if (VECTOR_SIZE(conf->mptable) > 0) { - c += snprint_mptable(conf, c, reply + maxlen - c, - conf->mptable); - again = ((c - reply) == maxlen); - if (again) - reply = REALLOC(reply, maxlen *= 2); - } - } + char * reply = snprint_config(conf, NULL); - printf("%s", reply); - FREE(reply); - return 0; + if (reply != NULL) { + printf("%s", reply); + FREE(reply); + return 0; + } else + return 1; } static int diff --git a/multipathd/cli_handlers.c b/multipathd/cli_handlers.c index ba50fb8f..6f043d7f 100644 --- a/multipathd/cli_handlers.c +++ b/multipathd/cli_handlers.c @@ -248,62 +248,16 @@ show_map_json (char ** r, int * len, struct multipath * mpp, int show_config (char ** r, int * len) { - char * c; - char * reply; - unsigned int maxlen = INITIAL_REPLY_LEN; - int again = 1; struct config *conf; - int fail = 0; - - c = reply = MALLOC(maxlen); + char *reply; conf = get_multipath_config(); pthread_cleanup_push(put_multipath_config, conf); - while (again) { - if (!reply) { - fail = 1; - break; - } - c = reply; - c += snprint_defaults(conf, c, reply + maxlen - c); - again = ((c - reply) == maxlen); - REALLOC_REPLY(reply, again, maxlen); - if (again) - continue; - c += snprint_blacklist(conf, c, reply + maxlen - c); - again = ((c - reply) == maxlen); - REALLOC_REPLY(reply, again, maxlen); - if (again) - continue; - c += snprint_blacklist_except(conf, c, reply + maxlen - c); - again = ((c - reply) == maxlen); - REALLOC_REPLY(reply, again, maxlen); - if (again) - continue; - c += snprint_hwtable(conf, c, reply + maxlen - c, - conf->hwtable); - again = ((c - reply) == maxlen); - REALLOC_REPLY(reply, again, maxlen); - if (again) - continue; - c += snprint_overrides(conf, c, reply + maxlen - c, - conf->overrides); - again = ((c - reply) == maxlen); - REALLOC_REPLY(reply, again, maxlen); - if (again) - continue; - if (VECTOR_SIZE(conf->mptable) > 0) { - c += snprint_mptable(conf, c, reply + maxlen - c, - conf->mptable); - again = ((c - reply) == maxlen); - REALLOC_REPLY(reply, again, maxlen); - } - } + reply = snprint_config(conf, len); pthread_cleanup_pop(1); - if (fail) + if (reply == NULL) return 1; *r = reply; - *len = (int)(c - reply + 1); return 0; }