3
3
use std:: { ffi:: OsString , os:: windows:: ffi:: OsStrExt } ;
4
4
5
5
use crate :: {
6
- windows:: { Cvt , Error } ,
6
+ windows:: { util :: OwnedHandle , Cvt , Error } ,
7
7
ResourceUsageData ,
8
8
} ;
9
9
10
10
use winapi:: um:: {
11
- handleapi:: CloseHandle ,
12
11
jobapi2:: {
13
12
AssignProcessToJobObject , CreateJobObjectW , QueryInformationJobObject ,
14
13
SetInformationJobObject , TerminateJobObject ,
@@ -25,19 +24,17 @@ use winapi::um::{
25
24
/// Responsible for resource isolation & adding & killing
26
25
#[ derive( Debug ) ]
27
26
pub ( crate ) struct Job {
28
- handle : HANDLE ,
27
+ handle : OwnedHandle ,
29
28
}
30
29
31
- unsafe impl Send for Job { }
32
- unsafe impl Sync for Job { }
33
-
34
30
impl Job {
35
31
pub ( crate ) fn new ( jail_id : & str ) -> Result < Self , Error > {
36
32
let name: OsString = format ! ( "minion-sandbox-job-{}" , jail_id) . into ( ) ;
37
33
let name: Vec < u16 > = name. encode_wide ( ) . collect ( ) ;
38
34
let handle = unsafe {
39
35
Cvt :: nonzero ( CreateJobObjectW ( std:: ptr:: null_mut ( ) , name. as_ptr ( ) ) as i32 ) ? as HANDLE
40
36
} ;
37
+ let handle = OwnedHandle :: new ( handle) ;
41
38
Ok ( Self { handle } )
42
39
}
43
40
pub ( crate ) fn enable_resource_limits (
@@ -60,7 +57,7 @@ impl Job {
60
57
| JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE ;
61
58
unsafe {
62
59
Cvt :: nonzero ( SetInformationJobObject (
63
- self . handle ,
60
+ self . handle . as_raw ( ) ,
64
61
JobObjectExtendedLimitInformation ,
65
62
( & mut info as * mut JOBOBJECT_EXTENDED_LIMIT_INFORMATION ) . cast ( ) ,
66
63
sizeof :: < JOBOBJECT_EXTENDED_LIMIT_INFORMATION > ( ) ,
@@ -69,16 +66,16 @@ impl Job {
69
66
Ok ( ( ) )
70
67
}
71
68
pub ( crate ) fn kill ( & self ) -> Result < ( ) , Error > {
72
- unsafe { Cvt :: nonzero ( TerminateJobObject ( self . handle , 0xDEADBEEF ) ) . map ( |_| ( ) ) }
69
+ unsafe { Cvt :: nonzero ( TerminateJobObject ( self . handle . as_raw ( ) , 0xDEADBEEF ) ) . map ( |_| ( ) ) }
73
70
}
74
71
pub ( crate ) fn add_process ( & self , process_handle : HANDLE ) -> Result < ( ) , Error > {
75
- unsafe { Cvt :: nonzero ( AssignProcessToJobObject ( self . handle , process_handle) ) . map ( |_| ( ) ) }
72
+ unsafe { Cvt :: nonzero ( AssignProcessToJobObject ( self . handle . as_raw ( ) , process_handle) ) . map ( |_| ( ) ) }
76
73
}
77
74
pub ( crate ) fn resource_usage ( & self ) -> Result < crate :: ResourceUsageData , Error > {
78
75
let cpu = unsafe {
79
76
let mut info: JOBOBJECT_BASIC_ACCOUNTING_INFORMATION = std:: mem:: zeroed ( ) ;
80
77
Cvt :: nonzero ( QueryInformationJobObject (
81
- self . handle ,
78
+ self . handle . as_raw ( ) ,
82
79
JobObjectBasicAccountingInformation ,
83
80
( & mut info as * mut JOBOBJECT_BASIC_ACCOUNTING_INFORMATION ) . cast ( ) ,
84
81
sizeof :: < JOBOBJECT_BASIC_ACCOUNTING_INFORMATION > ( ) ,
@@ -92,7 +89,7 @@ impl Job {
92
89
let memory = unsafe {
93
90
let mut info: JOBOBJECT_EXTENDED_LIMIT_INFORMATION = std:: mem:: zeroed ( ) ;
94
91
Cvt :: nonzero ( QueryInformationJobObject (
95
- self . handle ,
92
+ self . handle . as_raw ( ) ,
96
93
JobObjectExtendedLimitInformation ,
97
94
( & mut info as * mut JOBOBJECT_EXTENDED_LIMIT_INFORMATION ) . cast ( ) ,
98
95
sizeof :: < JOBOBJECT_EXTENDED_LIMIT_INFORMATION > ( ) ,
@@ -110,7 +107,7 @@ impl Job {
110
107
unsafe {
111
108
let mut info: JOBOBJECT_LIMIT_VIOLATION_INFORMATION = std:: mem:: zeroed ( ) ;
112
109
Cvt :: nonzero ( QueryInformationJobObject (
113
- self . handle ,
110
+ self . handle . as_raw ( ) ,
114
111
JobObjectLimitViolationInformation ,
115
112
( & mut info as * mut JOBOBJECT_LIMIT_VIOLATION_INFORMATION ) . cast ( ) ,
116
113
sizeof :: < JOBOBJECT_LIMIT_VIOLATION_INFORMATION > ( ) ,
@@ -127,14 +124,6 @@ impl Job {
127
124
}
128
125
}
129
126
130
- impl Drop for Job {
131
- fn drop ( & mut self ) {
132
- unsafe {
133
- CloseHandle ( self . handle ) ;
134
- }
135
- }
136
- }
137
-
138
127
fn sizeof < T > ( ) -> u32 {
139
128
let sz = std:: mem:: size_of :: < T > ( ) ;
140
129
assert ! ( sz <= ( u32 :: max_value( ) as usize ) ) ;
0 commit comments