2 /* 01/02/2003 Port to LTP avenkat@us.ibm.com */
3 /* 06/30/2001 Port to Linux nsharoff@us.ibm.com */
6 * Copyright (c) International Business Machines Corp., 2003
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.
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.
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
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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
30 #include <sys/types.h>
38 extern time_t time(time_t *);
39 extern char *ctime(const time_t *);
40 extern void exit(int);
42 #define ERROR(M) (void)fprintf(stderr, "%s: errno = %d; " M "\n", \
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); \
51 static char tmpname
[] = "fileXXXXXX";
53 /***** LTP Port *****/
59 int local_flag
= PASSED
;
60 char *TCID
= "mmapstress02"; //uiomove_phys_fail
77 * Don't check error codes - we could be signaled before the file is
81 (void)unlink(tmpname
);
87 main(int argc
, char *argv
[]) {
89 size_t pagesize
= sysconf(_SC_PAGE_SIZE
);
96 (void)fprintf(stderr
, "argc == 0\n");
100 (void)fprintf(stderr
, "usage: %s\n", argv
[0]);
104 if ((fd
= mkstemp(tmpname
)) == -1) {
105 ERROR("mkstemp failed");
108 sa
.sa_handler
= cleanup
;
110 if (sigemptyset(&sa
.sa_mask
)) {
111 ERROR("sigemptyset failed");
117 if (sbrk(2*pagesize
- ((ulong
)sbrk(0) & (pagesize
-1))) == (char *)-1) {
118 CLEANERROR("couldn't round up brk");
121 if ((mmapaddr
= sbrk(0)) == (caddr_t
)-1) {
122 CLEANERROR("couldn't find top of brk");
125 /* Write a page of garbage into the file, so we can mmap it without
126 * asking for PROT_WRITE.
128 for (i
= pagesize
; i
; i
--)
130 if (write(fd
, (char *)mmapaddr
-pagesize
, pagesize
) != pagesize
) {
131 CLEANERROR("write failed");
134 if (mmap(mmapaddr
, pagesize
, PROT_NONE
,
135 MAP_FIXED
|MAP_PRIVATE
|MAP_FILE
, fd
, 0) != mmapaddr
)
137 CLEANERROR("couldn't mmap file");
141 * Since the file is mmapped, mmreg_new and uiomove_phys handle all
144 if (lseek(fd
, 0, SEEK_SET
) != 0) {
145 CLEANERROR("lseek failed");
148 if (read(fd
, (char *)mmapaddr
, pagesize
) != -1) {
149 CLEANERROR("read succeded");
152 if (errno
!= EFAULT
) {
153 CLEANERROR("read didn't set errno = EFAULT");
156 if (write(fd
, (char *)mmapaddr
, pagesize
) != -1) {
157 CLEANERROR("write succeded");
160 if (errno
!= EFAULT
) {
161 CLEANERROR("write didn't set errno = EFAULT");
164 if (close(fd
) == -1) {
165 CLEANERROR("close failed");
168 if (unlink(tmpname
) == -1) {
169 ERROR("unlink failed");
173 // (void)printf("%s: Finished %s", argv[0], ctime(&t));
174 ok_exit(); /* LTP Port */
178 /***** LTP Port *****/
181 tst_resm(TPASS
, "Test passed\n");
189 tst_resm(TFAIL
, "Test failed");