Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent FD leakage when calling fork(2)+exec(2) #557

Closed
wants to merge 4 commits into from
Closed

Prevent FD leakage when calling fork(2)+exec(2) #557

wants to merge 4 commits into from

Conversation

feliperuiz
Copy link

I couldn't think of a better way to test this behavior, but I'm open to suggestions.

This should resolve #555.

In order to prevent file descriptor leakage when calling fork(2)+exec(2), the FD_CLOEXEC flag needs to be set. Just setting the flag using fcntl(2) after socket creation is, however, not enough, since, in a multi-threaded application, another thread might fork after we created the socket but before we had the chance of setting the flag. To remedy this, Linux introduced the SOCK_CLOEXEC flag in 2.6.27, to be set on socket creation. At the time of this commit, it has been ported to other systems, such as FreeBSD. Should the flag not be available, as is the case in macOS or older versions of the Linux kernel, we do the next best thing, which is trying to call fcntl(2) right after socket creation.
AppVeyor fails due to an implicit declaration of strerror_r.
I had renamed the definition but forgot to rename the call. My bad.
@feliperuiz feliperuiz closed this Nov 6, 2017
@feliperuiz feliperuiz deleted the set-cloexec branch November 6, 2017 11:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

File descriptor handlers leak on fork + exec
1 participant