Home | History | Annotate | Download | only in lseek
      1 /*
      2  *
      3  *   Copyright (c) International Business Machines  Corp., 2001
      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  * Test Name: lseek10
     22  *
     23  * Test Description:
     24  *  Verify that,
     25  *  1. lseek() returns -1 and sets errno to ESPIPE, if the file handle of
     26  *     the specified file is associated with a pipe, socket, or  FIFO.
     27  *  2. lseek() returns -1 and sets errno to EINVAL, if the 'Whence' argument
     28  *     is not a proper value.
     29  *  3. lseek() returns -1 and sets errno to EBADF, if the file handle of
     30  *     the specified file is not valid.
     31  *
     32  * Expected Result:
     33  *  lseek() should fail with return value -1 and set expected errno.
     34  *
     35  * Algorithm:
     36  *  Setup:
     37  *   Setup signal handling.
     38  *   Create temporary directory.
     39  *   Pause for SIGUSR1 if option specified.
     40  *
     41  *  Test:
     42  *   Loop if the proper options are given.
     43  *   Execute system call
     44  *   Check return code, if system call failed (return=-1)
     45  *   	if errno set == expected errno
     46  *   		Issue sys call fails with expected return value and errno.
     47  *   	Otherwise,
     48  *		Issue sys call fails with unexpected errno.
     49  *   Otherwise,
     50  *	Issue sys call returns unexpected value.
     51  *
     52  *  Cleanup:
     53  *   Print errno log and/or timing stats if options given
     54  *   Delete the temporary directory(s)/file(s) created.
     55  *
     56  * Usage:  <for command-line>
     57  *  lseek10 [-c n] [-e] [-i n] [-I x] [-p x] [-t]
     58  *     where,  -c n : Run n copies concurrently.
     59  *             -e   : Turn on errno logging.
     60  *	       -i n : Execute test n times.
     61  *	       -I x : Execute test for x seconds.
     62  *	       -P x : Pause for x seconds between iterations.
     63  *	       -t   : Turn on syscall timing.
     64  *
     65  * HISTORY
     66  *	07/2001 Ported by Wayne Boyer
     67  *
     68  * RESTRICTIONS:
     69  *  None.
     70  */
     71 
     72 #include <stdio.h>
     73 #include <unistd.h>
     74 #include <sys/types.h>
     75 #include <errno.h>
     76 #include <unistd.h>
     77 #include <fcntl.h>
     78 #include <utime.h>
     79 #include <string.h>
     80 #include <sys/stat.h>
     81 #include <signal.h>
     82 
     83 #include "test.h"
     84 
     85 #define TEMP_FILE1	"tmp_file1"
     86 #define TEMP_FILE2	"tmp_file2"
     87 #define TEMP_FILE3	"tmp_file3"
     88 #define FILE_MODE	S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
     89 #define PIPE_MODE	S_IFIFO | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
     90 #define SEEK_TOP	10
     91 
     92 char *TCID = "lseek10";
     93 int TST_TOTAL = 3;
     94 
     95 int no_setup();
     96 int setup1();			/* setup function to test lseek() for ESPIPE */
     97 int setup2();			/* setup function to test lseek() for EINVAL */
     98 int setup3();			/* setup function to test lseek() for EBADF */
     99 
    100 int fd1;			/* file handle for testfile1  */
    101 int fd2;			/* file handle for testfile2  */
    102 int fd3;			/* file handle for testfile3  */
    103 
    104 struct test_case_t {		/* test case struct. to hold ref. test cond's */
    105 	int fd;
    106 	int Whence;
    107 	char *desc;
    108 	int exp_errno;
    109 	int (*setupfunc) ();
    110 } Test_cases[] = {
    111 	{
    112 	1, SEEK_SET, "'fd' associated with a pipe/fifo", ESPIPE, setup1}, {
    113 	2, SEEK_TOP, "'whence' argument is not valid", EINVAL, setup2}, {
    114 	3, SEEK_SET, "'fd' is not an open file descriptor", EBADF, setup3},
    115 	{
    116 	0, 0, NULL, 0, no_setup}
    117 };
    118 
    119 void setup();			/* Main setup function of test */
    120 void cleanup();			/* cleanup function for the test */
    121 
    122 int main(int ac, char **av)
    123 {
    124 	int lc;
    125 	int fildes;		/* file handle for testfile */
    126 	int whence;		/* position of file handle in the file */
    127 	char *test_desc;	/* test specific error message */
    128 	int ind;		/* counter to test different test conditions */
    129 
    130 	tst_parse_opts(ac, av, NULL, NULL);
    131 
    132 	setup();
    133 
    134 	for (lc = 0; TEST_LOOPING(lc); lc++) {
    135 
    136 		tst_count = 0;
    137 
    138 		for (ind = 0; Test_cases[ind].desc != NULL; ind++) {
    139 			fildes = Test_cases[ind].fd;
    140 			test_desc = Test_cases[ind].desc;
    141 			whence = Test_cases[ind].Whence;
    142 
    143 			/* Assign the 'fd' values appropriatly */
    144 			if (fildes == 1) {
    145 				fildes = fd1;
    146 			} else if (fildes == 2) {
    147 				fildes = fd2;
    148 			} else {
    149 				fildes = fd3;
    150 			}
    151 			/*
    152 			 * Invoke lseek(2) to test different test conditions.
    153 			 * Verify that it fails with -1 return value and
    154 			 * sets appropriate errno.
    155 			 */
    156 			TEST(lseek(fildes, 0, whence));
    157 
    158 			if (TEST_RETURN != (off_t) - 1) {
    159 				tst_resm(TFAIL,
    160 					 "lseek() returned %ld, expected "
    161 					 "-1, errno:%d", TEST_RETURN,
    162 					 Test_cases[ind].exp_errno);
    163 				continue;
    164 			}
    165 			if (TEST_ERRNO == Test_cases[ind].exp_errno) {
    166 				tst_resm(TPASS, "lseek() fails, %s, errno:%d",
    167 					 test_desc, TEST_ERRNO);
    168 			} else {
    169 				tst_resm(TFAIL, "lseek() fails, %s, errno:%d, "
    170 					 "expected errno:%d", test_desc,
    171 					 TEST_ERRNO, Test_cases[ind].exp_errno);
    172 			}
    173 		}
    174 	}
    175 
    176 	cleanup();
    177 	tst_exit();
    178 
    179 }
    180 
    181 /*
    182  * setup() - performs all ONE TIME setup for this test.
    183  *	     Create a temporary directory and change directory to it.
    184  *	     Invoke individual test setup functions according to the order
    185  *	     set in test struct. definition.
    186  */
    187 void setup(void)
    188 {
    189 	int ind;
    190 
    191 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
    192 
    193 	TEST_PAUSE;
    194 
    195 	tst_tmpdir();
    196 
    197 	/* call individual setup functions */
    198 	for (ind = 0; Test_cases[ind].desc != NULL; ind++) {
    199 		Test_cases[ind].setupfunc();
    200 	}
    201 }
    202 
    203 /*
    204  * no_setup() - This is a dummy function which simply returns 0.
    205  */
    206 int no_setup(void)
    207 {
    208 	return 0;
    209 }
    210 
    211 /*
    212  * setup1() - setup function for a test condition for which lseek(2)
    213  *	      returns -1 and sets errno to ESPIPE.
    214  *	      Creat a named pipe/fifo using mknod() and open it for
    215  *	      reading/writing.
    216  *	      This function returns 0 on success.
    217  */
    218 int setup1(void)
    219 {
    220 	/* Creat a named pipe/fifo using mknod() */
    221 	if (mknod(TEMP_FILE1, PIPE_MODE, 0) < 0) {
    222 		tst_brkm(TBROK, cleanup,
    223 			 "mknod(%s, %#o, 0) Failed, errno=%d :%s",
    224 			 TEMP_FILE1, FILE_MODE, errno, strerror(errno));
    225 	}
    226 
    227 	/* Open the named pipe/fifo for reading/writing */
    228 	if ((fd1 = open(TEMP_FILE1, O_RDWR)) < 0) {
    229 		tst_brkm(TBROK, cleanup,
    230 			 "open(%s, O_RDWR) Failed, errno=%d, :%s",
    231 			 TEMP_FILE1, errno, strerror(errno));
    232 	}
    233 
    234 	return 0;
    235 }
    236 
    237 /*
    238  * setup2() - setup function for a test condition for which lseek(2)
    239  *	      returns -1 and sets errno to EINVAL.
    240  *	      Creat a temporary file for reading/writing and write some data
    241  *	      into it.
    242  *	      This function returns 0 on success.
    243  */
    244 int setup2(void)
    245 {
    246 	char write_buff[BUFSIZ];	/* buffer to hold data */
    247 
    248 	/* Get the data to be written to temporary file */
    249 	strcpy(write_buff, "abcdefg");
    250 
    251 	/* Creat/open a temporary file under above directory */
    252 	if ((fd2 = open(TEMP_FILE2, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
    253 		tst_brkm(TBROK, cleanup,
    254 			 "open(%s, O_RDWR|O_CREAT, %#o) Failed, errno=%d :%s",
    255 			 TEMP_FILE2, FILE_MODE, errno, strerror(errno));
    256 	}
    257 
    258 	/* Write data into temporary file */
    259 	if (write(fd2, write_buff, sizeof(write_buff)) <= 0) {
    260 		tst_brkm(TBROK, cleanup,
    261 			 "write(2) on %s Failed, errno=%d : %s",
    262 			 TEMP_FILE2, errno, strerror(errno));
    263 	}
    264 
    265 	return 0;
    266 }
    267 
    268 /*
    269  * setup3() - setup function for a test condition for which lseek(2)
    270  *	      returns -1 and sets errno to EBADF.
    271  *	      Creat a temporary file for reading/writing and close it.
    272  *	      This function returns 0 on success.
    273  */
    274 int setup3(void)
    275 {
    276 	/* Creat/open a temporary file under above directory */
    277 	if ((fd3 = open(TEMP_FILE3, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
    278 		tst_brkm(TBROK, cleanup,
    279 			 "open(%s, O_RDWR|O_CREAT, %#o) Failed, errno=%d :%s",
    280 			 TEMP_FILE3, FILE_MODE, errno, strerror(errno));
    281 	}
    282 
    283 	/* Close the temporary file created above */
    284 	if (close(fd3) < 0) {
    285 		tst_brkm(TBROK, cleanup,
    286 			 "close(%s) Failed, errno=%d : %s:",
    287 			 TEMP_FILE3, errno, strerror(errno));
    288 	}
    289 
    290 	return 0;
    291 }
    292 
    293 /*
    294  * cleanup() - performs all ONE TIME cleanup for this test at
    295  *             completion or premature exit.
    296  *	       Remove the test directory and testfile(s) created in the setup.
    297  */
    298 void cleanup(void)
    299 {
    300 
    301 	/* Close the temporary file(s) created in setup1/setup2 */
    302 	if (close(fd1) < 0) {
    303 		tst_brkm(TFAIL, NULL,
    304 			 "close(%s) Failed, errno=%d : %s:",
    305 			 TEMP_FILE1, errno, strerror(errno));
    306 	}
    307 	if (close(fd2) < 0) {
    308 		tst_brkm(TFAIL, NULL,
    309 			 "close(%s) Failed, errno=%d : %s:",
    310 			 TEMP_FILE2, errno, strerror(errno));
    311 	}
    312 
    313 	tst_rmdir();
    314 
    315 }
    316