]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - lib/libc/sys/fcntl.2
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / lib / libc / sys / fcntl.2
1 .\" Copyright (c) 1983, 1993
2 .\"     The Regents of the University of California.  All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\"    notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\"    notice, this list of conditions and the following disclaimer in the
11 .\"    documentation and/or other materials provided with the distribution.
12 .\" 4. Neither the name of the University nor the names of its contributors
13 .\"    may be used to endorse or promote products derived from this software
14 .\"    without specific prior written permission.
15 .\"
16 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 .\" SUCH DAMAGE.
27 .\"
28 .\"     @(#)fcntl.2     8.2 (Berkeley) 1/12/94
29 .\" $FreeBSD$
30 .\"
31 .Dd February 8, 2013
32 .Dt FCNTL 2
33 .Os
34 .Sh NAME
35 .Nm fcntl
36 .Nd file control
37 .Sh LIBRARY
38 .Lb libc
39 .Sh SYNOPSIS
40 .In fcntl.h
41 .Ft int
42 .Fn fcntl "int fd" "int cmd" "..."
43 .Sh DESCRIPTION
44 The
45 .Fn fcntl
46 system call provides for control over descriptors.
47 The argument
48 .Fa fd
49 is a descriptor to be operated on by
50 .Fa cmd
51 as described below.
52 Depending on the value of
53 .Fa cmd ,
54 .Fn fcntl
55 can take an additional third argument
56 .Fa "int arg" .
57 .Bl -tag -width F_DUP2FD_CLOEXEC
58 .It Dv F_DUPFD
59 Return a new descriptor as follows:
60 .Pp
61 .Bl -bullet -compact -offset 4n
62 .It
63 Lowest numbered available descriptor greater than or equal to
64 .Fa arg .
65 .It
66 Same object references as the original descriptor.
67 .It
68 New descriptor shares the same file offset if the object
69 was a file.
70 .It
71 Same access mode (read, write or read/write).
72 .It
73 Same file status flags (i.e., both file descriptors
74 share the same file status flags).
75 .It
76 The close-on-exec flag
77 .Dv FD_CLOEXEC
78 associated with the new file descriptor is cleared, so the file descriptor is
79 to remain open across
80 .Xr execve 2
81 system calls.
82 .El
83 .It Dv F_DUPFD_CLOEXEC
84 Like
85 .Dv F_DUPFD ,
86 but the
87 .Dv FD_CLOEXEC
88 flag associated with the new file descriptor is set, so the file descriptor
89 is closed when
90 .Xr execve 2
91 system call executes.
92 .It Dv F_DUP2FD
93 It is functionally equivalent to
94 .Bd -literal -offset indent
95 dup2(fd, arg)
96 .Ed
97 .It Dv F_DUP2FD_CLOEXEC
98 Like
99 .Dv F_DUP2FD ,
100 but the
101 .Dv FD_CLOEXEC
102 flag associated with the new file descriptor is set.
103 .Pp
104 The
105 .Dv F_DUP2FD
106 and
107 .Dv F_DUP2FD_CLOEXEC
108 constants are not portable, so they should not be used if
109 portability is needed.
110 Use
111 .Fn dup2
112 instead of
113 .Dv F_DUP2FD .
114 .It Dv F_GETFD
115 Get the close-on-exec flag associated with the file descriptor
116 .Fa fd
117 as
118 .Dv FD_CLOEXEC .
119 If the returned value ANDed with
120 .Dv FD_CLOEXEC
121 is 0,
122 the file will remain open across
123 .Fn exec ,
124 otherwise the file will be closed upon execution of
125 .Fn exec
126 .Fa ( arg
127 is ignored).
128 .It Dv F_SETFD
129 Set the close-on-exec flag associated with
130 .Fa fd
131 to
132 .Fa arg ,
133 where
134 .Fa arg
135 is either 0 or
136 .Dv FD_CLOEXEC ,
137 as described above.
138 .It Dv F_GETFL
139 Get descriptor status flags, as described below
140 .Fa ( arg
141 is ignored).
142 .It Dv F_SETFL
143 Set descriptor status flags to
144 .Fa arg .
145 .It Dv F_GETOWN
146 Get the process ID or process group
147 currently receiving
148 .Dv SIGIO
149 and
150 .Dv SIGURG
151 signals; process groups are returned
152 as negative values
153 .Fa ( arg
154 is ignored).
155 .It Dv F_SETOWN
156 Set the process or process group
157 to receive
158 .Dv SIGIO
159 and
160 .Dv SIGURG
161 signals;
162 process groups are specified by supplying
163 .Fa arg
164 as negative, otherwise
165 .Fa arg
166 is interpreted as a process ID.
167 .It Dv F_READAHEAD
168 Set or clear the read ahead amount for sequential access to the third
169 argument,
170 .Fa arg ,
171 which is rounded up to the nearest block size.
172 A zero value in
173 .Fa arg
174 turns off read ahead, a negative value restores the system default.
175 .It Dv F_RDAHEAD
176 Equivalent to Darwin counterpart which sets read ahead amount of 128KB
177 when the third argument,
178 .Fa arg
179 is non-zero.
180 A zero value in
181 .Fa arg
182 turns off read ahead.
183 .El
184 .Pp
185 The flags for the
186 .Dv F_GETFL
187 and
188 .Dv F_SETFL
189 flags are as follows:
190 .Bl -tag -width O_NONBLOCKX
191 .It Dv O_NONBLOCK
192 Non-blocking I/O; if no data is available to a
193 .Xr read 2
194 system call, or if a
195 .Xr write 2
196 operation would block,
197 the read or write call returns -1 with the error
198 .Er EAGAIN .
199 .It Dv O_APPEND
200 Force each write to append at the end of file;
201 corresponds to the
202 .Dv O_APPEND
203 flag of
204 .Xr open 2 .
205 .It Dv O_DIRECT
206 Minimize or eliminate the cache effects of reading and writing.
207 The system
208 will attempt to avoid caching the data you read or write.
209 If it cannot
210 avoid caching the data, it will minimize the impact the data has on the cache.
211 Use of this flag can drastically reduce performance if not used with care.
212 .It Dv O_ASYNC
213 Enable the
214 .Dv SIGIO
215 signal to be sent to the process group
216 when I/O is possible, e.g.,
217 upon availability of data to be read.
218 .El
219 .Pp
220 Several commands are available for doing advisory file locking;
221 they all operate on the following structure:
222 .Bd -literal
223 struct flock {
224         off_t   l_start;        /* starting offset */
225         off_t   l_len;          /* len = 0 means until end of file */
226         pid_t   l_pid;          /* lock owner */
227         short   l_type;         /* lock type: read/write, etc. */
228         short   l_whence;       /* type of l_start */
229         int     l_sysid;        /* remote system id or zero for local */
230 };
231 .Ed
232 The commands available for advisory record locking are as follows:
233 .Bl -tag -width F_SETLKWX
234 .It Dv F_GETLK
235 Get the first lock that blocks the lock description pointed to by the
236 third argument,
237 .Fa arg ,
238 taken as a pointer to a
239 .Fa "struct flock"
240 (see above).
241 The information retrieved overwrites the information passed to
242 .Fn fcntl
243 in the
244 .Fa flock
245 structure.
246 If no lock is found that would prevent this lock from being created,
247 the structure is left unchanged by this system call except for the
248 lock type which is set to
249 .Dv F_UNLCK .
250 .It Dv F_SETLK
251 Set or clear a file segment lock according to the lock description
252 pointed to by the third argument,
253 .Fa arg ,
254 taken as a pointer to a
255 .Fa "struct flock"
256 (see above).
257 .Dv F_SETLK
258 is used to establish shared (or read) locks
259 .Pq Dv F_RDLCK
260 or exclusive (or write) locks,
261 .Pq Dv F_WRLCK ,
262 as well as remove either type of lock
263 .Pq Dv F_UNLCK .
264 If a shared or exclusive lock cannot be set,
265 .Fn fcntl
266 returns immediately with
267 .Er EAGAIN .
268 .It Dv F_SETLKW
269 This command is the same as
270 .Dv F_SETLK
271 except that if a shared or exclusive lock is blocked by other locks,
272 the process waits until the request can be satisfied.
273 If a signal that is to be caught is received while
274 .Fn fcntl
275 is waiting for a region, the
276 .Fn fcntl
277 will be interrupted if the signal handler has not specified the
278 .Dv SA_RESTART
279 (see
280 .Xr sigaction 2 ) .
281 .El
282 .Pp
283 When a shared lock has been set on a segment of a file,
284 other processes can set shared locks on that segment
285 or a portion of it.
286 A shared lock prevents any other process from setting an exclusive
287 lock on any portion of the protected area.
288 A request for a shared lock fails if the file descriptor was not
289 opened with read access.
290 .Pp
291 An exclusive lock prevents any other process from setting a shared lock or
292 an exclusive lock on any portion of the protected area.
293 A request for an exclusive lock fails if the file was not
294 opened with write access.
295 .Pp
296 The value of
297 .Fa l_whence
298 is
299 .Dv SEEK_SET ,
300 .Dv SEEK_CUR ,
301 or
302 .Dv SEEK_END
303 to indicate that the relative offset,
304 .Fa l_start
305 bytes, will be measured from the start of the file,
306 current position, or end of the file, respectively.
307 The value of
308 .Fa l_len
309 is the number of consecutive bytes to be locked.
310 If
311 .Fa l_len
312 is negative,
313 .Fa l_start
314 means end edge of the region.
315 The
316 .Fa l_pid
317 and
318 .Fa l_sysid
319 fields are only used with
320 .Dv F_GETLK
321 to return the process ID of the process holding a blocking lock and
322 the system ID of the system that owns that process.
323 Locks created by the local system will have a system ID of zero.
324 After a successful
325 .Dv F_GETLK
326 request, the value of
327 .Fa l_whence
328 is
329 .Dv SEEK_SET .
330 .Pp
331 Locks may start and extend beyond the current end of a file,
332 but may not start or extend before the beginning of the file.
333 A lock is set to extend to the largest possible value of the
334 file offset for that file if
335 .Fa l_len
336 is set to zero.
337 If
338 .Fa l_whence
339 and
340 .Fa l_start
341 point to the beginning of the file, and
342 .Fa l_len
343 is zero, the entire file is locked.
344 If an application wishes only to do entire file locking, the
345 .Xr flock 2
346 system call is much more efficient.
347 .Pp
348 There is at most one type of lock set for each byte in the file.
349 Before a successful return from an
350 .Dv F_SETLK
351 or an
352 .Dv F_SETLKW
353 request when the calling process has previously existing locks
354 on bytes in the region specified by the request,
355 the previous lock type for each byte in the specified
356 region is replaced by the new lock type.
357 As specified above under the descriptions
358 of shared locks and exclusive locks, an
359 .Dv F_SETLK
360 or an
361 .Dv F_SETLKW
362 request fails or blocks respectively when another process has existing
363 locks on bytes in the specified region and the type of any of those
364 locks conflicts with the type specified in the request.
365 .Pp
366 This interface follows the completely stupid semantics of System V and
367 .St -p1003.1-88
368 that require that all locks associated with a file for a given process are
369 removed when
370 .Em any
371 file descriptor for that file is closed by that process.
372 This semantic means that applications must be aware of any files that
373 a subroutine library may access.
374 For example if an application for updating the password file locks the
375 password file database while making the update, and then calls
376 .Xr getpwnam 3
377 to retrieve a record,
378 the lock will be lost because
379 .Xr getpwnam 3
380 opens, reads, and closes the password database.
381 The database close will release all locks that the process has
382 associated with the database, even if the library routine never
383 requested a lock on the database.
384 Another minor semantic problem with this interface is that
385 locks are not inherited by a child process created using the
386 .Xr fork 2
387 system call.
388 The
389 .Xr flock 2
390 interface has much more rational last close semantics and
391 allows locks to be inherited by child processes.
392 The
393 .Xr flock 2
394 system call is recommended for applications that want to ensure the integrity
395 of their locks when using library routines or wish to pass locks
396 to their children.
397 .Pp
398 The
399 .Fn fcntl ,
400 .Xr flock 2 ,
401 and
402 .Xr lockf 3
403 locks are compatible.
404 Processes using different locking interfaces can cooperate
405 over the same file safely.
406 However, only one of such interfaces should be used within
407 the same process.
408 If a file is locked by a process through
409 .Xr flock 2 ,
410 any record within the file will be seen as locked
411 from the viewpoint of another process using
412 .Fn fcntl
413 or
414 .Xr lockf 3 ,
415 and vice versa.
416 Note that
417 .Fn fcntl F_GETLK
418 returns \-1 in
419 .Fa l_pid
420 if the process holding a blocking lock previously locked the
421 file descriptor by
422 .Xr flock 2 .
423 .Pp
424 All locks associated with a file for a given process are
425 removed when the process terminates.
426 .Pp
427 All locks obtained before a call to
428 .Xr execve 2
429 remain in effect until the new program releases them.
430 If the new program does not know about the locks, they will not be
431 released until the program exits.
432 .Pp
433 A potential for deadlock occurs if a process controlling a locked region
434 is put to sleep by attempting to lock the locked region of another process.
435 This implementation detects that sleeping until a locked region is unlocked
436 would cause a deadlock and fails with an
437 .Er EDEADLK
438 error.
439 .Sh RETURN VALUES
440 Upon successful completion, the value returned depends on
441 .Fa cmd
442 as follows:
443 .Bl -tag -width F_GETOWNX -offset indent
444 .It Dv F_DUPFD
445 A new file descriptor.
446 .It Dv F_DUP2FD
447 A file descriptor equal to
448 .Fa arg .
449 .It Dv F_GETFD
450 Value of flag (only the low-order bit is defined).
451 .It Dv F_GETFL
452 Value of flags.
453 .It Dv F_GETOWN
454 Value of file descriptor owner.
455 .It other
456 Value other than -1.
457 .El
458 .Pp
459 Otherwise, a value of -1 is returned and
460 .Va errno
461 is set to indicate the error.
462 .Sh ERRORS
463 The
464 .Fn fcntl
465 system call will fail if:
466 .Bl -tag -width Er
467 .It Bq Er EAGAIN
468 The argument
469 .Fa cmd
470 is
471 .Dv F_SETLK ,
472 the type of lock
473 .Pq Fa l_type
474 is a shared lock
475 .Pq Dv F_RDLCK
476 or exclusive lock
477 .Pq Dv F_WRLCK ,
478 and the segment of a file to be locked is already
479 exclusive-locked by another process;
480 or the type is an exclusive lock and some portion of the
481 segment of a file to be locked is already shared-locked or
482 exclusive-locked by another process.
483 .It Bq Er EBADF
484 The
485 .Fa fd
486 argument
487 is not a valid open file descriptor.
488 .Pp
489 The argument
490 .Fa cmd
491 is
492 .Dv F_DUP2FD ,
493 and
494 .Fa arg
495 is not a valid file descriptor.
496 .Pp
497 The argument
498 .Fa cmd
499 is
500 .Dv F_SETLK
501 or
502 .Dv F_SETLKW ,
503 the type of lock
504 .Pq Fa l_type
505 is a shared lock
506 .Pq Dv F_RDLCK ,
507 and
508 .Fa fd
509 is not a valid file descriptor open for reading.
510 .Pp
511 The argument
512 .Fa cmd
513 is
514 .Dv F_SETLK
515 or
516 .Dv F_SETLKW ,
517 the type of lock
518 .Pq Fa l_type
519 is an exclusive lock
520 .Pq Dv F_WRLCK ,
521 and
522 .Fa fd
523 is not a valid file descriptor open for writing.
524 .It Bq Er EDEADLK
525 The argument
526 .Fa cmd
527 is
528 .Dv F_SETLKW ,
529 and a deadlock condition was detected.
530 .It Bq Er EINTR
531 The argument
532 .Fa cmd
533 is
534 .Dv F_SETLKW ,
535 and the system call was interrupted by a signal.
536 .It Bq Er EINVAL
537 The
538 .Fa cmd
539 argument
540 is
541 .Dv F_DUPFD
542 and
543 .Fa arg
544 is negative or greater than the maximum allowable number
545 (see
546 .Xr getdtablesize 2 ) .
547 .Pp
548 The argument
549 .Fa cmd
550 is
551 .Dv F_GETLK ,
552 .Dv F_SETLK
553 or
554 .Dv F_SETLKW
555 and the data to which
556 .Fa arg
557 points is not valid.
558 .It Bq Er EMFILE
559 The argument
560 .Fa cmd
561 is
562 .Dv F_DUPFD
563 and the maximum number of file descriptors permitted for the
564 process are already in use,
565 or no file descriptors greater than or equal to
566 .Fa arg
567 are available.
568 .It Bq Er ENOLCK
569 The argument
570 .Fa cmd
571 is
572 .Dv F_SETLK
573 or
574 .Dv F_SETLKW ,
575 and satisfying the lock or unlock request would result in the
576 number of locked regions in the system exceeding a system-imposed limit.
577 .It Bq Er EOPNOTSUPP
578 The argument
579 .Fa cmd
580 is
581 .Dv F_GETLK ,
582 .Dv F_SETLK
583 or
584 .Dv F_SETLKW
585 and
586 .Fa fd
587 refers to a file for which locking is not supported.
588 .It Bq Er EOVERFLOW
589 The argument
590 .Fa cmd
591 is
592 .Dv F_GETLK ,
593 .Dv F_SETLK
594 or
595 .Dv F_SETLKW
596 and an
597 .Fa off_t
598 calculation overflowed.
599 .It Bq Er EPERM
600 The
601 .Fa cmd
602 argument
603 is
604 .Dv F_SETOWN
605 and
606 the process ID or process group given as an argument is in a
607 different session than the caller.
608 .It Bq Er ESRCH
609 The
610 .Fa cmd
611 argument
612 is
613 .Dv F_SETOWN
614 and
615 the process ID given as argument is not in use.
616 .El
617 .Pp
618 In addition, if
619 .Fa fd
620 refers to a descriptor open on a terminal device (as opposed to a
621 descriptor open on a socket), a
622 .Fa cmd
623 of
624 .Dv F_SETOWN
625 can fail for the same reasons as in
626 .Xr tcsetpgrp 3 ,
627 and a
628 .Fa cmd
629 of
630 .Dv F_GETOWN
631 for the reasons as stated in
632 .Xr tcgetpgrp 3 .
633 .Sh SEE ALSO
634 .Xr close 2 ,
635 .Xr dup2 2 ,
636 .Xr execve 2 ,
637 .Xr flock 2 ,
638 .Xr getdtablesize 2 ,
639 .Xr open 2 ,
640 .Xr sigaction 2 ,
641 .Xr lockf 3 ,
642 .Xr tcgetpgrp 3 ,
643 .Xr tcsetpgrp 3
644 .Sh STANDARDS
645 The
646 .Dv F_DUP2FD
647 constant is non portable.
648 It is provided for compatibility with AIX and Solaris.
649 .Sh HISTORY
650 The
651 .Fn fcntl
652 system call appeared in
653 .Bx 4.2 .
654 .Pp
655 The
656 .Dv F_DUP2FD
657 constant first appeared in
658 .Fx 7.1 .