blob: 3c4ff421dbae672dffefe5fb46e2b174b0553a6c [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
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20/*
21 * NAME
22 * posix_fadvise02.c
23 *
24 * DESCRIPTION
25 * Check the value that posix_fadvise returns for wrong file descriptor.
26 *
27 * USAGE
subrata_modak56207ce2009-03-23 13:35:39 +000028 * posix_fadvise02
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>
42#include "test.h"
43#include "usctest.h"
44
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
subrata_modak585950c2008-08-20 10:55:19 +000057TCID_DEFINE(posix_fadvise02); /* Test program identifier. */
subrata_modakbad4dde2007-11-22 13:58:24 +000058extern int Tst_count; /* Test Case counter for tst_* routines */
59
subrata_modak4bb656a2009-02-26 12:02:09 +000060#define WRONG_FD 42 /* The number has no meaning.
subrata_modakbad4dde2007-11-22 13:58:24 +000061 Just used as something wrong fd */
62
63struct test_case_t {
subrata_modak56207ce2009-03-23 13:35:39 +000064 int fd;
subrata_modakbad4dde2007-11-22 13:58:24 +000065 off_t offset;
66 off_t len;
subrata_modak56207ce2009-03-23 13:35:39 +000067 int advice;
68 int error;
subrata_modakbad4dde2007-11-22 13:58:24 +000069} TC[] = {
subrata_modak56207ce2009-03-23 13:35:39 +000070 {
71 WRONG_FD, 0, 0, POSIX_FADV_NORMAL, EBADF}, {
72 WRONG_FD, 0, 0, POSIX_FADV_SEQUENTIAL, EBADF}, {
73 WRONG_FD, 0, 0, POSIX_FADV_RANDOM, EBADF}, {
74 WRONG_FD, 0, 0, POSIX_FADV_NOREUSE, EBADF}, {
75 WRONG_FD, 0, 0, POSIX_FADV_WILLNEED, EBADF}, {
76WRONG_FD, 0, 0, POSIX_FADV_DONTNEED, EBADF},};
subrata_modakbad4dde2007-11-22 13:58:24 +000077
78int TST_TOTAL = sizeof(TC) / sizeof(TC[0]);
79
subrata_modak56207ce2009-03-23 13:35:39 +000080int main(int ac, char **av)
subrata_modakbad4dde2007-11-22 13:58:24 +000081{
82 int lc; /* loop counter */
83 char *msg; /* message returned from parse_opts */
84 int i;
85
subrata_modak56207ce2009-03-23 13:35:39 +000086 /* Check this system has fadvise64 system which is used
87 in posix_fadvise. */
88 if ((_FILE_OFFSET_BITS != 64) && (__NR_fadvise64 == 0)) {
89 tst_resm(TWARN,
90 "This test can only run on kernels that implements ");
91 tst_resm(TWARN, "fadvise64 which is used from posix_fadvise");
92 exit(0);
93 }
subrata_modakbad4dde2007-11-22 13:58:24 +000094
95 /*
96 * parse standard options
97 */
subrata_modak56207ce2009-03-23 13:35:39 +000098 if ((msg = parse_opts(ac, av, (option_t *) NULL, NULL)) != (char *)NULL)
99 tst_brkm(TBROK, cleanup, "OPTION PARSING ERROR - %s", msg);
subrata_modakbad4dde2007-11-22 13:58:24 +0000100
101 /*
102 * perform global setup for test
103 */
104 setup();
105
106 /*
107 * check looping state if -i option given on the command line
108 */
subrata_modak56207ce2009-03-23 13:35:39 +0000109 for (lc = 0; TEST_LOOPING(lc); lc++) {
subrata_modakbad4dde2007-11-22 13:58:24 +0000110
111 /* reset Tst_count in case we are looping. */
subrata_modak56207ce2009-03-23 13:35:39 +0000112 Tst_count = 0;
subrata_modakbad4dde2007-11-22 13:58:24 +0000113
114 /* loop through the test cases */
115 for (i = 0; i < TST_TOTAL; i++) {
116
subrata_modak56207ce2009-03-23 13:35:39 +0000117 TEST(posix_fadvise
118 (TC[i].fd, TC[i].offset, TC[i].len, TC[i].advice));
subrata_modakbad4dde2007-11-22 13:58:24 +0000119
120 if (TEST_RETURN == 0) {
121 tst_resm(TFAIL, "call succeeded unexpectedly");
122 continue;
123 }
124
125 /* Man page says:
126 "On error, an error number is returned." */
127 if (TEST_RETURN == TC[i].error) {
128 tst_resm(TPASS, "expected failure - "
subrata_modak56207ce2009-03-23 13:35:39 +0000129 "returned value = %d : %s",
130 TEST_RETURN, strerror(TEST_RETURN));
subrata_modakbad4dde2007-11-22 13:58:24 +0000131 } else {
subrata_modak56207ce2009-03-23 13:35:39 +0000132 tst_resm(TFAIL,
133 "unexpected returnd value - %d : %s - "
subrata_modakbad4dde2007-11-22 13:58:24 +0000134 "expected %d", TEST_RETURN,
135 strerror(TEST_RETURN), TC[i].error);
136 }
137 }
subrata_modak56207ce2009-03-23 13:35:39 +0000138 } /* End for TEST_LOOPING */
subrata_modakbad4dde2007-11-22 13:58:24 +0000139
140 /*
141 * cleanup and exit
142 */
143 cleanup();
144
subrata_modak43337a32009-02-26 11:43:51 +0000145 return 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000146} /* End main */
subrata_modakbad4dde2007-11-22 13:58:24 +0000147
148/*
149 * setup() - performs all ONE TIME setup for this test.
150 */
subrata_modak56207ce2009-03-23 13:35:39 +0000151void setup()
subrata_modakbad4dde2007-11-22 13:58:24 +0000152{
153 /* capture signals */
154 tst_sig(NOFORK, DEF_HANDLER, cleanup);
155
156 /* Pause if that option was specified */
157 TEST_PAUSE;
158
159 /* Make WRONG_FD really wrong. */
subrata_modak56207ce2009-03-23 13:35:39 +0000160 retry:
subrata_modakbad4dde2007-11-22 13:58:24 +0000161 errno = 0;
162 if (close(WRONG_FD) != 0) {
subrata_modak56207ce2009-03-23 13:35:39 +0000163 if (errno == EBADF) ; /* Good. Do nothing. */
subrata_modakbad4dde2007-11-22 13:58:24 +0000164 if (errno == EINTR)
subrata_modak56207ce2009-03-23 13:35:39 +0000165 goto retry;
subrata_modak4bb656a2009-02-26 12:02:09 +0000166 else if (errno == EIO)
subrata_modak56207ce2009-03-23 13:35:39 +0000167 tst_brkm(TBROK, cleanup,
168 "Unable to close a file descriptor(%d): %s\n",
169 WRONG_FD, strerror(EIO));
subrata_modakbad4dde2007-11-22 13:58:24 +0000170 }
171
subrata_modak56207ce2009-03-23 13:35:39 +0000172} /* End setup() */
subrata_modakbad4dde2007-11-22 13:58:24 +0000173
174/*
175 * cleanup() - performs all ONE TIME cleanup for this test at
176 * completion or premature exit.
177 */
subrata_modak56207ce2009-03-23 13:35:39 +0000178void cleanup()
subrata_modakbad4dde2007-11-22 13:58:24 +0000179{
180 /*
181 * print timing stats if that option was specified.
182 * print errno log if that option was specified.
183 */
184 TEST_CLEANUP;
185
186 /* exit with return code appropriate for results */
187 tst_exit();
subrata_modak56207ce2009-03-23 13:35:39 +0000188} /* End cleanup() */