1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
.\" Hey Emacs! This file is -*- nroff -*- source.
.\"
.\" This manpage is Copyright (C) 1992 Drew Eckhardt;
.\" 1993 Michael Haardt, Ian Jackson.
.\"
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Permission is granted to copy and distribute modified versions of this
.\" manual under the conditions for verbatim copying, provided that the
.\" entire resulting derived work is distributed under the terms of a
.\" permission notice identical to this one
.\"
.\" Since the Linux kernel and libraries are constantly changing, this
.\" manual page may be incorrect or out-of-date. The author(s) assume no
.\" responsibility for errors or omissions, or for damages resulting from
.\" the use of the information contained herein. The author(s) may not
.\" have taken the same level of care in the production of this manual,
.\" which is licensed free of charge, as they might when working
.\" professionally.
.\"
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
.\"
.\" Modified Sat Jul 24 13:39:26 1993 by Rik Faith <faith@cs.unc.edu>
.\" Modified Tue Sep 26 21:47:21 1995 by Andries Brouwer <aeb@cwi.nl>
.\" and again on 960413 and 980804.
.\"
.TH FCNTL 2 "26 September 1995" Linux "Linux Programmer's Manual"
.SH NAME
fcntl \- manipulate file descriptor
.SH SYNOPSIS
.nf
.B #include <unistd.h>
.B #include <fcntl.h>
.sp
.BI "int fcntl(int " fd ", int " cmd );
.BI "int fcntl(int " fd ", int " cmd ", long " arg );
.fi
.SH DESCRIPTION
.B fcntl
performs one of various miscellaneous operations on
.IR fd .
The operation in question is determined by
.IR cmd :
.TP 0.9i
.B F_DUPFD
Makes
.I arg
be a copy of
.IR fd ,
closing
.I fd
first if necessary.
.sp
The same functionality can be more easily achieved by using
.BR dup2 (2).
.sp
The old and new descriptors may be used interchangeably. They share locks,
file position pointers and flags; for example, if the file position is
modified by using
.B lseek
on one of the descriptors, the position is also changed for the other.
.sp
The two descriptors do not share the close-on-exec flag, however.
The close-on-exec flag of the copy is off, meaning that it will
be closed on exec.
.sp
On success, the new descriptor is returned.
.TP
.B F_GETFD
Read the close-on-exec flag. If the low-order bit is 0, the file will
remain open across
.BR exec ,
otherwise it will be closed.
.TP
.B F_SETFD
Set the close-on-exec flag to the value specified by
.I arg
(only the least significant bit is used).
.TP
.B F_GETFL
Read the descriptor's flags (all flags (as set by
.BR open (2))
are returned).
.TP
.B F_SETFL
Set the descriptor's flags to the value specified by
.IR arg .
Only
.BR O_APPEND " and " O_NONBLOCK
may be set.
.sp
The flags are shared between copies (made with
.B dup
etc.) of the same file descriptor.
.sp
The flags and their semantics are described in
.BR open (2).
.TP
.BR F_GETLK ", " F_SETLK " and " F_SETLKW
Manage discretionary file locks.
The third argument
.I arg
is a pointer to a struct flock
(that may be overwritten by this call).
.TP
.B F_GETLK
Return the flock structure that prevents us from obtaining
the lock, or set the
.B l_type
field of the lock to
.B F_UNLCK
if there is no obstruction.
.TP
.B F_SETLK
The lock is set (when
.B l_type
is
.B F_RDLCK
or
.BR F_WRLCK )
or cleared (when it is
.BR F_UNLCK ).
If the lock is held by someone
else, this call returns -1 and sets
.I errno
to
.B EACCES
or
.BR EAGAIN .
.TP
.B F_SETLKW
Like
.BR F_SETLK ,
but instead of returning an error we wait for the lock to be released.
If a signal that is to be caught is received while
.IR fcntl ()
is waiting, it is interrupted and returns immediately
(with return value \-1 and
.I errno
set to EINTR).
.TP
.B F_GETOWN
Get the process ID or process group currently receiving SIGIO
and SIGURG signals for events on file descriptor
.IR fd .
Process groups are returned as negative values.
.TP
.B F_SETOWN
Set the process ID or process group that will receive SIGIO
and SIGURG signals for events on file descriptor
.IR fd .
Process groups are specified using negative values.
.\" From glibc.info:
If you set the O_ASYNC status flag on a file descriptor
(either by providing this flag with the
.I open
call, or by using the F_SETFL command of
.IR fcntl ),
a SIGIO signal is sent whenever input or output becomes possible
on that file descriptor. The process or process group to receive
the signal can be selected by using the F_SETOWN command to the
.I fcntl
function. If the file descriptor is a socket, this also selects
the recipient of SIGURG signals that are delivered when out-of-band
data arrives on that socket. (SIGURG is sent in any situation where
.I select
would report the socket as having an "exceptional condition".)
If the file descriptor corresponds to a terminal device, then SIGIO
signals are sent to the foreground process group of the terminal.
The use of O_ASYNC, F_GETOWN, F_SETOWN is BSD-specific.
POSIX has asynchronous I/O and the
.I aio_sigevent
structure to achieve similar things.
.SH "RETURN VALUE"
For a successful call, the return value depends on the operation:
.TP 0.9i
.B F_DUPFD
The new descriptor.
.TP
.B F_GETFD
Value of flag.
.TP
.B F_GETFL
Value of flags.
.TP
.B F_GETOWN
Value of descriptor owner.
.TP
.BR F_SETFD ,
.BR F_SETFL ,
.BR F_GETLK ,
.BR F_SETLK ,
.BR F_SETLKW
Some value different from \-1.
.PP
On error, \-1 is returned, and
.I errno
is set appropriately.
.SH ERRORS
.TP 0.9i
.B EACCES
Operation is prohibited by locks held by other processes.
.TP
.B EAGAIN
Operation is prohibited because the file has been memory-mapped by
another process.
.TP
.B EDEADLK
It was detected that the specified F_SETLKW command would cause a deadlock.
.TP
.B EBADF
.I fd
is not an open file descriptor.
.TP
.B EINTR
The F_SETLKW command was interrupted by a signal.
.TP
.B EINVAL
For
.BR F_DUPFD ,
.I arg
is negative or is greater than the maximum allowable value.
.TP
.B EMFILE
For
.BR F_DUPFD ,
the process already has the maximum number of file descriptors open.
.TP
.B ENOLCK
Too many segment locks open, lock table is full.
.SH NOTES
The errors returned by
.B dup2
are different from those returned by
.BR F_DUPFD .
.SH "CONFORMING TO"
SVr4, SVID, POSIX, X/OPEN, BSD 4.3. Only the operations F_DUPFD,
F_GETFD, F_SETFD, F_GETFL, F_SETFL, F_GETLK, F_SETLK and F_SETLKW
are specified in POSIX.1; F_GETOWN and F_SETOWN are BSDisms not
supported in SVr4. The flags legal for
F_GETFL/F_SETFL are those supported by
.BR open (2)
and vary between these systems; O_APPEND, O_NONBLOCK, O_RDONLY,
and O_RDWR are specified in POSIX.1. SVr4 supports several other
options and flags not documented here.
.PP
POSIX.1 documents an additional EINTR condition. SVr4 documents
additional EFAULT, EINTR, EIO, ENOLINK and EOVERFLOW error conditions.
.SH "SEE ALSO"
.BR dup2 (2),
.BR flock (2),
.BR open (2),
.BR socket (2)
|