Skip to contentSkip to conversation

Fix some return value related issues

PATCH In review ramesh.thomas at intel.com 2 participants 7 messages Feb 22, 2021 – Feb 23, 2021

lore
Share & tools
lore

ramesh.thomas at intel.com

#0 lore raw

cover Fix some return value related issues

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

Adds handling of kernel module initialization and status returned from command handler and at place were batch enabling of devices are done.

Ramesh Thomas (5):
accel-config: Remove redundant function
accel-config: Make command handler return status instead of exiting
out
accel-config: Check and return error if kernel module is not loaded
accel-config: Print error message associated with error at exit
accel-config: Print error message associated with error if enable
fails

 accfg/accel-config.c | 54 ++++++++++++++++++++++++++++++++++++++++----
 accfg/enable.c       |  7 +++---
 util/main.c          | 45 +++++-------------------------------
 util/main.h          |  2 +-
 4 files changed, 60 insertions(+), 48 deletions(-)
Signature · 1 line
2.26.2
#1 lore raw

PATCH 1/5 accel-config: Remove redundant function

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

The run_builtin function is not necessary for accel-config because it does not need the additional handling of pipes and sockets that the project it was derived from needed to.

Signed-off-by Ramesh Thomas <ramesh.thomas(a)intel.com>
---
 util/main.c | 39 +--------------------------------------
 1 file changed, 1 insertion(+), 38 deletions(-)
util/main.c +1 -38
index 5260c77..b95d468 100644
@@ -81,43 +81,6 @@ int main_handle_options(const char ***argv, int *argc, const char *usage_msg,
 	return handled;
 }
 
-static int run_builtin(struct cmd_struct *p, int argc, const char **argv,
-		void *ctx)
-{
-	int status;
-	struct stat st;
-
-	status = p->fn(argc, argv, ctx);
-
-	if (status)
-		return status & 0xff;
-
-	/* Somebody closed stdout? */
-	if (fstat(fileno(stdout), &st))
-		return 0;
-	/* Ignore write errors for pipes and sockets.. */
-	if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode))
-		return 0;
-
-	status = 1;
-	/* Check for ENOSPC and EIO errors.. */
-	if (fflush(stdout)) {
-		fprintf(stderr, "write failure on standard output: %s", strerror(errno));
-		goto out;
-	}
-	if (ferror(stdout)) {
-		fprintf(stderr, "unknown write failure on standard output");
-		goto out;
-	}
-	if (fclose(stdout)) {
-		fprintf(stderr, "close failed on standard output: %s", strerror(errno));
-		goto out;
-	}
-	status = 0;
-out:
-	return status;
-}
-
 void main_handle_internal_command(int argc, const char **argv, void *ctx,
 		struct cmd_struct *cmds, int num_cmds)
 {
@@ -134,6 +97,6 @@ void main_handle_internal_command(int argc, const char **argv, void *ctx,
 		struct cmd_struct *p = cmds+i;
 		if (strcmp(p->cmd, cmd))
 			continue;
-		exit(run_builtin(p, argc, argv, ctx));
+		exit(p->fn(argc, argv, ctx));
 	}
 }
Signature · 1 line
2.26.2
#2 lore raw

PATCH 2/5 accel-config: Make command handler return status instead of exiting out

In reply to ramesh.thomas at intel.com

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

Make main_handle_internal_command() return a value indicating status instead of exiting out directly. This will allow post processing after handling commands.

Signed-off-by Ramesh Thomas <ramesh.thomas(a)intel.com>
---
 accfg/accel-config.c | 9 ++++++---
 util/main.c          | 8 ++++++--
 util/main.h          | 2 +-
 3 files changed, 13 insertions(+), 6 deletions(-)
accfg/accel-config.c +6 -3
index f963fc7..b096a5c 100644
@@ -98,9 +98,12 @@ int main(int argc, const char **argv)
 	rc = accfg_new(&ctx);
 	if (rc)
 		return 0;
-	main_handle_internal_command(argc, argv, ctx, commands,
+	rc = main_handle_internal_command(argc, argv, ctx, commands,
 				     ARRAY_SIZE(commands));
 	accfg_unref(ctx);
-	fprintf(stderr, "Unknown command: '%s'\n", argv[0]);
-	return -EINVAL;
+
+	if (!rc)
+		return EXIT_SUCCESS;
+
+	return EXIT_FAILURE;
 }
util/main.c +6 -2
index b95d468..2bf7796 100644
@@ -81,7 +81,7 @@ int main_handle_options(const char ***argv, int *argc, const char *usage_msg,
 	return handled;
 }
 
