Wanlong Gao | 4983353 | 2012-06-18 20:13:17 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 FUJITSU LIMITED. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU Library General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <errno.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <unistd.h> |
| 26 | #include <sys/shm.h> |
| 27 | #include <sys/mman.h> |
| 28 | #include <fcntl.h> |
| 29 | |
| 30 | #include "test.h" |
Wanlong Gao | 4983353 | 2012-06-18 20:13:17 +0800 | [diff] [blame] | 31 | |
| 32 | char *TCID = "madvise04"; |
| 33 | |
| 34 | #ifdef MADV_DONTDUMP |
| 35 | |
Wanlong Gao | 4983353 | 2012-06-18 20:13:17 +0800 | [diff] [blame] | 36 | int TST_TOTAL = 2; |
| 37 | |
| 38 | #define BUFFER_SIZE 256 |
| 39 | |
| 40 | static void setup(void); |
| 41 | static void cleanup(void); |
| 42 | static void check_and_print(char *advice); |
| 43 | |
| 44 | int main(int argc, char *argv[]) |
| 45 | { |
| 46 | int lc, fd; |
| 47 | int i; |
| 48 | char *file = NULL; |
| 49 | struct stat stat; |
| 50 | |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 51 | const char *msg; |
Wanlong Gao | 4983353 | 2012-06-18 20:13:17 +0800 | [diff] [blame] | 52 | char filename[64]; |
| 53 | char *progname = NULL; |
| 54 | char *str_for_file = "abcdefghijklmnopqrstuvwxyz12345\n"; |
| 55 | |
| 56 | msg = parse_opts(argc, argv, NULL, NULL); |
| 57 | if (msg) |
| 58 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
| 59 | |
| 60 | setup(); |
| 61 | |
| 62 | progname = *argv; |
| 63 | sprintf(filename, "%s-out.%d", progname, getpid()); |
| 64 | |
| 65 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 66 | tst_count = 0; |
Wanlong Gao | 4983353 | 2012-06-18 20:13:17 +0800 | [diff] [blame] | 67 | |
| 68 | fd = open(filename, O_RDWR | O_CREAT, 0664); |
| 69 | if (fd < 0) |
| 70 | tst_brkm(TBROK, cleanup, "open failed"); |
Caspar Zhang | 0101f63 | 2013-03-07 14:24:26 +0800 | [diff] [blame] | 71 | #ifdef DEBUG |
Wanlong Gao | 4983353 | 2012-06-18 20:13:17 +0800 | [diff] [blame] | 72 | tst_resm(TINFO, "filename = %s opened successfully", filename); |
| 73 | #endif |
| 74 | |
| 75 | /* Writing 40 KB of random data into this file |
| 76 | [32 * 1280 = 40960] */ |
| 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"); |
Wanlong Gao | 4983353 | 2012-06-18 20:13:17 +0800 | [diff] [blame] | 81 | |
| 82 | if (fstat(fd, &stat) == -1) |
| 83 | tst_brkm(TBROK, cleanup, "fstat failed"); |
| 84 | |
| 85 | file = mmap(NULL, stat.st_size, PROT_READ, MAP_SHARED, fd, 0); |
| 86 | if (file == MAP_FAILED) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 87 | tst_brkm(TBROK | TERRNO, cleanup, "mmap failed"); |
Wanlong Gao | 4983353 | 2012-06-18 20:13:17 +0800 | [diff] [blame] | 88 | |
| 89 | /* (1) Test case for MADV_DONTDUMP */ |
| 90 | TEST(madvise(file, stat.st_size, MADV_DONTDUMP)); |
| 91 | check_and_print("MADV_DONTDUMP"); |
| 92 | |
| 93 | /* (2) Test case for MADV_DODUMP */ |
| 94 | TEST(madvise(file, stat.st_size, MADV_DODUMP)); |
| 95 | check_and_print("MADV_DODUMP"); |
| 96 | |
| 97 | /* Finally Unmapping the whole file */ |
| 98 | if (munmap(file, stat.st_size) < 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 99 | tst_brkm(TBROK | TERRNO, cleanup, "munmap failed"); |
Wanlong Gao | 4983353 | 2012-06-18 20:13:17 +0800 | [diff] [blame] | 100 | |
| 101 | close(fd); |
| 102 | } |
| 103 | |
| 104 | cleanup(); |
| 105 | tst_exit(); |
| 106 | } |
| 107 | |
| 108 | static void setup(void) |
| 109 | { |
| 110 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 111 | TEST_PAUSE; |
| 112 | tst_tmpdir(); |
| 113 | } |
| 114 | |
| 115 | static void cleanup(void) |
| 116 | { |
Wanlong Gao | 4983353 | 2012-06-18 20:13:17 +0800 | [diff] [blame] | 117 | tst_rmdir(); |
| 118 | } |
| 119 | |
| 120 | static void check_and_print(char *advice) |
| 121 | { |
| 122 | if (TEST_RETURN == -1) { |
| 123 | tst_resm(TFAIL, |
| 124 | "madvise test for %s failed with " |
| 125 | "return = %ld, errno = %d : %s", |
| 126 | advice, TEST_RETURN, TEST_ERRNO, strerror(TEST_ERRNO)); |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 127 | } else { |
Wanlong Gao | 4983353 | 2012-06-18 20:13:17 +0800 | [diff] [blame] | 128 | tst_resm(TPASS, "madvise test for %s PASSED", advice); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | #else |
| 133 | int main(void) |
| 134 | { |
| 135 | /* Requires kernel version >= 3.4 */ |
| 136 | tst_brkm(TCONF, NULL, |
| 137 | "This system doesn't have required madvise support, " |
| 138 | "MADV_DONTDUMP and MADV_DODUMP were added from 3.4. " |
| 139 | "If your kernel version >= 3.4, maybe you need updating " |
| 140 | "your glibc-headers"); |
| 141 | } |
| 142 | #endif |