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

This commit is contained in:
apio 2023-08-17 11:20:32 +02:00
parent ce9d0f5856
commit e0fb90d97e
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);
if (fd < 0)
{
os::eprintln("wind: could not create shared memory region: shm_open failed (%s) - %s", path, strerror(errno));
return err(errno);
int olderr = 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
if (ftruncate(fd, size) < 0)
{
int olderr = errno;
os::eprintln("wind: could not create shared memory region: ftruncate failed (%d, %zu) - %s", fd, size,
strerror(errno));
strerror(olderr));
shm_unlink(path);
close(fd);
return 0;
return err(olderr);
}
void* p = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (p == MAP_FAILED)
{
int olderr = errno;
os::eprintln("wind: could not create shared memory region: mmap failed (%zu, %d) - %s", size, fd,
strerror(errno));
strerror(olderr));
shm_unlink(path);
close(fd);
return 0;
return err(olderr);
}
if (outfd) *outfd = fd;