From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34981) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zoqc8-00052k-7f for qemu-devel@nongnu.org; Wed, 21 Oct 2015 06:27:24 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Zoqc4-0006Di-71 for qemu-devel@nongnu.org; Wed, 21 Oct 2015 06:27:24 -0400 Received: from mx1.redhat.com ([209.132.183.28]:54554) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zoqc4-0006Da-04 for qemu-devel@nongnu.org; Wed, 21 Oct 2015 06:27:20 -0400 Date: Wed, 21 Oct 2015 13:27:16 +0300 From: "Michael S. Tsirkin" Message-ID: <1445423133-5119-15-git-send-email-mst@redhat.com> References: <1445423133-5119-1-git-send-email-mst@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline In-Reply-To: <1445423133-5119-1-git-send-email-mst@redhat.com> Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PULL 14/38] util: add fallback for qemu_memfd_alloc() List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Peter Maydell , Thibaut Collet , =?us-ascii?B?PT9VVEYtOD9xP01hcmMtQW5kcj1DMz1BOT0yMEx1cmVhdT89?= From: Marc-Andr=E9 Lureau Add an open/unlink/mmap fallback for system that do not support memfd (only available since 3.17, ~1y ago). This patch may require additional SELinux policies to work for enforced systems, but should fail gracefully in this case. Signed-off-by: Marc-Andr=E9 Lureau Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin Tested-by: Thibaut Collet --- util/memfd.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/util/memfd.c b/util/memfd.c index c119483..4b23765 100644 --- a/util/memfd.c +++ b/util/memfd.c @@ -97,8 +97,24 @@ void *qemu_memfd_alloc(const char *name, size_t size, = unsigned int seals, return NULL; } } else { - perror("memfd"); - return NULL; + const char *tmpdir =3D g_get_tmp_dir(); + gchar *fname; + + fname =3D g_strdup_printf("%s/memfd-XXXXXX", tmpdir); + mfd =3D mkstemp(fname); + unlink(fname); + g_free(fname); + + if (mfd =3D=3D -1) { + perror("mkstemp"); + return NULL; + } + + if (ftruncate(mfd, size) =3D=3D -1) { + perror("ftruncate"); + close(mfd); + return NULL; + } } =20 ptr =3D mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, mfd, 0); --=20 MST