plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2001 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 3 | * |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 8 | * |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 12 | * the GNU General Public License for more details. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 13 | * |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 20 | * Test Description: |
| 21 | * Call mmap() with prot parameter set to PROT_READ and with the file |
| 22 | * descriptor being open for read, to map a file creating mapped memory |
| 23 | * with read access. The minimum file permissions should be 0444. |
| 24 | * |
| 25 | * The call should succeed to create the mapped region with required |
| 26 | * attributes. |
| 27 | * |
| 28 | * Expected Result: |
| 29 | * mmap() should succeed returning the address of the mapped region, |
| 30 | * the mapped region should contain the contents of the mapped file. |
| 31 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 32 | * HISTORY |
| 33 | * 07/2001 Ported by Wayne Boyer |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 34 | */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 35 | #include <stdio.h> |
| 36 | #include <stdlib.h> |
| 37 | #include <sys/types.h> |
| 38 | #include <errno.h> |
| 39 | #include <unistd.h> |
| 40 | #include <fcntl.h> |
| 41 | #include <string.h> |
| 42 | #include <signal.h> |
| 43 | #include <sys/stat.h> |
| 44 | #include <sys/mman.h> |
| 45 | |
| 46 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 47 | |
| 48 | #define TEMPFILE "mmapfile" |
| 49 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 50 | char *TCID = "mmap02"; |
| 51 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 52 | |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 53 | static char *addr; |
| 54 | static char *dummy; |
| 55 | static size_t page_sz; |
| 56 | static int fildes; |
| 57 | |
| 58 | static void setup(void); |
| 59 | static void cleanup(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 60 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 61 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 62 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 63 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 64 | const char *msg; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 65 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 66 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 67 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 68 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 69 | setup(); |
| 70 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 71 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 72 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 73 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 74 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 75 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 76 | * Call mmap to map the temporary file 'TEMPFILE' |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 77 | * with read access. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 78 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 79 | errno = 0; |
| 80 | addr = mmap(0, page_sz, PROT_READ, |
| 81 | MAP_FILE | MAP_SHARED, fildes, 0); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 82 | |
| 83 | /* Check for the return value of mmap() */ |
robbiew | d5c2112 | 2002-03-22 16:22:40 +0000 | [diff] [blame] | 84 | if (addr == MAP_FAILED) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 85 | tst_resm(TFAIL | TERRNO, "mmap of %s failed", TEMPFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 86 | continue; |
| 87 | } |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 88 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 89 | /* |
| 90 | * Read the file contents into the dummy |
| 91 | * string. |
| 92 | */ |
| 93 | if (read(fildes, dummy, page_sz) < 0) { |
| 94 | tst_brkm(TFAIL | TERRNO, cleanup, |
| 95 | "reading %s failed", TEMPFILE); |
| 96 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 97 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 98 | /* |
| 99 | * Check whether mapped memory region has |
| 100 | * the file contents. |
| 101 | */ |
| 102 | if (memcmp(dummy, addr, page_sz)) { |
| 103 | tst_resm(TFAIL, "mapped memory area contains " |
| 104 | "invalid data"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 106 | tst_resm(TPASS, |
| 107 | "Functionality of mmap() successful"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /* Clean up things in case we are looping */ |
| 111 | /* Unmap the mapped memory */ |
| 112 | if (munmap(addr, page_sz) != 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 113 | tst_brkm(TFAIL | TERRNO, cleanup, "munmapping failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 114 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 115 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 116 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 117 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 118 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 119 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 120 | |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 121 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 122 | { |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 123 | char *tst_buff; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 124 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 125 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 126 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 127 | TEST_PAUSE; |
| 128 | |
Cyril Hrubis | ec22fb2 | 2013-06-04 20:37:05 +0200 | [diff] [blame] | 129 | page_sz = getpagesize(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 130 | |
| 131 | /* Allocate space for the test buffer */ |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 132 | if ((tst_buff = calloc(page_sz, sizeof(char))) == NULL) { |
Garrett Cooper | 11d5104 | 2010-11-22 20:47:29 -0800 | [diff] [blame] | 133 | tst_brkm(TFAIL, NULL, "calloc failed (tst_buff)"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /* Fill the test buffer with the known data */ |
| 137 | memset(tst_buff, 'A', page_sz); |
| 138 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 139 | tst_tmpdir(); |
| 140 | |
| 141 | /* Creat a temporary file used for mapping */ |
| 142 | if ((fildes = open(TEMPFILE, O_RDWR | O_CREAT, 0666)) < 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 143 | free(tst_buff); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 144 | tst_brkm(TFAIL | TERRNO, cleanup, "opening %s failed", |
| 145 | TEMPFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /* Write test buffer contents into temporary file */ |
robbiew | 9136137 | 2003-07-09 17:51:16 +0000 | [diff] [blame] | 149 | if (write(fildes, tst_buff, page_sz) < page_sz) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 150 | free(tst_buff); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 151 | tst_brkm(TFAIL | TERRNO, cleanup, |
| 152 | "writing to %s failed", TEMPFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 153 | cleanup(); |
| 154 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 155 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 156 | /* Free the memory allocated for test buffer */ |
| 157 | free(tst_buff); |
| 158 | |
Garrett Cooper | 11d5104 | 2010-11-22 20:47:29 -0800 | [diff] [blame] | 159 | /* Change Mode permissions on Temporary file */ |
| 160 | if (fchmod(fildes, 0444) < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 161 | tst_brkm(TFAIL | TERRNO, cleanup, "fchmod(%s, 0444) failed", |
| 162 | TEMPFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Garrett Cooper | 11d5104 | 2010-11-22 20:47:29 -0800 | [diff] [blame] | 165 | /* Close the temporary file */ |
| 166 | if (close(fildes) < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 167 | tst_brkm(TFAIL | TERRNO, cleanup, "closing %s failed", |
| 168 | TEMPFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /* Open the temporary file again, - Readonly mode */ |
| 172 | if ((fildes = open(TEMPFILE, O_RDONLY)) < 0) { |
Garrett Cooper | 11d5104 | 2010-11-22 20:47:29 -0800 | [diff] [blame] | 173 | tst_brkm(TFAIL, cleanup, "reopening %s readonly failed", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 174 | TEMPFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 175 | } |
| 176 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 177 | /* Allocate and initialize dummy string of system page size bytes */ |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 178 | if ((dummy = calloc(page_sz, sizeof(char))) == NULL) { |
Garrett Cooper | 11d5104 | 2010-11-22 20:47:29 -0800 | [diff] [blame] | 179 | tst_brkm(TFAIL, cleanup, "calloc failed (dummy)"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | } |
| 183 | |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 184 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 185 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 186 | close(fildes); |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 187 | free(dummy); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 188 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 189 | } |