xref: /openbsd-src/regress/sys/kern/mmap3/mmaptest.c (revision 66ac7e0f41afa5ee38be0f647d036d1b3410f7fc)
1*66ac7e0fSdoug /* $OpenBSD: mmaptest.c,v 1.7 2014/09/27 06:28:45 doug Exp $ */
2027e7dcfSespie /*
3027e7dcfSespie  * Copyright (c) 2002 Marc Espie.
4027e7dcfSespie  *
5027e7dcfSespie  * Redistribution and use in source and binary forms, with or without
6027e7dcfSespie  * modification, are permitted provided that the following conditions
7027e7dcfSespie  * are met:
8027e7dcfSespie  * 1. Redistributions of source code must retain the above copyright
9027e7dcfSespie  *    notice, this list of conditions and the following disclaimer.
10027e7dcfSespie  * 2. Redistributions in binary form must reproduce the above copyright
11027e7dcfSespie  *    notice, this list of conditions and the following disclaimer in the
12027e7dcfSespie  *    documentation and/or other materials provided with the distribution.
13027e7dcfSespie  *
14027e7dcfSespie  * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
15027e7dcfSespie  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16027e7dcfSespie  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17027e7dcfSespie  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OPENBSD
18027e7dcfSespie  * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19027e7dcfSespie  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20027e7dcfSespie  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21027e7dcfSespie  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22027e7dcfSespie  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23027e7dcfSespie  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24027e7dcfSespie  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25027e7dcfSespie  */
26027e7dcfSespie 
27027e7dcfSespie #include <sys/types.h>
28027e7dcfSespie #include <sys/mman.h>
29027e7dcfSespie #include <fcntl.h>
30027e7dcfSespie #include <stdlib.h>
31027e7dcfSespie #include <stdio.h>
32027e7dcfSespie #include <err.h>
33ac336a76Sdavid #include <unistd.h>
34027e7dcfSespie 
359e6ef651Smickey #define	AREA	(16 * 4096)
36027e7dcfSespie /*
37027e7dcfSespie  * Check for mmap/ftruncate interaction.  Specifically, ftruncate on
38027e7dcfSespie  * a short file may lose modifications made through an mmapped area.
39027e7dcfSespie  */
40027e7dcfSespie int
main(int argc,char * argv[])41db3296cfSderaadt main(int argc, char *argv[])
42027e7dcfSespie {
43027e7dcfSespie 	int i;
44027e7dcfSespie 	int fd;
459e6ef651Smickey 	char area[AREA];
46027e7dcfSespie 	char *a2;
479e6ef651Smickey 	for (i = 0 ; i < AREA; i++)
48027e7dcfSespie 		area[i] = 5;
49027e7dcfSespie 
50027e7dcfSespie 	fd = open("test.out", O_WRONLY|O_CREAT|O_TRUNC, 0600);
51027e7dcfSespie 	if (fd == -1)
52027e7dcfSespie 		err(1, "open(test.out)");
539e6ef651Smickey 	if (write(fd, area, AREA) != AREA)
54027e7dcfSespie 		err(1, "write");
55027e7dcfSespie 	if (close(fd))
56027e7dcfSespie 		err(1, "close");
57027e7dcfSespie 	fd = open("test.out", O_RDWR);
58027e7dcfSespie 	if (fd == -1)
59027e7dcfSespie 		err(1, "open(test.out) 2");
609e6ef651Smickey 	a2 = mmap(NULL, AREA, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
61*66ac7e0fSdoug 	if (a2 == MAP_FAILED)
62027e7dcfSespie 		err(1, "mmap");
63027e7dcfSespie 	a2[10] = 3;
649e6ef651Smickey 	msync(a2, AREA, MS_SYNC|MS_INVALIDATE);
659e6ef651Smickey 	if (mlock(a2, AREA))
669e6ef651Smickey 		err(1, "mlock");
67027e7dcfSespie 	if (ftruncate(fd, 128))
68027e7dcfSespie 		err(1, "ftruncate");
699e6ef651Smickey 	if (munlock(a2, AREA))
709e6ef651Smickey 		err(1, "munlock");
71027e7dcfSespie 	if (close(fd))
72027e7dcfSespie 		err(1, "close");
73027e7dcfSespie 	fd = open("test.out", O_RDONLY);
74027e7dcfSespie 	if (fd == -1)
75027e7dcfSespie 		err(1, "open(test.out) 3");
769e6ef651Smickey 	if (read(fd, area, AREA) != 128)
77027e7dcfSespie 		err(1, "read");
78027e7dcfSespie 	if (area[10] != 3)
79027e7dcfSespie 		errx(1, "area[10] != 3");
80027e7dcfSespie 	if (close(fd))
81027e7dcfSespie 		err(1, "close");
82027e7dcfSespie 	return 0;
83027e7dcfSespie }
84027e7dcfSespie 
85