mridge | 961f230 | 2004-08-24 20:32:21 +0000 | [diff] [blame] | 1 | /* |
Caspar Zhang | 0101f63 | 2013-03-07 14:24:26 +0800 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2004 |
| 3 | * Copyright (c) Linux Test Project, 2013 |
| 4 | * |
mridge | 961f230 | 2004-08-24 20:32:21 +0000 | [diff] [blame] | 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 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_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 19 | |
Caspar Zhang | 0101f63 | 2013-03-07 14:24:26 +0800 | [diff] [blame] | 20 | /* |
| 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 | */ |
mridge | 961f230 | 2004-08-24 20:32:21 +0000 | [diff] [blame] | 25 | |
Caspar Zhang | 0101f63 | 2013-03-07 14:24:26 +0800 | [diff] [blame] | 26 | #include <sys/types.h> |
| 27 | #include <sys/mman.h> |
| 28 | #include <sys/stat.h> |
| 29 | #include <errno.h> |
| 30 | #include <fcntl.h> |
mridge | 961f230 | 2004-08-24 20:32:21 +0000 | [diff] [blame] | 31 | #include <stdio.h> |
| 32 | #include <string.h> |
mridge | 961f230 | 2004-08-24 20:32:21 +0000 | [diff] [blame] | 33 | #include <unistd.h> |
mridge | 961f230 | 2004-08-24 20:32:21 +0000 | [diff] [blame] | 34 | |
| 35 | #include "test.h" |
mridge | 961f230 | 2004-08-24 20:32:21 +0000 | [diff] [blame] | 36 | |
Wanlong Gao | e6f0e85 | 2012-06-18 19:59:48 +0800 | [diff] [blame] | 37 | static void setup(void); |
| 38 | static void cleanup(void); |
| 39 | static void check_and_print(char *advice); |
mridge | 961f230 | 2004-08-24 20:32:21 +0000 | [diff] [blame] | 40 | |
Wanlong Gao | e6f0e85 | 2012-06-18 19:59:48 +0800 | [diff] [blame] | 41 | char *TCID = "madvise01"; |
| 42 | int TST_TOTAL = 5; |
mridge | 961f230 | 2004-08-24 20:32:21 +0000 | [diff] [blame] | 43 | |
| 44 | int main(int argc, char *argv[]) |
| 45 | { |
| 46 | int lc, fd; |
Wanlong Gao | e6f0e85 | 2012-06-18 19:59:48 +0800 | [diff] [blame] | 47 | int i = 0; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 48 | char *file = NULL; |
mridge | 961f230 | 2004-08-24 20:32:21 +0000 | [diff] [blame] | 49 | struct stat stat; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 50 | |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 51 | const char *msg = NULL; |
mridge | 961f230 | 2004-08-24 20:32:21 +0000 | [diff] [blame] | 52 | char filename[64]; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 53 | char *progname = NULL; |
Wanlong Gao | e6f0e85 | 2012-06-18 19:59:48 +0800 | [diff] [blame] | 54 | char *str_for_file = "abcdefghijklmnopqrstuvwxyz12345\n"; |
mridge | 961f230 | 2004-08-24 20:32:21 +0000 | [diff] [blame] | 55 | |
Wanlong Gao | e6f0e85 | 2012-06-18 19:59:48 +0800 | [diff] [blame] | 56 | msg = parse_opts(argc, argv, NULL, NULL); |
| 57 | if (msg) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 58 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 59 | |
mridge | 961f230 | 2004-08-24 20:32:21 +0000 | [diff] [blame] | 60 | setup(); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 61 | |
mridge | 961f230 | 2004-08-24 20:32:21 +0000 | [diff] [blame] | 62 | progname = *argv; |
| 63 | sprintf(filename, "%s-out.%d", progname, getpid()); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 64 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 65 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 66 | tst_count = 0; |
mridge | 961f230 | 2004-08-24 20:32:21 +0000 | [diff] [blame] | 67 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 68 | fd = open(filename, O_RDWR | O_CREAT, 0664); |
Wanlong Gao | e6f0e85 | 2012-06-18 19:59:48 +0800 | [diff] [blame] | 69 | if (fd < 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 70 | tst_brkm(TBROK | TERRNO, cleanup, "open failed"); |
Caspar Zhang | 0101f63 | 2013-03-07 14:24:26 +0800 | [diff] [blame] | 71 | #ifdef DEBUG |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 72 | tst_resm(TINFO, "filename = %s opened successfully", filename); |
mridge | 961f230 | 2004-08-24 20:32:21 +0000 | [diff] [blame] | 73 | #endif |
| 74 | |
| 75 | /* Writing 40 KB of random data into this file |
| 76 | [32 * 1280 = 40960] */ |
Garrett Cooper | 838a7ed | 2010-12-19 06:33:24 -0800 | [diff] [blame] | 77 | for (i = 0; i < 1280; i++) |
| 78 | if (write(fd, str_for_file, strlen(str_for_file)) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 79 | tst_brkm(TBROK | TERRNO, cleanup, |
| 80 | "write failed"); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 81 | |
Garrett Cooper | 838a7ed | 2010-12-19 06:33:24 -0800 | [diff] [blame] | 82 | if (fstat(fd, &stat) == -1) |
| 83 | tst_brkm(TBROK, cleanup, "fstat failed"); |
mridge | 961f230 | 2004-08-24 20:32:21 +0000 | [diff] [blame] | 84 | |
| 85 | /* Map the input file into memory */ |
Wanlong Gao | e6f0e85 | 2012-06-18 19:59:48 +0800 | [diff] [blame] | 86 | file = mmap(NULL, stat.st_size, PROT_READ, MAP_SHARED, fd, 0); |
| 87 | if (file == MAP_FAILED) |
Garrett Cooper | 838a7ed | 2010-12-19 06:33:24 -0800 | [diff] [blame] | 88 | tst_brkm(TBROK, cleanup, "mmap failed"); |
mridge | 961f230 | 2004-08-24 20:32:21 +0000 | [diff] [blame] | 89 | |
Garrett Cooper | 838a7ed | 2010-12-19 06:33:24 -0800 | [diff] [blame] | 90 | /* (1) Test case for MADV_NORMAL */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 91 | TEST(madvise(file, stat.st_size, MADV_NORMAL)); |
mridge | 961f230 | 2004-08-24 20:32:21 +0000 | [diff] [blame] | 92 | check_and_print("MADV_NORMAL"); |
| 93 | |
Garrett Cooper | 838a7ed | 2010-12-19 06:33:24 -0800 | [diff] [blame] | 94 | /* (2) Test case for MADV_RANDOM */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 95 | TEST(madvise(file, stat.st_size, MADV_RANDOM)); |
mridge | 961f230 | 2004-08-24 20:32:21 +0000 | [diff] [blame] | 96 | check_and_print("MADV_RANDOM"); |
| 97 | |
Garrett Cooper | 838a7ed | 2010-12-19 06:33:24 -0800 | [diff] [blame] | 98 | /* (3) Test case for MADV_SEQUENTIAL */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 99 | TEST(madvise(file, stat.st_size, MADV_SEQUENTIAL)); |
mridge | 961f230 | 2004-08-24 20:32:21 +0000 | [diff] [blame] | 100 | check_and_print("MADV_SEQUENTIAL"); |
| 101 | |
Garrett Cooper | 838a7ed | 2010-12-19 06:33:24 -0800 | [diff] [blame] | 102 | /* (4) Test case for MADV_WILLNEED */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 103 | TEST(madvise(file, stat.st_size, MADV_WILLNEED)); |
mridge | 961f230 | 2004-08-24 20:32:21 +0000 | [diff] [blame] | 104 | check_and_print("MADV_WILLNEED"); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 105 | |
Garrett Cooper | 838a7ed | 2010-12-19 06:33:24 -0800 | [diff] [blame] | 106 | /* (5) Test case for MADV_DONTNEED */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 107 | TEST(madvise(file, stat.st_size, MADV_DONTNEED)); |
mridge | 961f230 | 2004-08-24 20:32:21 +0000 | [diff] [blame] | 108 | check_and_print("MADV_DONTNEED"); |
| 109 | |
Garrett Cooper | 838a7ed | 2010-12-19 06:33:24 -0800 | [diff] [blame] | 110 | if (munmap(file, stat.st_size) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 111 | tst_brkm(TBROK | TERRNO, cleanup, "munmap failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 112 | |
mridge | 961f230 | 2004-08-24 20:32:21 +0000 | [diff] [blame] | 113 | close(fd); |
| 114 | } |
| 115 | |
| 116 | cleanup(); |
Garrett Cooper | 838a7ed | 2010-12-19 06:33:24 -0800 | [diff] [blame] | 117 | tst_exit(); |
mridge | 961f230 | 2004-08-24 20:32:21 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Wanlong Gao | e6f0e85 | 2012-06-18 19:59:48 +0800 | [diff] [blame] | 120 | static void setup(void) |
mridge | 961f230 | 2004-08-24 20:32:21 +0000 | [diff] [blame] | 121 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 122 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 123 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
mridge | 961f230 | 2004-08-24 20:32:21 +0000 | [diff] [blame] | 124 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 125 | TEST_PAUSE; |
mridge | 961f230 | 2004-08-24 20:32:21 +0000 | [diff] [blame] | 126 | |
mridge | 961f230 | 2004-08-24 20:32:21 +0000 | [diff] [blame] | 127 | tst_tmpdir(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 128 | } |
mridge | 961f230 | 2004-08-24 20:32:21 +0000 | [diff] [blame] | 129 | |
Wanlong Gao | e6f0e85 | 2012-06-18 19:59:48 +0800 | [diff] [blame] | 130 | static void cleanup(void) |
mridge | 961f230 | 2004-08-24 20:32:21 +0000 | [diff] [blame] | 131 | { |
mridge | 961f230 | 2004-08-24 20:32:21 +0000 | [diff] [blame] | 132 | tst_rmdir(); |
| 133 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 134 | } |
mridge | 961f230 | 2004-08-24 20:32:21 +0000 | [diff] [blame] | 135 | |
Wanlong Gao | e6f0e85 | 2012-06-18 19:59:48 +0800 | [diff] [blame] | 136 | static void check_and_print(char *advice) |
mridge | 961f230 | 2004-08-24 20:32:21 +0000 | [diff] [blame] | 137 | { |
Garrett Cooper | 838a7ed | 2010-12-19 06:33:24 -0800 | [diff] [blame] | 138 | if (TEST_RETURN == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 139 | tst_resm(TFAIL | TTERRNO, "madvise test for %s failed", advice); |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 140 | else |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 141 | tst_resm(TPASS, "madvise test for %s PASSED", advice); |
Wanlong Gao | e6f0e85 | 2012-06-18 19:59:48 +0800 | [diff] [blame] | 142 | } |