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