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() to map a file creating a mapped region with read access |
| 22 | * under the following conditions - |
| 23 | * - The prot parameter is set to PROT_READ |
| 24 | * - The file descriptor is open for writing. |
| 25 | * |
| 26 | * The call should fail to map the file. |
| 27 | * |
| 28 | * Expected Result: |
| 29 | * mmap() should fail returning -1 and errno should get set to EACCES. |
| 30 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 31 | * HISTORY |
| 32 | * 07/2001 Ported by Wayne Boyer |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 33 | */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 34 | #include <stdio.h> |
| 35 | #include <stdlib.h> |
| 36 | #include <sys/types.h> |
| 37 | #include <errno.h> |
| 38 | #include <unistd.h> |
| 39 | #include <fcntl.h> |
| 40 | #include <string.h> |
| 41 | #include <signal.h> |
| 42 | #include <sys/stat.h> |
| 43 | #include <sys/mman.h> |
| 44 | |
| 45 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 46 | |
| 47 | #define TEMPFILE "mmapfile" |
| 48 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 49 | char *TCID = "mmap06"; |
| 50 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 51 | |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 52 | static size_t page_sz; |
| 53 | static char *addr; |
| 54 | static int fildes; |
| 55 | |
| 56 | static void setup(void); |
| 57 | static void cleanup(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 58 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 59 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 60 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 61 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 62 | const char *msg; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 63 | |
Garrett Cooper | 11d5104 | 2010-11-22 20:47:29 -0800 | [diff] [blame] | 64 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 65 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 66 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 67 | setup(); |
| 68 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 69 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 70 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 71 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 73 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 74 | * Call mmap to map the temporary file 'TEMPFILE' |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 75 | * with read access. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 76 | */ |
robbiew | d5c2112 | 2002-03-22 16:22:40 +0000 | [diff] [blame] | 77 | errno = 0; |
| 78 | addr = mmap(0, page_sz, PROT_READ, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 79 | MAP_FILE | MAP_SHARED, fildes, 0); |
robbiew | d5c2112 | 2002-03-22 16:22:40 +0000 | [diff] [blame] | 80 | TEST_ERRNO = errno; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | |
| 82 | /* Check for the return value of mmap() */ |
robbiew | d5c2112 | 2002-03-22 16:22:40 +0000 | [diff] [blame] | 83 | if (addr != MAP_FAILED) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 84 | tst_resm(TFAIL | TERRNO, |
| 85 | "mmap() returned invalid value, expected: %p", |
| 86 | MAP_FAILED); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 87 | /* Unmap the mapped memory */ |
| 88 | if (munmap(addr, page_sz) != 0) { |
Garrett Cooper | 11d5104 | 2010-11-22 20:47:29 -0800 | [diff] [blame] | 89 | tst_resm(TBROK, "munmap() failed"); |
| 90 | cleanup(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 91 | } |
| 92 | continue; |
| 93 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 94 | if (TEST_ERRNO == EACCES) { |
Garrett Cooper | 11d5104 | 2010-11-22 20:47:29 -0800 | [diff] [blame] | 95 | tst_resm(TPASS, "mmap failed with EACCES"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 96 | } else { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 97 | tst_resm(TFAIL | TERRNO, |
| 98 | "mmap failed with unexpected errno"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 99 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 100 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 102 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 103 | |
| 104 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 106 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 107 | { |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 108 | char *tst_buff; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 109 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 110 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 111 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 112 | TEST_PAUSE; |
| 113 | |
Cyril Hrubis | ec22fb2 | 2013-06-04 20:37:05 +0200 | [diff] [blame] | 114 | page_sz = getpagesize(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | |
| 116 | /* Allocate space for the test buffer */ |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 117 | if ((tst_buff = calloc(page_sz, sizeof(char))) == NULL) { |
Garrett Cooper | 11d5104 | 2010-11-22 20:47:29 -0800 | [diff] [blame] | 118 | tst_brkm(TFAIL, NULL, "calloc() failed (tst_buff)"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /* Fill the test buffer with the known data */ |
| 122 | memset(tst_buff, 'A', page_sz); |
| 123 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 124 | tst_tmpdir(); |
| 125 | |
| 126 | /* Creat a temporary file used for mapping */ |
| 127 | if ((fildes = open(TEMPFILE, O_WRONLY | O_CREAT, 0666)) < 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 128 | free(tst_buff); |
Garrett Cooper | 11d5104 | 2010-11-22 20:47:29 -0800 | [diff] [blame] | 129 | tst_brkm(TFAIL, cleanup, "opening %s failed", TEMPFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /* Write test buffer contents into temporary file */ |
robbiew | 9136137 | 2003-07-09 17:51:16 +0000 | [diff] [blame] | 133 | if (write(fildes, tst_buff, page_sz) < page_sz) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 134 | free(tst_buff); |
Garrett Cooper | 11d5104 | 2010-11-22 20:47:29 -0800 | [diff] [blame] | 135 | tst_brkm(TFAIL, cleanup, "writing to %s failed", TEMPFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 136 | } |
| 137 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 138 | free(tst_buff); |
| 139 | } |
| 140 | |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 141 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 142 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 143 | close(fildes); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 144 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 145 | } |