wayland: Use memfd_create

This commit is contained in:
Ladislav Michl 2019-10-04 22:16:54 +02:00 committed by Ivan Vučica
parent e805a4a6c0
commit 4311a8191b

View file

@ -2,6 +2,8 @@
Copyright (C) 2016 Sergio L. Pascual <slp@sinrega.org>
*/
#define _GNU_SOURCE
#include "wayland/WaylandServer.h"
#include "cairo/WaylandCairoSurface.h"
#include <cairo/cairo.h>
@ -11,60 +13,16 @@
#include <fcntl.h>
#include <sys/mman.h>
#define GSWINDEVICE ((struct window *)gsDevice)
int
os_fd_set_cloexec(int fd)
{
long flags;
if (fd == -1)
return -1;
flags = fcntl(fd, F_GETFD);
if (flags == -1)
return -1;
if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1)
return -1;
return 0;
}
/* Linux specific version */
static int
set_cloexec_or_close(int fd)
{
if (os_fd_set_cloexec(fd) != 0) {
close(fd);
return -1;
}
return fd;
}
static int
create_tmpfile_cloexec(char *tmpname)
{
int fd;
fd = mkstemp(tmpname);
if (fd >= 0) {
fd = set_cloexec_or_close(fd);
unlink(tmpname);
}
return fd;
}
int
os_create_anonymous_file(off_t size)
{
static const char template[] = "/weston-shared-XXXXXX";
const char *path;
char *name;
int fd;
int ret;
path = getenv("XDG_RUNTIME_DIR");
if (!path) {
@ -79,17 +37,15 @@ os_create_anonymous_file(off_t size)
strcpy(name, path);
strcat(name, template);
fd = create_tmpfile_cloexec(name);
fd = memfd_create(name, MFD_CLOEXEC);
free(name);
if (fd < 0)
return -1;
ret = posix_fallocate(fd, 0, size);
if (ret != 0) {
if (ftruncate(fd, size) != 0) {
close(fd);
errno = ret;
return -1;
}