|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +#![cfg(unix)] |
| 10 | + |
| 11 | +use std::fs::OpenOptions; |
| 12 | +use std::os::fd::BorrowedFd; |
| 13 | +use std::os::unix::io::AsRawFd; |
| 14 | + |
| 15 | +use anyhow::Context; |
| 16 | +use nix::libc::STDERR_FILENO; |
| 17 | +use nix::libc::STDOUT_FILENO; |
| 18 | +use nix::poll::PollFd; |
| 19 | +use nix::poll::PollFlags; |
| 20 | +use nix::poll::poll; |
| 21 | + |
| 22 | +/// This probe is invasive. It requires writing bytes. We compile it |
| 23 | +/// out by default in favor of [`fd_looks_broken`]. |
| 24 | +#[cfg(feature = "stdio-write-probe")] |
| 25 | +pub(crate) fn is_stdout_broken_write_probe() -> bool { |
| 26 | + use std::os::fd::BorrowedFd; |
| 27 | + |
| 28 | + use nix::errno::Errno; |
| 29 | + use nix::unistd::write; |
| 30 | + let fd = unsafe { BorrowedFd::borrow_raw(nix::libc::STDOUT_FILENO) }; |
| 31 | + matches!(write(fd, b"\0"), Err(Errno::EPIPE | Errno::EBADF)) |
| 32 | +} |
| 33 | + |
| 34 | +/// Non-invasive: returns true if the fd looks broken (peer gone) or |
| 35 | +/// invalid. Based on |
| 36 | +/// https://stackoverflow.com/questions/9212243/linux-checking-if-a-socket-pipe-is-broken-without-doing-a-read-write. |
| 37 | +pub(crate) fn fd_looks_broken(fd_raw: i32) -> bool { |
| 38 | + // SAFETY: we only borrow for the duration of this call; `fd_raw` |
| 39 | + // must be a valid fd. |
| 40 | + let fd = unsafe { BorrowedFd::borrow_raw(fd_raw) }; |
| 41 | + |
| 42 | + // Request writable readiness and error/hangup notifications. |
| 43 | + let mut fds = [PollFd::new( |
| 44 | + fd, |
| 45 | + PollFlags::POLLOUT | PollFlags::POLLERR | PollFlags::POLLHUP, |
| 46 | + )]; |
| 47 | + |
| 48 | + // NOTE: 0u16 means "don't block" in this nix API. |
| 49 | + match poll(&mut fds, 0u16) { |
| 50 | + Ok(n) if n > 0 => { |
| 51 | + let re = fds[0].revents().unwrap_or(PollFlags::empty()); |
| 52 | + // On the write end of a pipe/pty, POLLHUP or POLLERR |
| 53 | + // means the reader is gone. |
| 54 | + re.intersects(PollFlags::POLLERR | PollFlags::POLLHUP) |
| 55 | + } |
| 56 | + Ok(_) => false, // no events → treat as not broken (regular |
| 57 | + // files often look like this) |
| 58 | + Err(_) => true, // e.g., EBADF → definitely broken/invalid |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +/// Returns true if stdout's endpoint appears gone (pipe/pty peer |
| 63 | +/// closed) or invalid. |
| 64 | +pub(crate) fn is_stdout_broken() -> bool { |
| 65 | + fd_looks_broken(STDOUT_FILENO) |
| 66 | +} |
| 67 | + |
| 68 | +/// Returns true if stderr's endpoint appears gone (pipe/pty peer |
| 69 | +/// closed) or invalid. |
| 70 | +pub(crate) fn is_stderr_broken() -> bool { |
| 71 | + fd_looks_broken(STDERR_FILENO) |
| 72 | +} |
| 73 | + |
| 74 | +/// Returns true if either stdout or stderr looks broken. |
| 75 | +pub(crate) fn any_stdio_broken() -> bool { |
| 76 | + is_stdout_broken() || is_stderr_broken() |
| 77 | +} |
| 78 | + |
| 79 | +/// Redirects stdout and stderr to the specified file. |
| 80 | +/// |
| 81 | +/// The file is opened in append mode and created if it doesn't exist. |
| 82 | +/// This permanently modifies the process's stdio streams. |
| 83 | +pub(crate) fn redirect_stdio_to_file(path: &str) -> anyhow::Result<()> { |
| 84 | + let file = OpenOptions::new() |
| 85 | + .create(true) |
| 86 | + .append(true) |
| 87 | + .open(path) |
| 88 | + .with_context(|| format!("failed to open log file: {}", path))?; |
| 89 | + let raw_fd = file.as_raw_fd(); |
| 90 | + // SAFETY: `raw_fd` is a valid file descriptor obtained from |
| 91 | + // `as_raw_fd()` on an open file. `STDOUT_FILENO` (`1`) and |
| 92 | + // `STDERR_FILENO` (`2`) are always valid file descriptor numbers. |
| 93 | + // `dup2` is safe to call with these valid file descriptors. |
| 94 | + unsafe { |
| 95 | + if nix::libc::dup2(raw_fd, STDOUT_FILENO) == -1 { |
| 96 | + anyhow::bail!( |
| 97 | + "failed to redirect stdout: {}", |
| 98 | + std::io::Error::last_os_error() |
| 99 | + ); |
| 100 | + } |
| 101 | + if nix::libc::dup2(raw_fd, STDERR_FILENO) == -1 { |
| 102 | + anyhow::bail!( |
| 103 | + "failed to redirect stderr: {}", |
| 104 | + std::io::Error::last_os_error() |
| 105 | + ); |
| 106 | + } |
| 107 | + } |
| 108 | + std::mem::forget(file); |
| 109 | + Ok(()) |
| 110 | +} |
| 111 | + |
| 112 | +/// Redirects stdout and stderr to a user-specific log file in /tmp. |
| 113 | +/// |
| 114 | +/// Creates a log file at `/tmp/{user}/monarch-process-exit-{pid}.log` |
| 115 | +/// and redirects stdio to it. The user directory is created if it |
| 116 | +/// doesn't exist. |
| 117 | +pub(crate) fn redirect_stdio_to_user_pid_file() -> anyhow::Result<()> { |
| 118 | + let user = std::env::var("USER").unwrap_or_else(|_| "unknown".to_string()); |
| 119 | + let pid = std::process::id(); |
| 120 | + let log_dir = format!("/tmp/{}", user); |
| 121 | + std::fs::create_dir_all(&log_dir)?; |
| 122 | + let path = format!("{}/monarch-process-exit-{}.log", log_dir, pid); |
| 123 | + redirect_stdio_to_file(&path)?; |
| 124 | + Ok(()) |
| 125 | +} |
| 126 | + |
| 127 | +/// Redirects stdio to a log file if stdout is broken. |
| 128 | +pub(crate) fn handle_broken_pipes() { |
| 129 | + if any_stdio_broken() { |
| 130 | + if redirect_stdio_to_user_pid_file().is_ok() { |
| 131 | + tracing::info!( |
| 132 | + "stdio for {} redirected due to broken pipe", |
| 133 | + std::process::id() |
| 134 | + ); |
| 135 | + } |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +#[cfg(all(unix, test))] |
| 140 | +mod tests { |
| 141 | + use nix::libc::STDERR_FILENO; |
| 142 | + use nix::libc::STDOUT_FILENO; |
| 143 | + use tempfile::TempDir; |
| 144 | + |
| 145 | + use super::*; |
| 146 | + |
| 147 | + struct StdioGuard { |
| 148 | + saved_stdout: i32, |
| 149 | + saved_stderr: i32, |
| 150 | + } |
| 151 | + |
| 152 | + impl StdioGuard { |
| 153 | + fn new() -> Self { |
| 154 | + // SAFETY: `STDOUT_FILENO` (`1`) and `STDERR_FILENO` (`2`) |
| 155 | + // are always valid file descriptor numbers. `dup()` is |
| 156 | + // safe to call on these standard descriptors and will |
| 157 | + // return new file descriptors pointing to the same files. |
| 158 | + unsafe { |
| 159 | + let saved_stdout = nix::libc::dup(STDOUT_FILENO); |
| 160 | + let saved_stderr = nix::libc::dup(STDERR_FILENO); |
| 161 | + Self { |
| 162 | + saved_stdout, |
| 163 | + saved_stderr, |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + impl Drop for StdioGuard { |
| 170 | + fn drop(&mut self) { |
| 171 | + // SAFETY: `saved_stdout` and `saved_stderr` are valid |
| 172 | + // file descriptors returned by `dup()` in `new()`. |
| 173 | + // `STDOUT_FILENO` and `STDERR_FILENO` are always valid |
| 174 | + // target descriptors. `dup2()` and `close()` are safe to |
| 175 | + // call with these valid fds. |
| 176 | + unsafe { |
| 177 | + nix::libc::dup2(self.saved_stdout, STDOUT_FILENO); |
| 178 | + nix::libc::dup2(self.saved_stderr, STDERR_FILENO); |
| 179 | + nix::libc::close(self.saved_stdout); |
| 180 | + nix::libc::close(self.saved_stderr); |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + #[test] |
| 186 | + fn test_is_stdout_broken_with_working_stdout() { |
| 187 | + assert!(!is_stdout_broken()); |
| 188 | + } |
| 189 | + |
| 190 | + #[test] |
| 191 | + fn test_redirect_stdio_to_file_creates_file() { |
| 192 | + let _guard = StdioGuard::new(); |
| 193 | + |
| 194 | + let temp_dir = TempDir::new().unwrap(); |
| 195 | + let log_path = temp_dir.path().join("test.log"); |
| 196 | + let path_str = log_path.to_str().unwrap(); |
| 197 | + |
| 198 | + assert!(redirect_stdio_to_file(path_str).is_ok()); |
| 199 | + assert!(log_path.exists()); |
| 200 | + } |
| 201 | +} |
0 commit comments