According to POSIX, the size of shared memory object must be set with
ftruncate before writing to it. This happenen to work on Linux because
the implementation simply creates files on a tmpfs but on FreeBSD it is
implemented using actual shared memory objects and as such requires the
size to be set first.
Signed-off-by: Michael Jeanson <mjeanson@efficios.com>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Change-Id: I048f65104d7c34142025337ec1f6777650bfd62f
}
memcpy(obj->wait_fd, waitfd, sizeof(waitfd));
}
memcpy(obj->wait_fd, waitfd, sizeof(waitfd));
+ /*
+ * Set POSIX shared memory object size
+ *
+ * First, use ftruncate() to set its size, some implementations won't
+ * allow writes past the size set by ftruncate.
+ * Then, use write() to fill it with zeros, this allows us to fully
+ * allocate it and detect a shortage of shm space without dealing with
+ * a SIGBUS.
+ */
- ret = zero_file(shmfd, memory_map_size);
- if (ret) {
- PERROR("zero_file");
- goto error_zero_file;
- }
ret = ftruncate(shmfd, memory_map_size);
if (ret) {
PERROR("ftruncate");
goto error_ftruncate;
}
ret = ftruncate(shmfd, memory_map_size);
if (ret) {
PERROR("ftruncate");
goto error_ftruncate;
}
+ ret = zero_file(shmfd, memory_map_size);
+ if (ret) {
+ PERROR("zero_file");
+ goto error_zero_file;
+ }
/*
* Also ensure the file metadata is synced with the storage by using
* fsync(2).
/*
* Also ensure the file metadata is synced with the storage by using
* fsync(2).