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
|
.\" Copyright, the authors of the Linux man-pages project
.\"
.\" SPDX-License-Identifier: Linux-man-pages-copyleft
.\"
.TH F_GETFD 2const 2025-07-19 "Linux man-pages (unreleased)"
.SH NAME
F_GETFD,
F_SETFD
\-
get/set file descriptor flags
.SH LIBRARY
Standard C library
.RI ( libc ,\~ \-lc )
.SH SYNOPSIS
.nf
.B #include <fcntl.h>
.P
.BI "int fcntl(int " fd ", F_GETFD);"
.BI "int fcntl(int " fd ", F_SETFD, int " arg );
.fi
.SH DESCRIPTION
These operations manipulate the flags associated with
a file descriptor.
Currently, only one such flag is defined:
.BR FD_CLOEXEC ,
the close-on-exec flag.
If the
.B FD_CLOEXEC
bit is set,
the file descriptor will automatically be closed during a successful
.BR execve (2).
(If the
.BR execve (2)
fails, the file descriptor is left open.)
If the
.B FD_CLOEXEC
bit is not set, the file descriptor will remain open across an
.BR execve (2).
.TP
.B F_GETFD
Return (as the function result) the file descriptor flags;
.I arg
is ignored.
.TP
.B F_SETFD
Set the file descriptor flags to the value specified by
.IR arg .
.SH RETURN VALUE
.TP
.B F_GETFD
Value of file descriptor flags.
.TP
.B F_SETFD
Zero.
.P
On error, \-1 is returned, and
.I errno
is set to indicate the error.
.SH ERRORS
See
.BR fcntl (2).
.SH STANDARDS
POSIX.1-2008.
.SH HISTORY
SVr4, 4.3BSD, POSIX.1-2001.
.SH CAVEATS
In multithreaded programs, using
.BR fcntl (2)
.B F_SETFD
to set the close-on-exec flag at the same time as another thread performs a
.BR fork (2)
plus
.BR execve (2)
is vulnerable to a race condition that may unintentionally leak
the file descriptor to the program executed in the child process.
See the discussion of the
.B O_CLOEXEC
flag in
.BR open (2)
for details and a remedy to the problem.
.SH SEE ALSO
.BR fcntl (2)
|