blob: 3c82040e29c4433f6e0af1ebf86e2368d4ed1796 [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/exec access
22 * under the following conditions -
23 * - The prot parameter is set to PROT_READ|PROT_EXEC
24 * - The file descriptor is open for read
25 * - The file being mapped has read and execute permission bit set.
26 * - The minimum file permissions should be 0555.
27 *
28 * The call should succeed to map the file creating mapped memory with the
29 * required attributes.
30 *
31 * Expected Result:
32 * mmap() should succeed returning the address of the mapped region,
33 * and the mapped region should contain the contents of the mapped file.
34 *
plars865695b2001-08-27 22:15:12 +000035 * HISTORY
36 * 07/2001 Ported by Wayne Boyer
plars865695b2001-08-27 22:15:12 +000037 */
38
39#include <stdio.h>
40#include <stdlib.h>
41#include <sys/types.h>
42#include <errno.h>
43#include <unistd.h>
44#include <fcntl.h>
45#include <string.h>
46#include <signal.h>
47#include <sys/stat.h>
48#include <sys/mman.h>
49
50#include "test.h"
plars865695b2001-08-27 22:15:12 +000051
52#define TEMPFILE "mmapfile"
53
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020054char *TCID = "mmap04";
55int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000056
Cyril Hrubis5efee332013-06-04 20:14:58 +020057static size_t page_sz;
58static char *addr;
59static char *dummy;
60static int fildes;
61
62static void setup(void);
63static void cleanup(void);
plars865695b2001-08-27 22:15:12 +000064
subrata_modak56207ce2009-03-23 13:35:39 +000065int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000066{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020067 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020068 const char *msg;
subrata_modakbdbaec52009-02-26 12:14:51 +000069
Garrett Cooper11d51042010-11-22 20:47:29 -080070 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
plars865695b2001-08-27 22:15:12 +000071 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000072
plars865695b2001-08-27 22:15:12 +000073 setup();
74
plars865695b2001-08-27 22:15:12 +000075 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -080076
Caspar Zhangd59a6592013-03-07 14:59:12 +080077 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000078
subrata_modak4bb656a2009-02-26 12:02:09 +000079 /*
plars865695b2001-08-27 22:15:12 +000080 * Call mmap to map the temporary file 'TEMPFILE'
subrata_modak56207ce2009-03-23 13:35:39 +000081 * with read and execute access.
plars865695b2001-08-27 22:15:12 +000082 */
subrata_modak56207ce2009-03-23 13:35:39 +000083 errno = 0;
84 addr = mmap(0, page_sz, PROT_READ | PROT_EXEC,
85 MAP_FILE | MAP_SHARED, fildes, 0);
plars865695b2001-08-27 22:15:12 +000086
87 /* Check for the return value of mmap() */
robbiewd5c21122002-03-22 16:22:40 +000088 if (addr == MAP_FAILED) {
Wanlong Gao354ebb42012-12-07 10:10:04 +080089 tst_resm(TFAIL | TERRNO, "mmap of %s failed", TEMPFILE);
plars865695b2001-08-27 22:15:12 +000090 continue;
91 }
92
Cyril Hrubise38b9612014-06-02 17:20:57 +020093 /*
94 * Read the file contents into the dummy
95 * variable.
96 */
97 if (read(fildes, dummy, page_sz) < 0) {
98 tst_brkm(TFAIL, cleanup, "reading %s failed",
99 TEMPFILE);
plars865695b2001-08-27 22:15:12 +0000100 }
Cyril Hrubise38b9612014-06-02 17:20:57 +0200101
102 /*
103 * Check whether the mapped memory region
104 * has the file contents.
105 */
106 if (memcmp(dummy, addr, page_sz)) {
107 tst_resm(TFAIL,
108 "mapped memory region contains invalid "
109 "data");
110 } else {
111 tst_resm(TPASS,
112 "Functionality of mmap() successful");
113 }
114
plars865695b2001-08-27 22:15:12 +0000115 /* Clean up things in case we are looping. */
116 /* Unmap the mapped memory */
117 if (munmap(addr, page_sz) != 0) {
Garrett Cooper11d51042010-11-22 20:47:29 -0800118 tst_brkm(TFAIL, cleanup, "munmapping failed");
plars865695b2001-08-27 22:15:12 +0000119 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800120 }
Cyril Hrubise38b9612014-06-02 17:20:57 +0200121
plars865695b2001-08-27 22:15:12 +0000122 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800123 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800124}
plars865695b2001-08-27 22:15:12 +0000125
Cyril Hrubis5efee332013-06-04 20:14:58 +0200126static void setup(void)
plars865695b2001-08-27 22:15:12 +0000127{
Cyril Hrubis5efee332013-06-04 20:14:58 +0200128 char *tst_buff;
plars865695b2001-08-27 22:15:12 +0000129
plars865695b2001-08-27 22:15:12 +0000130 tst_sig(NOFORK, DEF_HANDLER, cleanup);
131
plars865695b2001-08-27 22:15:12 +0000132 TEST_PAUSE;
133
Cyril Hrubisec22fb22013-06-04 20:37:05 +0200134 page_sz = getpagesize();
plars865695b2001-08-27 22:15:12 +0000135
Cyril Hrubis5efee332013-06-04 20:14:58 +0200136 if ((tst_buff = calloc(page_sz, sizeof(char))) == NULL) {
Garrett Cooper11d51042010-11-22 20:47:29 -0800137 tst_brkm(TFAIL, NULL, "calloc failed (tst_buff)");
plars865695b2001-08-27 22:15:12 +0000138 }
139
140 /* Fill the test buffer with the known data */
141 memset(tst_buff, 'A', page_sz);
142
plars865695b2001-08-27 22:15:12 +0000143 tst_tmpdir();
144
145 /* Creat a temporary file used for mapping */
146 if ((fildes = open(TEMPFILE, O_WRONLY | O_CREAT, 0666)) < 0) {
plars865695b2001-08-27 22:15:12 +0000147 free(tst_buff);
Garrett Cooper11d51042010-11-22 20:47:29 -0800148 tst_brkm(TFAIL, cleanup, "opening %s failed", TEMPFILE);
plars865695b2001-08-27 22:15:12 +0000149 }
150
151 /* Write test buffer contents into temporary file */
robbiew91361372003-07-09 17:51:16 +0000152 if (write(fildes, tst_buff, page_sz) < page_sz) {
plars865695b2001-08-27 22:15:12 +0000153 free(tst_buff);
Garrett Cooper11d51042010-11-22 20:47:29 -0800154 tst_brkm(TFAIL, cleanup, "writing to %s failed", TEMPFILE);
plars865695b2001-08-27 22:15:12 +0000155 }
156
157 /* Free the memory allocated for test buffer */
158 free(tst_buff);
159
160 /* Make sure proper permissions set on file */
Garrett Cooper11d51042010-11-22 20:47:29 -0800161 if (fchmod(fildes, 0555) < 0) {
162 tst_brkm(TFAIL, cleanup, "fchmod of %s failed", TEMPFILE);
plars865695b2001-08-27 22:15:12 +0000163 }
164
165 /* Close the temporary file opened for write */
166 if (close(fildes) < 0) {
Garrett Cooper11d51042010-11-22 20:47:29 -0800167 tst_brkm(TFAIL, cleanup, "closing %s failed", TEMPFILE);
plars865695b2001-08-27 22:15:12 +0000168 }
169
170 /* Allocate and initialize dummy string of system page size bytes */
Cyril Hrubis5efee332013-06-04 20:14:58 +0200171 if ((dummy = calloc(page_sz, sizeof(char))) == NULL) {
Garrett Cooper11d51042010-11-22 20:47:29 -0800172 tst_brkm(TFAIL, cleanup, "calloc failed (dummy)");
plars865695b2001-08-27 22:15:12 +0000173 }
174
175 /* Open the temporary file again for reading */
176 if ((fildes = open(TEMPFILE, O_RDONLY)) < 0) {
Garrett Cooper11d51042010-11-22 20:47:29 -0800177 tst_brkm(TFAIL, cleanup,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800178 "opening %s read-only failed", TEMPFILE);
plars865695b2001-08-27 22:15:12 +0000179 }
180}
181
Cyril Hrubis5efee332013-06-04 20:14:58 +0200182static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000183{
subrata_modak56207ce2009-03-23 13:35:39 +0000184 close(fildes);
Cyril Hrubis5efee332013-06-04 20:14:58 +0200185 free(dummy);
plars865695b2001-08-27 22:15:12 +0000186 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700187}