|
1 | 1 | use std::{ |
2 | 2 | io::{Read, Stdin}, |
3 | | - os::fd::AsRawFd, |
| 3 | + os::fd::{AsRawFd, RawFd}, |
4 | 4 | task::{Poll, ready}, |
5 | 5 | }; |
6 | 6 |
|
@@ -333,29 +333,44 @@ impl AsyncStdin { |
333 | 333 | if status == -1 { |
334 | 334 | return Err(std::io::Error::last_os_error()).context(AsyncifyStdinSnafu); |
335 | 335 | } |
| 336 | + } |
| 337 | + |
| 338 | + // From here on stdin is non-blocking, so every error path has to revert the flags itself. |
| 339 | + // `Drop` cannot do that for us, because it only runs once `Self` has been constructed, and |
| 340 | + // the flags are shared with the stdin of the shell we are returning to. |
| 341 | + // SAFETY: `stdin` is a handle to the process-wide standard input. Its file descriptor is |
| 342 | + // valid and open, and `Stdin` neither closes nor replaces it, so it keeps referring to the |
| 343 | + // same file description for the entire lifetime of the `AsyncFd`. |
| 344 | + let fd = match unsafe { AsyncFd::register(stdin) } { |
| 345 | + Ok(fd) => fd, |
| 346 | + Err(err) => { |
| 347 | + let (stdin, err) = err.into_parts(); |
| 348 | + revert_stdin_flags(stdin.as_raw_fd(), old_flags); |
| 349 | + return Err(err).context(AsyncFdStdinSnafu); |
| 350 | + } |
336 | 351 | }; |
337 | | - Ok(Self { |
338 | | - // SAFETY: `stdin` is a handle to the process-wide standard input. Its file |
339 | | - // descriptor is valid and open, and `Stdin` neither closes nor replaces it, so it |
340 | | - // keeps referring to the same file description for the entire lifetime of the |
341 | | - // `AsyncFd`. |
342 | | - fd: unsafe { AsyncFd::register(stdin) } |
343 | | - .map_err(|err| err.into_parts().1) |
344 | | - .context(AsyncFdStdinSnafu)?, |
345 | | - old_flags, |
346 | | - }) |
| 352 | + |
| 353 | + Ok(Self { fd, old_flags }) |
| 354 | + } |
| 355 | +} |
| 356 | + |
| 357 | +/// Reverts the file status flags of the stdin file descriptor `fd` to `old_flags`. |
| 358 | +/// |
| 359 | +/// A failure is only logged, because the callers either fail already or are being dropped. Panicking |
| 360 | +/// here would be counter-productive: while unwinding, it would abort the process and thereby skip |
| 361 | +/// restoring the terminal from raw mode, which is worse than a non-blocking stdin. |
| 362 | +fn revert_stdin_flags(fd: RawFd, old_flags: i32) { |
| 363 | + if unsafe { libc::fcntl(fd, libc::F_SETFL, old_flags) } == -1 { |
| 364 | + error!( |
| 365 | + "unable to revert stdin flags: {error}", |
| 366 | + error = std::io::Error::last_os_error() |
| 367 | + ); |
347 | 368 | } |
348 | 369 | } |
349 | 370 |
|
350 | 371 | impl Drop for AsyncStdin { |
351 | 372 | fn drop(&mut self) { |
352 | | - let status = unsafe { libc::fcntl(self.fd.as_raw_fd(), libc::F_SETFL, self.old_flags) }; |
353 | | - if status == -1 { |
354 | | - panic!( |
355 | | - "unable to revert stdin flags: {error}", |
356 | | - error = std::io::Error::last_os_error() |
357 | | - ); |
358 | | - } |
| 373 | + revert_stdin_flags(self.fd.as_raw_fd(), self.old_flags); |
359 | 374 | } |
360 | 375 | } |
361 | 376 |
|
|
0 commit comments