man-pages/man2/flock.2.html
2021-03-31 01:06:50 +01:00

373 lines
8.2 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of FLOCK</TITLE>
</HEAD><BODY>
<H1>FLOCK</H1>
Section: Linux Programmer's Manual (2)<BR>Updated: 2017-09-15<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
flock - apply or remove an advisory lock on an open file
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<B>#include &lt;<A HREF="file:///usr/include/sys/file.h">sys/file.h</A>&gt;</B>
<P>
<B>int flock(int </B><I>fd</I><B>, int </B><I>operation</I><B>);</B>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
Apply or remove an advisory lock on the open file specified by
<I>fd</I>.
The argument
<I>operation</I>
is one of the following:
<DL COMPACT><DT id="1"><DD>
<DL COMPACT>
<DT id="2"><B>LOCK_SH</B>
<DD>
Place a shared lock.
More than one process may hold a shared lock for a given file
at a given time.
<DT id="3"><B>LOCK_EX</B>
<DD>
Place an exclusive lock.
Only one process may hold an exclusive lock for a given
file at a given time.
<DT id="4"><B>LOCK_UN</B>
<DD>
Remove an existing lock held by this process.
</DL>
</DL>
<P>
A call to
<B>flock</B>()
may block if an incompatible lock is held by another process.
To make a nonblocking request, include
<B>LOCK_NB</B>
(by ORing)
with any of the above operations.
<P>
A single file may not simultaneously have both shared and exclusive locks.
<P>
Locks created by
<B>flock</B>()
are associated with an open file description (see
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2)).
This means that duplicate file descriptors (created by, for example,
<B><A HREF="/cgi-bin/man/man2html?2+fork">fork</A></B>(2)
or
<B><A HREF="/cgi-bin/man/man2html?2+dup">dup</A></B>(2))
refer to the same lock, and this lock may be modified
or released using any of these file descriptors.
Furthermore, the lock is released either by an explicit
<B>LOCK_UN</B>
operation on any of these duplicate file descriptors, or when all
such file descriptors have been closed.
<P>
If a process uses
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2)
(or similar) to obtain more than one file descriptor for the same file,
these file descriptors are treated independently by
<B>flock</B>().
An attempt to lock the file using one of these file descriptors
may be denied by a lock that the calling process has
already placed via another file descriptor.
<P>
A process may hold only one type of lock (shared or exclusive)
on a file.
Subsequent
<B>flock</B>()
calls on an already locked file will convert an existing lock to the new
lock mode.
<P>
Locks created by
<B>flock</B>()
are preserved across an
<B><A HREF="/cgi-bin/man/man2html?2+execve">execve</A></B>(2).
<P>
A shared or exclusive lock can be placed on a file regardless of the
mode in which the file was opened.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
On success, zero is returned.
On error, -1 is returned, and
<I>errno</I>
is set appropriately.
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
<DL COMPACT>
<DT id="5"><B>EBADF</B>
<DD>
<I>fd</I>
is not an open file descriptor.
<DT id="6"><B>EINTR</B>
<DD>
While waiting to acquire a lock, the call was interrupted by
delivery of a signal caught by a handler; see
<B><A HREF="/cgi-bin/man/man2html?7+signal">signal</A></B>(7).
<DT id="7"><B>EINVAL</B>
<DD>
<I>operation</I>
is invalid.
<DT id="8"><B>ENOLCK</B>
<DD>
The kernel ran out of memory for allocating lock records.
<DT id="9"><B>EWOULDBLOCK</B>
<DD>
The file is locked and the
<B>LOCK_NB</B>
flag was selected.
</DL>
<A NAME="lbAG">&nbsp;</A>
<H2>CONFORMING TO</H2>
4.4BSD (the
<B>flock</B>()
call first appeared in 4.2BSD).
A version of
<B>flock</B>(),
possibly implemented in terms of
<B><A HREF="/cgi-bin/man/man2html?2+fcntl">fcntl</A></B>(2),
appears on most UNIX systems.
<A NAME="lbAH">&nbsp;</A>
<H2>NOTES</H2>
Since kernel 2.0,
<B>flock</B>()
is implemented as a system call in its own right rather
than being emulated in the GNU C library as a call to
<B><A HREF="/cgi-bin/man/man2html?2+fcntl">fcntl</A></B>(2).
With this implementation,
there is no interaction between the types of lock
placed by
<B>flock</B>()
and
<B><A HREF="/cgi-bin/man/man2html?2+fcntl">fcntl</A></B>(2),
and
<B>flock</B>()
does not detect deadlock.
(Note, however, that on some systems, such as the modern BSDs,
<B>flock</B>()
and
<B><A HREF="/cgi-bin/man/man2html?2+fcntl">fcntl</A></B>(2)
locks
<I>do</I>
interact with one another.)
<P>
<B>flock</B>()
places advisory locks only; given suitable permissions on a file,
a process is free to ignore the use of
<B>flock</B>()
and perform I/O on the file.
<P>
<B>flock</B>()
and
<B><A HREF="/cgi-bin/man/man2html?2+fcntl">fcntl</A></B>(2)
locks have different semantics with respect to forked processes and
<B><A HREF="/cgi-bin/man/man2html?2+dup">dup</A></B>(2).
On systems that implement
<B>flock</B>()
using
<B><A HREF="/cgi-bin/man/man2html?2+fcntl">fcntl</A></B>(2),
the semantics of
<B>flock</B>()
will be different from those described in this manual page.
<P>
Converting a lock
(shared to exclusive, or vice versa) is not guaranteed to be atomic:
the existing lock is first removed, and then a new lock is established.
Between these two steps,
a pending lock request by another process may be granted,
with the result that the conversion either blocks, or fails if
<B>LOCK_NB</B>
was specified.
(This is the original BSD behavior,
and occurs on many other implementations.)
<A NAME="lbAI">&nbsp;</A>
<H3>NFS details</H3>
In Linux kernels up to 2.6.11,
<B>flock</B>()
does not lock files over NFS
(i.e., the scope of locks was limited to the local system).
Instead, one could use
<B><A HREF="/cgi-bin/man/man2html?2+fcntl">fcntl</A></B>(2)
byte-range locking, which does work over NFS,
given a sufficiently recent version of
Linux and a server which supports locking.
<P>
Since Linux 2.6.12, NFS clients support
<B>flock</B>()
locks by emulating them as
<B><A HREF="/cgi-bin/man/man2html?2+fcntl">fcntl</A></B>(2)
byte-range locks on the entire file.
This means that
<B><A HREF="/cgi-bin/man/man2html?2+fcntl">fcntl</A></B>(2)
and
<B>flock</B>()
locks
<I>do</I>
interact with one another over NFS.
It also means that in order to place an exclusive lock,
the file must be opened for writing.
<P>
Since Linux 2.6.37,
the kernel supports a compatibility mode that allows
<B>flock</B>()
locks (and also
<B><A HREF="/cgi-bin/man/man2html?2+fcntl">fcntl</A></B>(2)
byte region locks) to be treated as local;
see the discussion of the
<I>local_lock</I>
option in
<B><A HREF="/cgi-bin/man/man2html?5+nfs">nfs</A></B>(5).
<A NAME="lbAJ">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?1+flock">flock</A></B>(1),
<B><A HREF="/cgi-bin/man/man2html?2+close">close</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+dup">dup</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+execve">execve</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+fcntl">fcntl</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+fork">fork</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?3+lockf">lockf</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?8+lslocks">lslocks</A></B>(8)
<P>
<I>Documentation/filesystems/locks.txt</I>
in the Linux kernel source tree
(<I>Documentation/locks.txt</I>
in older kernels)
<A NAME="lbAK">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="10"><A HREF="#lbAB">NAME</A><DD>
<DT id="11"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="12"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="13"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="14"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="15"><A HREF="#lbAG">CONFORMING TO</A><DD>
<DT id="16"><A HREF="#lbAH">NOTES</A><DD>
<DL>
<DT id="17"><A HREF="#lbAI">NFS details</A><DD>
</DL>
<DT id="18"><A HREF="#lbAJ">SEE ALSO</A><DD>
<DT id="19"><A HREF="#lbAK">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:32 GMT, March 31, 2021
</BODY>
</HTML>