Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions crates/host_env/src/fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ pub use libc::F_GETPATH;
#[cfg(target_os = "linux")]
pub use libc::{FICLONE, FICLONERANGE};

pub fn normalize_ioctl_request(request: i64) -> libc::c_ulong {
(request as u32) as libc::c_ulong
}

pub fn fcntl_int(fd: i32, cmd: i32, arg: i32) -> io::Result<i32> {
unsafe { libc::fcntl(fd, cmd, arg) }.check_libc_neg()
}
Expand Down Expand Up @@ -143,6 +139,23 @@ pub enum LockfError {
Io(io::Error),
}

#[cfg(not(any(target_os = "wasi", target_os = "redox")))]
pub fn lockf_type(cmd: i32) -> Result<libc::c_int, LockfError> {
let l_type = if cmd == libc::LOCK_UN {
libc::F_UNLCK
} else if (cmd & libc::LOCK_SH) != 0 {
libc::F_RDLCK
} else if (cmd & libc::LOCK_EX) != 0 {
libc::F_WRLCK
} else {
return Err(LockfError::InvalidCmd);
};

// Lock constants are c_short on BSD-family targets and c_int on Linux.
#[allow(clippy::unnecessary_cast)]
Ok(l_type as libc::c_int)
}

#[cfg(not(any(target_os = "wasi", target_os = "redox")))]
pub fn lockf(fd: i32, cmd: i32, len: i64, start: i64, whence: i32) -> Result<i32, LockfError> {
fn convert_field<T, U>(value: T) -> Result<U, LockfError>
Expand All @@ -155,19 +168,11 @@ pub fn lockf(fd: i32, cmd: i32, len: i64, start: i64, whence: i32) -> Result<i32
.map_err(|err| LockfError::Overflow(err.to_string()))
}

let l_type = if cmd == libc::LOCK_UN {
libc::F_UNLCK
} else if (cmd & libc::LOCK_SH) != 0 {
libc::F_RDLCK
} else if (cmd & libc::LOCK_EX) != 0 {
libc::F_WRLCK
} else {
return Err(LockfError::InvalidCmd);
};
let l_type = lockf_type(cmd)?;

let lock = libc::flock {
l_type: convert_field(l_type)?,
l_whence: convert_field(whence)?,
l_whence: whence as _,
l_start: convert_field(start)?,
l_len: convert_field(len)?,
..unsafe { core::mem::zeroed() }
Expand Down
18 changes: 13 additions & 5 deletions crates/host_env/src/termios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,17 +181,25 @@ pub fn tcgetwinsize(fd: i32) -> std::io::Result<(u16, u16)> {
Ok((size.ws_row, size.ws_col))
}

pub fn tcsetwinsize(fd: i32, row: u16, col: u16) -> std::io::Result<()> {
pub enum SetWinSizeError {
Io(std::io::Error),
OutOfRange,
}

pub fn tcsetwinsize(fd: i32, row: libc::c_long, col: libc::c_long) -> Result<(), SetWinSizeError> {
let mut size: libc::winsize = unsafe { core::mem::zeroed() };
let ret = unsafe { libc::ioctl(fd, TIOCGWINSZ as _, &mut size) };
if ret < 0 {
return Err(std::io::Error::last_os_error());
return Err(SetWinSizeError::Io(std::io::Error::last_os_error()));
}
size.ws_row = row as _;
size.ws_col = col as _;
if libc::c_long::from(size.ws_row) != row || libc::c_long::from(size.ws_col) != col {
return Err(SetWinSizeError::OutOfRange);
}
size.ws_row = row;
size.ws_col = col;
let ret = unsafe { libc::ioctl(fd, TIOCSWINSZ as _, &size) };
if ret < 0 {
return Err(std::io::Error::last_os_error());
return Err(SetWinSizeError::Io(std::io::Error::last_os_error()));
}
Ok(())
}
Loading
Loading