Files
mckernel/test/issues/1422/filemap_sigbus.c
Yoshihisa Morizumi 9c7d0cfaec getrusage: Fix memory_stat_mapped_file when SIGBUS occurs in file map
Change-Id: Ia4686f32a3c888d5c886ab6cc6c2b510885447f5
Refs: #1422
2021-03-01 05:55:37 +00:00

50 lines
825 B
C

/* filemap_sigbus.c COPYRIGHT FUJITSU LIMITED 2019 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
int main(int argc, char *argv[])
{
int ret = -1;
int fd = -1;
int i = 0;
unsigned long *buf = NULL;
const long pgsize = sysconf(_SC_PAGESIZE);
if (argc != 2) {
printf("args invalid.\n");
ret = 0;
goto out;
}
fd = open(argv[1], O_RDWR);
if (fd == -1) {
perror("open");
goto out;
}
buf = (unsigned long *)mmap(0, 3 * pgsize, PROT_READ | PROT_WRITE,
MAP_PRIVATE, fd, 0);
if (buf == MAP_FAILED) {
perror("mmap");
goto out;
}
/* Generate SIGBUS */
for (i = 0; i < 3 * pgsize / sizeof(unsigned long); i++) {
buf[i] = i;
}
munmap(buf, 3 * pgsize);
close(fd);
ret = 0;
out:
return ret;
}