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