1f08c3bdfSopenharmony_ci/*
2f08c3bdfSopenharmony_ci *
3f08c3bdfSopenharmony_ci *   Copyright (c) International Business Machines  Corp., 2001
4f08c3bdfSopenharmony_ci *
5f08c3bdfSopenharmony_ci *   This program is free software;  you can redistribute it and/or modify
6f08c3bdfSopenharmony_ci *   it under the terms of the GNU General Public License as published by
7f08c3bdfSopenharmony_ci *   the Free Software Foundation; either version 2 of the License, or
8f08c3bdfSopenharmony_ci *   (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
13f08c3bdfSopenharmony_ci *   the 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18f08c3bdfSopenharmony_ci */
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_ci/*
21f08c3bdfSopenharmony_ci * Test Name: msync02
22f08c3bdfSopenharmony_ci *
23f08c3bdfSopenharmony_ci * Test Description:
24f08c3bdfSopenharmony_ci *  Verify that msync() succeeds when the region to synchronize is mapped
25f08c3bdfSopenharmony_ci *  shared and the flags argument is MS_INVALIDATE.
26f08c3bdfSopenharmony_ci *
27f08c3bdfSopenharmony_ci * Expected Result:
28f08c3bdfSopenharmony_ci *  msync() should succeed with a return value of 0, and successfully
29f08c3bdfSopenharmony_ci *  synchronize the memory region.
30f08c3bdfSopenharmony_ci *
31f08c3bdfSopenharmony_ci * Algorithm:
32f08c3bdfSopenharmony_ci *  Setup:
33f08c3bdfSopenharmony_ci *   Setup signal handling.
34f08c3bdfSopenharmony_ci *   Create temporary directory.
35f08c3bdfSopenharmony_ci *   Pause for SIGUSR1 if option specified.
36f08c3bdfSopenharmony_ci *
37f08c3bdfSopenharmony_ci *  Test:
38f08c3bdfSopenharmony_ci *   Loop if the proper options are given.
39f08c3bdfSopenharmony_ci *   Execute system call
40f08c3bdfSopenharmony_ci *   Check return code, if system call failed (return=-1)
41f08c3bdfSopenharmony_ci *	Log the errno and Issue a FAIL message.
42f08c3bdfSopenharmony_ci *   Otherwise,
43f08c3bdfSopenharmony_ci *	Verify the Functionality of system call
44f08c3bdfSopenharmony_ci *      if successful,
45f08c3bdfSopenharmony_ci *		Issue Functionality-Pass message.
46f08c3bdfSopenharmony_ci *      Otherwise,
47f08c3bdfSopenharmony_ci *		Issue Functionality-Fail message.
48f08c3bdfSopenharmony_ci *  Cleanup:
49f08c3bdfSopenharmony_ci *   Print errno log and/or timing stats if options given
50f08c3bdfSopenharmony_ci *   Delete the temporary directory created.
51f08c3bdfSopenharmony_ci *
52f08c3bdfSopenharmony_ci * Usage:  <for command-line>
53f08c3bdfSopenharmony_ci *  msync02 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
54f08c3bdfSopenharmony_ci *     where,  -c n : Run n copies concurrently.
55f08c3bdfSopenharmony_ci *             -f   : Turn off functionality Testing.
56f08c3bdfSopenharmony_ci *	       -i n : Execute test n times.
57f08c3bdfSopenharmony_ci *	       -I x : Execute test for x seconds.
58f08c3bdfSopenharmony_ci *	       -P x : Pause for x seconds between iterations.
59f08c3bdfSopenharmony_ci *	       -t   : Turn on syscall timing.
60f08c3bdfSopenharmony_ci *
61f08c3bdfSopenharmony_ci * HISTORY
62f08c3bdfSopenharmony_ci *	07/2001 Ported by Wayne Boyer
63f08c3bdfSopenharmony_ci *
64f08c3bdfSopenharmony_ci * RESTRICTIONS:
65f08c3bdfSopenharmony_ci *  None.
66f08c3bdfSopenharmony_ci */
67f08c3bdfSopenharmony_ci#include <errno.h>
68f08c3bdfSopenharmony_ci#include <unistd.h>
69f08c3bdfSopenharmony_ci#include <fcntl.h>
70f08c3bdfSopenharmony_ci#include <sys/mman.h>
71f08c3bdfSopenharmony_ci
72f08c3bdfSopenharmony_ci#include "test.h"
73f08c3bdfSopenharmony_ci
74f08c3bdfSopenharmony_ci#define TEMPFILE	"msync_file"
75f08c3bdfSopenharmony_ci#define BUF_SIZE	256
76f08c3bdfSopenharmony_ci
77f08c3bdfSopenharmony_cichar *TCID = "msync02";
78f08c3bdfSopenharmony_ciint TST_TOTAL = 1;
79f08c3bdfSopenharmony_ci
80f08c3bdfSopenharmony_cichar *addr;			/* addr of memory mapped region */
81f08c3bdfSopenharmony_cisize_t page_sz;			/* system page size */
82f08c3bdfSopenharmony_ciint fildes;			/* file descriptor for tempfile */
83f08c3bdfSopenharmony_cichar write_buf[10] = "Testing";	/* buffer to hold data to be written */
84f08c3bdfSopenharmony_ci
85f08c3bdfSopenharmony_civoid setup();			/* Main setup function of test */
86f08c3bdfSopenharmony_civoid cleanup();			/* cleanup function for the test */
87f08c3bdfSopenharmony_ci
88f08c3bdfSopenharmony_ciint main(int ac, char **av)
89f08c3bdfSopenharmony_ci{
90f08c3bdfSopenharmony_ci	tst_parse_opts(ac, av, NULL, NULL);
91f08c3bdfSopenharmony_ci
92f08c3bdfSopenharmony_ci	setup();
93f08c3bdfSopenharmony_ci
94f08c3bdfSopenharmony_ci	TEST(msync(addr, page_sz, MS_INVALIDATE));
95f08c3bdfSopenharmony_ci
96f08c3bdfSopenharmony_ci	if (TEST_RETURN == -1)
97f08c3bdfSopenharmony_ci		tst_resm(TFAIL | TTERRNO, "msync failed");
98f08c3bdfSopenharmony_ci	else if (memcmp(addr + 100, write_buf, strlen(write_buf)) != 0)
99f08c3bdfSopenharmony_ci		tst_resm(TFAIL, "memory region contains invalid data");
100f08c3bdfSopenharmony_ci	else
101f08c3bdfSopenharmony_ci		tst_resm(TPASS, "Functionality of msync successful");
102f08c3bdfSopenharmony_ci
103f08c3bdfSopenharmony_ci	cleanup();
104f08c3bdfSopenharmony_ci	tst_exit();
105f08c3bdfSopenharmony_ci}
106f08c3bdfSopenharmony_ci
107f08c3bdfSopenharmony_civoid setup(void)
108f08c3bdfSopenharmony_ci{
109f08c3bdfSopenharmony_ci	size_t c_total = 0, nwrite = 0;	/* no. of bytes to be written */
110f08c3bdfSopenharmony_ci	char tst_buf[BUF_SIZE];
111f08c3bdfSopenharmony_ci
112f08c3bdfSopenharmony_ci	tst_sig(NOFORK, DEF_HANDLER, cleanup);
113f08c3bdfSopenharmony_ci
114f08c3bdfSopenharmony_ci	TEST_PAUSE;
115f08c3bdfSopenharmony_ci
116f08c3bdfSopenharmony_ci	page_sz = (size_t)getpagesize();
117f08c3bdfSopenharmony_ci
118f08c3bdfSopenharmony_ci	tst_tmpdir();
119f08c3bdfSopenharmony_ci
120f08c3bdfSopenharmony_ci	if ((fildes = open(TEMPFILE, O_RDWR | O_CREAT, 0666)) < 0)
121f08c3bdfSopenharmony_ci		tst_brkm(TBROK | TERRNO, cleanup, "open failed");
122f08c3bdfSopenharmony_ci
123f08c3bdfSopenharmony_ci	/* Write one page size of char data into temporary file */
124f08c3bdfSopenharmony_ci	while (c_total < page_sz) {
125f08c3bdfSopenharmony_ci		if ((nwrite = write(fildes, tst_buf, sizeof(tst_buf))) <= 0)
126f08c3bdfSopenharmony_ci			tst_brkm(TBROK | TERRNO, cleanup, "write failed");
127f08c3bdfSopenharmony_ci		else
128f08c3bdfSopenharmony_ci			c_total += nwrite;
129f08c3bdfSopenharmony_ci	}
130f08c3bdfSopenharmony_ci
131f08c3bdfSopenharmony_ci	addr = mmap(0, page_sz, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED,
132f08c3bdfSopenharmony_ci		    fildes, 0);
133f08c3bdfSopenharmony_ci
134f08c3bdfSopenharmony_ci	if (addr == MAP_FAILED)
135f08c3bdfSopenharmony_ci		tst_brkm(TBROK | TERRNO, cleanup, "mmap failed");
136f08c3bdfSopenharmony_ci
137f08c3bdfSopenharmony_ci	/* Again, Seek to the specified byte offset (100) position */
138f08c3bdfSopenharmony_ci	if (lseek(fildes, 100, SEEK_SET) != 100)
139f08c3bdfSopenharmony_ci		tst_brkm(TBROK | TERRNO, cleanup, "lseek failed");
140f08c3bdfSopenharmony_ci
141f08c3bdfSopenharmony_ci	/* Write the string in write_buf at the 100 byte offset */
142f08c3bdfSopenharmony_ci	if (write(fildes, write_buf, strlen(write_buf)) != (long)strlen(write_buf))
143f08c3bdfSopenharmony_ci		tst_brkm(TBROK | TERRNO, cleanup, "write failed");
144f08c3bdfSopenharmony_ci}
145f08c3bdfSopenharmony_ci
146f08c3bdfSopenharmony_civoid cleanup(void)
147f08c3bdfSopenharmony_ci{
148f08c3bdfSopenharmony_ci	if (munmap(addr, page_sz) == -1)
149f08c3bdfSopenharmony_ci		tst_resm(TBROK | TERRNO, "munmap failed");
150f08c3bdfSopenharmony_ci
151f08c3bdfSopenharmony_ci	/* Close the temporary file */
152f08c3bdfSopenharmony_ci	if (close(fildes) == -1)
153f08c3bdfSopenharmony_ci		tst_resm(TWARN | TERRNO, "close failed");
154f08c3bdfSopenharmony_ci
155f08c3bdfSopenharmony_ci	tst_rmdir();
156f08c3bdfSopenharmony_ci}
157