summaryrefslogtreecommitdiff
path: root/minix/man/man2/fcntl.2
blob: 7d75901818b786f420650cc4d72a442d7399a811 (plain)
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
.TH FCNTL 2
.SH NAME
fcntl \- miscellaneous file descriptor control functions
.SH SYNOPSIS
.nf
.ft B
#include <fcntl.h>

int fcntl(int \fIfd\fP, int \fIcmd\fP, \fR[\fP\fIdata\fP\fR]\fP)
.ft P
.fi
.SH DESCRIPTION
.de SP
.if t .sp 0.4
.if n .sp
..
.B Fcntl()
performs several file descriptor related functions, like duplicating a file
descriptor, setting the "close on exec" attribute, etc.  The
.I fd
argument is the file descriptor to operate on,
.I cmd
is the command code of the operation to perform, and
.I data
is an optional argument to give or receive parameters.  The command
codes and other symbols and types are declared in <fcntl.h>.  The commands
are:
.SP
.BI "fcntl(" fd ", F_DUPFD, int " fd2 ")"
.RS
Returns a new file descriptor that is a duplicate of file descriptor
.IR fd .
It shares the same file pointer and the same file status flags, but has
separate file descriptor flags that are initially off.  The value of the
duplicate file descriptor is the first free file descriptor greater than
or equal to
.IR fd2 .
.RE
.SP
.BI "fcntl(" fd ", F_DUPFD_CLOEXEC, int " fd2 ")"
.RS
As
.BR F_DUPFD ,
but the "close on exec" flag is set on the returned file descriptor.
.RE
.SP
.BI "fcntl(" fd ", F_GETFD)"
.RS
Returns the file descriptor flags associated with file descriptor
.IR fd .
The flags are the "close on exec" flag
.B FD_CLOEXEC
that, when set, causes the file descriptor to be closed when the process
executes another program.  The Minix-vmd specific
.B FD_ASYNCHIO
flag marks a file descriptor for asynchronous I/O operation.
.RE
.SP
.BI "fcntl(" fd ", F_SETFD, int " flags ")"
.RS
Set the file descriptor flags of
.I fd
to
.IR flags .
.RE
.SP
.BI "fcntl(" fd ", F_GETFL)"
.RS
Return the file status flags and file access modes associated with the file
associated with file descriptor
.IR fd .
The file status flags are
.B O_NONBLOCK
(non blocking I/O) and
.B O_APPEND
(append mode).  The file access modes are
.B O_RDONLY
(read-only),
.B O_WRONLY
(write-only) and
.B O_RDWR
(read-write).  These flags are also used in the second argument of
.BR open (2).
.RE
.SP
.BI "fcntl(" fd ", F_SETFL, int " flags ")"
.RS
Set the file status flags of the file referenced by
.I fd
to
.IR flags .
Only
.B O_NONBLOCK
and
.B O_APPEND
may be changed.  Access mode flags are ignored.
.RE
.SP
The next four commands use a parameter of type
.B struct flock
that is defined in <fcntl.h> as:
.SP
.RS
.nf
.ta +4n +8n +12n
struct flock {
	short	l_type;	/* F_RDLCK, F_WRLCK, or F_UNLCK */
	short	l_whence;	/* SEEK_SET, SEEK_CUR, or SEEK_END */
	off_t	l_start;	/* byte offset to start of segment */
	off_t	l_len;	/* length of segment */
	pid_t	l_pid;	/* process id of the locks' owner */
};
.fi
.RE
.SP
This structure describes a segment of a file.
.B L_type
is the lock operation performed on the file segment:
.B F_RDLCK
to set a read lock,
.B F_WRLCK
to set a write lock, and
.B F_UNLCK
to remove a lock.  Several processes may have a read lock on a segment, but
only one process can have a write lock.
.B L_whence
tells if the
.B l_start
offset must be interpreted from the start of the file
.RB ( SEEK_SET ),
the current file position
.RB ( SEEK_CUR ),
or the end of the file
.RB ( SEEK_END ).
This is analogous to the third parameter of
.BR lseek (2).
These
.B SEEK_*
symbols are declared in <unistd.h>.
.B L_start
is the starting offset of the segment of the file.
.B L_end
is the length of the segment.  If zero then the segment extends until end of
file.
.B L_pid
is the process-id of the process currently holding a lock on the segment.
It is returned by
.BR F_GETLK .
.SP
.BI "fcntl(" fd ", F_GETLK, struct flock *" lkp ")"
.RS
Find out if some other process has a lock on a segment of the file
associated by file descriptor
.I fd
that overlaps with the segment described by the
.B flock
structure pointed to by
.IR lkp .
If the segment is not locked then
.B l_type
is set to
.BR F_UNLCK .
Otherwise an
.B flock
structure is returned through
.I lkp
that describes the lock held by the other process.
.B L_start
is set relative to the start of the file.
.RE
.SP
.BI "fcntl(" fd ", F_SETLK, struct flock *" lkp ")"
.RS
Register a lock on a segment of the file associated with file descriptor
.IR fd .
The file segment is described by the
.B struct flock
pointed to by
.IR lkp .
This call returns an error if any part of the segment is already locked.
.RE
.SP
.BI "fcntl(" fd ", F_SETLKW, struct flock *" lkp ")"
.RS
Register a lock on a segment of the file associated with file descriptor
.IR fd .
The file segment is described by the
.B struct flock
pointed to by
.IR lkp .
This call blocks waiting for the lock to be released if any part of the
segment is already locked.
.RE
.SP
.BI "fcntl(" fd ", F_FREESP, struct flock *" lkp ")"
.RS
This call frees a segment of disk space occupied by the
file associated with file descriptor
.IR fd .
The segment is described by the
.B struct flock
pointed to by
.IR lkp .
The file is truncated in length to the byte position indicated by
.B l_start
if
.B l_len
is zero.  If
.B l_len
is nonzero then the file keeps its size, but the freed bytes now read as
zeros.  (Other than sharing the flock structure, this call has nothing to do
with locking.)  (This call is common among UNIX(-like) systems.)
.RE
.SP
.BI "fcntl(" fd ", F_SEEK, u64_t " pos ")"
.RS
This Minix-vmd specific call sets the file position of the file associated
with file descriptor
.I fd
to the byte offset indicated by the 64-bit number
.IR pos .
This is analogous to the call
.SP
.RS
.BI "lseek(" fd ", " pos ", SEEK_SET)"
.RE
.SP
except that
.B F_SEEK
can be used on devices larger than 4 gigabyte.
.RE
.SH "SEE ALSO"
.BR open (2),
.BR dup (2),
.BR lseek (2),
.BR ftruncate (3),
.BR int64 (3).
.SH DIAGNOSTICS
.B Fcntl
returns a file descriptor, flags, or
.B 0
to indicate success.  On error
.B \-1
is returned, with
.B errno
set to the appropriate error code.  The most notable errors are:
.TP 5
.B EINTR
If a blocked
.B F_SETLKW
operation is interrupted by a signal that is caught.
.TP
.B EAGAIN
By
.B F_SETLK
if a segment cannot be locked.
.TP
.B EBADF
A bad file descriptor in general, or an attempt to place a write lock on a
file that is not open for writing, etc.
.TP
.B ENOLCK
No locks available, the file system code has run out of internal table
space.
.SH AUTHOR
Kees J. Bot <kjb@cs.vu.nl>

.\"
.\" $PchId: fcntl.2,v 1.2 2000/08/11 19:39:51 philip Exp $