mfdtruncate.c
text/x-csrc
Filename: mfdtruncate.c
Type: text/x-csrc
Part: 0
#define _GNU_SOURCE 1 /* See feature_test_macros(7) */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
#include <stdbool.h>
#define MBSIZE(x) ((x) * 1024 * 1024)
int
main(int argc, char **argv)
{
int flags = MAP_NORESERVE;
void *memaddr;
pid_t pid = getpid();
char *localmem;
int fd = memfd_create("mmap_fd_exp", 0);
size_t size = MBSIZE(300);
localmem = malloc(size);
memset(localmem, 1, size);
if (fd < 0)
{
printf("memfd_create failed with errno %d\n", errno);
exit(__LINE__);
}
if (ftruncate(fd, size) < 0)
{
printf("ftruncate failed with errno %d on fd = %d and size = %ld\n", errno, fd, size);
exit(__LINE__);
}
memaddr = mmap(NULL, size, PROT_WRITE | PROT_READ, MAP_SHARED | MAP_NORESERVE, fd, 0);
if (memaddr == MAP_FAILED)
{
printf("mmap failed with error %m\n");
exit(__LINE__);
}
memset(memaddr, 1, size);
if (memcmp(memaddr, localmem, size) != 0)
{
printf("mmap memory and local memory are not equal upto size = %ld\n", size);
exit(__LINE__);
}
/* causes a segmentation fault: memset(memaddr, 0, maxsize + 1); */
size = MBSIZE(100);
if (ftruncate(fd, size) < 0)
{
printf("ftruncate failed with errno %d on fd = %d and size = %ld\n", errno, fd, size);
exit(__LINE__);
}
memset(memaddr, 1, size);
if (memcmp(memaddr, localmem, size) != 0)
{
printf("mmap memory and local memory are not equal upto size = %ld\n", size);
exit(__LINE__);
}
/* causes a segmentation fault: memset(memaddr, 1, MBSIZE(200)); */
size = MBSIZE(200);
if (ftruncate(fd, size) < 0)
{
printf("ftruncate failed with errno %d on fd = %d and size = %ld\n", errno, fd, size);
exit(__LINE__);
}
memset(memaddr, 1, size);
if (memcmp(memaddr, localmem, size) != 0)
{
printf("mmap memory and local memory are not equal upto size = %ld\n", size);
exit(__LINE__);
}
}