blob: befcff2e17fdeab40bdaf9b3d52ae8268ac4e5a9 [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"
40#include "usctest.h"
41/***** ** ** *****/
42
43#ifndef MMU_NARROWPTEPG
44#define MMU_NARROWPTEPG 1024
45#endif
46
47/***** LTP Port *****/
48#define FAILED 0
49#define PASSED 1
50
51int local_flag = PASSED;
Wanlong Gao354ebb42012-12-07 10:10:04 +080052char *TCID = "mmapstress05"; //mfile_insque
robbiew84457092003-01-10 17:48:12 +000053FILE *temp;
54int TST_TOTAL = 1;
robbiew84457092003-01-10 17:48:12 +000055
56int anyfail();
57void ok_exit();
58/***** ** ** *****/
59
60#define ERROR(M) (void)fprintf(stderr, "%s: errno = %d; " M "\n", \
61 progname, errno);
62#define CLEAN (void)close(fd); \
Subrata Modak76a720a2010-07-03 21:08:18 +053063 if (munmap(mmapaddr+pagesize, pagesize) == -1) { \
64 ERROR("munmap failed"); \
65 } \
66 if (munmap(mmapaddr, pagesize) == -1) { \
67 ERROR("munmap failed"); \
68 } \
69 if (munmap(mmapaddr+2*pagesize, pagesize) == -1) { \
70 ERROR("munmap failed"); \
71 } \
robbiew84457092003-01-10 17:48:12 +000072 if (unlink(tmpname)) { \
73 ERROR("couldn't clean up temp file"); \
74 }
75
76#define CERROR(M) CLEAN; ERROR(M)
77
78#define CATCH_SIG(SIG) \
79 if (sigaction(SIG, &sa, 0) == -1) { \
80 ERROR("couldn't catch signal " #SIG); \
81 exit(1); \
82 }
83
Wanlong Gao354ebb42012-12-07 10:10:04 +080084extern time_t time(time_t *);
85extern char *ctime(const time_t *);
robbiew84457092003-01-10 17:48:12 +000086extern void exit(int);
87
88static int fd;
89//static char *tmpname; 12/31/02
90static char tmpname[] = "fileXXXXXX";
91static char *progname;
92
Wanlong Gao354ebb42012-12-07 10:10:04 +080093 /*ARGSUSED*/ static
94void cleanup(int sig)
robbiew84457092003-01-10 17:48:12 +000095{
96 /*
97 * Don't check error codes - we could be signaled before the file is
98 * created.
99 */
100 (void)close(fd);
101 (void)unlink(tmpname);
102 exit(1);
103}
104
Wanlong Gao354ebb42012-12-07 10:10:04 +0800105int main(int argc, char *argv[])
robbiew84457092003-01-10 17:48:12 +0000106{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800107 size_t pagesize = (size_t) sysconf(_SC_PAGE_SIZE);
108 caddr_t mmapaddr;
109 char *buf;
110 time_t t;
111 int i;
112 struct sigaction sa;
robbiew84457092003-01-10 17:48:12 +0000113
114 if (!argc) {
115 (void)fprintf(stderr, "argc == 0\n");
116 return 1;
117 }
118 tst_tmpdir();
119 progname = argv[0];
120 (void)time(&t);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800121// (void)printf("%s: Started %s", argv[0], ctime(&t)); LTP Port
122 if (sbrk(pagesize - ((ulong) sbrk(0) & (pagesize - 1))) == (char *)-1) {
robbiew84457092003-01-10 17:48:12 +0000123 ERROR("couldn't round up brk");
124 anyfail();
125 }
126 if ((buf = sbrk(pagesize)) == (char *)-1) {
127 ERROR("couldn't allocate output buffer");
128 anyfail();
129 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800130 if ((mmapaddr = (caddr_t) sbrk(0)) == (caddr_t) - 1) {
robbiew84457092003-01-10 17:48:12 +0000131 ERROR("couldn't find top of brk");
132 anyfail();
133 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800134
robbiew84457092003-01-10 17:48:12 +0000135 /* i changed the second argument to NULL
Wanlong Gao354ebb42012-12-07 10:10:04 +0800136 from argv[0]. otherwise it causes the
137 open to fail
138 -- sreeni
139 */
robbiew84457092003-01-10 17:48:12 +0000140
Wanlong Gao354ebb42012-12-07 10:10:04 +0800141 if ((fd = mkstemp(tmpname)) == -1) {
robbiewe93a3242005-08-31 20:27:12 +0000142 ERROR("mkstemp failed");
robbiew84457092003-01-10 17:48:12 +0000143 anyfail();
144 }
145 sa.sa_handler = cleanup;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800146 sa.sa_flags = 0;
147 if (sigemptyset(&sa.sa_mask)) {
robbiew84457092003-01-10 17:48:12 +0000148 ERROR("sigemptyset failed");
149 anyfail();
Wanlong Gao354ebb42012-12-07 10:10:04 +0800150 }
robbiew84457092003-01-10 17:48:12 +0000151 CATCH_SIG(SIGINT);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800152 CATCH_SIG(SIGQUIT);
153 CATCH_SIG(SIGTERM);
robbiew84457092003-01-10 17:48:12 +0000154 for (i = 0; i < pagesize; i++)
155 buf[i] = 'a';
156 if (write(fd, buf, pagesize) != pagesize) {
157 CERROR("couldn't write page case 1");
158 anyfail();
159 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800160 if (lseek(fd, MMU_NARROWPTEPG * pagesize, SEEK_SET) == -1) {
robbiew84457092003-01-10 17:48:12 +0000161 CERROR("lseek case 1 failed");
162 anyfail();
163 }
164 if (write(fd, buf, pagesize) != pagesize) {
165 CERROR("couldn't write page case 2");
166 anyfail();
167 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800168 if (lseek(fd, 2 * MMU_NARROWPTEPG * pagesize, SEEK_SET) == -1) {
robbiew84457092003-01-10 17:48:12 +0000169 CERROR("lseek case 2 failed");
170 anyfail();
171 }
172 if (write(fd, buf, pagesize) != pagesize) {
173 CERROR("couldn't write page case 3");
174 anyfail();
175 }
176 /* fd now references a sparce file which has three pages widely spaced.
177 * Hopefully different mfile objects will be needed to reference each
178 * page.
179 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800180 if (mmap(mmapaddr + pagesize, pagesize, PROT_READ,
181 MAP_FILE | MAP_PRIVATE | MAP_FIXED, fd,
182 MMU_NARROWPTEPG * pagesize)
183 == (caddr_t) - 1) {
robbiew84457092003-01-10 17:48:12 +0000184 CERROR("first mmap (of third page) failed");
185 anyfail();
186 }
187 if (mmap(mmapaddr, pagesize, PROT_READ,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800188 MAP_FILE | MAP_PRIVATE | MAP_FIXED, fd,
189 2 * MMU_NARROWPTEPG * pagesize)
190 == (caddr_t) - 1) {
robbiew84457092003-01-10 17:48:12 +0000191 CERROR("second mmap (of fifth page) failed");
192 anyfail();
193 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800194 if (mmap(mmapaddr + 2 * pagesize, pagesize, PROT_READ,
195 MAP_FILE | MAP_PRIVATE | MAP_FIXED, fd, 0) == (caddr_t) - 1) {
robbiew84457092003-01-10 17:48:12 +0000196 CERROR("third mmap (of first page) failed");
197 anyfail();
198 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800199 CLEAN; /*comment */
robbiew84457092003-01-10 17:48:12 +0000200 (void)time(&t);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800201// (void)printf("%s: Finished %s", argv[0], ctime(&t)); LTP Port
robbiew84457092003-01-10 17:48:12 +0000202 ok_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800203 tst_exit();
robbiew84457092003-01-10 17:48:12 +0000204}
205
206void ok_exit()
207{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800208 tst_resm(TPASS, "Test passed\n");
209 tst_rmdir();
robbiew84457092003-01-10 17:48:12 +0000210 tst_exit();
211}
212
robbiew84457092003-01-10 17:48:12 +0000213int anyfail()
214{
Cyril Hrubis9fa8ad02014-12-16 13:20:49 +0100215 tst_brkm(TFAIL, tst_rmdir, "Test failed\n");
Chris Dearmanec6edca2012-10-17 19:54:01 -0700216}