Files
linux/tools/testing/selftests/filelock/ofdlocks.c
Bala-Vignesh-Reddy e6fbd1759c selftests: complete kselftest include centralization
This follow-up patch completes centralization of kselftest.h and
ksefltest_harness.h includes in remaining seltests files, replacing all
relative paths with a non-relative paths using shared -I include path in
lib.mk

Tested with gcc-13.3 and clang-18.1, and cross-compiled successfully on
riscv, arm64, x86_64 and powerpc arch.

[reddybalavignesh9979@gmail.com: add selftests include path for kselftest.h]
  Link: https://lkml.kernel.org/r/20251017090201.317521-1-reddybalavignesh9979@gmail.com
Link: https://lkml.kernel.org/r/20251016104409.68985-1-reddybalavignesh9979@gmail.com
Signed-off-by: Bala-Vignesh-Reddy <reddybalavignesh9979@gmail.com>
Suggested-by: Andrew Morton <akpm@linux-foundation.org>
Link: https://lore.kernel.org/lkml/20250820143954.33d95635e504e94df01930d0@linux-foundation.org/
Reviewed-by: Wei Yang <richard.weiyang@gmail.com>
Cc: David Hildenbrand <david@redhat.com>
Cc: David S. Miller <davem@davemloft.net>
Cc: Eric Dumazet <edumazet@google.com>
Cc: Günther Noack <gnoack@google.com>
Cc: Jakub Kacinski <kuba@kernel.org>
Cc: Liam Howlett <liam.howlett@oracle.com>
Cc: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Mickael Salaun <mic@digikod.net>
Cc: Ming Lei <ming.lei@redhat.com>
Cc: Paolo Abeni <pabeni@redhat.com>
Cc: Shuah Khan <shuah@kernel.org>
Cc: Simon Horman <horms@kernel.org>
Cc: Suren Baghdasaryan <surenb@google.com>
Cc: Vlastimil Babka <vbabka@suse.cz>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2025-11-27 14:24:31 -08:00

133 lines
3.0 KiB
C

// SPDX-License-Identifier: GPL-2.0
#define _GNU_SOURCE
#include <fcntl.h>
#include <assert.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "kselftest.h"
static int lock_set(int fd, struct flock *fl)
{
int ret;
fl->l_pid = 0; // needed for OFD locks
fl->l_whence = SEEK_SET;
ret = fcntl(fd, F_OFD_SETLK, fl);
if (ret)
perror("fcntl()");
return ret;
}
static int lock_get(int fd, struct flock *fl)
{
int ret;
fl->l_pid = 0; // needed for OFD locks
fl->l_whence = SEEK_SET;
ret = fcntl(fd, F_OFD_GETLK, fl);
if (ret)
perror("fcntl()");
return ret;
}
int main(void)
{
int rc;
struct flock fl, fl2;
int fd = open("/tmp/aa", O_RDWR | O_CREAT | O_EXCL, 0600);
int fd2 = open("/tmp/aa", O_RDONLY);
unlink("/tmp/aa");
assert(fd != -1);
assert(fd2 != -1);
ksft_print_msg("[INFO] opened fds %i %i\n", fd, fd2);
/* Set some read lock */
fl.l_type = F_RDLCK;
fl.l_start = 5;
fl.l_len = 3;
rc = lock_set(fd, &fl);
if (rc == 0) {
ksft_print_msg
("[SUCCESS] set OFD read lock on first fd\n");
} else {
ksft_print_msg("[FAIL] to set OFD read lock on first fd\n");
return -1;
}
/* Make sure read locks do not conflict on different fds. */
fl.l_type = F_RDLCK;
fl.l_start = 5;
fl.l_len = 1;
rc = lock_get(fd2, &fl);
if (rc != 0)
return -1;
if (fl.l_type != F_UNLCK) {
ksft_print_msg("[FAIL] read locks conflicted\n");
return -1;
}
/* Make sure read/write locks do conflict on different fds. */
fl.l_type = F_WRLCK;
fl.l_start = 5;
fl.l_len = 1;
rc = lock_get(fd2, &fl);
if (rc != 0)
return -1;
if (fl.l_type != F_UNLCK) {
ksft_print_msg
("[SUCCESS] read and write locks conflicted\n");
} else {
ksft_print_msg
("[SUCCESS] read and write locks not conflicted\n");
return -1;
}
/* Get info about the lock on first fd. */
fl.l_type = F_UNLCK;
fl.l_start = 5;
fl.l_len = 1;
rc = lock_get(fd, &fl);
if (rc != 0) {
ksft_print_msg
("[FAIL] F_OFD_GETLK with F_UNLCK not supported\n");
return -1;
}
if (fl.l_type != F_UNLCK) {
ksft_print_msg
("[SUCCESS] F_UNLCK test returns: locked, type %i pid %i len %zi\n",
fl.l_type, fl.l_pid, fl.l_len);
} else {
ksft_print_msg
("[FAIL] F_OFD_GETLK with F_UNLCK did not return lock info\n");
return -1;
}
/* Try the same but by locking everything by len==0. */
fl2.l_type = F_UNLCK;
fl2.l_start = 0;
fl2.l_len = 0;
rc = lock_get(fd, &fl2);
if (rc != 0) {
ksft_print_msg
("[FAIL] F_OFD_GETLK with F_UNLCK not supported\n");
return -1;
}
if (memcmp(&fl, &fl2, sizeof(fl))) {
ksft_print_msg
("[FAIL] F_UNLCK test returns: locked, type %i pid %i len %zi\n",
fl.l_type, fl.l_pid, fl.l_len);
return -1;
}
ksft_print_msg("[SUCCESS] F_UNLCK with len==0 returned the same\n");
/* Get info about the lock on second fd - no locks on it. */
fl.l_type = F_UNLCK;
fl.l_start = 0;
fl.l_len = 0;
lock_get(fd2, &fl);
if (fl.l_type != F_UNLCK) {
ksft_print_msg
("[FAIL] F_OFD_GETLK with F_UNLCK return lock info from another fd\n");
return -1;
}
return 0;
}