1lockf(3)                   Library Functions Manual                   lockf(3)
2
3
4

NAME

6       lockf - apply, test or remove a POSIX lock on an open file
7

LIBRARY

9       Standard C library (libc, -lc)
10

SYNOPSIS

12       #include <unistd.h>
13
14       int lockf(int fd, int cmd, off_t len);
15
16   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
17
18       lockf():
19           _XOPEN_SOURCE >= 500
20               || /* glibc >= 2.19: */ _DEFAULT_SOURCE
21               || /* glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
22

DESCRIPTION

24       Apply,  test, or remove a POSIX lock on a section of an open file.  The
25       file is specified by fd, a file descriptor open for writing, the action
26       by  cmd,  and  the section consists of byte positions pos..pos+len-1 if
27       len is positive, and pos-len..pos-1 if len is negative,  where  pos  is
28       the current file position, and if len is zero, the section extends from
29       the current file position to infinity, encompassing the present and fu‐
30       ture  end-of-file positions.  In all cases, the section may extend past
31       current end-of-file.
32
33       On Linux, lockf() is just an interface  on  top  of  fcntl(2)  locking.
34       Many other systems implement lockf() in this way, but note that POSIX.1
35       leaves the relationship between lockf() and fcntl(2) locks unspecified.
36       A  portable application should probably avoid mixing calls to these in‐
37       terfaces.
38
39       Valid operations are given below:
40
41       F_LOCK Set an exclusive lock on the specified section of the file.   If
42              (part  of) this section is already locked, the call blocks until
43              the previous lock is released.  If this section overlaps an ear‐
44              lier  locked  section, both are merged.  File locks are released
45              as soon as the process holding the locks closes  some  file  de‐
46              scriptor  for  the file.  A child process does not inherit these
47              locks.
48
49       F_TLOCK
50              Same as F_LOCK but the call never blocks and  returns  an  error
51              instead if the file is already locked.
52
53       F_ULOCK
54              Unlock  the  indicated  section  of  the file.  This may cause a
55              locked section to be split into two locked sections.
56
57       F_TEST Test the lock: return 0 if the specified section is unlocked  or
58              locked  by  this process; return -1, set errno to EAGAIN (EACCES
59              on some other systems), if another process holds a lock.
60

RETURN VALUE

62       On success, zero is returned.  On error, -1 is returned, and  errno  is
63       set to indicate the error.
64

ERRORS

66       EACCES or EAGAIN
67              The  file  is locked and F_TLOCK or F_TEST was specified, or the
68              operation is prohibited because the file has been  memory-mapped
69              by another process.
70
71       EBADF  fd  is  not an open file descriptor; or cmd is F_LOCK or F_TLOCK
72              and fd is not a writable file descriptor.
73
74       EDEADLK
75              The command was F_LOCK and this lock  operation  would  cause  a
76              deadlock.
77
78       EINTR  While waiting to acquire a lock, the call was interrupted by de‐
79              livery of a signal caught by a handler; see signal(7).
80
81       EINVAL An invalid operation was specified in cmd.
82
83       ENOLCK Too many segment locks open, lock table is full.
84

ATTRIBUTES

86       For an  explanation  of  the  terms  used  in  this  section,  see  at‐
87       tributes(7).
88
89       ┌────────────────────────────────────────────┬───────────────┬─────────┐
90Interface                                   Attribute     Value   
91       ├────────────────────────────────────────────┼───────────────┼─────────┤
92lockf()                                     │ Thread safety │ MT-Safe │
93       └────────────────────────────────────────────┴───────────────┴─────────┘
94

STANDARDS

96       POSIX.1-2008.
97

HISTORY

99       POSIX.1-2001, SVr4.
100

SEE ALSO

102       fcntl(2), flock(2)
103
104       locks.txt and mandatory-locking.txt in the Linux kernel source directo‐
105       ry Documentation/filesystems (on older kernels, these files are direct‐
106       ly  under  the  Documentation  directory,  and mandatory-locking.txt is
107       called mandatory.txt)
108
109
110
111Linux man-pages 6.04              2023-03-30                          lockf(3)
Impressum