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