Commit 011b6954 authored by Pekka Paalanen's avatar Pekka Paalanen Committed by Kristian Høgsberg

os: use posix_fallocate in creating sharable buffers

If posix_fallocate is available, use it instead of ftruncate. Unlike
ftruncate, when posix_fallocate succeeds, it guarantees that you cannot
run out of disk space, when later writing to the mmap()'ed file.

With posix_fallocate, if os_create_anonymous_file() succeeds, the
program cannot get a SIGBUS later from accessing this file via mmap. If
there is insufficient disk space, the function fails and errno is set to

This is useful on systems, that limit the available buffer space by
having XDG_RUNTIME_DIR on a small tmpfs.
Signed-off-by: Pekka Paalanen's avatarPekka Paalanen <>
parent 5a019e38
......@@ -39,7 +39,7 @@ if test "x$GCC" = "xyes"; then
AC_CHECK_FUNCS([accept4 mkostemp])
AC_CHECK_FUNCS([accept4 mkostemp posix_fallocate])
[AC_MSG_ERROR("SFD_CLOEXEC is needed to compile wayland")],
......@@ -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.
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) {
ret = posix_fallocate(fd, 0, size);
if (ret != 0) {
errno = ret;
return -1;
ret = ftruncate(fd, size);
if (ret < 0) {
return -1;
return fd;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment