Imported Upstream version 20091031
[ltp-debian.git] / testcases / kernel / syscalls / fadvise / posix_fadvise03.c
blob37e32bdc6aa52ba5ecfff151ca978f020747ed94
1 /*
3 * Copyright (c) Red Hat Inc., 2007
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.
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.
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
21 * NAME
22 * posix_fadvise03.c
24 * DESCRIPTION
25 * Check the value that posix_fadvise returns for wrong ADVISE value.
27 * USAGE
28 * posix_fadvise03
30 * HISTORY
31 * 11/2007 Initial version by Masatake YAMATO <yamato@redhat.com>
33 * RESTRICTIONS
34 * None
37 #define _XOPEN_SOURCE 600
38 #include <fcntl.h>
39 #include <unistd.h>
40 #include <signal.h>
41 #include <errno.h>
42 #include <limits.h>
43 #include "test.h"
44 #include "usctest.h"
46 #include "linux_syscall_numbers.h"
47 #ifndef _FILE_OFFSET_BITS
48 #define _FILE_OFFSET_BITS 32
49 #endif
51 #ifndef __NR_fadvise64
52 #define __NR_fadvise64 0
53 #endif
55 void setup();
56 void cleanup();
58 TCID_DEFINE(posix_fadvise03); /* Test program identifier. */
59 extern int Tst_count; /* Test Case counter for tst_* routines */
61 char fname[] = "/bin/cat"; /* test executable to open */
62 int fd = -1; /* initialized in open */
64 int expected_error = EINVAL;
66 int defined_advise[] = {
67 POSIX_FADV_NORMAL,
68 POSIX_FADV_SEQUENTIAL,
69 POSIX_FADV_RANDOM,
70 POSIX_FADV_NOREUSE,
71 POSIX_FADV_WILLNEED,
72 POSIX_FADV_DONTNEED,
75 #define defined_advise_total (sizeof(defined_advise) / sizeof(defined_advise[0]))
77 #if 0
78 /* Too many test cases. */
79 int TST_TOTAL = (INT_MAX - defined_advise_total);
80 int advise_limit = INT_MAX;
81 #else
82 int TST_TOTAL = (32 - defined_advise_total);
83 int advise_limit = 32;
84 #endif /* 0 */
86 /* is_defined_advise:
87 Return 1 if advise is in defined_advise.
88 Return 0 if not. */
89 static int is_defined_advise(int advise)
91 int i;
92 for (i = 0; i < defined_advise_total; i++) {
93 if (defined_advise[i] == advise)
94 return 1;
97 return 0;
100 int main(int ac, char **av)
102 int lc; /* loop counter */
103 char *msg; /* message returned from parse_opts */
104 int advise;
106 /* Check this system has fadvise64 system which is used
107 in posix_fadvise. */
108 if ((_FILE_OFFSET_BITS != 64) && (__NR_fadvise64 == 0)) {
109 tst_resm(TWARN,
110 "This test can only run on kernels that implements ");
111 tst_resm(TWARN, "fadvise64 which is used from posix_fadvise");
112 exit(0);
116 * parse standard options
118 if ((msg = parse_opts(ac, av, (option_t *) NULL, NULL)) != (char *)NULL)
119 tst_brkm(TBROK, cleanup, "OPTION PARSING ERROR - %s", msg);
122 * perform global setup for test
124 setup();
127 * check looping state if -i option given on the command line
129 for (lc = 0; TEST_LOOPING(lc); lc++) {
131 /* reset Tst_count in case we are looping. */
132 Tst_count = 0;
134 /* loop through the test cases */
135 for (advise = 0; advise < advise_limit; advise++) {
137 /* Don't use defiend advise as an argument. */
138 if (is_defined_advise(advise)) {
139 continue;
142 TEST(posix_fadvise(fd, 0, 0, advise));
144 if (TEST_RETURN == 0) {
145 tst_resm(TFAIL, "call succeeded unexpectedly");
146 continue;
149 /* Man page says:
150 "On error, an error number is returned." */
151 if (TEST_RETURN == expected_error) {
152 tst_resm(TPASS,
153 "expected failure - "
154 "returned value = %ld, advise = %d : %s",
155 TEST_RETURN,
156 advise, strerror(TEST_RETURN));
157 } else {
158 tst_resm(TFAIL,
159 "unexpected return value - %ld : %s, advise %d - "
160 "expected %d",
161 TEST_RETURN,
162 strerror(TEST_RETURN),
163 advise, expected_error);
166 } /* End for TEST_LOOPING */
169 * cleanup and exit
171 cleanup();
173 return 0;
174 } /* End main */
177 * setup() - performs all ONE TIME setup for this test.
179 void setup()
181 /* capture signals */
182 tst_sig(NOFORK, DEF_HANDLER, cleanup);
184 /* Pause if that option was specified */
185 TEST_PAUSE;
187 fd = open(fname, O_RDONLY);
188 if (fd < 0) {
189 tst_brkm(TBROK, cleanup,
190 "Unable to open a file(\"%s\") for test: %s\n",
191 fname, strerror(errno));
193 } /* End setup() */
196 * cleanup() - performs all ONE TIME cleanup for this test at
197 * completion or premature exit.
199 void cleanup()
202 * print timing stats if that option was specified.
203 * print errno log if that option was specified.
205 TEST_CLEANUP;
207 if (fd != -1) {
208 close(fd);
211 /* exit with return code appropriate for results */
212 tst_exit();
213 } /* End cleanup() */