diff --git a/configure.ac b/configure.ac index f8be4562..b2895677 100644 --- a/configure.ac +++ b/configure.ac @@ -39,7 +39,7 @@ if test "x$GCC" = "xyes"; then fi AC_SUBST(GCC_CFLAGS) -AC_CHECK_FUNCS([accept4 mkostemp]) +AC_CHECK_FUNCS([accept4 mkostemp posix_fallocate]) AC_CHECK_DECL(SFD_CLOEXEC,[], [AC_MSG_ERROR("SFD_CLOEXEC is needed to compile wayland")], diff --git a/cursor/os-compatibility.c b/cursor/os-compatibility.c index 418b0d3b..0c412428 100644 --- a/cursor/os-compatibility.c +++ b/cursor/os-compatibility.c @@ -90,6 +90,12 @@ create_tmpfile_cloexec(char *tmpname) * The file is suitable for buffer sharing between processes by * transmitting the file descriptor over Unix sockets using the * SCM_RIGHTS methods. + * + * If the C library implements posix_fallocate(), it is used to + * guarantee that disk space is available for the file at the + * given size. If disk space is insufficent, errno is set to ENOSPC. + * If posix_fallocate() is not supported, program may receive + * SIGBUS on accessing mmap()'ed file contents instead. */ int os_create_anonymous_file(off_t size) @@ -98,6 +104,7 @@ os_create_anonymous_file(off_t size) const char *path; char *name; int fd; + int ret; path = getenv("XDG_RUNTIME_DIR"); if (!path) { @@ -119,10 +126,20 @@ os_create_anonymous_file(off_t size) if (fd < 0) return -1; - if (ftruncate(fd, size) < 0) { +#ifdef HAVE_POSIX_FALLOCATE + ret = posix_fallocate(fd, 0, size); + if (ret != 0) { + close(fd); + errno = ret; + return -1; + } +#else + ret = ftruncate(fd, size); + if (ret < 0) { close(fd); return -1; } +#endif return fd; }