tools/testing/selftests/filelock/ofdlocks.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/filelock/ofdlocks.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/filelock/ofdlocks.c- Extension
.c- Size
- 2885 bytes
- Lines
- 121
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
fcntl.hassert.hstdio.hunistd.hstring.hkselftest.h
Detected Declarations
function lock_setfunction lock_getfunction main
Annotated Snippet
// 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)
ksft_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)
ksft_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);
ksft_print_header();
ksft_set_plan(4);
unlink("/tmp/aa");
assert(fd != -1);
assert(fd2 != -1);
ksft_print_msg("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);
ksft_test_result(rc == 0, "set OFD read lock on first fd\n");
if (rc != 0)
ksft_finished();
/* 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)
ksft_finished();
if (fl.l_type != F_UNLCK)
ksft_exit_fail_msg("read locks conflicted\n");
/* 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)
ksft_finished();
ksft_test_result(fl.l_type != F_UNLCK,
"read and write locks conflicted\n");
if (fl.l_type == F_UNLCK)
ksft_finished();
/* 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_exit_fail_msg("F_OFD_GETLK with F_UNLCK not supported\n");
ksft_test_result(fl.l_type != F_UNLCK,
"F_OFD_GETLK with F_UNLCK returned lock info\n");
if (fl.l_type == F_UNLCK)
Annotation
- Immediate include surface: `fcntl.h`, `assert.h`, `stdio.h`, `unistd.h`, `string.h`, `kselftest.h`.
- Detected declarations: `function lock_set`, `function lock_get`, `function main`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.