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