Skip to content

Commit 42e8618

Browse files
author
Hui Zhu
authored
Merge pull request #55 from Tim-Zhang/close-exec-for-socket
Close exec for socket
2 parents dfd7d40 + 9a9aabf commit 42e8618

File tree

4 files changed

+37
-5
lines changed

4 files changed

+37
-5
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ttrpc"
3-
version = "0.4.3"
3+
version = "0.4.4"
44
authors = ["The AntFin Kata Team <[email protected]>"]
55
edition = "2018"
66
license = "Apache-2.0"

example/Cargo.lock

Lines changed: 22 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/asynchronous/server.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::ttrpc::{Code, Request};
1616
use crate::MessageHeader;
1717
use futures::StreamExt as _;
1818
use std::marker::Unpin;
19-
use std::os::unix::io::FromRawFd;
19+
use std::os::unix::io::{AsRawFd, FromRawFd};
2020
use std::os::unix::net::UnixListener as SysUnixListener;
2121
use tokio::{
2222
self,
@@ -118,11 +118,12 @@ impl Server {
118118
pub async fn do_start<I, S>(&self, listenfd: RawFd, mut incoming: I) -> Result<()>
119119
where
120120
I: Stream<Item = std::io::Result<S>> + Unpin,
121-
S: AsyncRead + AsyncWrite + Send + 'static,
121+
S: AsyncRead + AsyncWrite + AsRawFd + Send + 'static,
122122
{
123123
while let Some(result) = incoming.next().await {
124124
match result {
125125
Ok(stream) => {
126+
common::set_fd_close_exec(stream.as_raw_fd())?;
126127
let methods = self.methods.clone();
127128
tokio::spawn(async move {
128129
let (mut reader, mut writer) = split(stream);

src/common.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#![allow(unused_macros)]
99

1010
use crate::error::{Error, Result};
11-
use nix::fcntl::{fcntl, FcntlArg, OFlag};
11+
use nix::fcntl::{fcntl, FcntlArg, FdFlag, OFlag};
1212
use nix::sys::socket::*;
1313
use std::os::unix::io::RawFd;
1414
use std::str::FromStr;
@@ -54,6 +54,16 @@ pub fn parse_host(host: &str) -> Result<(Domain, Vec<&str>)> {
5454
Ok((domain, hostv))
5555
}
5656

57+
pub fn set_fd_close_exec(fd: RawFd) -> Result<RawFd> {
58+
if let Err(e) = fcntl(fd, FcntlArg::F_SETFD(FdFlag::FD_CLOEXEC)) {
59+
return Err(Error::Others(format!(
60+
"failed to set fd: {} as close-on-exec: {}",
61+
fd, e
62+
)));
63+
}
64+
Ok(fd)
65+
}
66+
5767
pub fn do_bind(host: &str) -> Result<(RawFd, Domain)> {
5868
let (domain, hostv) = parse_host(host)?;
5969

0 commit comments

Comments
 (0)