std: Set CLOEXEC for all fds opened on unix
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]
This commit is contained in:
parent
88fc543866
commit
d6c72306c8
6 changed files with 125 additions and 30 deletions
|
@ -47,7 +47,9 @@ impl Socket {
|
|||
};
|
||||
unsafe {
|
||||
let fd = try!(cvt(libc::socket(fam, ty, 0)));
|
||||
Ok(Socket(FileDesc::new(fd)))
|
||||
let fd = FileDesc::new(fd);
|
||||
fd.set_cloexec();
|
||||
Ok(Socket(fd))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -56,13 +58,16 @@ impl Socket {
|
|||
let fd = try!(cvt_r(|| unsafe {
|
||||
libc::accept(self.0.raw(), storage, len)
|
||||
}));
|
||||
Ok(Socket(FileDesc::new(fd)))
|
||||
let fd = FileDesc::new(fd);
|
||||
fd.set_cloexec();
|
||||
Ok(Socket(fd))
|
||||
}
|
||||
|
||||
pub fn duplicate(&self) -> io::Result<Socket> {
|
||||
cvt(unsafe { libc::dup(self.0.raw()) }).map(|fd| {
|
||||
Socket(FileDesc::new(fd))
|
||||
})
|
||||
let fd = try!(cvt(unsafe { libc::dup(self.0.raw()) }));
|
||||
let fd = FileDesc::new(fd);
|
||||
fd.set_cloexec();
|
||||
Ok(Socket(fd))
|
||||
}
|
||||
|
||||
pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue