xref: /titanic_41/usr/src/test/libc-tests/tests/random/inz_region.c (revision 28f17cb2cce2c4ba0dcda9d9f52a8935aa02e489)
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.
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
main(void)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) * 2;
39 
40 	buf = mmap(NULL, mapsz, PROT_READ | PROT_WRITE,
41 	    MAP_PRIVATE | MAP_ANON, -1, 0);
42 	assert(buf != MAP_FAILED);
43 	memset(buf, 'a', mapsz);
44 
45 	ret = memcntl(buf, mapsz, MC_INHERIT_ZERO, 0, 0, 0);
46 	assert(ret == 0);
47 
48 	child = fork();
49 	if (child == 0) {
50 		for (i = 0, ubuf = buf; i < mapsz; i++)
51 			assert(ubuf[i] == 0);
52 		exit(0);
53 	}
54 	assert(child != -1);
55 
56 	do {
57 		ret = waitid(P_PID, child, &info, WEXITED);
58 	} while (ret == -1 && errno == EINTR);
59 	assert(ret == 0);
60 	assert(info.si_pid == child);
61 	assert(info.si_status == 0);
62 
63 	for (i = 0, ubuf = buf; i < mapsz; i++)
64 		assert(ubuf[i] == 'a');
65 
66 	return (0);
67 }
68