Received: from eggs.gnu.org ([2001:4830:134:3::10]:38445) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zhhao-0008C7-DF for qemu-devel@nongnu.org; Thu, 01 Oct 2015 13:24:33 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Zhhan-0008Rt-Jv for qemu-devel@nongnu.org; Thu, 01 Oct 2015 13:24:30 -0400 Received: from mail-qk0-x234.google.com ([2607:f8b0:400d:c09::234]:36069) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zhhan-0008Rc-FM for qemu-devel@nongnu.org; Thu, 01 Oct 2015 13:24:29 -0400 Received: by qkcf65 with SMTP id f65so32124954qkc.3 for ; Thu, 01 Oct 2015 10:24:29 -0700 (PDT) Sender: =?UTF-8?B?TWFyYy1BbmRyw6kgTHVyZWF1?= From: marcandre.lureau@redhat.com Date: Thu, 1 Oct 2015 19:23:49 +0200 Message-Id: <1443720248-15482-6-git-send-email-marcandre.lureau@redhat.com> In-Reply-To: <1443720248-15482-1-git-send-email-marcandre.lureau@redhat.com> References: <1443720248-15482-1-git-send-email-marcandre.lureau@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Subject: [Qemu-devel] [PATCH v7 05/24] 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: haifeng.lin@huawei.com, mst@redhat.com, thibaut.collet@6wind.com, jasowang@redhat.com, pbonzini@redhat.com, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= From: Marc-André 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é Lureau --- util/memfd.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/util/memfd.c b/util/memfd.c index dd47552..ff577b5 100644 --- a/util/memfd.c +++ b/util/memfd.c @@ -100,8 +100,24 @@ void *qemu_memfd_alloc(const char *name, size_t size, unsigned int seals, return NULL; } } else { - perror("memfd"); - return NULL; + const char *tmpdir = g_get_tmp_dir(); + gchar *fname; + + fname = g_strdup_printf("%s/memfd-XXXXXX", tmpdir); + mfd = mkstemp(fname); + unlink(fname); + g_free(fname); + + if (mfd == -1) { + perror("mkstemp"); + return NULL; + } + + if (ftruncate(mfd, size) == -1) { + perror("ftruncate"); + close(mfd); + return NULL; + } } ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, mfd, 0); -- 2.4.3