blob: d59edac5783f0a13949d5c3c9b7e87d6c80decdf [file] [log] [blame]
subrata_modak9326a852007-11-28 09:55:25 +00001/*
Cyril Hrubisda6be572012-11-28 13:20:30 +01002 * Copyright (c) International Business Machines Corp., 2004
3 * Copyright (c) 2012 Cyril Hrubis <chrubis@suse.cz>
4 *
subrata_modak9326a852007-11-28 09:55:25 +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 */
19
Cyril Hrubisda6be572012-11-28 13:20:30 +010020/*
21 * This is a test case for madvise(2) system call. No error should be returned.
22 */
subrata_modak9326a852007-11-28 09:55:25 +000023
Caspar Zhang0101f632013-03-07 14:24:26 +080024#include <sys/types.h>
25#include <sys/mman.h>
26#include <sys/shm.h>
27#include <sys/stat.h>
28#include <errno.h>
29#include <fcntl.h>
subrata_modak9326a852007-11-28 09:55:25 +000030#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
subrata_modak9326a852007-11-28 09:55:25 +000033#include <unistd.h>
subrata_modak9326a852007-11-28 09:55:25 +000034
35#include "test.h"
subrata_modak9326a852007-11-28 09:55:25 +000036
Wanlong Gaob9c4e0f2012-06-18 20:00:30 +080037char *TCID = "madvise03";
Garrett Cooper1e587382011-03-15 05:02:48 -070038
Garrett Cooper838a7ed2010-12-19 06:33:24 -080039#ifdef MADV_REMOVE
40
Wanlong Gaob9c4e0f2012-06-18 20:00:30 +080041int TST_TOTAL = 3;
Garrett Cooper838a7ed2010-12-19 06:33:24 -080042
Wanlong Gaob9c4e0f2012-06-18 20:00:30 +080043static void setup(void);
44static void cleanup(void);
45static void check_and_print(char *advice);
46static long get_shmmax(void);
subrata_modak9326a852007-11-28 09:55:25 +000047
subrata_modak69302462008-10-29 08:12:06 +000048static int shmid1;
subrata_modak9326a852007-11-28 09:55:25 +000049
50int main(int argc, char *argv[])
51{
52 int lc, fd;
Wanlong Gaob9c4e0f2012-06-18 20:00:30 +080053 int i;
subrata_modak56207ce2009-03-23 13:35:39 +000054 char *file = NULL;
subrata_modak9326a852007-11-28 09:55:25 +000055 struct stat stat;
subrata_modak9326a852007-11-28 09:55:25 +000056 void *addr1;
subrata_modak56207ce2009-03-23 13:35:39 +000057 long shm_size = 0;
subrata_modakbdbaec52009-02-26 12:14:51 +000058
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020059 const char *msg = NULL;
subrata_modak9326a852007-11-28 09:55:25 +000060 char filename[64];
subrata_modak56207ce2009-03-23 13:35:39 +000061 char *progname = NULL;
Wanlong Gaob9c4e0f2012-06-18 20:00:30 +080062 char *str_for_file = "abcdefghijklmnopqrstuvwxyz12345\n";
subrata_modak9326a852007-11-28 09:55:25 +000063
Wanlong Gaob9c4e0f2012-06-18 20:00:30 +080064 msg = parse_opts(argc, argv, NULL, NULL);
65 if (msg)
subrata_modak56207ce2009-03-23 13:35:39 +000066 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -080067
subrata_modak9326a852007-11-28 09:55:25 +000068 setup();
subrata_modakbdbaec52009-02-26 12:14:51 +000069
subrata_modak9326a852007-11-28 09:55:25 +000070 progname = *argv;
71 sprintf(filename, "%s-out.%d", progname, getpid());
subrata_modakbdbaec52009-02-26 12:14:51 +000072
subrata_modak56207ce2009-03-23 13:35:39 +000073 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080074 tst_count = 0;
subrata_modak9326a852007-11-28 09:55:25 +000075
Wanlong Gaob9c4e0f2012-06-18 20:00:30 +080076 fd = open(filename, O_RDWR | O_CREAT, 0664);
77 if (fd < 0)
Garrett Cooper838a7ed2010-12-19 06:33:24 -080078 tst_brkm(TBROK, cleanup, "open failed");
Caspar Zhang0101f632013-03-07 14:24:26 +080079#ifdef DEBUG
subrata_modak56207ce2009-03-23 13:35:39 +000080 tst_resm(TINFO, "filename = %s opened successfully", filename);
subrata_modak9326a852007-11-28 09:55:25 +000081#endif
82
83 /* Writing 40 KB of random data into this file
84 [32 * 1280 = 40960] */
Garrett Cooper838a7ed2010-12-19 06:33:24 -080085 for (i = 0; i < 1280; i++)
86 if (write(fd, str_for_file, strlen(str_for_file)) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +080087 tst_brkm(TBROK | TERRNO, cleanup,
88 "write failed");
subrata_modakbdbaec52009-02-26 12:14:51 +000089
Garrett Cooper838a7ed2010-12-19 06:33:24 -080090 if (fstat(fd, &stat) == -1)
91 tst_brkm(TBROK, cleanup, "fstat failed");
subrata_modak9326a852007-11-28 09:55:25 +000092
Wanlong Gaob9c4e0f2012-06-18 20:00:30 +080093 file = mmap(NULL, stat.st_size, PROT_READ, MAP_SHARED, fd, 0);
94 if (file == MAP_FAILED)
Wanlong Gao354ebb42012-12-07 10:10:04 +080095 tst_brkm(TBROK | TERRNO, cleanup, "mmap failed");
subrata_modak9326a852007-11-28 09:55:25 +000096
subrata_modakbdbaec52009-02-26 12:14:51 +000097 /* Allocate shared memory segment */
subrata_modak56207ce2009-03-23 13:35:39 +000098 shm_size = get_shmmax();
Garrett Cooper838a7ed2010-12-19 06:33:24 -080099
100#define min(a, b) ((a) < (b) ? (a) : (b))
Wanlong Gao354ebb42012-12-07 10:10:04 +0800101 shmid1 = shmget(IPC_PRIVATE, min(1024 * 1024, shm_size),
102 IPC_CREAT | IPC_EXCL | 0701);
Wanlong Gaob9c4e0f2012-06-18 20:00:30 +0800103 if (shmid1 == -1)
Garrett Cooper838a7ed2010-12-19 06:33:24 -0800104 tst_brkm(TBROK, cleanup, "shmget failed");
subrata_modak9326a852007-11-28 09:55:25 +0000105
Simon Xu22f99ea2012-08-03 00:53:57 +0800106 /* Attach shared memory segment to an address selected by the system */
107 addr1 = shmat(shmid1, NULL, 0);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800108 if (addr1 == (void *)-1)
subrata_modak56207ce2009-03-23 13:35:39 +0000109 tst_brkm(TBROK, cleanup, "shmat error");
subrata_modak9326a852007-11-28 09:55:25 +0000110
Wanlong Gaob9c4e0f2012-06-18 20:00:30 +0800111 /* (1) Test case for MADV_REMOVE */
Simon Xu22f99ea2012-08-03 00:53:57 +0800112 TEST(madvise(addr1, 4096, MADV_REMOVE));
subrata_modak9326a852007-11-28 09:55:25 +0000113 check_and_print("MADV_REMOVE");
114
Wanlong Gaob9c4e0f2012-06-18 20:00:30 +0800115 /* (2) Test case for MADV_DONTFORK */
subrata_modak56207ce2009-03-23 13:35:39 +0000116 TEST(madvise(file, (stat.st_size / 2), MADV_DONTFORK));
subrata_modak9326a852007-11-28 09:55:25 +0000117 check_and_print("MADV_DONTFORK");
118
Wanlong Gaob9c4e0f2012-06-18 20:00:30 +0800119 /* (3) Test case for MADV_DOFORK */
subrata_modak56207ce2009-03-23 13:35:39 +0000120 TEST(madvise(file, (stat.st_size / 2), MADV_DOFORK));
subrata_modak9326a852007-11-28 09:55:25 +0000121 check_and_print("MADV_DOFORK");
122
subrata_modak56207ce2009-03-23 13:35:39 +0000123 /* Finally Unmapping the whole file */
Garrett Cooper838a7ed2010-12-19 06:33:24 -0800124 if (munmap(file, stat.st_size) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800125 tst_brkm(TBROK | TERRNO, cleanup, "munmap failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000126
subrata_modak9326a852007-11-28 09:55:25 +0000127 close(fd);
128 }
129
130 cleanup();
Garrett Cooper838a7ed2010-12-19 06:33:24 -0800131 tst_exit();
subrata_modak9326a852007-11-28 09:55:25 +0000132}
133
Wanlong Gaob9c4e0f2012-06-18 20:00:30 +0800134static void setup(void)
subrata_modak9326a852007-11-28 09:55:25 +0000135{
Garrett Cooper2c282152010-12-16 00:55:50 -0800136
subrata_modak56207ce2009-03-23 13:35:39 +0000137 tst_sig(NOFORK, DEF_HANDLER, cleanup);
subrata_modak9326a852007-11-28 09:55:25 +0000138
subrata_modak56207ce2009-03-23 13:35:39 +0000139 TEST_PAUSE;
subrata_modak9326a852007-11-28 09:55:25 +0000140
subrata_modak9326a852007-11-28 09:55:25 +0000141 tst_tmpdir();
Garrett Cooper2c282152010-12-16 00:55:50 -0800142}
subrata_modak9326a852007-11-28 09:55:25 +0000143
Wanlong Gaob9c4e0f2012-06-18 20:00:30 +0800144static void cleanup(void)
subrata_modak9326a852007-11-28 09:55:25 +0000145{
subrata_modak56207ce2009-03-23 13:35:39 +0000146 if (shmid1 != -1)
147 if (shmctl(shmid1, IPC_RMID, 0) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800148 tst_resm(TBROK | TERRNO,
149 "shmctl(.., IPC_RMID, ..) failed");
subrata_modak69302462008-10-29 08:12:06 +0000150
subrata_modak9326a852007-11-28 09:55:25 +0000151 tst_rmdir();
Garrett Cooper2c282152010-12-16 00:55:50 -0800152}
subrata_modak9326a852007-11-28 09:55:25 +0000153
Wanlong Gaob9c4e0f2012-06-18 20:00:30 +0800154static void check_and_print(char *advice)
subrata_modak9326a852007-11-28 09:55:25 +0000155{
subrata_modak56207ce2009-03-23 13:35:39 +0000156 if (TEST_RETURN == -1) {
Francesco Rundo5a6d0252014-02-26 10:00:54 +0100157 if (TEST_ERRNO == ENOTSUP && !strcmp(advice, "MADV_REMOVE")) {
158 tst_resm(TCONF, "madvise MADV_REMOVE returned ENOTSUP"
159 " CONFIG_TMPFS=y not in kernel .config?");
160 return;
161 }
subrata_modak9326a852007-11-28 09:55:25 +0000162 tst_resm(TFAIL,
subrata_modak56207ce2009-03-23 13:35:39 +0000163 "madvise test for %s failed with "
subrata_modak358c3ee2009-10-26 14:55:46 +0000164 "return = %ld, errno = %d : %s",
subrata_modak56207ce2009-03-23 13:35:39 +0000165 advice, TEST_RETURN, TEST_ERRNO, strerror(TEST_ERRNO));
Cyril Hrubise38b9612014-06-02 17:20:57 +0200166 } else {
subrata_modak56207ce2009-03-23 13:35:39 +0000167 tst_resm(TPASS, "madvise test for %s PASSED", advice);
subrata_modak9326a852007-11-28 09:55:25 +0000168 }
169}
subrata_modak56207ce2009-03-23 13:35:39 +0000170
Wanlong Gaob9c4e0f2012-06-18 20:00:30 +0800171static long get_shmmax(void)
subrata_modak69302462008-10-29 08:12:06 +0000172{
subrata_modak56207ce2009-03-23 13:35:39 +0000173 long maxsize;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800174
Cyril Hrubisda6be572012-11-28 13:20:30 +0100175 SAFE_FILE_SCANF(cleanup, "/proc/sys/kernel/shmmax", "%ld", &maxsize);
subrata_modak69302462008-10-29 08:12:06 +0000176
Wanlong Gaob9c4e0f2012-06-18 20:00:30 +0800177 return maxsize;
Garrett Cooper838a7ed2010-12-19 06:33:24 -0800178}
179#else
Cyril Hrubisda6be572012-11-28 13:20:30 +0100180int main(void)
Garrett Cooper838a7ed2010-12-19 06:33:24 -0800181{
182 /* "Requires 2.6.16+" were the original comments */
183 tst_brkm(TCONF, NULL,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800184 "this system doesn't have required madvise support");
Garrett Cooper838a7ed2010-12-19 06:33:24 -0800185}
Garrett Cooper1e587382011-03-15 05:02:48 -0700186#endif