Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the FD_CLOEXEC fcntl() flag do?

Tags:

c

Like so:

if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) { ... 

Though I've read man fcntl, I can't figure out what it does.

like image 253
compile-fan Avatar asked May 25 '11 13:05

compile-fan


People also ask

What is the significance of FD_CLOEXEC flag?

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() .

What is Fcntl used for?

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.

What is Fcntl h in C?

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.

What is Fcntl h in Unix?

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):


1 Answers

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.

like image 131
R.. GitHub STOP HELPING ICE Avatar answered Nov 08 '22 00:11

R.. GitHub STOP HELPING ICE