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>
This commit is contained in:
parent
60efffa41b
commit
c2069ff624
23
util/osdep.c
23
util/osdep.c
|
@ -279,6 +279,20 @@ int qemu_lock_fd_test(int fd, int64_t start, int64_t len, bool exclusive)
|
||||||
}
|
}
|
||||||
#endif
|
#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
|
* Opens a file with FD_CLOEXEC set
|
||||||
*/
|
*/
|
||||||
|
@ -318,14 +332,7 @@ int qemu_open(const char *name, int flags, ...)
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef O_CLOEXEC
|
ret = qemu_open_cloexec(name, flags, mode);
|
||||||
ret = open(name, flags | O_CLOEXEC, mode);
|
|
||||||
#else
|
|
||||||
ret = open(name, flags, mode);
|
|
||||||
if (ret >= 0) {
|
|
||||||
qemu_set_cloexec(ret);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef O_DIRECT
|
#ifdef O_DIRECT
|
||||||
if (ret == -1 && errno == EINVAL && (flags & O_DIRECT)) {
|
if (ret == -1 && errno == EINVAL && (flags & O_DIRECT)) {
|
||||||
|
|
Loading…
Reference in New Issue