blob: a915351ad24f05eb0c56e8e9afd30e69730a587f [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
plars865695b2001-08-27 22:15:12 +00002 * Copyright (C) 2000 Juan Quintela <quintela@fi.udc.es>
3 * Aaron Laffin <alaffin@sgi.com>
subrata_modak4bb656a2009-02-26 12:02:09 +00004 *
plars865695b2001-08-27 22:15:12 +00005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
subrata_modak4bb656a2009-02-26 12:02:09 +00009 *
plars865695b2001-08-27 22:15:12 +000010 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
subrata_modak4bb656a2009-02-26 12:02:09 +000014 *
plars865695b2001-08-27 22:15:12 +000015 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Cyril Hrubis5efee332013-06-04 20:14:58 +020018 *
19 * mmap001.c - Tests mmapping a big file and writing it once
plars865695b2001-08-27 22:15:12 +000020 */
21#include <sys/types.h>
22#include <sys/stat.h>
23#include <fcntl.h>
24#include <sys/mman.h>
25#include <stdlib.h>
26#include <stdio.h>
27#include <unistd.h>
28#include <errno.h>
29#include <string.h>
30
31#include "test.h"
plars865695b2001-08-27 22:15:12 +000032
subrata_modak56207ce2009-03-23 13:35:39 +000033char *TCID = "mmap001";
34int TST_TOTAL = 5;
subrata_modak56207ce2009-03-23 13:35:39 +000035static char *filename = NULL;
plars865695b2001-08-27 22:15:12 +000036static int m_opt = 0;
37static char *m_copt;
38
Cyril Hrubis5efee332013-06-04 20:14:58 +020039static void cleanup(void)
plars865695b2001-08-27 22:15:12 +000040{
Cyril Hrubis5efee332013-06-04 20:14:58 +020041 free(filename);
plars865695b2001-08-27 22:15:12 +000042
subrata_modak56207ce2009-03-23 13:35:39 +000043 tst_rmdir();
plars865695b2001-08-27 22:15:12 +000044}
45
Cyril Hrubis5efee332013-06-04 20:14:58 +020046static void setup(void)
plars865695b2001-08-27 22:15:12 +000047{
subrata_modak56207ce2009-03-23 13:35:39 +000048 char buf[1024];
49 /*
50 * setup a default signal hander and a
51 * temporary working directory.
52 */
53 tst_sig(FORK, DEF_HANDLER, cleanup);
plars865695b2001-08-27 22:15:12 +000054
subrata_modak56207ce2009-03-23 13:35:39 +000055 TEST_PAUSE;
plars865695b2001-08-27 22:15:12 +000056
subrata_modak56207ce2009-03-23 13:35:39 +000057 tst_tmpdir();
plars865695b2001-08-27 22:15:12 +000058
subrata_modak56207ce2009-03-23 13:35:39 +000059 snprintf(buf, 1024, "testfile.%d", getpid());
plars865695b2001-08-27 22:15:12 +000060
Garrett Cooper11d51042010-11-22 20:47:29 -080061 if ((filename = strdup(buf)) == NULL) {
Wanlong Gao354ebb42012-12-07 10:10:04 +080062 tst_brkm(TBROK | TERRNO, cleanup, "strdup failed");
Garrett Cooper11d51042010-11-22 20:47:29 -080063 }
Garrett Cooper2c282152010-12-16 00:55:50 -080064
plars865695b2001-08-27 22:15:12 +000065}
66
Cyril Hrubis5efee332013-06-04 20:14:58 +020067static void help(void)
plars865695b2001-08-27 22:15:12 +000068{
subrata_modak56207ce2009-03-23 13:35:39 +000069 printf(" -m x size of mmap in pages (default 1000)\n");
plars865695b2001-08-27 22:15:12 +000070}
71
72/*
73 * add the -m option whose parameter is the
74 * pages that should be mapped.
75 */
subrata_modak56207ce2009-03-23 13:35:39 +000076option_t options[] = {
77 {"m:", &m_opt, &m_copt},
78 {NULL, NULL, NULL}
plars865695b2001-08-27 22:15:12 +000079};
80
subrata_modak56207ce2009-03-23 13:35:39 +000081int main(int argc, char *argv[])
plars865695b2001-08-27 22:15:12 +000082{
Cyril Hrubis74225622014-06-02 17:54:38 +020083 char *array;
84 const char *msg;
subrata_modak56207ce2009-03-23 13:35:39 +000085 int i, lc;
86 int fd;
87 unsigned int pages, memsize;
plars865695b2001-08-27 22:15:12 +000088
Garrett Cooper45e285d2010-11-22 12:19:25 -080089 if ((msg = parse_opts(argc, argv, options, help)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080090 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000091
subrata_modak56207ce2009-03-23 13:35:39 +000092 if (m_opt) {
93 memsize = pages = atoi(m_copt);
plars865695b2001-08-27 22:15:12 +000094
subrata_modak56207ce2009-03-23 13:35:39 +000095 if (memsize < 1) {
96 tst_brkm(TBROK, cleanup, "Invalid arg for -m: %s",
Wanlong Gao354ebb42012-12-07 10:10:04 +080097 m_copt);
subrata_modak56207ce2009-03-23 13:35:39 +000098 }
plars865695b2001-08-27 22:15:12 +000099
subrata_modak56207ce2009-03-23 13:35:39 +0000100 memsize *= getpagesize(); /* N PAGES */
plars865695b2001-08-27 22:15:12 +0000101
subrata_modak56207ce2009-03-23 13:35:39 +0000102 } else {
103 /*
104 * default size 1000 pages;
105 */
106 memsize = pages = 1000;
107 memsize *= getpagesize();
108 }
plars865695b2001-08-27 22:15:12 +0000109
subrata_modak56207ce2009-03-23 13:35:39 +0000110 tst_resm(TINFO, "mmap()ing file of %u pages or %u bytes", pages,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800111 memsize);
plars865695b2001-08-27 22:15:12 +0000112
subrata_modak56207ce2009-03-23 13:35:39 +0000113 setup();
plars865695b2001-08-27 22:15:12 +0000114
subrata_modak56207ce2009-03-23 13:35:39 +0000115 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800116 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000117
Wanlong Gao354ebb42012-12-07 10:10:04 +0800118 fd = open(filename, O_RDWR | O_CREAT, 0666);
subrata_modak56207ce2009-03-23 13:35:39 +0000119 if ((fd == -1))
Wanlong Gao354ebb42012-12-07 10:10:04 +0800120 tst_brkm(TBROK | TERRNO, cleanup,
121 "opening %s failed", filename);
plars865695b2001-08-27 22:15:12 +0000122
subrata_modak56207ce2009-03-23 13:35:39 +0000123 if (lseek(fd, memsize, SEEK_SET) != memsize) {
Garrett Cooper11d51042010-11-22 20:47:29 -0800124 TEST_ERRNO = errno;
subrata_modak56207ce2009-03-23 13:35:39 +0000125 close(fd);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800126 tst_brkm(TBROK | TTERRNO, cleanup, "lseek failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000127 }
subrata_modak4bb656a2009-02-26 12:02:09 +0000128
subrata_modak56207ce2009-03-23 13:35:39 +0000129 if (write(fd, "\0", 1) != 1) {
Garrett Cooper11d51042010-11-22 20:47:29 -0800130 TEST_ERRNO = errno;
subrata_modak56207ce2009-03-23 13:35:39 +0000131 close(fd);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800132 tst_brkm(TBROK | TTERRNO, cleanup,
133 "writing to %s failed", filename);
subrata_modak56207ce2009-03-23 13:35:39 +0000134 }
plars865695b2001-08-27 22:15:12 +0000135
subrata_modak56207ce2009-03-23 13:35:39 +0000136 array = mmap(0, memsize, PROT_WRITE, MAP_SHARED, fd, 0);
Cyril Hrubis5efee332013-06-04 20:14:58 +0200137 if (array == MAP_FAILED) {
Garrett Cooper11d51042010-11-22 20:47:29 -0800138 TEST_ERRNO = errno;
139 close(fd);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800140 tst_brkm(TBROK | TTERRNO, cleanup,
141 "mmapping %s failed", filename);
subrata_modak56207ce2009-03-23 13:35:39 +0000142 } else {
143 tst_resm(TPASS, "mmap() completed successfully.");
144 }
plars865695b2001-08-27 22:15:12 +0000145
Cyril Hrubise38b9612014-06-02 17:20:57 +0200146 tst_resm(TINFO, "touching mmaped memory");
Garrett Cooper11d51042010-11-22 20:47:29 -0800147
Cyril Hrubise38b9612014-06-02 17:20:57 +0200148 for (i = 0; i < memsize; i++) {
149 array[i] = (char)i;
subrata_modak56207ce2009-03-23 13:35:39 +0000150 }
Cyril Hrubise38b9612014-06-02 17:20:57 +0200151
152 /*
153 * seems that if the map area was bad, we'd get SEGV,
154 * hence we can indicate a PASS.
155 */
156 tst_resm(TPASS,
157 "we're still here, mmaped area must be good");
158
159 TEST(msync(array, memsize, MS_SYNC));
160
161 if (TEST_RETURN == -1) {
162 tst_resm(TFAIL | TTERRNO,
163 "synchronizing mmapped page failed");
164 } else {
165 tst_resm(TPASS,
166 "synchronizing mmapped page passed");
167 }
168
subrata_modak56207ce2009-03-23 13:35:39 +0000169 TEST(munmap(array, memsize));
plars865695b2001-08-27 22:15:12 +0000170
subrata_modak56207ce2009-03-23 13:35:39 +0000171 if (TEST_RETURN == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800172 tst_resm(TFAIL | TTERRNO,
173 "munmapping %s failed", filename);
subrata_modak56207ce2009-03-23 13:35:39 +0000174 } else {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800175 tst_resm(TPASS, "munmapping %s successful", filename);
subrata_modak56207ce2009-03-23 13:35:39 +0000176 }
plars865695b2001-08-27 22:15:12 +0000177
subrata_modak56207ce2009-03-23 13:35:39 +0000178 close(fd);
179 unlink(filename);
Garrett Cooper11d51042010-11-22 20:47:29 -0800180
subrata_modak56207ce2009-03-23 13:35:39 +0000181 }
182 cleanup();
Garrett Cooper11d51042010-11-22 20:47:29 -0800183 tst_exit();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700184}