source: trunk/minix/man/man2/fcntl.2@ 9

Last change on this file since 9 was 9, checked in by Mattia Monga, 13 years ago

Minix 3.1.2a

File size: 6.1 KB
Line 
1.TH FCNTL 2
2.SH NAME
3fcntl \- miscellaneous file descriptor control functions
4.SH SYNOPSIS
5.nf
6.ft B
7#include <fcntl.h>
8
9int fcntl(int \fIfd\fP, int \fIcmd\fP, \fR[\fP\fIdata\fP\fR]\fP)
10.ft P
11.fi
12.SH DESCRIPTION
13.de SP
14.if t .sp 0.4
15.if n .sp
16..
17.B Fcntl()
18performs several file descriptor related functions, like duplicating a file
19descriptor, setting the "close on exec" attribute, etc. The
20.I fd
21argument is the file descriptor to operate on,
22.I cmd
23is the command code of the operation to perform, and
24.I data
25is an optional argument to give or receive parameters. The command
26codes and other symbols and types are declared in <fcntl.h>. The commands
27are:
28.SP
29.BI "fcntl(" fd ", F_DUPFD, int " fd2 ")"
30.RS
31Returns a new file descriptor that is a duplicate of file descriptor
32.IR fd .
33It shares the same file pointer and the same file status flags, but has
34separate file descriptor flags that are initially off. The value of the
35duplicate file descriptor is the first free file descriptor greater than
36or equal to
37.IR fd2 .
38.RE
39.SP
40.BI "fcntl(" fd ", F_GETFD)"
41.RS
42Returns the file descriptor flags associated with file descriptor
43.IR fd .
44The flags are the "close on exec" flag
45.B FD_CLOEXEC
46that, when set, causes the file descriptor to be closed when the process
47executes another program. The Minix-vmd specific
48.B FD_ASYNCHIO
49flag marks a file descriptor for asynchronous I/O operation.
50.RE
51.SP
52.BI "fcntl(" fd ", F_SETFD, int " flags ")"
53.RS
54Set the file descriptor flags of
55.I fd
56to
57.IR flags .
58.RE
59.SP
60.BI "fcntl(" fd ", F_GETFL)"
61.RS
62Return the file status flags and file access modes associated with the file
63associated with file descriptor
64.IR fd .
65The file status flags are
66.B O_NONBLOCK
67(non blocking I/O) and
68.B O_APPEND
69(append mode). The file access modes are
70.B O_RDONLY
71(read-only),
72.B O_WRONLY
73(write-only) and
74.B O_RDWR
75(read-write). These flags are also used in the second argument of
76.BR open (2).
77.RE
78.SP
79.BI "fcntl(" fd ", F_SETFL, int " flags ")"
80.RS
81Set the file status flags of the file referenced by
82.I fd
83to
84.IR flags .
85Only
86.B O_NONBLOCK
87and
88.B O_APPEND
89may be changed. Access mode flags are ignored.
90.RE
91.SP
92The next four commands use a parameter of type
93.B struct flock
94that is defined in <fcntl.h> as:
95.SP
96.RS
97.nf
98.ta +4n +8n +12n
99struct flock {
100 short l_type; /* F_RDLCK, F_WRLCK, or F_UNLCK */
101 short l_whence; /* SEEK_SET, SEEK_CUR, or SEEK_END */
102 off_t l_start; /* byte offset to start of segment */
103 off_t l_len; /* length of segment */
104 pid_t l_pid; /* process id of the locks' owner */
105};
106.fi
107.RE
108.SP
109This structure describes a segment of a file.
110.B L_type
111is the lock operation performed on the file segment:
112.B F_RDLCK
113to set a read lock,
114.B F_WRLCK
115to set a write lock, and
116.B F_UNLCK
117to remove a lock. Several processes may have a read lock on a segment, but
118only one process can have a write lock.
119.B L_whence
120tells if the
121.B l_start
122offset must be interpreted from the start of the file
123.RB ( SEEK_SET ),
124the current file position
125.RB ( SEEK_CUR ),
126or the end of the file
127.RB ( SEEK_END ).
128This is analogous to the third parameter of
129.BR lseek (2).
130These
131.B SEEK_*
132symbols are declared in <unistd.h>.
133.B L_start
134is the starting offset of the segment of the file.
135.B L_end
136is the length of the segment. If zero then the segment extends until end of
137file.
138.B L_pid
139is the process-id of the process currently holding a lock on the segment.
140It is returned by
141.BR F_GETLK .
142.SP
143.BI "fcntl(" fd ", F_GETLK, struct flock *" lkp ")"
144.RS
145Find out if some other process has a lock on a segment of the file
146associated by file descriptor
147.I fd
148that overlaps with the segment described by the
149.B flock
150structure pointed to by
151.IR lkp .
152If the segment is not locked then
153.B l_type
154is set to
155.BR F_UNLCK .
156Otherwise an
157.B flock
158structure is returned through
159.I lkp
160that describes the lock held by the other process.
161.B L_start
162is set relative to the start of the file.
163.RE
164.SP
165.BI "fcntl(" fd ", F_SETLK, struct flock *" lkp ")"
166.RS
167Register a lock on a segment of the file associated with file descriptor
168.IR fd .
169The file segment is described by the
170.B struct flock
171pointed to by
172.IR lkp .
173This call returns an error if any part of the segment is already locked.
174.RE
175.SP
176.BI "fcntl(" fd ", F_SETLKW, struct flock *" lkp ")"
177.RS
178Register a lock on a segment of the file associated with file descriptor
179.IR fd .
180The file segment is described by the
181.B struct flock
182pointed to by
183.IR lkp .
184This call blocks waiting for the lock to be released if any part of the
185segment is already locked.
186.RE
187.SP
188.BI "fcntl(" fd ", F_FREESP, struct flock *" lkp ")"
189.RS
190This call frees a segment of disk space occupied by the
191file associated with file descriptor
192.IR fd .
193The segment is described by the
194.B struct flock
195pointed to by
196.IR lkp .
197The file is truncated in length to the byte position indicated by
198.B l_start
199if
200.B l_len
201is zero. If
202.B l_len
203is nonzero then the file keeps its size, but the freed bytes now read as
204zeros. (Other than sharing the flock structure, this call has nothing to do
205with locking.) (This call is common among UNIX(-like) systems.)
206.RE
207.SP
208.BI "fcntl(" fd ", F_SEEK, u64_t " pos ")"
209.RS
210This Minix-vmd specific call sets the file position of the file associated
211with file descriptor
212.I fd
213to the byte offset indicated by the 64-bit number
214.IR pos .
215This is analogous to the call
216.SP
217.RS
218.BI "lseek(" fd ", " pos ", SEEK_SET)"
219.RE
220.SP
221except that
222.B F_SEEK
223can be used on devices larger than 4 gigabyte.
224.RE
225.SH "SEE ALSO"
226.BR open (2),
227.BR dup (2),
228.BR lseek (2),
229.BR ftruncate (3),
230.BR int64 (3).
231.SH DIAGNOSTICS
232.B Fcntl
233returns a file descriptor, flags, or
234.B 0
235to indicate success. On error
236.B \-1
237is returned, with
238.B errno
239set to the appropriate error code. The most notable errors are:
240.TP 5
241.B EINTR
242If a blocked
243.B F_SETLKW
244operation is interrupted by a signal that is caught.
245.TP
246.B EAGAIN
247By
248.B F_SETLK
249if a segment cannot be locked.
250.TP
251.B EBADF
252A bad file descriptor in general, or an attempt to place a write lock on a
253file that is not open for writing, etc.
254.TP
255.B ENOLCK
256No locks available, the file system code has run out of internal table
257space.
258.SH AUTHOR
259Kees J. Bot <kjb@cs.vu.nl>
260
261.\"
262.\" $PchId: fcntl.2,v 1.2 2000/08/11 19:39:51 philip Exp $
Note: See TracBrowser for help on using the repository browser.