From nobody Thu Nov 6 06:23:37 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 153961340480672.80478564645898; Mon, 15 Oct 2018 07:23:24 -0700 (PDT) Received: from localhost ([::1]:52597 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gC3m7-0005t0-9s for importer@patchew.org; Mon, 15 Oct 2018 10:23:15 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48790) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gC3ef-0000EH-99 for qemu-devel@nongnu.org; Mon, 15 Oct 2018 10:15:34 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gC3ed-0007eW-8Y for qemu-devel@nongnu.org; Mon, 15 Oct 2018 10:15:33 -0400 Received: from mx1.redhat.com ([209.132.183.28]:20646) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gC3eK-0007PY-3M; Mon, 15 Oct 2018 10:15:12 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 5D015308FF30; Mon, 15 Oct 2018 14:15:11 +0000 (UTC) Received: from localhost (ovpn-204-236.brq.redhat.com [10.40.204.236]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D66908AB48; Mon, 15 Oct 2018 14:15:10 +0000 (UTC) From: Max Reitz To: qemu-block@nongnu.org Date: Mon, 15 Oct 2018 16:14:50 +0200 Message-Id: <20181015141453.32632-7-mreitz@redhat.com> In-Reply-To: <20181015141453.32632-1-mreitz@redhat.com> References: <20181015141453.32632-1-mreitz@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.49]); Mon, 15 Oct 2018 14:15:11 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 6/9] iotests: Explicitly inherit FDs in Python 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: Kevin Wolf , Cleber Rosa , qemu-devel@nongnu.org, Eduardo Habkost , Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RDMRC_1 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Python 3.2 introduced the inheritable attribute for FDs. At the same time, it changed the default so that all FDs are not inheritable by default, that only inheritable FDs are inherited to subprocesses, and only if close_fds is explicitly set to False. Adhere to this by setting close_fds to False when working with subprocesses that may want to inherit FDs, and by trying to set_inheritable() on FDs that we do want to bequeath to them. Signed-off-by: Max Reitz --- scripts/qemu.py | 13 +++++++++++-- scripts/qmp/qmp.py | 7 +++++++ tests/qemu-iotests/147 | 7 +++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/scripts/qemu.py b/scripts/qemu.py index f099ce7278..28366c4a67 100644 --- a/scripts/qemu.py +++ b/scripts/qemu.py @@ -142,10 +142,18 @@ class QEMUMachine(object): if opts: options.append(opts) =20 + # This did not exist before 3.2, but since then it is + # mandatory for our purpose + try: + os.set_inheritable(fd, True) + except AttributeError: + pass + self._args.append('-add-fd') self._args.append(','.join(options)) return self =20 + # The caller needs to make sure the FD is inheritable def send_fd_scm(self, fd_file_path): # In iotest.py, the qmp should always use unix socket. assert self._qmp.is_scm_available() @@ -159,7 +167,7 @@ class QEMUMachine(object): "%s" % fd_file_path] devnull =3D open(os.path.devnull, 'rb') proc =3D subprocess.Popen(fd_param, stdin=3Ddevnull, stdout=3Dsubp= rocess.PIPE, - stderr=3Dsubprocess.STDOUT) + stderr=3Dsubprocess.STDOUT, close_fds=3DFa= lse) output =3D proc.communicate()[0] if output: LOG.debug(output) @@ -280,7 +288,8 @@ class QEMUMachine(object): stdin=3Ddevnull, stdout=3Dself._qemu_log_file, stderr=3Dsubprocess.STDOUT, - shell=3DFalse) + shell=3DFalse, + close_fds=3DFalse) self._post_launch() =20 def wait(self): diff --git a/scripts/qmp/qmp.py b/scripts/qmp/qmp.py index 5c8cf6a056..009be8345b 100644 --- a/scripts/qmp/qmp.py +++ b/scripts/qmp/qmp.py @@ -10,6 +10,7 @@ =20 import json import errno +import os import socket import logging =20 @@ -253,4 +254,10 @@ class QEMUMonitorProtocol(object): return self.__sock.fileno() =20 def is_scm_available(self): + # This did not exist before 3.2, but since then it is + # mandatory for our purpose + try: + os.set_inheritable(self.get_sock_fd(), True) + except AttributeError: + pass return self.__sock.family =3D=3D socket.AF_UNIX diff --git a/tests/qemu-iotests/147 b/tests/qemu-iotests/147 index d2081df84b..b58455645b 100755 --- a/tests/qemu-iotests/147 +++ b/tests/qemu-iotests/147 @@ -229,6 +229,13 @@ class BuiltinNBD(NBDBlockdevAddBase): sockfd =3D socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sockfd.connect(unix_socket) =20 + # This did not exist before 3.2, but since then it is + # mandatory for our purpose + try: + os.set_inheritable(sockfd.fileno(), True) + except AttributeError: + pass + result =3D self.vm.send_fd_scm(str(sockfd.fileno())) self.assertEqual(result, 0, 'Failed to send socket FD') =20 --=20 2.17.1