systemf(): simplify further, exited OK or was signaled

Coverity highlighted a problem with logically dead code from the change
prior that triggered this refactor.  When system() returns we may have
exited either normally or due to a signal.

My reasoning behind systemf() is to allow users to treat external calls
as any other syscall, abstracting away the WIFFOO() macros.  This leaves
us with either the return value of the external program >= 0, or -1 and
errno for everything else.  I.e., if fork() or alloca() fails we may get
ENOMEM and if the program doesn't catch signals properly, EINTR.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg
2021-03-27 08:48:09 +01:00
parent 748948b665
commit dc5c4ae36c
+11 -20
View File
@@ -25,8 +25,8 @@ int systemf(const char *fmt, ...)
{
va_list ap;
char *cmd;
int len, status;
int rc = -1;
int len;
int rc;
va_start(ap, fmt);
len = vsnprintf(NULL, 0, fmt, ap);
@@ -42,26 +42,17 @@ int systemf(const char *fmt, ...)
vsnprintf(cmd, len, fmt, ap);
va_end(ap);
status = system(cmd);
if (status == -1)
rc = system(cmd);
if (rc == -1)
return -1;
if (!WIFEXITED(status)) {
if (WIFSIGNALED(status) &&
(WTERMSIG(status) == SIGINT ||
WTERMSIG(status) == SIGQUIT)) {
errno = EINTR;
rc = -1;
} else if (!rc) {
/*
* Alert callee that command didn't complete
* successfully. Some programs don't change
* their exit code when signaled.
*/
rc = 1;
}
} else
rc = WEXITSTATUS(status);
if (WIFEXITED(rc)) {
errno = 0;
rc = WEXITSTATUS(rc);
} else if (WIFSIGNALED(rc)) {
errno = EINTR;
rc = -1;
}
return rc;
}