Like so:
if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) { ...
Though I've read man fcntl
, I can't figure out what it does.
It marks the file descriptor so that it will be close() d automatically when the process or any children it fork() s calls one of the exec*() family of functions. This is useful to keep from leaking your file descriptors to random programs run by e.g. system() .
The fcntl is a system call. It allows the program to place a read or a write lock. This function can be used to amend the file properties that are either opened already or can be opened through any action applied to it. It is a versatile function and is used to modify files in many ways like open, read and write, etc.
You can help to develop the work, or you can ask for assistance in the project room. fcntl. h is the header in the C POSIX library for the C programming language that contains constructs that refer to file control, e.g. opening a file, retrieving and changing the permissions of file, locking a file for edit, etc.
DESCRIPTION. The <fcntl. h> header defines the following requests and arguments for use by the functions fcntl() and open(). Values for cmd used by fcntl() (the following values are unique):
It sets the close-on-exec flag for the file descriptor, which causes the file descriptor to be automatically (and atomically) closed when any of the exec
-family functions succeed.
It also tests the return value to see if the operation failed, which is rather useless if the file descriptor is valid, since there is no condition under which this operation should fail on a valid file descriptor.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With