F_GETLEASE(2const)
π¦ F_GETLEASE, F_SETLEASE β the lease manual, bestie edition
okay so bestie, you're in the F_GETLEASE(2const) page π this is a section-2-const situation, and we're talking about leases β yes, file leases, not apartment leases, though honestly both are about who gets to touch the thing.
π LIBRARY
Standard C library (libc, -lc). yep that's it, that's the whole vibe.
β¨ SYNOPSIS
#define _GNU_SOURCE
#include <fcntl.h>
int fcntl(int fd, F_SETLEASE, int arg);
int fcntl(int fd, F_GETLEASE);
both of these are fcntl() operations, so like, same function, different little command word. iykyk π
π DESCRIPTION
So here's the tea, bestie. πΈ F_SETLEASE and F_GETLEASE are how you establish a new lease and retrieve the current lease on the open file description that fd points at.
And what even IS a lease? Okay so β a file lease is a whole mechanism where the process holding the lease (we call her the "lease holder", main character energy π) gets notified β by receiving a signal, no cap β when some other process (the "lease breaker", booo π
) tries to open(2) or truncate(2) the file that fd refers to. literally a little doorbell that rings when someone's about to mess with your file.
π₯ F_SETLEASE
Sets or removes a file lease, depending on which of these values you put in the integer arg:
F_RDLCKβ Establish a read lease. This means the calling process gets notified when the file is opened for writing or is truncated. Heads up though: a read lease can only go on a file descriptor that's opened read-only. πF_WRLCKβ Establish a write lease. This one notifies the caller when the file is opened for reading or writing, or is truncated. And a write lease may only be placed on a file if there are no other open file descriptors for the file. not one. zero. periodt. πF_UNLCKβ Remove our lease from the file. bye bestie π«Ά
okay important structural lore: leases are associated with an open file description (peep open(2)). What that means in practice is that duplicate file descriptors β the ones born from things like fork(2) or dup(2) β all refer to the same lease, and you can modify or release that lease using any of those descriptors. Also, the lease gets released either by an explicit F_UNLCK operation on any of those duplicate descriptors, or when all of those descriptors have been closed. so like, it's not per-fd, it's per-open-file-description. slay β¨
Also! Leases may be taken out only on regular files. And an unprivileged process may only take out a lease on a file whose UID (owner) matches the filesystem UID of the process. If you've got the CAP_LEASE capability though? go off, you can take out leases on arbitrary files. π
π F_GETLEASE
Tells you what type of lease is hanging out with the file descriptor fd, by returning either F_RDLCK, F_WRLCK, or F_UNLCK β meaning a read lease, a write lease, or no lease at all. And arg is ignored here, don't even worry about it. π
π the lease break storyline (this is the good part)
When a process β our lease breaker β performs an open(2) or truncate(2) that conflicts with a lease established via F_SETLEASE, the kernel blocks the system call and notifies the lease holder by sending it a signal (SIGIO by default). β¨
Then the lease holder is supposed to respond to that signal by doing whatever cleanup is required so another process can access the file (e.g., flushing cached buffers), and then either remove or downgrade her lease.
Here's how, no confusion:
- A lease is removed by performing an
F_SETLEASEoperation specifyingargasF_UNLCK. - If the lease holder currently holds a write lease on the file, and the lease breaker is opening the file for reading, then it's enough for the lease holder to just downgrade the lease to a read lease β done by performing an
F_SETLEASEoperation specifyingargasF_RDLCK.
If the lease holder fails to downgrade or remove the lease within the number of seconds specified in /proc/sys/fs/lease-break-time, then the kernel forcibly removes or downgrades her lease. the kernel said "time's up bestie" π
Once a lease break has been initiated, F_GETLEASE returns the target lease type (either F_RDLCK or F_UNLCK, depending on what would be compatible with the lease breaker) β and it keeps returning that until the lease holder voluntarily downgrades or removes the lease, or the kernel forcibly does so after the lease break timer expires. π
Once the lease has been voluntarily or forcibly removed or downgraded, and assuming the lease breaker hasn't unblocked its system call, the kernel permits the lease breaker's system call to proceed. π
π«Ά the interrupt lore (all the edge cases, we keep them)
- If the lease breaker's blocked
open(2)ortruncate(2)is interrupted by a signal handler, then the system call fails with the errorEINTRβ but the other steps still occur as described above. π - If the lease breaker is killed by a signal while blocked in
open(2)ortruncate(2), then the other steps still occur as described above. π - If the lease breaker specifies the
O_NONBLOCKflag when callingopen(2), then the call immediately fails with the errorEWOULDBLOCKβ but, you guessed it, the other steps still occur as described above. π
π the signal glow-up
The default signal used to notify the lease holder is SIGIO, but you can change it using the F_SETSIG operation to fcntl().
And here's the cute part: if an F_SETSIG operation is performed (even one specifying SIGIO), and the signal handler is established using SA_SIGINFO, then the handler will receive a siginfo_t structure as its second argument, and the si_fd field of that argument will hold the file descriptor of the leased file that has been accessed by another process. (This is useful if the caller holds leases against multiple files.) β¨ such a serve.
β RETURN VALUE
F_GETLEASEβ Type of lease held on file descriptor.F_SETLEASEβ Zero.
On error, -1 is returned, and errno is set to indicate the error. π
π ERRORS
See fcntl(2).
These operations can also fail with the following error codes:
EAGAINβ The operation is prohibited because the file is open in a way that conflicts with the requested lease.EINVALβ The operation is prohibited because the underlying filesystem doesn't support leases, or becausefddoes not represent a regular file.
πΈ STANDARDS
Linux. π§
π§ HISTORY
Linux 2.4.
π SEE ALSO
ok that's the whole page, bestie. go forth and lease responsibly π«Ά
mwah π this was the F_GETLEASE(2const) page from Linux man-pages 6.20, dated 2026-01-14. love u, bye! πβ¨
π¦ where this page comes from
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- π the page's license: Linux-man-pages-copyleft
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms