Home | History | Annotate | Download | only in flock
      1 /*
      2  * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
      3  *
      4  * This program is free software; you can redistribute it and/or modify it
      5  * under the terms of version 2 of the GNU General Public License as
      6  * published by the Free Software Foundation.
      7  *
      8  * This program is distributed in the hope that it would be useful, but
      9  * WITHOUT ANY WARRANTY; without even the implied warranty of
     10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
     11  *
     12  * You should have received a copy of the GNU General Public License along
     13  * with this program; if not, write the Free Software Foundation, Inc.,
     14  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
     15  *
     16  */
     17 /**********************************************************
     18  *
     19  *    TEST IDENTIFIER   : flock02
     20  *
     21  *    EXECUTED BY       : anyone
     22  *
     23  *    TEST TITLE        : Error condition test for flock(2)
     24  *
     25  *    TEST CASE TOTAL   : 3
     26  *
     27  *    AUTHOR            : Vatsal Avasthi <vatsal.avasthi (at) wipro.com>
     28  *
     29  *    SIGNALS
     30  *      Uses SIGUSR1 to pause before test if option set.
     31  *      (See the parse_opts(3) man page).
     32  *
     33  *    DESCRIPTION
     34  * 	This test verifies that
     35  *	1)	flock(2) returns -1 and sets error number to EBADF
     36  *		if the file descriptor is invalid.
     37  *      2)	flock(2) returns -1 and sets error number to EINVAL
     38  *		if the argument operation does not include LOCK_SH,LOCK_EX,LOCK_UN.$
     39  *	3)	flock(2) returns -1 and sets error number to EINVAL
     40  *		if an invalid combination of locking modes is used i.e LOCK_SH with LOCK_EX
     41  *
     42  *	Setup:
     43  *        Setup signal handling.
     44  *        Pause for SIGUSR1 if option specified.
     45  *        Create a temporary directory and chdir to it.
     46  * 	  Create a temporary file
     47  *
     48  *	Test:
     49  *	Loop if proper options are given.
     50  *		Execute system call
     51  *		Check return code,
     52  *			if system call failed (return == -1) and errno == expected_errno
     53  *				Issue system call fails with expected return value and error number
     54  *			else
     55  *				Issue system call failed to produce expected error.
     56  *
     57  *      Cleanup:
     58  *        Print errno log and/or timing stats if options given
     59  *	  Deletes temporary directory.
     60  *
     61  * USAGE:  <for command-line>
     62  *      flock02 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-h] [-f] [-p]
     63  *                      where,  -c n : Run n copies concurrently.
     64  *                              -f   : Turn off functional testing
     65  *    				-e   : Turn on errno logging.
     66  *                              -h   : Show help screen                        $
     67  *				-i n : Execute test n times.
     68  *                              -I x : Execute test for x seconds.
     69  *                              -p   : Pause for SIGUSR1 before starting
     70  *                              -P x : Pause for x seconds between iterations.
     71  *                              -t   : Turn on syscall timing.
     72  *
     73  ****************************************************************/
     74 
     75 #include <sys/types.h>
     76 #include <sys/file.h>
     77 #include <sys/wait.h>
     78 #include <errno.h>
     79 #include <stdio.h>
     80 #include "test.h"
     81 
     82 void setup(void);
     83 void cleanup(void);
     84 
     85 char *TCID = "flock02";
     86 int TST_TOTAL = 3;
     87 char filename[100];
     88 int fd;
     89 
     90 int main(int argc, char **argv)
     91 {
     92 	int lc;
     93 
     94 	tst_parse_opts(argc, argv, NULL, NULL);
     95 
     96 	setup();
     97 
     98 	for (lc = 0; TEST_LOOPING(lc); lc++) {
     99 
    100 		tst_count = 0;
    101 
    102 		TEST(flock(-1, LOCK_SH));
    103 
    104 		if (TEST_RETURN == -1 && TEST_ERRNO == EBADF)
    105 			tst_resm(TPASS, "flock failed as expected with EBADF");
    106 		else if (TEST_RETURN == 0)
    107 			tst_resm(TFAIL, "flock succeeded unexpectedly");
    108 		else
    109 			tst_resm(TFAIL | TTERRNO, "flock failed unexpectedly");
    110 
    111 		/* Test system call with invalid argument */
    112 		TEST(flock(fd, LOCK_NB));
    113 
    114 		if (TEST_RETURN == -1 && TEST_ERRNO == EINVAL)
    115 			tst_resm(TPASS, "flock failed as expected with EINVAL");
    116 		else if (TEST_RETURN == 0)
    117 			tst_resm(TFAIL, "flock succeeded unexpectedly");
    118 		else
    119 			tst_resm(TFAIL | TTERRNO, "flock failed unexpectedly");
    120 
    121 		/* Test system call with invalid combination of arguments */
    122 		TEST(flock(fd, LOCK_SH | LOCK_EX));
    123 
    124 		if (TEST_RETURN == -1 && TEST_ERRNO == EINVAL) {
    125 			tst_resm(TPASS, "flock failed as expected with EINVAL");
    126 			continue;	/*next loop for MTKERNEL  */
    127 		} else if (TEST_RETURN == 0)
    128 			tst_resm(TFAIL, "flock succeeded unexpectedly");
    129 		else
    130 			tst_resm(TFAIL | TTERRNO, "flock failed unexpectedly");
    131 
    132 	}
    133 
    134 	close(fd);
    135 
    136 	cleanup();
    137 
    138 	tst_exit();
    139 
    140 }
    141 
    142 void setup(void)
    143 {
    144 
    145 	tst_sig(FORK, DEF_HANDLER, cleanup);
    146 
    147 	TEST_PAUSE;
    148 
    149 	tst_tmpdir();
    150 
    151 	sprintf(filename, "flock02.%d", getpid());
    152 
    153 	fd = creat(filename, 0666);
    154 	if (fd < 0)
    155 		tst_brkm(TFAIL | TERRNO, cleanup, "creat failed");
    156 }
    157 
    158 void cleanup(void)
    159 {
    160 	unlink(filename);
    161 	tst_rmdir();
    162 
    163 }
    164