Fix temporary file creation in mmapstress tests
[ltp-debian.git] / testcases / kernel / mem / mmapstress / mmapstress02.c
blobf801a2ee3515b5e3aa18b7e4dd8d86e863336b2d
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 */
5 /*
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
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.
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>
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", \
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); \
51 static char tmpname[] = "fileXXXXXX";
52 static int fd;
53 /***** LTP Port *****/
54 #include "test.h"
55 #include "usctest.h"
56 #define FAILED 0
57 #define PASSED 1
59 int local_flag = PASSED;
60 char *TCID = "mmapstress02"; //uiomove_phys_fail
61 FILE *temp;
62 int TST_TOTAL = 1;
63 extern int Tst_count;
65 int anyfail();
66 void ok_exit();
67 /***** ** ** *****/
71 /*ARGSUSED*/
72 static
73 void
74 cleanup(int sig)
77 * Don't check error codes - we could be signaled before the file is
78 * created.
80 (void)close(fd);
81 (void)unlink(tmpname);
82 tst_rmdir();
83 tst_exit();
86 int
87 main(int argc, char *argv[]) {
88 caddr_t mmapaddr;
89 size_t pagesize = sysconf(_SC_PAGE_SIZE);
90 time_t t;
91 int i;
92 struct sigaction sa;
94 tst_tmpdir();
95 if (!argc) {
96 (void)fprintf(stderr, "argc == 0\n");
97 return 1;
99 if (argc != 1) {
100 (void)fprintf(stderr, "usage: %s\n", argv[0]);
101 return 1;
103 (void)time(&t);
104 if ((fd = mkstemp(tmpname)) == -1) {
105 ERROR("mkstemp failed");
106 anyfail();
108 sa.sa_handler = cleanup;
109 sa.sa_flags = 0;
110 if (sigemptyset(&sa.sa_mask)) {
111 ERROR("sigemptyset failed");
112 anyfail();
114 CATCH_SIG(SIGINT);
115 CATCH_SIG(SIGQUIT);
116 CATCH_SIG(SIGTERM);
117 if (sbrk(2*pagesize - ((ulong)sbrk(0) & (pagesize-1))) == (char *)-1) {
118 CLEANERROR("couldn't round up brk");
119 anyfail();
121 if ((mmapaddr = sbrk(0)) == (caddr_t)-1) {
122 CLEANERROR("couldn't find top of brk");
123 anyfail();
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--)
129 *(mmapaddr-i) = 'a';
130 if (write(fd, (char *)mmapaddr-pagesize, pagesize) != pagesize) {
131 CLEANERROR("write failed");
132 anyfail();
134 if (mmap(mmapaddr, pagesize, PROT_NONE,
135 MAP_FIXED|MAP_PRIVATE|MAP_FILE, fd, 0) != mmapaddr)
137 CLEANERROR("couldn't mmap file");
138 anyfail();
141 * Since the file is mmapped, mmreg_new and uiomove_phys handle all
142 * I/O
144 if (lseek(fd, 0, SEEK_SET) != 0) {
145 CLEANERROR("lseek failed");
146 anyfail();
148 if (read(fd, (char *)mmapaddr, pagesize) != -1) {
149 CLEANERROR("read succeded");
150 anyfail();
152 if (errno != EFAULT) {
153 CLEANERROR("read didn't set errno = EFAULT");
154 anyfail();
156 if (write(fd, (char *)mmapaddr, pagesize) != -1) {
157 CLEANERROR("write succeded");
158 anyfail();
160 if (errno != EFAULT) {
161 CLEANERROR("write didn't set errno = EFAULT");
162 anyfail();
164 if (close(fd) == -1) {
165 CLEANERROR("close failed");
166 anyfail();
168 if (unlink(tmpname) == -1) {
169 ERROR("unlink failed");
170 anyfail();
172 (void)time(&t);
173 // (void)printf("%s: Finished %s", argv[0], ctime(&t));
174 ok_exit(); /* LTP Port */
175 return 0;
178 /***** LTP Port *****/
179 void ok_exit()
181 tst_resm(TPASS, "Test passed\n");
182 tst_rmdir();
183 tst_exit();
187 int anyfail()
189 tst_resm(TFAIL, "Test failed");
190 tst_rmdir();
191 tst_exit();
192 return 0;
195 /***** ** ** *****/