libkmod-module: convert return value from system() to errno
PATCH 1/1 libkmod-module: convert return value from system() to errno
Don't use exit status of a command directly as errno code, callers will be confused.
--- libkmod/libkmod-module.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
libkmod/libkmod-module.c +0 -0
@@ -983,11 +983,13 @@ static int command_do(struct kmod_module *mod, const char *type,if (err ==-1 ||WEXITSTATUS (err)) { ERR(mod->ctx, "Error running %s command for %s\n", type,
modname);
- if (err != -1)
- err = -WEXITSTATUS(err);
+ if (err != -1) /* nonzero exit status: something bad happened */
+ return -EINVAL;
+ else /* child process could not be created */
+ return -errno;
}- return err;
+ return 0;
}
struct probe_insert_cb {
Signature · 1 line
2.24.0
From f560864a4a8fd61c2881cfefb970db27c2418690 Mon Sep 17 00:00:00 2001
From: Topi Miettinen <[email protected]>
Date: Mon, 23 Dec 2019 18:58:13 +0200
Subject: [PATCH] libkmod-module: convert return value from system() to errno
Don't use exit status of a command directly as errno code, callers will be confused.
--- libkmod/libkmod-module.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
libkmod/libkmod-module.c +5 -3
@@ -983,11 +983,13 @@ static int command_do(struct kmod_module *mod, const char *type,if (err ==-1 ||WEXITSTATUS (err)) {ERR (mod->ctx,"Error running %s command for %s\n" , type, modname); -if (err !=-1 ) - err = -WEXITSTATUS (err); +if (err !=-1 )/* nonzero exit status: something bad happened */ +return -EINVAL ; +else /* child process could not be created */ +return -errno; } -return err; +return 0 ; }struct probe_insert_cb {
Signature · 1 line
2.24.0
Quoted 14 lines · Topi Miettinen
Don't use exit status of a command directly as errno code, callers will be confused.
Signed-off-by Topi Miettinen <[email protected]>--- libkmod/libkmod-module.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)libkmod/libkmod-module.c +0
-0
libkmod/libkmod-module.c static int command_do(struct kmod_module *mod, · line 989
Quoted 7 lines
libkmod/libkmod-module.c +0
-2@@ -983,11 +983,13 @@ static int command_do(struct kmod_module *mod, const char *type, if (err == -1 || WEXITSTATUS(err)) { ERR(mod->ctx, "Error running %s command for %s\n", type, modname); -if (err !=-1 ) - err = -WEXITSTATUS (err);
I don't think we actually care about differentiating them. So just a plain return -EINVAL; here would suffice, makes sense?
Lucas De Marchi
Quoted 14 lines
libkmod/libkmod-module.c +1
-0+if (err !=-1 )/* nonzero exit status: something bad happened */
libkmod/libkmod-module.c +4
-2+return -EINVAL ; + else /* child process could not be created */ + return -errno; } -return err; +return 0 ; }struct probe_insert_cb { --2.24.0
Signature · 1 line
Lucas De Marchi
Quoted 10 lines · Lucas De Marchi
Quoted 9 lines · Topi Miettinen
Don't use exit status of a command directly as errno code, callers will be confused.
Signed-off-by Topi Miettinen <[email protected]>--- libkmod/libkmod-module.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
Quoted 15 lines
libkmod/libkmod-module.c static int command_do(struct kmod_module *mod, · line 989
Quoted 12 lines
libkmod/libkmod-module.c +0
-2@@ -983,11 +983,13 @@ static int command_do(struct kmod_module *mod, const char *type, if (err == -1 || WEXITSTATUS(err)) { ERR(mod->ctx, "Error running %s command for %s\n", type, modname); -if (err !=-1 ) - err = -WEXITSTATUS (err);I don't think we actually care about differentiating them. So just a plain return
-EINVAL; here would suffice, makes sense?
I think it would lose potentially valuable information. For example EPERM could tell the system administrator of a problem with MAC configuration preventing execution of the shell, ENOENT could show that the shell or shared libraries are missing and so forth.
-Topi
Quoted 27 lines · Topi Miettinen
Quoted 10 lines · Lucas De Marchi
Quoted 9 lines · Topi Miettinen
Don't use exit status of a command directly as errno code, callers will be confused.
Signed-off-by Topi Miettinen <[email protected]>--- libkmod/libkmod-module.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)Quoted 15 lines
libkmod/libkmod-module.c static int command_do(struct kmod_module *mod, · line 989
Quoted 12 lines
libkmod/libkmod-module.c +0
-2@@ -983,11 +983,13 @@ static int command_do(struct kmod_module *mod, const char *type, if (err == -1 || WEXITSTATUS(err)) { ERR(mod->ctx, "Error running %s command for %s\n", type, modname); -if (err !=-1 ) - err = -WEXITSTATUS (err);I don't think we actually care about differentiating them. So just a plain return
-EINVAL; here would suffice, makes sense?
Quoted 1 line
I think it would lose potentially valuable information. For example EPERM could tell the system administrator of a problem with MAC configuration preventing execution of the shell, ENOENT could show that the shell or shared libraries are missing and so forth.
makes sense, but we take decisions on the callers depending on the return value. I don't want to mix that with return values from the commands executed. E.g. if the command returned EEXIST the caller would treat a fail differently.
I think it would be good here to give different error messages and always return -EINVAL. I'm thinking on squashing the following diff, what do you think?
libkmod/libkmod-module.c +10 -7
@@ -980,13 +980,16 @@ static int command_do(struct kmod_module *mod, const char *type, err =system (cmd);unsetenv ("MODPROBE_MODULE" ); -if (err ==-1 ||WEXITSTATUS (err)) { -ERR (mod->ctx,"Error running %s command for %s\n" , - type, modname); -if (err !=-1 )/* nonzero exit status: something bad happened */ -return -EINVAL ; -else /* child process could not be created */ -return -errno; +if (err ==-1 ) { +ERR (mod->ctx,"Could not run %s command '%s' for module %s: %m\n" , + type, cmd, modname); +return -EINVAL ; + } + +if (WEXITSTATUS (err)) { +ERR (mod->ctx,"Error running %s command '%s' for module %s: retcode %d\n" , + type, cmd, modname,WEXITSTATUS (err)); +return -EINVAL ; }return 0 ;
thanks
Lucas De Marchi
Quoted 30 lines · Lucas De Marchi
Quoted 27 lines · Topi Miettinen
Quoted 10 lines · Lucas De Marchi
Quoted 9 lines · Topi Miettinen
Don't use exit status of a command directly as errno code, callers will be confused.
Signed-off-by Topi Miettinen <[email protected]>---
libkmod/libkmod-module.c| 8 +++++---1 file changed, 5 insertions(+), 3 deletions(-)Quoted 15 lines
libkmod/libkmod-module.c static int command_do(struct kmod_module *mod, · line 989
Quoted 12 lines
libkmod/libkmod-module.c +0
-2@@ -983,11 +983,13 @@ static int command_do(struct kmod_module *mod, const char *type, if (err == -1 || WEXITSTATUS(err)) { ERR(mod->ctx, "Error running %s command for %s\n", type, modname); - if (err != -1) - err = -WEXITSTATUS(err);I don't think we actually care about differentiating them. So just a plain return -EINVAL; here would suffice, makes sense?
Quoted 1 line
I think it would lose potentially valuable information. For example EPERM could tell the system administrator of a problem with MAC configuration preventing execution of the shell, ENOENT could show that the shell or shared libraries are missing and so forth.
Quoted 8 lines
makes sense, but we take decisions on the callers depending on the return value. I don't want to mix that with return values from the commands executed. E.g. if the command returned EEXIST the caller would treat a fail differently.
I think it would be good here to give different error messages and always return
-EINVAL. I'm thinking on squashing the following diff, what do you think?
OK, maybe that's better.
-Topi
Quoted 35 lines
libkmod/libkmod-module.c +10
-7@@ -980,13 +980,16 @@ static int command_do(struct kmod_module *mod,const char *type, err = system(cmd); unsetenv("MODPROBE_MODULE"); - if (err == -1 || WEXITSTATUS(err)) { - ERR(mod->ctx, "Error running %s command for %s\n", - type, modname); - if (err != -1) /* nonzero exit status: something bad happened */ - return -EINVAL; - else /* child process could not be created */ - return -errno; + if (err == -1) { + ERR(mod->ctx, "Could not run %s command '%s' for module %s: %m\n", + type, cmd, modname); + return -EINVAL; + } + + if (WEXITSTATUS(err)) { + ERR(mod->ctx, "Error running %s command '%s' for module %s: retcode %d\n", + type, cmd, modname, WEXITSTATUS(err)); + return -EINVAL; }return 0;
thanks
Lucas De Marchi