Fix Rust formatting and clippy warnings

- Fix cargo fmt formatting issues
- Fix unused variable warnings by prefixing with underscore
- Fix if_not_else clippy warning
- Fix uninlined_format_args warnings
- Fix redundant closure warning
This commit is contained in:
Peter Steinberger 2025-06-18 15:24:17 +02:00
parent e3e92c646d
commit 4a0121eabe
3 changed files with 43 additions and 35 deletions

View file

@ -1047,9 +1047,12 @@ fn handle_session_kill(control_path: &Path, path: &str) -> Response<String> {
if let Ok(session_info) = serde_json::from_str::<serde_json::Value>(&content) { if let Ok(session_info) = serde_json::from_str::<serde_json::Value>(&content) {
if let Some(pid) = session_info.get("pid").and_then(serde_json::Value::as_u64) { if let Some(pid) = session_info.get("pid").and_then(serde_json::Value::as_u64) {
// Wait for the process to actually die // Wait for the process to actually die
for _ in 0..30 { // 30 * 100ms = 3 seconds max for _ in 0..30 {
// 30 * 100ms = 3 seconds max
// Only reap zombies for PTY sessions // Only reap zombies for PTY sessions
if let Some(spawn_type) = session_info.get("spawn_type").and_then(|s| s.as_str()) { if let Some(spawn_type) =
session_info.get("spawn_type").and_then(|s| s.as_str())
{
if spawn_type == "pty" { if spawn_type == "pty" {
sessions::reap_zombies(); sessions::reap_zombies();
} }

View file

@ -256,14 +256,14 @@ pub fn is_pid_alive(pid: u32) -> bool {
match output { match output {
Ok(output) => { Ok(output) => {
if !output.status.success() { if output.status.success() {
// Process doesn't exist
false
} else {
// Check if it's a zombie process (status starts with 'Z') // Check if it's a zombie process (status starts with 'Z')
let stat = String::from_utf8_lossy(&output.stdout); let stat = String::from_utf8_lossy(&output.stdout);
let stat = stat.trim(); let stat = stat.trim();
!stat.starts_with('Z') !stat.starts_with('Z')
} else {
// Process doesn't exist
false
} }
} }
Err(_) => false, Err(_) => false,
@ -272,8 +272,8 @@ pub fn is_pid_alive(pid: u32) -> bool {
/// Attempt to reap zombie children /// Attempt to reap zombie children
pub fn reap_zombies() { pub fn reap_zombies() {
use std::ptr;
use libc::{waitpid, WNOHANG, WUNTRACED}; use libc::{waitpid, WNOHANG, WUNTRACED};
use std::ptr;
loop { loop {
// Try to reap any zombie children // Try to reap any zombie children
@ -285,7 +285,7 @@ pub fn reap_zombies() {
} }
// Successfully reaped a zombie child // Successfully reaped a zombie child
eprintln!("Reaped zombie child with PID: {}", result); eprintln!("Reaped zombie child with PID: {result}");
} }
} }

View file

@ -103,7 +103,6 @@ fn spawn_via_socket_impl(command: &[String], working_dir: Option<&str>) -> Resul
fn spawn_via_pty(command: &[String], working_dir: Option<&str>) -> Result<String> { fn spawn_via_pty(command: &[String], working_dir: Option<&str>) -> Result<String> {
let session_id = Uuid::new_v4().to_string(); let session_id = Uuid::new_v4().to_string();
// Create PTY // Create PTY
let pty_result = openpty( let pty_result = openpty(
&Winsize { &Winsize {
@ -218,26 +217,26 @@ fn spawn_via_pty(command: &[String], working_dir: Option<&str>) -> Result<String
ForkResult::Child => { ForkResult::Child => {
// Child process - set up PTY and exec command // Child process - set up PTY and exec command
if let Err(e) = close(master_fd) { if let Err(_e) = close(master_fd) {
std::process::exit(1); std::process::exit(1);
} }
// Create new session // Create new session
if let Err(e) = setsid() { if let Err(_e) = setsid() {
std::process::exit(1); std::process::exit(1);
} }
// Set up stdin/stdout/stderr to use the slave PTY // Set up stdin/stdout/stderr to use the slave PTY
if let Err(e) = dup2(slave_fd, 0) { if let Err(_e) = dup2(slave_fd, 0) {
std::process::exit(1); std::process::exit(1);
} }
if let Err(e) = dup2(slave_fd, 1) { if let Err(_e) = dup2(slave_fd, 1) {
std::process::exit(1); std::process::exit(1);
} }
if let Err(e) = dup2(slave_fd, 2) { if let Err(_e) = dup2(slave_fd, 2) {
std::process::exit(1); std::process::exit(1);
} }
if let Err(e) = close(slave_fd) { if let Err(_e) = close(slave_fd) {
std::process::exit(1); std::process::exit(1);
} }
@ -255,7 +254,7 @@ fn spawn_via_pty(command: &[String], working_dir: Option<&str>) -> Result<String
dir.to_string() dir.to_string()
}; };
if let Err(e) = std::env::set_current_dir(&expanded_dir) { if let Err(_e) = std::env::set_current_dir(&expanded_dir) {
std::process::exit(1); std::process::exit(1);
} }
} }
@ -276,7 +275,6 @@ fn spawn_via_pty(command: &[String], working_dir: Option<&str>) -> Result<String
command.first().unwrap() command.first().unwrap()
}; };
let args: Vec<CString> = if command.is_empty() { let args: Vec<CString> = if command.is_empty() {
vec![CString::new(program)?] vec![CString::new(program)?]
} else { } else {
@ -287,12 +285,10 @@ fn spawn_via_pty(command: &[String], working_dir: Option<&str>) -> Result<String
.map_err(|e| anyhow::anyhow!("Invalid command argument: {}", e))? .map_err(|e| anyhow::anyhow!("Invalid command argument: {}", e))?
}; };
// Use execvp to execute the command // Use execvp to execute the command
match nix::unistd::execvp(&CString::new(program)?, &args) { match nix::unistd::execvp(&CString::new(program)?, &args) {
Ok(_) => { Ok(_) => {}
} Err(_e) => {
Err(e) => {
std::process::exit(127); // Standard exit code for command not found std::process::exit(127); // Standard exit code for command not found
} }
} }
@ -348,7 +344,9 @@ fn handle_pty_session(
let stdin_path_clone = stdin_path; let stdin_path_clone = stdin_path;
let session_id_clone = session_id.to_string(); let session_id_clone = session_id.to_string();
std::thread::spawn(move || { std::thread::spawn(move || {
if let Err(e) = handle_stdin_to_pty(master_fd_dup2, &stdin_path_clone, &session_id_clone) { if let Err(e) =
handle_stdin_to_pty(master_fd_dup2, &stdin_path_clone, &session_id_clone)
{
eprintln!("Stdin handler error: {e}"); eprintln!("Stdin handler error: {e}");
} }
// Clean up the duplicated fd when done // Clean up the duplicated fd when done
@ -451,20 +449,27 @@ fn handle_stdin_to_pty(master_fd: RawFd, stdin_path: &str, session_id: &str) ->
// Check for Ctrl+C and send SIGINT directly for responsiveness // Check for Ctrl+C and send SIGINT directly for responsiveness
if n == 1 && buffer[0] == 0x03 { if n == 1 && buffer[0] == 0x03 {
// Ctrl+C detected - send SIGINT to process group for immediate response // Ctrl+C detected - send SIGINT to process group for immediate response
let session_json_path = format!("{}/{}/session.json", let session_json_path = format!(
"{}/{}/session.json",
env::var("TTY_FWD_CONTROL_DIR").unwrap_or_else(|_| { env::var("TTY_FWD_CONTROL_DIR").unwrap_or_else(|_| {
format!("{}/.vibetunnel/control", env::var("HOME").unwrap_or_default()) format!(
"{}/.vibetunnel/control",
env::var("HOME").unwrap_or_default()
)
}), }),
session_id); session_id
);
if let Ok(content) = std::fs::read_to_string(&session_json_path) { if let Ok(content) = std::fs::read_to_string(&session_json_path) {
if let Ok(session_info) = serde_json::from_str::<serde_json::Value>(&content) { if let Ok(session_info) =
if let Some(pid) = session_info.get("pid").and_then(|p| p.as_u64()) { serde_json::from_str::<serde_json::Value>(&content)
{
if let Some(pid) = session_info.get("pid").and_then(serde_json::Value::as_u64) {
// Send SIGINT to the process group for immediate response // Send SIGINT to the process group for immediate response
unsafe { unsafe {
libc::kill(-(pid as i32), libc::SIGINT); libc::kill(-(pid as i32), libc::SIGINT);
} }
eprintln!("Sent SIGINT to process group {}", pid); eprintln!("Sent SIGINT to process group {pid}");
} }
} }
} }