blob: 580ae47cc59055be179a541adf3ca5da915934d0 [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_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"
subrata_modakbad4dde2007-11-22 13:58:24 +000043
subrata_modak9b199e32008-01-08 12:01:20 +000044#include "linux_syscall_numbers.h"
45#ifndef _FILE_OFFSET_BITS
46#define _FILE_OFFSET_BITS 32
47#endif
48
subrata_modakc29113c2008-04-22 15:29:43 +000049#ifndef __NR_fadvise64
50#define __NR_fadvise64 0
51#endif
52
subrata_modakbad4dde2007-11-22 13:58:24 +000053void setup();
54void cleanup();
55
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020056TCID_DEFINE(posix_fadvise02);
subrata_modakbad4dde2007-11-22 13:58:24 +000057
subrata_modak4bb656a2009-02-26 12:02:09 +000058#define WRONG_FD 42 /* The number has no meaning.
subrata_modakbad4dde2007-11-22 13:58:24 +000059 Just used as something wrong fd */
60
61struct test_case_t {
subrata_modak56207ce2009-03-23 13:35:39 +000062 int fd;
subrata_modakbad4dde2007-11-22 13:58:24 +000063 off_t offset;
64 off_t len;
subrata_modak56207ce2009-03-23 13:35:39 +000065 int advice;
66 int error;
subrata_modakbad4dde2007-11-22 13:58:24 +000067} TC[] = {
subrata_modak56207ce2009-03-23 13:35:39 +000068 {
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}, {
74WRONG_FD, 0, 0, POSIX_FADV_DONTNEED, EBADF},};
subrata_modakbad4dde2007-11-22 13:58:24 +000075
76int TST_TOTAL = sizeof(TC) / sizeof(TC[0]);
77
subrata_modak56207ce2009-03-23 13:35:39 +000078int main(int ac, char **av)
subrata_modakbad4dde2007-11-22 13:58:24 +000079{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020080 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020081 const char *msg;
subrata_modakbad4dde2007-11-22 13:58:24 +000082 int i;
83
subrata_modak56207ce2009-03-23 13:35:39 +000084 /* Check this system has fadvise64 system which is used
85 in posix_fadvise. */
86 if ((_FILE_OFFSET_BITS != 64) && (__NR_fadvise64 == 0)) {
87 tst_resm(TWARN,
88 "This test can only run on kernels that implements ");
89 tst_resm(TWARN, "fadvise64 which is used from posix_fadvise");
90 exit(0);
91 }
subrata_modakbad4dde2007-11-22 13:58:24 +000092
93 /*
94 * parse standard options
95 */
Garrett Cooper45e285d2010-11-22 12:19:25 -080096 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080097 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
subrata_modakbad4dde2007-11-22 13:58:24 +000098
99 /*
100 * perform global setup for test
101 */
102 setup();
103
104 /*
105 * check looping state if -i option given on the command line
106 */
subrata_modak56207ce2009-03-23 13:35:39 +0000107 for (lc = 0; TEST_LOOPING(lc); lc++) {
subrata_modakbad4dde2007-11-22 13:58:24 +0000108
Caspar Zhangd59a6592013-03-07 14:59:12 +0800109 tst_count = 0;
subrata_modakbad4dde2007-11-22 13:58:24 +0000110
111 /* loop through the test cases */
112 for (i = 0; i < TST_TOTAL; i++) {
113
subrata_modak56207ce2009-03-23 13:35:39 +0000114 TEST(posix_fadvise
115 (TC[i].fd, TC[i].offset, TC[i].len, TC[i].advice));
subrata_modakbad4dde2007-11-22 13:58:24 +0000116
117 if (TEST_RETURN == 0) {
118 tst_resm(TFAIL, "call succeeded unexpectedly");
119 continue;
120 }
121
122 /* Man page says:
123 "On error, an error number is returned." */
124 if (TEST_RETURN == TC[i].error) {
125 tst_resm(TPASS, "expected failure - "
subrata_modak358c3ee2009-10-26 14:55:46 +0000126 "returned value = %ld : %s",
subrata_modak56207ce2009-03-23 13:35:39 +0000127 TEST_RETURN, strerror(TEST_RETURN));
subrata_modakbad4dde2007-11-22 13:58:24 +0000128 } else {
subrata_modak56207ce2009-03-23 13:35:39 +0000129 tst_resm(TFAIL,
subrata_modak358c3ee2009-10-26 14:55:46 +0000130 "unexpected returnd value - %ld : %s - "
subrata_modakbad4dde2007-11-22 13:58:24 +0000131 "expected %d", TEST_RETURN,
132 strerror(TEST_RETURN), TC[i].error);
133 }
134 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800135 }
subrata_modakbad4dde2007-11-22 13:58:24 +0000136
137 /*
138 * cleanup and exit
139 */
140 cleanup();
141
Garrett Cooper2c282152010-12-16 00:55:50 -0800142 tst_exit();
143}
subrata_modakbad4dde2007-11-22 13:58:24 +0000144
145/*
146 * setup() - performs all ONE TIME setup for this test.
147 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400148void setup(void)
subrata_modakbad4dde2007-11-22 13:58:24 +0000149{
Garrett Cooper2c282152010-12-16 00:55:50 -0800150
subrata_modakbad4dde2007-11-22 13:58:24 +0000151 tst_sig(NOFORK, DEF_HANDLER, cleanup);
152
subrata_modakbad4dde2007-11-22 13:58:24 +0000153 TEST_PAUSE;
154
155 /* Make WRONG_FD really wrong. */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800156retry:
subrata_modakbad4dde2007-11-22 13:58:24 +0000157 errno = 0;
158 if (close(WRONG_FD) != 0) {
subrata_modak56207ce2009-03-23 13:35:39 +0000159 if (errno == EBADF) ; /* Good. Do nothing. */
subrata_modakbad4dde2007-11-22 13:58:24 +0000160 if (errno == EINTR)
subrata_modak56207ce2009-03-23 13:35:39 +0000161 goto retry;
subrata_modak4bb656a2009-02-26 12:02:09 +0000162 else if (errno == EIO)
subrata_modak56207ce2009-03-23 13:35:39 +0000163 tst_brkm(TBROK, cleanup,
164 "Unable to close a file descriptor(%d): %s\n",
165 WRONG_FD, strerror(EIO));
subrata_modakbad4dde2007-11-22 13:58:24 +0000166 }
167
Garrett Cooper2c282152010-12-16 00:55:50 -0800168}
subrata_modakbad4dde2007-11-22 13:58:24 +0000169
170/*
171 * cleanup() - performs all ONE TIME cleanup for this test at
172 * completion or premature exit.
173 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400174void cleanup(void)
subrata_modakbad4dde2007-11-22 13:58:24 +0000175{
subrata_modakbad4dde2007-11-22 13:58:24 +0000176
Chris Dearmanec6edca2012-10-17 19:54:01 -0700177}