From nobody Mon Sep 16 19:37:30 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=reject dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1719585108; cv=none; d=zohomail.com; s=zohoarc; b=fU9uXC1vwm3kaEt4n15XrZCIfPsP+22O2r4qBJoyGuBB/J7tnRci4dxMDVf7lFGyuX1h3ysEGJ05/rhV4nikyQhb99awm4vrlNxWwLF054cGOo7j9PiPnX1sJH3jKc65bwDpOIfZJYSJovwWkMqWzTNiR3jV88QnWsDMkvbdUA8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1719585108; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Rfv1TRT8DgfBFjODV1M7RVz/7dh85giKSn0Y1bjukyE=; b=UWnikokC+SgmDX+HK9/sS0XIGwH/qrrm/A9GzSyf/4d8H/eSJhmOTZhxzYrXhIpSwf15+Q5wMk8jOJz/FHuc48PrrPkIu3kegsD5Ip9L5SW9ZQJ3844l4po7AvJWU0PtssYssNSoBxKwSAHC4SMX67w7NvsgXV+UQCsfdXTeE6Y= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1719585108557551.3359230832938; Fri, 28 Jun 2024 07:31:48 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.750661.1158730 (Exim 4.92) (envelope-from ) id 1sNCdJ-0001w4-GY; Fri, 28 Jun 2024 14:31:25 +0000 Received: by outflank-mailman (output) from mailman id 750661.1158730; Fri, 28 Jun 2024 14:31:25 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sNCdJ-0001vx-Dw; Fri, 28 Jun 2024 14:31:25 +0000 Received: by outflank-mailman (input) for mailman id 750661; Fri, 28 Jun 2024 14:31:24 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sNCdI-0001vg-3X for xen-devel@lists.xenproject.org; Fri, 28 Jun 2024 14:31:24 +0000 Received: from mail-ej1-x62b.google.com (mail-ej1-x62b.google.com [2a00:1450:4864:20::62b]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 17d9ff0d-355b-11ef-90a3-e314d9c70b13; Fri, 28 Jun 2024 16:31:23 +0200 (CEST) Received: by mail-ej1-x62b.google.com with SMTP id a640c23a62f3a-a724a8097deso83092866b.1 for ; Fri, 28 Jun 2024 07:31:23 -0700 (PDT) Received: from andrewcoop.eng.citrite.net ([160.101.139.1]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a72aaf63390sm84944566b.69.2024.06.28.07.31.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Jun 2024 07:31:21 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 17d9ff0d-355b-11ef-90a3-e314d9c70b13 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.com; s=google; t=1719585082; x=1720189882; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Rfv1TRT8DgfBFjODV1M7RVz/7dh85giKSn0Y1bjukyE=; b=bkOg5IW+rHEEaDaqvba9+5iGflgAT1BjJ+MC6Ag4h1v1Rqr3BLpGfCAgnpibAp3LfH vfjZmjCAd4jTXoawwMKe9ykxHCDSjj8AMfSsimG3dTAUaPrDk/9ubKDjqwFoHofT0pM+ XusQ2/PbmcUMaFgU/ix+tzHVZOB47kfSlJ+G0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719585082; x=1720189882; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Rfv1TRT8DgfBFjODV1M7RVz/7dh85giKSn0Y1bjukyE=; b=sjrXKQpxXRX8vwlzzTvYabxjrTqvSvHe2qZITYmgMiYghk0Rz2PvyNvoxj8A8aOVea BMHKjYjGbHZWmsgZXwAPwryoPByYZUzZqErxDEi6t/JQlPV/l5SWnDbDX1JwKIMjBsTl 7AwBkq9+9VDUZj7aBkAVbibMc9HYoxOynLLNA3G3o10H5jrtsu+r+/Fi9EBk+90NLS37 0f1nk6jz0KI8ZU9dSzcoHuVLCswRpNLRBphWQfJtUM48RD2ng4y7CV0DWSkyuhFiufIl ZW1JkDBv9qr8cjVcMdUtUUHEcAlC3EFUX9rHp/0cHHWvPRUCuW3xWZLNZ27PbGh2zb01 MgRA== X-Gm-Message-State: AOJu0YzLOsjlja6uClcL6znYa1F53XlrBLAFslKCoDAOG/85yrQBrq3+ ++PQ5u8UXOaef1lB72I2rYaEAHVl0R9aGPKvW94MLRyDFlVnqToUY97wK71kiSxt5/Nktvx9L1K iUuY= X-Google-Smtp-Source: AGHT+IFPs7aSrfbhtvCVV1yH+c3angPA7Ax0G4Igm+BQEAUJNpmkSc8KgjnrxHqYCsmPzCLZvUgy7g== X-Received: by 2002:a17:906:3651:b0:a72:50f7:3c6f with SMTP id a640c23a62f3a-a7250f744ccmr856963266b.14.1719585081777; Fri, 28 Jun 2024 07:31:21 -0700 (PDT) From: Andrew Cooper To: Xen-devel Cc: Andrew Cooper , Anthony PERARD , Juergen Gross , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Frediano Ziglio , Oleksii Kurochko Subject: [PATCH for-4.19 1/3] tools/libxs: Fix CLOEXEC handling in get_dev() Date: Fri, 28 Jun 2024 15:31:14 +0100 Message-Id: <20240628143116.1044976-2-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240628143116.1044976-1-andrew.cooper3@citrix.com> References: <20240628143116.1044976-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @citrix.com) X-ZM-MESSAGEID: 1719585109725100005 Move the O_CLOEXEC compatibility outside of an #ifdef USE_PTHREAD block. Introduce set_cloexec() to wrap fcntl() setting FD_CLOEXEC. It will be reu= sed for other CLOEXEC fixes too. Use set_cloexec() when O_CLOEXEC isn't available as a best-effort fallback. Fixes: f4f2f3402b2f ("tools/libxs: Open /dev/xen/xenbus fds as O_CLOEXEC") Signed-off-by: Andrew Cooper Acked-by: Anthony PERARD Reviewed-by: Juergen Gross --- CC: Anthony PERARD CC: Juergen Gross CC: Roger Pau Monn=C3=A9 CC: Frediano Ziglio CC: Oleksii Kurochko --- tools/libs/store/xs.c | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/tools/libs/store/xs.c b/tools/libs/store/xs.c index 14985150737e..037e79d98b58 100644 --- a/tools/libs/store/xs.c +++ b/tools/libs/store/xs.c @@ -40,6 +40,10 @@ #include #include =20 +#ifndef O_CLOEXEC +#define O_CLOEXEC 0 +#endif + struct xs_stored_msg { XEN_TAILQ_ENTRY(struct xs_stored_msg) list; struct xsd_sockmsg hdr; @@ -54,10 +58,6 @@ struct xs_stored_msg { #include #endif =20 -#ifndef O_CLOEXEC -#define O_CLOEXEC 0 -#endif - struct xs_handle { /* Communications channel to xenstore daemon. */ int fd; @@ -176,6 +176,16 @@ static bool setnonblock(int fd, int nonblock) { return true; } =20 +static bool set_cloexec(int fd) +{ + int flags =3D fcntl(fd, F_GETFL); + + if (flags < 0) + return false; + + return fcntl(fd, flags | FD_CLOEXEC) >=3D 0; +} + int xs_fileno(struct xs_handle *h) { char c =3D 0; @@ -230,8 +240,24 @@ static int get_socket(const char *connect_to) =20 static int get_dev(const char *connect_to) { - /* We cannot open read-only because requests are writes */ - return open(connect_to, O_RDWR | O_CLOEXEC); + int fd, saved_errno; + + fd =3D open(connect_to, O_RDWR | O_CLOEXEC); + if (fd < 0) + return -1; + + /* Compat for non-O_CLOEXEC environments. Racy. */ + if (!O_CLOEXEC && !set_cloexec(fd)) + goto error; + + return fd; + +error: + saved_errno =3D errno; + close(fd); + errno =3D saved_errno; + + return -1; } =20 static int all_restrict_cb(Xentoolcore__Active_Handle *ah, domid_t domid) { --=20 2.39.2 From nobody Mon Sep 16 19:37:30 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=reject dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1719585105; cv=none; d=zohomail.com; s=zohoarc; b=gOFyTs78a6OaHeInQzq8hAI/Fu1vnfY3tF9ECnLj1bCz84fpFfZQO+tl2EDKSAoSFbWR+8qEnl6KoNdBOtvcQWpZSo4TRhfG9dHXknKvqfufsep4g5cT4eRMxxDyFGSlxu0Skeojll7WomCKE1gd+4LTt4qjoahUjlWgPBDBkKA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1719585105; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=0HcoQ3jxCZ06h2GCy+THa2ZWR0EVbPJaxvXputzPwx0=; b=DVdZdTbwKFGw8IwJlADMyWFXxpYK89HEjuo4HHnDiZenxjnZI72rh8k6QASA3T+rv6J/9GZ2gCSUXk4ZnXjCh4cOHz6p5bOv97VlMJAwsKRXrh7fkwLOm+yMDT/ouJWzCXxl2KjjBN4aifFCi+mm4HSnL/6w9m+2dFsLkKujrw8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1719585105412153.7456543793403; Fri, 28 Jun 2024 07:31:45 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.750662.1158737 (Exim 4.92) (envelope-from ) id 1sNCdJ-0001yn-Qy; Fri, 28 Jun 2024 14:31:25 +0000 Received: by outflank-mailman (output) from mailman id 750662.1158737; Fri, 28 Jun 2024 14:31:25 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sNCdJ-0001xe-K0; Fri, 28 Jun 2024 14:31:25 +0000 Received: by outflank-mailman (input) for mailman id 750662; Fri, 28 Jun 2024 14:31:24 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sNCdI-0001vg-PW for xen-devel@lists.xenproject.org; Fri, 28 Jun 2024 14:31:24 +0000 Received: from mail-ed1-x529.google.com (mail-ed1-x529.google.com [2a00:1450:4864:20::529]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 1806494c-355b-11ef-90a3-e314d9c70b13; Fri, 28 Jun 2024 16:31:23 +0200 (CEST) Received: by mail-ed1-x529.google.com with SMTP id 4fb4d7f45d1cf-57d1d614049so953588a12.1 for ; Fri, 28 Jun 2024 07:31:23 -0700 (PDT) Received: from andrewcoop.eng.citrite.net ([160.101.139.1]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a72aaf63390sm84944566b.69.2024.06.28.07.31.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Jun 2024 07:31:22 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 1806494c-355b-11ef-90a3-e314d9c70b13 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.com; s=google; t=1719585082; x=1720189882; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=0HcoQ3jxCZ06h2GCy+THa2ZWR0EVbPJaxvXputzPwx0=; b=BziVxMbWZMiCPHzA6+OkT0V4E+B6qYrEQqWFeRux6NsXy/SUmDo4wLU0zafrpyCsVx nD2ZfNWSQMHlNYq5OVPGEutK2h9KHhD0SvDRUK0md+v94ol3E/30UvwulSqbJoQKWc67 6UVfsz2sF2jk5HYyxO0nXCSYFQVQYpe4A7tnY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719585082; x=1720189882; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=0HcoQ3jxCZ06h2GCy+THa2ZWR0EVbPJaxvXputzPwx0=; b=g+nUO7xHhvel+mBgPyKVVtCnGsCCHBMHsVypPDjIATT2zmZryP2ww46u3eayJsup3A sD4kQEntRgVpq46koieEzHsxOwuAHfL2XnpsgkEjpRmneHCoFWkntcItl+jzxgTvLUis SdFuvAhmC6YcRHZM/xL+PGFr+OmaMOj8oyZLUQ6HenoeZIoUCe1JiFA2mlum60c+1q/f kbFm64EMOgFaTsnF/y8vBQlW8YHZCBa/XUrboICzqUvdN4NV6tV4BvZU6cYyfs1/W9+h bW7SrrLe+saobajFHPk2/y7v59OiIO6hduV4Wc3o4x6bFQKRjkRLfD2L00vawzFToP67 3jww== X-Gm-Message-State: AOJu0YyykN652JVhpfgdjNsmzKnUB8xijy4F2GOQ4EC6ZPB3NEqyYoet fx8ilydrql/owRivU4k0eUIqRikLZe2m7Ago/Wxn0YPM2fV7kPLGHksHMBXrCVYGA/jtuxEhxyE h+dk= X-Google-Smtp-Source: AGHT+IF9o3Kg7jr3zKcI7qTmqOEqOBoKhlT5CXjFMwttb5naf+XjdFMl8lOQTAOMnf7ijdGKtzbaQw== X-Received: by 2002:a17:906:f59b:b0:a72:b34f:e15b with SMTP id a640c23a62f3a-a72b34fe2e9mr130747566b.57.1719585082472; Fri, 28 Jun 2024 07:31:22 -0700 (PDT) From: Andrew Cooper To: Xen-devel Cc: Andrew Cooper , Frediano Ziglio , Anthony PERARD , Juergen Gross , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Oleksii Kurochko Subject: [PATCH for-4.19 2/3] tools/libxs: Fix CLOEXEC handling in get_socket() Date: Fri, 28 Jun 2024 15:31:15 +0100 Message-Id: <20240628143116.1044976-3-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240628143116.1044976-1-andrew.cooper3@citrix.com> References: <20240628143116.1044976-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @citrix.com) X-ZM-MESSAGEID: 1719585105935100001 get_socket() opens a socket, then uses fcntl() to set CLOEXEC. This is racy with exec(). Open the socket with SOCK_CLOEXEC. Use the same compatibility strategy as O_CLOEXEC on ancient versions of Linux. Reported-by: Frediano Ziglio Signed-off-by: Andrew Cooper Acked-by: Anthony PERARD Reviewed-by: Juergen Gross --- CC: Anthony PERARD CC: Juergen Gross CC: Roger Pau Monn=C3=A9 CC: Frediano Ziglio CC: Oleksii Kurochko --- tools/libs/store/xs.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/tools/libs/store/xs.c b/tools/libs/store/xs.c index 037e79d98b58..11a766c50887 100644 --- a/tools/libs/store/xs.c +++ b/tools/libs/store/xs.c @@ -44,6 +44,10 @@ #define O_CLOEXEC 0 #endif =20 +#ifndef SOCK_CLOEXEC +#define SOCK_CLOEXEC 0 +#endif + struct xs_stored_msg { XEN_TAILQ_ENTRY(struct xs_stored_msg) list; struct xsd_sockmsg hdr; @@ -207,16 +211,14 @@ int xs_fileno(struct xs_handle *h) static int get_socket(const char *connect_to) { struct sockaddr_un addr; - int sock, saved_errno, flags; + int sock, saved_errno; =20 - sock =3D socket(PF_UNIX, SOCK_STREAM, 0); + sock =3D socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); if (sock < 0) return -1; =20 - if ((flags =3D fcntl(sock, F_GETFD)) < 0) - goto error; - flags |=3D FD_CLOEXEC; - if (fcntl(sock, F_SETFD, flags) < 0) + /* Compat for non-SOCK_CLOEXEC environments. Racy. */ + if (!SOCK_CLOEXEC && !set_cloexec(sock)) goto error; =20 addr.sun_family =3D AF_UNIX; --=20 2.39.2 From nobody Mon Sep 16 19:37:30 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=reject dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1719585106; cv=none; d=zohomail.com; s=zohoarc; b=QdGdUzSQplfQiXkdFBPi2seJPed+9HuONKkrZAHwDlRdTmi5e8lakc+iwktHTC3/FE0bCaYc6TdLPgnHQu0pPu+DSwmcuv2+bosNWDLES6ODU+RHykOR58udGZNNkZj9h3oMq4YLJnld6hovNB9kmftVmD1ysrIjhOqh+wdxvKo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1719585106; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=NdLvmDIa3nEZMz+AIRiV7A13HKXJr2DbGh0lxxYahWU=; b=LAISxAr2VBSFCzvtk45QW1EXUqS+V500VwECEq45tH2NqDzwKDe28LRyanymDO7kAue4UbO7WxGBl6k0TiUx+iDOSZLxHVLMOsak0rsO6VE7Oo5dqz/EJ4ue575jpM3hb5A4eo0Uj0HqE0kOp+1sCfrpGxMdD7jQ5SMumt/UEXk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1719585106951480.18186985071793; Fri, 28 Jun 2024 07:31:46 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.750664.1158760 (Exim 4.92) (envelope-from ) id 1sNCdM-0002dR-9k; Fri, 28 Jun 2024 14:31:28 +0000 Received: by outflank-mailman (output) from mailman id 750664.1158760; Fri, 28 Jun 2024 14:31:28 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sNCdM-0002dI-78; Fri, 28 Jun 2024 14:31:28 +0000 Received: by outflank-mailman (input) for mailman id 750664; Fri, 28 Jun 2024 14:31:26 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sNCdK-0001vh-5U for xen-devel@lists.xenproject.org; Fri, 28 Jun 2024 14:31:26 +0000 Received: from mail-ed1-x530.google.com (mail-ed1-x530.google.com [2a00:1450:4864:20::530]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 18abf6f8-355b-11ef-b4bb-af5377834399; Fri, 28 Jun 2024 16:31:24 +0200 (CEST) Received: by mail-ed1-x530.google.com with SMTP id 4fb4d7f45d1cf-584ef6c07c2so2726740a12.1 for ; Fri, 28 Jun 2024 07:31:24 -0700 (PDT) Received: from andrewcoop.eng.citrite.net ([160.101.139.1]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a72aaf63390sm84944566b.69.2024.06.28.07.31.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Jun 2024 07:31:22 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 18abf6f8-355b-11ef-b4bb-af5377834399 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.com; s=google; t=1719585084; x=1720189884; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=NdLvmDIa3nEZMz+AIRiV7A13HKXJr2DbGh0lxxYahWU=; b=EtNWVEfcwMfiX50+98HpaJq5RKwhtknWaSiwXeI9IdUB1HwE4jTNI+IYiq5790m1Wt gfGJvZg5XeLtdM8LrYUXuBn7v83Fib7YpQB3D1uIyKiCwRqRzlvm/Q4yqw+NR81fgcVT MCH5ybwKPUM+RkusUEh8T/1scXRKbdCbc1/1A= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719585084; x=1720189884; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=NdLvmDIa3nEZMz+AIRiV7A13HKXJr2DbGh0lxxYahWU=; b=l7bopm46xvouw0x8Iy0U2N6Q8BtizvATp4z5UC6oO7UtknQyNgByoLpEYrIhxmZ3fi yyYP+R6QfHf9WiKgKToQrybFc0VttozU7DI2ojWQeKgNKHBHqUlmeyIvSlXyCg8TED+m akdLkxOJLp3+N8NIDiDKxEyK1RCfNc6MB9HNZNMFckLGtZy0LNtWMdrRiu3xzHBdvVb9 p51RSs3JoaNYy1Q3PVyxiG7DPqWLdVMoERholuAlNw6JmhflYW30NVHJsIrN8fcTkwAh suh9y6f2y04Do7eTkP2GgYI9e79xWMKPy7b2eQHWS0IdvhwQGRvQ9VopogQR42KRb6a+ taTQ== X-Gm-Message-State: AOJu0YxeIN6htE9167GUG+KCvpRnVNLWBaA4VwiuMJR7nODuf0c/j91s 3lhm1KQ5+Oc+IHrG7ZVRq9aGz/7SCCFGVSuSsVX3weuHIegymLcDQIMtTRpwTiAuDdAJ0Vw87mb d8ek= X-Google-Smtp-Source: AGHT+IH0Vk3s+qgCV1Zk3GHG2PVLElkv8IK3qGoOJzLuF5QlAFWgK8137FGB4jtTGQEENAUKrepxKw== X-Received: by 2002:a17:906:5fd3:b0:a6f:b352:a74b with SMTP id a640c23a62f3a-a72aefd2d3cmr123807966b.38.1719585083864; Fri, 28 Jun 2024 07:31:23 -0700 (PDT) From: Andrew Cooper To: Xen-devel Cc: Andrew Cooper , Anthony PERARD , Juergen Gross , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Frediano Ziglio , Oleksii Kurochko Subject: [PATCH for-4.19 3/3] tools/libxs: Fix CLOEXEC handling in xs_fileno() Date: Fri, 28 Jun 2024 15:31:16 +0100 Message-Id: <20240628143116.1044976-4-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240628143116.1044976-1-andrew.cooper3@citrix.com> References: <20240628143116.1044976-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @citrix.com) X-ZM-MESSAGEID: 1719585107793100003 xs_fileno() opens a pipe on first use to communicate between the watch thre= ad and the main thread. Nothing ever sets CLOEXEC on the file descriptors. Check for the availability of the pipe2() function with configure. Despite starting life as Linux-only, FreeBSD and NetBSD have gained it. When pipe2() isn't available, try our best with pipe() and set_cloexec(). Signed-off-by: Andrew Cooper Acked-by: Anthony PERARD Reviewed-by: Juergen Gross --- CC: Anthony PERARD CC: Juergen Gross CC: Roger Pau Monn=C3=A9 CC: Frediano Ziglio CC: Oleksii Kurochko --- tools/config.h.in | 3 +++ tools/configure | 12 ++++++++++++ tools/configure.ac | 2 ++ tools/libs/store/xs.c | 16 +++++++++++++++- 4 files changed, 32 insertions(+), 1 deletion(-) diff --git a/tools/config.h.in b/tools/config.h.in index 0bb2fe08a143..50ad60fcb091 100644 --- a/tools/config.h.in +++ b/tools/config.h.in @@ -39,6 +39,9 @@ /* Define to 1 if you have the header file. */ #undef HAVE_MEMORY_H =20 +/* Define to 1 if you have the `pipe2' function. */ +#undef HAVE_PIPE2 + /* pygrub enabled */ #undef HAVE_PYGRUB =20 diff --git a/tools/configure b/tools/configure index 459bfb56520e..a6b43bfc6064 100755 --- a/tools/configure +++ b/tools/configure @@ -9751,6 +9751,18 @@ if test "$ax_found" =3D "0"; then : fi =20 =20 +for ac_func in pipe2 +do : + ac_fn_c_check_func "$LINENO" "pipe2" "ac_cv_func_pipe2" +if test "x$ac_cv_func_pipe2" =3D xyes; then : + cat >>confdefs.h <<_ACEOF +#define HAVE_PIPE2 1 +_ACEOF + +fi +done + + cat >confcache <<\_ACEOF # This file is a shell script that caches the results of configure # tests run on this system so they can be shared between configure diff --git a/tools/configure.ac b/tools/configure.ac index 851887080c5e..ac0fdc4314c4 100644 --- a/tools/configure.ac +++ b/tools/configure.ac @@ -543,4 +543,6 @@ AS_IF([test "x$pvshim" =3D "xy"], [ =20 AX_FIND_HEADER([INCLUDE_ENDIAN_H], [endian.h sys/endian.h]) =20 +AC_CHECK_FUNCS([pipe2]) + AC_OUTPUT() diff --git a/tools/libs/store/xs.c b/tools/libs/store/xs.c index 11a766c50887..27bd20933efd 100644 --- a/tools/libs/store/xs.c +++ b/tools/libs/store/xs.c @@ -190,13 +190,27 @@ static bool set_cloexec(int fd) return fcntl(fd, flags | FD_CLOEXEC) >=3D 0; } =20 +static int pipe_cloexec(int fds[2]) +{ +#if HAVE_PIPE2 + return pipe2(fds, O_CLOEXEC); +#else + if (pipe(fds) < 0) + return -1; + /* Best effort to set CLOEXEC. Racy. */ + set_cloexec(fds[0]); + set_cloexec(fds[1]); + return 0; +#endif +} + int xs_fileno(struct xs_handle *h) { char c =3D 0; =20 mutex_lock(&h->watch_mutex); =20 - if ((h->watch_pipe[0] =3D=3D -1) && (pipe(h->watch_pipe) !=3D -1)) { + if ((h->watch_pipe[0] =3D=3D -1) && (pipe_cloexec(h->watch_pipe) !=3D -1)= ) { /* Kick things off if the watch list is already non-empty. */ if (!XEN_TAILQ_EMPTY(&h->watch_list)) while (write(h->watch_pipe[1], &c, 1) !=3D 1) --=20 2.39.2