1f08c3bdfSopenharmony_ci/*
2f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines  Corp., 2001
3f08c3bdfSopenharmony_ci *
4f08c3bdfSopenharmony_ci * This program is free software;  you can redistribute it and/or modify
5f08c3bdfSopenharmony_ci * it under the terms of the GNU General Public License as published by
6f08c3bdfSopenharmony_ci * the Free Software Foundation; either version 2 of the License, or
7f08c3bdfSopenharmony_ci * (at your option) any later version.
8f08c3bdfSopenharmony_ci *
9f08c3bdfSopenharmony_ci * This program is distributed in the hope that it will be useful,
10f08c3bdfSopenharmony_ci * but WITHOUT ANY WARRANTY;  without even the implied warranty of
11f08c3bdfSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
12f08c3bdfSopenharmony_ci * the GNU General Public License for more details.
13f08c3bdfSopenharmony_ci *
14f08c3bdfSopenharmony_ci * You should have received a copy of the GNU General Public License
15f08c3bdfSopenharmony_ci * along with this program;  if not, write to the Free Software
16f08c3bdfSopenharmony_ci * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17f08c3bdfSopenharmony_ci */
18f08c3bdfSopenharmony_ci
19f08c3bdfSopenharmony_ci/*
20f08c3bdfSopenharmony_ci * Test Description:
21f08c3bdfSopenharmony_ci *  Call mmap() to map a file creating a mapped region with read access
22f08c3bdfSopenharmony_ci *  under the following conditions -
23f08c3bdfSopenharmony_ci *	- The prot parameter is set to PROT_WRITE
24f08c3bdfSopenharmony_ci *	- The file descriptor is open for writing.
25f08c3bdfSopenharmony_ci *	- The flags parameter has MAP_PRIVATE set.
26f08c3bdfSopenharmony_ci *
27f08c3bdfSopenharmony_ci *  The call should fail to map the file.
28f08c3bdfSopenharmony_ci *
29f08c3bdfSopenharmony_ci * Expected Result:
30f08c3bdfSopenharmony_ci *  mmap() should fail returning -1 and errno should get set to EACCES.
31f08c3bdfSopenharmony_ci *
32f08c3bdfSopenharmony_ci * HISTORY
33f08c3bdfSopenharmony_ci *	07/2001 Ported by Wayne Boyer
34f08c3bdfSopenharmony_ci */
35f08c3bdfSopenharmony_ci#include <stdio.h>
36f08c3bdfSopenharmony_ci#include <stdlib.h>
37f08c3bdfSopenharmony_ci#include <sys/types.h>
38f08c3bdfSopenharmony_ci#include <errno.h>
39f08c3bdfSopenharmony_ci#include <unistd.h>
40f08c3bdfSopenharmony_ci#include <fcntl.h>
41f08c3bdfSopenharmony_ci#include <string.h>
42f08c3bdfSopenharmony_ci#include <signal.h>
43f08c3bdfSopenharmony_ci#include <sys/stat.h>
44f08c3bdfSopenharmony_ci#include <sys/mman.h>
45f08c3bdfSopenharmony_ci
46f08c3bdfSopenharmony_ci#include "test.h"
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_ci#define TEMPFILE	"mmapfile"
49f08c3bdfSopenharmony_ci
50f08c3bdfSopenharmony_cichar *TCID = "mmap07";
51f08c3bdfSopenharmony_ciint TST_TOTAL = 1;
52f08c3bdfSopenharmony_ci
53f08c3bdfSopenharmony_cistatic size_t page_sz;
54f08c3bdfSopenharmony_cistatic char *addr;
55f08c3bdfSopenharmony_cistatic int fildes;
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_cistatic void setup(void);
58f08c3bdfSopenharmony_cistatic void cleanup(void);
59f08c3bdfSopenharmony_ci
60f08c3bdfSopenharmony_ciint main(int ac, char **av)
61f08c3bdfSopenharmony_ci{
62f08c3bdfSopenharmony_ci	int lc;
63f08c3bdfSopenharmony_ci
64f08c3bdfSopenharmony_ci	tst_parse_opts(ac, av, NULL, NULL);
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_ci	setup();
67f08c3bdfSopenharmony_ci
68f08c3bdfSopenharmony_ci	for (lc = 0; TEST_LOOPING(lc); lc++) {
69f08c3bdfSopenharmony_ci
70f08c3bdfSopenharmony_ci		tst_count = 0;
71f08c3bdfSopenharmony_ci
72f08c3bdfSopenharmony_ci		/*
73f08c3bdfSopenharmony_ci		 * Call mmap to map the temporary file 'TEMPFILE'
74f08c3bdfSopenharmony_ci		 * with write access.
75f08c3bdfSopenharmony_ci		 */
76f08c3bdfSopenharmony_ci		errno = 0;
77f08c3bdfSopenharmony_ci		addr = mmap(0, page_sz, PROT_WRITE,
78f08c3bdfSopenharmony_ci			    MAP_FILE | MAP_PRIVATE, fildes, 0);
79f08c3bdfSopenharmony_ci		TEST_ERRNO = errno;
80f08c3bdfSopenharmony_ci
81f08c3bdfSopenharmony_ci		/* Check for the return value of mmap() */
82f08c3bdfSopenharmony_ci		if (addr != MAP_FAILED) {
83f08c3bdfSopenharmony_ci			tst_resm(TFAIL | TERRNO,
84f08c3bdfSopenharmony_ci				 "mmap() returned invalid value, expected: %p",
85f08c3bdfSopenharmony_ci				 MAP_FAILED);
86f08c3bdfSopenharmony_ci			/* Unmap the mapped memory */
87f08c3bdfSopenharmony_ci			if (munmap(addr, page_sz) != 0) {
88f08c3bdfSopenharmony_ci				tst_resm(TBROK, "munmap() failed");
89f08c3bdfSopenharmony_ci				cleanup();
90f08c3bdfSopenharmony_ci			}
91f08c3bdfSopenharmony_ci			continue;
92f08c3bdfSopenharmony_ci		}
93f08c3bdfSopenharmony_ci		if (TEST_ERRNO == EACCES) {
94f08c3bdfSopenharmony_ci			tst_resm(TPASS, "mmap failed with EACCES");
95f08c3bdfSopenharmony_ci		} else {
96f08c3bdfSopenharmony_ci			tst_resm(TFAIL | TERRNO,
97f08c3bdfSopenharmony_ci				 "mmap failed with unexpected errno");
98f08c3bdfSopenharmony_ci		}
99f08c3bdfSopenharmony_ci
100f08c3bdfSopenharmony_ci	}
101f08c3bdfSopenharmony_ci	cleanup();
102f08c3bdfSopenharmony_ci	tst_exit();
103f08c3bdfSopenharmony_ci
104f08c3bdfSopenharmony_ci}
105f08c3bdfSopenharmony_ci
106f08c3bdfSopenharmony_cistatic void setup(void)
107f08c3bdfSopenharmony_ci{
108f08c3bdfSopenharmony_ci	char *tst_buff;
109f08c3bdfSopenharmony_ci
110f08c3bdfSopenharmony_ci	tst_sig(NOFORK, DEF_HANDLER, cleanup);
111f08c3bdfSopenharmony_ci
112f08c3bdfSopenharmony_ci	TEST_PAUSE;
113f08c3bdfSopenharmony_ci
114f08c3bdfSopenharmony_ci	page_sz = getpagesize();
115f08c3bdfSopenharmony_ci
116f08c3bdfSopenharmony_ci	/* Allocate space for the test buffer */
117f08c3bdfSopenharmony_ci	if ((tst_buff = calloc(page_sz, sizeof(char))) == NULL) {
118f08c3bdfSopenharmony_ci		tst_brkm(TFAIL, NULL,
119f08c3bdfSopenharmony_ci			 "calloc() failed to allocate space for tst_buff");
120f08c3bdfSopenharmony_ci	}
121f08c3bdfSopenharmony_ci
122f08c3bdfSopenharmony_ci	/* Fill the test buffer with the known data */
123f08c3bdfSopenharmony_ci	memset(tst_buff, 'A', page_sz);
124f08c3bdfSopenharmony_ci
125f08c3bdfSopenharmony_ci	tst_tmpdir();
126f08c3bdfSopenharmony_ci
127f08c3bdfSopenharmony_ci	/* Creat a temporary file used for mapping */
128f08c3bdfSopenharmony_ci	if ((fildes = open(TEMPFILE, O_WRONLY | O_CREAT, 0666)) < 0) {
129f08c3bdfSopenharmony_ci		free(tst_buff);
130f08c3bdfSopenharmony_ci		tst_brkm(TFAIL, cleanup, "open() on %s failed", TEMPFILE);
131f08c3bdfSopenharmony_ci	}
132f08c3bdfSopenharmony_ci
133f08c3bdfSopenharmony_ci	/* Write test buffer contents into temporary file */
134f08c3bdfSopenharmony_ci	if (write(fildes, tst_buff, page_sz) < (int)page_sz) {
135f08c3bdfSopenharmony_ci		free(tst_buff);
136f08c3bdfSopenharmony_ci		tst_brkm(TFAIL, cleanup, "writing to %s failed", TEMPFILE);
137f08c3bdfSopenharmony_ci	}
138f08c3bdfSopenharmony_ci
139f08c3bdfSopenharmony_ci	free(tst_buff);
140f08c3bdfSopenharmony_ci}
141f08c3bdfSopenharmony_ci
142f08c3bdfSopenharmony_cistatic void cleanup(void)
143f08c3bdfSopenharmony_ci{
144f08c3bdfSopenharmony_ci	close(fildes);
145f08c3bdfSopenharmony_ci	tst_rmdir();
146f08c3bdfSopenharmony_ci}
147