xref: /openbsd-src/regress/sys/kern/minherit/minherit.c (revision ac336a76b5f272dade8319849c50bc940d378fa3)
1 /*	$OpenBSD: minherit.c,v 1.3 2003/08/02 01:24:36 david Exp $	*/
2 /*
3  * Written by Artur Grabowski <art@openbsd.org> Public Domain.
4  */
5 #include <sys/types.h>
6 #include <sys/wait.h>
7 #include <sys/mman.h>
8 
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <err.h>
13 #include <unistd.h>
14 
15 #define MAGIC "inherited"
16 
17 int
main(int argc,char * argv[])18 main(int argc, char *argv[])
19 {
20 	void *map1, *map2;
21 	int page_size;
22 	int status;
23 
24 	page_size = getpagesize();
25 
26 	if ((map1 = mmap(NULL, page_size, PROT_READ|PROT_WRITE, MAP_ANON,
27 	    -1, 0)) == MAP_FAILED)
28 		err(1, "mmap");
29 
30 	if ((map2 = mmap(NULL, page_size, PROT_READ|PROT_WRITE, MAP_ANON,
31 	    -1, 0)) == MAP_FAILED)
32 		err(1, "mmap");
33 
34 	memset(map1, 0, sizeof(MAGIC));
35 	memcpy(map2, MAGIC, sizeof(MAGIC));
36 
37 	if (minherit(map1, page_size, MAP_INHERIT_SHARE) != 0)
38 		err(1, "minherit");
39 
40 	if (minherit(map2, page_size, MAP_INHERIT_NONE) != 0)
41 		err(1, "minherit");
42 
43 	switch(fork()) {
44 	case -1:
45 		err(1, "fork");
46 	case 0:
47 		memcpy(map1, MAGIC, sizeof(MAGIC));
48 		/* map2 is not mapped and should give us error on munmap */
49 		if (munmap(map2, page_size) == 0)
50 			_exit(1);
51 		_exit(0);
52 	}
53 
54 	if (wait(&status) < 0)
55 		err(1, "wait");
56 
57 	if (!WIFEXITED(status))
58 		err(1, "child error");
59 
60 	if (memcmp(map1, MAGIC, sizeof(MAGIC)) != 0)
61 		return 1;
62 
63 	return WEXITSTATUS(status) != 0;
64 }
65