From: Michael Roth <mdroth@linux.vnet.ibm.com>
To: "Denis V. Lunev" <den@openvz.org>
Cc: Olga Krishtal <okrishtal@virtuozzo.com>, qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] [PATCH 01/10] util, qga: drop guest_file_toggle_flags
Date: Wed, 08 Jul 2015 19:09:38 -0500 [thread overview]
Message-ID: <20150709000938.23206.13696@loki> (raw)
In-Reply-To: <559DA6DE.2050407@openvz.org>
Quoting Denis V. Lunev (2015-07-08 17:40:30)
> On 09/07/15 00:16, Michael Roth wrote:
> > Quoting Denis V. Lunev (2015-06-30 05:25:14)
> >> From: Olga Krishtal <okrishtal@virtuozzo.com>
> >>
> >> guest_file_toggle_flags is a copy from semi-portable qemu_set_nonblock.
> >> The latter is not working properly for Windows due to reduced Windows
> >> Posix implementation.
> >>
> >> On Windows OS there is a separate API for changing flags of file, pipes
> >> and sockets. Portable way to change file descriptor flags requires
> >> to detect file descriptor type and proper actions depending of that
> >> type. The patch adds wrapper qemu_set_fd_nonblocking into Windows specific
> >> code to handle this stuff properly.
> >>
> >> The only problem is that qemu_set_nonblock is void but this should not
> >> be a problem.
> >>
> >> Signed-off-by: Olga Krishtal <okrishtal@virtuozzo.com>
> >> Signed-off-by: Denis V. Lunev <den@openvz.org>
> >> CC: Eric Blake <eblake@redhat.com>
> >> CC: Michael Roth <mdroth@linux.vnet.ibm.com>
> >> ---
> >> qga/commands-posix.c | 27 ++-------------------------
> >> util/oslib-win32.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++-----
> >> 2 files changed, 49 insertions(+), 30 deletions(-)
> >>
> >> diff --git a/qga/commands-posix.c b/qga/commands-posix.c
> >> index befd00b..40dbe25 100644
> >> --- a/qga/commands-posix.c
> >> +++ b/qga/commands-posix.c
> >> @@ -28,6 +28,7 @@
> >> #include "qapi/qmp/qerror.h"
> >> #include "qemu/queue.h"
> >> #include "qemu/host-utils.h"
> >> +#include "qemu/sockets.h"
> >>
> >> #ifndef CONFIG_HAS_ENVIRON
> >> #ifdef __APPLE__
> >> @@ -376,27 +377,6 @@ safe_open_or_create(const char *path, const char *mode, Error **errp)
> >> return NULL;
> >> }
> >>
> >> -static int guest_file_toggle_flags(int fd, int flags, bool set, Error **err)
> >> -{
> >> - int ret, old_flags;
> >> -
> >> - old_flags = fcntl(fd, F_GETFL);
> >> - if (old_flags == -1) {
> >> - error_setg_errno(err, errno, QERR_QGA_COMMAND_FAILED,
> >> - "failed to fetch filehandle flags");
> >> - return -1;
> >> - }
> >> -
> >> - ret = fcntl(fd, F_SETFL, set ? (old_flags | flags) : (old_flags & ~flags));
> >> - if (ret == -1) {
> >> - error_setg_errno(err, errno, QERR_QGA_COMMAND_FAILED,
> >> - "failed to set filehandle flags");
> >> - return -1;
> >> - }
> >> -
> >> - return ret;
> >> -}
> >> -
> >> int64_t qmp_guest_file_open(const char *path, bool has_mode, const char *mode,
> >> Error **errp)
> >> {
> >> @@ -417,10 +397,7 @@ int64_t qmp_guest_file_open(const char *path, bool has_mode, const char *mode,
> >> /* set fd non-blocking to avoid common use cases (like reading from a
> >> * named pipe) from hanging the agent
> >> */
> >> - if (guest_file_toggle_flags(fileno(fh), O_NONBLOCK, true, errp) < 0) {
> >> - fclose(fh);
> >> - return -1;
> >> - }
> >> + qemu_set_nonblock(fileno(fh));
> >>
> >> handle = guest_file_handle_add(fh, errp);
> >> if (handle < 0) {
> >> diff --git a/util/oslib-win32.c b/util/oslib-win32.c
> >> index 730a670..1a6ae72 100644
> >> --- a/util/oslib-win32.c
> >> +++ b/util/oslib-win32.c
> >> @@ -119,17 +119,59 @@ struct tm *localtime_r(const time_t *timep, struct tm *result)
> >> return p;
> >> }
> >>
> >> -void qemu_set_block(int fd)
> >> +static void qemu_set_fd_nonblocking(int fd, bool nonblocking)
> >> {
> >> - unsigned long opt = 0;
> >> - WSAEventSelect(fd, NULL, 0);
> >> + HANDLE handle;
> >> + DWORD file_type, pipe_state;
> >> +
> >> + handle = (HANDLE)_get_osfhandle(fd);
> >> + if (handle == INVALID_HANDLE_VALUE) {
> >> + return;
> >> + }
> >> +
> >> + file_type = GetFileType(handle);
> >> + if (file_type != FILE_TYPE_PIPE) {
> >> + return;
> >> + }
> >> +
> >> + /* If file_type == FILE_TYPE_PIPE, according to msdn
> >> + * the specified file is socket or named pipe */
> >> + if (GetNamedPipeHandleState(handle, &pipe_state, NULL,
> >> + NULL, NULL, NULL, 0)) {
> >> + /* The fd is named pipe fd */
> >> + if (!nonblocking == !(pipe_state & PIPE_NOWAIT)) {
> >> + /* In this case we do not need perform any operation, because
> >> + * nonblocking = true and PIPE_NOWAIT is already set or
> >> + * nonblocking = false and PIPE_NOWAIT is not set */
> >> + return;
> >> + }
> >> +
> >> + if (nonblocking) {
> >> + pipe_state |= PIPE_NOWAIT;
> >> + } else {
> >> + pipe_state &= ~PIPE_NOWAIT;
> >> + }
> >> +
> >> + SetNamedPipeHandleState(handle, &pipe_state, NULL, NULL);
> >> + return;
> >> + }
> >> +
> >> + /* The fd is socket fd */
> >> + unsigned long opt = (unsigned long)nonblocking;
> >> + if (!nonblocking) {
> >> + WSAEventSelect(fd, NULL, 0);
> >> + }
> >> ioctlsocket(fd, FIONBIO, &opt);
> >> }
> >>
> >> +void qemu_set_block(int fd)
> >> +{
> >> + qemu_set_fd_nonblocking(fd, false);
> >> +}
> >> +
> >> void qemu_set_nonblock(int fd)
> >> {
> >> - unsigned long opt = 1;
> >> - ioctlsocket(fd, FIONBIO, &opt);
> >> + qemu_set_fd_nonblocking(fd, true);
> >> qemu_fd_register(fd);
> > We wouldn't want to pass a non-socket FD to qemu_fd_register(), so
> > this should get moved to qemu_set_fd_nonblocking() at least.
> >
> > I think it's confusing to have a qemu_set_nonblock() that's limited
> > to pipes and sockets. At least with the current code it's fairly
> > obvious it's intended for sockets.
> >
> > It might be worthwhile if we could at least cover disk files, but
> > I seem to recall that being non-trivial on w32 and generally
> > requiring non-posix functions that support overlapped IO. So, if
> > we're only extending this to support pipes it should probably just
> > be a separate helper in qga/.
> >
> at my opinion you are a little bit wrong here. First of all, from
> Linux point of view, O_NONBLOCK for ordinary file is noop, i.e.
> there is no difference in behavior with and without this flag
> for such descriptors.
Yes, I think you're right... I'd forgotten that the reason we use it
in guest-file-open was for dealing with pipes/chardevs/sockets rather
than anything to do with files.
>
> This means that the write will take some time and will finish
> without indefinite timeout. Sockets and pipes are different,
> they can hang in read/write forever and thus the flag has
> some value to avoid this.
>
> The same applies for Windows platform too. Setting nonblock
> here we want to avoid infinite hang and we do not want to
> implement asynchronous IO, which is implemented using
> overlapped API.
>
> There are only 2 primitives in QGA which subjects to the rules
> above - sockets and pipes. Thus this common helper makes
> sense. If we will need more primitives - we will add proper
> distinction into these calls.
There's still an important invariant here in that O_NONBLOCK ensures
EAGAIN in cases where you might otherwise wait indefinitely. That
covers named/anonymous pipes, sockets, files, chardevs, any fd.
To take the socket-only w32 implementation and only tack on pipes
seems somewhat arbitrary in comparison, but I do understand your
logic from a "FDs that make sense to use it with" standpoint. I think
you'll need to Cc: our w32 maintainer Stefan Weil <sw@weilnetz.de>
for any such change in oslib-win32.c though.
Personally I think set_pipe_nonblock() in qga/commands-win32.c is
clearer, and it seems unlikely we'll ever end up with a 2nd w32
user of it so we don't stand to gain much from a generalized
function.
>
> Den
>
next prev parent reply other threads:[~2015-07-09 1:30 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-06-30 10:25 [Qemu-devel] [PATCH v6 0/10] QGA: disk and volume info for Windows & guest exec Denis V. Lunev
2015-06-30 10:25 ` [Qemu-devel] [PATCH 01/10] util, qga: drop guest_file_toggle_flags Denis V. Lunev
2015-07-07 8:19 ` Denis V. Lunev
2015-07-08 21:26 ` Michael Roth
2015-07-08 21:16 ` Michael Roth
2015-07-08 22:40 ` Denis V. Lunev
2015-07-09 0:09 ` Michael Roth [this message]
2015-06-30 10:25 ` [Qemu-devel] [PATCH 02/10] qga: implement guest-pipe-open command Denis V. Lunev
2015-06-30 10:25 ` [Qemu-devel] [PATCH 03/10] qga: guest exec functionality for Unix guests Denis V. Lunev
2015-06-30 10:25 ` [Qemu-devel] [PATCH 04/10] qga: handle possible SIGPIPE in guest-file-write Denis V. Lunev
2015-06-30 10:25 ` [Qemu-devel] [PATCH 05/10] qga: guest-pipe-open for Windows guest Denis V. Lunev
2015-06-30 10:25 ` [Qemu-devel] [PATCH 06/10] qga: guest exec functionality for Windows guests Denis V. Lunev
2015-07-07 1:31 ` Michael Roth
2015-07-07 8:06 ` Denis V. Lunev
2015-07-07 9:12 ` Olga Krishtal
2015-07-07 9:59 ` Denis V. Lunev
2015-07-07 10:07 ` Olga Krishtal
2015-07-08 22:02 ` Michael Roth
2015-07-08 22:47 ` Denis V. Lunev
2015-07-09 1:30 ` Michael Roth
2015-07-10 13:23 ` Denis V. Lunev
2015-06-30 10:25 ` [Qemu-devel] [PATCH 07/10] qga: added empty qmp_quest_get_fsinfo functionality Denis V. Lunev
2015-06-30 10:25 ` [Qemu-devel] [PATCH 08/10] qga: added mountpoint and filesystem type for single volume Denis V. Lunev
2015-06-30 10:25 ` [Qemu-devel] [PATCH 09/10] qga: added bus type and disk location path Denis V. Lunev
2015-06-30 10:25 ` [Qemu-devel] [PATCH 10/10] qga: added GuestPCIAddress information Denis V. Lunev
-- strict thread matches above, loose matches on Subject: below --
2015-06-19 16:57 [Qemu-devel] [PATCH v5 0/10] QGA: disk and volume info for Windows & guest exec Denis V. Lunev
2015-06-19 16:57 ` [Qemu-devel] [PATCH 01/10] util, qga: drop guest_file_toggle_flags Denis V. Lunev
2015-06-19 16:51 [Qemu-devel] [PATCH v4 0/10] QGA: disk and volume info for Windows & guest exec Denis V. Lunev
2015-06-19 16:51 ` [Qemu-devel] [PATCH 01/10] util, qga: drop guest_file_toggle_flags Denis V. Lunev
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20150709000938.23206.13696@loki \
--to=mdroth@linux.vnet.ibm.com \
--cc=den@openvz.org \
--cc=okrishtal@virtuozzo.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).