From: Karel Zak Date: Tue, 28 Jan 2025 12:13:02 +0000 (+0100) Subject: libmount: add private mnt_context_read_mesgs() X-Git-Tag: v2.42-start~64^2~1 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=8369f2a7c46df5b4813f39c5f2f1e60c35500f75;p=thirdparty%2Futil-linux.git libmount: add private mnt_context_read_mesgs() Add a more generic function for reading messages from a file descriptor. The new mount API is already used in multiple files. Signed-off-by: Karel Zak --- diff --git a/libmount/src/context.c b/libmount/src/context.c index a3bc33ab9..3c1258882 100644 --- a/libmount/src/context.c +++ b/libmount/src/context.c @@ -2729,6 +2729,9 @@ void mnt_context_reset_mesgs(struct libmnt_context *cxt) int mnt_context_append_mesg(struct libmnt_context *cxt, const char *msg) { + if (msg) { + DBG(CXT, ul_debug("mesg: '%s'", msg)); + } return strv_extend(&cxt->mesgs, msg); } @@ -2744,6 +2747,36 @@ int mnt_context_sprintf_mesg(struct libmnt_context *cxt, const char *msg, ...) return rc; } +int mnt_context_read_mesgs(struct libmnt_context *cxt, int fd) +{ + uint8_t buf[BUFSIZ]; + ssize_t sz; + size_t count = 0; + int errsv = errno; + + if (fd < 0) + return 0; + + while ((sz = read(fd, buf, sizeof(buf) - 1)) != -1) { + + if (sz <= 0) + continue; + if (buf[sz - 1] == '\n') + buf[--sz] = '\0'; + else + buf[sz] = '\0'; + + if (!*buf) + continue; + + mnt_context_append_mesg(cxt, (char *) buf); + count++;; + } + + errno = errsv; + return count; +} + /** * mnt_context_get_nmesgs: * @cxt: mount context diff --git a/libmount/src/hook_mount.c b/libmount/src/hook_mount.c index e128c4a68..1ffd19e83 100644 --- a/libmount/src/hook_mount.c +++ b/libmount/src/hook_mount.c @@ -62,25 +62,6 @@ static void close_sysapi_fds(struct libmnt_sysapi *api) api->fd_tree = api->fd_fs = -1; } -static void save_fd_messages(struct libmnt_context *cxt, int fd) -{ - uint8_t buf[BUFSIZ]; - int rc; - - while ((rc = read(fd, buf, sizeof(buf) - 1)) != -1) { - - if (rc == 0) - continue; - if (buf[rc - 1] == '\n') - buf[--rc] = '\0'; - else - buf[rc] = '\0'; - - DBG(CXT, ul_debug("message from kernel: \"%*s\"", rc, buf)); - mnt_context_append_mesg(cxt, (char *) buf); - } -} - static void hookset_set_syscall_status(struct libmnt_context *cxt, const char *name, int x) { @@ -93,7 +74,7 @@ static void hookset_set_syscall_status(struct libmnt_context *cxt, api = get_sysapi(cxt); if (api && api->fd_fs >= 0) - save_fd_messages(cxt, api->fd_fs); + mnt_context_read_mesgs(cxt, api->fd_fs); } /* diff --git a/libmount/src/mountP.h b/libmount/src/mountP.h index 47007d6c8..a4ce7c042 100644 --- a/libmount/src/mountP.h +++ b/libmount/src/mountP.h @@ -684,6 +684,7 @@ extern int mnt_context_mount_setopt(struct libmnt_context *cxt, int c, char *arg extern void mnt_context_reset_mesgs(struct libmnt_context *cxt); extern int mnt_context_append_mesg(struct libmnt_context *cxt, const char *msg); extern int mnt_context_sprintf_mesg(struct libmnt_context *cxt, const char *msg, ...); +extern int mnt_context_read_mesgs(struct libmnt_context *cxt, int fd); extern int mnt_context_propagation_only(struct libmnt_context *cxt) __attribute__((nonnull));