.\" 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 6.15" .SH NAME F_GETFD, F_SETFD \- get/set file descriptor flags .SH LIBRARY Standard C library .RI ( libc ,\~ \-lc ) .SH SYNOPSIS .nf .B #include .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)