xref: /openbsd-src/regress/sys/uvm/mmap_write_self/mmap_write_self.c (revision f6aab3d83b51b91c24247ad2c2573574de475a82)
1 /*
2  * Copyright (c) 2011 Artur Grabowski <art@openbsd.org>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 
18 #include <sys/types.h>
19 #include <sys/mman.h>
20 #include <err.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <errno.h>
26 
27 /*
28  * Test a corner case writing to a file from an mmap region from that file
29  * should fail.
30  */
31 int
32 main(int argc, char **argv)
33 {
34 	char name[20] = "/tmp/fluff.XXXXXX";
35 	char *buf;
36 	size_t ps;
37 	int fd;
38 
39 	ps = getpagesize();
40 
41 	if ((fd = mkstemp(name)) == -1)
42 		err(1, "mkstemp");
43 
44 	if (unlink(name) == -1)
45 		err(1, "unlink");
46 
47 	buf = mmap(NULL, ps, PROT_READ, MAP_FILE|MAP_SHARED, fd, 0);
48 	if (buf == MAP_FAILED)
49 		err(1, "mmap");
50 
51 	if (pwrite(fd, buf, ps, 0) == ps)
52 		errx(1, "write to self succeeded");
53 
54 	if (errno != EFAULT)
55 		err(1, "unexpected errno");
56 
57 	return (0);
58 }
59