1 /* Test POSIX lock on an open file (lockf).
2 Copyright (C) 2019-2022 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, see <https://www.gnu.org/licenses/>.
16 */
17
18 #include <unistd.h>
19 #include <stdint.h>
20 #include <errno.h>
21 #include <stdio.h>
22
23 #include <support/temp_file.h>
24 #include <support/capture_subprocess.h>
25 #include <support/check.h>
26
27 static char *temp_filename;
28 static int temp_fd;
29
30 static void
do_prepare(int argc,char ** argv)31 do_prepare (int argc, char **argv)
32 {
33 temp_fd = create_temp_file ("tst-lockfd.", &temp_filename);
34 TEST_VERIFY_EXIT (temp_fd != -1);
35 }
36 #define PREPARE do_prepare
37
38 static void
do_test_child_lockf(void * closure)39 do_test_child_lockf (void *closure)
40 {
41 /* Check if parent has [0, 1024) locked. */
42 TEST_COMPARE (lseek (temp_fd, 0, SEEK_SET), 0);
43 TEST_COMPARE (lockf (temp_fd, F_TLOCK, 1024), -1);
44 TEST_COMPARE (errno, EAGAIN);
45 TEST_COMPARE (lockf (temp_fd, F_TEST, 1024), -1);
46 TEST_COMPARE (errno, EACCES);
47 /* Also Check if parent has last 1024 bytes locked. */
48 TEST_COMPARE (lseek (temp_fd, INT32_MAX-1024, SEEK_SET), INT32_MAX-1024);
49 TEST_COMPARE (lockf (temp_fd, F_TEST, 1024), -1);
50
51 /* And try to lock [1024, 2048). */
52 TEST_COMPARE (lseek (temp_fd, 1024, SEEK_SET), 1024);
53 TEST_COMPARE (lockf (temp_fd, F_LOCK, 1024), 0);
54
55 /* Check if non-LFS interface cap access to 32-bif off_t. */
56 TEST_COMPARE (lseek64 (temp_fd, (off64_t)INT32_MAX, SEEK_SET),
57 (off64_t)INT32_MAX);
58 TEST_COMPARE (lockf64 (temp_fd, F_TEST, 1024), 0);
59 }
60
61 static void
do_test_child_lockf64(void * closure)62 do_test_child_lockf64 (void *closure)
63 {
64 /* Check if parent has [0, 1024) locked. */
65 TEST_COMPARE (lseek64 (temp_fd, 0, SEEK_SET), 0);
66 TEST_COMPARE (lockf64 (temp_fd, F_TLOCK, 1024), -1);
67 TEST_COMPARE (errno, EAGAIN);
68 TEST_COMPARE (lockf64 (temp_fd, F_TEST, 1024), -1);
69 TEST_COMPARE (errno, EACCES);
70 /* Also Check if parent has last 1024 bytes locked. */
71 TEST_COMPARE (lseek64 (temp_fd, INT32_MAX-1024, SEEK_SET), INT32_MAX-1024);
72 TEST_COMPARE (lockf64 (temp_fd, F_TEST, 1024), -1);
73
74 /* And try to lock [1024, 2048). */
75 TEST_COMPARE (lseek64 (temp_fd, 1024, SEEK_SET), 1024);
76 TEST_COMPARE (lockf64 (temp_fd, F_LOCK, 1024), 0);
77
78 /* And also [INT32_MAX, INT32_MAX+1024). */
79 {
80 off64_t off = (off64_t)INT32_MAX;
81 TEST_COMPARE (lseek64 (temp_fd, off, SEEK_SET), off);
82 TEST_COMPARE (lockf64 (temp_fd, F_LOCK, 1024), 0);
83 }
84
85 /* Check if [INT32_MAX+1024, INT64_MAX) is locked. */
86 {
87 off64_t off = (off64_t)INT32_MAX+1024;
88 TEST_COMPARE (lseek64 (temp_fd, off, SEEK_SET), off);
89 TEST_COMPARE (lockf64 (temp_fd, F_TLOCK, 1024), -1);
90 TEST_COMPARE (errno, EAGAIN);
91 TEST_COMPARE (lockf64 (temp_fd, F_TEST, 1024), -1);
92 TEST_COMPARE (errno, EACCES);
93 }
94 }
95
96 static int
do_test(void)97 do_test (void)
98 {
99 /* Basic tests to check if a lock can be obtained and checked. */
100 TEST_COMPARE (lockf (temp_fd, F_LOCK, 1024), 0);
101 TEST_COMPARE (lockf (temp_fd, F_LOCK, INT32_MAX), 0);
102 TEST_COMPARE (lockf (temp_fd, F_TLOCK, 1024), 0);
103 TEST_COMPARE (lockf (temp_fd, F_TEST, 1024), 0);
104 TEST_COMPARE (lseek (temp_fd, 1024, SEEK_SET), 1024);
105 TEST_COMPARE (lockf (temp_fd, F_ULOCK, 1024), 0);
106 /* Parent process should have ([0, 1024), [2048, INT32_MAX)) ranges locked. */
107
108 {
109 struct support_capture_subprocess result;
110 result = support_capture_subprocess (do_test_child_lockf, NULL);
111 support_capture_subprocess_check (&result, "lockf", 0, sc_allow_none);
112 }
113
114 if (sizeof (off_t) != sizeof (off64_t))
115 {
116 /* Check if previously locked regions with LFS symbol. */
117 TEST_COMPARE (lseek (temp_fd, 0, SEEK_SET), 0);
118 TEST_COMPARE (lockf64 (temp_fd, F_LOCK, 1024), 0);
119 TEST_COMPARE (lockf64 (temp_fd, F_TLOCK, 1024), 0);
120 TEST_COMPARE (lockf64 (temp_fd, F_TEST, 1024), 0);
121 /* Lock region [INT32_MAX+1024, INT64_MAX). */
122 off64_t off = (off64_t)INT32_MAX + 1024;
123 TEST_COMPARE (lseek64 (temp_fd, off, SEEK_SET), off);
124 TEST_COMPARE (lockf64 (temp_fd, F_LOCK, 1024), 0);
125 /* Parent process should have ([0, 1024), [2048, INT32_MAX),
126 [INT32_MAX+1024, INT64_MAX)) ranges locked. */
127
128 {
129 struct support_capture_subprocess result;
130 result = support_capture_subprocess (do_test_child_lockf64, NULL);
131 support_capture_subprocess_check (&result, "lockf", 0, sc_allow_none);
132 }
133 }
134
135 return 0;
136 }
137
138 #include <support/test-driver.c>
139