aboutsummaryrefslogtreecommitdiffstats
path: root/src/process.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/process.c')
-rw-r--r--src/process.c36
1 files changed, 27 insertions, 9 deletions
diff --git a/src/process.c b/src/process.c
index 3dd93f0..e2fb6b3 100644
--- a/src/process.c
+++ b/src/process.c
@@ -1,6 +1,8 @@
#include "process.h"
#include "err.h"
#include "log.h"
+#include <errno.h>
+#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
@@ -8,21 +10,37 @@ int
spawn(char **argv)
{
pid_t pid;
+ int status;
if (!argv[0] || *argv[0] == '\0') {
- log_write(LOG_ERR, "Invalid argv");
- return -1;
+ return SF_ERR_INVALID_ARG;
}
if ((pid = fork()) < 0) {
- log_write(LOG_ERR, "fork error");
- } else if (pid == 0) {
- if (execvp(argv[0], argv) < 0) {
- die("exec error");
+ log_write(LOG_ERR, "fork error: %s", strerror(errno));
+ return -errno;
+ }
+
+ if (pid == 0) {
+ execvp(argv[0], argv);
+
+ _exit(errno == ENONET ? 127 : 126);
+ }
+
+ if (waitpid(pid, &status, 0) < 0) {
+ return -errno;
+ }
+
+ if (WIFEXITED(status)) {
+ int exit_code = WEXITSTATUS(status);
+ if (exit_code != 0) {
+ log_write(LOG_ERR, "Command failed with code %d",
+ exit_code);
+ return SF_ERR_EXEC_FAILED;
}
- } else {
- wait(NULL);
+ } else if (WIFSIGNALED(status)) {
+ return SF_ERR_SIGNALED;
}
- return 0;
+ return SF_OK;
}