lockf(3)

๐Ÿ’… lockf(3) โ€” like, the POSIX file-locking girlie

okay bestie, welcome to lockf(3) from the Library Functions Manual โœจ this one lives in the Standard C library (libc, -lc) and she is ALL about putting locks on bits of an open file. apply them, test them, remove them โ€” the whole vibe. ๐ŸŽ€


โœจ SYNOPSIS โœจ

#include <unistd.h>

int lockf(int fd, int op, off_t size);

๐Ÿฆ‹ Feature Test Macro Requirements for glibc (peep feature_test_macros(7))

lockf():
    _XOPEN_SOURCE >= 500
        || /* glibc >= 2.19: */ _DEFAULT_SOURCE
        || /* glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE

no cap, you need one of those macros defined or glibc will simply not hand her over ๐Ÿ™ˆ


๐ŸŒธ DESCRIPTION ๐ŸŒธ

So here's the tea: lockf() applies, tests, or removes a POSIX lock on a section of an open file. ๐Ÿซถ

  • the file is the one behind fd โ€” a file descriptor that's open for writing
  • the action is whatever you put in op
  • and the section? it's all about byte positions, and it depends on the sign of size:
    • if size is positive, the section is pos..pos+size-1
    • if size is negative, the section is pos-size..pos-1
    • where pos is the current file position
    • and if size is zero, the section stretches from the current file position all the way to infinity, absorbing both the present and future end-of-file positions ๐ŸŒˆ

and in every single one of those cases, the section is allowed to extend past the current end-of-file. slay, boundaries are fake.

On Linux, lockf() is literally just an interface sitting on top of fcntl(2) locking. Many other systems do lockf() this exact same way, but โ€” heads up โ€” POSIX.1 leaves the relationship between lockf() and fcntl(2) locks unspecified. So if you want a portable app, you should probably not be mixing calls to these two interfaces. pick a lane, bestie ๐Ÿ˜ญ

The valid operations are right here, and there are four of them:

  • F_LOCK โ€” set an exclusive lock on the specified section of the file. if (part of) that section is already locked, the call blocks until the previous lock gets released. and if this section overlaps an earlier locked section, the two get merged into one ๐Ÿซ‚. also: file locks are released the moment the process holding them closes some file descriptor for the file. and a child process does not inherit these locks. main character energy only, no hand-me-downs ๐Ÿ‘‘
  • F_TLOCK โ€” same as F_LOCK, except this one never blocks, and instead returns an error if the file is already locked. non-committal queen ๐Ÿ’…
  • F_ULOCK โ€” unlock the indicated section of the file. this can cause a locked section to get split into two locked sections, which is honestly kind of iconic ๐ŸŽ€
  • F_TEST โ€” test the lock: return 0 if the specified section is unlocked or locked by this process; return -1 and set errno to EAGAIN (EACCES on some other systems) if another process holds a lock. she's just asking questions, not starting drama ๐Ÿ”ฅ

๐Ÿ’– RETURN VALUE ๐Ÿ’–

On success, zero is returned. โœจ

On error, -1 is returned, and errno is set to tell you exactly what went wrong. ๐Ÿ˜ญ


๐Ÿ’€ ERRORS ๐Ÿ’€

  • EACCES or EAGAIN โ€” the file is locked and F_TLOCK or F_TEST was specified, or the operation is prohibited because the file has been memory-mapped by another process. ngl, rude.
  • EBADF โ€” fd is not an open file descriptor; or op is F_LOCK or F_TLOCK and fd is not a writable file descriptor. you gotta be writable for those two, periodt.
  • EDEADLK โ€” op was F_LOCK and this lock operation would cause a deadlock. we do not do deadlocks in this house ๐Ÿ™ˆ
  • EINTR โ€” while waiting to acquire a lock, the call was interrupted by delivery of a signal caught by a handler; see signal(7).
  • EINVAL โ€” an invalid operation was specified in op.
  • ENOLCK โ€” too many segment locks open, lock table is full. the lock table is, like, full full ๐Ÿ“š

โญ ATTRIBUTES โญ

For an explanation of the terms used in this section, peep attributes(7). here's the deets:

  • Interface: lockf()
  • Attribute: Thread safety
  • Value: MT-Safe

so yeah, she's MT-Safe ๐Ÿ’œ


๐ŸŒŸ STANDARDS ๐ŸŒŸ

POSIX.1-2008. certified, official, canon โœจ


๐Ÿงš HISTORY ๐Ÿงš

POSIX.1-2001, SVr4. she's been around, she's seen things ๐Ÿ’…


๐Ÿ”ฅ SEE ALSO ๐Ÿ”ฅ

also, for the extra-credit girlies: locks.txt and mandatory-locking.txt live in the Linux kernel source directory Documentation/filesystems โ€” and btw, on older kernels those files sit directly under the Documentation directory, and mandatory-locking.txt is called mandatory.txt there. iykyk ๐ŸŒธ


and that's the whole page bestie, mwah ๐Ÿ’– this was Linux man-pages 6.20, dated 2026-02-08 โ€” go forth and lock responsibly, no cap ๐Ÿซถโœจ
๐Ÿ“ฆ 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: GPL-2.0-or-later
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms