More precise size for mmap

This commit is contained in:
topjohnwu 2017-11-20 03:40:37 +08:00
parent 8c02d120a2
commit 858e7bae2b

View File

@ -8,6 +8,7 @@
#include <string.h> #include <string.h>
#include <sys/sendfile.h> #include <sys/sendfile.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <linux/fs.h>
#ifndef NO_SELINUX #ifndef NO_SELINUX
#include <selinux/selinux.h> #include <selinux/selinux.h>
@ -337,20 +338,24 @@ void restorecon(int dirfd, int force) {
#endif // NO_SELINUX #endif // NO_SELINUX
void mmap_ro(const char *filename, void **buf, size_t *size) { static void _mmap(int rw, const char *filename, void **buf, size_t *size) {
int fd = xopen(filename, O_RDONLY); struct stat st;
*size = lseek(fd, 0, SEEK_END); stat(filename, &st);
lseek(fd, 0, SEEK_SET); int fd = xopen(filename, rw ? O_RDWR : O_RDONLY);
*buf = *size > 0 ? xmmap(NULL, *size, PROT_READ, MAP_SHARED, fd, 0) : NULL; if (S_ISBLK(st.st_mode))
ioctl(fd, BLKGETSIZE64, size);
else
*size = st.st_size;
*buf = *size > 0 ? xmmap(NULL, *size, PROT_READ | (rw ? PROT_WRITE : 0), MAP_SHARED, fd, 0) : NULL;
close(fd); close(fd);
} }
void mmap_ro(const char *filename, void **buf, size_t *size) {
_mmap(0, filename, buf, size);
}
void mmap_rw(const char *filename, void **buf, size_t *size) { void mmap_rw(const char *filename, void **buf, size_t *size) {
int fd = xopen(filename, O_RDWR); _mmap(1, filename, buf, size);
*size = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
*buf = *size > 0 ? xmmap(NULL, *size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0) : NULL;
close(fd);
} }
void full_read(int fd, void **buf, size_t *size) { void full_read(int fd, void **buf, size_t *size) {