Fix TIOCSCTTY usage for cross-platform compatibility

- Use proper type casting for ioctl on both Linux and non-Linux platforms
- Separate platform-specific code blocks for clarity
- Fix potential clippy warnings on Linux CI
This commit is contained in:
Peter Steinberger 2025-06-18 14:25:18 +02:00
parent 5f067c7a80
commit 4cbbdf29fc

View file

@ -75,14 +75,23 @@ unsafe fn login_tty_compat(fd: i32) -> Result<(), Error> {
// Make the tty our controlling terminal
#[cfg(target_os = "linux")]
let tiocsctty = TIOCSCTTY;
#[cfg(not(target_os = "linux"))]
let tiocsctty = libc::TIOCSCTTY;
{
if libc::ioctl(fd, TIOCSCTTY as libc::c_ulong, 0) == -1 {
// Try without forcing
if libc::ioctl(fd, TIOCSCTTY as libc::c_ulong, 1) == -1 {
return Err(Error::msg("ioctl TIOCSCTTY failed"));
}
}
}
if libc::ioctl(fd, tiocsctty.try_into().unwrap_or(0x540E), 0) == -1 {
// Try without forcing
if libc::ioctl(fd, tiocsctty.try_into().unwrap_or(0x540E), 1) == -1 {
return Err(Error::msg("ioctl TIOCSCTTY failed"));
#[cfg(not(target_os = "linux"))]
{
// Use the libc constant directly on non-Linux platforms
if libc::ioctl(fd, libc::TIOCSCTTY as libc::c_ulong, 0) == -1 {
// Try without forcing
if libc::ioctl(fd, libc::TIOCSCTTY as libc::c_ulong, 1) == -1 {
return Err(Error::msg("ioctl TIOCSCTTY failed"));
}
}
}