From 839d14f0ec797d8124f3573a04f963557f7d16b6 Mon Sep 17 00:00:00 2001 From: Angus Salkeld Date: Fri, 9 Nov 2012 10:15:25 +1100 Subject: [PATCH] Make sure that mmap'ed files smaller than a page size are written to. This is a test to make sure that there is actually enough memory to back the requested shared memory. Signed-off-by: Angus Salkeld --- lib/unix.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/unix.c b/lib/unix.c index 70ea4614b..800fcf8e1 100644 --- a/lib/unix.c +++ b/lib/unix.c @@ -112,22 +112,23 @@ qb_sys_mmap_file_open(char *path, const char *file, size_t bytes, if (file_flags & O_CREAT) { long page_size = sysconf(_SC_PAGESIZE); + long write_size = QB_MIN(page_size, bytes); if (page_size < 0) { res = -errno; goto unlink_exit; } - buffer = calloc(1, page_size); + buffer = calloc(1, write_size); if (buffer == NULL) { res = -ENOMEM; goto unlink_exit; } - for (i = 0; i < (bytes / page_size); i++) { + for (i = 0; i < (bytes / write_size); i++) { retry_write: - written = write(fd, buffer, page_size); + written = write(fd, buffer, write_size); if (written == -1 && errno == EINTR) { goto retry_write; } - if (written != page_size) { + if (written != write_size) { res = -ENOSPC; free(buffer); goto unlink_exit;