From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51040) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zgxs5-0002Cz-OE for qemu-devel@nongnu.org; Tue, 29 Sep 2015 12:35:19 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Zgxs4-0003rB-W8 for qemu-devel@nongnu.org; Tue, 29 Sep 2015 12:35:17 -0400 Received: from mail-qg0-x22d.google.com ([2607:f8b0:400d:c04::22d]:35205) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zgxs4-0003r7-Su for qemu-devel@nongnu.org; Tue, 29 Sep 2015 12:35:16 -0400 Received: by qgt47 with SMTP id 47so11014046qgt.2 for ; Tue, 29 Sep 2015 09:35:16 -0700 (PDT) Sender: =?UTF-8?B?TWFyYy1BbmRyw6kgTHVyZWF1?= From: marcandre.lureau@redhat.com Date: Tue, 29 Sep 2015 18:34:36 +0200 Message-Id: <1443544494-28737-7-git-send-email-marcandre.lureau@redhat.com> In-Reply-To: <1443544494-28737-1-git-send-email-marcandre.lureau@redhat.com> References: <1443544494-28737-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 v6 06/24] memfd: add fallback for memfd 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. This patch may require additional SELinux policies to work for enforced systems, but should gracefully fail nonetheless. Signed-off-by: Marc-André Lureau --- util/memfd.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/util/memfd.c b/util/memfd.c index 3168902..970b5b0 100644 --- a/util/memfd.c +++ b/util/memfd.c @@ -84,8 +84,26 @@ void *qemu_memfd_alloc(const char *name, size_t size, unsigned int seals, return NULL; } } else { - perror("memfd"); - return NULL; + const char *tmpdir = getenv("TMPDIR"); + gchar *fname; + + tmpdir = tmpdir ? tmpdir : "/tmp"; + + 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