1f08c3bdfSopenharmony_ci/*
2f08c3bdfSopenharmony_ci *
3f08c3bdfSopenharmony_ci *   Copyright (c) International Business Machines  Corp., 2004
4f08c3bdfSopenharmony_ci *
5f08c3bdfSopenharmony_ci *   This program is free software;  you can redistribute it and/or modify
6f08c3bdfSopenharmony_ci *   it under the terms of the GNU General Public License as published by
7f08c3bdfSopenharmony_ci *   the Free Software Foundation; either version 2 of the License, or
8f08c3bdfSopenharmony_ci *   (at your option) any later version.
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci *   This program is distributed in the hope that it will be useful,
11f08c3bdfSopenharmony_ci *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12f08c3bdfSopenharmony_ci *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13f08c3bdfSopenharmony_ci *   the GNU General Public License for more details.
14f08c3bdfSopenharmony_ci *
15f08c3bdfSopenharmony_ci *   You should have received a copy of the GNU General Public License
16f08c3bdfSopenharmony_ci *   along with this program;  if not, write to the Free Software
17f08c3bdfSopenharmony_ci *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18f08c3bdfSopenharmony_ci */
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_ci/**********************************************************
21f08c3bdfSopenharmony_ci *
22f08c3bdfSopenharmony_ci *    TEST IDENTIFIER	: fcntl24
23f08c3bdfSopenharmony_ci *
24f08c3bdfSopenharmony_ci *    EXECUTED BY	: anyone
25f08c3bdfSopenharmony_ci *
26f08c3bdfSopenharmony_ci *    TEST TITLE	: Basic test for fcntl(2) using F_SETLEASE & F_WRLCK argument.
27f08c3bdfSopenharmony_ci *
28f08c3bdfSopenharmony_ci *    TEST CASE TOTAL	: 1
29f08c3bdfSopenharmony_ci *
30f08c3bdfSopenharmony_ci *    WALL CLOCK TIME	: 1
31f08c3bdfSopenharmony_ci *
32f08c3bdfSopenharmony_ci *    CPU TYPES		: ALL
33f08c3bdfSopenharmony_ci *
34f08c3bdfSopenharmony_ci *    AUTHOR		: Robbie Williamson
35f08c3bdfSopenharmony_ci *
36f08c3bdfSopenharmony_ci *    TEST CASES
37f08c3bdfSopenharmony_ci *
38f08c3bdfSopenharmony_ci *	1.) fcntl(2) returns...(See Description)
39f08c3bdfSopenharmony_ci *
40f08c3bdfSopenharmony_ci *    INPUT SPECIFICATIONS
41f08c3bdfSopenharmony_ci *	The standard options for system call tests are accepted.
42f08c3bdfSopenharmony_ci *	(See the parse_opts(3) man page).
43f08c3bdfSopenharmony_ci *
44f08c3bdfSopenharmony_ci *    OUTPUT SPECIFICATIONS
45f08c3bdfSopenharmony_ci *
46f08c3bdfSopenharmony_ci *    DURATION
47f08c3bdfSopenharmony_ci *	Terminates - with frequency and infinite modes.
48f08c3bdfSopenharmony_ci *
49f08c3bdfSopenharmony_ci *    SIGNALS
50f08c3bdfSopenharmony_ci *	Uses SIGUSR1 to pause before test if option set.
51f08c3bdfSopenharmony_ci *	(See the parse_opts(3) man page).
52f08c3bdfSopenharmony_ci *
53f08c3bdfSopenharmony_ci *    RESOURCES
54f08c3bdfSopenharmony_ci *	None
55f08c3bdfSopenharmony_ci *
56f08c3bdfSopenharmony_ci *    ENVIRONMENTAL NEEDS
57f08c3bdfSopenharmony_ci *      No run-time environmental needs.
58f08c3bdfSopenharmony_ci *
59f08c3bdfSopenharmony_ci *    SPECIAL PROCEDURAL REQUIREMENTS
60f08c3bdfSopenharmony_ci *	None
61f08c3bdfSopenharmony_ci *
62f08c3bdfSopenharmony_ci *    INTERCASE DEPENDENCIES
63f08c3bdfSopenharmony_ci *	None
64f08c3bdfSopenharmony_ci *
65f08c3bdfSopenharmony_ci *    DETAILED DESCRIPTION
66f08c3bdfSopenharmony_ci *	This is a Phase I test for the fcntl(2) system call.  It is intended
67f08c3bdfSopenharmony_ci *	to provide a limited exposure of the system call, for now.  It
68f08c3bdfSopenharmony_ci *	should/will be extended when full functional tests are written for
69f08c3bdfSopenharmony_ci *	fcntl(2).
70f08c3bdfSopenharmony_ci *
71f08c3bdfSopenharmony_ci *	Setup:
72f08c3bdfSopenharmony_ci *	  Setup signal handling.
73f08c3bdfSopenharmony_ci *	  Pause for SIGUSR1 if option specified.
74f08c3bdfSopenharmony_ci *
75f08c3bdfSopenharmony_ci *	Test:
76f08c3bdfSopenharmony_ci *	 Loop if the proper options are given.
77f08c3bdfSopenharmony_ci *	  Execute system call
78f08c3bdfSopenharmony_ci *	  Check return code, if system call failed (return=-1)
79f08c3bdfSopenharmony_ci *		Log the errno and Issue a FAIL message.
80f08c3bdfSopenharmony_ci *	  Otherwise, Issue a PASS message.
81f08c3bdfSopenharmony_ci *
82f08c3bdfSopenharmony_ci *	Cleanup:
83f08c3bdfSopenharmony_ci *	  Print errno log and/or timing stats if options given
84f08c3bdfSopenharmony_ci *
85f08c3bdfSopenharmony_ci *
86f08c3bdfSopenharmony_ci *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/
87f08c3bdfSopenharmony_ci
88f08c3bdfSopenharmony_ci#include <sys/types.h>
89f08c3bdfSopenharmony_ci#include <sys/stat.h>
90f08c3bdfSopenharmony_ci#include <fcntl.h>
91f08c3bdfSopenharmony_ci#include <unistd.h>
92f08c3bdfSopenharmony_ci#include <errno.h>
93f08c3bdfSopenharmony_ci#include <string.h>
94f08c3bdfSopenharmony_ci#include <signal.h>
95f08c3bdfSopenharmony_ci#include "test.h"
96f08c3bdfSopenharmony_ci
97f08c3bdfSopenharmony_civoid setup();
98f08c3bdfSopenharmony_civoid cleanup();
99f08c3bdfSopenharmony_ci
100f08c3bdfSopenharmony_cichar *TCID = "fcntl24";
101f08c3bdfSopenharmony_ciint TST_TOTAL = 1;
102f08c3bdfSopenharmony_ci
103f08c3bdfSopenharmony_cichar fname[255];
104f08c3bdfSopenharmony_ciint fd;
105f08c3bdfSopenharmony_ci
106f08c3bdfSopenharmony_ciint main(int ac, char **av)
107f08c3bdfSopenharmony_ci{
108f08c3bdfSopenharmony_ci	int lc;
109f08c3bdfSopenharmony_ci	long type;
110f08c3bdfSopenharmony_ci
111f08c3bdfSopenharmony_ci    /***************************************************************
112f08c3bdfSopenharmony_ci     * parse standard options
113f08c3bdfSopenharmony_ci     ***************************************************************/
114f08c3bdfSopenharmony_ci	tst_parse_opts(ac, av, NULL, NULL);
115f08c3bdfSopenharmony_ci
116f08c3bdfSopenharmony_ci    /***************************************************************
117f08c3bdfSopenharmony_ci     * perform global setup for test
118f08c3bdfSopenharmony_ci     ***************************************************************/
119f08c3bdfSopenharmony_ci	setup();
120f08c3bdfSopenharmony_ci
121f08c3bdfSopenharmony_ci	switch ((type = tst_fs_type(cleanup, "."))) {
122f08c3bdfSopenharmony_ci	case TST_NFS_MAGIC:
123f08c3bdfSopenharmony_ci	case TST_RAMFS_MAGIC:
124f08c3bdfSopenharmony_ci	case TST_TMPFS_MAGIC:
125f08c3bdfSopenharmony_ci		tst_brkm(TCONF, cleanup,
126f08c3bdfSopenharmony_ci			 "Cannot do fcntl on a file on %s filesystem",
127f08c3bdfSopenharmony_ci			 tst_fs_type_name(type));
128f08c3bdfSopenharmony_ci	break;
129f08c3bdfSopenharmony_ci	}
130f08c3bdfSopenharmony_ci
131f08c3bdfSopenharmony_ci    /***************************************************************
132f08c3bdfSopenharmony_ci     * check looping state if -c option given
133f08c3bdfSopenharmony_ci     ***************************************************************/
134f08c3bdfSopenharmony_ci	for (lc = 0; TEST_LOOPING(lc); lc++) {
135f08c3bdfSopenharmony_ci
136f08c3bdfSopenharmony_ci		tst_count = 0;
137f08c3bdfSopenharmony_ci
138f08c3bdfSopenharmony_ci#ifdef F_SETLEASE
139f08c3bdfSopenharmony_ci		/*
140f08c3bdfSopenharmony_ci		 * Call fcntl(2) with F_SETLEASE & F_WRLCK argument on fname
141f08c3bdfSopenharmony_ci		 */
142f08c3bdfSopenharmony_ci		TEST(fcntl(fd, F_SETLEASE, F_WRLCK));
143f08c3bdfSopenharmony_ci
144f08c3bdfSopenharmony_ci		/* check return code */
145f08c3bdfSopenharmony_ci		if (TEST_RETURN == -1) {
146f08c3bdfSopenharmony_ci			if (type == TST_OVERLAYFS_MAGIC && TEST_ERRNO == EAGAIN) {
147f08c3bdfSopenharmony_ci				tst_resm(TINFO | TTERRNO,
148f08c3bdfSopenharmony_ci					 "fcntl(F_SETLEASE, F_WRLCK) failed on overlayfs as expected");
149f08c3bdfSopenharmony_ci			} else {
150f08c3bdfSopenharmony_ci				tst_resm(TFAIL,
151f08c3bdfSopenharmony_ci					"fcntl(%s, F_SETLEASE, F_WRLCK) Failed, errno=%d : %s",
152f08c3bdfSopenharmony_ci					fname, TEST_ERRNO, strerror(TEST_ERRNO));
153f08c3bdfSopenharmony_ci			}
154f08c3bdfSopenharmony_ci		} else {
155f08c3bdfSopenharmony_ci			TEST(fcntl(fd, F_GETLEASE));
156f08c3bdfSopenharmony_ci			if (TEST_RETURN != F_WRLCK)
157f08c3bdfSopenharmony_ci				tst_resm(TFAIL,
158f08c3bdfSopenharmony_ci					 "fcntl(%s, F_GETLEASE) did not return F_WRLCK, returned %ld",
159f08c3bdfSopenharmony_ci					 fname, TEST_RETURN);
160f08c3bdfSopenharmony_ci			else {
161f08c3bdfSopenharmony_ci				TEST(fcntl(fd, F_SETLEASE, F_UNLCK));
162f08c3bdfSopenharmony_ci				if (TEST_RETURN != 0)
163f08c3bdfSopenharmony_ci						tst_resm(TFAIL,
164f08c3bdfSopenharmony_ci						 "fcntl(%s, F_SETLEASE, F_UNLCK) did not return 0, returned %ld",
165f08c3bdfSopenharmony_ci						 fname, TEST_RETURN);
166f08c3bdfSopenharmony_ci				else
167f08c3bdfSopenharmony_ci					tst_resm(TPASS,
168f08c3bdfSopenharmony_ci						 "fcntl(%s, F_SETLEASE, F_WRLCK)",
169f08c3bdfSopenharmony_ci						 fname);
170f08c3bdfSopenharmony_ci			}
171f08c3bdfSopenharmony_ci		}
172f08c3bdfSopenharmony_ci#else
173f08c3bdfSopenharmony_ci		tst_resm(TINFO, "F_SETLEASE not defined, skipping test");
174f08c3bdfSopenharmony_ci#endif
175f08c3bdfSopenharmony_ci	}
176f08c3bdfSopenharmony_ci
177f08c3bdfSopenharmony_ci	cleanup();
178f08c3bdfSopenharmony_ci	tst_exit();
179f08c3bdfSopenharmony_ci}
180f08c3bdfSopenharmony_ci
181f08c3bdfSopenharmony_ci/***************************************************************
182f08c3bdfSopenharmony_ci * setup() - performs all ONE TIME setup for this test.
183f08c3bdfSopenharmony_ci ***************************************************************/
184f08c3bdfSopenharmony_civoid setup(void)
185f08c3bdfSopenharmony_ci{
186f08c3bdfSopenharmony_ci
187f08c3bdfSopenharmony_ci	tst_sig(NOFORK, DEF_HANDLER, cleanup);
188f08c3bdfSopenharmony_ci
189f08c3bdfSopenharmony_ci	TEST_PAUSE;
190f08c3bdfSopenharmony_ci
191f08c3bdfSopenharmony_ci	tst_tmpdir();
192f08c3bdfSopenharmony_ci
193f08c3bdfSopenharmony_ci	sprintf(fname, "tfile_%d", getpid());
194f08c3bdfSopenharmony_ci	if ((fd = open(fname, O_RDWR | O_CREAT, 0777)) == -1) {
195f08c3bdfSopenharmony_ci		tst_brkm(TBROK, cleanup,
196f08c3bdfSopenharmony_ci			 "open(%s, O_RDWR|O_CREAT,0777) Failed, errno=%d : %s",
197f08c3bdfSopenharmony_ci			 fname, errno, strerror(errno));
198f08c3bdfSopenharmony_ci	}
199f08c3bdfSopenharmony_ci}
200f08c3bdfSopenharmony_ci
201f08c3bdfSopenharmony_ci/***************************************************************
202f08c3bdfSopenharmony_ci * cleanup() - performs all ONE TIME cleanup for this test at
203f08c3bdfSopenharmony_ci *		completion or premature exit.
204f08c3bdfSopenharmony_ci ***************************************************************/
205f08c3bdfSopenharmony_civoid cleanup(void)
206f08c3bdfSopenharmony_ci{
207f08c3bdfSopenharmony_ci
208f08c3bdfSopenharmony_ci	/* close the file we've had open */
209f08c3bdfSopenharmony_ci	if (close(fd) == -1) {
210f08c3bdfSopenharmony_ci		tst_resm(TWARN, "close(%s) Failed, errno=%d : %s", fname, errno,
211f08c3bdfSopenharmony_ci			 strerror(errno));
212f08c3bdfSopenharmony_ci	}
213f08c3bdfSopenharmony_ci
214f08c3bdfSopenharmony_ci	tst_rmdir();
215f08c3bdfSopenharmony_ci
216f08c3bdfSopenharmony_ci}
217