Home | History | Annotate | Download | only in fadvise
      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_fadvise04.c
     23  *
     24  * DESCRIPTION
     25  *	Check the value that posix_fadvise returns for pipe descriptor.
     26  *
     27  * USAGE
     28  *	posix_fadvise04
     29  *
     30  * HISTORY
     31  *	11/2007 Initial version by Masatake YAMATO <yamato (at) 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 #include "linux_syscall_numbers.h"
     44 #ifndef _FILE_OFFSET_BITS
     45 #define _FILE_OFFSET_BITS 32
     46 #endif
     47 
     48 #ifndef __NR_fadvise64
     49 #define __NR_fadvise64 0
     50 #endif
     51 
     52 void setup();
     53 void cleanup();
     54 
     55 TCID_DEFINE(posix_fadvise04);
     56 
     57 #define GIVEN_IN_SETUP 42	/* No mean. Just used as padding.
     58 				   This is overwritten by setup(). */
     59 
     60 struct test_case_t {
     61 	int fd;
     62 	off_t offset;
     63 	off_t len;
     64 	int advice;
     65 	int error;
     66 } TC[] = {
     67 	{
     68 	GIVEN_IN_SETUP, 0, 0, POSIX_FADV_NORMAL, ESPIPE}, {
     69 	GIVEN_IN_SETUP, 0, 0, POSIX_FADV_SEQUENTIAL, ESPIPE}, {
     70 	GIVEN_IN_SETUP, 0, 0, POSIX_FADV_RANDOM, ESPIPE}, {
     71 	GIVEN_IN_SETUP, 0, 0, POSIX_FADV_NOREUSE, ESPIPE}, {
     72 	GIVEN_IN_SETUP, 0, 0, POSIX_FADV_WILLNEED, ESPIPE}, {
     73 GIVEN_IN_SETUP, 0, 0, POSIX_FADV_DONTNEED, ESPIPE},};
     74 
     75 int TST_TOTAL = sizeof(TC) / sizeof(TC[0]);
     76 
     77 int main(int ac, char **av)
     78 {
     79 	int lc;
     80 	int i;
     81 
     82 	/* Check this system has fadvise64 system which is used
     83 	   in posix_fadvise. */
     84 	if ((_FILE_OFFSET_BITS != 64) && (__NR_fadvise64 == 0)) {
     85 		tst_resm(TWARN,
     86 			 "This test can only run on kernels that implements ");
     87 		tst_resm(TWARN, "fadvise64 which is used from posix_fadvise");
     88 		exit(0);
     89 	}
     90 
     91 	/* Disable test if the version of the kernel is less than 2.6.16 */
     92 	if ((tst_kvercmp(2, 6, 16)) < 0) {
     93 		tst_resm(TWARN, "This test can only run on kernels that are ");
     94 		tst_resm(TWARN, "2.6.16 and higher");
     95 		exit(0);
     96 	}
     97 
     98 	/*
     99 	 * parse standard options
    100 	 */
    101 	tst_parse_opts(ac, av, NULL, NULL);
    102 
    103 	/*
    104 	 * perform global setup for test
    105 	 */
    106 	setup();
    107 
    108 	/*
    109 	 * check looping state if -i option given on the command line
    110 	 */
    111 	for (lc = 0; TEST_LOOPING(lc); lc++) {
    112 
    113 		tst_count = 0;
    114 
    115 		/* loop through the test cases */
    116 		for (i = 0; i < TST_TOTAL; i++) {
    117 
    118 			TEST(posix_fadvise
    119 			     (TC[i].fd, TC[i].offset, TC[i].len, TC[i].advice));
    120 
    121 			if (TEST_RETURN == 0) {
    122 				tst_resm(TFAIL, "call succeeded unexpectedly");
    123 				continue;
    124 			}
    125 
    126 			/* Man page says:
    127 			   "On error, an error number is returned." */
    128 			if (TEST_RETURN == TC[i].error) {
    129 				tst_resm(TPASS, "expected failure - "
    130 					 "returned value = %ld : %s",
    131 					 TEST_RETURN, strerror(TEST_RETURN));
    132 			} else {
    133 				tst_resm(TFAIL,
    134 					 "unexpected return value - %ld : %s - "
    135 					 "expected %d", TEST_RETURN,
    136 					 strerror(TEST_RETURN), TC[i].error);
    137 			}
    138 		}
    139 	}
    140 
    141 	/*
    142 	 * cleanup and exit
    143 	 */
    144 	cleanup();
    145 
    146 	tst_exit();
    147 }
    148 
    149 /*
    150  * setup() - performs all ONE TIME setup for this test.
    151  */
    152 void setup(void)
    153 {
    154 	int pipedes[2];
    155 
    156 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
    157 
    158 	TEST_PAUSE;
    159 
    160 	/* Make a pipe */
    161 	if (pipe(pipedes) != 0) {
    162 		tst_brkm(TBROK, cleanup,
    163 			 "Untable to make a pipe: %s\n", strerror(errno));
    164 	} else {
    165 		int i;
    166 
    167 		/* Close write side first.
    168 		   I don't use it in test. */
    169 		close(pipedes[1]);
    170 
    171 		/* Fill fd field of all test cases
    172 		   with read side of pipe. */
    173 		for (i = 0; i < TST_TOTAL; i++) {
    174 			TC[i].fd = pipedes[0];
    175 		}
    176 	}
    177 }
    178 
    179 /*
    180  * cleanup() - performs all ONE TIME cleanup for this test at
    181  *		completion or premature exit.
    182  */
    183 void cleanup(void)
    184 {
    185 
    186 	/* Close pipe of read side */
    187 	close(TC[0].fd);
    188 
    189 }
    190