@@ -58,6 +58,7 @@ libsupport-routines = \
support_descriptors \
support_enter_mount_namespace \
support_enter_network_namespace \
+ support_fcntl_support_ofd_locks \
support_format_address_family \
support_format_addrinfo \
support_format_dns_packet \
@@ -178,6 +178,9 @@ static __inline bool support_itimer_support_time64 (void)
#endif
}
+/* Return true if the kernel/file supports open file description locks. */
+extern bool support_fcntl_support_ofd_locks (int fd);
+
/* Return true if stat supports nanoseconds resolution. PATH is used
for tests and its ctime may change. */
extern bool support_stat_nanoseconds (const char *path);
new file mode 100644
@@ -0,0 +1,44 @@
+/* Return whether the kernel/file supports OFD locks.
+ Copyright (C) 2023 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <https://www.gnu.org/licenses/>. */
+
+#include <support/support.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <string.h>
+
+bool
+support_fcntl_support_ofd_locks (int fd)
+{
+#ifdef F_OFD_GETLK
+ int res;
+ struct flock flock;
+ memset (&flock, 0, sizeof (flock));
+
+ flock.l_type = F_WRLCK;
+ flock.l_whence = SEEK_SET;
+ flock.l_start = 0;
+ flock.l_len = INT32_MAX;
+ flock.l_pid = 0;
+
+ res = fcntl (fd, F_OFD_GETLK, &flock);
+ return res != -1 || errno != EINVAL;
+#else
+ (void) fd;
+ return false;
+#endif
+}