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: msync01 |
| 22 | * |
| 23 | * Test Description: |
| 24 | * Verify that, msync() succeeds, when the region to synchronize, is part |
| 25 | * of, or all of a mapped region. |
| 26 | * |
| 27 | * Expected Result: |
| 28 | * msync() should succeed with a return value of 0, and succesfully |
| 29 | * synchronize the memory region. Data read from mapped region should be |
| 30 | * the same as the initialized data. |
| 31 | * |
| 32 | * Algorithm: |
| 33 | * Setup: |
| 34 | * Setup signal handling. |
| 35 | * Create temporary directory. |
| 36 | * Pause for SIGUSR1 if option specified. |
| 37 | * |
| 38 | * Test: |
| 39 | * Loop if the proper options are given. |
| 40 | * Execute system call |
| 41 | * Check return code, if system call failed (return=-1) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 42 | * Log the errno and Issue a FAIL message. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 43 | * Otherwise, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 44 | * Verify the Functionality of system call |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 45 | * if successful, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 46 | * Issue Functionality-Pass message. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 47 | * Otherwise, |
| 48 | * Issue Functionality-Fail message. |
| 49 | * Cleanup: |
| 50 | * Print errno log and/or timing stats if options given |
| 51 | * Delete the temporary directory created. |
| 52 | * |
| 53 | * Usage: <for command-line> |
| 54 | * msync01 [-c n] [-f] [-i n] [-I x] [-P x] [-t] |
| 55 | * where, -c n : Run n copies concurrently. |
| 56 | * -f : Turn off functionality Testing. |
| 57 | * -i n : Execute test n times. |
| 58 | * -I x : Execute test for x seconds. |
| 59 | * -P x : Pause for x seconds between iterations. |
| 60 | * -t : Turn on syscall timing. |
| 61 | * |
| 62 | * HISTORY |
| 63 | * 07/2001 Ported by Wayne Boyer |
| 64 | * |
| 65 | * RESTRICTIONS: |
| 66 | * None. |
| 67 | */ |
| 68 | #include <errno.h> |
| 69 | #include <unistd.h> |
| 70 | #include <fcntl.h> |
| 71 | #include <sys/mman.h> |
| 72 | |
| 73 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 74 | |
| 75 | #define TEMPFILE "msync_file" |
| 76 | #define BUF_SIZE 256 |
| 77 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 78 | char *TCID = "msync01"; |
| 79 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 80 | |
| 81 | char *addr; /* addr of memory mapped region */ |
| 82 | size_t page_sz; /* system page size */ |
| 83 | int fildes; /* file descriptor for tempfile */ |
| 84 | char write_buf[BUF_SIZE]; /* buffer to hold data to be written */ |
| 85 | |
| 86 | void setup(); /* Main setup function of test */ |
| 87 | void cleanup(); /* cleanup function for the test */ |
| 88 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 89 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 90 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 91 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 92 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 93 | char read_buf[BUF_SIZE]; /* buffer to hold data read from file */ |
| 94 | int nread = 0, count, err_flg = 0; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 95 | |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 96 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 97 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 99 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 100 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 101 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | setup(); |
| 104 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | TEST(msync(addr, page_sz, MS_ASYNC)); |
| 106 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 107 | if (TEST_RETURN == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 108 | tst_resm(TFAIL | TERRNO, "msync failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 109 | continue; |
| 110 | } |
| 111 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 112 | if (lseek(fildes, (off_t) 100, SEEK_SET) != 100) |
| 113 | tst_brkm(TBROK | TERRNO, cleanup, |
| 114 | "lseek failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 116 | /* |
| 117 | * Seeking to specified offset. successful. |
| 118 | * Now, read the data (256 bytes) and compare |
| 119 | * them with the expected. |
| 120 | */ |
| 121 | nread = read(fildes, read_buf, sizeof(read_buf)); |
| 122 | if (nread != BUF_SIZE) |
| 123 | tst_brkm(TBROK, cleanup, "read failed"); |
| 124 | else { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 125 | /* |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 126 | * Check whether read data (from mapped |
| 127 | * file) contains the expected data |
| 128 | * which was initialised in the setup. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 129 | */ |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 130 | for (count = 0; count < nread; count++) |
| 131 | if (read_buf[count] != 1) |
| 132 | err_flg++; |
| 133 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 134 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 135 | if (err_flg != 0) |
| 136 | tst_resm(TFAIL, |
| 137 | "data read from file doesn't match"); |
| 138 | else |
| 139 | tst_resm(TPASS, |
| 140 | "Functionality of msync() successful"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 141 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 142 | cleanup(); |
| 143 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 144 | } |
Garrett Cooper | 56abfc3 | 2010-12-18 20:22:53 -0800 | [diff] [blame] | 145 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 146 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 147 | |
| 148 | /* |
| 149 | * setup() - performs all ONE TIME setup for this test. |
| 150 | * |
| 151 | * Get system page size, |
| 152 | * Creat a temporary directory and a file under it used for mapping. |
| 153 | * Write 1 page size char data into file. |
| 154 | * Initialize paged region (256 bytes) from the specified offset pos. |
| 155 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 156 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 157 | { |
| 158 | int c_total = 0, nwrite = 0; /* no. of bytes to be written */ |
| 159 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 160 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 161 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 162 | TEST_PAUSE; |
| 163 | |
Garrett Cooper | 56abfc3 | 2010-12-18 20:22:53 -0800 | [diff] [blame] | 164 | if ((page_sz = getpagesize()) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 165 | tst_brkm(TBROK | TERRNO, NULL, "getpagesize failed"); |
Garrett Cooper | 56abfc3 | 2010-12-18 20:22:53 -0800 | [diff] [blame] | 166 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 167 | tst_tmpdir(); |
| 168 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 169 | if ((fildes = open(TEMPFILE, O_RDWR | O_CREAT, 0666)) < 0) |
| 170 | tst_brkm(TBROK | TERRNO, cleanup, "open failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 171 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 172 | while (c_total < page_sz) { |
| 173 | nwrite = write(fildes, write_buf, sizeof(write_buf)); |
Garrett Cooper | 56abfc3 | 2010-12-18 20:22:53 -0800 | [diff] [blame] | 174 | if (nwrite <= 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 175 | tst_brkm(TBROK | TERRNO, cleanup, "write failed"); |
Garrett Cooper | 56abfc3 | 2010-12-18 20:22:53 -0800 | [diff] [blame] | 176 | else |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 177 | c_total += nwrite; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 178 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 179 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 180 | /* |
| 181 | * Call mmap to map virtual memory (mul. of page size bytes) from the |
| 182 | * beginning of temporary file (offset is 0) into memory. |
| 183 | */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 184 | addr = mmap(0, page_sz, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 185 | fildes, 0); |
| 186 | |
| 187 | /* Check for the return value of mmap() */ |
Garrett Cooper | 56abfc3 | 2010-12-18 20:22:53 -0800 | [diff] [blame] | 188 | if (addr == MAP_FAILED) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 189 | tst_brkm(TBROK | TERRNO, cleanup, "mmap failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 190 | |
| 191 | /* Set 256 bytes, at 100 byte offset in the mapped region */ |
| 192 | memset(addr + 100, 1, 256); |
| 193 | } |
| 194 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 195 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 196 | { |
Garrett Cooper | 56abfc3 | 2010-12-18 20:22:53 -0800 | [diff] [blame] | 197 | if (munmap(addr, page_sz) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 198 | tst_resm(TBROK | TERRNO, "munmap failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 199 | |
Garrett Cooper | 56abfc3 | 2010-12-18 20:22:53 -0800 | [diff] [blame] | 200 | if (close(fildes) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 201 | tst_resm(TWARN | TERRNO, "close failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 202 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 203 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 204 | } |