blob: c0a7ed91b3fa23267c49bf2da9cab61a6709ed62 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
Xiaoguang Wang417a0522014-02-21 11:03:09 +08002 * Copyright (c) International Business Machines Corp., 2001
3 * Copyright (c) 2014 Fujitsu Ltd.
4 * Author: Xiaoguang Wang <wangxg.fnst@cn.fujitsu.com>
plars865695b2001-08-27 22:15:12 +00005 *
Xiaoguang Wang417a0522014-02-21 11:03:09 +08006 * 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.
plars865695b2001-08-27 22:15:12 +00009 *
Xiaoguang Wang417a0522014-02-21 11:03:09 +080010 * 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.
plars865695b2001-08-27 22:15:12 +000013 *
Xiaoguang Wang417a0522014-02-21 11:03:09 +080014 * 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.
plars865695b2001-08-27 22:15:12 +000017 */
18
19/*
Xiaoguang Wang417a0522014-02-21 11:03:09 +080020 * 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.
plars865695b2001-08-27 22:15:12 +000031 */
Xiaoguang Wang417a0522014-02-21 11:03:09 +080032
33#include <stdio.h>
plars865695b2001-08-27 22:15:12 +000034#include <errno.h>
35#include <unistd.h>
Xiaoguang Wang417a0522014-02-21 11:03:09 +080036#include <fcntl.h>
37#include <string.h>
38#include <signal.h>
39#include <sys/types.h>
40#include <sys/stat.h>
plars865695b2001-08-27 22:15:12 +000041#include <sys/mman.h>
Xiaoguang Wang417a0522014-02-21 11:03:09 +080042#include <sys/mount.h>
43#include <pwd.h>
robbiewb897fbf2002-09-04 16:34:14 +000044#include <sys/resource.h>
plars865695b2001-08-27 22:15:12 +000045
46#include "test.h"
Xiaoguang Wang417a0522014-02-21 11:03:09 +080047#include "safe_macros.h"
48
49#define INV_SYNC -1
50#define TEMPFILE "msync_file"
51#define BUF_SIZE 256
52
53static void setup(void);
54static void cleanup(void);
55
56static int fd;
57static char *addr1;
58static char *addr2;
59static char *addr3;
60
61#if !defined(UCLINUX)
62static char *addr4;
63#endif
64
65static size_t page_sz;
66
67static 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
82static void msync_verify(struct test_case_t *tc);
plars865695b2001-08-27 22:15:12 +000083
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020084char *TCID = "msync03";
Xiaoguang Wang417a0522014-02-21 11:03:09 +080085int TST_TOTAL = ARRAY_SIZE(test_cases);
plars865695b2001-08-27 22:15:12 +000086
subrata_modak56207ce2009-03-23 13:35:39 +000087int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000088{
Xiaoguang Wang417a0522014-02-21 11:03:09 +080089 int i, lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020090 const char *msg;
subrata_modakbdbaec52009-02-26 12:14:51 +000091
Xiaoguang Wang417a0522014-02-21 11:03:09 +080092 msg = parse_opts(ac, av, NULL, NULL);
93 if (msg != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080094 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000095
plars865695b2001-08-27 22:15:12 +000096 setup();
97
plars865695b2001-08-27 22:15:12 +000098 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080099 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000100
Xiaoguang Wang417a0522014-02-21 11:03:09 +0800101 for (i = 0; i < TST_TOTAL; i++)
102 msync_verify(&test_cases[i]);
Garrett Cooper2c282152010-12-16 00:55:50 -0800103 }
plars865695b2001-08-27 22:15:12 +0000104
plars865695b2001-08-27 22:15:12 +0000105 cleanup();
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
Xiaoguang Wang417a0522014-02-21 11:03:09 +0800109static void setup(void)
plars865695b2001-08-27 22:15:12 +0000110{
Xiaoguang Wang417a0522014-02-21 11:03:09 +0800111 size_t nwrite = 0;
112 char write_buf[BUF_SIZE];
113 struct rlimit rl;
plars865695b2001-08-27 22:15:12 +0000114
plars865695b2001-08-27 22:15:12 +0000115 tst_sig(NOFORK, DEF_HANDLER, cleanup);
116
Xiaoguang Wang417a0522014-02-21 11:03:09 +0800117 tst_tmpdir();
118
plars865695b2001-08-27 22:15:12 +0000119 TEST_PAUSE;
120
Xiaoguang Wang417a0522014-02-21 11:03:09 +0800121 page_sz = (size_t)sysconf(_SC_PAGESIZE);
plars865695b2001-08-27 22:15:12 +0000122
Xiaoguang Wang417a0522014-02-21 11:03:09 +0800123 fd = SAFE_OPEN(cleanup, TEMPFILE, O_RDWR | O_CREAT, 0666);
plars865695b2001-08-27 22:15:12 +0000124
Xiaoguang Wang417a0522014-02-21 11:03:09 +0800125 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
plars865695b2001-08-27 22:15:12 +0000146}
147
Xiaoguang Wang417a0522014-02-21 11:03:09 +0800148static 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 Wang417a0522014-02-21 11:03:09 +0800156 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
166static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000167{
Xiaoguang Wang417a0522014-02-21 11:03:09 +0800168 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 Dearmanec6edca2012-10-17 19:54:01 -0700175}