3 fcntl \- miscellaneous file descriptor control functions
9 int fcntl(int \fIfd\fP, int \fIcmd\fP, \fR[\fP\fIdata\fP\fR]\fP)
18 performs several file descriptor related functions, like duplicating a file
19 descriptor, setting the "close on exec" attribute, etc. The
21 argument is the file descriptor to operate on,
23 is the command code of the operation to perform, and
25 is an optional argument to give or receive parameters. The command
26 codes and other symbols and types are declared in <fcntl.h>. The commands
29 .BI "fcntl(" fd ", F_DUPFD, int " fd2 ")"
31 Returns a new file descriptor that is a duplicate of file descriptor
33 It shares the same file pointer and the same file status flags, but has
34 separate file descriptor flags that are initially off. The value of the
35 duplicate file descriptor is the first free file descriptor greater than
40 .BI "fcntl(" fd ", F_DUPFD_CLOEXEC, int " fd2 ")"
44 but the "close on exec" flag is set on the returned file descriptor.
47 .BI "fcntl(" fd ", F_GETFD)"
49 Returns the file descriptor flags associated with file descriptor
51 The flags are the "close on exec" flag
53 that, when set, causes the file descriptor to be closed when the process
54 executes another program. The Minix-vmd specific
56 flag marks a file descriptor for asynchronous I/O operation.
59 .BI "fcntl(" fd ", F_SETFD, int " flags ")"
61 Set the file descriptor flags of
67 .BI "fcntl(" fd ", F_GETFL)"
69 Return the file status flags and file access modes associated with the file
70 associated with file descriptor
72 The file status flags are
74 (non blocking I/O) and
76 (append mode). The file access modes are
82 (read-write). These flags are also used in the second argument of
86 .BI "fcntl(" fd ", F_SETFL, int " flags ")"
88 Set the file status flags of the file referenced by
96 may be changed. Access mode flags are ignored.
99 The next four commands use a parameter of type
101 that is defined in <fcntl.h> as:
107 short l_type; /* F_RDLCK, F_WRLCK, or F_UNLCK */
108 short l_whence; /* SEEK_SET, SEEK_CUR, or SEEK_END */
109 off_t l_start; /* byte offset to start of segment */
110 off_t l_len; /* length of segment */
111 pid_t l_pid; /* process id of the locks' owner */
116 This structure describes a segment of a file.
118 is the lock operation performed on the file segment:
122 to set a write lock, and
124 to remove a lock. Several processes may have a read lock on a segment, but
125 only one process can have a write lock.
129 offset must be interpreted from the start of the file
131 the current file position
133 or the end of the file
135 This is analogous to the third parameter of
139 symbols are declared in <unistd.h>.
141 is the starting offset of the segment of the file.
143 is the length of the segment. If zero then the segment extends until end of
146 is the process-id of the process currently holding a lock on the segment.
150 .BI "fcntl(" fd ", F_GETLK, struct flock *" lkp ")"
152 Find out if some other process has a lock on a segment of the file
153 associated by file descriptor
155 that overlaps with the segment described by the
157 structure pointed to by
159 If the segment is not locked then
165 structure is returned through
167 that describes the lock held by the other process.
169 is set relative to the start of the file.
172 .BI "fcntl(" fd ", F_SETLK, struct flock *" lkp ")"
174 Register a lock on a segment of the file associated with file descriptor
176 The file segment is described by the
180 This call returns an error if any part of the segment is already locked.
183 .BI "fcntl(" fd ", F_SETLKW, struct flock *" lkp ")"
185 Register a lock on a segment of the file associated with file descriptor
187 The file segment is described by the
191 This call blocks waiting for the lock to be released if any part of the
192 segment is already locked.
195 .BI "fcntl(" fd ", F_FREESP, struct flock *" lkp ")"
197 This call frees a segment of disk space occupied by the
198 file associated with file descriptor
200 The segment is described by the
204 The file is truncated in length to the byte position indicated by
210 is nonzero then the file keeps its size, but the freed bytes now read as
211 zeros. (Other than sharing the flock structure, this call has nothing to do
212 with locking.) (This call is common among UNIX(-like) systems.)
215 .BI "fcntl(" fd ", F_SEEK, u64_t " pos ")"
217 This Minix-vmd specific call sets the file position of the file associated
220 to the byte offset indicated by the 64-bit number
222 This is analogous to the call
225 .BI "lseek(" fd ", " pos ", SEEK_SET)"
230 can be used on devices larger than 4 gigabyte.
240 returns a file descriptor, flags, or
242 to indicate success. On error
246 set to the appropriate error code. The most notable errors are:
251 operation is interrupted by a signal that is caught.
256 if a segment cannot be locked.
259 A bad file descriptor in general, or an attempt to place a write lock on a
260 file that is not open for writing, etc.
263 No locks available, the file system code has run out of internal table
266 Kees J. Bot <kjb@cs.vu.nl>
269 .\" $PchId: fcntl.2,v 1.2 2000/08/11 19:39:51 philip Exp $