blob: 4d080d17a9aeea216cb215d775e2ae050df530bc [file] [log] [blame]
robbiew84457092003-01-10 17:48:12 +00001/* IBM Corporation */
2/* 01/02/2003 Port to LTP avenkat@us.ibm.com*/
3/* 06/30/2001 Port to Linux nsharoff@us.ibm.com */
4/*
5 * Copyright (c) International Business Machines Corp., 2003
6 *
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
16 * the GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
robbiew84457092003-01-10 17:48:12 +000021 */
22
23/* mfile_insque:
24 * This test mmaps a portion of a file, and then mmaps the next
25 * portion of the file in front of the virtual space containing the
26 * original mmap. It then mmaps the preceding portion of the file behind
27 * the original mmap. None of the mmaps can be concatenated.
28 */
29#include <sys/types.h>
30#include <sys/mman.h>
31#include <unistd.h>
32#include <fcntl.h>
33#include <signal.h>
34#include <stdio.h>
robbiewc0325f72003-02-10 18:46:26 +000035#include <errno.h>
robbiew84457092003-01-10 17:48:12 +000036/* #include <sys/pte.h> */
37
38/***** LTP Port *****/
39#include "test.h"
robbiew84457092003-01-10 17:48:12 +000040/***** ** ** *****/
41
42#ifndef MMU_NARROWPTEPG
43#define MMU_NARROWPTEPG 1024
44#endif
45
46/***** LTP Port *****/
47#define FAILED 0
48#define PASSED 1
49
50int local_flag = PASSED;
Wanlong Gao354ebb42012-12-07 10:10:04 +080051char *TCID = "mmapstress05"; //mfile_insque
robbiew84457092003-01-10 17:48:12 +000052FILE *temp;
53int TST_TOTAL = 1;
robbiew84457092003-01-10 17:48:12 +000054
55int anyfail();
56void ok_exit();
57/***** ** ** *****/
58
59#define ERROR(M) (void)fprintf(stderr, "%s: errno = %d; " M "\n", \
60 progname, errno);
61#define CLEAN (void)close(fd); \
Subrata Modak76a720a2010-07-03 21:08:18 +053062 if (munmap(mmapaddr+pagesize, pagesize) == -1) { \
63 ERROR("munmap failed"); \
64 } \
65 if (munmap(mmapaddr, pagesize) == -1) { \
66 ERROR("munmap failed"); \
67 } \
68 if (munmap(mmapaddr+2*pagesize, pagesize) == -1) { \
69 ERROR("munmap failed"); \
70 } \
robbiew84457092003-01-10 17:48:12 +000071 if (unlink(tmpname)) { \
72 ERROR("couldn't clean up temp file"); \
73 }
74
75#define CERROR(M) CLEAN; ERROR(M)
76
77#define CATCH_SIG(SIG) \
78 if (sigaction(SIG, &sa, 0) == -1) { \
79 ERROR("couldn't catch signal " #SIG); \
80 exit(1); \
81 }
82
Wanlong Gao354ebb42012-12-07 10:10:04 +080083extern time_t time(time_t *);
84extern char *ctime(const time_t *);
robbiew84457092003-01-10 17:48:12 +000085extern void exit(int);
86
87static int fd;
88//static char *tmpname; 12/31/02
89static char tmpname[] = "fileXXXXXX";
90static char *progname;
91
Wanlong Gao354ebb42012-12-07 10:10:04 +080092 /*ARGSUSED*/ static
93void cleanup(int sig)
robbiew84457092003-01-10 17:48:12 +000094{
95 /*
96 * Don't check error codes - we could be signaled before the file is
97 * created.
98 */
99 (void)close(fd);
100 (void)unlink(tmpname);
101 exit(1);
102}
103
Wanlong Gao354ebb42012-12-07 10:10:04 +0800104int main(int argc, char *argv[])
robbiew84457092003-01-10 17:48:12 +0000105{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800106 size_t pagesize = (size_t) sysconf(_SC_PAGE_SIZE);
107 caddr_t mmapaddr;
108 char *buf;
109 time_t t;
110 int i;
111 struct sigaction sa;
robbiew84457092003-01-10 17:48:12 +0000112
113 if (!argc) {
114 (void)fprintf(stderr, "argc == 0\n");
115 return 1;
116 }
117 tst_tmpdir();
118 progname = argv[0];
119 (void)time(&t);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800120// (void)printf("%s: Started %s", argv[0], ctime(&t)); LTP Port
121 if (sbrk(pagesize - ((ulong) sbrk(0) & (pagesize - 1))) == (char *)-1) {
robbiew84457092003-01-10 17:48:12 +0000122 ERROR("couldn't round up brk");
123 anyfail();
124 }
125 if ((buf = sbrk(pagesize)) == (char *)-1) {
126 ERROR("couldn't allocate output buffer");
127 anyfail();
128 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800129 if ((mmapaddr = (caddr_t) sbrk(0)) == (caddr_t) - 1) {
robbiew84457092003-01-10 17:48:12 +0000130 ERROR("couldn't find top of brk");
131 anyfail();
132 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800133
robbiew84457092003-01-10 17:48:12 +0000134 /* i changed the second argument to NULL
Wanlong Gao354ebb42012-12-07 10:10:04 +0800135 from argv[0]. otherwise it causes the
136 open to fail
137 -- sreeni
138 */
robbiew84457092003-01-10 17:48:12 +0000139
Wanlong Gao354ebb42012-12-07 10:10:04 +0800140 if ((fd = mkstemp(tmpname)) == -1) {
robbiewe93a3242005-08-31 20:27:12 +0000141 ERROR("mkstemp failed");
robbiew84457092003-01-10 17:48:12 +0000142 anyfail();
143 }
144 sa.sa_handler = cleanup;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800145 sa.sa_flags = 0;
146 if (sigemptyset(&sa.sa_mask)) {
robbiew84457092003-01-10 17:48:12 +0000147 ERROR("sigemptyset failed");
148 anyfail();
Wanlong Gao354ebb42012-12-07 10:10:04 +0800149 }
robbiew84457092003-01-10 17:48:12 +0000150 CATCH_SIG(SIGINT);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800151 CATCH_SIG(SIGQUIT);
152 CATCH_SIG(SIGTERM);
robbiew84457092003-01-10 17:48:12 +0000153 for (i = 0; i < pagesize; i++)
154 buf[i] = 'a';
155 if (write(fd, buf, pagesize) != pagesize) {
156 CERROR("couldn't write page case 1");
157 anyfail();
158 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800159 if (lseek(fd, MMU_NARROWPTEPG * pagesize, SEEK_SET) == -1) {
robbiew84457092003-01-10 17:48:12 +0000160 CERROR("lseek case 1 failed");
161 anyfail();
162 }
163 if (write(fd, buf, pagesize) != pagesize) {
164 CERROR("couldn't write page case 2");
165 anyfail();
166 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800167 if (lseek(fd, 2 * MMU_NARROWPTEPG * pagesize, SEEK_SET) == -1) {
robbiew84457092003-01-10 17:48:12 +0000168 CERROR("lseek case 2 failed");
169 anyfail();
170 }
171 if (write(fd, buf, pagesize) != pagesize) {
172 CERROR("couldn't write page case 3");
173 anyfail();
174 }
175 /* fd now references a sparce file which has three pages widely spaced.
176 * Hopefully different mfile objects will be needed to reference each
177 * page.
178 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800179 if (mmap(mmapaddr + pagesize, pagesize, PROT_READ,
180 MAP_FILE | MAP_PRIVATE | MAP_FIXED, fd,
181 MMU_NARROWPTEPG * pagesize)
182 == (caddr_t) - 1) {
robbiew84457092003-01-10 17:48:12 +0000183 CERROR("first mmap (of third page) failed");
184 anyfail();
185 }
186 if (mmap(mmapaddr, pagesize, PROT_READ,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800187 MAP_FILE | MAP_PRIVATE | MAP_FIXED, fd,
188 2 * MMU_NARROWPTEPG * pagesize)
189 == (caddr_t) - 1) {
robbiew84457092003-01-10 17:48:12 +0000190 CERROR("second mmap (of fifth page) failed");
191 anyfail();
192 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800193 if (mmap(mmapaddr + 2 * pagesize, pagesize, PROT_READ,
194 MAP_FILE | MAP_PRIVATE | MAP_FIXED, fd, 0) == (caddr_t) - 1) {
robbiew84457092003-01-10 17:48:12 +0000195 CERROR("third mmap (of first page) failed");
196 anyfail();
197 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800198 CLEAN; /*comment */
robbiew84457092003-01-10 17:48:12 +0000199 (void)time(&t);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800200// (void)printf("%s: Finished %s", argv[0], ctime(&t)); LTP Port
robbiew84457092003-01-10 17:48:12 +0000201 ok_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800202 tst_exit();
robbiew84457092003-01-10 17:48:12 +0000203}
204
205void ok_exit()
206{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800207 tst_resm(TPASS, "Test passed\n");
208 tst_rmdir();
robbiew84457092003-01-10 17:48:12 +0000209 tst_exit();
210}
211
robbiew84457092003-01-10 17:48:12 +0000212int anyfail()
213{
Cyril Hrubis9fa8ad02014-12-16 13:20:49 +0100214 tst_brkm(TFAIL, tst_rmdir, "Test failed\n");
Chris Dearmanec6edca2012-10-17 19:54:01 -0700215}