1 /* $NetBSD: t_msync.c,v 1.3 2017/01/14 20:52:42 christos Exp $ */
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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_msync.c,v 1.3 2017/01/14 20:52:42 christos Exp $");
45 static const off_t off = 512;
46 static const char path[] = "msync";
48 static const char *msync_sync(const char *, int);
51 msync_sync(const char *garbage, int flags)
53 char *buf, *map = MAP_FAILED;
54 const char *str = NULL;
59 * Create a temporary file, write
60 * one page to it, and map the file.
67 memset(buf, 'x', page);
69 fd = open(path, O_RDWR | O_CREAT, 0700);
73 return "failed to open";
76 ATF_REQUIRE_MSG(write(fd, buf, page) != -1, "write(2) failed: %s",
79 map = mmap(NULL, page, PROT_READ | PROT_WRITE, MAP_FILE|MAP_PRIVATE,
82 if (map == MAP_FAILED) {
83 str = "failed to map";
88 * Seek to an arbitrary offset and
89 * write garbage to this position.
91 if (lseek(fd, off, SEEK_SET) != off) {
92 str = "failed to seek";
96 len = strlen(garbage);
97 rv = write(fd, garbage, len);
99 if (rv != (ssize_t)len) {
100 str = "failed to write garbage";
105 * Synchronize the mapping and verify
106 * that garbage is at the given offset.
108 if (msync(map, page, flags) != 0) {
109 str = "failed to msync";
113 if (memcmp(map + off, garbage, len) != 0) {
114 str = "msync did not synchronize";
124 if (map != MAP_FAILED)
125 (void)munmap(map, page);
131 ATF_TC_HEAD(msync_async, tc)
133 atf_tc_set_md_var(tc, "descr", "Test of msync(2), MS_ASYNC");
136 ATF_TC_BODY(msync_async, tc)
140 str = msync_sync("garbage", MS_ASYNC);
143 atf_tc_fail("%s", str);
147 ATF_TC_HEAD(msync_err, tc)
149 atf_tc_set_md_var(tc, "descr", "Test error conditions in msync(2)");
152 ATF_TC_BODY(msync_err, tc)
155 char *map = MAP_FAILED;
158 * Test that invalid flags error out.
162 ATF_REQUIRE_ERRNO(EINVAL, msync_sync("error", -1) != NULL);
164 ATF_REQUIRE_ERRNO(EINVAL, msync_sync("error", INT_MAX) != NULL);
166 ATF_REQUIRE(msync_sync("error", -1) != NULL);
167 ATF_REQUIRE(msync_sync("error", INT_MAX) != NULL);
173 * Map a page and then unmap to get an unmapped address.
175 map = mmap(NULL, page, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
177 ATF_REQUIRE(map != MAP_FAILED);
179 (void)munmap(map, page);
181 ATF_REQUIRE(msync(map, page, MS_SYNC) != 0);
183 ATF_REQUIRE(errno == ENOMEM);
185 ATF_REQUIRE(errno == EFAULT);
189 ATF_TC(msync_invalidate);
190 ATF_TC_HEAD(msync_invalidate, tc)
192 atf_tc_set_md_var(tc, "descr", "Test of msync(2), MS_INVALIDATE");
195 ATF_TC_BODY(msync_invalidate, tc)
199 str = msync_sync("garbage", MS_INVALIDATE);
202 atf_tc_fail("%s", str);
206 ATF_TC_HEAD(msync_sync, tc)
208 atf_tc_set_md_var(tc, "descr", "Test of msync(2), MS_SYNC");
211 ATF_TC_BODY(msync_sync, tc)
215 str = msync_sync("garbage", MS_SYNC);
218 atf_tc_fail("%s", str);
224 page = sysconf(_SC_PAGESIZE);
226 ATF_REQUIRE(page >= 0);
227 ATF_REQUIRE(page > off);
229 ATF_TP_ADD_TC(tp, msync_async);
230 ATF_TP_ADD_TC(tp, msync_err);
231 ATF_TP_ADD_TC(tp, msync_invalidate);
232 ATF_TP_ADD_TC(tp, msync_sync);
234 return atf_no_error();