|File: [cvs.NetBSD.org] / src / lib / libc / gen / lockf.3 (download)
Revision 1.12, Sat Oct 15 21:35:49 2011 UTC (19 months ago) by rmind
CVS Tags: yamt-pagecache-base8, yamt-pagecache-base7, yamt-pagecache-base6, yamt-pagecache-base5, yamt-pagecache-base4, yamt-pagecache-base3, yamt-pagecache-base2, yamt-pagecache-base, yamt-pagecache, tls-maxphys-nbase, tls-maxphys-base, tls-maxphys, netbsd-6-base, netbsd-6-1-RELEASE, netbsd-6-1-RC4, netbsd-6-1-RC3, netbsd-6-1-RC2, netbsd-6-1-RC1, netbsd-6-1, netbsd-6-0-RELEASE, netbsd-6-0-RC2, netbsd-6-0-RC1, netbsd-6-0-2-RELEASE, netbsd-6-0-1-RELEASE, netbsd-6-0, netbsd-6, matt-nb6-plus-nbase, matt-nb6-plus-base, matt-nb6-plus, agc-symver-base, agc-symver, HEAD
Changes since 1.11: +12 -4
Mention POSIX conformance of some functions, when they appeared, sprinkle .Xr.
.\" $NetBSD: lockf.3,v 1.12 2011/10/15 21:35:49 rmind Exp $
.\" Copyright (c) 1997 The NetBSD Foundation, Inc.
.\" All rights reserved.
.\" This code is derived from software contributed to The NetBSD Foundation
.\" by Klaus Klein and S.P. Zeidler.
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
.\" POSSIBILITY OF SUCH DAMAGE.
.Dd October 15, 2011
.Dt LOCKF 3
.Nd record locking on files
.Fn lockf "int filedes" "int function" "off_t size"
function allows sections of a file to be locked with advisory-mode locks.
from other processes which attempt to lock the locked file section will
either return an error value or block until the section becomes unlocked.
All the locks for a process are removed when the process terminates.
is an open file descriptor.
The file descriptor must have been opened either for write-only
.Dv ( O_WRONLY )
.Dv ( O_RDWR )
argument is a control value which specifies the action to be taken.
The permissible values for
are as follows:
.Bl -tag -width F_ULOCKXX -compact -offset indent
.It Sy Function
.It Dv F_ULOCK
unlock locked sections
.It Dv F_LOCK
lock a section for exclusive use
.It Dv F_TLOCK
test and lock a section for exclusive use
.It Dv F_TEST
test a section for locks by other processes
removes locks from a section of the file;
both lock a section of a file if the section is available;
detects if a lock by another process is present on the specified section.
argument is the number of contiguous bytes to be locked or
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 (the preceding bytes up to but not including the
However, it is not permitted to lock a section that
starts or extends before the beginning of the file.
is 0, the section from the current offset through the largest possible
file offset is locked (that is, from the current offset through the
present or any future end-of-file).
The sections locked with
may, in whole or in part, contain or be contained by a previously
locked section for the same process.
When this occurs, or if adjacent
locked sections would occur, the sections are combined into a single
If the request would cause the number of locks to
exceed a system-imposed limit, the request will fail.
requests differ only by the action taken if the section is not
blocks the calling process until the section is available.
makes the function fail if the section is already locked by another
File locks are released on first close by the locking process of any
file descriptor for the file.
requests release (wholly or in part) one or more locked sections
controlled by the process.
Locked sections will be unlocked starting
at the current file offset through
bytes or to the end of file if size is 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 are still locked by the process.
Releasing the center
portion of a locked section will cause the remaining locked beginning
and end portions to become two separate locked sections.
request would cause the number of locks in the system to exceed a
system-imposed limit, the request will fail.
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, will be treated
as a request to unlock from the start of the requested section with a
size equal to 0.
request will attempt to unlock only the requested section.
A potential for deadlock occurs if a process controlling a locked
region is put to sleep by attempting to lock the locked region of
This implementation detects that sleeping until a
locked region is unlocked would cause a deadlock and fails with an
.Fn lockf ,
.Xr fcntl 2
.Xr flock 2
locks may be safely used concurrently.
Blocking on a section is interrupted by any signal.
.Sh RETURN VALUES
If successful, the
function returns 0.
Otherwise, it returns \-1, sets
to indicate an error, and existing locks are not changed.
will fail if:
.Bl -tag -width Er
.It Bq Er EAGAIN
and the section is already locked by another process.
.It Bq Er EBADF
is not a valid open file descriptor.
.Dv F_TLOCK ,
is not a valid file descriptor open for writing.
.It Bq Er EDEADLK
and a deadlock is detected.
.It Bq Er EINTR
was interrupted by the delivery of a signal.
.It Bq Er EINVAL
is not one of
.Dv F_ULOCK ,
.Dv F_LOCK ,
.Dv F_TEST .
refers to a file that does not support locking.
.It Bq Er ENOLCK
.Dv F_ULOCK ,
.Dv F_TLOCK ,
and satisfying the lock or unlock request would result in the number
of locked regions in the system exceeding a system-imposed limit.
.Sh SEE ALSO
.Xr fcntl 2 ,
.Xr flock 2 ,
.Xr flockfile 3
function conforms to
.St -p1003.1-2008 .
function first appeared in
.Fx 1.4 .