From patchwork Mon Oct 10 04:04:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bin Meng X-Patchwork-Id: 13002242 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 70417C433FE for ; Mon, 10 Oct 2022 04:15:14 +0000 (UTC) Received: from localhost ([::1]:55622 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ohkC9-0001xf-EP for qemu-devel@archiver.kernel.org; Mon, 10 Oct 2022 00:15:13 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:36672) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ohk2M-0004eC-R4; Mon, 10 Oct 2022 00:05:07 -0400 Received: from mx0a-0064b401.pphosted.com ([205.220.166.238]:33964) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ohk2K-0003GD-R1; Mon, 10 Oct 2022 00:05:06 -0400 Received: from pps.filterd (m0250809.ppops.net [127.0.0.1]) by mx0a-0064b401.pphosted.com (8.17.1.19/8.17.1.5) with ESMTP id 29A3qTi2008565; Sun, 9 Oct 2022 21:04:57 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=windriver.com; h=from : to : cc : subject : date : message-id : content-transfer-encoding : content-type : mime-version; s=PPS06212021; bh=1qeWhE+rJX6yHjv14S0VeaIe0u40rTX5lWhl2aB0Xlo=; b=HdO1pUXLfYgomZl25ZgGZtY+hIUW1DXK1M7l8azSmzfjwFmOn/1jEBidTBeOXINuQWio K/Wb1TQlEZYFCeHtn0C0X3qipOezULjc+NXJXlDqsAYtLFMZ2NaNTmHEzgR29Bf64crK YM0kRBKC/cc+AAex7QejZ6RraNXZm7CcJ0F19NmKFj7i4adNoyEjTD+NCvhlD68eNRiG /cYEi/yupcjYwdi2w39PyWOJj3drXRLx3pegASK+HXQ0Mkbn0pcAk8MtbesgxTgv10at w1CLYfcw64d/F7zBKNU/eVoH4gCAhCdajC1ytwdMIX7nrqKPIWwIfgh8qO9RWrb5/nkP JA== Received: from nam11-dm6-obe.outbound.protection.outlook.com (mail-dm6nam11lp2176.outbound.protection.outlook.com [104.47.57.176]) by mx0a-0064b401.pphosted.com (PPS) with ESMTPS id 3k38qj8yu7-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sun, 09 Oct 2022 21:04:57 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=aM7kKK+i0kBE4RyzmwoeFocDl5WDZDl1hY9cTmXCy+Bo2UVM2Fm8sp0/cczQ22xCw6MVvgsNZxhEah5Eeh9lygGrDktRc+QwL9HMALVDwIuQj5pP1haNin8PyrmIVUz1IJJpmHo/qdU8DTWcPRWUj1XEzhLsjkqTxuDYtuHlgLLFIaeQgIi1hXf981nXDos7ZaP2zQvU3OFg3uQjs6UNHbTFI+arSNb3xdeDN4DVoneWJnlJciqNX2YGE0YGO2Fk3yShVpKeQf/36+dHJL+jUgMWunt4q/+TMVxdLE/SKlyyDFehIHfJUXvhXOkRfEkCEWGmtgqEt9tHJtlP9esh9Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=1qeWhE+rJX6yHjv14S0VeaIe0u40rTX5lWhl2aB0Xlo=; b=IY8S//s50nYf/e8SklNY8FbTc5M6gMKzgf82vdM1T+683d+xd6Wsw7OBnGVGM1avQNSyVPkI0eUoO7bqe0V6kUx3G2TQlEosrXRisKuNoVoFUw5YEj3VQl3YxdBNnN5gi6O1fAtTePWI75tn/CwNm++w6VXoo8SA87UFRv1lfNo61Tr/TGXLDq+CuDtwNjGU8su/3KL8HB/mQCV/iKaE8dNCX4cE6oO+3SdcaMGq2PPzwPY+Sucgn5uI6mbURHKkLq/pbIsLDhyzyRIcJkWhdRbw3g5CsR0tYU1GO4qjU7D7GiWcMkmryhHqKj/9kuiQrIv3kaqOu/oxg0e1Dot1wQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=windriver.com; dmarc=pass action=none header.from=windriver.com; dkim=pass header.d=windriver.com; arc=none Received: from DM4PR11MB5358.namprd11.prod.outlook.com (2603:10b6:5:395::7) by PH7PR11MB5957.namprd11.prod.outlook.com (2603:10b6:510:1e0::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5676.40; Mon, 10 Oct 2022 04:04:54 +0000 Received: from DM4PR11MB5358.namprd11.prod.outlook.com ([fe80::1c50:83e9:d2fa:ebe9]) by DM4PR11MB5358.namprd11.prod.outlook.com ([fe80::1c50:83e9:d2fa:ebe9%8]) with mapi id 15.20.5676.031; Mon, 10 Oct 2022 04:04:53 +0000 From: Bin Meng To: =?utf-8?q?Marc-Andr=C3=A9_Lureau?= , =?utf-8?q?Daniel_P_=2E_Berrang=C3=A9?= , Markus Armbruster Cc: Hanna Reitz , Kevin Wolf , qemu-block@nongnu.org, qemu-devel@nongnu.org Subject: [PATCH v6 1/2] block: Ignore close() failure in get_tmp_filename() Date: Mon, 10 Oct 2022 12:04:30 +0800 Message-Id: <20221010040432.3380478-1-bin.meng@windriver.com> X-Mailer: git-send-email 2.25.1 X-ClientProxiedBy: SL2PR04CA0010.apcprd04.prod.outlook.com (2603:1096:100:2d::22) To DM4PR11MB5358.namprd11.prod.outlook.com (2603:10b6:5:395::7) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM4PR11MB5358:EE_|PH7PR11MB5957:EE_ X-MS-Office365-Filtering-Correlation-Id: a6283e48-96dd-4366-37a9-08daaa749612 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 5vZFUNR9O9twvNnRQUTHmF5yVSIytoSLWzosANnOaClfaegV8wQwLZyOigqP95cM3ZvJDwlkHZYUPnzJ2O5q/UrDemCJu3E8rh94qbkz6VichCsC/UVX3tKRNaKXUhz3vYmaA4TuxIMW/g6bBY2frdAh7V7Ke1AGcwXg85ngCo0XHpL+mIkPrJnpMdJZlMMNolrRsYIzzyF2o27f+hzujhIZS3qfHqIg53H5G47o8ELTuqkeexBqdH45tpGbiVedUm1zPmOqc6wJL7/q8xDyWyLAxRKWXPG5TD5flGiYDltyaQ807DrimKmcJj+sPNFGO5dxzi9C3c9F41PG/dZXRnLOSBhoiAgbpGKcVViPp8M1/RNVnlMPn+j1Xm9o5dVrdi/rdQX/UvX/GWoPJq9BW8USTwmjqXPKx3qTHc5NAOESPdBintITVsgM3Wji5HKi1KHq+2RsU/OpB+vhsBZ/fgeoQ36U0i4JXwlG9qxUnqKFgajfc53PoFvdi2lhQ622CQpDYaNe7awg4hERLvrNXPVu5zzMX5psc8PJZ8mRTYb9YI5gMvI0kG5Z/3HFvuiEShwLxe4idDP6ABdaey+UWzViwEaxbeC5hSaRgH5sk8M4rAHpPlayZW3hWHSBA/OBOBDqB12PrsdofMtYaYo1UCQje1oB82JskjCFgBC+uqqcBuaz1ygOfK4mmHrKeqn0xylej9ggQj7QSd1vDhagi5SLTlXdtkNtLqCglVL5KZxLX6WaWievw9X8ovOnuxS5 X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:DM4PR11MB5358.namprd11.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230022)(4636009)(136003)(39840400004)(376002)(366004)(346002)(396003)(451199015)(54906003)(110136005)(6486002)(66946007)(66556008)(66476007)(4744005)(478600001)(4326008)(41300700001)(44832011)(8676002)(36756003)(5660300002)(316002)(8936002)(6666004)(38350700002)(38100700002)(26005)(6512007)(2616005)(1076003)(86362001)(6506007)(52116002)(83380400001)(186003)(2906002)(66899015); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 2j2Nhwmyv+IGefBrMP9VlgbCfKY7b5TJdv8+mpdZOUYStvtJCi3BWtn/TYwdvsWKXd4VgE2rTheR8cfDJjY2+ecmckbZ+gbYKjXkIoZoXduLS9LuuUx85pzuQm3prggXU48veIxE5yBc8MWZ+2FSQ5tyAxwnqCUdm4oAWhLgJety/OlH/R6hUYwlpDE0oyAvhL6NpvaUMmBOeMS4/hPKBre0VQy0xjvaf/g5okNOVCK8z/r0yG1udhy/Z04bsaxHArdf/uevHE2WBE7ixQieH1vhv9X/lmHKdF5Kfcg7ruZtGIhLnrxilGZyAnTjJPc5VAvVhWCpLNQUeFQoZsJYlPdevKKMrCkF/aGmX0Kv4gq7usltvQtceNcDZ8ZDvJ+f2r0ncWW29AuisWGX/T6AMl80XFqB6Nu3wYA3gBM2YuUXqJSp/xn+NN7hO6Rr7Si3Mzcl75bx+sO98TlNUid5j+2rZJEzXGAo8gb8vtBG5zQ+A6oltJPJSaYoblcJjPTnd9n3dvV/hS5EE7oGwxWOFObOsb7g4wwsSeRRS+8TFGU8//TCZvY1tzU/CG4MpnpWus2HxJBYVl6HXdX9Mv3Ckn97s6vYSwsNTHEIXWpSEuLxAmOb/+WwcwF7QPxO2JqbLvbd6Hu7G2LhkS5gwiKUTavcy1gIgn4thWdVc38OSVed8dRaR55AsAEUSbTlWh+CQ2mvWJ3tGsgmnNdjFTvsXGBdDEUt3+M7FLfJ6UMTSYHvluQOUZ/o5GLMe1d1dnji06lcAQspT9jIsUmJF0aQMyT23rxSvjG7NALtNd0qB3pfQyGAztCgzfriLNJlgtf+KvnNKtZWCCMmg0br6rhLNvVDSYQtNSYc9ApoK+WmfBOPXtYxZznDs9trISI89ZdAxUdHRainn/TlcuqrGfz0eQYl5kWIXucbX94aZz58bSR/qkCrGjljo62BO6sP2iGeYMeMlZyV4NrGKO4x/Q2g+hutuQiuzl6KE22lsd8I8yWrZYDdmeLS2kyyFOeCwIpCRYjgx8UqccbL0mxVPByiEXv9i0IXUJF8WMHsqJIL6jsSYR/OSu60ruflJbHVB9w5eSX8x6Qsf+jmm0VbBspkEEg+FiyWFsa5UDoaSMPBJeRHvkd6Bfs4eyB9uKGm6G7GjKbb9jW6f8dMdO0YBr/FN8wrwUtArN6hjPRPGaA+jnqCUW7OY2tbfuXzTu5DIC5kwCMOIRP80GEquwFrehSPhHUXKoWZ/ZGlx18eq94pdfRUYge8tXMrWRUnOyCAafAe/8cau7uSd/OTGWohCA8z14Ig3+clz+nSM9Yzfc//EdfFZ5eJK5faUFXLNowVMclpAfrD+BRzGa50B8kgs5wOH4WxhcHOe2SLD16d69afk6unkbjRe9z0NJFjMCGLXWSVQLq791+50/ghHOCkp18Ps19Umpjn5jf1Jpmzq5xMdWAl5uqbjz+s3gAurtuwONlu+N/5hPb7nhYqXZ7WROW2Vse07HZFm7syZMmlMWyp3MyOzOVONlpEa/9aWqKXHcEeV9qBS14aWYAx4bdK4biSGNR8dR45hn77tcWJpob5PSq8yCJ/e8VE9YacsVWFua6q X-OriginatorOrg: windriver.com X-MS-Exchange-CrossTenant-Network-Message-Id: a6283e48-96dd-4366-37a9-08daaa749612 X-MS-Exchange-CrossTenant-AuthSource: DM4PR11MB5358.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Oct 2022 04:04:53.8301 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 8ddb2873-a1ad-4a18-ae4e-4644631433be X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: ayXOzWP6+lb3YnJoix2iTG5nfeiOa9kmdFovJfaN6DhBqhQOaGAZVumVjE5BDbB+tHIpEDmM5OZ9IcKxi1eBqQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR11MB5957 X-Proofpoint-ORIG-GUID: 1OXoRG8ddgmsmqPHLe16kPUgJtNQIzCG X-Proofpoint-GUID: 1OXoRG8ddgmsmqPHLe16kPUgJtNQIzCG X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.895,Hydra:6.0.528,FMLib:17.11.122.1 definitions=2022-10-07_04,2022-10-07_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 lowpriorityscore=0 malwarescore=0 spamscore=0 suspectscore=0 priorityscore=1501 mlxlogscore=999 bulkscore=0 impostorscore=0 mlxscore=0 adultscore=0 clxscore=1011 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2209130000 definitions=main-2210100022 Received-SPF: pass client-ip=205.220.166.238; envelope-from=prvs=7282db322c=bin.meng@windriver.com; helo=mx0a-0064b401.pphosted.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" The temporary file has been created and is ready for use. Checking return value of close() does not seem useful. The file descriptor is almost certainly closed; see close(2) under "Dealing with error returns from close()". Let's simply ignore close() failure here. Suggested-by: Markus Armbruster Signed-off-by: Bin Meng Reviewed-by: Markus Armbruster --- (no changes since v5) Changes in v5: - new patch: "block: Ignore close() failure in get_tmp_filename()" block.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/block.c b/block.c index bc85f46eed..582c205307 100644 --- a/block.c +++ b/block.c @@ -886,10 +886,7 @@ int get_tmp_filename(char *filename, int size) if (fd < 0) { return -errno; } - if (close(fd) != 0) { - unlink(filename); - return -errno; - } + close(fd); return 0; #endif } From patchwork Mon Oct 10 04:04:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bin Meng X-Patchwork-Id: 13002240 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id AA461C433FE for ; Mon, 10 Oct 2022 04:10:33 +0000 (UTC) Received: from localhost ([::1]:50444 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ohk7c-0007AI-Ho for qemu-devel@archiver.kernel.org; Mon, 10 Oct 2022 00:10:32 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:36670) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ohk2M-0004dt-CJ; Mon, 10 Oct 2022 00:05:06 -0400 Received: from mx0b-0064b401.pphosted.com ([205.220.178.238]:58942) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ohk2J-0003GK-DX; Mon, 10 Oct 2022 00:05:05 -0400 Received: from pps.filterd (m0250811.ppops.net [127.0.0.1]) by mx0a-0064b401.pphosted.com (8.17.1.19/8.17.1.5) with ESMTP id 29A40txf008795; Mon, 10 Oct 2022 04:04:58 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=windriver.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-type : content-transfer-encoding : mime-version; s=PPS06212021; bh=jvDkOmeb1/yZERBYmTQ0pnfnU2d81Gvgu5RDl+31E/c=; b=TZII8aWp8D93rpPsBj7vXIgnPWit7Etns4IE4KFtCuLUYpHNSp/JWIjLWMJaRfC8m1TE djITrQpmSmrlDlQMeA1JiubSnJLCPe48CCQBJL8qKe1yrsVDMzKP37xIDRRstjy6wU5g gK/+92gw/RwJ36kP54OPEgQTYM7LxprBN6mCgTX/XyaRFZ4Xel7HHcwcpjt3YR0Hg7Vz DIcx2ojkz3vVr24KyX2kGl9ipfOBWxN8Pghdim7SlizWRw//jnUjKZSgHwdNU/PEuJvC Xkwp+rlopIUHYmm9/lLJd3Y8avefOUzfXMTIiboUVI7jIp7DNXq3h3iPnugiRC+JRoip VA== Received: from nam11-dm6-obe.outbound.protection.outlook.com (mail-dm6nam11lp2169.outbound.protection.outlook.com [104.47.57.169]) by mx0a-0064b401.pphosted.com (PPS) with ESMTPS id 3k2ybs17du-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 10 Oct 2022 04:04:58 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=LI7VohA2/Rg1JYPkKmls0FOouvydKd2TW7NUvQvWc19OUzFwD0Dxs4MZ/B3gF+KY6zOqVxxL5iZmGGETVUJ5ZrTm8LDYk93VtZAHi2DZTeJ810sniZg8kLo1Jqf793jpfU1yu3nm6KxwxOYyS8EcAYrKzRuHm9W1FGensj9YoO2xhXicX80ibewHx8bs7gn21v746poKwlEA9oq5faL+wTkA1DCnJ54AZU2u9wd057LGtFLuuZ8mjWTgXnQNxoy6/Xiclsip0DjqnuJJpg7trATnvGK2wUyla+SWTEpVwjCSRYnQWzhIRPzly5CGodMjeIWyLlw8qCpzeXv/GwC7Xw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=jvDkOmeb1/yZERBYmTQ0pnfnU2d81Gvgu5RDl+31E/c=; b=Dcm2dFaaASxCGE0SACVGPnec6mWLRxc6RwN+gAJNMXWUBwfVya2ob9ur4Rb846ExXZCxxeXmm4S44sFi0TI9/kENZfcd0SUlKv4GsJR3uSOJ8Kq9edyYRJICcaP5c4ixBzc4pkd54ru4Rche3PT9v2LyfNcxDUfswnGHKy/cL2K+/xsTp3X0O+SG3zevNqNk5cbVhE4Ha4msYwj1spu85mbpX1UwSVi4d/dYoh5QjkVYPfDOvNJBbHHZ+TUYDa2Fo4xKOqD5hLA9FkNkdtYcBYVwthlkDrRat8qpo/6livUjTDVx7SD4DlnchQc2YJZVeXZqN1DgRdm8ARPlO62daw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=windriver.com; dmarc=pass action=none header.from=windriver.com; dkim=pass header.d=windriver.com; arc=none Received: from DM4PR11MB5358.namprd11.prod.outlook.com (2603:10b6:5:395::7) by PH7PR11MB5957.namprd11.prod.outlook.com (2603:10b6:510:1e0::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5676.40; Mon, 10 Oct 2022 04:04:57 +0000 Received: from DM4PR11MB5358.namprd11.prod.outlook.com ([fe80::1c50:83e9:d2fa:ebe9]) by DM4PR11MB5358.namprd11.prod.outlook.com ([fe80::1c50:83e9:d2fa:ebe9%8]) with mapi id 15.20.5676.031; Mon, 10 Oct 2022 04:04:57 +0000 From: Bin Meng To: =?utf-8?q?Marc-Andr=C3=A9_Lureau?= , =?utf-8?q?Daniel_P_=2E_Berrang=C3=A9?= , Markus Armbruster Cc: Hanna Reitz , Kevin Wolf , qemu-block@nongnu.org, qemu-devel@nongnu.org Subject: [PATCH v6 2/2] block: Refactor get_tmp_filename() Date: Mon, 10 Oct 2022 12:04:31 +0800 Message-Id: <20221010040432.3380478-2-bin.meng@windriver.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221010040432.3380478-1-bin.meng@windriver.com> References: <20221010040432.3380478-1-bin.meng@windriver.com> X-ClientProxiedBy: SL2PR04CA0010.apcprd04.prod.outlook.com (2603:1096:100:2d::22) To DM4PR11MB5358.namprd11.prod.outlook.com (2603:10b6:5:395::7) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM4PR11MB5358:EE_|PH7PR11MB5957:EE_ X-MS-Office365-Filtering-Correlation-Id: 94a4bfcf-8969-4bf5-0192-08daaa7497e8 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: r1AqCl08kx7NpM0A51FnZ+FVuq1DWpMHuuxqusnErl4rneJez+4wSKmlxzdWqcl+sjRTd9imzufbQ0xa9S60sF3bkV8PB3uF+mlZWLWk7wgRE3E8IdvbOI38rDBIjnpgTBEwzez2dEUSV7HyNOWcu4Y+WQQIDMsPT60EEmp8IAaUBfKVFAXvM6I1Te6YinQ20n5N2q6tbWY7wstl4KVy9/jea9ealyQzyomsUEHuei3T9guQadMEsHxeR4aCNK+nms7UWeIPi9YO/EE+1SNJ4I1u9uQPrDLOazrkJX8juDEVYrB5Sq25QE44TF+tcsbypopP+1DaM9Nnlx+hzS4e/PX4sl1xrTT7aXaDqtvvwPSJodOYoAG8vUghiJ4LbbKVRVMmOnRTkgfq/sX8PhTfNjz4P+NRx0iw+LxHK9Lcxj/6t0PX7deXZTAKs+nF8DfqN0glKXzAcSCNhqfHaRFFjly4lJbTEaOZAZ4EGM1ARm9F/UDe2GgNbS4eqZ/plLAVvqb3OPVQ00IvAS9aO81+vPSrLc+ZHRqPA5X0sKzEOqzy0P/y6kfsFIm26ysbI9wk08bPIzIJ77p7qBGKi6QhEZR+TmAXg433e31VBvl2x5DyNBM5QPJEP+eQW6d2pomZ+iL112LXrnK+OkEulgqRzP4pPB3wY37eZXJH9PGov7pKz537cQ9/QfonLA0EP82YKH/S+qrduFM54bG9aninlhai9ZI8oDWDxF4I6LVniek6EbeigfG+UaA2+UjtXWq0qe4nwyGLkbWt7W3AkGj00A== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:DM4PR11MB5358.namprd11.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230022)(4636009)(136003)(39840400004)(376002)(366004)(346002)(396003)(451199015)(54906003)(110136005)(6486002)(66946007)(66556008)(66476007)(478600001)(4326008)(41300700001)(44832011)(8676002)(36756003)(5660300002)(316002)(8936002)(6666004)(38350700002)(38100700002)(26005)(6512007)(2616005)(1076003)(86362001)(6506007)(52116002)(83380400001)(186003)(2906002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?6P9UWWyKxfuNU7cqgAIrtmtTGkyt?= =?utf-8?q?MZZkh6M7IpCj9DBXVf6lR2EX7ec2BUnXZoVcxa59pyEXQpUdmXoYTP7I5GvOBxxhs?= =?utf-8?q?iJOXs16BJVIIFIyBEvfHW1GRckiTTf7GuIX0fH7Jfh8XquNgHkAqHkoD0DTehLoOh?= =?utf-8?q?4JskGKU+NEofxyagZjqht1lE7+3RGGEIrpQVqDsiaE9/r1ajjytu1ZXG2TyMQ2M6i?= =?utf-8?q?e/8On2alQw1hqFD+urGEba+inXXafZBF5/IXKSLmnlunCi57FwHE83FfUgSz3qciB?= =?utf-8?q?iMVr0ORrpDVUGoUGncGYuEjigR25uBeHa82UmDGnShV/LftH8EZ0JT2vNSSsIAfMG?= =?utf-8?q?EI6NwHQ3i7v49bFuY7aYB6wpBSLuHo5AombcjAcbcC4QmmeYhXKPoaCk+ZQENKs3v?= =?utf-8?q?v5ee6WQla3mhXadZTUa27Hk9yS11gDOn35nEZ0+y3E3p1kUlKKX6H26Fcd0Rug2p5?= =?utf-8?q?yV810jCidXeAvq7DD2C874e3Nk5ZnX6xs7bwUjFOTeUTkMxMnujJvnc3+usc3mv9s?= =?utf-8?q?cpaKb5aj0YYJ5Uk0aEz96GH8MuFcGuyWBQuEISPg+LPHxqDCS8jms5H+2zjbikypv?= =?utf-8?q?5gPQeybpC+PbBHQDAVtQysZ4FjLMVnh1gRUyx+80CacCVrrMU4dfcmBjf7e4j+O/Y?= =?utf-8?q?Rr+CsonEZ3p4TDz+aKDo3HeOeYf+DGVWCBPf856iMAp8tHjkyxhvu+m1lgqj5HHBB?= =?utf-8?q?bS8v93Z09doX1WCkAKuimqG0Yp6O0j68Ld4ayMnpFVY33jBpstJTNPehA+maZg6Te?= =?utf-8?q?tMaLWu3Qs+1cU1CWM5FBG0dG9nGUvEdSDKRQ8UjFdK5cCrgvyzZNbBGCAKPIBwLXY?= =?utf-8?q?iJN1hK8DbrS1uI+/GecmhJIXkkGWXwue6lSvPhXK5j8XutJkb5fNiShQ2B7xdQCVR?= =?utf-8?q?g7BJBQVdnI6TYv4XIL88erZqrUkAQTR3fad63w2SccqQMK+vh9YmwVEHlrAgo273Y?= =?utf-8?q?caKGtz90FcQcmpdW/AlMRkNQCexNCjHARnimKIkba9bZ+K996cHa/oNcopJqTIMq2?= =?utf-8?q?5sx8goJw9WKD+zMe4jgszWgmPsOIrsvVXZpT5/pl3N6spD0fdPzSULzj3Bq7vDdmf?= =?utf-8?q?mq07kwCRiSTze8o+QIqzMZF4vKo5YYMRwcaHdTR9s8qcaujt7tOWTl0wZsfs/abNx?= =?utf-8?q?40S/l1yeoRPsGa9lAchOtbQ6I5qcoipF6EOhffRFkw8jVMP4sLfM8kETxeIpv/FNx?= =?utf-8?q?P1+mhbeJAublHiMZos70imhgl0MYH5egwlWv/PgkgkZryDjo45Z5vLHGbp2rQS8dh?= =?utf-8?q?RTyY0VJuy/52/cbljYOeyTsSMIA2A30Qrq+TiCMTgA7YTukzJ9WQWCd/8Vs3BbbM/?= =?utf-8?q?feJXnWi64YDWDQm7CmNsmvK/1A5cp9BF/iCEGrHH+GF7ICHzzJ0vZQFPFD16ojxB7?= =?utf-8?q?gL38KLcUJIZ/Dp47PLBdCWs2Qb4WDzZGinKKlIPG0zY1yoqlnQTcXVYAB+2rOSKsh?= =?utf-8?q?0E8VZaSMUiFZQb06AUs+P0WNpxuxWcFZoTqQBowa7UmsdvmornkDVVr9E/2ceGlo2?= =?utf-8?q?lVgnShrjC0ghU5goBYYog2o0+722U+XzUQ=3D=3D?= X-OriginatorOrg: windriver.com X-MS-Exchange-CrossTenant-Network-Message-Id: 94a4bfcf-8969-4bf5-0192-08daaa7497e8 X-MS-Exchange-CrossTenant-AuthSource: DM4PR11MB5358.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Oct 2022 04:04:56.9870 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 8ddb2873-a1ad-4a18-ae4e-4644631433be X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: YWXQSE8C6ygRCTS/t21N2Ed29nU/v180Y2F0FH0ohnBCLfrS+fmkar2Xoz/eKLENbYxF41x/L7rQ2fua/IvzDQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR11MB5957 X-Proofpoint-ORIG-GUID: MCU1b7lpZW1vQ5G9mUQkmWyRP-LBeCEq X-Proofpoint-GUID: MCU1b7lpZW1vQ5G9mUQkmWyRP-LBeCEq X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.895,Hydra:6.0.528,FMLib:17.11.122.1 definitions=2022-10-07_04,2022-10-07_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 malwarescore=0 bulkscore=0 clxscore=1015 priorityscore=1501 impostorscore=0 phishscore=0 mlxlogscore=999 spamscore=0 suspectscore=0 lowpriorityscore=0 mlxscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2209130000 definitions=main-2210100022 Received-SPF: pass client-ip=205.220.178.238; envelope-from=prvs=7282db322c=bin.meng@windriver.com; helo=mx0b-0064b401.pphosted.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" At present there are two callers of get_tmp_filename() and they are inconsistent. One does: /* TODO: extra byte is a hack to ensure MAX_PATH space on Windows. */ char *tmp_filename = g_malloc0(PATH_MAX + 1); ... ret = get_tmp_filename(tmp_filename, PATH_MAX + 1); while the other does: s->qcow_filename = g_malloc(PATH_MAX); ret = get_tmp_filename(s->qcow_filename, PATH_MAX); As we can see different 'size' arguments are passed. There are also platform specific implementations inside the function, and the use of snprintf is really undesirable. The function name is also misleading. It creates a temporary file, not just a filename. Refactor this routine by changing its name and signature to: char *create_tmp_file(Error **errp) and use g_get_tmp_dir() / g_mkstemp() for a consistent implementation. While we are here, add some comments to mention that /var/tmp is preferred over /tmp on non-win32 hosts. Signed-off-by: Bin Meng --- Changes in v6: - use g_mkstemp() and stick to use /var/tmp for non-win32 hosts Changes in v5: - minor change in the commit message - add some notes in the function comment block - add g_autofree for tmp_filename Changes in v4: - Rename the function to create_tmp_file() and take "Error **errp" as a parameter, so that callers can pass errp all the way down to this routine. - Commit message updated to reflect the latest change Changes in v3: - Do not use errno directly, instead still let get_tmp_filename() return a negative number to indicate error Changes in v2: - Use g_autofree and g_steal_pointer include/block/block_int-common.h | 2 +- block.c | 56 +++++++++++++++++--------------- block/vvfat.c | 7 ++-- 3 files changed, 34 insertions(+), 31 deletions(-) diff --git a/include/block/block_int-common.h b/include/block/block_int-common.h index 8947abab76..d7c0a7e96f 100644 --- a/include/block/block_int-common.h +++ b/include/block/block_int-common.h @@ -1230,7 +1230,7 @@ static inline BlockDriverState *child_bs(BdrvChild *child) } int bdrv_check_request(int64_t offset, int64_t bytes, Error **errp); -int get_tmp_filename(char *filename, int size); +char *create_tmp_file(Error **errp); void bdrv_parse_filename_strip_prefix(const char *filename, const char *prefix, QDict *options); diff --git a/block.c b/block.c index 582c205307..8eeaa5623e 100644 --- a/block.c +++ b/block.c @@ -860,35 +860,42 @@ int bdrv_probe_geometry(BlockDriverState *bs, HDGeometry *geo) /* * Create a uniquely-named empty temporary file. - * Return 0 upon success, otherwise a negative errno value. + * Return the actual file name used upon success, otherwise NULL. + * This string should be freed with g_free() when not needed any longer. + * + * Note: creating a temporary file for the caller to (re)open is + * inherently racy. Use g_file_open_tmp() instead whenever practical. */ -int get_tmp_filename(char *filename, int size) +char *create_tmp_file(Error **errp) { -#ifdef _WIN32 - char temp_dir[MAX_PATH]; - /* GetTempFileName requires that its output buffer (4th param) - have length MAX_PATH or greater. */ - assert(size >= MAX_PATH); - return (GetTempPath(MAX_PATH, temp_dir) - && GetTempFileName(temp_dir, "qem", 0, filename) - ? 0 : -GetLastError()); -#else int fd; const char *tmpdir; - tmpdir = getenv("TMPDIR"); - if (!tmpdir) { + g_autofree char *filename = NULL; + + tmpdir = g_get_tmp_dir(); +#ifndef _WIN32 + /* + * See commit 69bef79 ("block: use /var/tmp instead of /tmp for -snapshot") + * + * This function is used to create temporary disk images (like -snapshot), + * so the files can become very large. /tmp is often a tmpfs where as + * /var/tmp is usually on a disk, so more appropriate for disk images. + */ + if (!g_strcmp0(tmpdir, "/tmp")) { tmpdir = "/var/tmp"; } - if (snprintf(filename, size, "%s/vl.XXXXXX", tmpdir) >= size) { - return -EOVERFLOW; - } - fd = mkstemp(filename); +#endif + + filename = g_strdup_printf("%s/vl.XXXXXX", tmpdir); + fd = g_mkstemp(filename); if (fd < 0) { - return -errno; + error_setg_errno(errp, -errno, "Could not open temporary file '%s'", + filename); + return NULL; } close(fd); - return 0; -#endif + + return g_steal_pointer(&filename); } /* @@ -3714,8 +3721,7 @@ static BlockDriverState *bdrv_append_temp_snapshot(BlockDriverState *bs, QDict *snapshot_options, Error **errp) { - /* TODO: extra byte is a hack to ensure MAX_PATH space on Windows. */ - char *tmp_filename = g_malloc0(PATH_MAX + 1); + g_autofree char *tmp_filename = NULL; int64_t total_size; QemuOpts *opts = NULL; BlockDriverState *bs_snapshot = NULL; @@ -3734,9 +3740,8 @@ static BlockDriverState *bdrv_append_temp_snapshot(BlockDriverState *bs, } /* Create the temporary image */ - ret = get_tmp_filename(tmp_filename, PATH_MAX + 1); - if (ret < 0) { - error_setg_errno(errp, -ret, "Could not get temporary filename"); + tmp_filename = create_tmp_file(errp); + if (!tmp_filename) { goto out; } @@ -3770,7 +3775,6 @@ static BlockDriverState *bdrv_append_temp_snapshot(BlockDriverState *bs, out: qobject_unref(snapshot_options); - g_free(tmp_filename); return bs_snapshot; } diff --git a/block/vvfat.c b/block/vvfat.c index d6dd919683..f9bf8406d3 100644 --- a/block/vvfat.c +++ b/block/vvfat.c @@ -3146,10 +3146,9 @@ static int enable_write_target(BlockDriverState *bs, Error **errp) array_init(&(s->commits), sizeof(commit_t)); - s->qcow_filename = g_malloc(PATH_MAX); - ret = get_tmp_filename(s->qcow_filename, PATH_MAX); - if (ret < 0) { - error_setg_errno(errp, -ret, "can't create temporary file"); + s->qcow_filename = create_tmp_file(errp); + if (!s->qcow_filename) { + ret = -ENOENT; goto err; }