diff options
| author | Karel Zak <kzak@redhat.com> | 2025-01-28 13:13:02 +0100 |
|---|---|---|
| committer | Karel Zak <kzak@redhat.com> | 2025-02-03 12:34:41 +0100 |
| commit | 8369f2a7c46df5b4813f39c5f2f1e60c35500f75 (patch) | |
| tree | cfe3cd1f4576971a0434db7c2088f9c2991c370c | |
| parent | 14233b28c0169d89d224065c805368893a097a6b (diff) | |
| download | util-linux-8369f2a7c46df5b4813f39c5f2f1e60c35500f75.tar.gz | |
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 <kzak@redhat.com>
| -rw-r--r-- | libmount/src/context.c | 33 | ||||
| -rw-r--r-- | libmount/src/hook_mount.c | 21 | ||||
| -rw-r--r-- | libmount/src/mountP.h | 1 |
3 files changed, 35 insertions, 20 deletions
diff --git a/libmount/src/context.c b/libmount/src/context.c index a3bc33ab93..3c1258882a 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 e128c4a680..1ffd19e836 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 47007d6c8c..a4ce7c0426 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)); |
