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