Next: File Status Flags, Previous: Duplicating Descriptors, Up: Low-Level I/O
File descriptor flags are miscellaneous attributes of a file descriptor. These flags are associated with particular file descriptors, so that if you have created duplicate file descriptors from a single opening of a file, each descriptor has its own set of flags.
Currently there is just one file descriptor flag: FD_CLOEXEC,
which causes the descriptor to be closed if you use any of the
exec... functions (see Executing a File).
The symbols in this section are defined in the header file fcntl.h.
This macro is used as the command argument to
fcntl, to specify that it should return the file descriptor flags associated with the filedes argument.The normal return value from
fcntlwith this command is a nonnegative number which can be interpreted as the bitwise OR of the individual flags (except that currently there is only one flag to use).In case of an error,
fcntlreturns -1. The followingerrnoerror conditions are defined for this command:
EBADF- The filedes argument is invalid.
This macro is used as the command argument to
fcntl, to specify that it should set the file descriptor flags associated with the filedes argument. This requires a thirdintargument to specify the new flags, so the form of the call is:fcntl (filedes, F_SETFD, new-flags)The normal return value from
fcntlwith this command is an unspecified value other than -1, which indicates an error. The flags and error conditions are the same as for theF_GETFDcommand.
The following macro is defined for use as a file descriptor flag with
the fcntl function. The value is an integer constant usable
as a bit mask value.
This flag specifies that the file descriptor should be closed when an
execfunction is invoked; see Executing a File. When a file descriptor is allocated (as withopenordup), this bit is initially cleared on the new file descriptor, meaning that descriptor will survive into the new program afterexec.
If you want to modify the file descriptor flags, you should get the
current flags with F_GETFD and modify the value. Don't assume
that the flags listed here are the only ones that are implemented; your
program may be run years from now and more flags may exist then. For
example, here is a function to set or clear the flag FD_CLOEXEC
without altering any other flags:
/* Set theFD_CLOEXECflag of desc if value is nonzero, or clear the flag if value is 0. Return 0 on success, or -1 on error witherrnoset. */ int set_cloexec_flag (int desc, int value) { int oldflags = fcntl (desc, F_GETFD, 0); /* If reading the flags failed, return error indication now. */ if (oldflags < 0) return oldflags; /* Set just the flag we want to set. */ if (value != 0) oldflags |= FD_CLOEXEC; else oldflags &= ~FD_CLOEXEC; /* Store modified flag word in the descriptor. */ return fcntl (desc, F_SETFD, oldflags); }