robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 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 |
| 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 Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 21 | */ |
| 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> |
robbiew | c0325f7 | 2003-02-10 18:46:26 +0000 | [diff] [blame] | 35 | #include <errno.h> |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 36 | /* #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 | |
| 51 | int local_flag = PASSED; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 52 | char *TCID = "mmapstress05"; //mfile_insque |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 53 | FILE *temp; |
| 54 | int TST_TOTAL = 1; |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 55 | |
| 56 | int anyfail(); |
| 57 | void 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 Modak | 76a720a | 2010-07-03 21:08:18 +0530 | [diff] [blame] | 63 | 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 | } \ |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 72 | 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 84 | extern time_t time(time_t *); |
| 85 | extern char *ctime(const time_t *); |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 86 | extern void exit(int); |
| 87 | |
| 88 | static int fd; |
| 89 | //static char *tmpname; 12/31/02 |
| 90 | static char tmpname[] = "fileXXXXXX"; |
| 91 | static char *progname; |
| 92 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 93 | /*ARGSUSED*/ static |
| 94 | void cleanup(int sig) |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 95 | { |
| 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 105 | int main(int argc, char *argv[]) |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 106 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 107 | 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; |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 113 | |
| 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 121 | // (void)printf("%s: Started %s", argv[0], ctime(&t)); LTP Port |
| 122 | if (sbrk(pagesize - ((ulong) sbrk(0) & (pagesize - 1))) == (char *)-1) { |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 123 | 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 130 | if ((mmapaddr = (caddr_t) sbrk(0)) == (caddr_t) - 1) { |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 131 | ERROR("couldn't find top of brk"); |
| 132 | anyfail(); |
| 133 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 134 | |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 135 | /* i changed the second argument to NULL |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 136 | from argv[0]. otherwise it causes the |
| 137 | open to fail |
| 138 | -- sreeni |
| 139 | */ |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 140 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 141 | if ((fd = mkstemp(tmpname)) == -1) { |
robbiew | e93a324 | 2005-08-31 20:27:12 +0000 | [diff] [blame] | 142 | ERROR("mkstemp failed"); |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 143 | anyfail(); |
| 144 | } |
| 145 | sa.sa_handler = cleanup; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 146 | sa.sa_flags = 0; |
| 147 | if (sigemptyset(&sa.sa_mask)) { |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 148 | ERROR("sigemptyset failed"); |
| 149 | anyfail(); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 150 | } |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 151 | CATCH_SIG(SIGINT); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 152 | CATCH_SIG(SIGQUIT); |
| 153 | CATCH_SIG(SIGTERM); |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 154 | 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 160 | if (lseek(fd, MMU_NARROWPTEPG * pagesize, SEEK_SET) == -1) { |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 161 | 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 168 | if (lseek(fd, 2 * MMU_NARROWPTEPG * pagesize, SEEK_SET) == -1) { |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 169 | 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 180 | if (mmap(mmapaddr + pagesize, pagesize, PROT_READ, |
| 181 | MAP_FILE | MAP_PRIVATE | MAP_FIXED, fd, |
| 182 | MMU_NARROWPTEPG * pagesize) |
| 183 | == (caddr_t) - 1) { |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 184 | CERROR("first mmap (of third page) failed"); |
| 185 | anyfail(); |
| 186 | } |
| 187 | if (mmap(mmapaddr, pagesize, PROT_READ, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 188 | MAP_FILE | MAP_PRIVATE | MAP_FIXED, fd, |
| 189 | 2 * MMU_NARROWPTEPG * pagesize) |
| 190 | == (caddr_t) - 1) { |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 191 | CERROR("second mmap (of fifth page) failed"); |
| 192 | anyfail(); |
| 193 | } |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 194 | if (mmap(mmapaddr + 2 * pagesize, pagesize, PROT_READ, |
| 195 | MAP_FILE | MAP_PRIVATE | MAP_FIXED, fd, 0) == (caddr_t) - 1) { |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 196 | CERROR("third mmap (of first page) failed"); |
| 197 | anyfail(); |
| 198 | } |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 199 | CLEAN; /*comment */ |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 200 | (void)time(&t); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 201 | // (void)printf("%s: Finished %s", argv[0], ctime(&t)); LTP Port |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 202 | ok_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 203 | tst_exit(); |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | void ok_exit() |
| 207 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 208 | tst_resm(TPASS, "Test passed\n"); |
| 209 | tst_rmdir(); |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 210 | tst_exit(); |
| 211 | } |
| 212 | |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 213 | int anyfail() |
| 214 | { |
Cyril Hrubis | 9fa8ad0 | 2014-12-16 13:20:49 +0100 | [diff] [blame^] | 215 | tst_brkm(TFAIL, tst_rmdir, "Test failed\n"); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 216 | } |