blob: 32b40487a4b796b449f9bdd080799bb3e5b5499c [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
13 * with this program; if not, write the Free Software Foundation, Inc., 59
14 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
15 *
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
34 * Test to verify flock(2) succeds with all kind of locks.
35 * 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
49 *
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"
73#include "usctest.h"
74
75void setup(void);
76void cleanup(void);
77
78/* 0 terminated list of expected errnos */
subrata_modak56207ce2009-03-23 13:35:39 +000079int exp_enos[] = { EWOULDBLOCK, EAGAIN, EINVAL, 0 };
robbiewe5bf4d02002-12-02 18:23:30 +000080
subrata_modak56207ce2009-03-23 13:35:39 +000081char *TCID = "flock01"; /* Test program identifier */
82int TST_TOTAL = 3; /* Total number of test cases */
robbiewe5bf4d02002-12-02 18:23:30 +000083extern int Tst_count;
84char filename[100];
85int fd;
86
subrata_modak56207ce2009-03-23 13:35:39 +000087struct test_case_t {
robbiewe5bf4d02002-12-02 18:23:30 +000088 int operation;
89 char *opt;
subrata_modak56207ce2009-03-23 13:35:39 +000090} test_cases[] = {
91 {
92 LOCK_SH, "Shared Lock"}, {
93 LOCK_UN, "Unlock"}, {
94LOCK_EX, "Exclusive Lock"},};
subrata_modakbdbaec52009-02-26 12:14:51 +000095
robbiewe5bf4d02002-12-02 18:23:30 +000096int main(int argc, char **argv)
97{
subrata_modak56207ce2009-03-23 13:35:39 +000098 int lc, i;
robbiewe5bf4d02002-12-02 18:23:30 +000099 /* loop counter */
subrata_modak56207ce2009-03-23 13:35:39 +0000100 char *msg; /* message returned from parse_opts */
robbiewe5bf4d02002-12-02 18:23:30 +0000101
102 /* parse standard options */
Garrett Cooper45e285d2010-11-22 12:19:25 -0800103 if ((msg = parse_opts(argc, argv, NULL, NULL)) !=
104 NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -0800105 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
subrata_modak56207ce2009-03-23 13:35:39 +0000106 /*NOTREACHED*/}
robbiewe5bf4d02002-12-02 18:23:30 +0000107
108 /* global setup */
109 setup();
110
robbiewe5bf4d02002-12-02 18:23:30 +0000111 /* The following loop checks looping state if -i option given */
112
113 for (lc = 0; TEST_LOOPING(lc); lc++) {
114
115 /* reset Tst_count in case we are looping */
116 Tst_count = 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000117
118 for (i = 0; i < TST_TOTAL; ++i) {
119
120 /* Testing system call */
121 TEST(flock(fd, test_cases[i].operation));
122
123 if (TEST_RETURN == -1) {
124 TEST_ERROR_LOG(TEST_ERRNO);
125 tst_resm(TFAIL,
126 "flock() failed to get %s, error number=%d : %s",
127 test_cases[i].opt, TEST_ERRNO,
128 strerror(TEST_ERRNO));
129 continue; /*next loop for MTKERNEL */
130 } else {
131 tst_resm(TPASS,
132 "flock() succeded with %s, returned error number=%d",
133 test_cases[i].opt, TEST_ERRNO);
134 }
135
robbiewe5bf4d02002-12-02 18:23:30 +0000136 }
subrata_modak56207ce2009-03-23 13:35:39 +0000137
138 } /* End of TEST_LOOPING */
robbiewe5bf4d02002-12-02 18:23:30 +0000139
subrata_modakdad6e1a2007-10-30 10:46:58 +0000140 close(fd);
141
robbiewe5bf4d02002-12-02 18:23:30 +0000142 cleanup();
143
144 return 0;
145
146}
147
robbiewe5bf4d02002-12-02 18:23:30 +0000148/*
149 * setup()
150 * performs all ONE TIME setup for this test
151 */
subrata_modak56207ce2009-03-23 13:35:39 +0000152void setup(void)
robbiewe5bf4d02002-12-02 18:23:30 +0000153{
154 /* capture signals */
155 tst_sig(FORK, DEF_HANDLER, cleanup);
156
robbiewe5bf4d02002-12-02 18:23:30 +0000157 /* Set up the expected error numbers for -e option */
158 TEST_EXP_ENOS(exp_enos);
159
160 /* Pause if that option was specified
161 * TEST_PAUSE contains the code to fork the test with the -i option.
162 * You want to make sure you do this before you create your temporary
163 * directory.
164 */
165 TEST_PAUSE;
166
167 /* Create a unique temporary directory and chdir() to it. */
168 tst_tmpdir();
169
170 sprintf(filename, "flock01.%d", getpid());
171
172 /* creating temporary file */
173 fd = creat(filename, 0644);
174 if (fd < 0) {
175 tst_resm(TFAIL, "creating a new file failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000176
robbiewe5bf4d02002-12-02 18:23:30 +0000177 TEST_CLEANUP;
subrata_modak56207ce2009-03-23 13:35:39 +0000178
robbiewe5bf4d02002-12-02 18:23:30 +0000179 /* Removing temp directory */
180 tst_rmdir();
subrata_modak56207ce2009-03-23 13:35:39 +0000181
subrata_modak4bb656a2009-02-26 12:02:09 +0000182 /* exit with resturn code appropriate for result */
robbiewe5bf4d02002-12-02 18:23:30 +0000183 tst_exit();
subrata_modak56207ce2009-03-23 13:35:39 +0000184
robbiewe5bf4d02002-12-02 18:23:30 +0000185 }
186}
187
188/*
189 * cleanup()
190 * performs all ONE TIME cleanup for this test at
191 * completion or premature exit
192 */
subrata_modak56207ce2009-03-23 13:35:39 +0000193void cleanup(void)
robbiewe5bf4d02002-12-02 18:23:30 +0000194{
195 /*
196 * print timing stats if that option was specified.
197 * print errno log if that option was specified.
198 */
199 TEST_CLEANUP;
200
201 unlink(filename);
202 tst_rmdir();
203
204 /* exit with return code appropriate for results */
205 tst_exit();
subrata_modak56207ce2009-03-23 13:35:39 +0000206 /*NOTREACHED*/}