shm: Use memfd_create when possible

It doesn't require shared memory dir and thus allows
to avoid cases when this dir is detected incorrectly,
as in https://bugreports.qt.io/browse/QTBUG-71440

Signed-off-by: Alexander Volkov <a.volkov@rusbitech.ru>
This commit is contained in:
Alexander Volkov 2019-02-11 18:54:10 +03:00
parent 678d64aa2e
commit f6753c117e
4 changed files with 17 additions and 1 deletions

View File

@ -35,6 +35,9 @@ in this Software without prior written authorization from The Open Group.
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#ifdef HAVE_MEMFD_CREATE
#include <sys/mman.h>
#endif
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
@ -1201,6 +1204,15 @@ shm_tmpfile(void)
};
int fd;
#ifdef HAVE_MEMFD_CREATE
fd = memfd_create("xorg", MFD_CLOEXEC|MFD_ALLOW_SEALING);
if (fd != -1) {
fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK);
DebugF ("Using memfd_create\n");
return fd;
}
#endif
#ifdef O_TMPFILE
for (int i = 0; i < ARRAY_SIZE(shmdirs); i++) {
fd = open(shmdirs[i], O_TMPFILE|O_RDWR|O_CLOEXEC|O_EXCL, 0666);

View File

@ -159,7 +159,7 @@ dnl Checks for library functions.
AC_CHECK_FUNCS([backtrace geteuid getuid issetugid getresuid \
getdtablesize getifaddrs getpeereid getpeerucred getprogname getzoneid \
mmap posix_fallocate seteuid shmctl64 strncasecmp vasprintf vsnprintf \
walkcontext setitimer poll epoll_create1 mkostemp])
walkcontext setitimer poll epoll_create1 mkostemp memfd_create])
AC_CONFIG_LIBOBJ_DIR([os])
AC_REPLACE_FUNCS([reallocarray strcasecmp strcasestr strlcat strlcpy strndup\
timingsafe_memcmp])

View File

@ -128,6 +128,9 @@
/* Define to 1 if you have the <linux/fb.h> header file. */
#undef HAVE_LINUX_FB_H
/* Define to 1 if you have the `memfd_create' function. */
#undef HAVE_MEMFD_CREATE
/* Define to 1 if you have the `mkostemp' function. */
#undef HAVE_MKOSTEMP

View File

@ -141,6 +141,7 @@ conf_data.set('HAVE_GETPEEREID', cc.has_function('getpeereid'))
conf_data.set('HAVE_GETPEERUCRED', cc.has_function('getpeerucred'))
conf_data.set('HAVE_GETPROGNAME', cc.has_function('getprogname'))
conf_data.set('HAVE_GETZONEID', cc.has_function('getzoneid'))
conf_data.set('HAVE_MEMFD_CREATE', cc.has_function('memfd_create'))
conf_data.set('HAVE_MKOSTEMP', cc.has_function('mkostemp'))
conf_data.set('HAVE_MMAP', cc.has_function('mmap'))
conf_data.set('HAVE_POLL', cc.has_function('poll'))