1
+ use crate :: common:: { resolve_pid, MaybeHasPid } ;
2
+ use crate :: { Pid , ProcCtlError , ProcCtlResult } ;
1
3
use std:: path:: PathBuf ;
2
4
use std:: process:: Child ;
3
5
use std:: sync:: Mutex ;
4
- // Switch to `use std::sync::OnceLock;` on the next Rust release https://github.com/rust-lang/rust/issues/74465
5
- use crate :: common:: { resolve_pid, MaybeHasPid } ;
6
- use crate :: { Pid , ProcCtlError , ProcCtlResult } ;
7
- use once_cell:: sync:: OnceCell ;
8
- use sysinfo:: { PidExt , Process , ProcessExt , ProcessRefreshKind , RefreshKind , System , SystemExt } ;
6
+ use std:: sync:: OnceLock ;
7
+ use sysinfo:: { Process , ProcessRefreshKind , RefreshKind , System } ;
9
8
10
9
/// Information about a process
11
10
#[ derive( Debug , Clone ) ]
@@ -15,21 +14,22 @@ pub struct ProcInfo {
15
14
/// The command used to launch the process
16
15
pub cmd : Vec < String > ,
17
16
/// The path to the executable the process is running
18
- pub exe : PathBuf ,
17
+ pub exe : Option < PathBuf > ,
19
18
/// The process ID
20
19
pub pid : Pid ,
21
20
/// Parent process ID if relevant
22
21
pub parent : Option < Pid > ,
23
22
/// Environment variables available to the process
24
23
pub env : Vec < String > ,
25
24
/// The current working directory of the process
26
- pub cwd : PathBuf ,
25
+ pub cwd : Option < PathBuf > ,
27
26
}
28
27
29
28
/// Get information about a process
30
29
#[ derive( Debug ) ]
31
30
pub struct ProcQuery {
32
31
process_id : Option < Pid > ,
32
+ name : Option < String > ,
33
33
min_num_children : Option < usize > ,
34
34
}
35
35
@@ -38,18 +38,27 @@ impl ProcQuery {
38
38
pub fn new ( ) -> Self {
39
39
ProcQuery {
40
40
process_id : None ,
41
+ name : None ,
41
42
min_num_children : None ,
42
43
}
43
44
}
44
45
45
46
/// Set the process ID to match
46
47
///
47
- /// Either this function or ` process_id_from_child` are required to be called before the query is usable.
48
+ /// One of this, [ProcQuery::process_name] or [ProcQuery:: process_id_from_child] must be called before the query is usable.
48
49
pub fn process_id ( mut self , pid : Pid ) -> Self {
49
50
self . process_id = Some ( pid) ;
50
51
self
51
52
}
52
53
54
+ /// Set the process name to match
55
+ ///
56
+ /// One of this, [ProcQuery::process_id] or [ProcQuery::process_id_from_child] must be called before the query is usable.
57
+ pub fn process_name ( mut self , name : impl AsRef < str > ) -> Self {
58
+ self . name = Some ( name. as_ref ( ) . to_string ( ) ) ;
59
+ self
60
+ }
61
+
53
62
/// Get the process ID of a child process
54
63
///
55
64
/// Either this function or `process_id` are required to be called before the query is usable.
@@ -63,6 +72,34 @@ impl ProcQuery {
63
72
self
64
73
}
65
74
75
+ /// List all processes matching the current filters.
76
+ pub fn list_processes ( & self ) -> ProcCtlResult < Vec < ProcInfo > > {
77
+ let mut sys_handle = sys_handle ( ) . lock ( ) . unwrap ( ) ;
78
+ sys_handle. refresh_processes ( ) ;
79
+ let processes = sys_handle. processes ( ) ;
80
+ let infos: Vec < ProcInfo > = processes
81
+ . values ( )
82
+ . filter ( |p| {
83
+ if let Some ( pid) = self . process_id {
84
+ if p. pid ( ) . as_u32 ( ) != pid {
85
+ return false ;
86
+ }
87
+ }
88
+
89
+ if let Some ( name) = & self . name {
90
+ if p. name ( ) != name {
91
+ return false ;
92
+ }
93
+ }
94
+
95
+ true
96
+ } )
97
+ . map ( |p| p. into ( ) )
98
+ . collect ( ) ;
99
+
100
+ Ok ( infos)
101
+ }
102
+
66
103
/// Find the children of the selected process
67
104
pub fn children ( & self ) -> ProcCtlResult < Vec < ProcInfo > > {
68
105
let pid = resolve_pid ( self ) ?;
@@ -121,7 +158,7 @@ impl ProcQuery {
121
158
}
122
159
123
160
fn sys_handle ( ) -> & ' static Mutex < System > {
124
- static SYS_HANDLE : OnceCell < Mutex < System > > = OnceCell :: new ( ) ;
161
+ static SYS_HANDLE : OnceLock < Mutex < System > > = OnceLock :: new ( ) ;
125
162
SYS_HANDLE . get_or_init ( || {
126
163
let mut sys = System :: new_with_specifics (
127
164
RefreshKind :: new ( ) . with_processes ( ProcessRefreshKind :: new ( ) ) ,
@@ -137,11 +174,11 @@ impl From<&Process> for ProcInfo {
137
174
ProcInfo {
138
175
name : value. name ( ) . to_owned ( ) ,
139
176
cmd : value. cmd ( ) . to_owned ( ) ,
140
- exe : value. exe ( ) . to_owned ( ) ,
177
+ exe : value. exe ( ) . map ( |p| p . to_owned ( ) ) ,
141
178
pid : value. pid ( ) . as_u32 ( ) as Pid ,
142
179
parent : value. parent ( ) . map ( |p| p. as_u32 ( ) as Pid ) ,
143
180
env : value. environ ( ) . to_owned ( ) ,
144
- cwd : value. cwd ( ) . to_owned ( ) ,
181
+ cwd : value. cwd ( ) . map ( |p| p . to_owned ( ) ) ,
145
182
}
146
183
}
147
184
}
0 commit comments