From patchwork Tue Oct 27 17:48:43 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Denis V. Lunev" X-Patchwork-Id: 536767 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 09AE914030E for ; Wed, 28 Oct 2015 04:49:30 +1100 (AEDT) Received: from localhost ([::1]:33039 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zr8ND-0003Qi-Rq for incoming@patchwork.ozlabs.org; Tue, 27 Oct 2015 13:49:27 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:35873) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zr8Mh-0002ZA-Rz for qemu-devel@nongnu.org; Tue, 27 Oct 2015 13:48:56 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Zr8Mg-0005UV-Hm for qemu-devel@nongnu.org; Tue, 27 Oct 2015 13:48:55 -0400 Received: from mailhub.sw.ru ([195.214.232.25]:34619 helo=relay.sw.ru) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zr8Mg-0005UJ-4P for qemu-devel@nongnu.org; Tue, 27 Oct 2015 13:48:54 -0400 Received: from irbis.sw.ru ([10.30.2.139]) by relay.sw.ru (8.13.4/8.13.4) with ESMTP id t9RHmhqU001048; Tue, 27 Oct 2015 20:48:48 +0300 (MSK) From: "Denis V. Lunev" To: Date: Tue, 27 Oct 2015 20:48:43 +0300 Message-Id: <1445968123-1773-4-git-send-email-den@openvz.org> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1445968123-1773-1-git-send-email-den@openvz.org> References: <1445968123-1773-1-git-send-email-den@openvz.org> X-detected-operating-system: by eggs.gnu.org: OpenBSD 3.x X-Received-From: 195.214.232.25 Cc: Michael Roth , Olga Krishtal , Stefan Weil , qemu-devel@nongnu.org, "Denis V. Lunev" Subject: [Qemu-devel] [PATCH 3/3] qga: set file descriptor in qmp_guest_file_open non-blocking on Win32 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Olga Krishtal Set fd non-blocking to avoid common use cases (like reading from a named pipe) from hanging the agent. This was missed in the original code. The patch introduces analog of qemu_set_non/block for HANDLES. The usage of handles in qemu_set_non/block is impossible, because for win32 there is a difference between file discriptors and file handles, and all file ops are made via Win32 api. Signed-off-by: Olga Krishtal Signed-off-by: Denis V. Lunev CC: Michael Roth CC: Stefan Weil --- include/qemu/sockets.h | 2 ++ qga/commands-win32.c | 6 ++++++ util/oslib-win32.c | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+) diff --git a/include/qemu/sockets.h b/include/qemu/sockets.h index 5a183c5..e2323f4 100644 --- a/include/qemu/sockets.h +++ b/include/qemu/sockets.h @@ -39,6 +39,8 @@ int socket_set_cork(int fd, int v); int socket_set_nodelay(int fd); void qemu_set_block(int fd); void qemu_set_nonblock(int fd); +void qemu_set_nonblock_by_handle(int64_t fh); +void qemu_set_block_by_handle(int64_t fh); int socket_set_fast_reuse(int fd); int send_all(int fd, const void *buf, int len1); int recv_all(int fd, void *buf, int len1, bool single_read); diff --git a/qga/commands-win32.c b/qga/commands-win32.c index 97f19d5..f959d75 100644 --- a/qga/commands-win32.c +++ b/qga/commands-win32.c @@ -34,6 +34,7 @@ #include "qapi/qmp/qerror.h" #include "qemu/queue.h" #include "qemu/host-utils.h" +#include "qemu/sockets.h" #ifndef SHTDN_REASON_FLAG_PLANNED #define SHTDN_REASON_FLAG_PLANNED 0x80000000 @@ -158,6 +159,11 @@ int64_t qmp_guest_file_open(const char *path, bool has_mode, return -1; } + /* set fd non-blocking to avoid common use cases (like reading from a + * named pipe) from hanging the agent + */ + qemu_set_nonblock_by_handle((int64_t)fh); + fd = guest_file_handle_add(fh, errp); if (fd < 0) { CloseHandle(fh); diff --git a/util/oslib-win32.c b/util/oslib-win32.c index 09f9e98..4a9fd8e 100644 --- a/util/oslib-win32.c +++ b/util/oslib-win32.c @@ -121,6 +121,37 @@ struct tm *localtime_r(const time_t *timep, struct tm *result) } #endif /* CONFIG_LOCALTIME_R */ +static void qemu_set_handle_nonblocking(int64_t fh, bool nonblocking) +{ + DWORD file_type, pipe_state; + HANDLE handle = (HANDLE)fh; + 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)) { + return; + } + /* 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); +} + void qemu_set_block(int fd) { unsigned long opt = 0; @@ -135,6 +166,16 @@ void qemu_set_nonblock(int fd) qemu_fd_register(fd); } +void qemu_set_block_by_handle(int64_t fh) +{ + qemu_set_handle_nonblocking(fh, false); +} + +void qemu_set_nonblock_by_handle(int64_t fh) +{ + qemu_set_handle_nonblocking(fh, true); +} + int socket_set_fast_reuse(int fd) { /* Enabling the reuse of an endpoint that was used by a socket still in