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