1 /* $NetBSD: t_msync.c,v 1.2 2012/03/16 06:15:17 matt Exp $ */ 2 3 /*- 4 * Copyright (c) 2011 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jukka Ruohonen. 9 * 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. 18 * 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. 30 */ 31 #include <sys/cdefs.h> 32 __RCSID("$NetBSD: t_msync.c,v 1.2 2012/03/16 06:15:17 matt Exp $"); 33 34 #include <sys/mman.h> 35 36 #include <atf-c.h> 37 #include <errno.h> 38 #include <fcntl.h> 39 #include <limits.h> 40 #include <stdlib.h> 41 #include <string.h> 42 #include <unistd.h> 43 44 static long page = 0; 45 static const off_t off = 512; 46 static const char path[] = "msync"; 47 48 static const char *msync_sync(const char *, int); 49 50 static const char * 51 msync_sync(const char *garbage, int flags) 52 { 53 char *buf, *map = MAP_FAILED; 54 const char *str = NULL; 55 size_t i, len; 56 ssize_t tot; 57 int fd, rv; 58 59 /* 60 * Create a temporary file, write 61 * one page to it, and map the file. 62 */ 63 buf = malloc(page); 64 65 if (buf == NULL) 66 return NULL; 67 68 #ifdef __FreeBSD__ 69 memset(buf, 'x', page); 70 #else 71 for (i = 0; i < (size_t)page; i++) 72 buf[i] = 'x'; 73 #endif 74 75 fd = open(path, O_RDWR | O_CREAT, 0700); 76 77 if (fd < 0) { 78 #ifdef __FreeBSD__ 79 free(buf); 80 return "failed to open"; 81 #else 82 str = "failed to open"; 83 goto out; 84 #endif 85 } 86 87 #if __FreeBSD__ 88 (void)write(fd, buf, page); 89 #else 90 tot = 0; 91 92 while (tot < page) { 93 94 rv = write(fd, buf, sizeof(buf)); 95 if (rv < 0) { 96 str = "failed to write"; 97 goto out; 98 } 99 100 tot += rv; 101 } 102 #endif 103 104 map = mmap(NULL, page, PROT_READ | PROT_WRITE, MAP_FILE|MAP_PRIVATE, 105 fd, 0); 106 107 if (map == MAP_FAILED) { 108 str = "failed to map"; 109 goto out; 110 } 111 112 /* 113 * Seek to an arbitrary offset and 114 * write garbage to this position. 115 */ 116 if (lseek(fd, off, SEEK_SET) != off) { 117 str = "failed to seek"; 118 goto out; 119 } 120 121 len = strlen(garbage); 122 rv = write(fd, garbage, len); 123 124 if (rv != (ssize_t)len) { 125 str = "failed to write garbage"; 126 goto out; 127 } 128 129 /* 130 * Synchronize the mapping and verify 131 * that garbage is at the given offset. 132 */ 133 if (msync(map, page, flags) != 0) { 134 str = "failed to msync"; 135 goto out; 136 } 137 138 if (memcmp(map + off, garbage, len) != 0) { 139 str = "msync did not synchronize"; 140 goto out; 141 } 142 143 out: 144 free(buf); 145 146 (void)close(fd); 147 (void)unlink(path); 148 149 if (map != MAP_FAILED) 150 (void)munmap(map, page); 151 152 return str; 153 } 154 155 ATF_TC(msync_async); 156 ATF_TC_HEAD(msync_async, tc) 157 { 158 atf_tc_set_md_var(tc, "descr", "Test of msync(2), MS_ASYNC"); 159 } 160 161 ATF_TC_BODY(msync_async, tc) 162 { 163 const char *str; 164 165 str = msync_sync("garbage", MS_ASYNC); 166 167 if (str != NULL) 168 atf_tc_fail("%s", str); 169 } 170 171 ATF_TC(msync_err); 172 ATF_TC_HEAD(msync_err, tc) 173 { 174 atf_tc_set_md_var(tc, "descr", "Test error conditions in msync(2)"); 175 } 176 177 ATF_TC_BODY(msync_err, tc) 178 { 179 180 char *map = MAP_FAILED; 181 182 /* 183 * Test that invalid flags error out. 184 */ 185 #ifdef __FreeBSD__ 186 errno = 0; 187 ATF_REQUIRE_ERRNO(EINVAL, msync_sync("error", -1) != NULL); 188 errno = 0; 189 ATF_REQUIRE_ERRNO(EINVAL, msync_sync("error", INT_MAX) != NULL); 190 #else 191 ATF_REQUIRE(msync_sync("error", -1) != NULL); 192 ATF_REQUIRE(msync_sync("error", INT_MAX) != NULL); 193 #endif 194 195 errno = 0; 196 197 /* 198 * Map a page and then unmap to get an unmapped address. 199 */ 200 map = mmap(NULL, page, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, 201 -1, 0); 202 ATF_REQUIRE(map != MAP_FAILED); 203 204 (void)munmap(map, page); 205 206 ATF_REQUIRE(msync(map, page, MS_SYNC) != 0); 207 #ifdef __FreeBSD__ 208 ATF_REQUIRE(errno == ENOMEM); 209 #else 210 ATF_REQUIRE(errno == EFAULT); 211 #endif 212 } 213 214 ATF_TC(msync_invalidate); 215 ATF_TC_HEAD(msync_invalidate, tc) 216 { 217 atf_tc_set_md_var(tc, "descr", "Test of msync(2), MS_INVALIDATE"); 218 } 219 220 ATF_TC_BODY(msync_invalidate, tc) 221 { 222 const char *str; 223 224 str = msync_sync("garbage", MS_INVALIDATE); 225 226 if (str != NULL) 227 atf_tc_fail("%s", str); 228 } 229 230 ATF_TC(msync_sync); 231 ATF_TC_HEAD(msync_sync, tc) 232 { 233 atf_tc_set_md_var(tc, "descr", "Test of msync(2), MS_SYNC"); 234 } 235 236 ATF_TC_BODY(msync_sync, tc) 237 { 238 const char *str; 239 240 str = msync_sync("garbage", MS_SYNC); 241 242 if (str != NULL) 243 atf_tc_fail("%s", str); 244 } 245 246 ATF_TP_ADD_TCS(tp) 247 { 248 249 page = sysconf(_SC_PAGESIZE); 250 251 ATF_REQUIRE(page >= 0); 252 ATF_REQUIRE(page > off); 253 254 ATF_TP_ADD_TC(tp, msync_async); 255 ATF_TP_ADD_TC(tp, msync_err); 256 ATF_TP_ADD_TC(tp, msync_invalidate); 257 ATF_TP_ADD_TC(tp, msync_sync); 258 259 return atf_no_error(); 260 } 261