From c01f009e42c38dba54a83d69e253ddedbdcb51f9 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 28 Sep 2017 17:34:22 +0530 Subject: [PATCH] Remove sz requirement and implement proper fix for failure to mmap shm objects on OS X. Apparently they have to be mapped shared. --- kitty/graphics.c | 7 +++---- kitty_tests/graphics.py | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/kitty/graphics.c b/kitty/graphics.c index a307e4cc2..e1c9ab681 100644 --- a/kitty/graphics.c +++ b/kitty/graphics.c @@ -137,11 +137,11 @@ static inline bool mmap_img_file(GraphicsManager UNUSED *self, Image *img, size_t sz, off_t offset) { if (!sz) { struct stat s; - if (fstat(img->load_data.fd, &s) != 0) ABRT(EBADF, "Failed to fstat() file with error: [%d] %s", errno, strerror(errno)); + if (fstat(img->load_data.fd, &s) != 0) ABRT(EBADF, "Failed to fstat() the fd: %d file with error: [%d] %s", img->load_data.fd, errno, strerror(errno)); sz = s.st_size; } - void *addr = mmap(0, sz, PROT_READ, MAP_PRIVATE, img->load_data.fd, offset); - if (addr == MAP_FAILED) ABRT(EBADF, "Failed to map image file with error: [%d] %s", errno, strerror(errno)); + void *addr = mmap(0, sz, PROT_READ, MAP_SHARED, img->load_data.fd, offset); + if (addr == MAP_FAILED) ABRT(EBADF, "Failed to map image file fd: %d at offset: %zd with size: %zu with error: [%d] %s", img->load_data.fd, offset, sz, errno, strerror(errno)); img->load_data.mapped_file = addr; img->load_data.mapped_file_sz = sz; return true; @@ -337,7 +337,6 @@ handle_add_command(GraphicsManager *self, const GraphicsCommand *g, const uint8_ else fd = open(fname, O_CLOEXEC | O_RDONLY); if (fd == -1) ABRT(EBADF, "Failed to open file %s for graphics transmission with error: [%d] %s", fname, errno, strerror(errno)); img->load_data.fd = fd; - if (tt == 's' && !g->data_sz) ABRT(EINVAL, "data size required for shared memory object"); img->data_loaded = mmap_img_file(self, img, g->data_sz, g->data_offset); if (tt == 't') unlink(fname); else if (tt == 's') shm_unlink(fname); diff --git a/kitty_tests/graphics.py b/kitty_tests/graphics.py index 62830cd18..d9dc9d8eb 100644 --- a/kitty_tests/graphics.py +++ b/kitty_tests/graphics.py @@ -90,5 +90,5 @@ class TestGraphics(BaseTest): # Test loading from POSIX SHM name = '/kitty-test-shm' g.shm_write(name, random_data) - sl(name, s=24, v=32, t='s', S=len(random_data), expecting_data=random_data) + sl(name, s=24, v=32, t='s', expecting_data=random_data) self.assertRaises(FileNotFoundError, g.shm_unlink, name) # check that file was deleted