etc/protocols - sync with NetBSD-8
[minix.git] / tests / lib / libc / sys / t_minherit.c
blob0bdf6bc0a0d88eebfca39b2d5f4d0b52a9ab8a38
1 /* $NetBSD: t_minherit.c,v 1.1 2014/07/18 12:34:52 christos Exp $ */
3 /*-
4 * Copyright (c) 2014 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_minherit.c,v 1.1 2014/07/18 12:34:52 christos Exp $");
34 #include <sys/param.h>
35 #include <sys/mman.h>
36 #include <sys/sysctl.h>
37 #include <sys/wait.h>
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
45 #include <atf-c.h>
47 static long page;
49 static void *
50 makemap(int v, int f) {
51 void *map = mmap(NULL, page, PROT_READ|PROT_WRITE,
52 MAP_SHARED|MAP_ANON, -1, 0);
53 ATF_REQUIRE(map != MAP_FAILED);
54 memset(map, v, page);
55 if (f != 666)
56 ATF_REQUIRE(minherit(map, page, f) == 0);
57 else
58 ATF_REQUIRE(minherit(map, page, f) == -1);
59 return map;
62 ATF_TC(minherit_copy);
63 ATF_TC_HEAD(minherit_copy, tc)
65 atf_tc_set_md_var(tc, "descr",
66 "Test for MAP_INHERIT_COPY from minherit(2)");
69 ATF_TC_BODY(minherit_copy, tc)
71 void *map1 = makemap(1, MAP_INHERIT_COPY);
72 void *map2 = makemap(1, MAP_INHERIT_COPY);
73 switch (fork()) {
74 default:
75 ATF_REQUIRE(wait(NULL) != -1);
76 ATF_REQUIRE(memcmp(map1, map2, page) == 0);
77 break;
78 case -1:
79 ATF_REQUIRE(0);
80 break;
81 case 0:
82 ATF_REQUIRE(memcmp(map1, map2, page) == 0);
83 memset(map1, 0, page);
84 exit(0);
88 ATF_TC(minherit_share);
89 ATF_TC_HEAD(minherit_share, tc)
91 atf_tc_set_md_var(tc, "descr",
92 "Test for MAP_INHERIT_SHARE from minherit(2)");
95 ATF_TC_BODY(minherit_share, tc)
97 void *map1 = makemap(1, MAP_INHERIT_SHARE);
98 void *map2 = makemap(1, MAP_INHERIT_SHARE);
100 switch (fork()) {
101 default:
102 ATF_REQUIRE(wait(NULL) != -1);
103 memset(map2, 0, page);
104 ATF_REQUIRE(memcmp(map1, map2, page) == 0);
105 break;
106 case -1:
107 ATF_REQUIRE(0);
108 break;
109 case 0:
110 ATF_REQUIRE(memcmp(map1, map2, page) == 0);
111 memset(map1, 0, page);
112 exit(0);
116 static void
117 segv(int n) {
118 _exit(n);
121 ATF_TC(minherit_none);
122 ATF_TC_HEAD(minherit_none, tc)
124 atf_tc_set_md_var(tc, "descr",
125 "Test for MAP_INHERIT_NONE from minherit(2)");
128 ATF_TC_BODY(minherit_none, tc)
130 void *map1 = makemap(0, MAP_INHERIT_NONE);
131 int status;
133 switch (fork()) {
134 default:
135 ATF_REQUIRE(wait(&status) != -1);
136 ATF_REQUIRE(WEXITSTATUS(status) == SIGSEGV);
137 break;
138 case -1:
139 ATF_REQUIRE(0);
140 break;
141 case 0:
142 ATF_REQUIRE(signal(SIGSEGV, segv) != SIG_ERR);
143 memset(map1, 0, page);
144 exit(0);
148 ATF_TC(minherit_zero);
149 ATF_TC_HEAD(minherit_zero, tc)
151 atf_tc_set_md_var(tc, "descr",
152 "Test for MAP_INHERIT_ZERO from minherit(2)");
155 ATF_TC_BODY(minherit_zero, tc)
157 void *map1 = makemap(1, MAP_INHERIT_ZERO);
158 void *map2 = makemap(0, MAP_INHERIT_SHARE);
160 switch (fork()) {
161 default:
162 ATF_REQUIRE(wait(NULL) != -1);
163 memset(map2, 1, page);
164 ATF_REQUIRE(memcmp(map1, map2, page) == 0);
165 break;
166 case -1:
167 ATF_REQUIRE(0);
168 break;
169 case 0:
170 ATF_REQUIRE(memcmp(map1, map2, page) == 0);
171 memset(map1, 2, page);
172 exit(0);
176 ATF_TC(minherit_bad);
177 ATF_TC_HEAD(minherit_bad, tc)
179 atf_tc_set_md_var(tc, "descr",
180 "Test for bad minherit(2)");
183 ATF_TC_BODY(minherit_bad, tc)
185 (void)makemap(0, 666);
188 ATF_TP_ADD_TCS(tp)
190 page = sysconf(_SC_PAGESIZE);
191 ATF_REQUIRE(page >= 0);
193 ATF_TP_ADD_TC(tp, minherit_copy);
194 ATF_TP_ADD_TC(tp, minherit_share);
195 ATF_TP_ADD_TC(tp, minherit_none);
196 ATF_TP_ADD_TC(tp, minherit_zero);
197 ATF_TP_ADD_TC(tp, minherit_bad);
199 return atf_no_error();