From patchwork Fri Jun 28 14:31:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew Cooper X-Patchwork-Id: 13716249 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.xenproject.org (lists.xenproject.org [192.237.175.120]) (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 DABA3C2BD09 for ; Fri, 28 Jun 2024 14:31:37 +0000 (UTC) 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 X-Outflank-Mailman: Message body and most headers restored to incoming version 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-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_Pau_Monn=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 Move the O_CLOEXEC compatibility outside of an #ifdef USE_PTHREAD block. Introduce set_cloexec() to wrap fcntl() setting FD_CLOEXEC. It will be reused 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 --- CC: Anthony PERARD CC: Juergen Gross CC: Roger Pau Monné 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 +#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 -#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; } +static bool set_cloexec(int fd) +{ + int flags = fcntl(fd, F_GETFL); + + if (flags < 0) + return false; + + return fcntl(fd, flags | FD_CLOEXEC) >= 0; +} + int xs_fileno(struct xs_handle *h) { char c = 0; @@ -230,8 +240,24 @@ static int get_socket(const char *connect_to) 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 = 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 = errno; + close(fd); + errno = saved_errno; + + return -1; } static int all_restrict_cb(Xentoolcore__Active_Handle *ah, domid_t domid) { From patchwork Fri Jun 28 14:31:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew Cooper X-Patchwork-Id: 13716250 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.xenproject.org (lists.xenproject.org [192.237.175.120]) (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 2B3D9C30658 for ; Fri, 28 Jun 2024 14:31:38 +0000 (UTC) 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 X-Outflank-Mailman: Message body and most headers restored to incoming version 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-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_Pau_Monn=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 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 --- CC: Anthony PERARD CC: Juergen Gross CC: Roger Pau Monné 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 +#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; - sock = socket(PF_UNIX, SOCK_STREAM, 0); + sock = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); if (sock < 0) return -1; - if ((flags = fcntl(sock, F_GETFD)) < 0) - goto error; - flags |= FD_CLOEXEC; - if (fcntl(sock, F_SETFD, flags) < 0) + /* Compat for non-SOCK_CLOEXEC environments. Racy. */ + if (!SOCK_CLOEXEC && !set_cloexec(sock)) goto error; addr.sun_family = AF_UNIX; From patchwork Fri Jun 28 14:31:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew Cooper X-Patchwork-Id: 13716251 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.xenproject.org (lists.xenproject.org [192.237.175.120]) (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 DF6EAC3065B for ; Fri, 28 Jun 2024 14:31:38 +0000 (UTC) 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 X-Outflank-Mailman: Message body and most headers restored to incoming version 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-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_Pau_Monn=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 xs_fileno() opens a pipe on first use to communicate between the watch thread 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 --- CC: Anthony PERARD CC: Juergen Gross CC: Roger Pau Monné 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 +/* Define to 1 if you have the `pipe2' function. */ +#undef HAVE_PIPE2 + /* pygrub enabled */ #undef HAVE_PYGRUB 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" = "0"; then : fi +for ac_func in pipe2 +do : + ac_fn_c_check_func "$LINENO" "pipe2" "ac_cv_func_pipe2" +if test "x$ac_cv_func_pipe2" = 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" = "xy"], [ AX_FIND_HEADER([INCLUDE_ENDIAN_H], [endian.h sys/endian.h]) +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) >= 0; } +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 = 0; mutex_lock(&h->watch_mutex); - if ((h->watch_pipe[0] == -1) && (pipe(h->watch_pipe) != -1)) { + if ((h->watch_pipe[0] == -1) && (pipe_cloexec(h->watch_pipe) != -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) != 1)