From patchwork Thu Dec 13 11:37:51 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Li Qiang X-Patchwork-Id: 10728515 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 5C30C13BF for ; Thu, 13 Dec 2018 11:38:41 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4B9672BC49 for ; Thu, 13 Dec 2018 11:38:41 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3D1FB2BC5B; Thu, 13 Dec 2018 11:38:41 +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=-7.7 required=2.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id CC6F72BC49 for ; Thu, 13 Dec 2018 11:38:40 +0000 (UTC) Received: from localhost ([::1]:51843 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gXPKB-0000sa-Fj for patchwork-qemu-devel@patchwork.kernel.org; Thu, 13 Dec 2018 06:38:39 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50462) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gXPJb-0000Wb-27 for qemu-devel@nongnu.org; Thu, 13 Dec 2018 06:38:03 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gXPJW-0003De-R4 for qemu-devel@nongnu.org; Thu, 13 Dec 2018 06:38:03 -0500 Received: from mail-pg1-x542.google.com ([2607:f8b0:4864:20::542]:33700) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gXPJW-0003Cj-KY for qemu-devel@nongnu.org; Thu, 13 Dec 2018 06:37:58 -0500 Received: by mail-pg1-x542.google.com with SMTP id z11so960877pgu.0 for ; Thu, 13 Dec 2018 03:37:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=by1qgMOXGzr0L42QqtvXxwmeFErOM9kpIv9zb/+twN0=; b=t0fVtjFeaYxfF7HcZ6eT3DP21mRnTTSlS86Ofh/iXeF2e6izicFrlBiBTqJhcHgSav j3W0x+IolckdJQs5wb6JcyXdqmJTGSPFiMgfToFSBWDwYtSOclYrE8UlxwKJxiMpf731 RnjRTPgPQDKFU2hR86apgvRY+Uc+bJzivR56iiIe0aXE5Uc/A+d5q+O+4j4GtUVgzTXC NHOhXQyt2vieurQZD8HSo5P0g1uohfe92ZhcG+91Cn2XXUs5iEYXTmMoK/nlJIxymAzy Sw4T/ZCOCO69sk0Q6LczQ9Ji6mhCqjG25la+5hWapCv9ynI+b7IWFKnbzADSV474ksqG WOjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=by1qgMOXGzr0L42QqtvXxwmeFErOM9kpIv9zb/+twN0=; b=aB5ymelBRriUIJXBiEcT9S3U9xeGG/w6JCNmFyoaJpMjFM09w+5OjlwMhY4lyXdKka PthIfbYmb9+gg07yGtlzpX4nzDKArke/fJYbLoxWyrlcTjuT/QGzCBok8iAukydPgj3K ZcudE5kZHpJVf4O6WauV2+rdzRKQ6K5rLicW2IEoL/b2OVBydrW7fm2Mm36HoC0TliDF pfxHX0el8Ofqm1XXxNOuYgF1fWcxJA5/NQ/7ZE8+fwAv5PV1onGpCxY7wcAjhG1qYJ5U 37XnGzi1sPLpusKfHSVFp7DgX2IGoNcGUYmSmHuZCz5jJ+aK32zR+s0P6fRcmu6gSsmJ CndQ== X-Gm-Message-State: AA+aEWaxwdz9vFo62b/v0Xc0zg+HdZmzjRf1/uMofaOeWk3zSPXcohwA +1JTI9iggA0QyMOPun8MBUs= X-Google-Smtp-Source: AFSGD/W3EePp/ut1BefTjooSLutKn+MJjCua+1/NoQoP337vWzTDK36Bh/96+oSmaHTtoOCImugiyQ== X-Received: by 2002:a62:435a:: with SMTP id q87mr23367300pfa.109.1544701077760; Thu, 13 Dec 2018 03:37:57 -0800 (PST) Received: from localhost.localdomain.localdomain ([103.129.255.252]) by smtp.gmail.com with ESMTPSA id i62sm1910434pge.44.2018.12.13.03.37.55 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 13 Dec 2018 03:37:57 -0800 (PST) From: Li Qiang To: peter.maydell@linaro.org, marcandre.lureau@redhat.com, pbonzini@redhat.com, berrange@redhat.com Date: Thu, 13 Dec 2018 03:37:51 -0800 Message-Id: <1544701071-2922-1-git-send-email-liq3ea@gmail.com> X-Mailer: git-send-email 1.8.3.1 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::542 Subject: [Qemu-devel] [PATCH v2] util: check the return value of fcntl in qemu_set_{block, nonblock} X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Li Qiang , qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Also add diagnostics info in 'qemu_set_cloexec' so that we can know what happen when error occurs. Signed-off-by: Li Qiang --- Change since v1: add diagnostics info util/oslib-posix.c | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/util/oslib-posix.c b/util/oslib-posix.c index c1bee2a581..14cbef1e35 100644 --- a/util/oslib-posix.c +++ b/util/oslib-posix.c @@ -38,6 +38,7 @@ #include #include #include "qemu/cutils.h" +#include "qemu/error-report.h" #ifdef CONFIG_LINUX #include @@ -233,14 +234,32 @@ void qemu_set_block(int fd) { int f; f = fcntl(fd, F_GETFL); - fcntl(fd, F_SETFL, f & ~O_NONBLOCK); + if (f < 0) { + error_report("Unable to get file status flag on fd %d: %s(errno=%d)", + fd, strerror(errno), errno); + abort(); + } + if (fcntl(fd, F_SETFL, f & ~O_NONBLOCK) < 0) { + error_report("Unable to set blocking flag on fd %d: %s(errno=%d)", + fd, strerror(errno), errno); + abort(); + } } void qemu_set_nonblock(int fd) { int f; f = fcntl(fd, F_GETFL); - fcntl(fd, F_SETFL, f | O_NONBLOCK); + if (f < 0) { + error_report("Unable to get file status flag on fd %d: %s(errno=%d)", + fd, strerror(errno), errno); + abort(); + } + if (fcntl(fd, F_SETFL, f | O_NONBLOCK) < 0) { + error_report("Unable to set nonblocking flag on fd %d: %s(errno=%d)", + fd, strerror(errno), errno); + abort(); + } } int socket_set_fast_reuse(int fd) @@ -259,9 +278,17 @@ void qemu_set_cloexec(int fd) { int f; f = fcntl(fd, F_GETFD); - assert(f != -1); - f = fcntl(fd, F_SETFD, f | FD_CLOEXEC); - assert(f != -1); + if (f < 0) { + error_report("Unable to get fd flags on fd %d: %s(errno=%d)", + fd, strerror(errno), errno); + abort(); + } + if (fcntl(fd, F_SETFD, f | FD_CLOEXEC) < 0) { + error_report("Unable to set fd close-on-exec flag on fd %d:" + "%s(errno=%d)", + fd, strerror(errno), errno); + abort(); + } } /*