]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/sysv/linux/tst-ofdlocks.c
Fix Linux fcntl OFD locks for non-LFS architectures (BZ#20251)
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / tst-ofdlocks.c
1 /* Check non representable OFD locks regions in non-LFS mode (BZ #20251)
2 Copyright (C) 2018 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 <http://www.gnu.org/licenses/>.
16 */
17
18 #include <unistd.h>
19 #include <fcntl.h>
20 #include <stdint.h>
21 #include <errno.h>
22
23 #include <support/temp_file.h>
24 #include <support/check.h>
25
26 static char *temp_filename;
27 static int temp_fd;
28
29 static void
30 do_prepare (int argc, char **argv)
31 {
32 temp_fd = create_temp_file ("tst-ofdlocks.", &temp_filename);
33 TEST_VERIFY_EXIT (temp_fd != -1);
34 }
35
36 #define PREPARE do_prepare
37
38 static int
39 do_test (void)
40 {
41 /* It first allocates a open file description lock range which can not
42 be represented in a 32 bit struct flock. */
43 struct flock64 lck64 = {
44 .l_type = F_WRLCK,
45 .l_whence = SEEK_SET,
46 .l_start = (off64_t)INT32_MAX + 1024,
47 .l_len = 1024,
48 };
49 TEST_VERIFY_EXIT (fcntl64 (temp_fd, F_OFD_SETLKW, &lck64) == 0);
50
51 /* Open file description locks placed through the same open file description
52 (either by same file descriptor or a duplicated one created by fork,
53 dup, fcntl F_DUPFD, etc.) overwrites then old lock. To force a
54 conflicting lock combination, it creates a new file descriptor. */
55 int fd = open64 (temp_filename, O_RDWR, 0666);
56 TEST_VERIFY_EXIT (fd != -1);
57
58 /* It tries then to allocate another open file descriptior with a valid
59 non-LFS bits struct flock but which will result in a conflicted region
60 which can not be represented in a non-LFS struct flock. */
61 struct flock lck = {
62 .l_type = F_WRLCK,
63 .l_whence = SEEK_SET,
64 .l_start = INT32_MAX - 1024,
65 .l_len = 4 * 1024,
66 };
67 int r = fcntl (fd, F_OFD_GETLK, &lck);
68 if (sizeof (off_t) != sizeof (off64_t))
69 TEST_VERIFY (r == -1 && errno == EOVERFLOW);
70 else
71 TEST_VERIFY (r == 0);
72
73 return 0;
74 }
75
76 #include <support/test-driver.c>