xref: /titanic_51/usr/src/test/libc-tests/tests/random/inz_split.c (revision f7b98820db2e767eb246fc6aef8f740f838f03c6)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright (c) 2015, Joyent, Inc.
14  */
15 
16 /*
17  * Verify that using MC_INHERIT_ZERO works just fine when applied to an entire
18  * region that then gets split in half, hence creating new segments.
19  */
20 
21 #include <sys/types.h>
22 #include <unistd.h>
23 #include <assert.h>
24 #include <sys/mman.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <stdlib.h>
28 #include <wait.h>
29 
30 int
31 main(void)
32 {
33 	void *buf;
34 	pid_t child;
35 	int ret, i;
36 	siginfo_t info;
37 	uint8_t *ubuf;
38 	size_t mapsz = sysconf(_SC_PAGESIZE) * 8;
39 	size_t spltsz = sysconf(_SC_PAGESIZE) * 4;
40 	size_t spltoff = sysconf(_SC_PAGESIZE) * 2;
41 
42 	buf = mmap(NULL, mapsz, PROT_READ | PROT_WRITE,
43 	    MAP_PRIVATE | MAP_ANON, -1, 0);
44 	assert(buf != MAP_FAILED);
45 	memset(buf, 'a', mapsz);
46 
47 	ret = memcntl(buf, mapsz, MC_INHERIT_ZERO, 0, 0, 0);
48 	assert(ret == 0);
49 
50 	ret = munmap(buf + spltoff, spltsz);
51 	assert(ret == 0);
52 
53 	child = fork();
54 	if (child == 0) {
55 		ubuf = buf;
56 		for (i = 0; i < spltoff; i++)
57 			assert(ubuf[i] == 0);
58 		for (i = spltoff + spltsz; i < mapsz; i++)
59 			assert(ubuf[i] == 0);
60 		exit(0);
61 	}
62 	assert(child != -1);
63 
64 	do {
65 		ret = waitid(P_PID, child, &info, WEXITED);
66 	} while (ret == -1 && errno == EINTR);
67 	assert(ret == 0);
68 	assert(info.si_pid == child);
69 	assert(info.si_status == 0);
70 
71 	ubuf = buf;
72 	for (i = 0; i < spltoff; i++)
73 		assert(ubuf[i] == 'a');
74 	for (i = spltoff + spltsz; i < mapsz; i++)
75 		assert(ubuf[i] == 'a');
76 
77 	return (0);
78 }
79