blob: f239d55a2848b6dee52d67ede35ce64b898671e7 [file] [log] [blame]
robbiewd0223fd2006-01-13 22:33:44 +00001/*
2 * Copyright (C) Bull S.A.S 2006
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
11 * the GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080015 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
robbiewd0223fd2006-01-13 22:33:44 +000016 */
17
18/**********************************************************
subrata_modak4bb656a2009-02-26 12:02:09 +000019 *
robbiewd0223fd2006-01-13 22:33:44 +000020 * TEST IDENTIFIER : fcntl28
subrata_modak4bb656a2009-02-26 12:02:09 +000021 *
robbiewd0223fd2006-01-13 22:33:44 +000022 * EXECUTED BY : anyone
subrata_modak4bb656a2009-02-26 12:02:09 +000023 *
robbiewd0223fd2006-01-13 22:33:44 +000024 * TEST TITLE : Basic test for fcntl(2) using F_SETLEASE & F_RDLCK argument.
subrata_modak4bb656a2009-02-26 12:02:09 +000025 *
robbiewd0223fd2006-01-13 22:33:44 +000026 * TEST CASE TOTAL : 1
subrata_modak4bb656a2009-02-26 12:02:09 +000027 *
robbiewd0223fd2006-01-13 22:33:44 +000028 * WALL CLOCK TIME : 1
subrata_modak4bb656a2009-02-26 12:02:09 +000029 *
subrata_modak56207ce2009-03-23 13:35:39 +000030 * CPU TYPES : ALL
subrata_modak4bb656a2009-02-26 12:02:09 +000031 *
subrata_modak56207ce2009-03-23 13:35:39 +000032 * AUTHOR : Jacky Malcles
subrata_modak4bb656a2009-02-26 12:02:09 +000033 *
robbiewd0223fd2006-01-13 22:33:44 +000034 * TEST CASES
subrata_modak4bb656a2009-02-26 12:02:09 +000035 *
subrata_modak56207ce2009-03-23 13:35:39 +000036 * 1.) fcntl(2) returns...(See Description)
Garrett Cooper2c282152010-12-16 00:55:50 -080037 *
robbiewd0223fd2006-01-13 22:33:44 +000038 * INPUT SPECIFICATIONS
subrata_modak56207ce2009-03-23 13:35:39 +000039 * The standard options for system call tests are accepted.
robbiewd0223fd2006-01-13 22:33:44 +000040 * (See the parse_opts(3) man page).
subrata_modak4bb656a2009-02-26 12:02:09 +000041 *
robbiewd0223fd2006-01-13 22:33:44 +000042 * OUTPUT SPECIFICATIONS
subrata_modak56207ce2009-03-23 13:35:39 +000043 *
robbiewd0223fd2006-01-13 22:33:44 +000044 * DURATION
subrata_modak56207ce2009-03-23 13:35:39 +000045 * Terminates - with frequency and infinite modes.
subrata_modak4bb656a2009-02-26 12:02:09 +000046 *
robbiewd0223fd2006-01-13 22:33:44 +000047 * SIGNALS
subrata_modak56207ce2009-03-23 13:35:39 +000048 * Uses SIGUSR1 to pause before test if option set.
49 * (See the parse_opts(3) man page).
robbiewd0223fd2006-01-13 22:33:44 +000050 *
51 * RESOURCES
subrata_modak56207ce2009-03-23 13:35:39 +000052 * None
subrata_modak4bb656a2009-02-26 12:02:09 +000053 *
robbiewd0223fd2006-01-13 22:33:44 +000054 * ENVIRONMENTAL NEEDS
55 * No run-time environmental needs.
subrata_modak4bb656a2009-02-26 12:02:09 +000056 *
robbiewd0223fd2006-01-13 22:33:44 +000057 * SPECIAL PROCEDURAL REQUIREMENTS
subrata_modak56207ce2009-03-23 13:35:39 +000058 * None
subrata_modak4bb656a2009-02-26 12:02:09 +000059 *
robbiewd0223fd2006-01-13 22:33:44 +000060 * INTERCASE DEPENDENCIES
subrata_modak56207ce2009-03-23 13:35:39 +000061 * None
subrata_modak4bb656a2009-02-26 12:02:09 +000062 *
robbiewd0223fd2006-01-13 22:33:44 +000063 * DETAILED DESCRIPTION
64 * This is a Phase I test for the fcntl(2) system call. It is intended
65 * to provide a limited exposure of the system call, for now. It
66 * should/will be extended when full functional tests are written for
67 * fcntl(2).
subrata_modak4bb656a2009-02-26 12:02:09 +000068 *
subrata_modak56207ce2009-03-23 13:35:39 +000069 * Setup:
70 * Setup signal handling.
robbiewd0223fd2006-01-13 22:33:44 +000071 * Pause for SIGUSR1 if option specified.
subrata_modak4bb656a2009-02-26 12:02:09 +000072 *
subrata_modak56207ce2009-03-23 13:35:39 +000073 * Test:
robbiewd0223fd2006-01-13 22:33:44 +000074 * Loop if the proper options are given.
subrata_modak56207ce2009-03-23 13:35:39 +000075 * Execute system call
robbiewd0223fd2006-01-13 22:33:44 +000076 * Check return code, if system call failed (return=-1)
subrata_modak56207ce2009-03-23 13:35:39 +000077 * Log the errno and Issue a FAIL message.
robbiewd0223fd2006-01-13 22:33:44 +000078 * Otherwise, Issue a PASS message.
subrata_modak4bb656a2009-02-26 12:02:09 +000079 *
subrata_modak56207ce2009-03-23 13:35:39 +000080 * Cleanup:
81 * Print errno log and/or timing stats if options given
subrata_modak4bb656a2009-02-26 12:02:09 +000082 *
83 *
robbiewd0223fd2006-01-13 22:33:44 +000084 *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/
85
86#include <sys/types.h>
87#include <sys/stat.h>
88#include <fcntl.h>
89#include <unistd.h>
90#include <errno.h>
91#include <string.h>
92#include <signal.h>
93#include "test.h"
robbiewd0223fd2006-01-13 22:33:44 +000094
vapieraa354722006-05-26 06:26:37 +000095void setup();
96void cleanup();
robbiewd0223fd2006-01-13 22:33:44 +000097
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020098char *TCID = "fcntl28";
99int TST_TOTAL = 1;
robbiewd0223fd2006-01-13 22:33:44 +0000100
robbiewd0223fd2006-01-13 22:33:44 +0000101char fname[255];
102int fd;
103
subrata_modak56207ce2009-03-23 13:35:39 +0000104int main(int ac, char **av)
robbiewd0223fd2006-01-13 22:33:44 +0000105{
subrata_modak56207ce2009-03-23 13:35:39 +0000106 int lc, expected_result = -1; /* loop counter, expected resul */
107 /* from system call */
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200108 const char *msg;
robbiewd0223fd2006-01-13 22:33:44 +0000109 /***************************************************************
110 * parse standard options
111 ***************************************************************/
Garrett Cooper45e285d2010-11-22 12:19:25 -0800112 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800113 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiewd0223fd2006-01-13 22:33:44 +0000114
115 /***************************************************************
116 * perform global setup for test
117 ***************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000118 setup();
robbiewd0223fd2006-01-13 22:33:44 +0000119
subrata_modak56207ce2009-03-23 13:35:39 +0000120 expected_result = -1;
mreed104f2a7082006-07-06 21:24:16 +0000121
robbiewd0223fd2006-01-13 22:33:44 +0000122 /***************************************************************
123 * check looping state if -c option given
124 ***************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000125 for (lc = 0; TEST_LOOPING(lc); lc++) {
robbiewd0223fd2006-01-13 22:33:44 +0000126
Caspar Zhangd59a6592013-03-07 14:59:12 +0800127 tst_count = 0;
robbiewd0223fd2006-01-13 22:33:44 +0000128
129#ifdef F_SETLEASE
subrata_modak56207ce2009-03-23 13:35:39 +0000130 /*
131 * Call fcntl(2) with F_SETLEASE & F_RDLCK argument on fname
132 */
133 TEST(fcntl(fd, F_SETLEASE, F_RDLCK));
134
135 /* check return code */
136 if (TEST_RETURN == expected_result) {
subrata_modak56207ce2009-03-23 13:35:39 +0000137 tst_resm(TPASS,
138 "fcntl(fd, F_SETLEASE, F_RDLCK) succeeded");
139 } else {
140 tst_resm(TFAIL, "fcntl(%s, F_SETLEASE, F_RDLCK)"
141 " failed with errno %d : %s", fname,
142 TEST_ERRNO, strerror(TEST_ERRNO));
143 }
subrata_modak4bb656a2009-02-26 12:02:09 +0000144
vapier75467512007-09-24 05:34:19 +0000145#else
146 tst_resm(TINFO, "F_SETLEASE not defined, skipping test");
robbiewd0223fd2006-01-13 22:33:44 +0000147#endif
Garrett Cooper2c282152010-12-16 00:55:50 -0800148 }
robbiewd0223fd2006-01-13 22:33:44 +0000149
150 /***************************************************************
151 * cleanup and exit
152 ***************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000153 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800154 tst_exit();
robbiewd0223fd2006-01-13 22:33:44 +0000155
Garrett Cooper2c282152010-12-16 00:55:50 -0800156}
robbiewd0223fd2006-01-13 22:33:44 +0000157
158/***************************************************************
159 * setup() - performs all ONE TIME setup for this test.
160 ***************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -0400161void setup(void)
robbiewd0223fd2006-01-13 22:33:44 +0000162{
Garrett Cooper2c282152010-12-16 00:55:50 -0800163
subrata_modak56207ce2009-03-23 13:35:39 +0000164 tst_sig(NOFORK, DEF_HANDLER, cleanup);
robbiewd0223fd2006-01-13 22:33:44 +0000165
subrata_modak56207ce2009-03-23 13:35:39 +0000166 TEST_PAUSE;
robbiewd0223fd2006-01-13 22:33:44 +0000167
subrata_modak56207ce2009-03-23 13:35:39 +0000168 tst_tmpdir();
robbiewd0223fd2006-01-13 22:33:44 +0000169
subrata_modak56207ce2009-03-23 13:35:39 +0000170 sprintf(fname, "tfile_%d", getpid());
171 if ((fd = open(fname, O_WRONLY | O_CREAT, 0222)) == -1) {
172 tst_brkm(TBROK, cleanup,
173 "open(%s, O_WRONLY|O_CREAT,0222) Failed, errno=%d : %s",
174 fname, errno, strerror(errno));
175 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800176}
robbiewd0223fd2006-01-13 22:33:44 +0000177
178/***************************************************************
179 * cleanup() - performs all ONE TIME cleanup for this test at
subrata_modak56207ce2009-03-23 13:35:39 +0000180 * completion or premature exit.
robbiewd0223fd2006-01-13 22:33:44 +0000181 ***************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -0400182void cleanup(void)
robbiewd0223fd2006-01-13 22:33:44 +0000183{
robbiewd0223fd2006-01-13 22:33:44 +0000184
subrata_modak56207ce2009-03-23 13:35:39 +0000185 /* close the file we've had open */
186 if (close(fd) == -1) {
187 tst_resm(TWARN, "close(%s) Failed, errno=%d : %s", fname, errno,
188 strerror(errno));
189 }
robbiewd0223fd2006-01-13 22:33:44 +0000190
subrata_modak56207ce2009-03-23 13:35:39 +0000191 tst_rmdir();
robbiewd0223fd2006-01-13 22:33:44 +0000192
Chris Dearmanec6edca2012-10-17 19:54:01 -0700193}