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