1*f40f3adcSEnji Cooper /* $NetBSD: t_msync.c,v 1.3 2017/01/14 20:52:42 christos Exp $ */
257718be8SEnji Cooper
357718be8SEnji Cooper /*-
457718be8SEnji Cooper * Copyright (c) 2011 The NetBSD Foundation, Inc.
557718be8SEnji Cooper * All rights reserved.
657718be8SEnji Cooper *
757718be8SEnji Cooper * This code is derived from software contributed to The NetBSD Foundation
857718be8SEnji Cooper * by Jukka Ruohonen.
957718be8SEnji Cooper *
1057718be8SEnji Cooper * Redistribution and use in source and binary forms, with or without
1157718be8SEnji Cooper * modification, are permitted provided that the following conditions
1257718be8SEnji Cooper * are met:
1357718be8SEnji Cooper * 1. Redistributions of source code must retain the above copyright
1457718be8SEnji Cooper * notice, this list of conditions and the following disclaimer.
1557718be8SEnji Cooper * 2. Redistributions in binary form must reproduce the above copyright
1657718be8SEnji Cooper * notice, this list of conditions and the following disclaimer in the
1757718be8SEnji Cooper * documentation and/or other materials provided with the distribution.
1857718be8SEnji Cooper *
1957718be8SEnji Cooper * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2057718be8SEnji Cooper * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2157718be8SEnji Cooper * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2257718be8SEnji Cooper * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2357718be8SEnji Cooper * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2457718be8SEnji Cooper * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2557718be8SEnji Cooper * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2657718be8SEnji Cooper * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2757718be8SEnji Cooper * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2857718be8SEnji Cooper * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2957718be8SEnji Cooper * POSSIBILITY OF SUCH DAMAGE.
3057718be8SEnji Cooper */
3157718be8SEnji Cooper #include <sys/cdefs.h>
32*f40f3adcSEnji Cooper __RCSID("$NetBSD: t_msync.c,v 1.3 2017/01/14 20:52:42 christos Exp $");
3357718be8SEnji Cooper
3457718be8SEnji Cooper #include <sys/mman.h>
3557718be8SEnji Cooper
3657718be8SEnji Cooper #include <atf-c.h>
3757718be8SEnji Cooper #include <errno.h>
3857718be8SEnji Cooper #include <fcntl.h>
3957718be8SEnji Cooper #include <limits.h>
4057718be8SEnji Cooper #include <stdlib.h>
4157718be8SEnji Cooper #include <string.h>
4257718be8SEnji Cooper #include <unistd.h>
4357718be8SEnji Cooper
4457718be8SEnji Cooper static long page = 0;
4557718be8SEnji Cooper static const off_t off = 512;
4657718be8SEnji Cooper static const char path[] = "msync";
4757718be8SEnji Cooper
4857718be8SEnji Cooper static const char *msync_sync(const char *, int);
4957718be8SEnji Cooper
5057718be8SEnji Cooper static const char *
msync_sync(const char * garbage,int flags)5157718be8SEnji Cooper msync_sync(const char *garbage, int flags)
5257718be8SEnji Cooper {
5357718be8SEnji Cooper char *buf, *map = MAP_FAILED;
5457718be8SEnji Cooper const char *str = NULL;
551f6a3aa8SEnji Cooper size_t len;
5657718be8SEnji Cooper int fd, rv;
5757718be8SEnji Cooper
5857718be8SEnji Cooper /*
5957718be8SEnji Cooper * Create a temporary file, write
6057718be8SEnji Cooper * one page to it, and map the file.
6157718be8SEnji Cooper */
6257718be8SEnji Cooper buf = malloc(page);
6357718be8SEnji Cooper
6457718be8SEnji Cooper if (buf == NULL)
6557718be8SEnji Cooper return NULL;
6657718be8SEnji Cooper
672a1440c6SEnji Cooper memset(buf, 'x', page);
6857718be8SEnji Cooper
6957718be8SEnji Cooper fd = open(path, O_RDWR | O_CREAT, 0700);
7057718be8SEnji Cooper
7157718be8SEnji Cooper if (fd < 0) {
722a1440c6SEnji Cooper free(buf);
732a1440c6SEnji Cooper return "failed to open";
7457718be8SEnji Cooper }
7557718be8SEnji Cooper
76*f40f3adcSEnji Cooper ATF_REQUIRE_MSG(write(fd, buf, page) != -1, "write(2) failed: %s",
77*f40f3adcSEnji Cooper strerror(errno));
7857718be8SEnji Cooper
7957718be8SEnji Cooper map = mmap(NULL, page, PROT_READ | PROT_WRITE, MAP_FILE|MAP_PRIVATE,
8057718be8SEnji Cooper fd, 0);
8157718be8SEnji Cooper
8257718be8SEnji Cooper if (map == MAP_FAILED) {
8357718be8SEnji Cooper str = "failed to map";
8457718be8SEnji Cooper goto out;
8557718be8SEnji Cooper }
8657718be8SEnji Cooper
8757718be8SEnji Cooper /*
8857718be8SEnji Cooper * Seek to an arbitrary offset and
8957718be8SEnji Cooper * write garbage to this position.
9057718be8SEnji Cooper */
9157718be8SEnji Cooper if (lseek(fd, off, SEEK_SET) != off) {
9257718be8SEnji Cooper str = "failed to seek";
9357718be8SEnji Cooper goto out;
9457718be8SEnji Cooper }
9557718be8SEnji Cooper
9657718be8SEnji Cooper len = strlen(garbage);
9757718be8SEnji Cooper rv = write(fd, garbage, len);
9857718be8SEnji Cooper
9957718be8SEnji Cooper if (rv != (ssize_t)len) {
10057718be8SEnji Cooper str = "failed to write garbage";
10157718be8SEnji Cooper goto out;
10257718be8SEnji Cooper }
10357718be8SEnji Cooper
10457718be8SEnji Cooper /*
10557718be8SEnji Cooper * Synchronize the mapping and verify
10657718be8SEnji Cooper * that garbage is at the given offset.
10757718be8SEnji Cooper */
10857718be8SEnji Cooper if (msync(map, page, flags) != 0) {
10957718be8SEnji Cooper str = "failed to msync";
11057718be8SEnji Cooper goto out;
11157718be8SEnji Cooper }
11257718be8SEnji Cooper
11357718be8SEnji Cooper if (memcmp(map + off, garbage, len) != 0) {
11457718be8SEnji Cooper str = "msync did not synchronize";
11557718be8SEnji Cooper goto out;
11657718be8SEnji Cooper }
11757718be8SEnji Cooper
11857718be8SEnji Cooper out:
11957718be8SEnji Cooper free(buf);
12057718be8SEnji Cooper
12157718be8SEnji Cooper (void)close(fd);
12257718be8SEnji Cooper (void)unlink(path);
12357718be8SEnji Cooper
12457718be8SEnji Cooper if (map != MAP_FAILED)
12557718be8SEnji Cooper (void)munmap(map, page);
12657718be8SEnji Cooper
12757718be8SEnji Cooper return str;
12857718be8SEnji Cooper }
12957718be8SEnji Cooper
13057718be8SEnji Cooper ATF_TC(msync_async);
ATF_TC_HEAD(msync_async,tc)13157718be8SEnji Cooper ATF_TC_HEAD(msync_async, tc)
13257718be8SEnji Cooper {
13357718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "Test of msync(2), MS_ASYNC");
13457718be8SEnji Cooper }
13557718be8SEnji Cooper
ATF_TC_BODY(msync_async,tc)13657718be8SEnji Cooper ATF_TC_BODY(msync_async, tc)
13757718be8SEnji Cooper {
13857718be8SEnji Cooper const char *str;
13957718be8SEnji Cooper
14057718be8SEnji Cooper str = msync_sync("garbage", MS_ASYNC);
14157718be8SEnji Cooper
14257718be8SEnji Cooper if (str != NULL)
14357718be8SEnji Cooper atf_tc_fail("%s", str);
14457718be8SEnji Cooper }
14557718be8SEnji Cooper
14657718be8SEnji Cooper ATF_TC(msync_err);
ATF_TC_HEAD(msync_err,tc)14757718be8SEnji Cooper ATF_TC_HEAD(msync_err, tc)
14857718be8SEnji Cooper {
14957718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "Test error conditions in msync(2)");
15057718be8SEnji Cooper }
15157718be8SEnji Cooper
ATF_TC_BODY(msync_err,tc)15257718be8SEnji Cooper ATF_TC_BODY(msync_err, tc)
15357718be8SEnji Cooper {
15457718be8SEnji Cooper
15557718be8SEnji Cooper char *map = MAP_FAILED;
15657718be8SEnji Cooper
15757718be8SEnji Cooper /*
15857718be8SEnji Cooper * Test that invalid flags error out.
15957718be8SEnji Cooper */
160dfbf472aSEnji Cooper #ifdef __FreeBSD__
161dfbf472aSEnji Cooper errno = 0;
162dfbf472aSEnji Cooper ATF_REQUIRE_ERRNO(EINVAL, msync_sync("error", -1) != NULL);
163dfbf472aSEnji Cooper errno = 0;
164dfbf472aSEnji Cooper ATF_REQUIRE_ERRNO(EINVAL, msync_sync("error", INT_MAX) != NULL);
165dfbf472aSEnji Cooper #else
16657718be8SEnji Cooper ATF_REQUIRE(msync_sync("error", -1) != NULL);
16757718be8SEnji Cooper ATF_REQUIRE(msync_sync("error", INT_MAX) != NULL);
168dfbf472aSEnji Cooper #endif
16957718be8SEnji Cooper
17057718be8SEnji Cooper errno = 0;
17157718be8SEnji Cooper
17257718be8SEnji Cooper /*
17357718be8SEnji Cooper * Map a page and then unmap to get an unmapped address.
17457718be8SEnji Cooper */
17557718be8SEnji Cooper map = mmap(NULL, page, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
17657718be8SEnji Cooper -1, 0);
17757718be8SEnji Cooper ATF_REQUIRE(map != MAP_FAILED);
17857718be8SEnji Cooper
17957718be8SEnji Cooper (void)munmap(map, page);
18057718be8SEnji Cooper
18157718be8SEnji Cooper ATF_REQUIRE(msync(map, page, MS_SYNC) != 0);
182dfbf472aSEnji Cooper #ifdef __FreeBSD__
183dfbf472aSEnji Cooper ATF_REQUIRE(errno == ENOMEM);
184dfbf472aSEnji Cooper #else
18557718be8SEnji Cooper ATF_REQUIRE(errno == EFAULT);
186dfbf472aSEnji Cooper #endif
18757718be8SEnji Cooper }
18857718be8SEnji Cooper
18957718be8SEnji Cooper ATF_TC(msync_invalidate);
ATF_TC_HEAD(msync_invalidate,tc)19057718be8SEnji Cooper ATF_TC_HEAD(msync_invalidate, tc)
19157718be8SEnji Cooper {
19257718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "Test of msync(2), MS_INVALIDATE");
19357718be8SEnji Cooper }
19457718be8SEnji Cooper
ATF_TC_BODY(msync_invalidate,tc)19557718be8SEnji Cooper ATF_TC_BODY(msync_invalidate, tc)
19657718be8SEnji Cooper {
19757718be8SEnji Cooper const char *str;
19857718be8SEnji Cooper
19957718be8SEnji Cooper str = msync_sync("garbage", MS_INVALIDATE);
20057718be8SEnji Cooper
20157718be8SEnji Cooper if (str != NULL)
20257718be8SEnji Cooper atf_tc_fail("%s", str);
20357718be8SEnji Cooper }
20457718be8SEnji Cooper
20557718be8SEnji Cooper ATF_TC(msync_sync);
ATF_TC_HEAD(msync_sync,tc)20657718be8SEnji Cooper ATF_TC_HEAD(msync_sync, tc)
20757718be8SEnji Cooper {
20857718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "Test of msync(2), MS_SYNC");
20957718be8SEnji Cooper }
21057718be8SEnji Cooper
ATF_TC_BODY(msync_sync,tc)21157718be8SEnji Cooper ATF_TC_BODY(msync_sync, tc)
21257718be8SEnji Cooper {
21357718be8SEnji Cooper const char *str;
21457718be8SEnji Cooper
21557718be8SEnji Cooper str = msync_sync("garbage", MS_SYNC);
21657718be8SEnji Cooper
21757718be8SEnji Cooper if (str != NULL)
21857718be8SEnji Cooper atf_tc_fail("%s", str);
21957718be8SEnji Cooper }
22057718be8SEnji Cooper
ATF_TP_ADD_TCS(tp)22157718be8SEnji Cooper ATF_TP_ADD_TCS(tp)
22257718be8SEnji Cooper {
22357718be8SEnji Cooper
22457718be8SEnji Cooper page = sysconf(_SC_PAGESIZE);
22557718be8SEnji Cooper
22657718be8SEnji Cooper ATF_REQUIRE(page >= 0);
22757718be8SEnji Cooper ATF_REQUIRE(page > off);
22857718be8SEnji Cooper
22957718be8SEnji Cooper ATF_TP_ADD_TC(tp, msync_async);
23057718be8SEnji Cooper ATF_TP_ADD_TC(tp, msync_err);
23157718be8SEnji Cooper ATF_TP_ADD_TC(tp, msync_invalidate);
23257718be8SEnji Cooper ATF_TP_ADD_TC(tp, msync_sync);
23357718be8SEnji Cooper
23457718be8SEnji Cooper return atf_no_error();
23557718be8SEnji Cooper }
236