blob: db5b92b5d480930439716bd423473c087ecdf23d [file] [log] [blame]
Wanlong Gao49833532012-06-18 20:13:17 +08001/*
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 Gao49833532012-06-18 20:13:17 +080031
32char *TCID = "madvise04";
33
34#ifdef MADV_DONTDUMP
35
Wanlong Gao49833532012-06-18 20:13:17 +080036int TST_TOTAL = 2;
37
38#define BUFFER_SIZE 256
39
40static void setup(void);
41static void cleanup(void);
42static void check_and_print(char *advice);
43
44int main(int argc, char *argv[])
45{
46 int lc, fd;
47 int i;
48 char *file = NULL;
49 struct stat stat;
50
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020051 const char *msg;
Wanlong Gao49833532012-06-18 20:13:17 +080052 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 Zhangd59a6592013-03-07 14:59:12 +080066 tst_count = 0;
Wanlong Gao49833532012-06-18 20:13:17 +080067
68 fd = open(filename, O_RDWR | O_CREAT, 0664);
69 if (fd < 0)
70 tst_brkm(TBROK, cleanup, "open failed");
Caspar Zhang0101f632013-03-07 14:24:26 +080071#ifdef DEBUG
Wanlong Gao49833532012-06-18 20:13:17 +080072 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 Gao354ebb42012-12-07 10:10:04 +080079 tst_brkm(TBROK | TERRNO, cleanup,
80 "write failed");
Wanlong Gao49833532012-06-18 20:13:17 +080081
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 Gao354ebb42012-12-07 10:10:04 +080087 tst_brkm(TBROK | TERRNO, cleanup, "mmap failed");
Wanlong Gao49833532012-06-18 20:13:17 +080088
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 Gao354ebb42012-12-07 10:10:04 +080099 tst_brkm(TBROK | TERRNO, cleanup, "munmap failed");
Wanlong Gao49833532012-06-18 20:13:17 +0800100
101 close(fd);
102 }
103
104 cleanup();
105 tst_exit();
106}
107
108static void setup(void)
109{
110 tst_sig(NOFORK, DEF_HANDLER, cleanup);
111 TEST_PAUSE;
112 tst_tmpdir();
113}
114
115static void cleanup(void)
116{
Wanlong Gao49833532012-06-18 20:13:17 +0800117 tst_rmdir();
118}
119
120static 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 Hrubise38b9612014-06-02 17:20:57 +0200127 } else {
Wanlong Gao49833532012-06-18 20:13:17 +0800128 tst_resm(TPASS, "madvise test for %s PASSED", advice);
129 }
130}
131
132#else
133int 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