blob: df6e84fda9547d38ee18f3d1a20648071617d33f [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() with prot parameter set to PROT_READ and with the file
22 * descriptor being open for read, to map a file creating mapped memory
23 * with read access. The minimum file permissions should be 0444.
24 *
25 * The call should succeed to create the mapped region with required
26 * attributes.
27 *
28 * Expected Result:
29 * mmap() should succeed returning the address of the mapped region,
30 * the mapped region should contain the contents of the mapped file.
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 = "mmap02";
51int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000052
Cyril Hrubis5efee332013-06-04 20:14:58 +020053static char *addr;
54static char *dummy;
55static size_t page_sz;
56static int fildes;
57
58static void setup(void);
59static void cleanup(void);
plars865695b2001-08-27 22:15:12 +000060
subrata_modak56207ce2009-03-23 13:35:39 +000061int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000062{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020063 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020064 const char *msg;
subrata_modakbdbaec52009-02-26 12:14:51 +000065
Garrett Cooper53740502010-12-16 00:04:01 -080066 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
plars865695b2001-08-27 22:15:12 +000067 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000068
plars865695b2001-08-27 22:15:12 +000069 setup();
70
plars865695b2001-08-27 22:15:12 +000071 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -080072
Caspar Zhangd59a6592013-03-07 14:59:12 +080073 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000074
subrata_modak4bb656a2009-02-26 12:02:09 +000075 /*
plars865695b2001-08-27 22:15:12 +000076 * Call mmap to map the temporary file 'TEMPFILE'
subrata_modak56207ce2009-03-23 13:35:39 +000077 * with read access.
plars865695b2001-08-27 22:15:12 +000078 */
subrata_modak56207ce2009-03-23 13:35:39 +000079 errno = 0;
80 addr = mmap(0, page_sz, PROT_READ,
81 MAP_FILE | MAP_SHARED, fildes, 0);
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, "mmap of %s failed", TEMPFILE);
plars865695b2001-08-27 22:15:12 +000086 continue;
87 }
Cyril Hrubis5efee332013-06-04 20:14:58 +020088
Cyril Hrubise38b9612014-06-02 17:20:57 +020089 /*
90 * Read the file contents into the dummy
91 * string.
92 */
93 if (read(fildes, dummy, page_sz) < 0) {
94 tst_brkm(TFAIL | TERRNO, cleanup,
95 "reading %s failed", TEMPFILE);
96 }
plars865695b2001-08-27 22:15:12 +000097
Cyril Hrubise38b9612014-06-02 17:20:57 +020098 /*
99 * Check whether mapped memory region has
100 * the file contents.
101 */
102 if (memcmp(dummy, addr, page_sz)) {
103 tst_resm(TFAIL, "mapped memory area contains "
104 "invalid data");
plars865695b2001-08-27 22:15:12 +0000105 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200106 tst_resm(TPASS,
107 "Functionality of mmap() successful");
plars865695b2001-08-27 22:15:12 +0000108 }
109
110 /* Clean up things in case we are looping */
111 /* Unmap the mapped memory */
112 if (munmap(addr, page_sz) != 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800113 tst_brkm(TFAIL | TERRNO, cleanup, "munmapping failed");
plars865695b2001-08-27 22:15:12 +0000114 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800115 }
plars865695b2001-08-27 22:15:12 +0000116
plars865695b2001-08-27 22:15:12 +0000117 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800118 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800119}
plars865695b2001-08-27 22:15:12 +0000120
Cyril Hrubis5efee332013-06-04 20:14:58 +0200121static void setup(void)
plars865695b2001-08-27 22:15:12 +0000122{
Cyril Hrubis5efee332013-06-04 20:14:58 +0200123 char *tst_buff;
plars865695b2001-08-27 22:15:12 +0000124
plars865695b2001-08-27 22:15:12 +0000125 tst_sig(FORK, DEF_HANDLER, cleanup);
126
plars865695b2001-08-27 22:15:12 +0000127 TEST_PAUSE;
128
Cyril Hrubisec22fb22013-06-04 20:37:05 +0200129 page_sz = getpagesize();
plars865695b2001-08-27 22:15:12 +0000130
131 /* Allocate space for the test buffer */
Cyril Hrubis5efee332013-06-04 20:14:58 +0200132 if ((tst_buff = calloc(page_sz, sizeof(char))) == NULL) {
Garrett Cooper11d51042010-11-22 20:47:29 -0800133 tst_brkm(TFAIL, NULL, "calloc failed (tst_buff)");
plars865695b2001-08-27 22:15:12 +0000134 }
135
136 /* Fill the test buffer with the known data */
137 memset(tst_buff, 'A', page_sz);
138
plars865695b2001-08-27 22:15:12 +0000139 tst_tmpdir();
140
141 /* Creat a temporary file used for mapping */
142 if ((fildes = open(TEMPFILE, O_RDWR | O_CREAT, 0666)) < 0) {
plars865695b2001-08-27 22:15:12 +0000143 free(tst_buff);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800144 tst_brkm(TFAIL | TERRNO, cleanup, "opening %s failed",
145 TEMPFILE);
plars865695b2001-08-27 22:15:12 +0000146 }
147
148 /* Write test buffer contents into temporary file */
robbiew91361372003-07-09 17:51:16 +0000149 if (write(fildes, tst_buff, page_sz) < page_sz) {
plars865695b2001-08-27 22:15:12 +0000150 free(tst_buff);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800151 tst_brkm(TFAIL | TERRNO, cleanup,
152 "writing to %s failed", TEMPFILE);
plars865695b2001-08-27 22:15:12 +0000153 cleanup();
154 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000155
plars865695b2001-08-27 22:15:12 +0000156 /* Free the memory allocated for test buffer */
157 free(tst_buff);
158
Garrett Cooper11d51042010-11-22 20:47:29 -0800159 /* Change Mode permissions on Temporary file */
160 if (fchmod(fildes, 0444) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800161 tst_brkm(TFAIL | TERRNO, cleanup, "fchmod(%s, 0444) failed",
162 TEMPFILE);
plars865695b2001-08-27 22:15:12 +0000163 }
164
Garrett Cooper11d51042010-11-22 20:47:29 -0800165 /* Close the temporary file */
166 if (close(fildes) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800167 tst_brkm(TFAIL | TERRNO, cleanup, "closing %s failed",
168 TEMPFILE);
plars865695b2001-08-27 22:15:12 +0000169 }
170
171 /* Open the temporary file again, - Readonly mode */
172 if ((fildes = open(TEMPFILE, O_RDONLY)) < 0) {
Garrett Cooper11d51042010-11-22 20:47:29 -0800173 tst_brkm(TFAIL, cleanup, "reopening %s readonly failed",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800174 TEMPFILE);
plars865695b2001-08-27 22:15:12 +0000175 }
176
plars865695b2001-08-27 22:15:12 +0000177 /* Allocate and initialize dummy string of system page size bytes */
Cyril Hrubis5efee332013-06-04 20:14:58 +0200178 if ((dummy = calloc(page_sz, sizeof(char))) == NULL) {
Garrett Cooper11d51042010-11-22 20:47:29 -0800179 tst_brkm(TFAIL, cleanup, "calloc failed (dummy)");
plars865695b2001-08-27 22:15:12 +0000180 }
181
182}
183
Cyril Hrubis5efee332013-06-04 20:14:58 +0200184static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000185{
subrata_modak56207ce2009-03-23 13:35:39 +0000186 close(fildes);
Cyril Hrubis5efee332013-06-04 20:14:58 +0200187 free(dummy);
plars865695b2001-08-27 22:15:12 +0000188 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700189}