[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 2/8] util: split off a helper for dealing with O_CLOEXEC flag
From: |
Daniel P . Berrangé |
Subject: |
[PULL 2/8] util: split off a helper for dealing with O_CLOEXEC flag |
Date: |
Wed, 16 Sep 2020 10:46:59 +0100 |
We're going to have multiple callers to open() from qemu_open()
soon. Readability would thus benefit from having a helper for
dealing with O_CLOEXEC.
Reviewed-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
util/osdep.c | 23 +++++++++++++++--------
1 file changed, 15 insertions(+), 8 deletions(-)
diff --git a/util/osdep.c b/util/osdep.c
index 3d94b4d732..0d8fa9f016 100644
--- a/util/osdep.c
+++ b/util/osdep.c
@@ -279,6 +279,20 @@ int qemu_lock_fd_test(int fd, int64_t start, int64_t len,
bool exclusive)
}
#endif
+static int qemu_open_cloexec(const char *name, int flags, mode_t mode)
+{
+ int ret;
+#ifdef O_CLOEXEC
+ ret = open(name, flags | O_CLOEXEC, mode);
+#else
+ ret = open(name, flags, mode);
+ if (ret >= 0) {
+ qemu_set_cloexec(ret);
+ }
+#endif
+ return ret;
+}
+
/*
* Opens a file with FD_CLOEXEC set
*/
@@ -318,14 +332,7 @@ int qemu_open(const char *name, int flags, ...)
va_end(ap);
}
-#ifdef O_CLOEXEC
- ret = open(name, flags | O_CLOEXEC, mode);
-#else
- ret = open(name, flags, mode);
- if (ret >= 0) {
- qemu_set_cloexec(ret);
- }
-#endif
+ ret = qemu_open_cloexec(name, flags, mode);
#ifdef O_DIRECT
if (ret == -1 && errno == EINVAL && (flags & O_DIRECT)) {
--
2.26.2
- [PULL 0/8] Block odirect patches, Daniel P . Berrangé, 2020/09/16
- [PULL 1/8] monitor: simplify functions for getting a dup'd fdset entry, Daniel P . Berrangé, 2020/09/16
- [PULL 2/8] util: split off a helper for dealing with O_CLOEXEC flag,
Daniel P . Berrangé <=
- [PULL 4/8] util: refactor qemu_open_old to split off variadic args handling, Daniel P . Berrangé, 2020/09/16
- [PULL 3/8] util: rename qemu_open() to qemu_open_old(), Daniel P . Berrangé, 2020/09/16
- [PULL 5/8] util: add Error object for qemu_open_internal error reporting, Daniel P . Berrangé, 2020/09/16
- [PULL 6/8] util: introduce qemu_open and qemu_create with error reporting, Daniel P . Berrangé, 2020/09/16
- [PULL 7/8] util: give a specific error message when O_DIRECT doesn't work, Daniel P . Berrangé, 2020/09/16
- [PULL 8/8] block/file: switch to use qemu_open/qemu_create for improved errors, Daniel P . Berrangé, 2020/09/16
- Re: [PULL 0/8] Block odirect patches, Peter Maydell, 2020/09/17