-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
On Linux, fix how the pid argument is passed to the `TIOCSPGRP` ioctl used by `tcsetgrp`. And, on Linux, it appears `tcgetpgrp` can return a pid of 0 when the fd is a pseudo-terminal device. This isn't documented behavior, and isn't compatible with rustix's current signaure for `tcgetpgrp` since it returns a `Pid` which can't be zero, so handle this situation by having it return `Errno::OPNOTSUPP` for now.
- Loading branch information
1 parent
cf2ad7f
commit bc4fa30
Showing
7 changed files
with
211 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use rustix::io::Errno; | ||
use rustix::termios::{tcgetpgrp, tcsetpgrp, Pid}; | ||
use tempfile::tempdir; | ||
|
||
#[cfg(feature = "fs")] | ||
#[test] | ||
fn pgrp_notty() { | ||
let tmpdir = tempdir().unwrap(); | ||
let fd = rustix::fs::open( | ||
tmpdir.path(), | ||
rustix::fs::OFlags::RDONLY, | ||
rustix::fs::Mode::empty(), | ||
) | ||
.unwrap(); | ||
|
||
// A file is not a tty. | ||
assert_eq!(tcgetpgrp(&fd), Err(Errno::NOTTY)); | ||
assert_eq!(tcsetpgrp(&fd, Pid::INIT), Err(Errno::NOTTY)); | ||
} | ||
|
||
// Disable on illumos where `tcgetattr` doesn't appear to support | ||
// pseudoterminals. | ||
#[cfg(not(target_os = "illumos"))] | ||
#[cfg(feature = "pty")] | ||
#[test] | ||
fn pgrp_pseudoterminal() { | ||
use rustix::pty::*; | ||
use rustix::termios::*; | ||
|
||
let pty = match openpt(OpenptFlags::NOCTTY) { | ||
Ok(pty) => pty, | ||
Err(rustix::io::Errno::NOSYS) => return, | ||
Err(e) => Err(e).unwrap(), | ||
}; | ||
|
||
// Linux's `tcgetpgrp` returns 0 here, which is not documented, so rustix | ||
// translates it into `OPNOTSUPP`. | ||
#[cfg(linux_kernel)] | ||
assert_eq!(tcgetpgrp(&pty), Err(rustix::io::Errno::OPNOTSUPP)); | ||
|
||
// FreeBSD's `tcgetpgrp` returns 100000 here, or presumably some other | ||
// number if that number is already taken, which is documented behavior, | ||
// but impossible to test for reliably. | ||
#[cfg(not(linux_kernel))] | ||
assert!(matches!(tcgetpgrp(&pty), Ok(_))); | ||
|
||
// We shouldn't be able to set the process group to pid 1. | ||
match tcsetpgrp(&pty, rustix::termios::Pid::INIT).unwrap_err() { | ||
#[cfg(freebsdlike)] | ||
rustix::io::Errno::PERM => {} | ||
#[cfg(any(apple, linux_kernel))] | ||
rustix::io::Errno::NOTTY => {} | ||
err => Err(err).unwrap(), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use rustix::io::Errno; | ||
use rustix::termios::tcgetsid; | ||
use tempfile::tempdir; | ||
|
||
#[cfg(feature = "fs")] | ||
#[test] | ||
fn sid_notty() { | ||
let tmpdir = tempdir().unwrap(); | ||
let fd = rustix::fs::open( | ||
tmpdir.path(), | ||
rustix::fs::OFlags::RDONLY, | ||
rustix::fs::Mode::empty(), | ||
) | ||
.unwrap(); | ||
|
||
// A file is not a tty. | ||
assert_eq!(tcgetsid(&fd), Err(Errno::NOTTY)); | ||
} | ||
|
||
#[cfg(all(feature = "stdio", feature = "process"))] | ||
#[test] | ||
fn sid_match() { | ||
match tcgetsid(rustix::stdio::stdin()) { | ||
Ok(sid) => assert_eq!(sid, rustix::process::getsid(None).unwrap()), | ||
Err(_err) => {} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters