2011-10-16 01:35:49 +04:00
|
|
|
.\" $NetBSD: lockf.3,v 1.12 2011/10/15 21:35:49 rmind Exp $
|
1997-12-20 23:23:15 +03:00
|
|
|
.\"
|
|
|
|
.\" 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.
|
|
|
|
.\"
|
2011-10-16 01:35:49 +04:00
|
|
|
.Dd October 15, 2011
|
1997-12-20 23:23:15 +03:00
|
|
|
.Dt LOCKF 3
|
1999-03-22 22:44:33 +03:00
|
|
|
.Os
|
1997-12-20 23:23:15 +03:00
|
|
|
.Sh NAME
|
|
|
|
.Nm lockf
|
|
|
|
.Nd record locking on files
|
1998-02-05 21:45:17 +03:00
|
|
|
.Sh LIBRARY
|
|
|
|
.Lb libc
|
1997-12-20 23:23:15 +03:00
|
|
|
.Sh SYNOPSIS
|
2003-04-16 17:34:34 +04:00
|
|
|
.In unistd.h
|
1997-12-20 23:23:15 +03:00
|
|
|
.Ft int
|
|
|
|
.Fn lockf "int filedes" "int function" "off_t size"
|
|
|
|
.Sh DESCRIPTION
|
|
|
|
The
|
|
|
|
.Fn lockf
|
|
|
|
function allows sections of a file to be locked with advisory-mode locks.
|
|
|
|
Calls to
|
|
|
|
.Fn lockf
|
|
|
|
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.
|
|
|
|
.Pp
|
|
|
|
The argument
|
|
|
|
.Fa filedes
|
|
|
|
is an open file descriptor.
|
|
|
|
The file descriptor must have been opened either for write-only
|
|
|
|
.Dv ( O_WRONLY )
|
|
|
|
or read/write
|
|
|
|
.Dv ( O_RDWR )
|
|
|
|
operation.
|
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Fa function
|
|
|
|
argument is a control value which specifies the action to be taken.
|
|
|
|
The permissible values for
|
|
|
|
.Fa function
|
|
|
|
are as follows:
|
2011-03-30 20:37:09 +04:00
|
|
|
.Pp
|
1997-12-20 23:23:15 +03:00
|
|
|
.Bl -tag -width F_ULOCKXX -compact -offset indent
|
|
|
|
.It Sy Function
|
|
|
|
.Sy Description
|
|
|
|
.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
|
|
|
|
.El
|
|
|
|
.Pp
|
|
|
|
.Dv F_ULOCK
|
|
|
|
removes locks from a section of the file;
|
|
|
|
.Dv F_LOCK
|
|
|
|
and
|
|
|
|
.Dv F_TLOCK
|
|
|
|
both lock a section of a file if the section is available;
|
|
|
|
.Dv F_TEST
|
|
|
|
detects if a lock by another process is present on the specified section.
|
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Fa size
|
|
|
|
argument is the number of contiguous bytes to be locked or
|
2002-10-01 20:48:34 +04:00
|
|
|
unlocked.
|
|
|
|
The section to be locked or unlocked starts at the current
|
1997-12-20 23:23:15 +03:00
|
|
|
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
|
2002-10-01 20:48:34 +04:00
|
|
|
current offset).
|
|
|
|
However, it is not permitted to lock a section that
|
1997-12-20 23:23:15 +03:00
|
|
|
starts or extends before the beginning of the file.
|
|
|
|
If
|
|
|
|
.Fa size
|
|
|
|
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).
|
|
|
|
.Pp
|
|
|
|
The sections locked with
|
|
|
|
.Dv F_LOCK
|
|
|
|
or
|
|
|
|
.Dv F_TLOCK
|
|
|
|
may, in whole or in part, contain or be contained by a previously
|
2002-10-01 20:48:34 +04:00
|
|
|
locked section for the same process.
|
|
|
|
When this occurs, or if adjacent
|
1997-12-20 23:23:15 +03:00
|
|
|
locked sections would occur, the sections are combined into a single
|
2002-10-01 20:48:34 +04:00
|
|
|
locked section.
|
|
|
|
If the request would cause the number of locks to
|
1997-12-20 23:23:15 +03:00
|
|
|
exceed a system-imposed limit, the request will fail.
|
|
|
|
.Pp
|
|
|
|
.Dv F_LOCK
|
|
|
|
and
|
|
|
|
.Dv F_TLOCK
|
|
|
|
requests differ only by the action taken if the section is not
|
|
|
|
available.
|
|
|
|
.Dv F_LOCK
|
|
|
|
blocks the calling process until the section is available.
|
|
|
|
.Dv F_TLOCK
|
|
|
|
makes the function fail if the section is already locked by another
|
|
|
|
process.
|
|
|
|
.Pp
|
|
|
|
File locks are released on first close by the locking process of any
|
|
|
|
file descriptor for the file.
|
|
|
|
.Pp
|
|
|
|
.Dv F_ULOCK
|
|
|
|
requests release (wholly or in part) one or more locked sections
|
2002-10-01 20:48:34 +04:00
|
|
|
controlled by the process.
|
|
|
|
Locked sections will be unlocked starting
|
1997-12-20 23:23:15 +03:00
|
|
|
at the current file offset through
|
|
|
|
.Fa size
|
2002-10-01 20:48:34 +04:00
|
|
|
bytes or to the end of file if size is 0.
|
|
|
|
When all of a locked section
|
1997-12-20 23:23:15 +03:00
|
|
|
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
|
2002-10-01 20:48:34 +04:00
|
|
|
that section are still locked by the process.
|
|
|
|
Releasing the center
|
1997-12-20 23:23:15 +03:00
|
|
|
portion of a locked section will cause the remaining locked beginning
|
2002-10-01 20:48:34 +04:00
|
|
|
and end portions to become two separate locked sections.
|
|
|
|
If the
|
1997-12-20 23:23:15 +03:00
|
|
|
request would cause the number of locks in the system to exceed a
|
|
|
|
system-imposed limit, the request will fail.
|
|
|
|
.Pp
|
|
|
|
An
|
|
|
|
.Dv 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, will be treated
|
|
|
|
as a request to unlock from the start of the requested section with a
|
2002-10-01 20:48:34 +04:00
|
|
|
size equal to 0.
|
|
|
|
Otherwise an
|
1997-12-20 23:23:15 +03:00
|
|
|
.Dv F_ULOCK
|
|
|
|
request will attempt to unlock only the requested section.
|
|
|
|
.Pp
|
|
|
|
A potential for deadlock occurs if a process controlling a locked
|
|
|
|
region is put to sleep by attempting to lock the locked region of
|
2002-10-01 20:48:34 +04:00
|
|
|
another process.
|
|
|
|
This implementation detects that sleeping until a
|
1997-12-20 23:23:15 +03:00
|
|
|
locked region is unlocked would cause a deadlock and fails with an
|
|
|
|
.Er EDEADLK
|
|
|
|
error.
|
|
|
|
.Pp
|
|
|
|
.Fn lockf ,
|
|
|
|
.Xr fcntl 2
|
|
|
|
and
|
|
|
|
.Xr flock 2
|
|
|
|
locks may be safely used concurrently.
|
|
|
|
.Pp
|
|
|
|
Blocking on a section is interrupted by any signal.
|
|
|
|
.Sh RETURN VALUES
|
|
|
|
If successful, the
|
|
|
|
.Fn lockf
|
|
|
|
function returns 0.
|
2002-10-01 20:59:46 +04:00
|
|
|
Otherwise, it returns \-1, sets
|
1999-02-16 22:36:52 +03:00
|
|
|
.Va errno
|
1997-12-20 23:23:15 +03:00
|
|
|
to indicate an error, and existing locks are not changed.
|
|
|
|
.Sh ERRORS
|
|
|
|
.Fn lockf
|
|
|
|
will fail if:
|
|
|
|
.Bl -tag -width Er
|
|
|
|
.It Bq Er EAGAIN
|
|
|
|
The argument
|
|
|
|
.Fa function
|
|
|
|
is
|
|
|
|
.Dv F_TLOCK
|
|
|
|
or
|
|
|
|
.Dv F_TEST
|
|
|
|
and the section is already locked by another process.
|
|
|
|
.It Bq Er EBADF
|
|
|
|
The argument
|
|
|
|
.Fa filedes
|
|
|
|
is not a valid open file descriptor.
|
|
|
|
.Pp
|
|
|
|
The argument
|
|
|
|
.Fa function
|
|
|
|
is
|
|
|
|
.Dv F_LOCK
|
|
|
|
or
|
|
|
|
.Dv F_TLOCK ,
|
|
|
|
and
|
|
|
|
.Fa filedes
|
|
|
|
is not a valid file descriptor open for writing.
|
|
|
|
.It Bq Er EDEADLK
|
|
|
|
The argument
|
|
|
|
.Fa function
|
|
|
|
is
|
|
|
|
.Dv F_LOCK
|
|
|
|
and a deadlock is detected.
|
|
|
|
.It Bq Er EINTR
|
|
|
|
The argument
|
|
|
|
.Fa function
|
|
|
|
is F_LOCK
|
|
|
|
and
|
|
|
|
.Fn lockf
|
|
|
|
was interrupted by the delivery of a signal.
|
|
|
|
.It Bq Er EINVAL
|
|
|
|
The argument
|
|
|
|
.Fa function
|
|
|
|
is not one of
|
|
|
|
.Dv F_ULOCK ,
|
|
|
|
.Dv F_LOCK ,
|
|
|
|
.Dv F_TLOCK
|
|
|
|
or
|
|
|
|
.Dv F_TEST .
|
|
|
|
.Pp
|
|
|
|
The argument
|
|
|
|
.Fa filedes
|
|
|
|
refers to a file that does not support locking.
|
|
|
|
.It Bq Er ENOLCK
|
|
|
|
The argument
|
|
|
|
.Fa function
|
|
|
|
is
|
|
|
|
.Dv F_ULOCK ,
|
|
|
|
.Dv F_LOCK
|
|
|
|
or
|
|
|
|
.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.
|
1999-03-09 22:45:09 +03:00
|
|
|
.El
|
1997-12-20 23:23:15 +03:00
|
|
|
.Sh SEE ALSO
|
|
|
|
.Xr fcntl 2 ,
|
2011-10-16 01:35:49 +04:00
|
|
|
.Xr flock 2 ,
|
|
|
|
.Xr flockfile 3
|
1997-12-20 23:23:15 +03:00
|
|
|
.Sh STANDARDS
|
|
|
|
The
|
|
|
|
.Fn lockf
|
|
|
|
function conforms to
|
2011-10-16 01:35:49 +04:00
|
|
|
.St -xpg4.2
|
|
|
|
and
|
|
|
|
.St -p1003.1-2008 .
|
|
|
|
.Sh HISTORY
|
|
|
|
The
|
|
|
|
.Fn lockf
|
|
|
|
function first appeared in
|
|
|
|
.Fx 1.4 .
|