blob: bd139ec70e5b8132131dbe9dd31df251ce85ad5a [file] [log] [blame]
mridge961f2302004-08-24 20:32:21 +00001/*
Caspar Zhang0101f632013-03-07 14:24:26 +08002 * Copyright (c) International Business Machines Corp., 2004
3 * Copyright (c) Linux Test Project, 2013
4 *
mridge961f2302004-08-24 20:32:21 +00005 * 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 the
13 * GNU Library 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
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
subrata_modak4bb656a2009-02-26 12:02:09 +000019
Caspar Zhang0101f632013-03-07 14:24:26 +080020/*
21 * This is a test case for madvise(2) system call.
22 * It tests madvise(2) with combinations of advice values.
23 * No error should be returned.
24 */
mridge961f2302004-08-24 20:32:21 +000025
Caspar Zhang0101f632013-03-07 14:24:26 +080026#include <sys/types.h>
27#include <sys/mman.h>
28#include <sys/stat.h>
29#include <errno.h>
30#include <fcntl.h>
mridge961f2302004-08-24 20:32:21 +000031#include <stdio.h>
32#include <string.h>
mridge961f2302004-08-24 20:32:21 +000033#include <unistd.h>
mridge961f2302004-08-24 20:32:21 +000034
35#include "test.h"
mridge961f2302004-08-24 20:32:21 +000036
Wanlong Gaoe6f0e852012-06-18 19:59:48 +080037static void setup(void);
38static void cleanup(void);
39static void check_and_print(char *advice);
mridge961f2302004-08-24 20:32:21 +000040
Wanlong Gaoe6f0e852012-06-18 19:59:48 +080041char *TCID = "madvise01";
42int TST_TOTAL = 5;
mridge961f2302004-08-24 20:32:21 +000043
44int main(int argc, char *argv[])
45{
46 int lc, fd;
Wanlong Gaoe6f0e852012-06-18 19:59:48 +080047 int i = 0;
subrata_modak56207ce2009-03-23 13:35:39 +000048 char *file = NULL;
mridge961f2302004-08-24 20:32:21 +000049 struct stat stat;
subrata_modakbdbaec52009-02-26 12:14:51 +000050
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020051 const char *msg = NULL;
mridge961f2302004-08-24 20:32:21 +000052 char filename[64];
subrata_modak56207ce2009-03-23 13:35:39 +000053 char *progname = NULL;
Wanlong Gaoe6f0e852012-06-18 19:59:48 +080054 char *str_for_file = "abcdefghijklmnopqrstuvwxyz12345\n";
mridge961f2302004-08-24 20:32:21 +000055
Wanlong Gaoe6f0e852012-06-18 19:59:48 +080056 msg = parse_opts(argc, argv, NULL, NULL);
57 if (msg)
subrata_modak56207ce2009-03-23 13:35:39 +000058 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
subrata_modakbdbaec52009-02-26 12:14:51 +000059
mridge961f2302004-08-24 20:32:21 +000060 setup();
subrata_modakbdbaec52009-02-26 12:14:51 +000061
mridge961f2302004-08-24 20:32:21 +000062 progname = *argv;
63 sprintf(filename, "%s-out.%d", progname, getpid());
subrata_modakbdbaec52009-02-26 12:14:51 +000064
subrata_modak56207ce2009-03-23 13:35:39 +000065 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080066 tst_count = 0;
mridge961f2302004-08-24 20:32:21 +000067
Wanlong Gao354ebb42012-12-07 10:10:04 +080068 fd = open(filename, O_RDWR | O_CREAT, 0664);
Wanlong Gaoe6f0e852012-06-18 19:59:48 +080069 if (fd < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +080070 tst_brkm(TBROK | TERRNO, cleanup, "open failed");
Caspar Zhang0101f632013-03-07 14:24:26 +080071#ifdef DEBUG
subrata_modak56207ce2009-03-23 13:35:39 +000072 tst_resm(TINFO, "filename = %s opened successfully", filename);
mridge961f2302004-08-24 20:32:21 +000073#endif
74
75 /* Writing 40 KB of random data into this file
76 [32 * 1280 = 40960] */
Garrett Cooper838a7ed2010-12-19 06:33:24 -080077 for (i = 0; i < 1280; i++)
78 if (write(fd, str_for_file, strlen(str_for_file)) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +080079 tst_brkm(TBROK | TERRNO, cleanup,
80 "write failed");
subrata_modakbdbaec52009-02-26 12:14:51 +000081
Garrett Cooper838a7ed2010-12-19 06:33:24 -080082 if (fstat(fd, &stat) == -1)
83 tst_brkm(TBROK, cleanup, "fstat failed");
mridge961f2302004-08-24 20:32:21 +000084
85 /* Map the input file into memory */
Wanlong Gaoe6f0e852012-06-18 19:59:48 +080086 file = mmap(NULL, stat.st_size, PROT_READ, MAP_SHARED, fd, 0);
87 if (file == MAP_FAILED)
Garrett Cooper838a7ed2010-12-19 06:33:24 -080088 tst_brkm(TBROK, cleanup, "mmap failed");
mridge961f2302004-08-24 20:32:21 +000089
Garrett Cooper838a7ed2010-12-19 06:33:24 -080090 /* (1) Test case for MADV_NORMAL */
subrata_modak56207ce2009-03-23 13:35:39 +000091 TEST(madvise(file, stat.st_size, MADV_NORMAL));
mridge961f2302004-08-24 20:32:21 +000092 check_and_print("MADV_NORMAL");
93
Garrett Cooper838a7ed2010-12-19 06:33:24 -080094 /* (2) Test case for MADV_RANDOM */
subrata_modak56207ce2009-03-23 13:35:39 +000095 TEST(madvise(file, stat.st_size, MADV_RANDOM));
mridge961f2302004-08-24 20:32:21 +000096 check_and_print("MADV_RANDOM");
97
Garrett Cooper838a7ed2010-12-19 06:33:24 -080098 /* (3) Test case for MADV_SEQUENTIAL */
subrata_modak56207ce2009-03-23 13:35:39 +000099 TEST(madvise(file, stat.st_size, MADV_SEQUENTIAL));
mridge961f2302004-08-24 20:32:21 +0000100 check_and_print("MADV_SEQUENTIAL");
101
Garrett Cooper838a7ed2010-12-19 06:33:24 -0800102 /* (4) Test case for MADV_WILLNEED */
subrata_modak56207ce2009-03-23 13:35:39 +0000103 TEST(madvise(file, stat.st_size, MADV_WILLNEED));
mridge961f2302004-08-24 20:32:21 +0000104 check_and_print("MADV_WILLNEED");
subrata_modakbdbaec52009-02-26 12:14:51 +0000105
Garrett Cooper838a7ed2010-12-19 06:33:24 -0800106 /* (5) Test case for MADV_DONTNEED */
subrata_modak56207ce2009-03-23 13:35:39 +0000107 TEST(madvise(file, stat.st_size, MADV_DONTNEED));
mridge961f2302004-08-24 20:32:21 +0000108 check_and_print("MADV_DONTNEED");
109
Garrett Cooper838a7ed2010-12-19 06:33:24 -0800110 if (munmap(file, stat.st_size) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800111 tst_brkm(TBROK | TERRNO, cleanup, "munmap failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000112
mridge961f2302004-08-24 20:32:21 +0000113 close(fd);
114 }
115
116 cleanup();
Garrett Cooper838a7ed2010-12-19 06:33:24 -0800117 tst_exit();
mridge961f2302004-08-24 20:32:21 +0000118}
119
Wanlong Gaoe6f0e852012-06-18 19:59:48 +0800120static void setup(void)
mridge961f2302004-08-24 20:32:21 +0000121{
Garrett Cooper2c282152010-12-16 00:55:50 -0800122
subrata_modak56207ce2009-03-23 13:35:39 +0000123 tst_sig(NOFORK, DEF_HANDLER, cleanup);
mridge961f2302004-08-24 20:32:21 +0000124
subrata_modak56207ce2009-03-23 13:35:39 +0000125 TEST_PAUSE;
mridge961f2302004-08-24 20:32:21 +0000126
mridge961f2302004-08-24 20:32:21 +0000127 tst_tmpdir();
Garrett Cooper2c282152010-12-16 00:55:50 -0800128}
mridge961f2302004-08-24 20:32:21 +0000129
Wanlong Gaoe6f0e852012-06-18 19:59:48 +0800130static void cleanup(void)
mridge961f2302004-08-24 20:32:21 +0000131{
mridge961f2302004-08-24 20:32:21 +0000132 tst_rmdir();
133
Garrett Cooper2c282152010-12-16 00:55:50 -0800134}
mridge961f2302004-08-24 20:32:21 +0000135
Wanlong Gaoe6f0e852012-06-18 19:59:48 +0800136static void check_and_print(char *advice)
mridge961f2302004-08-24 20:32:21 +0000137{
Garrett Cooper838a7ed2010-12-19 06:33:24 -0800138 if (TEST_RETURN == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800139 tst_resm(TFAIL | TTERRNO, "madvise test for %s failed", advice);
Cyril Hrubise38b9612014-06-02 17:20:57 +0200140 else
subrata_modak56207ce2009-03-23 13:35:39 +0000141 tst_resm(TPASS, "madvise test for %s PASSED", advice);
Wanlong Gaoe6f0e852012-06-18 19:59:48 +0800142}