From patchwork Wed Apr 10 09:26:52 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yury Kotov X-Patchwork-Id: 10893521 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8A492922 for ; Wed, 10 Apr 2019 09:27:44 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 73E7E2018F for ; Wed, 10 Apr 2019 09:27:44 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5FBD328A02; Wed, 10 Apr 2019 09:27:44 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.7 required=2.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 882D02018F for ; Wed, 10 Apr 2019 09:27:43 +0000 (UTC) Received: from localhost ([127.0.0.1]:56367 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hE9WA-0000EE-HW for patchwork-qemu-devel@patchwork.kernel.org; Wed, 10 Apr 2019 05:27:42 -0400 Received: from eggs.gnu.org ([209.51.188.92]:39281) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hE9VZ-0008Kv-6X for qemu-devel@nongnu.org; Wed, 10 Apr 2019 05:27:06 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hE9VY-00014A-5q for qemu-devel@nongnu.org; Wed, 10 Apr 2019 05:27:05 -0400 Received: from forwardcorp1j.mail.yandex.net ([2a02:6b8:0:1619::183]:38784) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hE9VX-0000xk-G2 for qemu-devel@nongnu.org; Wed, 10 Apr 2019 05:27:04 -0400 Received: from mxbackcorp1j.mail.yandex.net (mxbackcorp1j.mail.yandex.net [IPv6:2a02:6b8:0:1619::162]) by forwardcorp1j.mail.yandex.net (Yandex) with ESMTP id 441122E1512; Wed, 10 Apr 2019 12:26:59 +0300 (MSK) Received: from smtpcorp1o.mail.yandex.net (smtpcorp1o.mail.yandex.net [2a02:6b8:0:1a2d::30]) by mxbackcorp1j.mail.yandex.net (nwsmtp/Yandex) with ESMTP id 2yLQePubGh-QwMqtOd1; Wed, 10 Apr 2019 12:26:59 +0300 Precedence: bulk DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex-team.ru; s=default; t=1554888419; bh=qzVUe+kFoue1PLwKTfFPiIOFXUwUUgzArHlxHpLVHgc=; h=Message-Id:Date:Subject:To:From:Cc; b=VCbfWikvBVL0wXcHp5Jm9saEbV74OQsrZdVFhsmQuXKJfRvY7gGENyZv/vuu5VADM gJLXSJZuprOs900ZwmHrscKpFDYnUnwIyyAoPI2LC0MCkHevMjouYtOiHcIaFiqEMp 265tSwqkxvZrwfdxgYP/tQa4Cn8sTxh+z/23G1/U= Authentication-Results: mxbackcorp1j.mail.yandex.net; dkim=pass header.i=@yandex-team.ru Received: from dynamic-red.dhcp.yndx.net (dynamic-red.dhcp.yndx.net [2a02:6b8:0:40c:1c6a:cfcd:3ad6:4876]) by smtpcorp1o.mail.yandex.net (nwsmtp/Yandex) with ESMTPSA id 4IllViJHpG-QwYeQP9I; Wed, 10 Apr 2019 12:26:58 +0300 (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (Client certificate not present) From: Yury Kotov To: "Dr. David Alan Gilbert" , Juan Quintela Date: Wed, 10 Apr 2019 12:26:52 +0300 Message-Id: <20190410092652.22616-1-yury-kotov@yandex-team.ru> X-Mailer: git-send-email 2.21.0 MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a02:6b8:0:1619::183 Subject: [Qemu-devel] [PATCH] migration: Fix handling fd protocol X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: qemu-devel@nongnu.org, yc-core@yandex-team.ru Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Currently such case is possible for incoming: QMP: add-fd (fdset = 0, fd of some file): adds fd to fdset 0 and returns QEMU's fd (e.g. 33) QMP: migrate-incoming (uri = "fd:33"): fd is stored in QIOChannel *ioc ... Incoming migration completes and unrefs ioc -> close(33) QMP: remove-fd (fdset = 0, fd = 33): removes fd from fdset 0 and qemu_close() -> close(33) => double close For outgoing migration the case is the same but getfd instead of add-fd. Fix it by duping client's fd. Signed-off-by: Yury Kotov --- migration/fd.c | 39 +++++++++++++++++++++++++++++++-------- migration/trace-events | 4 ++-- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/migration/fd.c b/migration/fd.c index a7c13df4ad..c9ff07ac41 100644 --- a/migration/fd.c +++ b/migration/fd.c @@ -15,6 +15,7 @@ */ #include "qemu/osdep.h" +#include "qapi/error.h" #include "channel.h" #include "fd.h" #include "migration.h" @@ -26,15 +27,27 @@ void fd_start_outgoing_migration(MigrationState *s, const char *fdname, Error **errp) { QIOChannel *ioc; - int fd = monitor_get_fd(cur_mon, fdname, errp); + int fd, dup_fd; + + fd = monitor_get_fd(cur_mon, fdname, errp); if (fd == -1) { return; } - trace_migration_fd_outgoing(fd); - ioc = qio_channel_new_fd(fd, errp); + /* fd is previously created by qmp command 'getfd', + * so client is responsible to close it. Dup it to save original value from + * QIOChannel's destructor */ + dup_fd = qemu_dup(fd); + if (dup_fd == -1) { + error_setg(errp, "Cannot dup fd %s: %s (%d)", fdname, strerror(errno), + errno); + return; + } + + trace_migration_fd_outgoing(fd, dup_fd); + ioc = qio_channel_new_fd(dup_fd, errp); if (!ioc) { - close(fd); + close(dup_fd); return; } @@ -55,14 +68,24 @@ static gboolean fd_accept_incoming_migration(QIOChannel *ioc, void fd_start_incoming_migration(const char *infd, Error **errp) { QIOChannel *ioc; - int fd; + int fd, dup_fd; fd = strtol(infd, NULL, 0); - trace_migration_fd_incoming(fd); - ioc = qio_channel_new_fd(fd, errp); + /* fd is previously created by qmp command 'add-fd' or something else, + * so client is responsible to close it. Dup it to save original value from + * QIOChannel's destructor */ + dup_fd = qemu_dup(fd); + if (dup_fd == -1) { + error_setg(errp, "Cannot dup fd %d: %s (%d)", fd, strerror(errno), + errno); + return; + } + + trace_migration_fd_incoming(fd, dup_fd); + ioc = qio_channel_new_fd(dup_fd, errp); if (!ioc) { - close(fd); + close(dup_fd); return; } diff --git a/migration/trace-events b/migration/trace-events index de2e136e57..d2d30a6b3c 100644 --- a/migration/trace-events +++ b/migration/trace-events @@ -258,8 +258,8 @@ migration_exec_outgoing(const char *cmd) "cmd=%s" migration_exec_incoming(const char *cmd) "cmd=%s" # fd.c -migration_fd_outgoing(int fd) "fd=%d" -migration_fd_incoming(int fd) "fd=%d" +migration_fd_outgoing(int fd, int dup_fd) "fd=%d dup_fd=%d" +migration_fd_incoming(int fd, int dup_fd) "fd=%d dup_fd=%d" # socket.c migration_socket_incoming_accepted(void) ""