1 /*
2 * Copyright (C) 2000 Juan Quintela <quintela@fi.udc.es>
3 * Aaron Laffin <alaffin@sgi.com>
4 *
5 * 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.
9 *
10 * 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.
14 *
15 * 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.
18 *
19 * mmap001.c - Tests mmapping a big file and writing it once
20 */
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"
32
33 char *TCID = "mmap001";
34 int TST_TOTAL = 5;
35 static char *filename = NULL;
36 static int m_opt = 0;
37 static char *m_copt;
38
cleanup(void)39 static void cleanup(void)
40 {
41 free(filename);
42
43 tst_rmdir();
44 }
45
setup(void)46 static void setup(void)
47 {
48 char buf[1024];
49 /*
50 * setup a default signal hander and a
51 * temporary working directory.
52 */
53 tst_sig(FORK, DEF_HANDLER, cleanup);
54
55 TEST_PAUSE;
56
57 tst_tmpdir();
58
59 snprintf(buf, 1024, "testfile.%d", getpid());
60
61 if ((filename = strdup(buf)) == NULL) {
62 tst_brkm(TBROK | TERRNO, cleanup, "strdup failed");
63 }
64
65 }
66
help(void)67 static void help(void)
68 {
69 printf(" -m x size of mmap in pages (default 1000)\n");
70 }
71
72 /*
73 * add the -m option whose parameter is the
74 * pages that should be mapped.
75 */
76 option_t options[] = {
77 {"m:", &m_opt, &m_copt},
78 {NULL, NULL, NULL}
79 };
80
main(int argc,char * argv[])81 int main(int argc, char *argv[])
82 {
83 char *array;
84 int lc;
85 unsigned int i;
86 int fd;
87 unsigned int pages, memsize;
88
89 tst_parse_opts(argc, argv, options, help);
90
91 if (m_opt) {
92 memsize = pages = atoi(m_copt);
93
94 if (memsize < 1) {
95 tst_brkm(TBROK, cleanup, "Invalid arg for -m: %s",
96 m_copt);
97 }
98
99 memsize *= getpagesize(); /* N PAGES */
100
101 } else {
102 /*
103 * default size 1000 pages;
104 */
105 memsize = pages = 1000;
106 memsize *= getpagesize();
107 }
108
109 tst_resm(TINFO, "mmap()ing file of %u pages or %u bytes", pages,
110 memsize);
111
112 setup();
113
114 for (lc = 0; TEST_LOOPING(lc); lc++) {
115 tst_count = 0;
116
117 fd = open(filename, O_RDWR | O_CREAT, 0666);
118 if ((fd == -1))
119 tst_brkm(TBROK | TERRNO, cleanup,
120 "opening %s failed", filename);
121
122 if (lseek(fd, memsize, SEEK_SET) != memsize) {
123 TEST_ERRNO = errno;
124 close(fd);
125 tst_brkm(TBROK | TTERRNO, cleanup, "lseek failed");
126 }
127
128 if (write(fd, "\0", 1) != 1) {
129 TEST_ERRNO = errno;
130 close(fd);
131 tst_brkm(TBROK | TTERRNO, cleanup,
132 "writing to %s failed", filename);
133 }
134
135 array = mmap(0, memsize, PROT_WRITE, MAP_SHARED, fd, 0);
136 if (array == MAP_FAILED) {
137 TEST_ERRNO = errno;
138 close(fd);
139 tst_brkm(TBROK | TTERRNO, cleanup,
140 "mmapping %s failed", filename);
141 } else {
142 tst_resm(TPASS, "mmap() completed successfully.");
143 }
144
145 tst_resm(TINFO, "touching mmaped memory");
146
147 for (i = 0; i < memsize; i++) {
148 array[i] = (char)i;
149 }
150
151 /*
152 * seems that if the map area was bad, we'd get SEGV,
153 * hence we can indicate a PASS.
154 */
155 tst_resm(TPASS,
156 "we're still here, mmaped area must be good");
157
158 TEST(msync(array, memsize, MS_SYNC));
159
160 if (TEST_RETURN == -1) {
161 tst_resm(TFAIL | TTERRNO,
162 "synchronizing mmapped page failed");
163 } else {
164 tst_resm(TPASS,
165 "synchronizing mmapped page passed");
166 }
167
168 TEST(munmap(array, memsize));
169
170 if (TEST_RETURN == -1) {
171 tst_resm(TFAIL | TTERRNO,
172 "munmapping %s failed", filename);
173 } else {
174 tst_resm(TPASS, "munmapping %s successful", filename);
175 }
176
177 close(fd);
178 unlink(filename);
179
180 }
181 cleanup();
182 tst_exit();
183 }
184