|
| 1 | +use crate::fs; |
| 2 | +use crate::os::unix::fs::MetadataExt; |
1 | 3 | use crate::os::unix::process::{CommandExt, ExitStatusExt}; |
2 | 4 | use crate::panic::catch_unwind; |
3 | 5 | use crate::process::Command; |
| 6 | +use crate::sys::AsInner; |
4 | 7 |
|
5 | 8 | // Many of the other aspects of this situation, including heap alloc concurrency |
6 | 9 | // safety etc., are tested in tests/ui/process/process-panic-after-fork.rs |
7 | 10 |
|
| 11 | +/// Use dev + ino to uniquely identify a file |
| 12 | +fn md_file_id(md: &fs::Metadata) -> (u64, u64) { |
| 13 | + (md.dev(), md.ino()) |
| 14 | +} |
| 15 | + |
8 | 16 | #[test] |
9 | 17 | fn exitstatus_display_tests() { |
10 | 18 | // In practice this is the same on every Unix. |
@@ -74,3 +82,151 @@ fn test_command_fork_no_unwind() { |
74 | 82 | || signal == libc::SIGSEGV |
75 | 83 | ); |
76 | 84 | } |
| 85 | + |
| 86 | +/// For `Command`'s fd-related tests, we want to be sure they work both with exec |
| 87 | +/// and with `posix_spawn`. We test both the default which should use `posix_spawn` |
| 88 | +/// on supported platforms, and using `pre_exec` to force spawn using `exec`. |
| 89 | +mod fd_impls { |
| 90 | + use super::{assert_spawn_method, md_file_id}; |
| 91 | + use crate::fs; |
| 92 | + use crate::io::{self, Write}; |
| 93 | + use crate::os::fd::AsRawFd; |
| 94 | + use crate::os::unix::process::CommandExt; |
| 95 | + use crate::process::{Command, Stdio}; |
| 96 | + |
| 97 | + /// Check setting the child's stdin via `.fd`. |
| 98 | + pub fn test_stdin(use_exec: bool) { |
| 99 | + let (pipe_reader, mut pipe_writer) = io::pipe().unwrap(); |
| 100 | + |
| 101 | + let fd_num = libc::STDIN_FILENO; |
| 102 | + |
| 103 | + let mut cmd = Command::new("cat"); |
| 104 | + cmd.stdout(Stdio::piped()).fd(fd_num, pipe_reader); |
| 105 | + |
| 106 | + if use_exec { |
| 107 | + unsafe { |
| 108 | + cmd.pre_exec(|| Ok(())); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + let mut child = cmd.spawn().unwrap(); |
| 113 | + let mut stdout = child.stdout.take().unwrap(); |
| 114 | + |
| 115 | + assert_spawn_method(&cmd, use_exec); |
| 116 | + |
| 117 | + pipe_writer.write_all(b"Hello, world!").unwrap(); |
| 118 | + drop(pipe_writer); |
| 119 | + |
| 120 | + child.wait().unwrap().exit_ok().unwrap(); |
| 121 | + assert_eq!(io::read_to_string(&mut stdout).unwrap(), "Hello, world!"); |
| 122 | + } |
| 123 | + |
| 124 | + // FIXME: fails on android |
| 125 | + #[cfg_attr(not(target_os = "android"), should_panic)] |
| 126 | + /// Check that the last `.fd` mapping is preserved when there are conflicts. |
| 127 | + pub fn test_swap(use_exec: bool) { |
| 128 | + let (pipe_reader1, mut pipe_writer1) = io::pipe().unwrap(); |
| 129 | + let (pipe_reader2, mut pipe_writer2) = io::pipe().unwrap(); |
| 130 | + |
| 131 | + let num1 = pipe_reader1.as_raw_fd(); |
| 132 | + let num2 = pipe_reader2.as_raw_fd(); |
| 133 | + |
| 134 | + let mut cmd = Command::new("cat"); |
| 135 | + cmd.arg(format!("/dev/fd/{num1}")) |
| 136 | + .arg(format!("/dev/fd/{num2}")) |
| 137 | + .stdout(Stdio::piped()) |
| 138 | + .fd(num2, pipe_reader1) |
| 139 | + .fd(num1, pipe_reader2); |
| 140 | + |
| 141 | + if use_exec { |
| 142 | + unsafe { |
| 143 | + cmd.pre_exec(|| Ok(())); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + pipe_writer1.write_all(b"Hello from pipe 1!").unwrap(); |
| 148 | + drop(pipe_writer1); |
| 149 | + |
| 150 | + pipe_writer2.write_all(b"Hello from pipe 2!").unwrap(); |
| 151 | + drop(pipe_writer2); |
| 152 | + |
| 153 | + let mut child = cmd.spawn().unwrap(); |
| 154 | + let mut stdout = child.stdout.take().unwrap(); |
| 155 | + |
| 156 | + assert_spawn_method(&cmd, use_exec); |
| 157 | + |
| 158 | + child.wait().unwrap().exit_ok().unwrap(); |
| 159 | + // the second pipe's output is clobbered; this is expected. |
| 160 | + assert_eq!(io::read_to_string(&mut stdout).unwrap(), "Hello from pipe 1!"); |
| 161 | + } |
| 162 | + |
| 163 | + // ensure that the fd is properly closed in the parent, but only after the child is spawned. |
| 164 | + pub fn test_close_time(use_exec: bool) { |
| 165 | + let (_pipe_reader, pipe_writer) = io::pipe().unwrap(); |
| 166 | + |
| 167 | + let fd = pipe_writer.as_raw_fd(); |
| 168 | + let fd_path = format!("/dev/fd/{fd}"); |
| 169 | + |
| 170 | + let mut cmd = Command::new("true"); |
| 171 | + cmd.fd(123, pipe_writer); |
| 172 | + |
| 173 | + if use_exec { |
| 174 | + unsafe { |
| 175 | + cmd.pre_exec(|| Ok(())); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + // Get the identifier of the fd (metadata follows symlinks) |
| 180 | + let fd_id = md_file_id(&fs::metadata(&fd_path).expect("fd should be open")); |
| 181 | + |
| 182 | + cmd.spawn().unwrap().wait().unwrap().exit_ok().unwrap(); |
| 183 | + |
| 184 | + assert_spawn_method(&cmd, use_exec); |
| 185 | + |
| 186 | + // After the child is spawned, our fd should be closed |
| 187 | + match fs::metadata(&fd_path) { |
| 188 | + // Ok; fd exists but points to a different file |
| 189 | + Ok(md) => assert_ne!(md_file_id(&md), fd_id), |
| 190 | + // Ok; fd does not exist |
| 191 | + Err(_) => (), |
| 192 | + } |
| 193 | + } |
| 194 | +} |
| 195 | + |
| 196 | +#[test] |
| 197 | +fn fd_test_stdin() { |
| 198 | + fd_impls::test_stdin(false); |
| 199 | + fd_impls::test_stdin(true); |
| 200 | +} |
| 201 | + |
| 202 | +#[test] |
| 203 | +fn fd_test_swap() { |
| 204 | + fd_impls::test_swap(false); |
| 205 | + fd_impls::test_swap(true); |
| 206 | +} |
| 207 | + |
| 208 | +#[test] |
| 209 | +fn fd_test_close_time() { |
| 210 | + fd_impls::test_close_time(false); |
| 211 | + fd_impls::test_close_time(true); |
| 212 | +} |
| 213 | + |
| 214 | +#[track_caller] |
| 215 | +fn assert_spawn_method(cmd: &Command, use_exec: bool) { |
| 216 | + let used_posix_spawn = cmd.as_inner().get_last_spawn_was_posix_spawn().unwrap(); |
| 217 | + if use_exec { |
| 218 | + assert!(!used_posix_spawn, "posix_spawn used but exec was expected"); |
| 219 | + } else if cfg!(any( |
| 220 | + target_os = "freebsd", |
| 221 | + target_os = "illumos", |
| 222 | + all(target_os = "linux", target_env = "gnu"), |
| 223 | + all(target_os = "linux", target_env = "musl"), |
| 224 | + target_os = "nto", |
| 225 | + target_vendor = "apple", |
| 226 | + target_os = "cygwin", |
| 227 | + )) { |
| 228 | + assert!(used_posix_spawn, "platform supports posix_spawn but it wasn't used"); |
| 229 | + } else { |
| 230 | + assert!(!used_posix_spawn); |
| 231 | + } |
| 232 | +} |
0 commit comments