All of lore.kernel.org
 help / color / mirror / Atom feed
* [Accel-config] [PATCH v1 7/7] accel-config: Fix and refactor status return and printing
@ 2021-03-02 17:44 ramesh.thomas
  0 siblings, 0 replies; only message in thread
From: ramesh.thomas @ 2021-03-02 17:44 UTC (permalink / raw)
  To: accel-config

[-- Attachment #1: Type: text/plain, Size: 4622 bytes --]

From: Ramesh Thomas <ramesh.thomas(a)intel.com>

Return values should be 0 = success and < 0 = error. Some places were
not return correct values. Simplify the return and status messages.

Signed-off-by: Ramesh Thomas <ramesh.thomas(a)intel.com>
---
 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, const char *usage,
 		usage,
 		NULL
 	};
-	int i, rc = 0, success = 0, fail = 0, fail_reason = 0;
+	int i, rc = -EINVAL, success = 0;
 	const char *all = "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 = accfg_device_get_state(device);
-				if (((state == 1) && (action == DEV_ACTION_ENABLE))
-				    ||((state == 0) && (action == DEV_ACTION_DISABLE)))
-					success++;
-				else
-					fail++;
-			} else if (!fail) {
-				fail_reason = rc;
+				if (((state != ACCFG_DEVICE_ENABLED) &&
+						(action == DEV_ACTION_ENABLE)) ||
+						((state != ACCFG_DEVICE_DISABLED) &&
+						 (action == DEV_ACTION_DISABLE)))
+					rc = ENXIO;
+			}
+			if (rc == 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 == 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 == 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 == 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 = 0, success = 0, fail = 0, fail_reason = 0;
+	int i, rc = -EINVAL, success = 0;
 	const char *all = "all";
 
 	argc = parse_options(argc, argv, options, u, 0);
@@ -281,7 +263,7 @@ static int wq_action(int argc, const char **argv, const char *usage,
 
 		rc = 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 == 0) {
 					success++;
 				} else {
-					fail++;
-					fail_reason = 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, const char *usage,
 			fprintf(stderr, "no wq matches id: %s\n", wq_name);
 	}
 
-	if (success) {
-		if (action == 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 == WQ_ACTION_ENABLE ? "enabled" : "disabled",
+				success, argc);
 
-	if (fail) {
-		if (action == 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

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2021-03-02 17:44 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-02 17:44 [Accel-config] [PATCH v1 7/7] accel-config: Fix and refactor status return and printing ramesh.thomas

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.