summaryrefslogtreecommitdiff
path: root/shared
diff options
context:
space:
mode:
authorPekka Paalanen <pekka.paalanen@collabora.co.uk>2013-11-29 17:48:51 +0200
committerKristian Høgsberg <krh@bitplanet.net>2013-12-02 16:20:27 -0800
commit5b4ddbc11e84a22caefa655f4504e2999726458b (patch)
treef33fff07598506e28a161ba3eb39db98a173faed /shared
parent8a81b83900ae3153a07f0824a57d0daed436fc18 (diff)
downloadweston-5b4ddbc11e84a22caefa655f4504e2999726458b.tar.gz
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 ENOSPC. 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 <pekka.paalanen@collabora.co.uk>
Diffstat (limited to 'shared')
-rw-r--r--shared/os-compatibility.c19
1 files changed, 18 insertions, 1 deletions
diff --git a/shared/os-compatibility.c b/shared/os-compatibility.c
index 4f96dd4c..611e7c80 100644
--- a/shared/os-compatibility.c
+++ b/shared/os-compatibility.c
@@ -132,6 +132,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)
@@ -140,6 +146,7 @@ os_create_anonymous_file(off_t size)
const char *path;
char *name;
int fd;
+ int ret;
path = getenv("XDG_RUNTIME_DIR");
if (!path) {
@@ -161,10 +168,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;
}