plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 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 Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 41 | * Log the errno and Issue a FAIL message. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 42 | * Otherwise, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 43 | * Verify the Functionality of system call |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 44 | * if successful, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 45 | * Issue Functionality-Pass message. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 46 | * 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" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 73 | |
| 74 | #define TEMPFILE "msync_file" |
| 75 | #define BUF_SIZE 256 |
| 76 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 77 | char *TCID = "msync02"; |
| 78 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 79 | |
| 80 | char *addr; /* addr of memory mapped region */ |
| 81 | size_t page_sz; /* system page size */ |
| 82 | int fildes; /* file descriptor for tempfile */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 83 | char write_buf[10] = "Testing"; /* buffer to hold data to be written */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | |
| 85 | void setup(); /* Main setup function of test */ |
| 86 | void cleanup(); /* cleanup function for the test */ |
| 87 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 88 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 89 | { |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 90 | |
Garrett Cooper | 56abfc3 | 2010-12-18 20:22:53 -0800 | [diff] [blame] | 91 | setup(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 92 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 93 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 94 | |
Garrett Cooper | 56abfc3 | 2010-12-18 20:22:53 -0800 | [diff] [blame] | 95 | TEST(msync(addr, page_sz, MS_INVALIDATE)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 96 | |
Garrett Cooper | 56abfc3 | 2010-12-18 20:22:53 -0800 | [diff] [blame] | 97 | if (TEST_RETURN == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 98 | 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 Cooper | 56abfc3 | 2010-12-18 20:22:53 -0800 | [diff] [blame] | 101 | else |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 102 | tst_resm(TPASS, "Functionality of msync successful"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | |
Garrett Cooper | 56abfc3 | 2010-12-18 20:22:53 -0800 | [diff] [blame] | 104 | cleanup(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | |
Garrett Cooper | 56abfc3 | 2010-12-18 20:22:53 -0800 | [diff] [blame] | 106 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 107 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 109 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 110 | { |
| 111 | int c_total = 0, nwrite = 0; /* no. of bytes to be written */ |
| 112 | char tst_buf[BUF_SIZE]; |
| 113 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 114 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 115 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 116 | TEST_PAUSE; |
| 117 | |
Garrett Cooper | 56abfc3 | 2010-12-18 20:22:53 -0800 | [diff] [blame] | 118 | if ((page_sz = getpagesize()) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 119 | tst_brkm(TBROK | TERRNO, NULL, "getpagesize failed"); |
Garrett Cooper | 56abfc3 | 2010-12-18 20:22:53 -0800 | [diff] [blame] | 120 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 121 | tst_tmpdir(); |
| 122 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 123 | if ((fildes = open(TEMPFILE, O_RDWR | O_CREAT, 0666)) < 0) |
| 124 | tst_brkm(TBROK | TERRNO, cleanup, "open failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 125 | |
| 126 | /* Write one page size of char data into temporary file */ |
| 127 | while (c_total < page_sz) { |
Garrett Cooper | 56abfc3 | 2010-12-18 20:22:53 -0800 | [diff] [blame] | 128 | if ((nwrite = write(fildes, tst_buf, sizeof(tst_buf))) <= 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 129 | tst_brkm(TBROK | TERRNO, cleanup, "write failed"); |
Garrett Cooper | 56abfc3 | 2010-12-18 20:22:53 -0800 | [diff] [blame] | 130 | else |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 131 | c_total += nwrite; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 132 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 133 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 134 | addr = mmap(0, page_sz, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 135 | fildes, 0); |
| 136 | |
Garrett Cooper | 56abfc3 | 2010-12-18 20:22:53 -0800 | [diff] [blame] | 137 | if (addr == MAP_FAILED) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 138 | tst_brkm(TBROK | TERRNO, cleanup, "mmap failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 139 | |
| 140 | /* Again, Seek to the specified byte offset (100) position */ |
Garrett Cooper | 56abfc3 | 2010-12-18 20:22:53 -0800 | [diff] [blame] | 141 | if (lseek(fildes, 100, SEEK_SET) != 100) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 142 | tst_brkm(TBROK | TERRNO, cleanup, "lseek failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 143 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 144 | /* Write the string in write_buf at the 100 byte offset */ |
Garrett Cooper | 56abfc3 | 2010-12-18 20:22:53 -0800 | [diff] [blame] | 145 | if (write(fildes, write_buf, strlen(write_buf)) != strlen(write_buf)) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 146 | tst_brkm(TBROK | TERRNO, cleanup, "write failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 149 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 150 | { |
Garrett Cooper | 56abfc3 | 2010-12-18 20:22:53 -0800 | [diff] [blame] | 151 | if (munmap(addr, page_sz) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 152 | tst_resm(TBROK | TERRNO, "munmap failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 153 | |
| 154 | /* Close the temporary file */ |
Garrett Cooper | 56abfc3 | 2010-12-18 20:22:53 -0800 | [diff] [blame] | 155 | if (close(fildes) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 156 | tst_resm(TWARN | TERRNO, "close failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 157 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 158 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 159 | } |