blob: b28e63aa87715a6b0a4221ad0f144740a81e63e1 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
Cyril Hrubis5efee332013-06-04 20:14:58 +02002 * Copyright (c) International Business Machines Corp., 2001
plars865695b2001-08-27 22:15:12 +00003 *
Cyril Hrubis5efee332013-06-04 20:14:58 +02004 * 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.
plars865695b2001-08-27 22:15:12 +00008 *
Cyril Hrubis5efee332013-06-04 20:14:58 +02009 * 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.
plars865695b2001-08-27 22:15:12 +000013 *
Cyril Hrubis5efee332013-06-04 20:14:58 +020014 * 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
plars865695b2001-08-27 22:15:12 +000017 */
18
19/*
plars865695b2001-08-27 22:15:12 +000020 * 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 *
plars865695b2001-08-27 22:15:12 +000032 * HISTORY
33 * 07/2001 Ported by Wayne Boyer
plars865695b2001-08-27 22:15:12 +000034 */
plars865695b2001-08-27 22:15:12 +000035#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"
plars865695b2001-08-27 22:15:12 +000047
48#define TEMPFILE "mmapfile"
49
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020050char *TCID = "mmap07";
51int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000052
Cyril Hrubis5efee332013-06-04 20:14:58 +020053static size_t page_sz;
54static char *addr;
55static int fildes;
56
57static void setup(void);
58static void cleanup(void);
plars865695b2001-08-27 22:15:12 +000059
subrata_modak56207ce2009-03-23 13:35:39 +000060int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000061{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020062 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020063 const char *msg;
subrata_modakbdbaec52009-02-26 12:14:51 +000064
Garrett Cooper11d51042010-11-22 20:47:29 -080065 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
plars865695b2001-08-27 22:15:12 +000066 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000067
plars865695b2001-08-27 22:15:12 +000068 setup();
69
plars865695b2001-08-27 22:15:12 +000070 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -080071
Caspar Zhangd59a6592013-03-07 14:59:12 +080072 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000073
subrata_modak4bb656a2009-02-26 12:02:09 +000074 /*
plars865695b2001-08-27 22:15:12 +000075 * Call mmap to map the temporary file 'TEMPFILE'
subrata_modak56207ce2009-03-23 13:35:39 +000076 * with write access.
plars865695b2001-08-27 22:15:12 +000077 */
robbiewd5c21122002-03-22 16:22:40 +000078 errno = 0;
79 addr = mmap(0, page_sz, PROT_WRITE,
subrata_modak56207ce2009-03-23 13:35:39 +000080 MAP_FILE | MAP_PRIVATE, fildes, 0);
robbiewd5c21122002-03-22 16:22:40 +000081 TEST_ERRNO = errno;
plars865695b2001-08-27 22:15:12 +000082
83 /* Check for the return value of mmap() */
robbiewd5c21122002-03-22 16:22:40 +000084 if (addr != MAP_FAILED) {
Wanlong Gao354ebb42012-12-07 10:10:04 +080085 tst_resm(TFAIL | TERRNO,
86 "mmap() returned invalid value, expected: %p",
87 MAP_FAILED);
plars865695b2001-08-27 22:15:12 +000088 /* Unmap the mapped memory */
89 if (munmap(addr, page_sz) != 0) {
Garrett Cooper11d51042010-11-22 20:47:29 -080090 tst_resm(TBROK, "munmap() failed");
91 cleanup();
plars865695b2001-08-27 22:15:12 +000092 }
93 continue;
94 }
plars865695b2001-08-27 22:15:12 +000095 if (TEST_ERRNO == EACCES) {
Garrett Cooper11d51042010-11-22 20:47:29 -080096 tst_resm(TPASS, "mmap failed with EACCES");
plars865695b2001-08-27 22:15:12 +000097 } else {
Wanlong Gao354ebb42012-12-07 10:10:04 +080098 tst_resm(TFAIL | TERRNO,
99 "mmap failed with unexpected errno");
plars865695b2001-08-27 22:15:12 +0000100 }
101
Garrett Cooper2c282152010-12-16 00:55:50 -0800102 }
plars865695b2001-08-27 22:15:12 +0000103 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800104 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800105
106}
plars865695b2001-08-27 22:15:12 +0000107
Cyril Hrubis5efee332013-06-04 20:14:58 +0200108static void setup(void)
plars865695b2001-08-27 22:15:12 +0000109{
Cyril Hrubis5efee332013-06-04 20:14:58 +0200110 char *tst_buff;
plars865695b2001-08-27 22:15:12 +0000111
plars865695b2001-08-27 22:15:12 +0000112 tst_sig(NOFORK, DEF_HANDLER, cleanup);
113
plars865695b2001-08-27 22:15:12 +0000114 TEST_PAUSE;
115
Cyril Hrubisec22fb22013-06-04 20:37:05 +0200116 page_sz = getpagesize();
plars865695b2001-08-27 22:15:12 +0000117
118 /* Allocate space for the test buffer */
Cyril Hrubis5efee332013-06-04 20:14:58 +0200119 if ((tst_buff = calloc(page_sz, sizeof(char))) == NULL) {
plars865695b2001-08-27 22:15:12 +0000120 tst_brkm(TFAIL, NULL,
121 "calloc() failed to allocate space for tst_buff");
plars865695b2001-08-27 22:15:12 +0000122 }
123
124 /* Fill the test buffer with the known data */
125 memset(tst_buff, 'A', page_sz);
126
plars865695b2001-08-27 22:15:12 +0000127 tst_tmpdir();
128
129 /* Creat a temporary file used for mapping */
130 if ((fildes = open(TEMPFILE, O_WRONLY | O_CREAT, 0666)) < 0) {
plars865695b2001-08-27 22:15:12 +0000131 free(tst_buff);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800132 tst_brkm(TFAIL, cleanup, "open() on %s failed", TEMPFILE);
plars865695b2001-08-27 22:15:12 +0000133 }
134
135 /* Write test buffer contents into temporary file */
robbiew91361372003-07-09 17:51:16 +0000136 if (write(fildes, tst_buff, page_sz) < page_sz) {
plars865695b2001-08-27 22:15:12 +0000137 free(tst_buff);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800138 tst_brkm(TFAIL, cleanup, "writing to %s failed", TEMPFILE);
plars865695b2001-08-27 22:15:12 +0000139 }
140
plars865695b2001-08-27 22:15:12 +0000141 free(tst_buff);
142}
143
Cyril Hrubis5efee332013-06-04 20:14:58 +0200144static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000145{
subrata_modak56207ce2009-03-23 13:35:39 +0000146 close(fildes);
plars865695b2001-08-27 22:15:12 +0000147 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700148}