blob: 534c60a8ae3db362c0d56eab2db133552d90c4a5 [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_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 *
plars865695b2001-08-27 22:15:12 +000031 * HISTORY
32 * 07/2001 Ported by Wayne Boyer
plars865695b2001-08-27 22:15:12 +000033 */
plars865695b2001-08-27 22:15:12 +000034#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"
plars865695b2001-08-27 22:15:12 +000046
47#define TEMPFILE "mmapfile"
48
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020049char *TCID = "mmap06";
50int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000051
Cyril Hrubis5efee332013-06-04 20:14:58 +020052static size_t page_sz;
53static char *addr;
54static int fildes;
55
56static void setup(void);
57static void cleanup(void);
plars865695b2001-08-27 22:15:12 +000058
subrata_modak56207ce2009-03-23 13:35:39 +000059int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000060{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020061 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020062 const char *msg;
subrata_modakbdbaec52009-02-26 12:14:51 +000063
Garrett Cooper11d51042010-11-22 20:47:29 -080064 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
plars865695b2001-08-27 22:15:12 +000065 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000066
plars865695b2001-08-27 22:15:12 +000067 setup();
68
plars865695b2001-08-27 22:15:12 +000069 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -080070
Caspar Zhangd59a6592013-03-07 14:59:12 +080071 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000072
subrata_modak4bb656a2009-02-26 12:02:09 +000073 /*
plars865695b2001-08-27 22:15:12 +000074 * Call mmap to map the temporary file 'TEMPFILE'
subrata_modak56207ce2009-03-23 13:35:39 +000075 * with read access.
plars865695b2001-08-27 22:15:12 +000076 */
robbiewd5c21122002-03-22 16:22:40 +000077 errno = 0;
78 addr = mmap(0, page_sz, PROT_READ,
subrata_modak56207ce2009-03-23 13:35:39 +000079 MAP_FILE | MAP_SHARED, fildes, 0);
robbiewd5c21122002-03-22 16:22:40 +000080 TEST_ERRNO = errno;
plars865695b2001-08-27 22:15:12 +000081
82 /* Check for the return value of mmap() */
robbiewd5c21122002-03-22 16:22:40 +000083 if (addr != MAP_FAILED) {
Wanlong Gao354ebb42012-12-07 10:10:04 +080084 tst_resm(TFAIL | TERRNO,
85 "mmap() returned invalid value, expected: %p",
86 MAP_FAILED);
plars865695b2001-08-27 22:15:12 +000087 /* Unmap the mapped memory */
88 if (munmap(addr, page_sz) != 0) {
Garrett Cooper11d51042010-11-22 20:47:29 -080089 tst_resm(TBROK, "munmap() failed");
90 cleanup();
plars865695b2001-08-27 22:15:12 +000091 }
92 continue;
93 }
plars865695b2001-08-27 22:15:12 +000094 if (TEST_ERRNO == EACCES) {
Garrett Cooper11d51042010-11-22 20:47:29 -080095 tst_resm(TPASS, "mmap failed with EACCES");
plars865695b2001-08-27 22:15:12 +000096 } else {
Wanlong Gao354ebb42012-12-07 10:10:04 +080097 tst_resm(TFAIL | TERRNO,
98 "mmap failed with unexpected errno");
plars865695b2001-08-27 22:15:12 +000099 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800100 }
plars865695b2001-08-27 22:15:12 +0000101 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800102 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800103
104}
plars865695b2001-08-27 22:15:12 +0000105
Cyril Hrubis5efee332013-06-04 20:14:58 +0200106static void setup(void)
plars865695b2001-08-27 22:15:12 +0000107{
Cyril Hrubis5efee332013-06-04 20:14:58 +0200108 char *tst_buff;
plars865695b2001-08-27 22:15:12 +0000109
plars865695b2001-08-27 22:15:12 +0000110 tst_sig(NOFORK, DEF_HANDLER, cleanup);
111
plars865695b2001-08-27 22:15:12 +0000112 TEST_PAUSE;
113
Cyril Hrubisec22fb22013-06-04 20:37:05 +0200114 page_sz = getpagesize();
plars865695b2001-08-27 22:15:12 +0000115
116 /* Allocate space for the test buffer */
Cyril Hrubis5efee332013-06-04 20:14:58 +0200117 if ((tst_buff = calloc(page_sz, sizeof(char))) == NULL) {
Garrett Cooper11d51042010-11-22 20:47:29 -0800118 tst_brkm(TFAIL, NULL, "calloc() failed (tst_buff)");
plars865695b2001-08-27 22:15:12 +0000119 }
120
121 /* Fill the test buffer with the known data */
122 memset(tst_buff, 'A', page_sz);
123
plars865695b2001-08-27 22:15:12 +0000124 tst_tmpdir();
125
126 /* Creat a temporary file used for mapping */
127 if ((fildes = open(TEMPFILE, O_WRONLY | O_CREAT, 0666)) < 0) {
plars865695b2001-08-27 22:15:12 +0000128 free(tst_buff);
Garrett Cooper11d51042010-11-22 20:47:29 -0800129 tst_brkm(TFAIL, cleanup, "opening %s failed", TEMPFILE);
plars865695b2001-08-27 22:15:12 +0000130 }
131
132 /* Write test buffer contents into temporary file */
robbiew91361372003-07-09 17:51:16 +0000133 if (write(fildes, tst_buff, page_sz) < page_sz) {
plars865695b2001-08-27 22:15:12 +0000134 free(tst_buff);
Garrett Cooper11d51042010-11-22 20:47:29 -0800135 tst_brkm(TFAIL, cleanup, "writing to %s failed", TEMPFILE);
plars865695b2001-08-27 22:15:12 +0000136 }
137
plars865695b2001-08-27 22:15:12 +0000138 free(tst_buff);
139}
140
Cyril Hrubis5efee332013-06-04 20:14:58 +0200141static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000142{
subrata_modak56207ce2009-03-23 13:35:39 +0000143 close(fildes);
plars865695b2001-08-27 22:15:12 +0000144 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700145}