1 /* $Id: mmaptest.c,v 1.1 2001/06/19 08:41:59 niklas Exp $ */ 2 3 /* 4 * Copyright (c) 2001 Niklas Hallqvist. All rights reserverd. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by Theo de Raadt. 17 * 4. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/types.h> 33 #include <sys/mman.h> 34 #include <err.h> 35 #include <fcntl.h> 36 #include <stdio.h> 37 #include <unistd.h> 38 39 #define TEMPL "test-fileXXXXXXXXXX" 40 #define MAGIC 0x1234 41 42 int 43 main(int argc, char **argv) 44 { 45 int fd; 46 void *v; 47 int i; 48 ssize_t n; 49 static char nm[] = TEMPL; 50 off_t sz; 51 52 fd = mkstemp(nm); 53 if (fd == -1) 54 err(1, "mkstemp"); 55 sz = sysconf(_SC_PAGESIZE); 56 if (sz == -1) 57 err(1, "sysconf"); 58 if (ftruncate(fd, sz) == -1) 59 err (1, "ftruncate"); 60 v = mmap(0, sz, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); 61 if (v == MAP_FAILED) 62 err(1, "mmap"); 63 *(int *)v = MAGIC; 64 if (msync(v, sz, MS_SYNC) == -1) 65 err(1, "msync"); 66 if (munmap(v, sz) == -1) 67 err(1, "munmap"); 68 if (close(fd) == -1) 69 err(1, "close"); 70 fd = open(nm, O_RDONLY); 71 if (fd == -1) 72 err(1, "open"); 73 if (unlink(nm) == -1) 74 err(1, "unlink"); 75 n = read(fd, &i, sizeof i); 76 if (n == -1) 77 err(1, "read"); 78 if (n != sizeof i) 79 errx(1, "short read"); 80 if (close(fd) == -1) 81 err(1, "close"); 82 exit(i != MAGIC); 83 } 84