xref: /haiku/src/tests/system/kernel/mmap_resize_test.cpp (revision fba84f08b854b61ac65e44d2269667847fd10bcd)
1*fba84f08SIngo Weinhold /*
2*fba84f08SIngo Weinhold  * Copyright 2010, Ingo Weinhold, ingo_weinhold@gmx.de.
3*fba84f08SIngo Weinhold  * Distributed under the terms of the MIT License.
4*fba84f08SIngo Weinhold  */
5*fba84f08SIngo Weinhold 
6*fba84f08SIngo Weinhold 
7*fba84f08SIngo Weinhold #include <errno.h>
8*fba84f08SIngo Weinhold #include <fcntl.h>
9*fba84f08SIngo Weinhold #include <stdio.h>
10*fba84f08SIngo Weinhold #include <stdlib.h>
11*fba84f08SIngo Weinhold #include <string.h>
12*fba84f08SIngo Weinhold #include <sys/mman.h>
13*fba84f08SIngo Weinhold #include <unistd.h>
14*fba84f08SIngo Weinhold 
15*fba84f08SIngo Weinhold 
16*fba84f08SIngo Weinhold int
main()17*fba84f08SIngo Weinhold main()
18*fba84f08SIngo Weinhold {
19*fba84f08SIngo Weinhold 	const char* fileName = "/tmp/mmap-resize-test-file";
20*fba84f08SIngo Weinhold 
21*fba84f08SIngo Weinhold 	// create file
22*fba84f08SIngo Weinhold 	printf("creating file...\n");
23*fba84f08SIngo Weinhold 	int fd = open(fileName, O_CREAT | O_RDWR | O_TRUNC, 0644);
24*fba84f08SIngo Weinhold 	if (fd < 0) {
25*fba84f08SIngo Weinhold 		fprintf(stderr, "Failed to open \"%s\": %s\n", fileName,
26*fba84f08SIngo Weinhold 			strerror(errno));
27*fba84f08SIngo Weinhold 		exit(1);
28*fba84f08SIngo Weinhold 	}
29*fba84f08SIngo Weinhold 
30*fba84f08SIngo Weinhold 	// write half a page
31*fba84f08SIngo Weinhold 	printf("writing data to file...\n");
32*fba84f08SIngo Weinhold 	char buffer[2048];
33*fba84f08SIngo Weinhold 	memset(buffer, 0xdd, sizeof(buffer));
34*fba84f08SIngo Weinhold 	if (write(fd, buffer, sizeof(buffer)) != (ssize_t)sizeof(buffer)) {
35*fba84f08SIngo Weinhold 		fprintf(stderr, "Failed to write to file!\n");
36*fba84f08SIngo Weinhold 		exit(1);
37*fba84f08SIngo Weinhold 	}
38*fba84f08SIngo Weinhold 
39*fba84f08SIngo Weinhold 	// map the page
40*fba84f08SIngo Weinhold 	printf("mapping file...\n");
41*fba84f08SIngo Weinhold 	void* address = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
42*fba84f08SIngo Weinhold 	if (address == MAP_FAILED) {
43*fba84f08SIngo Weinhold 		fprintf(stderr, "Failed to map the file: %s\n", strerror(errno));
44*fba84f08SIngo Weinhold 		exit(1);
45*fba84f08SIngo Weinhold 	}
46*fba84f08SIngo Weinhold 
47*fba84f08SIngo Weinhold 	// truncate the file
48*fba84f08SIngo Weinhold 	printf("truncating file...\n");
49*fba84f08SIngo Weinhold 	if (ftruncate(fd, 0) < 0) {
50*fba84f08SIngo Weinhold 		fprintf(stderr, "Failed to truncate the file: %s\n", strerror(errno));
51*fba84f08SIngo Weinhold 		exit(1);
52*fba84f08SIngo Weinhold 	}
53*fba84f08SIngo Weinhold 
54*fba84f08SIngo Weinhold 	// touch data
55*fba84f08SIngo Weinhold 	printf("touching no longer mapped data (should fail with SIGBUS)...\n");
56*fba84f08SIngo Weinhold 	*(int*)address = 42;
57*fba84f08SIngo Weinhold 
58*fba84f08SIngo Weinhold 	printf("Shouldn't get here!!!\n");
59*fba84f08SIngo Weinhold 
60*fba84f08SIngo Weinhold 	return 0;
61*fba84f08SIngo Weinhold }
62