Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Paste
P367
graphics/wayland: standalone posix_fallocate snippet
Active
Public
Actions
Authored by
jbeich
on Feb 27 2020, 5:39 PM.
Edit Paste
Archive Paste
View Raw File
Subscribe
Mute Notifications
Award Token
Flag For Later
Tags
None
Subscribers
None
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#define HAVE_POSIX_FALLOCATE 1
int
os_resize_anonymous_file(int fd, off_t size)
{
#ifdef HAVE_POSIX_FALLOCATE
/*
* Filesystems that do support fallocate will return EINVAL or
* EOPNOTSUPP. In this case we need to fall back to ftruncate
*/
errno = posix_fallocate(fd, 0, size);
if (errno == 0)
return 0;
else if (errno != EINVAL && errno != EOPNOTSUPP) {
warn("posix_fallocate failed");
return -1;
}
#endif
if (ftruncate(fd, size) < 0) {
warn("ftruncate failed");
return -1;
}
return 0;
}
int
main(void)
{
int fd;
off_t size = 12345;
fd = shm_open(SHM_ANON, O_CREAT | O_RDWR | O_CLOEXEC, 0600); // shm_open is always CLOEXEC
if (fd < 0)
err(1, "shm_open(SHM_ANON) failed");
if (os_resize_anonymous_file(fd, size) < 0) {
close(fd);
return -1;
}
return 0;
}
Event Timeline
jbeich
edited the content of this paste.
(Show Details)
Feb 27 2020, 5:39 PM
2020-02-27 17:39:17 (UTC+0)
jbeich
changed the title of this paste from untitled to
graphics/wayland: standalone posix_fallocate snippet
.
jbeich
updated the paste's language from
autodetect
to
c
.
jbeich
edited the content of this paste.
(Show Details)
Feb 27 2020, 5:40 PM
2020-02-27 17:40:15 (UTC+0)
Log In to Comment