Skip to content

Commit 035e7cc

Browse files
committed
Update nix crate
Signed-off-by: Maksym Pavlenko <[email protected]>
1 parent a0e042a commit 035e7cc

File tree

8 files changed

+44
-16
lines changed

8 files changed

+44
-16
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ async-trait = "0.1.52"
2828
futures = "0.3.19"
2929
libc = "0.2.112"
3030
log = "0.4"
31-
nix = "0.26"
31+
nix = "0.27"
3232
oci-spec = "0.6"
3333
os_pipe = "1.1"
3434
prost = "0.12"

crates/runc-shim/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ containerd-shim = { path = "../shim", version = "0.5.0", features = ["async"] }
2929
crossbeam = "0.8.1"
3030
libc.workspace = true
3131
log.workspace = true
32-
nix.workspace = true
32+
nix = { workspace = true, features = ["socket", "uio", "term"] }
3333
oci-spec.workspace = true
3434
runc = { path = "../runc", version = "0.2.0", features = ["async"] }
3535
serde.workspace = true

crates/runc-shim/src/common.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,18 @@
1414
limitations under the License.
1515
*/
1616

17-
use std::{env, fs::File, io::IoSliceMut, ops::Deref, os::unix::io::RawFd, path::Path, sync::Arc};
17+
use std::{
18+
env,
19+
fs::File,
20+
io::IoSliceMut,
21+
ops::Deref,
22+
os::{
23+
fd::{AsRawFd, FromRawFd, OwnedFd},
24+
unix::io::RawFd,
25+
},
26+
path::Path,
27+
sync::Arc,
28+
};
1829

1930
use containerd_shim::{
2031
api::{ExecProcessRequest, Options},
@@ -176,7 +187,7 @@ pub fn create_runc(
176187
#[derive(Default)]
177188
pub(crate) struct CreateConfig {}
178189

179-
pub fn receive_socket(stream_fd: RawFd) -> containerd_shim::Result<RawFd> {
190+
pub fn receive_socket(stream_fd: RawFd) -> containerd_shim::Result<OwnedFd> {
180191
let mut buf = [0u8; 4096];
181192
let mut iovec = [IoSliceMut::new(&mut buf)];
182193
let mut space = cmsg_space!([RawFd; 2]);
@@ -201,13 +212,17 @@ pub fn receive_socket(stream_fd: RawFd) -> containerd_shim::Result<RawFd> {
201212
warn!("failed to get path from array {}", e);
202213
"".to_string()
203214
});
215+
216+
let fd = unsafe { OwnedFd::from_raw_fd(fds[0]) };
217+
204218
let path = path.trim_matches(char::from(0));
205219
debug!(
206220
"copy_console: console socket get path: {}, fd: {}",
207-
path, &fds[0]
221+
path,
222+
fd.as_raw_fd(),
208223
);
209-
tcgetattr(fds[0])?;
210-
Ok(fds[0])
224+
tcgetattr(&fd)?;
225+
Ok(fd)
211226
}
212227

213228
pub fn has_shared_pid_namespace(spec: &Spec) -> bool {

crates/runc-shim/src/runc.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616

1717
use std::{
1818
convert::TryFrom,
19-
os::unix::{
20-
io::{AsRawFd, FromRawFd, RawFd},
21-
prelude::ExitStatusExt,
19+
os::{
20+
fd::{IntoRawFd, OwnedFd},
21+
unix::{
22+
io::{AsRawFd, FromRawFd},
23+
prelude::ExitStatusExt,
24+
},
2225
},
2326
path::{Path, PathBuf},
2427
process::ExitStatus,
@@ -479,8 +482,8 @@ async fn copy_console(
479482
) -> Result<Console> {
480483
debug!("copy_console: waiting for runtime to send console fd");
481484
let stream = console_socket.accept().await?;
482-
let fd = asyncify(move || -> Result<RawFd> { receive_socket(stream.as_raw_fd()) }).await?;
483-
let f = unsafe { File::from_raw_fd(fd) };
485+
let fd = asyncify(move || -> Result<OwnedFd> { receive_socket(stream.as_raw_fd()) }).await?;
486+
let f = unsafe { File::from_raw_fd(fd.into_raw_fd()) };
484487
if !stdio.stdin.is_empty() {
485488
debug!("copy_console: pipe stdin to console");
486489
let console_stdin = f

crates/runc/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ docs = []
1818
[dependencies]
1919
libc.workspace = true
2020
log.workspace = true
21-
nix.workspace = true
21+
nix = { workspace = true, features = ["user", "fs"] }
2222
oci-spec.workspace = true
2323
os_pipe.workspace = true
2424
path-absolutize = "3.0.11"

crates/shim/Cargo.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,13 @@ go-flag = "0.1.0"
3838
lazy_static = "1.4.0"
3939
libc.workspace = true
4040
log = { workspace = true, features = ["std"] }
41-
nix.workspace = true
41+
nix = { workspace = true, features = [
42+
"ioctl",
43+
"fs",
44+
"socket",
45+
"signal",
46+
"mount",
47+
] }
4248
oci-spec.workspace = true
4349
page_size = "0.6.0"
4450
prctl = "1.0.0"

crates/shim/src/synchronous/publisher.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@ use crate::util::connect;
3030
use crate::{
3131
error::Result,
3232
util::{convert_to_any, timestamp},
33-
Error,
3433
};
3534

35+
#[cfg(not(target_os = "macos"))] // Prevent unused warning.
36+
use crate::Error;
37+
3638
#[cfg(windows)]
3739
const RETRY_COUNT: i32 = 3;
3840

crates/shim/src/util.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ impl From<JsonOptions> for Options {
101101

102102
#[cfg(unix)]
103103
pub fn connect(address: impl AsRef<str>) -> Result<RawFd> {
104+
use std::os::fd::IntoRawFd;
105+
104106
use nix::{sys::socket::*, unistd::close};
105107

106108
let unix_addr = UnixAddr::new(address.as_ref())?;
@@ -112,7 +114,7 @@ pub fn connect(address: impl AsRef<str>) -> Result<RawFd> {
112114
#[cfg(not(target_os = "linux"))]
113115
const SOCK_CLOEXEC: SockFlag = SockFlag::empty();
114116

115-
let fd = socket(AddressFamily::Unix, SockType::Stream, SOCK_CLOEXEC, None)?;
117+
let fd = socket(AddressFamily::Unix, SockType::Stream, SOCK_CLOEXEC, None)?.into_raw_fd();
116118

117119
// MacOS doesn't support atomic creation of a socket descriptor with `SOCK_CLOEXEC` flag,
118120
// so there is a chance of leak if fork + exec happens in between of these calls.

0 commit comments

Comments
 (0)