block: improve error reporting for unsupported O_DIRECT

-----BEGIN PGP SIGNATURE-----
 
 iQIzBAABCAAdFiEE2vOm/bJrYpEtDo4/vobrtBUQT98FAl9h3swACgkQvobrtBUQ
 T98myQ/9HULnWb2N48utxP2DuqNW/YhKOIUJolJJajfRxb/lnkx/haQEyATJxnNn
 tKWmYYopXcrfDpR9VDqxHgyB7eh5UbJCTBm6TTJNMHNazNQCd6kikGbIshW3lgoH
 VQLWlQLJd9ptzdj+YRt6FgU11rpYEGd1fOGWbGDDKNwiRZfWjgqPI2X/Ks0ABRpl
 MdPpsRKr86BimZz3xWPORj4DtihiaT9qnuZ9x39mHvBm83Ko5sYbui04/+bFEjXt
 G0AtUIlLKRHwWnjQf4+WEUm3CAKpATSGgB0OdckXgdyx8Q6bmFTpaFXxXIuFTGlR
 73zqE5q7wFq5YjjYk/Hn0qKYaMzM/vm3TR5Ay2Aixj3TaPllTfQ3vEmwMiHW37eC
 8/274YJ/zC4NgZqaAwRDpStcB1Dwr8K1NJY5O4mi8XoIVJ0bK2iPmwr9jt6Na/BH
 Iy+SQ06e0IIaT9NXPYHT3ogaJRjebAebtQ1pzQ4iTYhfjxlsmymp9JV9FhR4dwua
 ZK+e/HHY+8pIXLVTc/RwWyoKQlGNl/z6KK0P2btaJCWQcbjs1AgxVYUva8EIHi92
 m6taSH3AxiDjdmFTo8JWIHLOR9PIyVGohi9zfOD/sFFH82tehsShxlYfd87UPQdZ
 Az5b/kViFJ7A1oM2sBaLeDqjBrt2cDjA7/SO1jQhPceP7DGM3Sg=
 =u7xc
 -----END PGP SIGNATURE-----

Merge remote-tracking branch 'remotes/berrange/tags/block-odirect-pull-request' into staging

block: improve error reporting for unsupported O_DIRECT

# gpg: Signature made Wed 16 Sep 2020 10:45:48 BST
# gpg:                using RSA key DAF3A6FDB26B62912D0E8E3FBE86EBB415104FDF
# gpg: Good signature from "Daniel P. Berrange <dan@berrange.com>" [full]
# gpg:                 aka "Daniel P. Berrange <berrange@redhat.com>" [full]
# Primary key fingerprint: DAF3 A6FD B26B 6291 2D0E  8E3F BE86 EBB4 1510 4FDF

* remotes/berrange/tags/block-odirect-pull-request:
  block/file: switch to use qemu_open/qemu_create for improved errors
  util: give a specific error message when O_DIRECT doesn't work
  util: introduce qemu_open and qemu_create with error reporting
  util: add Error object for qemu_open_internal error reporting
  util: refactor qemu_open_old to split off variadic args handling
  util: rename qemu_open() to qemu_open_old()
  util: split off a helper for dealing with O_CLOEXEC flag
  monitor: simplify functions for getting a dup'd fdset entry

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
This commit is contained in:
Peter Maydell 2020-09-16 16:25:30 +01:00
commit 525009d04f
27 changed files with 171 additions and 137 deletions

View File

