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