blob: e1076a90296dfd3bc83edc8f51cf003536432471 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 *
3 * Copyright (c) International Business Machines Corp., 2001
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
19
20/*
21 * Test Name: msync02
22 *
23 * Test Description:
24 * Verify that msync() succeeds when the region to synchronize is mapped
25 * shared and the flags argument is MS_INVALIDATE.
26 *
27 * Expected Result:
28 * msync() should succeed with a return value of 0, and successfully
29 * synchronize the memory region.
30 *
31 * Algorithm:
32 * Setup:
33 * Setup signal handling.
34 * Create temporary directory.
35 * Pause for SIGUSR1 if option specified.
36 *
37 * Test:
38 * Loop if the proper options are given.
39 * Execute system call
40 * Check return code, if system call failed (return=-1)
subrata_modak56207ce2009-03-23 13:35:39 +000041 * Log the errno and Issue a FAIL message.
plars865695b2001-08-27 22:15:12 +000042 * Otherwise,
subrata_modak56207ce2009-03-23 13:35:39 +000043 * Verify the Functionality of system call
plars865695b2001-08-27 22:15:12 +000044 * if successful,
subrata_modak56207ce2009-03-23 13:35:39 +000045 * Issue Functionality-Pass message.
plars865695b2001-08-27 22:15:12 +000046 * Otherwise,
47 * Issue Functionality-Fail message.
48 * Cleanup:
49 * Print errno log and/or timing stats if options given
50 * Delete the temporary directory created.
51 *
52 * Usage: <for command-line>
53 * msync02 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
54 * where, -c n : Run n copies concurrently.
55 * -f : Turn off functionality Testing.
56 * -i n : Execute test n times.
57 * -I x : Execute test for x seconds.
58 * -P x : Pause for x seconds between iterations.
59 * -t : Turn on syscall timing.
60 *
61 * HISTORY
62 * 07/2001 Ported by Wayne Boyer
63 *
64 * RESTRICTIONS:
65 * None.
66 */
67#include <errno.h>
68#include <unistd.h>
69#include <fcntl.h>
70#include <sys/mman.h>
71
72#include "test.h"
plars865695b2001-08-27 22:15:12 +000073
74#define TEMPFILE "msync_file"
75#define BUF_SIZE 256
76
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020077char *TCID = "msync02";
78int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000079
80char *addr; /* addr of memory mapped region */
81size_t page_sz; /* system page size */
82int fildes; /* file descriptor for tempfile */
subrata_modak56207ce2009-03-23 13:35:39 +000083char write_buf[10] = "Testing"; /* buffer to hold data to be written */
plars865695b2001-08-27 22:15:12 +000084
85void setup(); /* Main setup function of test */
86void cleanup(); /* cleanup function for the test */
87
subrata_modak56207ce2009-03-23 13:35:39 +000088int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000089{
subrata_modakbdbaec52009-02-26 12:14:51 +000090
Garrett Cooper56abfc32010-12-18 20:22:53 -080091 setup();
plars865695b2001-08-27 22:15:12 +000092
Caspar Zhangd59a6592013-03-07 14:59:12 +080093 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000094
Garrett Cooper56abfc32010-12-18 20:22:53 -080095 TEST(msync(addr, page_sz, MS_INVALIDATE));
plars865695b2001-08-27 22:15:12 +000096
Garrett Cooper56abfc32010-12-18 20:22:53 -080097 if (TEST_RETURN == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +080098 tst_resm(TFAIL | TTERRNO, "msync failed");
99 else if (memcmp(addr + 100, write_buf, strlen(write_buf)) != 0)
100 tst_resm(TFAIL, "memory region contains invalid data");
Garrett Cooper56abfc32010-12-18 20:22:53 -0800101 else
Wanlong Gao354ebb42012-12-07 10:10:04 +0800102 tst_resm(TPASS, "Functionality of msync successful");
plars865695b2001-08-27 22:15:12 +0000103
Garrett Cooper56abfc32010-12-18 20:22:53 -0800104 cleanup();
plars865695b2001-08-27 22:15:12 +0000105
Garrett Cooper56abfc32010-12-18 20:22:53 -0800106 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800107}
plars865695b2001-08-27 22:15:12 +0000108
Mike Frysingerc57fba52014-04-09 18:56:30 -0400109void setup(void)
plars865695b2001-08-27 22:15:12 +0000110{
111 int c_total = 0, nwrite = 0; /* no. of bytes to be written */
112 char tst_buf[BUF_SIZE];
113
plars865695b2001-08-27 22:15:12 +0000114 tst_sig(NOFORK, DEF_HANDLER, cleanup);
115
plars865695b2001-08-27 22:15:12 +0000116 TEST_PAUSE;
117
Garrett Cooper56abfc32010-12-18 20:22:53 -0800118 if ((page_sz = getpagesize()) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800119 tst_brkm(TBROK | TERRNO, NULL, "getpagesize failed");
Garrett Cooper56abfc32010-12-18 20:22:53 -0800120
plars865695b2001-08-27 22:15:12 +0000121 tst_tmpdir();
122
Wanlong Gao354ebb42012-12-07 10:10:04 +0800123 if ((fildes = open(TEMPFILE, O_RDWR | O_CREAT, 0666)) < 0)
124 tst_brkm(TBROK | TERRNO, cleanup, "open failed");
plars865695b2001-08-27 22:15:12 +0000125
126 /* Write one page size of char data into temporary file */
127 while (c_total < page_sz) {
Garrett Cooper56abfc32010-12-18 20:22:53 -0800128 if ((nwrite = write(fildes, tst_buf, sizeof(tst_buf))) <= 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800129 tst_brkm(TBROK | TERRNO, cleanup, "write failed");
Garrett Cooper56abfc32010-12-18 20:22:53 -0800130 else
plars865695b2001-08-27 22:15:12 +0000131 c_total += nwrite;
plars865695b2001-08-27 22:15:12 +0000132 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000133
subrata_modak56207ce2009-03-23 13:35:39 +0000134 addr = mmap(0, page_sz, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED,
plars865695b2001-08-27 22:15:12 +0000135 fildes, 0);
136
Garrett Cooper56abfc32010-12-18 20:22:53 -0800137 if (addr == MAP_FAILED)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800138 tst_brkm(TBROK | TERRNO, cleanup, "mmap failed");
plars865695b2001-08-27 22:15:12 +0000139
140 /* Again, Seek to the specified byte offset (100) position */
Garrett Cooper56abfc32010-12-18 20:22:53 -0800141 if (lseek(fildes, 100, SEEK_SET) != 100)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800142 tst_brkm(TBROK | TERRNO, cleanup, "lseek failed");
plars865695b2001-08-27 22:15:12 +0000143
subrata_modak56207ce2009-03-23 13:35:39 +0000144 /* Write the string in write_buf at the 100 byte offset */
Garrett Cooper56abfc32010-12-18 20:22:53 -0800145 if (write(fildes, write_buf, strlen(write_buf)) != strlen(write_buf))
Wanlong Gao354ebb42012-12-07 10:10:04 +0800146 tst_brkm(TBROK | TERRNO, cleanup, "write failed");
plars865695b2001-08-27 22:15:12 +0000147}
148
Mike Frysingerc57fba52014-04-09 18:56:30 -0400149void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000150{
Garrett Cooper56abfc32010-12-18 20:22:53 -0800151 if (munmap(addr, page_sz) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800152 tst_resm(TBROK | TERRNO, "munmap failed");
plars865695b2001-08-27 22:15:12 +0000153
154 /* Close the temporary file */
Garrett Cooper56abfc32010-12-18 20:22:53 -0800155 if (close(fildes) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800156 tst_resm(TWARN | TERRNO, "close failed");
plars865695b2001-08-27 22:15:12 +0000157
plars865695b2001-08-27 22:15:12 +0000158 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700159}