Fail like the shell when execvp fails
Subsystems (2)
PATCH 1/1 Fail like the shell when execvp fails
Commands executing another command (e.g. taskset) should fail in a consistent and detectable way when the other command fail to execute. Change most of the commands that used to fail with EXIT_FAILURE to fail with the special exit code 127, like the shell.
For example, running the fuser command via taskset when fuser command is not installed is failing now like this:
$ taskset -c 1 fuser /path/to/file; echo $?
taskset: failed to execute fuser: No such file or directory
1This failure looks like a real failure of the fuser command:
$ taskset -c 1 fuser /path/to/file; echo $?
Specified filename /path/to/file does not exist.
1Based on the exit code, a management program may do the wrong thing, leading to catastrophic results. Detecting such failure requires fragile parsing of standard error.
With this patch this failure is easy to detect:
$ taskset -c 1 fuser /path/to/file; echo $?
taskset: failed to execute fuser: No such file or directory
127The return code seems to be undocumented for most commands, except the su command, promising to exit with exit code 126 if a command cannot be executed, and 127 if a command was not found. However, the actual command always exit with exit code 1. This patch implement the documented behavior.
--- disk-utils/mkfs.c | 3 ++- include/exitcodes.h | 7 +++++++ lib/pager.c | 3 ++- login-utils/su-common.c | 7 ++++++- schedutils/chrt.c | 3 ++- schedutils/ionice.c | 3 ++- schedutils/taskset.c | 3 ++- sys-utils/flock.c | 4 +++- sys-utils/nsenter.c | 3 ++- sys-utils/prlimit.c | 3 ++- sys-utils/setarch.c | 7 ++++--- sys-utils/setpriv.c | 3 ++- sys-utils/setsid.c | 3 ++- sys-utils/swapon.c | 3 ++- sys-utils/unshare.c | 3 ++- tests/expected/misc/setarch-options | 2 +- text-utils/more.c | 3 ++- 17 files changed, 45 insertions(+), 18 deletions(-)
disk-utils/mkfs.c +2 -1
@@ -33,6 +33,7 @@#include "closestream.h" #include "nls.h" #include "xalloc.h" +#include "exitcodes.h" #ifndef DEFAULT_FSTYPE #define DEFAULT_FSTYPE "ext2"
@@ -136,5 +137,5 @@ int main(int argc, char **argv)/* Execute the program */ execvp (progname, argv + optind); -err (EXIT_FAILURE ,_ ("failed to execute %s" ), progname); +err (EX_EXEC ,_ ("failed to execute %s" ), progname); }
include/exitcodes.h +7 -0
@@ -32,4 +32,11 @@#define MOUNT_EX_FAIL 32/* mount failure */ #define MOUNT_EX_SOMEOK 64/* some mount succeeded */ +/* Exit codes use by the su program */ +#define SU_EX_EXEC 126/* faied to execute a program */ +#define SU_EX_NOENT 127/* program was not found */ + +/* General exit codes */ +#define EX_EXEC 127/* faied to execute a program */ +#endif /* UTIL_LINUX_EXITCODES_H */
lib/pager.c +2 -1
@@ -17,6 +17,7 @@#include "c.h" #include "xalloc.h" #include "nls.h" +#include "exitcodes.h" #define NULL_DEVICE "/dev/null"
@@ -73,7 +74,7 @@ static int start_command(struct child_process *cmd) cmd->preexec_cb ();execvp (cmd->argv[0 ], (char *const *) cmd->argv); -exit (127 );/* cmd not found */ +exit (EX_EXEC );/* cmd not found */ }if (cmd->pid <0 ) {
login-utils/su-common.c +6 -1
@@ -71,6 +71,7 @@ enum#include "closestream.h" #include "strutils.h" #include "ttyutils.h" +#include "exitcodes.h" /* name of the pam configuration files. separate configs for su and su - */ #define PAM_SRVNAME_SU "su"
@@ -983,7 +984,11 @@ su_main (int argc, char **argv, int mode)run_shell (shell, command, argv + optind,max (0 , argc - optind)); else {execvp (argv[optind], &argv[optind]); -err (EXIT_FAILURE ,_ ("failed to execute %s" ), argv[optind]); +/* su(1) promise to fail with exit code 126 if the command +cannot be executed, and 127 if the command was not +found. */ +err (errno ==ENOENT ?SU_EX_NOENT :SU_EX_EXEC , +_ ("failed to execute %s" ), argv[optind]); } }
schedutils/chrt.c +2 -1
@@ -32,6 +32,7 @@#include "closestream.h" #include "strutils.h" #include "procutils.h" +#include "exitcodes.h" /* the SCHED_BATCH is supported since Linux 2.6.16 * -- temporary workaround for people with old glibc headers
@@ -517,7 +518,7 @@ int main(int argc, char **argv)if (!ctl->pid) { argv += optind +1 ;execvp (argv[0 ], argv); -err (EXIT_FAILURE ,_ ("failed to execute %s" ), argv[0 ]); +err (EX_EXEC ,_ ("failed to execute %s" ), argv[0 ]); }return EXIT_SUCCESS ;
schedutils/ionice.c +2 -1
@@ -18,6 +18,7 @@#include "strutils.h" #include "c.h" #include "closestream.h" +#include "exitcodes.h" static int tolerant;
@@ -257,7 +258,7 @@ int main(int argc, char **argv)*/ ioprio_setid (0 , ioclass, data,IOPRIO_WHO_PROCESS );execvp (argv[optind], &argv[optind]); -err (EXIT_FAILURE ,_ ("failed to execute %s" ), argv[optind]); +err (EX_EXEC ,_ ("failed to execute %s" ), argv[optind]); }else usage (stderr);
schedutils/taskset.c +2 -1
@@ -34,6 +34,7 @@#include "procutils.h" #include "c.h" #include "closestream.h" +#include "exitcodes.h" struct taskset {pid_t pid;/* task PID */
@@ -236,7 +237,7 @@ int main(int argc, char **argv)if (!pid) { argv += optind +1 ;execvp (argv[0 ], argv); -err (EXIT_FAILURE ,_ ("failed to execute %s" ), argv[0 ]); +err (EX_EXEC ,_ ("failed to execute %s" ), argv[0 ]); }return EXIT_SUCCESS ;
sys-utils/flock.c +3 -1
@@ -45,6 +45,7 @@#include "closestream.h" #include "monotonic.h" #include "timer.h" +#include "exitcodes.h" static void __attribute__((__noreturn__))usage (int ex) {
@@ -330,7 +331,8 @@ int main(int argc, char *argv[])execvp (cmd_argv[0 ], cmd_argv);/* execvp() failed */ warn (_ ("failed to execute %s" ), cmd_argv[0 ]); -_exit ((errno ==ENOMEM ) ?EX_OSERR :EX_UNAVAILABLE ); +/* Behave mostly like the shell */ +_exit ((errno ==ENOMEM ) ?EX_OSERR :EX_EXEC ); }else { do { w = waitpid(f, &status, 0);
sys-utils/nsenter.c +2 -1
@@ -40,6 +40,7 @@#include "closestream.h" #include "namespace.h" #include "exec_shell.h" +#include "exitcodes.h" static struct namespace_file { int nstype;
@@ -413,7 +414,7 @@ int main(int argc, char *argv[])if (optind < argc) {execvp (argv[optind], argv + optind); -err (EXIT_FAILURE ,_ ("failed to execute %s" ), argv[optind]); +err (EX_EXEC ,_ ("failed to execute %s" ), argv[optind]); }exec_shell (); }
sys-utils/prlimit.c +2 -1
@@ -35,6 +35,7 @@#include "strutils.h" #include "list.h" #include "closestream.h" +#include "exitcodes.h" #ifndef RLIMIT_RTTIME # define RLIMIT_RTTIME 15
@@ -641,7 +642,7 @@ int main(int argc, char **argv)if (argc > optind) {/* prlimit [options] COMMAND */ execvp (argv[optind], &argv[optind]); -err (EXIT_FAILURE ,_ ("failed to execute %s" ), argv[optind]); +err (EX_EXEC ,_ ("failed to execute %s" ), argv[optind]); }return EXIT_SUCCESS ;
sys-utils/setarch.c +4 -3
@@ -35,6 +35,7 @@#include "nls.h" #include "c.h" #include "closestream.h" +#include "exitcodes.h" #ifndef HAVE_PERSONALITY # include <syscall.h>
@@ -399,10 +400,10 @@ int main(int argc, char *argv[])if (!argc) {execl ("/bin/sh" ,"-sh" ,NULL ); -err (EXIT_FAILURE ,_ ("failed to execute %s" ),"/bin/sh" ); +err (EX_EXEC ,_ ("failed to execute %s" ),"/bin/sh" ); }execvp (argv[0 ], argv); -err (EXIT_FAILURE ,"%s" , argv[0 ]); -return EXIT_FAILURE ; +err (EX_EXEC ,"%s" , argv[0 ]); +return EX_EXEC ; }
sys-utils/setpriv.c +2 -1
@@ -38,6 +38,7 @@#include "strutils.h" #include "xalloc.h" #include "pathnames.h" +#include "exitcodes.h" #ifndef PR_SET_NO_NEW_PRIVS # define PR_SET_NO_NEW_PRIVS 38
@@ -839,5 +840,5 @@ int main(int argc, char **argv)execvp (argv[optind], argv + optind); -err (EXIT_FAILURE ,_ ("cannot execute: %s" ), argv[optind]); +err (EX_EXEC ,_ ("cannot execute: %s" ), argv[optind]); }
sys-utils/setsid.c +2 -1
@@ -24,6 +24,7 @@#include "c.h" #include "nls.h" #include "closestream.h" +#include "exitcodes.h" static void __attribute__ ((__noreturn__))usage (FILE * out) {
@@ -114,5 +115,5 @@ int main(int argc, char **argv)err (EXIT_FAILURE ,_ ("failed to set the controlling terminal" )); }execvp (argv[optind], argv + optind); -err (EXIT_FAILURE ,_ ("failed to execute %s" ), argv[optind]); +err (EX_EXEC ,_ ("failed to execute %s" ), argv[optind]); }
sys-utils/swapon.c +2 -1
@@ -23,6 +23,7 @@#include "strutils.h" #include "optutils.h" #include "closestream.h" +#include "exitcodes.h" #include "swapheader.h" #include "swapprober.h"
@@ -337,7 +338,7 @@ static int swap_reinitialize(struct swap_device *dev) cmd[idx++] = dev->path; cmd[idx++] =NULL ;execvp (cmd[0 ], (char *const *) cmd); -err (EXIT_FAILURE ,_ ("failed to execute %s" ), cmd[0 ]); +err (EX_EXEC ,_ ("failed to execute %s" ), cmd[0 ]);default :/* parent */ do {
sys-utils/unshare.c +2 -1
@@ -40,6 +40,7 @@#include "xalloc.h" #include "pathnames.h" #include "all-io.h" +#include "exitcodes.h" /* synchronize parent and child by pipe */ #define PIPE_SYNC_BYTE 0x06
@@ -458,7 +459,7 @@ int main(int argc, char *argv[])if (optind < argc) {execvp (argv[optind], argv + optind); -err (EXIT_FAILURE ,_ ("failed to execute %s" ), argv[optind]); +err (EX_EXEC ,_ ("failed to execute %s" ), argv[optind]); }exec_shell (); }
tests/expected/misc/setarch-options +1 -1
@@ -4,7 +4,7 @@ exit: 1 ###### unknown command Execute command `/das/gibs/nicht'. setarch: /das/gibs/nicht: No such file or directory -exit: 1 +exit: 127 ###### noop uname -a Execute command `uname'. uname -a unchanged
text-utils/more.c +2 -1
@@ -65,6 +65,7 @@#include "xalloc.h" #include "widechar.h" #include "closestream.h" +#include "exitcodes.h" #include <regex.h>
@@ -1622,7 +1623,7 @@ void execute(char *filename, char *cmd, ...)execvp (cmd, args);putserr (_ ("exec failed\n" )); -exit (EXIT_FAILURE ); +exit (EX_EXEC ); }if (id >0 ) {signal (SIGINT ,SIG_IGN );
Signature · 1 line
2.4.11
Quoted 46 lines · Nir Soffer
Commands executing another command (e.g. taskset) should fail in a consistent and detectable way when the other command fail to execute. Change most of the commands that used to fail with
EXIT_FAILUREto fail with the special exit code 127, like the shell.For example, running the fuser command via taskset when fuser command is not installed is failing now like this:
$ taskset -c 1 fuser /path/to/file; echo $? taskset: failed to execute fuser: No such file or directory 1This failure looks like a real failure of the fuser command:
$ taskset -c 1 fuser /path/to/file; echo $? Specified filename /path/to/file does not exist. 1Based on the exit code, a management program may do the wrong thing, leading to catastrophic results. Detecting such failure requires fragile parsing of standard error.
With this patch this failure is easy to detect:
$ taskset -c 1 fuser /path/to/file; echo $? taskset: failed to execute fuser: No such file or directory 127The return code seems to be undocumented for most commands, except the su command, promising to exit with exit code 126 if a command cannot be executed, and 127 if a command was not found. However, the actual command always exit with exit code 1. This patch implement the documented behavior.
Signed-off-by Nir Soffer <[email protected]>--- disk-utils/mkfs.c | 3 ++- include/exitcodes.h | 7 +++++++ lib/pager.c | 3 ++- login-utils/su-common.c | 7 ++++++- schedutils/chrt.c | 3 ++- schedutils/ionice.c | 3 ++- schedutils/taskset.c | 3 ++- sys-utils/flock.c | 4 +++- sys-utils/nsenter.c | 3 ++- sys-utils/prlimit.c | 3 ++-
Quoted 8 lines
sys-utils/setarch.c | 7 ++++--- sys-utils/setpriv.c | 3 ++- sys-utils/setsid.c | 3 ++- sys-utils/swapon.c | 3 ++- sys-utils/unshare.c | 3 ++- tests/expected/misc/setarch-options | 2 +- text-utils/more.c | 3 ++- 17 files changed, 45 insertions(+), 18 deletions(-)
Lots of change in a single patch. Could these change be split to something like:
1. include/exitcodes.h
2. lib/pager.c
3. changes to commands
4. updates to tests
5. manual page updates
Quoted 24 lines
disk-utils/mkfs.c +2
-1@@ -33,6 +33,7 @@#include "closestream.h" #include "nls.h" #include "xalloc.h" +#include "exitcodes.h" #ifndef DEFAULT_FSTYPE #define DEFAULT_FSTYPE "ext2"@@ -136,5 +137,5 @@ int main(int argc, char **argv)/* Execute the program */ execvp (progname, argv + optind); -err (EXIT_FAILURE ,_ ("failed to execute %s" ), progname); +err (EX_EXEC ,_ ("failed to execute %s" ), progname); }include/exitcodes.h +0
-0
include/exitcodes.h line 41
Quoted 10 lines
include/exitcodes.h +7
-0@@ -32,4 +32,11 @@#define MOUNT_EX_FAIL 32/* mount failure */ #define MOUNT_EX_SOMEOK 64/* some mount succeeded */ +/* Exit codes use by the su program */ +#define SU_EX_EXEC 126/* faied to execute a program */ +#define SU_EX_NOENT 127/* program was not found */ + +/* General exit codes */ +#define EX_EXEC 127/* faied to execute a program */ +
Looks like typo x2
s/faied/failed/
Quoted 123 lines
include/exitcodes.h +0
-0#endif /* UTIL_LINUX_EXITCODES_H */ lib/pager.c +2
-1@@ -17,6 +17,7 @@#include "c.h" #include "xalloc.h" #include "nls.h" +#include "exitcodes.h" #define NULL_DEVICE "/dev/null"@@ -73,7 +74,7 @@ static int start_command(struct child_process *cmd) cmd->preexec_cb ();execvp (cmd->argv[0 ], (char *const *) cmd->argv); -exit (127 );/* cmd not found */ +exit (EX_EXEC );/* cmd not found */ }if (cmd->pid <0 ) {login-utils/su-common.c +6
-1@@ -71,6 +71,7 @@ enum#include "closestream.h" #include "strutils.h" #include "ttyutils.h" +#include "exitcodes.h" /* name of the pam configuration files. separate configs for su and su - */ #define PAM_SRVNAME_SU "su"@@ -983,7 +984,11 @@ su_main (int argc, char **argv, int mode)run_shell (shell, command, argv + optind,max (0 , argc - optind)); else {execvp (argv[optind], &argv[optind]); -err (EXIT_FAILURE ,_ ("failed to execute %s" ), argv[optind]); +/* su(1) promise to fail with exit code 126 if the command +cannot be executed, and 127 if the command was not +found. */ +err (errno ==ENOENT ?SU_EX_NOENT :SU_EX_EXEC , +_ ("failed to execute %s" ), argv[optind]); } }schedutils/chrt.c +2
-1@@ -32,6 +32,7 @@#include "closestream.h" #include "strutils.h" #include "procutils.h" +#include "exitcodes.h" /* the SCHED_BATCH is supported since Linux 2.6.16 * -- temporary workaround for people with old glibc headers @@ -517,7 +518,7 @@ int main(int argc, char **argv)if (!ctl->pid) { argv += optind +1 ;execvp (argv[0 ], argv); -err (EXIT_FAILURE ,_ ("failed to execute %s" ), argv[0 ]); +err (EX_EXEC ,_ ("failed to execute %s" ), argv[0 ]); }return EXIT_SUCCESS ;schedutils/ionice.c +2
-1@@ -18,6 +18,7 @@#include "strutils.h" #include "c.h" #include "closestream.h" +#include "exitcodes.h" static int tolerant;@@ -257,7 +258,7 @@ int main(int argc, char **argv)*/ ioprio_setid (0 , ioclass, data,IOPRIO_WHO_PROCESS );execvp (argv[optind], &argv[optind]); -err (EXIT_FAILURE ,_ ("failed to execute %s" ), argv[optind]); +err (EX_EXEC ,_ ("failed to execute %s" ), argv[optind]); }else usage (stderr);schedutils/taskset.c +2
-1@@ -34,6 +34,7 @@#include "procutils.h" #include "c.h" #include "closestream.h" +#include "exitcodes.h" struct taskset {pid_t pid;/* task PID */ @@ -236,7 +237,7 @@ int main(int argc, char **argv)if (!pid) { argv += optind +1 ;execvp (argv[0 ], argv); -err (EXIT_FAILURE ,_ ("failed to execute %s" ), argv[0 ]); +err (EX_EXEC ,_ ("failed to execute %s" ), argv[0 ]); }return EXIT_SUCCESS ;sys-utils/flock.c +1
-0@@ -45,6 +45,7 @@#include "closestream.h" #include "monotonic.h" #include "timer.h" +#include "exitcodes.h" static void __attribute__((__noreturn__))usage (int ex) {
sys-utils/flock.c int main(int argc, char *argv[]) · line 335
Quoted 6 lines
sys-utils/flock.c +2
-1@@ -330,7 +331,8 @@ int main(int argc, char *argv[])execvp (cmd_argv[0 ], cmd_argv);/* execvp() failed */ warn (_ ("failed to execute %s" ), cmd_argv[0 ]); -_exit ((errno ==ENOMEM ) ?EX_OSERR :EX_UNAVAILABLE ); +/* Behave mostly like the shell */ +_exit ((errno ==ENOMEM ) ?EX_OSERR :EX_EXEC );
This conflicts with flock(1) manual page. Either this change should not be done, or manual page needs update as well.
Quoted 186 lines
sys-utils/flock.c +0
-0}else { do { w = waitpid(f, &status, 0);sys-utils/nsenter.c +2
-1@@ -40,6 +40,7 @@#include "closestream.h" #include "namespace.h" #include "exec_shell.h" +#include "exitcodes.h" static struct namespace_file { int nstype;@@ -413,7 +414,7 @@ int main(int argc, char *argv[])if (optind < argc) {execvp (argv[optind], argv + optind); -err (EXIT_FAILURE ,_ ("failed to execute %s" ), argv[optind]); +err (EX_EXEC ,_ ("failed to execute %s" ), argv[optind]); }exec_shell (); }sys-utils/prlimit.c +2
-1@@ -35,6 +35,7 @@#include "strutils.h" #include "list.h" #include "closestream.h" +#include "exitcodes.h" #ifndef RLIMIT_RTTIME # define RLIMIT_RTTIME 15@@ -641,7 +642,7 @@ int main(int argc, char **argv)if (argc > optind) {/* prlimit [options] COMMAND */ execvp (argv[optind], &argv[optind]); -err (EXIT_FAILURE ,_ ("failed to execute %s" ), argv[optind]); +err (EX_EXEC ,_ ("failed to execute %s" ), argv[optind]); }return EXIT_SUCCESS ;sys-utils/setarch.c +4
-3@@ -35,6 +35,7 @@#include "nls.h" #include "c.h" #include "closestream.h" +#include "exitcodes.h" #ifndef HAVE_PERSONALITY # include <syscall.h> @@ -399,10 +400,10 @@ int main(int argc, char *argv[])if (!argc) {execl ("/bin/sh" ,"-sh" ,NULL ); -err (EXIT_FAILURE ,_ ("failed to execute %s" ),"/bin/sh" ); +err (EX_EXEC ,_ ("failed to execute %s" ),"/bin/sh" ); }execvp (argv[0 ], argv); -err (EXIT_FAILURE ,"%s" , argv[0 ]); -return EXIT_FAILURE ; +err (EX_EXEC ,"%s" , argv[0 ]); +return EX_EXEC ; }sys-utils/setpriv.c +2
-1@@ -38,6 +38,7 @@#include "strutils.h" #include "xalloc.h" #include "pathnames.h" +#include "exitcodes.h" #ifndef PR_SET_NO_NEW_PRIVS # define PR_SET_NO_NEW_PRIVS 38@@ -839,5 +840,5 @@ int main(int argc, char **argv)execvp (argv[optind], argv + optind); -err (EXIT_FAILURE ,_ ("cannot execute: %s" ), argv[optind]); +err (EX_EXEC ,_ ("cannot execute: %s" ), argv[optind]); }sys-utils/setsid.c +2
-1@@ -24,6 +24,7 @@#include "c.h" #include "nls.h" #include "closestream.h" +#include "exitcodes.h" static void __attribute__ ((__noreturn__))usage (FILE * out) {@@ -114,5 +115,5 @@ int main(int argc, char **argv)err (EXIT_FAILURE ,_ ("failed to set the controlling terminal" )); }execvp (argv[optind], argv + optind); -err (EXIT_FAILURE ,_ ("failed to execute %s" ), argv[optind]); +err (EX_EXEC ,_ ("failed to execute %s" ), argv[optind]); }sys-utils/swapon.c +2
-1@@ -23,6 +23,7 @@#include "strutils.h" #include "optutils.h" #include "closestream.h" +#include "exitcodes.h" #include "swapheader.h" #include "swapprober.h" @@ -337,7 +338,7 @@ static int swap_reinitialize(struct swap_device *dev) cmd[idx++] = dev->path; cmd[idx++] =NULL ;execvp (cmd[0 ], (char *const *) cmd); -err (EXIT_FAILURE ,_ ("failed to execute %s" ), cmd[0 ]); +err (EX_EXEC ,_ ("failed to execute %s" ), cmd[0 ]);default :/* parent */ do {sys-utils/unshare.c +2
-1@@ -40,6 +40,7 @@#include "xalloc.h" #include "pathnames.h" #include "all-io.h" +#include "exitcodes.h" /* synchronize parent and child by pipe */ #define PIPE_SYNC_BYTE 0x06@@ -458,7 +459,7 @@ int main(int argc, char *argv[])if (optind < argc) {execvp (argv[optind], argv + optind); -err (EXIT_FAILURE ,_ ("failed to execute %s" ), argv[optind]); +err (EX_EXEC ,_ ("failed to execute %s" ), argv[optind]); }exec_shell (); }tests/expected/misc/setarch-options +1
-1@@ -4,7 +4,7 @@ exit: 1 ###### unknown command Execute command `/das/gibs/nicht'. setarch: /das/gibs/nicht: No such file or directory -exit: 1 +exit: 127 ###### noop uname -a Execute command `uname'. uname -a unchangedtext-utils/more.c +2
-1@@ -65,6 +65,7 @@#include "xalloc.h" #include "widechar.h" #include "closestream.h" +#include "exitcodes.h" #include <regex.h> @@ -1622,7 +1623,7 @@ void execute(char *filename, char *cmd, ...)execvp (cmd, args);putserr (_ ("exec failed\n" )); -exit (EXIT_FAILURE ); +exit (EX_EXEC ); }if (id >0 ) {signal (SIGINT ,SIG_IGN );
Signature · 2 lines
Sami Kerola http://www.iki.fi/kerolasa/
Quoted 58 lines · Sami Kerola
Quoted 46 lines · Nir Soffer
Commands executing another command (e.g. taskset) should fail in a consistent and detectable way when the other command fail to execute. Change most of the commands that used to fail with
EXIT_FAILUREto fail with the special exit code 127, like the shell.For example, running the fuser command via taskset when fuser command is not installed is failing now like this:
$ taskset -c 1 fuser /path/to/file; echo $? taskset: failed to execute fuser: No such file or directory 1This failure looks like a real failure of the fuser command:
$ taskset -c 1 fuser /path/to/file; echo $? Specified filename /path/to/file does not exist. 1Based on the exit code, a management program may do the wrong thing, leading to catastrophic results. Detecting such failure requires fragile parsing of standard error.
With this patch this failure is easy to detect:
$ taskset -c 1 fuser /path/to/file; echo $? taskset: failed to execute fuser: No such file or directory 127The return code seems to be undocumented for most commands, except the su command, promising to exit with exit code 126 if a command cannot be executed, and 127 if a command was not found. However, the actual command always exit with exit code 1. This patch implement the documented behavior.
Signed-off-by Nir Soffer <[email protected]>--- disk-utils/mkfs.c | 3 ++- include/exitcodes.h | 7 +++++++ lib/pager.c | 3 ++- login-utils/su-common.c | 7 ++++++- schedutils/chrt.c | 3 ++- schedutils/ionice.c | 3 ++- schedutils/taskset.c | 3 ++- sys-utils/flock.c | 4 +++- sys-utils/nsenter.c | 3 ++- sys-utils/prlimit.c | 3 ++-Quoted 8 lines
sys-utils/setarch.c | 7 ++++--- sys-utils/setpriv.c | 3 ++- sys-utils/setsid.c | 3 ++- sys-utils/swapon.c | 3 ++- sys-utils/unshare.c | 3 ++- tests/expected/misc/setarch-options | 2 +- text-utils/more.c | 3 ++- 17 files changed, 45 insertions(+), 18 deletions(-)
Quoted 8 lines
Lots of change in a single patch. Could these change be split to something like:
1.
include/exitcodes.h
2.lib/pager.c
3. changes to commands
4. updates to tests
5. manual page updates
Sure, I can separate it like this.
Quoted 40 lines
Quoted 27 lines
disk-utils/mkfs.c +2
-1@@ -33,6 +33,7 @@#include "closestream.h" #include "nls.h" #include "xalloc.h" +#include "exitcodes.h" #ifndef DEFAULT_FSTYPE #define DEFAULT_FSTYPE "ext2"@@ -136,5 +137,5 @@ int main(int argc, char **argv)/* Execute the program */ execvp (progname, argv + optind); -err (EXIT_FAILURE ,_ ("failed to execute %s" ), progname); +err (EX_EXEC ,_ ("failed to execute %s" ), progname); }include/exitcodes.h +0
-0include/exitcodes.h line 41
Quoted 10 lines
include/exitcodes.h +7
-0@@ -32,4 +32,11 @@#define MOUNT_EX_FAIL 32/* mount failure */ #define MOUNT_EX_SOMEOK 64/* some mount succeeded */ +/* Exit codes use by the su program */ +#define SU_EX_EXEC 126/* faied to execute a program */ +#define SU_EX_NOENT 127/* program was not found */ + +/* General exit codes */ +#define EX_EXEC 127/* faied to execute a program */ +Looks like typo x2
Quoted 1 line
s/faied/failed/
Indeed
Quoted 122 lines
Quoted 122 lines
include/exitcodes.h +0
-0#endif /* UTIL_LINUX_EXITCODES_H */ lib/pager.c +2
-1@@ -17,6 +17,7 @@#include "c.h" #include "xalloc.h" #include "nls.h" +#include "exitcodes.h" #define NULL_DEVICE "/dev/null"@@ -73,7 +74,7 @@ static int start_command(struct child_process *cmd) cmd->preexec_cb ();execvp (cmd->argv[0 ], (char *const *) cmd->argv); -exit (127 );/* cmd not found */ +exit (EX_EXEC );/* cmd not found */ }if (cmd->pid <0 ) {login-utils/su-common.c +6
-1@@ -71,6 +71,7 @@ enum#include "closestream.h" #include "strutils.h" #include "ttyutils.h" +#include "exitcodes.h" /* name of the pam configuration files. separate configs for su and su - */ #define PAM_SRVNAME_SU "su"@@ -983,7 +984,11 @@ su_main (int argc, char **argv, int mode)run_shell (shell, command, argv + optind,max (0 , argc - optind)); else {execvp (argv[optind], &argv[optind]); -err (EXIT_FAILURE ,_ ("failed to execute %s" ), argv[optind]); +/* su(1) promise to fail with exit code 126 if the command +cannot be executed, and 127 if the command was not +found. */ +err (errno ==ENOENT ?SU_EX_NOENT :SU_EX_EXEC , +_ ("failed to execute %s" ), argv[optind]); } }schedutils/chrt.c +2
-1@@ -32,6 +32,7 @@#include "closestream.h" #include "strutils.h" #include "procutils.h" +#include "exitcodes.h" /* the SCHED_BATCH is supported since Linux 2.6.16 * -- temporary workaround for people with old glibc headers @@ -517,7 +518,7 @@ int main(int argc, char **argv)if (!ctl->pid) { argv += optind +1 ;execvp (argv[0 ], argv); -err (EXIT_FAILURE ,_ ("failed to execute %s" ), argv[0 ]); +err (EX_EXEC ,_ ("failed to execute %s" ), argv[0 ]); }return EXIT_SUCCESS ;schedutils/ionice.c +2
-1@@ -18,6 +18,7 @@#include "strutils.h" #include "c.h" #include "closestream.h" +#include "exitcodes.h" static int tolerant;@@ -257,7 +258,7 @@ int main(int argc, char **argv)*/ ioprio_setid (0 , ioclass, data,IOPRIO_WHO_PROCESS );execvp (argv[optind], &argv[optind]); -err (EXIT_FAILURE ,_ ("failed to execute %s" ), argv[optind]); +err (EX_EXEC ,_ ("failed to execute %s" ), argv[optind]); }else usage (stderr);schedutils/taskset.c +2
-1@@ -34,6 +34,7 @@#include "procutils.h" #include "c.h" #include "closestream.h" +#include "exitcodes.h" struct taskset {pid_t pid;/* task PID */ @@ -236,7 +237,7 @@ int main(int argc, char **argv)if (!pid) { argv += optind +1 ;execvp (argv[0 ], argv); -err (EXIT_FAILURE ,_ ("failed to execute %s" ), argv[0 ]); +err (EX_EXEC ,_ ("failed to execute %s" ), argv[0 ]); }return EXIT_SUCCESS ;sys-utils/flock.c +1
-0@@ -45,6 +45,7 @@#include "closestream.h" #include "monotonic.h" #include "timer.h" +#include "exitcodes.h"
Quoted 12 lines
sys-utils/flock.c int main(int argc, char *argv[]) · line 335
Quoted 9 lines
sys-utils/flock.c +2
-1static void __attribute__((__noreturn__))usage (int ex) {@@ -330,7 +331,8 @@ int main(int argc, char *argv[])execvp (cmd_argv[0 ], cmd_argv);/* execvp() failed */ warn (_ ("failed to execute %s" ), cmd_argv[0 ]); -_exit ((errno ==ENOMEM ) ?EX_OSERR :EX_UNAVAILABLE ); +/* Behave mostly like the shell */ +_exit ((errno ==ENOMEM ) ?EX_OSERR :EX_EXEC );This conflicts with flock(1) manual page. Either this change should not be done, or manual page needs update as well.
The manual is not very clear about the exit status:
EXIT STATUS
The command uses sysexits.h return values for everything,
except when using
either of the options -n or -w which report a failure to
acquire the lock with a
return value given by the -E option, or 1 by default.
Based on this, you can check for errors 64-78 (or on the values defined in your system).
I'll undo this change for now, in case someone is depending on this range.
Quoted 180 lines
Quoted 180 lines
sys-utils/flock.c +0
-0}else { do { w = waitpid(f, &status, 0);sys-utils/nsenter.c +2
-1@@ -40,6 +40,7 @@#include "closestream.h" #include "namespace.h" #include "exec_shell.h" +#include "exitcodes.h" static struct namespace_file { int nstype;@@ -413,7 +414,7 @@ int main(int argc, char *argv[])if (optind < argc) {execvp (argv[optind], argv + optind); -err (EXIT_FAILURE ,_ ("failed to execute %s" ), argv[optind]); +err (EX_EXEC ,_ ("failed to execute %s" ), argv[optind]); }exec_shell (); }sys-utils/prlimit.c +2
-1@@ -35,6 +35,7 @@#include "strutils.h" #include "list.h" #include "closestream.h" +#include "exitcodes.h" #ifndef RLIMIT_RTTIME # define RLIMIT_RTTIME 15@@ -641,7 +642,7 @@ int main(int argc, char **argv)if (argc > optind) {/* prlimit [options] COMMAND */ execvp (argv[optind], &argv[optind]); -err (EXIT_FAILURE ,_ ("failed to execute %s" ), argv[optind]); +err (EX_EXEC ,_ ("failed to execute %s" ), argv[optind]); }return EXIT_SUCCESS ;sys-utils/setarch.c +4
-3@@ -35,6 +35,7 @@#include "nls.h" #include "c.h" #include "closestream.h" +#include "exitcodes.h" #ifndef HAVE_PERSONALITY # include <syscall.h> @@ -399,10 +400,10 @@ int main(int argc, char *argv[])if (!argc) {execl ("/bin/sh" ,"-sh" ,NULL ); -err (EXIT_FAILURE ,_ ("failed to execute %s" ),"/bin/sh" ); +err (EX_EXEC ,_ ("failed to execute %s" ),"/bin/sh" ); }execvp (argv[0 ], argv); -err (EXIT_FAILURE ,"%s" , argv[0 ]); -return EXIT_FAILURE ; +err (EX_EXEC ,"%s" , argv[0 ]); +return EX_EXEC ; }sys-utils/setpriv.c +2
-1@@ -38,6 +38,7 @@#include "strutils.h" #include "xalloc.h" #include "pathnames.h" +#include "exitcodes.h" #ifndef PR_SET_NO_NEW_PRIVS # define PR_SET_NO_NEW_PRIVS 38@@ -839,5 +840,5 @@ int main(int argc, char **argv)execvp (argv[optind], argv + optind); -err (EXIT_FAILURE ,_ ("cannot execute: %s" ), argv[optind]); +err (EX_EXEC ,_ ("cannot execute: %s" ), argv[optind]); }sys-utils/setsid.c +2
-1@@ -24,6 +24,7 @@#include "c.h" #include "nls.h" #include "closestream.h" +#include "exitcodes.h" static void __attribute__ ((__noreturn__))usage (FILE * out) {@@ -114,5 +115,5 @@ int main(int argc, char **argv)err (EXIT_FAILURE ,_ ("failed to set the controlling terminal" )); }execvp (argv[optind], argv + optind); -err (EXIT_FAILURE ,_ ("failed to execute %s" ), argv[optind]); +err (EX_EXEC ,_ ("failed to execute %s" ), argv[optind]); }sys-utils/swapon.c +2
-1@@ -23,6 +23,7 @@#include "strutils.h" #include "optutils.h" #include "closestream.h" +#include "exitcodes.h" #include "swapheader.h" #include "swapprober.h" @@ -337,7 +338,7 @@ static int swap_reinitialize(struct swap_device *dev) cmd[idx++] = dev->path; cmd[idx++] =NULL ;execvp (cmd[0 ], (char *const *) cmd); -err (EXIT_FAILURE ,_ ("failed to execute %s" ), cmd[0 ]); +err (EX_EXEC ,_ ("failed to execute %s" ), cmd[0 ]);default :/* parent */ do {sys-utils/unshare.c +2
-1@@ -40,6 +40,7 @@#include "xalloc.h" #include "pathnames.h" #include "all-io.h" +#include "exitcodes.h" /* synchronize parent and child by pipe */ #define PIPE_SYNC_BYTE 0x06@@ -458,7 +459,7 @@ int main(int argc, char *argv[])if (optind < argc) {execvp (argv[optind], argv + optind); -err (EXIT_FAILURE ,_ ("failed to execute %s" ), argv[optind]); +err (EX_EXEC ,_ ("failed to execute %s" ), argv[optind]); }exec_shell (); }tests/expected/misc/setarch-options +1
-1@@ -4,7 +4,7 @@ exit: 1 ###### unknown command Execute command `/das/gibs/nicht'. setarch: /das/gibs/nicht: No such file or directory -exit: 1 +exit: 127 ###### noop uname -a Execute command `uname'. uname -a unchangedtext-utils/more.c +1
-0@@ -65,6 +65,7 @@#include "xalloc.h" #include "widechar.h" #include "closestream.h" +#include "exitcodes.h" #include <regex.h> @@ -1622,7 +1623,7 @@ void execute(char *filename, char *cmd, ...)
Quoted 12 lines
text-utils/more.c void execute(char *filename, char *cmd, ...) · line 1629
Quoted 8 lines
text-utils/more.c +1
-1execvp (cmd, args);putserr (_ ("exec failed\n" )); -exit (EXIT_FAILURE ); +exit (EX_EXEC ); }if (id >0 ) {signal (SIGINT ,SIG_IGN );--
Sami Kerola
http://www.iki.fi/kerolasa/
Thanks for the review.
Cheers,
Nir
Quoted 17 lines · Nir Soffer
For example, running the fuser command via taskset when fuser command is not installed is failing now like this:
$ taskset -c 1 fuser /path/to/file; echo $? taskset: failed to execute fuser: No such file or directory 1This failure looks like a real failure of the fuser command:
$ taskset -c 1 fuser /path/to/file; echo $? Specified filename /path/to/file does not exist. 1Based on the exit code, a management program may do the wrong thing, leading to catastrophic results. Detecting such failure requires fragile parsing of standard error.
Quoted 5 lines
With this patch this failure is easy to detect:
$ taskset -c 1 fuser /path/to/file; echo $? taskset: failed to execute fuser: No such file or directory 127
TBH I don't like this idea; how would you see from the exit code that 'taskset' itself wasn't the culprit? I mean execve()ing taskset worked, but that tool simply wasn't lucky to execve() the secondary tool, well. So EXIT_FAILURE is the right thing to do IMO, and it is like that in almost any other - also non-util-linux tools (chroot, nice, ...). Therefore, I don't see much gain from changing this here - not to mention the probable fallout for tools/script relying on the previous EXIT_FAILURE.
Can you reference any general discussion about this?
Have a nice day,
Berny
Quoted 20 lines · Bernhard Voelker
Quoted 19 lines · Nir Soffer
For example, running the fuser command via taskset when fuser command is not installed is failing now like this:
$ taskset -c 1 fuser /path/to/file; echo $? taskset: failed to execute fuser: No such file or directory 1This failure looks like a real failure of the fuser command:
$ taskset -c 1 fuser /path/to/file; echo $? Specified filename /path/to/file does not exist. 1Based on the exit code, a management program may do the wrong thing, leading to catastrophic results. Detecting such failure requires fragile parsing of standard error.
With this patch this failure is easy to detect:
Quoted 8 lines
Quoted 3 lines
$ taskset -c 1 fuser /path/to/file; echo $? taskset: failed to execute fuser: No such file or directory 127TBH I don't like this idea; how would you see from the exit code that 'taskset' itself wasn't the culprit? I mean execve()ing taskset worked, but that tool simply wasn't lucky to
execve()the secondary tool, well.
So taskset will exit with 127, which is not an error code you expect from the underlying tool?
Quoted 3 lines
So
EXIT_FAILUREis the right thing to do IMO,
and it is like that in almost any other - also non-util-linux tools (chroot, nice, ...).
Good examples:
$ nice -n 5 bleep 1; echo $? nice: bleep: No such file or directory 127
$ sudo chroot /tmp bleep 1; echo $? chroot: failed to run command ‘bleep’: No such file or directory 127
This is the behavior I suggest to adapt.
Quoted 3 lines
Therefore, I don't see much gain from changing
this here - not to mention the probable fallout fortools/scriptrelying on the previousEXIT_FAILURE.
Tools cannot depend on EXIT_FAILURE from command like taskset, since they are interested in the underlying tool exit code.
For example, if a tool is depending on EXIT_FAILURE exit code from taskset while running fuser command, this tool is broken, ignoring the possible EXIT_FAILURE from fuser - which has very specific semantics.
Quoted 1 line
Can you reference any general discussion about this?
No
Quoted 3 lines
Have a nice day,
Berny
Cheers,
Nir
Quoted 44 lines · Nir Soffer
Quoted 20 lines · Bernhard Voelker
Quoted 19 lines · Nir Soffer
For example, running the fuser command via taskset when fuser command is not installed is failing now like this:
$ taskset -c 1 fuser /path/to/file; echo $? taskset: failed to execute fuser: No such file or directory 1This failure looks like a real failure of the fuser command:
$ taskset -c 1 fuser /path/to/file; echo $? Specified filename /path/to/file does not exist. 1Based on the exit code, a management program may do the wrong thing, leading to catastrophic results. Detecting such failure requires fragile parsing of standard error.
With this patch this failure is easy to detect:
Quoted 8 lines
Quoted 3 lines
$ taskset -c 1 fuser /path/to/file; echo $? taskset: failed to execute fuser: No such file or directory 127TBH I don't like this idea; how would you see from the exit code that 'taskset' itself wasn't the culprit? I mean execve()ing taskset worked, but that tool simply wasn't lucky to
execve()the secondary tool, well.So taskset will exit with 127, which is not an error code you expect from the underlying tool?
Quoted 3 lines
So
EXIT_FAILUREis the right thing to do IMO,
and it is like that in almost any other - also non-util-linux tools (chroot, nice, ...).Good examples:
$ nice -n 5 bleep 1; echo $? nice: bleep: No such file or directory 127
Quoted 5 lines
$ sudo chroot /tmp bleep 1; echo $? chroot: failed to run command ‘bleep’: No such file or directory 127This is the behavior I suggest to adapt.
Looking in coreutils, it is using these exit codes:
/* Exit statuses for programs like 'env' that exec other programs. */ enum
{
EXIT_TIMEDOUT = 124, /* Time expired before child completed. */ EXIT_CANCELED = 125, /* Internal error prior to exec attempt. */ EXIT_CANNOT_INVOKE = 126, /* Program located, but not usable. */ EXIT_ENOENT = 127 /* Could not find program to exec. */
};
So we may adapt this behavior?
Quoted 22 lines
Quoted 4 lines
Therefore, I don't see much gain from changing
this here - not to mention the probable fallout fortools/scriptrelying on the previousEXIT_FAILURE.Tools cannot depend on
EXIT_FAILUREfrom command like taskset, since they are interested in the underlying tool exit code.For example, if a tool is depending on
EXIT_FAILUREexit code from taskset while running fuser command, this tool is broken, ignoring the possibleEXIT_FAILUREfrom fuser - which has very specific semantics.Quoted 1 line
Can you reference any general discussion about this?
No
Quoted 3 lines
Have a nice day,
BernyCheers,
Nir
Quoted 62 lines · Nir Soffer
Quoted 44 lines · Nir Soffer
Quoted 20 lines · Bernhard Voelker
Quoted 19 lines · Nir Soffer
For example, running the fuser command via taskset when fuser command is not installed is failing now like this:
$ taskset -c 1 fuser /path/to/file; echo $? taskset: failed to execute fuser: No such file or directory 1This failure looks like a real failure of the fuser command:
$ taskset -c 1 fuser /path/to/file; echo $? Specified filename /path/to/file does not exist. 1Based on the exit code, a management program may do the wrong thing, leading to catastrophic results. Detecting such failure requires fragile parsing of standard error.
With this patch this failure is easy to detect:
Quoted 8 lines
Quoted 3 lines
$ taskset -c 1 fuser /path/to/file; echo $? taskset: failed to execute fuser: No such file or directory 127TBH I don't like this idea; how would you see from the exit code that 'taskset' itself wasn't the culprit? I mean execve()ing taskset worked, but that tool simply wasn't lucky to
execve()the secondary tool, well.So taskset will exit with 127, which is not an error code you expect from the underlying tool?
Quoted 3 lines
So
EXIT_FAILUREis the right thing to do IMO,
and it is like that in almost any other - also non-util-linux tools (chroot, nice, ...).Good examples:
$ nice -n 5 bleep 1; echo $? nice: bleep: No such file or directory 127Quoted 5 lines
$ sudo chroot /tmp bleep 1; echo $? chroot: failed to run command ‘bleep’: No such file or directory 127This is the behavior I suggest to adapt.
Looking in coreutils, it is using these exit codes:
/* Exit statuses for programs like 'env' that exec other programs. */ enum
{
EXIT_TIMEDOUT= 124, /* Time expired before child completed. */EXIT_CANCELED= 125, /* Internal error prior to exec attempt. */EXIT_CANNOT_INVOKE= 126, /* Program located, but not usable. */EXIT_ENOENT= 127 /* Could not find program to exec. */
};
Quoted 1 line
So we may adapt this behavior?
Good examples, indeed. I really should've looked that up myself before replying, sorry. Thanks for correcting me. So your patch actually makes util-linux more consistent with other tools ... which is good.
Have a nice day,
Berny
Karel Zak
maintainerQuoted 6 lines · Bernhard Voelker
Quoted 1 line
So we may adapt this behavior?
Good examples, indeed. I really should've looked that up myself before replying, sorry. Thanks for correcting me. So your patch actually makes util-linux more consistent with other tools ... which is good.
I agree. Please, send updated version of the patch. Thanks.
Karel
Signature · 2 lines
Karel Zak <[email protected]> http://karelzak.blogspot.com