Content-Type: multipart/mixed; boundary="===============2906645294734793583==" MIME-Version: 1.0 From: ramesh.thomas at intel.com Subject: [Accel-config] [PATCH v1 7/7] accel-config: Fix and refactor status return and printing Date: Tue, 02 Mar 2021 12:44:42 -0500 Message-ID: <20210302174442.206008-8-ramesh.thomas@intel.com> In-Reply-To: 20210302174442.206008-1-ramesh.thomas@intel.com To: accel-config@lists.01.org List-ID: --===============2906645294734793583== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Ramesh Thomas Return values should be 0 =3D success and < 0 =3D error. Some places were not return correct values. Simplify the return and status messages. Signed-off-by: Ramesh Thomas --- accfg/enable.c | 88 ++++++++++++++------------------------------------ 1 file changed, 24 insertions(+), 64 deletions(-) diff --git a/accfg/enable.c b/accfg/enable.c index 642e30a..9cb6794 100644 --- a/accfg/enable.c +++ b/accfg/enable.c @@ -100,7 +100,7 @@ static int device_action(int argc, const char **argv, c= onst char *usage, usage, NULL }; - int i, rc =3D 0, success =3D 0, fail =3D 0, fail_reason =3D 0; + int i, rc =3D -EINVAL, success =3D 0; const char *all =3D "all"; enum accfg_device_state state; = @@ -139,13 +139,15 @@ static int device_action(int argc, const char **argv,= const char *usage, * matches with the enable/disable */ state =3D accfg_device_get_state(device); - if (((state =3D=3D 1) && (action =3D=3D DEV_ACTION_ENABLE)) - ||((state =3D=3D 0) && (action =3D=3D DEV_ACTION_DISABLE))) - success++; - else - fail++; - } else if (!fail) { - fail_reason =3D rc; + if (((state !=3D ACCFG_DEVICE_ENABLED) && + (action =3D=3D DEV_ACTION_ENABLE)) || + ((state !=3D ACCFG_DEVICE_DISABLED) && + (action =3D=3D DEV_ACTION_DISABLE))) + rc =3D ENXIO; + } + if (rc =3D=3D 0) { + success++; + } else { fprintf(stderr, "failed in %s\n", argv[i]); = print_device_cmd_status(device); @@ -157,34 +159,14 @@ static int device_action(int argc, const char **argv,= const char *usage, argv[i]); } = - if (success) { - if (action =3D=3D DEV_ACTION_ENABLE) - fprintf(stderr, "successfully enabled %d device%s\n", - success, success > 1 ? "s" : ""); - else - fprintf(stderr, "successfully disabled %d device%s\n", - success, success > 1 ? "s" : ""); - return success; - } - - if (fail) { - if (action =3D=3D DEV_ACTION_ENABLE) - fprintf(stderr, "failed to enable %d device%s\n", fail, - fail > 1 ? "s" : ""); - else - fprintf(stderr, "failed to disable %d device%s\n", fail, - fail > 1 ? "s" : ""); + fprintf(stderr, "%s %d device(s) out of %d\n", + action =3D=3D DEV_ACTION_ENABLE ? "enabled" : "disabled", + success, argc); = - return fail; - } + if (success) + return 0; = - if (fail_reason) { - fprintf(stderr, "failed due to reason %s\n", - strerror(abs(fail_reason))); - return fail_reason; - } - - return -ENXIO; + return rc; } = static int action_disable_wq(struct accfg_wq *wq, const char *wq_name) @@ -245,7 +227,7 @@ static int wq_action(int argc, const char **argv, const= char *usage, NULL }; unsigned long dev_id, wq_id; - int i, rc =3D 0, success =3D 0, fail =3D 0, fail_reason =3D 0; + int i, rc =3D -EINVAL, success =3D 0; const char *all =3D "all"; = argc =3D parse_options(argc, argv, options, u, 0); @@ -281,7 +263,7 @@ static int wq_action(int argc, const char **argv, const= char *usage, = rc =3D sprintf(wq_name, "wq%lu.%lu", dev_id, wq_id); if (rc < 0) - return errno; + return rc; = accfg_device_foreach(ctx, device) { if (!util_device_filter(device, dev_name)) @@ -294,8 +276,6 @@ static int wq_action(int argc, const char **argv, const= char *usage, if (rc =3D=3D 0) { success++; } else { - fail++; - fail_reason =3D rc; fprintf(stderr, "failed in %s\n", wq_name); = print_device_cmd_status(device); @@ -307,34 +287,14 @@ static int wq_action(int argc, const char **argv, con= st char *usage, fprintf(stderr, "no wq matches id: %s\n", wq_name); } = - if (success) { - if (action =3D=3D WQ_ACTION_ENABLE) - fprintf(stderr, "successfully enabled %d wq%s\n", - success, - success > 1 ? "s" : ""); - else - fprintf(stderr, "successfully disabled %d wq%s\n", - success, - success > 1 ? "s" : ""); - return success; - } + fprintf(stderr, "%s %d wq(s) out of %d\n", + action =3D=3D WQ_ACTION_ENABLE ? "enabled" : "disabled", + success, argc); = - if (fail) { - if (action =3D=3D WQ_ACTION_ENABLE) - fprintf(stderr, "failed to enable %d wq%s\n", - fail, fail > 1 ? "s" : ""); - else - fprintf(stderr, "failed to disable %d wq%s\n", - fail, fail > 1 ? "s" : ""); - return fail; - } + if (success) + return 0; = - if (fail_reason) { - fprintf(stderr, "failed due to reason %s\n", - strerror(abs(fail_reason))); - return fail_reason; - } - return -ENXIO; + return rc; } = int cmd_disable_device(int argc, const char **argv, void *ctx) -- = 2.26.2 --===============2906645294734793583==--