xref: /illumos-gate/usr/src/test/libc-tests/tests/random/inz_mlock.c (revision 20a7641f9918de8574b8b3b47dbe35c4bfc78df1)
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 is mlocked.
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) * 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 = mlock(buf, mapsz);
46 	assert(ret == 0);
47 
48 	ret = memcntl(buf, mapsz, MC_INHERIT_ZERO, 0, 0, 0);
49 	assert(ret == 0);
50 
51 	child = fork();
52 	if (child == 0) {
53 		for (i = 0, ubuf = buf; i < mapsz; i++)
54 			assert(ubuf[i] == 0);
55 		exit(0);
56 	}
57 	assert(child != -1);
58 
59 	do {
60 		ret = waitid(P_PID, child, &info, WEXITED);
61 	} while (ret == -1 && errno == EINTR);
62 	assert(ret == 0);
63 	assert(info.si_pid == child);
64 	assert(info.si_status == 0);
65 
66 	for (i = 0, ubuf = buf; i < mapsz; i++)
67 		assert(ubuf[i] == 'a');
68 
69 	return (0);
70 }
71