blob: 3af85dbfe40af639310fcebf20344794f1d28574 [file] [log] [blame]
robbiewe5bf4d02002-12-02 18:23:30 +00001/*
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
Wanlong Gaofed96412012-10-24 10:10:29 +080013 * with this program; if not, write the Free Software Foundation, Inc.,
14 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
robbiewe5bf4d02002-12-02 18:23:30 +000015 *
16 */
17/**********************************************************
subrata_modak4bb656a2009-02-26 12:02:09 +000018 *
robbiewe5bf4d02002-12-02 18:23:30 +000019 * TEST IDENTIFIER : flock01
subrata_modak4bb656a2009-02-26 12:02:09 +000020 *
robbiewe5bf4d02002-12-02 18:23:30 +000021 * EXECUTED BY : anyone
subrata_modak4bb656a2009-02-26 12:02:09 +000022 *
robbiewe5bf4d02002-12-02 18:23:30 +000023 * TEST TITLE : Basic test for flock(2)
subrata_modak4bb656a2009-02-26 12:02:09 +000024 *
robbiewe5bf4d02002-12-02 18:23:30 +000025 * TEST CASE TOTAL : 3
subrata_modak4bb656a2009-02-26 12:02:09 +000026 *
robbiewe5bf4d02002-12-02 18:23:30 +000027 * AUTHOR : Vatsal Avasthi <vatsal.avasthi@wipro.com>
subrata_modak4bb656a2009-02-26 12:02:09 +000028 *
robbiewe5bf4d02002-12-02 18:23:30 +000029 * SIGNALS
30 * Uses SIGUSR1 to pause before test if option set.
31 * (See the parse_opts(3) man page).
32 *
33 * DESCRIPTION
Sedat Dilek385d7922013-01-16 15:54:15 +010034 * Test to verify flock(2) succeeds with all kind of locks.
robbiewe5bf4d02002-12-02 18:23:30 +000035 * Intends to provide a limited exposure of system call.
subrata_modak56207ce2009-03-23 13:35:39 +000036 * $
robbiewe5bf4d02002-12-02 18:23:30 +000037 * 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
subrata_modak4bb656a2009-02-26 12:02:09 +000042 *
robbiewe5bf4d02002-12-02 18:23:30 +000043 * 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
subrata_modakbdbaec52009-02-26 12:14:51 +000048 * otherwise issue a PASS message
Garrett Cooper2c282152010-12-16 00:55:50 -080049 *
robbiewe5bf4d02002-12-02 18:23:30 +000050 * Cleanup:
51 * Print errno log and/or timing stats if options given
52 * Deletes temporary directory.
subrata_modak4bb656a2009-02-26 12:02:09 +000053 *
robbiewe5bf4d02002-12-02 18:23:30 +000054 * 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.
subrata_modak56207ce2009-03-23 13:35:39 +000059 * -h : Show help screen $
robbiewe5bf4d02002-12-02 18:23:30 +000060 * -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
robbiewe5bf4d02002-12-02 18:23:30 +000068#include <errno.h>
69#include <stdio.h>
70#include <sys/wait.h>
71#include <sys/file.h>
72#include "test.h"
robbiewe5bf4d02002-12-02 18:23:30 +000073
74void setup(void);
75void cleanup(void);
76
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020077char *TCID = "flock01";
78int TST_TOTAL = 3;
robbiewe5bf4d02002-12-02 18:23:30 +000079char filename[100];
80int fd;
81
subrata_modak56207ce2009-03-23 13:35:39 +000082struct test_case_t {
robbiewe5bf4d02002-12-02 18:23:30 +000083 int operation;
84 char *opt;
subrata_modak56207ce2009-03-23 13:35:39 +000085} test_cases[] = {
86 {
87 LOCK_SH, "Shared Lock"}, {
88 LOCK_UN, "Unlock"}, {
89LOCK_EX, "Exclusive Lock"},};
subrata_modakbdbaec52009-02-26 12:14:51 +000090
robbiewe5bf4d02002-12-02 18:23:30 +000091int main(int argc, char **argv)
92{
subrata_modak56207ce2009-03-23 13:35:39 +000093 int lc, i;
robbiewe5bf4d02002-12-02 18:23:30 +000094 /* loop counter */
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020095 const char *msg;
robbiewe5bf4d02002-12-02 18:23:30 +000096
Wanlong Gao354ebb42012-12-07 10:10:04 +080097 if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -080098 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Wanlong Gao354ebb42012-12-07 10:10:04 +080099 }
robbiewe5bf4d02002-12-02 18:23:30 +0000100
101 /* global setup */
102 setup();
103
robbiewe5bf4d02002-12-02 18:23:30 +0000104 /* The following loop checks looping state if -i option given */
105
106 for (lc = 0; TEST_LOOPING(lc); lc++) {
107
Caspar Zhangd59a6592013-03-07 14:59:12 +0800108 /* reset tst_count in case we are looping */
109 tst_count = 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000110
111 for (i = 0; i < TST_TOTAL; ++i) {
112
113 /* Testing system call */
114 TEST(flock(fd, test_cases[i].operation));
115
116 if (TEST_RETURN == -1) {
subrata_modak56207ce2009-03-23 13:35:39 +0000117 tst_resm(TFAIL,
118 "flock() failed to get %s, error number=%d : %s",
119 test_cases[i].opt, TEST_ERRNO,
120 strerror(TEST_ERRNO));
121 continue; /*next loop for MTKERNEL */
122 } else {
123 tst_resm(TPASS,
Sedat Dilek385d7922013-01-16 15:54:15 +0100124 "flock() succeeded with %s, returned error number=%d",
subrata_modak56207ce2009-03-23 13:35:39 +0000125 test_cases[i].opt, TEST_ERRNO);
126 }
127
robbiewe5bf4d02002-12-02 18:23:30 +0000128 }
subrata_modak56207ce2009-03-23 13:35:39 +0000129
Garrett Cooper2c282152010-12-16 00:55:50 -0800130 }
robbiewe5bf4d02002-12-02 18:23:30 +0000131
subrata_modakdad6e1a2007-10-30 10:46:58 +0000132 close(fd);
133
robbiewe5bf4d02002-12-02 18:23:30 +0000134 cleanup();
135
Garrett Cooper2c282152010-12-16 00:55:50 -0800136 tst_exit();
robbiewe5bf4d02002-12-02 18:23:30 +0000137
138}
139
robbiewe5bf4d02002-12-02 18:23:30 +0000140/*
141 * setup()
142 * performs all ONE TIME setup for this test
143 */
subrata_modak56207ce2009-03-23 13:35:39 +0000144void setup(void)
robbiewe5bf4d02002-12-02 18:23:30 +0000145{
Garrett Cooper2c282152010-12-16 00:55:50 -0800146
robbiewe5bf4d02002-12-02 18:23:30 +0000147 tst_sig(FORK, DEF_HANDLER, cleanup);
148
robbiewe5bf4d02002-12-02 18:23:30 +0000149 /* Pause if that option was specified
150 * TEST_PAUSE contains the code to fork the test with the -i option.
151 * You want to make sure you do this before you create your temporary
152 * directory.
153 */
154 TEST_PAUSE;
155
156 /* Create a unique temporary directory and chdir() to it. */
157 tst_tmpdir();
158
159 sprintf(filename, "flock01.%d", getpid());
160
161 /* creating temporary file */
162 fd = creat(filename, 0644);
163 if (fd < 0) {
164 tst_resm(TFAIL, "creating a new file failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000165
robbiewe5bf4d02002-12-02 18:23:30 +0000166 /* Removing temp directory */
167 tst_rmdir();
subrata_modak56207ce2009-03-23 13:35:39 +0000168
subrata_modak4bb656a2009-02-26 12:02:09 +0000169 /* exit with resturn code appropriate for result */
robbiewe5bf4d02002-12-02 18:23:30 +0000170 tst_exit();
subrata_modak56207ce2009-03-23 13:35:39 +0000171
robbiewe5bf4d02002-12-02 18:23:30 +0000172 }
173}
174
175/*
176 * cleanup()
177 * performs all ONE TIME cleanup for this test at
178 * completion or premature exit
179 */
subrata_modak56207ce2009-03-23 13:35:39 +0000180void cleanup(void)
robbiewe5bf4d02002-12-02 18:23:30 +0000181{
robbiewe5bf4d02002-12-02 18:23:30 +0000182
183 unlink(filename);
184 tst_rmdir();
185
Wanlong Gao354ebb42012-12-07 10:10:04 +0800186}