Fix temporary file creation in mmapstress tests
[ltp-debian.git] / testcases / kernel / mem / mmapstress / mmapstress05.c
blob4d25b8d64b5124d70d29396169257c381a243b7a
1 /* 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
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.
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.
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
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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.
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>
35 #include <errno.h>
36 /* #include <sys/pte.h> */
38 /***** LTP Port *****/
39 #include "test.h"
40 #include "usctest.h"
41 /***** ** ** *****/
43 #ifndef MMU_NARROWPTEPG
44 #define MMU_NARROWPTEPG 1024
45 #endif
47 /***** LTP Port *****/
48 #define FAILED 0
49 #define PASSED 1
51 int local_flag = PASSED;
52 char *TCID = "mmapstress05";//mfile_insque
53 FILE *temp;
54 int TST_TOTAL = 1;
55 extern int Tst_count;
57 int anyfail();
58 void ok_exit();
59 /***** ** ** *****/
61 #define ERROR(M) (void)fprintf(stderr, "%s: errno = %d; " M "\n", \
62 progname, errno);
63 #define CLEAN (void)close(fd); \
64 if (unlink(tmpname)) { \
65 ERROR("couldn't clean up temp file"); \
68 #define CERROR(M) CLEAN; ERROR(M)
70 #define CATCH_SIG(SIG) \
71 if (sigaction(SIG, &sa, 0) == -1) { \
72 ERROR("couldn't catch signal " #SIG); \
73 exit(1); \
76 extern time_t time(time_t *);
77 extern char *ctime(const time_t *);
78 extern void exit(int);
80 static int fd;
81 //static char *tmpname; 12/31/02
82 static char tmpname[] = "fileXXXXXX";
83 static char *progname;
85 /*ARGSUSED*/
86 static
87 void
88 cleanup(int sig)
91 * Don't check error codes - we could be signaled before the file is
92 * created.
94 (void)close(fd);
95 (void)unlink(tmpname);
96 exit(1);
99 int
100 main(int argc, char *argv[])
102 size_t pagesize = (size_t)sysconf(_SC_PAGE_SIZE);
103 caddr_t mmapaddr;
104 char *buf;
105 time_t t;
106 int i;
107 struct sigaction sa;
109 if (!argc) {
110 (void)fprintf(stderr, "argc == 0\n");
111 return 1;
113 tst_tmpdir();
114 progname = argv[0];
115 (void)time(&t);
116 // (void)printf("%s: Started %s", argv[0], ctime(&t)); LTP Port
117 if (sbrk(pagesize-((ulong)sbrk(0)&(pagesize-1))) == (char *)-1) {
118 ERROR("couldn't round up brk");
119 anyfail();
121 if ((buf = sbrk(pagesize)) == (char *)-1) {
122 ERROR("couldn't allocate output buffer");
123 anyfail();
125 if ((mmapaddr = (caddr_t)sbrk(0)) == (caddr_t)-1) {
126 ERROR("couldn't find top of brk");
127 anyfail();
130 /* i changed the second argument to NULL
131 from argv[0]. otherwise it causes the
132 open to fail
133 -- sreeni
136 if ((fd = mkstemp(tmpname))==-1) {
137 ERROR("mkstemp failed");
138 anyfail();
140 sa.sa_handler = cleanup;
141 sa.sa_flags = 0;
142 if (sigemptyset(&sa.sa_mask)) {
143 ERROR("sigemptyset failed");
144 anyfail();
146 CATCH_SIG(SIGINT);
147 CATCH_SIG(SIGQUIT);
148 CATCH_SIG(SIGTERM);
149 for (i = 0; i < pagesize; i++)
150 buf[i] = 'a';
151 if (write(fd, buf, pagesize) != pagesize) {
152 CERROR("couldn't write page case 1");
153 anyfail();
155 if (lseek(fd, MMU_NARROWPTEPG*pagesize, SEEK_SET) == -1) {
156 CERROR("lseek case 1 failed");
157 anyfail();
159 if (write(fd, buf, pagesize) != pagesize) {
160 CERROR("couldn't write page case 2");
161 anyfail();
163 if (lseek(fd, 2*MMU_NARROWPTEPG*pagesize, SEEK_SET) == -1) {
164 CERROR("lseek case 2 failed");
165 anyfail();
167 if (write(fd, buf, pagesize) != pagesize) {
168 CERROR("couldn't write page case 3");
169 anyfail();
171 /* fd now references a sparce file which has three pages widely spaced.
172 * Hopefully different mfile objects will be needed to reference each
173 * page.
175 if (mmap(mmapaddr+pagesize, pagesize, PROT_READ,
176 MAP_FILE|MAP_PRIVATE|MAP_FIXED, fd, MMU_NARROWPTEPG*pagesize)
177 == (caddr_t)-1)
179 CERROR("first mmap (of third page) failed");
180 anyfail();
182 if (mmap(mmapaddr, pagesize, PROT_READ,
183 MAP_FILE|MAP_PRIVATE|MAP_FIXED, fd, 2*MMU_NARROWPTEPG*pagesize)
184 == (caddr_t)-1)
186 CERROR("second mmap (of fifth page) failed");
187 anyfail();
189 if (mmap(mmapaddr+2*pagesize, pagesize, PROT_READ,
190 MAP_FILE|MAP_PRIVATE|MAP_FIXED, fd, 0) == (caddr_t)-1)
192 CERROR("third mmap (of first page) failed");
193 anyfail();
195 CLEAN; /*comment*/
196 (void)time(&t);
197 // (void)printf("%s: Finished %s", argv[0], ctime(&t)); LTP Port
198 ok_exit();
199 return 0;
202 void ok_exit()
204 tst_resm(TPASS, "Test passed\n");
205 tst_rmdir();
206 tst_exit();
210 int anyfail()
212 tst_resm(TFAIL, "Test failed\n");
213 tst_rmdir();
214 tst_exit();
215 return 0;