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_fadvise01.c
23 *
24 * DESCRIPTION
25 * Check the value that posix_fadvise returns for wrong ADVISE value.
26 *
27 * USAGE
28 * posix_fadvise01
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
40 #include <unistd.h>
41 #include <signal.h>
42 #include <errno.h>
43
44 #include "test.h"
45
46 #include "linux_syscall_numbers.h"
47 #ifndef _FILE_OFFSET_BITS
48 #define _FILE_OFFSET_BITS 32
49 #endif
50
51 #ifndef __NR_fadvise64
52 #define __NR_fadvise64 0
53 #endif
54
55 void setup();
56 void cleanup();
57
58 TCID_DEFINE(posix_fadvise01);
59
60 #ifndef ANDROID
61 char fname[] = "/bin/cat"; /* test executable to open */
62 #else
63 char fname[] = "/system/bin/cat"; /* test executable to open */
64 #endif
65 int fd = -1; /* initialized in open */
66
67 int expected_return = 0;
68
69 int defined_advise[] = {
70 POSIX_FADV_NORMAL,
71 POSIX_FADV_SEQUENTIAL,
72 POSIX_FADV_RANDOM,
73 POSIX_FADV_NOREUSE,
74 POSIX_FADV_WILLNEED,
75 POSIX_FADV_DONTNEED,
76 };
77
78 #define defined_advise_total ARRAY_SIZE(defined_advise)
79
80 int TST_TOTAL = defined_advise_total;
81
main(int ac,char ** av)82 int main(int ac, char **av)
83 {
84 int lc;
85 int i;
86
87 /* Check this system has fadvise64 system which is used
88 in posix_fadvise. */
89 if ((_FILE_OFFSET_BITS != 64) && (__NR_fadvise64 == 0)) {
90 tst_resm(TWARN,
91 "This test can only run on kernels that implements ");
92 tst_resm(TWARN, "fadvise64 which is used from posix_fadvise");
93 exit(0);
94 }
95
96 /*
97 * parse standard options
98 */
99 tst_parse_opts(ac, av, NULL, NULL);
100
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 */
109 for (lc = 0; TEST_LOOPING(lc); lc++) {
110
111 tst_count = 0;
112
113 /* loop through the test cases */
114 for (i = 0; i < defined_advise_total; i++) {
115
116 TEST(posix_fadvise(fd, 0, 0, defined_advise[i]));
117
118 /* Man page says:
119 "On error, an error number is returned." */
120 if (TEST_RETURN == expected_return) {
121 tst_resm(TPASS, "call succeeded expectedly");
122 } else {
123 tst_resm(TFAIL,
124 "unexpected return value - %ld : %s, advise %d - "
125 "expected %d",
126 TEST_RETURN,
127 strerror(TEST_RETURN),
128 defined_advise[i], expected_return);
129 }
130 }
131 }
132
133 /*
134 * cleanup and exit
135 */
136 cleanup();
137
138 tst_exit();
139 }
140
141 /*
142 * setup() - performs all ONE TIME setup for this test.
143 */
setup(void)144 void setup(void)
145 {
146
147 tst_sig(NOFORK, DEF_HANDLER, cleanup);
148
149 TEST_PAUSE;
150
151 fd = open(fname, O_RDONLY);
152 if (fd < 0) {
153 tst_brkm(TBROK, cleanup,
154 "Unable to open a file(\"%s\") for test: %s\n",
155 fname, strerror(errno));
156 }
157 }
158
159 /*
160 * cleanup() - performs all ONE TIME cleanup for this test at
161 * completion or premature exit.
162 */
cleanup(void)163 void cleanup(void)
164 {
165
166 if (fd != -1) {
167 close(fd);
168 }
169
170 }
171