Skip to content

Commit f3d6f9a

Browse files
Merge pull request #1181 from sozu-proxy/devel/edemolis/fix/raw-fd
Fix OwnedFd closing RawFd
2 parents f998bb9 + 28fff56 commit f3d6f9a

File tree

4 files changed

+28
-44
lines changed

4 files changed

+28
-44
lines changed

bin/src/ctl/request_builder.rs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,7 @@ impl CommandManager {
8080

8181
pub fn reload_configuration(&mut self, path: Option<String>) -> Result<(), CtlError> {
8282
debug!("Reloading configuration…");
83-
let path = match path {
84-
Some(p) => p,
85-
None => String::new(),
86-
};
87-
self.send_request(RequestType::ReloadConfiguration(path).into())
83+
self.send_request(RequestType::ReloadConfiguration(path.unwrap_or_default()).into())
8884
}
8985

9086
pub fn list_frontends(
@@ -248,12 +244,9 @@ impl CommandManager {
248244
address: address.into(),
249245
hostname,
250246
path: PathRule::from_cli_options(path_prefix, path_regex, path_equals),
251-
method: method.map(String::from),
247+
method,
252248
position: RulePosition::Tree.into(),
253-
tags: match tags {
254-
Some(tags) => tags,
255-
None => BTreeMap::new(),
256-
},
249+
tags: tags.unwrap_or_default(),
257250
})
258251
.into(),
259252
),
@@ -271,7 +264,7 @@ impl CommandManager {
271264
address: address.into(),
272265
hostname,
273266
path: PathRule::from_cli_options(path_prefix, path_regex, path_equals),
274-
method: method.map(String::from),
267+
method,
275268
..Default::default()
276269
})
277270
.into(),
@@ -296,12 +289,9 @@ impl CommandManager {
296289
address: address.into(),
297290
hostname,
298291
path: PathRule::from_cli_options(path_prefix, path_regex, path_equals),
299-
method: method.map(String::from),
292+
method,
300293
position: RulePosition::Tree.into(),
301-
tags: match tags {
302-
Some(tags) => tags,
303-
None => BTreeMap::new(),
304-
},
294+
tags: tags.unwrap_or_default(),
305295
})
306296
.into(),
307297
),
@@ -319,7 +309,7 @@ impl CommandManager {
319309
address: address.into(),
320310
hostname,
321311
path: PathRule::from_cli_options(path_prefix, path_regex, path_equals),
322-
method: method.map(String::from),
312+
method,
323313
..Default::default()
324314
})
325315
.into(),

bin/src/util.rs

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ use std::{
22
ffi::OsString,
33
fs::{File, read_link},
44
io::{Error as IoError, Write},
5-
os::{
6-
fd::{FromRawFd, OwnedFd},
7-
unix::io::RawFd,
8-
},
5+
os::{fd::BorrowedFd, unix::io::RawFd},
96
path::PathBuf,
107
};
118

@@ -52,37 +49,31 @@ pub enum UtilError {
5249
/// FD_CLOEXEC is set by default on every fd in Rust standard lib,
5350
/// so we need to remove the flag on the client, otherwise
5451
/// it won't be accessible
55-
pub fn enable_close_on_exec(fd: RawFd) -> Result<i32, UtilError> {
56-
let file_descriptor = fcntl(unsafe { OwnedFd::from_raw_fd(fd) }, FcntlArg::F_GETFD)
57-
.map_err(|err_no| UtilError::GetFlags(fd, err_no))?;
52+
pub fn enable_close_on_exec(raw_fd: RawFd) -> Result<i32, UtilError> {
53+
let fd = unsafe { BorrowedFd::borrow_raw(raw_fd) };
54+
let old_flags =
55+
fcntl(fd, FcntlArg::F_GETFD).map_err(|err_no| UtilError::GetFlags(raw_fd, err_no))?;
5856

59-
let mut new_flags = FdFlag::from_bits(file_descriptor).ok_or(UtilError::ConvertFlags(fd))?;
57+
let mut new_flags = FdFlag::from_bits(old_flags).ok_or(UtilError::ConvertFlags(raw_fd))?;
6058

6159
new_flags.insert(FdFlag::FD_CLOEXEC);
6260

63-
fcntl(
64-
unsafe { OwnedFd::from_raw_fd(fd) },
65-
FcntlArg::F_SETFD(new_flags),
66-
)
67-
.map_err(|err_no| UtilError::SetFlags(fd, err_no))
61+
fcntl(fd, FcntlArg::F_SETFD(new_flags)).map_err(|err_no| UtilError::SetFlags(raw_fd, err_no))
6862
}
6963

7064
/// FD_CLOEXEC is set by default on every fd in Rust standard lib,
7165
/// so we need to remove the flag on the client, otherwise
7266
/// it won't be accessible
73-
pub fn disable_close_on_exec(fd: RawFd) -> Result<i32, UtilError> {
74-
let old_flags = fcntl(unsafe { OwnedFd::from_raw_fd(fd) }, FcntlArg::F_GETFD)
75-
.map_err(|err_no| UtilError::GetFlags(fd, err_no))?;
67+
pub fn disable_close_on_exec(raw_fd: RawFd) -> Result<i32, UtilError> {
68+
let fd = unsafe { BorrowedFd::borrow_raw(raw_fd) };
69+
let old_flags =
70+
fcntl(fd, FcntlArg::F_GETFD).map_err(|err_no| UtilError::GetFlags(raw_fd, err_no))?;
7671

77-
let mut new_flags = FdFlag::from_bits(old_flags).ok_or(UtilError::ConvertFlags(fd))?;
72+
let mut new_flags = FdFlag::from_bits(old_flags).ok_or(UtilError::ConvertFlags(raw_fd))?;
7873

7974
new_flags.remove(FdFlag::FD_CLOEXEC);
8075

81-
fcntl(
82-
unsafe { OwnedFd::from_raw_fd(fd) },
83-
FcntlArg::F_SETFD(new_flags),
84-
)
85-
.map_err(|err_no| UtilError::SetFlags(fd, err_no))
76+
fcntl(fd, FcntlArg::F_SETFD(new_flags)).map_err(|err_no| UtilError::SetFlags(raw_fd, err_no))
8677
}
8778

8879
pub fn setup_metrics(config: &Config) -> Result<(), UtilError> {

command/src/proto/display.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,10 @@ impl ResponseContent {
157157
fn display(&self, json: bool) -> Result<(), DisplayError> {
158158
let content_type = match &self.content_type {
159159
Some(content_type) => content_type,
160-
None => return Ok(println!("No content")),
160+
None => {
161+
println!("No content");
162+
return Ok(());
163+
}
161164
};
162165

163166
if json {
@@ -876,7 +879,8 @@ pub fn print_certificates_with_validity(
876879
certs: &CertificatesWithFingerprints,
877880
) -> Result<(), DisplayError> {
878881
if certs.certs.is_empty() {
879-
return Ok(println!("No certificates match your request."));
882+
println!("No certificates match your request.");
883+
return Ok(());
880884
}
881885

882886
let mut table = Table::new();

lib/src/backends.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,12 +311,11 @@ impl BackendMap {
311311
let conn = borrowed.try_connect();
312312

313313
conn.map(|tcp_stream| (backend.clone(), tcp_stream))
314-
.map_err(|e| {
314+
.inspect_err(|_| {
315315
error!(
316316
"could not connect {} to {:?} using session {} ({} failures)",
317317
cluster_id, borrowed.address, sticky_session, borrowed.failures
318-
);
319-
e
318+
)
320319
})
321320
});
322321

0 commit comments

Comments
 (0)