1f08c3bdfSopenharmony_ci/*
2f08c3bdfSopenharmony_ci * Copyright (c) 2015 Fujitsu Ltd.
3f08c3bdfSopenharmony_ci * Author: Guangwen Feng <fenggw-fnst@cn.fujitsu.com>
4f08c3bdfSopenharmony_ci *
5f08c3bdfSopenharmony_ci * This program is free software; you can redistribute it and/or modify it
6f08c3bdfSopenharmony_ci * under the terms of version 2 of the GNU General Public License as
7f08c3bdfSopenharmony_ci * published by the Free Software Foundation.
8f08c3bdfSopenharmony_ci *
9f08c3bdfSopenharmony_ci * This program is distributed in the hope that it would be useful, but
10f08c3bdfSopenharmony_ci * WITHOUT ANY WARRANTY; without even the implied warranty of
11f08c3bdfSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12f08c3bdfSopenharmony_ci *
13f08c3bdfSopenharmony_ci * You should have received a copy of the GNU General Public License
14f08c3bdfSopenharmony_ci * alone with this program.
15f08c3bdfSopenharmony_ci */
16f08c3bdfSopenharmony_ci
17f08c3bdfSopenharmony_ci/*
18f08c3bdfSopenharmony_ci * DESCRIPTION
19f08c3bdfSopenharmony_ci *  Basic test for fcntl(2) using F_SETLEASE & F_WRLCK argument.
20f08c3bdfSopenharmony_ci *  "A write lease may be placed on a file only if there are
21f08c3bdfSopenharmony_ci *  no other open file descriptors for the file."
22f08c3bdfSopenharmony_ci */
23f08c3bdfSopenharmony_ci
24f08c3bdfSopenharmony_ci#include <errno.h>
25f08c3bdfSopenharmony_ci
26f08c3bdfSopenharmony_ci#include "test.h"
27f08c3bdfSopenharmony_ci#include "safe_macros.h"
28f08c3bdfSopenharmony_ci
29f08c3bdfSopenharmony_cistatic void setup(void);
30f08c3bdfSopenharmony_cistatic void verify_fcntl(int);
31f08c3bdfSopenharmony_cistatic void cleanup(void);
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_ci#define FILE_MODE	(S_IRWXU | S_IRWXG | S_IRWXO | S_ISUID | S_ISGID)
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_cistatic int fd1;
36f08c3bdfSopenharmony_cistatic int fd2;
37f08c3bdfSopenharmony_cistatic long type;
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_cistatic struct test_case_t {
40f08c3bdfSopenharmony_ci	int fd1_flag;
41f08c3bdfSopenharmony_ci	int fd2_flag;
42f08c3bdfSopenharmony_ci} test_cases[] = {
43f08c3bdfSopenharmony_ci	{O_RDONLY, O_RDONLY},
44f08c3bdfSopenharmony_ci	{O_RDONLY, O_WRONLY},
45f08c3bdfSopenharmony_ci	{O_RDONLY, O_RDWR},
46f08c3bdfSopenharmony_ci	{O_WRONLY, O_RDONLY},
47f08c3bdfSopenharmony_ci	{O_WRONLY, O_WRONLY},
48f08c3bdfSopenharmony_ci	{O_WRONLY, O_RDWR},
49f08c3bdfSopenharmony_ci	{O_RDWR, O_RDONLY},
50f08c3bdfSopenharmony_ci	{O_RDWR, O_WRONLY},
51f08c3bdfSopenharmony_ci	{O_RDWR, O_RDWR},
52f08c3bdfSopenharmony_ci};
53f08c3bdfSopenharmony_ci
54f08c3bdfSopenharmony_cichar *TCID = "fcntl32";
55f08c3bdfSopenharmony_ciint TST_TOTAL = ARRAY_SIZE(test_cases);
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_ciint main(int ac, char **av)
58f08c3bdfSopenharmony_ci{
59f08c3bdfSopenharmony_ci	int lc;
60f08c3bdfSopenharmony_ci	int tc;
61f08c3bdfSopenharmony_ci
62f08c3bdfSopenharmony_ci	tst_parse_opts(ac, av, NULL, NULL);
63f08c3bdfSopenharmony_ci
64f08c3bdfSopenharmony_ci	setup();
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_ci	for (lc = 0; TEST_LOOPING(lc); lc++) {
67f08c3bdfSopenharmony_ci		tst_count = 0;
68f08c3bdfSopenharmony_ci
69f08c3bdfSopenharmony_ci		for (tc = 0; tc < TST_TOTAL; tc++)
70f08c3bdfSopenharmony_ci			verify_fcntl(tc);
71f08c3bdfSopenharmony_ci	}
72f08c3bdfSopenharmony_ci
73f08c3bdfSopenharmony_ci	cleanup();
74f08c3bdfSopenharmony_ci	tst_exit();
75f08c3bdfSopenharmony_ci}
76f08c3bdfSopenharmony_ci
77f08c3bdfSopenharmony_cistatic void setup(void)
78f08c3bdfSopenharmony_ci{
79f08c3bdfSopenharmony_ci	tst_sig(NOFORK, DEF_HANDLER, cleanup);
80f08c3bdfSopenharmony_ci	TEST_PAUSE;
81f08c3bdfSopenharmony_ci
82f08c3bdfSopenharmony_ci	tst_tmpdir();
83f08c3bdfSopenharmony_ci
84f08c3bdfSopenharmony_ci	switch ((type = tst_fs_type(cleanup, "."))) {
85f08c3bdfSopenharmony_ci	case TST_NFS_MAGIC:
86f08c3bdfSopenharmony_ci	case TST_RAMFS_MAGIC:
87f08c3bdfSopenharmony_ci	case TST_TMPFS_MAGIC:
88f08c3bdfSopenharmony_ci		tst_brkm(TCONF, cleanup,
89f08c3bdfSopenharmony_ci			 "Cannot do fcntl(F_SETLEASE, F_WRLCK) "
90f08c3bdfSopenharmony_ci			 "on %s filesystem",
91f08c3bdfSopenharmony_ci			 tst_fs_type_name(type));
92f08c3bdfSopenharmony_ci	default:
93f08c3bdfSopenharmony_ci		break;
94f08c3bdfSopenharmony_ci	}
95f08c3bdfSopenharmony_ci
96f08c3bdfSopenharmony_ci	SAFE_TOUCH(cleanup, "file", FILE_MODE, NULL);
97f08c3bdfSopenharmony_ci}
98f08c3bdfSopenharmony_ci
99f08c3bdfSopenharmony_cistatic void verify_fcntl(int i)
100f08c3bdfSopenharmony_ci{
101f08c3bdfSopenharmony_ci	fd1 = SAFE_OPEN(cleanup, "file", test_cases[i].fd1_flag);
102f08c3bdfSopenharmony_ci	fd2 = SAFE_OPEN(cleanup, "file", test_cases[i].fd2_flag);
103f08c3bdfSopenharmony_ci
104f08c3bdfSopenharmony_ci	TEST(fcntl(fd1, F_SETLEASE, F_WRLCK));
105f08c3bdfSopenharmony_ci
106f08c3bdfSopenharmony_ci	if (TEST_RETURN == 0) {
107f08c3bdfSopenharmony_ci		tst_resm(TFAIL, "fcntl(F_SETLEASE, F_WRLCK) "
108f08c3bdfSopenharmony_ci			 "succeeded unexpectedly");
109f08c3bdfSopenharmony_ci	} else {
110f08c3bdfSopenharmony_ci		if (TEST_ERRNO == EBUSY || TEST_ERRNO == EAGAIN) {
111f08c3bdfSopenharmony_ci			tst_resm(TPASS | TTERRNO,
112f08c3bdfSopenharmony_ci				 "fcntl(F_SETLEASE, F_WRLCK) "
113f08c3bdfSopenharmony_ci				 "failed as expected");
114f08c3bdfSopenharmony_ci		} else {
115f08c3bdfSopenharmony_ci			tst_resm(TFAIL | TTERRNO,
116f08c3bdfSopenharmony_ci				 "fcntl(F_SETLEASE, F_WRLCK) "
117f08c3bdfSopenharmony_ci				 "failed unexpectedly, "
118f08c3bdfSopenharmony_ci				 "expected errno is EBUSY or EAGAIN");
119f08c3bdfSopenharmony_ci		}
120f08c3bdfSopenharmony_ci	}
121f08c3bdfSopenharmony_ci
122f08c3bdfSopenharmony_ci	SAFE_CLOSE(cleanup, fd1);
123f08c3bdfSopenharmony_ci	fd1 = 0;
124f08c3bdfSopenharmony_ci	SAFE_CLOSE(cleanup, fd2);
125f08c3bdfSopenharmony_ci	fd2 = 0;
126f08c3bdfSopenharmony_ci}
127f08c3bdfSopenharmony_ci
128f08c3bdfSopenharmony_cistatic void cleanup(void)
129f08c3bdfSopenharmony_ci{
130f08c3bdfSopenharmony_ci	if (fd1 > 0 && close(fd1))
131f08c3bdfSopenharmony_ci		tst_resm(TWARN | TERRNO, "Failed to close file");
132f08c3bdfSopenharmony_ci
133f08c3bdfSopenharmony_ci	if (fd2 > 0 && close(fd2))
134f08c3bdfSopenharmony_ci		tst_resm(TWARN | TERRNO, "Failed to close file");
135f08c3bdfSopenharmony_ci
136f08c3bdfSopenharmony_ci	tst_rmdir();
137f08c3bdfSopenharmony_ci}
138