File: fcntl.2

package info (click to toggle)
manpages 1.29-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 4,796 kB
  • ctags: 5
  • sloc: sh: 79; makefile: 61
file content (349 lines) | stat: -rw-r--r-- 9,922 bytes parent folder | download
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
.\" Hey Emacs! This file is -*- nroff -*- source.
.\"
.\" This manpage is Copyright (C) 1992 Drew Eckhardt;
.\"                               1993 Michael Haardt, Ian Jackson;
.\"                               1998 Jamie Lokier.
.\"
.\" 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 and 981223.
.\" Modified Fri Dec 11 17:57:27 1998 by Jamie Lokier <jamie@imbolc.ucc.ie>
.\" Applied correction by Christian Ehrhardt - aeb, 990712
.\"
.TH FCNTL 2 "12 July 1999" 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 );
.BI "int fcntl(int " fd ", int " cmd ", struct flock * " lock );
.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
Find the lowest numbered availiable file descriptor
greater than or equal to
.I arg
and make it be a copy of
.IR fd .
This is different form
.BR dup2 (2)
which uses exactly the descriptor specified.
.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
not be closed on exec.
.sp
On success, the new descriptor is returned.
.TP
.B F_GETFD
Read the close-on-exec flag.  If the
.B FD_CLOEXEC
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 the
.B FD_CLOEXEC
bit of
.IR arg .
.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 ", " O_NONBLOCK " and " O_ASYNC
may be set; the other flags are unaffected.
.sp
The flags are shared between copies (made with
.BR dup "(2), " fork (2),
etc.) of the same file descriptor.
.sp
The flags and their semantics are described in
.BR open (2).
.P
.BR F_GETLK ", " F_SETLK " and " F_SETLKW
are used to manage discretionary file locks.
The third argument
.I lock
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
.B fcntl
is waiting, it is interrupted and (after the signal handler has returned)
returns immediately (with return value \-1 and
.I errno
set to
.BR EINTR ).
.P
.BR F_GETOWN ", " F_SETOWN ", " F_GETSIG " and " F_SETSIG
are used to manage I/O availability signals:
.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.
.RB ( F_SETSIG
can be used to specify a different signal instead of SIGIO).

.\" From glibc.info:
If you set the
.B O_ASYNC
status flag on a file descriptor (either by providing this flag with the
.IR open (2)
call, or by using the
.B F_SETFL
command of
.BR fcntl ),
a SIGIO signal is sent whenever input or output becomes possible
on that file descriptor.
.sp
The process or process group to receive the signal can be selected by
using the
.B F_SETOWN
command to the
.B 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
.BR select (2)
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.
.TP
.B F_GETSIG
Get the signal sent when input or output becomes possible.  A value of
zero means SIGIO is sent.  Any other value (including SIGIO) is the
signal sent instead, and in this case additional info is available to
the signal handler if installed with SA_SIGINFO.
.TP
.B F_SETSIG
Sets the signal sent when input or output becomes possible.  A value of
zero means to send the default SIGIO signal.  Any other value (including
SIGIO) is the signal to send instead, and in this case additional info
is available to the signal handler if installed with SA_SIGINFO.
.sp
By using F_SETSIG with a non-zero value, and setting SA_SIGINFO for the
signal handler (see
.BR sigaction (2)),
extra information about I/O events is passed to
the handler in a
.I siginfo_t
structure.
If the
.I si_code
field indicates the source is SI_SIGIO, the
.I si_fd
field gives the file descriptor associated with the event.  Otherwise,
there is no indication which file descriptors are pending, and you
should use the usual mechanisms
.RB ( select (2),
.BR poll (2),
.BR read (2)
with
.B O_NONBLOCK
set etc.) to determine which file descriptors are available for I/O.
.sp
By selecting a POSIX.1b real time signal (value >= SIGRTMIN), multiple
I/O events may be queued using the same signal numbers.  (Queuing is
dependent on available memory).  Extra information is available
if SA_SIGINFO is set for the signal handler, as above.
.PP
Using these mechanisms, a program can implement fully asynchronous I/O
without using
.BR select (2)
or
.BR poll (2)
most of the time.
.PP
The use of
.BR O_ASYNC ,
.BR F_GETOWN ,
.B F_SETOWN
is specific to BSD and Linux.
.B F_GETSIG
and
.B F_SETSIG
are Linux-specific.  POSIX has asynchronous I/O and the
.I aio_sigevent
structure to achieve similar things; these are also available
in Linux as part of the GNU C Library (Glibc).
.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
.B F_GETSIG
Value of signal sent when read or write becomes possible, or zero
for traditional SIGIO behaviour.
.TP
All other commands
Zero.
.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 EBADF
.I fd
is not an open file descriptor.
.TP
.B EDEADLK
It was detected that the specified
.B F_SETLKW
command would cause a deadlock.
.TP
.B EFAULT
.I lock
is outside your accessible address space.
.TP
.B EINTR
For
.BR F_SETLKW ,
the command was interrupted by a signal.
For
.BR F_GETLK " and " F_SETLK ,
the command was interrupted by a signal before the lock was checked or
acquired.  Most likely when locking a remote file (e.g. locking over
NFS), but can sometimes happen locally.
.TP
.B EINVAL
For
.BR F_DUPFD ,
.I arg
is negative or is greater than the maximum allowable value.  For
.BR F_SETSIG ,
.I arg
is not an allowable signal number.
.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, or a remote locking
protocol failed (e.g. locking over NFS).
.TP
.B EPERM
Attempted to clear the
.B O_APPEND
flag on a file that has the append-only attribute set.
.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; F_GETSIG and F_SETSIG are specific to Linux.  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
SVr4 documents additional EIO, ENOLINK and EOVERFLOW error conditions.
.SH "SEE ALSO"
.BR dup2 (2),
.BR flock (2),
.BR open (2),
.BR socket (2)