1 /* Test that closing O_PATH descriptors does not release POSIX advisory locks.
2 Copyright (C) 2020-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
23 #include <support/check.h>
24 #include <support/namespace.h>
25 #include <support/support.h>
26 #include <support/temp_file.h>
27 #include <support/xunistd.h>
29 /* The subprocess writes the errno value of the lock operation
31 static int *shared_errno
;
33 /* The path of the temporary file which is locked. */
36 /* Try to obtain an exclusive lock on the file at path. */
38 subprocess (void *closure
)
40 int fd
= xopen (path
, O_RDWR
, 0);
41 struct flock64 lock
= { .l_type
= F_WRLCK
, };
42 int ret
= fcntl64 (fd
, F_SETLK
, &lock
);
46 *shared_errno
= errno
;
50 /* Return true if the file at path is currently locked, false if
56 support_isolate_in_subprocess (subprocess
, NULL
);
57 if (*shared_errno
== 0)
58 /* Lock was acquired by the subprocess, so this process has not
63 /* POSIX allows both EACCES and EAGAIN. Linux use EACCES. */
64 TEST_COMPARE (*shared_errno
, EAGAIN
);
72 shared_errno
= support_shared_allocate (sizeof (*shared_errno
));
73 int fd
= create_temp_file ("tst-o_path-locks-", &path
);
75 /* The file is not locked initially. */
76 TEST_VERIFY (!probe_lock ());
78 struct flock64 lock
= { .l_type
= F_WRLCK
, };
79 TEST_COMPARE (fcntl64 (fd
, F_SETLK
, &lock
), 0);
81 /* The lock has been acquired. */
82 TEST_VERIFY (probe_lock ());
84 /* Closing the same file via a different descriptor releases the
86 xclose (xopen (path
, O_RDONLY
, 0));
87 TEST_VERIFY (!probe_lock ());
89 /* But not if it is an O_PATH descriptor. */
90 TEST_COMPARE (fcntl64 (fd
, F_SETLK
, &lock
), 0);
91 xclose (xopen (path
, O_PATH
, 0));
92 TEST_VERIFY (probe_lock ());
96 support_shared_free (shared_errno
);
100 #include <support/test-driver.c>