plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
Xiaoguang Wang | 417a052 | 2014-02-21 11:03:09 +0800 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2001 |
| 3 | * Copyright (c) 2014 Fujitsu Ltd. |
| 4 | * Author: Xiaoguang Wang <wangxg.fnst@cn.fujitsu.com> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 5 | * |
Xiaoguang Wang | 417a052 | 2014-02-21 11:03:09 +0800 | [diff] [blame] | 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of version 2 of the GNU General Public License as |
| 8 | * published by the Free Software Foundation. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 9 | * |
Xiaoguang Wang | 417a052 | 2014-02-21 11:03:09 +0800 | [diff] [blame] | 10 | * This program is distributed in the hope that it would be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 13 | * |
Xiaoguang Wang | 417a052 | 2014-02-21 11:03:09 +0800 | [diff] [blame] | 14 | * You should have received a copy of the GNU General Public License along |
| 15 | * with this program; if not, write the Free Software Foundation, Inc., |
| 16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | /* |
Xiaoguang Wang | 417a052 | 2014-02-21 11:03:09 +0800 | [diff] [blame] | 20 | * Description: |
| 21 | * Verify that, |
| 22 | * 1. msync() fails with -1 return value and sets errno to EBUSY |
| 23 | * if MS_INVALIDATE was specified in flags, and a memory lock |
| 24 | * exists for the specified address range. |
| 25 | * 2. msync() fails with -1 return value and sets errno to EINVAL |
| 26 | * if addr is not a multiple of PAGESIZE; or any bit other than |
| 27 | * MS_ASYNC | MS_INVALIDATE | MS_SYNC is set in flags; or both |
| 28 | * MS_SYNC and MS_ASYNC are set in flags. |
| 29 | * 3. msync() fails with -1 return value and sets errno to ENOMEM |
| 30 | * if the indicated memory (or part of it) was not mapped. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 31 | */ |
Xiaoguang Wang | 417a052 | 2014-02-21 11:03:09 +0800 | [diff] [blame] | 32 | |
| 33 | #include <stdio.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 34 | #include <errno.h> |
| 35 | #include <unistd.h> |
Xiaoguang Wang | 417a052 | 2014-02-21 11:03:09 +0800 | [diff] [blame] | 36 | #include <fcntl.h> |
| 37 | #include <string.h> |
| 38 | #include <signal.h> |
| 39 | #include <sys/types.h> |
| 40 | #include <sys/stat.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 41 | #include <sys/mman.h> |
Xiaoguang Wang | 417a052 | 2014-02-21 11:03:09 +0800 | [diff] [blame] | 42 | #include <sys/mount.h> |
| 43 | #include <pwd.h> |
robbiew | b897fbf | 2002-09-04 16:34:14 +0000 | [diff] [blame] | 44 | #include <sys/resource.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 45 | |
| 46 | #include "test.h" |
Xiaoguang Wang | 417a052 | 2014-02-21 11:03:09 +0800 | [diff] [blame] | 47 | #include "safe_macros.h" |
| 48 | |
| 49 | #define INV_SYNC -1 |
| 50 | #define TEMPFILE "msync_file" |
| 51 | #define BUF_SIZE 256 |
| 52 | |
| 53 | static void setup(void); |
| 54 | static void cleanup(void); |
| 55 | |
| 56 | static int fd; |
| 57 | static char *addr1; |
| 58 | static char *addr2; |
| 59 | static char *addr3; |
| 60 | |
| 61 | #if !defined(UCLINUX) |
| 62 | static char *addr4; |
| 63 | #endif |
| 64 | |
| 65 | static size_t page_sz; |
| 66 | |
| 67 | static struct test_case_t { |
| 68 | char **addr; |
| 69 | int flags; |
| 70 | int exp_errno; |
| 71 | } test_cases[] = { |
| 72 | { &addr1, MS_INVALIDATE, EBUSY }, |
| 73 | { &addr1, MS_ASYNC | MS_SYNC, EINVAL }, |
| 74 | { &addr1, INV_SYNC, EINVAL }, |
| 75 | { &addr2, MS_SYNC, EINVAL }, |
| 76 | { &addr3, MS_SYNC, EINVAL }, |
| 77 | #if !defined(UCLINUX) |
| 78 | { &addr4, MS_SYNC, ENOMEM }, |
| 79 | #endif |
| 80 | }; |
| 81 | |
| 82 | static void msync_verify(struct test_case_t *tc); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 83 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 84 | char *TCID = "msync03"; |
Xiaoguang Wang | 417a052 | 2014-02-21 11:03:09 +0800 | [diff] [blame] | 85 | int TST_TOTAL = ARRAY_SIZE(test_cases); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 86 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 87 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 88 | { |
Xiaoguang Wang | 417a052 | 2014-02-21 11:03:09 +0800 | [diff] [blame] | 89 | int i, lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 90 | const char *msg; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 91 | |
Xiaoguang Wang | 417a052 | 2014-02-21 11:03:09 +0800 | [diff] [blame] | 92 | msg = parse_opts(ac, av, NULL, NULL); |
| 93 | if (msg != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 94 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 96 | setup(); |
| 97 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 99 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 100 | |
Xiaoguang Wang | 417a052 | 2014-02-21 11:03:09 +0800 | [diff] [blame] | 101 | for (i = 0; i < TST_TOTAL; i++) |
| 102 | msync_verify(&test_cases[i]); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 103 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 104 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | cleanup(); |
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 | |
Xiaoguang Wang | 417a052 | 2014-02-21 11:03:09 +0800 | [diff] [blame] | 109 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 110 | { |
Xiaoguang Wang | 417a052 | 2014-02-21 11:03:09 +0800 | [diff] [blame] | 111 | size_t nwrite = 0; |
| 112 | char write_buf[BUF_SIZE]; |
| 113 | struct rlimit rl; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 114 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 116 | |
Xiaoguang Wang | 417a052 | 2014-02-21 11:03:09 +0800 | [diff] [blame] | 117 | tst_tmpdir(); |
| 118 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 119 | TEST_PAUSE; |
| 120 | |
Xiaoguang Wang | 417a052 | 2014-02-21 11:03:09 +0800 | [diff] [blame] | 121 | page_sz = (size_t)sysconf(_SC_PAGESIZE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 122 | |
Xiaoguang Wang | 417a052 | 2014-02-21 11:03:09 +0800 | [diff] [blame] | 123 | fd = SAFE_OPEN(cleanup, TEMPFILE, O_RDWR | O_CREAT, 0666); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 124 | |
Xiaoguang Wang | 417a052 | 2014-02-21 11:03:09 +0800 | [diff] [blame] | 125 | memset(write_buf, 'a', BUF_SIZE); |
| 126 | while (nwrite < page_sz) { |
| 127 | SAFE_WRITE(cleanup, 1, fd, write_buf, BUF_SIZE); |
| 128 | nwrite += BUF_SIZE; |
| 129 | } |
| 130 | |
| 131 | addr1 = SAFE_MMAP(cleanup, 0, page_sz, PROT_READ | PROT_WRITE, |
| 132 | MAP_SHARED | MAP_LOCKED, fd, 0); |
| 133 | |
| 134 | /* addr2 is not a multiple of PAGESIZE */ |
| 135 | addr2 = addr1 + 1; |
| 136 | |
| 137 | /* addr3 is outside the address space of the process */ |
| 138 | if (getrlimit(RLIMIT_DATA, &rl) < 0) |
| 139 | tst_brkm(TBROK | TERRNO, NULL, "getrlimit failed"); |
| 140 | addr3 = (char *)rl.rlim_max; |
| 141 | |
| 142 | #if !defined(UCLINUX) |
| 143 | /* memory pointed to by addr4 was not mapped */ |
| 144 | addr4 = get_high_address(); |
| 145 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Xiaoguang Wang | 417a052 | 2014-02-21 11:03:09 +0800 | [diff] [blame] | 148 | static void msync_verify(struct test_case_t *tc) |
| 149 | { |
| 150 | TEST(msync(*(tc->addr), page_sz, tc->flags)); |
| 151 | if (TEST_RETURN != -1) { |
| 152 | tst_resm(TFAIL, "msync succeeded unexpectedly"); |
| 153 | return; |
| 154 | } |
| 155 | |
Xiaoguang Wang | 417a052 | 2014-02-21 11:03:09 +0800 | [diff] [blame] | 156 | if (TEST_ERRNO == tc->exp_errno) { |
| 157 | tst_resm(TPASS | TTERRNO, "msync failed as expected"); |
| 158 | } else { |
| 159 | tst_resm(TFAIL | TTERRNO, |
| 160 | "msync failed unexpectedly; expected: " |
| 161 | "%d - %s", tc->exp_errno, |
| 162 | strerror(tc->exp_errno)); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 167 | { |
Xiaoguang Wang | 417a052 | 2014-02-21 11:03:09 +0800 | [diff] [blame] | 168 | if (addr1 && munmap(addr1, page_sz) < 0) |
| 169 | tst_resm(TWARN | TERRNO, "munmap() failed"); |
| 170 | |
| 171 | if (fd > 0 && close(fd) < 0) |
| 172 | tst_resm(TWARN | TERRNO, "close() failed"); |
| 173 | |
| 174 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 175 | } |