From c40235720ee0a742f4d8aca2333e1ccad726269a Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Wed, 22 May 2024 10:52:04 +0200 Subject: [PATCH] libmount: add functions to use error buffer * Add mnt_context_set_errmsg() and mnt_context_append_errmsg() functions. * Replace custom code with mnt_context_syscall_save_status() function. * Optionally use syscall name when generating error message. Signed-off-by: Karel Zak --- libmount/src/context.c | 31 +++++++++++++++++++++++++++++-- libmount/src/context_mount.c | 11 +++++++---- libmount/src/hook_mount.c | 8 ++------ libmount/src/hook_mount_legacy.c | 11 +++-------- libmount/src/mountP.h | 5 ++++- 5 files changed, 45 insertions(+), 21 deletions(-) diff --git a/libmount/src/context.c b/libmount/src/context.c index ef5da32a0d..7cf4ef79ed 100644 --- a/libmount/src/context.c +++ b/libmount/src/context.c @@ -2635,8 +2635,35 @@ void mnt_context_syscall_reset_status(struct libmnt_context *cxt) cxt->syscall_status = 0; cxt->syscall_name = NULL; - free(cxt->syscall_errmsg); - cxt->syscall_errmsg = NULL; + free(cxt->errmsg); + cxt->errmsg = NULL; +} + +int mnt_context_set_errmsg(struct libmnt_context *cxt, const char *msg) +{ + char *p = NULL; + + if (msg) { + p = strdup(msg); + if (!p) + return -ENOMEM; + } + + free(cxt->errmsg); + cxt->errmsg = p; + + return 0; +} + +int mnt_context_append_errmsg(struct libmnt_context *cxt, const char *msg) +{ + if (cxt->errmsg) { + int rc = strappend(&cxt->errmsg, "; "); + if (rc) + return rc; + } + + return strappend(&cxt->errmsg, msg); } /** diff --git a/libmount/src/context_mount.c b/libmount/src/context_mount.c index f9d610f778..783244a61c 100644 --- a/libmount/src/context_mount.c +++ b/libmount/src/context_mount.c @@ -1572,10 +1572,13 @@ int mnt_context_get_mount_excode( */ syserr = mnt_context_get_syscall_errno(cxt); - if (buf && cxt->syscall_errmsg) { - snprintf(buf, bufsz, _("%s system call failed: %s"), - cxt->syscall_name ? : "mount", - cxt->syscall_errmsg); + if (buf && cxt->errmsg) { + if (cxt->syscall_name) + snprintf(buf, bufsz, _("%s system call failed: %s"), + cxt->syscall_name, cxt->errmsg); + else + xstrncpy(buf, cxt->errmsg, bufsz); + return MNT_EX_FAIL; } diff --git a/libmount/src/hook_mount.c b/libmount/src/hook_mount.c index c85c92b983..6b7caff853 100644 --- a/libmount/src/hook_mount.c +++ b/libmount/src/hook_mount.c @@ -70,8 +70,7 @@ static void save_fd_messages(struct libmnt_context *cxt, int fd) uint8_t buf[BUFSIZ]; int rc; - free(cxt->syscall_errmsg); - cxt->syscall_errmsg = NULL; + mnt_context_set_errmsg(cxt, NULL); while ((rc = read(fd, buf, sizeof(buf))) != -1) { if (rc > 0 && buf[rc - 1] == '\n') @@ -80,10 +79,7 @@ static void save_fd_messages(struct libmnt_context *cxt, int fd) if (rc < 3 || strncmp((char *) buf, "e ", 2) != 0) continue; - if (cxt->syscall_errmsg) - strappend(&cxt->syscall_errmsg, "; "); - - strappend(&cxt->syscall_errmsg, ((char *) buf) + 2); + mnt_context_append_errmsg(cxt, ((char *) buf) + 2); } } diff --git a/libmount/src/hook_mount_legacy.c b/libmount/src/hook_mount_legacy.c index 7e62864673..df3a691e0d 100644 --- a/libmount/src/hook_mount_legacy.c +++ b/libmount/src/hook_mount_legacy.c @@ -76,10 +76,8 @@ static int hook_propagation(struct libmnt_context *cxt, if (rc) { /* Update global syscall status if only this function called */ - if (mnt_context_propagation_only(cxt)) { - cxt->syscall_status = -errno; - cxt->syscall_name = "mount"; - } + if (mnt_context_propagation_only(cxt)) + mnt_context_syscall_save_status(cxt, "mount", rc == 0); DBG(HOOK, ul_debugobj(hs, " mount(2) failed [errno=%d %m]", errno)); rc = -MNT_ERR_APPLYFLAGS; @@ -240,10 +238,7 @@ static int hook_mount(struct libmnt_context *cxt, options : "")); if (mount(src, target, type, flags, options)) { - cxt->syscall_status = -errno; - cxt->syscall_name = "mount"; - DBG(HOOK, ul_debugobj(hs, " mount(2) failed [errno=%d %m]", - -cxt->syscall_status)); + mnt_context_syscall_save_status(cxt, "mount", 0); rc = -cxt->syscall_status; return rc; } diff --git a/libmount/src/mountP.h b/libmount/src/mountP.h index 63a9675bbd..db58e9f749 100644 --- a/libmount/src/mountP.h +++ b/libmount/src/mountP.h @@ -432,7 +432,7 @@ struct libmnt_context int syscall_status; /* 1: not called yet, 0: success, <0: -errno */ const char *syscall_name; /* failed syscall name */ - char *syscall_errmsg; /* message from kernel */ + char *errmsg; /* library or kernel message */ struct libmnt_ns ns_orig; /* original namespace */ struct libmnt_ns ns_tgt; /* target namespace */ @@ -626,6 +626,9 @@ extern int mnt_context_update_tabs(struct libmnt_context *cxt); extern int mnt_context_umount_setopt(struct libmnt_context *cxt, int c, char *arg); extern int mnt_context_mount_setopt(struct libmnt_context *cxt, int c, char *arg); +extern int mnt_context_set_errmsg(struct libmnt_context *cxt, const char *msg); +extern int mnt_context_append_errmsg(struct libmnt_context *cxt, const char *msg); + extern int mnt_context_propagation_only(struct libmnt_context *cxt) __attribute__((nonnull)); -- 2.47.2