diff options
author | Timo Teräs <timo.teras@iki.fi> | 2023-09-18 16:11:08 +0300 |
---|---|---|
committer | Timo Teräs <timo.teras@iki.fi> | 2023-09-18 16:11:08 +0300 |
commit | 48d91f482eb48a0a107b714ee183bb7e07782e14 (patch) | |
tree | af4ed1966b3c6c10d76e4614dd430a44d7aa6be4 | |
parent | 80f571ab394c2fbb0130bb6ae95f831a37e29814 (diff) | |
download | apk-tools-48d91f482eb48a0a107b714ee183bb7e07782e14.tar.gz apk-tools-48d91f482eb48a0a107b714ee183bb7e07782e14.tar.bz2 apk-tools-48d91f482eb48a0a107b714ee183bb7e07782e14.tar.xz apk-tools-48d91f482eb48a0a107b714ee183bb7e07782e14.zip |
db, uvol, print: describe process exit status better
Print proper analysis instead of "exited with error 0"
fixes #10895
-rw-r--r-- | src/apk_print.h | 3 | ||||
-rw-r--r-- | src/database.c | 6 | ||||
-rw-r--r-- | src/fs_uvol.c | 15 | ||||
-rw-r--r-- | src/print.c | 16 |
4 files changed, 32 insertions, 8 deletions
diff --git a/src/apk_print.h b/src/apk_print.h index c3e254a..d42e15a 100644 --- a/src/apk_print.h +++ b/src/apk_print.h @@ -12,7 +12,10 @@ #include "apk_blob.h" +#define APK_EXIT_STATUS_MAX_SIZE 128 + const char *apk_error_str(int error); +int apk_exit_status_str(int status, char *buf, size_t sz); int apk_get_human_size_unit(apk_blob_t b); const char *apk_get_human_size(off_t size, off_t *dest); diff --git a/src/database.c b/src/database.c index 047f6f3..3873025 100644 --- a/src/database.c +++ b/src/database.c @@ -2019,6 +2019,7 @@ int apk_db_fire_triggers(struct apk_database *db) int apk_db_run_script(struct apk_database *db, char *fn, char **argv) { + char buf[APK_EXIT_STATUS_MAX_SIZE]; struct apk_out *out = &db->ctx->out; int status; pid_t pid; @@ -2049,8 +2050,9 @@ int apk_db_run_script(struct apk_database *db, char *fn, char **argv) exit(127); /* should not get here */ } while (waitpid(pid, &status, 0) < 0 && errno == EINTR); - if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { - apk_err(out, "%s: script exited with error %d", basename(fn), WEXITSTATUS(status)); + + if (apk_exit_status_str(status, buf, sizeof buf)) { + apk_err(out, "%s: script %s", basename(fn), buf); return -1; } return 0; diff --git a/src/fs_uvol.c b/src/fs_uvol.c index 292891e..26930a7 100644 --- a/src/fs_uvol.c +++ b/src/fs_uvol.c @@ -17,6 +17,7 @@ static int uvol_run(struct apk_ctx *ac, char *action, const char *volname, char *arg1, char *arg2) { + char buf[APK_EXIT_STATUS_MAX_SIZE]; struct apk_out *out = &ac->out; pid_t pid; int r, status; @@ -28,12 +29,13 @@ static int uvol_run(struct apk_ctx *ac, char *action, const char *volname, char r = posix_spawn(&pid, apk_ctx_get_uvol(ac), &act, 0, argv, environ); posix_spawn_file_actions_destroy(&act); if (r != 0) { - apk_err(out, "%s: uvol exec error: %s", volname, apk_error_str(r)); + apk_err(out, "%s: uvol run exec error: %s", volname, apk_error_str(r)); return r; } while (waitpid(pid, &status, 0) < 0 && errno == EINTR); - if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { - apk_err(out, "%s: uvol exited with error %d", volname, WEXITSTATUS(status)); + + if (apk_exit_status_str(status, buf, sizeof buf)) { + apk_err(out, "%s: uvol run %s", volname, buf); return -APKE_UVOL_ERROR; } return 0; @@ -42,6 +44,7 @@ static int uvol_run(struct apk_ctx *ac, char *action, const char *volname, char static int uvol_extract(struct apk_ctx *ac, const char *volname, char *arg1, off_t sz, struct apk_istream *is, apk_progress_cb cb, void *cb_ctx) { + char buf[APK_EXIT_STATUS_MAX_SIZE]; struct apk_out *out = &ac->out; struct apk_ostream *os; pid_t pid; @@ -70,11 +73,11 @@ static int uvol_extract(struct apk_ctx *ac, const char *volname, char *arg1, off } while (waitpid(pid, &status, 0) < 0 && errno == EINTR); - if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { - apk_err(out, "%s: uvol exited with error %d", volname, WEXITSTATUS(status)); + + if (apk_exit_status_str(status, buf, sizeof buf)) { + apk_err(out, "%s: uvol extract %s", volname, buf); return -APKE_UVOL_ERROR; } - return 0; } diff --git a/src/print.c b/src/print.c index 4ffc536..f34e35f 100644 --- a/src/print.c +++ b/src/print.c @@ -15,6 +15,7 @@ #include <errno.h> #include <sys/ioctl.h> #include <sys/types.h> +#include <sys/wait.h> #include "apk_defines.h" #include "apk_print.h" @@ -67,6 +68,21 @@ const char *apk_error_str(int error) } } +int apk_exit_status_str(int status, char *buf, size_t sz) +{ + if (WIFEXITED(status) && WEXITSTATUS(status) == 0) + return 0; + if (WIFEXITED(status)) + return snprintf(buf, sz, "exited with error %d", WEXITSTATUS(status)); + if (WIFSIGNALED(status)) + return snprintf(buf, sz, "killed by signal %d", WTERMSIG(status)); + if (WIFSTOPPED(status)) + return snprintf(buf, sz, "stopped by signal %d", WSTOPSIG(status)); + if (WIFCONTINUED(status)) + return snprintf(buf, sz, "continued"); + return snprintf(buf, sz, "status unknown %x", status); +} + static const char *size_units[] = {"B", "KiB", "MiB", "GiB", "TiB"}; int apk_get_human_size_unit(apk_blob_t b) |