From nobody Thu Dec 26 19:43:52 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1652465654; cv=none; d=zohomail.com; s=zohoarc; b=I/YnpqGjL0vpRhwNKxqTg4VmqK5FgyFmRGDbzw4jUcMyGCZEc42LyKHxE1tpRG7mbT3lovnmUVfVHD1gWH9OirkoIUP7jp/xKo8Qp2BauRPgLDp9T148Qq9PaMPE+5U/3R9oYlfNP5mSrJ12zrjY1ENvn/g3xJmpPPnDKlLxpEc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1652465654; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=5o9fChlsjwtRFsvSIL+alf7xKwV3z5PfCag2L300olU=; b=Pfc5IPbisgILWFyKFV/UEeHsTRBpVHjNH7LgHDrzo+i6HlfFm+WmAUXDHW0NbWyr7RSbxrjnIV75hpEV9XwpZmRqyUpjE6Nvvaahr+HrpemL4pXGmNwG4ru53zBXXLuScAybyDxWaG0kZ8MOKZ9H9spg4yDxbW2cvVLDCURi0Bg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1652465654512731.9368219147805; Fri, 13 May 2022 11:14:14 -0700 (PDT) Received: from localhost ([::1]:44164 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1npZnp-0001B2-1d for importer2@patchew.org; Fri, 13 May 2022 14:14:13 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:56570) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1npZiS-0000fM-Ok for qemu-devel@nongnu.org; Fri, 13 May 2022 14:08:40 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:37213) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1npZiQ-000708-Su for qemu-devel@nongnu.org; Fri, 13 May 2022 14:08:40 -0400 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-391-kpUkhDgnNh252WU0cgOvLA-1; Fri, 13 May 2022 14:08:34 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D0C6B2919EBE; Fri, 13 May 2022 18:08:33 +0000 (UTC) Received: from localhost (unknown [10.39.192.38]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2516740D2820; Fri, 13 May 2022 18:08:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1652465318; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=5o9fChlsjwtRFsvSIL+alf7xKwV3z5PfCag2L300olU=; b=bQbQpEZHjm2h3vIvusGJVLenm4i1SeaGhjdMSHirt1kEKOGTukwn4bZ1y+AESmsfXRH07n xwgXv2qy2YnWH6IiKQsCX+jC6Dtf/+kaz0Ky0kMDbZ33vvpwth1NJIsf6fQZ2R4st2cku3 2aCN739RHn7gSB/xA3P9mEg//cJaCCA= X-MC-Unique: kpUkhDgnNh252WU0cgOvLA-1 From: marcandre.lureau@redhat.com To: qemu-devel@nongnu.org Cc: Konstantin Kostiuk , Michael Roth , Markus Armbruster , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Subject: [PATCH v3 04/15] qga: flatten safe_open_or_create() Date: Fri, 13 May 2022 20:08:10 +0200 Message-Id: <20220513180821.905149-5-marcandre.lureau@redhat.com> In-Reply-To: <20220513180821.905149-1-marcandre.lureau@redhat.com> References: <20220513180821.905149-1-marcandre.lureau@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.84 on 10.11.54.2 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=marcandre.lureau@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -28 X-Spam_score: -2.9 X-Spam_bar: -- X-Spam_report: (-2.9 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, 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, T_SCC_BODY_TEXT_LINE=-0.01 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+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1652465656269100001 From: Marc-Andr=C3=A9 Lureau There is a bit too much nesting in the function, this can be simplified a bit to improve readability. This also helps with the following error handling changes. Signed-off-by: Marc-Andr=C3=A9 Lureau Reviewed-by: Markus Armbruster --- qga/commands-posix.c | 122 ++++++++++++++++++++++--------------------- 1 file changed, 62 insertions(+), 60 deletions(-) diff --git a/qga/commands-posix.c b/qga/commands-posix.c index 69f209af87..15eb7cb77d 100644 --- a/qga/commands-posix.c +++ b/qga/commands-posix.c @@ -339,73 +339,75 @@ find_open_flag(const char *mode_str, Error **errp) static FILE * safe_open_or_create(const char *path, const char *mode, Error **errp) { - Error *local_err =3D NULL; int oflag; + int fd =3D -1; + FILE *f =3D NULL; + + oflag =3D find_open_flag(mode, errp); + if (oflag < 0) { + goto end; + } + + /* If the caller wants / allows creation of a new file, we implement it + * with a two step process: open() + (open() / fchmod()). + * + * First we insist on creating the file exclusively as a new file. If + * that succeeds, we're free to set any file-mode bits on it. (The + * motivation is that we want to set those file-mode bits independently + * of the current umask.) + * + * If the exclusive creation fails because the file already exists + * (EEXIST is not possible for any other reason), we just attempt to + * open the file, but in this case we won't be allowed to change the + * file-mode bits on the preexistent file. + * + * The pathname should never disappear between the two open()s in + * practice. If it happens, then someone very likely tried to race us. + * In this case just go ahead and report the ENOENT from the second + * open() to the caller. + * + * If the caller wants to open a preexistent file, then the first + * open() is decisive and its third argument is ignored, and the second + * open() and the fchmod() are never called. + */ + fd =3D open(path, oflag | ((oflag & O_CREAT) ? O_EXCL : 0), 0); + if (fd =3D=3D -1 && errno =3D=3D EEXIST) { + oflag &=3D ~(unsigned)O_CREAT; + fd =3D open(path, oflag); + } + if (fd =3D=3D -1) { + error_setg_errno(errp, errno, + "failed to open file '%s' " + "(mode: '%s')", + path, mode); + goto end; + } =20 - oflag =3D find_open_flag(mode, &local_err); - if (local_err =3D=3D NULL) { - int fd; - - /* If the caller wants / allows creation of a new file, we impleme= nt it - * with a two step process: open() + (open() / fchmod()). - * - * First we insist on creating the file exclusively as a new file.= If - * that succeeds, we're free to set any file-mode bits on it. (The - * motivation is that we want to set those file-mode bits independ= ently - * of the current umask.) - * - * If the exclusive creation fails because the file already exists - * (EEXIST is not possible for any other reason), we just attempt = to - * open the file, but in this case we won't be allowed to change t= he - * file-mode bits on the preexistent file. - * - * The pathname should never disappear between the two open()s in - * practice. If it happens, then someone very likely tried to race= us. - * In this case just go ahead and report the ENOENT from the second - * open() to the caller. - * - * If the caller wants to open a preexistent file, then the first - * open() is decisive and its third argument is ignored, and the s= econd - * open() and the fchmod() are never called. - */ - fd =3D open(path, oflag | ((oflag & O_CREAT) ? O_EXCL : 0), 0); - if (fd =3D=3D -1 && errno =3D=3D EEXIST) { - oflag &=3D ~(unsigned)O_CREAT; - fd =3D open(path, oflag); - } + qemu_set_cloexec(fd); =20 - if (fd =3D=3D -1) { - error_setg_errno(&local_err, errno, "failed to open file '%s' " - "(mode: '%s')", path, mode); - } else { - qemu_set_cloexec(fd); + if ((oflag & O_CREAT) && fchmod(fd, DEFAULT_NEW_FILE_MODE) =3D=3D -1) { + error_setg_errno(errp, errno, + "failed to set permission 0%03o on new file '%s' = (mode: '%s')", + (unsigned)DEFAULT_NEW_FILE_MODE, path, mode); + goto end; + } =20 - if ((oflag & O_CREAT) && fchmod(fd, DEFAULT_NEW_FILE_MODE) =3D= =3D -1) { - error_setg_errno(&local_err, errno, "failed to set permiss= ion " - "0%03o on new file '%s' (mode: '%s')", - (unsigned)DEFAULT_NEW_FILE_MODE, path, mo= de); - } else { - FILE *f; - - f =3D fdopen(fd, mode); - if (f =3D=3D NULL) { - error_setg_errno(&local_err, errno, "failed to associa= te " - "stdio stream with file descriptor %d= , " - "file '%s' (mode: '%s')", fd, path, m= ode); - } else { - return f; - } - } + f =3D fdopen(fd, mode); + if (f =3D=3D NULL) { + error_setg_errno(errp, errno, + "failed to associate stdio stream with file descr= iptor %d, " + "file '%s' (mode: '%s')", + fd, path, mode); + } =20 - close(fd); - if (oflag & O_CREAT) { - unlink(path); - } +end: + if (f =3D=3D NULL && fd !=3D -1) { + close(fd); + if (oflag & O_CREAT) { + unlink(path); } } - - error_propagate(errp, local_err); - return NULL; + return f; } =20 int64_t qmp_guest_file_open(const char *path, bool has_mode, const char *m= ode, --=20 2.36.1