|
| 1 | +use once_cell::sync::Lazy; |
| 2 | +use std::process::Command; |
| 3 | +use std::sync::atomic::{AtomicI32, Ordering}; |
| 4 | +use std::thread; |
| 5 | +use std::os::unix::process::CommandExt; |
| 6 | +use libc; |
| 7 | + |
| 8 | +static FG_PGID: Lazy<AtomicI32> = Lazy::new(|| AtomicI32::new(0)); |
| 9 | + |
| 10 | +pub fn init_signal_handlers() { |
| 11 | + static START: std::sync::Once = std::sync::Once::new(); |
| 12 | + START.call_once(|| { |
| 13 | + // Spawn a thread to handle signals using signal-hook's iterator |
| 14 | + let _ = thread::spawn(|| { |
| 15 | + let mut signals = match signal_hook::iterator::Signals::new(&[ |
| 16 | + libc::SIGCHLD, |
| 17 | + libc::SIGINT, |
| 18 | + libc::SIGTSTP, |
| 19 | + ]) { |
| 20 | + Ok(s) => s, |
| 21 | + Err(e) => { |
| 22 | + eprintln!("Failed to register signal handlers: {}", e); |
| 23 | + return; |
| 24 | + } |
| 25 | + }; |
| 26 | + |
| 27 | + for signal in signals.forever() { |
| 28 | + match signal { |
| 29 | + libc::SIGCHLD => { |
| 30 | + // Reap any finished children (non-blocking) |
| 31 | + loop { |
| 32 | + let mut status: libc::c_int = 0; |
| 33 | + let pid = unsafe { libc::waitpid(-1, &mut status as *mut i32, libc::WNOHANG) }; |
| 34 | + if pid <= 0 { |
| 35 | + break; |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + libc::SIGINT => { |
| 40 | + let pgid = FG_PGID.load(Ordering::SeqCst); |
| 41 | + if pgid > 0 { |
| 42 | + unsafe { libc::kill(-(pgid as libc::pid_t), libc::SIGINT) }; |
| 43 | + } |
| 44 | + } |
| 45 | + libc::SIGTSTP => { |
| 46 | + let pgid = FG_PGID.load(Ordering::SeqCst); |
| 47 | + if pgid > 0 { |
| 48 | + unsafe { libc::kill(-(pgid as libc::pid_t), libc::SIGTSTP) }; |
| 49 | + } |
| 50 | + } |
| 51 | + _ => {} |
| 52 | + } |
| 53 | + } |
| 54 | + }); |
| 55 | + }); |
| 56 | +} |
| 57 | + |
| 58 | +pub fn spawn_and_wait(command_name: &str, args: &[String]) { |
| 59 | + // Build the command and ensure the child is placed into its own process group |
| 60 | + let mut cmd = Command::new(command_name); |
| 61 | + cmd.args(args) |
| 62 | + .stdin(std::process::Stdio::inherit()) |
| 63 | + .stdout(std::process::Stdio::inherit()) |
| 64 | + .stderr(std::process::Stdio::inherit()) |
| 65 | + .before_exec(|| { |
| 66 | + // create new process group for the child |
| 67 | + let r = unsafe { libc::setpgid(0, 0) }; |
| 68 | + if r != 0 { |
| 69 | + return Err(std::io::Error::last_os_error()); |
| 70 | + } |
| 71 | + Ok(()) |
| 72 | + }); |
| 73 | + |
| 74 | + match cmd.spawn() { |
| 75 | + Ok(mut child) => { |
| 76 | + let pid = child.id() as i32; |
| 77 | + FG_PGID.store(pid, Ordering::SeqCst); |
| 78 | + |
| 79 | + // Give terminal control to child process group |
| 80 | + unsafe { libc::tcsetpgrp(libc::STDIN_FILENO, pid) }; |
| 81 | + |
| 82 | + // Wait for the child to exit (blocking) |
| 83 | + let mut status: libc::c_int = 0; |
| 84 | + let _ = unsafe { libc::waitpid(pid, &mut status as *mut i32, 0) }; |
| 85 | + |
| 86 | + // Restore terminal control to the shell |
| 87 | + let shell_pgid = unsafe { libc::getpgrp() }; |
| 88 | + unsafe { libc::tcsetpgrp(libc::STDIN_FILENO, shell_pgid) }; |
| 89 | + FG_PGID.store(0, Ordering::SeqCst); |
| 90 | + |
| 91 | + // Attempt to reap the std::process::Child to avoid zombies |
| 92 | + let _ = child.try_wait(); |
| 93 | + } |
| 94 | + Err(e) => { |
| 95 | + eprintln!("trushell: command not found '{}': {}", command_name, e); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments