1 /*
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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
28 * posix_fadvise02
29 *
30 * HISTORY
31 * 11/2007 Initial version by Masatake YAMATO <yamato@redhat.com>
32 *
33 * RESTRICTIONS
34 * None
35 */
36
37 #define _XOPEN_SOURCE 600
38 #include <fcntl.h>
39 #include <unistd.h>
40 #include <signal.h>
41 #include <errno.h>
42 #include "test.h"
43
44 #include "linux_syscall_numbers.h"
45 #ifndef _FILE_OFFSET_BITS
46 #define _FILE_OFFSET_BITS 32
47 #endif
48
49 #ifndef __NR_fadvise64
50 #define __NR_fadvise64 0
51 #endif
52
53 void setup();
54 void cleanup();
55
56 TCID_DEFINE(posix_fadvise02);
57
58 #define WRONG_FD 42 /* The number has no meaning.
59 Just used as something wrong fd */
60
61 struct test_case_t {
62 int fd;
63 off_t offset;
64 off_t len;
65 int advice;
66 int error;
67 } TC[] = {
68 {
69 WRONG_FD, 0, 0, POSIX_FADV_NORMAL, EBADF}, {
70 WRONG_FD, 0, 0, POSIX_FADV_SEQUENTIAL, EBADF}, {
71 WRONG_FD, 0, 0, POSIX_FADV_RANDOM, EBADF}, {
72 WRONG_FD, 0, 0, POSIX_FADV_NOREUSE, EBADF}, {
73 WRONG_FD, 0, 0, POSIX_FADV_WILLNEED, EBADF}, {
74 WRONG_FD, 0, 0, POSIX_FADV_DONTNEED, EBADF},};
75
76 int TST_TOTAL = sizeof(TC) / sizeof(TC[0]);
77
main(int ac,char ** av)78 int main(int ac, char **av)
79 {
80 int lc;
81 int i;
82
83 /* Check this system has fadvise64 system which is used
84 in posix_fadvise. */
85 if ((_FILE_OFFSET_BITS != 64) && (__NR_fadvise64 == 0)) {
86 tst_resm(TWARN,
87 "This test can only run on kernels that implements ");
88 tst_resm(TWARN, "fadvise64 which is used from posix_fadvise");
89 exit(0);
90 }
91
92 /*
93 * parse standard options
94 */
95 tst_parse_opts(ac, av, NULL, NULL);
96
97 /*
98 * perform global setup for test
99 */
100 setup();
101
102 /*
103 * check looping state if -i option given on the command line
104 */
105 for (lc = 0; TEST_LOOPING(lc); lc++) {
106
107 tst_count = 0;
108
109 /* loop through the test cases */
110 for (i = 0; i < TST_TOTAL; i++) {
111
112 TEST(posix_fadvise
113 (TC[i].fd, TC[i].offset, TC[i].len, TC[i].advice));
114
115 if (TEST_RETURN == 0) {
116 tst_resm(TFAIL, "call succeeded unexpectedly");
117 continue;
118 }
119
120 /* Man page says:
121 "On error, an error number is returned." */
122 if (TEST_RETURN == TC[i].error) {
123 tst_resm(TPASS, "expected failure - "
124 "returned value = %ld : %s",
125 TEST_RETURN, strerror(TEST_RETURN));
126 } else {
127 tst_resm(TFAIL,
128 "unexpected returnd value - %ld : %s - "
129 "expected %d", TEST_RETURN,
130 strerror(TEST_RETURN), TC[i].error);
131 }
132 }
133 }
134
135 /*
136 * cleanup and exit
137 */
138 cleanup();
139
140 tst_exit();
141 }
142
143 /*
144 * setup() - performs all ONE TIME setup for this test.
145 */
setup(void)146 void setup(void)
147 {
148
149 tst_sig(NOFORK, DEF_HANDLER, cleanup);
150
151 TEST_PAUSE;
152
153 /* Make WRONG_FD really wrong. */
154 retry:
155 errno = 0;
156 if (close(WRONG_FD) != 0) {
157 if (errno == EBADF) ; /* Good. Do nothing. */
158 if (errno == EINTR)
159 goto retry;
160 else if (errno == EIO)
161 tst_brkm(TBROK, cleanup,
162 "Unable to close a file descriptor(%d): %s\n",
163 WRONG_FD, strerror(EIO));
164 }
165
166 }
167
168 /*
169 * cleanup() - performs all ONE TIME cleanup for this test at
170 * completion or premature exit.
171 */
cleanup(void)172 void cleanup(void)
173 {
174
175 }
176