Skip to content
Open
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
3 changes: 3 additions & 0 deletions src/uu/setsid/src/setsid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,20 +169,23 @@ pub fn uu_app() -> Command {
.arg(
Arg::new("ctty")
.short('c')
.long("ctty")
.action(ArgAction::SetTrue)
.value_parser(ValueParser::bool())
.help("Take the current controlling terminal"),
)
.arg(
Arg::new("fork")
.short('f')
.long("fork")
.action(ArgAction::SetTrue)
.value_parser(ValueParser::bool())
.long_help("Always create a new process. By default this is only done if we are already a process group lead."),
)
.arg(
Arg::new("wait")
.short('w')
.long("wait")
.action(ArgAction::SetTrue)
.value_parser(ValueParser::bool())
.help("Wait for the command to finish and exit with its exit code."),
Expand Down
44 changes: 44 additions & 0 deletions tests/by-util/test_setsid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ mod unix {
.no_output();
}

#[test]
fn long_options() {
new_ucmd!()
.args(&["--fork", "--wait", "/usr/bin/false"])
.fails()
.code_is(1)
.no_output();
new_ucmd!()
.args(&["--wait", "/usr/bin/true"])
.succeeds()
.no_output();
}

#[test]
fn non_fork_returns_not_found_error() {
new_ucmd!()
Expand Down Expand Up @@ -93,6 +106,37 @@ mod unix {
.stderr_is("setsid: failed to set the controlling terminal: Permission denied\r\n");
}

#[cfg(target_os = "linux")]
#[test]
fn long_options_take_new_controlling_tty() {
let shell_cmd = format!(
"/usr/bin/cat /proc/self/stat; {} setsid --wait --ctty /usr/bin/cat /proc/self/stat",
get_tests_binary()
);

let cmd_result = UCommand::new()
.terminal_simulation(true)
.arg(&shell_cmd)
.succeeds();

let output = cmd_result.code_is(0).no_stderr().stdout_str();

// the controlling terminal is the 7th field of /proc/self/stat
let (before, after) = output
.split_once('\n')
.expect("expected 2 lines of output at least");
let before = before
.split_whitespace()
.nth(6)
.expect("unexpected stat format");
let after = after
.split_whitespace()
.nth(6)
.expect("unexpected stat format");

assert_ne!(before, after);
}

#[cfg(target_os = "linux")]
#[test]
fn unprivileged_user_can_take_new_controlling_tty() {
Expand Down
Loading