blob: f0192ca8e03fe31f112a77a2afc1deed1fafcfb7 [file] [log] [blame]
robbiewf9c0e012005-05-10 18:13:02 +00001/*
2 *
3 * Copyright (C) Bull S.A. 2005
4 * Copyright (c) International Business Machines Corp., 2005
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
14 * the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
robbiewf9c0e012005-05-10 18:13:02 +000019 */
20
21/**********************************************************
subrata_modak4bb656a2009-02-26 12:02:09 +000022 *
robbiewf9c0e012005-05-10 18:13:02 +000023 * TEST IDENTIFIER : fcntl26
subrata_modak4bb656a2009-02-26 12:02:09 +000024 *
robbiewf9c0e012005-05-10 18:13:02 +000025 * EXECUTED BY : anyone
subrata_modak4bb656a2009-02-26 12:02:09 +000026 *
robbiewf9c0e012005-05-10 18:13:02 +000027 * TEST TITLE : Basic test for fcntl(2) using F_SETLEASE & F_WRLCK argument.
subrata_modak4bb656a2009-02-26 12:02:09 +000028 *
robbiewf9c0e012005-05-10 18:13:02 +000029 * TEST CASE TOTAL : 1
subrata_modak4bb656a2009-02-26 12:02:09 +000030 *
robbiewf9c0e012005-05-10 18:13:02 +000031 * WALL CLOCK TIME : 1
subrata_modak4bb656a2009-02-26 12:02:09 +000032 *
subrata_modak56207ce2009-03-23 13:35:39 +000033 * CPU TYPES : ALL
subrata_modak4bb656a2009-02-26 12:02:09 +000034 *
subrata_modak56207ce2009-03-23 13:35:39 +000035 * AUTHOR : Jacky Malcles
subrata_modak4bb656a2009-02-26 12:02:09 +000036 *
robbiewf9c0e012005-05-10 18:13:02 +000037 * TEST CASES
subrata_modak4bb656a2009-02-26 12:02:09 +000038 *
subrata_modak56207ce2009-03-23 13:35:39 +000039 * 1.) fcntl(2) returns...(See Description)
Garrett Cooper2c282152010-12-16 00:55:50 -080040 *
robbiewf9c0e012005-05-10 18:13:02 +000041 * INPUT SPECIFICATIONS
subrata_modak56207ce2009-03-23 13:35:39 +000042 * The standard options for system call tests are accepted.
robbiewf9c0e012005-05-10 18:13:02 +000043 * (See the parse_opts(3) man page).
subrata_modak4bb656a2009-02-26 12:02:09 +000044 *
robbiewf9c0e012005-05-10 18:13:02 +000045 * OUTPUT SPECIFICATIONS
subrata_modak56207ce2009-03-23 13:35:39 +000046 *
robbiewf9c0e012005-05-10 18:13:02 +000047 * DURATION
subrata_modak56207ce2009-03-23 13:35:39 +000048 * Terminates - with frequency and infinite modes.
subrata_modak4bb656a2009-02-26 12:02:09 +000049 *
robbiewf9c0e012005-05-10 18:13:02 +000050 * SIGNALS
subrata_modak56207ce2009-03-23 13:35:39 +000051 * Uses SIGUSR1 to pause before test if option set.
52 * (See the parse_opts(3) man page).
robbiewf9c0e012005-05-10 18:13:02 +000053 *
54 * RESOURCES
subrata_modak56207ce2009-03-23 13:35:39 +000055 * None
subrata_modak4bb656a2009-02-26 12:02:09 +000056 *
robbiewf9c0e012005-05-10 18:13:02 +000057 * ENVIRONMENTAL NEEDS
58 * No run-time environmental needs.
subrata_modak4bb656a2009-02-26 12:02:09 +000059 *
robbiewf9c0e012005-05-10 18:13:02 +000060 * SPECIAL PROCEDURAL REQUIREMENTS
subrata_modak56207ce2009-03-23 13:35:39 +000061 * None
subrata_modak4bb656a2009-02-26 12:02:09 +000062 *
robbiewf9c0e012005-05-10 18:13:02 +000063 * INTERCASE DEPENDENCIES
subrata_modak56207ce2009-03-23 13:35:39 +000064 * None
subrata_modak4bb656a2009-02-26 12:02:09 +000065 *
robbiewf9c0e012005-05-10 18:13:02 +000066 * DETAILED DESCRIPTION
67 * This is a Phase I test for the fcntl(2) system call. It is intended
68 * to provide a limited exposure of the system call, for now. It
69 * should/will be extended when full functional tests are written for
70 * fcntl(2).
subrata_modak4bb656a2009-02-26 12:02:09 +000071 *
subrata_modak56207ce2009-03-23 13:35:39 +000072 * Setup:
73 * Setup signal handling.
robbiewf9c0e012005-05-10 18:13:02 +000074 * Pause for SIGUSR1 if option specified.
subrata_modak4bb656a2009-02-26 12:02:09 +000075 *
subrata_modak56207ce2009-03-23 13:35:39 +000076 * Test:
robbiewf9c0e012005-05-10 18:13:02 +000077 * Loop if the proper options are given.
subrata_modak56207ce2009-03-23 13:35:39 +000078 * Execute system call
robbiewf9c0e012005-05-10 18:13:02 +000079 * Check return code, if system call failed (return=-1)
subrata_modak56207ce2009-03-23 13:35:39 +000080 * Log the errno and Issue a FAIL message.
robbiewf9c0e012005-05-10 18:13:02 +000081 * Otherwise, Issue a PASS message.
subrata_modak4bb656a2009-02-26 12:02:09 +000082 *
subrata_modak56207ce2009-03-23 13:35:39 +000083 * Cleanup:
84 * Print errno log and/or timing stats if options given
subrata_modak4bb656a2009-02-26 12:02:09 +000085 *
86 *
robbiewf9c0e012005-05-10 18:13:02 +000087 *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/
88
89#include <sys/types.h>
90#include <sys/stat.h>
91#include <fcntl.h>
92#include <unistd.h>
93#include <errno.h>
94#include <string.h>
95#include <signal.h>
96#include "test.h"
Cyril Hrubis0f0e3482014-02-27 16:08:04 +010097#include "tst_fs_type.h"
robbiewf9c0e012005-05-10 18:13:02 +000098
vapieraa354722006-05-26 06:26:37 +000099void setup();
100void cleanup();
robbiewf9c0e012005-05-10 18:13:02 +0000101
Cyril Hrubisfdce7d52013-04-04 18:35:48 +0200102char *TCID = "fcntl26";
103int TST_TOTAL = 1;
robbiewf9c0e012005-05-10 18:13:02 +0000104
robbiewf9c0e012005-05-10 18:13:02 +0000105char fname[255];
106int fd;
107
subrata_modak56207ce2009-03-23 13:35:39 +0000108int main(int ac, char **av)
robbiewf9c0e012005-05-10 18:13:02 +0000109{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200110 int lc;
Cyril Hrubis0f0e3482014-02-27 16:08:04 +0100111 long type;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200112 const char *msg;
subrata_modak56207ce2009-03-23 13:35:39 +0000113
robbiewf9c0e012005-05-10 18:13:02 +0000114 /***************************************************************
115 * parse standard options
116 ***************************************************************/
Garrett Cooper45e285d2010-11-22 12:19:25 -0800117 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800118 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiewf9c0e012005-05-10 18:13:02 +0000119
120 /***************************************************************
121 * perform global setup for test
122 ***************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000123 setup();
robbiewf9c0e012005-05-10 18:13:02 +0000124
Cyril Hrubis0f0e3482014-02-27 16:08:04 +0100125 switch ((type = tst_fs_type(cleanup, "."))) {
126 case TST_NFS_MAGIC:
127 case TST_RAMFS_MAGIC:
128 case TST_TMPFS_MAGIC:
subrata_modak56207ce2009-03-23 13:35:39 +0000129 tst_brkm(TCONF, cleanup,
Cyril Hrubis0f0e3482014-02-27 16:08:04 +0100130 "Cannot do fcntl on a file on %s filesystem",
131 tst_fs_type_name(type));
132 break;
subrata_modaka52f4de2009-08-04 11:31:56 +0000133 }
134
robbiewf9c0e012005-05-10 18:13:02 +0000135 /***************************************************************
136 * check looping state if -c option given
137 ***************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000138 for (lc = 0; TEST_LOOPING(lc); lc++) {
robbiewf9c0e012005-05-10 18:13:02 +0000139
Caspar Zhangd59a6592013-03-07 14:59:12 +0800140 tst_count = 0;
robbiewf9c0e012005-05-10 18:13:02 +0000141
robbiewe93a3242005-08-31 20:27:12 +0000142#ifdef F_SETLEASE
subrata_modak56207ce2009-03-23 13:35:39 +0000143 /*
144 * Call fcntl(2) with F_SETLEASE & F_WRLCK argument on fname
145 */
146 TEST(fcntl(fd, F_SETLEASE, F_WRLCK));
147
148 /* check return code */
149 if (TEST_RETURN == -1) {
subrata_modak56207ce2009-03-23 13:35:39 +0000150 tst_resm(TFAIL,
151 "fcntl(%s, F_SETLEASE, F_WRLCK) Failed, errno=%d : %s",
152 fname, TEST_ERRNO, strerror(TEST_ERRNO));
153 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200154 TEST(fcntl(fd, F_GETLEASE));
155 if (TEST_RETURN != F_WRLCK)
156 tst_resm(TFAIL,
157 "fcntl(%s, F_GETLEASE) did not return F_WRLCK, returned %ld",
158 fname, TEST_RETURN);
159 else {
160 TEST(fcntl(fd, F_SETLEASE, F_UNLCK));
161 if (TEST_RETURN != 0)
subrata_modak56207ce2009-03-23 13:35:39 +0000162 tst_resm(TFAIL,
Cyril Hrubise38b9612014-06-02 17:20:57 +0200163 "fcntl(%s, F_SETLEASE, F_UNLCK) did not return 0, returned %ld",
subrata_modak56207ce2009-03-23 13:35:39 +0000164 fname, TEST_RETURN);
Cyril Hrubise38b9612014-06-02 17:20:57 +0200165 else
166 tst_resm(TPASS,
167 "fcntl(%s, F_SETLEASE, F_WRLCK)",
168 fname);
subrata_modak56207ce2009-03-23 13:35:39 +0000169 }
170 }
vapier75467512007-09-24 05:34:19 +0000171#else
172 tst_resm(TINFO, "F_SETLEASE not defined, skipping test");
robbiewe93a3242005-08-31 20:27:12 +0000173#endif
Garrett Cooper2c282152010-12-16 00:55:50 -0800174 }
robbiewf9c0e012005-05-10 18:13:02 +0000175
subrata_modak56207ce2009-03-23 13:35:39 +0000176 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800177 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800178}
robbiewf9c0e012005-05-10 18:13:02 +0000179
180/***************************************************************
181 * setup() - performs all ONE TIME setup for this test.
182 ***************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -0400183void setup(void)
robbiewf9c0e012005-05-10 18:13:02 +0000184{
Garrett Cooper2c282152010-12-16 00:55:50 -0800185
subrata_modak56207ce2009-03-23 13:35:39 +0000186 tst_sig(NOFORK, DEF_HANDLER, cleanup);
robbiewf9c0e012005-05-10 18:13:02 +0000187
subrata_modak56207ce2009-03-23 13:35:39 +0000188 TEST_PAUSE;
robbiewf9c0e012005-05-10 18:13:02 +0000189
subrata_modak56207ce2009-03-23 13:35:39 +0000190 tst_tmpdir();
robbiewf9c0e012005-05-10 18:13:02 +0000191
subrata_modak56207ce2009-03-23 13:35:39 +0000192 sprintf(fname, "tfile_%d", getpid());
193 if ((fd = open(fname, O_WRONLY | O_CREAT, 0777)) == -1) {
194 tst_brkm(TBROK, cleanup,
195 "open(%s, O_WRONLY|O_CREAT,0777) Failed, errno=%d : %s",
196 fname, errno, strerror(errno));
197 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800198}
robbiewf9c0e012005-05-10 18:13:02 +0000199
200/***************************************************************
201 * cleanup() - performs all ONE TIME cleanup for this test at
subrata_modak56207ce2009-03-23 13:35:39 +0000202 * completion or premature exit.
robbiewf9c0e012005-05-10 18:13:02 +0000203 ***************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -0400204void cleanup(void)
robbiewf9c0e012005-05-10 18:13:02 +0000205{
robbiewf9c0e012005-05-10 18:13:02 +0000206
subrata_modak56207ce2009-03-23 13:35:39 +0000207 /* close the file we've had open */
208 if (close(fd) == -1) {
209 tst_resm(TWARN, "close(%s) Failed, errno=%d : %s", fname, errno,
210 strerror(errno));
211 }
robbiewf9c0e012005-05-10 18:13:02 +0000212
subrata_modak56207ce2009-03-23 13:35:39 +0000213 tst_rmdir();
robbiewf9c0e012005-05-10 18:13:02 +0000214
Chris Dearmanec6edca2012-10-17 19:54:01 -0700215}