@ -2013,7 +2013,7 @@ static int kvm_init(MachineState *ms)
#endif
QLIST_INIT(&s->kvm_parked_vcpus);
s->vmfd = -1;
s->fd = qemu_open("/dev/kvm", O_RDWR);
s->fd = qemu_open_old("/dev/kvm", O_RDWR);
if (s->fd == -1) {
fprintf(stderr, "Could not access KVM kernel module: %m\n");
ret = -errno;

View File

@ -75,7 +75,7 @@ static void rng_random_opened(RngBackend *b, Error **errp)
error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
"filename", "a valid filename");
} else {
s->fd = qemu_open(s->filename, O_RDONLY | O_NONBLOCK);
s->fd = qemu_open_old(s->filename, O_RDONLY | O_NONBLOCK);
if (s->fd == -1) {
error_setg_file_open(errp, errno, s->filename);
}

View File

@ -218,7 +218,7 @@ static int tpm_passthrough_open_sysfs_cancel(TPMPassthruState *tpm_pt)
char path[PATH_MAX];
if (tpm_pt->options->cancel_path) {
fd = qemu_open(tpm_pt->options->cancel_path, O_WRONLY);
fd = qemu_open_old(tpm_pt->options->cancel_path, O_WRONLY);
if (fd < 0) {
error_report("tpm_passthrough: Could not open TPM cancel path: %s",
strerror(errno));
@ -236,11 +236,11 @@ static int tpm_passthrough_open_sysfs_cancel(TPMPassthruState *tpm_pt)
dev++;
if (snprintf(path, sizeof(path), "/sys/class/tpm/%s/device/cancel",
dev) < sizeof(path)) {
fd = qemu_open(path, O_WRONLY);
fd = qemu_open_old(path, O_WRONLY);
if (fd < 0) {
if (snprintf(path, sizeof(path), "/sys/class/misc/%s/device/cancel",
dev) < sizeof(path)) {
fd = qemu_open(path, O_WRONLY);
fd = qemu_open_old(path, O_WRONLY);
}
}
}
@ -272,7 +272,7 @@ tpm_passthrough_handle_device_opts(TPMPassthruState *tpm_pt, QemuOpts *opts)
}
tpm_pt->tpm_dev = value ? value : TPM_PASSTHROUGH_DEFAULT_DEVICE;
tpm_pt->tpm_fd = qemu_open(tpm_pt->tpm_dev, O_RDWR);
tpm_pt->tpm_fd = qemu_open_old(tpm_pt->tpm_dev, O_RDWR);
if (tpm_pt->tpm_fd < 0) {
error_report("Cannot access TPM device using '%s': %s",
tpm_pt->tpm_dev, strerror(errno));

View File

@ -630,11 +630,10 @@ static int raw_open_common(BlockDriverState *bs, QDict *options,
raw_parse_flags(bdrv_flags, &s->open_flags, false);
s->fd = -1;
fd = qemu_open(filename, s->open_flags, 0644);
fd = qemu_open(filename, s->open_flags, errp);
ret = fd < 0 ? -errno : 0;
if (ret < 0) {
error_setg_file_open(errp, -ret, filename);
if (ret == -EROFS) {
ret = -EACCES;
}
@ -1037,10 +1036,8 @@ static int raw_reconfigure_getfd(BlockDriverState *bs, int flags,
const char *normalized_filename = bs->filename;
ret = raw_normalize_devicepath(&normalized_filename, errp);
if (ret >= 0) {
assert(!(*open_flags & O_CREAT));
fd = qemu_open(normalized_filename, *open_flags);
fd = qemu_open(normalized_filename, *open_flags, errp);
if (fd == -1) {
error_setg_errno(errp, errno, "Could not reopen file");
return -1;
}
}
@ -2411,10 +2408,9 @@ raw_co_create(BlockdevCreateOptions *options, Error **errp)
}
/* Create file */
fd = qemu_open(file_opts->filename, O_RDWR | O_CREAT | O_BINARY, 0644);
fd = qemu_create(file_opts->filename, O_RDWR | O_BINARY, 0644, errp);
if (fd < 0) {
result = -errno;
error_setg_errno(errp, -result, "Could not create file");
goto out;
}
@ -3335,7 +3331,7 @@ static bool setup_cdrom(char *bsd_path, Error **errp)
for (index = 0; index < num_of_test_partitions; index++) {
snprintf(test_partition, sizeof(test_partition), "%ss%d", bsd_path,
index);
fd = qemu_open(test_partition, O_RDONLY | O_BINARY | O_LARGEFILE);
fd = qemu_open(test_partition, O_RDONLY | O_BINARY | O_LARGEFILE, NULL);
if (fd >= 0) {
partition_found = true;
qemu_close(fd);
@ -3653,7 +3649,7 @@ static int cdrom_probe_device(const char *filename)
int prio = 0;
struct stat st;
fd = qemu_open(filename, O_RDONLY | O_NONBLOCK);
fd = qemu_open(filename, O_RDONLY | O_NONBLOCK, NULL);
if (fd < 0) {
goto out;
}
@ -3787,7 +3783,7 @@ static int cdrom_reopen(BlockDriverState *bs)
*/
if (s->fd >= 0)
qemu_close(s->fd);
fd = qemu_open(bs->filename, s->open_flags, 0644);
fd = qemu_open(bs->filename, s->open_flags, NULL);
if (fd < 0) {
s->fd = -1;
return -EIO;

View File

@ -596,10 +596,9 @@ static int raw_co_create(BlockdevCreateOptions *options, Error **errp)
return -EINVAL;
}
fd = qemu_open(file_opts->filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
0644);
fd = qemu_create(file_opts->filename, O_WRONLY | O_TRUNC | O_BINARY,
0644, errp);
if (fd < 0) {
error_setg_errno(errp, errno, "Could not create file");
return -EIO;
}
set_sparse(fd);

View File

@ -1352,7 +1352,8 @@ static int open_file(BDRVVVFATState* s,mapping_t* mapping)
if(!s->current_mapping ||
strcmp(s->current_mapping->path,mapping->path)) {
/* open file */
int fd = qemu_open(mapping->path, O_RDONLY | O_BINARY | O_LARGEFILE);
int fd = qemu_open_old(mapping->path,
O_RDONLY | O_BINARY | O_LARGEFILE);
if(fd<0)
return -1;
vvfat_close_current_file(s);
@ -2513,7 +2514,7 @@ static int commit_one_file(BDRVVVFATState* s,
for (i = s->cluster_size; i < offset; i += s->cluster_size)
c = modified_fat_get(s, c);
fd = qemu_open(mapping->path, O_RDWR | O_CREAT | O_BINARY, 0666);
fd = qemu_open_old(mapping->path, O_RDWR | O_CREAT | O_BINARY, 0666);
if (fd < 0) {
fprintf(stderr, "Could not open %s... (%s, %d)\n", mapping->path,
strerror(errno), errno);

View File

@ -119,7 +119,7 @@ int qmp_chardev_open_file_source(char *src, int flags, Error **errp)
{
int fd = -1;
TFR(fd = qemu_open(src, flags, 0666));
TFR(fd = qemu_open_old(src, flags, 0666));
if (fd == -1) {
error_setg_file_open(errp, errno, src);
}

View File

@ -132,8 +132,8 @@ static void qemu_chr_open_pipe(Chardev *chr,
filename_in = g_strdup_printf("%s.in", filename);
filename_out = g_strdup_printf("%s.out", filename);
TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
TFR(fd_in = qemu_open_old(filename_in, O_RDWR | O_BINARY));
TFR(fd_out = qemu_open_old(filename_out, O_RDWR | O_BINARY));
g_free(filename_in);
g_free(filename_out);
if (fd_in < 0 || fd_out < 0) {
@ -143,7 +143,7 @@ static void qemu_chr_open_pipe(Chardev *chr,
if (fd_out >= 0) {
close(fd_out);
}
TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
TFR(fd_in = fd_out = qemu_open_old(filename, O_RDWR | O_BINARY));
if (fd_in < 0) {
error_setg_file_open(errp, errno, filename);
return;

View File

@ -235,7 +235,7 @@ static void qemu_char_open(Chardev *chr, ChardevBackend *backend,
} else {
flags |= O_TRUNC;
}
chr->logfd = qemu_open(common->logfile, flags, 0666);
chr->logfd = qemu_open_old(common->logfile, flags, 0666);
if (chr->logfd < 0) {
error_setg_errno(errp, errno,
"Unable to open logfile %s",

View File

@ -1994,7 +1994,7 @@ void qmp_dump_guest_memory(bool paging, const char *file,
#endif
if (strstart(file, "file:", &p)) {
fd = qemu_open(p, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR);
fd = qemu_open_old(p, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR);
if (fd < 0) {
error_setg_file_open(errp, errno, p);
return;

View File

@ -125,7 +125,7 @@ void qmp_dump_skeys(const char *filename, Error **errp)
return;
}
fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
fd = qemu_open_old(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
if (fd < 0) {
error_setg_file_open(errp, errno, filename);
return;

View File

@ -1147,7 +1147,7 @@ static void usb_host_realize(USBDevice *udev, Error **errp)
if (s->hostdevice) {
int fd;
s->needs_autoscan = false;
fd = qemu_open(s->hostdevice, O_RDWR);
fd = qemu_open_old(s->hostdevice, O_RDWR);
if (fd < 0) {
error_setg_errno(errp, errno, "failed to open %s", s->hostdevice);
return;

View File

@ -383,7 +383,7 @@ static int u2f_passthru_open_from_device(struct udev_device *device)
{
const char *devnode = udev_device_get_devnode(device);
int fd = qemu_open(devnode, O_RDWR);
int fd = qemu_open_old(devnode, O_RDWR);
if (fd < 0) {
return -1;
} else if (!u2f_passthru_is_u2f_device(fd)) {
@ -482,7 +482,7 @@ static void u2f_passthru_realize(U2FKeyState *base, Error **errp)
return;
#endif
} else {
fd = qemu_open(key->hidraw, O_RDWR);
fd = qemu_open_old(key->hidraw, O_RDWR);
if (fd < 0) {
error_setg(errp, "%s: Failed to open %s", TYPE_U2F_PASSTHRU,
key->hidraw);

View File

@ -1254,7 +1254,7 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as,
}
}
fd = qemu_open("/dev/vfio/vfio", O_RDWR);
fd = qemu_open_old("/dev/vfio/vfio", O_RDWR);
if (fd < 0) {
error_setg_errno(errp, errno, "failed to open /dev/vfio/vfio");
ret = -errno;
@ -1479,7 +1479,7 @@ VFIOGroup *vfio_get_group(int groupid, AddressSpace *as, Error **errp)
group = g_malloc0(sizeof(*group));
snprintf(path, sizeof(path), "/dev/vfio/%d", groupid);
group->fd = qemu_open(path, O_RDWR);
group->fd = qemu_open_old(path, O_RDWR);
if (group->fd < 0) {
error_setg_errno(errp, errno, "failed to open %s", path);
goto free_group_exit;

View File

@ -43,8 +43,7 @@ int monitor_read_password(MonitorHMP *mon, ReadLineFunc *readline_func,
AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
bool has_opaque, const char *opaque,
Error **errp);
int monitor_fdset_get_fd(int64_t fdset_id, int flags);
int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd);
int monitor_fdset_dup_fd_add(int64_t fdset_id, int flags);
void monitor_fdset_dup_fd_remove(int dup_fd);
int64_t monitor_fdset_dup_fd_find(int dup_fd);

View File

@ -497,10 +497,17 @@ int qemu_madvise(void *addr, size_t len, int advice);
int qemu_mprotect_rwx(void *addr, size_t size);
int qemu_mprotect_none(void *addr, size_t size);
int qemu_open(const char *name, int flags, ...);
/*
* Don't introduce new usage of this function, prefer the following
* qemu_open/qemu_create that take an "Error **errp"
*/
int qemu_open_old(const char *name, int flags, ...);
int qemu_open(const char *name, int flags, Error **errp);
int qemu_create(const char *name, int flags, mode_t mode, Error **errp);
int qemu_close(int fd);
int qemu_unlink(const char *name);
#ifndef _WIN32
int qemu_dup_flags(int fd, int flags);
int qemu_dup(int fd);
#endif
int qemu_lock_fd(int fd, int64_t start, int64_t len, bool exclusive);

View File

@ -51,7 +51,7 @@ qio_channel_file_new_path(const char *path,
ioc = QIO_CHANNEL_FILE(object_new(TYPE_QIO_CHANNEL_FILE));
ioc->fd = qemu_open(path, flags, mode);
ioc->fd = qemu_open_old(path, flags, mode);
if (ioc->fd < 0) {
object_unref(OBJECT(ioc));
error_setg_errno(errp, errno,

View File

@ -1547,69 +1547,61 @@ AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
return fdinfo;
}
int monitor_fdset_get_fd(int64_t fdset_id, int flags)
int monitor_fdset_dup_fd_add(int64_t fdset_id, int flags)
{
#ifdef _WIN32
return -ENOENT;
#else
MonFdset *mon_fdset;
MonFdsetFd *mon_fdset_fd;
int mon_fd_flags;
int ret;
qemu_mutex_lock(&mon_fdsets_lock);
QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
MonFdsetFd *mon_fdset_fd;
MonFdsetFd *mon_fdset_fd_dup;
int fd = -1;
int dup_fd;
int mon_fd_flags;
if (mon_fdset->id != fdset_id) {
continue;
}
QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
if (mon_fd_flags == -1) {
ret = -errno;
goto out;
qemu_mutex_unlock(&mon_fdsets_lock);
return -1;
}
if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
ret = mon_fdset_fd->fd;
goto out;
fd = mon_fdset_fd->fd;
break;
}
}
ret = -EACCES;
goto out;
}
ret = -ENOENT;
out:
qemu_mutex_unlock(&mon_fdsets_lock);
return ret;
#endif
}
int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
{
MonFdset *mon_fdset;
MonFdsetFd *mon_fdset_fd_dup;
qemu_mutex_lock(&mon_fdsets_lock);
QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
if (mon_fdset->id != fdset_id) {
continue;
if (fd == -1) {
qemu_mutex_unlock(&mon_fdsets_lock);
errno = EACCES;
return -1;
}
QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
if (mon_fdset_fd_dup->fd == dup_fd) {
goto err;
}
dup_fd = qemu_dup_flags(fd, flags);
if (dup_fd == -1) {
qemu_mutex_unlock(&mon_fdsets_lock);
return -1;
}
mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
mon_fdset_fd_dup->fd = dup_fd;
QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
qemu_mutex_unlock(&mon_fdsets_lock);
return 0;
return dup_fd;
}
err:
qemu_mutex_unlock(&mon_fdsets_lock);
errno = ENOENT;
return -1;
#endif
}
static int64_t monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)

View File

@ -184,7 +184,7 @@ static int net_vhost_vdpa_init(NetClientState *peer, const char *device,
snprintf(nc->info_str, sizeof(nc->info_str), TYPE_VHOST_VDPA);
nc->queue_index = 0;
s = DO_UPCAST(VhostVDPAState, nc, nc);
vdpa_device_fd = qemu_open(vhostdev, O_RDWR);
vdpa_device_fd = qemu_open_old(vhostdev, O_RDWR);
if (vdpa_device_fd == -1) {
return -errno;
}

View File

@ -297,7 +297,7 @@ void os_setup_post(void)
error_report("not able to chdir to /: %s", strerror(errno));
exit(1);
}
TFR(fd = qemu_open("/dev/null", O_RDWR));
TFR(fd = qemu_open_old("/dev/null", O_RDWR));
if (fd == -1) {
exit(1);
}

View File

@ -127,7 +127,7 @@ static gboolean ga_channel_open(GAChannel *c, const gchar *path,
switch (c->method) {
case GA_CHANNEL_VIRTIO_SERIAL: {
assert(fd < 0);
fd = qemu_open(path, O_RDWR | O_NONBLOCK
fd = qemu_open_old(path, O_RDWR | O_NONBLOCK
#ifndef CONFIG_SOLARIS
| O_ASYNC
#endif
@ -157,7 +157,7 @@ static gboolean ga_channel_open(GAChannel *c, const gchar *path,
struct termios tio;
assert(fd < 0);
fd = qemu_open(path, O_RDWR | O_NOCTTY | O_NONBLOCK);
fd = qemu_open_old(path, O_RDWR | O_NOCTTY | O_NONBLOCK);
if (fd == -1) {
g_critical("error opening channel: %s", strerror(errno));
return false;

View File

@ -1365,7 +1365,7 @@ int64_t qmp_guest_fsfreeze_freeze_list(bool has_mountpoints,
}
}
fd = qemu_open(mount->dirname, O_RDONLY);
fd = qemu_open_old(mount->dirname, O_RDONLY);
if (fd == -1) {
error_setg_errno(errp, errno, "failed to open %s", mount->dirname);
goto error;
@ -1432,7 +1432,7 @@ int64_t qmp_guest_fsfreeze_thaw(Error **errp)
QTAILQ_FOREACH(mount, &mounts, next) {
logged = false;
fd = qemu_open(mount->dirname, O_RDONLY);
fd = qemu_open_old(mount->dirname, O_RDONLY);
if (fd == -1) {
continue;
}
@ -1522,7 +1522,7 @@ qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **errp)
list->next = response->paths;
response->paths = list;
fd = qemu_open(mount->dirname, O_RDONLY);
fd = qemu_open_old(mount->dirname, O_RDONLY);
if (fd == -1) {
result->error = g_strdup_printf("failed to open: %s",
strerror(errno));

View File

@ -1,8 +1,9 @@
#include "qemu/osdep.h"
#include "monitor/monitor.h"
int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
int monitor_fdset_dup_fd_add(int64_t fdset_id, int flags)
{
errno = ENOSYS;
return -1;
}
@ -11,11 +12,6 @@ int64_t monitor_fdset_dup_fd_find(int dup_fd)
return -1;
}
int monitor_fdset_get_fd(int64_t fdset_id, int flags)
{
return -ENOENT;
}
void monitor_fdset_dup_fd_remove(int dupfd)
{
}

View File

@ -71,7 +71,7 @@ bool kvm_arm_create_scratch_host_vcpu(const uint32_t *cpus_to_try,
{
int ret = 0, kvmfd = -1, vmfd = -1, cpufd = -1;
kvmfd = qemu_open("/dev/kvm", O_RDWR);
kvmfd = qemu_open_old("/dev/kvm", O_RDWR);
if (kvmfd < 0) {
goto err;
}

View File

@ -373,7 +373,7 @@ void qmp_screendump(const char *filename, bool has_device, const char *device,
return;
}
fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
fd = qemu_open_old(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
if (fd == -1) {
error_setg(errp, "failed to open file '%s': %s", filename,
strerror(errno));

View File

@ -22,6 +22,7 @@
* THE SOFTWARE.
*/
#include "qemu/osdep.h"
#include "qapi/error.h"
/* Needed early for CONFIG_BSD etc. */
@ -122,7 +123,7 @@ static int fcntl_op_getlk = -1;
/*
* Dups an fd and sets the flags
*/
static int qemu_dup_flags(int fd, int flags)
int qemu_dup_flags(int fd, int flags)
{
int ret;
int serrno;
@ -279,58 +280,9 @@ int qemu_lock_fd_test(int fd, int64_t start, int64_t len, bool exclusive)
}
#endif
/*
* Opens a file with FD_CLOEXEC set
*/
int qemu_open(const char *name, int flags, ...)
static int qemu_open_cloexec(const char *name, int flags, mode_t mode)
{
int ret;
int mode = 0;
#ifndef _WIN32
const char *fdset_id_str;
/* Attempt dup of fd from fd set */
if (strstart(name, "/dev/fdset/", &fdset_id_str)) {
int64_t fdset_id;
int fd, dupfd;
fdset_id = qemu_parse_fdset(fdset_id_str);
if (fdset_id == -1) {
errno = EINVAL;
return -1;
}
fd = monitor_fdset_get_fd(fdset_id, flags);
if (fd < 0) {
errno = -fd;
return -1;
}
dupfd = qemu_dup_flags(fd, flags);
if (dupfd == -1) {
return -1;
}
ret = monitor_fdset_dup_fd_add(fdset_id, dupfd);
if (ret == -1) {
close(dupfd);
errno = EINVAL;
return -1;
}
return dupfd;
}
#endif
if (flags & O_CREAT) {
va_list ap;
va_start(ap, flags);
mode = va_arg(ap, int);
va_end(ap);
}
#ifdef O_CLOEXEC
ret = open(name, flags | O_CLOEXEC, mode);
#else
@ -339,6 +291,98 @@ int qemu_open(const char *name, int flags, ...)
qemu_set_cloexec(ret);
}
#endif
return ret;
}
/*
* Opens a file with FD_CLOEXEC set
*/
static int
qemu_open_internal(const char *name, int flags, mode_t mode, Error **errp)
{
int ret;
#ifndef _WIN32
const char *fdset_id_str;
/* Attempt dup of fd from fd set */
if (strstart(name, "/dev/fdset/", &fdset_id_str)) {
int64_t fdset_id;
int dupfd;
fdset_id = qemu_parse_fdset(fdset_id_str);
if (fdset_id == -1) {
error_setg(errp, "Could not parse fdset %s", name);
errno = EINVAL;
return -1;
}
dupfd = monitor_fdset_dup_fd_add(fdset_id, flags);
if (dupfd == -1) {
error_setg_errno(errp, errno, "Could not dup FD for %s flags %x",
name, flags);
return -1;
}
return dupfd;
}
#endif
ret = qemu_open_cloexec(name, flags, mode);
if (ret == -1) {
const char *action = flags & O_CREAT ? "create" : "open";
#ifdef O_DIRECT
/* Give more helpful error message for O_DIRECT */
if (errno == EINVAL && (flags & O_DIRECT)) {
ret = open(name, flags & ~O_DIRECT, mode);
if (ret != -1) {
close(ret);
error_setg(errp, "Could not %s '%s': "
"filesystem does not support O_DIRECT",
action, name);
errno = EINVAL; /* restore first open()'s errno */
return -1;
}
}
#endif /* O_DIRECT */
error_setg_errno(errp, errno, "Could not %s '%s'",
action, name);
}
return ret;
}
int qemu_open(const char *name, int flags, Error **errp)
{
assert(!(flags & O_CREAT));
return qemu_open_internal(name, flags, 0, errp);
}
int qemu_create(const char *name, int flags, mode_t mode, Error **errp)
{
assert(!(flags & O_CREAT));
return qemu_open_internal(name, flags | O_CREAT, mode, errp);
}
int qemu_open_old(const char *name, int flags, ...)
{
va_list ap;
mode_t mode = 0;
int ret;
va_start(ap, flags);
if (flags & O_CREAT) {
mode = va_arg(ap, int);
}
va_end(ap);
ret = qemu_open_internal(name, flags, mode, NULL);
#ifdef O_DIRECT
if (ret == -1 && errno == EINVAL && (flags & O_DIRECT)) {

View File

@ -125,7 +125,7 @@ bool qemu_write_pidfile(const char *path, Error **errp)
.l_len = 0,
};
fd = qemu_open(path, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
fd = qemu_open_old(path, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
if (fd == -1) {
error_setg_errno(errp, errno, "Cannot open pid file");
return false;