Move file-posix's helper to add a flag (or a set of flags) to an FD's existing set of flags into osdep.c for other places to use.
Suggested-by: Eric Blake <ebl...@redhat.com> Signed-off-by: Hanna Czenczek <hre...@redhat.com> --- include/qemu/osdep.h | 1 + block/file-posix.c | 17 +---------------- util/osdep.c | 18 ++++++++++++++++++ 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h index 96fe51bc39..ff11070c7e 100644 --- a/include/qemu/osdep.h +++ b/include/qemu/osdep.h @@ -619,6 +619,7 @@ int qemu_lock_fd(int fd, int64_t start, int64_t len, bool exclusive); int qemu_unlock_fd(int fd, int64_t start, int64_t len); int qemu_lock_fd_test(int fd, int64_t start, int64_t len, bool exclusive); bool qemu_has_ofd_lock(void); +int qemu_fcntl_addfl(int fd, int flag); #endif bool qemu_has_direct_io(void); diff --git a/block/file-posix.c b/block/file-posix.c index 9b5f08ccb2..045e94d54d 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -1047,21 +1047,6 @@ static int raw_handle_perm_lock(BlockDriverState *bs, return ret; } -/* Sets a specific flag */ -static int fcntl_setfl(int fd, int flag) -{ - int flags; - - flags = fcntl(fd, F_GETFL); - if (flags == -1) { - return -errno; - } - if (fcntl(fd, F_SETFL, flags | flag) == -1) { - return -errno; - } - return 0; -} - static int raw_reconfigure_getfd(BlockDriverState *bs, int flags, int *open_flags, uint64_t perm, Error **errp) { @@ -1100,7 +1085,7 @@ static int raw_reconfigure_getfd(BlockDriverState *bs, int flags, /* dup the original fd */ fd = qemu_dup(s->fd); if (fd >= 0) { - ret = fcntl_setfl(fd, *open_flags); + ret = qemu_fcntl_addfl(fd, *open_flags); if (ret) { qemu_close(fd); fd = -1; diff --git a/util/osdep.c b/util/osdep.c index 770369831b..000e7daac8 100644 --- a/util/osdep.c +++ b/util/osdep.c @@ -280,6 +280,24 @@ int qemu_lock_fd_test(int fd, int64_t start, int64_t len, bool exclusive) return fl.l_type == F_UNLCK ? 0 : -EAGAIN; } } + +/** + * Set the given flag(s) (fcntl GETFL/SETFL) on the given FD, while retaining + * other flags. + */ +int qemu_fcntl_addfl(int fd, int flag) +{ + int flags; + + flags = fcntl(fd, F_GETFL); + if (flags == -1) { + return -errno; + } + if (fcntl(fd, F_SETFL, flags | flag) == -1) { + return -errno; + } + return 0; +} #endif bool qemu_has_direct_io(void) -- 2.49.0