|
| 1 | +use libproc::libproc::file_info::pidfdinfo; |
| 2 | +use libproc::libproc::file_info::{ListFDs, ProcFDType}; |
| 3 | +use libproc::libproc::net_info::{SocketFDInfo, SocketInfoKind}; |
| 4 | +use libproc::libproc::proc_pid::{listpidinfo, pidinfo}; |
| 5 | +use libproc::libproc::task_info::TaskAllInfo; |
| 6 | +use libproc::processes::{pids_by_type, ProcFilter}; |
| 7 | +use log::{debug, info, warn}; |
| 8 | +use nix::sys::signal::{self, Signal}; |
| 9 | +use nix::unistd::Pid; |
| 10 | +use std::ffi::CStr; |
| 11 | +use std::io; |
| 12 | + |
| 13 | +/// Collect information about all processes. |
| 14 | +/// |
| 15 | +/// # Returns |
| 16 | +/// |
| 17 | +/// A vector containing the information of all processes. |
| 18 | +fn collect_proc() -> Vec<TaskAllInfo> { |
| 19 | + let mut base_procs = Vec::new(); |
| 20 | + |
| 21 | + if let Ok(procs) = pids_by_type(ProcFilter::All) { |
| 22 | + for p in procs { |
| 23 | + if let Ok(task) = pidinfo::<TaskAllInfo>(p as i32, 0) { |
| 24 | + base_procs.push(task); |
| 25 | + } |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + base_procs |
| 30 | +} |
| 31 | + |
| 32 | +/// Kill processes listening on the specified port. |
| 33 | +/// |
| 34 | +/// # Arguments |
| 35 | +/// |
| 36 | +/// * `port` - The port number to kill processes listening on. |
| 37 | +/// |
| 38 | +/// # Returns |
| 39 | +/// |
| 40 | +/// A `Result` containing a boolean value. If true, at least one process was killed; otherwise, false. |
| 41 | +pub fn kill_processes_by_port(port: u16) -> Result<bool, io::Error> { |
| 42 | + let process_infos = collect_proc(); |
| 43 | + let mut killed = false; |
| 44 | + |
| 45 | + for task in process_infos { |
| 46 | + let pid = task.pbsd.pbi_pid as i32; |
| 47 | + let mut kill_process = false; |
| 48 | + |
| 49 | + let fds = listpidinfo::<ListFDs>(pid, task.pbsd.pbi_nfiles as usize); |
| 50 | + if let Ok(fds) = fds { |
| 51 | + for fd in fds { |
| 52 | + if let ProcFDType::Socket = fd.proc_fdtype.into() { |
| 53 | + if let Ok(socket) = pidfdinfo::<SocketFDInfo>(pid, fd.proc_fd) { |
| 54 | + match socket.psi.soi_kind.into() { |
| 55 | + SocketInfoKind::In => { |
| 56 | + if socket.psi.soi_protocol == libc::IPPROTO_UDP { |
| 57 | + let info = unsafe { socket.psi.soi_proto.pri_in }; |
| 58 | + let local_port = u16::from_be(info.insi_lport as u16); |
| 59 | + if local_port == port { |
| 60 | + kill_process = true; |
| 61 | + break; |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + SocketInfoKind::Tcp => { |
| 66 | + let info = unsafe { socket.psi.soi_proto.pri_tcp }; |
| 67 | + let local_port = u16::from_be(info.tcpsi_ini.insi_lport as u16); |
| 68 | + if local_port == port { |
| 69 | + kill_process = true; |
| 70 | + break; |
| 71 | + } |
| 72 | + } |
| 73 | + _ => (), |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + if kill_process { |
| 81 | + debug!("Found process with PID {}", pid); |
| 82 | + let pid = Pid::from_raw(pid); |
| 83 | + let cmd = unsafe { |
| 84 | + CStr::from_ptr(task.pbsd.pbi_comm.as_ptr()) |
| 85 | + .to_string_lossy() |
| 86 | + .into_owned() |
| 87 | + }; |
| 88 | + |
| 89 | + if cmd.starts_with("com.docker") { |
| 90 | + warn!("Warning: Found Docker. You might need to stop the container manually."); |
| 91 | + } else { |
| 92 | + info!("Killing process with PID {}", pid); |
| 93 | + match signal::kill(pid, Signal::SIGKILL) { |
| 94 | + Ok(_) => { |
| 95 | + killed = true; |
| 96 | + } |
| 97 | + Err(e) => { |
| 98 | + return Err(io::Error::new( |
| 99 | + io::ErrorKind::Other, |
| 100 | + format!("Failed to kill process {}: {}", pid, e), |
| 101 | + )); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + Ok(killed) |
| 109 | +} |
0 commit comments