aboutsummaryrefslogtreecommitdiffstats
path: root/src/process.c
diff options
context:
space:
mode:
authorverdant <im@verdant.ee>2026-08-06 14:52:03 +0800
committerverdant <im@verdant.ee>2026-08-06 14:52:03 +0800
commitb87d0c4a36c2cfb3d79b09838b88fcab1a8d07f3 (patch)
tree93d9da04b2a7eead52d3f83bf0c3ed41f4e163f4 /src/process.c
parent1e1cb4e2b948ce7d54999c7f62a444ccb41759c7 (diff)
downloadsf-b87d0c4a36c2cfb3d79b09838b88fcab1a8d07f3.tar.gz
sf-b87d0c4a36c2cfb3d79b09838b88fcab1a8d07f3.zip
Introduce error code system
Refactor spawn function, use return error code, provide more clearly error handling.
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;
}