From patchwork Wed Apr 27 11:10:52 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hannes Reinecke X-Patchwork-Id: 8955271 Return-Path: X-Original-To: patchwork-dm-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 61ABF9F1C1 for ; Wed, 27 Apr 2016 11:15:14 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 82E95201E4 for ; Wed, 27 Apr 2016 11:15:13 +0000 (UTC) Received: from mx5-phx2.redhat.com (mx5-phx2.redhat.com [209.132.183.37]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 71F50200F0 for ; Wed, 27 Apr 2016 11:15:12 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by mx5-phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u3RBBwiT012455; Wed, 27 Apr 2016 07:11:58 -0400 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id u3RBBKpl012402 for ; Wed, 27 Apr 2016 07:11:20 -0400 Received: from mx1.redhat.com (ext-mx04.extmail.prod.ext.phx2.redhat.com [10.5.110.28]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u3RBBKbD031498 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Wed, 27 Apr 2016 07:11:20 -0400 Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) (using TLSv1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 3BEBE80B56; Wed, 27 Apr 2016 11:11:19 +0000 (UTC) X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay1.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 13AA3ADC5; Wed, 27 Apr 2016 11:11:10 +0000 (UTC) From: Hannes Reinecke To: Christophe Varoqui Date: Wed, 27 Apr 2016 13:10:52 +0200 Message-Id: <1461755458-29225-52-git-send-email-hare@suse.de> In-Reply-To: <1461755458-29225-1-git-send-email-hare@suse.de> References: <1461755458-29225-1-git-send-email-hare@suse.de> X-RedHat-Spam-Score: -0.607 (BAYES_50, DCC_REPUT_00_12, RCVD_IN_DNSWL_NONE, RP_MATCHES_RCVD, SPF_PASS) 195.135.220.15 mx2.suse.de 195.135.220.15 mx2.suse.de X-Scanned-By: MIMEDefang 2.68 on 10.5.11.27 X-Scanned-By: MIMEDefang 2.75 on 10.5.110.28 X-loop: dm-devel@redhat.com Cc: dm-devel@redhat.com, Mike Snitzer Subject: [dm-devel] [PATCH 51/57] multipathd: Provide standard error description on cli failure 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-Spam-Status: No, score=-5.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP When the cli command fails we should be returning the error in addition to the standard help text. Signed-off-by: Hannes Reinecke --- multipathd/cli.c | 53 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 16 deletions(-) diff --git a/multipathd/cli.c b/multipathd/cli.c index 6a5c6db..ab1365b 100644 --- a/multipathd/cli.c +++ b/multipathd/cli.c @@ -1,6 +1,7 @@ /* * Copyright (c) 2005 Christophe Varoqui */ +#include #include #include #include @@ -224,10 +225,14 @@ find_key (const char * str) return foundkw; } -#define E_SYNTAX 1 -#define E_NOPARM 2 -#define E_NOMEM 3 - +/* + * get_cmdvec + * + * returns: + * ENOMEM: not enough memory to allocate command + * EAGAIN: command not found + * EINVAL: argument missing for command + */ static int get_cmdvec (char * cmd, vector *v) { @@ -241,13 +246,13 @@ get_cmdvec (char * cmd, vector *v) strvec = alloc_strvec(cmd); if (!strvec) - return E_NOMEM; + return ENOMEM; cmdvec = vector_alloc(); if (!cmdvec) { free_strvec(strvec); - return E_NOMEM; + return ENOMEM; } vector_foreach_slot(strvec, buff, i) { @@ -260,17 +265,17 @@ get_cmdvec (char * cmd, vector *v) } kw = find_key(buff); if (!kw) { - r = E_SYNTAX; + r = EAGAIN; goto out; } cmdkw = alloc_key(); if (!cmdkw) { - r = E_NOMEM; + r = ENOMEM; goto out; } if (!vector_alloc_slot(cmdvec)) { FREE(cmdkw); - r = E_NOMEM; + r = ENOMEM; goto out; } vector_set_slot(cmdvec, cmdkw); @@ -280,7 +285,7 @@ get_cmdvec (char * cmd, vector *v) get_param = 1; } if (get_param) { - r = E_NOPARM; + r = EINVAL; goto out; } *v = cmdvec; @@ -340,13 +345,29 @@ genhelp_sprint_aliases (char * reply, int maxlen, vector keys, } static int -do_genhelp(char *reply, int maxlen) { +do_genhelp(char *reply, int maxlen, const char *cmd, int error) { int len = 0; int i, j; uint64_t fp; struct handler * h; struct key * kw; + switch(error) { + case ENOMEM: + len += snprintf(reply + len, maxlen - len, + "%s: Not enough memory\n", cmd); + break; + case EAGAIN: + len += snprintf(reply + len, maxlen - len, + "%s: not found\n", cmd); + break; + case EINVAL: + len += snprintf(reply + len, maxlen - len, + "%s: Missing argument\n", cmd); + break; + } + if (len >= maxlen) + goto out; len += snprintf(reply + len, maxlen - len, VERSION_STRING); if (len >= maxlen) goto out; @@ -388,7 +409,7 @@ out: static char * -genhelp_handler (void) +genhelp_handler (const char *cmd, int error) { char * reply; char * p = NULL; @@ -401,7 +422,7 @@ genhelp_handler (void) if (!reply) return NULL; p = reply; - p += do_genhelp(reply, maxlen); + p += do_genhelp(reply, maxlen, cmd, error); again = ((p - reply) >= maxlen); REALLOC_REPLY(reply, again, maxlen); } @@ -418,7 +439,7 @@ parse_cmd (char * cmd, char ** reply, int * len, void * data) r = get_cmdvec(cmd, &cmdvec); if (r) { - *reply = genhelp_handler(); + *reply = genhelp_handler(cmd, r); *len = strlen(*reply) + 1; return 0; } @@ -426,7 +447,7 @@ parse_cmd (char * cmd, char ** reply, int * len, void * data) h = find_handler(fingerprint(cmdvec)); if (!h || !h->fn) { - *reply = genhelp_handler(); + *reply = genhelp_handler(cmd, EINVAL); *len = strlen(*reply) + 1; free_keys(cmdvec); return 0; @@ -562,7 +583,7 @@ key_generator (const char * str, int state) /* * If last keyword takes a param, don't even try to guess */ - if (r == E_NOPARM) { + if (r == EINVAL) { has_param = 1; return (strdup("(value)")); }