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

Set CLOEXEC for all fds on unix by default #24034

Merged
merged 3 commits into from
Apr 10, 2015
Merged

Conversation

alexcrichton
Copy link
Member

The commit messages have more details as to what's going on, but this is a breaking change for any libraries which expect file descriptors to be inherited by default.

Closes #12148

@rust-highfive
Copy link
Collaborator

r? @brson

(rust_highfive has picked a reviewer for you, use r? to override)

@alexcrichton
Copy link
Member Author

r? @aturon
cc @pcwalton

unsafe { libc::_exit(1) }
}

rustrt::rust_unset_sigprocmask();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll point out that I deleted this and did not re-add it. I personally have no idea why this is here, and I can't really figure it out otherwise. I'd be fine re-adding though!

@aturon
Copy link
Member

aturon commented Apr 9, 2015

Ack, this r? request fell through my inbox. Reading now...

@aturon
Copy link
Member

aturon commented Apr 9, 2015

Looks great! Thanks @alexcrichton.

@bors: r+

@bors
Copy link
Contributor

bors commented Apr 9, 2015

📌 Commit 0999ece has been approved by aturon

@aturon
Copy link
Member

aturon commented Apr 9, 2015

Oh, and please file a bug for finishing this work by using the atomic APIs whenever available.

@alexcrichton
Copy link
Member Author

Thanks @aturon! I've opened #24237 on the topic.

@bors
Copy link
Contributor

bors commented Apr 9, 2015

⌛ Testing commit 0999ece with merge e787ff3...

@bors
Copy link
Contributor

bors commented Apr 9, 2015

⛄ The build was interrupted to prioritize another pull request.

@bors
Copy link
Contributor

bors commented Apr 9, 2015

⌛ Testing commit 0999ece with merge cd83aec...

@bors
Copy link
Contributor

bors commented Apr 9, 2015

⛄ The build was interrupted to prioritize another pull request.

@bors
Copy link
Contributor

bors commented Apr 9, 2015

🔒 Merge conflict

@bors
Copy link
Contributor

bors commented Apr 9, 2015

☔ The latest upstream changes (presumably #24232) made this pull request unmergeable. Please resolve the merge conflicts.

This commit starts to set the CLOEXEC flag for all files and sockets opened by
the standard library by default on all unix platforms. There are a few points of
note in this commit:

* The implementation is not 100% satisfactory in the face of threads. File
  descriptors only have the `F_CLOEXEC` flag set *after* they are opened,
  allowing for a fork/exec to happen in the middle and leak the descriptor.
  Some platforms do support atomically opening a descriptor while setting the
  `CLOEXEC` flag, and it is left as a future extension to bind these apis as it
  is unclear how to do so nicely at this time.

* The implementation does not offer a method of opting into the old behavior of
  not setting `CLOEXEC`. This will possibly be added in the future through
  extensions on `OpenOptions`, for example.

* This change does not yet audit any Windows APIs to see if the handles are
  inherited by default by accident.

This is a breaking change for users who call `fork` or `exec` outside of the
standard library itself and expect file descriptors to be inherted. All file
descriptors created by the standard library will no longer be inherited.

[breaking-change]
* De-indent quite a bit by removing usage of FnOnce closures
* Clearly separate code for the parent/child after the fork
* Use `fs2::{File, OpenOptions}` instead of calling `open` manually
* Use RAII to close I/O objects wherever possible
* Remove loop for closing all file descriptors, all our own ones are now
  `CLOEXEC` by default so they cannot be inherited
@alexcrichton
Copy link
Member Author

@bors: r=aturon

@bors
Copy link
Contributor

bors commented Apr 10, 2015

📌 Commit 551e15d has been approved by aturon

@bors
Copy link
Contributor

bors commented Apr 10, 2015

⌛ Testing commit 551e15d with merge a73ce4c...

@bors
Copy link
Contributor

bors commented Apr 10, 2015

💔 Test failed - auto-linux-64-x-android-t

The logic for only closing file descriptors >= 3 was inherited from quite some
time ago and ends up meaning that some internal APIs are less consistent than
they should be. By unconditionally closing everything entering a `FileDesc` we
ensure that we're consistent in our behavior as well as robustly handling the
stdio case.
@alexcrichton
Copy link
Member Author

@bors: r=aturon eadc3bc

@bors
Copy link
Contributor

bors commented Apr 10, 2015

⌛ Testing commit eadc3bc with merge f367bd1...

@bors
Copy link
Contributor

bors commented Apr 10, 2015

⛄ The build was interrupted to prioritize another pull request.

bors added a commit that referenced this pull request Apr 10, 2015
The commit messages have more details as to what's going on, but this is a breaking change for any libraries which expect file descriptors to be inherited by default.

Closes #12148
@bors
Copy link
Contributor

bors commented Apr 10, 2015

⌛ Testing commit eadc3bc with merge 9539627...

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.

Danger of leaking file descriptors when spawning processes
5 participants