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 | /* |
| 6 | * Copyright (c) International Business Machines Corp., 2003 |
| 7 | * |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 17 | * the GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | /* uiomove_phys_fail: |
| 25 | * Test a copyout/copyin failure in the kernel primitive uiomove_phys by |
| 26 | * reading into or writing from a mmaped regular file which lacks the |
| 27 | * needed permissions. |
| 28 | */ |
| 29 | |
| 30 | #include <sys/types.h> |
| 31 | #include <sys/mman.h> |
| 32 | #include <unistd.h> |
| 33 | #include <fcntl.h> |
| 34 | #include <signal.h> |
| 35 | #include <errno.h> |
| 36 | #include <stdio.h> |
| 37 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 38 | extern time_t time(time_t *); |
| 39 | extern char *ctime(const time_t *); |
| 40 | extern void exit(int); |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 41 | |
| 42 | #define ERROR(M) (void)fprintf(stderr, "%s: errno = %d; " M "\n", \ |
| 43 | argv[0], errno) |
| 44 | #define CLEANERROR(M) (void)unlink(tmpname); ERROR(M) |
| 45 | #define CATCH_SIG(SIG) \ |
| 46 | if (sigaction(SIG, &sa, 0) == -1) { \ |
| 47 | ERROR("couldn't catch signal " #SIG); \ |
| 48 | exit(1); \ |
| 49 | } |
| 50 | |
| 51 | static char tmpname[] = "fileXXXXXX"; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 52 | static int fd; |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 53 | /***** LTP Port *****/ |
| 54 | #include "test.h" |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 55 | #define FAILED 0 |
| 56 | #define PASSED 1 |
| 57 | |
| 58 | int local_flag = PASSED; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 59 | char *TCID = "mmapstress02"; //uiomove_phys_fail |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 60 | FILE *temp; |
| 61 | int TST_TOTAL = 1; |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 62 | |
| 63 | int anyfail(); |
| 64 | void ok_exit(); |
| 65 | /***** ** ** *****/ |
| 66 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 67 | /*ARGSUSED*/ static |
| 68 | void cleanup(int sig) |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 69 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 70 | /* |
| 71 | * Don't check error codes - we could be signaled before the file is |
| 72 | * created. |
| 73 | */ |
| 74 | (void)close(fd); |
| 75 | (void)unlink(tmpname); |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 76 | tst_rmdir(); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 77 | tst_exit(); |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 80 | int main(int argc, char *argv[]) |
| 81 | { |
| 82 | caddr_t mmapaddr; |
| 83 | size_t pagesize = sysconf(_SC_PAGE_SIZE); |
| 84 | time_t t; |
| 85 | int i; |
| 86 | struct sigaction sa; |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 87 | |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 88 | tst_tmpdir(); |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 89 | if (!argc) { |
| 90 | (void)fprintf(stderr, "argc == 0\n"); |
| 91 | return 1; |
| 92 | } |
| 93 | if (argc != 1) { |
| 94 | (void)fprintf(stderr, "usage: %s\n", argv[0]); |
| 95 | return 1; |
| 96 | } |
| 97 | (void)time(&t); |
subrata_modak | acb8449 | 2009-10-26 14:36:28 +0000 | [diff] [blame] | 98 | if ((fd = mkstemp(tmpname)) == -1) { |
robbiew | e93a324 | 2005-08-31 20:27:12 +0000 | [diff] [blame] | 99 | ERROR("mkstemp failed"); |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 100 | anyfail(); |
| 101 | } |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 102 | sa.sa_handler = cleanup; |
| 103 | sa.sa_flags = 0; |
| 104 | if (sigemptyset(&sa.sa_mask)) { |
| 105 | ERROR("sigemptyset failed"); |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 106 | anyfail(); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 107 | } |
| 108 | CATCH_SIG(SIGINT); |
| 109 | CATCH_SIG(SIGQUIT); |
| 110 | CATCH_SIG(SIGTERM); |
| 111 | if (sbrk(2 * pagesize - ((ulong) sbrk(0) & (pagesize - 1))) == |
| 112 | (char *)-1) { |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 113 | CLEANERROR("couldn't round up brk"); |
| 114 | anyfail(); |
| 115 | } |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 116 | if ((mmapaddr = sbrk(0)) == (caddr_t) - 1) { |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 117 | CLEANERROR("couldn't find top of brk"); |
| 118 | anyfail(); |
| 119 | } |
| 120 | /* Write a page of garbage into the file, so we can mmap it without |
| 121 | * asking for PROT_WRITE. |
| 122 | */ |
| 123 | for (i = pagesize; i; i--) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 124 | *(mmapaddr - i) = 'a'; |
| 125 | if (write(fd, (char *)mmapaddr - pagesize, pagesize) != pagesize) { |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 126 | CLEANERROR("write failed"); |
| 127 | anyfail(); |
| 128 | } |
| 129 | if (mmap(mmapaddr, pagesize, PROT_NONE, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 130 | MAP_FIXED | MAP_PRIVATE | MAP_FILE, fd, 0) != mmapaddr) { |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 131 | CLEANERROR("couldn't mmap file"); |
| 132 | anyfail(); |
| 133 | } |
| 134 | /* |
| 135 | * Since the file is mmapped, mmreg_new and uiomove_phys handle all |
| 136 | * I/O |
| 137 | */ |
| 138 | if (lseek(fd, 0, SEEK_SET) != 0) { |
| 139 | CLEANERROR("lseek failed"); |
| 140 | anyfail(); |
| 141 | } |
| 142 | if (read(fd, (char *)mmapaddr, pagesize) != -1) { |
| 143 | CLEANERROR("read succeded"); |
| 144 | anyfail(); |
| 145 | } |
| 146 | if (errno != EFAULT) { |
| 147 | CLEANERROR("read didn't set errno = EFAULT"); |
| 148 | anyfail(); |
| 149 | } |
| 150 | if (write(fd, (char *)mmapaddr, pagesize) != -1) { |
| 151 | CLEANERROR("write succeded"); |
| 152 | anyfail(); |
| 153 | } |
| 154 | if (errno != EFAULT) { |
| 155 | CLEANERROR("write didn't set errno = EFAULT"); |
| 156 | anyfail(); |
| 157 | } |
| 158 | if (close(fd) == -1) { |
| 159 | CLEANERROR("close failed"); |
| 160 | anyfail(); |
| 161 | } |
Subrata Modak | 76a720a | 2010-07-03 21:08:18 +0530 | [diff] [blame] | 162 | if (munmap(mmapaddr, pagesize) == -1) { |
| 163 | CLEANERROR("munmap failed"); |
| 164 | anyfail(); |
| 165 | } |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 166 | if (unlink(tmpname) == -1) { |
| 167 | ERROR("unlink failed"); |
| 168 | anyfail(); |
| 169 | } |
| 170 | (void)time(&t); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 171 | // (void)printf("%s: Finished %s", argv[0], ctime(&t)); |
| 172 | ok_exit(); /* LTP Port */ |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 173 | tst_exit(); |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /***** LTP Port *****/ |
| 177 | void ok_exit() |
| 178 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 179 | tst_resm(TPASS, "Test passed\n"); |
| 180 | tst_rmdir(); |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 181 | tst_exit(); |
| 182 | } |
| 183 | |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 184 | int anyfail() |
| 185 | { |
Cyril Hrubis | 9fa8ad0 | 2014-12-16 13:20:49 +0100 | [diff] [blame] | 186 | tst_brkm(TFAIL, tst_rmdir, "Test failed"); |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 189 | /***** ** ** *****/ |