-void main_handle_internal_command(int argc, const char **argv, void *ctx,
+int main_handle_internal_command(int argc, const char **argv, void *ctx,
 		struct cmd_struct *cmds, int num_cmds)
 {
 	const char *cmd = argv[0];
@@ -97,6 +97,10 @@ void main_handle_internal_command(int argc, const char **argv, void *ctx,
 		struct cmd_struct *p = cmds+i;
 		if (strcmp(p->cmd, cmd))
 			continue;
-		exit(p->fn(argc, argv, ctx));
+		return p->fn(argc, argv, ctx);
 	}
+
+	fprintf(stderr, "Unknown command: '%s'\n", argv[0]);
+
+	return -EINVAL;
 }
util/main.h +1 -1
index bcfe9f3..cccba7a 100644
@@ -19,7 +19,7 @@
 struct cmd_struct;
 int main_handle_options(const char ***argv, int *argc, const char *usage_msg,
 		struct cmd_struct *cmds, int num_cmds);
-void main_handle_internal_command(int argc, const char **argv, void *ctx,
+int main_handle_internal_command(int argc, const char **argv, void *ctx,
 		struct cmd_struct *cmds, int num_cmds);
 int help_show_man_page(const char *cmd, const char *util_name,
 		const char *viewer);
Signature · 1 line
2.26.2
#3 lore raw

PATCH 3/5 accel-config: Check and return error if kernel module is not loaded

In reply to ramesh.thomas at intel.com

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

Check if kernel module is loaded before running commands. Return failure if not loaded.

Signed-off-by Ramesh Thomas <ramesh.thomas(a)intel.com>
---
 accfg/accel-config.c | 44 +++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 41 insertions(+), 3 deletions(-)
accfg/accel-config.c +41 -3
index b096a5c..f12e449 100644
@@ -7,6 +7,7 @@
 #include <unistd.h>
 #include <sys/stat.h>
 #include <sys/types.h>
+#include <libkmod.h>
 #include <builtin.h>
 #include <accfg/libaccel_config.h>
 #include <ccan/array_size/array_size.h>
@@ -74,9 +75,34 @@ static struct cmd_struct commands[] = {
 #endif
 };
 
+static int idxd_kmod_init(struct kmod_ctx **ctx, struct kmod_module **mod)
+{
+	int rc;
+
+	*ctx = kmod_new(NULL, NULL);
+	if (!*ctx)
+		return -ENXIO;
+
+	rc = kmod_module_new_from_name(*ctx, "idxd", mod);
+	if (rc < 0) {
+		kmod_unref(*ctx);
+		return rc;
+	}
+
+	rc = kmod_module_get_initstate(*mod);
+	if (rc < 0) {
+		kmod_module_unref(*mod);
+		kmod_unref(*ctx);
+	}
+
+	return rc;
+}
+
 int main(int argc, const char **argv)
 {
 	struct accfg_ctx *ctx;
+	struct kmod_ctx *kmod_ctx;
+	struct kmod_module *mod;
 	int rc;
 
 	/* Look for flags.. */
@@ -95,15 +121,27 @@ int main(int argc, const char **argv)
 		return 0;
 	}
 
+	rc = idxd_kmod_init(&kmod_ctx, &mod);
+	if (rc < 0) {
+		fprintf(stderr, "Failed initializing kernel module\n");
+		goto error_exit;
+	}
+
 	rc = accfg_new(&ctx);
-	if (rc)
-		return 0;
+	if (rc) {
+		kmod_module_unref(mod);
+		kmod_unref(kmod_ctx);
+		goto error_exit;
+	}
+
 	rc = main_handle_internal_command(argc, argv, ctx, commands,
 				     ARRAY_SIZE(commands));
 	accfg_unref(ctx);
+	kmod_module_unref(mod);
+	kmod_unref(kmod_ctx);
 
 	if (!rc)
 		return EXIT_SUCCESS;
-
+error_exit:
 	return EXIT_FAILURE;
 }
Signature · 1 line
2.26.2
#4 lore raw

PATCH 4/5 accel-config: Print error message associated with error at exit

In reply to ramesh.thomas at intel.com

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

Assign error code returned by commands to errno and use perror to print a short description of the error at app exit.

Signed-off-by Ramesh Thomas <ramesh.thomas(a)intel.com>
---
 accfg/accel-config.c | 3 +++
 1 file changed, 3 insertions(+)
accfg/accel-config.c +3 -0
index f12e449..de6fdb9 100644
@@ -143,5 +143,8 @@ int main(int argc, const char **argv)
 	if (!rc)
 		return EXIT_SUCCESS;
 error_exit:
+	errno = abs(rc);
+	perror("Error");
+
 	return EXIT_FAILURE;
 }
Signature · 1 line
2.26.2
#5 lore raw

PATCH 5/5 accel-config: Print error message associated with error if enable fails

In reply to ramesh.thomas at intel.com

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

Print associated error message of failed reason when device enable fails.

Signed-off-by Ramesh Thomas <ramesh.thomas(a)intel.com>
---
 accfg/enable.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
accfg/enable.c +4 -3
index 8fbdbb0..34328ca 100644
@@ -172,7 +172,8 @@ static int device_action(int argc, const char **argv, const char *usage,
 	}
 
 	if (fail_reason) {
-		fprintf(stderr, "failed due to reason %d\n", fail_reason);
+		fprintf(stderr, "failed due to reason %s\n",
+				strerror(abs(fail_reason)));
 		return fail_reason;
 	}
 
@@ -334,8 +335,8 @@ static int wq_action(int argc, const char **argv, const char *usage,
 	}
 
 	if (fail_reason) {
-		fprintf(stderr, "failed due to reason %d\n",
-			fail_reason);
+		fprintf(stderr, "failed due to reason %s\n",
+			strerror(abs(fail_reason)));
 		return fail_reason;
 	}
 	return -ENXIO;
Signature · 1 line
2.26.2
Reviewed-by on the v1 series
#6 lore raw
In reply to ramesh.thomas at intel.com
Quoted 14 lines · ramesh.thomas(a)intel.com

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

Adds handling of kernel module initialization and status returned from command handler and at place were batch enabling of devices are done.

Ramesh Thomas (5):
accel-config: Remove redundant function
accel-config: Make command handler return status instead of exiting
out
accel-config: Check and return error if kernel module is not loaded
accel-config: Print error message associated with error at exit
accel-config: Print error message associated with error if enable
fails

Quoted 5 lines
  accfg/accel-config.c | 54 ++++++++++++++++++++++++++++++++++++++++----
  accfg/enable.c       |  7 +++---
  util/main.c          | 45 +++++-------------------------------
  util/main.h          |  2 +-
  4 files changed, 60 insertions(+), 48 deletions(-)
Reviewed-by Dave Jiang <dave.jiang(a)intel.com>

for the series.