1LOCKF(3)                   Linux Programmer's Manual                  LOCKF(3)
2
3
4

NAME

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

SYNOPSIS

9       #include <unistd.h>
10
11       int lockf(int fd, int cmd, off_t len);
12
13   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
14
15       lockf(): _BSD_SOURCE || _SVID_SOURCE || _XOPEN_SOURCE >= 500
16

DESCRIPTION

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

RETURN VALUE

56       On success, zero is returned.  On error, -1 is returned, and  errno  is
57       set appropriately.
58

ERRORS

60       EACCES or EAGAIN
61              The  file  is locked and F_TLOCK or F_TEST was specified, or the
62              operation is prohibited because the file has been  memory-mapped
63              by another process.
64
65       EBADF  fd is not an open file descriptor.
66
67       EDEADLK
68              The  command  was  T_LOCK  and this lock operation would cause a
69              deadlock.
70
71       EINVAL An invalid operation was specified in fd.
72
73       ENOLCK Too many segment locks open, lock table is full.
74

CONFORMING TO

76       SVr4, POSIX.1-2001.
77

SEE ALSO

79       fcntl(2), flock(2)
80       There are also locks.txt and mandatory-locking.txt in the kernel source
81       directory  Documentation/filesystems.   (On  older kernels, these files
82       are directly under the Documentation/  directory,  and  mandatory-lock‐
83       ing.txt is called mandatory.txt.)
84

COLOPHON

86       This  page  is  part of release 3.25 of the Linux man-pages project.  A
87       description of the project, and information about reporting  bugs,  can
88       be found at http://www.kernel.org/doc/man-pages/.
89
90
91
92GNU                               2009-07-25                          LOCKF(3)
Impressum