1- use crate :: cli:: Mode ;
21use crate :: docker:: DockerContainer ;
32#[ cfg( target_os = "linux" ) ]
43use crate :: linux:: find_target_processes;
54#[ cfg( target_os = "macos" ) ]
65use crate :: macos:: find_target_processes;
7- use log:: info;
8- use nix:: sys:: signal:: { kill, Signal } ;
9- use nix:: unistd:: Pid ;
10- use std:: io:: Error ;
11-
12- #[ derive( Debug ) ]
13- pub struct NativeProcess {
14- /// System native process ID.
15- pub pid : Pid ,
16- pub name : String ,
17- }
6+ #[ cfg( target_os = "windows" ) ]
7+ use crate :: windows:: find_target_processes;
8+ use crate :: { cli:: Mode , signal:: KillportSignal } ;
9+ use std:: { fmt:: Display , io:: Error } ;
1810
1911/// Interface for killable targets such as native process and docker container.
2012pub trait Killable {
21- fn kill ( & self , signal : Signal ) -> Result < bool , Error > ;
22- fn get_type ( & self ) -> String ;
13+ fn kill ( & self , signal : KillportSignal ) -> Result < bool , Error > ;
14+
15+ fn get_type ( & self ) -> KillableType ;
16+
2317 fn get_name ( & self ) -> String ;
2418}
2519
26- impl Killable for NativeProcess {
27- /// Entry point to kill the linux native process.
28- ///
29- /// # Arguments
30- ///
31- /// * `signal` - A enum value representing the signal type.
32- fn kill ( & self , signal : Signal ) -> Result < bool , Error > {
33- info ! ( "Killing process '{}' with PID {}" , self . name, self . pid) ;
34-
35- kill ( self . pid , signal) . map ( |_| true ) . map_err ( |e| {
36- Error :: new (
37- std:: io:: ErrorKind :: Other ,
38- format ! (
39- "Failed to kill process '{}' with PID {}: {}" ,
40- self . name, self . pid, e
41- ) ,
42- )
43- } )
44- }
45-
46- /// Returns the type of the killable target.
47- ///
48- /// This method is used to identify the type of the target (either a native process or a Docker container)
49- /// that is being handled. This information can be useful for logging, error handling, or other needs
50- /// where type of the target is relevant.
51- ///
52- /// # Returns
53- ///
54- /// * `String` - A string that describes the type of the killable target. For a `NativeProcess` it will return "process",
55- /// and for a `DockerContainer` it will return "container".
56- fn get_type ( & self ) -> String {
57- "process" . to_string ( )
58- }
20+ #[ derive( Debug , Clone , PartialEq , Eq ) ]
21+ pub enum KillableType {
22+ Process ,
23+ Container ,
24+ }
5925
60- fn get_name ( & self ) -> String {
61- self . name . to_string ( )
26+ impl Display for KillableType {
27+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
28+ f. write_str ( match self {
29+ KillableType :: Process => "process" ,
30+ KillableType :: Container => "container" ,
31+ } )
6232 }
6333}
6434
@@ -68,10 +38,8 @@ impl Killable for DockerContainer {
6838 /// # Arguments
6939 ///
7040 /// * `signal` - A enum value representing the signal type.
71- fn kill ( & self , signal : Signal ) -> Result < bool , Error > {
72- if let Err ( err) = Self :: kill_container ( & self . name , signal) {
73- return Err ( err) ;
74- }
41+ fn kill ( & self , signal : KillportSignal ) -> Result < bool , Error > {
42+ Self :: kill_container ( & self . name , signal) ?;
7543
7644 Ok ( true )
7745 }
@@ -84,10 +52,10 @@ impl Killable for DockerContainer {
8452 ///
8553 /// # Returns
8654 ///
87- /// * `String` - A string that describes the type of the killable target. For a `NativeProcess ` it will return "process",
55+ /// * `String` - A string that describes the type of the killable target. For a `UnixProcess ` it will return "process",
8856 /// and for a `DockerContainer` it will return "container".
89- fn get_type ( & self ) -> String {
90- "container" . to_string ( )
57+ fn get_type ( & self ) -> KillableType {
58+ KillableType :: Container
9159 }
9260
9361 fn get_name ( & self ) -> String {
@@ -104,10 +72,10 @@ pub trait KillportOperations {
10472 fn kill_service_by_port (
10573 & self ,
10674 port : u16 ,
107- signal : Signal ,
75+ signal : KillportSignal ,
10876 mode : Mode ,
10977 dry_run : bool ,
110- ) -> Result < Vec < ( String , String ) > , Error > ;
78+ ) -> Result < Vec < ( KillableType , String ) > , Error > ;
11179}
11280
11381pub struct Killport ;
@@ -133,9 +101,10 @@ impl KillportOperations for Killport {
133101
134102 for process in target_processes {
135103 // Check if the process name contains 'docker' and skip if in docker mode
136- if docker_present && process. name . to_lowercase ( ) . contains ( "docker" ) {
104+ if docker_present && process. get_name ( ) . to_lowercase ( ) . contains ( "docker" ) {
137105 continue ;
138106 }
107+
139108 target_killables. push ( Box :: new ( process) ) ;
140109 }
141110 }
@@ -166,10 +135,10 @@ impl KillportOperations for Killport {
166135 fn kill_service_by_port (
167136 & self ,
168137 port : u16 ,
169- signal : Signal ,
138+ signal : KillportSignal ,
170139 mode : Mode ,
171140 dry_run : bool ,
172- ) -> Result < Vec < ( String , String ) > , Error > {
141+ ) -> Result < Vec < ( KillableType , String ) > , Error > {
173142 let mut results = Vec :: new ( ) ;
174143 let target_killables = self . find_target_killables ( port, mode) ?; // Use the existing function to find targets
175144
@@ -179,7 +148,7 @@ impl KillportOperations for Killport {
179148 results. push ( ( killable. get_type ( ) , killable. get_name ( ) ) ) ;
180149 } else {
181150 // In actual mode, attempt to kill the entity and collect its information if successful
182- if killable. kill ( signal) ? {
151+ if killable. kill ( signal. clone ( ) ) ? {
183152 results. push ( ( killable. get_type ( ) , killable. get_name ( ) ) ) ;
184153 }
185154 }
0 commit comments