Blame man-pages-posix-2013-a/man3p/fcntl.3p

Packit 7cfc04
'\" et
Packit 7cfc04
.TH FCNTL "3P" 2013 "IEEE/The Open Group" "POSIX Programmer's Manual"
Packit 7cfc04
.SH PROLOG
Packit 7cfc04
This manual page is part of the POSIX Programmer's Manual.
Packit 7cfc04
The Linux implementation of this interface may differ (consult
Packit 7cfc04
the corresponding Linux manual page for details of Linux behavior),
Packit 7cfc04
or the interface may not be implemented on Linux.
Packit 7cfc04
Packit 7cfc04
.SH NAME
Packit 7cfc04
fcntl
Packit 7cfc04
\(em file control
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.LP
Packit 7cfc04
.nf
Packit 7cfc04
#include <fcntl.h>
Packit 7cfc04
.P
Packit 7cfc04
int fcntl(int \fIfildes\fP, int \fIcmd\fP, ...);
Packit 7cfc04
.fi
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
The
Packit 7cfc04
\fIfcntl\fR()
Packit 7cfc04
function shall perform the operations described below on open files. The
Packit 7cfc04
.IR fildes
Packit 7cfc04
argument is a file descriptor.
Packit 7cfc04
.P
Packit 7cfc04
The available values for
Packit 7cfc04
.IR cmd
Packit 7cfc04
are defined in
Packit 7cfc04
.IR <fcntl.h> 
Packit 7cfc04
and are as follows:
Packit 7cfc04
.IP F_DUPFD 14
Packit 7cfc04
Return a new file descriptor which shall be the lowest numbered
Packit 7cfc04
available (that is, not already open) file descriptor greater than or
Packit 7cfc04
equal to the third argument,
Packit 7cfc04
.IR arg ,
Packit 7cfc04
taken as an integer of type
Packit 7cfc04
.BR int .
Packit 7cfc04
The new file descriptor shall refer to the same open file description as
Packit 7cfc04
the original file descriptor, and shall share any locks. The FD_CLOEXEC
Packit 7cfc04
flag associated with the new file descriptor shall be cleared to keep
Packit 7cfc04
the file open across calls to one of the
Packit 7cfc04
.IR exec
Packit 7cfc04
functions.
Packit 7cfc04
.IP F_DUPFD_CLOEXEC 14
Packit 7cfc04
.br
Packit 7cfc04
Like F_DUPFD, but the FD_CLOEXEC flag associated with the new file
Packit 7cfc04
descriptor shall be set.
Packit 7cfc04
.IP F_GETFD 14
Packit 7cfc04
Get the file descriptor flags defined in
Packit 7cfc04
.IR <fcntl.h> 
Packit 7cfc04
that are associated with the file descriptor
Packit 7cfc04
.IR fildes .
Packit 7cfc04
File descriptor flags are associated with a single file descriptor and
Packit 7cfc04
do not affect other file descriptors that refer to the same file.
Packit 7cfc04
.IP F_SETFD 14
Packit 7cfc04
Set the file descriptor flags defined in
Packit 7cfc04
.IR <fcntl.h> ,
Packit 7cfc04
that are associated with
Packit 7cfc04
.IR fildes ,
Packit 7cfc04
to the third argument,
Packit 7cfc04
.IR arg ,
Packit 7cfc04
taken as type
Packit 7cfc04
.BR int .
Packit 7cfc04
If the FD_CLOEXEC flag in the third argument
Packit 7cfc04
is 0, the file descriptor shall remain open across the
Packit 7cfc04
.IR exec
Packit 7cfc04
functions; otherwise, the file descriptor shall be closed upon
Packit 7cfc04
successful execution of one of the
Packit 7cfc04
.IR exec
Packit 7cfc04
functions.
Packit 7cfc04
.IP F_GETFL 14
Packit 7cfc04
Get the file status flags and file access modes, defined in
Packit 7cfc04
.IR <fcntl.h> ,
Packit 7cfc04
for the file description associated with
Packit 7cfc04
.IR fildes .
Packit 7cfc04
The file access modes can be extracted from the return value using the
Packit 7cfc04
mask O_ACCMODE, which is defined in
Packit 7cfc04
.IR <fcntl.h> .
Packit 7cfc04
File status flags and file access modes are associated with the file
Packit 7cfc04
description and do not affect other file descriptors that refer to the
Packit 7cfc04
same file with different open file descriptions. The flags returned may
Packit 7cfc04
include non-standard file status flags which the application did not
Packit 7cfc04
set, provided that these additional flags do not alter the behavior of
Packit 7cfc04
a conforming application.
Packit 7cfc04
.IP F_SETFL 14
Packit 7cfc04
Set the file status flags, defined in
Packit 7cfc04
.IR <fcntl.h> ,
Packit 7cfc04
for the file description associated with
Packit 7cfc04
.IR fildes
Packit 7cfc04
from the corresponding bits in the third argument,
Packit 7cfc04
.IR arg ,
Packit 7cfc04
taken as type
Packit 7cfc04
.BR int .
Packit 7cfc04
Bits corresponding to the file access mode and the file creation
Packit 7cfc04
flags, as defined in
Packit 7cfc04
.IR <fcntl.h> ,
Packit 7cfc04
that are set in
Packit 7cfc04
.IR arg
Packit 7cfc04
shall be ignored. If any bits in
Packit 7cfc04
.IR arg
Packit 7cfc04
other than those mentioned here are changed by the application, the
Packit 7cfc04
result is unspecified. If
Packit 7cfc04
.IR fildes
Packit 7cfc04
does not support non-blocking operations, it is unspecified whether the
Packit 7cfc04
O_NONBLOCK flag will be ignored.
Packit 7cfc04
.IP F_GETOWN 14
Packit 7cfc04
If
Packit 7cfc04
.IR fildes
Packit 7cfc04
refers to a socket, get the process or process group ID specified to
Packit 7cfc04
receive SIGURG signals when out-of-band data is available. Positive
Packit 7cfc04
values indicate a process ID; negative values, other than \(mi1,
Packit 7cfc04
indicate a process group ID. If
Packit 7cfc04
.IR fildes
Packit 7cfc04
does not refer to a socket, the results are unspecified.
Packit 7cfc04
.IP F_SETOWN 14
Packit 7cfc04
If
Packit 7cfc04
.IR fildes
Packit 7cfc04
refers to a socket, set the process or process group ID specified to
Packit 7cfc04
receive SIGURG signals when out-of-band data is available, using the
Packit 7cfc04
value of the third argument,
Packit 7cfc04
.IR arg ,
Packit 7cfc04
taken as type
Packit 7cfc04
.BR int .
Packit 7cfc04
Positive values indicate a process ID; negative values, other than
Packit 7cfc04
\(mi1, indicate a process group ID. If
Packit 7cfc04
.IR fildes
Packit 7cfc04
does not refer to a socket, the results are unspecified.
Packit 7cfc04
.P
Packit 7cfc04
The following values for
Packit 7cfc04
.IR cmd
Packit 7cfc04
are available for advisory record locking. Record locking shall be
Packit 7cfc04
supported for regular files, and may be supported for other files.
Packit 7cfc04
.IP F_GETLK 14
Packit 7cfc04
Get the first lock which blocks the lock description pointed to by the
Packit 7cfc04
third argument,
Packit 7cfc04
.IR arg ,
Packit 7cfc04
taken as a pointer to type
Packit 7cfc04
.BR "struct flock" ,
Packit 7cfc04
defined in
Packit 7cfc04
.IR <fcntl.h> .
Packit 7cfc04
The information retrieved shall overwrite the information passed to
Packit 7cfc04
\fIfcntl\fR()
Packit 7cfc04
in the structure
Packit 7cfc04
.BR flock .
Packit 7cfc04
If no lock is found that would prevent this lock from being created,
Packit 7cfc04
then the structure shall be left unchanged except for the lock type
Packit 7cfc04
which shall be set to F_UNLCK.
Packit 7cfc04
.IP F_SETLK 14
Packit 7cfc04
Set or clear a file segment lock according to the lock description
Packit 7cfc04
pointed to by the third argument,
Packit 7cfc04
.IR arg ,
Packit 7cfc04
taken as a pointer to type
Packit 7cfc04
.BR "struct flock" ,
Packit 7cfc04
defined in
Packit 7cfc04
.IR <fcntl.h> .
Packit 7cfc04
F_SETLK can establish shared (or read) locks (F_RDLCK) or
Packit 7cfc04
exclusive (or write) locks (F_WRLCK), as well as to remove either type
Packit 7cfc04
of lock (F_UNLCK). F_RDLCK, F_WRLCK, and F_UNLCK are defined in
Packit 7cfc04
.IR <fcntl.h> .
Packit 7cfc04
If a shared or exclusive lock cannot be set,
Packit 7cfc04
\fIfcntl\fR()
Packit 7cfc04
shall return immediately with a return value of \(mi1.
Packit 7cfc04
.IP F_SETLKW 14
Packit 7cfc04
This command shall be equivalent to F_SETLK except that if a shared or
Packit 7cfc04
exclusive lock is blocked by other locks, the thread shall wait until
Packit 7cfc04
the request can be satisfied. If a signal that is to be caught is
Packit 7cfc04
received while
Packit 7cfc04
\fIfcntl\fR()
Packit 7cfc04
is waiting for a region,
Packit 7cfc04
\fIfcntl\fR()
Packit 7cfc04
shall be interrupted. Upon return from the signal handler,
Packit 7cfc04
\fIfcntl\fR()
Packit 7cfc04
shall return \(mi1 with
Packit 7cfc04
.IR errno
Packit 7cfc04
set to
Packit 7cfc04
.BR [EINTR] ,
Packit 7cfc04
and the lock operation shall not be done.
Packit 7cfc04
.P
Packit 7cfc04
Additional implementation-defined values for
Packit 7cfc04
.IR cmd
Packit 7cfc04
may be defined in
Packit 7cfc04
.IR <fcntl.h> .
Packit 7cfc04
Their names shall start with F_.
Packit 7cfc04
.P
Packit 7cfc04
When a shared lock is set on a segment of a file, other processes shall
Packit 7cfc04
be able to set shared locks on that segment or a portion of it. A
Packit 7cfc04
shared lock prevents any other process from setting an exclusive lock
Packit 7cfc04
on any portion of the protected area. A request for a shared lock
Packit 7cfc04
shall fail if the file descriptor was not opened with read access.
Packit 7cfc04
.P
Packit 7cfc04
An exclusive lock shall prevent any other process from setting a shared
Packit 7cfc04
lock or an exclusive lock on any portion of the protected area. A
Packit 7cfc04
request for an exclusive lock shall fail if the file descriptor was not
Packit 7cfc04
opened with write access.
Packit 7cfc04
.P
Packit 7cfc04
The structure
Packit 7cfc04
.BR flock
Packit 7cfc04
describes the type (\c
Packit 7cfc04
.IR l_type ),
Packit 7cfc04
starting offset (\c
Packit 7cfc04
.IR l_whence ),
Packit 7cfc04
relative offset (\c
Packit 7cfc04
.IR l_start ),
Packit 7cfc04
size (\c
Packit 7cfc04
.IR l_len ),
Packit 7cfc04
and process ID (\c
Packit 7cfc04
.IR l_pid )
Packit 7cfc04
of the segment of the file to be affected.
Packit 7cfc04
.P
Packit 7cfc04
The value of
Packit 7cfc04
.IR l_whence
Packit 7cfc04
is SEEK_SET, SEEK_CUR, or SEEK_END,
Packit 7cfc04
to indicate that the relative offset
Packit 7cfc04
.IR l_start
Packit 7cfc04
bytes shall be measured from the start of the file, current position,
Packit 7cfc04
or end of the file, respectively. The value of
Packit 7cfc04
.IR l_len
Packit 7cfc04
is the number of consecutive bytes to be locked. The value of
Packit 7cfc04
.IR l_len
Packit 7cfc04
may be negative (where the definition of
Packit 7cfc04
.BR off_t
Packit 7cfc04
permits negative values of
Packit 7cfc04
.IR l_len ).
Packit 7cfc04
The
Packit 7cfc04
.IR l_pid
Packit 7cfc04
field is only used with F_GETLK to return the process ID of the process
Packit 7cfc04
holding a blocking lock. After a successful F_GETLK request, when a
Packit 7cfc04
blocking lock is found, the values returned in the
Packit 7cfc04
.BR flock
Packit 7cfc04
structure shall be as follows:
Packit 7cfc04
.IP "\fIl_type\fP" 10
Packit 7cfc04
Type of blocking lock found.
Packit 7cfc04
.IP "\fIl_whence\fP" 10
Packit 7cfc04
SEEK_SET.
Packit 7cfc04
.IP "\fIl_start\fP" 10
Packit 7cfc04
Start of the blocking lock.
Packit 7cfc04
.IP "\fIl_len\fP" 10
Packit 7cfc04
Length of the blocking lock.
Packit 7cfc04
.IP "\fIl_pid\fP" 10
Packit 7cfc04
Process ID of the process that holds the blocking lock.
Packit 7cfc04
.P
Packit 7cfc04
If the command is F_SETLKW and the process must wait for another
Packit 7cfc04
process to release a lock, then the range of bytes to be locked shall
Packit 7cfc04
be determined before the
Packit 7cfc04
\fIfcntl\fR()
Packit 7cfc04
function blocks. If the file size or file descriptor seek offset change
Packit 7cfc04
while
Packit 7cfc04
\fIfcntl\fR()
Packit 7cfc04
is blocked, this shall not affect the range of bytes locked.
Packit 7cfc04
.P
Packit 7cfc04
If
Packit 7cfc04
.IR l_len
Packit 7cfc04
is positive, the area affected shall start at
Packit 7cfc04
.IR l_start
Packit 7cfc04
and end at
Packit 7cfc04
.IR l_start +\c
Packit 7cfc04
.IR l_len \(mi1.
Packit 7cfc04
If
Packit 7cfc04
.IR l_len
Packit 7cfc04
is negative, the area affected shall start at
Packit 7cfc04
.IR l_start +\c
Packit 7cfc04
.IR l_len
Packit 7cfc04
and end at
Packit 7cfc04
.IR l_start \(mi1.
Packit 7cfc04
Locks may start and extend beyond the current end of a file, but shall
Packit 7cfc04
not extend before the beginning of the file. A lock shall be set to
Packit 7cfc04
extend to the largest possible value of the file offset for that file
Packit 7cfc04
by setting
Packit 7cfc04
.IR l_len
Packit 7cfc04
to 0. If such a lock also has
Packit 7cfc04
.IR l_start
Packit 7cfc04
set to 0 and
Packit 7cfc04
.IR l_whence
Packit 7cfc04
is set to SEEK_SET, the whole file shall be locked.
Packit 7cfc04
.P
Packit 7cfc04
There shall be at most one type of lock set for each byte in the file.
Packit 7cfc04
Before a successful return from an F_SETLK or an F_SETLKW request when
Packit 7cfc04
the calling process has previously existing locks on bytes in the
Packit 7cfc04
region specified by the request, the previous lock type for each byte
Packit 7cfc04
in the specified region shall be replaced by the new lock type. As
Packit 7cfc04
specified above under the descriptions of shared locks and exclusive
Packit 7cfc04
locks, an F_SETLK or an F_SETLKW request (respectively) shall fail or
Packit 7cfc04
block when another process has existing locks on bytes in the specified
Packit 7cfc04
region and the type of any of those locks conflicts with the type
Packit 7cfc04
specified in the request.
Packit 7cfc04
.P
Packit 7cfc04
All locks associated with a file for a given process shall be removed
Packit 7cfc04
when a file descriptor for that file is closed by that process or the
Packit 7cfc04
process holding that file descriptor terminates. Locks are not
Packit 7cfc04
inherited by a child process.
Packit 7cfc04
.P
Packit 7cfc04
A potential for deadlock occurs if a process controlling a locked
Packit 7cfc04
region is put to sleep by attempting to lock the locked region of
Packit 7cfc04
another process. If the system detects that sleeping until a locked
Packit 7cfc04
region is unlocked would cause a deadlock,
Packit 7cfc04
\fIfcntl\fR()
Packit 7cfc04
shall fail with an
Packit 7cfc04
.BR [EDEADLK] 
Packit 7cfc04
error.
Packit 7cfc04
.P
Packit 7cfc04
An unlock (F_UNLCK) request in which
Packit 7cfc04
.IR l_len
Packit 7cfc04
is non-zero and the offset of the last byte of the requested segment is
Packit 7cfc04
the maximum value for an object of type
Packit 7cfc04
.BR off_t ,
Packit 7cfc04
when the process has an existing lock in which
Packit 7cfc04
.IR l_len
Packit 7cfc04
is 0 and which includes the last byte of the requested segment, shall be
Packit 7cfc04
treated as a request to unlock from the start of the requested segment
Packit 7cfc04
with an
Packit 7cfc04
.IR l_len
Packit 7cfc04
equal to 0. Otherwise, an unlock (F_UNLCK) request shall attempt to
Packit 7cfc04
unlock only the requested segment.
Packit 7cfc04
.P
Packit 7cfc04
When the file descriptor
Packit 7cfc04
.IR fildes
Packit 7cfc04
refers to a shared memory object, the behavior of
Packit 7cfc04
\fIfcntl\fR()
Packit 7cfc04
shall be the same as for a regular file except the effect of the
Packit 7cfc04
following values for the argument
Packit 7cfc04
.IR cmd
Packit 7cfc04
shall be unspecified: F_SETFL, F_GETLK, F_SETLK, and F_SETLKW.
Packit 7cfc04
.P
Packit 7cfc04
If
Packit 7cfc04
.IR fildes
Packit 7cfc04
refers to a typed memory object, the result of the
Packit 7cfc04
\fIfcntl\fR()
Packit 7cfc04
function is unspecified.
Packit 7cfc04
.SH "RETURN VALUE"
Packit 7cfc04
Upon successful completion, the value returned shall depend on
Packit 7cfc04
.IR cmd
Packit 7cfc04
as follows:
Packit 7cfc04
.IP F_DUPFD 12
Packit 7cfc04
A new file descriptor.
Packit 7cfc04
.IP F_DUPFD_CLOEXEC 12
Packit 7cfc04
.br
Packit 7cfc04
A new file descriptor.
Packit 7cfc04
.IP F_GETFD 12
Packit 7cfc04
Value of flags defined in
Packit 7cfc04
.IR <fcntl.h> .
Packit 7cfc04
The return value shall not be negative.
Packit 7cfc04
.IP F_SETFD 12
Packit 7cfc04
Value other than \(mi1.
Packit 7cfc04
.IP F_GETFL 12
Packit 7cfc04
Value of file status flags and access modes. The return value is not
Packit 7cfc04
negative.
Packit 7cfc04
.IP F_SETFL 12
Packit 7cfc04
Value other than \(mi1.
Packit 7cfc04
.IP F_GETLK 12
Packit 7cfc04
Value other than \(mi1.
Packit 7cfc04
.IP F_SETLK 12
Packit 7cfc04
Value other than \(mi1.
Packit 7cfc04
.IP F_SETLKW 12
Packit 7cfc04
Value other than \(mi1.
Packit 7cfc04
.IP F_GETOWN 12
Packit 7cfc04
Value of the socket owner process or process group; this will not be
Packit 7cfc04
\(mi1.
Packit 7cfc04
.IP F_SETOWN 12
Packit 7cfc04
Value other than \(mi1.
Packit 7cfc04
.P
Packit 7cfc04
Otherwise, \(mi1 shall be returned and
Packit 7cfc04
.IR errno
Packit 7cfc04
set to indicate the error.
Packit 7cfc04
.SH ERRORS
Packit 7cfc04
The
Packit 7cfc04
\fIfcntl\fR()
Packit 7cfc04
function shall fail if:
Packit 7cfc04
.TP
Packit 7cfc04
.BR EACCES " or " EAGAIN
Packit 7cfc04
.br
Packit 7cfc04
The
Packit 7cfc04
.IR cmd
Packit 7cfc04
argument is F_SETLK; the type of lock (\c
Packit 7cfc04
.IR l_type )
Packit 7cfc04
is a shared (F_RDLCK) or exclusive (F_WRLCK) lock and the segment of a
Packit 7cfc04
file to be locked is already exclusive-locked by another process, or the
Packit 7cfc04
type is an exclusive lock and some portion of the segment of a file to
Packit 7cfc04
be locked is already shared-locked or exclusive-locked by another process.
Packit 7cfc04
.TP
Packit 7cfc04
.BR EBADF
Packit 7cfc04
The
Packit 7cfc04
.IR fildes
Packit 7cfc04
argument is not a valid open file descriptor, or the argument
Packit 7cfc04
.IR cmd
Packit 7cfc04
is F_SETLK or F_SETLKW, the type of lock,
Packit 7cfc04
.IR l_type ,
Packit 7cfc04
is a shared lock (F_RDLCK), and
Packit 7cfc04
.IR fildes
Packit 7cfc04
is not a valid file descriptor open for reading, or the type of lock,
Packit 7cfc04
.IR l_type ,
Packit 7cfc04
is an exclusive lock (F_WRLCK), and
Packit 7cfc04
.IR fildes
Packit 7cfc04
is not a valid file descriptor open for writing.
Packit 7cfc04
.TP
Packit 7cfc04
.BR EINTR
Packit 7cfc04
The
Packit 7cfc04
.IR cmd
Packit 7cfc04
argument is F_SETLKW and the function was interrupted by a signal.
Packit 7cfc04
.TP
Packit 7cfc04
.BR EINVAL
Packit 7cfc04
The
Packit 7cfc04
.IR cmd
Packit 7cfc04
argument is invalid, or the
Packit 7cfc04
.IR cmd
Packit 7cfc04
argument is F_DUPFD or F_DUPFD_CLOEXEC and
Packit 7cfc04
.IR arg
Packit 7cfc04
is negative or greater than or equal to
Packit 7cfc04
{OPEN_MAX},
Packit 7cfc04
or the
Packit 7cfc04
.IR cmd
Packit 7cfc04
argument is F_GETLK, F_SETLK, or F_SETLKW and the data pointed to by
Packit 7cfc04
.IR arg
Packit 7cfc04
is not valid, or
Packit 7cfc04
.IR fildes
Packit 7cfc04
refers to a file that does not support locking.
Packit 7cfc04
.TP
Packit 7cfc04
.BR EMFILE
Packit 7cfc04
The argument
Packit 7cfc04
.IR cmd
Packit 7cfc04
is F_DUPFD or F_DUPFD_CLOEXEC and all file descriptors available to
Packit 7cfc04
the process are currently open, or no file descriptors greater than or
Packit 7cfc04
equal to
Packit 7cfc04
.IR arg
Packit 7cfc04
are available.
Packit 7cfc04
.TP
Packit 7cfc04
.BR ENOLCK
Packit 7cfc04
The argument
Packit 7cfc04
.IR cmd
Packit 7cfc04
is F_SETLK or F_SETLKW and satisfying the lock or unlock request would
Packit 7cfc04
result in the number of locked regions in the system exceeding a
Packit 7cfc04
system-imposed limit.
Packit 7cfc04
.TP
Packit 7cfc04
.BR EOVERFLOW
Packit 7cfc04
One of the values to be returned cannot be represented correctly.
Packit 7cfc04
.TP
Packit 7cfc04
.BR EOVERFLOW
Packit 7cfc04
The
Packit 7cfc04
.IR cmd
Packit 7cfc04
argument is F_GETLK, F_SETLK, or F_SETLKW and the smallest or, if
Packit 7cfc04
.IR l_len
Packit 7cfc04
is non-zero, the largest offset of any byte in the requested segment
Packit 7cfc04
cannot be represented correctly in an object of type
Packit 7cfc04
.BR off_t .
Packit 7cfc04
.br
Packit 7cfc04
.P
Packit 7cfc04
The
Packit 7cfc04
\fIfcntl\fR()
Packit 7cfc04
function may fail if:
Packit 7cfc04
.TP
Packit 7cfc04
.BR EDEADLK
Packit 7cfc04
The
Packit 7cfc04
.IR cmd
Packit 7cfc04
argument is F_SETLKW, the lock is blocked by a lock from another
Packit 7cfc04
process, and putting the calling process to sleep to wait for that lock
Packit 7cfc04
to become free would cause a deadlock.
Packit 7cfc04
.LP
Packit 7cfc04
.IR "The following sections are informative."
Packit 7cfc04
.SH EXAMPLES
Packit 7cfc04
.SS "Locking and Unlocking a File"
Packit 7cfc04
.P
Packit 7cfc04
The following example demonstrates how to place a lock on bytes 100 to
Packit 7cfc04
109 of a file and then later remove it. F_SETLK is used to perform a
Packit 7cfc04
non-blocking lock request so that the process does not have to wait if
Packit 7cfc04
an incompatible lock is held by another process; instead the process
Packit 7cfc04
can take some other action.
Packit 7cfc04
.sp
Packit 7cfc04
.RS 4
Packit 7cfc04
.nf
Packit 7cfc04
\fB
Packit 7cfc04
#include <stdlib.h>
Packit 7cfc04
#include <unistd.h>
Packit 7cfc04
#include <fcntl.h>
Packit 7cfc04
#include <errno.h>
Packit 7cfc04
#include <stdio.h>
Packit 7cfc04
.P
Packit 7cfc04
int
Packit 7cfc04
main(int argc, char *argv[])
Packit 7cfc04
{
Packit 7cfc04
    int fd;
Packit 7cfc04
    struct flock fl;
Packit 7cfc04
.P
Packit 7cfc04
    fd = open("testfile", O_RDWR);
Packit 7cfc04
    if (fd == -1)
Packit 7cfc04
        /* Handle error */;
Packit 7cfc04
.P
Packit 7cfc04
    /* Make a non-blocking request to place a write lock
Packit 7cfc04
       on bytes 100-109 of testfile */
Packit 7cfc04
.P
Packit 7cfc04
    fl.l_type = F_WRLCK;
Packit 7cfc04
    fl.l_whence = SEEK_SET;
Packit 7cfc04
    fl.l_start = 100;
Packit 7cfc04
    fl.l_len = 10;
Packit 7cfc04
.P
Packit 7cfc04
    if (fcntl(fd, F_SETLK, &fl) == \(mi1) {
Packit 7cfc04
        if (errno == EACCES || errno == EAGAIN) {
Packit 7cfc04
            printf("Already locked by another process\en");
Packit 7cfc04
.P
Packit 7cfc04
            /* We can't get the lock at the moment */
Packit 7cfc04
.P
Packit 7cfc04
        } else {
Packit 7cfc04
            /* Handle unexpected error */;
Packit 7cfc04
        }
Packit 7cfc04
    } else { /* Lock was granted... */
Packit 7cfc04
.P
Packit 7cfc04
        /* Perform I/O on bytes 100 to 109 of file */
Packit 7cfc04
.P
Packit 7cfc04
        /* Unlock the locked bytes */
Packit 7cfc04
.P
Packit 7cfc04
        fl.l_type = F_UNLCK;
Packit 7cfc04
        fl.l_whence = SEEK_SET;
Packit 7cfc04
        fl.l_start = 100;
Packit 7cfc04
        fl.l_len = 10;
Packit 7cfc04
        if (fcntl(fd, F_SETLK, &fl) == \(mi1)
Packit 7cfc04
            /* Handle error */;
Packit 7cfc04
    }
Packit 7cfc04
    exit(EXIT_SUCCESS);
Packit 7cfc04
} /* main */
Packit 7cfc04
.fi \fR
Packit 7cfc04
.P
Packit 7cfc04
.RE
Packit 7cfc04
.SS "Setting the Close-on-Exec Flag"
Packit 7cfc04
.P
Packit 7cfc04
The following example demonstrates how to set the close-on-exec flag
Packit 7cfc04
for the file descriptor
Packit 7cfc04
.IR fd .
Packit 7cfc04
.sp
Packit 7cfc04
.RS 4
Packit 7cfc04
.nf
Packit 7cfc04
\fB
Packit 7cfc04
#include <unistd.h>
Packit 7cfc04
#include <fcntl.h>
Packit 7cfc04
\&...
Packit 7cfc04
    int flags;
Packit 7cfc04
.P
Packit 7cfc04
    flags = fcntl(fd, F_GETFD);
Packit 7cfc04
    if (flags == \(mi1)
Packit 7cfc04
        /* Handle error */;
Packit 7cfc04
    flags |= FD_CLOEXEC;
Packit 7cfc04
    if (fcntl(fd, F_SETFD, flags) == \(mi1)
Packit 7cfc04
        /* Handle error */;"
Packit 7cfc04
.fi \fR
Packit 7cfc04
.P
Packit 7cfc04
.RE
Packit 7cfc04
.SH "APPLICATION USAGE"
Packit 7cfc04
The
Packit 7cfc04
.IR arg
Packit 7cfc04
values to F_GETFD, F_SETFD, F_GETFL, and F_SETFL all represent flag
Packit 7cfc04
values to allow for future growth. Applications using these functions
Packit 7cfc04
should do a read-modify-write operation on them, rather than assuming
Packit 7cfc04
that only the values defined by this volume of POSIX.1\(hy2008 are valid. It is a common error to
Packit 7cfc04
forget this, particularly in the case of F_SETFD. Some implementations
Packit 7cfc04
set additional file status flags to advise the application of default
Packit 7cfc04
behavior, even though the application did not request these flags.
Packit 7cfc04
.SH RATIONALE
Packit 7cfc04
The ellipsis in the SYNOPSIS is the syntax specified by the ISO\ C standard
Packit 7cfc04
for a variable number of arguments. It is used because System V uses
Packit 7cfc04
pointers for the implementation of file locking functions.
Packit 7cfc04
.P
Packit 7cfc04
This volume of POSIX.1\(hy2008 permits concurrent read and write access to file data using the
Packit 7cfc04
\fIfcntl\fR()
Packit 7cfc04
function; this is a change from the 1984 /usr/group standard and early proposals. Without
Packit 7cfc04
concurrency controls, this feature may not be fully utilized without
Packit 7cfc04
occasional loss of data.
Packit 7cfc04
.P
Packit 7cfc04
Data losses occur in several ways. One case occurs when several
Packit 7cfc04
processes try to update the same record, without sequencing controls;
Packit 7cfc04
several updates may occur in parallel and the last writer ``wins''.
Packit 7cfc04
Another case is a bit-tree or other internal list-based database that
Packit 7cfc04
is undergoing reorganization. Without exclusive use to the tree segment
Packit 7cfc04
by the updating process, other reading processes chance getting lost in
Packit 7cfc04
the database when the index blocks are split, condensed, inserted, or
Packit 7cfc04
deleted. While
Packit 7cfc04
\fIfcntl\fR()
Packit 7cfc04
is useful for many applications, it is not intended to be overly
Packit 7cfc04
general and does not handle the bit-tree example well.
Packit 7cfc04
.P
Packit 7cfc04
This facility is only required for regular files because it is not
Packit 7cfc04
appropriate for many devices such as terminals and network
Packit 7cfc04
connections.
Packit 7cfc04
.P
Packit 7cfc04
Since
Packit 7cfc04
\fIfcntl\fR()
Packit 7cfc04
works with ``any file descriptor associated with that file, however it
Packit 7cfc04
is obtained'', the file descriptor may have been inherited through a
Packit 7cfc04
\fIfork\fR()
Packit 7cfc04
or
Packit 7cfc04
.IR exec
Packit 7cfc04
operation and thus may affect a file that another process also has
Packit 7cfc04
open.
Packit 7cfc04
.P
Packit 7cfc04
The use of the open file description to identify what to lock requires
Packit 7cfc04
extra calls and presents problems if several processes are sharing an
Packit 7cfc04
open file description, but there are too many implementations of the
Packit 7cfc04
existing mechanism for this volume of POSIX.1\(hy2008 to use different specifications.
Packit 7cfc04
.P
Packit 7cfc04
Another consequence of this model is that closing any file descriptor
Packit 7cfc04
for a given file (whether or not it is the same open file description
Packit 7cfc04
that created the lock) causes the locks on that file to be relinquished
Packit 7cfc04
for that process. Equivalently, any close for any file/process pair
Packit 7cfc04
relinquishes the locks owned on that file for that process. But note
Packit 7cfc04
that while an open file description may be shared through
Packit 7cfc04
\fIfork\fR(),
Packit 7cfc04
locks are not inherited through
Packit 7cfc04
\fIfork\fR().
Packit 7cfc04
Yet locks may be inherited through one of the
Packit 7cfc04
.IR exec
Packit 7cfc04
functions.
Packit 7cfc04
.P
Packit 7cfc04
The identification of a machine in a network environment is outside
Packit 7cfc04
the scope of this volume of POSIX.1\(hy2008. Thus, an
Packit 7cfc04
.IR l_sysid
Packit 7cfc04
member, such as found in System V, is not included in the locking
Packit 7cfc04
structure.
Packit 7cfc04
.P
Packit 7cfc04
Changing of lock types can result in a previously locked region being
Packit 7cfc04
split into smaller regions.
Packit 7cfc04
.P
Packit 7cfc04
Mandatory locking was a major feature of the 1984 /usr/group standard.
Packit 7cfc04
.P
Packit 7cfc04
For advisory file record locking to be effective, all processes that
Packit 7cfc04
have access to a file must cooperate and use the advisory mechanism
Packit 7cfc04
before doing I/O on the file. Enforcement-mode record locking is
Packit 7cfc04
important when it cannot be assumed that all processes are cooperating.
Packit 7cfc04
For example, if one user uses an editor to update a file at the same
Packit 7cfc04
time that a second user executes another process that updates the same
Packit 7cfc04
file and if only one of the two processes is using advisory locking,
Packit 7cfc04
the processes are not cooperating. Enforcement-mode record locking
Packit 7cfc04
would protect against accidental collisions.
Packit 7cfc04
.P
Packit 7cfc04
Secondly, advisory record locking requires a process using locking to
Packit 7cfc04
bracket each I/O operation with lock (or test) and unlock operations.
Packit 7cfc04
With enforcement-mode file and record locking, a process can lock the
Packit 7cfc04
file once and unlock when all I/O operations have been completed.
Packit 7cfc04
Enforcement-mode record locking provides a base that can be enhanced;
Packit 7cfc04
for example, with sharable locks. That is, the mechanism could be
Packit 7cfc04
enhanced to allow a process to lock a file so other processes could
Packit 7cfc04
read it, but none of them could write it.
Packit 7cfc04
.P
Packit 7cfc04
Mandatory locks were omitted for several reasons:
Packit 7cfc04
.IP " 1." 4
Packit 7cfc04
Mandatory lock setting was done by multiplexing the set-group-ID
Packit 7cfc04
bit in most implementations; this was confusing, at best.
Packit 7cfc04
.IP " 2." 4
Packit 7cfc04
The relationship to file truncation as supported in 4.2 BSD
Packit 7cfc04
was not well specified.
Packit 7cfc04
.IP " 3." 4
Packit 7cfc04
Any publicly readable file could be locked by anyone. Many historical
Packit 7cfc04
implementations keep the password database in a publicly readable
Packit 7cfc04
file. A malicious user could thus prohibit logins. Another
Packit 7cfc04
possibility would be to hold open a long-distance telephone line.
Packit 7cfc04
.IP " 4." 4
Packit 7cfc04
Some demand-paged historical implementations offer memory mapped files,
Packit 7cfc04
and enforcement cannot be done on that type of file.
Packit 7cfc04
.P
Packit 7cfc04
Since sleeping on a region is interrupted with any signal,
Packit 7cfc04
\fIalarm\fR()
Packit 7cfc04
may be used to provide a timeout facility in applications requiring
Packit 7cfc04
it. This is useful in deadlock detection. Since implementation of
Packit 7cfc04
full deadlock detection is not always feasible, the
Packit 7cfc04
.BR [EDEADLK] 
Packit 7cfc04
error was made optional.
Packit 7cfc04
.SH "FUTURE DIRECTIONS"
Packit 7cfc04
None.
Packit 7cfc04
.SH "SEE ALSO"
Packit 7cfc04
.IR "\fIalarm\fR\^(\|)",
Packit 7cfc04
.IR "\fIclose\fR\^(\|)",
Packit 7cfc04
.IR "\fIexec\fR\^",
Packit 7cfc04
.IR "\fIopen\fR\^(\|)",
Packit 7cfc04
.IR "\fIsigaction\fR\^(\|)"
Packit 7cfc04
.P
Packit 7cfc04
The Base Definitions volume of POSIX.1\(hy2008,
Packit 7cfc04
.IR "\fB<fcntl.h>\fP",
Packit 7cfc04
.IR "\fB<signal.h>\fP"
Packit 7cfc04
.SH COPYRIGHT
Packit 7cfc04
Portions of this text are reprinted and reproduced in electronic form
Packit 7cfc04
from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
Packit 7cfc04
-- Portable Operating System Interface (POSIX), The Open Group Base
Packit 7cfc04
Specifications Issue 7, Copyright (C) 2013 by the Institute of
Packit 7cfc04
Electrical and Electronics Engineers, Inc and The Open Group.
Packit 7cfc04
(This is POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
Packit 7cfc04
event of any discrepancy between this version and the original IEEE and
Packit 7cfc04
The Open Group Standard, the original IEEE and The Open Group Standard
Packit 7cfc04
is the referee document. The original Standard can be obtained online at
Packit 7cfc04
http://www.unix.org/online.html .
Packit 7cfc04
Packit 7cfc04
Any typographical or formatting errors that appear
Packit 7cfc04
in this page are most likely
Packit 7cfc04
to have been introduced during the conversion of the source files to
Packit 7cfc04
man page format. To report such errors, see
Packit 7cfc04
https://www.kernel.org/doc/man-pages/reporting_bugs.html .