LOCKF(3P)

πŸ’– LOCKF(3P) β€” record locking on files πŸ’–

okay bestie, so this one is from the POSIX Programmer's Manual, and it's literally the blueprint for lockf(). Quick heads up though, because the universe loves chaos: the Linux version of this interface may do its own thing (so go read the Linux man page if you're actually on Linux, bestie), or it might not even be implemented on Linux at all. πŸ™ˆ


🫢 PROLOG 🫢

This manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ β€” consult the corresponding Linux manual page for details of Linux behavior β€” or the interface may not be implemented on Linux. Yeah. Read the fine print, it's giving "terms and conditions". πŸ’€


✨ NAME ✨

lockf β€” record locking on files. slay


🌸 SYNOPSIS 🌸

#include <unistd.h>

int lockf(int fildes, int function, off_t size);

πŸ’… DESCRIPTION πŸ’…

Okay so here's the vibe: lockf() locks sections of a file with advisory-mode locks. If threads in other processes try to lock a section that's already locked, they either get an error value back or they just block until that section frees up. And every lock a process holds? All gone the moment the process terminates. Record locking with lockf() shall be supported for regular files, and may be supported for other files. iykyk. πŸ¦‹

The fildes argument is an open file descriptor. To actually establish a lock with this function, that file descriptor has to be opened with write-only permission (O_WRONLY) or read/write permission (O_RDWR). No read-only shortcuts, sorry bestie. πŸ’€

The function argument is a control value that says what action to take. The permissible values for function are defined in <unistd.h> and they are, in no particular order except this order:

  • F_ULOCK β€” Unlock locked sections.
  • F_LOCK β€” Lock a section for exclusive use.
  • F_TLOCK β€” Test and lock a section for exclusive use.
  • F_TEST β€” Test a section for locks by other processes.

F_TEST shall detect if a lock by another process is present on the specified section.

F_LOCK and F_TLOCK shall both lock a section of a file if the section is available.

F_ULOCK shall remove locks from a section of the file.

Now, the size argument is the number of contiguous bytes to be locked or unlocked. The section to be locked or unlocked starts at the current offset in the file and extends forward for a positive size or backward for a negative size (that's the preceding bytes, up to but not including the current offset). If size is 0, the section from the current offset through the largest possible file offset shall be locked β€” that is, from the current offset through the present or any future end-of-file. And plot twist: an area need not be allocated to the file to be locked, because locks may exist past the end-of-file. πŸ”₯

The sections locked with F_LOCK or F_TLOCK may, in whole or in part, contain or be contained by a previously locked section for the same process. When that happens, or if adjacent locked sections would occur, the sections shall be combined into a single locked section. And if the request would cause the number of locks to exceed a system-imposed limit, the request shall fail. the way that system said "that's enough" πŸ’€

F_LOCK and F_TLOCK requests differ only by the action taken if the section is not available. F_LOCK shall block the calling thread until the section is available. F_TLOCK shall cause the function to fail if the section is already locked by another process. one waits patiently, the other is a drama queen. iconic behavior. πŸ‘‘

File locks shall be released on first close by the locking process of any file descriptor for the file.

F_ULOCK requests may release (wholly or in part) one or more locked sections controlled by the process. Locked sections shall be unlocked starting at the current file offset through size bytes or to the end-of-file if size is (off_t)0. When all of a locked section is not released (that is, when the beginning or end of the area to be unlocked falls within a locked section), the remaining portions of that section shall remain locked by the process. Releasing the center portion of a locked section shall cause the remaining locked beginning and end portions to become two separate locked sections. it's giving mitosis fr fr 🧚

If the request would cause the number of locks in the system to exceed a system-imposed limit, the request shall fail.

⚠️ Deadlock alert, bestie. A potential for deadlock occurs if the threads of a process controlling a locked section are blocked by accessing a locked section of another process. If the system detects that deadlock would occur, lockf() shall fail with an [EDEADLK] error. mutual blocking is NOT the moment. 😭

The interaction between fcntl() and lockf() locks is unspecified. yeah. nobody knows. πŸ’€

Blocking on a section shall be interrupted by any signal.

Okay this next one is a whole mouthful but we're doing it: an F_ULOCK request in which size is non-zero and the offset of the last byte of the requested section is the maximum value for an object of type off_t, when the process has an existing lock in which size is 0 and which includes the last byte of the requested section, shall be treated as a request to unlock from the start of the requested section with a size equal to 0. Otherwise, an F_ULOCK request shall attempt to unlock only the requested section. phew. 🫢

Attempting to lock a section of a file that is associated with a buffered stream produces unspecified results. πŸ™ˆ


🌈 RETURN VALUE 🌈

Upon successful completion, lockf() shall return 0. Otherwise, it shall return -1, set errno to indicate an error, and existing locks shall not be changed. no sneaky partial edits, periodt.


πŸ”₯ ERRORS πŸ”₯

The lockf() function shall fail if:

  • EBADF β€” The fildes argument is not a valid open file descriptor; or function is F_LOCK or F_TLOCK and fildes is not a valid file descriptor open for writing.
  • EACCES or EAGAIN β€” The function argument is F_TLOCK or F_TEST and the section is already locked by another process.
  • EDEADLK β€” The function argument is F_LOCK and a deadlock is detected.
  • EINTR β€” A signal was caught during execution of the function.
  • EINVAL β€” The function argument is not one of F_LOCK, F_TLOCK, F_TEST, or F_ULOCK; or size plus the current file offset is less than 0.
  • EOVERFLOW β€” The offset of the first, or if size is not 0 then the last, byte in the requested section cannot be represented correctly in an object of type off_t.

The lockf() function may fail if:

  • EAGAIN β€” The function argument is F_LOCK or F_TLOCK and the file is mapped with mmap().
  • EDEADLK or ENOLCK β€” The function argument is F_LOCK, F_TLOCK, or F_ULOCK, and the request would cause the number of locks to exceed a system-imposed limit.
  • EOPNOTSUPP or EINVAL β€” The implementation does not support the locking of files of the type indicated by the fildes argument.

The following sections are informative. (translation: the strict part is over, now we vibe ✨)


πŸŽ€ EXAMPLES πŸŽ€

πŸ¦‹ Locking a Portion of a File πŸ¦‹

In the following example, a file named /home/cnd/mod1 is being modified. Other processes that use locking are prevented from changing it during this process. Only the first 10000 bytes are locked, and the lock call fails if another process has any part of this area locked already. so it's giving "this is mine, back off" πŸ’…

#include <fcntl.h>
#include <unistd.h>

int fildes;
int status;
...
fildes = open("/home/cnd/mod1", O_RDWR);
status = lockf(fildes, F_TLOCK, (off_t)10000);

⭐ APPLICATION USAGE ⭐

Record-locking should not be used in combination with the fopen(), fread(), fwrite(), and other stdio functions. Instead, the more primitive, non-buffered functions (such as open()) should be used. Unexpected results may occur in processes that do buffering in the user address space β€” the process may later read/write data which is/was locked. The stdio functions are the most common source of unexpected buffering. so yeah, mixing them is giving "chaos, but not the fun kind". πŸ’€

The alarm() function may be used to provide a timeout facility in applications requiring it. handy for when you need a "bestie, wrap it up" timer. ⏰


πŸ’œ RATIONALE πŸ’œ

None. she said nothing, and honestly? respect. πŸ™ˆ


🌸 FUTURE DIRECTIONS 🌸

None. same energy. ✨


🫢 SEE ALSO 🫢

alarm(), chmod(), close(), creat(), fcntl(), fopen(), mmap(), open(), read(), write()

The Base Definitions volume of POSIX.1‐2017, <unistd.h>


Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html . no cap, the original is the main character here. πŸ‘‘

Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html . if you spot one, tell the devs, not me bestie 🫢


and that's the whole thing!! this one's brought to you by IEEE / The Open Group, 2017 β€” thanks for coming to my ted talk, go forth and lock responsibly, mwah πŸ’–βœ¨πŸŽ€πŸ¦‹πŸŒΈπŸ’…

πŸ“¦ where this page comes from