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   : flock01
     20  *
     21  *    EXECUTED BY       : anyone
     22  *
     23  *    TEST TITLE        : Basic 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  * 	Test to verify flock(2) succeeds with all kind of locks.
     35  *	Intends to provide a limited exposure of system call.
     36  *    $
     37  *	Setup:
     38  *        Setup signal handling.
     39  *        Pause for SIGUSR1 if option specified.
     40  *        Create a temporary directory and chdir to it.
     41  * 	  Create a temporary file
     42  *
     43  *	Test:
     44  *	Loop if proper options are given.
     45  *		Execute system call
     46  *		Check return code, if system call failed (return == -1)
     47  *				Log the error number and issue a FAIL message
     48  *		otherwise issue a PASS message
     49  *
     50  *      Cleanup:
     51  *        Print errno log and/or timing stats if options given
     52  *	  Deletes temporary directory.
     53  *
     54  * USAGE:  <for command-line>
     55  *      flock01 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-h] [-f] [-p]
     56  *                      where,  -c n : Run n copies concurrently.
     57  *                              -f   : Turn off functional testing
     58  *    				-e   : Turn on errno logging.
     59  *                              -h   : Show help screen                        $
     60  *				-i n : Execute test n times.
     61  *                              -I x : Execute test for x seconds.
     62  *                              -p   : Pause for SIGUSR1 before starting
     63  *                              -P x : Pause for x seconds between iterations.
     64  *                              -t   : Turn on syscall timing.
     65  *
     66  ****************************************************************/
     67 
     68 #include <errno.h>
     69 #include <stdio.h>
     70 #include <sys/wait.h>
     71 #include <sys/file.h>
     72 #include "test.h"
     73 
     74 void setup(void);
     75 void cleanup(void);
     76 
     77 char *TCID = "flock01";
     78 int TST_TOTAL = 3;
     79 char filename[100];
     80 int fd;
     81 
     82 struct test_case_t {
     83 	int operation;
     84 	char *opt;
     85 } test_cases[] = {
     86 	{ LOCK_SH, "Shared Lock" },
     87 	{ LOCK_UN, "Unlock"},
     88 	{ LOCK_EX, "Exclusive Lock"}
     89 };
     90 
     91 int main(int argc, char **argv)
     92 {
     93 	int lc, i;
     94 
     95 	tst_parse_opts(argc, argv, NULL, NULL);
     96 
     97 	/* global setup */
     98 	setup();
     99 
    100 	/* The following loop checks looping state if -i option given */
    101 
    102 	for (lc = 0; TEST_LOOPING(lc); lc++) {
    103 
    104 		/* reset tst_count in case we are looping */
    105 		tst_count = 0;
    106 
    107 		for (i = 0; i < TST_TOTAL; ++i) {
    108 
    109 			/* Testing system call */
    110 			TEST(flock(fd, test_cases[i].operation));
    111 			if (TEST_RETURN == -1) {
    112 				tst_resm(TFAIL | TTERRNO,
    113 					 "flock() failed to get %s",
    114 					 test_cases[i].opt);
    115 				continue;	/*next loop for MTKERNEL  */
    116 			} else {
    117 				tst_resm(TPASS,
    118 					 "flock() succeeded with %s",
    119 					 test_cases[i].opt);
    120 			}
    121 
    122 		}
    123 
    124 	}
    125 
    126 	close(fd);
    127 
    128 	cleanup();
    129 
    130 	tst_exit();
    131 
    132 }
    133 
    134 /*
    135  * setup()
    136  *	performs all ONE TIME setup for this test
    137  */
    138 void setup(void)
    139 {
    140 
    141 	tst_sig(FORK, DEF_HANDLER, cleanup);
    142 
    143 	/* Pause if that option was specified
    144 	 * TEST_PAUSE contains the code to fork the test with the -i option.
    145 	 * You want to make sure you do this before you create your temporary
    146 	 * directory.
    147 	 */
    148 	TEST_PAUSE;
    149 
    150 	/* Create a unique temporary directory and chdir() to it. */
    151 	tst_tmpdir();
    152 
    153 	sprintf(filename, "flock01.%d", getpid());
    154 
    155 	/* creating temporary file */
    156 	fd = creat(filename, 0644);
    157 	if (fd < 0)
    158 		tst_brkm(TBROK, tst_rmdir, "creating a new file failed");
    159 }
    160 
    161 /*
    162  * cleanup()
    163  *	performs all ONE TIME cleanup for this test at
    164  * 	completion or premature exit
    165  */
    166 void cleanup(void)
    167 {
    168 
    169 	unlink(filename);
    170 	tst_rmdir();
    171 
    172 }
    173