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