blob: d3f672f9310d3bd4d2adfc818e68476085c34b15 [file] [log] [blame]
robbiew25ae5452004-03-02 22:55:42 +00001/*
2 *
3 * Copyright (c) International Business Machines Corp., 2004
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
robbiew25ae5452004-03-02 22:55:42 +000018 */
19
20/**********************************************************
subrata_modak4bb656a2009-02-26 12:02:09 +000021 *
robbiewf9c0e012005-05-10 18:13:02 +000022 * TEST IDENTIFIER : fcntl24
subrata_modak4bb656a2009-02-26 12:02:09 +000023 *
robbiew25ae5452004-03-02 22:55:42 +000024 * EXECUTED BY : anyone
subrata_modak4bb656a2009-02-26 12:02:09 +000025 *
robbiew25ae5452004-03-02 22:55:42 +000026 * TEST TITLE : Basic test for fcntl(2) using F_SETLEASE & F_WRLCK argument.
subrata_modak4bb656a2009-02-26 12:02:09 +000027 *
robbiew25ae5452004-03-02 22:55:42 +000028 * TEST CASE TOTAL : 1
subrata_modak4bb656a2009-02-26 12:02:09 +000029 *
robbiew25ae5452004-03-02 22:55:42 +000030 * WALL CLOCK TIME : 1
subrata_modak4bb656a2009-02-26 12:02:09 +000031 *
robbiew25ae5452004-03-02 22:55:42 +000032 * CPU TYPES : ALL
subrata_modak4bb656a2009-02-26 12:02:09 +000033 *
robbiew25ae5452004-03-02 22:55:42 +000034 * AUTHOR : Robbie Williamson
subrata_modak4bb656a2009-02-26 12:02:09 +000035 *
robbiew25ae5452004-03-02 22:55:42 +000036 * TEST CASES
subrata_modak4bb656a2009-02-26 12:02:09 +000037 *
subrata_modak56207ce2009-03-23 13:35:39 +000038 * 1.) fcntl(2) returns...(See Description)
subrata_modakbdbaec52009-02-26 12:14:51 +000039 *
robbiew25ae5452004-03-02 22:55:42 +000040 * INPUT SPECIFICATIONS
subrata_modak56207ce2009-03-23 13:35:39 +000041 * The standard options for system call tests are accepted.
robbiew25ae5452004-03-02 22:55:42 +000042 * (See the parse_opts(3) man page).
subrata_modak4bb656a2009-02-26 12:02:09 +000043 *
robbiew25ae5452004-03-02 22:55:42 +000044 * OUTPUT SPECIFICATIONS
subrata_modak56207ce2009-03-23 13:35:39 +000045 *
robbiew25ae5452004-03-02 22:55:42 +000046 * DURATION
subrata_modak56207ce2009-03-23 13:35:39 +000047 * Terminates - with frequency and infinite modes.
subrata_modak4bb656a2009-02-26 12:02:09 +000048 *
robbiew25ae5452004-03-02 22:55:42 +000049 * SIGNALS
subrata_modak56207ce2009-03-23 13:35:39 +000050 * Uses SIGUSR1 to pause before test if option set.
51 * (See the parse_opts(3) man page).
robbiew25ae5452004-03-02 22:55:42 +000052 *
53 * RESOURCES
subrata_modak56207ce2009-03-23 13:35:39 +000054 * None
subrata_modak4bb656a2009-02-26 12:02:09 +000055 *
robbiew25ae5452004-03-02 22:55:42 +000056 * ENVIRONMENTAL NEEDS
57 * No run-time environmental needs.
subrata_modak4bb656a2009-02-26 12:02:09 +000058 *
robbiew25ae5452004-03-02 22:55:42 +000059 * SPECIAL PROCEDURAL REQUIREMENTS
subrata_modak56207ce2009-03-23 13:35:39 +000060 * None
subrata_modak4bb656a2009-02-26 12:02:09 +000061 *
robbiew25ae5452004-03-02 22:55:42 +000062 * INTERCASE DEPENDENCIES
subrata_modak56207ce2009-03-23 13:35:39 +000063 * None
subrata_modak4bb656a2009-02-26 12:02:09 +000064 *
robbiew25ae5452004-03-02 22:55:42 +000065 * DETAILED DESCRIPTION
66 * This is a Phase I test for the fcntl(2) system call. It is intended
67 * to provide a limited exposure of the system call, for now. It
68 * should/will be extended when full functional tests are written for
69 * fcntl(2).
subrata_modak4bb656a2009-02-26 12:02:09 +000070 *
subrata_modak56207ce2009-03-23 13:35:39 +000071 * Setup:
72 * Setup signal handling.
robbiew25ae5452004-03-02 22:55:42 +000073 * Pause for SIGUSR1 if option specified.
subrata_modak4bb656a2009-02-26 12:02:09 +000074 *
subrata_modak56207ce2009-03-23 13:35:39 +000075 * Test:
robbiew25ae5452004-03-02 22:55:42 +000076 * Loop if the proper options are given.
subrata_modak56207ce2009-03-23 13:35:39 +000077 * Execute system call
robbiew25ae5452004-03-02 22:55:42 +000078 * Check return code, if system call failed (return=-1)
79 * Log the errno and Issue a FAIL message.
80 * Otherwise, Issue a PASS message.
subrata_modak4bb656a2009-02-26 12:02:09 +000081 *
subrata_modak56207ce2009-03-23 13:35:39 +000082 * Cleanup:
83 * Print errno log and/or timing stats if options given
subrata_modak4bb656a2009-02-26 12:02:09 +000084 *
85 *
robbiew25ae5452004-03-02 22:55:42 +000086 *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/
87
88#include <sys/types.h>
89#include <sys/stat.h>
90#include <fcntl.h>
91#include <unistd.h>
92#include <errno.h>
93#include <string.h>
94#include <signal.h>
95#include "test.h"
Cyril Hrubis0f0e3482014-02-27 16:08:04 +010096#include "tst_fs_type.h"
robbiew25ae5452004-03-02 22:55:42 +000097
vapieraa354722006-05-26 06:26:37 +000098void setup();
99void cleanup();
robbiew25ae5452004-03-02 22:55:42 +0000100
Cyril Hrubisfdce7d52013-04-04 18:35:48 +0200101char *TCID = "fcntl24";
102int TST_TOTAL = 1;
robbiew25ae5452004-03-02 22:55:42 +0000103
robbiew25ae5452004-03-02 22:55:42 +0000104char fname[255];
105int fd;
106
subrata_modak56207ce2009-03-23 13:35:39 +0000107int main(int ac, char **av)
robbiew25ae5452004-03-02 22:55:42 +0000108{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200109 int lc;
Cyril Hrubis0f0e3482014-02-27 16:08:04 +0100110 long type;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200111 const char *msg;
subrata_modak56207ce2009-03-23 13:35:39 +0000112
robbiew25ae5452004-03-02 22:55:42 +0000113 /***************************************************************
114 * parse standard options
115 ***************************************************************/
Garrett Cooper45e285d2010-11-22 12:19:25 -0800116 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800117 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiew25ae5452004-03-02 22:55:42 +0000118
119 /***************************************************************
120 * perform global setup for test
121 ***************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000122 setup();
robbiew25ae5452004-03-02 22:55:42 +0000123
Cyril Hrubis0f0e3482014-02-27 16:08:04 +0100124 switch ((type = tst_fs_type(cleanup, "."))) {
125 case TST_NFS_MAGIC:
126 case TST_RAMFS_MAGIC:
127 case TST_TMPFS_MAGIC:
subrata_modak56207ce2009-03-23 13:35:39 +0000128 tst_brkm(TCONF, cleanup,
Cyril Hrubis0f0e3482014-02-27 16:08:04 +0100129 "Cannot do fcntl on a file on %s filesystem",
130 tst_fs_type_name(type));
131 break;
subrata_modaka52f4de2009-08-04 11:31:56 +0000132 }
133
robbiew25ae5452004-03-02 22:55:42 +0000134 /***************************************************************
135 * check looping state if -c option given
136 ***************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000137 for (lc = 0; TEST_LOOPING(lc); lc++) {
robbiew25ae5452004-03-02 22:55:42 +0000138
Caspar Zhangd59a6592013-03-07 14:59:12 +0800139 tst_count = 0;
robbiew25ae5452004-03-02 22:55:42 +0000140
robbiewe93a3242005-08-31 20:27:12 +0000141#ifdef F_SETLEASE
subrata_modak56207ce2009-03-23 13:35:39 +0000142 /*
143 * Call fcntl(2) with F_SETLEASE & F_WRLCK argument on fname
144 */
145 TEST(fcntl(fd, F_SETLEASE, F_WRLCK));
subrata_modakbdbaec52009-02-26 12:14:51 +0000146
subrata_modak56207ce2009-03-23 13:35:39 +0000147 /* check return code */
148 if (TEST_RETURN == -1) {
subrata_modak56207ce2009-03-23 13:35:39 +0000149 tst_resm(TFAIL,
150 "fcntl(%s, F_SETLEASE, F_WRLCK) Failed, errno=%d : %s",
151 fname, TEST_ERRNO, strerror(TEST_ERRNO));
152 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200153 TEST(fcntl(fd, F_GETLEASE));
154 if (TEST_RETURN != F_WRLCK)
155 tst_resm(TFAIL,
156 "fcntl(%s, F_GETLEASE) did not return F_WRLCK, returned %ld",
157 fname, TEST_RETURN);
158 else {
159 TEST(fcntl(fd, F_SETLEASE, F_UNLCK));
160 if (TEST_RETURN != 0)
subrata_modak56207ce2009-03-23 13:35:39 +0000161 tst_resm(TFAIL,
Cyril Hrubise38b9612014-06-02 17:20:57 +0200162 "fcntl(%s, F_SETLEASE, F_UNLCK) did not return 0, returned %ld",
163 fname, TEST_RETURN);
164 else
165 tst_resm(TPASS,
166 "fcntl(%s, F_SETLEASE, F_WRLCK)",
167 fname);
subrata_modak56207ce2009-03-23 13:35:39 +0000168 }
169 }
vapier75467512007-09-24 05:34:19 +0000170#else
171 tst_resm(TINFO, "F_SETLEASE not defined, skipping test");
robbiewe93a3242005-08-31 20:27:12 +0000172#endif
Garrett Cooper2c282152010-12-16 00:55:50 -0800173 }
robbiew25ae5452004-03-02 22:55:42 +0000174
subrata_modak56207ce2009-03-23 13:35:39 +0000175 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800176 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800177}
robbiew25ae5452004-03-02 22:55:42 +0000178
179/***************************************************************
180 * setup() - performs all ONE TIME setup for this test.
181 ***************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -0400182void setup(void)
robbiew25ae5452004-03-02 22:55:42 +0000183{
Garrett Cooper2c282152010-12-16 00:55:50 -0800184
subrata_modak56207ce2009-03-23 13:35:39 +0000185 tst_sig(NOFORK, DEF_HANDLER, cleanup);
robbiew25ae5452004-03-02 22:55:42 +0000186
subrata_modak56207ce2009-03-23 13:35:39 +0000187 TEST_PAUSE;
robbiew25ae5452004-03-02 22:55:42 +0000188
subrata_modak56207ce2009-03-23 13:35:39 +0000189 tst_tmpdir();
robbiew25ae5452004-03-02 22:55:42 +0000190
subrata_modak56207ce2009-03-23 13:35:39 +0000191 sprintf(fname, "tfile_%d", getpid());
192 if ((fd = open(fname, O_RDWR | O_CREAT, 0777)) == -1) {
193 tst_brkm(TBROK, cleanup,
194 "open(%s, O_RDWR|O_CREAT,0777) Failed, errno=%d : %s",
195 fname, errno, strerror(errno));
196 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800197}
robbiew25ae5452004-03-02 22:55:42 +0000198
199/***************************************************************
200 * cleanup() - performs all ONE TIME cleanup for this test at
201 * completion or premature exit.
202 ***************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -0400203void cleanup(void)
robbiew25ae5452004-03-02 22:55:42 +0000204{
robbiew25ae5452004-03-02 22:55:42 +0000205
subrata_modak56207ce2009-03-23 13:35:39 +0000206 /* close the file we've had open */
207 if (close(fd) == -1) {
208 tst_resm(TWARN, "close(%s) Failed, errno=%d : %s", fname, errno,
209 strerror(errno));
210 }
robbiew25ae5452004-03-02 22:55:42 +0000211
subrata_modak56207ce2009-03-23 13:35:39 +0000212 tst_rmdir();
robbiew25ae5452004-03-02 22:55:42 +0000213
Chris Dearmanec6edca2012-10-17 19:54:01 -0700214}