1f08c3bdfSopenharmony_ci/*
2f08c3bdfSopenharmony_ci * Copyright (C) 2000 Juan Quintela <quintela@fi.udc.es>
3f08c3bdfSopenharmony_ci *                    Aaron Laffin <alaffin@sgi.com>
4f08c3bdfSopenharmony_ci *
5f08c3bdfSopenharmony_ci * This program is free software; you can redistribute it and/or
6f08c3bdfSopenharmony_ci * modify it under the terms of the GNU General Public License
7f08c3bdfSopenharmony_ci * as published by the Free Software Foundation; either version 2
8f08c3bdfSopenharmony_ci * of the License, or (at your option) any later version.
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci * This program is distributed in the hope that it will be useful,
11f08c3bdfSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of
12f08c3bdfSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13f08c3bdfSopenharmony_ci * GNU General Public License for more details.
14f08c3bdfSopenharmony_ci *
15f08c3bdfSopenharmony_ci * You should have received a copy of the GNU General Public License
16f08c3bdfSopenharmony_ci * along with this program; if not, write to the Free Software
17f08c3bdfSopenharmony_ci * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18f08c3bdfSopenharmony_ci *
19f08c3bdfSopenharmony_ci * mmap001.c - Tests mmapping a big file and writing it once
20f08c3bdfSopenharmony_ci */
21f08c3bdfSopenharmony_ci#include <sys/types.h>
22f08c3bdfSopenharmony_ci#include <sys/stat.h>
23f08c3bdfSopenharmony_ci#include <fcntl.h>
24f08c3bdfSopenharmony_ci#include <sys/mman.h>
25f08c3bdfSopenharmony_ci#include <stdlib.h>
26f08c3bdfSopenharmony_ci#include <stdio.h>
27f08c3bdfSopenharmony_ci#include <unistd.h>
28f08c3bdfSopenharmony_ci#include <errno.h>
29f08c3bdfSopenharmony_ci#include <string.h>
30f08c3bdfSopenharmony_ci
31f08c3bdfSopenharmony_ci#include "test.h"
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_cichar *TCID = "mmap001";
34f08c3bdfSopenharmony_ciint TST_TOTAL = 5;
35f08c3bdfSopenharmony_cistatic char *filename = NULL;
36f08c3bdfSopenharmony_cistatic int m_opt = 0;
37f08c3bdfSopenharmony_cistatic char *m_copt;
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_cistatic void cleanup(void)
40f08c3bdfSopenharmony_ci{
41f08c3bdfSopenharmony_ci	free(filename);
42f08c3bdfSopenharmony_ci
43f08c3bdfSopenharmony_ci	tst_rmdir();
44f08c3bdfSopenharmony_ci}
45f08c3bdfSopenharmony_ci
46f08c3bdfSopenharmony_cistatic void setup(void)
47f08c3bdfSopenharmony_ci{
48f08c3bdfSopenharmony_ci	char buf[1024];
49f08c3bdfSopenharmony_ci	/*
50f08c3bdfSopenharmony_ci	 * setup a default signal hander and a
51f08c3bdfSopenharmony_ci	 * temporary working directory.
52f08c3bdfSopenharmony_ci	 */
53f08c3bdfSopenharmony_ci	tst_sig(FORK, DEF_HANDLER, cleanup);
54f08c3bdfSopenharmony_ci
55f08c3bdfSopenharmony_ci	TEST_PAUSE;
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_ci	tst_tmpdir();
58f08c3bdfSopenharmony_ci
59f08c3bdfSopenharmony_ci	snprintf(buf, 1024, "testfile.%d", getpid());
60f08c3bdfSopenharmony_ci
61f08c3bdfSopenharmony_ci	if ((filename = strdup(buf)) == NULL) {
62f08c3bdfSopenharmony_ci		tst_brkm(TBROK | TERRNO, cleanup, "strdup failed");
63f08c3bdfSopenharmony_ci	}
64f08c3bdfSopenharmony_ci
65f08c3bdfSopenharmony_ci}
66f08c3bdfSopenharmony_ci
67f08c3bdfSopenharmony_cistatic void help(void)
68f08c3bdfSopenharmony_ci{
69f08c3bdfSopenharmony_ci	printf("  -m x    size of mmap in pages (default 1000)\n");
70f08c3bdfSopenharmony_ci}
71f08c3bdfSopenharmony_ci
72f08c3bdfSopenharmony_ci/*
73f08c3bdfSopenharmony_ci * add the -m option whose parameter is the
74f08c3bdfSopenharmony_ci * pages that should be mapped.
75f08c3bdfSopenharmony_ci */
76f08c3bdfSopenharmony_cioption_t options[] = {
77f08c3bdfSopenharmony_ci	{"m:", &m_opt, &m_copt},
78f08c3bdfSopenharmony_ci	{NULL, NULL, NULL}
79f08c3bdfSopenharmony_ci};
80f08c3bdfSopenharmony_ci
81f08c3bdfSopenharmony_ciint main(int argc, char *argv[])
82f08c3bdfSopenharmony_ci{
83f08c3bdfSopenharmony_ci	char *array;
84f08c3bdfSopenharmony_ci	int lc;
85f08c3bdfSopenharmony_ci	unsigned int i;
86f08c3bdfSopenharmony_ci	int fd;
87f08c3bdfSopenharmony_ci	unsigned int pages, memsize;
88f08c3bdfSopenharmony_ci
89f08c3bdfSopenharmony_ci	tst_parse_opts(argc, argv, options, help);
90f08c3bdfSopenharmony_ci
91f08c3bdfSopenharmony_ci	if (m_opt) {
92f08c3bdfSopenharmony_ci		memsize = pages = atoi(m_copt);
93f08c3bdfSopenharmony_ci
94f08c3bdfSopenharmony_ci		if (memsize < 1) {
95f08c3bdfSopenharmony_ci			tst_brkm(TBROK, cleanup, "Invalid arg for -m: %s",
96f08c3bdfSopenharmony_ci				 m_copt);
97f08c3bdfSopenharmony_ci		}
98f08c3bdfSopenharmony_ci
99f08c3bdfSopenharmony_ci		memsize *= getpagesize();	/* N PAGES */
100f08c3bdfSopenharmony_ci
101f08c3bdfSopenharmony_ci	} else {
102f08c3bdfSopenharmony_ci		/*
103f08c3bdfSopenharmony_ci		 * default size 1000 pages;
104f08c3bdfSopenharmony_ci		 */
105f08c3bdfSopenharmony_ci		memsize = pages = 1000;
106f08c3bdfSopenharmony_ci		memsize *= getpagesize();
107f08c3bdfSopenharmony_ci	}
108f08c3bdfSopenharmony_ci
109f08c3bdfSopenharmony_ci	tst_resm(TINFO, "mmap()ing file of %u pages or %u bytes", pages,
110f08c3bdfSopenharmony_ci		 memsize);
111f08c3bdfSopenharmony_ci
112f08c3bdfSopenharmony_ci	setup();
113f08c3bdfSopenharmony_ci
114f08c3bdfSopenharmony_ci	for (lc = 0; TEST_LOOPING(lc); lc++) {
115f08c3bdfSopenharmony_ci		tst_count = 0;
116f08c3bdfSopenharmony_ci
117f08c3bdfSopenharmony_ci		fd = open(filename, O_RDWR | O_CREAT, 0666);
118f08c3bdfSopenharmony_ci		if ((fd == -1))
119f08c3bdfSopenharmony_ci			tst_brkm(TBROK | TERRNO, cleanup,
120f08c3bdfSopenharmony_ci				 "opening %s failed", filename);
121f08c3bdfSopenharmony_ci
122f08c3bdfSopenharmony_ci		if (lseek(fd, memsize, SEEK_SET) != memsize) {
123f08c3bdfSopenharmony_ci			TEST_ERRNO = errno;
124f08c3bdfSopenharmony_ci			close(fd);
125f08c3bdfSopenharmony_ci			tst_brkm(TBROK | TTERRNO, cleanup, "lseek failed");
126f08c3bdfSopenharmony_ci		}
127f08c3bdfSopenharmony_ci
128f08c3bdfSopenharmony_ci		if (write(fd, "\0", 1) != 1) {
129f08c3bdfSopenharmony_ci			TEST_ERRNO = errno;
130f08c3bdfSopenharmony_ci			close(fd);
131f08c3bdfSopenharmony_ci			tst_brkm(TBROK | TTERRNO, cleanup,
132f08c3bdfSopenharmony_ci				 "writing to %s failed", filename);
133f08c3bdfSopenharmony_ci		}
134f08c3bdfSopenharmony_ci
135f08c3bdfSopenharmony_ci		array = mmap(0, memsize, PROT_WRITE, MAP_SHARED, fd, 0);
136f08c3bdfSopenharmony_ci		if (array == MAP_FAILED) {
137f08c3bdfSopenharmony_ci			TEST_ERRNO = errno;
138f08c3bdfSopenharmony_ci			close(fd);
139f08c3bdfSopenharmony_ci			tst_brkm(TBROK | TTERRNO, cleanup,
140f08c3bdfSopenharmony_ci				 "mmapping %s failed", filename);
141f08c3bdfSopenharmony_ci		} else {
142f08c3bdfSopenharmony_ci			tst_resm(TPASS, "mmap() completed successfully.");
143f08c3bdfSopenharmony_ci		}
144f08c3bdfSopenharmony_ci
145f08c3bdfSopenharmony_ci		tst_resm(TINFO, "touching mmaped memory");
146f08c3bdfSopenharmony_ci
147f08c3bdfSopenharmony_ci		for (i = 0; i < memsize; i++) {
148f08c3bdfSopenharmony_ci			array[i] = (char)i;
149f08c3bdfSopenharmony_ci		}
150f08c3bdfSopenharmony_ci
151f08c3bdfSopenharmony_ci		/*
152f08c3bdfSopenharmony_ci		 * seems that if the map area was bad, we'd get SEGV,
153f08c3bdfSopenharmony_ci		 * hence we can indicate a PASS.
154f08c3bdfSopenharmony_ci		 */
155f08c3bdfSopenharmony_ci		tst_resm(TPASS,
156f08c3bdfSopenharmony_ci			 "we're still here, mmaped area must be good");
157f08c3bdfSopenharmony_ci
158f08c3bdfSopenharmony_ci		TEST(msync(array, memsize, MS_SYNC));
159f08c3bdfSopenharmony_ci
160f08c3bdfSopenharmony_ci		if (TEST_RETURN == -1) {
161f08c3bdfSopenharmony_ci			tst_resm(TFAIL | TTERRNO,
162f08c3bdfSopenharmony_ci				 "synchronizing mmapped page failed");
163f08c3bdfSopenharmony_ci		} else {
164f08c3bdfSopenharmony_ci			tst_resm(TPASS,
165f08c3bdfSopenharmony_ci				 "synchronizing mmapped page passed");
166f08c3bdfSopenharmony_ci		}
167f08c3bdfSopenharmony_ci
168f08c3bdfSopenharmony_ci		TEST(munmap(array, memsize));
169f08c3bdfSopenharmony_ci
170f08c3bdfSopenharmony_ci		if (TEST_RETURN == -1) {
171f08c3bdfSopenharmony_ci			tst_resm(TFAIL | TTERRNO,
172f08c3bdfSopenharmony_ci				 "munmapping %s failed", filename);
173f08c3bdfSopenharmony_ci		} else {
174f08c3bdfSopenharmony_ci			tst_resm(TPASS, "munmapping %s successful", filename);
175f08c3bdfSopenharmony_ci		}
176f08c3bdfSopenharmony_ci
177f08c3bdfSopenharmony_ci		close(fd);
178f08c3bdfSopenharmony_ci		unlink(filename);
179f08c3bdfSopenharmony_ci
180f08c3bdfSopenharmony_ci	}
181f08c3bdfSopenharmony_ci	cleanup();
182f08c3bdfSopenharmony_ci	tst_exit();
183f08c3bdfSopenharmony_ci}
184