blob: db8876506e5c3cb15f93f4fad5521e8ac4a6e025 [file] [log] [blame]
subrata_modakbad4dde2007-11-22 13:58:24 +00001/*
2 *
3 * Copyright (c) Red Hat Inc., 2007
4 *
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
13 * the GNU 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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
subrata_modakbad4dde2007-11-22 13:58:24 +000018 */
19
20/*
21 * NAME
22 * posix_fadvise03.c
23 *
24 * DESCRIPTION
25 * Check the value that posix_fadvise returns for wrong ADVISE value.
26 *
27 * USAGE
subrata_modak56207ce2009-03-23 13:35:39 +000028 * posix_fadvise03
subrata_modakbad4dde2007-11-22 13:58:24 +000029 *
30 * HISTORY
31 * 11/2007 Initial version by Masatake YAMATO <yamato@redhat.com>
32 *
33 * RESTRICTIONS
subrata_modak56207ce2009-03-23 13:35:39 +000034 * None
subrata_modakbad4dde2007-11-22 13:58:24 +000035 */
36
subrata_modakbad4dde2007-11-22 13:58:24 +000037#define _XOPEN_SOURCE 600
38#include <fcntl.h>
subrata_modakbad4dde2007-11-22 13:58:24 +000039#include <unistd.h>
subrata_modakbad4dde2007-11-22 13:58:24 +000040#include <signal.h>
41#include <errno.h>
subrata_modakbad4dde2007-11-22 13:58:24 +000042#include <limits.h>
subrata_modakbad4dde2007-11-22 13:58:24 +000043#include "test.h"
subrata_modakbad4dde2007-11-22 13:58:24 +000044
subrata_modak9b199e32008-01-08 12:01:20 +000045#include "linux_syscall_numbers.h"
46#ifndef _FILE_OFFSET_BITS
47#define _FILE_OFFSET_BITS 32
48#endif
49
subrata_modakc29113c2008-04-22 15:29:43 +000050#ifndef __NR_fadvise64
51#define __NR_fadvise64 0
52#endif
53
subrata_modakbad4dde2007-11-22 13:58:24 +000054void setup();
55void cleanup();
56
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020057TCID_DEFINE(posix_fadvise03);
subrata_modakbad4dde2007-11-22 13:58:24 +000058
59char fname[] = "/bin/cat"; /* test executable to open */
subrata_modak56207ce2009-03-23 13:35:39 +000060int fd = -1; /* initialized in open */
subrata_modakbad4dde2007-11-22 13:58:24 +000061
subrata_modak56207ce2009-03-23 13:35:39 +000062int expected_error = EINVAL;
subrata_modakbad4dde2007-11-22 13:58:24 +000063
subrata_modak4bb656a2009-02-26 12:02:09 +000064int defined_advise[] = {
65 POSIX_FADV_NORMAL,
subrata_modak56207ce2009-03-23 13:35:39 +000066 POSIX_FADV_SEQUENTIAL,
67 POSIX_FADV_RANDOM,
68 POSIX_FADV_NOREUSE,
69 POSIX_FADV_WILLNEED,
70 POSIX_FADV_DONTNEED,
subrata_modakbad4dde2007-11-22 13:58:24 +000071};
subrata_modak56207ce2009-03-23 13:35:39 +000072
subrata_modakbad4dde2007-11-22 13:58:24 +000073#define defined_advise_total (sizeof(defined_advise) / sizeof(defined_advise[0]))
74
75#if 0
76/* Too many test cases. */
subrata_modak56207ce2009-03-23 13:35:39 +000077int TST_TOTAL = (INT_MAX - defined_advise_total);
subrata_modakbad4dde2007-11-22 13:58:24 +000078int advise_limit = INT_MAX;
subrata_modak4bb656a2009-02-26 12:02:09 +000079#else
subrata_modak56207ce2009-03-23 13:35:39 +000080int TST_TOTAL = (32 - defined_advise_total);
subrata_modakbad4dde2007-11-22 13:58:24 +000081int advise_limit = 32;
82#endif /* 0 */
83
84/* is_defined_advise:
85 Return 1 if advise is in defined_advise.
86 Return 0 if not. */
subrata_modak56207ce2009-03-23 13:35:39 +000087static int is_defined_advise(int advise)
subrata_modakbad4dde2007-11-22 13:58:24 +000088{
89 int i;
90 for (i = 0; i < defined_advise_total; i++) {
91 if (defined_advise[i] == advise)
subrata_modak56207ce2009-03-23 13:35:39 +000092 return 1;
subrata_modakbad4dde2007-11-22 13:58:24 +000093 }
94
95 return 0;
96}
97
subrata_modak56207ce2009-03-23 13:35:39 +000098int main(int ac, char **av)
subrata_modakbad4dde2007-11-22 13:58:24 +000099{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200100 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200101 const char *msg;
subrata_modakbad4dde2007-11-22 13:58:24 +0000102 int advise;
103
subrata_modak56207ce2009-03-23 13:35:39 +0000104 /* Check this system has fadvise64 system which is used
105 in posix_fadvise. */
106 if ((_FILE_OFFSET_BITS != 64) && (__NR_fadvise64 == 0)) {
107 tst_resm(TWARN,
108 "This test can only run on kernels that implements ");
109 tst_resm(TWARN, "fadvise64 which is used from posix_fadvise");
110 exit(0);
111 }
subrata_modakbad4dde2007-11-22 13:58:24 +0000112
113 /*
114 * parse standard options
115 */
Garrett Cooper45e285d2010-11-22 12:19:25 -0800116 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800117 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
subrata_modakbad4dde2007-11-22 13:58:24 +0000118
119 /*
120 * perform global setup for test
121 */
122 setup();
123
124 /*
125 * check looping state if -i option given on the command line
126 */
subrata_modak56207ce2009-03-23 13:35:39 +0000127 for (lc = 0; TEST_LOOPING(lc); lc++) {
subrata_modakbad4dde2007-11-22 13:58:24 +0000128
Caspar Zhangd59a6592013-03-07 14:59:12 +0800129 tst_count = 0;
subrata_modakbad4dde2007-11-22 13:58:24 +0000130
131 /* loop through the test cases */
132 for (advise = 0; advise < advise_limit; advise++) {
subrata_modak56207ce2009-03-23 13:35:39 +0000133
subrata_modakbad4dde2007-11-22 13:58:24 +0000134 /* Don't use defiend advise as an argument. */
135 if (is_defined_advise(advise)) {
136 continue;
137 }
subrata_modak56207ce2009-03-23 13:35:39 +0000138
subrata_modakbad4dde2007-11-22 13:58:24 +0000139 TEST(posix_fadvise(fd, 0, 0, advise));
140
141 if (TEST_RETURN == 0) {
142 tst_resm(TFAIL, "call succeeded unexpectedly");
143 continue;
144 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000145
subrata_modakbad4dde2007-11-22 13:58:24 +0000146 /* Man page says:
147 "On error, an error number is returned." */
148 if (TEST_RETURN == expected_error) {
149 tst_resm(TPASS,
150 "expected failure - "
subrata_modak358c3ee2009-10-26 14:55:46 +0000151 "returned value = %ld, advise = %d : %s",
subrata_modakbad4dde2007-11-22 13:58:24 +0000152 TEST_RETURN,
subrata_modak56207ce2009-03-23 13:35:39 +0000153 advise, strerror(TEST_RETURN));
subrata_modakbad4dde2007-11-22 13:58:24 +0000154 } else {
155 tst_resm(TFAIL,
subrata_modak358c3ee2009-10-26 14:55:46 +0000156 "unexpected return value - %ld : %s, advise %d - "
subrata_modak4bb656a2009-02-26 12:02:09 +0000157 "expected %d",
subrata_modakbad4dde2007-11-22 13:58:24 +0000158 TEST_RETURN,
subrata_modak4bb656a2009-02-26 12:02:09 +0000159 strerror(TEST_RETURN),
subrata_modak56207ce2009-03-23 13:35:39 +0000160 advise, expected_error);
subrata_modakbad4dde2007-11-22 13:58:24 +0000161 }
162 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800163 }
subrata_modakbad4dde2007-11-22 13:58:24 +0000164
165 /*
166 * cleanup and exit
167 */
168 cleanup();
169
Garrett Cooper2c282152010-12-16 00:55:50 -0800170 tst_exit();
171}
subrata_modakbad4dde2007-11-22 13:58:24 +0000172
173/*
174 * setup() - performs all ONE TIME setup for this test.
175 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400176void setup(void)
subrata_modakbad4dde2007-11-22 13:58:24 +0000177{
Garrett Cooper2c282152010-12-16 00:55:50 -0800178
subrata_modakbad4dde2007-11-22 13:58:24 +0000179 tst_sig(NOFORK, DEF_HANDLER, cleanup);
180
subrata_modakbad4dde2007-11-22 13:58:24 +0000181 TEST_PAUSE;
182
183 fd = open(fname, O_RDONLY);
184 if (fd < 0) {
subrata_modak4bb656a2009-02-26 12:02:09 +0000185 tst_brkm(TBROK, cleanup,
subrata_modakbad4dde2007-11-22 13:58:24 +0000186 "Unable to open a file(\"%s\") for test: %s\n",
subrata_modak56207ce2009-03-23 13:35:39 +0000187 fname, strerror(errno));
subrata_modak4bb656a2009-02-26 12:02:09 +0000188 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800189}
subrata_modakbad4dde2007-11-22 13:58:24 +0000190
191/*
192 * cleanup() - performs all ONE TIME cleanup for this test at
193 * completion or premature exit.
194 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400195void cleanup(void)
subrata_modakbad4dde2007-11-22 13:58:24 +0000196{
subrata_modakbad4dde2007-11-22 13:58:24 +0000197
198 if (fd != -1) {
199 close(fd);
200 }
201
Chris Dearmanec6edca2012-10-17 19:54:01 -0700202}