wind: Handle ftruncate() and mmap() errors properly

This commit is contained in:
apio 2023-08-17 11:20:32 +02:00
parent b656ceedfe
commit 88a202ba33
Signed by: apio
GPG Key ID: B8A7D06E42258954

View File

@ -26,29 +26,32 @@ static Result<u32*> create_shm_region(const char* path, int* outfd, ui::Rect rec
int fd = shm_open(path, O_RDWR | O_CREAT | O_EXCL, 0600); int fd = shm_open(path, O_RDWR | O_CREAT | O_EXCL, 0600);
if (fd < 0) if (fd < 0)
{ {
os::eprintln("wind: could not create shared memory region: shm_open failed (%s) - %s", path, strerror(errno)); int olderr = errno;
return err(errno); os::eprintln("wind: could not create shared memory region: shm_open failed (%s) - %s", path, strerror(olderr));
return err(olderr);
} }
usize size = align_up<PAGE_SIZE>(rect.width * rect.height * 4); // 4 bytes per pixel usize size = align_up<PAGE_SIZE>(rect.width * rect.height * 4); // 4 bytes per pixel
if (ftruncate(fd, size) < 0) if (ftruncate(fd, size) < 0)
{ {
int olderr = errno;
os::eprintln("wind: could not create shared memory region: ftruncate failed (%d, %zu) - %s", fd, size, os::eprintln("wind: could not create shared memory region: ftruncate failed (%d, %zu) - %s", fd, size,
strerror(errno)); strerror(olderr));
shm_unlink(path); shm_unlink(path);
close(fd); close(fd);
return 0; return err(olderr);
} }
void* p = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); void* p = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (p == MAP_FAILED) if (p == MAP_FAILED)
{ {
int olderr = errno;
os::eprintln("wind: could not create shared memory region: mmap failed (%zu, %d) - %s", size, fd, os::eprintln("wind: could not create shared memory region: mmap failed (%zu, %d) - %s", size, fd,
strerror(errno)); strerror(olderr));
shm_unlink(path); shm_unlink(path);
close(fd); close(fd);
return 0; return err(olderr);
} }
if (outfd) *outfd = fd; if (outfd) *outfd = fd;