From 8eb925b14a34d65e5e33861ea4aa1f565fb3d7ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= Date: Thu, 5 Oct 2023 13:18:15 +0200 Subject: [PATCH 1/3] libmount: change syscall status macros to be functions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Functions are easier to modify and reason about. Signed-off-by: Thomas Weißschuh --- libmount/src/mountP.h | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/libmount/src/mountP.h b/libmount/src/mountP.h index 339e2761a53..de44c064ab0 100644 --- a/libmount/src/mountP.h +++ b/libmount/src/mountP.h @@ -479,22 +479,24 @@ struct libmnt_context /* Flags usable with MS_BIND|MS_REMOUNT */ #define MNT_BIND_SETTABLE (MS_NOSUID|MS_NODEV|MS_NOEXEC|MS_NOATIME|MS_NODIRATIME|MS_RELATIME|MS_RDONLY|MS_NOSYMFOLLOW) -#define set_syscall_status(_cxt, _name, _x) __extension__ ({ \ - if (!(_x)) { \ - DBG(CXT, ul_debug("syscall '%s' [%m]", _name)); \ - (_cxt)->syscall_status = -errno; \ - (_cxt)->syscall_name = (_name); \ - } else { \ - DBG(CXT, ul_debug("syscall '%s' [success]", _name)); \ - (_cxt)->syscall_status = 0; \ - } \ - }) - -#define reset_syscall_status(_cxt) __extension__ ({ \ - DBG(CXT, ul_debug("reset syscall status")); \ - (_cxt)->syscall_status = 0; \ - (_cxt)->syscall_name = NULL; \ - }) +static inline void set_syscall_status(struct libmnt_context *cxt, const char *name, int x) +{ + if (!x) { + DBG(CXT, ul_debug("syscall '%s' [%m]", name)); + cxt->syscall_status = -errno; + cxt->syscall_name = name; + } else { + DBG(CXT, ul_debug("syscall '%s' [success]", name)); + cxt->syscall_status = 0; + } +} + +static inline void reset_syscall_status(struct libmnt_context *cxt) +{ + DBG(CXT, ul_debug("reset syscall status")); + cxt->syscall_status = 0; + cxt->syscall_name = NULL; +} /* optmap.c */ extern const struct libmnt_optmap *mnt_optmap_get_entry( From 2d5e138e8c28e89fea8e3e960ca863787bb6fe91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= Date: Thu, 5 Oct 2023 13:19:14 +0200 Subject: [PATCH 2/3] libmount: add helper to log mount messages as emitted by kernel MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The kernel helpfully provides feedback about failed operations via the filesystem descriptor. Read that information and expose it via libmounts debug facilities. Signed-off-by: Thomas Weißschuh --- libmount/src/hook_mount.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/libmount/src/hook_mount.c b/libmount/src/hook_mount.c index 4b2a534f74e..2e0b5f0faca 100644 --- a/libmount/src/hook_mount.c +++ b/libmount/src/hook_mount.c @@ -64,6 +64,31 @@ static void close_sysapi_fds(struct libmnt_sysapi *api) api->fd_tree = api->fd_fs = -1; } +static void debug_fs_fd_messages(int fd) +{ + uint8_t buf[BUFSIZ]; + int rc; + + while ((rc = read(fd, buf, sizeof(buf))) != -1) { + if (rc > 0 && buf[rc - 1] == '\n') + buf[rc - 1] = '\0'; + DBG(CXT, ul_debug("message from kernel: %*s", rc, buf)); + } +} + +static void set_syscall_status_cxt_log(struct libmnt_context *cxt, + const char *name, int x) +{ + struct libmnt_sysapi *api; + + set_syscall_status(cxt, name, x); + + if (!x) { + api = get_sysapi(cxt); + debug_fs_fd_messages(api->fd_fs); + } +} + /* * This hookset uses 'struct libmnt_sysapi' (mountP.h) as hookset data. */ From 2f0e92b634ebac2feef1eb873d5e5ade7ebab834 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= Date: Thu, 5 Oct 2023 13:21:23 +0200 Subject: [PATCH 3/3] libmount: report all kernel messages for fd-based mount API MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Thomas Weißschuh --- libmount/src/hook_mount.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libmount/src/hook_mount.c b/libmount/src/hook_mount.c index 2e0b5f0faca..f343ab92476 100644 --- a/libmount/src/hook_mount.c +++ b/libmount/src/hook_mount.c @@ -89,6 +89,8 @@ static void set_syscall_status_cxt_log(struct libmnt_context *cxt, } } +#define set_syscall_status set_syscall_status_cxt_log + /* * This hookset uses 'struct libmnt_sysapi' (mountP.h) as hookset data. */