aboutsummaryrefslogtreecommitdiff
path: root/util/osdep.c
diff options
context:
space:
mode:
authorDaniel P. Berrangé <berrange@redhat.com>2020-09-02 16:58:06 +0100
committerDaniel P. Berrangé <berrange@redhat.com>2020-09-16 10:33:48 +0100
commitc2069ff624eda7f94bc118d7b5825bce29d7ca94 (patch)
treef016348563fe56d16c6f75f1a9b0dbd48f08e6b8 /util/osdep.c
parent60efffa41b34cc299fed65b67e2c2641846d592b (diff)
downloadqemu-c2069ff624eda7f94bc118d7b5825bce29d7ca94.zip
qemu-c2069ff624eda7f94bc118d7b5825bce29d7ca94.tar.gz
qemu-c2069ff624eda7f94bc118d7b5825bce29d7ca94.tar.bz2
util: split off a helper for dealing with O_CLOEXEC flag
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>
Diffstat (limited to 'util/osdep.c')
-rw-r--r--util/osdep.c23
1 files changed, 15 insertions, 8 deletions
diff --git a/util/osdep.c b/util/osdep.c
index 3d94b4d..0d8fa9f 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)) {